diff options
author | Aleksey Kladov <[email protected]> | 2018-08-09 15:43:39 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-09 15:43:39 +0100 |
commit | d8b2a5efc0e5de3b0d72f29ccc86185f0827c9d3 (patch) | |
tree | 46f8e8feb046ece8511ac1981f53bfa1762d3af3 /src/ast.rs | |
parent | 36bd28633baf6015b767e9e70d2d53185271db50 (diff) |
Generate AST
Diffstat (limited to 'src/ast.rs')
-rw-r--r-- | src/ast.rs | 94 |
1 files changed, 0 insertions, 94 deletions
diff --git a/src/ast.rs b/src/ast.rs deleted file mode 100644 index a595b9324..000000000 --- a/src/ast.rs +++ /dev/null | |||
@@ -1,94 +0,0 @@ | |||
1 | use std::sync::Arc; | ||
2 | use { | ||
3 | SyntaxNode, SyntaxRoot, TreeRoot, | ||
4 | SyntaxKind::*, | ||
5 | }; | ||
6 | |||
7 | #[derive(Debug)] | ||
8 | pub struct File<R: TreeRoot = Arc<SyntaxRoot>> { | ||
9 | syntax: SyntaxNode<R>, | ||
10 | } | ||
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 | |||
23 | impl File<Arc<SyntaxRoot>> { | ||
24 | pub fn parse(text: &str) -> Self { | ||
25 | File { | ||
26 | syntax: ::parse(text), | ||
27 | } | ||
28 | } | ||
29 | } | ||
30 | |||
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> { | ||
91 | pub fn syntax(&self) -> SyntaxNode<R> { | ||
92 | self.syntax.clone() | ||
93 | } | ||
94 | } | ||