diff options
Diffstat (limited to 'crates/libsyntax2/src/ast/mod.rs')
-rw-r--r-- | crates/libsyntax2/src/ast/mod.rs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/crates/libsyntax2/src/ast/mod.rs b/crates/libsyntax2/src/ast/mod.rs new file mode 100644 index 000000000..eeb7ae6f6 --- /dev/null +++ b/crates/libsyntax2/src/ast/mod.rs | |||
@@ -0,0 +1,74 @@ | |||
1 | mod generated; | ||
2 | |||
3 | use std::sync::Arc; | ||
4 | use { | ||
5 | SyntaxNode, SyntaxRoot, TreeRoot, SyntaxError, | ||
6 | SyntaxKind::*, | ||
7 | }; | ||
8 | pub use self::generated::*; | ||
9 | |||
10 | pub trait AstNode<R: TreeRoot>: Sized { | ||
11 | fn cast(syntax: SyntaxNode<R>) -> Option<Self>; | ||
12 | fn syntax(&self) -> &SyntaxNode<R>; | ||
13 | } | ||
14 | |||
15 | impl File<Arc<SyntaxRoot>> { | ||
16 | pub fn parse(text: &str) -> Self { | ||
17 | File::cast(::parse(text)).unwrap() | ||
18 | } | ||
19 | } | ||
20 | |||
21 | impl<R: TreeRoot> File<R> { | ||
22 | pub fn errors(&self) -> Vec<SyntaxError> { | ||
23 | self.syntax().root.errors.clone() | ||
24 | } | ||
25 | |||
26 | pub fn functions<'a>(&'a self) -> impl Iterator<Item = Function<R>> + 'a { | ||
27 | self.syntax() | ||
28 | .children() | ||
29 | .filter_map(Function::cast) | ||
30 | } | ||
31 | } | ||
32 | |||
33 | impl<R: TreeRoot> Function<R> { | ||
34 | pub fn name(&self) -> Option<Name<R>> { | ||
35 | self.syntax() | ||
36 | .children() | ||
37 | .filter_map(Name::cast) | ||
38 | .next() | ||
39 | } | ||
40 | |||
41 | pub fn has_atom_attr(&self, atom: &str) -> bool { | ||
42 | self.syntax() | ||
43 | .children() | ||
44 | .filter(|node| node.kind() == ATTR) | ||
45 | .any(|attr| { | ||
46 | let mut metas = attr.children().filter(|node| node.kind() == META_ITEM); | ||
47 | let meta = match metas.next() { | ||
48 | None => return false, | ||
49 | Some(meta) => { | ||
50 | if metas.next().is_some() { | ||
51 | return false; | ||
52 | } | ||
53 | meta | ||
54 | } | ||
55 | }; | ||
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 | } | ||
69 | |||
70 | impl<R: TreeRoot> Name<R> { | ||
71 | pub fn text(&self) -> String { | ||
72 | self.syntax().text() | ||
73 | } | ||
74 | } | ||