aboutsummaryrefslogtreecommitdiff
path: root/crates/libsyntax2/src/ast/mod.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-17 20:00:13 +0100
committerAleksey Kladov <[email protected]>2018-08-17 20:00:13 +0100
commitd3c90ded2b9a4f75e101fa3abc60cd3aebc439c9 (patch)
tree6d2388eb68605331a0dd090269372bc98dd038cd /crates/libsyntax2/src/ast/mod.rs
parent70097504f78c4c41368a0b864a94df95fb9c27f7 (diff)
Borrowed AST
Diffstat (limited to 'crates/libsyntax2/src/ast/mod.rs')
-rw-r--r--crates/libsyntax2/src/ast/mod.rs61
1 files changed, 34 insertions, 27 deletions
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;
4use smol_str::SmolStr; 4use smol_str::SmolStr;
5 5
6use { 6use {
7 SyntaxNode, SyntaxNodeRef, OwnedRoot, TreeRoot, SyntaxError, 7 SyntaxNode, SyntaxNodeRef, TreeRoot, SyntaxError,
8 SyntaxKind::*, 8 SyntaxKind::*,
9}; 9};
10pub use self::generated::*; 10pub use self::generated::*;
11 11
12pub trait AstNode<R: TreeRoot> { 12pub trait AstNode<'a>: Clone + Copy {
13 fn cast(syntax: SyntaxNode<R>) -> Option<Self> 13 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self>
14 where Self: Sized; 14 where Self: Sized;
15 fn syntax(&self) -> &SyntaxNode<R>; 15 fn syntax(self) -> SyntaxNodeRef<'a>;
16 fn syntax_ref<'a>(&'a self) -> SyntaxNodeRef<'a> where R: 'a {
17 self.syntax().as_ref()
18 }
19} 16}
20 17
21pub trait NameOwner<R: TreeRoot>: AstNode<R> { 18pub trait NameOwner<'a>: AstNode<'a> {
22 fn name(&self) -> Option<Name<R>> { 19 fn name(self) -> Option<Name<'a>> {
23 self.syntax() 20 self.syntax()
24 .children() 21 .children()
25 .filter_map(Name::cast) 22 .filter_map(Name::cast)
@@ -27,27 +24,37 @@ pub trait NameOwner<R: TreeRoot>: AstNode<R> {
27 } 24 }
28} 25}
29 26
30pub trait AttrsOwner<R: TreeRoot>: AstNode<R> { 27pub trait AttrsOwner<'a>: AstNode<'a> {
31 fn attrs<'a>(&'a self) -> Box<Iterator<Item=Attr<R>> + 'a> where R: 'a { 28 fn attrs(&self) -> Box<Iterator<Item=Attr<'a>> + 'a> {
32 let it = self.syntax().children() 29 let it = self.syntax().children()
33 .filter_map(Attr::cast); 30 .filter_map(Attr::cast);
34 Box::new(it) 31 Box::new(it)
35 } 32 }
36} 33}
37 34
38impl File<OwnedRoot> { 35#[derive(Clone, Debug)]
39 pub fn parse(text: &str) -> Self { 36pub struct ParsedFile {
40 File::cast(::parse(text)).unwrap() 37 root: SyntaxNode
41 }
42} 38}
43 39
44impl<R: TreeRoot> File<R> { 40impl ParsedFile {
41 pub fn parse(text: &str) -> Self {
42 let root = ::parse(text);
43 ParsedFile { root }
44 }
45 pub fn ast(&self) -> File {
46 File::cast(self.syntax()).unwrap()
47 }
48 pub fn syntax(&self) -> SyntaxNodeRef {
49 self.root.as_ref()
50 }
45 pub fn errors(&self) -> Vec<SyntaxError> { 51 pub fn errors(&self) -> Vec<SyntaxError> {
46 self.syntax().root.syntax_root().errors.clone() 52 self.syntax().root.syntax_root().errors.clone()
47 } 53 }
54
48} 55}
49 56
50impl<R: TreeRoot> FnDef<R> { 57impl<'a> FnDef<'a> {
51 pub fn has_atom_attr(&self, atom: &str) -> bool { 58 pub fn has_atom_attr(&self, atom: &str) -> bool {
52 self.attrs() 59 self.attrs()
53 .filter_map(|x| x.as_atom()) 60 .filter_map(|x| x.as_atom())
@@ -55,7 +62,7 @@ impl<R: TreeRoot> FnDef<R> {
55 } 62 }
56} 63}
57 64
58impl<R: TreeRoot> Attr<R> { 65impl<'a> Attr<'a> {
59 pub fn as_atom(&self) -> Option<SmolStr> { 66 pub fn as_atom(&self) -> Option<SmolStr> {
60 let tt = self.value()?; 67 let tt = self.value()?;
61 let (_bra, attr, _ket) = tt.syntax().children().collect_tuple()?; 68 let (_bra, attr, _ket) = tt.syntax().children().collect_tuple()?;
@@ -66,7 +73,7 @@ impl<R: TreeRoot> Attr<R> {
66 } 73 }
67 } 74 }
68 75
69 pub fn as_call(&self) -> Option<(SmolStr, TokenTree<R>)> { 76 pub fn as_call(&self) -> Option<(SmolStr, TokenTree<'a>)> {
70 let tt = self.value()?; 77 let tt = self.value()?;
71 let (_bra, attr, args, _ket) = tt.syntax().children().collect_tuple()?; 78 let (_bra, attr, args, _ket) = tt.syntax().children().collect_tuple()?;
72 let args = TokenTree::cast(args)?; 79 let args = TokenTree::cast(args)?;
@@ -78,7 +85,7 @@ impl<R: TreeRoot> Attr<R> {
78 } 85 }
79} 86}
80 87
81impl<R: TreeRoot> Name<R> { 88impl<'a> Name<'a> {
82 pub fn text(&self) -> SmolStr { 89 pub fn text(&self) -> SmolStr {
83 let ident = self.syntax().first_child() 90 let ident = self.syntax().first_child()
84 .unwrap(); 91 .unwrap();
@@ -86,7 +93,7 @@ impl<R: TreeRoot> Name<R> {
86 } 93 }
87} 94}
88 95
89impl<R: TreeRoot> NameRef<R> { 96impl<'a> NameRef<'a> {
90 pub fn text(&self) -> SmolStr { 97 pub fn text(&self) -> SmolStr {
91 let ident = self.syntax().first_child() 98 let ident = self.syntax().first_child()
92 .unwrap(); 99 .unwrap();
@@ -94,22 +101,22 @@ impl<R: TreeRoot> NameRef<R> {
94 } 101 }
95} 102}
96 103
97impl <R: TreeRoot> ImplItem<R> { 104impl<'a> ImplItem<'a> {
98 pub fn target_type(&self) -> Option<TypeRef<R>> { 105 pub fn target_type(&self) -> Option<TypeRef<'a>> {
99 match self.target() { 106 match self.target() {
100 (Some(t), None) | (_, Some(t)) => Some(t), 107 (Some(t), None) | (_, Some(t)) => Some(t),
101 _ => None, 108 _ => None,
102 } 109 }
103 } 110 }
104 111
105 pub fn target_trait(&self) -> Option<TypeRef<R>> { 112 pub fn target_trait(&self) -> Option<TypeRef<'a>> {
106 match self.target() { 113 match self.target() {
107 (Some(t), Some(_)) => Some(t), 114 (Some(t), Some(_)) => Some(t),
108 _ => None, 115 _ => None,
109 } 116 }
110 } 117 }
111 118
112 fn target(&self) -> (Option<TypeRef<R>>, Option<TypeRef<R>>) { 119 fn target(&self) -> (Option<TypeRef<'a>>, Option<TypeRef<'a>>) {
113 let mut types = self.syntax().children().filter_map(TypeRef::cast); 120 let mut types = self.syntax().children().filter_map(TypeRef::cast);
114 let first = types.next(); 121 let first = types.next();
115 let second = types.next(); 122 let second = types.next();
@@ -117,9 +124,9 @@ impl <R: TreeRoot> ImplItem<R> {
117 } 124 }
118} 125}
119 126
120impl <R: TreeRoot> Module<R> { 127impl<'a> Module<'a> {
121 pub fn has_semi(&self) -> bool { 128 pub fn has_semi(&self) -> bool {
122 match self.syntax_ref().last_child() { 129 match self.syntax().last_child() {
123 None => false, 130 None => false,
124 Some(node) => node.kind() == SEMI, 131 Some(node) => node.kind() == SEMI,
125 } 132 }