aboutsummaryrefslogtreecommitdiff
path: root/src/ast.rs
blob: 3a92874669ac8a248d7ecd7835cfe0d978f890b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use std::sync::Arc;
use {SyntaxNode, TreeRoot, SyntaxRoot, SyntaxNodeRef};

#[derive(Debug)]
pub struct File<R: TreeRoot = Arc<SyntaxRoot>> {
    syntax: SyntaxNode<R>
}

impl File<Arc<SyntaxRoot>> {
    pub fn parse(text: &str) -> Self {
        File { syntax: ::parse(text.to_owned()) }
    }
}

impl<R: TreeRoot> File<R> {
    pub fn syntax(&self) -> SyntaxNode<R> {
        self.syntax.clone()
    }

    pub fn for_each_node(&self, mut f: impl FnMut(SyntaxNodeRef)) {
        let syntax = self.syntax();
        let syntax = syntax.borrow();
        go(syntax, &mut f);

        fn go(syntax: SyntaxNodeRef, f: &mut FnMut(SyntaxNodeRef)) {
            f(syntax);
            syntax.children().into_iter().for_each(f)
        }
    }
}