diff options
author | Aleksey Kladov <[email protected]> | 2018-08-16 10:51:40 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-16 10:51:40 +0100 |
commit | 7094291573dc819e3115950ec3b2316bd5e9ea33 (patch) | |
tree | be18ef6c5baab68acac484d00277125484d15820 /crates/libsyntax2/src/ast | |
parent | 1193c5f829dc96683132c12d5395d7805787af2a (diff) |
tt-attrs
Diffstat (limited to 'crates/libsyntax2/src/ast')
-rw-r--r-- | crates/libsyntax2/src/ast/generated.rs | 52 | ||||
-rw-r--r-- | crates/libsyntax2/src/ast/mod.rs | 53 |
2 files changed, 79 insertions, 26 deletions
diff --git a/crates/libsyntax2/src/ast/generated.rs b/crates/libsyntax2/src/ast/generated.rs index 1ec05c950..c575e15df 100644 --- a/crates/libsyntax2/src/ast/generated.rs +++ b/crates/libsyntax2/src/ast/generated.rs | |||
@@ -23,6 +23,31 @@ impl<R: TreeRoot> AstNode<R> for ArrayType<R> { | |||
23 | 23 | ||
24 | impl<R: TreeRoot> ArrayType<R> {} | 24 | impl<R: TreeRoot> ArrayType<R> {} |
25 | 25 | ||
26 | // Attr | ||
27 | #[derive(Debug, Clone, Copy)] | ||
28 | pub struct Attr<R: TreeRoot = Arc<SyntaxRoot>> { | ||
29 | syntax: SyntaxNode<R>, | ||
30 | } | ||
31 | |||
32 | impl<R: TreeRoot> AstNode<R> for Attr<R> { | ||
33 | fn cast(syntax: SyntaxNode<R>) -> Option<Self> { | ||
34 | match syntax.kind() { | ||
35 | ATTR => Some(Attr { syntax }), | ||
36 | _ => None, | ||
37 | } | ||
38 | } | ||
39 | fn syntax(&self) -> &SyntaxNode<R> { &self.syntax } | ||
40 | } | ||
41 | |||
42 | impl<R: TreeRoot> Attr<R> { | ||
43 | pub fn value(&self) -> Option<TokenTree<R>> { | ||
44 | self.syntax() | ||
45 | .children() | ||
46 | .filter_map(TokenTree::cast) | ||
47 | .next() | ||
48 | } | ||
49 | } | ||
50 | |||
26 | // ConstDef | 51 | // ConstDef |
27 | #[derive(Debug, Clone, Copy)] | 52 | #[derive(Debug, Clone, Copy)] |
28 | pub struct ConstDef<R: TreeRoot = Arc<SyntaxRoot>> { | 53 | pub struct ConstDef<R: TreeRoot = Arc<SyntaxRoot>> { |
@@ -40,6 +65,7 @@ impl<R: TreeRoot> AstNode<R> for ConstDef<R> { | |||
40 | } | 65 | } |
41 | 66 | ||
42 | impl<R: TreeRoot> ast::NameOwner<R> for ConstDef<R> {} | 67 | impl<R: TreeRoot> ast::NameOwner<R> for ConstDef<R> {} |
68 | impl<R: TreeRoot> ast::AttrsOwner<R> for ConstDef<R> {} | ||
43 | impl<R: TreeRoot> ConstDef<R> {} | 69 | impl<R: TreeRoot> ConstDef<R> {} |
44 | 70 | ||
45 | // DynTraitType | 71 | // DynTraitType |
@@ -77,6 +103,7 @@ impl<R: TreeRoot> AstNode<R> for EnumDef<R> { | |||
77 | } | 103 | } |
78 | 104 | ||
79 | impl<R: TreeRoot> ast::NameOwner<R> for EnumDef<R> {} | 105 | impl<R: TreeRoot> ast::NameOwner<R> for EnumDef<R> {} |
106 | impl<R: TreeRoot> ast::AttrsOwner<R> for EnumDef<R> {} | ||
80 | impl<R: TreeRoot> EnumDef<R> {} | 107 | impl<R: TreeRoot> EnumDef<R> {} |
81 | 108 | ||
82 | // File | 109 | // File |
@@ -120,6 +147,7 @@ impl<R: TreeRoot> AstNode<R> for FnDef<R> { | |||
120 | } | 147 | } |
121 | 148 | ||
122 | impl<R: TreeRoot> ast::NameOwner<R> for FnDef<R> {} | 149 | impl<R: TreeRoot> ast::NameOwner<R> for FnDef<R> {} |
150 | impl<R: TreeRoot> ast::AttrsOwner<R> for FnDef<R> {} | ||
123 | impl<R: TreeRoot> FnDef<R> {} | 151 | impl<R: TreeRoot> FnDef<R> {} |
124 | 152 | ||
125 | // FnPointerType | 153 | // FnPointerType |
@@ -211,6 +239,7 @@ impl<R: TreeRoot> AstNode<R> for Module<R> { | |||
211 | } | 239 | } |
212 | 240 | ||
213 | impl<R: TreeRoot> ast::NameOwner<R> for Module<R> {} | 241 | impl<R: TreeRoot> ast::NameOwner<R> for Module<R> {} |
242 | impl<R: TreeRoot> ast::AttrsOwner<R> for Module<R> {} | ||
214 | impl<R: TreeRoot> Module<R> {} | 243 | impl<R: TreeRoot> Module<R> {} |
215 | 244 | ||
216 | // Name | 245 | // Name |
@@ -266,6 +295,7 @@ impl<R: TreeRoot> AstNode<R> for NamedField<R> { | |||
266 | } | 295 | } |
267 | 296 | ||
268 | impl<R: TreeRoot> ast::NameOwner<R> for NamedField<R> {} | 297 | impl<R: TreeRoot> ast::NameOwner<R> for NamedField<R> {} |
298 | impl<R: TreeRoot> ast::AttrsOwner<R> for NamedField<R> {} | ||
269 | impl<R: TreeRoot> NamedField<R> {} | 299 | impl<R: TreeRoot> NamedField<R> {} |
270 | 300 | ||
271 | // NeverType | 301 | // NeverType |
@@ -436,6 +466,7 @@ impl<R: TreeRoot> AstNode<R> for StaticDef<R> { | |||
436 | } | 466 | } |
437 | 467 | ||
438 | impl<R: TreeRoot> ast::NameOwner<R> for StaticDef<R> {} | 468 | impl<R: TreeRoot> ast::NameOwner<R> for StaticDef<R> {} |
469 | impl<R: TreeRoot> ast::AttrsOwner<R> for StaticDef<R> {} | ||
439 | impl<R: TreeRoot> StaticDef<R> {} | 470 | impl<R: TreeRoot> StaticDef<R> {} |
440 | 471 | ||
441 | // StructDef | 472 | // StructDef |
@@ -455,6 +486,7 @@ impl<R: TreeRoot> AstNode<R> for StructDef<R> { | |||
455 | } | 486 | } |
456 | 487 | ||
457 | impl<R: TreeRoot> ast::NameOwner<R> for StructDef<R> {} | 488 | impl<R: TreeRoot> ast::NameOwner<R> for StructDef<R> {} |
489 | impl<R: TreeRoot> ast::AttrsOwner<R> for StructDef<R> {} | ||
458 | impl<R: TreeRoot> StructDef<R> { | 490 | impl<R: TreeRoot> StructDef<R> { |
459 | pub fn fields<'a>(&'a self) -> impl Iterator<Item = NamedField<R>> + 'a { | 491 | pub fn fields<'a>(&'a self) -> impl Iterator<Item = NamedField<R>> + 'a { |
460 | self.syntax() | 492 | self.syntax() |
@@ -463,6 +495,24 @@ impl<R: TreeRoot> StructDef<R> { | |||
463 | } | 495 | } |
464 | } | 496 | } |
465 | 497 | ||
498 | // TokenTree | ||
499 | #[derive(Debug, Clone, Copy)] | ||
500 | pub struct TokenTree<R: TreeRoot = Arc<SyntaxRoot>> { | ||
501 | syntax: SyntaxNode<R>, | ||
502 | } | ||
503 | |||
504 | impl<R: TreeRoot> AstNode<R> for TokenTree<R> { | ||
505 | fn cast(syntax: SyntaxNode<R>) -> Option<Self> { | ||
506 | match syntax.kind() { | ||
507 | TOKEN_TREE => Some(TokenTree { syntax }), | ||
508 | _ => None, | ||
509 | } | ||
510 | } | ||
511 | fn syntax(&self) -> &SyntaxNode<R> { &self.syntax } | ||
512 | } | ||
513 | |||
514 | impl<R: TreeRoot> TokenTree<R> {} | ||
515 | |||
466 | // TraitDef | 516 | // TraitDef |
467 | #[derive(Debug, Clone, Copy)] | 517 | #[derive(Debug, Clone, Copy)] |
468 | pub struct TraitDef<R: TreeRoot = Arc<SyntaxRoot>> { | 518 | pub struct TraitDef<R: TreeRoot = Arc<SyntaxRoot>> { |
@@ -480,6 +530,7 @@ impl<R: TreeRoot> AstNode<R> for TraitDef<R> { | |||
480 | } | 530 | } |
481 | 531 | ||
482 | impl<R: TreeRoot> ast::NameOwner<R> for TraitDef<R> {} | 532 | impl<R: TreeRoot> ast::NameOwner<R> for TraitDef<R> {} |
533 | impl<R: TreeRoot> ast::AttrsOwner<R> for TraitDef<R> {} | ||
483 | impl<R: TreeRoot> TraitDef<R> {} | 534 | impl<R: TreeRoot> TraitDef<R> {} |
484 | 535 | ||
485 | // TupleType | 536 | // TupleType |
@@ -517,6 +568,7 @@ impl<R: TreeRoot> AstNode<R> for TypeDef<R> { | |||
517 | } | 568 | } |
518 | 569 | ||
519 | impl<R: TreeRoot> ast::NameOwner<R> for TypeDef<R> {} | 570 | impl<R: TreeRoot> ast::NameOwner<R> for TypeDef<R> {} |
571 | impl<R: TreeRoot> ast::AttrsOwner<R> for TypeDef<R> {} | ||
520 | impl<R: TreeRoot> TypeDef<R> {} | 572 | impl<R: TreeRoot> TypeDef<R> {} |
521 | 573 | ||
522 | // TypeRef | 574 | // TypeRef |
diff --git a/crates/libsyntax2/src/ast/mod.rs b/crates/libsyntax2/src/ast/mod.rs index 679e292a2..fe8f91d15 100644 --- a/crates/libsyntax2/src/ast/mod.rs +++ b/crates/libsyntax2/src/ast/mod.rs | |||
@@ -2,10 +2,11 @@ mod generated; | |||
2 | 2 | ||
3 | use std::sync::Arc; | 3 | use std::sync::Arc; |
4 | 4 | ||
5 | use itertools::Itertools; | ||
5 | use smol_str::SmolStr; | 6 | use smol_str::SmolStr; |
6 | 7 | ||
7 | use { | 8 | use { |
8 | SyntaxNode, SyntaxRoot, TreeRoot, SyntaxError, | 9 | SyntaxNode, SyntaxNodeRef, SyntaxRoot, TreeRoot, SyntaxError, |
9 | SyntaxKind::*, | 10 | SyntaxKind::*, |
10 | }; | 11 | }; |
11 | pub use self::generated::*; | 12 | pub use self::generated::*; |
@@ -14,6 +15,9 @@ pub trait AstNode<R: TreeRoot> { | |||
14 | fn cast(syntax: SyntaxNode<R>) -> Option<Self> | 15 | fn cast(syntax: SyntaxNode<R>) -> Option<Self> |
15 | where Self: Sized; | 16 | where Self: Sized; |
16 | fn syntax(&self) -> &SyntaxNode<R>; | 17 | fn syntax(&self) -> &SyntaxNode<R>; |
18 | fn syntax_ref<'a>(&'a self) -> SyntaxNodeRef<'a> where R: 'a { | ||
19 | self.syntax().as_ref() | ||
20 | } | ||
17 | } | 21 | } |
18 | 22 | ||
19 | pub trait NameOwner<R: TreeRoot>: AstNode<R> { | 23 | pub trait NameOwner<R: TreeRoot>: AstNode<R> { |
@@ -25,6 +29,14 @@ pub trait NameOwner<R: TreeRoot>: AstNode<R> { | |||
25 | } | 29 | } |
26 | } | 30 | } |
27 | 31 | ||
32 | pub trait AttrsOwner<R: TreeRoot>: AstNode<R> { | ||
33 | fn attrs<'a>(&'a self) -> Box<Iterator<Item=Attr<R>> + 'a> where R: 'a { | ||
34 | let it = self.syntax().children() | ||
35 | .filter_map(Attr::cast); | ||
36 | Box::new(it) | ||
37 | } | ||
38 | } | ||
39 | |||
28 | impl File<Arc<SyntaxRoot>> { | 40 | impl File<Arc<SyntaxRoot>> { |
29 | pub fn parse(text: &str) -> Self { | 41 | pub fn parse(text: &str) -> Self { |
30 | File::cast(::parse(text)).unwrap() | 42 | File::cast(::parse(text)).unwrap() |
@@ -39,31 +51,20 @@ impl<R: TreeRoot> File<R> { | |||
39 | 51 | ||
40 | impl<R: TreeRoot> FnDef<R> { | 52 | impl<R: TreeRoot> FnDef<R> { |
41 | pub fn has_atom_attr(&self, atom: &str) -> bool { | 53 | pub fn has_atom_attr(&self, atom: &str) -> bool { |
42 | self.syntax() | 54 | self.attrs() |
43 | .children() | 55 | .filter_map(|x| x.value()) |
44 | .filter(|node| node.kind() == ATTR) | 56 | .filter_map(|x| as_atom(x)) |
45 | .any(|attr| { | 57 | .any(|x| x == atom) |
46 | let mut metas = attr.children().filter(|node| node.kind() == META_ITEM); | 58 | } |
47 | let meta = match metas.next() { | 59 | } |
48 | None => return false, | 60 | |
49 | Some(meta) => { | 61 | fn as_atom<R: TreeRoot>(tt: TokenTree<R>) -> Option<SmolStr> { |
50 | if metas.next().is_some() { | 62 | let syntax = tt.syntax_ref(); |
51 | return false; | 63 | let (_bra, attr, _ket) = syntax.children().collect_tuple()?; |
52 | } | 64 | if attr.kind() == IDENT { |
53 | meta | 65 | Some(attr.leaf_text().unwrap()) |
54 | } | 66 | } else { |
55 | }; | 67 | None |
56 | let mut children = meta.children(); | ||
57 | match children.next() { | ||
58 | None => false, | ||
59 | Some(child) => { | ||
60 | if children.next().is_some() { | ||
61 | return false; | ||
62 | } | ||
63 | child.kind() == IDENT && child.text() == atom | ||
64 | } | ||
65 | } | ||
66 | }) | ||
67 | } | 68 | } |
68 | } | 69 | } |
69 | 70 | ||