toml_parser/decoder/
scalar.rs

1use winnow::stream::ContainsToken as _;
2use winnow::stream::FindSlice as _;
3use winnow::stream::Offset as _;
4use winnow::stream::Stream as _;
5
6use crate::decoder::StringBuilder;
7use crate::ErrorSink;
8use crate::Expected;
9use crate::ParseError;
10use crate::Raw;
11use crate::Span;
12
13const ALLOCATION_ERROR: &str = "could not allocate for string";
14
15#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
16pub enum ScalarKind {
17    String,
18    Boolean(bool),
19    DateTime,
20    Float,
21    Integer(IntegerRadix),
22}
23
24impl ScalarKind {
25    pub fn description(&self) -> &'static str {
26        match self {
27            Self::String => "string",
28            Self::Boolean(_) => "boolean",
29            Self::DateTime => "date-time",
30            Self::Float => "float",
31            Self::Integer(radix) => radix.description(),
32        }
33    }
34
35    pub fn invalid_description(&self) -> &'static str {
36        match self {
37            Self::String => "invalid string",
38            Self::Boolean(_) => "invalid boolean",
39            Self::DateTime => "invalid date-time",
40            Self::Float => "invalid float",
41            Self::Integer(radix) => radix.invalid_description(),
42        }
43    }
44}
45
46#[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
47pub enum IntegerRadix {
48    #[default]
49    Dec,
50    Hex,
51    Oct,
52    Bin,
53}
54
55impl IntegerRadix {
56    pub fn description(&self) -> &'static str {
57        match self {
58            Self::Dec => "integer",
59            Self::Hex => "hexadecimal",
60            Self::Oct => "octal",
61            Self::Bin => "binary",
62        }
63    }
64
65    pub fn value(&self) -> u32 {
66        match self {
67            Self::Dec => 10,
68            Self::Hex => 16,
69            Self::Oct => 8,
70            Self::Bin => 2,
71        }
72    }
73
74    pub fn invalid_description(&self) -> &'static str {
75        match self {
76            Self::Dec => "invalid integer number",
77            Self::Hex => "invalid hexadecimal number",
78            Self::Oct => "invalid octal number",
79            Self::Bin => "invalid binary number",
80        }
81    }
82
83    fn validator(&self) -> fn(char) -> bool {
84        match self {
85            Self::Dec => |c| c.is_ascii_digit(),
86            Self::Hex => |c| c.is_ascii_hexdigit(),
87            Self::Oct => |c| matches!(c, '0'..='7'),
88            Self::Bin => |c| matches!(c, '0'..='1'),
89        }
90    }
91}
92
93pub(crate) fn decode_unquoted_scalar<'i>(
94    raw: Raw<'i>,
95    output: &mut dyn StringBuilder<'i>,
96    error: &mut dyn ErrorSink,
97) -> ScalarKind {
98    let s = raw.as_str();
99    let Some(first) = s.as_bytes().first() else {
100        return decode_invalid(raw, output, error);
101    };
102    match first {
103        // number starts
104        b'+' | b'-' => {
105            let value = &raw.as_str()[1..];
106            decode_sign_prefix(raw, value, output, error)
107        }
108        // Report as if they were numbers because its most likely a typo
109        b'_' => decode_datetime_or_float_or_integer(raw.as_str(), raw, output, error),
110        // Date/number starts
111        b'0' => decode_zero_prefix(raw.as_str(), false, raw, output, error),
112        b'1'..=b'9' => decode_datetime_or_float_or_integer(raw.as_str(), raw, output, error),
113        // Report as if they were numbers because its most likely a typo
114        b'.' => {
115            let kind = ScalarKind::Float;
116            let stream = raw.as_str();
117            ensure_float(stream, raw, error);
118            decode_float_or_integer(stream, raw, kind, output, error)
119        }
120        b't' | b'T' => {
121            const SYMBOL: &str = "true";
122            let kind = ScalarKind::Boolean(true);
123            let expected = &[Expected::Literal(SYMBOL)];
124            decode_symbol(raw, SYMBOL, kind, expected, output, error)
125        }
126        b'f' | b'F' => {
127            const SYMBOL: &str = "false";
128            let kind = ScalarKind::Boolean(false);
129            let expected = &[Expected::Literal(SYMBOL)];
130            decode_symbol(raw, SYMBOL, kind, expected, output, error)
131        }
132        b'i' | b'I' => {
133            const SYMBOL: &str = "inf";
134            let kind = ScalarKind::Float;
135            let expected = &[Expected::Literal(SYMBOL)];
136            decode_symbol(raw, SYMBOL, kind, expected, output, error)
137        }
138        b'n' | b'N' => {
139            const SYMBOL: &str = "nan";
140            let kind = ScalarKind::Float;
141            let expected = &[Expected::Literal(SYMBOL)];
142            decode_symbol(raw, SYMBOL, kind, expected, output, error)
143        }
144        _ => decode_invalid(raw, output, error),
145    }
146}
147
148pub(crate) fn decode_sign_prefix<'i>(
149    raw: Raw<'i>,
150    value: &'i str,
151    output: &mut dyn StringBuilder<'i>,
152    error: &mut dyn ErrorSink,
153) -> ScalarKind {
154    let Some(first) = value.as_bytes().first() else {
155        return decode_invalid(raw, output, error);
156    };
157    match first {
158        // number starts
159        b'+' | b'-' => {
160            let start = value.offset_from(&raw.as_str());
161            let end = start + 1;
162            error.report_error(
163                ParseError::new("redundant numeric sign")
164                    .with_context(Span::new_unchecked(0, raw.len()))
165                    .with_expected(&[])
166                    .with_unexpected(Span::new_unchecked(start, end)),
167            );
168
169            let value = &value[1..];
170            decode_sign_prefix(raw, value, output, error)
171        }
172        // Report as if they were numbers because its most likely a typo
173        b'_' => decode_datetime_or_float_or_integer(value, raw, output, error),
174        // Date/number starts
175        b'0' => decode_zero_prefix(value, true, raw, output, error),
176        b'1'..=b'9' => decode_datetime_or_float_or_integer(value, raw, output, error),
177        // Report as if they were numbers because its most likely a typo
178        b'.' => {
179            let kind = ScalarKind::Float;
180            let stream = raw.as_str();
181            ensure_float(stream, raw, error);
182            decode_float_or_integer(stream, raw, kind, output, error)
183        }
184        b'i' | b'I' => {
185            const SYMBOL: &str = "inf";
186            let kind = ScalarKind::Float;
187            if value != SYMBOL {
188                let expected = &[Expected::Literal(SYMBOL)];
189                let start = value.offset_from(&raw.as_str());
190                let end = start + value.len();
191                error.report_error(
192                    ParseError::new(kind.invalid_description())
193                        .with_context(Span::new_unchecked(0, raw.len()))
194                        .with_expected(expected)
195                        .with_unexpected(Span::new_unchecked(start, end)),
196                );
197                decode_as(raw, SYMBOL, kind, output, error)
198            } else {
199                decode_as_is(raw, kind, output, error)
200            }
201        }
202        b'n' | b'N' => {
203            const SYMBOL: &str = "nan";
204            let kind = ScalarKind::Float;
205            if value != SYMBOL {
206                let expected = &[Expected::Literal(SYMBOL)];
207                let start = value.offset_from(&raw.as_str());
208                let end = start + value.len();
209                error.report_error(
210                    ParseError::new(kind.invalid_description())
211                        .with_context(Span::new_unchecked(0, raw.len()))
212                        .with_expected(expected)
213                        .with_unexpected(Span::new_unchecked(start, end)),
214                );
215                decode_as(raw, SYMBOL, kind, output, error)
216            } else {
217                decode_as_is(raw, kind, output, error)
218            }
219        }
220        _ => decode_invalid(raw, output, error),
221    }
222}
223
224pub(crate) fn decode_zero_prefix<'i>(
225    value: &'i str,
226    signed: bool,
227    raw: Raw<'i>,
228    output: &mut dyn StringBuilder<'i>,
229    error: &mut dyn ErrorSink,
230) -> ScalarKind {
231    debug_assert_eq!(value.as_bytes()[0], b'0');
232    if value.len() == 1 {
233        let kind = ScalarKind::Integer(IntegerRadix::Dec);
234        // No extra validation needed
235        decode_float_or_integer(raw.as_str(), raw, kind, output, error)
236    } else {
237        let radix = value.as_bytes()[1];
238        match radix {
239            b'x' | b'X' => {
240                if signed {
241                    error.report_error(
242                        ParseError::new("integers with a radix cannot be signed")
243                            .with_context(Span::new_unchecked(0, raw.len()))
244                            .with_expected(&[])
245                            .with_unexpected(Span::new_unchecked(0, 1)),
246                    );
247                }
248                if radix == b'X' {
249                    let start = value.offset_from(&raw.as_str());
250                    let end = start + 2;
251                    error.report_error(
252                        ParseError::new("radix must be lowercase")
253                            .with_context(Span::new_unchecked(0, raw.len()))
254                            .with_expected(&[Expected::Literal("0x")])
255                            .with_unexpected(Span::new_unchecked(start, end)),
256                    );
257                }
258                let radix = IntegerRadix::Hex;
259                let kind = ScalarKind::Integer(radix);
260                let stream = &value[2..];
261                ensure_radixed_value(stream, raw, radix, error);
262                decode_float_or_integer(stream, raw, kind, output, error)
263            }
264            b'o' | b'O' => {
265                if signed {
266                    error.report_error(
267                        ParseError::new("integers with a radix cannot be signed")
268                            .with_context(Span::new_unchecked(0, raw.len()))
269                            .with_expected(&[])
270                            .with_unexpected(Span::new_unchecked(0, 1)),
271                    );
272                }
273                if radix == b'O' {
274                    let start = value.offset_from(&raw.as_str());
275                    let end = start + 2;
276                    error.report_error(
277                        ParseError::new("radix must be lowercase")
278                            .with_context(Span::new_unchecked(0, raw.len()))
279                            .with_expected(&[Expected::Literal("0o")])
280                            .with_unexpected(Span::new_unchecked(start, end)),
281                    );
282                }
283                let radix = IntegerRadix::Oct;
284                let kind = ScalarKind::Integer(radix);
285                let stream = &value[2..];
286                ensure_radixed_value(stream, raw, radix, error);
287                decode_float_or_integer(stream, raw, kind, output, error)
288            }
289            b'b' | b'B' => {
290                if signed {
291                    error.report_error(
292                        ParseError::new("integers with a radix cannot be signed")
293                            .with_context(Span::new_unchecked(0, raw.len()))
294                            .with_expected(&[])
295                            .with_unexpected(Span::new_unchecked(0, 1)),
296                    );
297                }
298                if radix == b'B' {
299                    let start = value.offset_from(&raw.as_str());
300                    let end = start + 2;
301                    error.report_error(
302                        ParseError::new("radix must be lowercase")
303                            .with_context(Span::new_unchecked(0, raw.len()))
304                            .with_expected(&[Expected::Literal("0b")])
305                            .with_unexpected(Span::new_unchecked(start, end)),
306                    );
307                }
308                let radix = IntegerRadix::Bin;
309                let kind = ScalarKind::Integer(radix);
310                let stream = &value[2..];
311                ensure_radixed_value(stream, raw, radix, error);
312                decode_float_or_integer(stream, raw, kind, output, error)
313            }
314            b'd' | b'D' => {
315                if signed {
316                    error.report_error(
317                        ParseError::new("integers with a radix cannot be signed")
318                            .with_context(Span::new_unchecked(0, raw.len()))
319                            .with_expected(&[])
320                            .with_unexpected(Span::new_unchecked(0, 1)),
321                    );
322                }
323                let radix = IntegerRadix::Dec;
324                let kind = ScalarKind::Integer(radix);
325                let stream = &value[2..];
326                error.report_error(
327                    ParseError::new("redundant integer number prefix")
328                        .with_context(Span::new_unchecked(0, raw.len()))
329                        .with_expected(&[])
330                        .with_unexpected(Span::new_unchecked(0, 2)),
331                );
332                ensure_radixed_value(stream, raw, radix, error);
333                decode_float_or_integer(stream, raw, kind, output, error)
334            }
335            _ => decode_datetime_or_float_or_integer(value, raw, output, error),
336        }
337    }
338}
339
340pub(crate) fn decode_datetime_or_float_or_integer<'i>(
341    value: &'i str,
342    raw: Raw<'i>,
343    output: &mut dyn StringBuilder<'i>,
344    error: &mut dyn ErrorSink,
345) -> ScalarKind {
346    let Some(digit_end) = value
347        .as_bytes()
348        .offset_for(|b| !(b'0'..=b'9').contains_token(b))
349    else {
350        let kind = ScalarKind::Integer(IntegerRadix::Dec);
351        let stream = raw.as_str();
352        ensure_no_leading_zero(value, raw, error);
353        return decode_float_or_integer(stream, raw, kind, output, error);
354    };
355
356    #[cfg(feature = "unsafe")] // SAFETY: ascii digits ensures UTF-8 boundary
357    let rest = unsafe { &value.get_unchecked(digit_end..) };
358    #[cfg(not(feature = "unsafe"))]
359    let rest = &value[digit_end..];
360
361    if rest.starts_with("-") || rest.starts_with(":") {
362        decode_as_is(raw, ScalarKind::DateTime, output, error)
363    } else if rest.contains(" ") {
364        decode_invalid(raw, output, error)
365    } else if is_float(rest) {
366        let kind = ScalarKind::Float;
367        let stream = raw.as_str();
368        ensure_float(value, raw, error);
369        decode_float_or_integer(stream, raw, kind, output, error)
370    } else if rest.starts_with("_") {
371        let kind = ScalarKind::Integer(IntegerRadix::Dec);
372        let stream = raw.as_str();
373        ensure_no_leading_zero(value, raw, error);
374        decode_float_or_integer(stream, raw, kind, output, error)
375    } else {
376        decode_invalid(raw, output, error)
377    }
378}
379
380/// ```abnf
381/// float = float-int-part ( exp / frac [ exp ] )
382///
383/// float-int-part = dec-int
384/// frac = decimal-point zero-prefixable-int
385/// decimal-point = %x2E               ; .
386/// zero-prefixable-int = DIGIT *( DIGIT / underscore DIGIT )
387///
388/// exp = "e" float-exp-part
389/// float-exp-part = [ minus / plus ] zero-prefixable-int
390/// ```
391pub(crate) fn ensure_float<'i>(mut value: &'i str, raw: Raw<'i>, error: &mut dyn ErrorSink) {
392    ensure_dec_uint(&mut value, raw, false, "invalid mantissa", error);
393
394    if value.starts_with(".") {
395        let _ = value.next_token();
396        ensure_dec_uint(&mut value, raw, true, "invalid fraction", error);
397    }
398
399    if value.starts_with(['e', 'E']) {
400        let _ = value.next_token();
401        if value.starts_with(['+', '-']) {
402            let _ = value.next_token();
403        }
404        ensure_dec_uint(&mut value, raw, true, "invalid exponent", error);
405    }
406
407    if !value.is_empty() {
408        let start = value.offset_from(&raw.as_str());
409        let end = raw.len();
410        error.report_error(
411            ParseError::new(ScalarKind::Float.invalid_description())
412                .with_context(Span::new_unchecked(0, raw.len()))
413                .with_expected(&[])
414                .with_unexpected(Span::new_unchecked(start, end)),
415        );
416    }
417}
418
419pub(crate) fn ensure_dec_uint<'i>(
420    value: &mut &'i str,
421    raw: Raw<'i>,
422    zero_prefix: bool,
423    invalid_description: &'static str,
424    error: &mut dyn ErrorSink,
425) {
426    let start = *value;
427    let mut digit_count = 0;
428    while let Some(current) = value.chars().next() {
429        if current.is_ascii_digit() {
430            digit_count += 1;
431        } else if current == '_' {
432        } else {
433            break;
434        }
435        let _ = value.next_token();
436    }
437
438    match digit_count {
439        0 => {
440            let start = start.offset_from(&raw.as_str());
441            let end = start;
442            error.report_error(
443                ParseError::new(invalid_description)
444                    .with_context(Span::new_unchecked(0, raw.len()))
445                    .with_expected(&[Expected::Description("digits")])
446                    .with_unexpected(Span::new_unchecked(start, end)),
447            );
448        }
449        1 => {}
450        _ if start.starts_with("0") && !zero_prefix => {
451            let start = start.offset_from(&raw.as_str());
452            let end = start + 1;
453            error.report_error(
454                ParseError::new("unexpected leading zero")
455                    .with_context(Span::new_unchecked(0, raw.len()))
456                    .with_expected(&[])
457                    .with_unexpected(Span::new_unchecked(start, end)),
458            );
459        }
460        _ => {}
461    }
462}
463
464pub(crate) fn ensure_no_leading_zero<'i>(value: &'i str, raw: Raw<'i>, error: &mut dyn ErrorSink) {
465    if value.starts_with("0") {
466        let start = value.offset_from(&raw.as_str());
467        let end = start + 1;
468        error.report_error(
469            ParseError::new("unexpected leading zero")
470                .with_context(Span::new_unchecked(0, raw.len()))
471                .with_expected(&[])
472                .with_unexpected(Span::new_unchecked(start, end)),
473        );
474    }
475}
476
477pub(crate) fn ensure_radixed_value(
478    value: &str,
479    raw: Raw<'_>,
480    radix: IntegerRadix,
481    error: &mut dyn ErrorSink,
482) {
483    let invalid = ['+', '-'];
484    let value = if let Some(value) = value.strip_prefix(invalid) {
485        let pos = raw.as_str().find(invalid).unwrap();
486        error.report_error(
487            ParseError::new("unexpected sign")
488                .with_context(Span::new_unchecked(0, raw.len()))
489                .with_expected(&[])
490                .with_unexpected(Span::new_unchecked(pos, pos + 1)),
491        );
492        value
493    } else {
494        value
495    };
496
497    let valid = radix.validator();
498    for (index, c) in value.char_indices() {
499        if !valid(c) && c != '_' {
500            let pos = value.offset_from(&raw.as_str()) + index;
501            error.report_error(
502                ParseError::new(radix.invalid_description())
503                    .with_context(Span::new_unchecked(0, raw.len()))
504                    .with_unexpected(Span::new_unchecked(pos, pos)),
505            );
506        }
507    }
508}
509
510pub(crate) fn decode_float_or_integer<'i>(
511    stream: &'i str,
512    raw: Raw<'i>,
513    kind: ScalarKind,
514    output: &mut dyn StringBuilder<'i>,
515    error: &mut dyn ErrorSink,
516) -> ScalarKind {
517    output.clear();
518
519    let underscore = "_";
520
521    if has_underscore(stream) {
522        if stream.starts_with(underscore) {
523            error.report_error(
524                ParseError::new("`_` may only go between digits")
525                    .with_context(Span::new_unchecked(0, raw.len()))
526                    .with_expected(&[])
527                    .with_unexpected(Span::new_unchecked(0, underscore.len())),
528            );
529        }
530        if 1 < stream.len() && stream.ends_with(underscore) {
531            let start = stream.offset_from(&raw.as_str());
532            let end = start + stream.len();
533            error.report_error(
534                ParseError::new("`_` may only go between digits")
535                    .with_context(Span::new_unchecked(0, raw.len()))
536                    .with_expected(&[])
537                    .with_unexpected(Span::new_unchecked(end - underscore.len(), end)),
538            );
539        }
540
541        for part in stream.split(underscore) {
542            let part_start = part.offset_from(&raw.as_str());
543            let part_end = part_start + part.len();
544
545            if 0 < part_start {
546                let first = part.as_bytes().first().copied().unwrap_or(b'0');
547                if !is_any_digit(first, kind) {
548                    let start = part_start - 1;
549                    let end = part_start;
550                    debug_assert_eq!(&raw.as_str()[start..end], underscore);
551                    error.report_error(
552                        ParseError::new("`_` may only go between digits")
553                            .with_context(Span::new_unchecked(0, raw.len()))
554                            .with_unexpected(Span::new_unchecked(start, end)),
555                    );
556                }
557            }
558            if 1 < part.len() && part_end < raw.len() {
559                let last = part.as_bytes().last().copied().unwrap_or(b'0');
560                if !is_any_digit(last, kind) {
561                    let start = part_end;
562                    let end = start + underscore.len();
563                    debug_assert_eq!(&raw.as_str()[start..end], underscore);
564                    error.report_error(
565                        ParseError::new("`_` may only go between digits")
566                            .with_context(Span::new_unchecked(0, raw.len()))
567                            .with_unexpected(Span::new_unchecked(start, end)),
568                    );
569                }
570            }
571
572            if part.is_empty() && part_start != 0 && part_end != raw.len() {
573                let start = part_start;
574                let end = start + 1;
575                error.report_error(
576                    ParseError::new("`_` may only go between digits")
577                        .with_context(Span::new_unchecked(0, raw.len()))
578                        .with_unexpected(Span::new_unchecked(start, end)),
579                );
580            }
581
582            if !part.is_empty() && !output.push_str(part) {
583                error.report_error(
584                    ParseError::new(ALLOCATION_ERROR)
585                        .with_unexpected(Span::new_unchecked(part_start, part_end)),
586                );
587            }
588        }
589    } else {
590        if !output.push_str(stream) {
591            error.report_error(
592                ParseError::new(ALLOCATION_ERROR)
593                    .with_unexpected(Span::new_unchecked(0, raw.len())),
594            );
595        }
596    }
597
598    kind
599}
600
601fn is_any_digit(b: u8, kind: ScalarKind) -> bool {
602    if kind == ScalarKind::Float {
603        is_dec_integer_digit(b)
604    } else {
605        is_any_integer_digit(b)
606    }
607}
608
609fn is_any_integer_digit(b: u8) -> bool {
610    (b'0'..=b'9', b'a'..=b'f', b'A'..=b'F').contains_token(b)
611}
612
613fn is_dec_integer_digit(b: u8) -> bool {
614    (b'0'..=b'9').contains_token(b)
615}
616
617fn has_underscore(raw: &str) -> bool {
618    raw.as_bytes().find_slice(b'_').is_some()
619}
620
621fn is_float(raw: &str) -> bool {
622    raw.as_bytes().find_slice((b'.', b'e', b'E')).is_some()
623}
624
625pub(crate) fn decode_as_is<'i>(
626    raw: Raw<'i>,
627    kind: ScalarKind,
628    output: &mut dyn StringBuilder<'i>,
629    error: &mut dyn ErrorSink,
630) -> ScalarKind {
631    let kind = decode_as(raw, raw.as_str(), kind, output, error);
632    kind
633}
634
635pub(crate) fn decode_as<'i>(
636    raw: Raw<'i>,
637    symbol: &'i str,
638    kind: ScalarKind,
639    output: &mut dyn StringBuilder<'i>,
640    error: &mut dyn ErrorSink,
641) -> ScalarKind {
642    output.clear();
643    if !output.push_str(symbol) {
644        error.report_error(
645            ParseError::new(ALLOCATION_ERROR).with_unexpected(Span::new_unchecked(0, raw.len())),
646        );
647    }
648    kind
649}
650
651pub(crate) fn decode_symbol<'i>(
652    raw: Raw<'i>,
653    symbol: &'static str,
654    kind: ScalarKind,
655    expected: &'static [Expected],
656    output: &mut dyn StringBuilder<'i>,
657    error: &mut dyn ErrorSink,
658) -> ScalarKind {
659    if raw.as_str() != symbol {
660        if raw.as_str().contains(" ") {
661            return decode_invalid(raw, output, error);
662        } else {
663            error.report_error(
664                ParseError::new(kind.invalid_description())
665                    .with_context(Span::new_unchecked(0, raw.len()))
666                    .with_expected(expected)
667                    .with_unexpected(Span::new_unchecked(0, raw.len())),
668            );
669        }
670    }
671
672    decode_as(raw, symbol, kind, output, error)
673}
674
675pub(crate) fn decode_invalid<'i>(
676    raw: Raw<'i>,
677    output: &mut dyn StringBuilder<'i>,
678    error: &mut dyn ErrorSink,
679) -> ScalarKind {
680    if raw.as_str().ends_with("'''") {
681        error.report_error(
682            ParseError::new("missing opening quote")
683                .with_context(Span::new_unchecked(0, raw.len()))
684                .with_expected(&[Expected::Literal(r#"'''"#)])
685                .with_unexpected(Span::new_unchecked(0, 0)),
686        );
687    } else if raw.as_str().ends_with(r#"""""#) {
688        error.report_error(
689            ParseError::new("missing opening quote")
690                .with_context(Span::new_unchecked(0, raw.len()))
691                .with_expected(&[Expected::Description("multi-line basic string")])
692                .with_expected(&[Expected::Literal(r#"""""#)])
693                .with_unexpected(Span::new_unchecked(0, 0)),
694        );
695    } else if raw.as_str().ends_with("'") {
696        error.report_error(
697            ParseError::new("missing opening quote")
698                .with_context(Span::new_unchecked(0, raw.len()))
699                .with_expected(&[Expected::Literal(r#"'"#)])
700                .with_unexpected(Span::new_unchecked(0, 0)),
701        );
702    } else if raw.as_str().ends_with(r#"""#) {
703        error.report_error(
704            ParseError::new("missing opening quote")
705                .with_context(Span::new_unchecked(0, raw.len()))
706                .with_expected(&[Expected::Literal(r#"""#)])
707                .with_unexpected(Span::new_unchecked(0, 0)),
708        );
709    } else {
710        error.report_error(
711            ParseError::new("string values must be quoted")
712                .with_context(Span::new_unchecked(0, raw.len()))
713                .with_expected(&[Expected::Description("literal string")])
714                .with_unexpected(Span::new_unchecked(0, raw.len())),
715        );
716    }
717
718    output.clear();
719    if !output.push_str(raw.as_str()) {
720        error.report_error(
721            ParseError::new(ALLOCATION_ERROR).with_unexpected(Span::new_unchecked(0, raw.len())),
722        );
723    }
724    ScalarKind::String
725}