Skip to main content

oak_idl/parser/
mod.rs

1/// Element types for the IDL language.
2pub mod element_type;
3
4use crate::{language::IdlLanguage, lexer::IdlLexer, parser::element_type::IdlElementType};
5use oak_core::{
6    TextEdit,
7    parser::{ParseCache, ParseOutput, Parser, parse_with_lexer},
8    source::Source,
9};
10
11/// A parser for the IDL language.
12pub struct IdlParser<'config> {
13    pub(crate) config: &'config IdlLanguage,
14}
15
16impl<'config> IdlParser<'config> {
17    /// Creates a new IDL parser with the given configuration.
18    pub fn new(config: &'config IdlLanguage) -> Self {
19        Self { config }
20    }
21}
22
23impl<'config> Parser<IdlLanguage> for IdlParser<'config> {
24    fn parse<'a, S: Source + ?Sized>(&self, text: &'a S, edits: &[TextEdit], cache: &'a mut impl ParseCache<IdlLanguage>) -> ParseOutput<'a, IdlLanguage> {
25        let lexer = IdlLexer::new(&self.config);
26        parse_with_lexer(&lexer, text, edits, cache, |state| {
27            let checkpoint = state.checkpoint();
28
29            while state.not_at_end() {
30                state.bump();
31            }
32
33            Ok(state.finish_at(checkpoint, IdlElementType::Module))
34        })
35    }
36}