Add expect_identifier function

For utility
This commit is contained in:
greg 2015-12-28 23:11:26 -08:00
parent 1af1589550
commit 06c3999430

View File

@ -56,6 +56,21 @@ impl Parser {
}
}
fn expect_identifier(&mut self, identifier_str: &str) -> ParseResult<()> {
use tokenizer::Token::*;
match self.next() {
Some(Identifier(ref s)) if s == identifier_str => Ok(()),
Some(next) => {
let err = format!("Expected identifier `{}` but got {:?}", identifier_str, next);
Err(ParseError { err: err })
}
None => {
let err = format!("Expected identifier `{}` but got end of input", identifier_str);
Err(ParseError { err: err })
}
}
}
fn parse(&mut self) -> ParseResult<AST> {
let r = self.expr();
try!(self.expect(Token::Separator));