From d3c90ded2b9a4f75e101fa3abc60cd3aebc439c9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 17 Aug 2018 22:00:13 +0300 Subject: Borrowed AST --- crates/libsyntax2/src/ast/mod.rs | 61 ++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 27 deletions(-) (limited to 'crates/libsyntax2/src/ast/mod.rs') diff --git a/crates/libsyntax2/src/ast/mod.rs b/crates/libsyntax2/src/ast/mod.rs index b52230e9c..46509b5ec 100644 --- a/crates/libsyntax2/src/ast/mod.rs +++ b/crates/libsyntax2/src/ast/mod.rs @@ -4,22 +4,19 @@ use itertools::Itertools; use smol_str::SmolStr; use { - SyntaxNode, SyntaxNodeRef, OwnedRoot, TreeRoot, SyntaxError, + SyntaxNode, SyntaxNodeRef, TreeRoot, SyntaxError, SyntaxKind::*, }; pub use self::generated::*; -pub trait AstNode { - fn cast(syntax: SyntaxNode) -> Option +pub trait AstNode<'a>: Clone + Copy { + fn cast(syntax: SyntaxNodeRef<'a>) -> Option where Self: Sized; - fn syntax(&self) -> &SyntaxNode; - fn syntax_ref<'a>(&'a self) -> SyntaxNodeRef<'a> where R: 'a { - self.syntax().as_ref() - } + fn syntax(self) -> SyntaxNodeRef<'a>; } -pub trait NameOwner: AstNode { - fn name(&self) -> Option> { +pub trait NameOwner<'a>: AstNode<'a> { + fn name(self) -> Option> { self.syntax() .children() .filter_map(Name::cast) @@ -27,27 +24,37 @@ pub trait NameOwner: AstNode { } } -pub trait AttrsOwner: AstNode { - fn attrs<'a>(&'a self) -> Box> + 'a> where R: 'a { +pub trait AttrsOwner<'a>: AstNode<'a> { + fn attrs(&self) -> Box> + 'a> { let it = self.syntax().children() .filter_map(Attr::cast); Box::new(it) } } -impl File { - pub fn parse(text: &str) -> Self { - File::cast(::parse(text)).unwrap() - } +#[derive(Clone, Debug)] +pub struct ParsedFile { + root: SyntaxNode } -impl File { +impl ParsedFile { + pub fn parse(text: &str) -> Self { + let root = ::parse(text); + ParsedFile { root } + } + pub fn ast(&self) -> File { + File::cast(self.syntax()).unwrap() + } + pub fn syntax(&self) -> SyntaxNodeRef { + self.root.as_ref() + } pub fn errors(&self) -> Vec { self.syntax().root.syntax_root().errors.clone() } + } -impl FnDef { +impl<'a> FnDef<'a> { pub fn has_atom_attr(&self, atom: &str) -> bool { self.attrs() .filter_map(|x| x.as_atom()) @@ -55,7 +62,7 @@ impl FnDef { } } -impl Attr { +impl<'a> Attr<'a> { pub fn as_atom(&self) -> Option { let tt = self.value()?; let (_bra, attr, _ket) = tt.syntax().children().collect_tuple()?; @@ -66,7 +73,7 @@ impl Attr { } } - pub fn as_call(&self) -> Option<(SmolStr, TokenTree)> { + pub fn as_call(&self) -> Option<(SmolStr, TokenTree<'a>)> { let tt = self.value()?; let (_bra, attr, args, _ket) = tt.syntax().children().collect_tuple()?; let args = TokenTree::cast(args)?; @@ -78,7 +85,7 @@ impl Attr { } } -impl Name { +impl<'a> Name<'a> { pub fn text(&self) -> SmolStr { let ident = self.syntax().first_child() .unwrap(); @@ -86,7 +93,7 @@ impl Name { } } -impl NameRef { +impl<'a> NameRef<'a> { pub fn text(&self) -> SmolStr { let ident = self.syntax().first_child() .unwrap(); @@ -94,22 +101,22 @@ impl NameRef { } } -impl ImplItem { - pub fn target_type(&self) -> Option> { +impl<'a> ImplItem<'a> { + pub fn target_type(&self) -> Option> { match self.target() { (Some(t), None) | (_, Some(t)) => Some(t), _ => None, } } - pub fn target_trait(&self) -> Option> { + pub fn target_trait(&self) -> Option> { match self.target() { (Some(t), Some(_)) => Some(t), _ => None, } } - fn target(&self) -> (Option>, Option>) { + fn target(&self) -> (Option>, Option>) { let mut types = self.syntax().children().filter_map(TypeRef::cast); let first = types.next(); let second = types.next(); @@ -117,9 +124,9 @@ impl ImplItem { } } -impl Module { +impl<'a> Module<'a> { pub fn has_semi(&self) -> bool { - match self.syntax_ref().last_child() { + match self.syntax().last_child() { None => false, Some(node) => node.kind() == SEMI, } -- cgit v1.2.3