diff options
Diffstat (limited to 'src/parser.rs')
-rw-r--r-- | src/parser.rs | 38 |
1 files changed, 38 insertions, 0 deletions
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!() + } + } +} |