aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/libsyntax2/src/ast/generated.rs21
-rw-r--r--crates/libsyntax2/src/ast/generated.rs.tera9
-rw-r--r--crates/libsyntax2/src/ast/mod.rs43
3 files changed, 28 insertions, 45 deletions
diff --git a/crates/libsyntax2/src/ast/generated.rs b/crates/libsyntax2/src/ast/generated.rs
index d0e07fddc..0f53e8f4a 100644
--- a/crates/libsyntax2/src/ast/generated.rs
+++ b/crates/libsyntax2/src/ast/generated.rs
@@ -40,10 +40,7 @@ impl<'a> AstNode<'a> for Attr<'a> {
40 40
41impl<'a> Attr<'a> { 41impl<'a> Attr<'a> {
42 pub fn value(self) -> Option<TokenTree<'a>> { 42 pub fn value(self) -> Option<TokenTree<'a>> {
43 self.syntax() 43 super::child_opt(self)
44 .children()
45 .filter_map(TokenTree::cast)
46 .next()
47 } 44 }
48} 45}
49 46
@@ -125,15 +122,11 @@ impl<'a> AstNode<'a> for File<'a> {
125 122
126impl<'a> File<'a> { 123impl<'a> File<'a> {
127 pub fn functions(self) -> impl Iterator<Item = FnDef<'a>> + 'a { 124 pub fn functions(self) -> impl Iterator<Item = FnDef<'a>> + 'a {
128 self.syntax() 125 super::children(self)
129 .children()
130 .filter_map(FnDef::cast)
131 } 126 }
132 127
133 pub fn modules(self) -> impl Iterator<Item = Module<'a>> + 'a { 128 pub fn modules(self) -> impl Iterator<Item = Module<'a>> + 'a {
134 self.syntax() 129 super::children(self)
135 .children()
136 .filter_map(Module::cast)
137 } 130 }
138} 131}
139 132
@@ -250,9 +243,7 @@ impl<'a> ast::NameOwner<'a> for Module<'a> {}
250impl<'a> ast::AttrsOwner<'a> for Module<'a> {} 243impl<'a> ast::AttrsOwner<'a> for Module<'a> {}
251impl<'a> Module<'a> { 244impl<'a> Module<'a> {
252 pub fn modules(self) -> impl Iterator<Item = Module<'a>> + 'a { 245 pub fn modules(self) -> impl Iterator<Item = Module<'a>> + 'a {
253 self.syntax() 246 super::children(self)
254 .children()
255 .filter_map(Module::cast)
256 } 247 }
257} 248}
258 249
@@ -507,9 +498,7 @@ impl<'a> ast::TypeParamsOwner<'a> for StructDef<'a> {}
507impl<'a> ast::AttrsOwner<'a> for StructDef<'a> {} 498impl<'a> ast::AttrsOwner<'a> for StructDef<'a> {}
508impl<'a> StructDef<'a> { 499impl<'a> StructDef<'a> {
509 pub fn fields(self) -> impl Iterator<Item = NamedField<'a>> + 'a { 500 pub fn fields(self) -> impl Iterator<Item = NamedField<'a>> + 'a {
510 self.syntax() 501 super::children(self)
511 .children()
512 .filter_map(NamedField::cast)
513 } 502 }
514} 503}
515 504
diff --git a/crates/libsyntax2/src/ast/generated.rs.tera b/crates/libsyntax2/src/ast/generated.rs.tera
index 0572cceaa..69f9236b7 100644
--- a/crates/libsyntax2/src/ast/generated.rs.tera
+++ b/crates/libsyntax2/src/ast/generated.rs.tera
@@ -58,9 +58,7 @@ impl<'a> {{ node }}<'a> {
58{%- set method_name = m.0 -%} 58{%- set method_name = m.0 -%}
59{%- set ChildName = m.1 %} 59{%- set ChildName = m.1 %}
60 pub fn {{ method_name }}(self) -> impl Iterator<Item = {{ ChildName }}<'a>> + 'a { 60 pub fn {{ method_name }}(self) -> impl Iterator<Item = {{ ChildName }}<'a>> + 'a {
61 self.syntax() 61 super::children(self)
62 .children()
63 .filter_map({{ ChildName }}::cast)
64 } 62 }
65{% endfor -%} 63{% endfor -%}
66{%- endif -%} 64{%- endif -%}
@@ -70,10 +68,7 @@ impl<'a> {{ node }}<'a> {
70{%- set method_name = m.0 -%} 68{%- set method_name = m.0 -%}
71{%- set ChildName = m.1 %} 69{%- set ChildName = m.1 %}
72 pub fn {{ method_name }}(self) -> Option<{{ ChildName }}<'a>> { 70 pub fn {{ method_name }}(self) -> Option<{{ ChildName }}<'a>> {
73 self.syntax() 71 super::child_opt(self)
74 .children()
75 .filter_map({{ ChildName }}::cast)
76 .next()
77 } 72 }
78{% endfor -%} 73{% endfor -%}
79{%- endif -%} 74{%- endif -%}
diff --git a/crates/libsyntax2/src/ast/mod.rs b/crates/libsyntax2/src/ast/mod.rs
index 1784b871e..ba80fc64e 100644
--- a/crates/libsyntax2/src/ast/mod.rs
+++ b/crates/libsyntax2/src/ast/mod.rs
@@ -9,7 +9,7 @@ use {
9}; 9};
10pub use self::generated::*; 10pub use self::generated::*;
11 11
12pub trait AstNode<'a>: Clone + Copy { 12pub trait AstNode<'a>: Clone + Copy + 'a {
13 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> 13 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self>
14 where Self: Sized; 14 where Self: Sized;
15 fn syntax(self) -> SyntaxNodeRef<'a>; 15 fn syntax(self) -> SyntaxNodeRef<'a>;
@@ -17,34 +17,23 @@ pub trait AstNode<'a>: Clone + Copy {
17 17
18pub trait NameOwner<'a>: AstNode<'a> { 18pub trait NameOwner<'a>: AstNode<'a> {
19 fn name(self) -> Option<Name<'a>> { 19 fn name(self) -> Option<Name<'a>> {
20 self.syntax() 20 child_opt(self)
21 .children()
22 .filter_map(Name::cast)
23 .next()
24 } 21 }
25} 22}
26 23
27pub trait TypeParamsOwner<'a>: AstNode<'a> { 24pub trait TypeParamsOwner<'a>: AstNode<'a> {
28 fn type_param_list(self) -> Option<TypeParamList<'a>> { 25 fn type_param_list(self) -> Option<TypeParamList<'a>> {
29 self.syntax() 26 child_opt(self)
30 .children()
31 .filter_map(TypeParamList::cast)
32 .next()
33 } 27 }
34 28
35 fn where_clause(self) -> Option<WhereClause<'a>> { 29 fn where_clause(self) -> Option<WhereClause<'a>> {
36 self.syntax() 30 child_opt(self)
37 .children()
38 .filter_map(WhereClause::cast)
39 .next()
40 } 31 }
41} 32}
42 33
43pub trait AttrsOwner<'a>: AstNode<'a> { 34pub trait AttrsOwner<'a>: AstNode<'a> {
44 fn attrs(&self) -> Box<Iterator<Item=Attr<'a>> + 'a> { 35 fn attrs(self) -> Box<Iterator<Item=Attr<'a>> + 'a> {
45 let it = self.syntax().children() 36 Box::new(children(self))
46 .filter_map(Attr::cast);
47 Box::new(it)
48 } 37 }
49} 38}
50 39
@@ -118,22 +107,22 @@ impl<'a> NameRef<'a> {
118} 107}
119 108
120impl<'a> ImplItem<'a> { 109impl<'a> ImplItem<'a> {
121 pub fn target_type(&self) -> Option<TypeRef<'a>> { 110 pub fn target_type(self) -> Option<TypeRef<'a>> {
122 match self.target() { 111 match self.target() {
123 (Some(t), None) | (_, Some(t)) => Some(t), 112 (Some(t), None) | (_, Some(t)) => Some(t),
124 _ => None, 113 _ => None,
125 } 114 }
126 } 115 }
127 116
128 pub fn target_trait(&self) -> Option<TypeRef<'a>> { 117 pub fn target_trait(self) -> Option<TypeRef<'a>> {
129 match self.target() { 118 match self.target() {
130 (Some(t), Some(_)) => Some(t), 119 (Some(t), Some(_)) => Some(t),
131 _ => None, 120 _ => None,
132 } 121 }
133 } 122 }
134 123
135 fn target(&self) -> (Option<TypeRef<'a>>, Option<TypeRef<'a>>) { 124 fn target(self) -> (Option<TypeRef<'a>>, Option<TypeRef<'a>>) {
136 let mut types = self.syntax().children().filter_map(TypeRef::cast); 125 let mut types = children(self);
137 let first = types.next(); 126 let first = types.next();
138 let second = types.next(); 127 let second = types.next();
139 (first, second) 128 (first, second)
@@ -141,10 +130,20 @@ impl<'a> ImplItem<'a> {
141} 130}
142 131
143impl<'a> Module<'a> { 132impl<'a> Module<'a> {
144 pub fn has_semi(&self) -> bool { 133 pub fn has_semi(self) -> bool {
145 match self.syntax().last_child() { 134 match self.syntax().last_child() {
146 None => false, 135 None => false,
147 Some(node) => node.kind() == SEMI, 136 Some(node) => node.kind() == SEMI,
148 } 137 }
149 } 138 }
150} 139}
140
141fn child_opt<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> Option<C> {
142 children(parent).next()
143}
144
145fn children<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> impl Iterator<Item=C> + 'a {
146 parent.syntax()
147 .children()
148 .filter_map(C::cast)
149}