aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ast.rs30
-rw-r--r--src/lib.rs4
-rw-r--r--src/yellow/mod.rs4
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 @@
1use std::sync::Arc;
2use {SyntaxNode, TreeRoot, SyntaxRoot, SyntaxNodeRef};
3
4#[derive(Debug)]
5pub struct File<R: TreeRoot = Arc<SyntaxRoot>> {
6 syntax: SyntaxNode<R>
7}
8
9impl File<Arc<SyntaxRoot>> {
10 pub fn parse(text: &str) -> Self {
11 File { syntax: ::parse(text.to_owned()) }
12 }
13}
14
15impl<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;
29mod yellow; 29mod yellow;
30/// Utilities for simple uses of the parser. 30/// Utilities for simple uses of the parser.
31pub mod utils; 31pub mod utils;
32pub mod ast;
32 33
33pub use { 34pub 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
40pub(crate) use yellow::SyntaxError; 42pub(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;
3mod red; 3mod red;
4mod syntax; 4mod syntax;
5 5
6pub use self::syntax::{SyntaxNode, SyntaxNodeRef}; 6pub use self::syntax::{SyntaxNode, SyntaxNodeRef, SyntaxRoot, TreeRoot};
7pub(crate) use self::{ 7pub(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};