diff options
Diffstat (limited to 'crates/libsyntax2/src/ast/mod.rs')
-rw-r--r-- | crates/libsyntax2/src/ast/mod.rs | 61 |
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; | |||
4 | use smol_str::SmolStr; | 4 | use smol_str::SmolStr; |
5 | 5 | ||
6 | use { | 6 | use { |
7 | SyntaxNode, SyntaxNodeRef, OwnedRoot, TreeRoot, SyntaxError, | 7 | SyntaxNode, SyntaxNodeRef, TreeRoot, SyntaxError, |
8 | SyntaxKind::*, | 8 | SyntaxKind::*, |
9 | }; | 9 | }; |
10 | pub use self::generated::*; | 10 | pub use self::generated::*; |
11 | 11 | ||
12 | pub trait AstNode<R: TreeRoot> { | 12 | pub 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 | ||
21 | pub trait NameOwner<R: TreeRoot>: AstNode<R> { | 18 | pub 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 | ||
30 | pub trait AttrsOwner<R: TreeRoot>: AstNode<R> { | 27 | pub 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 | ||
38 | impl File<OwnedRoot> { | 35 | #[derive(Clone, Debug)] |
39 | pub fn parse(text: &str) -> Self { | 36 | pub struct ParsedFile { |
40 | File::cast(::parse(text)).unwrap() | 37 | root: SyntaxNode |
41 | } | ||
42 | } | 38 | } |
43 | 39 | ||
44 | impl<R: TreeRoot> File<R> { | 40 | impl 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 | ||
50 | impl<R: TreeRoot> FnDef<R> { | 57 | impl<'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 | ||
58 | impl<R: TreeRoot> Attr<R> { | 65 | impl<'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 | ||
81 | impl<R: TreeRoot> Name<R> { | 88 | impl<'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 | ||
89 | impl<R: TreeRoot> NameRef<R> { | 96 | impl<'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 | ||
97 | impl <R: TreeRoot> ImplItem<R> { | 104 | impl<'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 | ||
120 | impl <R: TreeRoot> Module<R> { | 127 | impl<'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 | } |