1
#![allow(clippy::identity_op)]
2

            
3
use alloc::{
4
    format,
5
    string::{String, ToString},
6
    vec::Vec,
7
};
8
use core::{
9
    char::from_u32 as char_from_u32,
10
    str::{self, from_utf8, FromStr, Utf8Error},
11
};
12

            
13
use unicode_ident::{is_xid_continue, is_xid_start};
14

            
15
use crate::{
16
    error::{Error, Position, Result, Span, SpannedError, SpannedResult},
17
    extensions::Extensions,
18
    value::Number,
19
};
20

            
21
2424491
const fn is_int_char(c: char) -> bool {
22
2424491
    c.is_ascii_hexdigit() || c == '_'
23
2424491
}
24

            
25
1338206
const fn is_float_char(c: char) -> bool {
26
1338206
    c.is_ascii_digit() || matches!(c, 'e' | 'E' | '.' | '+' | '-' | '_')
27
1338206
}
28

            
29
843727
pub fn is_ident_first_char(c: char) -> bool {
30
843727
    c == '_' || is_xid_start(c)
31
843727
}
32

            
33
4184438
pub fn is_ident_raw_char(c: char) -> bool {
34
4184438
    matches!(c, '.' | '+' | '-') | is_xid_continue(c)
35
4184438
}
36

            
37
5398820
pub const fn is_whitespace_char(c: char) -> bool {
38
3175374
    matches!(
39
5398820
        c,
40
        ' ' | '\t'
41
            | '\n'
42
            | '\r'
43
            | '\x0B'
44
            | '\x0C'
45
            | '\u{85}'
46
            | '\u{200E}'
47
            | '\u{200F}'
48
            | '\u{2028}'
49
            | '\u{2029}'
50
    )
51
5398820
}
52

            
53
#[cfg(feature = "integer128")]
54
pub(crate) type LargeUInt = u128;
55
#[cfg(not(feature = "integer128"))]
56
pub(crate) type LargeUInt = u64;
57
#[cfg(feature = "integer128")]
58
pub(crate) type LargeSInt = i128;
59
#[cfg(not(feature = "integer128"))]
60
pub(crate) type LargeSInt = i64;
61

            
62
pub struct Parser<'a> {
63
    /// Bits set according to the [`Extensions`] enum.
64
    pub exts: Extensions,
65
    src: &'a str,
66
    cursor: ParserCursor,
67
    prev_cursor: ParserCursor,
68
}
69

            
70
#[derive(Copy, Clone)] // GRCOV_EXCL_LINE
71
pub struct ParserCursor {
72
    cursor: usize,
73
    pre_ws_cursor: usize,
74
    last_ws_len: usize,
75
}
76

            
77
enum ParsedAttribute {
78
    None,
79
    Extensions(Extensions),
80
    Ignored,
81
}
82

            
83
const WS_CURSOR_UNCLOSED_LINE: usize = usize::MAX;
84

            
85
impl PartialEq for ParserCursor {
86
8
    fn eq(&self, other: &Self) -> bool {
87
8
        self.cursor == other.cursor
88
8
    }
89
}
90

            
91
impl PartialOrd for ParserCursor {
92
3388
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
93
3388
        self.cursor.partial_cmp(&other.cursor)
94
3388
    }
95
}
96

            
97
/// constructor and parsing utilities
98
impl<'a> Parser<'a> {
99
534084
    pub fn new(src: &'a str) -> SpannedResult<Self> {
100
534084
        let mut parser = Parser {
101
534084
            exts: Extensions::empty(),
102
534084
            src,
103
534084
            cursor: ParserCursor {
104
534084
                cursor: 0,
105
534084
                pre_ws_cursor: 0,
106
534084
                last_ws_len: 0,
107
534084
            },
108
534084
            prev_cursor: ParserCursor {
109
534084
                cursor: 0,
110
534084
                pre_ws_cursor: 0,
111
534084
                last_ws_len: 0,
112
534084
            },
113
534084
        };
114

            
115
534084
        parser.skip_ws().map_err(|e| parser.span_error(e))?;
116

            
117
        // Loop over all document attributes
118
        loop {
119
568186
            match parser.attribute().map_err(|e| parser.span_error(e))? {
120
533276
                ParsedAttribute::None => break,
121
34344
                ParsedAttribute::Extensions(extensions) => {
122
34344
                    parser.exts |= extensions;
123
34344
                }
124
36
                ParsedAttribute::Ignored => {}
125
            }
126

            
127
34380
            parser.skip_ws().map_err(|e| parser.span_error(e))?;
128
        }
129

            
130
533276
        Ok(parser)
131
534084
    }
132

            
133
176511
    fn set_cursor(&mut self, cursor: ParserCursor) {
134
176511
        self.cursor = cursor;
135
176511
    }
136

            
137
102670
    pub fn span_error(&self, code: Error) -> SpannedError {
138
102670
        SpannedError {
139
102670
            code,
140
102670
            span: Span {
141
102670
                start: Position::from_src_end(&self.src[..self.prev_cursor.cursor]),
142
102670
                end: Position::from_src_end(&self.src[..self.cursor.cursor]),
143
102670
            },
144
102670
        }
145
102670
    }
146

            
147
6198701
    pub fn advance_bytes(&mut self, bytes: usize) {
148
6198701
        self.prev_cursor = self.cursor;
149
6198701
        self.cursor.cursor += bytes;
150
6198701
    }
151

            
152
963567
    pub fn next_char(&mut self) -> Result<char> {
153
963567
        let c = self.peek_char_or_eof()?;
154
963274
        self.cursor.cursor += c.len_utf8();
155
963274
        Ok(c)
156
963567
    }
157

            
158
38834
    pub fn skip_next_char(&mut self) {
159
38834
        core::mem::drop(self.next_char());
160
38834
    }
161

            
162
2919058
    pub fn peek_char(&self) -> Option<char> {
163
2919058
        self.src().chars().next()
164
2919058
    }
165

            
166
2192690
    pub fn peek_char_or_eof(&self) -> Result<char> {
167
2192690
        self.peek_char().ok_or(Error::Eof)
168
2192690
    }
169

            
170
6383108
    pub fn check_char(&self, c: char) -> bool {
171
6383108
        self.src().starts_with(c)
172
6383108
    }
173

            
174
13119856
    pub fn check_str(&self, s: &str) -> bool {
175
13119856
        self.src().starts_with(s)
176
13119856
    }
177

            
178
39387121
    pub fn src(&self) -> &'a str {
179
39387121
        &self.src[self.cursor.cursor..]
180
39387121
    }
181

            
182
25894
    pub fn pre_ws_src(&self) -> &'a str {
183
25894
        &self.src[self.cursor.pre_ws_cursor..]
184
25894
    }
185

            
186
1692992
    pub fn consume_str(&mut self, s: &str) -> bool {
187
1692992
        if self.check_str(s) {
188
484763
            self.advance_bytes(s.len());
189

            
190
484763
            true
191
        } else {
192
1208229
            false
193
        }
194
1692992
    }
195

            
196
4928712
    pub fn consume_char(&mut self, c: char) -> bool {
197
4928712
        if self.check_char(c) {
198
1494226
            self.advance_bytes(c.len_utf8());
199

            
200
1494226
            true
201
        } else {
202
3434486
            false
203
        }
204
4928712
    }
205

            
206
69269
    fn consume_all(&mut self, all: &[&str]) -> Result<bool> {
207
69269
        all.iter()
208
173448
            .map(|elem| {
209
173448
                if self.consume_str(elem) {
210
173409
                    self.skip_ws()?;
211

            
212
173409
                    Ok(true)
213
                } else {
214
39
                    Ok(false)
215
                }
216
173448
            })
217
173448
            .try_fold(true, |acc, x| x.map(|x| x && acc))
218
69269
    }
219

            
220
145379
    pub fn expect_char(&mut self, expected: char, error: Error) -> Result<()> {
221
145379
        if self.consume_char(expected) {
222
126148
            Ok(())
223
        } else {
224
19231
            Err(error)
225
        }
226
145379
    }
227

            
228
    #[must_use]
229
4031953
    pub fn next_chars_while_len(&self, condition: fn(char) -> bool) -> usize {
230
4031953
        self.next_chars_while_from_len(0, condition)
231
4031953
    }
232

            
233
    #[must_use]
234
5044246
    pub fn next_chars_while_from_len(&self, from: usize, condition: fn(char) -> bool) -> usize {
235
5044246
        self.src()[from..]
236
13387635
            .find(|c| !condition(c))
237
5044246
            .unwrap_or(self.src().len() - from)
238
5044246
    }
239
}
240

            
241
/// actual parsing of ron tokens
242
impl<'a> Parser<'a> {
243
215181
    fn parse_integer_digits<T: Num>(
244
215181
        &mut self,
245
215181
        s: &str,
246
215181
        base: u8,
247
215181
        f: fn(&mut T, u8) -> bool,
248
215181
    ) -> Result<T> {
249
215181
        let mut num_acc = T::from_u8(0);
250

            
251
529138
        for (i, c) in s.char_indices() {
252
529138
            if c == '_' {
253
6538
                continue;
254
522600
            }
255

            
256
522600
            if num_acc.checked_mul_ext(base) {
257
842
                self.advance_bytes(s.len());
258
842
                return Err(Error::IntegerOutOfBounds);
259
521758
            }
260

            
261
521758
            let digit = Self::decode_hex(c)?;
262

            
263
521758
            if digit >= base {
264
1946
                self.advance_bytes(i);
265
1946
                return Err(Error::InvalidIntegerDigit { digit: c, base });
266
519812
            }
267

            
268
519812
            if f(&mut num_acc, digit) {
269
3197
                self.advance_bytes(s.len());
270
3197
                return Err(Error::IntegerOutOfBounds);
271
516615
            }
272
        }
273

            
274
209196
        self.advance_bytes(s.len());
275

            
276
209196
        Ok(num_acc)
277
215181
    }
278

            
279
222167
    fn parse_integer<T: Num>(&mut self, sign: i8) -> Result<T> {
280
222167
        let base = match () {
281
222167
            () if self.consume_str("0b") => 2,
282
219665
            () if self.consume_str("0o") => 8,
283
217441
            () if self.consume_str("0x") => 16,
284
214939
            () => 10,
285
        };
286

            
287
222167
        let num_bytes = self.next_chars_while_len(is_int_char);
288

            
289
222167
        if num_bytes == 0 {
290
4484
            return Err(Error::ExpectedInteger);
291
217683
        }
292

            
293
217683
        if self.check_char('_') {
294
2502
            return Err(Error::UnderscoreAtBeginning);
295
215181
        }
296

            
297
215181
        let s = &self.src()[..num_bytes];
298

            
299
215181
        if sign > 0 {
300
200867
            self.parse_integer_digits(s, base, T::checked_add_ext)
301
        } else {
302
14314
            self.parse_integer_digits(s, base, T::checked_sub_ext)
303
        }
304
222167
    }
305

            
306
    #[allow(clippy::too_many_lines)]
307
318753
    pub fn integer<T: Integer>(&mut self) -> Result<T> {
308
318753
        let src_backup = self.src();
309

            
310
318753
        let is_negative = match self.peek_char_or_eof()? {
311
            '+' => {
312
28
                self.skip_next_char();
313
28
                false
314
            }
315
            '-' => {
316
4959
                self.skip_next_char();
317
4959
                true
318
            }
319
215176
            'b' if self.consume_str("b'") => {
320
                // Parse a byte literal
321
215176
                let byte = match self.next_char()? {
322
188762
                    '\\' => match self.parse_escape(EscapeEncoding::Binary, true)? {
323
                        // we know that this byte is an ASCII character
324
188484
                        EscapeCharacter::Ascii(b) => b,
325
                        EscapeCharacter::Utf8(_) => {
326
278
                            return Err(Error::InvalidEscape(
327
278
                                "Unexpected Unicode escape in byte literal",
328
278
                            ))
329
                        }
330
                    },
331
26414
                    b if b.is_ascii() => b as u8,
332
278
                    _ => return Err(Error::ExpectedByteLiteral),
333
                };
334

            
335
214620
                if !self.consume_char('\'') {
336
278
                    return Err(Error::ExpectedByteLiteral);
337
214342
                }
338

            
339
214342
                let bytes_ron = &src_backup[..src_backup.len() - self.src().len()];
340

            
341
214342
                return T::try_from_parsed_integer(ParsedInteger::U8(byte), bytes_ron);
342
            }
343
98590
            _ => false,
344
        };
345
103577
        let sign = if is_negative { -1 } else { 1 };
346

            
347
103577
        let num_bytes = self.next_chars_while_len(is_int_char);
348

            
349
103577
        if self.src()[num_bytes..].starts_with(['i', 'u']) {
350
11939
            let int_cursor = self.cursor;
351
11939
            self.advance_bytes(num_bytes);
352

            
353
            #[allow(clippy::never_loop)]
354
            loop {
355
11939
                let (res, suffix_bytes) = if self.consume_ident("i8") {
356
874
                    let suffix_bytes = self.src();
357
874
                    self.set_cursor(int_cursor);
358
874
                    (
359
874
                        self.parse_integer::<i8>(sign).map(ParsedInteger::I8),
360
874
                        suffix_bytes,
361
874
                    )
362
11065
                } else if self.consume_ident("i16") {
363
874
                    let suffix_bytes = self.src();
364
874
                    self.set_cursor(int_cursor);
365
874
                    (
366
874
                        self.parse_integer::<i16>(sign).map(ParsedInteger::I16),
367
874
                        suffix_bytes,
368
874
                    )
369
10191
                } else if self.consume_ident("i32") {
370
1156
                    let suffix_bytes = self.src();
371
1156
                    self.set_cursor(int_cursor);
372
1156
                    (
373
1156
                        self.parse_integer::<i32>(sign).map(ParsedInteger::I32),
374
1156
                        suffix_bytes,
375
1156
                    )
376
9035
                } else if self.consume_ident("i64") {
377
874
                    let suffix_bytes = self.src();
378
874
                    self.set_cursor(int_cursor);
379
874
                    (
380
874
                        self.parse_integer::<i64>(sign).map(ParsedInteger::I64),
381
874
                        suffix_bytes,
382
874
                    )
383
8161
                } else if self.consume_ident("u8") {
384
2260
                    let suffix_bytes = self.src();
385
2260
                    self.set_cursor(int_cursor);
386
2260
                    (
387
2260
                        self.parse_integer::<u8>(sign).map(ParsedInteger::U8),
388
2260
                        suffix_bytes,
389
2260
                    )
390
5901
                } else if self.consume_ident("u16") {
391
1156
                    let suffix_bytes = self.src();
392
1156
                    self.set_cursor(int_cursor);
393
1156
                    (
394
1156
                        self.parse_integer::<u16>(sign).map(ParsedInteger::U16),
395
1156
                        suffix_bytes,
396
1156
                    )
397
4745
                } else if self.consume_ident("u32") {
398
1156
                    let suffix_bytes = self.src();
399
1156
                    self.set_cursor(int_cursor);
400
1156
                    (
401
1156
                        self.parse_integer::<u32>(sign).map(ParsedInteger::U32),
402
1156
                        suffix_bytes,
403
1156
                    )
404
3589
                } else if self.consume_ident("u64") {
405
1156
                    let suffix_bytes = self.src();
406
1156
                    self.set_cursor(int_cursor);
407
1156
                    (
408
1156
                        self.parse_integer::<u64>(sign).map(ParsedInteger::U64),
409
1156
                        suffix_bytes,
410
1156
                    )
411
                } else {
412
                    #[cfg(feature = "integer128")]
413
1585
                    if self.consume_ident("i128") {
414
439
                        let suffix_bytes = self.src();
415
439
                        self.set_cursor(int_cursor);
416
439
                        (
417
439
                            self.parse_integer::<i128>(sign).map(ParsedInteger::I128),
418
439
                            suffix_bytes,
419
439
                        )
420
1146
                    } else if self.consume_ident("u128") {
421
580
                        let suffix_bytes = self.src();
422
580
                        self.set_cursor(int_cursor);
423
580
                        (
424
580
                            self.parse_integer::<u128>(sign).map(ParsedInteger::U128),
425
580
                            suffix_bytes,
426
580
                        )
427
                    } else {
428
566
                        break;
429
                    }
430
                    #[cfg(not(feature = "integer128"))]
431
                    {
432
848
                        break;
433
                    }
434
                };
435

            
436
10243
                if !matches!(
437
1829
                    &res,
438
                    Err(Error::UnderscoreAtBeginning | Error::InvalidIntegerDigit { .. })
439
10243
                ) {
440
10243
                    // Advance past the number suffix
441
10243
                    self.skip_identifier();
442
10243
                }
443

            
444
10525
                let integer_ron = &src_backup[..src_backup.len() - suffix_bytes.len()];
445

            
446
10525
                return res.and_then(|parsed| T::try_from_parsed_integer(parsed, integer_ron));
447
            }
448

            
449
1414
            self.set_cursor(int_cursor);
450
91638
        }
451

            
452
93052
        T::parse(self, sign)
453
318753
    }
454

            
455
307831
    pub fn any_number(&mut self) -> Result<Number> {
456
307831
        if self.next_bytes_is_float() {
457
10914
            return match self.float::<ParsedFloat>()? {
458
3384
                ParsedFloat::F32(v) => Ok(Number::F32(v.into())),
459
7530
                ParsedFloat::F64(v) => Ok(Number::F64(v.into())),
460
            };
461
296917
        }
462

            
463
296917
        let backup_cursor = self.cursor;
464

            
465
296917
        let (integer_err, integer_cursor) = match self.integer::<ParsedInteger>() {
466
293537
            Ok(integer) => {
467
293537
                return match integer {
468
846
                    ParsedInteger::I8(v) => Ok(Number::I8(v)),
469
560
                    ParsedInteger::I16(v) => Ok(Number::I16(v)),
470
560
                    ParsedInteger::I32(v) => Ok(Number::I32(v)),
471
560
                    ParsedInteger::I64(v) => Ok(Number::I64(v)),
472
                    #[cfg(feature = "integer128")]
473
558
                    ParsedInteger::I128(v) => Ok(Number::I128(v)),
474
287655
                    ParsedInteger::U8(v) => Ok(Number::U8(v)),
475
842
                    ParsedInteger::U16(v) => Ok(Number::U16(v)),
476
560
                    ParsedInteger::U32(v) => Ok(Number::U32(v)),
477
560
                    ParsedInteger::U64(v) => Ok(Number::U64(v)),
478
                    #[cfg(feature = "integer128")]
479
836
                    ParsedInteger::U128(v) => Ok(Number::U128(v)),
480
                }
481
            }
482
3380
            Err(err) => (err, self.cursor),
483
        };
484

            
485
3380
        self.set_cursor(backup_cursor);
486

            
487
        // Fall-back to parse an out-of-range integer as a float
488
3380
        match self.float::<ParsedFloat>() {
489
2816
            Ok(ParsedFloat::F32(v)) if self.cursor >= integer_cursor => Ok(Number::F32(v.into())),
490
564
            Ok(ParsedFloat::F64(v)) if self.cursor >= integer_cursor => Ok(Number::F64(v.into())),
491
            _ => {
492
                // Return the more precise integer error
493
1251
                self.set_cursor(integer_cursor);
494
1251
                Err(integer_err)
495
            }
496
        }
497
307831
    }
498

            
499
31008
    pub fn bool(&mut self) -> Result<bool> {
500
31008
        if self.consume_ident("true") {
501
17306
            Ok(true)
502
13702
        } else if self.consume_ident("false") {
503
13666
            Ok(false)
504
        } else {
505
36
            Err(Error::ExpectedBoolean)
506
        }
507
31008
    }
508

            
509
67815
    pub fn char(&mut self) -> Result<char> {
510
67815
        self.expect_char('\'', Error::ExpectedChar)?;
511

            
512
49144
        let c = self.next_char()?;
513

            
514
49144
        let c = if c == '\\' {
515
3626
            match self.parse_escape(EscapeEncoding::Utf8, true)? {
516
                // we know that this byte is an ASCII character
517
1680
                EscapeCharacter::Ascii(b) => char::from(b),
518
1112
                EscapeCharacter::Utf8(c) => c,
519
            }
520
        } else {
521
45518
            c
522
        };
523

            
524
48310
        self.expect_char('\'', Error::ExpectedChar)?;
525

            
526
48310
        Ok(c)
527
67815
    }
528

            
529
408589
    pub fn comma(&mut self) -> Result<bool> {
530
408589
        self.skip_ws()?;
531

            
532
408589
        if self.consume_char(',') {
533
237507
            self.skip_ws()?;
534

            
535
237507
            Ok(true)
536
        } else {
537
171082
            Ok(false)
538
        }
539
408589
    }
540

            
541
    /// Only returns true if the char after `ident` cannot belong
542
    /// to an identifier.
543
5670478
    pub fn check_ident(&mut self, ident: &str) -> bool {
544
5670478
        self.check_str(ident) && !self.check_ident_other_char(ident.len())
545
5670478
    }
546

            
547
280203
    fn check_ident_other_char(&self, index: usize) -> bool {
548
280203
        self.src()[index..]
549
280203
            .chars()
550
280203
            .next()
551
280203
            .map_or(false, is_xid_continue)
552
280203
    }
553

            
554
    /// Check which type of struct we are currently parsing. The parsing state
555
    ///  is only changed in case of an error, to provide a better position.
556
    ///
557
    /// [`NewtypeMode::NoParensMeanUnit`] detects (tuple) structs by a leading
558
    ///  opening bracket and reports a unit struct otherwise.
559
    /// [`NewtypeMode::InsideNewtype`] skips an initial check for unit structs,
560
    ///  and means that any leading opening bracket is not considered to open
561
    ///  a (tuple) struct but to be part of the structs inner contents.
562
    ///
563
    /// [`TupleMode::ImpreciseTupleOrNewtype`] only performs a cheap, O(1),
564
    ///  single-identifier lookahead check to distinguish tuple structs from
565
    ///  non-tuple structs.
566
    /// [`TupleMode::DifferentiateNewtype`] performs an expensive, O(N), look-
567
    ///  ahead over the entire next value tree, which can span the entirety of
568
    ///  the remaining document in the worst case.
569
78058
    pub fn check_struct_type(
570
78058
        &mut self,
571
78058
        newtype: NewtypeMode,
572
78058
        tuple: TupleMode,
573
78058
    ) -> Result<StructType> {
574
78058
        fn check_struct_type_inner(
575
78058
            parser: &mut Parser,
576
78058
            newtype: NewtypeMode,
577
78058
            tuple: TupleMode,
578
78058
        ) -> Result<StructType> {
579
78058
            if matches!(newtype, NewtypeMode::NoParensMeanUnit) && !parser.consume_char('(') {
580
12236
                return Ok(StructType::Unit);
581
65822
            }
582

            
583
65822
            parser.skip_ws()?;
584

            
585
            // Check for `Ident()`, which could be
586
            // - a zero-field struct or tuple (variant)
587
            // - an unwrapped newtype around a unit
588
65818
            if matches!(newtype, NewtypeMode::NoParensMeanUnit) && parser.check_char(')') {
589
834
                return Ok(StructType::EmptyTuple);
590
64984
            }
591

            
592
64984
            if parser.skip_identifier().is_some() {
593
46320
                parser.skip_ws()?;
594

            
595
46320
                match parser.peek_char() {
596
                    // Definitely a struct with named fields
597
40474
                    Some(':') => return Ok(StructType::Named),
598
                    // Definitely a tuple-like struct with fields
599
                    Some(',') => {
600
4178
                        parser.skip_next_char();
601
4178
                        parser.skip_ws()?;
602
4178
                        if parser.check_char(')') {
603
                            // A one-element tuple could be a newtype
604
                            return Ok(StructType::NewtypeTuple);
605
4178
                        }
606
                        // Definitely a tuple struct with more than one field
607
4178
                        return Ok(StructType::NonNewtypeTuple);
608
                    }
609
                    // Either a newtype or a tuple struct
610
1112
                    Some(')') => return Ok(StructType::NewtypeTuple),
611
                    // Something else, let's investigate further
612
556
                    Some(_) | None => (),
613
                };
614
18664
            }
615

            
616
19220
            if matches!(tuple, TupleMode::ImpreciseTupleOrNewtype) {
617
13089
                return Ok(StructType::AnyTuple);
618
6131
            }
619

            
620
6131
            let mut braces = 1_usize;
621
6131
            let mut more_than_one = false;
622

            
623
            // Skip ahead to see if the value is followed by another value
624
24231
            while braces > 0 {
625
                // Skip spurious braces in comments, strings, and characters
626
18671
                parser.skip_ws()?;
627
18671
                let cursor_backup = parser.cursor;
628
18671
                if parser.char().is_err() {
629
18671
                    parser.set_cursor(cursor_backup);
630
18671
                }
631
18671
                let cursor_backup = parser.cursor;
632
18671
                match parser.string() {
633
1112
                    Ok(_) => (),
634
                    // prevent quadratic complexity backtracking for unterminated string
635
                    Err(err @ (Error::ExpectedStringEnd | Error::Eof)) => return Err(err),
636
17559
                    Err(_) => parser.set_cursor(cursor_backup),
637
                }
638
18671
                let cursor_backup = parser.cursor;
639
                // we have already checked for strings, which subsume base64 byte strings
640
18671
                match parser.byte_string_no_base64() {
641
834
                    Ok(_) => (),
642
                    // prevent quadratic complexity backtracking for unterminated byte string
643
                    Err(err @ (Error::ExpectedStringEnd | Error::Eof)) => return Err(err),
644
17837
                    Err(_) => parser.set_cursor(cursor_backup),
645
                }
646

            
647
18671
                let c = parser.next_char()?;
648
18656
                if matches!(c, '(' | '[' | '{') {
649
1405
                    braces += 1;
650
17251
                } else if matches!(c, ')' | ']' | '}') {
651
6965
                    braces -= 1;
652
10301
                } else if c == ',' && braces == 1 {
653
556
                    parser.skip_ws()?;
654
556
                    more_than_one = !parser.check_char(')');
655
556
                    break;
656
9730
                }
657
            }
658

            
659
6116
            if more_than_one {
660
278
                Ok(StructType::NonNewtypeTuple)
661
            } else {
662
5838
                Ok(StructType::NewtypeTuple)
663
            }
664
78058
        }
665

            
666
        // Create a temporary working copy
667
78058
        let backup_cursor = self.cursor;
668

            
669
78058
        let result = check_struct_type_inner(self, newtype, tuple);
670

            
671
78058
        if result.is_ok() {
672
78039
            // Revert the parser to before the struct type check
673
78039
            self.set_cursor(backup_cursor);
674
78039
        }
675

            
676
78058
        result
677
78058
    }
678

            
679
    /// Only returns true if the char after `ident` cannot belong
680
    /// to an identifier.
681
5083425
    pub fn consume_ident(&mut self, ident: &str) -> bool {
682
5083425
        if self.check_ident(ident) {
683
150684
            self.advance_bytes(ident.len());
684

            
685
150684
            true
686
        } else {
687
4932741
            false
688
        }
689
5083425
    }
690

            
691
79338
    pub fn consume_struct_name(&mut self, ident: &'static str) -> Result<bool> {
692
79338
        if self.check_ident("") {
693
62329
            if self.exts.contains(Extensions::EXPLICIT_STRUCT_NAMES) {
694
834
                return Err(Error::ExpectedStructName(ident.to_string()));
695
61495
            }
696

            
697
61495
            return Ok(false);
698
17009
        }
699

            
700
17009
        let found_ident = match self.identifier() {
701
15063
            Ok(maybe_ident) => maybe_ident,
702
1390
            Err(Error::SuggestRawIdentifier(found_ident)) if found_ident == ident => {
703
278
                return Err(Error::SuggestRawIdentifier(found_ident))
704
            }
705
1668
            Err(_) => return Err(Error::ExpectedNamedStructLike(ident)),
706
        };
707

            
708
15063
        if ident.is_empty() {
709
308
            return Err(Error::ExpectedNamedStructLike(ident));
710
14755
        }
711

            
712
14755
        if found_ident != ident {
713
1680
            return Err(Error::ExpectedDifferentStructName {
714
1680
                expected: ident,
715
1680
                found: String::from(found_ident),
716
1680
            });
717
13075
        }
718

            
719
13075
        Ok(true)
720
79338
    }
721

            
722
    /// Parse a document attribute at the current cursor position.
723
568186
    fn attribute(&mut self) -> Result<ParsedAttribute> {
724
568186
        if !self.check_char('#') {
725
533276
            return Ok(ParsedAttribute::None);
726
34910
        }
727

            
728
34910
        if !self.consume_all(&["#", "!", "["])? {
729
12
            return Err(Error::ExpectedAttribute);
730
34898
        }
731

            
732
34898
        self.skip_ws()?;
733
34898
        if self.consume_ident("enable") {
734
34862
            self.skip_ws()?;
735
34862
            if !self.consume_str("(") {
736
                return Err(Error::ExpectedAttribute);
737
34862
            }
738

            
739
34862
            self.skip_ws()?;
740
34862
            let extensions = self.extension_list()?;
741
34359
            self.skip_ws()?;
742

            
743
34359
            if self.consume_all(&[")", "]"])? {
744
34344
                Ok(ParsedAttribute::Extensions(extensions))
745
            } else {
746
15
                Err(Error::ExpectedAttributeEnd)
747
            }
748
36
        } else if self.consume_ident("type") || self.consume_ident("schema") {
749
36
            self.skip_ws()?;
750
36
            if !self.consume_str("=") {
751
                return Err(Error::ExpectedAttribute);
752
36
            }
753

            
754
36
            self.skip_ws()?;
755
36
            self.string()?;
756
36
            self.skip_ws()?;
757

            
758
36
            if self.consume_str("]") {
759
36
                Ok(ParsedAttribute::Ignored)
760
            } else {
761
                Err(Error::ExpectedAttributeEnd)
762
            }
763
        } else {
764
            Err(Error::ExpectedAttribute)
765
        }
766
568186
    }
767

            
768
    /// Returns the extensions bit mask.
769
34862
    fn extension_list(&mut self) -> Result<Extensions> {
770
34862
        let mut extensions = Extensions::empty();
771

            
772
        loop {
773
35140
            let ident = self.identifier()?;
774
35140
            let extension = Extensions::from_ident(ident)
775
35140
                .ok_or_else(|| Error::NoSuchExtension(ident.into()))?;
776

            
777
35125
            extensions |= extension;
778

            
779
35125
            let comma = self.comma()?;
780

            
781
            // If we have no comma but another item, return an error
782
35125
            if !comma && self.check_ident_other_char(0) {
783
488
                return Err(Error::ExpectedComma);
784
34637
            }
785

            
786
            // If there's no comma, assume the list ended.
787
            // If there is, it might be a trailing one, thus we only
788
            // continue the loop if we get an ident char.
789
34637
            if !comma || !self.check_ident_other_char(0) {
790
34359
                break;
791
278
            }
792
        }
793

            
794
34359
        Ok(extensions)
795
34862
    }
796

            
797
15155
    pub fn float<T: Float>(&mut self) -> Result<T> {
798
        const F32_SUFFIX: &str = "f32";
799
        const F64_SUFFIX: &str = "f64";
800

            
801
88958
        for (literal, value_f32, value_f64) in &[
802
15155
            ("inf", f32::INFINITY, f64::INFINITY),
803
15155
            ("+inf", f32::INFINITY, f64::INFINITY),
804
15155
            ("-inf", f32::NEG_INFINITY, f64::NEG_INFINITY),
805
15155
            ("NaN", f32::NAN, f64::NAN),
806
15155
            ("+NaN", f32::NAN, f64::NAN),
807
15155
            ("-NaN", -f32::NAN, -f64::NAN),
808
15155
        ] {
809
88958
            if self.consume_ident(literal) {
810
88
                return T::parse(literal);
811
88870
            }
812

            
813
88870
            if let Some(suffix) = self.src().strip_prefix(literal) {
814
1156
                if let Some(post_suffix) = suffix.strip_prefix(F32_SUFFIX) {
815
576
                    if !post_suffix.chars().next().map_or(false, is_xid_continue) {
816
572
                        let float_ron = &self.src()[..literal.len() + F32_SUFFIX.len()];
817
572
                        self.advance_bytes(literal.len() + F32_SUFFIX.len());
818
572
                        return T::try_from_parsed_float(ParsedFloat::F32(*value_f32), float_ron);
819
4
                    }
820
580
                }
821

            
822
584
                if let Some(post_suffix) = suffix.strip_prefix(F64_SUFFIX) {
823
576
                    if !post_suffix.chars().next().map_or(false, is_xid_continue) {
824
572
                        let float_ron = &self.src()[..literal.len() + F64_SUFFIX.len()];
825
572
                        self.advance_bytes(literal.len() + F64_SUFFIX.len());
826
572
                        return T::try_from_parsed_float(ParsedFloat::F64(*value_f64), float_ron);
827
4
                    }
828
8
                }
829
87714
            }
830
        }
831

            
832
13923
        let num_bytes = self.next_chars_while_len(is_float_char);
833

            
834
13923
        if num_bytes == 0 {
835
46
            return Err(Error::ExpectedFloat);
836
13877
        }
837

            
838
13877
        if self.check_char('_') {
839
4
            return Err(Error::UnderscoreAtBeginning);
840
13873
        }
841

            
842
13873
        let mut f = String::with_capacity(num_bytes);
843
13873
        let mut allow_underscore = false;
844

            
845
251457
        for (i, c) in self.src()[..num_bytes].char_indices() {
846
768
            match c {
847
760
                '_' if allow_underscore => continue,
848
                '_' => {
849
8
                    self.advance_bytes(i);
850
8
                    return Err(Error::FloatUnderscore);
851
                }
852
237111
                '0'..='9' | 'e' | 'E' => allow_underscore = true,
853
11369
                '.' => allow_underscore = false,
854
2209
                _ => (),
855
            }
856

            
857
            // we know that the byte is an ASCII character here
858
250689
            f.push(c);
859
        }
860

            
861
13865
        if self.src()[num_bytes..].starts_with('f') {
862
1982
            let backup_cursor = self.cursor;
863
1982
            self.advance_bytes(num_bytes);
864

            
865
            #[allow(clippy::never_loop)]
866
            loop {
867
1982
                let res = if self.consume_ident(F32_SUFFIX) {
868
1128
                    f32::from_str(&f).map(ParsedFloat::F32)
869
854
                } else if self.consume_ident(F64_SUFFIX) {
870
572
                    f64::from_str(&f).map(ParsedFloat::F64)
871
                } else {
872
282
                    break;
873
                };
874

            
875
1700
                let parsed = if let Ok(parsed) = res {
876
1692
                    parsed
877
                } else {
878
8
                    self.set_cursor(backup_cursor);
879
8
                    return Err(Error::ExpectedFloat);
880
                };
881

            
882
1692
                let float_ron = &self.src[backup_cursor.cursor..self.cursor.cursor];
883

            
884
1692
                return T::try_from_parsed_float(parsed, float_ron);
885
            }
886

            
887
282
            self.set_cursor(backup_cursor);
888
11883
        }
889

            
890
12165
        let value = T::parse(&f)?;
891

            
892
12145
        self.advance_bytes(num_bytes);
893

            
894
12145
        Ok(value)
895
15155
    }
896

            
897
583485
    pub fn skip_identifier(&mut self) -> Option<&'a str> {
898
        #[allow(clippy::nonminimal_bool)]
899
583485
        if self.check_str("b\"") // byte string
900
582087
            || self.check_str("b'") // byte literal
901
367749
            || self.check_str("br#") // raw byte string
902
366915
            || self.check_str("br\"") // raw byte string
903
366081
            || self.check_str("r\"") // raw string
904
365525
            || self.check_str("r#\"") // raw string
905
365247
            || self.check_str("r##") // raw string
906
364969
            || false
907
        {
908
218516
            return None;
909
364969
        }
910

            
911
364969
        if self.check_str("r#") {
912
            // maybe a raw identifier
913
12
            let len = self.next_chars_while_from_len(2, is_ident_raw_char);
914
12
            if len > 0 {
915
4
                let ident = &self.src()[2..2 + len];
916
4
                self.advance_bytes(2 + len);
917
4
                return Some(ident);
918
8
            }
919
8
            return None;
920
364957
        }
921

            
922
364957
        if let Some(c) = self.peek_char() {
923
            // maybe a normal identifier
924
363567
            if is_ident_first_char(c) {
925
106113
                let len =
926
106113
                    c.len_utf8() + self.next_chars_while_from_len(c.len_utf8(), is_xid_continue);
927
106113
                let ident = &self.src()[..len];
928
106113
                self.advance_bytes(len);
929
106113
                return Some(ident);
930
257454
            }
931
1390
        }
932

            
933
258844
        None
934
583485
    }
935

            
936
302500
    pub fn identifier(&mut self) -> Result<&'a str> {
937
302500
        let first = self.peek_char_or_eof()?;
938
302500
        if !is_ident_first_char(first) {
939
2236
            if is_ident_raw_char(first) {
940
1112
                let ident_bytes = self.next_chars_while_len(is_ident_raw_char);
941
1112
                return Err(Error::SuggestRawIdentifier(
942
1112
                    self.src()[..ident_bytes].into(),
943
1112
                ));
944
1124
            }
945

            
946
1124
            return Err(Error::ExpectedIdentifier);
947
300264
        }
948

            
949
        // If the next 2-3 bytes signify the start of a (raw) (byte) string
950
        //  literal, return an error.
951
        #[allow(clippy::nonminimal_bool)]
952
300264
        if self.check_str("b\"") // byte string
953
299986
            || self.check_str("b'") // byte literal
954
299708
            || self.check_str("br#") // raw byte string
955
299430
            || self.check_str("br\"") // raw byte string
956
299152
            || self.check_str("r\"") // raw string
957
298874
            || self.check_str("r#\"") // raw string
958
298596
            || self.check_str("r##") // raw string
959
298318
            || false
960
        {
961
1946
            return Err(Error::ExpectedIdentifier);
962
298318
        }
963

            
964
298318
        let length = if self.check_str("r#") {
965
7256
            let cursor_backup = self.cursor;
966

            
967
7256
            self.advance_bytes(2);
968

            
969
            // Note: it's important to check this before advancing forward, so that
970
            // the value-type deserializer can fall back to parsing it differently.
971
7256
            if !matches!(self.peek_char(), Some(c) if is_ident_raw_char(c)) {
972
556
                self.set_cursor(cursor_backup);
973
556
                return Err(Error::ExpectedIdentifier);
974
6700
            }
975

            
976
6700
            self.next_chars_while_len(is_ident_raw_char)
977
291062
        } else if first == 'r' {
978
556
            let std_ident_length = self.next_chars_while_len(is_xid_continue);
979
556
            let raw_ident_length = self.next_chars_while_len(is_ident_raw_char);
980

            
981
556
            if raw_ident_length > std_ident_length {
982
278
                return Err(Error::SuggestRawIdentifier(
983
278
                    self.src()[..raw_ident_length].into(),
984
278
                ));
985
278
            }
986

            
987
278
            std_ident_length
988
        } else {
989
290506
            let std_ident_length = first.len_utf8()
990
290506
                + self.next_chars_while_from_len(first.len_utf8(), is_xid_continue);
991
290506
            let raw_ident_length = self.next_chars_while_len(is_ident_raw_char);
992

            
993
290506
            if raw_ident_length > std_ident_length {
994
834
                return Err(Error::SuggestRawIdentifier(
995
834
                    self.src()[..raw_ident_length].into(),
996
834
                ));
997
289672
            }
998

            
999
289672
            std_ident_length
        };
296650
        let ident = &self.src()[..length];
296650
        self.advance_bytes(length);
296650
        Ok(ident)
302500
    }
307835
    pub fn next_bytes_is_float(&mut self) -> bool {
307835
        if let Some(c) = self.peek_char() {
307831
            let skip = match c {
5086
                '+' | '-' => 1,
302745
                _ => 0,
            };
307831
            let valid_float_len = self.next_chars_while_from_len(skip, is_float_char);
307831
            let valid_int_len = self.next_chars_while_from_len(skip, is_int_char);
307831
            valid_float_len > valid_int_len
        } else {
4
            false
        }
307835
    }
3599570
    pub fn skip_ws(&mut self) -> Result<()> {
3599570
        if (self.cursor.last_ws_len != WS_CURSOR_UNCLOSED_LINE)
3599010
            && ((self.cursor.pre_ws_cursor + self.cursor.last_ws_len) < self.cursor.cursor)
2323697
        {
2323697
            // the last whitespace is disjoint from this one, we need to track a new one
2323697
            self.cursor.pre_ws_cursor = self.cursor.cursor;
2323697
        }
3599570
        if self.src().is_empty() {
429456
            return Ok(());
3170114
        }
        loop {
3198204
            self.advance_bytes(self.next_chars_while_len(is_whitespace_char));
3198204
            match self.skip_comment()? {
3168160
                None => break,
                Some(Comment::UnclosedLine) => {
1116
                    self.cursor.last_ws_len = WS_CURSOR_UNCLOSED_LINE;
1116
                    return Ok(());
                }
28090
                Some(Comment::ClosedLine | Comment::Block) => continue,
            }
        }
3168160
        self.cursor.last_ws_len = self.cursor.cursor - self.cursor.pre_ws_cursor;
3168160
        Ok(())
3599570
    }
17792
    pub fn has_unclosed_line_comment(&self) -> bool {
17792
        self.src().is_empty() && self.cursor.last_ws_len == WS_CURSOR_UNCLOSED_LINE
17792
    }
8666
    pub fn byte_string(&mut self) -> Result<ParsedByteStr<'a>> {
16
        fn expected_byte_string_found_base64(
16
            base64_str: &ParsedStr,
16
            byte_str: &ParsedByteStr,
16
        ) -> Error {
16
            let byte_str = match &byte_str {
16
                ParsedByteStr::Allocated(b) => b.as_slice(),
                ParsedByteStr::Slice(b) => b,
            }
16
            .iter()
120
            .flat_map(|c| core::ascii::escape_default(*c))
16
            .map(char::from)
16
            .collect::<String>();
16
            let base64_str = match &base64_str {
                ParsedStr::Allocated(s) => s.as_str(),
16
                ParsedStr::Slice(s) => s,
            };
16
            Error::InvalidValueForType {
16
                expected: format!("the Rusty byte string b\"{}\"", byte_str),
16
                found: format!("the ambiguous base64 string {:?}", base64_str),
16
            }
16
        }
        // FIXME @juntyr: remove in v0.13, since only byte_string_no_base64 will
        //                be used
8666
        if self.consume_char('"') {
8
            let base64_str = self.escaped_string()?;
8
            let base64_result = ParsedByteStr::try_from_base64(&base64_str);
8
            match base64_result {
8
                Some(byte_str) => Err(expected_byte_string_found_base64(&base64_str, &byte_str)),
                None => Err(Error::ExpectedByteString),
            }
8658
        } else if self.consume_char('r') {
12
            let base64_str = self.raw_string()?;
12
            let base64_result = ParsedByteStr::try_from_base64(&base64_str);
12
            match base64_result {
8
                Some(byte_str) => Err(expected_byte_string_found_base64(&base64_str, &byte_str)),
4
                None => Err(Error::ExpectedByteString),
            }
        } else {
8646
            self.byte_string_no_base64()
        }
8666
    }
27317
    pub fn byte_string_no_base64(&mut self) -> Result<ParsedByteStr<'a>> {
27317
        if self.consume_str("b\"") {
5866
            self.escaped_byte_string()
21451
        } else if self.consume_str("br") {
3614
            self.raw_byte_string()
        } else {
17837
            Err(Error::ExpectedByteString)
        }
27317
    }
5866
    fn escaped_byte_string(&mut self) -> Result<ParsedByteStr<'a>> {
5866
        match self.escaped_byte_buf(EscapeEncoding::Binary) {
5310
            Ok((bytes, advance)) => {
5310
                self.advance_bytes(advance);
5310
                Ok(bytes)
            }
556
            Err(err) => Err(err),
        }
5866
    }
3614
    fn raw_byte_string(&mut self) -> Result<ParsedByteStr<'a>> {
3614
        match self.raw_byte_buf() {
3058
            Ok((bytes, advance)) => {
3058
                self.advance_bytes(advance);
3058
                Ok(bytes)
            }
278
            Err(Error::ExpectedString) => Err(Error::ExpectedByteString),
278
            Err(err) => Err(err),
        }
3614
    }
106085
    pub fn string(&mut self) -> Result<ParsedStr<'a>> {
106085
        if self.consume_char('"') {
83736
            self.escaped_string()
22349
        } else if self.consume_char('r') {
3110
            self.raw_string()
        } else {
19239
            Err(Error::ExpectedString)
        }
106085
    }
83744
    fn escaped_string(&mut self) -> Result<ParsedStr<'a>> {
83744
        match self.escaped_byte_buf(EscapeEncoding::Utf8) {
81783
            Ok((bytes, advance)) => {
81783
                let string = ParsedStr::try_from_bytes(bytes).map_err(Error::from)?;
81783
                self.advance_bytes(advance);
81783
                Ok(string)
            }
1961
            Err(err) => Err(err),
        }
83744
    }
3122
    fn raw_string(&mut self) -> Result<ParsedStr<'a>> {
3122
        match self.raw_byte_buf() {
2562
            Ok((bytes, advance)) => {
2562
                let string = ParsedStr::try_from_bytes(bytes).map_err(Error::from)?;
2562
                self.advance_bytes(advance);
2562
                Ok(string)
            }
560
            Err(err) => Err(err),
        }
3122
    }
89610
    fn escaped_byte_buf(&mut self, encoding: EscapeEncoding) -> Result<(ParsedByteStr<'a>, usize)> {
        // Checking for '"' and '\\' separately is faster than searching for both at the same time
89610
        let str_end = self.src().find('"').ok_or(Error::ExpectedStringEnd)?;
89039
        let escape = self.src()[..str_end].find('\\');
89039
        if let Some(escape) = escape {
            // Now check if escaping is used inside the string
10604
            let mut i = escape;
10604
            let mut s = self.src().as_bytes()[..i].to_vec();
            loop {
23194
                self.advance_bytes(i + 1);
23194
                match self.parse_escape(encoding, false)? {
10398
                    EscapeCharacter::Ascii(c) => s.push(c),
10850
                    EscapeCharacter::Utf8(c) => match c.len_utf8() {
10286
                        1 => s.push(c as u8),
564
                        len => {
564
                            let start = s.len();
564
                            s.extend(core::iter::repeat(0).take(len));
564
                            c.encode_utf8(&mut s[start..]);
564
                        }
                    },
                }
                // Checking for '"' and '\\' separately is faster than searching for both at the same time
21248
                let new_str_end = self.src().find('"').ok_or(Error::ExpectedStringEnd)?;
21248
                let new_escape = self.src()[..new_str_end].find('\\');
21248
                if let Some(new_escape) = new_escape {
12590
                    s.extend_from_slice(&self.src().as_bytes()[..new_escape]);
12590
                    i = new_escape;
12590
                } else {
8658
                    s.extend_from_slice(&self.src().as_bytes()[..new_str_end]);
                    // Advance to the end of the string + 1 for the `"`.
8658
                    break Ok((ParsedByteStr::Allocated(s), new_str_end + 1));
                }
            }
        } else {
78435
            let s = &self.src().as_bytes()[..str_end];
            // Advance by the number of bytes of the string + 1 for the `"`.
78435
            Ok((ParsedByteStr::Slice(s), str_end + 1))
        }
89610
    }
6736
    fn raw_byte_buf(&mut self) -> Result<(ParsedByteStr<'a>, usize)> {
13460
        let num_hashes = self.next_chars_while_len(|c| c == '#');
6736
        let hashes = &self.src()[..num_hashes];
6736
        self.advance_bytes(num_hashes);
6736
        self.expect_char('"', Error::ExpectedString)?;
6176
        let ending = ["\"", hashes].concat();
6176
        let i = self.src().find(&ending).ok_or(Error::ExpectedStringEnd)?;
5620
        let s = &self.src().as_bytes()[..i];
        // Advance by the number of bytes of the byte string
        // + `num_hashes` + 1 for the `"`.
5620
        Ok((ParsedByteStr::Slice(s), i + num_hashes + 1))
6736
    }
196114
    fn decode_ascii_escape(&mut self) -> Result<u8> {
196114
        let mut n = 0;
196114
        for _ in 0..2 {
392228
            n <<= 4;
392228
            let byte = self.next_char()?;
392228
            let decoded = Self::decode_hex(byte)?;
391672
            n |= decoded;
        }
195558
        Ok(n)
196114
    }
    #[inline]
932890
    fn decode_hex(c: char) -> Result<u8> {
932890
        if !c.is_ascii() {
278
            return Err(Error::InvalidEscape("Non-hex digit found"));
932612
        }
        // c is an ASCII character that can be losslessly cast to u8
932612
        match c as u8 {
932334
            c @ b'0'..=b'9' => Ok(c - b'0'),
110152
            c @ b'a'..=b'f' => Ok(10 + c - b'a'),
55322
            c @ b'A'..=b'F' => Ok(10 + c - b'A'),
278
            _ => Err(Error::InvalidEscape("Non-hex digit found")),
        }
932890
    }
215582
    fn parse_escape(&mut self, encoding: EscapeEncoding, is_char: bool) -> Result<EscapeCharacter> {
215582
        let c = match self.next_char()? {
846
            '\'' => EscapeCharacter::Ascii(b'\''),
3360
            '"' => EscapeCharacter::Ascii(b'"'),
2502
            '\\' => EscapeCharacter::Ascii(b'\\'),
1390
            'n' => EscapeCharacter::Ascii(b'\n'),
556
            'r' => EscapeCharacter::Ascii(b'\r'),
556
            't' => EscapeCharacter::Ascii(b'\t'),
1668
            '0' => EscapeCharacter::Ascii(b'\0'),
            'x' => {
                // Fast exit for ascii escape in byte string
193028
                let b: u8 = self.decode_ascii_escape()?;
192472
                if let EscapeEncoding::Binary = encoding {
189684
                    return Ok(EscapeCharacter::Ascii(b));
2788
                }
                // Fast exit for ascii character in UTF-8 string
2788
                let mut bytes = [b, 0, 0, 0];
2788
                if let Ok(Some(c)) = from_utf8(&bytes[..=0]).map(|s| s.chars().next()) {
834
                    return Ok(EscapeCharacter::Utf8(c));
1954
                }
1954
                if is_char {
                    // Character literals are not allowed to use multiple byte
                    //  escapes to build a unicode character
278
                    return Err(Error::InvalidEscape(
278
                        "Not a valid byte-escaped Unicode character",
278
                    ));
1676
                }
                // UTF-8 character needs up to four bytes and we have already
                //  consumed one, so at most three to go
4194
                for i in 1..4 {
4194
                    if !self.consume_str(r"\x") {
1112
                        return Err(Error::InvalidEscape(
1112
                            "Not a valid byte-escaped Unicode character",
1112
                        ));
3082
                    }
3082
                    bytes[i] = self.decode_ascii_escape()?;
                    // Check if we now have a valid UTF-8 character
3082
                    if let Ok(Some(c)) = from_utf8(&bytes[..=i]).map(|s| s.chars().next()) {
286
                        return Ok(EscapeCharacter::Utf8(c));
2796
                    }
                }
278
                return Err(Error::InvalidEscape(
278
                    "Not a valid byte-escaped Unicode character",
278
                ));
            }
            'u' => {
11398
                self.expect_char('{', Error::InvalidEscape("Missing { in Unicode escape"))?;
11398
                let mut bytes: u32 = 0;
11398
                let mut num_digits = 0;
30302
                while num_digits < 6 {
30302
                    let byte = self.peek_char_or_eof()?;
30302
                    if byte == '}' {
11398
                        break;
18904
                    }
18904
                    self.skip_next_char();
18904
                    num_digits += 1;
18904
                    let byte = Self::decode_hex(byte)?;
18904
                    bytes <<= 4;
18904
                    bytes |= u32::from(byte);
                }
11398
                if num_digits == 0 {
278
                    return Err(Error::InvalidEscape(
278
                        "Expected 1-6 digits, got 0 digits in Unicode escape",
278
                    ));
11120
                }
11120
                self.expect_char(
                    '}',
11120
                    Error::InvalidEscape("No } at the end of Unicode escape"),
                )?;
11120
                let c = char_from_u32(bytes).ok_or(Error::InvalidEscape(
11120
                    "Not a valid Unicode-escaped character",
11120
                ))?;
11120
                EscapeCharacter::Utf8(c)
            }
278
            _ => return Err(Error::InvalidEscape("Unknown escape character")),
        };
21998
        Ok(c)
215582
    }
3198204
    fn skip_comment(&mut self) -> Result<Option<Comment>> {
3198204
        if self.consume_char('/') {
30044
            match self.next_char()? {
                '/' => {
123936
                    let bytes = self.next_chars_while_len(|c| c != '\n');
8348
                    self.advance_bytes(bytes);
8348
                    if self.src().is_empty() {
1116
                        Ok(Some(Comment::UnclosedLine))
                    } else {
7232
                        Ok(Some(Comment::ClosedLine))
                    }
                }
                '*' => {
21418
                    let mut level = 1;
49504
                    while level > 0 {
194354
                        let bytes = self.next_chars_while_len(|c| !matches!(c, '/' | '*'));
28646
                        if self.src().is_empty() {
282
                            return Err(Error::UnclosedBlockComment);
28364
                        }
28364
                        self.advance_bytes(bytes);
                        // check whether / or * and take action
28364
                        if self.consume_str("/*") {
1946
                            level += 1;
26418
                        } else if self.consume_str("*/") {
22804
                            level -= 1;
22804
                        } else {
3614
                            self.next_char().map_err(|_| Error::UnclosedBlockComment)?;
                        }
                    }
20858
                    Ok(Some(Comment::Block))
                }
278
                c => Err(Error::UnexpectedChar(c)),
            }
        } else {
3168160
            Ok(None)
        }
3198204
    }
}
enum Comment {
    ClosedLine,
    UnclosedLine,
    Block,
}
pub trait Num {
    fn from_u8(x: u8) -> Self;
    /// Returns `true` on overflow
    fn checked_mul_ext(&mut self, x: u8) -> bool;
    /// Returns `true` on overflow
    fn checked_add_ext(&mut self, x: u8) -> bool;
    /// Returns `true` on overflow
    fn checked_sub_ext(&mut self, x: u8) -> bool;
}
macro_rules! impl_num {
    ($ty:ty) => {
        impl Num for $ty {
1257593
            fn from_u8(x: u8) -> Self {
72874
                x as $ty
1257593
            }
522600
            fn checked_mul_ext(&mut self, x: u8) -> bool {
522600
                match self.checked_mul(Self::from_u8(x)) {
521758
                    Some(n) => {
521758
                        *self = n;
521758
                        false
                    }
842
                    None => true,
                }
522600
            }
434828
            fn checked_add_ext(&mut self, x: u8) -> bool {
434828
                match self.checked_add(Self::from_u8(x)) {
434411
                    Some(n) => {
434411
                        *self = n;
434411
                        false
                    }
417
                    None => true,
                }
434828
            }
84984
            fn checked_sub_ext(&mut self, x: u8) -> bool {
84984
                match self.checked_sub(Self::from_u8(x)) {
82204
                    Some(n) => {
82204
                        *self = n;
82204
                        false
                    }
2780
                    None => true,
                }
84984
            }
        }
    };
    ($($tys:ty)*) => {
        $( impl_num!($tys); )*
    };
}
impl_num! { i8 i16 i32 i64 u8 u16 u32 u64 }
#[cfg(feature = "integer128")]
impl_num! { i128 u128 }
pub trait Integer: Sized {
    fn parse(parser: &mut Parser, sign: i8) -> Result<Self>;
    fn try_from_parsed_integer(parsed: ParsedInteger, ron: &str) -> Result<Self>;
}
macro_rules! impl_integer {
    ($wrap:ident($ty:ty)) => {
        impl Integer for $ty {
109425
            fn parse(parser: &mut Parser, sign: i8) -> Result<Self> {
109425
                parser.parse_integer(sign)
109425
            }
28356
            fn try_from_parsed_integer(parsed: ParsedInteger, ron: &str) -> Result<Self> {
28356
                match parsed {
7506
                    ParsedInteger::$wrap(v) => Ok(v),
                    _ => Err(Error::InvalidValueForType {
20850
                        expected: format!(
                            "a{} {}-bit {}signed integer",
20850
                            if <$ty>::BITS == 8 { "n" } else { "n" },
                            <$ty>::BITS,
20850
                            if <$ty>::MIN == 0 { "un" } else { "" },
                        ),
20850
                        found: String::from(ron),
                    }),
                }
28356
            }
        }
    };
    ($($wraps:ident($tys:ty))*) => {
        $( impl_integer!($wraps($tys)); )*
    };
}
impl_integer! {
    I8(i8) I16(i16) I32(i32) I64(i64)
    U8(u8) U16(u16) U32(u32) U64(u64)
}
#[cfg(feature = "integer128")]
impl_integer! { I128(i128) U128(u128) }
pub enum ParsedInteger {
    I8(i8),
    I16(i16),
    I32(i32),
    I64(i64),
    #[cfg(feature = "integer128")]
    I128(i128),
    U8(u8),
    U16(u16),
    U32(u32),
    U64(u64),
    #[cfg(feature = "integer128")]
    U128(u128),
}
impl Integer for ParsedInteger {
76324
    fn parse(parser: &mut Parser, sign: i8) -> Result<Self> {
76324
        if sign < 0 {
1996
            let signed = parser.parse_integer::<LargeSInt>(-1)?;
582
            return if let Ok(x) = i8::try_from(signed) {
290
                Ok(ParsedInteger::I8(x))
292
            } else if let Ok(x) = i16::try_from(signed) {
4
                Ok(ParsedInteger::I16(x))
288
            } else if let Ok(x) = i32::try_from(signed) {
4
                Ok(ParsedInteger::I32(x))
            } else {
                #[cfg(not(feature = "integer128"))]
                {
2
                    Ok(ParsedInteger::I64(signed))
                }
                #[cfg(feature = "integer128")]
282
                if let Ok(x) = i64::try_from(signed) {
2
                    Ok(ParsedInteger::I64(x))
                } else {
280
                    Ok(ParsedInteger::I128(signed))
                }
            };
74328
        }
74328
        let unsigned = parser.parse_integer::<LargeUInt>(1)?;
73613
        if let Ok(x) = u8::try_from(unsigned) {
72761
            Ok(ParsedInteger::U8(x))
852
        } else if let Ok(x) = u16::try_from(unsigned) {
286
            Ok(ParsedInteger::U16(x))
566
        } else if let Ok(x) = u32::try_from(unsigned) {
4
            Ok(ParsedInteger::U32(x))
        } else {
            #[cfg(not(feature = "integer128"))]
            {
2
                Ok(ParsedInteger::U64(unsigned))
            }
            #[cfg(feature = "integer128")]
560
            if let Ok(x) = u64::try_from(unsigned) {
2
                Ok(ParsedInteger::U64(x))
            } else {
558
                Ok(ParsedInteger::U128(unsigned))
            }
        }
76324
    }
219342
    fn try_from_parsed_integer(parsed: ParsedInteger, _ron: &str) -> Result<Self> {
219342
        Ok(parsed)
219342
    }
}
pub trait Float: Sized {
    fn parse(float: &str) -> Result<Self>;
    fn try_from_parsed_float(parsed: ParsedFloat, ron: &str) -> Result<Self>;
}
macro_rules! impl_float {
    ($wrap:ident($ty:ty: $bits:expr)) => {
        impl Float for $ty {
32249
            fn parse(float: &str) -> Result<Self> {
32249
                <$ty>::from_str(float).map_err(|_| Error::ExpectedFloat)
32249
            }
3892
            fn try_from_parsed_float(parsed: ParsedFloat, ron: &str) -> Result<Self> {
3892
                match parsed {
3336
                    ParsedFloat::$wrap(v) => Ok(v),
556
                    _ => Err(Error::InvalidValueForType {
556
                        expected: format!(
556
                            "a {}-bit floating point number", $bits,
556
                        ),
556
                        found: String::from(ron),
556
                    }),
                }
3892
            }
        }
    };
    ($($wraps:ident($tys:ty: $bits:expr))*) => {
        $( impl_float!($wraps($tys: $bits)); )*
    };
}
impl_float! { F32(f32: 32) F64(f64: 64) }
pub enum ParsedFloat {
    F32(f32),
    F64(f64),
}
impl Float for ParsedFloat {
11514
    fn parse(float: &str) -> Result<Self> {
11514
        let value = f64::from_str(float).map_err(|_| Error::ExpectedFloat)?;
        #[allow(clippy::cast_possible_truncation)]
11514
        if value.total_cmp(&f64::from(value as f32)).is_eq() {
4532
            Ok(ParsedFloat::F32(value as f32))
        } else {
6982
            Ok(ParsedFloat::F64(value))
        }
11514
    }
2780
    fn try_from_parsed_float(parsed: ParsedFloat, _ron: &str) -> Result<Self> {
2780
        Ok(parsed)
2780
    }
}
pub enum StructType {
    AnyTuple,
    EmptyTuple,
    NewtypeTuple,
    NonNewtypeTuple,
    Named,
    Unit,
}
#[derive(Copy, Clone)] // GRCOV_EXCL_LINE
pub enum NewtypeMode {
    NoParensMeanUnit,
    InsideNewtype,
}
#[derive(Copy, Clone)] // GRCOV_EXCL_LINE
pub enum TupleMode {
    ImpreciseTupleOrNewtype,
    DifferentiateNewtype,
}
pub enum ParsedStr<'a> {
    Allocated(String),
    Slice(&'a str),
}
pub enum ParsedByteStr<'a> {
    Allocated(Vec<u8>),
    Slice(&'a [u8]),
}
impl<'a> ParsedStr<'a> {
84345
    pub fn try_from_bytes(bytes: ParsedByteStr<'a>) -> Result<Self, Utf8Error> {
84345
        match bytes {
5858
            ParsedByteStr::Allocated(byte_buf) => Ok(ParsedStr::Allocated(
5858
                String::from_utf8(byte_buf).map_err(|e| e.utf8_error())?,
            )),
78487
            ParsedByteStr::Slice(bytes) => Ok(ParsedStr::Slice(from_utf8(bytes)?)),
        }
84345
    }
}
impl<'a> ParsedByteStr<'a> {
20
    pub fn try_from_base64(str: &ParsedStr<'a>) -> Option<Self> {
        // Adapted from MIT licensed Jenin Sutradhar's base 64 decoder
        // https://github.com/JeninSutradhar/base64-Rust-Encoder-Decoder/blob/ee1fb08cbb78024ec8cf5e786815acb239169f02/src/lib.rs#L84-L128
20
        fn try_decode_base64(str: &str) -> Option<Vec<u8>> {
            const CHARSET: &[u8; 64] =
                b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
            const PADDING: u8 = b'=';
            // fast reject for missing padding
20
            if (str.len() % 4) != 0 {
4
                return None;
16
            }
16
            let bstr_no_padding = str.trim_end_matches(char::from(PADDING)).as_bytes();
            // fast reject for excessive padding
16
            if (str.len() - bstr_no_padding.len()) > 2 {
                return None;
16
            }
            // fast reject for extraneous bytes after padding
16
            if bstr_no_padding.contains(&PADDING) {
                return None;
16
            }
            // fast reject for non-ASCII
16
            if !str.is_ascii() {
                return None;
16
            }
16
            let mut collected_bits = 0_u8;
16
            let mut byte_buffer = 0_u16;
16
            let mut bytes = bstr_no_padding.iter().copied();
16
            let mut binary = Vec::new();
            'decodeloop: loop {
304
                while collected_bits < 8 {
184
                    if let Some(nextbyte) = bytes.next() {
                        #[allow(clippy::cast_possible_truncation)]
5424
                        if let Some(idx) = CHARSET.iter().position(|&x| x == nextbyte) {
168
                            byte_buffer |= ((idx & 0b0011_1111) as u16) << (10 - collected_bits);
168
                            collected_bits += 6;
168
                        } else {
                            return None;
                        }
                    } else {
16
                        break 'decodeloop;
                    }
                }
120
                binary.push(((0b1111_1111_0000_0000 & byte_buffer) >> 8) as u8);
120
                byte_buffer &= 0b0000_0000_1111_1111;
120
                byte_buffer <<= 8;
120
                collected_bits -= 8;
            }
16
            if usize::from(collected_bits) != ((str.len() - bstr_no_padding.len()) * 2) {
                return None;
16
            }
16
            Some(binary)
20
        }
20
        let base64_str = match str {
            ParsedStr::Allocated(string) => string.as_str(),
20
            ParsedStr::Slice(str) => str,
        };
20
        try_decode_base64(base64_str).map(ParsedByteStr::Allocated)
20
    }
}
#[derive(Copy, Clone)] // GRCOV_EXCL_LINE
enum EscapeEncoding {
    Binary,
    Utf8,
}
enum EscapeCharacter {
    Ascii(u8),
    Utf8(char),
}
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
4
    fn decode_x10() {
4
        let mut bytes = Parser::new("10").unwrap();
4
        assert_eq!(bytes.decode_ascii_escape(), Ok(b'\x10'));
4
    }
    #[test]
4
    fn track_prior_ws() {
        const SOURCE: &str = "   /*hey*/ 42       /*bye*/ 24  ";
4
        let mut bytes = Parser::new(SOURCE).unwrap();
4
        assert_eq!(bytes.src(), "42       /*bye*/ 24  ");
4
        assert_eq!(bytes.pre_ws_src(), SOURCE);
4
        bytes.skip_ws().unwrap();
4
        assert_eq!(bytes.src(), "42       /*bye*/ 24  ");
4
        assert_eq!(bytes.pre_ws_src(), SOURCE);
4
        assert_eq!(bytes.integer::<u8>().unwrap(), 42);
4
        assert_eq!(bytes.src(), "       /*bye*/ 24  ");
4
        assert_eq!(bytes.pre_ws_src(), SOURCE);
4
        bytes.skip_ws().unwrap();
4
        bytes.skip_ws().unwrap();
4
        assert_eq!(bytes.src(), "24  ");
4
        assert_eq!(bytes.pre_ws_src(), "       /*bye*/ 24  ");
4
        let mut bytes = Parser::new("42").unwrap();
4
        bytes.skip_ws().unwrap();
4
        bytes.skip_ws().unwrap();
4
        assert_eq!(bytes.src(), "42");
4
        assert_eq!(bytes.pre_ws_src(), "42");
4
        assert_eq!(bytes.integer::<u8>().unwrap(), 42);
4
        bytes.skip_ws().unwrap();
4
        bytes.skip_ws().unwrap();
4
        assert_eq!(bytes.src(), "");
4
        assert_eq!(bytes.pre_ws_src(), "");
4
        let mut bytes = Parser::new("  42  ").unwrap();
4
        bytes.skip_ws().unwrap();
4
        bytes.skip_ws().unwrap();
4
        assert_eq!(bytes.src(), "42  ");
4
        assert_eq!(bytes.pre_ws_src(), "  42  ");
4
        assert_eq!(bytes.integer::<u8>().unwrap(), 42);
4
        bytes.skip_ws().unwrap();
4
        bytes.skip_ws().unwrap();
4
        assert_eq!(bytes.src(), "");
4
        assert_eq!(bytes.pre_ws_src(), "  ");
4
        let mut bytes = Parser::new("  42  //").unwrap();
4
        bytes.skip_ws().unwrap();
4
        bytes.skip_ws().unwrap();
4
        assert_eq!(bytes.src(), "42  //");
4
        assert_eq!(bytes.pre_ws_src(), "  42  //");
4
        assert_eq!(bytes.integer::<u8>().unwrap(), 42);
4
        bytes.skip_ws().unwrap();
4
        bytes.skip_ws().unwrap();
4
        assert_eq!(bytes.src(), "");
4
        assert_eq!(bytes.pre_ws_src(), "  //");
4
    }
    #[test]
4
    fn parser_cursor_eq_cmp() {
4
        assert!(
4
            ParserCursor {
4
                cursor: 42,
4
                pre_ws_cursor: 42,
4
                last_ws_len: 42
4
            } == ParserCursor {
4
                cursor: 42,
4
                pre_ws_cursor: 24,
4
                last_ws_len: 24
4
            }
        );
4
        assert!(
4
            ParserCursor {
4
                cursor: 42,
4
                pre_ws_cursor: 42,
4
                last_ws_len: 42
4
            } != ParserCursor {
4
                cursor: 24,
4
                pre_ws_cursor: 42,
4
                last_ws_len: 42
4
            }
        );
4
        assert!(
4
            ParserCursor {
4
                cursor: 42,
4
                pre_ws_cursor: 42,
4
                last_ws_len: 42
4
            } < ParserCursor {
4
                cursor: 43,
4
                pre_ws_cursor: 24,
4
                last_ws_len: 24
4
            }
        );
4
        assert!(
4
            ParserCursor {
4
                cursor: 42,
4
                pre_ws_cursor: 42,
4
                last_ws_len: 42
4
            } > ParserCursor {
4
                cursor: 41,
4
                pre_ws_cursor: 24,
4
                last_ws_len: 24
4
            }
        );
4
    }
    #[test]
4
    fn empty_src_is_not_a_float() {
4
        assert!(!Parser::new("").unwrap().next_bytes_is_float());
4
    }
    #[test]
4
    fn base64_deprecation_error() {
4
        let err = crate::from_str::<bytes::Bytes>("\"SGVsbG8gcm9uIQ==\"").unwrap_err();
4
        assert_eq!(
            err,
4
            SpannedError {
4
                code: Error::InvalidValueForType {
4
                    expected: String::from("the Rusty byte string b\"Hello ron!\""),
4
                    found: String::from("the ambiguous base64 string \"SGVsbG8gcm9uIQ==\"")
4
                },
4
                span: Span {
4
                    start: Position { line: 1, col: 2 },
4
                    end: Position { line: 1, col: 19 },
4
                }
4
            }
        );
4
        let err = crate::from_str::<bytes::Bytes>("r\"SGVsbG8gcm9uIQ==\"").unwrap_err();
4
        assert_eq!(format!("{}", err.code), "Expected the Rusty byte string b\"Hello ron!\" but found the ambiguous base64 string \"SGVsbG8gcm9uIQ==\" instead");
4
        assert_eq!(
4
            crate::from_str::<bytes::Bytes>("\"invalid=\"").unwrap_err(),
4
            SpannedError {
4
                code: Error::InvalidValueForType {
4
                    expected: String::from("the Rusty byte string b\"\\x8a{\\xda\\x96\\'\""),
4
                    found: String::from("the ambiguous base64 string \"invalid=\"")
4
                },
4
                span: Span {
4
                    start: Position { line: 1, col: 2 },
4
                    end: Position { line: 1, col: 11 },
4
                }
4
            }
        );
4
        assert_eq!(
4
            crate::from_str::<bytes::Bytes>("r\"invalid=\"").unwrap_err(),
4
            SpannedError {
4
                code: Error::InvalidValueForType {
4
                    expected: String::from("the Rusty byte string b\"\\x8a{\\xda\\x96\\'\""),
4
                    found: String::from("the ambiguous base64 string \"invalid=\"")
4
                },
4
                span: Span {
4
                    start: Position { line: 1, col: 3 },
4
                    end: Position { line: 1, col: 12 },
4
                }
4
            }
        );
4
        assert_eq!(
4
            crate::from_str::<bytes::Bytes>("r\"invalid\"").unwrap_err(),
            SpannedError {
                code: Error::ExpectedByteString,
                span: Span {
                    start: Position { line: 1, col: 3 },
                    end: Position { line: 1, col: 11 },
                }
            }
        );
4
    }
}