toml_edit/parser/
key.rs

1use std::ops::RangeInclusive;
2
3use nom8::bytes::any;
4use nom8::bytes::take_while1;
5use nom8::combinator::peek;
6use nom8::multi::separated_list1;
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    separated_list1(
21        DOT_SEP,
22        (ws.span(), simple_key, ws.span()).map(|(pre, (raw, key), suffix)| {
23            Key::new(key)
24                .with_repr_unchecked(Repr::new_unchecked(raw))
25                .with_decor(Decor::new(
26                    RawString::with_span(pre),
27                    RawString::with_span(suffix),
28                ))
29        }),
30    )
31    .context(Context::Expression("key"))
32    .map_res(|k| {
33        // Inserting the key will require recursion down the line
34        RecursionCheck::check_depth(k.len())?;
35        Ok::<_, CustomError>(k)
36    })
37    .parse(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    dispatch! {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, span)| {
53        let raw = RawString::with_span(span);
54        (raw, k)
55    })
56    .parse(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_while1(UNQUOTED_CHAR)
62        .map(|b| unsafe { from_utf8_unchecked(b, "`is_unquoted_char` filters out on-ASCII") })
63        .parse(input)
64}
65
66pub(crate) fn is_unquoted_char(c: u8) -> bool {
67    use nom8::input::FindToken;
68    UNQUOTED_CHAR.find_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)).finish();
97            assert_eq!(
98                parsed,
99                Ok((RawString::with_span(0..(input.len())), expected.into())),
100                "Parsing {input:?}"
101            );
102        }
103    }
104}