aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-22 14:46:42 +0100
committerAleksey Kladov <[email protected]>2018-08-22 14:46:42 +0100
commit8e3bec11ebd1fce78701670cf9189b5a9d0d68f8 (patch)
tree16066a9bed666d0176bfa656a310fe9a264af53c
parent55896aedb16e9eae6309bf129d04a75e6d26530d (diff)
TypeParamList Owner
-rw-r--r--crates/libsyntax2/src/ast/generated.rs35
-rw-r--r--crates/libsyntax2/src/ast/mod.rs16
-rw-r--r--crates/libsyntax2/src/grammar.ron50
-rw-r--r--crates/libsyntax2/tests/data/parser/ok/0001_struct_item.rs6
-rw-r--r--crates/libsyntax2/tests/data/parser/ok/0001_struct_item.txt37
5 files changed, 118 insertions, 26 deletions
diff --git a/crates/libsyntax2/src/ast/generated.rs b/crates/libsyntax2/src/ast/generated.rs
index 0651da26d..d0e07fddc 100644
--- a/crates/libsyntax2/src/ast/generated.rs
+++ b/crates/libsyntax2/src/ast/generated.rs
@@ -64,6 +64,7 @@ impl<'a> AstNode<'a> for ConstDef<'a> {
64} 64}
65 65
66impl<'a> ast::NameOwner<'a> for ConstDef<'a> {} 66impl<'a> ast::NameOwner<'a> for ConstDef<'a> {}
67impl<'a> ast::TypeParamsOwner<'a> for ConstDef<'a> {}
67impl<'a> ast::AttrsOwner<'a> for ConstDef<'a> {} 68impl<'a> ast::AttrsOwner<'a> for ConstDef<'a> {}
68impl<'a> ConstDef<'a> {} 69impl<'a> ConstDef<'a> {}
69 70
@@ -102,6 +103,7 @@ impl<'a> AstNode<'a> for EnumDef<'a> {
102} 103}
103 104
104impl<'a> ast::NameOwner<'a> for EnumDef<'a> {} 105impl<'a> ast::NameOwner<'a> for EnumDef<'a> {}
106impl<'a> ast::TypeParamsOwner<'a> for EnumDef<'a> {}
105impl<'a> ast::AttrsOwner<'a> for EnumDef<'a> {} 107impl<'a> ast::AttrsOwner<'a> for EnumDef<'a> {}
106impl<'a> EnumDef<'a> {} 108impl<'a> EnumDef<'a> {}
107 109
@@ -152,6 +154,7 @@ impl<'a> AstNode<'a> for FnDef<'a> {
152} 154}
153 155
154impl<'a> ast::NameOwner<'a> for FnDef<'a> {} 156impl<'a> ast::NameOwner<'a> for FnDef<'a> {}
157impl<'a> ast::TypeParamsOwner<'a> for FnDef<'a> {}
155impl<'a> ast::AttrsOwner<'a> for FnDef<'a> {} 158impl<'a> ast::AttrsOwner<'a> for FnDef<'a> {}
156impl<'a> FnDef<'a> {} 159impl<'a> FnDef<'a> {}
157 160
@@ -351,6 +354,7 @@ impl<'a> AstNode<'a> for NominalDef<'a> {
351} 354}
352 355
353impl<'a> ast::AttrsOwner<'a> for NominalDef<'a> {} 356impl<'a> ast::AttrsOwner<'a> for NominalDef<'a> {}
357impl<'a> ast::TypeParamsOwner<'a> for NominalDef<'a> {}
354impl<'a> NominalDef<'a> {} 358impl<'a> NominalDef<'a> {}
355 359
356// ParenType 360// ParenType
@@ -478,6 +482,7 @@ impl<'a> AstNode<'a> for StaticDef<'a> {
478} 482}
479 483
480impl<'a> ast::NameOwner<'a> for StaticDef<'a> {} 484impl<'a> ast::NameOwner<'a> for StaticDef<'a> {}
485impl<'a> ast::TypeParamsOwner<'a> for StaticDef<'a> {}
481impl<'a> ast::AttrsOwner<'a> for StaticDef<'a> {} 486impl<'a> ast::AttrsOwner<'a> for StaticDef<'a> {}
482impl<'a> StaticDef<'a> {} 487impl<'a> StaticDef<'a> {}
483 488
@@ -498,6 +503,7 @@ impl<'a> AstNode<'a> for StructDef<'a> {
498} 503}
499 504
500impl<'a> ast::NameOwner<'a> for StructDef<'a> {} 505impl<'a> ast::NameOwner<'a> for StructDef<'a> {}
506impl<'a> ast::TypeParamsOwner<'a> for StructDef<'a> {}
501impl<'a> ast::AttrsOwner<'a> for StructDef<'a> {} 507impl<'a> ast::AttrsOwner<'a> for StructDef<'a> {}
502impl<'a> StructDef<'a> { 508impl<'a> StructDef<'a> {
503 pub fn fields(self) -> impl Iterator<Item = NamedField<'a>> + 'a { 509 pub fn fields(self) -> impl Iterator<Item = NamedField<'a>> + 'a {
@@ -580,9 +586,28 @@ impl<'a> AstNode<'a> for TypeDef<'a> {
580} 586}
581 587
582impl<'a> ast::NameOwner<'a> for TypeDef<'a> {} 588impl<'a> ast::NameOwner<'a> for TypeDef<'a> {}
589impl<'a> ast::TypeParamsOwner<'a> for TypeDef<'a> {}
583impl<'a> ast::AttrsOwner<'a> for TypeDef<'a> {} 590impl<'a> ast::AttrsOwner<'a> for TypeDef<'a> {}
584impl<'a> TypeDef<'a> {} 591impl<'a> TypeDef<'a> {}
585 592
593// TypeParamList
594#[derive(Debug, Clone, Copy)]
595pub struct TypeParamList<'a> {
596 syntax: SyntaxNodeRef<'a>,
597}
598
599impl<'a> AstNode<'a> for TypeParamList<'a> {
600 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
601 match syntax.kind() {
602 TYPE_PARAM_LIST => Some(TypeParamList { syntax }),
603 _ => None,
604 }
605 }
606 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
607}
608
609impl<'a> TypeParamList<'a> {}
610
586// TypeRef 611// TypeRef
587#[derive(Debug, Clone, Copy)] 612#[derive(Debug, Clone, Copy)]
588pub enum TypeRef<'a> { 613pub enum TypeRef<'a> {
@@ -641,21 +666,21 @@ impl<'a> AstNode<'a> for TypeRef<'a> {
641 666
642impl<'a> TypeRef<'a> {} 667impl<'a> TypeRef<'a> {}
643 668
644// Whitespace 669// WhereClause
645#[derive(Debug, Clone, Copy)] 670#[derive(Debug, Clone, Copy)]
646pub struct Whitespace<'a> { 671pub struct WhereClause<'a> {
647 syntax: SyntaxNodeRef<'a>, 672 syntax: SyntaxNodeRef<'a>,
648} 673}
649 674
650impl<'a> AstNode<'a> for Whitespace<'a> { 675impl<'a> AstNode<'a> for WhereClause<'a> {
651 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> { 676 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
652 match syntax.kind() { 677 match syntax.kind() {
653 WHITESPACE => Some(Whitespace { syntax }), 678 WHERE_CLAUSE => Some(WhereClause { syntax }),
654 _ => None, 679 _ => None,
655 } 680 }
656 } 681 }
657 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } 682 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
658} 683}
659 684
660impl<'a> Whitespace<'a> {} 685impl<'a> WhereClause<'a> {}
661 686
diff --git a/crates/libsyntax2/src/ast/mod.rs b/crates/libsyntax2/src/ast/mod.rs
index 5b9a07db4..1784b871e 100644
--- a/crates/libsyntax2/src/ast/mod.rs
+++ b/crates/libsyntax2/src/ast/mod.rs
@@ -24,6 +24,22 @@ pub trait NameOwner<'a>: AstNode<'a> {
24 } 24 }
25} 25}
26 26
27pub trait TypeParamsOwner<'a>: AstNode<'a> {
28 fn type_param_list(self) -> Option<TypeParamList<'a>> {
29 self.syntax()
30 .children()
31 .filter_map(TypeParamList::cast)
32 .next()
33 }
34
35 fn where_clause(self) -> Option<WhereClause<'a>> {
36 self.syntax()
37 .children()
38 .filter_map(WhereClause::cast)
39 .next()
40 }
41}
42
27pub trait AttrsOwner<'a>: AstNode<'a> { 43pub trait AttrsOwner<'a>: AstNode<'a> {
28 fn attrs(&self) -> Box<Iterator<Item=Attr<'a>> + 'a> { 44 fn attrs(&self) -> Box<Iterator<Item=Attr<'a>> + 'a> {
29 let it = self.syntax().children() 45 let it = self.syntax().children()
diff --git a/crates/libsyntax2/src/grammar.ron b/crates/libsyntax2/src/grammar.ron
index 8e644d3c4..83b56c349 100644
--- a/crates/libsyntax2/src/grammar.ron
+++ b/crates/libsyntax2/src/grammar.ron
@@ -222,15 +222,27 @@ Grammar(
222 ["modules", "Module"], 222 ["modules", "Module"],
223 ] 223 ]
224 ), 224 ),
225 "FnDef": ( traits: ["NameOwner", "AttrsOwner"] ), 225 "FnDef": ( traits: [
226 "NameOwner",
227 "TypeParamsOwner",
228 "AttrsOwner",
229 ] ),
226 "StructDef": ( 230 "StructDef": (
227 traits: ["NameOwner", "AttrsOwner"], 231 traits: [
232 "NameOwner",
233 "TypeParamsOwner",
234 "AttrsOwner",
235 ],
228 collections: [ 236 collections: [
229 ["fields", "NamedField"] 237 ["fields", "NamedField"]
230 ] 238 ]
231 ), 239 ),
232 "NamedField": ( traits: ["NameOwner", "AttrsOwner"] ), 240 "NamedField": ( traits: ["NameOwner", "AttrsOwner"] ),
233 "EnumDef": ( traits: ["NameOwner", "AttrsOwner"] ), 241 "EnumDef": ( traits: [
242 "NameOwner",
243 "TypeParamsOwner",
244 "AttrsOwner",
245 ] ),
234 "TraitDef": ( traits: ["NameOwner", "AttrsOwner"] ), 246 "TraitDef": ( traits: ["NameOwner", "AttrsOwner"] ),
235 "Module": ( 247 "Module": (
236 traits: ["NameOwner", "AttrsOwner"], 248 traits: ["NameOwner", "AttrsOwner"],
@@ -238,16 +250,23 @@ Grammar(
238 ["modules", "Module"] 250 ["modules", "Module"]
239 ] 251 ]
240 ), 252 ),
241 "ConstDef": ( traits: ["NameOwner", "AttrsOwner"] ), 253 "ConstDef": ( traits: [
242 "StaticDef": ( traits: ["NameOwner", "AttrsOwner"] ), 254 "NameOwner",
243 "TypeDef": ( traits: ["NameOwner", "AttrsOwner"] ), 255 "TypeParamsOwner",
256 "AttrsOwner",
257 ] ),
258 "StaticDef": ( traits: [
259 "NameOwner",
260 "TypeParamsOwner",
261 "AttrsOwner",
262 ] ),
263 "TypeDef": ( traits: [
264 "NameOwner",
265 "TypeParamsOwner",
266 "AttrsOwner",
267 ] ),
244 "ImplItem": (), 268 "ImplItem": (),
245 269
246 "Name": (),
247 "NameRef": (),
248 "Attr": ( options: [ ["value", "TokenTree"] ] ),
249 "TokenTree": (),
250
251 "ParenType": (), 270 "ParenType": (),
252 "TupleType": (), 271 "TupleType": (),
253 "NeverType": (), 272 "NeverType": (),
@@ -280,7 +299,14 @@ Grammar(
280 299
281 "NominalDef": ( 300 "NominalDef": (
282 enum: ["StructDef", "EnumDef"], 301 enum: ["StructDef", "EnumDef"],
283 traits: [ "AttrsOwner" ], 302 traits: [ "AttrsOwner", "TypeParamsOwner" ],
284 ), 303 ),
304
305 "Name": (),
306 "NameRef": (),
307 "Attr": ( options: [ ["value", "TokenTree"] ] ),
308 "TokenTree": (),
309 "TypeParamList": (),
310 "WhereClause": (),
285 }, 311 },
286) 312)
diff --git a/crates/libsyntax2/tests/data/parser/ok/0001_struct_item.rs b/crates/libsyntax2/tests/data/parser/ok/0001_struct_item.rs
index d3a8c1d23..512aeb3e7 100644
--- a/crates/libsyntax2/tests/data/parser/ok/0001_struct_item.rs
+++ b/crates/libsyntax2/tests/data/parser/ok/0001_struct_item.rs
@@ -1,3 +1,3 @@
1struct S { 1struct S<T: Copy> {
2 2 f: T,
3} \ No newline at end of file 3}
diff --git a/crates/libsyntax2/tests/data/parser/ok/0001_struct_item.txt b/crates/libsyntax2/tests/data/parser/ok/0001_struct_item.txt
index 798c8fd37..cfe128971 100644
--- a/crates/libsyntax2/tests/data/parser/ok/0001_struct_item.txt
+++ b/crates/libsyntax2/tests/data/parser/ok/0001_struct_item.txt
@@ -1,10 +1,35 @@
1FILE@[0; 13) 1FILE@[0; 32)
2 STRUCT_DEF@[0; 13) 2 STRUCT_DEF@[0; 31)
3 STRUCT_KW@[0; 6) 3 STRUCT_KW@[0; 6)
4 WHITESPACE@[6; 7) 4 WHITESPACE@[6; 7)
5 NAME@[7; 8) 5 NAME@[7; 8)
6 IDENT@[7; 8) "S" 6 IDENT@[7; 8) "S"
7 WHITESPACE@[8; 9) 7 TYPE_PARAM_LIST@[8; 17)
8 L_CURLY@[9; 10) 8 L_ANGLE@[8; 9)
9 WHITESPACE@[10; 12) 9 TYPE_PARAM@[9; 16)
10 R_CURLY@[12; 13) 10 NAME@[9; 10)
11 IDENT@[9; 10) "T"
12 COLON@[10; 11)
13 WHITESPACE@[11; 12)
14 PATH@[12; 16)
15 PATH_SEGMENT@[12; 16)
16 NAME_REF@[12; 16)
17 IDENT@[12; 16) "Copy"
18 R_ANGLE@[16; 17)
19 WHITESPACE@[17; 18)
20 L_CURLY@[18; 19)
21 WHITESPACE@[19; 24)
22 NAMED_FIELD@[24; 28)
23 NAME@[24; 25)
24 IDENT@[24; 25) "f"
25 COLON@[25; 26)
26 WHITESPACE@[26; 27)
27 PATH_TYPE@[27; 28)
28 PATH@[27; 28)
29 PATH_SEGMENT@[27; 28)
30 NAME_REF@[27; 28)
31 IDENT@[27; 28) "T"
32 COMMA@[28; 29)
33 WHITESPACE@[29; 30)
34 R_CURLY@[30; 31)
35 WHITESPACE@[31; 32)