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