aboutsummaryrefslogtreecommitdiff
path: root/crates/libsyntax2/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-27 10:22:09 +0100
committerAleksey Kladov <[email protected]>2018-08-27 10:22:09 +0100
commit07cbb7d73deed8dac3eecdbdc7e1eaf6938a6cd6 (patch)
tree4a1fa22fa8c908f0c3c9489a98aa2479f05def59 /crates/libsyntax2/src
parentc16530c988e817c5596fa38ebe9e12a302886a8f (diff)
Support if-let in scopes
Diffstat (limited to 'crates/libsyntax2/src')
-rw-r--r--crates/libsyntax2/src/ast/generated.rs66
-rw-r--r--crates/libsyntax2/src/ast/mod.rs12
-rw-r--r--crates/libsyntax2/src/grammar.ron36
-rw-r--r--crates/libsyntax2/src/grammar/expressions/atom.rs4
-rw-r--r--crates/libsyntax2/src/syntax_kinds/generated.rs6
5 files changed, 108 insertions, 16 deletions
diff --git a/crates/libsyntax2/src/ast/generated.rs b/crates/libsyntax2/src/ast/generated.rs
index 1dd161f52..6891e857c 100644
--- a/crates/libsyntax2/src/ast/generated.rs
+++ b/crates/libsyntax2/src/ast/generated.rs
@@ -141,7 +141,11 @@ impl<'a> AstNode<'a> for BlockExpr<'a> {
141 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } 141 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
142} 142}
143 143
144impl<'a> BlockExpr<'a> {} 144impl<'a> BlockExpr<'a> {
145 pub fn block(self) -> Option<Block<'a>> {
146 super::child_opt(self)
147 }
148}
145 149
146// BreakExpr 150// BreakExpr
147#[derive(Debug, Clone, Copy)] 151#[derive(Debug, Clone, Copy)]
@@ -197,6 +201,32 @@ impl<'a> AstNode<'a> for CastExpr<'a> {
197 201
198impl<'a> CastExpr<'a> {} 202impl<'a> CastExpr<'a> {}
199 203
204// Condition
205#[derive(Debug, Clone, Copy)]
206pub struct Condition<'a> {
207 syntax: SyntaxNodeRef<'a>,
208}
209
210impl<'a> AstNode<'a> for Condition<'a> {
211 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
212 match syntax.kind() {
213 CONDITION => Some(Condition { syntax }),
214 _ => None,
215 }
216 }
217 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
218}
219
220impl<'a> Condition<'a> {
221 pub fn pat(self) -> Option<Pat<'a>> {
222 super::child_opt(self)
223 }
224
225 pub fn expr(self) -> Option<Expr<'a>> {
226 super::child_opt(self)
227 }
228}
229
200// ConstDef 230// ConstDef
201#[derive(Debug, Clone, Copy)] 231#[derive(Debug, Clone, Copy)]
202pub struct ConstDef<'a> { 232pub struct ConstDef<'a> {
@@ -403,7 +433,11 @@ impl<'a> AstNode<'a> for ExprStmt<'a> {
403 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } 433 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
404} 434}
405 435
406impl<'a> ExprStmt<'a> {} 436impl<'a> ExprStmt<'a> {
437 pub fn expr(self) -> Option<Expr<'a>> {
438 super::child_opt(self)
439 }
440}
407 441
408// FieldExpr 442// FieldExpr
409#[derive(Debug, Clone, Copy)] 443#[derive(Debug, Clone, Copy)]
@@ -504,7 +538,11 @@ impl<'a> AstNode<'a> for ForExpr<'a> {
504 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } 538 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
505} 539}
506 540
507impl<'a> ForExpr<'a> {} 541impl<'a> ForExpr<'a> {
542 pub fn body(self) -> Option<Block<'a>> {
543 super::child_opt(self)
544 }
545}
508 546
509// ForType 547// ForType
510#[derive(Debug, Clone, Copy)] 548#[derive(Debug, Clone, Copy)]
@@ -540,7 +578,11 @@ impl<'a> AstNode<'a> for IfExpr<'a> {
540 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } 578 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
541} 579}
542 580
543impl<'a> IfExpr<'a> {} 581impl<'a> IfExpr<'a> {
582 pub fn condition(self) -> Option<Condition<'a>> {
583 super::child_opt(self)
584 }
585}
544 586
545// ImplItem 587// ImplItem
546#[derive(Debug, Clone, Copy)] 588#[derive(Debug, Clone, Copy)]
@@ -674,7 +716,11 @@ impl<'a> AstNode<'a> for LoopExpr<'a> {
674 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } 716 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
675} 717}
676 718
677impl<'a> LoopExpr<'a> {} 719impl<'a> LoopExpr<'a> {
720 pub fn body(self) -> Option<Block<'a>> {
721 super::child_opt(self)
722 }
723}
678 724
679// MatchArm 725// MatchArm
680#[derive(Debug, Clone, Copy)] 726#[derive(Debug, Clone, Copy)]
@@ -1742,5 +1788,13 @@ impl<'a> AstNode<'a> for WhileExpr<'a> {
1742 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } 1788 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
1743} 1789}
1744 1790
1745impl<'a> WhileExpr<'a> {} 1791impl<'a> WhileExpr<'a> {
1792 pub fn condition(self) -> Option<Condition<'a>> {
1793 super::child_opt(self)
1794 }
1795
1796 pub fn body(self) -> Option<Block<'a>> {
1797 super::child_opt(self)
1798 }
1799}
1746 1800
diff --git a/crates/libsyntax2/src/ast/mod.rs b/crates/libsyntax2/src/ast/mod.rs
index 6217c5b74..2ebee6a4f 100644
--- a/crates/libsyntax2/src/ast/mod.rs
+++ b/crates/libsyntax2/src/ast/mod.rs
@@ -115,6 +115,18 @@ impl<'a> Module<'a> {
115 } 115 }
116} 116}
117 117
118impl<'a> IfExpr<'a> {
119 pub fn then_branch(self) -> Option<Block<'a>> {
120 self.blocks().nth(0)
121 }
122 pub fn else_branch(self) -> Option<Block<'a>> {
123 self.blocks().nth(1)
124 }
125 fn blocks(self) -> impl Iterator<Item=Block<'a>> {
126 children(self)
127 }
128}
129
118fn child_opt<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> Option<C> { 130fn child_opt<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> Option<C> {
119 children(parent).next() 131 children(parent).next()
120} 132}
diff --git a/crates/libsyntax2/src/grammar.ron b/crates/libsyntax2/src/grammar.ron
index f3c3d3036..c9e128462 100644
--- a/crates/libsyntax2/src/grammar.ron
+++ b/crates/libsyntax2/src/grammar.ron
@@ -162,9 +162,10 @@ Grammar(
162 "PATH_EXPR", 162 "PATH_EXPR",
163 "LAMBDA_EXPR", 163 "LAMBDA_EXPR",
164 "IF_EXPR", 164 "IF_EXPR",
165 "WHILE_EXPR",
166 "CONDITION",
165 "LOOP_EXPR", 167 "LOOP_EXPR",
166 "FOR_EXPR", 168 "FOR_EXPR",
167 "WHILE_EXPR",
168 "CONTINUE_EXPR", 169 "CONTINUE_EXPR",
169 "BREAK_EXPR", 170 "BREAK_EXPR",
170 "LABEL", 171 "LABEL",
@@ -336,14 +337,27 @@ Grammar(
336 "ParenExpr": (), 337 "ParenExpr": (),
337 "PathExpr": (), 338 "PathExpr": (),
338 "LambdaExpr": (), 339 "LambdaExpr": (),
339 "IfExpr": (), 340 "IfExpr": (
340 "LoopExpr": (), 341 options: [ ["condition", "Condition"] ]
341 "ForExpr": (), 342 ),
342 "WhileExpr": (), 343 "LoopExpr": (
344 options: [ ["body", "Block"] ]
345 ),
346 "ForExpr": (
347 options: [ ["body", "Block"] ]
348 ),
349 "WhileExpr": (
350 options: [
351 ["condition", "Condition"],
352 ["body", "Block"],
353 ]
354 ),
343 "ContinueExpr": (), 355 "ContinueExpr": (),
344 "BreakExpr": (), 356 "BreakExpr": (),
345 "Label": (), 357 "Label": (),
346 "BlockExpr": (), 358 "BlockExpr": (
359 options: [ ["block", "Block"] ]
360 ),
347 "ReturnExpr": (), 361 "ReturnExpr": (),
348 "MatchExpr": (), 362 "MatchExpr": (),
349 "MatchArmList": (), 363 "MatchArmList": (),
@@ -432,11 +446,19 @@ Grammar(
432 "TypeParamList": ( collections: [ ["type_params", "TypeParam" ] ]), 446 "TypeParamList": ( collections: [ ["type_params", "TypeParam" ] ]),
433 "TypeParam": ( traits: ["NameOwner"] ), 447 "TypeParam": ( traits: ["NameOwner"] ),
434 "WhereClause": (), 448 "WhereClause": (),
435 "ExprStmt": (), 449 "ExprStmt": (
450 options: [ ["expr", "Expr"] ]
451 ),
436 "LetStmt": ( options: [ 452 "LetStmt": ( options: [
437 ["pat", "Pat"], 453 ["pat", "Pat"],
438 ["initializer", "Expr"], 454 ["initializer", "Expr"],
439 ]), 455 ]),
456 "Condition": (
457 options: [
458 [ "pat", "Pat" ],
459 [ "expr", "Expr" ],
460 ]
461 ),
440 "Stmt": ( 462 "Stmt": (
441 enum: ["ExprStmt", "LetStmt"], 463 enum: ["ExprStmt", "LetStmt"],
442 ), 464 ),
diff --git a/crates/libsyntax2/src/grammar/expressions/atom.rs b/crates/libsyntax2/src/grammar/expressions/atom.rs
index d9c3f998a..bb5402af7 100644
--- a/crates/libsyntax2/src/grammar/expressions/atom.rs
+++ b/crates/libsyntax2/src/grammar/expressions/atom.rs
@@ -237,11 +237,13 @@ fn for_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
237// test cond 237// test cond
238// fn foo() { if let Some(_) = None {} } 238// fn foo() { if let Some(_) = None {} }
239fn cond(p: &mut Parser) { 239fn cond(p: &mut Parser) {
240 let m = p.start();
240 if p.eat(LET_KW) { 241 if p.eat(LET_KW) {
241 patterns::pattern(p); 242 patterns::pattern(p);
242 p.expect(EQ); 243 p.expect(EQ);
243 } 244 }
244 expr_no_struct(p) 245 expr_no_struct(p);
246 m.complete(p, CONDITION);
245} 247}
246 248
247// test match_expr 249// test match_expr
diff --git a/crates/libsyntax2/src/syntax_kinds/generated.rs b/crates/libsyntax2/src/syntax_kinds/generated.rs
index 6a24cb19e..0a22b11c2 100644
--- a/crates/libsyntax2/src/syntax_kinds/generated.rs
+++ b/crates/libsyntax2/src/syntax_kinds/generated.rs
@@ -158,9 +158,10 @@ pub enum SyntaxKind {
158 PATH_EXPR, 158 PATH_EXPR,
159 LAMBDA_EXPR, 159 LAMBDA_EXPR,
160 IF_EXPR, 160 IF_EXPR,
161 WHILE_EXPR,
162 CONDITION,
161 LOOP_EXPR, 163 LOOP_EXPR,
162 FOR_EXPR, 164 FOR_EXPR,
163 WHILE_EXPR,
164 CONTINUE_EXPR, 165 CONTINUE_EXPR,
165 BREAK_EXPR, 166 BREAK_EXPR,
166 LABEL, 167 LABEL,
@@ -418,9 +419,10 @@ impl SyntaxKind {
418 PATH_EXPR => &SyntaxInfo { name: "PATH_EXPR" }, 419 PATH_EXPR => &SyntaxInfo { name: "PATH_EXPR" },
419 LAMBDA_EXPR => &SyntaxInfo { name: "LAMBDA_EXPR" }, 420 LAMBDA_EXPR => &SyntaxInfo { name: "LAMBDA_EXPR" },
420 IF_EXPR => &SyntaxInfo { name: "IF_EXPR" }, 421 IF_EXPR => &SyntaxInfo { name: "IF_EXPR" },
422 WHILE_EXPR => &SyntaxInfo { name: "WHILE_EXPR" },
423 CONDITION => &SyntaxInfo { name: "CONDITION" },
421 LOOP_EXPR => &SyntaxInfo { name: "LOOP_EXPR" }, 424 LOOP_EXPR => &SyntaxInfo { name: "LOOP_EXPR" },
422 FOR_EXPR => &SyntaxInfo { name: "FOR_EXPR" }, 425 FOR_EXPR => &SyntaxInfo { name: "FOR_EXPR" },
423 WHILE_EXPR => &SyntaxInfo { name: "WHILE_EXPR" },
424 CONTINUE_EXPR => &SyntaxInfo { name: "CONTINUE_EXPR" }, 426 CONTINUE_EXPR => &SyntaxInfo { name: "CONTINUE_EXPR" },
425 BREAK_EXPR => &SyntaxInfo { name: "BREAK_EXPR" }, 427 BREAK_EXPR => &SyntaxInfo { name: "BREAK_EXPR" },
426 LABEL => &SyntaxInfo { name: "LABEL" }, 428 LABEL => &SyntaxInfo { name: "LABEL" },