From 2fa90e736b026ee979d9eb59178dc1f792228250 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 28 Aug 2018 11:12:42 +0300 Subject: better recovery for exprs --- crates/libsyntax2/src/ast/generated.rs | 78 +++++++++++++++++++++++++++++++++- crates/libsyntax2/src/ast/mod.rs | 9 ++++ 2 files changed, 86 insertions(+), 1 deletion(-) (limited to 'crates/libsyntax2/src/ast') diff --git a/crates/libsyntax2/src/ast/generated.rs b/crates/libsyntax2/src/ast/generated.rs index f99d1274a..6181aada8 100644 --- a/crates/libsyntax2/src/ast/generated.rs +++ b/crates/libsyntax2/src/ast/generated.rs @@ -439,6 +439,24 @@ impl<'a> ExprStmt<'a> { } } +// ExternCrateItem +#[derive(Debug, Clone, Copy)] +pub struct ExternCrateItem<'a> { + syntax: SyntaxNodeRef<'a>, +} + +impl<'a> AstNode<'a> for ExternCrateItem<'a> { + fn cast(syntax: SyntaxNodeRef<'a>) -> Option { + match syntax.kind() { + EXTERN_CRATE_ITEM => Some(ExternCrateItem { syntax }), + _ => None, + } + } + fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } +} + +impl<'a> ExternCrateItem<'a> {} + // FieldExpr #[derive(Debug, Clone, Copy)] pub struct FieldExpr<'a> { @@ -839,11 +857,51 @@ impl<'a> AstNode<'a> for Module<'a> { impl<'a> ast::NameOwner<'a> for Module<'a> {} impl<'a> ast::AttrsOwner<'a> for Module<'a> {} impl<'a> Module<'a> { - pub fn modules(self) -> impl Iterator> + 'a { + pub fn items(self) -> impl Iterator> + 'a { super::children(self) } } +// ModuleItem +#[derive(Debug, Clone, Copy)] +pub enum ModuleItem<'a> { + StructDef(StructDef<'a>), + EnumDef(EnumDef<'a>), + FnDef(FnDef<'a>), + TraitDef(TraitDef<'a>), + ImplItem(ImplItem<'a>), + UseItem(UseItem<'a>), + ExternCrateItem(ExternCrateItem<'a>), +} + +impl<'a> AstNode<'a> for ModuleItem<'a> { + fn cast(syntax: SyntaxNodeRef<'a>) -> Option { + match syntax.kind() { + STRUCT_DEF => Some(ModuleItem::StructDef(StructDef { syntax })), + ENUM_DEF => Some(ModuleItem::EnumDef(EnumDef { syntax })), + FN_DEF => Some(ModuleItem::FnDef(FnDef { syntax })), + TRAIT_DEF => Some(ModuleItem::TraitDef(TraitDef { syntax })), + IMPL_ITEM => Some(ModuleItem::ImplItem(ImplItem { syntax })), + USE_ITEM => Some(ModuleItem::UseItem(UseItem { syntax })), + EXTERN_CRATE_ITEM => Some(ModuleItem::ExternCrateItem(ExternCrateItem { syntax })), + _ => None, + } + } + fn syntax(self) -> SyntaxNodeRef<'a> { + match self { + ModuleItem::StructDef(inner) => inner.syntax(), + ModuleItem::EnumDef(inner) => inner.syntax(), + ModuleItem::FnDef(inner) => inner.syntax(), + ModuleItem::TraitDef(inner) => inner.syntax(), + ModuleItem::ImplItem(inner) => inner.syntax(), + ModuleItem::UseItem(inner) => inner.syntax(), + ModuleItem::ExternCrateItem(inner) => inner.syntax(), + } + } +} + +impl<'a> ModuleItem<'a> {} + // Name #[derive(Debug, Clone, Copy)] pub struct Name<'a> { @@ -1762,6 +1820,24 @@ impl<'a> AstNode<'a> for TypeRef<'a> { impl<'a> TypeRef<'a> {} +// UseItem +#[derive(Debug, Clone, Copy)] +pub struct UseItem<'a> { + syntax: SyntaxNodeRef<'a>, +} + +impl<'a> AstNode<'a> for UseItem<'a> { + fn cast(syntax: SyntaxNodeRef<'a>) -> Option { + match syntax.kind() { + USE_ITEM => Some(UseItem { syntax }), + _ => None, + } + } + fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } +} + +impl<'a> UseItem<'a> {} + // WhereClause #[derive(Debug, Clone, Copy)] pub struct WhereClause<'a> { diff --git a/crates/libsyntax2/src/ast/mod.rs b/crates/libsyntax2/src/ast/mod.rs index 2ebee6a4f..9941138a7 100644 --- a/crates/libsyntax2/src/ast/mod.rs +++ b/crates/libsyntax2/src/ast/mod.rs @@ -115,6 +115,15 @@ impl<'a> Module<'a> { } } +impl<'a> LetStmt<'a> { + pub fn has_semi(self) -> bool { + match self.syntax().last_child() { + None => false, + Some(node) => node.kind() == SEMI, + } + } +} + impl<'a> IfExpr<'a> { pub fn then_branch(self) -> Option> { self.blocks().nth(0) -- cgit v1.2.3