1use std::ops::RangeInclusive;
2
3use winnow::combinator::peek;
4use winnow::combinator::separated1;
5use winnow::token::any;
6use winnow::token::take_while;
7
8use crate::key::Key;
9use crate::parser::errors::CustomError;
10use crate::parser::prelude::*;
11use crate::parser::strings::{basic_string, literal_string};
12use crate::parser::trivia::{from_utf8_unchecked, ws};
13use crate::repr::{Decor, Repr};
14use crate::InternalString;
15use crate::RawString;
16
17// key = simple-key / dotted-key
18// dotted-key = simple-key 1*( dot-sep simple-key )
19pub(crate) fn key(input: Input<'_>) -> IResult<Input<'_>, Vec<Key>, ParserError<'_>> {
20 separated1TryMap, …, …, …, …>, …, …, …, …, …, …>(
21 (ws.span(), simple_key, ws.span()).map(|(pre: Range, (raw: RawString, key: InternalString), suffix: Range)| {
22 KeyKey::new(key)
23 .with_repr_unchecked(Repr::new_unchecked(raw))
24 .with_decor(Decor::new(
25 prefix:RawString::with_span(pre),
26 suffix:RawString::with_span(suffix),
27 ))
28 }),
29 DOT_SEP,
30 )
31 .context(Context::Expression("key"))
32 .try_map(|k: Vec<_>| {
33 // Inserting the key will require recursion down the line
34 RecursionCheck::check_depth(k.len())?;
35 Ok::<_, CustomError>(k)
36 })
37 .parse_next(input)
38}
39
40// simple-key = quoted-key / unquoted-key
41// quoted-key = basic-string / literal-string
42pub(crate) fn simple_key(
43 input: Input<'_>,
44) -> IResult<Input<'_>, (RawString, InternalString), ParserError<'_>> {
45 dispatchMap) -> …, …, …, …>, …, …, …, …, …>! {peek(any);
46 crate::parser::strings::QUOTATION_MARK => basic_string
47 .map(|s: std::borrow::Cow<'_, str>| s.as_ref().into()),
48 crate::parser::strings::APOSTROPHE => literal_string.map(|s: &str| s.into()),
49 _ => unquoted_key.map(|s: &str| s.into()),
50 }
51 .with_span()
52 .map(|(k: InternalString, span: Range)| {
53 let raw: RawString = RawString::with_span(span);
54 (raw, k)
55 })
56 .parse_next(input)
57}
58
59// unquoted-key = 1*( ALPHA / DIGIT / %x2D / %x5F ) ; A-Z / a-z / 0-9 / - / _
60fn unquoted_key(input: Input<'_>) -> IResult<Input<'_>, &str, ParserError<'_>> {
61 take_whileMap, …>, …, …, …, …, …>(range:1.., UNQUOTED_CHAR)
62 .map(|b: &[u8]| unsafe { from_utf8_unchecked(bytes:b, safety_justification:"`is_unquoted_char` filters out on-ASCII") })
63 .parse_next(input)
64}
65
66pub(crate) fn is_unquoted_char(c: u8) -> bool {
67 use winnow::stream::ContainsToken;
68 UNQUOTED_CHAR.contains_token(c)
69}
70
71const UNQUOTED_CHAR: (
72 RangeInclusive<u8>,
73 RangeInclusive<u8>,
74 RangeInclusive<u8>,
75 u8,
76 u8,
77) = (b'A'..=b'Z', b'a'..=b'z', b'0'..=b'9', b'-', b'_');
78
79// dot-sep = ws %x2E ws ; . Period
80const DOT_SEP: u8 = b'.';
81
82#[cfg(test)]
83mod test {
84 use super::*;
85
86 #[test]
87 fn keys() {
88 let cases = [
89 ("a", "a"),
90 (r#""hello\n ""#, "hello\n "),
91 (r#"'hello\n '"#, "hello\\n "),
92 ];
93
94 for (input, expected) in cases {
95 dbg!(input);
96 let parsed = simple_key.parse(new_input(input));
97 assert_eq!(
98 parsed,
99 Ok((RawString::with_span(0..(input.len())), expected.into())),
100 "Parsing {input:?}"
101 );
102 }
103 }
104}
105