Skip to main content

oak_voml/parser/
mod.rs

1#![doc = include_str!("readme.md")]
2
3pub mod element_type;
4
5use crate::{language::VomlLanguage, lexer::VomlLexer};
6use oak_core::{
7    Source, TextEdit,
8    parser::{ParseCache, ParseOutput, Parser, ParserState, parse_with_lexer},
9};
10
11/// A parser for the Voml language.
12pub struct VomlParser<'config> {
13    /// The Voml language configuration.
14    pub(crate) config: &'config VomlLanguage,
15}
16
17impl<'config> VomlParser<'config> {
18    /// Creates a new `VomlParser` with the given configuration.
19    pub fn new(config: &'config VomlLanguage) -> Self {
20        Self { config }
21    }
22}
23
24impl<'config> Parser<VomlLanguage> for VomlParser<'config> {
25    fn parse<'a, S: Source + ?Sized>(&self, source: &'a S, edits: &[TextEdit], cache: &'a mut impl ParseCache<VomlLanguage>) -> ParseOutput<'a, VomlLanguage> {
26        let lexer = VomlLexer::new(self.config);
27        parse_with_lexer(&lexer, source, edits, cache, |state| {
28            let checkpoint = state.checkpoint();
29            while state.not_at_end() {
30                state.advance();
31            }
32            Ok(state.finish_at(checkpoint, element_type::VomlElementType::SourceFile))
33        })
34    }
35}