Skip to main content

oak_crystal/parser/
mod.rs

1#![doc = include_str!("readme.md")]
2
3pub mod element_type;
4use crate::language::CrystalLanguage;
5pub use element_type::CrystalElementType;
6use oak_core::{
7    TextEdit,
8    parser::{ParseCache, ParseOutput, Parser, ParserState, parse_with_lexer},
9    source::Source,
10};
11
12pub(crate) type State<'a, S> = ParserState<'a, CrystalLanguage, S>;
13
14/// Parser for the Crystal language.
15pub struct CrystalParser<'config> {
16    pub(crate) config: &'config CrystalLanguage,
17}
18
19impl<'config> CrystalParser<'config> {
20    /// Creates a new `CrystalParser` with the given configuration.
21    pub fn new(config: &'config CrystalLanguage) -> Self {
22        Self { config }
23    }
24}
25
26impl<'config> Parser<CrystalLanguage> for CrystalParser<'config> {
27    fn parse<'a, S: Source + ?Sized>(&self, text: &'a S, edits: &[TextEdit], cache: &'a mut impl ParseCache<CrystalLanguage>) -> ParseOutput<'a, CrystalLanguage> {
28        let lexer = crate::lexer::CrystalLexer::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.bump()
34            }
35
36            Ok(state.finish_at(checkpoint, CrystalElementType::SourceFile))
37        })
38    }
39}