Skip to main content

oak_dhall/parser/
mod.rs

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