aboutsummaryrefslogtreecommitdiff
path: root/crates/libsyntax2/src/ast
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/ast
parentc16530c988e817c5596fa38ebe9e12a302886a8f (diff)
Support if-let in scopes
Diffstat (limited to 'crates/libsyntax2/src/ast')
-rw-r--r--crates/libsyntax2/src/ast/generated.rs66
-rw-r--r--crates/libsyntax2/src/ast/mod.rs12
2 files changed, 72 insertions, 6 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}