Move definition around
This commit is contained in:
parent
5213dd327f
commit
25f5188d8c
@ -20,24 +20,13 @@ pub enum ParseResult {
|
|||||||
|
|
||||||
type Tokens<'a> = Peekable<Iter<'a,Token>>;
|
type Tokens<'a> = Peekable<Iter<'a,Token>>;
|
||||||
|
|
||||||
|
/* expect calls .next() and thus advances the token list */
|
||||||
macro_rules! expect {
|
macro_rules! expect {
|
||||||
($tok:expr, $tokens:expr) => ( if !expect_token($tok, $tokens) {
|
($tok:expr, $tokens:expr) => ( if !expect_token($tok, $tokens) {
|
||||||
return ParseResult::Err(format!("Expected {:?}", $tok));
|
return ParseResult::Err(format!("Expected {:?}", $tok));
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(input: Vec<Token>) -> ParseResult {
|
|
||||||
|
|
||||||
let mut tokens: Tokens = input.iter().peekable();
|
|
||||||
|
|
||||||
if let ParseResult::Ok(ast) = let_expression(&mut tokens) {
|
|
||||||
expect!(EOF, &mut tokens);
|
|
||||||
return ParseResult::Ok(ast);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ParseResult::Err("Bad parse".to_string());
|
|
||||||
}
|
|
||||||
|
|
||||||
fn expect_token(tok: Token, tokens: &mut Tokens) -> bool {
|
fn expect_token(tok: Token, tokens: &mut Tokens) -> bool {
|
||||||
if let Some(n) = tokens.next() {
|
if let Some(n) = tokens.next() {
|
||||||
let next = (*n).clone();
|
let next = (*n).clone();
|
||||||
@ -54,9 +43,22 @@ fn expect_token(tok: Token, tokens: &mut Tokens) -> bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn parse(input: Vec<Token>) -> ParseResult {
|
||||||
|
|
||||||
|
let mut tokens: Tokens = input.iter().peekable();
|
||||||
|
|
||||||
|
if let ParseResult::Ok(ast) = let_expression(&mut tokens) {
|
||||||
|
expect!(EOF, &mut tokens);
|
||||||
|
return ParseResult::Ok(ast);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ParseResult::Err("Bad parse".to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
fn let_expression(input: &mut Tokens) -> ParseResult {
|
fn let_expression(input: &mut Tokens) -> ParseResult {
|
||||||
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() {
|
||||||
|
Loading…
Reference in New Issue
Block a user