Skip to main content

oak_vampire/parser/
mod.rs

1use crate::{kind::VampireSyntaxKind, language::VampireLanguage, lexer::VampireLexer};
2use oak_core::{
3    GreenNode, OakError,
4    parser::{ParseCache, ParseOutput, Parser, ParserState, parse_with_lexer},
5    source::{Source, TextEdit},
6};
7
8pub(crate) type State<'a, S> = ParserState<'a, VampireLanguage, S>;
9
10pub struct VampireParser<'config> {
11    pub(crate) config: &'config VampireLanguage,
12}
13
14impl<'config> VampireParser<'config> {
15    pub fn new(config: &'config VampireLanguage) -> Self {
16        Self { config }
17    }
18
19    pub(crate) fn parse_root_internal<'a, S: Source + ?Sized>(&self, state: &mut State<'a, S>) -> Result<&'a GreenNode<'a, VampireLanguage>, OakError> {
20        let checkpoint = state.checkpoint();
21
22        while state.not_at_end() {
23            state.bump();
24        }
25
26        let root = state.finish_at(checkpoint, VampireSyntaxKind::Root.into());
27        Ok(root)
28    }
29}
30
31impl<'config> Parser<VampireLanguage> for VampireParser<'config> {
32    fn parse<'a, S: Source + ?Sized>(&self, text: &'a S, edits: &[TextEdit], cache: &'a mut impl ParseCache<VampireLanguage>) -> ParseOutput<'a, VampireLanguage> {
33        let lexer = VampireLexer::new(&self.config);
34        parse_with_lexer(&lexer, text, edits, cache, |state| self.parse_root_internal(state))
35    }
36}