Skip to main content

oak_vlang/parser/
mod.rs

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