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
2515029
const fn is_int_char(c: char) -> bool {
22
2515029
    c.is_ascii_hexdigit() || c == '_'
23
2515029
}
24

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

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

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

            
37
5482496
pub const fn is_whitespace_char(c: char) -> bool {
38
3227162
    matches!(
39
5482496
        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
5482496
}
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
3436
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
93
3436
        self.cursor.partial_cmp(&other.cursor)
94
3436
    }
95
}
96

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

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

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

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

            
130
546568
        Ok(parser)
131
547387
    }
132

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

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

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

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

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

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

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

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

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

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

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

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

            
190
495955
            true
191
        } else {
192
1234044
            false
193
        }
194
1729999
    }
195

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

            
200
1515687
            true
201
        } else {
202
3489991
            false
203
        }
204
5005678
    }
205

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

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

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

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

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

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

            
251
555897
        for (i, c) in s.char_indices() {
252
555897
            if c == '_' {
253
9450
                continue;
254
546447
            }
255

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

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

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

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

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

            
276
217557
        Ok(num_acc)
277
223628
    }
278

            
279
230996
    fn parse_integer<T: Num>(&mut self, sign: i8, base: u8) -> Result<T> {
280
230996
        let num_bytes = self.next_chars_while_len(is_int_char);
281

            
282
230996
        if num_bytes == 0 {
283
4548
            return Err(Error::ExpectedInteger);
284
226448
        }
285

            
286
226448
        if self.check_char('_') {
287
2820
            return Err(Error::UnderscoreAtBeginning);
288
223628
        }
289

            
290
223628
        let s = &self.src()[..num_bytes];
291

            
292
223628
        if sign > 0 {
293
206571
            self.parse_integer_digits(s, base, T::checked_add_ext)
294
        } else {
295
17057
            self.parse_integer_digits(s, base, T::checked_sub_ext)
296
        }
297
230996
    }
298

            
299
    #[allow(clippy::too_many_lines)]
300
323387
    pub fn integer<T: Integer>(&mut self) -> Result<T> {
301
323387
        let src_backup = self.src();
302

            
303
323387
        let is_negative = match self.peek_char_or_eof()? {
304
            '+' => {
305
28
                self.skip_next_char();
306
28
                false
307
            }
308
            '-' => {
309
5063
                self.skip_next_char();
310
5063
                true
311
            }
312
218272
            'b' if self.consume_str("b'") => {
313
                // Parse a byte literal
314
218272
                let byte = match self.next_char()? {
315
191478
                    '\\' => match self.parse_escape(EscapeEncoding::Binary, true)? {
316
                        // we know that this byte is an ASCII character
317
191196
                        EscapeCharacter::Ascii(b) => b,
318
                        EscapeCharacter::Utf8(_) => {
319
282
                            return Err(Error::InvalidEscape(
320
282
                                "Unexpected Unicode escape in byte literal",
321
282
                            ))
322
                        }
323
                    },
324
26794
                    b if b.is_ascii() => b as u8,
325
282
                    _ => return Err(Error::ExpectedByteLiteral),
326
                };
327

            
328
217708
                if !self.consume_char('\'') {
329
282
                    return Err(Error::ExpectedByteLiteral);
330
217426
                }
331

            
332
217426
                let bytes_ron = &src_backup[..src_backup.len() - self.src().len()];
333

            
334
217426
                return T::try_from_parsed_integer(ParsedInteger::U8(byte), bytes_ron);
335
            }
336
100024
            _ => false,
337
        };
338
105115
        let sign = if is_negative { -1 } else { 1 };
339

            
340
105115
        let base = match () {
341
105115
            () if self.consume_str("0b") => 2,
342
103669
            () if self.consume_str("0o") => 8,
343
102505
            () if self.consume_str("0x") => 16,
344
101337
            () => 10,
345
        };
346

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

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

            
353
            #[allow(clippy::never_loop)]
354
            loop {
355
12181
                let (res, suffix_bytes) = if self.consume_ident("i8") {
356
886
                    let suffix_bytes = self.src();
357
886
                    self.set_cursor(int_cursor);
358
886
                    (
359
886
                        self.parse_integer::<i8>(sign, base).map(ParsedInteger::I8),
360
886
                        suffix_bytes,
361
886
                    )
362
11295
                } else if self.consume_ident("i16") {
363
886
                    let suffix_bytes = self.src();
364
886
                    self.set_cursor(int_cursor);
365
886
                    (
366
886
                        self.parse_integer::<i16>(sign, base)
367
886
                            .map(ParsedInteger::I16),
368
886
                        suffix_bytes,
369
886
                    )
370
10409
                } else if self.consume_ident("i32") {
371
1212
                    let suffix_bytes = self.src();
372
1212
                    self.set_cursor(int_cursor);
373
1212
                    (
374
1212
                        self.parse_integer::<i32>(sign, base)
375
1212
                            .map(ParsedInteger::I32),
376
1212
                        suffix_bytes,
377
1212
                    )
378
9197
                } else if self.consume_ident("i64") {
379
886
                    let suffix_bytes = self.src();
380
886
                    self.set_cursor(int_cursor);
381
886
                    (
382
886
                        self.parse_integer::<i64>(sign, base)
383
886
                            .map(ParsedInteger::I64),
384
886
                        suffix_bytes,
385
886
                    )
386
8311
                } else if self.consume_ident("u8") {
387
2292
                    let suffix_bytes = self.src();
388
2292
                    self.set_cursor(int_cursor);
389
2292
                    (
390
2292
                        self.parse_integer::<u8>(sign, base).map(ParsedInteger::U8),
391
2292
                        suffix_bytes,
392
2292
                    )
393
6019
                } else if self.consume_ident("u16") {
394
1172
                    let suffix_bytes = self.src();
395
1172
                    self.set_cursor(int_cursor);
396
1172
                    (
397
1172
                        self.parse_integer::<u16>(sign, base)
398
1172
                            .map(ParsedInteger::U16),
399
1172
                        suffix_bytes,
400
1172
                    )
401
4847
                } else if self.consume_ident("u32") {
402
1204
                    let suffix_bytes = self.src();
403
1204
                    self.set_cursor(int_cursor);
404
1204
                    (
405
1204
                        self.parse_integer::<u32>(sign, base)
406
1204
                            .map(ParsedInteger::U32),
407
1204
                        suffix_bytes,
408
1204
                    )
409
3643
                } else if self.consume_ident("u64") {
410
1172
                    let suffix_bytes = self.src();
411
1172
                    self.set_cursor(int_cursor);
412
1172
                    (
413
1172
                        self.parse_integer::<u64>(sign, base)
414
1172
                            .map(ParsedInteger::U64),
415
1172
                        suffix_bytes,
416
1172
                    )
417
                } else {
418
                    #[cfg(feature = "integer128")]
419
1609
                    if self.consume_ident("i128") {
420
445
                        let suffix_bytes = self.src();
421
445
                        self.set_cursor(int_cursor);
422
445
                        (
423
445
                            self.parse_integer::<i128>(sign, base)
424
445
                                .map(ParsedInteger::I128),
425
445
                            suffix_bytes,
426
445
                        )
427
1164
                    } else if self.consume_ident("u128") {
428
588
                        let suffix_bytes = self.src();
429
588
                        self.set_cursor(int_cursor);
430
588
                        (
431
588
                            self.parse_integer::<u128>(sign, base)
432
588
                                .map(ParsedInteger::U128),
433
588
                            suffix_bytes,
434
588
                        )
435
                    } else {
436
576
                        break;
437
                    }
438
                    #[cfg(not(feature = "integer128"))]
439
                    {
440
862
                        break;
441
                    }
442
                };
443

            
444
10453
                if !matches!(
445
1859
                    &res,
446
                    Err(Error::UnderscoreAtBeginning | Error::InvalidIntegerDigit { .. })
447
10453
                ) {
448
10453
                    // Advance past the number suffix
449
10453
                    self.skip_identifier();
450
10453
                }
451

            
452
10743
                let integer_ron = &src_backup[..src_backup.len() - suffix_bytes.len()];
453

            
454
10743
                return res.and_then(|parsed| T::try_from_parsed_integer(parsed, integer_ron));
455
            }
456

            
457
1438
            self.set_cursor(int_cursor);
458
92934
        }
459

            
460
94372
        T::parse(self, sign, base)
461
323387
    }
462

            
463
312257
    pub fn any_number(&mut self) -> Result<Number> {
464
312257
        if self.next_bytes_is_float() {
465
11070
            return match self.float::<ParsedFloat>()? {
466
3432
                ParsedFloat::F32(v) => Ok(Number::F32(v.into())),
467
7638
                ParsedFloat::F64(v) => Ok(Number::F64(v.into())),
468
            };
469
301187
        }
470

            
471
301187
        let backup_cursor = self.cursor;
472

            
473
301187
        let (integer_err, integer_cursor) = match self.integer::<ParsedInteger>() {
474
297759
            Ok(integer) => {
475
297759
                return match integer {
476
858
                    ParsedInteger::I8(v) => Ok(Number::I8(v)),
477
568
                    ParsedInteger::I16(v) => Ok(Number::I16(v)),
478
568
                    ParsedInteger::I32(v) => Ok(Number::I32(v)),
479
568
                    ParsedInteger::I64(v) => Ok(Number::I64(v)),
480
                    #[cfg(feature = "integer128")]
481
566
                    ParsedInteger::I128(v) => Ok(Number::I128(v)),
482
291793
                    ParsedInteger::U8(v) => Ok(Number::U8(v)),
483
854
                    ParsedInteger::U16(v) => Ok(Number::U16(v)),
484
568
                    ParsedInteger::U32(v) => Ok(Number::U32(v)),
485
568
                    ParsedInteger::U64(v) => Ok(Number::U64(v)),
486
                    #[cfg(feature = "integer128")]
487
848
                    ParsedInteger::U128(v) => Ok(Number::U128(v)),
488
                }
489
            }
490
3428
            Err(err) => (err, self.cursor),
491
        };
492

            
493
3428
        self.set_cursor(backup_cursor);
494

            
495
        // Fall-back to parse an out-of-range integer as a float
496
3428
        match self.float::<ParsedFloat>() {
497
2856
            Ok(ParsedFloat::F32(v)) if self.cursor >= integer_cursor => Ok(Number::F32(v.into())),
498
572
            Ok(ParsedFloat::F64(v)) if self.cursor >= integer_cursor => Ok(Number::F64(v.into())),
499
            _ => {
500
                // Return the more precise integer error
501
1269
                self.set_cursor(integer_cursor);
502
1269
                Err(integer_err)
503
            }
504
        }
505
312257
    }
506

            
507
31452
    pub fn bool(&mut self) -> Result<bool> {
508
31452
        if self.consume_ident("true") {
509
17554
            Ok(true)
510
13898
        } else if self.consume_ident("false") {
511
13862
            Ok(false)
512
        } else {
513
36
            Err(Error::ExpectedBoolean)
514
        }
515
31452
    }
516

            
517
68789
    pub fn char(&mut self) -> Result<char> {
518
68789
        self.expect_char('\'', Error::ExpectedChar)?;
519

            
520
49850
        let c = self.next_char()?;
521

            
522
49850
        let c = if c == '\\' {
523
3678
            match self.parse_escape(EscapeEncoding::Utf8, true)? {
524
                // we know that this byte is an ASCII character
525
1704
                EscapeCharacter::Ascii(b) => char::from(b),
526
1128
                EscapeCharacter::Utf8(c) => c,
527
            }
528
        } else {
529
46172
            c
530
        };
531

            
532
49004
        self.expect_char('\'', Error::ExpectedChar)?;
533

            
534
49004
        Ok(c)
535
68789
    }
536

            
537
414454
    pub fn comma(&mut self) -> Result<bool> {
538
414454
        self.skip_ws()?;
539

            
540
414454
        if self.consume_char(',') {
541
240916
            self.skip_ws()?;
542

            
543
240916
            Ok(true)
544
        } else {
545
173538
            Ok(false)
546
        }
547
414454
    }
548

            
549
    /// Only returns true if the char after `ident` cannot belong
550
    /// to an identifier.
551
5778757
    pub fn check_ident(&mut self, ident: &str) -> bool {
552
5778757
        self.check_str(ident) && !self.check_ident_other_char(ident.len())
553
5778757
    }
554

            
555
289297
    fn check_ident_other_char(&self, index: usize) -> bool {
556
289297
        self.src()[index..]
557
289297
            .chars()
558
289297
            .next()
559
289297
            .map_or(false, is_xid_continue)
560
289297
    }
561

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

            
591
66768
            parser.skip_ws()?;
592

            
593
            // Check for `Ident()`, which could be
594
            // - a zero-field struct or tuple (variant)
595
            // - an unwrapped newtype around a unit
596
66764
            if matches!(newtype, NewtypeMode::NoParensMeanUnit) && parser.check_char(')') {
597
846
                return Ok(StructType::EmptyTuple);
598
65918
            }
599

            
600
65918
            if parser.skip_identifier().is_some() {
601
46986
                parser.skip_ws()?;
602

            
603
46986
                match parser.peek_char() {
604
                    // Definitely a struct with named fields
605
41056
                    Some(':') => return Ok(StructType::Named),
606
                    // Definitely a tuple-like struct with fields
607
                    Some(',') => {
608
4238
                        parser.skip_next_char();
609
4238
                        parser.skip_ws()?;
610
4238
                        if parser.check_char(')') {
611
                            // A one-element tuple could be a newtype
612
                            return Ok(StructType::NewtypeTuple);
613
4238
                        }
614
                        // Definitely a tuple struct with more than one field
615
4238
                        return Ok(StructType::NonNewtypeTuple);
616
                    }
617
                    // Either a newtype or a tuple struct
618
1128
                    Some(')') => return Ok(StructType::NewtypeTuple),
619
                    // Something else, let's investigate further
620
564
                    Some(_) | None => (),
621
                };
622
18932
            }
623

            
624
19496
            if matches!(tuple, TupleMode::ImpreciseTupleOrNewtype) {
625
13277
                return Ok(StructType::AnyTuple);
626
6219
            }
627

            
628
6219
            let mut braces = 1_usize;
629
6219
            let mut more_than_one = false;
630

            
631
            // Skip ahead to see if the value is followed by another value
632
24579
            while braces > 0 {
633
                // Skip spurious braces in comments, strings, and characters
634
18939
                parser.skip_ws()?;
635
18939
                let cursor_backup = parser.cursor;
636
18939
                if parser.char().is_err() {
637
18939
                    parser.set_cursor(cursor_backup);
638
18939
                }
639
18939
                let cursor_backup = parser.cursor;
640
18939
                match parser.string() {
641
1128
                    Ok(_) => (),
642
                    // prevent quadratic complexity backtracking for unterminated string
643
                    Err(err @ (Error::ExpectedStringEnd | Error::Eof)) => return Err(err),
644
17811
                    Err(_) => parser.set_cursor(cursor_backup),
645
                }
646
18939
                let cursor_backup = parser.cursor;
647
                // we have already checked for strings, which subsume base64 byte strings
648
18939
                match parser.byte_string_no_base64() {
649
846
                    Ok(_) => (),
650
                    // prevent quadratic complexity backtracking for unterminated byte string
651
                    Err(err @ (Error::ExpectedStringEnd | Error::Eof)) => return Err(err),
652
18093
                    Err(_) => parser.set_cursor(cursor_backup),
653
                }
654

            
655
18939
                let c = parser.next_char()?;
656
18924
                if matches!(c, '(' | '[' | '{') {
657
1425
                    braces += 1;
658
17499
                } else if matches!(c, ')' | ']' | '}') {
659
7065
                    braces -= 1;
660
10449
                } else if c == ',' && braces == 1 {
661
564
                    parser.skip_ws()?;
662
564
                    more_than_one = !parser.check_char(')');
663
564
                    break;
664
9870
                }
665
            }
666

            
667
6204
            if more_than_one {
668
282
                Ok(StructType::NonNewtypeTuple)
669
            } else {
670
5922
                Ok(StructType::NewtypeTuple)
671
            }
672
79180
        }
673

            
674
        // Create a temporary working copy
675
79180
        let backup_cursor = self.cursor;
676

            
677
79180
        let result = check_struct_type_inner(self, newtype, tuple);
678

            
679
79180
        if result.is_ok() {
680
79161
            // Revert the parser to before the struct type check
681
79161
            self.set_cursor(backup_cursor);
682
79161
        }
683

            
684
79180
        result
685
79180
    }
686

            
687
    /// Only returns true if the char after `ident` cannot belong
688
    /// to an identifier.
689
5183268
    pub fn consume_ident(&mut self, ident: &str) -> bool {
690
5183268
        if self.check_ident(ident) {
691
157922
            self.advance_bytes(ident.len());
692

            
693
157922
            true
694
        } else {
695
5025346
            false
696
        }
697
5183268
    }
698

            
699
80474
    pub fn consume_struct_name(&mut self, ident: &'static str) -> Result<bool> {
700
80474
        if self.check_ident("") {
701
63223
            if self.exts.contains(Extensions::EXPLICIT_STRUCT_NAMES) {
702
846
                return Err(Error::ExpectedStructName(ident.to_string()));
703
62377
            }
704

            
705
62377
            return Ok(false);
706
17251
        }
707

            
708
17251
        let found_ident = match self.identifier() {
709
15277
            Ok(maybe_ident) => maybe_ident,
710
1410
            Err(Error::SuggestRawIdentifier(found_ident)) if found_ident == ident => {
711
282
                return Err(Error::SuggestRawIdentifier(found_ident))
712
            }
713
1692
            Err(_) => return Err(Error::ExpectedNamedStructLike(ident)),
714
        };
715

            
716
15277
        if ident.is_empty() {
717
312
            return Err(Error::ExpectedNamedStructLike(ident));
718
14965
        }
719

            
720
14965
        if found_ident != ident {
721
1704
            return Err(Error::ExpectedDifferentStructName {
722
1704
                expected: ident,
723
1704
                found: String::from(found_ident),
724
1704
            });
725
13261
        }
726

            
727
13261
        Ok(true)
728
80474
    }
729

            
730
    /// Parse a document attribute at the current cursor position.
731
581978
    fn attribute(&mut self) -> Result<ParsedAttribute> {
732
581978
        if !self.check_char('#') {
733
546568
            return Ok(ParsedAttribute::None);
734
35410
        }
735

            
736
35410
        if !self.consume_all(&["#", "!", "["])? {
737
12
            return Err(Error::ExpectedAttribute);
738
35398
        }
739

            
740
35398
        self.skip_ws()?;
741
35398
        if self.consume_ident("enable") {
742
35362
            self.skip_ws()?;
743
35362
            if !self.consume_str("(") {
744
                return Err(Error::ExpectedAttribute);
745
35362
            }
746

            
747
35362
            self.skip_ws()?;
748
35362
            let extensions = self.extension_list()?;
749
34852
            self.skip_ws()?;
750

            
751
34852
            if self.consume_all(&[")", "]"])? {
752
34837
                Ok(ParsedAttribute::Extensions(extensions))
753
            } else {
754
15
                Err(Error::ExpectedAttributeEnd)
755
            }
756
36
        } else if self.consume_ident("type") || self.consume_ident("schema") {
757
36
            self.skip_ws()?;
758
36
            if !self.consume_str("=") {
759
                return Err(Error::ExpectedAttribute);
760
36
            }
761

            
762
36
            self.skip_ws()?;
763
36
            self.string()?;
764
36
            self.skip_ws()?;
765

            
766
36
            if self.consume_str("]") {
767
36
                Ok(ParsedAttribute::Ignored)
768
            } else {
769
                Err(Error::ExpectedAttributeEnd)
770
            }
771
        } else {
772
            Err(Error::ExpectedAttribute)
773
        }
774
581978
    }
775

            
776
    /// Returns the extensions bit mask.
777
35362
    fn extension_list(&mut self) -> Result<Extensions> {
778
35362
        let mut extensions = Extensions::empty();
779

            
780
        loop {
781
35644
            let ident = self.identifier()?;
782
35644
            let extension = Extensions::from_ident(ident)
783
35644
                .ok_or_else(|| Error::NoSuchExtension(ident.into()))?;
784

            
785
35629
            extensions |= extension;
786

            
787
35629
            let comma = self.comma()?;
788

            
789
            // If we have no comma but another item, return an error
790
35629
            if !comma && self.check_ident_other_char(0) {
791
495
                return Err(Error::ExpectedComma);
792
35134
            }
793

            
794
            // If there's no comma, assume the list ended.
795
            // If there is, it might be a trailing one, thus we only
796
            // continue the loop if we get an ident char.
797
35134
            if !comma || !self.check_ident_other_char(0) {
798
34852
                break;
799
282
            }
800
        }
801

            
802
34852
        Ok(extensions)
803
35362
    }
804

            
805
15359
    pub fn float<T: Float>(&mut self) -> Result<T> {
806
        const F32_SUFFIX: &str = "f32";
807
        const F64_SUFFIX: &str = "f64";
808

            
809
90158
        for (literal, value_f32, value_f64) in &[
810
15359
            ("inf", f32::INFINITY, f64::INFINITY),
811
15359
            ("+inf", f32::INFINITY, f64::INFINITY),
812
15359
            ("-inf", f32::NEG_INFINITY, f64::NEG_INFINITY),
813
15359
            ("NaN", f32::NAN, f64::NAN),
814
15359
            ("+NaN", f32::NAN, f64::NAN),
815
15359
            ("-NaN", -f32::NAN, -f64::NAN),
816
15359
        ] {
817
90158
            if self.consume_ident(literal) {
818
88
                return T::parse(literal);
819
90070
            }
820

            
821
90070
            if let Some(suffix) = self.src().strip_prefix(literal) {
822
1172
                if let Some(post_suffix) = suffix.strip_prefix(F32_SUFFIX) {
823
584
                    if !post_suffix.chars().next().map_or(false, is_xid_continue) {
824
580
                        let float_ron = &self.src()[..literal.len() + F32_SUFFIX.len()];
825
580
                        self.advance_bytes(literal.len() + F32_SUFFIX.len());
826
580
                        return T::try_from_parsed_float(ParsedFloat::F32(*value_f32), float_ron);
827
4
                    }
828
588
                }
829

            
830
592
                if let Some(post_suffix) = suffix.strip_prefix(F64_SUFFIX) {
831
584
                    if !post_suffix.chars().next().map_or(false, is_xid_continue) {
832
580
                        let float_ron = &self.src()[..literal.len() + F64_SUFFIX.len()];
833
580
                        self.advance_bytes(literal.len() + F64_SUFFIX.len());
834
580
                        return T::try_from_parsed_float(ParsedFloat::F64(*value_f64), float_ron);
835
4
                    }
836
8
                }
837
88898
            }
838
        }
839

            
840
14111
        let num_bytes = self.next_chars_while_len(is_float_char);
841

            
842
14111
        if num_bytes == 0 {
843
46
            return Err(Error::ExpectedFloat);
844
14065
        }
845

            
846
14065
        if self.check_char('_') {
847
4
            return Err(Error::UnderscoreAtBeginning);
848
14061
        }
849

            
850
14061
        let mut f = String::with_capacity(num_bytes);
851
14061
        let mut allow_underscore = false;
852

            
853
254929
        for (i, c) in self.src()[..num_bytes].char_indices() {
854
776
            match c {
855
768
                '_' if allow_underscore => continue,
856
                '_' => {
857
8
                    self.advance_bytes(i);
858
8
                    return Err(Error::FloatUnderscore);
859
                }
860
240389
                '0'..='9' | 'e' | 'E' => allow_underscore = true,
861
11525
                '.' => allow_underscore = false,
862
2239
                _ => (),
863
            }
864

            
865
            // we know that the byte is an ASCII character here
866
254153
            f.push(c);
867
        }
868

            
869
14053
        if self.src()[num_bytes..].starts_with('f') {
870
2010
            let backup_cursor = self.cursor;
871
2010
            self.advance_bytes(num_bytes);
872

            
873
            #[allow(clippy::never_loop)]
874
            loop {
875
2010
                let res = if self.consume_ident(F32_SUFFIX) {
876
1144
                    f32::from_str(&f).map(ParsedFloat::F32)
877
866
                } else if self.consume_ident(F64_SUFFIX) {
878
580
                    f64::from_str(&f).map(ParsedFloat::F64)
879
                } else {
880
286
                    break;
881
                };
882

            
883
1724
                let parsed = if let Ok(parsed) = res {
884
1716
                    parsed
885
                } else {
886
8
                    self.set_cursor(backup_cursor);
887
8
                    return Err(Error::ExpectedFloat);
888
                };
889

            
890
1716
                let float_ron = &self.src[backup_cursor.cursor..self.cursor.cursor];
891

            
892
1716
                return T::try_from_parsed_float(parsed, float_ron);
893
            }
894

            
895
286
            self.set_cursor(backup_cursor);
896
12043
        }
897

            
898
12329
        let value = T::parse(&f)?;
899

            
900
12309
        self.advance_bytes(num_bytes);
901

            
902
12309
        Ok(value)
903
15359
    }
904

            
905
596669
    pub fn skip_identifier(&mut self) -> Option<&'a str> {
906
        #[allow(clippy::nonminimal_bool)]
907
596669
        if self.check_str("b\"") // byte string
908
595251
            || self.check_str("b'") // byte literal
909
377829
            || self.check_str("br#") // raw byte string
910
376983
            || self.check_str("br\"") // raw byte string
911
376137
            || self.check_str("r\"") // raw string
912
375573
            || self.check_str("r#\"") // raw string
913
375291
            || self.check_str("r##") // raw string
914
375009
            || false
915
        {
916
221660
            return None;
917
375009
        }
918

            
919
375009
        if self.check_str("r#") {
920
            // maybe a raw identifier
921
12
            let len = self.next_chars_while_from_len(2, is_ident_raw_char);
922
12
            if len > 0 {
923
4
                let ident = &self.src()[2..2 + len];
924
4
                self.advance_bytes(2 + len);
925
4
                return Some(ident);
926
8
            }
927
8
            return None;
928
374997
        }
929

            
930
374997
        if let Some(c) = self.peek_char() {
931
            // maybe a normal identifier
932
373587
            if is_ident_first_char(c) {
933
112433
                let len =
934
112433
                    c.len_utf8() + self.next_chars_while_from_len(c.len_utf8(), is_xid_continue);
935
112433
                let ident = &self.src()[..len];
936
112433
                self.advance_bytes(len);
937
112433
                return Some(ident);
938
261154
            }
939
1410
        }
940

            
941
262564
        None
942
596669
    }
943

            
944
306842
    pub fn identifier(&mut self) -> Result<&'a str> {
945
306842
        let first = self.peek_char_or_eof()?;
946
306842
        if !is_ident_first_char(first) {
947
2268
            if is_ident_raw_char(first) {
948
1128
                let ident_bytes = self.next_chars_while_len(is_ident_raw_char);
949
1128
                return Err(Error::SuggestRawIdentifier(
950
1128
                    self.src()[..ident_bytes].into(),
951
1128
                ));
952
1140
            }
953

            
954
1140
            return Err(Error::ExpectedIdentifier);
955
304574
        }
956

            
957
        // If the next 2-3 bytes signify the start of a (raw) (byte) string
958
        //  literal, return an error.
959
        #[allow(clippy::nonminimal_bool)]
960
304574
        if self.check_str("b\"") // byte string
961
304292
            || self.check_str("b'") // byte literal
962
304010
            || self.check_str("br#") // raw byte string
963
303728
            || self.check_str("br\"") // raw byte string
964
303446
            || self.check_str("r\"") // raw string
965
303164
            || self.check_str("r#\"") // raw string
966
302882
            || self.check_str("r##") // raw string
967
302600
            || false
968
        {
969
1974
            return Err(Error::ExpectedIdentifier);
970
302600
        }
971

            
972
302600
        let length = if self.check_str("r#") {
973
7360
            let cursor_backup = self.cursor;
974

            
975
7360
            self.advance_bytes(2);
976

            
977
            // Note: it's important to check this before advancing forward, so that
978
            // the value-type deserializer can fall back to parsing it differently.
979
7360
            if !matches!(self.peek_char(), Some(c) if is_ident_raw_char(c)) {
980
564
                self.set_cursor(cursor_backup);
981
564
                return Err(Error::ExpectedIdentifier);
982
6796
            }
983

            
984
6796
            self.next_chars_while_len(is_ident_raw_char)
985
295240
        } else if first == 'r' {
986
564
            let std_ident_length = self.next_chars_while_len(is_xid_continue);
987
564
            let raw_ident_length = self.next_chars_while_len(is_ident_raw_char);
988

            
989
564
            if raw_ident_length > std_ident_length {
990
282
                return Err(Error::SuggestRawIdentifier(
991
282
                    self.src()[..raw_ident_length].into(),
992
282
                ));
993
282
            }
994

            
995
282
            std_ident_length
996
        } else {
997
294676
            let std_ident_length = first.len_utf8()
998
294676
                + self.next_chars_while_from_len(first.len_utf8(), is_xid_continue);
999
294676
            let raw_ident_length = self.next_chars_while_len(is_ident_raw_char);
294676
            if raw_ident_length > std_ident_length {
846
                return Err(Error::SuggestRawIdentifier(
846
                    self.src()[..raw_ident_length].into(),
846
                ));
293830
            }
293830
            std_ident_length
        };
300908
        let ident = &self.src()[..length];
300908
        self.advance_bytes(length);
300908
        Ok(ident)
306842
    }
312261
    pub fn next_bytes_is_float(&mut self) -> bool {
312261
        if let Some(c) = self.peek_char() {
312257
            let skip = match c {
5158
                '+' | '-' => 1,
307099
                _ => 0,
            };
312257
            let valid_float_len = self.next_chars_while_from_len(skip, is_float_char);
312257
            let valid_int_len = self.next_chars_while_from_len(skip, is_int_char);
312257
            valid_float_len > valid_int_len
        } else {
4
            false
        }
312261
    }
3662241
    pub fn skip_ws(&mut self) -> Result<()> {
3662241
        if (self.cursor.last_ws_len != WS_CURSOR_UNCLOSED_LINE)
3661673
            && ((self.cursor.pre_ws_cursor + self.cursor.last_ws_len) < self.cursor.cursor)
2362415
        {
2362415
            // the last whitespace is disjoint from this one, we need to track a new one
2362415
            self.cursor.pre_ws_cursor = self.cursor.cursor;
2362415
        }
3662241
        if self.src().is_empty() {
440414
            return Ok(());
3221827
        }
        loop {
3250320
            self.advance_bytes(self.next_chars_while_len(is_whitespace_char));
3250320
            match self.skip_comment()? {
3219845
                None => break,
                Some(Comment::UnclosedLine) => {
1132
                    self.cursor.last_ws_len = WS_CURSOR_UNCLOSED_LINE;
1132
                    return Ok(());
                }
28493
                Some(Comment::ClosedLine | Comment::Block) => continue,
            }
        }
3219845
        self.cursor.last_ws_len = self.cursor.cursor - self.cursor.pre_ws_cursor;
3219845
        Ok(())
3662241
    }
18048
    pub fn has_unclosed_line_comment(&self) -> bool {
18048
        self.src().is_empty() && self.cursor.last_ws_len == WS_CURSOR_UNCLOSED_LINE
18048
    }
8790
    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
8790
        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),
            }
8782
        } 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 {
8770
            self.byte_string_no_base64()
        }
8790
    }
27709
    pub fn byte_string_no_base64(&mut self) -> Result<ParsedByteStr<'a>> {
27709
        if self.consume_str("b\"") {
5950
            self.escaped_byte_string()
21759
        } else if self.consume_str("br") {
3666
            self.raw_byte_string()
        } else {
18093
            Err(Error::ExpectedByteString)
        }
27709
    }
5950
    fn escaped_byte_string(&mut self) -> Result<ParsedByteStr<'a>> {
5950
        match self.escaped_byte_buf(EscapeEncoding::Binary) {
5386
            Ok((bytes, advance)) => {
5386
                self.advance_bytes(advance);
5386
                Ok(bytes)
            }
564
            Err(err) => Err(err),
        }
5950
    }
3666
    fn raw_byte_string(&mut self) -> Result<ParsedByteStr<'a>> {
3666
        match self.raw_byte_buf() {
3102
            Ok((bytes, advance)) => {
3102
                self.advance_bytes(advance);
3102
                Ok(bytes)
            }
282
            Err(Error::ExpectedString) => Err(Error::ExpectedByteString),
282
            Err(err) => Err(err),
        }
3666
    }
107606
    pub fn string(&mut self) -> Result<ParsedStr<'a>> {
107606
        if self.consume_char('"') {
84937
            self.escaped_string()
22669
        } else if self.consume_char('r') {
3154
            self.raw_string()
        } else {
19515
            Err(Error::ExpectedString)
        }
107606
    }
84945
    fn escaped_string(&mut self) -> Result<ParsedStr<'a>> {
84945
        match self.escaped_byte_buf(EscapeEncoding::Utf8) {
82956
            Ok((bytes, advance)) => {
82956
                let string = ParsedStr::try_from_bytes(bytes).map_err(Error::from)?;
82956
                self.advance_bytes(advance);
82956
                Ok(string)
            }
1989
            Err(err) => Err(err),
        }
84945
    }
3166
    fn raw_string(&mut self) -> Result<ParsedStr<'a>> {
3166
        match self.raw_byte_buf() {
2598
            Ok((bytes, advance)) => {
2598
                let string = ParsedStr::try_from_bytes(bytes).map_err(Error::from)?;
2598
                self.advance_bytes(advance);
2598
                Ok(string)
            }
568
            Err(err) => Err(err),
        }
3166
    }
90895
    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
90895
        let str_end = self.src().find('"').ok_or(Error::ExpectedStringEnd)?;
90316
        let escape = self.src()[..str_end].find('\\');
90316
        if let Some(escape) = escape {
            // Now check if escaping is used inside the string
10756
            let mut i = escape;
10756
            let mut s = self.src().as_bytes()[..i].to_vec();
            loop {
23526
                self.advance_bytes(i + 1);
23526
                match self.parse_escape(encoding, false)? {
10546
                    EscapeCharacter::Ascii(c) => s.push(c),
11006
                    EscapeCharacter::Utf8(c) => match c.len_utf8() {
10434
                        1 => s.push(c as u8),
572
                        len => {
572
                            let start = s.len();
572
                            s.extend(core::iter::repeat(0).take(len));
572
                            c.encode_utf8(&mut s[start..]);
572
                        }
                    },
                }
                // Checking for '"' and '\\' separately is faster than searching for both at the same time
21552
                let new_str_end = self.src().find('"').ok_or(Error::ExpectedStringEnd)?;
21552
                let new_escape = self.src()[..new_str_end].find('\\');
21552
                if let Some(new_escape) = new_escape {
12770
                    s.extend_from_slice(&self.src().as_bytes()[..new_escape]);
12770
                    i = new_escape;
12770
                } else {
8782
                    s.extend_from_slice(&self.src().as_bytes()[..new_str_end]);
                    // Advance to the end of the string + 1 for the `"`.
8782
                    break Ok((ParsedByteStr::Allocated(s), new_str_end + 1));
                }
            }
        } else {
79560
            let s = &self.src().as_bytes()[..str_end];
            // Advance by the number of bytes of the string + 1 for the `"`.
79560
            Ok((ParsedByteStr::Slice(s), str_end + 1))
        }
90895
    }
6832
    fn raw_byte_buf(&mut self) -> Result<(ParsedByteStr<'a>, usize)> {
13652
        let num_hashes = self.next_chars_while_len(|c| c == '#');
6832
        let hashes = &self.src()[..num_hashes];
6832
        self.advance_bytes(num_hashes);
6832
        self.expect_char('"', Error::ExpectedString)?;
6264
        let ending = ["\"", hashes].concat();
6264
        let i = self.src().find(&ending).ok_or(Error::ExpectedStringEnd)?;
5700
        let s = &self.src().as_bytes()[..i];
        // Advance by the number of bytes of the byte string
        // + `num_hashes` + 1 for the `"`.
5700
        Ok((ParsedByteStr::Slice(s), i + num_hashes + 1))
6832
    }
198934
    fn decode_ascii_escape(&mut self) -> Result<u8> {
198934
        let mut n = 0;
198934
        for _ in 0..2 {
397868
            n <<= 4;
397868
            let byte = self.next_char()?;
397868
            let decoded = Self::decode_hex(byte)?;
397304
            n |= decoded;
        }
198370
        Ok(n)
198934
    }
    #[inline]
962637
    fn decode_hex(c: char) -> Result<u8> {
962637
        if !c.is_ascii() {
282
            return Err(Error::InvalidEscape("Non-hex digit found"));
962355
        }
        // c is an ASCII character that can be losslessly cast to u8
962355
        match c as u8 {
962073
            c @ b'0'..=b'9' => Ok(c - b'0'),
111736
            c @ b'a'..=b'f' => Ok(10 + c - b'a'),
57528
            c @ b'A'..=b'F' => Ok(10 + c - b'A'),
282
            _ => Err(Error::InvalidEscape("Non-hex digit found")),
        }
962637
    }
218682
    fn parse_escape(&mut self, encoding: EscapeEncoding, is_char: bool) -> Result<EscapeCharacter> {
218682
        let c = match self.next_char()? {
858
            '\'' => EscapeCharacter::Ascii(b'\''),
3408
            '"' => EscapeCharacter::Ascii(b'"'),
2538
            '\\' => EscapeCharacter::Ascii(b'\\'),
1410
            'n' => EscapeCharacter::Ascii(b'\n'),
564
            'r' => EscapeCharacter::Ascii(b'\r'),
564
            't' => EscapeCharacter::Ascii(b'\t'),
1692
            '0' => EscapeCharacter::Ascii(b'\0'),
            'x' => {
                // Fast exit for ascii escape in byte string
195804
                let b: u8 = self.decode_ascii_escape()?;
195240
                if let EscapeEncoding::Binary = encoding {
192412
                    return Ok(EscapeCharacter::Ascii(b));
2828
                }
                // Fast exit for ascii character in UTF-8 string
2828
                let mut bytes = [b, 0, 0, 0];
2828
                if let Ok(Some(c)) = from_utf8(&bytes[..=0]).map(|s| s.chars().next()) {
846
                    return Ok(EscapeCharacter::Utf8(c));
1982
                }
1982
                if is_char {
                    // Character literals are not allowed to use multiple byte
                    //  escapes to build a unicode character
282
                    return Err(Error::InvalidEscape(
282
                        "Not a valid byte-escaped Unicode character",
282
                    ));
1700
                }
                // UTF-8 character needs up to four bytes and we have already
                //  consumed one, so at most three to go
4254
                for i in 1..4 {
4254
                    if !self.consume_str(r"\x") {
1128
                        return Err(Error::InvalidEscape(
1128
                            "Not a valid byte-escaped Unicode character",
1128
                        ));
3126
                    }
3126
                    bytes[i] = self.decode_ascii_escape()?;
                    // Check if we now have a valid UTF-8 character
3126
                    if let Ok(Some(c)) = from_utf8(&bytes[..=i]).map(|s| s.chars().next()) {
290
                        return Ok(EscapeCharacter::Utf8(c));
2836
                    }
                }
282
                return Err(Error::InvalidEscape(
282
                    "Not a valid byte-escaped Unicode character",
282
                ));
            }
            'u' => {
11562
                self.expect_char('{', Error::InvalidEscape("Missing { in Unicode escape"))?;
11562
                let mut bytes: u32 = 0;
11562
                let mut num_digits = 0;
30738
                while num_digits < 6 {
30738
                    let byte = self.peek_char_or_eof()?;
30738
                    if byte == '}' {
11562
                        break;
19176
                    }
19176
                    self.skip_next_char();
19176
                    num_digits += 1;
19176
                    let byte = Self::decode_hex(byte)?;
19176
                    bytes <<= 4;
19176
                    bytes |= u32::from(byte);
                }
11562
                if num_digits == 0 {
282
                    return Err(Error::InvalidEscape(
282
                        "Expected 1-6 digits, got 0 digits in Unicode escape",
282
                    ));
11280
                }
11280
                self.expect_char(
                    '}',
11280
                    Error::InvalidEscape("No } at the end of Unicode escape"),
                )?;
11280
                let c = char_from_u32(bytes).ok_or(Error::InvalidEscape(
11280
                    "Not a valid Unicode-escaped character",
11280
                ))?;
11280
                EscapeCharacter::Utf8(c)
            }
282
            _ => return Err(Error::InvalidEscape("Unknown escape character")),
        };
22314
        Ok(c)
218682
    }
3250320
    fn skip_comment(&mut self) -> Result<Option<Comment>> {
3250320
        if self.consume_char('/') {
30475
            match self.next_char()? {
                '/' => {
125694
                    let bytes = self.next_chars_while_len(|c| c != '\n');
8467
                    self.advance_bytes(bytes);
8467
                    if self.src().is_empty() {
1132
                        Ok(Some(Comment::UnclosedLine))
                    } else {
7335
                        Ok(Some(Comment::ClosedLine))
                    }
                }
                '*' => {
21726
                    let mut level = 1;
50216
                    while level > 0 {
197150
                        let bytes = self.next_chars_while_len(|c| !matches!(c, '/' | '*'));
29058
                        if self.src().is_empty() {
286
                            return Err(Error::UnclosedBlockComment);
28772
                        }
28772
                        self.advance_bytes(bytes);
                        // check whether / or * and take action
28772
                        if self.consume_str("/*") {
1974
                            level += 1;
26798
                        } else if self.consume_str("*/") {
23132
                            level -= 1;
23132
                        } else {
3666
                            self.next_char().map_err(|_| Error::UnclosedBlockComment)?;
                        }
                    }
21158
                    Ok(Some(Comment::Block))
                }
282
                c => Err(Error::UnexpectedChar(c)),
            }
        } else {
3219845
            Ok(None)
        }
3250320
    }
}
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 {
1313694
            fn from_u8(x: u8) -> Self {
73918
                x as $ty
1313694
            }
546447
            fn checked_mul_ext(&mut self, x: u8) -> bool {
546447
                match self.checked_mul(Self::from_u8(x)) {
545593
                    Some(n) => {
545593
                        *self = n;
545593
                        false
                    }
854
                    None => true,
                }
546447
            }
448961
            fn checked_add_ext(&mut self, x: u8) -> bool {
448961
                match self.checked_add(Self::from_u8(x)) {
448538
                    Some(n) => {
448538
                        *self = n;
448538
                        false
                    }
423
                    None => true,
                }
448961
            }
94658
            fn checked_sub_ext(&mut self, x: u8) -> bool {
94658
                match self.checked_sub(Self::from_u8(x)) {
91838
                    Some(n) => {
91838
                        *self = n;
91838
                        false
                    }
2820
                    None => true,
                }
94658
            }
        }
    };
    ($($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, base: u8) -> 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 {
111558
            fn parse(parser: &mut Parser, sign: i8, base: u8) -> Result<Self> {
111558
                parser.parse_integer(sign, base)
111558
            }
33558
            fn try_from_parsed_integer(parsed: ParsedInteger, ron: &str) -> Result<Self> {
33558
                match parsed {
12408
                    ParsedInteger::$wrap(v) => Ok(v),
                    _ => Err(Error::InvalidValueForType {
21150
                        expected: format!(
                            "a{} {}-bit {}signed integer",
21150
                            if <$ty>::BITS == 8 { "n" } else { "n" },
                            <$ty>::BITS,
21150
                            if <$ty>::MIN == 0 { "un" } else { "" },
                        ),
21150
                        found: String::from(ron),
                    }),
                }
33558
            }
        }
    };
    ($($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 {
77420
    fn parse(parser: &mut Parser, sign: i8, base: u8) -> Result<Self> {
77420
        if sign < 0 {
2024
            let signed = parser.parse_integer::<LargeSInt>(-1, base)?;
590
            return if let Ok(x) = i8::try_from(signed) {
294
                Ok(ParsedInteger::I8(x))
296
            } else if let Ok(x) = i16::try_from(signed) {
4
                Ok(ParsedInteger::I16(x))
292
            } 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")]
286
                if let Ok(x) = i64::try_from(signed) {
2
                    Ok(ParsedInteger::I64(x))
                } else {
284
                    Ok(ParsedInteger::I128(signed))
                }
            };
75396
        }
75396
        let unsigned = parser.parse_integer::<LargeUInt>(1, base)?;
74671
        if let Ok(x) = u8::try_from(unsigned) {
73807
            Ok(ParsedInteger::U8(x))
864
        } else if let Ok(x) = u16::try_from(unsigned) {
290
            Ok(ParsedInteger::U16(x))
574
        } 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")]
568
            if let Ok(x) = u64::try_from(unsigned) {
2
                Ok(ParsedInteger::U64(x))
            } else {
566
                Ok(ParsedInteger::U128(unsigned))
            }
        }
77420
    }
222498
    fn try_from_parsed_integer(parsed: ParsedInteger, _ron: &str) -> Result<Self> {
222498
        Ok(parsed)
222498
    }
}
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 {
32709
            fn parse(float: &str) -> Result<Self> {
32709
                <$ty>::from_str(float).map_err(|_| Error::ExpectedFloat)
32709
            }
3948
            fn try_from_parsed_float(parsed: ParsedFloat, ron: &str) -> Result<Self> {
3948
                match parsed {
3384
                    ParsedFloat::$wrap(v) => Ok(v),
564
                    _ => Err(Error::InvalidValueForType {
564
                        expected: format!(
564
                            "a {}-bit floating point number", $bits,
564
                        ),
564
                        found: String::from(ron),
564
                    }),
                }
3948
            }
        }
    };
    ($($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 {
11678
    fn parse(float: &str) -> Result<Self> {
11678
        let value = f64::from_str(float).map_err(|_| Error::ExpectedFloat)?;
        #[allow(clippy::cast_possible_truncation)]
11678
        if value.total_cmp(&f64::from(value as f32)).is_eq() {
4596
            Ok(ParsedFloat::F32(value as f32))
        } else {
7082
            Ok(ParsedFloat::F64(value))
        }
11678
    }
2820
    fn try_from_parsed_float(parsed: ParsedFloat, _ron: &str) -> Result<Self> {
2820
        Ok(parsed)
2820
    }
}
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> {
85554
    pub fn try_from_bytes(bytes: ParsedByteStr<'a>) -> Result<Self, Utf8Error> {
85554
        match bytes {
5942
            ParsedByteStr::Allocated(byte_buf) => Ok(ParsedStr::Allocated(
5942
                String::from_utf8(byte_buf).map_err(|e| e.utf8_error())?,
            )),
79612
            ParsedByteStr::Slice(bytes) => Ok(ParsedStr::Slice(from_utf8(bytes)?)),
        }
85554
    }
}
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
    }
}