Skip to main content

oak_delphi/parser/
mod.rs

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