Starting work on guard arms

This commit is contained in:
greg 2018-07-13 21:50:38 -07:00
parent 5ecd298e6a
commit 837a180b09
2 changed files with 26 additions and 6 deletions

View File

@ -125,13 +125,25 @@ pub enum Discriminator {
pub enum IfExpressionBody {
SimpleConditional(Block, Option<Block>),
SimplePatternMatch(Pattern, Block, Option<Block>),
GuardList(Vec<Guard>)
GuardList(Vec<GuardArm>)
}
#[derive(Debug, PartialEq, Clone)]
pub struct Guard {
pat: Pattern,
body: Block,
pub struct GuardArm {
pub guard: Guard,
pub body: Block,
}
#[derive(Debug, PartialEq, Clone)]
pub enum Guard {
Pat(Pattern),
HalfExpr(HalfExpr)
}
#[derive(Debug, PartialEq, Clone)]
pub struct HalfExpr {
pub op: Option<BinOp>,
pub expr: ExpressionType,
}
#[derive(Debug, PartialEq, Clone)]

View File

@ -124,7 +124,8 @@ modified_precedence_expression := ???
conditional := block else_clause
simple_pattern_match := pattern 'then' conditional
else_clause := ε | 'else' block
guard_block := '{' (guard, ',')* '}'
guard_block := '{' (guard_arm, ',')* '}'
guard_arm := guard '->' block
guard := ??
/* Expression - While */
@ -701,10 +702,17 @@ impl Parser {
});
parse_method!(guard_block(&mut self) -> ParseResult<IfExpressionBody> {
let guards = delimited!(self, LCurlyBrace, guard, Comma, RCurlyBrace);
let guards = delimited!(self, LCurlyBrace, guard_arm, Comma, RCurlyBrace);
Ok(IfExpressionBody::GuardList(guards))
});
parse_method!(guard_arm(&mut self) -> ParseResult<GuardArm> {
let guard = self.guard()?;
expect!(self, Operator(ref c) if **c == "->");
let body = self.block()?;
Ok(GuardArm { guard, body })
});
parse_method!(guard(&mut self) -> ParseResult<Guard> {
unimplemented!()
});