Skip to main content

oak_groovy/parser/
mod.rs

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