From c16530c988e817c5596fa38ebe9e12a302886a8f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 27 Aug 2018 10:12:28 +0300 Subject: visitor-less scopes --- crates/libsyntax2/src/ast/generated.rs | 31 ++++++++++++++++++++++++++++++- crates/libsyntax2/src/grammar.ron | 6 +++++- 2 files changed, 35 insertions(+), 2 deletions(-) (limited to 'crates/libsyntax2') diff --git a/crates/libsyntax2/src/ast/generated.rs b/crates/libsyntax2/src/ast/generated.rs index b937fe5a2..1dd161f52 100644 --- a/crates/libsyntax2/src/ast/generated.rs +++ b/crates/libsyntax2/src/ast/generated.rs @@ -116,9 +116,13 @@ impl<'a> AstNode<'a> for Block<'a> { } impl<'a> Block<'a> { - pub fn let_stmts(self) -> impl Iterator> + 'a { + pub fn statements(self) -> impl Iterator> + 'a { super::children(self) } + + pub fn expr(self) -> Option> { + super::child_opt(self) + } } // BlockExpr @@ -1370,6 +1374,31 @@ impl<'a> ast::TypeParamsOwner<'a> for StaticDef<'a> {} impl<'a> ast::AttrsOwner<'a> for StaticDef<'a> {} impl<'a> StaticDef<'a> {} +// Stmt +#[derive(Debug, Clone, Copy)] +pub enum Stmt<'a> { + ExprStmt(ExprStmt<'a>), + LetStmt(LetStmt<'a>), +} + +impl<'a> AstNode<'a> for Stmt<'a> { + fn cast(syntax: SyntaxNodeRef<'a>) -> Option { + match syntax.kind() { + EXPR_STMT => Some(Stmt::ExprStmt(ExprStmt { syntax })), + LET_STMT => Some(Stmt::LetStmt(LetStmt { syntax })), + _ => None, + } + } + fn syntax(self) -> SyntaxNodeRef<'a> { + match self { + Stmt::ExprStmt(inner) => inner.syntax(), + Stmt::LetStmt(inner) => inner.syntax(), + } + } +} + +impl<'a> Stmt<'a> {} + // StructDef #[derive(Debug, Clone, Copy)] pub struct StructDef<'a> { diff --git a/crates/libsyntax2/src/grammar.ron b/crates/libsyntax2/src/grammar.ron index aa2742b3e..f3c3d3036 100644 --- a/crates/libsyntax2/src/grammar.ron +++ b/crates/libsyntax2/src/grammar.ron @@ -437,9 +437,13 @@ Grammar( ["pat", "Pat"], ["initializer", "Expr"], ]), + "Stmt": ( + enum: ["ExprStmt", "LetStmt"], + ), "Block": ( + options: [["expr", "Expr"]], collections: [ - ["let_stmts", "LetStmt"], + ["statements", "Stmt"], ] ), "ParamList": ( -- cgit v1.2.3