From 07cbb7d73deed8dac3eecdbdc7e1eaf6938a6cd6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 27 Aug 2018 12:22:09 +0300 Subject: Support if-let in scopes --- crates/libsyntax2/src/ast/generated.rs | 66 ++++++++++++++++++++++++++++++---- crates/libsyntax2/src/ast/mod.rs | 12 +++++++ 2 files changed, 72 insertions(+), 6 deletions(-) (limited to 'crates/libsyntax2/src/ast') 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> { fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } } -impl<'a> BlockExpr<'a> {} +impl<'a> BlockExpr<'a> { + pub fn block(self) -> Option> { + super::child_opt(self) + } +} // BreakExpr #[derive(Debug, Clone, Copy)] @@ -197,6 +201,32 @@ impl<'a> AstNode<'a> for CastExpr<'a> { impl<'a> CastExpr<'a> {} +// Condition +#[derive(Debug, Clone, Copy)] +pub struct Condition<'a> { + syntax: SyntaxNodeRef<'a>, +} + +impl<'a> AstNode<'a> for Condition<'a> { + fn cast(syntax: SyntaxNodeRef<'a>) -> Option { + match syntax.kind() { + CONDITION => Some(Condition { syntax }), + _ => None, + } + } + fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } +} + +impl<'a> Condition<'a> { + pub fn pat(self) -> Option> { + super::child_opt(self) + } + + pub fn expr(self) -> Option> { + super::child_opt(self) + } +} + // ConstDef #[derive(Debug, Clone, Copy)] pub struct ConstDef<'a> { @@ -403,7 +433,11 @@ impl<'a> AstNode<'a> for ExprStmt<'a> { fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } } -impl<'a> ExprStmt<'a> {} +impl<'a> ExprStmt<'a> { + pub fn expr(self) -> Option> { + super::child_opt(self) + } +} // FieldExpr #[derive(Debug, Clone, Copy)] @@ -504,7 +538,11 @@ impl<'a> AstNode<'a> for ForExpr<'a> { fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } } -impl<'a> ForExpr<'a> {} +impl<'a> ForExpr<'a> { + pub fn body(self) -> Option> { + super::child_opt(self) + } +} // ForType #[derive(Debug, Clone, Copy)] @@ -540,7 +578,11 @@ impl<'a> AstNode<'a> for IfExpr<'a> { fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } } -impl<'a> IfExpr<'a> {} +impl<'a> IfExpr<'a> { + pub fn condition(self) -> Option> { + super::child_opt(self) + } +} // ImplItem #[derive(Debug, Clone, Copy)] @@ -674,7 +716,11 @@ impl<'a> AstNode<'a> for LoopExpr<'a> { fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } } -impl<'a> LoopExpr<'a> {} +impl<'a> LoopExpr<'a> { + pub fn body(self) -> Option> { + super::child_opt(self) + } +} // MatchArm #[derive(Debug, Clone, Copy)] @@ -1742,5 +1788,13 @@ impl<'a> AstNode<'a> for WhileExpr<'a> { fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } } -impl<'a> WhileExpr<'a> {} +impl<'a> WhileExpr<'a> { + pub fn condition(self) -> Option> { + super::child_opt(self) + } + + pub fn body(self) -> Option> { + super::child_opt(self) + } +} 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> { } } +impl<'a> IfExpr<'a> { + pub fn then_branch(self) -> Option> { + self.blocks().nth(0) + } + pub fn else_branch(self) -> Option> { + self.blocks().nth(1) + } + fn blocks(self) -> impl Iterator> { + children(self) + } +} + fn child_opt<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> Option { children(parent).next() } -- cgit v1.2.3