Skip to main content

oak_d2/parser/
mod.rs

1pub mod element_type;
2
3use crate::{language::D2Language, lexer::D2Lexer};
4use oak_core::{
5    GreenNode, Parser,
6    parser::{ParseCache, ParseOutput, parse_with_lexer},
7    source::{Source, TextEdit},
8};
9
10pub struct D2Parser<'config> {
11    config: &'config D2Language,
12}
13
14impl<'config> D2Parser<'config> {
15    pub fn new(config: &'config D2Language) -> Self {
16        Self { config }
17    }
18}
19
20impl<'config> Parser<D2Language> for D2Parser<'config> {
21    fn parse<'a, S: Source + ?Sized>(&self, text: &'a S, edits: &[TextEdit], cache: &'a mut impl ParseCache<D2Language>) -> ParseOutput<'a, D2Language> {
22        let lexer = D2Lexer::new(self.config);
23        parse_with_lexer(&lexer, text, edits, cache, |state| {
24            let root = GreenNode::new(element_type::D2ElementType::Root, &[]);
25            Ok(state.arena().alloc(root))
26        })
27    }
28}