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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
130
562022
        Ok(parser)
131
562852
    }
132

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

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

            
147
7150
    pub fn is_number_start(&self, c: char) -> bool {
148
7150
        matches!(c, '0'..='9' | '+' | '-' | '.' | 'b') && (c != 'b' || self.src().starts_with("b'"))
149
7150
    }
150

            
151
6516253
    pub fn advance_bytes(&mut self, bytes: usize) {
152
6516253
        self.prev_cursor = self.cursor;
153
6516253
        self.cursor.cursor += bytes;
154
6516253
    }
155

            
156
996709
    pub fn next_char(&mut self) -> Result<char> {
157
996709
        let c = self.peek_char_or_eof()?;
158
996408
        self.cursor.cursor += c.len_utf8();
159
996408
        Ok(c)
160
996709
    }
161

            
162
42522
    pub fn skip_next_char(&mut self) {
163
42522
        core::mem::drop(self.next_char());
164
42522
    }
165

            
166
3051008
    pub fn peek_char(&self) -> Option<char> {
167
3051008
        self.src().chars().next()
168
3051008
    }
169

            
170
2284888
    pub fn peek_char_or_eof(&self) -> Result<char> {
171
2284888
        self.peek_char().ok_or(Error::Eof)
172
2284888
    }
173

            
174
7019109
    pub fn check_char(&self, c: char) -> bool {
175
7019109
        self.src().starts_with(c)
176
7019109
    }
177

            
178
16961806
    pub fn check_str(&self, s: &str) -> bool {
179
16961806
        self.src().starts_with(s)
180
16961806
    }
181

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

            
186
26638
    pub fn pre_ws_src(&self) -> &'a str {
187
26638
        &self.src[self.cursor.pre_ws_cursor..]
188
26638
    }
189

            
190
2419416
    pub fn consume_str(&mut self, s: &str) -> bool {
191
2419416
        if self.check_str(s) {
192
512415
            self.advance_bytes(s.len());
193

            
194
512415
            true
195
        } else {
196
1907001
            false
197
        }
198
2419416
    }
199

            
200
5161470
    pub fn consume_char(&mut self, c: char) -> bool {
201
5161470
        if self.check_char(c) {
202
1558884
            self.advance_bytes(c.len_utf8());
203

            
204
1558884
            true
205
        } else {
206
3602586
            false
207
        }
208
5161470
    }
209

            
210
71255
    fn consume_all(&mut self, all: &[&str]) -> Result<bool> {
211
71255
        all.iter()
212
178420
            .map(|elem| {
213
178420
                if self.consume_str(elem) {
214
178381
                    self.skip_ws()?;
215

            
216
178381
                    Ok(true)
217
                } else {
218
39
                    Ok(false)
219
                }
220
178420
            })
221
178420
            .try_fold(true, |acc, x| x.map(|x| x && acc))
222
71255
    }
223

            
224
149555
    pub fn expect_char(&mut self, expected: char, error: Error) -> Result<()> {
225
149555
        if self.consume_char(expected) {
226
129772
            Ok(())
227
        } else {
228
19783
            Err(error)
229
        }
230
149555
    }
231

            
232
    #[must_use]
233
4245785
    pub fn next_chars_while_len(&self, condition: fn(char) -> bool) -> usize {
234
4245785
        self.next_chars_while_from_len(0, condition)
235
4245785
    }
236

            
237
    #[must_use]
238
5310038
    pub fn next_chars_while_from_len(&self, from: usize, condition: fn(char) -> bool) -> usize {
239
5310038
        self.src()[from..]
240
14015849
            .find(|c| !condition(c))
241
5310038
            .unwrap_or(self.src().len() - from)
242
5310038
    }
243
}
244

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

            
255
573190
        for (i, c) in s.char_indices() {
256
573190
            if c == '_' {
257
9582
                continue;
258
563608
            }
259

            
260
563608
            if num_acc.checked_mul_ext(base) {
261
866
                self.advance_bytes(s.len());
262
866
                return Err(Error::IntegerOutOfBounds);
263
562742
            }
264

            
265
562742
            let digit = Self::decode_hex(c)?;
266

            
267
562742
            if digit >= base {
268
2002
                self.advance_bytes(i);
269
2002
                return Err(Error::InvalidIntegerDigit { digit: c, base });
270
560740
            }
271

            
272
560740
            if f(&mut num_acc, digit) {
273
3289
                self.advance_bytes(s.len());
274
3289
                return Err(Error::IntegerOutOfBounds);
275
557451
            }
276
        }
277

            
278
226356
        self.advance_bytes(s.len());
279

            
280
226356
        Ok(num_acc)
281
232513
    }
282

            
283
239977
    fn parse_integer<T: Num>(&mut self, sign: i8, base: u8) -> Result<T> {
284
239977
        let num_bytes = self.next_chars_while_len(is_int_char);
285

            
286
239977
        if num_bytes == 0 {
287
4604
            return Err(Error::ExpectedInteger);
288
235373
        }
289

            
290
235373
        if self.check_char('_') {
291
2860
            return Err(Error::UnderscoreAtBeginning);
292
232513
        }
293

            
294
232513
        let s = &self.src()[..num_bytes];
295

            
296
232513
        if sign > 0 {
297
215215
            self.parse_integer_digits(s, base, T::checked_add_ext)
298
        } else {
299
17298
            self.parse_integer_digits(s, base, T::checked_sub_ext)
300
        }
301
239977
    }
302

            
303
    #[allow(clippy::too_many_lines)]
304
332823
    pub fn integer<T: Integer>(&mut self) -> Result<T> {
305
332823
        let src_backup = self.src();
306

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

            
332
221940
                if !self.consume_char('\'') {
333
286
                    return Err(Error::ExpectedByteLiteral);
334
221654
                }
335

            
336
221654
                let bytes_ron = &src_backup[..src_backup.len() - self.src().len()];
337

            
338
221654
                return T::try_from_parsed_integer(ParsedInteger::U8(byte), bytes_ron);
339
            }
340
105152
            _ => false,
341
        };
342
110311
        let sign = if is_negative { -1 } else { 1 };
343

            
344
110311
        let base = match () {
345
110311
            () if self.consume_str("0b") => 2,
346
107701
            () if self.consume_str("0o") => 8,
347
105949
            () if self.consume_str("0x") => 16,
348
104193
            () => 10,
349
        };
350

            
351
110311
        let num_bytes = self.next_chars_while_len(is_int_char);
352

            
353
110311
        if self.src()[num_bytes..].starts_with(['i', 'u']) {
354
12343
            let int_cursor = self.cursor;
355
12343
            self.advance_bytes(num_bytes);
356

            
357
            #[allow(clippy::never_loop)]
358
            loop {
359
12343
                let (res, suffix_bytes) = if self.consume_ident("i8") {
360
898
                    let suffix_bytes = self.src();
361
898
                    self.set_cursor(int_cursor);
362
898
                    (
363
898
                        self.parse_integer::<i8>(sign, base).map(ParsedInteger::I8),
364
898
                        suffix_bytes,
365
898
                    )
366
11445
                } else if self.consume_ident("i16") {
367
898
                    let suffix_bytes = self.src();
368
898
                    self.set_cursor(int_cursor);
369
898
                    (
370
898
                        self.parse_integer::<i16>(sign, base)
371
898
                            .map(ParsedInteger::I16),
372
898
                        suffix_bytes,
373
898
                    )
374
10547
                } else if self.consume_ident("i32") {
375
1228
                    let suffix_bytes = self.src();
376
1228
                    self.set_cursor(int_cursor);
377
1228
                    (
378
1228
                        self.parse_integer::<i32>(sign, base)
379
1228
                            .map(ParsedInteger::I32),
380
1228
                        suffix_bytes,
381
1228
                    )
382
9319
                } else if self.consume_ident("i64") {
383
898
                    let suffix_bytes = self.src();
384
898
                    self.set_cursor(int_cursor);
385
898
                    (
386
898
                        self.parse_integer::<i64>(sign, base)
387
898
                            .map(ParsedInteger::I64),
388
898
                        suffix_bytes,
389
898
                    )
390
8421
                } else if self.consume_ident("u8") {
391
2324
                    let suffix_bytes = self.src();
392
2324
                    self.set_cursor(int_cursor);
393
2324
                    (
394
2324
                        self.parse_integer::<u8>(sign, base).map(ParsedInteger::U8),
395
2324
                        suffix_bytes,
396
2324
                    )
397
6097
                } else if self.consume_ident("u16") {
398
1188
                    let suffix_bytes = self.src();
399
1188
                    self.set_cursor(int_cursor);
400
1188
                    (
401
1188
                        self.parse_integer::<u16>(sign, base)
402
1188
                            .map(ParsedInteger::U16),
403
1188
                        suffix_bytes,
404
1188
                    )
405
4909
                } else if self.consume_ident("u32") {
406
1220
                    let suffix_bytes = self.src();
407
1220
                    self.set_cursor(int_cursor);
408
1220
                    (
409
1220
                        self.parse_integer::<u32>(sign, base)
410
1220
                            .map(ParsedInteger::U32),
411
1220
                        suffix_bytes,
412
1220
                    )
413
3689
                } else if self.consume_ident("u64") {
414
1188
                    let suffix_bytes = self.src();
415
1188
                    self.set_cursor(int_cursor);
416
1188
                    (
417
1188
                        self.parse_integer::<u64>(sign, base)
418
1188
                            .map(ParsedInteger::U64),
419
1188
                        suffix_bytes,
420
1188
                    )
421
                } else {
422
                    #[cfg(feature = "integer128")]
423
1629
                    if self.consume_ident("i128") {
424
451
                        let suffix_bytes = self.src();
425
451
                        self.set_cursor(int_cursor);
426
451
                        (
427
451
                            self.parse_integer::<i128>(sign, base)
428
451
                                .map(ParsedInteger::I128),
429
451
                            suffix_bytes,
430
451
                        )
431
1178
                    } else if self.consume_ident("u128") {
432
596
                        let suffix_bytes = self.src();
433
596
                        self.set_cursor(int_cursor);
434
596
                        (
435
596
                            self.parse_integer::<u128>(sign, base)
436
596
                                .map(ParsedInteger::U128),
437
596
                            suffix_bytes,
438
596
                        )
439
                    } else {
440
582
                        break;
441
                    }
442
                    #[cfg(not(feature = "integer128"))]
443
                    {
444
872
                        break;
445
                    }
446
                };
447

            
448
10595
                if !matches!(
449
1885
                    &res,
450
                    Err(Error::UnderscoreAtBeginning | Error::InvalidIntegerDigit { .. })
451
10595
                ) {
452
10595
                    // Advance past the number suffix
453
10595
                    self.skip_identifier();
454
10595
                }
455

            
456
10889
                let integer_ron = &src_backup[..src_backup.len() - suffix_bytes.len()];
457

            
458
10889
                return res.and_then(|parsed| T::try_from_parsed_integer(parsed, integer_ron));
459
            }
460

            
461
1454
            self.set_cursor(int_cursor);
462
97968
        }
463

            
464
99422
        T::parse(self, sign, base)
465
332823
    }
466

            
467
322689
    pub fn any_number(&mut self) -> Result<Number> {
468
322689
        if self.consume_ident("inf") || self.consume_ident("inff32") {
469
290
            return Ok(Number::F32(crate::value::F32(core::f32::INFINITY)));
470
322399
        } else if self.consume_ident("inff64") {
471
286
            return Ok(Number::F64(crate::value::F64(core::f64::INFINITY)));
472
322113
        } else if self.consume_ident("NaN") || self.consume_ident("NaNf32") {
473
290
            return Ok(Number::F32(crate::value::F32(core::f32::NAN)));
474
321823
        } else if self.consume_ident("NaNf64") {
475
286
            return Ok(Number::F64(crate::value::F64(core::f64::NAN)));
476
321537
        }
477

            
478
321537
        if self.next_bytes_is_float() {
479
11226
            return match self.float::<ParsedFloat>()? {
480
3480
                ParsedFloat::F32(v) => Ok(Number::F32(v.into())),
481
7746
                ParsedFloat::F64(v) => Ok(Number::F64(v.into())),
482
            };
483
310311
        }
484

            
485
310311
        let backup_cursor = self.cursor;
486

            
487
310311
        let (integer_err, integer_cursor) = match self.integer::<ParsedInteger>() {
488
306843
            Ok(integer) => {
489
306843
                return match integer {
490
870
                    ParsedInteger::I8(v) => Ok(Number::I8(v)),
491
576
                    ParsedInteger::I16(v) => Ok(Number::I16(v)),
492
576
                    ParsedInteger::I32(v) => Ok(Number::I32(v)),
493
576
                    ParsedInteger::I64(v) => Ok(Number::I64(v)),
494
                    #[cfg(feature = "integer128")]
495
574
                    ParsedInteger::I128(v) => Ok(Number::I128(v)),
496
300793
                    ParsedInteger::U8(v) => Ok(Number::U8(v)),
497
866
                    ParsedInteger::U16(v) => Ok(Number::U16(v)),
498
576
                    ParsedInteger::U32(v) => Ok(Number::U32(v)),
499
576
                    ParsedInteger::U64(v) => Ok(Number::U64(v)),
500
                    #[cfg(feature = "integer128")]
501
860
                    ParsedInteger::U128(v) => Ok(Number::U128(v)),
502
                }
503
            }
504
3468
            Err(err) => (err, self.cursor),
505
        };
506

            
507
3468
        self.set_cursor(backup_cursor);
508

            
509
        // Fall-back to parse an out-of-range integer as a float
510
3468
        match self.float::<ParsedFloat>() {
511
2888
            Ok(ParsedFloat::F32(v)) if self.cursor >= integer_cursor => Ok(Number::F32(v.into())),
512
580
            Ok(ParsedFloat::F64(v)) if self.cursor >= integer_cursor => Ok(Number::F64(v.into())),
513
            _ => {
514
                // Return the more precise integer error
515
1287
                self.set_cursor(integer_cursor);
516
1287
                Err(integer_err)
517
            }
518
        }
519
322689
    }
520

            
521
31896
    pub fn bool(&mut self) -> Result<bool> {
522
31896
        if self.consume_ident("true") {
523
17802
            Ok(true)
524
14094
        } else if self.consume_ident("false") {
525
14058
            Ok(false)
526
        } else {
527
36
            Err(Error::ExpectedBoolean)
528
        }
529
31896
    }
530

            
531
69763
    pub fn char(&mut self) -> Result<char> {
532
69763
        self.expect_char('\'', Error::ExpectedChar)?;
533

            
534
50556
        let c = self.next_char()?;
535

            
536
50556
        let c = if c == '\\' {
537
3730
            match self.parse_escape(EscapeEncoding::Utf8, true)? {
538
                // we know that this byte is an ASCII character
539
1728
                EscapeCharacter::Ascii(b) => char::from(b),
540
1144
                EscapeCharacter::Utf8(c) => c,
541
            }
542
        } else {
543
46826
            c
544
        };
545

            
546
49698
        self.expect_char('\'', Error::ExpectedChar)?;
547

            
548
49698
        Ok(c)
549
69763
    }
550

            
551
428613
    pub fn comma(&mut self) -> Result<bool> {
552
428613
        self.skip_ws()?;
553

            
554
428613
        if self.consume_char(',') {
555
248615
            self.skip_ws()?;
556

            
557
248615
            Ok(true)
558
        } else {
559
179998
            Ok(false)
560
        }
561
428613
    }
562

            
563
    /// Only returns true if the char after `ident` cannot belong
564
    /// to an identifier.
565
7850730
    pub fn check_ident(&mut self, ident: &str) -> bool {
566
7850730
        self.check_str(ident) && !self.check_ident_other_char(ident.len())
567
7850730
    }
568

            
569
303969
    fn check_ident_other_char(&self, index: usize) -> bool {
570
303969
        self.src()[index..]
571
303969
            .chars()
572
303969
            .next()
573
303969
            .map_or(false, is_xid_continue)
574
303969
    }
575

            
576
    /// Check which type of struct we are currently parsing. The parsing state
577
    ///  is only changed in case of an error, to provide a better position.
578
    ///
579
    /// [`NewtypeMode::NoParensMeanUnit`] detects (tuple) structs by a leading
580
    ///  opening bracket and reports a unit struct otherwise.
581
    /// [`NewtypeMode::InsideNewtype`] skips an initial check for unit structs,
582
    ///  and means that any leading opening bracket is not considered to open
583
    ///  a (tuple) struct but to be part of the structs inner contents.
584
    ///
585
    /// [`TupleMode::ImpreciseTupleOrNewtype`] only performs a cheap, O(1),
586
    ///  single-identifier lookahead check to distinguish tuple structs from
587
    ///  non-tuple structs.
588
    /// [`TupleMode::DifferentiateNewtype`] performs an expensive, O(N), look-
589
    ///  ahead over the entire next value tree, which can span the entirety of
590
    ///  the remaining document in the worst case.
591
80302
    pub fn check_struct_type(
592
80302
        &mut self,
593
80302
        newtype: NewtypeMode,
594
80302
        tuple: TupleMode,
595
80302
    ) -> Result<StructType> {
596
80302
        fn check_struct_type_inner(
597
80302
            parser: &mut Parser,
598
80302
            newtype: NewtypeMode,
599
80302
            tuple: TupleMode,
600
80302
        ) -> Result<StructType> {
601
80302
            if matches!(newtype, NewtypeMode::NoParensMeanUnit) && !parser.consume_char('(') {
602
12588
                return Ok(StructType::Unit);
603
67714
            }
604

            
605
67714
            parser.skip_ws()?;
606

            
607
            // Check for `Ident()`, which could be
608
            // - a zero-field struct or tuple (variant)
609
            // - an unwrapped newtype around a unit
610
67710
            if matches!(newtype, NewtypeMode::NoParensMeanUnit) && parser.check_char(')') {
611
858
                return Ok(StructType::EmptyTuple);
612
66852
            }
613

            
614
66852
            if parser.skip_identifier().is_some() {
615
47652
                parser.skip_ws()?;
616

            
617
47652
                match parser.peek_char() {
618
                    // Definitely a struct with named fields
619
41638
                    Some(':') => return Ok(StructType::Named),
620
                    // Definitely a tuple-like struct with fields
621
                    Some(',') => {
622
4298
                        parser.skip_next_char();
623
4298
                        parser.skip_ws()?;
624
4298
                        if parser.check_char(')') {
625
                            // A one-element tuple could be a newtype
626
                            return Ok(StructType::NewtypeTuple);
627
4298
                        }
628
                        // Definitely a tuple struct with more than one field
629
4298
                        return Ok(StructType::NonNewtypeTuple);
630
                    }
631
                    // Either a newtype or a tuple struct
632
1144
                    Some(')') => return Ok(StructType::NewtypeTuple),
633
                    // Something else, let's investigate further
634
572
                    Some(_) | None => (),
635
                };
636
19200
            }
637

            
638
19772
            if matches!(tuple, TupleMode::ImpreciseTupleOrNewtype) {
639
13465
                return Ok(StructType::AnyTuple);
640
6307
            }
641

            
642
6307
            let mut braces = 1_usize;
643
6307
            let mut more_than_one = false;
644

            
645
            // Skip ahead to see if the value is followed by another value
646
24927
            while braces > 0 {
647
                // Skip spurious braces in comments, strings, and characters
648
19207
                parser.skip_ws()?;
649
19207
                let cursor_backup = parser.cursor;
650
19207
                if parser.char().is_err() {
651
19207
                    parser.set_cursor(cursor_backup);
652
19207
                }
653
19207
                let cursor_backup = parser.cursor;
654
19207
                match parser.string() {
655
1144
                    Ok(_) => (),
656
                    // prevent quadratic complexity backtracking for unterminated string
657
                    Err(err @ (Error::ExpectedStringEnd | Error::Eof)) => return Err(err),
658
18063
                    Err(_) => parser.set_cursor(cursor_backup),
659
                }
660
19207
                let cursor_backup = parser.cursor;
661
                // we have already checked for strings, which subsume base64 byte strings
662
19207
                match parser.byte_string_no_base64() {
663
858
                    Ok(_) => (),
664
                    // prevent quadratic complexity backtracking for unterminated byte string
665
                    Err(err @ (Error::ExpectedStringEnd | Error::Eof)) => return Err(err),
666
18349
                    Err(_) => parser.set_cursor(cursor_backup),
667
                }
668

            
669
19207
                let c = parser.next_char()?;
670
19192
                if matches!(c, '(' | '[' | '{') {
671
1445
                    braces += 1;
672
17747
                } else if matches!(c, ')' | ']' | '}') {
673
7165
                    braces -= 1;
674
10597
                } else if c == ',' && braces == 1 {
675
572
                    parser.skip_ws()?;
676
572
                    more_than_one = !parser.check_char(')');
677
572
                    break;
678
10010
                }
679
            }
680

            
681
6292
            if more_than_one {
682
286
                Ok(StructType::NonNewtypeTuple)
683
            } else {
684
6006
                Ok(StructType::NewtypeTuple)
685
            }
686
80302
        }
687

            
688
        // Create a temporary working copy
689
80302
        let backup_cursor = self.cursor;
690

            
691
80302
        let result = check_struct_type_inner(self, newtype, tuple);
692

            
693
80302
        if result.is_ok() {
694
80283
            // Revert the parser to before the struct type check
695
80283
            self.set_cursor(backup_cursor);
696
80283
        }
697

            
698
80302
        result
699
80302
    }
700

            
701
    /// Only returns true if the char after `ident` cannot belong
702
    /// to an identifier.
703
7231075
    pub fn consume_ident(&mut self, ident: &str) -> bool {
704
7231075
        if self.check_ident(ident) {
705
162444
            self.advance_bytes(ident.len());
706

            
707
162444
            true
708
        } else {
709
7068631
            false
710
        }
711
7231075
    }
712

            
713
86472
    pub fn consume_struct_name(&mut self, ident: &'static str) -> Result<bool> {
714
86472
        if self.check_ident("") {
715
68979
            if self.exts.contains(Extensions::EXPLICIT_STRUCT_NAMES) {
716
858
                return Err(Error::ExpectedStructName(ident.to_string()));
717
68121
            }
718

            
719
68121
            return Ok(false);
720
17493
        }
721

            
722
17493
        let found_ident = match self.identifier() {
723
15491
            Ok(maybe_ident) => maybe_ident,
724
1430
            Err(Error::SuggestRawIdentifier(found_ident)) if found_ident == ident => {
725
286
                return Err(Error::SuggestRawIdentifier(found_ident))
726
            }
727
1716
            Err(_) => return Err(Error::ExpectedNamedStructLike(ident)),
728
        };
729

            
730
15491
        if ident.is_empty() {
731
316
            return Err(Error::ExpectedNamedStructLike(ident));
732
15175
        }
733

            
734
15175
        if found_ident != ident {
735
1728
            return Err(Error::ExpectedDifferentStructName {
736
1728
                expected: ident,
737
1728
                found: String::from(found_ident),
738
1728
            });
739
13447
        }
740

            
741
13447
        Ok(true)
742
86472
    }
743

            
744
    /// Parse a document attribute at the current cursor position.
745
597932
    fn attribute(&mut self) -> Result<ParsedAttribute> {
746
597932
        if !self.check_char('#') {
747
562022
            return Ok(ParsedAttribute::None);
748
35910
        }
749

            
750
35910
        if !self.consume_all(&["#", "!", "["])? {
751
12
            return Err(Error::ExpectedAttribute);
752
35898
        }
753

            
754
35898
        self.skip_ws()?;
755
35898
        if self.consume_ident("enable") {
756
35862
            self.skip_ws()?;
757
35862
            if !self.consume_str("(") {
758
                return Err(Error::ExpectedAttribute);
759
35862
            }
760

            
761
35862
            self.skip_ws()?;
762
35862
            let extensions = self.extension_list()?;
763
35345
            self.skip_ws()?;
764

            
765
35345
            if self.consume_all(&[")", "]"])? {
766
35330
                Ok(ParsedAttribute::Extensions(extensions))
767
            } else {
768
15
                Err(Error::ExpectedAttributeEnd)
769
            }
770
36
        } else if self.consume_ident("type") || self.consume_ident("schema") {
771
36
            self.skip_ws()?;
772
36
            if !self.consume_str("=") {
773
                return Err(Error::ExpectedAttribute);
774
36
            }
775

            
776
36
            self.skip_ws()?;
777
36
            self.string()?;
778
36
            self.skip_ws()?;
779

            
780
36
            if self.consume_str("]") {
781
36
                Ok(ParsedAttribute::Ignored)
782
            } else {
783
                Err(Error::ExpectedAttributeEnd)
784
            }
785
        } else {
786
            Err(Error::ExpectedAttribute)
787
        }
788
597932
    }
789

            
790
    /// Returns the extensions bit mask.
791
35862
    fn extension_list(&mut self) -> Result<Extensions> {
792
35862
        let mut extensions = Extensions::empty();
793

            
794
        loop {
795
36148
            let ident = self.identifier()?;
796
36148
            let extension = Extensions::from_ident(ident)
797
36148
                .ok_or_else(|| Error::NoSuchExtension(ident.into()))?;
798

            
799
36133
            extensions |= extension;
800

            
801
36133
            let comma = self.comma()?;
802

            
803
            // If we have no comma but another item, return an error
804
36133
            if !comma && self.check_ident_other_char(0) {
805
502
                return Err(Error::ExpectedComma);
806
35631
            }
807

            
808
            // If there's no comma, assume the list ended.
809
            // If there is, it might be a trailing one, thus we only
810
            // continue the loop if we get an ident char.
811
35631
            if !comma || !self.check_ident_other_char(0) {
812
35345
                break;
813
286
            }
814
        }
815

            
816
35345
        Ok(extensions)
817
35862
    }
818

            
819
15599
    pub fn float<T: Float>(&mut self) -> Result<T> {
820
        const F32_SUFFIX: &str = "f32";
821
        const F64_SUFFIX: &str = "f64";
822

            
823
91546
        for (literal, value_f32, value_f64) in &[
824
15599
            ("inf", f32::INFINITY, f64::INFINITY),
825
15599
            ("+inf", f32::INFINITY, f64::INFINITY),
826
15599
            ("-inf", f32::NEG_INFINITY, f64::NEG_INFINITY),
827
15599
            ("NaN", f32::NAN, f64::NAN),
828
15599
            ("+NaN", f32::NAN, f64::NAN),
829
15599
            ("-NaN", -f32::NAN, -f64::NAN),
830
15599
        ] {
831
91546
            if self.consume_ident(literal) {
832
96
                return T::parse(literal);
833
91450
            }
834

            
835
91450
            if let Some(suffix) = self.src().strip_prefix(literal) {
836
1188
                if let Some(post_suffix) = suffix.strip_prefix(F32_SUFFIX) {
837
592
                    if !post_suffix.chars().next().map_or(false, is_xid_continue) {
838
588
                        let float_ron = &self.src()[..literal.len() + F32_SUFFIX.len()];
839
588
                        self.advance_bytes(literal.len() + F32_SUFFIX.len());
840
588
                        return T::try_from_parsed_float(ParsedFloat::F32(*value_f32), float_ron);
841
4
                    }
842
596
                }
843

            
844
600
                if let Some(post_suffix) = suffix.strip_prefix(F64_SUFFIX) {
845
592
                    if !post_suffix.chars().next().map_or(false, is_xid_continue) {
846
588
                        let float_ron = &self.src()[..literal.len() + F64_SUFFIX.len()];
847
588
                        self.advance_bytes(literal.len() + F64_SUFFIX.len());
848
588
                        return T::try_from_parsed_float(ParsedFloat::F64(*value_f64), float_ron);
849
4
                    }
850
8
                }
851
90262
            }
852
        }
853

            
854
14327
        let raw_bytes = self.next_chars_while_len(is_float_char);
855
14327
        let src = &self.src()[..raw_bytes];
856
14327
        let num_bytes = src.find("..").unwrap_or(raw_bytes);
857

            
858
14327
        if num_bytes == 0 {
859
46
            return Err(Error::ExpectedFloat);
860
14281
        }
861

            
862
14281
        if self.check_char('_') {
863
4
            return Err(Error::UnderscoreAtBeginning);
864
14277
        }
865

            
866
14277
        let mut f = String::with_capacity(num_bytes);
867
14277
        let mut allow_underscore = false;
868

            
869
258485
        for (i, c) in self.src()[..num_bytes].char_indices() {
870
784
            match c {
871
776
                '_' if allow_underscore => continue,
872
                '_' => {
873
8
                    self.advance_bytes(i);
874
8
                    return Err(Error::FloatUnderscore);
875
                }
876
243723
                '0'..='9' | 'e' | 'E' => allow_underscore = true,
877
11709
                '.' => allow_underscore = false,
878
2269
                _ => (),
879
            }
880

            
881
            // we know that the byte is an ASCII character here
882
257701
            f.push(c);
883
        }
884

            
885
14269
        if self.src()[num_bytes..].starts_with('f') {
886
2038
            let backup_cursor = self.cursor;
887
2038
            self.advance_bytes(num_bytes);
888

            
889
            #[allow(clippy::never_loop)]
890
            loop {
891
2038
                let res = if self.consume_ident(F32_SUFFIX) {
892
1160
                    f32::from_str(&f).map(ParsedFloat::F32)
893
878
                } else if self.consume_ident(F64_SUFFIX) {
894
588
                    f64::from_str(&f).map(ParsedFloat::F64)
895
                } else {
896
290
                    break;
897
                };
898

            
899
1748
                let parsed = if let Ok(parsed) = res {
900
1740
                    parsed
901
                } else {
902
8
                    self.set_cursor(backup_cursor);
903
8
                    return Err(Error::ExpectedFloat);
904
                };
905

            
906
1740
                let float_ron = &self.src[backup_cursor.cursor..self.cursor.cursor];
907

            
908
1740
                return T::try_from_parsed_float(parsed, float_ron);
909
            }
910

            
911
290
            self.set_cursor(backup_cursor);
912
12231
        }
913

            
914
12521
        let value = T::parse(&f)?;
915

            
916
12501
        self.advance_bytes(num_bytes);
917

            
918
12501
        Ok(value)
919
15599
    }
920

            
921
607987
    pub fn skip_identifier(&mut self) -> Option<&'a str> {
922
        #[allow(clippy::nonminimal_bool)]
923
607987
        if self.check_str("b\"") // byte string
924
606549
            || self.check_str("b'") // byte literal
925
386043
            || self.check_str("br#") // raw byte string
926
385185
            || self.check_str("br\"") // raw byte string
927
384327
            || self.check_str("r\"") // raw string
928
383755
            || self.check_str("r#\"") // raw string
929
383469
            || self.check_str("r##") // raw string
930
383183
            || false
931
        {
932
224804
            return None;
933
383183
        }
934

            
935
383183
        if self.check_str("r#") {
936
            // maybe a raw identifier
937
12
            let len = self.next_chars_while_from_len(2, is_ident_raw_char);
938
12
            if len > 0 {
939
4
                let ident = &self.src()[2..2 + len];
940
4
                self.advance_bytes(2 + len);
941
4
                return Some(ident);
942
8
            }
943
8
            return None;
944
383171
        }
945

            
946
383171
        if let Some(c) = self.peek_char() {
947
            // maybe a normal identifier
948
381741
            if is_ident_first_char(c) {
949
114027
                let len =
950
114027
                    c.len_utf8() + self.next_chars_while_from_len(c.len_utf8(), is_xid_continue);
951
114027
                let ident = &self.src()[..len];
952
114027
                self.advance_bytes(len);
953
114027
                return Some(ident);
954
267714
            }
955
1430
        }
956

            
957
269144
        None
958
607987
    }
959

            
960
319478
    pub fn identifier(&mut self) -> Result<&'a str> {
961
319478
        let first = self.peek_char_or_eof()?;
962
319478
        if !is_ident_first_char(first) {
963
2300
            if is_ident_raw_char(first) {
964
1144
                let ident_bytes = self.next_chars_while_len(is_ident_raw_char);
965
1144
                return Err(Error::SuggestRawIdentifier(
966
1144
                    self.src()[..ident_bytes].into(),
967
1144
                ));
968
1156
            }
969

            
970
1156
            return Err(Error::ExpectedIdentifier);
971
317178
        }
972

            
973
        // If the next 2-3 bytes signify the start of a (raw) (byte) string
974
        //  literal, return an error.
975
        #[allow(clippy::nonminimal_bool)]
976
317178
        if self.check_str("b\"") // byte string
977
316892
            || self.check_str("b'") // byte literal
978
316606
            || self.check_str("br#") // raw byte string
979
316320
            || self.check_str("br\"") // raw byte string
980
316034
            || self.check_str("r\"") // raw string
981
315748
            || self.check_str("r#\"") // raw string
982
315462
            || self.check_str("r##") // raw string
983
315176
            || false
984
        {
985
2002
            return Err(Error::ExpectedIdentifier);
986
315176
        }
987

            
988
315176
        let length = if self.check_str("r#") {
989
7464
            let cursor_backup = self.cursor;
990

            
991
7464
            self.advance_bytes(2);
992

            
993
            // Note: it's important to check this before advancing forward, so that
994
            // the value-type deserializer can fall back to parsing it differently.
995
7464
            if !matches!(self.peek_char(), Some(c) if is_ident_raw_char(c)) {
996
572
                self.set_cursor(cursor_backup);
997
572
                return Err(Error::ExpectedIdentifier);
998
6892
            }
999

            
6892
            self.next_chars_while_len(is_ident_raw_char)
307712
        } else if first == 'r' {
572
            let std_ident_length = self.next_chars_while_len(is_xid_continue);
572
            let raw_ident_length = self.next_chars_while_len(is_ident_raw_char);
572
            if raw_ident_length > std_ident_length {
286
                return Err(Error::SuggestRawIdentifier(
286
                    self.src()[..raw_ident_length].into(),
286
                ));
286
            }
286
            std_ident_length
        } else {
307140
            let std_ident_length = first.len_utf8()
307140
                + self.next_chars_while_from_len(first.len_utf8(), is_xid_continue);
307140
            let raw_ident_length = self.next_chars_while_len(is_ident_raw_char);
307140
            if raw_ident_length > std_ident_length {
858
                return Err(Error::SuggestRawIdentifier(
858
                    self.src()[..raw_ident_length].into(),
858
                ));
306282
            }
306282
            std_ident_length
        };
313460
        let ident = &self.src()[..length];
313460
        self.advance_bytes(length);
313460
        Ok(ident)
319478
    }
321541
    pub fn next_bytes_is_float(&mut self) -> bool {
321541
        if let Some(c) = self.peek_char() {
321537
            let skip = match c {
5230
                '+' | '-' => 1,
316307
                _ => 0,
            };
321537
            let raw_float_len = self.next_chars_while_from_len(skip, is_float_char);
            // Trim at ".." to avoid treating range operators as float chars
321537
            let valid_float_len = self.src()[skip..]
321537
                .find("..")
321537
                .map(|i| i.min(raw_float_len))
321537
                .map_or(raw_float_len, |i| i.min(raw_float_len));
321537
            let valid_int_len = self.next_chars_while_from_len(skip, is_int_char);
321537
            valid_float_len > valid_int_len
        } else {
4
            false
        }
321541
    }
3778992
    pub fn skip_ws(&mut self) -> Result<()> {
3778992
        if (self.cursor.last_ws_len != WS_CURSOR_UNCLOSED_LINE)
3778416
            && ((self.cursor.pre_ws_cursor + self.cursor.last_ws_len) < self.cursor.cursor)
2436177
        {
2436177
            // the last whitespace is disjoint from this one, we need to track a new one
2436177
            self.cursor.pre_ws_cursor = self.cursor.cursor;
2436177
        }
3778992
        if self.src().is_empty() {
452938
            return Ok(());
3326054
        }
        loop {
3354950
            self.advance_bytes(self.next_chars_while_len(is_whitespace_char));
3354950
            match self.skip_comment()? {
3324044
                None => break,
                Some(Comment::UnclosedLine) => {
1148
                    self.cursor.last_ws_len = WS_CURSOR_UNCLOSED_LINE;
1148
                    return Ok(());
                }
28896
                Some(Comment::ClosedLine | Comment::Block) => continue,
            }
        }
3324044
        self.cursor.last_ws_len = self.cursor.cursor - self.cursor.pre_ws_cursor;
3324044
        Ok(())
3778992
    }
18304
    pub fn has_unclosed_line_comment(&self) -> bool {
18304
        self.src().is_empty() && self.cursor.last_ws_len == WS_CURSOR_UNCLOSED_LINE
18304
    }
8914
    pub fn byte_string(&mut self) -> Result<ParsedByteStr<'a>> {
16
        fn expected_byte_string_found_base64(
16
            base64_str: &ParsedStr,
16
            byte_str: &ParsedByteStr,
16
        ) -> Error {
16
            let byte_str = match &byte_str {
16
                ParsedByteStr::Allocated(b) => b.as_slice(),
                ParsedByteStr::Slice(b) => b,
            }
16
            .iter()
120
            .flat_map(|c| core::ascii::escape_default(*c))
16
            .map(char::from)
16
            .collect::<String>();
16
            let base64_str = match &base64_str {
                ParsedStr::Allocated(s) => s.as_str(),
16
                ParsedStr::Slice(s) => s,
            };
16
            Error::InvalidValueForType {
16
                expected: format!("the Rusty byte string b\"{}\"", byte_str),
16
                found: format!("the ambiguous base64 string {:?}", base64_str),
16
            }
16
        }
        // FIXME @juntyr: remove in v0.13, since only byte_string_no_base64 will
        //                be used
8914
        if self.consume_char('"') {
8
            let base64_str = self.escaped_string()?;
8
            let base64_result = ParsedByteStr::try_from_base64(&base64_str);
8
            match base64_result {
8
                Some(byte_str) => Err(expected_byte_string_found_base64(&base64_str, &byte_str)),
                None => Err(Error::ExpectedByteString),
            }
8906
        } else if self.consume_char('r') {
12
            let base64_str = self.raw_string()?;
12
            let base64_result = ParsedByteStr::try_from_base64(&base64_str);
12
            match base64_result {
8
                Some(byte_str) => Err(expected_byte_string_found_base64(&base64_str, &byte_str)),
4
                None => Err(Error::ExpectedByteString),
            }
        } else {
8894
            self.byte_string_no_base64()
        }
8914
    }
28101
    pub fn byte_string_no_base64(&mut self) -> Result<ParsedByteStr<'a>> {
28101
        if self.consume_str("b\"") {
6034
            self.escaped_byte_string()
22067
        } else if self.consume_str("br") {
3718
            self.raw_byte_string()
        } else {
18349
            Err(Error::ExpectedByteString)
        }
28101
    }
6034
    fn escaped_byte_string(&mut self) -> Result<ParsedByteStr<'a>> {
6034
        match self.escaped_byte_buf(EscapeEncoding::Binary) {
5462
            Ok((bytes, advance)) => {
5462
                self.advance_bytes(advance);
5462
                Ok(bytes)
            }
572
            Err(err) => Err(err),
        }
6034
    }
3718
    fn raw_byte_string(&mut self) -> Result<ParsedByteStr<'a>> {
3718
        match self.raw_byte_buf() {
3146
            Ok((bytes, advance)) => {
3146
                self.advance_bytes(advance);
3146
                Ok(bytes)
            }
286
            Err(Error::ExpectedString) => Err(Error::ExpectedByteString),
286
            Err(err) => Err(err),
        }
3718
    }
109127
    pub fn string(&mut self) -> Result<ParsedStr<'a>> {
109127
        if self.consume_char('"') {
86138
            self.escaped_string()
22989
        } else if self.consume_char('r') {
3198
            self.raw_string()
        } else {
19791
            Err(Error::ExpectedString)
        }
109127
    }
86146
    fn escaped_string(&mut self) -> Result<ParsedStr<'a>> {
86146
        match self.escaped_byte_buf(EscapeEncoding::Utf8) {
84129
            Ok((bytes, advance)) => {
84129
                let string = ParsedStr::try_from_bytes(bytes).map_err(Error::from)?;
84129
                self.advance_bytes(advance);
84129
                Ok(string)
            }
2017
            Err(err) => Err(err),
        }
86146
    }
3210
    fn raw_string(&mut self) -> Result<ParsedStr<'a>> {
3210
        match self.raw_byte_buf() {
2634
            Ok((bytes, advance)) => {
2634
                let string = ParsedStr::try_from_bytes(bytes).map_err(Error::from)?;
2634
                self.advance_bytes(advance);
2634
                Ok(string)
            }
576
            Err(err) => Err(err),
        }
3210
    }
92180
    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
92180
        let str_end = self.src().find('"').ok_or(Error::ExpectedStringEnd)?;
91593
        let escape = self.src()[..str_end].find('\\');
91593
        if let Some(escape) = escape {
            // Now check if escaping is used inside the string
10908
            let mut i = escape;
10908
            let mut s = self.src().as_bytes()[..i].to_vec();
            loop {
23858
                self.advance_bytes(i + 1);
23858
                match self.parse_escape(encoding, false)? {
10694
                    EscapeCharacter::Ascii(c) => s.push(c),
11162
                    EscapeCharacter::Utf8(c) => match c.len_utf8() {
10582
                        1 => s.push(c as u8),
580
                        len => {
580
                            let start = s.len();
580
                            s.extend(core::iter::repeat(0).take(len));
580
                            c.encode_utf8(&mut s[start..]);
580
                        }
                    },
                }
                // Checking for '"' and '\\' separately is faster than searching for both at the same time
21856
                let new_str_end = self.src().find('"').ok_or(Error::ExpectedStringEnd)?;
21856
                let new_escape = self.src()[..new_str_end].find('\\');
21856
                if let Some(new_escape) = new_escape {
12950
                    s.extend_from_slice(&self.src().as_bytes()[..new_escape]);
12950
                    i = new_escape;
12950
                } else {
8906
                    s.extend_from_slice(&self.src().as_bytes()[..new_str_end]);
                    // Advance to the end of the string + 1 for the `"`.
8906
                    break Ok((ParsedByteStr::Allocated(s), new_str_end + 1));
                }
            }
        } else {
80685
            let s = &self.src().as_bytes()[..str_end];
            // Advance by the number of bytes of the string + 1 for the `"`.
80685
            Ok((ParsedByteStr::Slice(s), str_end + 1))
        }
92180
    }
6928
    fn raw_byte_buf(&mut self) -> Result<(ParsedByteStr<'a>, usize)> {
13844
        let num_hashes = self.next_chars_while_len(|c| c == '#');
6928
        let hashes = &self.src()[..num_hashes];
6928
        self.advance_bytes(num_hashes);
6928
        self.expect_char('"', Error::ExpectedString)?;
6352
        let ending = ["\"", hashes].concat();
6352
        let i = self.src().find(&ending).ok_or(Error::ExpectedStringEnd)?;
5780
        let s = &self.src().as_bytes()[..i];
        // Advance by the number of bytes of the byte string
        // + `num_hashes` + 1 for the `"`.
5780
        Ok((ParsedByteStr::Slice(s), i + num_hashes + 1))
6928
    }
202326
    fn decode_ascii_escape(&mut self) -> Result<u8> {
202326
        let mut n = 0;
202326
        for _ in 0..2 {
404652
            n <<= 4;
404652
            let byte = self.next_char()?;
404652
            let decoded = Self::decode_hex(byte)?;
404080
            n |= decoded;
        }
201754
        Ok(n)
202326
    }
    #[inline]
986842
    fn decode_hex(c: char) -> Result<u8> {
986842
        if !c.is_ascii() {
286
            return Err(Error::InvalidEscape("Non-hex digit found"));
986556
        }
        // c is an ASCII character that can be losslessly cast to u8
986556
        match c as u8 {
986270
            c @ b'0'..=b'9' => Ok(c - b'0'),
113320
            c @ b'a'..=b'f' => Ok(10 + c - b'a'),
58344
            c @ b'A'..=b'F' => Ok(10 + c - b'A'),
286
            _ => Err(Error::InvalidEscape("Non-hex digit found")),
        }
986842
    }
222354
    fn parse_escape(&mut self, encoding: EscapeEncoding, is_char: bool) -> Result<EscapeCharacter> {
222354
        let c = match self.next_char()? {
870
            '\'' => EscapeCharacter::Ascii(b'\''),
3456
            '"' => EscapeCharacter::Ascii(b'"'),
2574
            '\\' => EscapeCharacter::Ascii(b'\\'),
1430
            'n' => EscapeCharacter::Ascii(b'\n'),
572
            'r' => EscapeCharacter::Ascii(b'\r'),
572
            't' => EscapeCharacter::Ascii(b'\t'),
1716
            '0' => EscapeCharacter::Ascii(b'\0'),
            'x' => {
                // Fast exit for ascii escape in byte string
199152
                let b: u8 = self.decode_ascii_escape()?;
198580
                if let EscapeEncoding::Binary = encoding {
195712
                    return Ok(EscapeCharacter::Ascii(b));
2868
                }
                // Fast exit for ascii character in UTF-8 string
2868
                let mut bytes = [b, 0, 0, 0];
2868
                if let Ok(Some(c)) = from_utf8(&bytes[..=0]).map(|s| s.chars().next()) {
858
                    return Ok(EscapeCharacter::Utf8(c));
2010
                }
2010
                if is_char {
                    // Character literals are not allowed to use multiple byte
                    //  escapes to build a unicode character
286
                    return Err(Error::InvalidEscape(
286
                        "Not a valid byte-escaped Unicode character",
286
                    ));
1724
                }
                // UTF-8 character needs up to four bytes and we have already
                //  consumed one, so at most three to go
4314
                for i in 1..4 {
4314
                    if !self.consume_str(r"\x") {
1144
                        return Err(Error::InvalidEscape(
1144
                            "Not a valid byte-escaped Unicode character",
1144
                        ));
3170
                    }
3170
                    bytes[i] = self.decode_ascii_escape()?;
                    // Check if we now have a valid UTF-8 character
3170
                    if let Ok(Some(c)) = from_utf8(&bytes[..=i]).map(|s| s.chars().next()) {
294
                        return Ok(EscapeCharacter::Utf8(c));
2876
                    }
                }
286
                return Err(Error::InvalidEscape(
286
                    "Not a valid byte-escaped Unicode character",
286
                ));
            }
            'u' => {
11726
                self.expect_char('{', Error::InvalidEscape("Missing { in Unicode escape"))?;
11726
                let mut bytes: u32 = 0;
11726
                let mut num_digits = 0;
31174
                while num_digits < 6 {
31174
                    let byte = self.peek_char_or_eof()?;
31174
                    if byte == '}' {
11726
                        break;
19448
                    }
19448
                    self.skip_next_char();
19448
                    num_digits += 1;
19448
                    let byte = Self::decode_hex(byte)?;
19448
                    bytes <<= 4;
19448
                    bytes |= u32::from(byte);
                }
11726
                if num_digits == 0 {
286
                    return Err(Error::InvalidEscape(
286
                        "Expected 1-6 digits, got 0 digits in Unicode escape",
286
                    ));
11440
                }
11440
                self.expect_char(
                    '}',
11440
                    Error::InvalidEscape("No } at the end of Unicode escape"),
                )?;
11440
                let c = char_from_u32(bytes).ok_or(Error::InvalidEscape(
11440
                    "Not a valid Unicode-escaped character",
11440
                ))?;
11440
                EscapeCharacter::Utf8(c)
            }
286
            _ => return Err(Error::InvalidEscape("Unknown escape character")),
        };
22630
        Ok(c)
222354
    }
3354950
    fn skip_comment(&mut self) -> Result<Option<Comment>> {
3354950
        if self.consume_char('/') {
30906
            match self.next_char()? {
                '/' => {
127452
                    let bytes = self.next_chars_while_len(|c| c != '\n');
8586
                    self.advance_bytes(bytes);
8586
                    if self.src().is_empty() {
1148
                        Ok(Some(Comment::UnclosedLine))
                    } else {
7438
                        Ok(Some(Comment::ClosedLine))
                    }
                }
                '*' => {
22034
                    let mut level = 1;
50928
                    while level > 0 {
199946
                        let bytes = self.next_chars_while_len(|c| !matches!(c, '/' | '*'));
29470
                        if self.src().is_empty() {
290
                            return Err(Error::UnclosedBlockComment);
29180
                        }
29180
                        self.advance_bytes(bytes);
                        // check whether / or * and take action
29180
                        if self.consume_str("/*") {
2002
                            level += 1;
27178
                        } else if self.consume_str("*/") {
23460
                            level -= 1;
23460
                        } else {
3718
                            self.next_char().map_err(|_| Error::UnclosedBlockComment)?;
                        }
                    }
21458
                    Ok(Some(Comment::Block))
                }
286
                c => Err(Error::UnexpectedChar(c)),
            }
        } else {
3324044
            Ok(None)
        }
3354950
    }
}
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 {
1356861
            fn from_u8(x: u8) -> Self {
74962
                x as $ty
1356861
            }
563608
            fn checked_mul_ext(&mut self, x: u8) -> bool {
563608
                match self.checked_mul(Self::from_u8(x)) {
562742
                    Some(n) => {
562742
                        *self = n;
562742
                        false
                    }
866
                    None => true,
                }
563608
            }
464748
            fn checked_add_ext(&mut self, x: u8) -> bool {
464748
                match self.checked_add(Self::from_u8(x)) {
464319
                    Some(n) => {
464319
                        *self = n;
464319
                        false
                    }
429
                    None => true,
                }
464748
            }
95992
            fn checked_sub_ext(&mut self, x: u8) -> bool {
95992
                match self.checked_sub(Self::from_u8(x)) {
93132
                    Some(n) => {
93132
                        *self = n;
93132
                        false
                    }
2860
                    None => true,
                }
95992
            }
        }
    };
    ($($tys:ty)*) => {
        $( impl_num!($tys); )*
    };
}
impl_num! { i8 i16 i32 i64 u8 u16 u32 u64 }
#[cfg(feature = "integer128")]
impl_num! { i128 u128 }
pub trait Integer: Sized {
    fn parse(parser: &mut Parser, sign: i8, base: u8) -> Result<Self>;
    fn try_from_parsed_integer(parsed: ParsedInteger, ron: &str) -> Result<Self>;
}
macro_rules! impl_integer {
    ($wrap:ident($ty:ty)) => {
        impl Integer for $ty {
115137
            fn parse(parser: &mut Parser, sign: i8, base: u8) -> Result<Self> {
115137
                parser.parse_integer(sign, base)
115137
            }
34034
            fn try_from_parsed_integer(parsed: ParsedInteger, ron: &str) -> Result<Self> {
34034
                match parsed {
12584
                    ParsedInteger::$wrap(v) => Ok(v),
                    _ => Err(Error::InvalidValueForType {
21450
                        expected: format!(
                            "a{} {}-bit {}signed integer",
21450
                            if <$ty>::BITS == 8 { "n" } else { "n" },
                            <$ty>::BITS,
21450
                            if <$ty>::MIN == 0 { "un" } else { "" },
                        ),
21450
                        found: String::from(ron),
                    }),
                }
34034
            }
        }
    };
    ($($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 {
82226
    fn parse(parser: &mut Parser, sign: i8, base: u8) -> Result<Self> {
82226
        if sign < 0 {
2052
            let signed = parser.parse_integer::<LargeSInt>(-1, base)?;
598
            return if let Ok(x) = i8::try_from(signed) {
298
                Ok(ParsedInteger::I8(x))
300
            } else if let Ok(x) = i16::try_from(signed) {
4
                Ok(ParsedInteger::I16(x))
296
            } 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")]
290
                if let Ok(x) = i64::try_from(signed) {
2
                    Ok(ParsedInteger::I64(x))
                } else {
288
                    Ok(ParsedInteger::I128(signed))
                }
            };
80174
        }
80174
        let unsigned = parser.parse_integer::<LargeUInt>(1, base)?;
79447
        if let Ok(x) = u8::try_from(unsigned) {
78571
            Ok(ParsedInteger::U8(x))
876
        } else if let Ok(x) = u16::try_from(unsigned) {
294
            Ok(ParsedInteger::U16(x))
582
        } 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")]
576
            if let Ok(x) = u64::try_from(unsigned) {
2
                Ok(ParsedInteger::U64(x))
            } else {
574
                Ok(ParsedInteger::U128(unsigned))
            }
        }
82226
    }
226798
    fn try_from_parsed_integer(parsed: ParsedInteger, _ron: &str) -> Result<Self> {
226798
        Ok(parsed)
226798
    }
}
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 {
36315
            fn parse(float: &str) -> Result<Self> {
36315
                <$ty>::from_str(float).map_err(|_| Error::ExpectedFloat)
36315
            }
4004
            fn try_from_parsed_float(parsed: ParsedFloat, ron: &str) -> Result<Self> {
4004
                match parsed {
3432
                    ParsedFloat::$wrap(v) => Ok(v),
572
                    _ => Err(Error::InvalidValueForType {
572
                        expected: format!(
572
                            "a {}-bit floating point number", $bits,
572
                        ),
572
                        found: String::from(ron),
572
                    }),
                }
4004
            }
        }
    };
    ($($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 {
11834
    fn parse(float: &str) -> Result<Self> {
11834
        let value = f64::from_str(float).map_err(|_| Error::ExpectedFloat)?;
        #[allow(clippy::cast_possible_truncation)]
11834
        if value.total_cmp(&f64::from(value as f32)).is_eq() {
4652
            Ok(ParsedFloat::F32(value as f32))
        } else {
7182
            Ok(ParsedFloat::F64(value))
        }
11834
    }
2860
    fn try_from_parsed_float(parsed: ParsedFloat, _ron: &str) -> Result<Self> {
2860
        Ok(parsed)
2860
    }
}
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> {
86763
    pub fn try_from_bytes(bytes: ParsedByteStr<'a>) -> Result<Self, Utf8Error> {
86763
        match bytes {
6026
            ParsedByteStr::Allocated(byte_buf) => Ok(ParsedStr::Allocated(
6026
                String::from_utf8(byte_buf).map_err(|e| e.utf8_error())?,
            )),
80737
            ParsedByteStr::Slice(bytes) => Ok(ParsedStr::Slice(from_utf8(bytes)?)),
        }
86763
    }
}
impl<'a> ParsedByteStr<'a> {
20
    pub fn try_from_base64(str: &ParsedStr<'a>) -> Option<Self> {
        // Adapted from MIT licensed Jenin Sutradhar's base 64 decoder
        // https://github.com/JeninSutradhar/base64-Rust-Encoder-Decoder/blob/ee1fb08cbb78024ec8cf5e786815acb239169f02/src/lib.rs#L84-L128
20
        fn try_decode_base64(str: &str) -> Option<Vec<u8>> {
            const CHARSET: &[u8; 64] =
                b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
            const PADDING: u8 = b'=';
            // fast reject for missing padding
20
            if (str.len() % 4) != 0 {
4
                return None;
16
            }
16
            let bstr_no_padding = str.trim_end_matches(char::from(PADDING)).as_bytes();
            // fast reject for excessive padding
16
            if (str.len() - bstr_no_padding.len()) > 2 {
                return None;
16
            }
            // fast reject for extraneous bytes after padding
16
            if bstr_no_padding.contains(&PADDING) {
                return None;
16
            }
            // fast reject for non-ASCII
16
            if !str.is_ascii() {
                return None;
16
            }
16
            let mut collected_bits = 0_u8;
16
            let mut byte_buffer = 0_u16;
16
            let mut bytes = bstr_no_padding.iter().copied();
16
            let mut binary = Vec::new();
            'decodeloop: loop {
304
                while collected_bits < 8 {
184
                    if let Some(nextbyte) = bytes.next() {
                        #[allow(clippy::cast_possible_truncation)]
5424
                        if let Some(idx) = CHARSET.iter().position(|&x| x == nextbyte) {
168
                            byte_buffer |= ((idx & 0b0011_1111) as u16) << (10 - collected_bits);
168
                            collected_bits += 6;
168
                        } else {
                            return None;
                        }
                    } else {
16
                        break 'decodeloop;
                    }
                }
120
                binary.push(((0b1111_1111_0000_0000 & byte_buffer) >> 8) as u8);
120
                byte_buffer &= 0b0000_0000_1111_1111;
120
                byte_buffer <<= 8;
120
                collected_bits -= 8;
            }
16
            if usize::from(collected_bits) != ((str.len() - bstr_no_padding.len()) * 2) {
                return None;
16
            }
16
            Some(binary)
20
        }
20
        let base64_str = match str {
            ParsedStr::Allocated(string) => string.as_str(),
20
            ParsedStr::Slice(str) => str,
        };
20
        try_decode_base64(base64_str).map(ParsedByteStr::Allocated)
20
    }
}
#[derive(Copy, Clone)] // GRCOV_EXCL_LINE
enum EscapeEncoding {
    Binary,
    Utf8,
}
enum EscapeCharacter {
    Ascii(u8),
    Utf8(char),
}
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
4
    fn decode_x10() {
4
        let mut bytes = Parser::new("10").unwrap();
4
        assert_eq!(bytes.decode_ascii_escape(), Ok(b'\x10'));
4
    }
    #[test]
4
    fn track_prior_ws() {
        const SOURCE: &str = "   /*hey*/ 42       /*bye*/ 24  ";
4
        let mut bytes = Parser::new(SOURCE).unwrap();
4
        assert_eq!(bytes.src(), "42       /*bye*/ 24  ");
4
        assert_eq!(bytes.pre_ws_src(), SOURCE);
4
        bytes.skip_ws().unwrap();
4
        assert_eq!(bytes.src(), "42       /*bye*/ 24  ");
4
        assert_eq!(bytes.pre_ws_src(), SOURCE);
4
        assert_eq!(bytes.integer::<u8>().unwrap(), 42);
4
        assert_eq!(bytes.src(), "       /*bye*/ 24  ");
4
        assert_eq!(bytes.pre_ws_src(), SOURCE);
4
        bytes.skip_ws().unwrap();
4
        bytes.skip_ws().unwrap();
4
        assert_eq!(bytes.src(), "24  ");
4
        assert_eq!(bytes.pre_ws_src(), "       /*bye*/ 24  ");
4
        let mut bytes = Parser::new("42").unwrap();
4
        bytes.skip_ws().unwrap();
4
        bytes.skip_ws().unwrap();
4
        assert_eq!(bytes.src(), "42");
4
        assert_eq!(bytes.pre_ws_src(), "42");
4
        assert_eq!(bytes.integer::<u8>().unwrap(), 42);
4
        bytes.skip_ws().unwrap();
4
        bytes.skip_ws().unwrap();
4
        assert_eq!(bytes.src(), "");
4
        assert_eq!(bytes.pre_ws_src(), "");
4
        let mut bytes = Parser::new("  42  ").unwrap();
4
        bytes.skip_ws().unwrap();
4
        bytes.skip_ws().unwrap();
4
        assert_eq!(bytes.src(), "42  ");
4
        assert_eq!(bytes.pre_ws_src(), "  42  ");
4
        assert_eq!(bytes.integer::<u8>().unwrap(), 42);
4
        bytes.skip_ws().unwrap();
4
        bytes.skip_ws().unwrap();
4
        assert_eq!(bytes.src(), "");
4
        assert_eq!(bytes.pre_ws_src(), "  ");
4
        let mut bytes = Parser::new("  42  //").unwrap();
4
        bytes.skip_ws().unwrap();
4
        bytes.skip_ws().unwrap();
4
        assert_eq!(bytes.src(), "42  //");
4
        assert_eq!(bytes.pre_ws_src(), "  42  //");
4
        assert_eq!(bytes.integer::<u8>().unwrap(), 42);
4
        bytes.skip_ws().unwrap();
4
        bytes.skip_ws().unwrap();
4
        assert_eq!(bytes.src(), "");
4
        assert_eq!(bytes.pre_ws_src(), "  //");
4
    }
    #[test]
4
    fn parser_cursor_eq_cmp() {
4
        assert!(
4
            ParserCursor {
4
                cursor: 42,
4
                pre_ws_cursor: 42,
4
                last_ws_len: 42
4
            } == ParserCursor {
4
                cursor: 42,
4
                pre_ws_cursor: 24,
4
                last_ws_len: 24
4
            }
        );
4
        assert!(
4
            ParserCursor {
4
                cursor: 42,
4
                pre_ws_cursor: 42,
4
                last_ws_len: 42
4
            } != ParserCursor {
4
                cursor: 24,
4
                pre_ws_cursor: 42,
4
                last_ws_len: 42
4
            }
        );
4
        assert!(
4
            ParserCursor {
4
                cursor: 42,
4
                pre_ws_cursor: 42,
4
                last_ws_len: 42
4
            } < ParserCursor {
4
                cursor: 43,
4
                pre_ws_cursor: 24,
4
                last_ws_len: 24
4
            }
        );
4
        assert!(
4
            ParserCursor {
4
                cursor: 42,
4
                pre_ws_cursor: 42,
4
                last_ws_len: 42
4
            } > ParserCursor {
4
                cursor: 41,
4
                pre_ws_cursor: 24,
4
                last_ws_len: 24
4
            }
        );
4
    }
    #[test]
4
    fn empty_src_is_not_a_float() {
4
        assert!(!Parser::new("").unwrap().next_bytes_is_float());
4
    }
    #[test]
4
    fn base64_deprecation_error() {
4
        let err = crate::from_str::<bytes::Bytes>("\"SGVsbG8gcm9uIQ==\"").unwrap_err();
4
        assert_eq!(
            err,
4
            SpannedError {
4
                code: Error::InvalidValueForType {
4
                    expected: String::from("the Rusty byte string b\"Hello ron!\""),
4
                    found: String::from("the ambiguous base64 string \"SGVsbG8gcm9uIQ==\"")
4
                },
4
                span: Span {
4
                    start: Position { line: 1, col: 2 },
4
                    end: Position { line: 1, col: 19 },
4
                }
4
            }
        );
4
        let err = crate::from_str::<bytes::Bytes>("r\"SGVsbG8gcm9uIQ==\"").unwrap_err();
4
        assert_eq!(format!("{}", err.code), "Expected the Rusty byte string b\"Hello ron!\" but found the ambiguous base64 string \"SGVsbG8gcm9uIQ==\" instead");
4
        assert_eq!(
4
            crate::from_str::<bytes::Bytes>("\"invalid=\"").unwrap_err(),
4
            SpannedError {
4
                code: Error::InvalidValueForType {
4
                    expected: String::from("the Rusty byte string b\"\\x8a{\\xda\\x96\\'\""),
4
                    found: String::from("the ambiguous base64 string \"invalid=\"")
4
                },
4
                span: Span {
4
                    start: Position { line: 1, col: 2 },
4
                    end: Position { line: 1, col: 11 },
4
                }
4
            }
        );
4
        assert_eq!(
4
            crate::from_str::<bytes::Bytes>("r\"invalid=\"").unwrap_err(),
4
            SpannedError {
4
                code: Error::InvalidValueForType {
4
                    expected: String::from("the Rusty byte string b\"\\x8a{\\xda\\x96\\'\""),
4
                    found: String::from("the ambiguous base64 string \"invalid=\"")
4
                },
4
                span: Span {
4
                    start: Position { line: 1, col: 3 },
4
                    end: Position { line: 1, col: 12 },
4
                }
4
            }
        );
4
        assert_eq!(
4
            crate::from_str::<bytes::Bytes>("r\"invalid\"").unwrap_err(),
            SpannedError {
                code: Error::ExpectedByteString,
                span: Span {
                    start: Position { line: 1, col: 3 },
                    end: Position { line: 1, col: 11 },
                }
            }
        );
4
    }
}