oak_json/parser/
mod.rs

1use crate::{kind::JsonSyntaxKind, language::JsonLanguage};
2use oak_core::{OakError, Parser, ParserState, Source, TextEdit, TokenType};
3
4pub(crate) type State<'a, S> = ParserState<'a, JsonLanguage, S>;
5
6/// JSON 语言解析器
7pub struct JsonParser<'config> {
8    /// 语言配置
9    pub(crate) config: &'config JsonLanguage,
10}
11
12impl<'config> JsonParser<'config> {
13    pub fn new(config: &'config JsonLanguage) -> Self {
14        Self { config }
15    }
16
17    pub(crate) fn parse_value<'a, S: Source + ?Sized>(&self, state: &mut State<'a, S>) -> Result<(), OakError> {
18        self.skip_trivia(state);
19        let token = if let Some(t) = state.current() {
20            if t.kind == JsonSyntaxKind::Eof {
21                return Err(state.unexpected_eof());
22            }
23            t
24        }
25        else {
26            return Err(state.unexpected_eof());
27        };
28
29        let kind = match token.kind {
30            JsonSyntaxKind::LeftBrace => JsonSyntaxKind::Object,
31            JsonSyntaxKind::LeftBracket => JsonSyntaxKind::Array,
32            JsonSyntaxKind::StringLiteral => JsonSyntaxKind::String,
33            JsonSyntaxKind::NumberLiteral => JsonSyntaxKind::Number,
34            JsonSyntaxKind::BooleanLiteral => JsonSyntaxKind::Boolean,
35            JsonSyntaxKind::NullLiteral => JsonSyntaxKind::Null,
36            _ => {
37                if self.config.bare_keys && token.kind == JsonSyntaxKind::BareKey {
38                    state.record_unexpected_token(format!("{:?}", token.kind));
39                    return Err(state.errors.last().unwrap().clone());
40                }
41                state.record_unexpected_token(format!("{:?}", token.kind));
42                return Err(state.errors.last().unwrap().clone());
43            }
44        };
45
46        state.incremental_node(kind, |state| match kind {
47            JsonSyntaxKind::Object => self.parse_object(state),
48            JsonSyntaxKind::Array => self.parse_array(state),
49            _ => {
50                state.bump();
51                Ok(())
52            }
53        })
54    }
55
56    fn parse_object<'a, S: Source + ?Sized>(&self, state: &mut State<'a, S>) -> Result<(), OakError> {
57        if !state.eat(JsonSyntaxKind::LeftBrace) {
58            state.record_expected("{");
59            return Err(state.errors.last().cloned().expect("Error should have been recorded"));
60        }
61
62        let mut first = true;
63        while state.not_at_end() {
64            self.skip_trivia(state);
65            if state.at(JsonSyntaxKind::RightBrace) {
66                break;
67            }
68
69            if state.at(JsonSyntaxKind::Eof) {
70                return Err(state.unexpected_eof());
71            }
72
73            if !first {
74                if !state.eat(JsonSyntaxKind::Comma) {
75                    state.record_expected(",");
76                    // Don't break here to try recover
77                    break;
78                }
79
80                self.skip_trivia(state);
81                if state.at(JsonSyntaxKind::RightBrace) {
82                    if !self.config.trailing_comma {
83                        state.record_trailing_comma_not_allowed();
84                        return Err(state.errors.last().cloned().expect("Error should have been recorded"));
85                    }
86                    break;
87                }
88            }
89            first = false;
90
91            self.parse_object_entry(state)?;
92            self.skip_trivia(state);
93        }
94
95        if !state.eat(JsonSyntaxKind::RightBrace) {
96            // Check if we are at EOF, if so return unexpected EOF
97            if state.at(JsonSyntaxKind::Eof) || !state.not_at_end() {
98                return Err(state.unexpected_eof());
99            }
100            state.record_expected("}");
101        }
102        Ok(())
103    }
104
105    fn parse_object_entry<'a, S: Source + ?Sized>(&self, state: &mut State<'a, S>) -> Result<(), OakError> {
106        state.incremental_node(JsonSyntaxKind::ObjectEntry, |state| {
107            if state.at(JsonSyntaxKind::StringLiteral) || (self.config.bare_keys && state.at(JsonSyntaxKind::BareKey)) {
108                state.bump();
109            }
110            else {
111                state.record_expected("key");
112                return Err(state.errors.last().cloned().expect("Error should have been recorded"));
113            }
114
115            self.skip_trivia(state);
116            if !state.eat(JsonSyntaxKind::Colon) {
117                state.record_expected(":");
118            }
119            self.skip_trivia(state);
120            self.parse_value(state)?;
121            Ok(())
122        })
123    }
124
125    fn parse_array<'a, S: Source + ?Sized>(&self, state: &mut State<'a, S>) -> Result<(), OakError> {
126        if !state.eat(JsonSyntaxKind::LeftBracket) {
127            state.record_expected("[");
128            return Err(state.errors.last().cloned().expect("Error should have been recorded"));
129        }
130
131        let mut first = true;
132        while state.not_at_end() {
133            self.skip_trivia(state);
134            if state.at(JsonSyntaxKind::RightBracket) {
135                break;
136            }
137
138            if state.at(JsonSyntaxKind::Eof) {
139                return Err(state.unexpected_eof());
140            }
141
142            if !first {
143                if !state.eat(JsonSyntaxKind::Comma) {
144                    state.record_expected(",");
145                    break;
146                }
147
148                self.skip_trivia(state);
149                if state.at(JsonSyntaxKind::RightBracket) {
150                    if !self.config.trailing_comma {
151                        state.record_trailing_comma_not_allowed();
152                        return Err(state.errors.last().cloned().expect("Error should have been recorded"));
153                    }
154                    break;
155                }
156            }
157            first = false;
158
159            self.parse_value(state)?;
160            self.skip_trivia(state);
161        }
162
163        if !state.eat(JsonSyntaxKind::RightBracket) {
164            if state.at(JsonSyntaxKind::Eof) || !state.not_at_end() {
165                return Err(state.unexpected_eof());
166            }
167            state.record_expected("]");
168        }
169        Ok(())
170    }
171
172    fn skip_trivia<'a, S: Source + ?Sized>(&self, state: &mut State<'a, S>) {
173        while let Some(token) = state.current() {
174            if token.kind.is_ignored() {
175                state.bump();
176            }
177            else {
178                break;
179            }
180        }
181    }
182}
183
184impl<'config> Parser<JsonLanguage> for JsonParser<'config> {
185    fn parse<'a, S: Source + ?Sized>(&self, text: &'a S, edits: &[TextEdit], cache: &'a mut impl oak_core::ParseCache<JsonLanguage>) -> oak_core::ParseOutput<'a, JsonLanguage> {
186        let lexer = crate::lexer::JsonLexer::new(self.config);
187        oak_core::parser::parse_with_lexer(&lexer, text, edits, cache, |state| {
188            let checkpoint = state.checkpoint();
189            let res = self.parse_value(state);
190            if res.is_err() {
191                // If parsing fails, we might still want to return a partial tree or an error tree
192                // But parse_value handles errors by returning Result<(), OakError>
193                // And it records errors in state.errors
194            }
195
196            // Ensure we consume all remaining trivia/whitespace
197            while state.not_at_end() {
198                if let Some(token) = state.current() {
199                    if token.kind.is_ignored() {
200                        state.bump(); // Changed from state.advance() to state.bump() to include in tree
201                        continue;
202                    }
203                }
204                break;
205            }
206
207            let root = state.finish_at(checkpoint, JsonSyntaxKind::Root);
208            Ok(root)
209        })
210    }
211}