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
2387769
const fn is_int_char(c: char) -> bool {
17
2387769
    c.is_ascii_hexdigit() || c == '_'
18
2387769
}
19

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

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

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

            
32
5347865
pub const fn is_whitespace_char(c: char) -> bool {
33
3158251
    matches!(
34
5347865
        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
5347865
}
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
3340
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
81
3340
        self.cursor.partial_cmp(&other.cursor)
82
3340
    }
83
}
84

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

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

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

            
104
564919
            if attribute.is_empty() {
105
531701
                break;
106
33218
            }
107
33218

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

            
112
531701
        Ok(parser)
113
532285
    }
114

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

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

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

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

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

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

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

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

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

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

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

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

            
168
506484
            true
169
        } else {
170
1201701
            false
171
        }
172
1708185
    }
173

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

            
178
1495329
            true
179
        } else {
180
3415844
            false
181
        }
182
4911173
    }
183

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

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

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

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

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

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

            
229
520085
        for (i, c) in s.char_indices() {
230
520085
            if c == '_' {
231
6446
                continue;
232
513639
            }
233
513639

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

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

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

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

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

            
254
207475
        Ok(num_acc)
255
213374
    }
256

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

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

            
267
220260
        if num_bytes == 0 {
268
4420
            return Err(Error::ExpectedInteger);
269
215840
        }
270
215840

            
271
215840
        if self.check_char('_') {
272
2466
            return Err(Error::UnderscoreAtBeginning);
273
213374
        }
274
213374

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

            
277
213374
        if sign > 0 {
278
200714
            self.parse_integer_digits(s, base, T::checked_add_ext)
279
        } else {
280
12660
            self.parse_integer_digits(s, base, T::checked_sub_ext)
281
        }
282
220260
    }
283

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

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

            
313
211532
                if !self.consume_char('\'') {
314
274
                    return Err(Error::ExpectedByteLiteral);
315
211258
                }
316
211258

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

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

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

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

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

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

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

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

            
427
1394
            self.set_cursor(int_cursor);
428
93872
        }
429

            
430
95266
        T::parse(self, sign)
431
317725
    }
432

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

            
441
292373
        let backup_cursor = self.cursor;
442

            
443
292373
        let (integer_err, integer_cursor) = match self.integer::<ParsedInteger>() {
444
289041
            Ok(integer) => {
445
289041
                return match integer {
446
834
                    ParsedInteger::I8(v) => Ok(Number::I8(v)),
447
552
                    ParsedInteger::I16(v) => Ok(Number::I16(v)),
448
552
                    ParsedInteger::I32(v) => Ok(Number::I32(v)),
449
552
                    ParsedInteger::I64(v) => Ok(Number::I64(v)),
450
                    #[cfg(feature = "integer128")]
451
550
                    ParsedInteger::I128(v) => Ok(Number::I128(v)),
452
283517
                    ParsedInteger::U8(v) => Ok(Number::U8(v)),
453
556
                    ParsedInteger::U16(v) => Ok(Number::U16(v)),
454
552
                    ParsedInteger::U32(v) => Ok(Number::U32(v)),
455
552
                    ParsedInteger::U64(v) => Ok(Number::U64(v)),
456
                    #[cfg(feature = "integer128")]
457
824
                    ParsedInteger::U128(v) => Ok(Number::U128(v)),
458
                }
459
            }
460
3332
            Err(err) => (err, self.cursor),
461
3332
        };
462
3332

            
463
3332
        self.set_cursor(backup_cursor);
464
3332

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

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

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

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

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

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

            
504
46374
        Ok(c)
505
65590
    }
506

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

            
510
402582
        if self.consume_char(',') {
511
232205
            self.skip_ws()?;
512

            
513
232205
            Ok(true)
514
        } else {
515
170377
            Ok(false)
516
        }
517
402582
    }
518

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

            
525
239535
    fn check_ident_other_char(&self, index: usize) -> bool {
526
239535
        self.src()[index..]
527
239535
            .chars()
528
239535
            .next()
529
239535
            .map_or(false, is_xid_continue)
530
239535
    }
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
84462
    pub fn check_struct_type(
548
84462
        &mut self,
549
84462
        newtype: NewtypeMode,
550
84462
        tuple: TupleMode,
551
84462
    ) -> Result<StructType> {
552
84462
        fn check_struct_type_inner(
553
84462
            parser: &mut Parser,
554
84462
            newtype: NewtypeMode,
555
84462
            tuple: TupleMode,
556
84462
        ) -> Result<StructType> {
557
84462
            if matches!(newtype, NewtypeMode::NoParensMeanUnit) && !parser.consume_char('(') {
558
12060
                return Ok(StructType::Unit);
559
72402
            }
560
72402

            
561
72402
            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
72398
            if matches!(newtype, NewtypeMode::NoParensMeanUnit) && parser.check_char(')') {
567
822
                return Ok(StructType::EmptyTuple);
568
71576
            }
569
71576

            
570
71576
            if parser.skip_identifier().is_some() {
571
53186
                parser.skip_ws()?;
572

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

            
594
18938
            if matches!(tuple, TupleMode::ImpreciseTupleOrNewtype) {
595
12898
                return Ok(StructType::AnyTuple);
596
6040
            }
597
6040

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

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

            
625
18394
                let c = parser.next_char()?;
626
18382
                if matches!(c, '(' | '[' | '{') {
627
1382
                    braces += 1;
628
17000
                } else if matches!(c, ')' | ']' | '}') {
629
6862
                    braces -= 1;
630
10150
                } else if c == ',' && braces == 1 {
631
548
                    parser.skip_ws()?;
632
548
                    more_than_one = !parser.check_char(')');
633
548
                    break;
634
9590
                }
635
            }
636

            
637
6028
            if more_than_one {
638
274
                Ok(StructType::NonNewtypeTuple)
639
            } else {
640
5754
                Ok(StructType::NewtypeTuple)
641
            }
642
84462
        }
643

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

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

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

            
654
84462
        result
655
84462
    }
656

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

            
663
111226
            true
664
        } else {
665
4924343
            false
666
        }
667
5035569
    }
668

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

            
675
60613
            return Ok(false);
676
18256
        }
677

            
678
18256
        let found_ident = match self.identifier() {
679
16338
            Ok(maybe_ident) => maybe_ident,
680
1370
            Err(Error::SuggestRawIdentifier(found_ident)) if found_ident == ident => {
681
274
                return Err(Error::SuggestRawIdentifier(found_ident))
682
            }
683
1644
            Err(_) => return Err(Error::ExpectedNamedStructLike(ident)),
684
        };
685

            
686
16338
        if ident.is_empty() {
687
298
            return Err(Error::ExpectedNamedStructLike(ident));
688
16040
        }
689
16040

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

            
697
14384
        Ok(true)
698
79691
    }
699

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

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

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

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

            
718
33778
            extensions |= extension;
719

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

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

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

            
735
33230
        self.skip_ws()?;
736

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
834
278
            self.set_cursor(backup_cursor);
835
11720
        }
836

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

            
839
11978
        self.advance_bytes(num_bytes);
840
11978

            
841
11978
        Ok(value)
842
14942
    }
843

            
844
589599
    pub fn skip_identifier(&mut self) -> Option<&'a str> {
845
589599
        #[allow(clippy::nonminimal_bool)]
846
589599
        if self.check_str("b\"") // byte string
847
588221
            || self.check_str("b'") // byte literal
848
376967
            || self.check_str("br#") // raw byte string
849
376145
            || self.check_str("br\"") // raw byte string
850
375323
            || self.check_str("r\"") // raw string
851
374775
            || self.check_str("r#\"") // raw string
852
374501
            || self.check_str("r##") // raw string
853
374227
            || false
854
        {
855
215372
            return None;
856
374227
        }
857
374227

            
858
374227
        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
374215
        }
868

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

            
880
262099
        None
881
589599
    }
882

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

            
893
1108
            return Err(Error::ExpectedIdentifier);
894
297058
        }
895
297058

            
896
297058
        // If the next 2-3 bytes signify the start of a (raw) (byte) string
897
297058
        //  literal, return an error.
898
297058
        #[allow(clippy::nonminimal_bool)]
899
297058
        if self.check_str("b\"") // byte string
900
296784
            || self.check_str("b'") // byte literal
901
296510
            || self.check_str("br#") // raw byte string
902
296236
            || self.check_str("br\"") // raw byte string
903
295962
            || self.check_str("r\"") // raw string
904
295688
            || self.check_str("r#\"") // raw string
905
295414
            || self.check_str("r##") // raw string
906
295140
            || false
907
        {
908
1918
            return Err(Error::ExpectedIdentifier);
909
295140
        }
910

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

            
914
7152
            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
7152
            if !matches!(self.peek_char(), Some(c) if is_ident_raw_char(c)) {
919
548
                self.set_cursor(cursor_backup);
920
548
                return Err(Error::ExpectedIdentifier);
921
6604
            }
922
6604

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

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

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

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

            
946
286618
            std_ident_length
947
        };
948

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

            
952
293496
        Ok(ident)
953
299262
    }
954

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

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

            
977
3578441
        if self.src().is_empty() {
978
425168
            return Ok(());
979
3153273
        }
980

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

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

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

            
996
3151347
        Ok(())
997
3578441
    }
998

            
999
17536
    pub fn has_unclosed_line_comment(&self) -> bool {
17536
        self.src().is_empty() && self.cursor.last_ws_len == WS_CURSOR_UNCLOSED_LINE
17536
    }
10456
    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
        }
10456
        if self.consume_char('"') {
1652
            let base64_str = self.escaped_string()?;
1652
            let base64_result = ParsedByteStr::try_from_base64(&base64_str);
1652

            
1652
            if cfg!(not(test)) {
                // FIXME @juntyr: remove in v0.10
                #[allow(deprecated)]
1644
                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),
                }
            }
8804
        } else if self.consume_char('r') {
282
            let base64_str = self.raw_string()?;
282
            let base64_result = ParsedByteStr::try_from_base64(&base64_str);
282

            
282
            if cfg!(not(test)) {
                // FIXME @juntyr: remove in v0.10
                #[allow(deprecated)]
274
                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 {
8522
            self.byte_string_no_base64()
        }
10456
    }
26916
    pub fn byte_string_no_base64(&mut self) -> Result<ParsedByteStr<'a>> {
26916
        if self.consume_str("b\"") {
5782
            self.escaped_byte_string()
21134
        } else if self.consume_str("br") {
3562
            self.raw_byte_string()
        } else {
17572
            Err(Error::ExpectedByteString)
        }
26916
    }
5782
    fn escaped_byte_string(&mut self) -> Result<ParsedByteStr<'a>> {
5782
        match self.escaped_byte_buf(EscapeEncoding::Binary) {
5234
            Ok((bytes, advance)) => {
5234
                self.advance_bytes(advance);
5234
                Ok(bytes)
            }
548
            Err(err) => Err(err),
        }
5782
    }
3562
    fn raw_byte_string(&mut self) -> Result<ParsedByteStr<'a>> {
3562
        match self.raw_byte_buf() {
3014
            Ok((bytes, advance)) => {
3014
                self.advance_bytes(advance);
3014
                Ok(bytes)
            }
274
            Err(Error::ExpectedString) => Err(Error::ExpectedByteString),
274
            Err(err) => Err(err),
        }
3562
    }
105095
    pub fn string(&mut self) -> Result<ParsedStr<'a>> {
105095
        if self.consume_char('"') {
83075
            self.escaped_string()
22020
        } else if self.consume_char('r') {
3066
            self.raw_string()
        } else {
18954
            Err(Error::ExpectedString)
        }
105095
    }
84727
    fn escaped_string(&mut self) -> Result<ParsedStr<'a>> {
84727
        match self.escaped_byte_buf(EscapeEncoding::Utf8) {
82797
            Ok((bytes, advance)) => {
82797
                let string = ParsedStr::try_from_bytes(bytes).map_err(Error::from)?;
82797
                self.advance_bytes(advance);
82797
                Ok(string)
            }
1930
            Err(err) => Err(err),
        }
84727
    }
3348
    fn raw_string(&mut self) -> Result<ParsedStr<'a>> {
3348
        match self.raw_byte_buf() {
2796
            Ok((bytes, advance)) => {
2796
                let string = ParsedStr::try_from_bytes(bytes).map_err(Error::from)?;
2796
                self.advance_bytes(advance);
2796
                Ok(string)
            }
552
            Err(err) => Err(err),
        }
3348
    }
90509
    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
90509
        let str_end = self.src().find('"').ok_or(Error::ExpectedStringEnd)?;
89949
        let escape = self.src()[..str_end].find('\\');
89949
        if let Some(escape) = escape {
            // Now check if escaping is used inside the string
10726
            let mut i = escape;
10726
            let mut s = self.src().as_bytes()[..i].to_vec();
            loop {
23684
                self.advance_bytes(i + 1);
23684

            
23684
                match self.parse_escape(encoding, false)? {
10250
                    EscapeCharacter::Ascii(c) => s.push(c),
11516
                    EscapeCharacter::Utf8(c) => match c.len_utf8() {
10960
                        1 => s.push(c as u8),
556
                        len => {
556
                            let start = s.len();
556
                            s.extend(std::iter::repeat(0).take(len));
556
                            c.encode_utf8(&mut s[start..]);
556
                        }
                    },
                }
                // Checking for '"' and '\\' separately is faster than searching for both at the same time
21766
                let new_str_end = self.src().find('"').ok_or(Error::ExpectedStringEnd)?;
21766
                let new_escape = self.src()[..new_str_end].find('\\');
21766
                if let Some(new_escape) = new_escape {
12958
                    s.extend_from_slice(&self.src().as_bytes()[..new_escape]);
12958
                    i = new_escape;
12958
                } else {
8808
                    s.extend_from_slice(&self.src().as_bytes()[..new_str_end]);
8808
                    // Advance to the end of the string + 1 for the `"`.
8808
                    break Ok((ParsedByteStr::Allocated(s), new_str_end + 1));
                }
            }
        } else {
79223
            let s = &self.src().as_bytes()[..str_end];
79223

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

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

            
5810
        // Advance by the number of bytes of the byte string
5810
        // + `num_hashes` + 1 for the `"`.
5810
        Ok((ParsedByteStr::Slice(s), i + num_hashes + 1))
6910
    }
194116
    fn decode_ascii_escape(&mut self) -> Result<u8> {
194116
        let mut n = 0;
581800
        for _ in 0..2 {
388232
            n <<= 4;
388232
            let byte = self.next_char()?;
388232
            let decoded = Self::decode_hex(byte)?;
387684
            n |= decoded;
        }
193568
        Ok(n)
194116
    }
    #[inline]
919673
    fn decode_hex(c: char) -> Result<u8> {
919673
        if !c.is_ascii() {
274
            return Err(Error::InvalidEscape("Non-hex digit found"));
919399
        }
919399

            
919399
        // c is an ASCII character that can be losslessly cast to u8
919399
        match c as u8 {
919125
            c @ b'0'..=b'9' => Ok(c - b'0'),
108568
            c @ b'a'..=b'f' => Ok(10 + c - b'a'),
54526
            c @ b'A'..=b'F' => Ok(10 + c - b'A'),
274
            _ => Err(Error::InvalidEscape("Non-hex digit found")),
        }
919673
    }
213304
    fn parse_escape(&mut self, encoding: EscapeEncoding, is_char: bool) -> Result<EscapeCharacter> {
213304
        let c = match self.next_char()? {
834
            '\'' => EscapeCharacter::Ascii(b'\''),
3312
            '"' => EscapeCharacter::Ascii(b'"'),
2466
            '\\' => EscapeCharacter::Ascii(b'\\'),
1370
            'n' => EscapeCharacter::Ascii(b'\n'),
548
            'r' => EscapeCharacter::Ascii(b'\r'),
548
            't' => EscapeCharacter::Ascii(b'\t'),
1644
            '0' => EscapeCharacter::Ascii(b'\0'),
            'x' => {
                // Fast exit for ascii escape in byte string
191074
                let b: u8 = self.decode_ascii_escape()?;
190526
                if let EscapeEncoding::Binary = encoding {
186956
                    return Ok(EscapeCharacter::Ascii(b));
3570
                }
3570

            
3570
                // Fast exit for ascii character in UTF-8 string
3570
                let mut bytes = [b, 0, 0, 0];
3570
                if let Ok(Some(c)) = from_utf8(&bytes[..=0]).map(|s| s.chars().next()) {
1644
                    return Ok(EscapeCharacter::Utf8(c));
1926
                }
1926

            
1926
                if is_char {
                    // Character literals are not allowed to use multiple byte
                    //  escapes to build a unicode character
274
                    return Err(Error::InvalidEscape(
274
                        "Not a valid byte-escaped Unicode character",
274
                    ));
1652
                }
                // UTF-8 character needs up to four bytes and we have already
                //  consumed one, so at most three to go
4408
                for i in 1..4 {
4134
                    if !self.consume_str(r"\x") {
1096
                        return Err(Error::InvalidEscape(
1096
                            "Not a valid byte-escaped Unicode character",
1096
                        ));
3038
                    }
3038
                    bytes[i] = self.decode_ascii_escape()?;
                    // Check if we now have a valid UTF-8 character
3038
                    if let Ok(Some(c)) = from_utf8(&bytes[..=i]).map(|s| s.chars().next()) {
282
                        return Ok(EscapeCharacter::Utf8(c));
2756
                    }
                }
274
                return Err(Error::InvalidEscape(
274
                    "Not a valid byte-escaped Unicode character",
274
                ));
            }
            'u' => {
11234
                self.expect_char('{', Error::InvalidEscape("Missing { in Unicode escape"))?;
11234
                let mut bytes: u32 = 0;
11234
                let mut num_digits = 0;
29866
                while num_digits < 6 {
29866
                    let byte = self.peek_char_or_eof()?;
29866
                    if byte == '}' {
11234
                        break;
18632
                    }
18632

            
18632
                    self.skip_next_char();
18632
                    num_digits += 1;
18632
                    let byte = Self::decode_hex(byte)?;
18632
                    bytes <<= 4;
18632
                    bytes |= u32::from(byte);
                }
11234
                if num_digits == 0 {
274
                    return Err(Error::InvalidEscape(
274
                        "Expected 1-6 digits, got 0 digits in Unicode escape",
274
                    ));
10960
                }
10960

            
10960
                self.expect_char(
10960
                    '}',
10960
                    Error::InvalidEscape("No } at the end of Unicode escape"),
10960
                )?;
10960
                let c = char_from_u32(bytes).ok_or(Error::InvalidEscape(
10960
                    "Not a valid Unicode-escaped character",
10960
                ))?;
10960
                EscapeCharacter::Utf8(c)
            }
274
            _ => return Err(Error::InvalidEscape("Unknown escape character")),
        };
21682
        Ok(c)
213304
    }
3180753
    fn skip_comment(&mut self) -> Result<Option<Comment>> {
3180753
        if self.consume_char('/') {
29406
            match self.next_char()? {
                '/' => {
116796
                    let bytes = self.next_chars_while_len(|c| c != '\n');
8022

            
8022
                    self.advance_bytes(bytes);
8022

            
8022
                    if self.src().is_empty() {
1100
                        Ok(Some(Comment::UnclosedLine))
                    } else {
6922
                        Ok(Some(Comment::ClosedLine))
                    }
                }
                '*' => {
21110
                    let mut level = 1;
48792
                    while level > 0 {
191558
                        let bytes = self.next_chars_while_len(|c| !matches!(c, '/' | '*'));
28234

            
28234
                        if self.src().is_empty() {
278
                            return Err(Error::UnclosedBlockComment);
27956
                        }
27956

            
27956
                        self.advance_bytes(bytes);
27956

            
27956
                        // check whether / or * and take action
27956
                        if self.consume_str("/*") {
1918
                            level += 1;
26038
                        } else if self.consume_str("*/") {
22476
                            level -= 1;
22476
                        } else {
3562
                            self.next_char().map_err(|_| Error::UnclosedBlockComment)?;
                        }
                    }
20558
                    Ok(Some(Comment::Block))
                }
274
                c => Err(Error::UnexpectedChar(c)),
            }
        } else {
3151347
            Ok(None)
        }
3180753
    }
}
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 {
1237904
            fn from_u8(x: u8) -> Self {
1237904
                x as $ty
1237904
            }
513639
            fn checked_mul_ext(&mut self, x: u8) -> bool {
513639
                match self.checked_mul(Self::from_u8(x)) {
512809
                    Some(n) => {
512809
                        *self = n;
512809
                        false
                    }
830
                    None => true,
                }
513639
            }
430019
            fn checked_add_ext(&mut self, x: u8) -> bool {
430019
                match self.checked_add(Self::from_u8(x)) {
429608
                    Some(n) => {
429608
                        *self = n;
429608
                        false
                    }
411
                    None => true,
                }
430019
            }
80872
            fn checked_sub_ext(&mut self, x: u8) -> bool {
80872
                match self.checked_sub(Self::from_u8(x)) {
78132
                    Some(n) => {
78132
                        *self = n;
78132
                        false
                    }
2740
                    None => true,
                }
80872
            }
        }
    };
    ($($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 {
109412
            fn parse(parser: &mut Parser, sign: i8) -> Result<Self> {
109412
                parser.parse_integer(sign)
109412
            }
27948
            fn try_from_parsed_integer(parsed: ParsedInteger, ron: &str) -> Result<Self> {
27948
                match parsed {
7398
                    ParsedInteger::$wrap(v) => Ok(v),
                    _ => Err(Error::InvalidValueForType {
20550
                        expected: format!(
20550
                            "a{} {}-bit {}signed integer",
20550
                            if <$ty>::BITS == 8 { "n" } else { "n" },
                            <$ty>::BITS,
20550
                            if <$ty>::MIN == 0 { "un" } else { "" },
                        ),
20550
                        found: String::from(ron),
                    }),
                }
27948
            }
        }
    };
    ($($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 {
74954
    fn parse(parser: &mut Parser, sign: i8) -> Result<Self> {
74954
        if sign < 0 {
1968
            let signed = parser.parse_integer::<LargeSInt>(-1)?;
574
            return if let Ok(x) = i8::try_from(signed) {
286
                Ok(ParsedInteger::I8(x))
288
            } else if let Ok(x) = i16::try_from(signed) {
4
                Ok(ParsedInteger::I16(x))
284
            } 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")]
278
                if let Ok(x) = i64::try_from(signed) {
2
                    Ok(ParsedInteger::I64(x))
                } else {
276
                    Ok(ParsedInteger::I128(signed))
                }
            };
72986
        }
72986
        let unsigned = parser.parse_integer::<LargeUInt>(1)?;
72281
        if let Ok(x) = u8::try_from(unsigned) {
71715
            Ok(ParsedInteger::U8(x))
566
        } else if let Ok(x) = u16::try_from(unsigned) {
8
            Ok(ParsedInteger::U16(x))
558
        } 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")]
552
            if let Ok(x) = u64::try_from(unsigned) {
2
                Ok(ParsedInteger::U64(x))
            } else {
550
                Ok(ParsedInteger::U128(unsigned))
            }
        }
74954
    }
216186
    fn try_from_parsed_integer(parsed: ParsedInteger, _ron: &str) -> Result<Self> {
216186
        Ok(parsed)
216186
    }
}
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 {
31786
            fn parse(float: &str) -> Result<Self> {
31786
                <$ty>::from_str(float).map_err(|_| Error::ExpectedFloat)
31786
            }
3836
            fn try_from_parsed_float(parsed: ParsedFloat, ron: &str) -> Result<Self> {
3836
                match parsed {
3288
                    ParsedFloat::$wrap(v) => Ok(v),
548
                    _ => Err(Error::InvalidValueForType {
548
                        expected: format!(
548
                            "a {}-bit floating point number", $bits,
548
                        ),
548
                        found: String::from(ron),
548
                    }),
                }
3836
            }
        }
    };
    ($($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 {
11350
    fn parse(float: &str) -> Result<Self> {
11350
        let value = f64::from_str(float).map_err(|_| Error::ExpectedFloat)?;
        #[allow(clippy::cast_possible_truncation)]
11350
        if value.total_cmp(&f64::from(value as f32)).is_eq() {
4468
            Ok(ParsedFloat::F32(value as f32))
        } else {
6882
            Ok(ParsedFloat::F64(value))
        }
11350
    }
2740
    fn try_from_parsed_float(parsed: ParsedFloat, _ron: &str) -> Result<Self> {
2740
        Ok(parsed)
2740
    }
}
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> {
85593
    pub fn try_from_bytes(bytes: ParsedByteStr<'a>) -> Result<Self, Utf8Error> {
85593
        match bytes {
6048
            ParsedByteStr::Allocated(byte_buf) => Ok(ParsedStr::Allocated(
6048
                String::from_utf8(byte_buf).map_err(|e| e.utf8_error())?,
            )),
79545
            ParsedByteStr::Slice(bytes) => Ok(ParsedStr::Slice(from_utf8(bytes)?)),
        }
85593
    }
}
impl<'a> ParsedByteStr<'a> {
1934
    pub fn try_from_base64(str: &ParsedStr<'a>) -> Result<Self, base64::DecodeError> {
1934
        let base64_str = match str {
274
            ParsedStr::Allocated(string) => string.as_str(),
1660
            ParsedStr::Slice(str) => str,
        };
1934
        base64::engine::Engine::decode(&base64::engine::general_purpose::STANDARD, base64_str)
1934
            .map(ParsedByteStr::Allocated)
1934
    }
}
#[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
    }
}