netidx_value/
parser.rs

1use crate::{pbuf::PBytes, Abstract, ValArray, Value};
2use arcstr::ArcStr;
3use base64::{engine::general_purpose::STANDARD as BASE64, Engine};
4use bytes::Bytes;
5use combine::{
6    attempt, between, choice, eof, from_str, look_ahead, many1, none_of, not_followed_by,
7    one_of, optional, parser,
8    parser::{
9        char::{digit, spaces, string},
10        combinator::recognize,
11        range::{take_while, take_while1},
12        repeat::escaped,
13    },
14    sep_by, sep_by1,
15    stream::{position, Range},
16    token, unexpected_any, EasyParser, ParseError, Parser, RangeStream,
17};
18use compact_str::CompactString;
19use escaping::Escape;
20use netidx_core::pack::Pack;
21use poolshark::local::LPooled;
22use rust_decimal::Decimal;
23use std::{borrow::Cow, str::FromStr, sync::LazyLock, time::Duration};
24use triomphe::Arc;
25
26// sep_by1, but a separator terminator is allowed, and ignored
27pub fn sep_by1_tok<I, O, OC, EP, SP, TP>(
28    p: EP,
29    sep: SP,
30    term: TP,
31) -> impl Parser<I, Output = OC>
32where
33    I: RangeStream<Token = char>,
34    I::Error: ParseError<I::Token, I::Range, I::Position>,
35    I::Range: Range,
36    OC: Extend<O> + Default,
37    SP: Parser<I>,
38    EP: Parser<I, Output = O>,
39    TP: Parser<I>,
40{
41    sep_by1(choice((look_ahead(term).map(|_| None::<O>), p.map(Some))), sep).map(
42        |mut e: LPooled<Vec<Option<O>>>| {
43            let mut res = OC::default();
44            res.extend(e.drain(..).filter_map(|e| e));
45            res
46        },
47    )
48}
49
50// sep_by, but a separator terminator is allowed, and ignored
51pub fn sep_by_tok<I, O, OC, EP, SP, TP>(
52    p: EP,
53    sep: SP,
54    term: TP,
55) -> impl Parser<I, Output = OC>
56where
57    I: RangeStream<Token = char>,
58    I::Error: ParseError<I::Token, I::Range, I::Position>,
59    I::Range: Range,
60    OC: Extend<O> + Default,
61    SP: Parser<I>,
62    EP: Parser<I, Output = O>,
63    TP: Parser<I>,
64{
65    sep_by(choice((look_ahead(term).map(|_| None::<O>), p.map(Some))), sep).map(
66        |mut e: LPooled<Vec<Option<O>>>| {
67            let mut res = OC::default();
68            res.extend(e.drain(..).filter_map(|e| e));
69            res
70        },
71    )
72}
73
74fn sptoken<I>(t: char) -> impl Parser<I, Output = char>
75where
76    I: RangeStream<Token = char>,
77    I::Error: ParseError<I::Token, I::Range, I::Position>,
78    I::Range: Range,
79{
80    spaces().with(token(t))
81}
82
83fn spstring<I>(t: &'static str) -> impl Parser<I, Output = &'static str>
84where
85    I: RangeStream<Token = char>,
86    I::Error: ParseError<I::Token, I::Range, I::Position>,
87    I::Range: Range,
88{
89    spaces().with(string(t))
90}
91
92fn csep<I>() -> impl Parser<I, Output = char>
93where
94    I: RangeStream<Token = char>,
95    I::Error: ParseError<I::Token, I::Range, I::Position>,
96    I::Range: Range,
97{
98    attempt(spaces().with(token(','))).skip(spaces())
99}
100
101fn should_escape_generic(c: char) -> bool {
102    c.is_control()
103}
104
105pub const VAL_MUST_ESC: [char; 2] = ['\\', '"'];
106pub static VAL_ESC: LazyLock<Escape> = LazyLock::new(|| {
107    Escape::new(
108        '\\',
109        &['\\', '"', '\n', '\r', '\0', '\t'],
110        &[('\n', "n"), ('\r', "r"), ('\t', "t"), ('\0', "0")],
111        Some(should_escape_generic),
112    )
113    .unwrap()
114});
115
116pub fn escaped_string<I>(
117    must_esc: &'static [char],
118    esc: &Escape,
119) -> impl Parser<I, Output = String>
120where
121    I: RangeStream<Token = char>,
122    I::Error: ParseError<I::Token, I::Range, I::Position>,
123    I::Range: Range,
124{
125    recognize(escaped(
126        take_while1(move |c| !must_esc.contains(&c)),
127        esc.get_escape_char(),
128        one_of(
129            esc.get_tr()
130                .iter()
131                .filter_map(|(_, s)| s.chars().next())
132                .chain(must_esc.iter().copied()),
133        ),
134    ))
135    .map(|s| match esc.unescape(&s) {
136        Cow::Borrowed(_) => s, // it didn't need unescaping, so just return it
137        Cow::Owned(s) => s,
138    })
139}
140
141fn quoted<I>(
142    must_escape: &'static [char],
143    esc: &Escape,
144) -> impl Parser<I, Output = String>
145where
146    I: RangeStream<Token = char>,
147    I::Error: ParseError<I::Token, I::Range, I::Position>,
148    I::Range: Range,
149{
150    between(token('"'), token('"'), escaped_string(must_escape, esc))
151}
152
153fn uint<I, T: FromStr + Clone + Copy>() -> impl Parser<I, Output = T>
154where
155    I: RangeStream<Token = char>,
156    I::Error: ParseError<I::Token, I::Range, I::Position>,
157    I::Range: Range,
158{
159    many1(digit()).then(|s: CompactString| match s.parse::<T>() {
160        Ok(i) => combine::value(i).right(),
161        Err(_) => unexpected_any("invalid unsigned integer").left(),
162    })
163}
164
165pub fn int<I, T: FromStr + Clone + Copy>() -> impl Parser<I, Output = T>
166where
167    I: RangeStream<Token = char>,
168    I::Error: ParseError<I::Token, I::Range, I::Position>,
169    I::Range: Range,
170{
171    recognize((optional(token('-')), take_while1(|c: char| c.is_digit(10)))).then(
172        |s: CompactString| match s.parse::<T>() {
173            Ok(i) => combine::value(i).right(),
174            Err(_) => unexpected_any("invalid signed integer").left(),
175        },
176    )
177}
178
179fn flt<I, T: FromStr + Clone + Copy>() -> impl Parser<I, Output = T>
180where
181    I: RangeStream<Token = char>,
182    I::Error: ParseError<I::Token, I::Range, I::Position>,
183    I::Range: Range,
184{
185    choice((
186        attempt(recognize((
187            optional(token('-')),
188            take_while1(|c: char| c.is_digit(10)),
189            optional(token('.')),
190            take_while(|c: char| c.is_digit(10)),
191            token('e'),
192            optional(token('-')),
193            take_while1(|c: char| c.is_digit(10)),
194        ))),
195        attempt(recognize((
196            optional(token('-')),
197            take_while1(|c: char| c.is_digit(10)),
198            token('.'),
199            take_while(|c: char| c.is_digit(10)),
200        ))),
201    ))
202    .then(|s: CompactString| match s.parse::<T>() {
203        Ok(i) => combine::value(i).right(),
204        Err(_) => unexpected_any("invalid float").left(),
205    })
206}
207
208fn base64<I>() -> impl Parser<I, Output = LPooled<Vec<u8>>>
209where
210    I: RangeStream<Token = char>,
211    I::Error: ParseError<I::Token, I::Range, I::Position>,
212    I::Range: Range,
213{
214    recognize((
215        take_while(|c: char| c.is_ascii_alphanumeric() || c == '+' || c == '/'),
216        take_while(|c: char| c == '='),
217    ))
218    .then(|s: LPooled<String>| {
219        let s = if &*s == "==" { LPooled::take() } else { s };
220        let mut buf: LPooled<Vec<u8>> = LPooled::take();
221        match BASE64.decode_vec(&*s, &mut buf) {
222            Ok(()) => combine::value(buf).right(),
223            Err(_) => unexpected_any("base64 decode failed").left(),
224        }
225    })
226}
227
228fn constant<I>(typ: &'static str) -> impl Parser<I, Output = ()>
229where
230    I: RangeStream<Token = char>,
231    I::Error: ParseError<I::Token, I::Range, I::Position>,
232    I::Range: Range,
233{
234    string(typ).with(spaces()).with(token(':')).with(spaces()).map(|_| ())
235}
236
237pub fn close_expr<I>() -> impl Parser<I, Output = ()>
238where
239    I: RangeStream<Token = char>,
240    I::Error: ParseError<I::Token, I::Range, I::Position>,
241    I::Range: Range,
242{
243    not_followed_by(none_of([' ', '\n', '\t', ';', ')', ',', ']', '}', '"']))
244}
245
246fn value_<I>(must_escape: &'static [char], esc: &Escape) -> impl Parser<I, Output = Value>
247where
248    I: RangeStream<Token = char>,
249    I::Error: ParseError<I::Token, I::Range, I::Position>,
250    I::Range: Range,
251{
252    spaces().with(choice((
253        choice((
254            attempt(constant("u8")).with(uint::<_, u8>().map(Value::U8)),
255            attempt(constant("u16")).with(uint::<_, u16>().map(Value::U16)),
256            attempt(constant("u32")).with(uint::<_, u32>().map(Value::U32)),
257            constant("u64").with(uint::<_, u64>().map(Value::U64)),
258            attempt(constant("i8")).with(int::<_, i8>().map(Value::I8)),
259            attempt(constant("i16")).with(int::<_, i16>().map(Value::I16)),
260            attempt(constant("i32")).with(int::<_, i32>().map(Value::I32)),
261            constant("i64").with(int::<_, i64>().map(Value::I64)),
262            attempt(constant("v32")).with(uint::<_, u32>().map(Value::V32)),
263            constant("v64").with(uint::<_, u64>().map(Value::V64)),
264            attempt(constant("z32")).with(int::<_, i32>().map(Value::Z32)),
265            constant("z64").with(int::<_, i64>().map(Value::Z64)),
266            attempt(constant("f32")).with(flt::<_, f32>().map(Value::F32)),
267            attempt(constant("f64")).with(flt::<_, f64>().map(Value::F64)),
268        )),
269        between(
270            token('['),
271            sptoken(']'),
272            sep_by_tok(value(must_escape, esc), csep(), token(']')),
273        )
274        .map(|mut vals: LPooled<Vec<Value>>| {
275            Value::Array(ValArray::from_iter_exact(vals.drain(..)))
276        }),
277        between(
278            token('{'),
279            sptoken('}'),
280            sep_by_tok(
281                (value(must_escape, esc), spstring("=>").with(value(must_escape, esc))),
282                csep(),
283                token('}'),
284            )
285            .map(|mut vals: LPooled<Vec<(Value, Value)>>| {
286                Value::Map(immutable_chunkmap::map::Map::from_iter(vals.drain(..)))
287            }),
288        ),
289        quoted(must_escape, esc).map(|s| Value::String(ArcStr::from(s))),
290        flt::<_, f64>().map(Value::F64),
291        int::<_, i64>().map(Value::I64),
292        string("true").map(|_| Value::Bool(true)),
293        string("false").map(|_| Value::Bool(false)),
294        string("null").map(|_| Value::Null),
295        constant("bytes")
296            .with(base64())
297            .map(|v| Value::Bytes(PBytes::new(Bytes::from(LPooled::detach(v))))),
298        constant("abstract").with(base64()).then(|v| {
299            match Abstract::decode(&mut &v[..]) {
300                Ok(a) => combine::value(Value::Abstract(a)).right(),
301                Err(_) => unexpected_any("failed to unpack abstract").left(),
302            }
303        }),
304        constant("error")
305            .with(value(must_escape, esc))
306            .map(|v| Value::Error(Arc::new(v))),
307        attempt(constant("decimal"))
308            .with(flt::<_, Decimal>())
309            .map(|d| Value::Decimal(Arc::new(d))),
310        attempt(constant("datetime"))
311            .with(from_str(quoted(must_escape, esc)))
312            .map(|d| Value::DateTime(Arc::new(d))),
313        constant("duration")
314            .with(flt::<_, f64>().and(choice((
315                string("ns"),
316                string("us"),
317                string("ms"),
318                string("s"),
319            ))))
320            .map(|(n, suffix)| {
321                let d = match suffix {
322                    "ns" => Duration::from_secs_f64(n / 1e9),
323                    "us" => Duration::from_secs_f64(n / 1e6),
324                    "ms" => Duration::from_secs_f64(n / 1e3),
325                    "s" => Duration::from_secs_f64(n),
326                    _ => unreachable!(),
327                };
328                Value::Duration(Arc::new(d))
329            }),
330    )))
331}
332
333parser! {
334    pub fn value['a, I](
335        must_escape: &'static [char],
336        esc: &'a Escape
337    )(I) -> Value
338    where [I: RangeStream<Token = char>, I::Range: Range]
339    {
340        value_(must_escape, esc)
341    }
342}
343
344pub fn parse_value(s: &str) -> anyhow::Result<Value> {
345    value(&VAL_MUST_ESC, &VAL_ESC)
346        .skip(spaces())
347        .skip(eof())
348        .easy_parse(position::Stream::new(s))
349        .map(|(r, _)| r)
350        .map_err(|e| anyhow::anyhow!(format!("{}", e)))
351}
352
353#[cfg(test)]
354mod tests {
355    use arcstr::literal;
356
357    use crate::Map;
358
359    use super::*;
360
361    #[test]
362    fn parse() {
363        assert_eq!(Value::U32(23), parse_value("u32:23").unwrap());
364        assert_eq!(Value::V32(42), parse_value("v32:42").unwrap());
365        assert_eq!(Value::I32(-10), parse_value("i32:-10").unwrap());
366        assert_eq!(Value::I32(12321), parse_value("i32:12321").unwrap());
367        assert_eq!(Value::Z32(-99), parse_value("z32:-99").unwrap());
368        assert_eq!(Value::U64(100), parse_value("u64:100").unwrap());
369        assert_eq!(Value::V64(100), parse_value("v64:100").unwrap());
370        assert_eq!(Value::I64(-100), parse_value("i64:-100").unwrap());
371        assert_eq!(Value::I64(-100), parse_value("-100").unwrap());
372        assert_eq!(Value::I64(100), parse_value("i64:100").unwrap());
373        assert_eq!(Value::I64(100), parse_value("100").unwrap());
374        assert_eq!(Value::Z64(-100), parse_value("z64:-100").unwrap());
375        assert_eq!(Value::Z64(100), parse_value("z64:100").unwrap());
376        assert_eq!(Value::F32(3.1415), parse_value("f32:3.1415").unwrap());
377        assert_eq!(Value::F32(675.6), parse_value("f32:675.6").unwrap());
378        assert_eq!(Value::F32(42.3435), parse_value("f32:42.3435").unwrap());
379        assert_eq!(Value::F32(1.123e9), parse_value("f32:1.123e9").unwrap());
380        assert_eq!(Value::F32(1e9), parse_value("f32:1e9").unwrap());
381        assert_eq!(Value::F32(21.2443e-6), parse_value("f32:21.2443e-6").unwrap());
382        assert_eq!(Value::F32(3.), parse_value("f32:3.").unwrap());
383        assert_eq!(Value::F64(3.1415), parse_value("f64:3.1415").unwrap());
384        assert_eq!(Value::F64(3.1415), parse_value("3.1415").unwrap());
385        assert_eq!(Value::F64(1.123e9), parse_value("1.123e9").unwrap());
386        assert_eq!(Value::F64(1e9), parse_value("1e9").unwrap());
387        assert_eq!(Value::F64(21.2443e-6), parse_value("21.2443e-6").unwrap());
388        assert_eq!(Value::F64(3.), parse_value("f64:3.").unwrap());
389        assert_eq!(Value::F64(3.), parse_value("3.").unwrap());
390        let c = ArcStr::from(r#"I've got a lovely "bunch" of (coconuts)"#);
391        let s = r#""I've got a lovely \"bunch\" of (coconuts)""#;
392        assert_eq!(Value::String(c), parse_value(s).unwrap());
393        let c = ArcStr::new();
394        assert_eq!(Value::String(c), parse_value(r#""""#).unwrap());
395        let c = ArcStr::from(r#"""#);
396        let s = r#""\"""#;
397        assert_eq!(Value::String(c), parse_value(s).unwrap());
398        assert_eq!(Value::Bool(true), parse_value("true").unwrap());
399        assert_eq!(Value::Bool(true), parse_value("true ").unwrap());
400        assert_eq!(Value::Bool(false), parse_value("false").unwrap());
401        assert_eq!(Value::Null, parse_value("null").unwrap());
402        assert_eq!(
403            Value::error(literal!("error")),
404            parse_value(r#"error:"error""#).unwrap()
405        );
406        let a = ValArray::from_iter_exact(
407            [Value::I64(42), Value::String(literal!("hello world"))].into_iter(),
408        );
409        assert_eq!(
410            Value::Array(a.clone()),
411            parse_value(r#"[42, "hello world", ]"#).unwrap()
412        );
413        assert_eq!(Value::Array(a), parse_value(r#"[42, "hello world"]"#).unwrap());
414        let m = Map::from_iter([
415            (Value::I64(42), Value::String(literal!("hello world"))),
416            (Value::String(literal!("hello world")), Value::I64(42)),
417        ]);
418        assert_eq!(
419            Value::Map(m.clone()),
420            parse_value(r#"{ 42 => "hello world", "hello world" => 42, }"#).unwrap()
421        );
422        assert_eq!(
423            Value::Map(m.clone()),
424            parse_value(r#"{ 42 => "hello world", "hello world" => 42}"#).unwrap()
425        )
426    }
427}