2015-07-22 03:12:01 -07:00
|
|
|
use std::slice::Iter;
|
2015-07-22 03:42:21 -07:00
|
|
|
use std::iter::Peekable;
|
2015-07-22 03:12:01 -07:00
|
|
|
|
2015-07-22 04:01:56 -07:00
|
|
|
use tokenizer::{Token, Kw};
|
2015-07-22 03:12:01 -07:00
|
|
|
use tokenizer::Token::*;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum AST {
|
|
|
|
Name(String),
|
|
|
|
LangString(String),
|
|
|
|
Number(f64),
|
|
|
|
BinOp(Box<AST>, Box<AST>, Box<AST>),
|
|
|
|
Binding(String, Box<AST>)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum ParseResult {
|
|
|
|
Ok(AST),
|
|
|
|
Err(String)
|
|
|
|
}
|
|
|
|
|
2015-07-22 03:42:21 -07:00
|
|
|
type Tokens<'a> = Peekable<Iter<'a,Token>>;
|
2015-07-22 03:36:32 -07:00
|
|
|
|
2015-07-22 03:45:42 -07:00
|
|
|
/* expect calls .next() and thus advances the token list */
|
2015-07-22 03:28:26 -07:00
|
|
|
macro_rules! expect {
|
|
|
|
($tok:expr, $tokens:expr) => ( if !expect_token($tok, $tokens) {
|
|
|
|
return ParseResult::Err(format!("Expected {:?}", $tok));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-07-22 03:36:32 -07:00
|
|
|
fn expect_token(tok: Token, tokens: &mut Tokens) -> bool {
|
2015-07-22 03:12:01 -07:00
|
|
|
if let Some(n) = tokens.next() {
|
|
|
|
let next = (*n).clone();
|
|
|
|
return match (tok, next) {
|
|
|
|
(EOF, EOF) => true,
|
|
|
|
(Separator, Separator) => true,
|
|
|
|
(LParen, LParen) => true,
|
|
|
|
(RParen, RParen) => true,
|
|
|
|
(Comma, Comma) => true,
|
|
|
|
(NumLiteral(_), NumLiteral(_)) => true,
|
|
|
|
(StrLiteral(_), StrLiteral(_)) => true,
|
|
|
|
(Identifier(ref i1), Identifier(ref i2)) => i1 == i2,
|
2015-07-22 04:01:56 -07:00
|
|
|
(Keyword(k1), Keyword(k2)) => k1 == k2,
|
2015-07-22 03:12:01 -07:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-22 03:45:42 -07:00
|
|
|
false
|
2015-07-22 03:12:01 -07:00
|
|
|
}
|
|
|
|
|
2015-07-22 03:45:42 -07:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-22 03:36:32 -07:00
|
|
|
fn let_expression(input: &mut Tokens) -> ParseResult {
|
2015-07-22 04:01:56 -07:00
|
|
|
expect!(Keyword(Kw::Let), input);
|
2015-07-22 03:28:26 -07:00
|
|
|
if let Some(&Identifier(ref name)) = input.next() {
|
|
|
|
if let Some(&Identifier(ref s)) = input.next() {
|
|
|
|
if s == "=" {
|
|
|
|
let next = input.next();
|
|
|
|
if let Some(&Identifier(ref value)) = next {
|
|
|
|
let ast = AST::Binding(name.clone(), Box::new(AST::Name(value.clone())));
|
|
|
|
return ParseResult::Ok(ast);
|
|
|
|
}
|
2015-07-22 03:12:01 -07:00
|
|
|
|
2015-07-22 03:28:26 -07:00
|
|
|
if let Some(&StrLiteral(ref value)) = next {
|
|
|
|
let ast = AST::Binding(name.clone(), Box::new(AST::LangString(value.clone())));
|
|
|
|
return ParseResult::Ok(ast);
|
|
|
|
}
|
2015-07-22 03:12:01 -07:00
|
|
|
|
2015-07-22 03:28:26 -07:00
|
|
|
if let Some(&NumLiteral(n)) = next {
|
|
|
|
let ast = AST::Binding(name.clone(), Box::new(AST::Number(n)));
|
|
|
|
return ParseResult::Ok(ast);
|
2015-07-22 03:12:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ParseResult::Err("Bad parse".to_string());
|
|
|
|
}
|