Make ParseResult just a normal Result type
No reason for it to be different
This commit is contained in:
parent
1416c9d444
commit
72b26755a7
@ -6,7 +6,7 @@ use std::cell::RefCell;
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use tokenizer::tokenize;
|
use tokenizer::tokenize;
|
||||||
use parser::{parse, ParseResult};
|
use parser::{parse};
|
||||||
use evaluate::{evaluate, Environment};
|
use evaluate::{evaluate, Environment};
|
||||||
|
|
||||||
mod tokenizer;
|
mod tokenizer;
|
||||||
@ -85,7 +85,7 @@ fn repl() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
match parse(tokens) {
|
match parse(tokens) {
|
||||||
ParseResult::Ok(ast) => {
|
Ok(ast) => {
|
||||||
if options.show_ast {
|
if options.show_ast {
|
||||||
println!("AST: {:?}", ast);
|
println!("AST: {:?}", ast);
|
||||||
}
|
}
|
||||||
@ -94,7 +94,7 @@ fn repl() {
|
|||||||
println!("{}", eval);
|
println!("{}", eval);
|
||||||
env = new_env;
|
env = new_env;
|
||||||
},
|
},
|
||||||
ParseResult::Err(err) => println!("Error: {}", err)
|
Err(err) => println!("Error: {}", err)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
@ -18,11 +18,7 @@ pub enum AST {
|
|||||||
DoNothing
|
DoNothing
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
pub type ParseResult = Result<AST, String>;
|
||||||
pub enum ParseResult {
|
|
||||||
Ok(AST),
|
|
||||||
Err(String)
|
|
||||||
}
|
|
||||||
|
|
||||||
type Tokens<'a> = Peekable<Iter<'a,Token>>;
|
type Tokens<'a> = Peekable<Iter<'a,Token>>;
|
||||||
|
|
||||||
@ -31,15 +27,15 @@ macro_rules! expect {
|
|||||||
($tok:expr, $tokens:expr) => ( if !expect_token($tok, $tokens) {
|
($tok:expr, $tokens:expr) => ( if !expect_token($tok, $tokens) {
|
||||||
let tokens_left: Vec<&Token> = $tokens.collect();
|
let tokens_left: Vec<&Token> = $tokens.collect();
|
||||||
let err_string = format!("Expected {:?}\ntokens: {:?}", $tok, tokens_left);
|
let err_string = format!("Expected {:?}\ntokens: {:?}", $tok, tokens_left);
|
||||||
return ParseResult::Err(err_string);
|
return Err(err_string);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! expect_parse {
|
macro_rules! expect_parse {
|
||||||
($parse_fn:ident, $tokens:ident) => (
|
($parse_fn:ident, $tokens:ident) => (
|
||||||
match $parse_fn($tokens) {
|
match $parse_fn($tokens) {
|
||||||
err@ParseResult::Err(_) => return err,
|
err@Err(_) => return err,
|
||||||
ParseResult::Ok(ast) => ast
|
Ok(ast) => ast
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,15 +64,15 @@ pub fn parse(input: Vec<Token>) -> ParseResult {
|
|||||||
let mut tokens: Tokens = input.iter().peekable();
|
let mut tokens: Tokens = input.iter().peekable();
|
||||||
|
|
||||||
if let Some(&&EOF) = tokens.peek() {
|
if let Some(&&EOF) = tokens.peek() {
|
||||||
return ParseResult::Ok(AST::Statements(vec!()));
|
return Ok(AST::Statements(vec!()));
|
||||||
}
|
}
|
||||||
|
|
||||||
match statements(&mut tokens) {
|
match statements(&mut tokens) {
|
||||||
ok@ParseResult::Ok(_) => {
|
ok@Ok(_) => {
|
||||||
expect!(EOF, &mut tokens);
|
expect!(EOF, &mut tokens);
|
||||||
ok
|
ok
|
||||||
},
|
},
|
||||||
err@ParseResult::Err(_) => err
|
err@Err(_) => err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,17 +89,17 @@ fn statements(tokens: &mut Tokens) -> ParseResult {
|
|||||||
Some(&Separator) => {
|
Some(&Separator) => {
|
||||||
tokens.next();
|
tokens.next();
|
||||||
match statement(tokens) {
|
match statement(tokens) {
|
||||||
ParseResult::Ok(ast_next) => {
|
Ok(ast_next) => {
|
||||||
statements.push(ast_next);
|
statements.push(ast_next);
|
||||||
},
|
},
|
||||||
err@ParseResult::Err(_) => return err
|
err@Err(_) => return err
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
_ => break
|
_ => break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ParseResult::Ok(AST::Statements(statements));
|
return Ok(AST::Statements(statements));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn statement(tokens: &mut Tokens) -> ParseResult {
|
fn statement(tokens: &mut Tokens) -> ParseResult {
|
||||||
@ -118,8 +114,8 @@ fn let_expression(tokens: &mut Tokens) -> ParseResult {
|
|||||||
if let Some(&Identifier(ref name)) = tokens.next() {
|
if let Some(&Identifier(ref name)) = tokens.next() {
|
||||||
if let Some(&Identifier(ref s)) = tokens.next() {
|
if let Some(&Identifier(ref s)) = tokens.next() {
|
||||||
if s == "=" {
|
if s == "=" {
|
||||||
if let ParseResult::Ok(expr) = expression(tokens) {
|
if let Ok(expr) = expression(tokens) {
|
||||||
return ParseResult::Ok(
|
return Ok(
|
||||||
AST::Binding(name.clone(),
|
AST::Binding(name.clone(),
|
||||||
Box::new(expr)));
|
Box::new(expr)));
|
||||||
}
|
}
|
||||||
@ -127,7 +123,7 @@ fn let_expression(tokens: &mut Tokens) -> ParseResult {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ParseResult::Err("Bad parse in let_expression()".to_string());
|
return Err("Bad parse in let_expression()".to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expression(tokens: &mut Tokens) -> ParseResult {
|
fn expression(tokens: &mut Tokens) -> ParseResult {
|
||||||
@ -155,8 +151,8 @@ fn if_expression(tokens: &mut Tokens) -> ParseResult {
|
|||||||
Some(&Keyword(Kw::Else)) => {
|
Some(&Keyword(Kw::Else)) => {
|
||||||
tokens.next();
|
tokens.next();
|
||||||
match expression(tokens) {
|
match expression(tokens) {
|
||||||
err@ParseResult::Err(_) => return err,
|
err@Err(_) => return err,
|
||||||
ParseResult::Ok(ast) => Some(ast)
|
Ok(ast) => Some(ast)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
_ => None
|
_ => None
|
||||||
@ -164,7 +160,7 @@ fn if_expression(tokens: &mut Tokens) -> ParseResult {
|
|||||||
|
|
||||||
expect!(Keyword(Kw::End), tokens);
|
expect!(Keyword(Kw::End), tokens);
|
||||||
|
|
||||||
ParseResult::Ok( AST::IfStatement(
|
Ok( AST::IfStatement(
|
||||||
Box::new(if_clause),
|
Box::new(if_clause),
|
||||||
Box::new(then_clause),
|
Box::new(then_clause),
|
||||||
else_clause.map(|ast| Box::new(ast))
|
else_clause.map(|ast| Box::new(ast))
|
||||||
@ -181,7 +177,7 @@ fn while_expression(tokens: &mut Tokens) -> ParseResult {
|
|||||||
|
|
||||||
expect!(Keyword(Kw::End), tokens);
|
expect!(Keyword(Kw::End), tokens);
|
||||||
|
|
||||||
ParseResult::Ok(AST::WhileStatement(
|
Ok(AST::WhileStatement(
|
||||||
Box::new(while_expression),
|
Box::new(while_expression),
|
||||||
Box::new(statements),
|
Box::new(statements),
|
||||||
))
|
))
|
||||||
@ -199,12 +195,12 @@ fn binop_expression(precedence: i32, tokens: &mut Tokens) -> ParseResult {
|
|||||||
match next_precedence {
|
match next_precedence {
|
||||||
Some(next) if precedence < next => {
|
Some(next) if precedence < next => {
|
||||||
left = match binop_rhs(next, left, tokens) {
|
left = match binop_rhs(next, left, tokens) {
|
||||||
err@ParseResult::Err(_) => return err,
|
err@Err(_) => return err,
|
||||||
ParseResult::Ok(ast) => ast
|
Ok(ast) => ast
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
_ => return ParseResult::Ok(left),
|
_ => return Ok(left),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -212,16 +208,16 @@ fn binop_expression(precedence: i32, tokens: &mut Tokens) -> ParseResult {
|
|||||||
fn binop_rhs(precedence: i32, lhs: AST, tokens: &mut Tokens) -> ParseResult {
|
fn binop_rhs(precedence: i32, lhs: AST, tokens: &mut Tokens) -> ParseResult {
|
||||||
|
|
||||||
let op: AST = match simple_expression(tokens) {
|
let op: AST = match simple_expression(tokens) {
|
||||||
err@ParseResult::Err(_) => return err,
|
err@Err(_) => return err,
|
||||||
ParseResult::Ok(ast) => ast
|
Ok(ast) => ast
|
||||||
};
|
};
|
||||||
|
|
||||||
let rhs: AST = match binop_expression(precedence, tokens) {
|
let rhs: AST = match binop_expression(precedence, tokens) {
|
||||||
err@ParseResult::Err(_) => return err,
|
err@Err(_) => return err,
|
||||||
ParseResult::Ok(ast) => ast
|
Ok(ast) => ast
|
||||||
};
|
};
|
||||||
|
|
||||||
ParseResult::Ok(AST::BinOp(
|
Ok(AST::BinOp(
|
||||||
Box::new(op),
|
Box::new(op),
|
||||||
Box::new(lhs),
|
Box::new(lhs),
|
||||||
Box::new(rhs)
|
Box::new(rhs)
|
||||||
@ -249,23 +245,23 @@ fn simple_expression(tokens: &mut Tokens) -> ParseResult {
|
|||||||
|
|
||||||
match next {
|
match next {
|
||||||
Some(&Keyword(Kw::Null)) =>
|
Some(&Keyword(Kw::Null)) =>
|
||||||
ParseResult::Ok(AST::Name("null".to_string())),
|
Ok(AST::Name("null".to_string())),
|
||||||
|
|
||||||
Some(&Identifier(ref value)) =>
|
Some(&Identifier(ref value)) =>
|
||||||
ParseResult::Ok(AST::Name(value.clone())),
|
Ok(AST::Name(value.clone())),
|
||||||
|
|
||||||
Some(&StrLiteral(ref value)) =>
|
Some(&StrLiteral(ref value)) =>
|
||||||
ParseResult::Ok(AST::LangString(value.clone())),
|
Ok(AST::LangString(value.clone())),
|
||||||
|
|
||||||
Some(&NumLiteral(n)) =>
|
Some(&NumLiteral(n)) =>
|
||||||
ParseResult::Ok(AST::Number(n)),
|
Ok(AST::Number(n)),
|
||||||
|
|
||||||
Some(&LParen) => {
|
Some(&LParen) => {
|
||||||
let within_paren = expression(tokens);
|
let within_paren = expression(tokens);
|
||||||
expect!(RParen, tokens);
|
expect!(RParen, tokens);
|
||||||
within_paren
|
within_paren
|
||||||
},
|
},
|
||||||
_ => ParseResult::Err("Bad parse in simple_expression()".to_string())
|
_ => Err("Bad parse in simple_expression()".to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -279,15 +275,15 @@ mod tests {
|
|||||||
::init_binop_table();
|
::init_binop_table();
|
||||||
|
|
||||||
match parse(tokenize("a + b * c")) {
|
match parse(tokenize("a + b * c")) {
|
||||||
ParseResult::Ok(ast) =>
|
Ok(ast) =>
|
||||||
assert_eq!(format!("{:?}", ast), "Statements([BinOp(Name(\"+\"), Name(\"a\"), BinOp(Name(\"*\"), Name(\"b\"), Name(\"c\")))])"),
|
assert_eq!(format!("{:?}", ast), "Statements([BinOp(Name(\"+\"), Name(\"a\"), BinOp(Name(\"*\"), Name(\"b\"), Name(\"c\")))])"),
|
||||||
ParseResult::Err(err) => panic!("err: {:?}", err)
|
Err(err) => panic!("err: {:?}", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
match parse(tokenize("(a + b) * c")) {
|
match parse(tokenize("(a + b) * c")) {
|
||||||
ParseResult::Ok(ast) =>
|
Ok(ast) =>
|
||||||
assert_eq!(format!("{:?}", ast), "Statements([BinOp(Name(\"*\"), BinOp(Name(\"+\"), Name(\"a\"), Name(\"b\")), Name(\"c\"))])"),
|
assert_eq!(format!("{:?}", ast), "Statements([BinOp(Name(\"*\"), BinOp(Name(\"+\"), Name(\"a\"), Name(\"b\")), Name(\"c\"))])"),
|
||||||
ParseResult::Err(err) => panic!("err: {:?}", err)
|
Err(err) => panic!("err: {:?}", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user