Converted all parser methods to use the annotation
This commit is contained in:
parent
3b20b40eb7
commit
6219a06d6f
@ -83,32 +83,6 @@ macro_rules! expect {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
macro_rules! parse_method {
|
||||
($name:ident(&mut $self:ident) -> $type:ty $body:block) => {
|
||||
fn $name(&mut $self) -> $type {
|
||||
let next_token = $self.peek_with_token_offset();
|
||||
let record = ParseRecord {
|
||||
production_name: stringify!($name).to_string(),
|
||||
next_token: format!("{}", next_token.to_string_with_metadata()),
|
||||
level: $self.parse_level,
|
||||
};
|
||||
$self.parse_level += 1;
|
||||
$self.parse_record.push(record);
|
||||
let result = { $body };
|
||||
|
||||
if $self.parse_level != 0 {
|
||||
$self.parse_level -= 1;
|
||||
}
|
||||
match result {
|
||||
Err(ParseError { token: None, msg }) =>
|
||||
Err(ParseError { token: Some(next_token), msg }),
|
||||
_ => result
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! delimited {
|
||||
($self:expr, $start:pat, $parse_fn:ident, $( $delim:pat )|+, $end:pat, nonstrict) => {
|
||||
delimited!($self, $start, $parse_fn, $( $delim )|*, $end, false)
|
||||
@ -266,7 +240,8 @@ enumerator := identifier '<-' expression | identifier '=' expression //TODO add
|
||||
*/
|
||||
|
||||
impl Parser {
|
||||
parse_method!(program(&mut self) -> ParseResult<AST> {
|
||||
#[recursive_descent_method]
|
||||
fn program(&mut self) -> ParseResult<AST> {
|
||||
let mut statements = Vec::new();
|
||||
loop {
|
||||
match self.peek() {
|
||||
@ -279,9 +254,10 @@ impl Parser {
|
||||
}
|
||||
}
|
||||
Ok(AST(statements))
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(statement(&mut self) -> ParseResult<Statement> {
|
||||
#[recursive_descent_method]
|
||||
fn statement(&mut self) -> ParseResult<Statement> {
|
||||
//TODO handle error recovery here
|
||||
match self.peek() {
|
||||
Keyword(Type) => self.type_declaration().map(|decl| { Statement::Declaration(decl) }),
|
||||
@ -291,14 +267,16 @@ impl Parser {
|
||||
Keyword(Impl) => self.impl_declaration().map(|decl| Statement::Declaration(decl)),
|
||||
_ => self.expression().map(|expr| { Statement::ExpressionStatement(expr) } ),
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(type_declaration(&mut self) -> ParseResult<Declaration> {
|
||||
#[recursive_descent_method]
|
||||
fn type_declaration(&mut self) -> ParseResult<Declaration> {
|
||||
expect!(self, Keyword(Type));
|
||||
self.type_declaration_body()
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(type_declaration_body(&mut self) -> ParseResult<Declaration> {
|
||||
#[recursive_descent_method]
|
||||
fn type_declaration_body(&mut self) -> ParseResult<Declaration> {
|
||||
if let Keyword(Alias) = self.peek() {
|
||||
self.type_alias()
|
||||
} else {
|
||||
@ -313,17 +291,19 @@ impl Parser {
|
||||
let body = self.type_body()?;
|
||||
Ok(Declaration::TypeDecl { name, body, mutable})
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(type_alias(&mut self) -> ParseResult<Declaration> {
|
||||
#[recursive_descent_method]
|
||||
fn type_alias(&mut self) -> ParseResult<Declaration> {
|
||||
expect!(self, Keyword(Alias));
|
||||
let alias = self.identifier()?;
|
||||
expect!(self, Operator(ref c) if **c == "=");
|
||||
let original = self.identifier()?;
|
||||
Ok(Declaration::TypeAlias(alias, original))
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(type_body(&mut self) -> ParseResult<TypeBody> {
|
||||
#[recursive_descent_method]
|
||||
fn type_body(&mut self) -> ParseResult<TypeBody> {
|
||||
let mut variants = Vec::new();
|
||||
variants.push(self.variant_specifier()?);
|
||||
loop {
|
||||
@ -335,9 +315,10 @@ impl Parser {
|
||||
}
|
||||
}
|
||||
Ok(TypeBody(variants))
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(variant_specifier(&mut self) -> ParseResult<Variant> {
|
||||
#[recursive_descent_method]
|
||||
fn variant_specifier(&mut self) -> ParseResult<Variant> {
|
||||
use self::Variant::*;
|
||||
|
||||
let name = self.identifier()?;
|
||||
@ -352,16 +333,18 @@ impl Parser {
|
||||
},
|
||||
_ => Ok(UnitStruct(name))
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(typed_identifier(&mut self) -> ParseResult<(Rc<String>, TypeIdentifier)> {
|
||||
#[recursive_descent_method]
|
||||
fn typed_identifier(&mut self) -> ParseResult<(Rc<String>, TypeIdentifier)> {
|
||||
let identifier = self.identifier()?;
|
||||
expect!(self, Colon);
|
||||
let type_name = self.type_name()?;
|
||||
Ok((identifier, type_name))
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(func_declaration(&mut self) -> ParseResult<Declaration> {
|
||||
#[recursive_descent_method]
|
||||
fn func_declaration(&mut self) -> ParseResult<Declaration> {
|
||||
let signature = self.func_signature()?;
|
||||
if let LCurlyBrace = self.peek() {
|
||||
let statements = delimited!(self, LCurlyBrace, statement, Newline | Semicolon, RCurlyBrace, nonstrict);
|
||||
@ -369,9 +352,10 @@ impl Parser {
|
||||
} else {
|
||||
Ok(Declaration::FuncSig(signature))
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(func_signature(&mut self) -> ParseResult<Signature> {
|
||||
#[recursive_descent_method]
|
||||
fn func_signature(&mut self) -> ParseResult<Signature> {
|
||||
expect!(self, Keyword(Func));
|
||||
let (name, operator) = match self.peek() {
|
||||
Operator(s) => {
|
||||
@ -387,19 +371,20 @@ impl Parser {
|
||||
_ => None,
|
||||
};
|
||||
Ok(Signature { name, operator, params, type_anno })
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(formal_param(&mut self) -> ParseResult<FormalParam> {
|
||||
#[recursive_descent_method]
|
||||
fn formal_param(&mut self) -> ParseResult<FormalParam> {
|
||||
let name = self.identifier()?;
|
||||
let ty = match self.peek() {
|
||||
Colon => Some(self.type_anno()?),
|
||||
_ => None
|
||||
};
|
||||
Ok((name, ty))
|
||||
});
|
||||
|
||||
parse_method!(binding_declaration(&mut self) -> ParseResult<Declaration> {
|
||||
}
|
||||
|
||||
#[recursive_descent_method]
|
||||
fn binding_declaration(&mut self) -> ParseResult<Declaration> {
|
||||
expect!(self, Keyword(Kw::Let));
|
||||
let constant = match self.peek() {
|
||||
Keyword(Kw::Mut) => {
|
||||
@ -413,20 +398,23 @@ impl Parser {
|
||||
let expr = self.expression()?;
|
||||
|
||||
Ok(Declaration::Binding { name, constant, expr })
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(interface_declaration(&mut self) -> ParseResult<Declaration> {
|
||||
#[recursive_descent_method]
|
||||
fn interface_declaration(&mut self) -> ParseResult<Declaration> {
|
||||
expect!(self, Keyword(Interface));
|
||||
let name = self.identifier()?;
|
||||
let signatures = self.signature_block()?;
|
||||
Ok(Declaration::Interface { name, signatures })
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(signature_block(&mut self) -> ParseResult<Vec<Signature>> {
|
||||
#[recursive_descent_method]
|
||||
fn signature_block(&mut self) -> ParseResult<Vec<Signature>> {
|
||||
Ok(delimited!(self, LCurlyBrace, func_signature, Newline | Semicolon, RCurlyBrace, nonstrict))
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(impl_declaration(&mut self) -> ParseResult<Declaration> {
|
||||
#[recursive_descent_method]
|
||||
fn impl_declaration(&mut self) -> ParseResult<Declaration> {
|
||||
expect!(self, Keyword(Impl));
|
||||
let first = self.type_name()?;
|
||||
let second = if let Keyword(For) = self.peek() {
|
||||
@ -449,13 +437,15 @@ impl Parser {
|
||||
(first, None) => Declaration::Impl { type_name: first, interface_name: None, block }
|
||||
};
|
||||
Ok(result)
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(decl_block(&mut self) -> ParseResult<Vec<Declaration>> {
|
||||
#[recursive_descent_method]
|
||||
fn decl_block(&mut self) -> ParseResult<Vec<Declaration>> {
|
||||
Ok(delimited!(self, LCurlyBrace, func_declaration, Newline | Semicolon, RCurlyBrace, nonstrict))
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(expression(&mut self) -> ParseResult<Expression> {
|
||||
#[recursive_descent_method]
|
||||
fn expression(&mut self) -> ParseResult<Expression> {
|
||||
let mut expr_body = self.precedence_expr(BinOp::min_precedence())?;
|
||||
let type_anno = match self.peek() {
|
||||
Colon => Some(self.type_anno()?),
|
||||
@ -466,22 +456,25 @@ impl Parser {
|
||||
}
|
||||
expr_body.1 = type_anno;
|
||||
Ok(expr_body)
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(type_anno(&mut self) -> ParseResult<TypeIdentifier> {
|
||||
#[recursive_descent_method]
|
||||
fn type_anno(&mut self) -> ParseResult<TypeIdentifier> {
|
||||
expect!(self, Colon);
|
||||
self.type_name()
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(type_name(&mut self) -> ParseResult<TypeIdentifier> {
|
||||
#[recursive_descent_method]
|
||||
fn type_name(&mut self) -> ParseResult<TypeIdentifier> {
|
||||
use self::TypeIdentifier::*;
|
||||
Ok(match self.peek() {
|
||||
LParen => Tuple(delimited!(self, LParen, type_name, Comma, RParen)),
|
||||
_ => Singleton(self.type_singleton_name()?),
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(type_singleton_name(&mut self) -> ParseResult<TypeSingletonName> {
|
||||
#[recursive_descent_method]
|
||||
fn type_singleton_name(&mut self) -> ParseResult<TypeSingletonName> {
|
||||
Ok(TypeSingletonName {
|
||||
name: self.identifier()?,
|
||||
params: match self.peek() {
|
||||
@ -489,7 +482,7 @@ impl Parser {
|
||||
_ => vec![],
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
// this implements Pratt parsing, see http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/
|
||||
fn precedence_expr(&mut self, precedence: i32) -> ParseResult<Expression> {
|
||||
@ -522,7 +515,8 @@ impl Parser {
|
||||
Ok(lhs)
|
||||
}
|
||||
|
||||
parse_method!(prefix_expr(&mut self) -> ParseResult<Expression> {
|
||||
#[recursive_descent_method]
|
||||
fn prefix_expr(&mut self) -> ParseResult<Expression> {
|
||||
match self.peek() {
|
||||
Operator(ref op) if PrefixOp::is_prefix(&*op) => {
|
||||
let sigil = match self.next() {
|
||||
@ -536,9 +530,10 @@ impl Parser {
|
||||
},
|
||||
_ => self.call_expr()
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(call_expr(&mut self) -> ParseResult<Expression> {
|
||||
#[recursive_descent_method]
|
||||
fn call_expr(&mut self) -> ParseResult<Expression> {
|
||||
let index = self.index_expr()?;
|
||||
Ok(if let LParen = self.peek() {
|
||||
let arguments = delimited!(self, LParen, expression, Comma, RParen);
|
||||
@ -546,9 +541,10 @@ impl Parser {
|
||||
} else {
|
||||
index
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(index_expr(&mut self) -> ParseResult<Expression> {
|
||||
#[recursive_descent_method]
|
||||
fn index_expr(&mut self) -> ParseResult<Expression> {
|
||||
let primary = self.primary()?;
|
||||
Ok(if let LSquareBracket = self.peek() {
|
||||
let indexers = delimited!(self, LSquareBracket, expression, Comma, RSquareBracket);
|
||||
@ -559,9 +555,10 @@ impl Parser {
|
||||
} else {
|
||||
primary
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(primary(&mut self) -> ParseResult<Expression> {
|
||||
#[recursive_descent_method]
|
||||
fn primary(&mut self) -> ParseResult<Expression> {
|
||||
match self.peek() {
|
||||
LCurlyBrace => self.curly_brace_expr(),
|
||||
LParen => self.paren_expr(),
|
||||
@ -572,18 +569,21 @@ impl Parser {
|
||||
Identifier(_) => self.identifier_expr(),
|
||||
_ => self.literal(),
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(list_expr(&mut self) -> ParseResult<Expression> {
|
||||
#[recursive_descent_method]
|
||||
fn list_expr(&mut self) -> ParseResult<Expression> {
|
||||
let exprs = delimited!(self, LSquareBracket, expression, Comma, RSquareBracket);
|
||||
Ok(Expression(ExpressionType::ListLiteral(exprs), None))
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(curly_brace_expr(&mut self) -> ParseResult<Expression> {
|
||||
#[recursive_descent_method]
|
||||
fn curly_brace_expr(&mut self) -> ParseResult<Expression> {
|
||||
self.lambda_expr()
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(lambda_expr(&mut self) -> ParseResult<Expression> {
|
||||
#[recursive_descent_method]
|
||||
fn lambda_expr(&mut self) -> ParseResult<Expression> {
|
||||
expect!(self, LCurlyBrace);
|
||||
let params = delimited!(self, Pipe, formal_param, Comma, Pipe);
|
||||
let mut body = Vec::new();
|
||||
@ -599,9 +599,10 @@ impl Parser {
|
||||
}
|
||||
expect!(self, RCurlyBrace);
|
||||
Ok(Expression(ExpressionType::Lambda { params, body }, None)) //TODO need to handle types somehow
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(paren_expr(&mut self) -> ParseResult<Expression> {
|
||||
#[recursive_descent_method]
|
||||
fn paren_expr(&mut self) -> ParseResult<Expression> {
|
||||
use self::ExpressionType::*;
|
||||
let old_struct_value = self.restrictions.no_struct_literal;
|
||||
self.restrictions.no_struct_literal = false;
|
||||
@ -615,9 +616,10 @@ impl Parser {
|
||||
};
|
||||
self.restrictions.no_struct_literal = old_struct_value;
|
||||
output
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(identifier_expr(&mut self) -> ParseResult<Expression> {
|
||||
#[recursive_descent_method]
|
||||
fn identifier_expr(&mut self) -> ParseResult<Expression> {
|
||||
use self::ExpressionType::*;
|
||||
let identifier = self.identifier()?;
|
||||
Ok(match self.peek() {
|
||||
@ -627,20 +629,23 @@ impl Parser {
|
||||
},
|
||||
_ => Expression(Value(identifier), None)
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(record_block(&mut self) -> ParseResult<Vec<(Rc<String>, Expression)>> {
|
||||
#[recursive_descent_method]
|
||||
fn record_block(&mut self) -> ParseResult<Vec<(Rc<String>, Expression)>> {
|
||||
Ok(delimited!(self, LCurlyBrace, record_entry, Comma, RCurlyBrace))
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(record_entry(&mut self) -> ParseResult<(Rc<String>, Expression)> {
|
||||
#[recursive_descent_method]
|
||||
fn record_entry(&mut self) -> ParseResult<(Rc<String>, Expression)> {
|
||||
let field_name = self.identifier()?;
|
||||
expect!(self, Colon);
|
||||
let value = self.expression()?;
|
||||
Ok((field_name, value))
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(if_expr(&mut self) -> ParseResult<Expression> {
|
||||
#[recursive_descent_method]
|
||||
fn if_expr(&mut self) -> ParseResult<Expression> {
|
||||
expect!(self, Keyword(Kw::If));
|
||||
let discriminator = Box::new({
|
||||
self.restrictions.no_struct_literal = true;
|
||||
@ -656,9 +661,10 @@ impl Parser {
|
||||
});
|
||||
|
||||
Ok(Expression(ExpressionType::IfExpression { discriminator, body }, None))
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(discriminator(&mut self) -> ParseResult<Discriminator> {
|
||||
#[recursive_descent_method]
|
||||
fn discriminator(&mut self) -> ParseResult<Discriminator> {
|
||||
let lhs = self.prefix_expr()?;
|
||||
let ref next = self.peek();
|
||||
Ok(if let Some(op) = BinOp::from_sigil_token(next) {
|
||||
@ -666,34 +672,38 @@ impl Parser {
|
||||
} else {
|
||||
Discriminator::Simple(lhs)
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(conditional(&mut self) -> ParseResult<IfExpressionBody> {
|
||||
#[recursive_descent_method]
|
||||
fn conditional(&mut self) -> ParseResult<IfExpressionBody> {
|
||||
expect!(self, Keyword(Kw::Then));
|
||||
let then_clause = self.expr_or_block()?;
|
||||
let else_clause = self.else_clause()?;
|
||||
Ok(IfExpressionBody::SimpleConditional(then_clause, else_clause))
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(simple_pattern_match(&mut self) -> ParseResult<IfExpressionBody> {
|
||||
#[recursive_descent_method]
|
||||
fn simple_pattern_match(&mut self) -> ParseResult<IfExpressionBody> {
|
||||
expect!(self, Keyword(Kw::Is));
|
||||
let pat = self.pattern()?;
|
||||
expect!(self, Keyword(Kw::Then));
|
||||
let then_clause = self.expr_or_block()?;
|
||||
let else_clause = self.else_clause()?;
|
||||
Ok(IfExpressionBody::SimplePatternMatch(pat, then_clause, else_clause))
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(else_clause(&mut self) -> ParseResult<Option<Block>> {
|
||||
#[recursive_descent_method]
|
||||
fn else_clause(&mut self) -> ParseResult<Option<Block>> {
|
||||
Ok(if let Keyword(Kw::Else) = self.peek() {
|
||||
self.next();
|
||||
Some(self.expr_or_block()?)
|
||||
} else {
|
||||
None
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(guard_block(&mut self) -> ParseResult<IfExpressionBody> {
|
||||
#[recursive_descent_method]
|
||||
fn guard_block(&mut self) -> ParseResult<IfExpressionBody> {
|
||||
//TODO - delimited! isn't sophisticated enough to do thisa
|
||||
//let guards = delimited!(self, LCurlyBrace, guard_arm, Comma, RCurlyBrace);
|
||||
expect!(self, LCurlyBrace);
|
||||
@ -714,16 +724,18 @@ impl Parser {
|
||||
}
|
||||
expect!(self, RCurlyBrace);
|
||||
Ok(IfExpressionBody::GuardList(guards))
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(guard_arm(&mut self) -> ParseResult<GuardArm> {
|
||||
#[recursive_descent_method]
|
||||
fn guard_arm(&mut self) -> ParseResult<GuardArm> {
|
||||
let guard = self.guard()?;
|
||||
expect!(self, Operator(ref c) if **c == "->");
|
||||
let body = self.expr_or_block()?;
|
||||
Ok(GuardArm { guard, body })
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(guard(&mut self) -> ParseResult<Guard> {
|
||||
#[recursive_descent_method]
|
||||
fn guard(&mut self) -> ParseResult<Guard> {
|
||||
Ok(match self.peek() {
|
||||
Keyword(Kw::Is) => {
|
||||
self.next();
|
||||
@ -740,18 +752,20 @@ impl Parser {
|
||||
Guard::HalfExpr(HalfExpr { op: None, expr })
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(pattern(&mut self) -> ParseResult<Pattern> {
|
||||
#[recursive_descent_method]
|
||||
fn pattern(&mut self) -> ParseResult<Pattern> {
|
||||
if let LParen = self.peek() {
|
||||
let tuple_pattern_variants = delimited!(self, LParen, pattern, Comma, RParen);
|
||||
Ok(Pattern::TuplePattern(tuple_pattern_variants))
|
||||
} else {
|
||||
self.simple_pattern()
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(simple_pattern(&mut self) -> ParseResult<Pattern> {
|
||||
#[recursive_descent_method]
|
||||
fn simple_pattern(&mut self) -> ParseResult<Pattern> {
|
||||
Ok(match self.peek() {
|
||||
Identifier(_) => {
|
||||
let id = self.identifier()?;
|
||||
@ -787,7 +801,7 @@ impl Parser {
|
||||
},
|
||||
other => return ParseError::new(&format!("{:?} is not a valid Pattern", other))
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
#[recursive_descent_method]
|
||||
fn signed_number_literal(&mut self) -> ParseResult<Pattern> {
|
||||
@ -802,7 +816,8 @@ impl Parser {
|
||||
Ok(Pattern::Literal(PatternLiteral::NumPattern { neg, num: expr_type }))
|
||||
}
|
||||
|
||||
parse_method!(record_pattern_entry(&mut self) -> ParseResult<(Rc<String>, Pattern)> {
|
||||
#[recursive_descent_method]
|
||||
fn record_pattern_entry(&mut self) -> ParseResult<(Rc<String>, Pattern)> {
|
||||
let name = self.identifier()?;
|
||||
Ok(match self.peek() {
|
||||
Colon => {
|
||||
@ -812,13 +827,15 @@ impl Parser {
|
||||
},
|
||||
_ => (name.clone(), Pattern::Literal(PatternLiteral::StringPattern(name.clone())))
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(block(&mut self) -> ParseResult<Block> {
|
||||
#[recursive_descent_method]
|
||||
fn block(&mut self) -> ParseResult<Block> {
|
||||
Ok(delimited!(self, LCurlyBrace, statement, Newline | Semicolon, RCurlyBrace, nonstrict))
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(expr_or_block(&mut self) -> ParseResult<Block> {
|
||||
#[recursive_descent_method]
|
||||
fn expr_or_block(&mut self) -> ParseResult<Block> {
|
||||
match self.peek() {
|
||||
LCurlyBrace => self.block(),
|
||||
_ => {
|
||||
@ -826,9 +843,10 @@ impl Parser {
|
||||
Ok(vec![Statement::ExpressionStatement(expr)])
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(while_expr(&mut self) -> ParseResult<Expression> {
|
||||
#[recursive_descent_method]
|
||||
fn while_expr(&mut self) -> ParseResult<Expression> {
|
||||
use self::ExpressionType::*;
|
||||
expect!(self, Keyword(Kw::While));
|
||||
let condition = {
|
||||
@ -839,16 +857,18 @@ impl Parser {
|
||||
};
|
||||
let body = self.block()?;
|
||||
Ok(Expression(WhileExpression {condition, body}, None))
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(while_cond(&mut self) -> ParseResult<Option<Expression>> {
|
||||
#[recursive_descent_method]
|
||||
fn while_cond(&mut self) -> ParseResult<Option<Expression>> {
|
||||
Ok(match self.peek() {
|
||||
LCurlyBrace => None,
|
||||
_ => Some(self.expression()?),
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(for_expr(&mut self) -> ParseResult<Expression> {
|
||||
#[recursive_descent_method]
|
||||
fn for_expr(&mut self) -> ParseResult<Expression> {
|
||||
expect!(self, Keyword(Kw::For));
|
||||
let enumerators = if let LCurlyBrace = self.peek() {
|
||||
delimited!(self, LCurlyBrace, enumerator, Comma | Newline, RCurlyBrace)
|
||||
@ -863,16 +883,18 @@ impl Parser {
|
||||
};
|
||||
let body = Box::new(self.for_expr_body()?);
|
||||
Ok(Expression(ExpressionType::ForExpression { enumerators, body }, None))
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(enumerator(&mut self) -> ParseResult<Enumerator> {
|
||||
#[recursive_descent_method]
|
||||
fn enumerator(&mut self) -> ParseResult<Enumerator> {
|
||||
let id = self.identifier()?;
|
||||
expect!(self, Operator(ref c) if **c == "<-");
|
||||
let generator = self.expression()?;
|
||||
Ok(Enumerator { id, generator })
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(for_expr_body(&mut self) -> ParseResult<ForBody> {
|
||||
#[recursive_descent_method]
|
||||
fn for_expr_body(&mut self) -> ParseResult<ForBody> {
|
||||
use self::ForBody::*;
|
||||
Ok(match self.peek() {
|
||||
LCurlyBrace => {
|
||||
@ -885,16 +907,18 @@ impl Parser {
|
||||
},
|
||||
_ => return ParseError::new("for expressions must end in a block or 'return'"),
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(identifier(&mut self) -> ParseResult<Rc<String>> {
|
||||
#[recursive_descent_method]
|
||||
fn identifier(&mut self) -> ParseResult<Rc<String>> {
|
||||
match self.next() {
|
||||
Identifier(s) => Ok(s),
|
||||
p => ParseError::new(&format!("Expected an identifier, got {:?}", p)),
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(literal(&mut self) -> ParseResult<Expression> {
|
||||
#[recursive_descent_method]
|
||||
fn literal(&mut self) -> ParseResult<Expression> {
|
||||
use self::ExpressionType::*;
|
||||
match self.peek() {
|
||||
DigitGroup(_) | HexLiteral(_) | BinNumberSigil | Period => self.number_literal(),
|
||||
@ -912,7 +936,7 @@ impl Parser {
|
||||
}
|
||||
e => ParseError::new(&format!("Expected a literal expression, got {:?}", e)),
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[recursive_descent_method]
|
||||
fn number_literal(&mut self) -> ParseResult<Expression> {
|
||||
@ -922,7 +946,8 @@ impl Parser {
|
||||
}
|
||||
}
|
||||
|
||||
parse_method!(int_literal(&mut self) -> ParseResult<Expression> {
|
||||
#[recursive_descent_method]
|
||||
fn int_literal(&mut self) -> ParseResult<Expression> {
|
||||
use self::ExpressionType::*;
|
||||
match self.next() {
|
||||
BinNumberSigil => {
|
||||
@ -937,9 +962,10 @@ impl Parser {
|
||||
},
|
||||
_ => return ParseError::new("Expected '0x' or '0b'"),
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(float_literal(&mut self) -> ParseResult<Expression> {
|
||||
#[recursive_descent_method]
|
||||
fn float_literal(&mut self) -> ParseResult<Expression> {
|
||||
use self::ExpressionType::*;
|
||||
let mut digits = self.digits()?;
|
||||
if let TokenType::Period = self.peek() {
|
||||
@ -957,9 +983,10 @@ impl Parser {
|
||||
Err(e) => ParseError::new(&format!("Integer failed to parse with error: {}", e)),
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
parse_method!(digits(&mut self) -> ParseResult<String> {
|
||||
#[recursive_descent_method]
|
||||
fn digits(&mut self) -> ParseResult<String> {
|
||||
let mut ds = String::new();
|
||||
loop {
|
||||
match self.peek() {
|
||||
@ -969,7 +996,7 @@ impl Parser {
|
||||
}
|
||||
}
|
||||
Ok(ds)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_binary(digits: String) -> ParseResult<u64> {
|
||||
|
Loading…
Reference in New Issue
Block a user