Put expect into early return macro
This commit is contained in:
parent
1059a88ee6
commit
67eafba97a
@ -17,20 +17,26 @@ pub enum ParseResult {
|
|||||||
Err(String)
|
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 {
|
pub fn parse(input: Vec<Token>) -> ParseResult {
|
||||||
|
|
||||||
let mut tokens = input.iter();
|
let mut tokens = input.iter();
|
||||||
|
|
||||||
if let ParseResult::Ok(ast) = let_expression(&mut tokens) {
|
if let ParseResult::Ok(ast) = let_expression(&mut tokens) {
|
||||||
if expect(EOF, &mut tokens) {
|
expect!(EOF, &mut tokens);
|
||||||
return ParseResult::Ok(ast);
|
return ParseResult::Ok(ast);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return ParseResult::Err("Bad parse".to_string());
|
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() {
|
if let Some(n) = tokens.next() {
|
||||||
let next = (*n).clone();
|
let next = (*n).clone();
|
||||||
return match (tok, next) {
|
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 {
|
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 name)) = input.next() {
|
||||||
if let Some(&Identifier(ref s)) = input.next() {
|
if let Some(&Identifier(ref s)) = input.next() {
|
||||||
if s == "=" {
|
if s == "=" {
|
||||||
@ -72,7 +78,6 @@ fn let_expression<'a>(input: &mut Iter<Token>) -> ParseResult {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return ParseResult::Err("Bad parse".to_string());
|
return ParseResult::Err("Bad parse".to_string());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user