Skip to main content

oak_actionscript/builder/
mod.rs

1use crate::{
2    ast::*,
3    language::ActionScriptLanguage,
4    parser::{ActionScriptElementType, ActionScriptParser},
5};
6use oak_core::{Builder, BuilderCache, GreenNode, Lexer, OakDiagnostics, OakError, Parser, RedNode, RedTree, SourceText, TextEdit, source::Source};
7
8/// ActionScript 语言的 AST 构建器
9#[derive(Clone)]
10pub struct ActionScriptBuilder<'config> {
11    /// 语言配置
12    config: &'config ActionScriptLanguage,
13}
14
15impl<'config> ActionScriptBuilder<'config> {
16    /// 创建新的 ActionScript 构建器
17    pub fn new(config: &'config ActionScriptLanguage) -> Self {
18        Self { config }
19    }
20}
21
22impl<'config> Builder<ActionScriptLanguage> for ActionScriptBuilder<'config> {
23    fn build<'a, S: Source + ?Sized>(&self, source: &S, edits: &[TextEdit], _cache: &'a mut impl BuilderCache<ActionScriptLanguage>) -> oak_core::builder::BuildOutput<ActionScriptLanguage> {
24        let parser = ActionScriptParser::new(self.config);
25        let lexer = crate::lexer::ActionScriptLexer::new(&self.config);
26
27        let mut cache = oak_core::parser::session::ParseSession::<ActionScriptLanguage>::default();
28        lexer.lex(source, edits, &mut cache);
29        let parse_result = parser.parse(source, edits, &mut cache);
30
31        match parse_result.result {
32            Ok(green_tree) => {
33                let source_text = SourceText::new(source.get_text_in((0..source.length()).into()).into_owned());
34                match self.build_root(green_tree.clone(), &source_text) {
35                    Ok(ast_root) => OakDiagnostics { result: Ok(ast_root), diagnostics: parse_result.diagnostics },
36                    Err(build_error) => {
37                        let mut diagnostics = parse_result.diagnostics;
38                        diagnostics.push(build_error.clone());
39                        OakDiagnostics { result: Err(build_error), diagnostics }
40                    }
41                }
42            }
43            Err(parse_error) => OakDiagnostics { result: Err(parse_error), diagnostics: parse_result.diagnostics },
44        }
45    }
46}
47
48impl<'config> ActionScriptBuilder<'config> {
49    /// 构建根节点
50    pub(crate) fn build_root(&self, green_tree: GreenNode<ActionScriptLanguage>, _source: &SourceText) -> Result<ActionScriptRoot, OakError> {
51        let red_root = RedNode::new(&green_tree, 0);
52        let mut items = Vec::new();
53
54        for child in red_root.children() {
55            match child {
56                RedTree::Node(n) => match n.green.kind {
57                    ActionScriptElementType::Class => items.push(ActionScriptItem::Class),
58                    ActionScriptElementType::Interface => items.push(ActionScriptItem::Interface),
59                    ActionScriptElementType::Function => items.push(ActionScriptItem::Function),
60                    _ => {}
61                },
62                _ => {}
63            }
64        }
65        Ok(ActionScriptRoot { items })
66    }
67}