diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ast.rs | 30 | ||||
-rw-r--r-- | src/lib.rs | 4 | ||||
-rw-r--r-- | src/yellow/mod.rs | 4 |
3 files changed, 35 insertions, 3 deletions
diff --git a/src/ast.rs b/src/ast.rs new file mode 100644 index 000000000..3a9287466 --- /dev/null +++ b/src/ast.rs | |||
@@ -0,0 +1,30 @@ | |||
1 | use std::sync::Arc; | ||
2 | use {SyntaxNode, TreeRoot, SyntaxRoot, SyntaxNodeRef}; | ||
3 | |||
4 | #[derive(Debug)] | ||
5 | pub struct File<R: TreeRoot = Arc<SyntaxRoot>> { | ||
6 | syntax: SyntaxNode<R> | ||
7 | } | ||
8 | |||
9 | impl File<Arc<SyntaxRoot>> { | ||
10 | pub fn parse(text: &str) -> Self { | ||
11 | File { syntax: ::parse(text.to_owned()) } | ||
12 | } | ||
13 | } | ||
14 | |||
15 | impl<R: TreeRoot> File<R> { | ||
16 | pub fn syntax(&self) -> SyntaxNode<R> { | ||
17 | self.syntax.clone() | ||
18 | } | ||
19 | |||
20 | pub fn for_each_node(&self, mut f: impl FnMut(SyntaxNodeRef)) { | ||
21 | let syntax = self.syntax(); | ||
22 | let syntax = syntax.borrow(); | ||
23 | go(syntax, &mut f); | ||
24 | |||
25 | fn go(syntax: SyntaxNodeRef, f: &mut FnMut(SyntaxNodeRef)) { | ||
26 | f(syntax); | ||
27 | syntax.children().into_iter().for_each(f) | ||
28 | } | ||
29 | } | ||
30 | } | ||
diff --git a/src/lib.rs b/src/lib.rs index f85ee1eef..3a8e6fa64 100644 --- a/src/lib.rs +++ b/src/lib.rs | |||
@@ -29,12 +29,14 @@ mod syntax_kinds; | |||
29 | mod yellow; | 29 | mod yellow; |
30 | /// Utilities for simple uses of the parser. | 30 | /// Utilities for simple uses of the parser. |
31 | pub mod utils; | 31 | pub mod utils; |
32 | pub mod ast; | ||
32 | 33 | ||
33 | pub use { | 34 | pub use { |
34 | lexer::{tokenize, Token}, | 35 | lexer::{tokenize, Token}, |
35 | syntax_kinds::SyntaxKind, | 36 | syntax_kinds::SyntaxKind, |
36 | text_unit::{TextRange, TextUnit}, | 37 | text_unit::{TextRange, TextUnit}, |
37 | yellow::{SyntaxNode, SyntaxNodeRef}, | 38 | yellow::{SyntaxNode, SyntaxNodeRef, TreeRoot, SyntaxRoot}, |
39 | ast::File, | ||
38 | }; | 40 | }; |
39 | 41 | ||
40 | pub(crate) use yellow::SyntaxError; | 42 | pub(crate) use yellow::SyntaxError; |
diff --git a/src/yellow/mod.rs b/src/yellow/mod.rs index cada65d2f..0cc90adbd 100644 --- a/src/yellow/mod.rs +++ b/src/yellow/mod.rs | |||
@@ -3,10 +3,10 @@ mod green; | |||
3 | mod red; | 3 | mod red; |
4 | mod syntax; | 4 | mod syntax; |
5 | 5 | ||
6 | pub use self::syntax::{SyntaxNode, SyntaxNodeRef}; | 6 | pub use self::syntax::{SyntaxNode, SyntaxNodeRef, SyntaxRoot, TreeRoot}; |
7 | pub(crate) use self::{ | 7 | pub(crate) use self::{ |
8 | builder::GreenBuilder, | 8 | builder::GreenBuilder, |
9 | green::{GreenNode, GreenNodeBuilder}, | 9 | green::{GreenNode, GreenNodeBuilder}, |
10 | red::RedNode, | 10 | red::RedNode, |
11 | syntax::{SyntaxError, SyntaxRoot}, | 11 | syntax::{SyntaxError}, |
12 | }; | 12 | }; |