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

            
3
use std::{
4
    char::from_u32 as char_from_u32,
5
    str::{self, from_utf8, FromStr, Utf8Error},
6
};
7

            
8
use unicode_ident::{is_xid_continue, is_xid_start};
9

            
10
use crate::{
11
    error::{Error, Position, Result, SpannedError, SpannedResult},
12
    extensions::Extensions,
13
    value::Number,
14
};
15

            
16
2422443
const fn is_int_char(c: char) -> bool {
17
2422443
    c.is_ascii_hexdigit() || c == '_'
18
2422443
}
19

            
20
1336520
const fn is_float_char(c: char) -> bool {
21
1336520
    c.is_ascii_digit() || matches!(c, 'e' | 'E' | '.' | '+' | '-' | '_')
22
1336520
}
23

            
24
858731
pub fn is_ident_first_char(c: char) -> bool {
25
858731
    c == '_' || is_xid_start(c)
26
858731
}
27

            
28
4178122
pub fn is_ident_raw_char(c: char) -> bool {
29
4178122
    matches!(c, '.' | '+' | '-') | is_xid_continue(c)
30
4178122
}
31

            
32
5425743
pub const fn is_whitespace_char(c: char) -> bool {
33
3204265
    matches!(
34
5425743
        c,
35
        ' ' | '\t'
36
            | '\n'
37
            | '\r'
38
            | '\x0B'
39
            | '\x0C'
40
            | '\u{85}'
41
            | '\u{200E}'
42
            | '\u{200F}'
43
            | '\u{2028}'
44
            | '\u{2029}'
45
    )
46
5425743
}
47

            
48
#[cfg(feature = "integer128")]
49
pub(crate) type LargeUInt = u128;
50
#[cfg(not(feature = "integer128"))]
51
pub(crate) type LargeUInt = u64;
52
#[cfg(feature = "integer128")]
53
pub(crate) type LargeSInt = i128;
54
#[cfg(not(feature = "integer128"))]
55
pub(crate) type LargeSInt = i64;
56

            
57
pub struct Parser<'a> {
58
    /// Bits set according to the [`Extensions`] enum.
59
    pub exts: Extensions,
60
    src: &'a str,
61
    cursor: ParserCursor,
62
}
63

            
64
#[derive(Copy, Clone)] // GRCOV_EXCL_LINE
65
pub struct ParserCursor {
66
    cursor: usize,
67
    pre_ws_cursor: usize,
68
    last_ws_len: usize,
69
}
70

            
71
const WS_CURSOR_UNCLOSED_LINE: usize = usize::MAX;
72

            
73
impl PartialEq for ParserCursor {
74
8
    fn eq(&self, other: &Self) -> bool {
75
8
        self.cursor == other.cursor
76
8
    }
77
}
78

            
79
impl PartialOrd for ParserCursor {
80
3388
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
81
3388
        self.cursor.partial_cmp(&other.cursor)
82
3388
    }
83
}
84

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

            
98
540035
        parser.skip_ws().map_err(|e| parser.span_error(e))?;
99

            
100
        // Loop over all extensions attributes
101
        loop {
102
573459
            let attribute = parser.extensions().map_err(|e| parser.span_error(e))?;
103

            
104
573145
            if attribute.is_empty() {
105
539443
                break;
106
33702
            }
107
33702

            
108
33702
            parser.exts |= attribute;
109
33702
            parser.skip_ws().map_err(|e| parser.span_error(e))?;
110
        }
111

            
112
539443
        Ok(parser)
113
540035
    }
114

            
115
184123
    fn set_cursor(&mut self, cursor: ParserCursor) {
116
184123
        self.cursor = cursor;
117
184123
    }
118

            
119
106711
    pub fn span_error(&self, code: Error) -> SpannedError {
120
106711
        SpannedError {
121
106711
            code,
122
106711
            position: Position::from_src_end(&self.src[..self.cursor.cursor]),
123
106711
        }
124
106711
    }
125

            
126
6255207
    pub fn advance_bytes(&mut self, bytes: usize) {
127
6255207
        self.cursor.cursor += bytes;
128
6255207
    }
129

            
130
963120
    pub fn next_char(&mut self) -> Result<char> {
131
963120
        let c = self.peek_char_or_eof()?;
132
962830
        self.cursor.cursor += c.len_utf8();
133
962830
        Ok(c)
134
963120
    }
135

            
136
37364
    pub fn skip_next_char(&mut self) {
137
37364
        std::mem::drop(self.next_char());
138
37364
    }
139

            
140
2950197
    pub fn peek_char(&self) -> Option<char> {
141
2950197
        self.src().chars().next()
142
2950197
    }
143

            
144
2201749
    pub fn peek_char_or_eof(&self) -> Result<char> {
145
2201749
        self.peek_char().ok_or(Error::Eof)
146
2201749
    }
147

            
148
6462783
    pub fn check_char(&self, c: char) -> bool {
149
6462783
        self.src().starts_with(c)
150
6462783
    }
151

            
152
13320812
    pub fn check_str(&self, s: &str) -> bool {
153
13320812
        self.src().starts_with(s)
154
13320812
    }
155

            
156
39798749
    pub fn src(&self) -> &'a str {
157
39798749
        &self.src[self.cursor.cursor..]
158
39798749
    }
159

            
160
25894
    pub fn pre_ws_src(&self) -> &'a str {
161
25894
        &self.src[self.cursor.pre_ws_cursor..]
162
25894
    }
163

            
164
1733083
    pub fn consume_str(&mut self, s: &str) -> bool {
165
1733083
        if self.check_str(s) {
166
513868
            self.advance_bytes(s.len());
167
513868

            
168
513868
            true
169
        } else {
170
1219215
            false
171
        }
172
1733083
    }
173

            
174
4982731
    pub fn consume_char(&mut self, c: char) -> bool {
175
4982731
        if self.check_char(c) {
176
1517123
            self.advance_bytes(c.len_utf8());
177
1517123

            
178
1517123
            true
179
        } else {
180
3465608
            false
181
        }
182
4982731
    }
183

            
184
67730
    fn consume_all(&mut self, all: &[&str]) -> Result<bool> {
185
67730
        all.iter()
186
237508
            .map(|elem| {
187
237508
                if self.consume_str(elem) {
188
237448
                    self.skip_ws()?;
189

            
190
237448
                    Ok(true)
191
                } else {
192
60
                    Ok(false)
193
                }
194
237508
            })
195
237508
            .try_fold(true, |acc, x| x.map(|x| x && acc))
196
67730
    }
197

            
198
143124
    pub fn expect_char(&mut self, expected: char, error: Error) -> Result<()> {
199
143124
        if self.consume_char(expected) {
200
123902
            Ok(())
201
        } else {
202
19222
            Err(error)
203
        }
204
143124
    }
205

            
206
    #[must_use]
207
4064623
    pub fn next_chars_while_len(&self, condition: fn(char) -> bool) -> usize {
208
4064623
        self.next_chars_while_from_len(0, condition)
209
4064623
    }
210

            
211
    #[must_use]
212
5085121
    pub fn next_chars_while_from_len(&self, from: usize, condition: fn(char) -> bool) -> usize {
213
5085121
        self.src()[from..]
214
13424735
            .find(|c| !condition(c))
215
5085121
            .unwrap_or(self.src().len() - from)
216
5085121
    }
217
}
218

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

            
229
527647
        for (i, c) in s.char_indices() {
230
527647
            if c == '_' {
231
6538
                continue;
232
521109
            }
233
521109

            
234
521109
            if num_acc.checked_mul_ext(base) {
235
842
                self.advance_bytes(s.len());
236
842
                return Err(Error::IntegerOutOfBounds);
237
520267
            }
238

            
239
520267
            let digit = Self::decode_hex(c)?;
240

            
241
520267
            if digit >= base {
242
1946
                self.advance_bytes(i);
243
1946
                return Err(Error::InvalidIntegerDigit { digit: c, base });
244
518321
            }
245
518321

            
246
518321
            if f(&mut num_acc, digit) {
247
3197
                self.advance_bytes(s.len());
248
3197
                return Err(Error::IntegerOutOfBounds);
249
515124
            }
250
        }
251

            
252
210497
        self.advance_bytes(s.len());
253
210497

            
254
210497
        Ok(num_acc)
255
216482
    }
256

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

            
265
223468
        let num_bytes = self.next_chars_while_len(is_int_char);
266
223468

            
267
223468
        if num_bytes == 0 {
268
4484
            return Err(Error::ExpectedInteger);
269
218984
        }
270
218984

            
271
218984
        if self.check_char('_') {
272
2502
            return Err(Error::UnderscoreAtBeginning);
273
216482
        }
274
216482

            
275
216482
        let s = &self.src()[..num_bytes];
276
216482

            
277
216482
        if sign > 0 {
278
203638
            self.parse_integer_digits(s, base, T::checked_add_ext)
279
        } else {
280
12844
            self.parse_integer_digits(s, base, T::checked_sub_ext)
281
        }
282
223468
    }
283

            
284
    #[allow(clippy::too_many_lines)]
285
322331
    pub fn integer<T: Integer>(&mut self) -> Result<T> {
286
322331
        let src_backup = self.src();
287

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

            
313
214620
                if !self.consume_char('\'') {
314
278
                    return Err(Error::ExpectedByteLiteral);
315
214342
                }
316
214342

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

            
319
214342
                return T::try_from_parsed_integer(ParsedInteger::U8(byte), bytes_ron);
320
            }
321
102189
            _ => false,
322
        };
323
107155
        let sign = if is_negative { -1 } else { 1 };
324

            
325
107155
        let num_bytes = self.next_chars_while_len(is_int_char);
326
107155

            
327
107155
        if self.src()[num_bytes..].starts_with(['i', 'u']) {
328
11939
            let int_cursor = self.cursor;
329
11939
            self.advance_bytes(num_bytes);
330

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

            
414
10243
                if !matches!(
415
1829
                    &res,
416
                    Err(Error::UnderscoreAtBeginning | Error::InvalidIntegerDigit { .. })
417
10243
                ) {
418
10243
                    // Advance past the number suffix
419
10243
                    self.skip_identifier();
420
10243
                }
421

            
422
10525
                let integer_ron = &src_backup[..src_backup.len() - suffix_bytes.len()];
423
10525

            
424
10525
                return res.and_then(|parsed| T::try_from_parsed_integer(parsed, integer_ron));
425
            }
426

            
427
1414
            self.set_cursor(int_cursor);
428
95216
        }
429

            
430
96630
        T::parse(self, sign)
431
322331
    }
432

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

            
441
296639
        let backup_cursor = self.cursor;
442

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

            
463
3380
        self.set_cursor(backup_cursor);
464
3380

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

            
477
28488
    pub fn bool(&mut self) -> Result<bool> {
478
28488
        if self.consume_ident("true") {
479
14786
            Ok(true)
480
13702
        } else if self.consume_ident("false") {
481
13666
            Ok(false)
482
        } else {
483
36
            Err(Error::ExpectedBoolean)
484
        }
485
28488
    }
486

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

            
490
47884
        let c = self.next_char()?;
491

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

            
502
47050
        self.expect_char('\'', Error::ExpectedChar)?;
503

            
504
47050
        Ok(c)
505
66546
    }
506

            
507
408446
    pub fn comma(&mut self) -> Result<bool> {
508
408446
        self.skip_ws()?;
509

            
510
408446
        if self.consume_char(',') {
511
235587
            self.skip_ws()?;
512

            
513
235587
            Ok(true)
514
        } else {
515
172859
            Ok(false)
516
        }
517
408446
    }
518

            
519
    /// Only returns true if the char after `ident` cannot belong
520
    /// to an identifier.
521
5704639
    pub fn check_ident(&mut self, ident: &str) -> bool {
522
5704639
        self.check_str(ident) && !self.check_ident_other_char(ident.len())
523
5704639
    }
524

            
525
243021
    fn check_ident_other_char(&self, index: usize) -> bool {
526
243021
        self.src()[index..]
527
243021
            .chars()
528
243021
            .next()
529
243021
            .map_or(false, is_xid_continue)
530
243021
    }
531

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

            
561
73458
            parser.skip_ws()?;
562

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

            
570
72620
            if parser.skip_identifier().is_some() {
571
53962
                parser.skip_ws()?;
572

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

            
594
19214
            if matches!(tuple, TupleMode::ImpreciseTupleOrNewtype) {
595
13086
                return Ok(StructType::AnyTuple);
596
6128
            }
597
6128

            
598
6128
            let mut braces = 1_usize;
599
6128
            let mut more_than_one = false;
600

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

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

            
637
6116
            if more_than_one {
638
278
                Ok(StructType::NonNewtypeTuple)
639
            } else {
640
5838
                Ok(StructType::NewtypeTuple)
641
            }
642
85694
        }
643

            
644
        // Create a temporary working copy
645
85694
        let backup_cursor = self.cursor;
646
85694

            
647
85694
        let result = check_struct_type_inner(self, newtype, tuple);
648
85694

            
649
85694
        if result.is_ok() {
650
85678
            // Revert the parser to before the struct type check
651
85678
            self.set_cursor(backup_cursor);
652
85678
        }
653

            
654
85694
        result
655
85694
    }
656

            
657
    /// Only returns true if the char after `ident` cannot belong
658
    /// to an identifier.
659
5108995
    pub fn consume_ident(&mut self, ident: &str) -> bool {
660
5108995
        if self.check_ident(ident) {
661
112846
            self.advance_bytes(ident.len());
662
112846

            
663
112846
            true
664
        } else {
665
4996149
            false
666
        }
667
5108995
    }
668

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

            
675
61495
            return Ok(false);
676
18520
        }
677

            
678
18520
        let found_ident = match self.identifier() {
679
16574
            Ok(maybe_ident) => maybe_ident,
680
1390
            Err(Error::SuggestRawIdentifier(found_ident)) if found_ident == ident => {
681
278
                return Err(Error::SuggestRawIdentifier(found_ident))
682
            }
683
1668
            Err(_) => return Err(Error::ExpectedNamedStructLike(ident)),
684
        };
685

            
686
16574
        if ident.is_empty() {
687
302
            return Err(Error::ExpectedNamedStructLike(ident));
688
16272
        }
689
16272

            
690
16272
        if found_ident != ident {
691
1680
            return Err(Error::ExpectedDifferentStructName {
692
1680
                expected: ident,
693
1680
                found: String::from(found_ident),
694
1680
            });
695
14592
        }
696
14592

            
697
14592
        Ok(true)
698
80849
    }
699

            
700
    /// Returns the extensions bit mask.
701
573459
    fn extensions(&mut self) -> Result<Extensions> {
702
573459
        if !self.check_char('#') {
703
539443
            return Ok(Extensions::empty());
704
34016
        }
705
34016

            
706
34016
        if !self.consume_all(&["#", "!", "[", "enable", "("])? {
707
12
            return Err(Error::ExpectedAttribute);
708
34004
        }
709
34004

            
710
34004
        self.skip_ws()?;
711
34004
        let mut extensions = Extensions::empty();
712

            
713
        loop {
714
34282
            let ident = self.identifier()?;
715
34282
            let extension = Extensions::from_ident(ident)
716
34282
                .ok_or_else(|| Error::NoSuchExtension(ident.into()))?;
717

            
718
34270
            extensions |= extension;
719

            
720
34270
            let comma = self.comma()?;
721

            
722
            // If we have no comma but another item, return an error
723
34270
            if !comma && self.check_ident_other_char(0) {
724
278
                return Err(Error::ExpectedComma);
725
33992
            }
726
33992

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

            
735
33714
        self.skip_ws()?;
736

            
737
33714
        if self.consume_all(&[")", "]"])? {
738
33702
            Ok(extensions)
739
        } else {
740
12
            Err(Error::ExpectedAttributeEnd)
741
        }
742
573459
    }
743

            
744
15146
    pub fn float<T: Float>(&mut self) -> Result<T> {
745
        const F32_SUFFIX: &str = "f32";
746
        const F64_SUFFIX: &str = "f64";
747

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

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

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

            
779
13914
        let num_bytes = self.next_chars_while_len(is_float_char);
780
13914

            
781
13914
        if num_bytes == 0 {
782
40
            return Err(Error::ExpectedFloat);
783
13874
        }
784
13874

            
785
13874
        if self.check_char('_') {
786
4
            return Err(Error::UnderscoreAtBeginning);
787
13870
        }
788
13870

            
789
13870
        let mut f = String::with_capacity(num_bytes);
790
13870
        let mut allow_underscore = false;
791

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

            
804
            // we know that the byte is an ASCII character here
805
250680
            f.push(c);
806
        }
807

            
808
13862
        if self.src()[num_bytes..].starts_with('f') {
809
1982
            let backup_cursor = self.cursor;
810
1982
            self.advance_bytes(num_bytes);
811

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

            
822
1700
                let parsed = if let Ok(parsed) = res {
823
1692
                    parsed
824
                } else {
825
8
                    self.set_cursor(backup_cursor);
826
8
                    return Err(Error::ExpectedFloat);
827
                };
828

            
829
1692
                let float_ron = &self.src[backup_cursor.cursor..self.cursor.cursor];
830
1692

            
831
1692
                return T::try_from_parsed_float(parsed, float_ron);
832
            }
833

            
834
282
            self.set_cursor(backup_cursor);
835
11880
        }
836

            
837
12162
        let value = T::parse(&f)?;
838

            
839
12142
        self.advance_bytes(num_bytes);
840
12142

            
841
12142
        Ok(value)
842
15146
    }
843

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

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

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

            
880
265921
        None
881
598201
    }
882

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

            
893
1124
            return Err(Error::ExpectedIdentifier);
894
301386
        }
895
301386

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

            
911
299440
        let length = if self.check_str("r#") {
912
7256
            let cursor_backup = self.cursor;
913
7256

            
914
7256
            self.advance_bytes(2);
915

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

            
923
6700
            self.next_chars_while_len(is_ident_raw_char)
924
292184
        } else if first == 'r' {
925
556
            let std_ident_length = self.next_chars_while_len(is_xid_continue);
926
556
            let raw_ident_length = self.next_chars_while_len(is_ident_raw_char);
927
556

            
928
556
            if raw_ident_length > std_ident_length {
929
278
                return Err(Error::SuggestRawIdentifier(
930
278
                    self.src()[..raw_ident_length].into(),
931
278
                ));
932
278
            }
933
278

            
934
278
            std_ident_length
935
        } else {
936
291628
            let std_ident_length = first.len_utf8()
937
291628
                + self.next_chars_while_from_len(first.len_utf8(), is_xid_continue);
938
291628
            let raw_ident_length = self.next_chars_while_len(is_ident_raw_char);
939
291628

            
940
291628
            if raw_ident_length > std_ident_length {
941
834
                return Err(Error::SuggestRawIdentifier(
942
834
                    self.src()[..raw_ident_length].into(),
943
834
                ));
944
290794
            }
945
290794

            
946
290794
            std_ident_length
947
        };
948

            
949
297772
        let ident = &self.src()[..length];
950
297772
        self.advance_bytes(length);
951
297772

            
952
297772
        Ok(ident)
953
303622
    }
954

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

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

            
977
3630575
        if self.src().is_empty() {
978
431360
            return Ok(());
979
3199215
        }
980

            
981
        loop {
982
3227095
            self.advance_bytes(self.next_chars_while_len(is_whitespace_char));
983
3227095

            
984
3227095
            match self.skip_comment()? {
985
3197261
                None => break,
986
                Some(Comment::UnclosedLine) => {
987
1116
                    self.cursor.last_ws_len = WS_CURSOR_UNCLOSED_LINE;
988
1116
                    return Ok(());
989
                }
990
27880
                Some(Comment::ClosedLine | Comment::Block) => continue,
991
            }
992
        }
993

            
994
3197261
        self.cursor.last_ws_len = self.cursor.cursor - self.cursor.pre_ws_cursor;
995
3197261

            
996
3197261
        Ok(())
997
3630575
    }
998

            
999
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| std::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(std::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
    }
}