1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
use crate::ast::*;
use crate::errors::MomoaError;
use crate::location::*;
use crate::tokens::*;
use crate::Mode;
use std::collections::HashMap;
//-----------------------------------------------------------------------------
// Options
//-----------------------------------------------------------------------------
pub struct ParserOptions {
pub allow_trailing_commas: bool,
}
impl Default for ParserOptions {
fn default() -> Self {
ParserOptions {
allow_trailing_commas: false,
}
}
}
//-----------------------------------------------------------------------------
// Parser
//-----------------------------------------------------------------------------
struct Parser<'a> {
text: &'a str,
it: Tokens<'a>,
loc: Location,
tokens: Vec<Token>,
peeked: Option<Token>,
options: ParserOptions,
}
impl<'a> Parser<'a> {
pub fn new(text: &'a str, mode: Mode, options: Option<ParserOptions>) -> Self {
Parser {
text,
it: Tokens::new(text, mode),
tokens: Vec::new(),
loc: Location {
line: 1,
column: 1,
offset: 0,
},
peeked: None,
options: options.unwrap_or_default(),
}
}
fn get_value_loc(&self, value: &Node) -> LocationRange {
match value {
Node::Document(d) => d.loc,
Node::Array(array) => array.loc,
Node::Boolean(b) => b.loc,
Node::Element(e) => e.loc,
Node::Member(m) => m.loc,
Node::Number(n) => n.loc,
Node::Null(n) => n.loc,
Node::Object(o) => o.loc,
Node::String(s) => s.loc,
}
}
/// Parses the text contained in the parser into a `Node`.
pub fn parse(&mut self) -> Result<Node, MomoaError> {
let body = self.parse_value()?;
let loc = self.get_value_loc(&body);
/*
* For regular JSON, there should be no further tokens; JSONC may have
* comments after the body.
*/
while let Some(token_result) = self.next_token() {
return Err(match token_result {
Ok(token)
if token.kind == TokenKind::LineComment
|| token.kind == TokenKind::BlockComment =>
{
continue
}
Ok(token) => MomoaError::UnexpectedToken {
unexpected: token.kind,
line: token.loc.start.line,
column: token.loc.start.column,
},
Err(error) => error,
});
}
Ok(Node::Document(Box::new(DocumentNode {
body,
loc,
tokens: self.tokens.clone(),
})))
}
fn parse_value(&mut self) -> Result<Node, MomoaError> {
// while loop instead of if because we need to account for comments
while let Some(token_result) = self.peek_token() {
match token_result {
Ok(token) => match token.kind {
TokenKind::LBrace => return self.parse_object(),
TokenKind::LBracket => return self.parse_array(),
TokenKind::Boolean => return self.parse_boolean(),
TokenKind::Number => return self.parse_number(),
TokenKind::Null => return self.parse_null(),
TokenKind::String => return self.parse_string(),
_ => panic!("Not implemented"),
},
Err(error) => return Err(error),
}
}
// otherwise we've hit an unexpected end of input
Err(MomoaError::UnexpectedEndOfInput {
line: self.loc.line,
column: self.loc.column,
})
}
/// Advances to the next token without returning it.
fn eat_token(&mut self) {
self.next_token();
}
/// Advances to the next token and returns it or errors.
fn next_token(&mut self) -> Option<Result<Token, MomoaError>> {
if let Some(token) = self.peeked {
self.peeked = None;
return Some(Ok(token));
}
self.it.next()
}
/// Returns the next token or error without advancing the iterator.
/// Muliple calls always return the same result.
fn peek_token(&mut self) -> Option<Result<Token, MomoaError>> {
// if there's a peeked token, return it and don't overwrite it
if let Some(token) = self.peeked {
return Some(Ok(token));
}
// if there's no peeked token, try to get a new one
while let Some(token_result) = self.it.next() {
match token_result {
/*
* JSON vs. JSONC: Only the JSONC tokenization will return
* a comment. The JSON tokenization throws an error if it
* finds a comment, so it's safe to not verify if comments
* are allowed here.
*/
Ok(token)
if token.kind == TokenKind::LineComment
|| token.kind == TokenKind::BlockComment =>
{
self.loc = token.loc.start;
self.tokens.push(token);
continue;
}
Ok(token) => {
self.peeked = Some(token);
return Some(Ok(token));
}
Err(error) => {
return Some(Err(error));
}
}
}
None
}
/// Advance only if the next token matches the given `kind`.
fn maybe_match(&mut self, kind: TokenKind) -> Option<Result<Token, MomoaError>> {
// check to see if there's another result coming from the iterator
while let Some(next_token_result) = self.peek_token() {
match next_token_result {
Ok(next_token) => match next_token.kind {
_k if _k == kind => {
self.eat_token();
self.loc = next_token.loc.start;
self.tokens.push(next_token);
return Some(Ok(next_token));
}
_ => return None,
},
Err(error) => return Some(Err(error)),
}
}
// otherwise we didn't match anything
None
}
/// Advance to the next token and throw an error if it doesn't match
/// `kind`.
fn must_match(&mut self, kind: TokenKind) -> Result<Token, MomoaError> {
// check if there is a token first
if let Some(next_token_result) = self.next_token() {
let next_token = next_token_result.unwrap();
if next_token.kind == kind {
self.loc = next_token.loc.start;
self.tokens.push(next_token);
return Ok(next_token);
}
return Err(MomoaError::UnexpectedToken {
unexpected: next_token.kind,
line: next_token.loc.start.line,
column: next_token.loc.start.column,
});
}
Err(MomoaError::UnexpectedEndOfInput {
line: self.loc.line,
column: self.loc.column,
})
}
fn get_text(&self, start: usize, end: usize) -> &str {
&self.text[start..end]
}
fn parse_boolean(&mut self) -> Result<Node, MomoaError> {
let token = self.must_match(TokenKind::Boolean)?;
let text = self.get_text(token.loc.start.offset, token.loc.end.offset);
let value = text == "true";
return Ok(Node::Boolean(Box::new(ValueNode {
value,
loc: token.loc,
})));
}
fn parse_number(&mut self) -> Result<Node, MomoaError> {
let token = self.must_match(TokenKind::Number)?;
let text = self.get_text(token.loc.start.offset, token.loc.end.offset);
let value = text.parse::<f64>().unwrap();
return Ok(Node::Number(Box::new(ValueNode {
value,
loc: token.loc,
})));
}
fn parse_null(&mut self) -> Result<Node, MomoaError> {
let token = self.must_match(TokenKind::Null)?;
return Ok(Node::Null(Box::new(NullNode { loc: token.loc })));
}
fn parse_string(&mut self) -> Result<Node, MomoaError> {
let token = self.must_match(TokenKind::String)?;
let text = self.get_text(token.loc.start.offset, token.loc.end.offset);
// TODO: Find a way to move this elsewhere
// for easier lookup of token kinds for characters
let escaped_chars: HashMap<&char, char> = HashMap::from([
(&'"', '"'),
(&'\\', '\\'),
(&'/', '/'),
(&'b', '\u{0008}'),
(&'f', '\u{000c}'),
(&'n', '\n'),
(&'r', '\r'),
(&'t', '\t'),
]);
/*
* Because we are building up a string, we want to avoid unnecessary
* reallocations as data is added. So we create a string with an initial
* capacity of the length of the text minus 2 (for the two quote
* characters), which will always be enough room to represent the string
* value.
*/
let mut value = String::with_capacity(text.len() - 2);
/*
* We need to build up a string from the characters because we need to
* interpret certain escape characters that may occur inside the string
* like \t and \n. We know that all escape sequences are valid because
* the tokenizer would have already thrown an error otherwise.
*/
let mut it = text.trim_matches('"').chars();
while let Some(c) = &it.next() {
match c {
'\\' => {
// will never be false, just need to grab the character
if let Some(nc) = &it.next() {
match nc {
// read hexadecimals
'u' => {
let mut hex_sequence = String::with_capacity(4);
for _ in 0..4 {
match &it.next() {
Some(hex_digit) => hex_sequence.push(*hex_digit),
_ => panic!("Should never reach here."),
}
}
let char_code =
u32::from_str_radix(hex_sequence.as_str(), 16).unwrap();
// actually safe because we can't have an invalid hex sequence at this point
let unicode_char = unsafe { char::from_u32_unchecked(char_code) };
value.push_str(format!("{}", unicode_char).as_str());
}
c => match escaped_chars.get(c) {
Some(nc) => value.push(*nc),
_ => {}
},
}
}
}
c => {
value.push(*c);
}
}
}
return Ok(Node::String(Box::new(ValueNode {
value,
loc: token.loc,
})));
}
/// Parses arrays in the format of [value, value].
fn parse_array(&mut self) -> Result<Node, MomoaError> {
let start;
let end;
match self.must_match(TokenKind::LBracket) {
Ok(token) => start = token.loc.start,
Err(error) => return Err(error),
}
let mut elements = Vec::<Node>::new();
let mut comma_dangle = false;
while let Some(peek_token_result) = self.peek_token() {
match peek_token_result {
Ok(token) if token.kind == TokenKind::Comma => {
return Err(MomoaError::UnexpectedToken {
unexpected: token.kind,
line: token.loc.start.line,
column: token.loc.start.column,
})
}
Ok(token) if token.kind == TokenKind::RBracket => {
if comma_dangle && !self.options.allow_trailing_commas {
return Err(MomoaError::UnexpectedToken {
unexpected: token.kind,
line: token.loc.start.line,
column: token.loc.start.column,
});
}
break;
}
Ok(_) => {
let value = self.parse_value()?;
elements.push(Node::Element(Box::new(ValueNode {
loc: self.get_value_loc(&value),
value,
})));
}
Err(error) => return Err(error),
}
// only a comma or right bracket is valid here
comma_dangle = self.maybe_match(TokenKind::Comma).is_some();
if !comma_dangle && !self.options.allow_trailing_commas {
break;
}
}
// now there must be a right bracket
let rbracket = self.must_match(TokenKind::RBracket)?;
end = rbracket.loc.end;
return Ok(Node::Array(Box::new(ArrayNode {
elements,
loc: LocationRange { start, end },
})));
}
/// Parses objects in teh format of { "key": value, "key": value }.
fn parse_object(&mut self) -> Result<Node, MomoaError> {
let start;
let end;
let lbrace = self.must_match(TokenKind::LBrace)?;
start = lbrace.loc.start;
let mut members = Vec::<Node>::new();
let mut comma_dangle = false;
while let Some(peek_token_result) = self.peek_token() {
match peek_token_result {
Ok(token) if token.kind == TokenKind::Comma => {
return Err(MomoaError::UnexpectedToken {
unexpected: token.kind,
line: token.loc.start.line,
column: token.loc.start.column,
})
}
Ok(token) if token.kind == TokenKind::RBrace => {
if comma_dangle && !self.options.allow_trailing_commas {
return Err(MomoaError::UnexpectedToken {
unexpected: token.kind,
line: token.loc.start.line,
column: token.loc.start.column,
});
}
break;
}
Ok(_) => {
// name: value
let name = self.parse_string()?;
self.must_match(TokenKind::Colon)?;
let value = self.parse_value()?;
members.push(Node::Member(Box::new(MemberNode {
loc: LocationRange {
start: self.get_value_loc(&name).start,
end: self.get_value_loc(&value).end,
},
name,
value,
})));
}
Err(error) => return Err(error),
}
// only a comma or right bracket is valid here
comma_dangle = self.maybe_match(TokenKind::Comma).is_some();
if !comma_dangle && !self.options.allow_trailing_commas {
break;
}
}
// now there must be a right bracket
let rbracket = self.must_match(TokenKind::RBrace)?;
end = rbracket.loc.end;
return Ok(Node::Object(Box::new(ObjectNode {
members,
loc: LocationRange { start, end },
})));
}
}
pub fn parse(text: &str, mode: Mode, options: Option<ParserOptions>) -> Result<Node, MomoaError> {
let mut parser = Parser::new(text, mode, options);
parser.parse()
}