emmylua_parser/syntax/tree/
lua_syntax_tree.rs

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
31
32
33
34
use rowan::GreenNode;

use crate::{
    parser_error::LuaParseError,
    syntax::{node::LuaChunk, traits::LuaAstNode},
    LuaSyntaxNode,
};

#[derive(Debug, Clone)]
pub struct LuaSyntaxTree {
    // store GreenNode instead of SyntaxNode, because SyntaxNode is not send and sync
    root: GreenNode,
    errors: Vec<LuaParseError>,
}

impl LuaSyntaxTree {
    pub fn new(root: GreenNode, errors: Vec<LuaParseError>) -> Self {
        LuaSyntaxTree { root, errors }
    }

    // get root node
    pub fn get_red_root(&self) -> LuaSyntaxNode {
        LuaSyntaxNode::new_root(self.root.clone())
    }

    // get chunk node, only can cast to LuaChunk
    pub fn get_chunk_node(&self) -> LuaChunk {
        LuaChunk::cast(self.get_red_root()).unwrap()
    }

    pub fn get_errors(&self) -> &[LuaParseError] {
        &self.errors
    }
}