Skip to main content

oak_vala/parser/
mod.rs

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