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, SpannedError, SpannedResult},
17
    extensions::Extensions,
18
    value::Number,
19
};
20

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

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

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

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

            
37
5425743
pub const fn is_whitespace_char(c: char) -> bool {
38
3204265
    matches!(
39
5425743
        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
5425743
}
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
}
68

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

            
76
const WS_CURSOR_UNCLOSED_LINE: usize = usize::MAX;
77

            
78
impl PartialEq for ParserCursor {
79
8
    fn eq(&self, other: &Self) -> bool {
80
8
        self.cursor == other.cursor
81
8
    }
82
}
83

            
84
impl PartialOrd for ParserCursor {
85
3388
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
86
3388
        self.cursor.partial_cmp(&other.cursor)
87
3388
    }
88
}
89

            
90
/// constructor and parsing utilities
91
impl<'a> Parser<'a> {
92
540035
    pub fn new(src: &'a str) -> SpannedResult<Self> {
93
540035
        let mut parser = Parser {
94
540035
            exts: Extensions::empty(),
95
540035
            src,
96
540035
            cursor: ParserCursor {
97
540035
                cursor: 0,
98
540035
                pre_ws_cursor: 0,
99
540035
                last_ws_len: 0,
100
540035
            },
101
540035
        };
102
540035

            
103
540035
        parser.skip_ws().map_err(|e| parser.span_error(e))?;
104

            
105
        // Loop over all extensions attributes
106
        loop {
107
573459
            let attribute = parser.extensions().map_err(|e| parser.span_error(e))?;
108

            
109
573145
            if attribute.is_empty() {
110
539443
                break;
111
33702
            }
112
33702

            
113
33702
            parser.exts |= attribute;
114
33702
            parser.skip_ws().map_err(|e| parser.span_error(e))?;
115
        }
116

            
117
539443
        Ok(parser)
118
540035
    }
119

            
120
184123
    fn set_cursor(&mut self, cursor: ParserCursor) {
121
184123
        self.cursor = cursor;
122
184123
    }
123

            
124
106711
    pub fn span_error(&self, code: Error) -> SpannedError {
125
106711
        SpannedError {
126
106711
            code,
127
106711
            position: Position::from_src_end(&self.src[..self.cursor.cursor]),
128
106711
        }
129
106711
    }
130

            
131
6255207
    pub fn advance_bytes(&mut self, bytes: usize) {
132
6255207
        self.cursor.cursor += bytes;
133
6255207
    }
134

            
135
963120
    pub fn next_char(&mut self) -> Result<char> {
136
963120
        let c = self.peek_char_or_eof()?;
137
962830
        self.cursor.cursor += c.len_utf8();
138
962830
        Ok(c)
139
963120
    }
140

            
141
37364
    pub fn skip_next_char(&mut self) {
142
37364
        core::mem::drop(self.next_char());
143
37364
    }
144

            
145
2950197
    pub fn peek_char(&self) -> Option<char> {
146
2950197
        self.src().chars().next()
147
2950197
    }
148

            
149
2201749
    pub fn peek_char_or_eof(&self) -> Result<char> {
150
2201749
        self.peek_char().ok_or(Error::Eof)
151
2201749
    }
152

            
153
6462783
    pub fn check_char(&self, c: char) -> bool {
154
6462783
        self.src().starts_with(c)
155
6462783
    }
156

            
157
13320812
    pub fn check_str(&self, s: &str) -> bool {
158
13320812
        self.src().starts_with(s)
159
13320812
    }
160

            
161
39798749
    pub fn src(&self) -> &'a str {
162
39798749
        &self.src[self.cursor.cursor..]
163
39798749
    }
164

            
165
25894
    pub fn pre_ws_src(&self) -> &'a str {
166
25894
        &self.src[self.cursor.pre_ws_cursor..]
167
25894
    }
168

            
169
1733083
    pub fn consume_str(&mut self, s: &str) -> bool {
170
1733083
        if self.check_str(s) {
171
513868
            self.advance_bytes(s.len());
172
513868

            
173
513868
            true
174
        } else {
175
1219215
            false
176
        }
177
1733083
    }
178

            
179
4982731
    pub fn consume_char(&mut self, c: char) -> bool {
180
4982731
        if self.check_char(c) {
181
1517123
            self.advance_bytes(c.len_utf8());
182
1517123

            
183
1517123
            true
184
        } else {
185
3465608
            false
186
        }
187
4982731
    }
188

            
189
67730
    fn consume_all(&mut self, all: &[&str]) -> Result<bool> {
190
67730
        all.iter()
191
237508
            .map(|elem| {
192
237508
                if self.consume_str(elem) {
193
237448
                    self.skip_ws()?;
194

            
195
237448
                    Ok(true)
196
                } else {
197
60
                    Ok(false)
198
                }
199
237508
            })
200
237508
            .try_fold(true, |acc, x| x.map(|x| x && acc))
201
67730
    }
202

            
203
143124
    pub fn expect_char(&mut self, expected: char, error: Error) -> Result<()> {
204
143124
        if self.consume_char(expected) {
205
123902
            Ok(())
206
        } else {
207
19222
            Err(error)
208
        }
209
143124
    }
210

            
211
    #[must_use]
212
4064623
    pub fn next_chars_while_len(&self, condition: fn(char) -> bool) -> usize {
213
4064623
        self.next_chars_while_from_len(0, condition)
214
4064623
    }
215

            
216
    #[must_use]
217
5085121
    pub fn next_chars_while_from_len(&self, from: usize, condition: fn(char) -> bool) -> usize {
218
5085121
        self.src()[from..]
219
13424735
            .find(|c| !condition(c))
220
5085121
            .unwrap_or(self.src().len() - from)
221
5085121
    }
222
}
223

            
224
/// actual parsing of ron tokens
225
impl<'a> Parser<'a> {
226
216482
    fn parse_integer_digits<T: Num>(
227
216482
        &mut self,
228
216482
        s: &str,
229
216482
        base: u8,
230
216482
        f: fn(&mut T, u8) -> bool,
231
216482
    ) -> Result<T> {
232
216482
        let mut num_acc = T::from_u8(0);
233

            
234
527647
        for (i, c) in s.char_indices() {
235
527647
            if c == '_' {
236
6538
                continue;
237
521109
            }
238
521109

            
239
521109
            if num_acc.checked_mul_ext(base) {
240
842
                self.advance_bytes(s.len());
241
842
                return Err(Error::IntegerOutOfBounds);
242
520267
            }
243

            
244
520267
            let digit = Self::decode_hex(c)?;
245

            
246
520267
            if digit >= base {
247
1946
                self.advance_bytes(i);
248
1946
                return Err(Error::InvalidIntegerDigit { digit: c, base });
249
518321
            }
250
518321

            
251
518321
            if f(&mut num_acc, digit) {
252
3197
                self.advance_bytes(s.len());
253
3197
                return Err(Error::IntegerOutOfBounds);
254
515124
            }
255
        }
256

            
257
210497
        self.advance_bytes(s.len());
258
210497

            
259
210497
        Ok(num_acc)
260
216482
    }
261

            
262
223468
    fn parse_integer<T: Num>(&mut self, sign: i8) -> Result<T> {
263
223468
        let base = match () {
264
223468
            () if self.consume_str("0b") => 2,
265
220966
            () if self.consume_str("0o") => 8,
266
218742
            () if self.consume_str("0x") => 16,
267
216240
            () => 10,
268
        };
269

            
270
223468
        let num_bytes = self.next_chars_while_len(is_int_char);
271
223468

            
272
223468
        if num_bytes == 0 {
273
4484
            return Err(Error::ExpectedInteger);
274
218984
        }
275
218984

            
276
218984
        if self.check_char('_') {
277
2502
            return Err(Error::UnderscoreAtBeginning);
278
216482
        }
279
216482

            
280
216482
        let s = &self.src()[..num_bytes];
281
216482

            
282
216482
        if sign > 0 {
283
203638
            self.parse_integer_digits(s, base, T::checked_add_ext)
284
        } else {
285
12844
            self.parse_integer_digits(s, base, T::checked_sub_ext)
286
        }
287
223468
    }
288

            
289
    #[allow(clippy::too_many_lines)]
290
322331
    pub fn integer<T: Integer>(&mut self) -> Result<T> {
291
322331
        let src_backup = self.src();
292

            
293
322331
        let is_negative = match self.peek_char_or_eof()? {
294
            '+' => {
295
28
                self.skip_next_char();
296
28
                false
297
            }
298
            '-' => {
299
4938
                self.skip_next_char();
300
4938
                true
301
            }
302
215176
            'b' if self.consume_str("b'") => {
303
                // Parse a byte literal
304
215176
                let byte = match self.next_char()? {
305
188762
                    '\\' => match self.parse_escape(EscapeEncoding::Binary, true)? {
306
                        // we know that this byte is an ASCII character
307
188484
                        EscapeCharacter::Ascii(b) => b,
308
                        EscapeCharacter::Utf8(_) => {
309
278
                            return Err(Error::InvalidEscape(
310
278
                                "Unexpected Unicode escape in byte literal",
311
278
                            ))
312
                        }
313
                    },
314
26414
                    b if b.is_ascii() => b as u8,
315
278
                    _ => return Err(Error::ExpectedByteLiteral),
316
                };
317

            
318
214620
                if !self.consume_char('\'') {
319
278
                    return Err(Error::ExpectedByteLiteral);
320
214342
                }
321
214342

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

            
324
214342
                return T::try_from_parsed_integer(ParsedInteger::U8(byte), bytes_ron);
325
            }
326
102189
            _ => false,
327
        };
328
107155
        let sign = if is_negative { -1 } else { 1 };
329

            
330
107155
        let num_bytes = self.next_chars_while_len(is_int_char);
331
107155

            
332
107155
        if self.src()[num_bytes..].starts_with(['i', 'u']) {
333
11939
            let int_cursor = self.cursor;
334
11939
            self.advance_bytes(num_bytes);
335

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

            
419
10243
                if !matches!(
420
1829
                    &res,
421
                    Err(Error::UnderscoreAtBeginning | Error::InvalidIntegerDigit { .. })
422
10243
                ) {
423
10243
                    // Advance past the number suffix
424
10243
                    self.skip_identifier();
425
10243
                }
426

            
427
10525
                let integer_ron = &src_backup[..src_backup.len() - suffix_bytes.len()];
428
10525

            
429
10525
                return res.and_then(|parsed| T::try_from_parsed_integer(parsed, integer_ron));
430
            }
431

            
432
1414
            self.set_cursor(int_cursor);
433
95216
        }
434

            
435
96630
        T::parse(self, sign)
436
322331
    }
437

            
438
307553
    pub fn any_number(&mut self) -> Result<Number> {
439
307553
        if self.next_bytes_is_float() {
440
10914
            return match self.float::<ParsedFloat>()? {
441
3384
                ParsedFloat::F32(v) => Ok(Number::F32(v.into())),
442
7530
                ParsedFloat::F64(v) => Ok(Number::F64(v.into())),
443
            };
444
296639
        }
445
296639

            
446
296639
        let backup_cursor = self.cursor;
447

            
448
296639
        let (integer_err, integer_cursor) = match self.integer::<ParsedInteger>() {
449
293259
            Ok(integer) => {
450
293259
                return match integer {
451
846
                    ParsedInteger::I8(v) => Ok(Number::I8(v)),
452
560
                    ParsedInteger::I16(v) => Ok(Number::I16(v)),
453
560
                    ParsedInteger::I32(v) => Ok(Number::I32(v)),
454
560
                    ParsedInteger::I64(v) => Ok(Number::I64(v)),
455
                    #[cfg(feature = "integer128")]
456
558
                    ParsedInteger::I128(v) => Ok(Number::I128(v)),
457
287655
                    ParsedInteger::U8(v) => Ok(Number::U8(v)),
458
564
                    ParsedInteger::U16(v) => Ok(Number::U16(v)),
459
560
                    ParsedInteger::U32(v) => Ok(Number::U32(v)),
460
560
                    ParsedInteger::U64(v) => Ok(Number::U64(v)),
461
                    #[cfg(feature = "integer128")]
462
836
                    ParsedInteger::U128(v) => Ok(Number::U128(v)),
463
                }
464
            }
465
3380
            Err(err) => (err, self.cursor),
466
3380
        };
467
3380

            
468
3380
        self.set_cursor(backup_cursor);
469
3380

            
470
3380
        // Fall-back to parse an out-of-range integer as a float
471
3380
        match self.float::<ParsedFloat>() {
472
2816
            Ok(ParsedFloat::F32(v)) if self.cursor >= integer_cursor => Ok(Number::F32(v.into())),
473
564
            Ok(ParsedFloat::F64(v)) if self.cursor >= integer_cursor => Ok(Number::F64(v.into())),
474
            _ => {
475
                // Return the more precise integer error
476
1251
                self.set_cursor(integer_cursor);
477
1251
                Err(integer_err)
478
            }
479
        }
480
307553
    }
481

            
482
28488
    pub fn bool(&mut self) -> Result<bool> {
483
28488
        if self.consume_ident("true") {
484
14786
            Ok(true)
485
13702
        } else if self.consume_ident("false") {
486
13666
            Ok(false)
487
        } else {
488
36
            Err(Error::ExpectedBoolean)
489
        }
490
28488
    }
491

            
492
66546
    pub fn char(&mut self) -> Result<char> {
493
66546
        self.expect_char('\'', Error::ExpectedChar)?;
494

            
495
47884
        let c = self.next_char()?;
496

            
497
47884
        let c = if c == '\\' {
498
3626
            match self.parse_escape(EscapeEncoding::Utf8, true)? {
499
                // we know that this byte is an ASCII character
500
1680
                EscapeCharacter::Ascii(b) => char::from(b),
501
1112
                EscapeCharacter::Utf8(c) => c,
502
            }
503
        } else {
504
44258
            c
505
        };
506

            
507
47050
        self.expect_char('\'', Error::ExpectedChar)?;
508

            
509
47050
        Ok(c)
510
66546
    }
511

            
512
408446
    pub fn comma(&mut self) -> Result<bool> {
513
408446
        self.skip_ws()?;
514

            
515
408446
        if self.consume_char(',') {
516
235587
            self.skip_ws()?;
517

            
518
235587
            Ok(true)
519
        } else {
520
172859
            Ok(false)
521
        }
522
408446
    }
523

            
524
    /// Only returns true if the char after `ident` cannot belong
525
    /// to an identifier.
526
5704639
    pub fn check_ident(&mut self, ident: &str) -> bool {
527
5704639
        self.check_str(ident) && !self.check_ident_other_char(ident.len())
528
5704639
    }
529

            
530
243021
    fn check_ident_other_char(&self, index: usize) -> bool {
531
243021
        self.src()[index..]
532
243021
            .chars()
533
243021
            .next()
534
243021
            .map_or(false, is_xid_continue)
535
243021
    }
536

            
537
    /// Check which type of struct we are currently parsing. The parsing state
538
    ///  is only changed in case of an error, to provide a better position.
539
    ///
540
    /// [`NewtypeMode::NoParensMeanUnit`] detects (tuple) structs by a leading
541
    ///  opening bracket and reports a unit struct otherwise.
542
    /// [`NewtypeMode::InsideNewtype`] skips an initial check for unit structs,
543
    ///  and means that any leading opening bracket is not considered to open
544
    ///  a (tuple) struct but to be part of the structs inner contents.
545
    ///
546
    /// [`TupleMode::ImpreciseTupleOrNewtype`] only performs a cheap, O(1),
547
    ///  single-identifier lookahead check to distinguish tuple structs from
548
    ///  non-tuple structs.
549
    /// [`TupleMode::DifferentiateNewtype`] performs an expensive, O(N), look-
550
    ///  ahead over the entire next value tree, which can span the entirety of
551
    ///  the remaining document in the worst case.
552
85694
    pub fn check_struct_type(
553
85694
        &mut self,
554
85694
        newtype: NewtypeMode,
555
85694
        tuple: TupleMode,
556
85694
    ) -> Result<StructType> {
557
85694
        fn check_struct_type_inner(
558
85694
            parser: &mut Parser,
559
85694
            newtype: NewtypeMode,
560
85694
            tuple: TupleMode,
561
85694
        ) -> Result<StructType> {
562
85694
            if matches!(newtype, NewtypeMode::NoParensMeanUnit) && !parser.consume_char('(') {
563
12236
                return Ok(StructType::Unit);
564
73458
            }
565
73458

            
566
73458
            parser.skip_ws()?;
567

            
568
            // Check for `Ident()`, which could be
569
            // - a zero-field struct or tuple (variant)
570
            // - an unwrapped newtype around a unit
571
73454
            if matches!(newtype, NewtypeMode::NoParensMeanUnit) && parser.check_char(')') {
572
834
                return Ok(StructType::EmptyTuple);
573
72620
            }
574
72620

            
575
72620
            if parser.skip_identifier().is_some() {
576
53962
                parser.skip_ws()?;
577

            
578
53962
                match parser.peek_char() {
579
                    // Definitely a struct with named fields
580
48116
                    Some(':') => return Ok(StructType::Named),
581
                    // Definitely a tuple-like struct with fields
582
                    Some(',') => {
583
4178
                        parser.skip_next_char();
584
4178
                        parser.skip_ws()?;
585
4178
                        if parser.check_char(')') {
586
                            // A one-element tuple could be a newtype
587
                            return Ok(StructType::NewtypeTuple);
588
4178
                        }
589
4178
                        // Definitely a tuple struct with more than one field
590
4178
                        return Ok(StructType::NonNewtypeTuple);
591
                    }
592
                    // Either a newtype or a tuple struct
593
1112
                    Some(')') => return Ok(StructType::NewtypeTuple),
594
                    // Something else, let's investigate further
595
556
                    Some(_) | None => (),
596
                };
597
18658
            }
598

            
599
19214
            if matches!(tuple, TupleMode::ImpreciseTupleOrNewtype) {
600
13086
                return Ok(StructType::AnyTuple);
601
6128
            }
602
6128

            
603
6128
            let mut braces = 1_usize;
604
6128
            let mut more_than_one = false;
605

            
606
            // Skip ahead to see if the value is followed by another value
607
24222
            while braces > 0 {
608
                // Skip spurious braces in comments, strings, and characters
609
18662
                parser.skip_ws()?;
610
18662
                let cursor_backup = parser.cursor;
611
18662
                if parser.char().is_err() {
612
18662
                    parser.set_cursor(cursor_backup);
613
18662
                }
614
18662
                let cursor_backup = parser.cursor;
615
18662
                match parser.string() {
616
1112
                    Ok(_) => (),
617
                    // prevent quadratic complexity backtracking for unterminated string
618
                    Err(err @ (Error::ExpectedStringEnd | Error::Eof)) => return Err(err),
619
17550
                    Err(_) => parser.set_cursor(cursor_backup),
620
                }
621
18662
                let cursor_backup = parser.cursor;
622
18662
                // we have already checked for strings, which subsume base64 byte strings
623
18662
                match parser.byte_string_no_base64() {
624
834
                    Ok(_) => (),
625
                    // prevent quadratic complexity backtracking for unterminated byte string
626
                    Err(err @ (Error::ExpectedStringEnd | Error::Eof)) => return Err(err),
627
17828
                    Err(_) => parser.set_cursor(cursor_backup),
628
                }
629

            
630
18662
                let c = parser.next_char()?;
631
18650
                if matches!(c, '(' | '[' | '{') {
632
1402
                    braces += 1;
633
17248
                } else if matches!(c, ')' | ']' | '}') {
634
6962
                    braces -= 1;
635
10298
                } else if c == ',' && braces == 1 {
636
556
                    parser.skip_ws()?;
637
556
                    more_than_one = !parser.check_char(')');
638
556
                    break;
639
9730
                }
640
            }
641

            
642
6116
            if more_than_one {
643
278
                Ok(StructType::NonNewtypeTuple)
644
            } else {
645
5838
                Ok(StructType::NewtypeTuple)
646
            }
647
85694
        }
648

            
649
        // Create a temporary working copy
650
85694
        let backup_cursor = self.cursor;
651
85694

            
652
85694
        let result = check_struct_type_inner(self, newtype, tuple);
653
85694

            
654
85694
        if result.is_ok() {
655
85678
            // Revert the parser to before the struct type check
656
85678
            self.set_cursor(backup_cursor);
657
85678
        }
658

            
659
85694
        result
660
85694
    }
661

            
662
    /// Only returns true if the char after `ident` cannot belong
663
    /// to an identifier.
664
5108995
    pub fn consume_ident(&mut self, ident: &str) -> bool {
665
5108995
        if self.check_ident(ident) {
666
112846
            self.advance_bytes(ident.len());
667
112846

            
668
112846
            true
669
        } else {
670
4996149
            false
671
        }
672
5108995
    }
673

            
674
80849
    pub fn consume_struct_name(&mut self, ident: &'static str) -> Result<bool> {
675
80849
        if self.check_ident("") {
676
62329
            if self.exts.contains(Extensions::EXPLICIT_STRUCT_NAMES) {
677
834
                return Err(Error::ExpectedStructName(ident.to_string()));
678
61495
            }
679
61495

            
680
61495
            return Ok(false);
681
18520
        }
682

            
683
18520
        let found_ident = match self.identifier() {
684
16574
            Ok(maybe_ident) => maybe_ident,
685
1390
            Err(Error::SuggestRawIdentifier(found_ident)) if found_ident == ident => {
686
278
                return Err(Error::SuggestRawIdentifier(found_ident))
687
            }
688
1668
            Err(_) => return Err(Error::ExpectedNamedStructLike(ident)),
689
        };
690

            
691
16574
        if ident.is_empty() {
692
302
            return Err(Error::ExpectedNamedStructLike(ident));
693
16272
        }
694
16272

            
695
16272
        if found_ident != ident {
696
1680
            return Err(Error::ExpectedDifferentStructName {
697
1680
                expected: ident,
698
1680
                found: String::from(found_ident),
699
1680
            });
700
14592
        }
701
14592

            
702
14592
        Ok(true)
703
80849
    }
704

            
705
    /// Returns the extensions bit mask.
706
573459
    fn extensions(&mut self) -> Result<Extensions> {
707
573459
        if !self.check_char('#') {
708
539443
            return Ok(Extensions::empty());
709
34016
        }
710
34016

            
711
34016
        if !self.consume_all(&["#", "!", "[", "enable", "("])? {
712
12
            return Err(Error::ExpectedAttribute);
713
34004
        }
714
34004

            
715
34004
        self.skip_ws()?;
716
34004
        let mut extensions = Extensions::empty();
717

            
718
        loop {
719
34282
            let ident = self.identifier()?;
720
34282
            let extension = Extensions::from_ident(ident)
721
34282
                .ok_or_else(|| Error::NoSuchExtension(ident.into()))?;
722

            
723
34270
            extensions |= extension;
724

            
725
34270
            let comma = self.comma()?;
726

            
727
            // If we have no comma but another item, return an error
728
34270
            if !comma && self.check_ident_other_char(0) {
729
278
                return Err(Error::ExpectedComma);
730
33992
            }
731
33992

            
732
33992
            // If there's no comma, assume the list ended.
733
33992
            // If there is, it might be a trailing one, thus we only
734
33992
            // continue the loop if we get an ident char.
735
33992
            if !comma || !self.check_ident_other_char(0) {
736
33714
                break;
737
278
            }
738
        }
739

            
740
33714
        self.skip_ws()?;
741

            
742
33714
        if self.consume_all(&[")", "]"])? {
743
33702
            Ok(extensions)
744
        } else {
745
12
            Err(Error::ExpectedAttributeEnd)
746
        }
747
573459
    }
748

            
749
15146
    pub fn float<T: Float>(&mut self) -> Result<T> {
750
        const F32_SUFFIX: &str = "f32";
751
        const F64_SUFFIX: &str = "f64";
752

            
753
102818
        for (literal, value_f32, value_f64) in &[
754
102818
            ("inf", f32::INFINITY, f64::INFINITY),
755
102818
            ("+inf", f32::INFINITY, f64::INFINITY),
756
102818
            ("-inf", f32::NEG_INFINITY, f64::NEG_INFINITY),
757
102818
            ("NaN", f32::NAN, f64::NAN),
758
102818
            ("+NaN", f32::NAN, f64::NAN),
759
102818
            ("-NaN", -f32::NAN, -f64::NAN),
760
102818
        ] {
761
88904
            if self.consume_ident(literal) {
762
88
                return T::parse(literal);
763
88816
            }
764

            
765
88816
            if let Some(suffix) = self.src().strip_prefix(literal) {
766
1156
                if let Some(post_suffix) = suffix.strip_prefix(F32_SUFFIX) {
767
576
                    if !post_suffix.chars().next().map_or(false, is_xid_continue) {
768
572
                        let float_ron = &self.src()[..literal.len() + F32_SUFFIX.len()];
769
572
                        self.advance_bytes(literal.len() + F32_SUFFIX.len());
770
572
                        return T::try_from_parsed_float(ParsedFloat::F32(*value_f32), float_ron);
771
4
                    }
772
580
                }
773

            
774
584
                if let Some(post_suffix) = suffix.strip_prefix(F64_SUFFIX) {
775
576
                    if !post_suffix.chars().next().map_or(false, is_xid_continue) {
776
572
                        let float_ron = &self.src()[..literal.len() + F64_SUFFIX.len()];
777
572
                        self.advance_bytes(literal.len() + F64_SUFFIX.len());
778
572
                        return T::try_from_parsed_float(ParsedFloat::F64(*value_f64), float_ron);
779
4
                    }
780
8
                }
781
87660
            }
782
        }
783

            
784
13914
        let num_bytes = self.next_chars_while_len(is_float_char);
785
13914

            
786
13914
        if num_bytes == 0 {
787
40
            return Err(Error::ExpectedFloat);
788
13874
        }
789
13874

            
790
13874
        if self.check_char('_') {
791
4
            return Err(Error::UnderscoreAtBeginning);
792
13870
        }
793
13870

            
794
13870
        let mut f = String::with_capacity(num_bytes);
795
13870
        let mut allow_underscore = false;
796

            
797
251448
        for (i, c) in self.src()[..num_bytes].char_indices() {
798
768
            match c {
799
760
                '_' if allow_underscore => continue,
800
                '_' => {
801
8
                    self.advance_bytes(i);
802
8
                    return Err(Error::FloatUnderscore);
803
                }
804
237105
                '0'..='9' | 'e' | 'E' => allow_underscore = true,
805
11366
                '.' => allow_underscore = false,
806
2209
                _ => (),
807
            }
808

            
809
            // we know that the byte is an ASCII character here
810
250680
            f.push(c);
811
        }
812

            
813
13862
        if self.src()[num_bytes..].starts_with('f') {
814
1982
            let backup_cursor = self.cursor;
815
1982
            self.advance_bytes(num_bytes);
816

            
817
            #[allow(clippy::never_loop)]
818
            loop {
819
1982
                let res = if self.consume_ident(F32_SUFFIX) {
820
1128
                    f32::from_str(&f).map(ParsedFloat::F32)
821
854
                } else if self.consume_ident(F64_SUFFIX) {
822
572
                    f64::from_str(&f).map(ParsedFloat::F64)
823
                } else {
824
282
                    break;
825
                };
826

            
827
1700
                let parsed = if let Ok(parsed) = res {
828
1692
                    parsed
829
                } else {
830
8
                    self.set_cursor(backup_cursor);
831
8
                    return Err(Error::ExpectedFloat);
832
                };
833

            
834
1692
                let float_ron = &self.src[backup_cursor.cursor..self.cursor.cursor];
835
1692

            
836
1692
                return T::try_from_parsed_float(parsed, float_ron);
837
            }
838

            
839
282
            self.set_cursor(backup_cursor);
840
11880
        }
841

            
842
12162
        let value = T::parse(&f)?;
843

            
844
12142
        self.advance_bytes(num_bytes);
845
12142

            
846
12142
        Ok(value)
847
15146
    }
848

            
849
598201
    pub fn skip_identifier(&mut self) -> Option<&'a str> {
850
598201
        #[allow(clippy::nonminimal_bool)]
851
598201
        if self.check_str("b\"") // byte string
852
596803
            || self.check_str("b'") // byte literal
853
382465
            || self.check_str("br#") // raw byte string
854
381631
            || self.check_str("br\"") // raw byte string
855
380797
            || self.check_str("r\"") // raw string
856
380241
            || self.check_str("r#\"") // raw string
857
379963
            || self.check_str("r##") // raw string
858
379685
            || false
859
        {
860
218516
            return None;
861
379685
        }
862
379685

            
863
379685
        if self.check_str("r#") {
864
            // maybe a raw identifier
865
12
            let len = self.next_chars_while_from_len(2, is_ident_raw_char);
866
12
            if len > 0 {
867
4
                let ident = &self.src()[2..2 + len];
868
4
                self.advance_bytes(2 + len);
869
4
                return Some(ident);
870
8
            }
871
8
            return None;
872
379673
        }
873

            
874
379673
        if let Some(c) = self.peek_char() {
875
            // maybe a normal identifier
876
378283
            if is_ident_first_char(c) {
877
113752
                let len =
878
113752
                    c.len_utf8() + self.next_chars_while_from_len(c.len_utf8(), is_xid_continue);
879
113752
                let ident = &self.src()[..len];
880
113752
                self.advance_bytes(len);
881
113752
                return Some(ident);
882
264531
            }
883
1390
        }
884

            
885
265921
        None
886
598201
    }
887

            
888
303622
    pub fn identifier(&mut self) -> Result<&'a str> {
889
303622
        let first = self.peek_char_or_eof()?;
890
303622
        if !is_ident_first_char(first) {
891
2236
            if is_ident_raw_char(first) {
892
1112
                let ident_bytes = self.next_chars_while_len(is_ident_raw_char);
893
1112
                return Err(Error::SuggestRawIdentifier(
894
1112
                    self.src()[..ident_bytes].into(),
895
1112
                ));
896
1124
            }
897
1124

            
898
1124
            return Err(Error::ExpectedIdentifier);
899
301386
        }
900
301386

            
901
301386
        // If the next 2-3 bytes signify the start of a (raw) (byte) string
902
301386
        //  literal, return an error.
903
301386
        #[allow(clippy::nonminimal_bool)]
904
301386
        if self.check_str("b\"") // byte string
905
301108
            || self.check_str("b'") // byte literal
906
300830
            || self.check_str("br#") // raw byte string
907
300552
            || self.check_str("br\"") // raw byte string
908
300274
            || self.check_str("r\"") // raw string
909
299996
            || self.check_str("r#\"") // raw string
910
299718
            || self.check_str("r##") // raw string
911
299440
            || false
912
        {
913
1946
            return Err(Error::ExpectedIdentifier);
914
299440
        }
915

            
916
299440
        let length = if self.check_str("r#") {
917
7256
            let cursor_backup = self.cursor;
918
7256

            
919
7256
            self.advance_bytes(2);
920

            
921
            // Note: it's important to check this before advancing forward, so that
922
            // the value-type deserializer can fall back to parsing it differently.
923
7256
            if !matches!(self.peek_char(), Some(c) if is_ident_raw_char(c)) {
924
556
                self.set_cursor(cursor_backup);
925
556
                return Err(Error::ExpectedIdentifier);
926
6700
            }
927
6700

            
928
6700
            self.next_chars_while_len(is_ident_raw_char)
929
292184
        } else if first == 'r' {
930
556
            let std_ident_length = self.next_chars_while_len(is_xid_continue);
931
556
            let raw_ident_length = self.next_chars_while_len(is_ident_raw_char);
932
556

            
933
556
            if raw_ident_length > std_ident_length {
934
278
                return Err(Error::SuggestRawIdentifier(
935
278
                    self.src()[..raw_ident_length].into(),
936
278
                ));
937
278
            }
938
278

            
939
278
            std_ident_length
940
        } else {
941
291628
            let std_ident_length = first.len_utf8()
942
291628
                + self.next_chars_while_from_len(first.len_utf8(), is_xid_continue);
943
291628
            let raw_ident_length = self.next_chars_while_len(is_ident_raw_char);
944
291628

            
945
291628
            if raw_ident_length > std_ident_length {
946
834
                return Err(Error::SuggestRawIdentifier(
947
834
                    self.src()[..raw_ident_length].into(),
948
834
                ));
949
290794
            }
950
290794

            
951
290794
            std_ident_length
952
        };
953

            
954
297772
        let ident = &self.src()[..length];
955
297772
        self.advance_bytes(length);
956
297772

            
957
297772
        Ok(ident)
958
303622
    }
959

            
960
307557
    pub fn next_bytes_is_float(&mut self) -> bool {
961
307557
        if let Some(c) = self.peek_char() {
962
307553
            let skip = match c {
963
5086
                '+' | '-' => 1,
964
302467
                _ => 0,
965
            };
966
307553
            let valid_float_len = self.next_chars_while_from_len(skip, is_float_char);
967
307553
            let valid_int_len = self.next_chars_while_from_len(skip, is_int_char);
968
307553
            valid_float_len > valid_int_len
969
        } else {
970
4
            false
971
        }
972
307557
    }
973

            
974
3630575
    pub fn skip_ws(&mut self) -> Result<()> {
975
3630575
        if (self.cursor.last_ws_len != WS_CURSOR_UNCLOSED_LINE)
976
3630015
            && ((self.cursor.pre_ws_cursor + self.cursor.last_ws_len) < self.cursor.cursor)
977
2350744
        {
978
2350744
            // the last whitespace is disjoint from this one, we need to track a new one
979
2350744
            self.cursor.pre_ws_cursor = self.cursor.cursor;
980
2350744
        }
981

            
982
3630575
        if self.src().is_empty() {
983
431360
            return Ok(());
984
3199215
        }
985

            
986
        loop {
987
3227095
            self.advance_bytes(self.next_chars_while_len(is_whitespace_char));
988
3227095

            
989
3227095
            match self.skip_comment()? {
990
3197261
                None => break,
991
                Some(Comment::UnclosedLine) => {
992
1116
                    self.cursor.last_ws_len = WS_CURSOR_UNCLOSED_LINE;
993
1116
                    return Ok(());
994
                }
995
27880
                Some(Comment::ClosedLine | Comment::Block) => continue,
996
            }
997
        }
998

            
999
3197261
        self.cursor.last_ws_len = self.cursor.cursor - self.cursor.pre_ws_cursor;
3197261

            
3197261
        Ok(())
3630575
    }
17792
    pub fn has_unclosed_line_comment(&self) -> bool {
17792
        self.src().is_empty() && self.cursor.last_ws_len == WS_CURSOR_UNCLOSED_LINE
17792
    }
10608
    pub fn byte_string(&mut self) -> Result<ParsedByteStr<'a>> {
8
        fn expected_byte_string_found_base64(
8
            base64_str: &ParsedStr,
8
            byte_str: &ParsedByteStr,
8
        ) -> Error {
8
            let byte_str = match &byte_str {
8
                ParsedByteStr::Allocated(b) => b.as_slice(),
                ParsedByteStr::Slice(b) => b,
            }
8
            .iter()
80
            .flat_map(|c| core::ascii::escape_default(*c))
8
            .map(char::from)
8
            .collect::<String>();
8
            let base64_str = match &base64_str {
                ParsedStr::Allocated(s) => s.as_str(),
8
                ParsedStr::Slice(s) => s,
            };
8
            Error::InvalidValueForType {
8
                expected: format!("the Rusty byte string b\"{}\"", byte_str),
8
                found: format!("the ambiguous base64 string {:?}", base64_str),
8
            }
8
        }
10608
        if self.consume_char('"') {
1676
            let base64_str = self.escaped_string()?;
1676
            let base64_result = ParsedByteStr::try_from_base64(&base64_str);
1676

            
1676
            if cfg!(not(test)) {
                // FIXME @juntyr: remove in v0.10
                #[allow(deprecated)]
1668
                base64_result.map_err(Error::Base64Error)
            } else {
8
                match base64_result {
                    // FIXME @juntyr: enable in v0.10
4
                    Ok(byte_str) => Err(expected_byte_string_found_base64(&base64_str, &byte_str)),
4
                    Err(_) => Err(Error::ExpectedByteString),
                }
            }
8932
        } else if self.consume_char('r') {
286
            let base64_str = self.raw_string()?;
286
            let base64_result = ParsedByteStr::try_from_base64(&base64_str);
286

            
286
            if cfg!(not(test)) {
                // FIXME @juntyr: remove in v0.10
                #[allow(deprecated)]
278
                base64_result.map_err(Error::Base64Error)
            } else {
8
                match base64_result {
                    // FIXME @juntyr: enable in v0.10
4
                    Ok(byte_str) => Err(expected_byte_string_found_base64(&base64_str, &byte_str)),
4
                    Err(_) => Err(Error::ExpectedByteString),
                }
            }
        } else {
8646
            self.byte_string_no_base64()
        }
10608
    }
27308
    pub fn byte_string_no_base64(&mut self) -> Result<ParsedByteStr<'a>> {
27308
        if self.consume_str("b\"") {
5866
            self.escaped_byte_string()
21442
        } else if self.consume_str("br") {
3614
            self.raw_byte_string()
        } else {
17828
            Err(Error::ExpectedByteString)
        }
27308
    }
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
    }
106625
    pub fn string(&mut self) -> Result<ParsedStr<'a>> {
106625
        if self.consume_char('"') {
84285
            self.escaped_string()
22340
        } else if self.consume_char('r') {
3110
            self.raw_string()
        } else {
19230
            Err(Error::ExpectedString)
        }
106625
    }
85961
    fn escaped_string(&mut self) -> Result<ParsedStr<'a>> {
85961
        match self.escaped_byte_buf(EscapeEncoding::Utf8) {
84003
            Ok((bytes, advance)) => {
84003
                let string = ParsedStr::try_from_bytes(bytes).map_err(Error::from)?;
84003
                self.advance_bytes(advance);
84003
                Ok(string)
            }
1958
            Err(err) => Err(err),
        }
85961
    }
3396
    fn raw_string(&mut self) -> Result<ParsedStr<'a>> {
3396
        match self.raw_byte_buf() {
2836
            Ok((bytes, advance)) => {
2836
                let string = ParsedStr::try_from_bytes(bytes).map_err(Error::from)?;
2836
                self.advance_bytes(advance);
2836
                Ok(string)
            }
560
            Err(err) => Err(err),
        }
3396
    }
91827
    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
91827
        let str_end = self.src().find('"').ok_or(Error::ExpectedStringEnd)?;
91259
        let escape = self.src()[..str_end].find('\\');
91259
        if let Some(escape) = escape {
            // Now check if escaping is used inside the string
10882
            let mut i = escape;
10882
            let mut s = self.src().as_bytes()[..i].to_vec();
            loop {
24028
                self.advance_bytes(i + 1);
24028

            
24028
                match self.parse_escape(encoding, false)? {
10398
                    EscapeCharacter::Ascii(c) => s.push(c),
11684
                    EscapeCharacter::Utf8(c) => match c.len_utf8() {
11120
                        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
22082
                let new_str_end = self.src().find('"').ok_or(Error::ExpectedStringEnd)?;
22082
                let new_escape = self.src()[..new_str_end].find('\\');
22082
                if let Some(new_escape) = new_escape {
13146
                    s.extend_from_slice(&self.src().as_bytes()[..new_escape]);
13146
                    i = new_escape;
13146
                } else {
8936
                    s.extend_from_slice(&self.src().as_bytes()[..new_str_end]);
8936
                    // Advance to the end of the string + 1 for the `"`.
8936
                    break Ok((ParsedByteStr::Allocated(s), new_str_end + 1));
                }
            }
        } else {
80377
            let s = &self.src().as_bytes()[..str_end];
80377

            
80377
            // Advance by the number of bytes of the string + 1 for the `"`.
80377
            Ok((ParsedByteStr::Slice(s), str_end + 1))
        }
91827
    }
7010
    fn raw_byte_buf(&mut self) -> Result<(ParsedByteStr<'a>, usize)> {
13734
        let num_hashes = self.next_chars_while_len(|c| c == '#');
7010
        let hashes = &self.src()[..num_hashes];
7010
        self.advance_bytes(num_hashes);
7010

            
7010
        self.expect_char('"', Error::ExpectedString)?;
6450
        let ending = ["\"", hashes].concat();
6450
        let i = self.src().find(&ending).ok_or(Error::ExpectedStringEnd)?;
5894
        let s = &self.src().as_bytes()[..i];
5894

            
5894
        // Advance by the number of bytes of the byte string
5894
        // + `num_hashes` + 1 for the `"`.
5894
        Ok((ParsedByteStr::Slice(s), i + num_hashes + 1))
7010
    }
196948
    fn decode_ascii_escape(&mut self) -> Result<u8> {
196948
        let mut n = 0;
590288
        for _ in 0..2 {
393896
            n <<= 4;
393896
            let byte = self.next_char()?;
393896
            let decoded = Self::decode_hex(byte)?;
393340
            n |= decoded;
        }
196392
        Ok(n)
196948
    }
    #[inline]
933067
    fn decode_hex(c: char) -> Result<u8> {
933067
        if !c.is_ascii() {
278
            return Err(Error::InvalidEscape("Non-hex digit found"));
932789
        }
932789

            
932789
        // c is an ASCII character that can be losslessly cast to u8
932789
        match c as u8 {
932511
            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")),
        }
933067
    }
216416
    fn parse_escape(&mut self, encoding: EscapeEncoding, is_char: bool) -> Result<EscapeCharacter> {
216416
        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
193862
                let b: u8 = self.decode_ascii_escape()?;
193306
                if let EscapeEncoding::Binary = encoding {
189684
                    return Ok(EscapeCharacter::Ascii(b));
3622
                }
3622

            
3622
                // Fast exit for ascii character in UTF-8 string
3622
                let mut bytes = [b, 0, 0, 0];
3622
                if let Ok(Some(c)) = from_utf8(&bytes[..=0]).map(|s| s.chars().next()) {
1668
                    return Ok(EscapeCharacter::Utf8(c));
1954
                }
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
4472
                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

            
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

            
11120
                self.expect_char(
11120
                    '}',
11120
                    Error::InvalidEscape("No } at the end of Unicode escape"),
11120
                )?;
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)
216416
    }
3227095
    fn skip_comment(&mut self) -> Result<Option<Comment>> {
3227095
        if self.consume_char('/') {
29834
            match self.next_char()? {
                '/' => {
118476
                    let bytes = self.next_chars_while_len(|c| c != '\n');
8138

            
8138
                    self.advance_bytes(bytes);
8138

            
8138
                    if self.src().is_empty() {
1116
                        Ok(Some(Comment::UnclosedLine))
                    } else {
7022
                        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

            
28646
                        if self.src().is_empty() {
282
                            return Err(Error::UnclosedBlockComment);
28364
                        }
28364

            
28364
                        self.advance_bytes(bytes);
28364

            
28364
                        // 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 {
3197261
            Ok(None)
        }
3227095
    }
}
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 {
1255912
            fn from_u8(x: u8) -> Self {
1255912
                x as $ty
1255912
            }
521109
            fn checked_mul_ext(&mut self, x: u8) -> bool {
521109
                match self.checked_mul(Self::from_u8(x)) {
520267
                    Some(n) => {
520267
                        *self = n;
520267
                        false
                    }
842
                    None => true,
                }
521109
            }
436277
            fn checked_add_ext(&mut self, x: u8) -> bool {
436277
                match self.checked_add(Self::from_u8(x)) {
435860
                    Some(n) => {
435860
                        *self = n;
435860
                        false
                    }
417
                    None => true,
                }
436277
            }
82044
            fn checked_sub_ext(&mut self, x: u8) -> bool {
82044
                match self.checked_sub(Self::from_u8(x)) {
79264
                    Some(n) => {
79264
                        *self = n;
79264
                        false
                    }
2780
                    None => true,
                }
82044
            }
        }
    };
    ($($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 {
111004
            fn parse(parser: &mut Parser, sign: i8) -> Result<Self> {
111004
                parser.parse_integer(sign)
111004
            }
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!(
20850
                            "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 {
76046
    fn parse(parser: &mut Parser, sign: i8) -> Result<Self> {
76046
        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))
                }
            };
74050
        }
74050
        let unsigned = parser.parse_integer::<LargeUInt>(1)?;
73335
        if let Ok(x) = u8::try_from(unsigned) {
72761
            Ok(ParsedInteger::U8(x))
574
        } else if let Ok(x) = u16::try_from(unsigned) {
8
            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))
            }
        }
76046
    }
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 {
32246
            fn parse(float: &str) -> Result<Self> {
32246
                <$ty>::from_str(float).map_err(|_| Error::ExpectedFloat)
32246
            }
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> {
86839
    pub fn try_from_bytes(bytes: ParsedByteStr<'a>) -> Result<Self, Utf8Error> {
86839
        match bytes {
6136
            ParsedByteStr::Allocated(byte_buf) => Ok(ParsedStr::Allocated(
6136
                String::from_utf8(byte_buf).map_err(|e| e.utf8_error())?,
            )),
80703
            ParsedByteStr::Slice(bytes) => Ok(ParsedStr::Slice(from_utf8(bytes)?)),
        }
86839
    }
}
impl<'a> ParsedByteStr<'a> {
1962
    pub fn try_from_base64(str: &ParsedStr<'a>) -> Result<Self, base64::DecodeError> {
1962
        let base64_str = match str {
278
            ParsedStr::Allocated(string) => string.as_str(),
1684
            ParsedStr::Slice(str) => str,
        };
1962
        base64::engine::Engine::decode(&base64::engine::general_purpose::STANDARD, base64_str)
1962
            .map(ParsedByteStr::Allocated)
1962
    }
}
#[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

            
4
        assert_eq!(bytes.src(), "42       /*bye*/ 24  ");
4
        assert_eq!(bytes.pre_ws_src(), SOURCE);
4
        bytes.skip_ws().unwrap();
4

            
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

            
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
        );
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
        );
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
        );
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
        );
4
    }
    #[test]
4
    fn empty_src_is_not_a_float() {
4
        assert!(!Parser::new("").unwrap().next_bytes_is_float());
4
    }
    #[test]
4
    fn v0_10_base64_deprecation_error() {
4
        let err = crate::from_str::<bytes::Bytes>("\"SGVsbG8gcm9uIQ==\"").unwrap_err();
4

            
4
        assert_eq!(
4
            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
                position: Position { line: 1, col: 19 },
4
            }
4
        );
4
        let err = crate::from_str::<bytes::Bytes>("r\"SGVsbG8gcm9uIQ==\"").unwrap_err();
4

            
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::ExpectedByteString,
4
                position: 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::ExpectedByteString,
4
                position: Position { line: 1, col: 12 },
4
            }
4
        );
4
    }
}