aboutsummaryrefslogtreecommitdiff
path: root/crates/libsyntax2/src/grammar
diff options
context:
space:
mode:
Diffstat (limited to 'crates/libsyntax2/src/grammar')
-rw-r--r--crates/libsyntax2/src/grammar/expressions/atom.rs61
-rw-r--r--crates/libsyntax2/src/grammar/mod.rs2
2 files changed, 46 insertions, 17 deletions
diff --git a/crates/libsyntax2/src/grammar/expressions/atom.rs b/crates/libsyntax2/src/grammar/expressions/atom.rs
index 9f470d561..9d98340af 100644
--- a/crates/libsyntax2/src/grammar/expressions/atom.rs
+++ b/crates/libsyntax2/src/grammar/expressions/atom.rs
@@ -30,7 +30,7 @@ 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, WHILE_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, BREAK_KW, CONTINUE_KW ], 33 IDENT, SELF_KW, SUPER_KW, COLONCOLON, BREAK_KW, CONTINUE_KW, LIFETIME ],
34 ]; 34 ];
35 35
36pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<CompletedMarker> { 36pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<CompletedMarker> {
@@ -48,9 +48,24 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<CompletedMark
48 PIPE => lambda_expr(p), 48 PIPE => lambda_expr(p),
49 MOVE_KW if la == PIPE => lambda_expr(p), 49 MOVE_KW if la == PIPE => lambda_expr(p),
50 IF_KW => if_expr(p), 50 IF_KW => if_expr(p),
51 WHILE_KW => while_expr(p), 51
52 LOOP_KW => loop_expr(p), 52 LOOP_KW => loop_expr(p, None),
53 FOR_KW => for_expr(p), 53 FOR_KW => for_expr(p, None),
54 WHILE_KW => while_expr(p, None),
55 LIFETIME if la == COLON => {
56 let m = p.start();
57 label(p);
58 match p.current() {
59 LOOP_KW => loop_expr(p, Some(m)),
60 FOR_KW => for_expr(p, Some(m)),
61 WHILE_KW => while_expr(p, Some(m)),
62 _ => {
63 p.error("expected a loop");
64 return None;
65 }
66 }
67 }
68
54 MATCH_KW => match_expr(p), 69 MATCH_KW => match_expr(p),
55 UNSAFE_KW if la == L_CURLY => block_expr(p), 70 UNSAFE_KW if la == L_CURLY => block_expr(p),
56 L_CURLY => block_expr(p), 71 L_CURLY => block_expr(p),
@@ -164,39 +179,53 @@ fn if_expr(p: &mut Parser) -> CompletedMarker {
164 m.complete(p, IF_EXPR) 179 m.complete(p, IF_EXPR)
165} 180}
166 181
167// test while_expr 182// test label
168// fn foo() { 183// fn foo() {
169// while true {}; 184// 'a: loop {}
170// while let Some(x) = it.next() {}; 185// 'b: while true {}
186// 'c: for x in () {}
171// } 187// }
172fn while_expr(p: &mut Parser) -> CompletedMarker { 188fn label(p: &mut Parser) {
173 assert!(p.at(WHILE_KW)); 189 assert!(p.at(LIFETIME) && p.nth(1) == COLON);
174 let m = p.start(); 190 let m = p.start();
175 p.bump(); 191 p.bump();
176 cond(p); 192 p.bump();
177 block(p); 193 m.complete(p, LABEL);
178 m.complete(p, WHILE_EXPR)
179} 194}
180 195
181// test loop_expr 196// test loop_expr
182// fn foo() { 197// fn foo() {
183// loop {}; 198// loop {};
184// } 199// }
185fn loop_expr(p: &mut Parser) -> CompletedMarker { 200fn loop_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
186 assert!(p.at(LOOP_KW)); 201 assert!(p.at(LOOP_KW));
187 let m = p.start(); 202 let m = m.unwrap_or_else(|| p.start());
188 p.bump(); 203 p.bump();
189 block(p); 204 block(p);
190 m.complete(p, LOOP_EXPR) 205 m.complete(p, LOOP_EXPR)
191} 206}
192 207
208// test while_expr
209// fn foo() {
210// while true {};
211// while let Some(x) = it.next() {};
212// }
213fn while_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
214 assert!(p.at(WHILE_KW));
215 let m = m.unwrap_or_else(|| p.start());
216 p.bump();
217 cond(p);
218 block(p);
219 m.complete(p, WHILE_EXPR)
220}
221
193// test for_expr 222// test for_expr
194// fn foo() { 223// fn foo() {
195// for x in [] {}; 224// for x in [] {};
196// } 225// }
197fn for_expr(p: &mut Parser) -> CompletedMarker { 226fn for_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
198 assert!(p.at(FOR_KW)); 227 assert!(p.at(FOR_KW));
199 let m = p.start(); 228 let m = m.unwrap_or_else(|| p.start());
200 p.bump(); 229 p.bump();
201 patterns::pattern(p); 230 patterns::pattern(p);
202 p.expect(IN_KW); 231 p.expect(IN_KW);
diff --git a/crates/libsyntax2/src/grammar/mod.rs b/crates/libsyntax2/src/grammar/mod.rs
index 0f168eb60..25887921b 100644
--- a/crates/libsyntax2/src/grammar/mod.rs
+++ b/crates/libsyntax2/src/grammar/mod.rs
@@ -32,7 +32,7 @@ mod type_params;
32mod types; 32mod types;
33 33
34use { 34use {
35 parser_api::{CompletedMarker, Parser, TokenSet}, 35 parser_api::{Marker, CompletedMarker, Parser, TokenSet},
36 SyntaxKind::{self, *}, 36 SyntaxKind::{self, *},
37}; 37};
38 38