try!() -> ?

This commit is contained in:
greg 2017-02-17 21:45:21 -08:00
parent d8df98ba01
commit dd93adf5b7
2 changed files with 30 additions and 30 deletions

View File

@ -290,8 +290,8 @@ impl Parser {
fn statement(&mut self) -> ParseResult<Statement> { fn statement(&mut self) -> ParseResult<Statement> {
let node: Statement = match self.peek() { let node: Statement = match self.peek() {
Some(Keyword(Kw::Fn)) => try!(self.declaration()), Some(Keyword(Kw::Fn)) => self.declaration()?,
Some(_) => Statement::ExprNode(try!(self.expression())), Some(_) => Statement::ExprNode(self.expression()?),
None => panic!("Unexpected end of tokens"), None => panic!("Unexpected end of tokens"),
}; };
Ok(node) Ok(node)
@ -299,9 +299,9 @@ impl Parser {
fn declaration(&mut self) -> ParseResult<Statement> { fn declaration(&mut self) -> ParseResult<Statement> {
expect!(self, Keyword(Kw::Fn)); expect!(self, Keyword(Kw::Fn));
let prototype = try!(self.prototype()); let prototype = self.prototype()?;
expect!(self, LCurlyBrace); expect!(self, LCurlyBrace);
let body = try!(self.body()); let body = self.body()?;
expect!(self, RCurlyBrace); expect!(self, RCurlyBrace);
Ok(Statement::FuncDefNode(Function { Ok(Statement::FuncDefNode(Function {
prototype: prototype, prototype: prototype,
@ -312,7 +312,7 @@ impl Parser {
fn prototype(&mut self) -> ParseResult<Prototype> { fn prototype(&mut self) -> ParseResult<Prototype> {
let name = expect_identifier!(self); let name = expect_identifier!(self);
expect!(self, LParen); expect!(self, LParen);
let parameters = try!(self.identlist()); let parameters = self.identlist()?;
expect!(self, RParen); expect!(self, RParen);
Ok(Prototype { Ok(Prototype {
name: name, name: name,
@ -339,7 +339,7 @@ impl Parser {
if let Some(RParen) = self.peek() { if let Some(RParen) = self.peek() {
break; break;
} }
let exp = try!(self.expression()); let exp = self.expression()?;
exprs.push(exp); exprs.push(exp);
match self.peek() { match self.peek() {
Some(Comma) => {self.next();}, Some(Comma) => {self.next();},
@ -359,7 +359,7 @@ impl Parser {
} }
fn expression(&mut self) -> ParseResult<Expression> { fn expression(&mut self) -> ParseResult<Expression> {
let lhs: Expression = try!(self.postop_expression()); let lhs: Expression = self.postop_expression()?;
self.precedence_expr(lhs, 0) self.precedence_expr(lhs, 0)
} }
@ -373,11 +373,11 @@ impl Parser {
break; break;
} }
self.next(); self.next();
let mut rhs = try!(self.postop_expression()); let mut rhs = self.postop_expression()?;
while let Some(Operator(ref op)) = self.peek() { while let Some(Operator(ref op)) = self.peek() {
if self.get_precedence(op) > precedence { if self.get_precedence(op) > precedence {
let new_prec = self.get_precedence(op); let new_prec = self.get_precedence(op);
rhs = try!(self.precedence_expr(rhs, new_prec)); rhs = self.precedence_expr(rhs, new_prec)?;
} else { } else {
break; break;
@ -391,10 +391,10 @@ impl Parser {
fn postop_expression(&mut self) -> ParseResult<Expression> { fn postop_expression(&mut self) -> ParseResult<Expression> {
use self::Expression::*; use self::Expression::*;
let expr = try!(self.primary_expression()); let expr = self.primary_expression()?;
let ret = match self.peek() { let ret = match self.peek() {
Some(LParen) => { Some(LParen) => {
let args = try!(self.call_expression()); let args = self.call_expression()?;
match expr { match expr {
Lambda(f) => Call(Callable::Lambda(f), args), Lambda(f) => Call(Callable::Lambda(f), args),
e => { e => {
@ -422,18 +422,18 @@ impl Parser {
self.next(); self.next();
Expression::Null Expression::Null
} }
Some(NumLiteral(_)) => try!(self.number_expression()), Some(NumLiteral(_)) => self.number_expression()?,
Some(Operator(OpTok(ref a))) if **a == "+" || **a == "-" => try!(self.number_expression()), Some(Operator(OpTok(ref a))) if **a == "+" || **a == "-" => self.number_expression()?,
Some(StrLiteral(s)) => { Some(StrLiteral(s)) => {
self.next(); self.next();
Expression::StringLiteral(s) Expression::StringLiteral(s)
} }
Some(Keyword(Kw::If)) => try!(self.conditional_expr()), Some(Keyword(Kw::If)) => self.conditional_expr()?,
Some(Keyword(Kw::While)) => try!(self.while_expr()), Some(Keyword(Kw::While)) => self.while_expr()?,
Some(Identifier(_)) => try!(self.identifier_expr()), Some(Identifier(_)) => self.identifier_expr()?,
Some(Token::LParen) => try!(self.paren_expr()), Some(Token::LParen) => self.paren_expr()?,
Some(Keyword(Kw::Fn)) => try!(self.lambda_expr()), Some(Keyword(Kw::Fn)) => self.lambda_expr()?,
Some(Token::LSquareBracket) => try!(self.list_expr()), Some(Token::LSquareBracket) => self.list_expr()?,
Some(e) => { Some(e) => {
return ParseError::result_from_str(&format!("Expected primary expression, got \ return ParseError::result_from_str(&format!("Expected primary expression, got \
{:?}", {:?}",
@ -479,11 +479,11 @@ impl Parser {
expect!(self, Keyword(Kw::Fn)); expect!(self, Keyword(Kw::Fn));
skip_whitespace!(self); skip_whitespace!(self);
expect!(self, LParen); expect!(self, LParen);
let parameters = try!(self.identlist()); let parameters = self.identlist()?;
expect!(self, RParen); expect!(self, RParen);
skip_whitespace!(self); skip_whitespace!(self);
expect!(self, LCurlyBrace); expect!(self, LCurlyBrace);
let body = try!(self.body()); let body = self.body()?;
expect!(self, RCurlyBrace); expect!(self, RCurlyBrace);
let prototype = Prototype { let prototype = Prototype {
@ -502,7 +502,7 @@ impl Parser {
fn while_expr(&mut self) -> ParseResult<Expression> { fn while_expr(&mut self) -> ParseResult<Expression> {
use self::Expression::*; use self::Expression::*;
expect!(self, Keyword(Kw::While)); expect!(self, Keyword(Kw::While));
let test = try!(self.expression()); let test = self.expression()?;
expect!(self, LCurlyBrace); expect!(self, LCurlyBrace);
let body = delimiter_block!( let body = delimiter_block!(
self, self,
@ -516,7 +516,7 @@ impl Parser {
fn conditional_expr(&mut self) -> ParseResult<Expression> { fn conditional_expr(&mut self) -> ParseResult<Expression> {
use self::Expression::*; use self::Expression::*;
expect!(self, Keyword(Kw::If)); expect!(self, Keyword(Kw::If));
let test = try!(self.expression()); let test = self.expression()?;
skip_whitespace!(self); skip_whitespace!(self);
expect!(self, LCurlyBrace); expect!(self, LCurlyBrace);
skip_whitespace!(self); skip_whitespace!(self);
@ -550,7 +550,7 @@ impl Parser {
let name = expect_identifier!(self); let name = expect_identifier!(self);
let expr = match self.peek() { let expr = match self.peek() {
Some(LParen) => { Some(LParen) => {
let args = try!(self.call_expression()); let args = self.call_expression()?;
Expression::Call(Callable::NamedFunction(name), args) Expression::Call(Callable::NamedFunction(name), args)
} }
__ => Expression::Variable(name), __ => Expression::Variable(name),
@ -560,14 +560,14 @@ impl Parser {
fn call_expression(&mut self) -> ParseResult<Vec<Expression>> { fn call_expression(&mut self) -> ParseResult<Vec<Expression>> {
expect!(self, LParen); expect!(self, LParen);
let args: Vec<Expression> = try!(self.exprlist()); let args: Vec<Expression> = self.exprlist()?;
expect!(self, RParen); expect!(self, RParen);
Ok(args) Ok(args)
} }
fn paren_expr(&mut self) -> ParseResult<Expression> { fn paren_expr(&mut self) -> ParseResult<Expression> {
expect!(self, Token::LParen); expect!(self, Token::LParen);
let expr = try!(self.expression()); let expr = self.expression()?;
expect!(self, Token::RParen); expect!(self, Token::RParen);
Ok(expr) Ok(expr)
} }

View File

@ -71,10 +71,10 @@ pub fn tokenize(input: &str) -> TokenizeResult {
'}' => RCurlyBrace, '}' => RCurlyBrace,
'[' => LSquareBracket, '[' => LSquareBracket,
']' => RSquareBracket, ']' => RSquareBracket,
'"' => try!(tokenize_str(&mut iter)), '"' => tokenize_str(&mut iter)?,
c if !char::is_alphanumeric(c) => try!(tokenize_operator(c, &mut iter)), c if !char::is_alphanumeric(c) => tokenize_operator(c, &mut iter)?,
c @ '.' | c if is_digit(&c) => try!(tokenize_number_or_period(c, &mut iter)), c @ '.' | c if is_digit(&c) => tokenize_number_or_period(c, &mut iter)?,
c => try!(tokenize_identifier(c, &mut iter)), c => tokenize_identifier(c, &mut iter)?,
}; };
tokens.push(cur_tok); tokens.push(cur_tok);
} }