diff options
author | Aleksey Kladov <[email protected]> | 2018-08-04 23:03:22 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-04 23:03:22 +0100 |
commit | b513e94d88c8702a051acf4d58c0335c9aed9db2 (patch) | |
tree | 5656edeea3efc1288c3157298980fce5fa701726 /src/grammar/expressions | |
parent | e4b84fbf36d04e54f68f914f724c8e804233d3aa (diff) |
Conditionals
Diffstat (limited to 'src/grammar/expressions')
-rw-r--r-- | src/grammar/expressions/atom.rs | 34 |
1 files changed, 27 insertions, 7 deletions
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<CompletedMarker> { | |||
29 | pub(super) const ATOM_EXPR_FIRST: TokenSet = | 29 | pub(super) const ATOM_EXPR_FIRST: TokenSet = |
30 | token_set_union![ | 30 | token_set_union![ |
31 | LITERAL_FIRST, | 31 | LITERAL_FIRST, |
32 | token_set![L_PAREN, PIPE, MOVE_KW, IF_KW, MATCH_KW, UNSAFE_KW, L_CURLY, RETURN_KW, | 32 | token_set![L_PAREN, PIPE, MOVE_KW, IF_KW, WHILE_KW, MATCH_KW, UNSAFE_KW, L_CURLY, RETURN_KW, |
33 | IDENT, SELF_KW, SUPER_KW, COLONCOLON ], | 33 | IDENT, SELF_KW, SUPER_KW, COLONCOLON ], |
34 | ]; | 34 | ]; |
35 | 35 | ||
@@ -47,6 +47,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<CompletedMark | |||
47 | PIPE => lambda_expr(p), | 47 | PIPE => lambda_expr(p), |
48 | MOVE_KW if la == PIPE => lambda_expr(p), | 48 | MOVE_KW if la == PIPE => lambda_expr(p), |
49 | IF_KW => if_expr(p), | 49 | IF_KW => if_expr(p), |
50 | WHILE_KW => while_expr(p), | ||
50 | MATCH_KW => match_expr(p), | 51 | MATCH_KW => match_expr(p), |
51 | UNSAFE_KW if la == L_CURLY => block_expr(p), | 52 | UNSAFE_KW if la == L_CURLY => block_expr(p), |
52 | L_CURLY => block_expr(p), | 53 | L_CURLY => block_expr(p), |
@@ -97,7 +98,8 @@ fn lambda_expr(p: &mut Parser) -> CompletedMarker { | |||
97 | fn if_expr(p: &mut Parser) -> CompletedMarker { | 98 | fn if_expr(p: &mut Parser) -> CompletedMarker { |
98 | assert!(p.at(IF_KW)); | 99 | assert!(p.at(IF_KW)); |
99 | let m = p.start(); | 100 | let m = p.start(); |
100 | if_head(p); | 101 | p.bump(); |
102 | cond(p); | ||
101 | block(p); | 103 | block(p); |
102 | if p.at(ELSE_KW) { | 104 | if p.at(ELSE_KW) { |
103 | p.bump(); | 105 | p.bump(); |
@@ -110,10 +112,28 @@ fn if_expr(p: &mut Parser) -> CompletedMarker { | |||
110 | m.complete(p, IF_EXPR) | 112 | m.complete(p, IF_EXPR) |
111 | } | 113 | } |
112 | 114 | ||
113 | fn if_head(p: &mut Parser) { | 115 | // test while_expr |
114 | assert!(p.at(IF_KW)); | 116 | // fn foo() { |
117 | // while true {}; | ||
118 | // while let Some(x) = it.next() {}; | ||
119 | // } | ||
120 | fn while_expr(p: &mut Parser) -> CompletedMarker { | ||
121 | assert!(p.at(WHILE_KW)); | ||
122 | let m = p.start(); | ||
115 | p.bump(); | 123 | p.bump(); |
116 | expr_no_struct(p); | 124 | cond(p); |
125 | block(p); | ||
126 | m.complete(p, WHILE_EXPR) | ||
127 | } | ||
128 | |||
129 | // test cond | ||
130 | // fn foo() { if let Some(_) = None {} } | ||
131 | fn cond(p: &mut Parser) { | ||
132 | if p.eat(LET_KW) { | ||
133 | patterns::pattern(p); | ||
134 | p.expect(EQ); | ||
135 | } | ||
136 | expr_no_struct(p) | ||
117 | } | 137 | } |
118 | 138 | ||
119 | // test match_expr | 139 | // test match_expr |
@@ -152,8 +172,8 @@ fn match_arm(p: &mut Parser) { | |||
152 | break; | 172 | break; |
153 | } | 173 | } |
154 | } | 174 | } |
155 | if p.at(IF_KW) { | 175 | if p.eat(IF_KW) { |
156 | if_head(p) | 176 | expr_no_struct(p); |
157 | } | 177 | } |
158 | p.expect(FAT_ARROW); | 178 | p.expect(FAT_ARROW); |
159 | expr(p); | 179 | expr(p); |