Using delmited! macro for more things

This commit is contained in:
greg 2017-09-30 14:11:38 -07:00
parent e88a0f59b5
commit d05f173dd3

View File

@ -309,8 +309,9 @@ literal := 'true' | 'false' | number_literal | STR_LITERAL
if_expr := 'if' expression block else_clause if_expr := 'if' expression block else_clause
else_clause := ε | 'else' block else_clause := ε | 'else' block
match_expr := 'match' expression '{' match_body '}' match_expr := 'match' expression match_body
match_body := pattern '=>' expression match_body := '{' (match_arm)* '}'
match_arm := pattern '=>' expression
pattern := identifier //TODO NOT DONE pattern := identifier //TODO NOT DONE
block := '{' (statement)* '}' block := '{' (statement)* '}'
@ -737,20 +738,7 @@ impl Parser {
}); });
parse_method!(index_expr(&mut self) -> ParseResult<Vec<Expression>> { parse_method!(index_expr(&mut self) -> ParseResult<Vec<Expression>> {
expect!(self, LSquareBracket, "Expected '['"); Ok(delimited!(self, LSquareBracket, expression, Comma, RSquareBracket))
let mut exprs = Vec::new();
loop {
if let RSquareBracket = self.peek() {
break;
}
exprs.push(self.expression()?);
match self.peek() {
Comma => { self.next(); }
_ => break,
};
}
expect!(self, RSquareBracket, "Expected ']'");
Ok(exprs)
}); });
parse_method!(if_expr(&mut self) -> ParseResult<Expression> { parse_method!(if_expr(&mut self) -> ParseResult<Expression> {
@ -777,28 +765,22 @@ impl Parser {
parse_method!(match_expr(&mut self) -> ParseResult<Expression> { parse_method!(match_expr(&mut self) -> ParseResult<Expression> {
expect!(self, Keyword(Kw::Match), "Expected 'match'"); expect!(self, Keyword(Kw::Match), "Expected 'match'");
let expr = self.expression()?; let expr = self.expression()?;
expect!(self, LCurlyBrace, "Expected '{'"); //TODO abstract these errors into the delimited macro
//expect!(self, LCurlyBrace, "Expected '{'");
let body = self.match_body()?; let body = self.match_body()?;
expect!(self, RCurlyBrace, "Expected '}'"); //expect!(self, RCurlyBrace, "Expected '}'");
Ok(Expression(ExpressionType::MatchExpression(Box::new(expr), body), None)) Ok(Expression(ExpressionType::MatchExpression(Box::new(expr), body), None))
}); });
parse_method!(match_body(&mut self) -> ParseResult<Vec<MatchArm>> { parse_method!(match_body(&mut self) -> ParseResult<Vec<MatchArm>> {
let mut arms = Vec::new(); Ok(delimited!(self, LCurlyBrace, match_arm, Comma, RCurlyBrace))
loop { });
if let RCurlyBrace = self.peek() {
break; parse_method!(match_arm(&mut self) -> ParseResult<MatchArm> {
}
let pat = self.pattern()?; let pat = self.pattern()?;
expect!(self, Operator(ref c) if **c == "=>", "Expected '=>'"); expect!(self, Operator(ref c) if **c == "=>", "Expected '=>'");
let expr = self.expression()?; let expr = self.expression()?;
arms.push(MatchArm {pat, expr}); Ok(MatchArm { pat, expr })
match self.peek() {
Comma => { self.next(); },
_ => break
}
}
Ok(arms)
}); });
parse_method!(pattern(&mut self) -> ParseResult<Pattern> { parse_method!(pattern(&mut self) -> ParseResult<Pattern> {