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