Skip to main content

oak_toml/parser/
mod.rs

1use crate::{kind::TomlSyntaxKind, language::TomlLanguage};
2use oak_core::{Parser, source::Source};
3
4/// TOML 语言解析器
5pub struct TomlParser<'config> {
6    /// 语言配置
7    pub(crate) config: &'config TomlLanguage,
8}
9
10impl<'config> TomlParser<'config> {
11    pub fn new(config: &'config TomlLanguage) -> Self {
12        Self { config }
13    }
14}
15
16impl<'config> Parser<TomlLanguage> for TomlParser<'config> {
17    fn parse<'a, S: Source + ?Sized>(&self, text: &'a S, edits: &[oak_core::TextEdit], cache: &'a mut impl oak_core::ParseCache<TomlLanguage>) -> oak_core::ParseOutput<'a, TomlLanguage> {
18        let lexer = crate::lexer::TomlLexer::new(&self.config);
19        oak_core::parser::parse_with_lexer(&lexer, text, edits, cache, |state| {
20            let checkpoint = state.checkpoint();
21
22            // 简单的解析逻辑:消耗所有 token 并放入 Root 节点
23            while state.current().is_some() {
24                state.bump();
25            }
26
27            Ok(state.finish_at(checkpoint, TomlSyntaxKind::Root.into()))
28        })
29    }
30}