Put expect into early return macro

This commit is contained in:
greg 2015-07-22 03:28:26 -07:00
parent 1059a88ee6
commit 67eafba97a
1 changed files with 26 additions and 21 deletions

View File

@ -17,20 +17,26 @@ pub enum ParseResult {
Err(String)
}
macro_rules! expect {
($tok:expr, $tokens:expr) => ( if !expect_token($tok, $tokens) {
println!("yo hitting");
return ParseResult::Err(format!("Expected {:?}", $tok));
})
}
pub fn parse(input: Vec<Token>) -> ParseResult {
let mut tokens = input.iter();
if let ParseResult::Ok(ast) = let_expression(&mut tokens) {
if expect(EOF, &mut tokens) {
expect!(EOF, &mut tokens);
return ParseResult::Ok(ast);
}
}
return ParseResult::Err("Bad parse".to_string());
}
fn expect(tok: Token, tokens: &mut Iter<Token>) -> bool {
fn expect_token(tok: Token, tokens: &mut Iter<Token>) -> bool {
if let Some(n) = tokens.next() {
let next = (*n).clone();
return match (tok, next) {
@ -50,7 +56,7 @@ fn expect(tok: Token, tokens: &mut Iter<Token>) -> bool {
}
fn let_expression<'a>(input: &mut Iter<Token>) -> ParseResult {
if expect(Identifier("let".to_string()), input) {
expect!(Identifier("let".to_string()), input);
if let Some(&Identifier(ref name)) = input.next() {
if let Some(&Identifier(ref s)) = input.next() {
if s == "=" {
@ -72,7 +78,6 @@ fn let_expression<'a>(input: &mut Iter<Token>) -> ParseResult {
}
}
}
}
return ParseResult::Err("Bad parse".to_string());
}