aboutsummaryrefslogtreecommitdiff
path: root/src/ast.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-09 15:43:39 +0100
committerAleksey Kladov <[email protected]>2018-08-09 15:43:39 +0100
commitd8b2a5efc0e5de3b0d72f29ccc86185f0827c9d3 (patch)
tree46f8e8feb046ece8511ac1981f53bfa1762d3af3 /src/ast.rs
parent36bd28633baf6015b767e9e70d2d53185271db50 (diff)
Generate AST
Diffstat (limited to 'src/ast.rs')
-rw-r--r--src/ast.rs94
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 @@
1use std::sync::Arc;
2use {
3 SyntaxNode, SyntaxRoot, TreeRoot,
4 SyntaxKind::*,
5};
6
7#[derive(Debug)]
8pub struct File<R: TreeRoot = Arc<SyntaxRoot>> {
9 syntax: SyntaxNode<R>,
10}
11
12#[derive(Debug)]
13pub struct Function<R: TreeRoot = Arc<SyntaxRoot>> {
14 syntax: SyntaxNode<R>,
15}
16
17#[derive(Debug)]
18pub struct Name<R: TreeRoot = Arc<SyntaxRoot>> {
19 syntax: SyntaxNode<R>,
20}
21
22
23impl File<Arc<SyntaxRoot>> {
24 pub fn parse(text: &str) -> Self {
25 File {
26 syntax: ::parse(text),
27 }
28 }
29}
30
31impl<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
40impl<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
82impl<R: TreeRoot> Name<R> {
83 pub fn text(&self) -> String {
84 self.syntax.text()
85 }
86}
87
88
89
90impl<R: TreeRoot> File<R> {
91 pub fn syntax(&self) -> SyntaxNode<R> {
92 self.syntax.clone()
93 }
94}