diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ast.rs | 75 |
1 files changed, 74 insertions, 1 deletions
diff --git a/src/ast.rs b/src/ast.rs index caf5fb7ef..a595b9324 100644 --- a/src/ast.rs +++ b/src/ast.rs | |||
@@ -1,11 +1,25 @@ | |||
1 | use std::sync::Arc; | 1 | use std::sync::Arc; |
2 | use {SyntaxNode, SyntaxRoot, TreeRoot}; | 2 | use { |
3 | SyntaxNode, SyntaxRoot, TreeRoot, | ||
4 | SyntaxKind::*, | ||
5 | }; | ||
3 | 6 | ||
4 | #[derive(Debug)] | 7 | #[derive(Debug)] |
5 | pub struct File<R: TreeRoot = Arc<SyntaxRoot>> { | 8 | pub struct File<R: TreeRoot = Arc<SyntaxRoot>> { |
6 | syntax: SyntaxNode<R>, | 9 | syntax: SyntaxNode<R>, |
7 | } | 10 | } |
8 | 11 | ||
12 | #[derive(Debug)] | ||
13 | pub struct Function<R: TreeRoot = Arc<SyntaxRoot>> { | ||
14 | syntax: SyntaxNode<R>, | ||
15 | } | ||
16 | |||
17 | #[derive(Debug)] | ||
18 | pub struct Name<R: TreeRoot = Arc<SyntaxRoot>> { | ||
19 | syntax: SyntaxNode<R>, | ||
20 | } | ||
21 | |||
22 | |||
9 | impl File<Arc<SyntaxRoot>> { | 23 | impl File<Arc<SyntaxRoot>> { |
10 | pub fn parse(text: &str) -> Self { | 24 | pub fn parse(text: &str) -> Self { |
11 | File { | 25 | File { |
@@ -15,6 +29,65 @@ impl File<Arc<SyntaxRoot>> { | |||
15 | } | 29 | } |
16 | 30 | ||
17 | impl<R: TreeRoot> File<R> { | 31 | impl<R: TreeRoot> File<R> { |
32 | pub fn functions<'a>(&'a self) -> impl Iterator<Item = Function<R>> + 'a { | ||
33 | self.syntax | ||
34 | .children() | ||
35 | .filter(|node| node.kind() == FN_ITEM) | ||
36 | .map(|node| Function { syntax: node }) | ||
37 | } | ||
38 | } | ||
39 | |||
40 | impl<R: TreeRoot> Function<R> { | ||
41 | pub fn syntax(&self) -> SyntaxNode<R> { | ||
42 | self.syntax.clone() | ||
43 | } | ||
44 | |||
45 | pub fn name(&self) -> Option<Name<R>> { | ||
46 | self.syntax | ||
47 | .children() | ||
48 | .filter(|node| node.kind() == NAME) | ||
49 | .map(|node| Name { syntax: node }) | ||
50 | .next() | ||
51 | } | ||
52 | |||
53 | pub fn has_atom_attr(&self, atom: &str) -> bool { | ||
54 | self.syntax | ||
55 | .children() | ||
56 | .filter(|node| node.kind() == ATTR) | ||
57 | .any(|attr| { | ||
58 | let mut metas = attr.children().filter(|node| node.kind() == META_ITEM); | ||
59 | let meta = match metas.next() { | ||
60 | None => return false, | ||
61 | Some(meta) => { | ||
62 | if metas.next().is_some() { | ||
63 | return false; | ||
64 | } | ||
65 | meta | ||
66 | } | ||
67 | }; | ||
68 | let mut children = meta.children(); | ||
69 | match children.next() { | ||
70 | None => false, | ||
71 | Some(child) => { | ||
72 | if children.next().is_some() { | ||
73 | return false; | ||
74 | } | ||
75 | child.kind() == IDENT && child.text() == atom | ||
76 | } | ||
77 | } | ||
78 | }) | ||
79 | } | ||
80 | } | ||
81 | |||
82 | impl<R: TreeRoot> Name<R> { | ||
83 | pub fn text(&self) -> String { | ||
84 | self.syntax.text() | ||
85 | } | ||
86 | } | ||
87 | |||
88 | |||
89 | |||
90 | impl<R: TreeRoot> File<R> { | ||
18 | pub fn syntax(&self) -> SyntaxNode<R> { | 91 | pub fn syntax(&self) -> SyntaxNode<R> { |
19 | self.syntax.clone() | 92 | self.syntax.clone() |
20 | } | 93 | } |