Make convenience macro for parse errors
This commit is contained in:
parent
8c0ac19fa8
commit
51745fd800
@ -40,6 +40,12 @@ struct Parser {
|
|||||||
tokens: Peekable<IntoIter<Token>>
|
tokens: Peekable<IntoIter<Token>>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! parse_error {
|
||||||
|
($($text:tt)*) => {
|
||||||
|
Err(ParseError { err: format!($($text)*) })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Parser {
|
impl Parser {
|
||||||
fn next(&mut self) -> Option<Token> {
|
fn next(&mut self) -> Option<Token> {
|
||||||
self.tokens.next()
|
self.tokens.next()
|
||||||
@ -57,12 +63,10 @@ impl Parser {
|
|||||||
match self.next() {
|
match self.next() {
|
||||||
Some(ref next) if *next == expected => Ok(()),
|
Some(ref next) if *next == expected => Ok(()),
|
||||||
Some(next) => {
|
Some(next) => {
|
||||||
let err = format!("Expected {:?} but got {:?}", expected, next);
|
return parse_error!("Expected {:?} but got {:?}", expected, next);
|
||||||
return Err(ParseError { err: err });
|
|
||||||
},
|
},
|
||||||
None => {
|
None => {
|
||||||
let err = format!("Expected {:?} but got end of input", expected);
|
return parse_error!("Expected {:?} but got end of input", expected);
|
||||||
return Err(ParseError { err: err });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -72,12 +76,10 @@ impl Parser {
|
|||||||
match self.next() {
|
match self.next() {
|
||||||
Some(Identifier(ref s)) if s == identifier_str => Ok(()),
|
Some(Identifier(ref s)) if s == identifier_str => Ok(()),
|
||||||
Some(next) => {
|
Some(next) => {
|
||||||
let err = format!("Expected identifier `{}` but got {:?}", identifier_str, next);
|
return parse_error!("Expected identifier `{}` but got {:?}", identifier_str, next);
|
||||||
Err(ParseError { err: err })
|
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
let err = format!("Expected identifier `{}` but got end of input", identifier_str);
|
return parse_error!("Expected identifier `{}` but got end of input", identifier_str);
|
||||||
Err(ParseError { err: err })
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -87,12 +89,10 @@ impl Parser {
|
|||||||
match self.next() {
|
match self.next() {
|
||||||
Some(NumLiteral(f)) => Ok(f),
|
Some(NumLiteral(f)) => Ok(f),
|
||||||
Some(t) => {
|
Some(t) => {
|
||||||
let err = format!("Expected NumLiteral, but got {:?}", t);
|
return parse_error!("Expected NumLiteral, but got {:?}", t);
|
||||||
Err(ParseError { err: err })
|
|
||||||
},
|
},
|
||||||
None => {
|
None => {
|
||||||
let err = format!("Expected NumLiteral but got end of input");
|
return parse_error!("Expected NumLiteral but got end of input");
|
||||||
Err(ParseError { err: err })
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -157,7 +157,7 @@ impl Parser {
|
|||||||
self.next();
|
self.next();
|
||||||
Ok(AST::Number(n))
|
Ok(AST::Number(n))
|
||||||
},
|
},
|
||||||
_ => Err(ParseError { err: format!("Expected LParen or NumLiteral") })
|
_ => parse_error!("Expected LParen or NumLiteral")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user