ron_pfnsec_fork/
parse.rs

1#![allow(clippy::identity_op)]
2
3use alloc::{
4    format,
5    string::{String, ToString},
6    vec::Vec,
7};
8use core::{
9    char::from_u32 as char_from_u32,
10    str::{self, from_utf8, FromStr, Utf8Error},
11};
12
13use unicode_ident::{is_xid_continue, is_xid_start};
14
15use crate::{
16    error::{Error, Position, Result, Span, SpannedError, SpannedResult},
17    extensions::Extensions,
18    value::Number,
19};
20
21const fn is_int_char(c: char) -> bool {
22    c.is_ascii_hexdigit() || c == '_'
23}
24
25const fn is_float_char(c: char) -> bool {
26    c.is_ascii_digit() || matches!(c, 'e' | 'E' | '.' | '+' | '-' | '_')
27}
28
29pub fn is_ident_first_char(c: char) -> bool {
30    c == '_' || is_xid_start(c)
31}
32
33pub fn is_ident_raw_char(c: char) -> bool {
34    matches!(c, '.' | '+' | '-') | is_xid_continue(c)
35}
36
37pub const fn is_whitespace_char(c: char) -> bool {
38    matches!(
39        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}
52
53#[cfg(feature = "integer128")]
54pub(crate) type LargeUInt = u128;
55#[cfg(not(feature = "integer128"))]
56pub(crate) type LargeUInt = u64;
57#[cfg(feature = "integer128")]
58pub(crate) type LargeSInt = i128;
59#[cfg(not(feature = "integer128"))]
60pub(crate) type LargeSInt = i64;
61
62pub 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
71pub struct ParserCursor {
72    cursor: usize,
73    pre_ws_cursor: usize,
74    last_ws_len: usize,
75}
76
77const WS_CURSOR_UNCLOSED_LINE: usize = usize::MAX;
78
79impl PartialEq for ParserCursor {
80    fn eq(&self, other: &Self) -> bool {
81        self.cursor == other.cursor
82    }
83}
84
85impl PartialOrd for ParserCursor {
86    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
87        self.cursor.partial_cmp(&other.cursor)
88    }
89}
90
91/// constructor and parsing utilities
92impl<'a> Parser<'a> {
93    pub fn new(src: &'a str) -> SpannedResult<Self> {
94        let mut parser = Parser {
95            exts: Extensions::empty(),
96            src,
97            cursor: ParserCursor {
98                cursor: 0,
99                pre_ws_cursor: 0,
100                last_ws_len: 0,
101            },
102            prev_cursor: ParserCursor {
103                cursor: 0,
104                pre_ws_cursor: 0,
105                last_ws_len: 0,
106            },
107        };
108
109        parser.skip_ws().map_err(|e| parser.span_error(e))?;
110
111        // Loop over all extensions attributes
112        loop {
113            let attribute = parser.extensions().map_err(|e| parser.span_error(e))?;
114
115            if attribute.is_empty() {
116                break;
117            }
118
119            parser.exts |= attribute;
120            parser.skip_ws().map_err(|e| parser.span_error(e))?;
121        }
122
123        Ok(parser)
124    }
125
126    fn set_cursor(&mut self, cursor: ParserCursor) {
127        self.cursor = cursor;
128    }
129
130    pub fn span_error(&self, code: Error) -> SpannedError {
131        SpannedError {
132            code,
133            span: Span {
134                start: Position::from_src_end(&self.src[..self.prev_cursor.cursor]),
135                end: Position::from_src_end(&self.src[..self.cursor.cursor]),
136            },
137        }
138    }
139
140    pub fn advance_bytes(&mut self, bytes: usize) {
141        self.prev_cursor = self.cursor;
142        self.cursor.cursor += bytes;
143    }
144
145    pub fn next_char(&mut self) -> Result<char> {
146        let c = self.peek_char_or_eof()?;
147        self.cursor.cursor += c.len_utf8();
148        Ok(c)
149    }
150
151    pub fn skip_next_char(&mut self) {
152        core::mem::drop(self.next_char());
153    }
154
155    pub fn peek_char(&self) -> Option<char> {
156        self.src().chars().next()
157    }
158
159    pub fn peek_char_or_eof(&self) -> Result<char> {
160        self.peek_char().ok_or(Error::Eof)
161    }
162
163    pub fn check_char(&self, c: char) -> bool {
164        self.src().starts_with(c)
165    }
166
167    pub fn check_str(&self, s: &str) -> bool {
168        self.src().starts_with(s)
169    }
170
171    pub fn src(&self) -> &'a str {
172        &self.src[self.cursor.cursor..]
173    }
174
175    pub fn pre_ws_src(&self) -> &'a str {
176        &self.src[self.cursor.pre_ws_cursor..]
177    }
178
179    pub fn consume_str(&mut self, s: &str) -> bool {
180        if self.check_str(s) {
181            self.advance_bytes(s.len());
182
183            true
184        } else {
185            false
186        }
187    }
188
189    pub fn consume_char(&mut self, c: char) -> bool {
190        if self.check_char(c) {
191            self.advance_bytes(c.len_utf8());
192
193            true
194        } else {
195            false
196        }
197    }
198
199    fn consume_all(&mut self, all: &[&str]) -> Result<bool> {
200        all.iter()
201            .map(|elem| {
202                if self.consume_str(elem) {
203                    self.skip_ws()?;
204
205                    Ok(true)
206                } else {
207                    Ok(false)
208                }
209            })
210            .try_fold(true, |acc, x| x.map(|x| x && acc))
211    }
212
213    pub fn expect_char(&mut self, expected: char, error: Error) -> Result<()> {
214        if self.consume_char(expected) {
215            Ok(())
216        } else {
217            Err(error)
218        }
219    }
220
221    #[must_use]
222    pub fn next_chars_while_len(&self, condition: fn(char) -> bool) -> usize {
223        self.next_chars_while_from_len(0, condition)
224    }
225
226    #[must_use]
227    pub fn next_chars_while_from_len(&self, from: usize, condition: fn(char) -> bool) -> usize {
228        self.src()[from..]
229            .find(|c| !condition(c))
230            .unwrap_or(self.src().len() - from)
231    }
232}
233
234/// actual parsing of ron tokens
235impl<'a> Parser<'a> {
236    fn parse_integer_digits<T: Num>(
237        &mut self,
238        s: &str,
239        base: u8,
240        f: fn(&mut T, u8) -> bool,
241    ) -> Result<T> {
242        let mut num_acc = T::from_u8(0);
243
244        for (i, c) in s.char_indices() {
245            if c == '_' {
246                continue;
247            }
248
249            if num_acc.checked_mul_ext(base) {
250                self.advance_bytes(s.len());
251                return Err(Error::IntegerOutOfBounds);
252            }
253
254            let digit = Self::decode_hex(c)?;
255
256            if digit >= base {
257                self.advance_bytes(i);
258                return Err(Error::InvalidIntegerDigit { digit: c, base });
259            }
260
261            if f(&mut num_acc, digit) {
262                self.advance_bytes(s.len());
263                return Err(Error::IntegerOutOfBounds);
264            }
265        }
266
267        self.advance_bytes(s.len());
268
269        Ok(num_acc)
270    }
271
272    fn parse_integer<T: Num>(&mut self, sign: i8) -> Result<T> {
273        let base = match () {
274            () if self.consume_str("0b") => 2,
275            () if self.consume_str("0o") => 8,
276            () if self.consume_str("0x") => 16,
277            () => 10,
278        };
279
280        let num_bytes = self.next_chars_while_len(is_int_char);
281
282        if num_bytes == 0 {
283            return Err(Error::ExpectedInteger);
284        }
285
286        if self.check_char('_') {
287            return Err(Error::UnderscoreAtBeginning);
288        }
289
290        let s = &self.src()[..num_bytes];
291
292        if sign > 0 {
293            self.parse_integer_digits(s, base, T::checked_add_ext)
294        } else {
295            self.parse_integer_digits(s, base, T::checked_sub_ext)
296        }
297    }
298
299    #[allow(clippy::too_many_lines)]
300    pub fn integer<T: Integer>(&mut self) -> Result<T> {
301        let src_backup = self.src();
302
303        let is_negative = match self.peek_char_or_eof()? {
304            '+' => {
305                self.skip_next_char();
306                false
307            }
308            '-' => {
309                self.skip_next_char();
310                true
311            }
312            'b' if self.consume_str("b'") => {
313                // Parse a byte literal
314                let byte = match self.next_char()? {
315                    '\\' => match self.parse_escape(EscapeEncoding::Binary, true)? {
316                        // we know that this byte is an ASCII character
317                        EscapeCharacter::Ascii(b) => b,
318                        EscapeCharacter::Utf8(_) => {
319                            return Err(Error::InvalidEscape(
320                                "Unexpected Unicode escape in byte literal",
321                            ))
322                        }
323                    },
324                    b if b.is_ascii() => b as u8,
325                    _ => return Err(Error::ExpectedByteLiteral),
326                };
327
328                if !self.consume_char('\'') {
329                    return Err(Error::ExpectedByteLiteral);
330                }
331
332                let bytes_ron = &src_backup[..src_backup.len() - self.src().len()];
333
334                return T::try_from_parsed_integer(ParsedInteger::U8(byte), bytes_ron);
335            }
336            _ => false,
337        };
338        let sign = if is_negative { -1 } else { 1 };
339
340        let num_bytes = self.next_chars_while_len(is_int_char);
341
342        if self.src()[num_bytes..].starts_with(['i', 'u']) {
343            let int_cursor = self.cursor;
344            self.advance_bytes(num_bytes);
345
346            #[allow(clippy::never_loop)]
347            loop {
348                let (res, suffix_bytes) = if self.consume_ident("i8") {
349                    let suffix_bytes = self.src();
350                    self.set_cursor(int_cursor);
351                    (
352                        self.parse_integer::<i8>(sign).map(ParsedInteger::I8),
353                        suffix_bytes,
354                    )
355                } else if self.consume_ident("i16") {
356                    let suffix_bytes = self.src();
357                    self.set_cursor(int_cursor);
358                    (
359                        self.parse_integer::<i16>(sign).map(ParsedInteger::I16),
360                        suffix_bytes,
361                    )
362                } else if self.consume_ident("i32") {
363                    let suffix_bytes = self.src();
364                    self.set_cursor(int_cursor);
365                    (
366                        self.parse_integer::<i32>(sign).map(ParsedInteger::I32),
367                        suffix_bytes,
368                    )
369                } else if self.consume_ident("i64") {
370                    let suffix_bytes = self.src();
371                    self.set_cursor(int_cursor);
372                    (
373                        self.parse_integer::<i64>(sign).map(ParsedInteger::I64),
374                        suffix_bytes,
375                    )
376                } else if self.consume_ident("u8") {
377                    let suffix_bytes = self.src();
378                    self.set_cursor(int_cursor);
379                    (
380                        self.parse_integer::<u8>(sign).map(ParsedInteger::U8),
381                        suffix_bytes,
382                    )
383                } else if self.consume_ident("u16") {
384                    let suffix_bytes = self.src();
385                    self.set_cursor(int_cursor);
386                    (
387                        self.parse_integer::<u16>(sign).map(ParsedInteger::U16),
388                        suffix_bytes,
389                    )
390                } else if self.consume_ident("u32") {
391                    let suffix_bytes = self.src();
392                    self.set_cursor(int_cursor);
393                    (
394                        self.parse_integer::<u32>(sign).map(ParsedInteger::U32),
395                        suffix_bytes,
396                    )
397                } else if self.consume_ident("u64") {
398                    let suffix_bytes = self.src();
399                    self.set_cursor(int_cursor);
400                    (
401                        self.parse_integer::<u64>(sign).map(ParsedInteger::U64),
402                        suffix_bytes,
403                    )
404                } else {
405                    #[cfg(feature = "integer128")]
406                    if self.consume_ident("i128") {
407                        let suffix_bytes = self.src();
408                        self.set_cursor(int_cursor);
409                        (
410                            self.parse_integer::<i128>(sign).map(ParsedInteger::I128),
411                            suffix_bytes,
412                        )
413                    } else if self.consume_ident("u128") {
414                        let suffix_bytes = self.src();
415                        self.set_cursor(int_cursor);
416                        (
417                            self.parse_integer::<u128>(sign).map(ParsedInteger::U128),
418                            suffix_bytes,
419                        )
420                    } else {
421                        break;
422                    }
423                    #[cfg(not(feature = "integer128"))]
424                    {
425                        break;
426                    }
427                };
428
429                if !matches!(
430                    &res,
431                    Err(Error::UnderscoreAtBeginning | Error::InvalidIntegerDigit { .. })
432                ) {
433                    // Advance past the number suffix
434                    self.skip_identifier();
435                }
436
437                let integer_ron = &src_backup[..src_backup.len() - suffix_bytes.len()];
438
439                return res.and_then(|parsed| T::try_from_parsed_integer(parsed, integer_ron));
440            }
441
442            self.set_cursor(int_cursor);
443        }
444
445        T::parse(self, sign)
446    }
447
448    pub fn any_number(&mut self) -> Result<Number> {
449        if self.next_bytes_is_float() {
450            return match self.float::<ParsedFloat>()? {
451                ParsedFloat::F32(v) => Ok(Number::F32(v.into())),
452                ParsedFloat::F64(v) => Ok(Number::F64(v.into())),
453            };
454        }
455
456        let backup_cursor = self.cursor;
457
458        let (integer_err, integer_cursor) = match self.integer::<ParsedInteger>() {
459            Ok(integer) => {
460                return match integer {
461                    ParsedInteger::I8(v) => Ok(Number::I8(v)),
462                    ParsedInteger::I16(v) => Ok(Number::I16(v)),
463                    ParsedInteger::I32(v) => Ok(Number::I32(v)),
464                    ParsedInteger::I64(v) => Ok(Number::I64(v)),
465                    #[cfg(feature = "integer128")]
466                    ParsedInteger::I128(v) => Ok(Number::I128(v)),
467                    ParsedInteger::U8(v) => Ok(Number::U8(v)),
468                    ParsedInteger::U16(v) => Ok(Number::U16(v)),
469                    ParsedInteger::U32(v) => Ok(Number::U32(v)),
470                    ParsedInteger::U64(v) => Ok(Number::U64(v)),
471                    #[cfg(feature = "integer128")]
472                    ParsedInteger::U128(v) => Ok(Number::U128(v)),
473                }
474            }
475            Err(err) => (err, self.cursor),
476        };
477
478        self.set_cursor(backup_cursor);
479
480        // Fall-back to parse an out-of-range integer as a float
481        match self.float::<ParsedFloat>() {
482            Ok(ParsedFloat::F32(v)) if self.cursor >= integer_cursor => Ok(Number::F32(v.into())),
483            Ok(ParsedFloat::F64(v)) if self.cursor >= integer_cursor => Ok(Number::F64(v.into())),
484            _ => {
485                // Return the more precise integer error
486                self.set_cursor(integer_cursor);
487                Err(integer_err)
488            }
489        }
490    }
491
492    pub fn bool(&mut self) -> Result<bool> {
493        if self.consume_ident("true") {
494            Ok(true)
495        } else if self.consume_ident("false") {
496            Ok(false)
497        } else {
498            Err(Error::ExpectedBoolean)
499        }
500    }
501
502    pub fn char(&mut self) -> Result<char> {
503        self.expect_char('\'', Error::ExpectedChar)?;
504
505        let c = self.next_char()?;
506
507        let c = if c == '\\' {
508            match self.parse_escape(EscapeEncoding::Utf8, true)? {
509                // we know that this byte is an ASCII character
510                EscapeCharacter::Ascii(b) => char::from(b),
511                EscapeCharacter::Utf8(c) => c,
512            }
513        } else {
514            c
515        };
516
517        self.expect_char('\'', Error::ExpectedChar)?;
518
519        Ok(c)
520    }
521
522    pub fn comma(&mut self) -> Result<bool> {
523        self.skip_ws()?;
524
525        if self.consume_char(',') {
526            self.skip_ws()?;
527
528            Ok(true)
529        } else {
530            Ok(false)
531        }
532    }
533
534    /// Only returns true if the char after `ident` cannot belong
535    /// to an identifier.
536    pub fn check_ident(&mut self, ident: &str) -> bool {
537        self.check_str(ident) && !self.check_ident_other_char(ident.len())
538    }
539
540    fn check_ident_other_char(&self, index: usize) -> bool {
541        self.src()[index..]
542            .chars()
543            .next()
544            .map_or(false, is_xid_continue)
545    }
546
547    /// Check which type of struct we are currently parsing. The parsing state
548    ///  is only changed in case of an error, to provide a better position.
549    ///
550    /// [`NewtypeMode::NoParensMeanUnit`] detects (tuple) structs by a leading
551    ///  opening bracket and reports a unit struct otherwise.
552    /// [`NewtypeMode::InsideNewtype`] skips an initial check for unit structs,
553    ///  and means that any leading opening bracket is not considered to open
554    ///  a (tuple) struct but to be part of the structs inner contents.
555    ///
556    /// [`TupleMode::ImpreciseTupleOrNewtype`] only performs a cheap, O(1),
557    ///  single-identifier lookahead check to distinguish tuple structs from
558    ///  non-tuple structs.
559    /// [`TupleMode::DifferentiateNewtype`] performs an expensive, O(N), look-
560    ///  ahead over the entire next value tree, which can span the entirety of
561    ///  the remaining document in the worst case.
562    pub fn check_struct_type(
563        &mut self,
564        newtype: NewtypeMode,
565        tuple: TupleMode,
566    ) -> Result<StructType> {
567        fn check_struct_type_inner(
568            parser: &mut Parser,
569            newtype: NewtypeMode,
570            tuple: TupleMode,
571        ) -> Result<StructType> {
572            if matches!(newtype, NewtypeMode::NoParensMeanUnit) && !parser.consume_char('(') {
573                return Ok(StructType::Unit);
574            }
575
576            parser.skip_ws()?;
577
578            // Check for `Ident()`, which could be
579            // - a zero-field struct or tuple (variant)
580            // - an unwrapped newtype around a unit
581            if matches!(newtype, NewtypeMode::NoParensMeanUnit) && parser.check_char(')') {
582                return Ok(StructType::EmptyTuple);
583            }
584
585            if parser.skip_identifier().is_some() {
586                parser.skip_ws()?;
587
588                match parser.peek_char() {
589                    // Definitely a struct with named fields
590                    Some(':') => return Ok(StructType::Named),
591                    // Definitely a tuple-like struct with fields
592                    Some(',') => {
593                        parser.skip_next_char();
594                        parser.skip_ws()?;
595                        if parser.check_char(')') {
596                            // A one-element tuple could be a newtype
597                            return Ok(StructType::NewtypeTuple);
598                        }
599                        // Definitely a tuple struct with more than one field
600                        return Ok(StructType::NonNewtypeTuple);
601                    }
602                    // Either a newtype or a tuple struct
603                    Some(')') => return Ok(StructType::NewtypeTuple),
604                    // Something else, let's investigate further
605                    Some(_) | None => (),
606                };
607            }
608
609            if matches!(tuple, TupleMode::ImpreciseTupleOrNewtype) {
610                return Ok(StructType::AnyTuple);
611            }
612
613            let mut braces = 1_usize;
614            let mut more_than_one = false;
615
616            // Skip ahead to see if the value is followed by another value
617            while braces > 0 {
618                // Skip spurious braces in comments, strings, and characters
619                parser.skip_ws()?;
620                let cursor_backup = parser.cursor;
621                if parser.char().is_err() {
622                    parser.set_cursor(cursor_backup);
623                }
624                let cursor_backup = parser.cursor;
625                match parser.string() {
626                    Ok(_) => (),
627                    // prevent quadratic complexity backtracking for unterminated string
628                    Err(err @ (Error::ExpectedStringEnd | Error::Eof)) => return Err(err),
629                    Err(_) => parser.set_cursor(cursor_backup),
630                }
631                let cursor_backup = parser.cursor;
632                // we have already checked for strings, which subsume base64 byte strings
633                match parser.byte_string_no_base64() {
634                    Ok(_) => (),
635                    // prevent quadratic complexity backtracking for unterminated byte string
636                    Err(err @ (Error::ExpectedStringEnd | Error::Eof)) => return Err(err),
637                    Err(_) => parser.set_cursor(cursor_backup),
638                }
639
640                let c = parser.next_char()?;
641                if matches!(c, '(' | '[' | '{') {
642                    braces += 1;
643                } else if matches!(c, ')' | ']' | '}') {
644                    braces -= 1;
645                } else if c == ',' && braces == 1 {
646                    parser.skip_ws()?;
647                    more_than_one = !parser.check_char(')');
648                    break;
649                }
650            }
651
652            if more_than_one {
653                Ok(StructType::NonNewtypeTuple)
654            } else {
655                Ok(StructType::NewtypeTuple)
656            }
657        }
658
659        // Create a temporary working copy
660        let backup_cursor = self.cursor;
661
662        let result = check_struct_type_inner(self, newtype, tuple);
663
664        if result.is_ok() {
665            // Revert the parser to before the struct type check
666            self.set_cursor(backup_cursor);
667        }
668
669        result
670    }
671
672    /// Only returns true if the char after `ident` cannot belong
673    /// to an identifier.
674    pub fn consume_ident(&mut self, ident: &str) -> bool {
675        if self.check_ident(ident) {
676            self.advance_bytes(ident.len());
677
678            true
679        } else {
680            false
681        }
682    }
683
684    pub fn consume_struct_name(&mut self, ident: &'static str) -> Result<bool> {
685        if self.check_ident("") {
686            if self.exts.contains(Extensions::EXPLICIT_STRUCT_NAMES) {
687                return Err(Error::ExpectedStructName(ident.to_string()));
688            }
689
690            return Ok(false);
691        }
692
693        let found_ident = match self.identifier() {
694            Ok(maybe_ident) => maybe_ident,
695            Err(Error::SuggestRawIdentifier(found_ident)) if found_ident == ident => {
696                return Err(Error::SuggestRawIdentifier(found_ident))
697            }
698            Err(_) => return Err(Error::ExpectedNamedStructLike(ident)),
699        };
700
701        if ident.is_empty() {
702            return Err(Error::ExpectedNamedStructLike(ident));
703        }
704
705        if found_ident != ident {
706            return Err(Error::ExpectedDifferentStructName {
707                expected: ident,
708                found: String::from(found_ident),
709            });
710        }
711
712        Ok(true)
713    }
714
715    /// Returns the extensions bit mask.
716    fn extensions(&mut self) -> Result<Extensions> {
717        if !self.check_char('#') {
718            return Ok(Extensions::empty());
719        }
720
721        if !self.consume_all(&["#", "!", "[", "enable", "("])? {
722            return Err(Error::ExpectedAttribute);
723        }
724
725        self.skip_ws()?;
726        let mut extensions = Extensions::empty();
727
728        loop {
729            let ident = self.identifier()?;
730            let extension = Extensions::from_ident(ident)
731                .ok_or_else(|| Error::NoSuchExtension(ident.into()))?;
732
733            extensions |= extension;
734
735            let comma = self.comma()?;
736
737            // If we have no comma but another item, return an error
738            if !comma && self.check_ident_other_char(0) {
739                return Err(Error::ExpectedComma);
740            }
741
742            // If there's no comma, assume the list ended.
743            // If there is, it might be a trailing one, thus we only
744            // continue the loop if we get an ident char.
745            if !comma || !self.check_ident_other_char(0) {
746                break;
747            }
748        }
749
750        self.skip_ws()?;
751
752        if self.consume_all(&[")", "]"])? {
753            Ok(extensions)
754        } else {
755            Err(Error::ExpectedAttributeEnd)
756        }
757    }
758
759    pub fn float<T: Float>(&mut self) -> Result<T> {
760        const F32_SUFFIX: &str = "f32";
761        const F64_SUFFIX: &str = "f64";
762
763        for (literal, value_f32, value_f64) in &[
764            ("inf", f32::INFINITY, f64::INFINITY),
765            ("+inf", f32::INFINITY, f64::INFINITY),
766            ("-inf", f32::NEG_INFINITY, f64::NEG_INFINITY),
767            ("NaN", f32::NAN, f64::NAN),
768            ("+NaN", f32::NAN, f64::NAN),
769            ("-NaN", -f32::NAN, -f64::NAN),
770        ] {
771            if self.consume_ident(literal) {
772                return T::parse(literal);
773            }
774
775            if let Some(suffix) = self.src().strip_prefix(literal) {
776                if let Some(post_suffix) = suffix.strip_prefix(F32_SUFFIX) {
777                    if !post_suffix.chars().next().map_or(false, is_xid_continue) {
778                        let float_ron = &self.src()[..literal.len() + F32_SUFFIX.len()];
779                        self.advance_bytes(literal.len() + F32_SUFFIX.len());
780                        return T::try_from_parsed_float(ParsedFloat::F32(*value_f32), float_ron);
781                    }
782                }
783
784                if let Some(post_suffix) = suffix.strip_prefix(F64_SUFFIX) {
785                    if !post_suffix.chars().next().map_or(false, is_xid_continue) {
786                        let float_ron = &self.src()[..literal.len() + F64_SUFFIX.len()];
787                        self.advance_bytes(literal.len() + F64_SUFFIX.len());
788                        return T::try_from_parsed_float(ParsedFloat::F64(*value_f64), float_ron);
789                    }
790                }
791            }
792        }
793
794        let num_bytes = self.next_chars_while_len(is_float_char);
795
796        if num_bytes == 0 {
797            return Err(Error::ExpectedFloat);
798        }
799
800        if self.check_char('_') {
801            return Err(Error::UnderscoreAtBeginning);
802        }
803
804        let mut f = String::with_capacity(num_bytes);
805        let mut allow_underscore = false;
806
807        for (i, c) in self.src()[..num_bytes].char_indices() {
808            match c {
809                '_' if allow_underscore => continue,
810                '_' => {
811                    self.advance_bytes(i);
812                    return Err(Error::FloatUnderscore);
813                }
814                '0'..='9' | 'e' | 'E' => allow_underscore = true,
815                '.' => allow_underscore = false,
816                _ => (),
817            }
818
819            // we know that the byte is an ASCII character here
820            f.push(c);
821        }
822
823        if self.src()[num_bytes..].starts_with('f') {
824            let backup_cursor = self.cursor;
825            self.advance_bytes(num_bytes);
826
827            #[allow(clippy::never_loop)]
828            loop {
829                let res = if self.consume_ident(F32_SUFFIX) {
830                    f32::from_str(&f).map(ParsedFloat::F32)
831                } else if self.consume_ident(F64_SUFFIX) {
832                    f64::from_str(&f).map(ParsedFloat::F64)
833                } else {
834                    break;
835                };
836
837                let parsed = if let Ok(parsed) = res {
838                    parsed
839                } else {
840                    self.set_cursor(backup_cursor);
841                    return Err(Error::ExpectedFloat);
842                };
843
844                let float_ron = &self.src[backup_cursor.cursor..self.cursor.cursor];
845
846                return T::try_from_parsed_float(parsed, float_ron);
847            }
848
849            self.set_cursor(backup_cursor);
850        }
851
852        let value = T::parse(&f)?;
853
854        self.advance_bytes(num_bytes);
855
856        Ok(value)
857    }
858
859    pub fn skip_identifier(&mut self) -> Option<&'a str> {
860        #[allow(clippy::nonminimal_bool)]
861        if self.check_str("b\"") // byte string
862            || self.check_str("b'") // byte literal
863            || self.check_str("br#") // raw byte string
864            || self.check_str("br\"") // raw byte string
865            || self.check_str("r\"") // raw string
866            || self.check_str("r#\"") // raw string
867            || self.check_str("r##") // raw string
868            || false
869        {
870            return None;
871        }
872
873        if self.check_str("r#") {
874            // maybe a raw identifier
875            let len = self.next_chars_while_from_len(2, is_ident_raw_char);
876            if len > 0 {
877                let ident = &self.src()[2..2 + len];
878                self.advance_bytes(2 + len);
879                return Some(ident);
880            }
881            return None;
882        }
883
884        if let Some(c) = self.peek_char() {
885            // maybe a normal identifier
886            if is_ident_first_char(c) {
887                let len =
888                    c.len_utf8() + self.next_chars_while_from_len(c.len_utf8(), is_xid_continue);
889                let ident = &self.src()[..len];
890                self.advance_bytes(len);
891                return Some(ident);
892            }
893        }
894
895        None
896    }
897
898    pub fn identifier(&mut self) -> Result<&'a str> {
899        let first = self.peek_char_or_eof()?;
900        if !is_ident_first_char(first) {
901            if is_ident_raw_char(first) {
902                let ident_bytes = self.next_chars_while_len(is_ident_raw_char);
903                return Err(Error::SuggestRawIdentifier(
904                    self.src()[..ident_bytes].into(),
905                ));
906            }
907
908            return Err(Error::ExpectedIdentifier);
909        }
910
911        // If the next 2-3 bytes signify the start of a (raw) (byte) string
912        //  literal, return an error.
913        #[allow(clippy::nonminimal_bool)]
914        if self.check_str("b\"") // byte string
915            || self.check_str("b'") // byte literal
916            || self.check_str("br#") // raw byte string
917            || self.check_str("br\"") // raw byte string
918            || self.check_str("r\"") // raw string
919            || self.check_str("r#\"") // raw string
920            || self.check_str("r##") // raw string
921            || false
922        {
923            return Err(Error::ExpectedIdentifier);
924        }
925
926        let length = if self.check_str("r#") {
927            let cursor_backup = self.cursor;
928
929            self.advance_bytes(2);
930
931            // Note: it's important to check this before advancing forward, so that
932            // the value-type deserializer can fall back to parsing it differently.
933            if !matches!(self.peek_char(), Some(c) if is_ident_raw_char(c)) {
934                self.set_cursor(cursor_backup);
935                return Err(Error::ExpectedIdentifier);
936            }
937
938            self.next_chars_while_len(is_ident_raw_char)
939        } else if first == 'r' {
940            let std_ident_length = self.next_chars_while_len(is_xid_continue);
941            let raw_ident_length = self.next_chars_while_len(is_ident_raw_char);
942
943            if raw_ident_length > std_ident_length {
944                return Err(Error::SuggestRawIdentifier(
945                    self.src()[..raw_ident_length].into(),
946                ));
947            }
948
949            std_ident_length
950        } else {
951            let std_ident_length = first.len_utf8()
952                + self.next_chars_while_from_len(first.len_utf8(), is_xid_continue);
953            let raw_ident_length = self.next_chars_while_len(is_ident_raw_char);
954
955            if raw_ident_length > std_ident_length {
956                return Err(Error::SuggestRawIdentifier(
957                    self.src()[..raw_ident_length].into(),
958                ));
959            }
960
961            std_ident_length
962        };
963
964        let ident = &self.src()[..length];
965        self.advance_bytes(length);
966
967        Ok(ident)
968    }
969
970    pub fn next_bytes_is_float(&mut self) -> bool {
971        if let Some(c) = self.peek_char() {
972            let skip = match c {
973                '+' | '-' => 1,
974                _ => 0,
975            };
976            let valid_float_len = self.next_chars_while_from_len(skip, is_float_char);
977            let valid_int_len = self.next_chars_while_from_len(skip, is_int_char);
978            valid_float_len > valid_int_len
979        } else {
980            false
981        }
982    }
983
984    pub fn skip_ws(&mut self) -> Result<()> {
985        if (self.cursor.last_ws_len != WS_CURSOR_UNCLOSED_LINE)
986            && ((self.cursor.pre_ws_cursor + self.cursor.last_ws_len) < self.cursor.cursor)
987        {
988            // the last whitespace is disjoint from this one, we need to track a new one
989            self.cursor.pre_ws_cursor = self.cursor.cursor;
990        }
991
992        if self.src().is_empty() {
993            return Ok(());
994        }
995
996        loop {
997            self.advance_bytes(self.next_chars_while_len(is_whitespace_char));
998
999            match self.skip_comment()? {
1000                None => break,
1001                Some(Comment::UnclosedLine) => {
1002                    self.cursor.last_ws_len = WS_CURSOR_UNCLOSED_LINE;
1003                    return Ok(());
1004                }
1005                Some(Comment::ClosedLine | Comment::Block) => continue,
1006            }
1007        }
1008
1009        self.cursor.last_ws_len = self.cursor.cursor - self.cursor.pre_ws_cursor;
1010
1011        Ok(())
1012    }
1013
1014    pub fn has_unclosed_line_comment(&self) -> bool {
1015        self.src().is_empty() && self.cursor.last_ws_len == WS_CURSOR_UNCLOSED_LINE
1016    }
1017
1018    pub fn byte_string(&mut self) -> Result<ParsedByteStr<'a>> {
1019        fn expected_byte_string_found_base64(
1020            base64_str: &ParsedStr,
1021            byte_str: &ParsedByteStr,
1022        ) -> Error {
1023            let byte_str = match &byte_str {
1024                ParsedByteStr::Allocated(b) => b.as_slice(),
1025                ParsedByteStr::Slice(b) => b,
1026            }
1027            .iter()
1028            .flat_map(|c| core::ascii::escape_default(*c))
1029            .map(char::from)
1030            .collect::<String>();
1031            let base64_str = match &base64_str {
1032                ParsedStr::Allocated(s) => s.as_str(),
1033                ParsedStr::Slice(s) => s,
1034            };
1035
1036            Error::InvalidValueForType {
1037                expected: format!("the Rusty byte string b\"{}\"", byte_str),
1038                found: format!("the ambiguous base64 string {:?}", base64_str),
1039            }
1040        }
1041
1042        if self.consume_char('"') {
1043            let base64_str = self.escaped_string()?;
1044            let base64_result = ParsedByteStr::try_from_base64(&base64_str);
1045
1046            if cfg!(not(test)) {
1047                // FIXME @juntyr: remove in v0.10
1048                #[allow(deprecated)]
1049                base64_result.map_err(Error::Base64Error)
1050            } else {
1051                match base64_result {
1052                    // FIXME @juntyr: enable in v0.10
1053                    Ok(byte_str) => Err(expected_byte_string_found_base64(&base64_str, &byte_str)),
1054                    Err(_) => Err(Error::ExpectedByteString),
1055                }
1056            }
1057        } else if self.consume_char('r') {
1058            let base64_str = self.raw_string()?;
1059            let base64_result = ParsedByteStr::try_from_base64(&base64_str);
1060
1061            if cfg!(not(test)) {
1062                // FIXME @juntyr: remove in v0.10
1063                #[allow(deprecated)]
1064                base64_result.map_err(Error::Base64Error)
1065            } else {
1066                match base64_result {
1067                    // FIXME @juntyr: enable in v0.10
1068                    Ok(byte_str) => Err(expected_byte_string_found_base64(&base64_str, &byte_str)),
1069                    Err(_) => Err(Error::ExpectedByteString),
1070                }
1071            }
1072        } else {
1073            self.byte_string_no_base64()
1074        }
1075    }
1076
1077    pub fn byte_string_no_base64(&mut self) -> Result<ParsedByteStr<'a>> {
1078        if self.consume_str("b\"") {
1079            self.escaped_byte_string()
1080        } else if self.consume_str("br") {
1081            self.raw_byte_string()
1082        } else {
1083            Err(Error::ExpectedByteString)
1084        }
1085    }
1086
1087    fn escaped_byte_string(&mut self) -> Result<ParsedByteStr<'a>> {
1088        match self.escaped_byte_buf(EscapeEncoding::Binary) {
1089            Ok((bytes, advance)) => {
1090                self.advance_bytes(advance);
1091                Ok(bytes)
1092            }
1093            Err(err) => Err(err),
1094        }
1095    }
1096
1097    fn raw_byte_string(&mut self) -> Result<ParsedByteStr<'a>> {
1098        match self.raw_byte_buf() {
1099            Ok((bytes, advance)) => {
1100                self.advance_bytes(advance);
1101                Ok(bytes)
1102            }
1103            Err(Error::ExpectedString) => Err(Error::ExpectedByteString),
1104            Err(err) => Err(err),
1105        }
1106    }
1107
1108    pub fn string(&mut self) -> Result<ParsedStr<'a>> {
1109        if self.consume_char('"') {
1110            self.escaped_string()
1111        } else if self.consume_char('r') {
1112            self.raw_string()
1113        } else {
1114            Err(Error::ExpectedString)
1115        }
1116    }
1117
1118    fn escaped_string(&mut self) -> Result<ParsedStr<'a>> {
1119        match self.escaped_byte_buf(EscapeEncoding::Utf8) {
1120            Ok((bytes, advance)) => {
1121                let string = ParsedStr::try_from_bytes(bytes).map_err(Error::from)?;
1122                self.advance_bytes(advance);
1123                Ok(string)
1124            }
1125            Err(err) => Err(err),
1126        }
1127    }
1128
1129    fn raw_string(&mut self) -> Result<ParsedStr<'a>> {
1130        match self.raw_byte_buf() {
1131            Ok((bytes, advance)) => {
1132                let string = ParsedStr::try_from_bytes(bytes).map_err(Error::from)?;
1133                self.advance_bytes(advance);
1134                Ok(string)
1135            }
1136            Err(err) => Err(err),
1137        }
1138    }
1139
1140    fn escaped_byte_buf(&mut self, encoding: EscapeEncoding) -> Result<(ParsedByteStr<'a>, usize)> {
1141        // Checking for '"' and '\\' separately is faster than searching for both at the same time
1142        let str_end = self.src().find('"').ok_or(Error::ExpectedStringEnd)?;
1143        let escape = self.src()[..str_end].find('\\');
1144
1145        if let Some(escape) = escape {
1146            // Now check if escaping is used inside the string
1147            let mut i = escape;
1148            let mut s = self.src().as_bytes()[..i].to_vec();
1149
1150            loop {
1151                self.advance_bytes(i + 1);
1152
1153                match self.parse_escape(encoding, false)? {
1154                    EscapeCharacter::Ascii(c) => s.push(c),
1155                    EscapeCharacter::Utf8(c) => match c.len_utf8() {
1156                        1 => s.push(c as u8),
1157                        len => {
1158                            let start = s.len();
1159                            s.extend(core::iter::repeat(0).take(len));
1160                            c.encode_utf8(&mut s[start..]);
1161                        }
1162                    },
1163                }
1164
1165                // Checking for '"' and '\\' separately is faster than searching for both at the same time
1166                let new_str_end = self.src().find('"').ok_or(Error::ExpectedStringEnd)?;
1167                let new_escape = self.src()[..new_str_end].find('\\');
1168
1169                if let Some(new_escape) = new_escape {
1170                    s.extend_from_slice(&self.src().as_bytes()[..new_escape]);
1171                    i = new_escape;
1172                } else {
1173                    s.extend_from_slice(&self.src().as_bytes()[..new_str_end]);
1174                    // Advance to the end of the string + 1 for the `"`.
1175                    break Ok((ParsedByteStr::Allocated(s), new_str_end + 1));
1176                }
1177            }
1178        } else {
1179            let s = &self.src().as_bytes()[..str_end];
1180
1181            // Advance by the number of bytes of the string + 1 for the `"`.
1182            Ok((ParsedByteStr::Slice(s), str_end + 1))
1183        }
1184    }
1185
1186    fn raw_byte_buf(&mut self) -> Result<(ParsedByteStr<'a>, usize)> {
1187        let num_hashes = self.next_chars_while_len(|c| c == '#');
1188        let hashes = &self.src()[..num_hashes];
1189        self.advance_bytes(num_hashes);
1190
1191        self.expect_char('"', Error::ExpectedString)?;
1192
1193        let ending = ["\"", hashes].concat();
1194        let i = self.src().find(&ending).ok_or(Error::ExpectedStringEnd)?;
1195
1196        let s = &self.src().as_bytes()[..i];
1197
1198        // Advance by the number of bytes of the byte string
1199        // + `num_hashes` + 1 for the `"`.
1200        Ok((ParsedByteStr::Slice(s), i + num_hashes + 1))
1201    }
1202
1203    fn decode_ascii_escape(&mut self) -> Result<u8> {
1204        let mut n = 0;
1205        for _ in 0..2 {
1206            n <<= 4;
1207            let byte = self.next_char()?;
1208            let decoded = Self::decode_hex(byte)?;
1209            n |= decoded;
1210        }
1211
1212        Ok(n)
1213    }
1214
1215    #[inline]
1216    fn decode_hex(c: char) -> Result<u8> {
1217        if !c.is_ascii() {
1218            return Err(Error::InvalidEscape("Non-hex digit found"));
1219        }
1220
1221        // c is an ASCII character that can be losslessly cast to u8
1222        match c as u8 {
1223            c @ b'0'..=b'9' => Ok(c - b'0'),
1224            c @ b'a'..=b'f' => Ok(10 + c - b'a'),
1225            c @ b'A'..=b'F' => Ok(10 + c - b'A'),
1226            _ => Err(Error::InvalidEscape("Non-hex digit found")),
1227        }
1228    }
1229
1230    fn parse_escape(&mut self, encoding: EscapeEncoding, is_char: bool) -> Result<EscapeCharacter> {
1231        let c = match self.next_char()? {
1232            '\'' => EscapeCharacter::Ascii(b'\''),
1233            '"' => EscapeCharacter::Ascii(b'"'),
1234            '\\' => EscapeCharacter::Ascii(b'\\'),
1235            'n' => EscapeCharacter::Ascii(b'\n'),
1236            'r' => EscapeCharacter::Ascii(b'\r'),
1237            't' => EscapeCharacter::Ascii(b'\t'),
1238            '0' => EscapeCharacter::Ascii(b'\0'),
1239            'x' => {
1240                // Fast exit for ascii escape in byte string
1241                let b: u8 = self.decode_ascii_escape()?;
1242                if let EscapeEncoding::Binary = encoding {
1243                    return Ok(EscapeCharacter::Ascii(b));
1244                }
1245
1246                // Fast exit for ascii character in UTF-8 string
1247                let mut bytes = [b, 0, 0, 0];
1248                if let Ok(Some(c)) = from_utf8(&bytes[..=0]).map(|s| s.chars().next()) {
1249                    return Ok(EscapeCharacter::Utf8(c));
1250                }
1251
1252                if is_char {
1253                    // Character literals are not allowed to use multiple byte
1254                    //  escapes to build a unicode character
1255                    return Err(Error::InvalidEscape(
1256                        "Not a valid byte-escaped Unicode character",
1257                    ));
1258                }
1259
1260                // UTF-8 character needs up to four bytes and we have already
1261                //  consumed one, so at most three to go
1262                for i in 1..4 {
1263                    if !self.consume_str(r"\x") {
1264                        return Err(Error::InvalidEscape(
1265                            "Not a valid byte-escaped Unicode character",
1266                        ));
1267                    }
1268
1269                    bytes[i] = self.decode_ascii_escape()?;
1270
1271                    // Check if we now have a valid UTF-8 character
1272                    if let Ok(Some(c)) = from_utf8(&bytes[..=i]).map(|s| s.chars().next()) {
1273                        return Ok(EscapeCharacter::Utf8(c));
1274                    }
1275                }
1276
1277                return Err(Error::InvalidEscape(
1278                    "Not a valid byte-escaped Unicode character",
1279                ));
1280            }
1281            'u' => {
1282                self.expect_char('{', Error::InvalidEscape("Missing { in Unicode escape"))?;
1283
1284                let mut bytes: u32 = 0;
1285                let mut num_digits = 0;
1286
1287                while num_digits < 6 {
1288                    let byte = self.peek_char_or_eof()?;
1289
1290                    if byte == '}' {
1291                        break;
1292                    }
1293
1294                    self.skip_next_char();
1295                    num_digits += 1;
1296
1297                    let byte = Self::decode_hex(byte)?;
1298                    bytes <<= 4;
1299                    bytes |= u32::from(byte);
1300                }
1301
1302                if num_digits == 0 {
1303                    return Err(Error::InvalidEscape(
1304                        "Expected 1-6 digits, got 0 digits in Unicode escape",
1305                    ));
1306                }
1307
1308                self.expect_char(
1309                    '}',
1310                    Error::InvalidEscape("No } at the end of Unicode escape"),
1311                )?;
1312                let c = char_from_u32(bytes).ok_or(Error::InvalidEscape(
1313                    "Not a valid Unicode-escaped character",
1314                ))?;
1315
1316                EscapeCharacter::Utf8(c)
1317            }
1318            _ => return Err(Error::InvalidEscape("Unknown escape character")),
1319        };
1320
1321        Ok(c)
1322    }
1323
1324    fn skip_comment(&mut self) -> Result<Option<Comment>> {
1325        if self.consume_char('/') {
1326            match self.next_char()? {
1327                '/' => {
1328                    let bytes = self.next_chars_while_len(|c| c != '\n');
1329
1330                    self.advance_bytes(bytes);
1331
1332                    if self.src().is_empty() {
1333                        Ok(Some(Comment::UnclosedLine))
1334                    } else {
1335                        Ok(Some(Comment::ClosedLine))
1336                    }
1337                }
1338                '*' => {
1339                    let mut level = 1;
1340
1341                    while level > 0 {
1342                        let bytes = self.next_chars_while_len(|c| !matches!(c, '/' | '*'));
1343
1344                        if self.src().is_empty() {
1345                            return Err(Error::UnclosedBlockComment);
1346                        }
1347
1348                        self.advance_bytes(bytes);
1349
1350                        // check whether / or * and take action
1351                        if self.consume_str("/*") {
1352                            level += 1;
1353                        } else if self.consume_str("*/") {
1354                            level -= 1;
1355                        } else {
1356                            self.next_char().map_err(|_| Error::UnclosedBlockComment)?;
1357                        }
1358                    }
1359
1360                    Ok(Some(Comment::Block))
1361                }
1362                c => Err(Error::UnexpectedChar(c)),
1363            }
1364        } else {
1365            Ok(None)
1366        }
1367    }
1368}
1369
1370enum Comment {
1371    ClosedLine,
1372    UnclosedLine,
1373    Block,
1374}
1375
1376pub trait Num {
1377    fn from_u8(x: u8) -> Self;
1378
1379    /// Returns `true` on overflow
1380    fn checked_mul_ext(&mut self, x: u8) -> bool;
1381
1382    /// Returns `true` on overflow
1383    fn checked_add_ext(&mut self, x: u8) -> bool;
1384
1385    /// Returns `true` on overflow
1386    fn checked_sub_ext(&mut self, x: u8) -> bool;
1387}
1388
1389macro_rules! impl_num {
1390    ($ty:ty) => {
1391        impl Num for $ty {
1392            fn from_u8(x: u8) -> Self {
1393                x as $ty
1394            }
1395
1396            fn checked_mul_ext(&mut self, x: u8) -> bool {
1397                match self.checked_mul(Self::from_u8(x)) {
1398                    Some(n) => {
1399                        *self = n;
1400                        false
1401                    }
1402                    None => true,
1403                }
1404            }
1405
1406            fn checked_add_ext(&mut self, x: u8) -> bool {
1407                match self.checked_add(Self::from_u8(x)) {
1408                    Some(n) => {
1409                        *self = n;
1410                        false
1411                    }
1412                    None => true,
1413                }
1414            }
1415
1416            fn checked_sub_ext(&mut self, x: u8) -> bool {
1417                match self.checked_sub(Self::from_u8(x)) {
1418                    Some(n) => {
1419                        *self = n;
1420                        false
1421                    }
1422                    None => true,
1423                }
1424            }
1425        }
1426    };
1427    ($($tys:ty)*) => {
1428        $( impl_num!($tys); )*
1429    };
1430}
1431
1432impl_num! { i8 i16 i32 i64 u8 u16 u32 u64 }
1433
1434#[cfg(feature = "integer128")]
1435impl_num! { i128 u128 }
1436
1437pub trait Integer: Sized {
1438    fn parse(parser: &mut Parser, sign: i8) -> Result<Self>;
1439
1440    fn try_from_parsed_integer(parsed: ParsedInteger, ron: &str) -> Result<Self>;
1441}
1442
1443macro_rules! impl_integer {
1444    ($wrap:ident($ty:ty)) => {
1445        impl Integer for $ty {
1446            fn parse(parser: &mut Parser, sign: i8) -> Result<Self> {
1447                parser.parse_integer(sign)
1448            }
1449
1450            fn try_from_parsed_integer(parsed: ParsedInteger, ron: &str) -> Result<Self> {
1451                match parsed {
1452                    ParsedInteger::$wrap(v) => Ok(v),
1453                    _ => Err(Error::InvalidValueForType {
1454                        expected: format!(
1455                            "a{} {}-bit {}signed integer",
1456                            if <$ty>::BITS == 8 { "n" } else { "n" },
1457                            <$ty>::BITS,
1458                            if <$ty>::MIN == 0 { "un" } else { "" },
1459                        ),
1460                        found: String::from(ron),
1461                    }),
1462                }
1463            }
1464        }
1465    };
1466    ($($wraps:ident($tys:ty))*) => {
1467        $( impl_integer!($wraps($tys)); )*
1468    };
1469}
1470
1471impl_integer! {
1472    I8(i8) I16(i16) I32(i32) I64(i64)
1473    U8(u8) U16(u16) U32(u32) U64(u64)
1474}
1475
1476#[cfg(feature = "integer128")]
1477impl_integer! { I128(i128) U128(u128) }
1478
1479pub enum ParsedInteger {
1480    I8(i8),
1481    I16(i16),
1482    I32(i32),
1483    I64(i64),
1484    #[cfg(feature = "integer128")]
1485    I128(i128),
1486    U8(u8),
1487    U16(u16),
1488    U32(u32),
1489    U64(u64),
1490    #[cfg(feature = "integer128")]
1491    U128(u128),
1492}
1493
1494impl Integer for ParsedInteger {
1495    fn parse(parser: &mut Parser, sign: i8) -> Result<Self> {
1496        if sign < 0 {
1497            let signed = parser.parse_integer::<LargeSInt>(-1)?;
1498
1499            return if let Ok(x) = i8::try_from(signed) {
1500                Ok(ParsedInteger::I8(x))
1501            } else if let Ok(x) = i16::try_from(signed) {
1502                Ok(ParsedInteger::I16(x))
1503            } else if let Ok(x) = i32::try_from(signed) {
1504                Ok(ParsedInteger::I32(x))
1505            } else {
1506                #[cfg(not(feature = "integer128"))]
1507                {
1508                    Ok(ParsedInteger::I64(signed))
1509                }
1510                #[cfg(feature = "integer128")]
1511                if let Ok(x) = i64::try_from(signed) {
1512                    Ok(ParsedInteger::I64(x))
1513                } else {
1514                    Ok(ParsedInteger::I128(signed))
1515                }
1516            };
1517        }
1518
1519        let unsigned = parser.parse_integer::<LargeUInt>(1)?;
1520
1521        if let Ok(x) = u8::try_from(unsigned) {
1522            Ok(ParsedInteger::U8(x))
1523        } else if let Ok(x) = u16::try_from(unsigned) {
1524            Ok(ParsedInteger::U16(x))
1525        } else if let Ok(x) = u32::try_from(unsigned) {
1526            Ok(ParsedInteger::U32(x))
1527        } else {
1528            #[cfg(not(feature = "integer128"))]
1529            {
1530                Ok(ParsedInteger::U64(unsigned))
1531            }
1532            #[cfg(feature = "integer128")]
1533            if let Ok(x) = u64::try_from(unsigned) {
1534                Ok(ParsedInteger::U64(x))
1535            } else {
1536                Ok(ParsedInteger::U128(unsigned))
1537            }
1538        }
1539    }
1540
1541    fn try_from_parsed_integer(parsed: ParsedInteger, _ron: &str) -> Result<Self> {
1542        Ok(parsed)
1543    }
1544}
1545
1546pub trait Float: Sized {
1547    fn parse(float: &str) -> Result<Self>;
1548
1549    fn try_from_parsed_float(parsed: ParsedFloat, ron: &str) -> Result<Self>;
1550}
1551
1552macro_rules! impl_float {
1553    ($wrap:ident($ty:ty: $bits:expr)) => {
1554        impl Float for $ty {
1555            fn parse(float: &str) -> Result<Self> {
1556                <$ty>::from_str(float).map_err(|_| Error::ExpectedFloat)
1557            }
1558
1559            fn try_from_parsed_float(parsed: ParsedFloat, ron: &str) -> Result<Self> {
1560                match parsed {
1561                    ParsedFloat::$wrap(v) => Ok(v),
1562                    _ => Err(Error::InvalidValueForType {
1563                        expected: format!(
1564                            "a {}-bit floating point number", $bits,
1565                        ),
1566                        found: String::from(ron),
1567                    }),
1568                }
1569            }
1570        }
1571    };
1572    ($($wraps:ident($tys:ty: $bits:expr))*) => {
1573        $( impl_float!($wraps($tys: $bits)); )*
1574    };
1575}
1576
1577impl_float! { F32(f32: 32) F64(f64: 64) }
1578
1579pub enum ParsedFloat {
1580    F32(f32),
1581    F64(f64),
1582}
1583
1584impl Float for ParsedFloat {
1585    fn parse(float: &str) -> Result<Self> {
1586        let value = f64::from_str(float).map_err(|_| Error::ExpectedFloat)?;
1587
1588        #[allow(clippy::cast_possible_truncation)]
1589        if value.total_cmp(&f64::from(value as f32)).is_eq() {
1590            Ok(ParsedFloat::F32(value as f32))
1591        } else {
1592            Ok(ParsedFloat::F64(value))
1593        }
1594    }
1595
1596    fn try_from_parsed_float(parsed: ParsedFloat, _ron: &str) -> Result<Self> {
1597        Ok(parsed)
1598    }
1599}
1600
1601pub enum StructType {
1602    AnyTuple,
1603    EmptyTuple,
1604    NewtypeTuple,
1605    NonNewtypeTuple,
1606    Named,
1607    Unit,
1608}
1609
1610#[derive(Copy, Clone)] // GRCOV_EXCL_LINE
1611pub enum NewtypeMode {
1612    NoParensMeanUnit,
1613    InsideNewtype,
1614}
1615
1616#[derive(Copy, Clone)] // GRCOV_EXCL_LINE
1617pub enum TupleMode {
1618    ImpreciseTupleOrNewtype,
1619    DifferentiateNewtype,
1620}
1621
1622pub enum ParsedStr<'a> {
1623    Allocated(String),
1624    Slice(&'a str),
1625}
1626
1627pub enum ParsedByteStr<'a> {
1628    Allocated(Vec<u8>),
1629    Slice(&'a [u8]),
1630}
1631
1632impl<'a> ParsedStr<'a> {
1633    pub fn try_from_bytes(bytes: ParsedByteStr<'a>) -> Result<Self, Utf8Error> {
1634        match bytes {
1635            ParsedByteStr::Allocated(byte_buf) => Ok(ParsedStr::Allocated(
1636                String::from_utf8(byte_buf).map_err(|e| e.utf8_error())?,
1637            )),
1638            ParsedByteStr::Slice(bytes) => Ok(ParsedStr::Slice(from_utf8(bytes)?)),
1639        }
1640    }
1641}
1642
1643impl<'a> ParsedByteStr<'a> {
1644    pub fn try_from_base64(str: &ParsedStr<'a>) -> Result<Self, base64::DecodeError> {
1645        let base64_str = match str {
1646            ParsedStr::Allocated(string) => string.as_str(),
1647            ParsedStr::Slice(str) => str,
1648        };
1649
1650        base64::engine::Engine::decode(&base64::engine::general_purpose::STANDARD, base64_str)
1651            .map(ParsedByteStr::Allocated)
1652    }
1653}
1654
1655#[derive(Copy, Clone)] // GRCOV_EXCL_LINE
1656enum EscapeEncoding {
1657    Binary,
1658    Utf8,
1659}
1660
1661enum EscapeCharacter {
1662    Ascii(u8),
1663    Utf8(char),
1664}
1665
1666#[cfg(test)]
1667mod tests {
1668    use super::*;
1669
1670    #[test]
1671    fn decode_x10() {
1672        let mut bytes = Parser::new("10").unwrap();
1673        assert_eq!(bytes.decode_ascii_escape(), Ok(b'\x10'));
1674    }
1675
1676    #[test]
1677    fn track_prior_ws() {
1678        const SOURCE: &str = "   /*hey*/ 42       /*bye*/ 24  ";
1679        let mut bytes = Parser::new(SOURCE).unwrap();
1680
1681        assert_eq!(bytes.src(), "42       /*bye*/ 24  ");
1682        assert_eq!(bytes.pre_ws_src(), SOURCE);
1683
1684        bytes.skip_ws().unwrap();
1685
1686        assert_eq!(bytes.src(), "42       /*bye*/ 24  ");
1687        assert_eq!(bytes.pre_ws_src(), SOURCE);
1688
1689        assert_eq!(bytes.integer::<u8>().unwrap(), 42);
1690
1691        assert_eq!(bytes.src(), "       /*bye*/ 24  ");
1692        assert_eq!(bytes.pre_ws_src(), SOURCE);
1693
1694        bytes.skip_ws().unwrap();
1695        bytes.skip_ws().unwrap();
1696
1697        assert_eq!(bytes.src(), "24  ");
1698        assert_eq!(bytes.pre_ws_src(), "       /*bye*/ 24  ");
1699
1700        let mut bytes = Parser::new("42").unwrap();
1701        bytes.skip_ws().unwrap();
1702        bytes.skip_ws().unwrap();
1703        assert_eq!(bytes.src(), "42");
1704        assert_eq!(bytes.pre_ws_src(), "42");
1705        assert_eq!(bytes.integer::<u8>().unwrap(), 42);
1706        bytes.skip_ws().unwrap();
1707        bytes.skip_ws().unwrap();
1708        assert_eq!(bytes.src(), "");
1709        assert_eq!(bytes.pre_ws_src(), "");
1710
1711        let mut bytes = Parser::new("  42  ").unwrap();
1712        bytes.skip_ws().unwrap();
1713        bytes.skip_ws().unwrap();
1714        assert_eq!(bytes.src(), "42  ");
1715        assert_eq!(bytes.pre_ws_src(), "  42  ");
1716        assert_eq!(bytes.integer::<u8>().unwrap(), 42);
1717        bytes.skip_ws().unwrap();
1718        bytes.skip_ws().unwrap();
1719        assert_eq!(bytes.src(), "");
1720        assert_eq!(bytes.pre_ws_src(), "  ");
1721
1722        let mut bytes = Parser::new("  42  //").unwrap();
1723        bytes.skip_ws().unwrap();
1724        bytes.skip_ws().unwrap();
1725        assert_eq!(bytes.src(), "42  //");
1726        assert_eq!(bytes.pre_ws_src(), "  42  //");
1727        assert_eq!(bytes.integer::<u8>().unwrap(), 42);
1728        bytes.skip_ws().unwrap();
1729        bytes.skip_ws().unwrap();
1730        assert_eq!(bytes.src(), "");
1731        assert_eq!(bytes.pre_ws_src(), "  //");
1732    }
1733
1734    #[test]
1735    fn parser_cursor_eq_cmp() {
1736        assert!(
1737            ParserCursor {
1738                cursor: 42,
1739                pre_ws_cursor: 42,
1740                last_ws_len: 42
1741            } == ParserCursor {
1742                cursor: 42,
1743                pre_ws_cursor: 24,
1744                last_ws_len: 24
1745            }
1746        );
1747        assert!(
1748            ParserCursor {
1749                cursor: 42,
1750                pre_ws_cursor: 42,
1751                last_ws_len: 42
1752            } != ParserCursor {
1753                cursor: 24,
1754                pre_ws_cursor: 42,
1755                last_ws_len: 42
1756            }
1757        );
1758
1759        assert!(
1760            ParserCursor {
1761                cursor: 42,
1762                pre_ws_cursor: 42,
1763                last_ws_len: 42
1764            } < ParserCursor {
1765                cursor: 43,
1766                pre_ws_cursor: 24,
1767                last_ws_len: 24
1768            }
1769        );
1770        assert!(
1771            ParserCursor {
1772                cursor: 42,
1773                pre_ws_cursor: 42,
1774                last_ws_len: 42
1775            } > ParserCursor {
1776                cursor: 41,
1777                pre_ws_cursor: 24,
1778                last_ws_len: 24
1779            }
1780        );
1781    }
1782
1783    #[test]
1784    fn empty_src_is_not_a_float() {
1785        assert!(!Parser::new("").unwrap().next_bytes_is_float());
1786    }
1787
1788    #[test]
1789    fn v0_10_base64_deprecation_error() {
1790        let err = crate::from_str::<bytes::Bytes>("\"SGVsbG8gcm9uIQ==\"").unwrap_err();
1791
1792        assert_eq!(
1793            err,
1794            SpannedError {
1795                code: Error::InvalidValueForType {
1796                    expected: String::from("the Rusty byte string b\"Hello ron!\""),
1797                    found: String::from("the ambiguous base64 string \"SGVsbG8gcm9uIQ==\"")
1798                },
1799                span: Span {
1800                    start: Position { line: 1, col: 2 },
1801                    end: Position { line: 1, col: 19 },
1802                }
1803            }
1804        );
1805
1806        let err = crate::from_str::<bytes::Bytes>("r\"SGVsbG8gcm9uIQ==\"").unwrap_err();
1807
1808        assert_eq!(format!("{}", err.code), "Expected the Rusty byte string b\"Hello ron!\" but found the ambiguous base64 string \"SGVsbG8gcm9uIQ==\" instead");
1809
1810        assert_eq!(
1811            crate::from_str::<bytes::Bytes>("\"invalid=\"").unwrap_err(),
1812            SpannedError {
1813                code: Error::ExpectedByteString,
1814                span: Span {
1815                    start: Position { line: 1, col: 2 },
1816                    end: Position { line: 1, col: 11 },
1817                }
1818            }
1819        );
1820
1821        assert_eq!(
1822            crate::from_str::<bytes::Bytes>("r\"invalid=\"").unwrap_err(),
1823            SpannedError {
1824                code: Error::ExpectedByteString,
1825                span: Span {
1826                    start: Position { line: 1, col: 3 },
1827                    end: Position { line: 1, col: 12 },
1828                }
1829            }
1830        );
1831    }
1832}