From c363db95be7e2520e5c69c49733544fe98dff838 Mon Sep 17 00:00:00 2001 From: stuebinm Date: Mon, 6 Jan 2025 02:48:29 +0100 Subject: new parser, support exporttree, generally nicer code --- src/parser.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/parser.rs (limited to 'src/parser.rs') diff --git a/src/parser.rs b/src/parser.rs new file mode 100644 index 0000000..13cd22b --- /dev/null +++ b/src/parser.rs @@ -0,0 +1,38 @@ + +pub struct Parser<'a> { + buf: &'a str, + offset: usize +} + +impl<'a> Parser<'a> { + pub fn new(buf: &'a str) -> Self { + Parser{buf, offset: 0} + } + + pub fn next_word(&mut self) -> &'a str { + match self.buf[self.offset..].split_once(' ') { + Some((a,_)) => { + self.offset += a.len() + 1; + a + }, + None => { + let res = &self.buf[self.offset..]; + self.offset += self.buf.len() - self.offset; + res + } + } + } + + pub fn remainder(&mut self) -> String { + let res = self.buf[self.offset..].to_owned(); + self.offset = self.buf.len(); + res + } + + pub fn assert_end(&self) { + if self.buf.len() != self.offset { + println!("ERROR: failed parsing"); + panic!() + } + } +} -- cgit v1.2.3