Clean up trailing space and fix rhai_runner print out
This commit is contained in:
@@ -86,11 +86,11 @@ pub struct FnDef {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Stmt { If(Box<Expr>, Box<Stmt>), IfElse(Box<Expr>, Box<Stmt>, Box<Stmt>), While(Box<Expr>, Box<Stmt>),
|
||||
pub enum Stmt { If(Box<Expr>, Box<Stmt>), IfElse(Box<Expr>, Box<Stmt>, Box<Stmt>), While(Box<Expr>, Box<Stmt>),
|
||||
Var(String, Option<Box<Expr>>), Block(Box<Vec<Stmt>>), Expr(Box<Expr>), Break, Return, ReturnWithVal(Box<Expr>) }
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Expr { IntConst(i32), Identifier(String), StringConst(String), FnCall(String, Box<Vec<Expr>>),
|
||||
pub enum Expr { IntConst(i32), Identifier(String), StringConst(String), FnCall(String, Box<Vec<Expr>>),
|
||||
Assignment(Box<Expr>, Box<Expr>), Dot(Box<Expr>, Box<Expr>), Index(String, Box<Expr>), Array(Box<Vec<Expr>>), True, False }
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -133,7 +133,7 @@ impl<'a> Iterator for TokenIterator<'a> {
|
||||
|
||||
while let Some(&nxt) = self.char_stream.peek() {
|
||||
match nxt {
|
||||
'0'...'9' | 'A'...'Z' | 'a'...'z' | '_' => {
|
||||
'0'...'9' | 'A'...'Z' | 'a'...'z' | '_' => {
|
||||
result.push(nxt); self.char_stream.next(); },
|
||||
_ => break
|
||||
}
|
||||
@@ -281,37 +281,37 @@ impl<'a> Iterator for TokenIterator<'a> {
|
||||
':' => { return Some(Token::Colon); },
|
||||
',' => { return Some(Token::Comma); },
|
||||
'.' => { return Some(Token::Period); },
|
||||
'=' => {
|
||||
'=' => {
|
||||
match self.char_stream.peek() {
|
||||
Some(&'=') => {self.char_stream.next(); return Some(Token::EqualTo); },
|
||||
_ => { return Some(Token::Equals); }
|
||||
}
|
||||
},
|
||||
'<' => {
|
||||
'<' => {
|
||||
match self.char_stream.peek() {
|
||||
Some(&'=') => {self.char_stream.next(); return Some(Token::LessThanEqual); },
|
||||
_ => { return Some(Token::LessThan); }
|
||||
}
|
||||
}
|
||||
'>' => {
|
||||
'>' => {
|
||||
match self.char_stream.peek() {
|
||||
Some(&'=') => {self.char_stream.next(); return Some(Token::GreaterThanEqual); },
|
||||
_ => { return Some(Token::GreaterThan); }
|
||||
}
|
||||
},
|
||||
'!' => {
|
||||
'!' => {
|
||||
match self.char_stream.peek() {
|
||||
Some(&'=') => {self.char_stream.next(); return Some(Token::NotEqualTo); },
|
||||
_ => { return Some(Token::Bang); }
|
||||
}
|
||||
},
|
||||
'|' => {
|
||||
'|' => {
|
||||
match self.char_stream.peek() {
|
||||
Some(&'|') => {self.char_stream.next(); return Some(Token::Or); },
|
||||
_ => { return Some(Token::Pipe); }
|
||||
}
|
||||
},
|
||||
'&' => {
|
||||
'&' => {
|
||||
match self.char_stream.peek() {
|
||||
Some(&'&') => {self.char_stream.next(); return Some(Token::And); },
|
||||
_ => { return Some(Token::Ampersand); }
|
||||
@@ -359,12 +359,12 @@ fn parse_paren_expr<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_call_expr<'a>(id: String, input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr, ParseError> {
|
||||
fn parse_call_expr<'a>(id: String, input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr, ParseError> {
|
||||
let mut args = Vec::new();
|
||||
|
||||
match input.peek() {
|
||||
Some(&Token::RParen) => {
|
||||
input.next();
|
||||
input.next();
|
||||
return Ok(Expr::FnCall(id, Box::new(args)))
|
||||
},
|
||||
_ => ()
|
||||
@@ -391,7 +391,7 @@ fn parse_call_expr<'a>(id: String, input: &mut Peekable<TokenIterator<'a>>) -> R
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_index_expr<'a>(id: String, input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr, ParseError> {
|
||||
fn parse_index_expr<'a>(id: String, input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr, ParseError> {
|
||||
if let Ok(idx) = parse_expr(input) {
|
||||
match input.peek() {
|
||||
Some(&Token::RSquare) => {
|
||||
@@ -406,7 +406,7 @@ fn parse_index_expr<'a>(id: String, input: &mut Peekable<TokenIterator<'a>>) ->
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_ident_expr<'a>(id: String, input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr, ParseError> {
|
||||
fn parse_ident_expr<'a>(id: String, input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr, ParseError> {
|
||||
match input.peek() {
|
||||
Some(&Token::LParen) => {input.next(); parse_call_expr(id, input)},
|
||||
Some(&Token::LSquare) => {input.next(); parse_index_expr(id, input)},
|
||||
@@ -426,7 +426,7 @@ fn parse_array_expr<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr,
|
||||
while let Some(_) = input.peek() {
|
||||
arr.push(try!(parse_expr(input)));
|
||||
match input.peek() {
|
||||
Some(& Token::Comma) => {input.next();},
|
||||
Some(& Token::Comma) => {input.next();},
|
||||
_ => ()
|
||||
}
|
||||
|
||||
@@ -434,7 +434,7 @@ fn parse_array_expr<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr,
|
||||
Some(& Token::RSquare) => break,
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match input.peek() {
|
||||
@@ -469,7 +469,7 @@ fn parse_binop<'a>(input: &mut Peekable<TokenIterator<'a>>, prec: i32, lhs: Expr
|
||||
loop {
|
||||
let mut curr_prec = -1;
|
||||
|
||||
if let Some(curr_op) = input.peek() {
|
||||
if let Some(curr_op) = input.peek() {
|
||||
curr_prec = get_precedence(curr_op);
|
||||
}
|
||||
|
||||
@@ -481,7 +481,7 @@ fn parse_binop<'a>(input: &mut Peekable<TokenIterator<'a>>, prec: i32, lhs: Expr
|
||||
let mut rhs = try!(parse_primary(input));
|
||||
|
||||
let mut next_prec = -1;
|
||||
|
||||
|
||||
if let Some(next_op) = input.peek() {
|
||||
next_prec = get_precedence(next_op);
|
||||
}
|
||||
@@ -491,7 +491,7 @@ fn parse_binop<'a>(input: &mut Peekable<TokenIterator<'a>>, prec: i32, lhs: Expr
|
||||
}
|
||||
else if curr_prec >= 100 {
|
||||
//Always bind right to left for precedence over 100
|
||||
rhs = try!(parse_binop(input, curr_prec, rhs));
|
||||
rhs = try!(parse_binop(input, curr_prec, rhs));
|
||||
}
|
||||
|
||||
lhs_curr = match op_token {
|
||||
@@ -585,7 +585,7 @@ fn parse_block<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, Pars
|
||||
while let Some(_) = input.peek() {
|
||||
stmts.push(try!(parse_stmt(input)));
|
||||
match input.peek() {
|
||||
Some(& Token::Semicolon) => {input.next();},
|
||||
Some(& Token::Semicolon) => {input.next();},
|
||||
_ => ()
|
||||
}
|
||||
|
||||
@@ -593,7 +593,7 @@ fn parse_block<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, Pars
|
||||
Some(& Token::RCurly) => break,
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match input.peek() {
|
||||
@@ -603,7 +603,7 @@ fn parse_block<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, Pars
|
||||
}
|
||||
|
||||
fn parse_expr_stmt<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, ParseError> {
|
||||
let expr = try!(parse_expr(input));
|
||||
let expr = try!(parse_expr(input));
|
||||
Ok(Stmt::Expr(Box::new(expr)))
|
||||
}
|
||||
|
||||
@@ -613,7 +613,7 @@ fn parse_stmt<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, Parse
|
||||
Some(& Token::While) => parse_while(input),
|
||||
Some(& Token::Break) => {input.next(); Ok(Stmt::Break)},
|
||||
Some(& Token::Return) => {
|
||||
input.next();
|
||||
input.next();
|
||||
match input.peek() {
|
||||
Some(& Token::Semicolon) => Ok(Stmt::Return),
|
||||
_ => {let ret = try!(parse_expr(input)); Ok(Stmt::ReturnWithVal(Box::new(ret))) }
|
||||
@@ -653,7 +653,7 @@ fn parse_fn<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<FnDef, ParseE
|
||||
Some(Token::Identifier(ref s)) => { params.push(s.clone()); },
|
||||
_ => return Err(ParseError::MalformedCallExpr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let body = try!(parse_block(input));
|
||||
@@ -672,7 +672,7 @@ fn parse_top_level<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<(Vec<S
|
||||
}
|
||||
|
||||
match input.peek() {
|
||||
Some(& Token::Semicolon) => {input.next();},
|
||||
Some(& Token::Semicolon) => {input.next();},
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user