From b513e94d88c8702a051acf4d58c0335c9aed9db2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 5 Aug 2018 01:03:22 +0300 Subject: Conditionals --- src/grammar.ron | 1 + src/grammar/expressions/atom.rs | 34 +++++++++++++++++++++++++++------- src/syntax_kinds/generated.rs | 2 ++ 3 files changed, 30 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/grammar.ron b/src/grammar.ron index 8ce5ebc49..9f80b3ebf 100644 --- a/src/grammar.ron +++ b/src/grammar.ron @@ -135,6 +135,7 @@ Grammar( "PATH_EXPR", "LAMBDA_EXPR", "IF_EXPR", + "WHILE_EXPR", "BLOCK_EXPR", "RETURN_EXPR", "MATCH_EXPR", diff --git a/src/grammar/expressions/atom.rs b/src/grammar/expressions/atom.rs index ad654df9e..6a0d47d82 100644 --- a/src/grammar/expressions/atom.rs +++ b/src/grammar/expressions/atom.rs @@ -29,7 +29,7 @@ pub(crate) fn literal(p: &mut Parser) -> Option { pub(super) const ATOM_EXPR_FIRST: TokenSet = token_set_union![ LITERAL_FIRST, - token_set![L_PAREN, PIPE, MOVE_KW, IF_KW, MATCH_KW, UNSAFE_KW, L_CURLY, RETURN_KW, + token_set![L_PAREN, PIPE, MOVE_KW, IF_KW, WHILE_KW, MATCH_KW, UNSAFE_KW, L_CURLY, RETURN_KW, IDENT, SELF_KW, SUPER_KW, COLONCOLON ], ]; @@ -47,6 +47,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option lambda_expr(p), MOVE_KW if la == PIPE => lambda_expr(p), IF_KW => if_expr(p), + WHILE_KW => while_expr(p), MATCH_KW => match_expr(p), UNSAFE_KW if la == L_CURLY => block_expr(p), L_CURLY => block_expr(p), @@ -97,7 +98,8 @@ fn lambda_expr(p: &mut Parser) -> CompletedMarker { fn if_expr(p: &mut Parser) -> CompletedMarker { assert!(p.at(IF_KW)); let m = p.start(); - if_head(p); + p.bump(); + cond(p); block(p); if p.at(ELSE_KW) { p.bump(); @@ -110,10 +112,28 @@ fn if_expr(p: &mut Parser) -> CompletedMarker { m.complete(p, IF_EXPR) } -fn if_head(p: &mut Parser) { - assert!(p.at(IF_KW)); +// test while_expr +// fn foo() { +// while true {}; +// while let Some(x) = it.next() {}; +// } +fn while_expr(p: &mut Parser) -> CompletedMarker { + assert!(p.at(WHILE_KW)); + let m = p.start(); p.bump(); - expr_no_struct(p); + cond(p); + block(p); + m.complete(p, WHILE_EXPR) +} + +// test cond +// fn foo() { if let Some(_) = None {} } +fn cond(p: &mut Parser) { + if p.eat(LET_KW) { + patterns::pattern(p); + p.expect(EQ); + } + expr_no_struct(p) } // test match_expr @@ -152,8 +172,8 @@ fn match_arm(p: &mut Parser) { break; } } - if p.at(IF_KW) { - if_head(p) + if p.eat(IF_KW) { + expr_no_struct(p); } p.expect(FAT_ARROW); expr(p); diff --git a/src/syntax_kinds/generated.rs b/src/syntax_kinds/generated.rs index 7bdd0267c..1d3d3b9c1 100644 --- a/src/syntax_kinds/generated.rs +++ b/src/syntax_kinds/generated.rs @@ -125,6 +125,7 @@ pub enum SyntaxKind { PATH_EXPR, LAMBDA_EXPR, IF_EXPR, + WHILE_EXPR, BLOCK_EXPR, RETURN_EXPR, MATCH_EXPR, @@ -343,6 +344,7 @@ impl SyntaxKind { PATH_EXPR => &SyntaxInfo { name: "PATH_EXPR" }, LAMBDA_EXPR => &SyntaxInfo { name: "LAMBDA_EXPR" }, IF_EXPR => &SyntaxInfo { name: "IF_EXPR" }, + WHILE_EXPR => &SyntaxInfo { name: "WHILE_EXPR" }, BLOCK_EXPR => &SyntaxInfo { name: "BLOCK_EXPR" }, RETURN_EXPR => &SyntaxInfo { name: "RETURN_EXPR" }, MATCH_EXPR => &SyntaxInfo { name: "MATCH_EXPR" }, -- cgit v1.2.3