Skip to main content

oak_python/parser/
mod.rs

1use crate::{language::PythonLanguage, lexer::PythonLexer};
2use oak_core::{
3    Source,
4    parser::{ParseCache, ParseOutput, Parser, parse_with_lexer},
5    source::TextEdit,
6};
7
8mod element_type;
9mod parse_comprehensions;
10mod parse_expressions;
11mod parse_statements;
12mod parse_utils;
13
14pub use element_type::PythonElementType;
15
16/// Python parser.
17pub struct PythonParser<'config> {
18    /// The Python language configuration.
19    config: &'config PythonLanguage,
20}
21
22impl<'config> PythonParser<'config> {
23    /// Create a new Python parser.
24    pub fn new(config: &'config PythonLanguage) -> Self {
25        Self { config }
26    }
27}
28
29impl<'config> Parser<PythonLanguage> for PythonParser<'config> {
30    fn parse<'a, S: Source + ?Sized>(&self, text: &'a S, edits: &[TextEdit], cache: &'a mut impl ParseCache<PythonLanguage>) -> ParseOutput<'a, PythonLanguage> {
31        let lexer = PythonLexer::new(self.config);
32        parse_with_lexer(&lexer, text, edits, cache, |state| {
33            let cp = state.checkpoint();
34            while !state.at(crate::lexer::PythonTokenType::Eof) {
35                self.parse_statement(state);
36                self.skip_trivia(state);
37            }
38            Ok(state.finish_at(cp, PythonElementType::Module))
39        })
40    }
41}