Skip to main content

oak_php/builder/
mod.rs

1use crate::{ast::PhpRoot, language::PhpLanguage, lexer::PhpLexer, parser::PhpParser};
2use oak_core::{Builder, BuilderCache, Lexer, OakDiagnostics, Parser, TextEdit, source::Source};
3
4/// A builder for PHP AST nodes.
5///
6/// This builder coordinates the lexing and parsing process to produce a `PhpRoot` AST.
7pub struct PhpBuilder<'config> {
8    config: &'config PhpLanguage,
9}
10
11impl<'config> PhpBuilder<'config> {
12    /// Creates a new `PhpBuilder` with the given language configuration.
13    pub fn new(config: &'config PhpLanguage) -> Self {
14        Self { config }
15    }
16}
17
18impl<'config> Builder<PhpLanguage> for PhpBuilder<'config> {
19    fn build<'a, S: Source + ?Sized>(&self, source: &S, edits: &[TextEdit], _cache: &'a mut impl BuilderCache<PhpLanguage>) -> oak_core::builder::BuildOutput<PhpLanguage> {
20        let parser = PhpParser::new(self.config);
21        let lexer = PhpLexer::new(self.config);
22        let mut session = oak_core::parser::session::ParseSession::<PhpLanguage>::default();
23        lexer.lex(source, edits, &mut session);
24        let parse_result = parser.parse(source, edits, &mut session);
25
26        match parse_result.result {
27            Ok(_) => {
28                // Placeholder for AST building
29                OakDiagnostics { result: Ok(PhpRoot { items: vec![] }), diagnostics: parse_result.diagnostics }
30            }
31            Err(e) => OakDiagnostics { result: Err(e), diagnostics: parse_result.diagnostics },
32        }
33    }
34}