arblang 0.1.24

Visualize and modify Unix pipelines: pipe a stream in, arb spawns a dynamic TUI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//! `jvparse` — jq's JSON text parser, ported from jq 1.8's `src/jv_parse.c`.
//!
//! [`crate::jqlang::parse_json`] reads arb's input stream with a strict, fast,
//! literal-preserving reader and falls back to this one for what the strict reader
//! refuses, since jq reads its input with `jv_parse.c` too. `fromjson` needs this
//! reader outright, because both what it accepts and how it refuses are
//! observable: `"01"`, `"1."`, `".5"`, `"+1"` and `"Infinity"` parse, `"nan1"`
//! does not, and a refusal is a fixed sentence with a line and column
//! (`Unfinished JSON term at EOF at line 1, column 4`). This is that reader, transcribed from the non-streaming path
//! of `jv_parse.c` (`scan`, `parse_token`, `found_string`, `check_literal`,
//! `jv_parser_next`, `jv_parse_sized_custom_flags`) so the decisions and the
//! messages are jq's by construction rather than by matching a list of cases.

use crate::jqlang::{num_from_literal, JqVal};
use std::rc::Rc;

/// jq's `MAX_PARSING_DEPTH`.
const MAX_PARSING_DEPTH: usize = 10000;

/// One `stack` slot of jq's parser: an open container, or an object key whose
/// value has not arrived yet (jq pushes the key string itself).
enum Frame {
    Arr(Vec<JqVal>),
    Obj(Vec<(Rc<str>, JqVal)>),
    Key(Rc<str>),
}

#[derive(PartialEq)]
enum St {
    Normal,
    String,
    StringEscape,
}

struct Parser {
    stack: Vec<Frame>,
    next: Option<JqVal>,
    token: Vec<u8>,
    st: St,
    line: usize,
    column: usize,
}

/// `scan`'s result: nothing yet, a finished value, or a refusal.
enum Step {
    More,
    Done(JqVal),
}

type P<T> = Result<T, &'static str>;

impl Parser {
    /// `value()`: a second value with no separator is refused.
    fn value(&mut self, v: JqVal) -> P<()> {
        if self.next.is_some() {
            return Err("Expected separator between values");
        }
        self.next = Some(v);
        Ok(())
    }

    /// `parse_token()`: the six structural characters.
    fn token(&mut self, ch: u8) -> P<()> {
        match ch {
            b'[' | b'{' => {
                if self.stack.len() >= MAX_PARSING_DEPTH {
                    return Err("Exceeds depth limit for parsing");
                }
                if self.next.is_some() {
                    return Err("Expected separator between values");
                }
                self.stack.push(if ch == b'[' {
                    Frame::Arr(Vec::new())
                } else {
                    Frame::Obj(Vec::new())
                });
            }
            b':' => {
                let Some(next) = self.next.take() else {
                    return Err("Expected string key before ':'");
                };
                if !matches!(self.stack.last(), Some(Frame::Obj(_))) {
                    return Err("':' not as part of an object");
                }
                let JqVal::Str(k) = next else {
                    return Err("Object keys must be strings");
                };
                self.stack.push(Frame::Key(k));
            }
            b',' => {
                let Some(next) = self.next.take() else {
                    return Err("Expected value before ','");
                };
                match self.stack.last_mut() {
                    None => return Err("',' not as part of an object or array"),
                    Some(Frame::Arr(a)) => a.push(next),
                    Some(Frame::Key(_)) => self.close_pair(next),
                    // `{"a", "b"}`
                    Some(Frame::Obj(_)) => return Err("Objects must consist of key:value pairs"),
                }
            }
            b']' => {
                let Some(Frame::Arr(a)) = self.stack.last_mut() else {
                    return Err("Unmatched ']'");
                };
                match self.next.take() {
                    Some(v) => a.push(v),
                    // `[1,2,3,]`
                    None if !a.is_empty() => return Err("Expected another array element"),
                    None => {}
                }
                let Some(Frame::Arr(a)) = self.stack.pop() else {
                    unreachable!("checked above")
                };
                self.next = Some(JqVal::arr(a));
            }
            b'}' => {
                if self.stack.is_empty() {
                    return Err("Unmatched '}'");
                }
                match self.next.take() {
                    Some(v) => {
                        if !matches!(self.stack.last(), Some(Frame::Key(_))) {
                            return Err("Objects must consist of key:value pairs");
                        }
                        self.close_pair(v);
                    }
                    None => match self.stack.last() {
                        Some(Frame::Obj(m)) if !m.is_empty() => {
                            return Err("Expected another key-value pair")
                        }
                        Some(Frame::Obj(_)) => {}
                        _ => return Err("Unmatched '}'"),
                    },
                }
                let Some(Frame::Obj(m)) = self.stack.pop() else {
                    unreachable!("a key frame always sits on an object frame")
                };
                self.next = Some(JqVal::obj(m));
            }
            _ => unreachable!("token() is only called for STRUCTURE characters"),
        }
        Ok(())
    }

    /// Set the pending key on the object beneath it (`jv_object_set`: a repeated
    /// key keeps its first position and takes the last value).
    fn close_pair(&mut self, v: JqVal) {
        let Some(Frame::Key(k)) = self.stack.pop() else {
            unreachable!("caller checked for a key frame")
        };
        let Some(Frame::Obj(m)) = self.stack.last_mut() else {
            unreachable!("a key frame always sits on an object frame")
        };
        match m.iter_mut().find(|(ek, _)| *ek == k) {
            Some(slot) => slot.1 = v,
            None => m.push((k, v)),
        }
    }

    /// `found_string()`: decode the escapes in the buffered string body.
    fn found_string(&mut self) -> P<()> {
        let t = std::mem::take(&mut self.token);
        let mut out = String::with_capacity(t.len());
        let mut raw: Vec<u8> = Vec::new();
        let mut i = 0;
        let flush = |raw: &mut Vec<u8>, out: &mut String| {
            out.push_str(&String::from_utf8_lossy(raw));
            raw.clear();
        };
        while i < t.len() {
            let c = t[i];
            i += 1;
            if c != b'\\' {
                if c < 0x20 {
                    return Err("Invalid string: control characters from U+0000 through U+001F must be escaped");
                }
                raw.push(c);
                continue;
            }
            let Some(&e) = t.get(i) else {
                return Err("Expected escape character at end of string");
            };
            i += 1;
            flush(&mut raw, &mut out);
            match e {
                b'\\' | b'"' | b'/' => out.push(e as char),
                b'b' => out.push('\u{8}'),
                b'f' => out.push('\u{c}'),
                b't' => out.push('\t'),
                b'n' => out.push('\n'),
                b'r' => out.push('\r'),
                b'u' => {
                    let Some(hex) = t.get(i..i + 4) else {
                        return Err("Invalid \\uXXXX escape");
                    };
                    let mut cp = unhex4(hex).ok_or("Invalid characters in \\uXXXX escape")?;
                    i += 4;
                    if (0xD800..=0xDBFF).contains(&cp) {
                        if t.len() < i + 6 || t[i] != b'\\' || t[i + 1] != b'u' {
                            return Err("Invalid \\uXXXX\\uXXXX surrogate pair escape");
                        }
                        let lo = unhex4(&t[i + 2..i + 6]).unwrap_or(0);
                        if !(0xDC00..=0xDFFF).contains(&lo) {
                            return Err("Invalid \\uXXXX\\uXXXX surrogate pair escape");
                        }
                        i += 6;
                        cp = 0x10000 + (((cp - 0xD800) << 10) | (lo - 0xDC00));
                    }
                    // A lone low surrogate encodes to bytes `jv_string_sized`
                    // repairs to U+FFFD, as does anything past U+10FFFF.
                    out.push(char::from_u32(cp).unwrap_or('\u{FFFD}'));
                }
                _ => return Err("Invalid escape"),
            }
        }
        flush(&mut raw, &mut out);
        self.value(JqVal::str(out))
    }

    /// `check_literal()`: the buffered bare token is `true`/`false`/`null` or a
    /// number literal.
    fn check_literal(&mut self) -> P<()> {
        if self.token.is_empty() {
            return Ok(());
        }
        let t = std::mem::take(&mut self.token);
        let pattern: Option<(&[u8], JqVal)> = match t[0] {
            b't' => Some((b"true", JqVal::Bool(true))),
            b'f' => Some((b"false", JqVal::Bool(false))),
            b'\'' => return Err("Invalid string literal; expected \", but got '"),
            // `n` followed by `u` is `null`; anything else (`nan`) is a number.
            b'n' if t.get(1) == Some(&b'u') => Some((b"null", JqVal::Null)),
            _ => None,
        };
        let v = match pattern {
            Some((p, v)) if t == p => v,
            Some(_) => return Err("Invalid literal"),
            None => number_literal(&t).ok_or("Invalid numeric literal")?,
        };
        self.value(v)
    }

    /// `parse_check_done()`: a complete top-level value is ready.
    fn check_done(&mut self) -> Option<JqVal> {
        if self.stack.is_empty() {
            self.next.take()
        } else {
            None
        }
    }

    /// `scan()`: feed one byte.
    fn scan(&mut self, ch: u8) -> P<Step> {
        self.column += 1;
        if ch == b'\n' {
            self.line += 1;
            self.column = 0;
        }
        let mut answer = Step::More;
        if self.st == St::Normal {
            let structure = matches!(ch, b'[' | b',' | b']' | b'{' | b':' | b'}');
            let ws = matches!(ch, b' ' | b'\t' | b'\r' | b'\n');
            if structure || ws || ch == b'"' {
                self.check_literal()?;
                if let Some(v) = self.check_done() {
                    answer = Step::Done(v);
                }
            }
            if ch == b'"' {
                self.st = St::String;
            } else if structure {
                self.token(ch)?;
            } else if !ws {
                self.token.push(ch);
            }
            if let Some(v) = self.check_done() {
                answer = Step::Done(v);
            }
        } else if ch == b'"' && self.st == St::String {
            self.found_string()?;
            self.st = St::Normal;
            if let Some(v) = self.check_done() {
                answer = Step::Done(v);
            }
        } else {
            self.token.push(ch);
            self.st = if ch == b'\\' && self.st == St::String {
                St::StringEscape
            } else {
                St::String
            };
        }
        Ok(answer)
    }
}

pub(crate) fn unhex4(h: &[u8]) -> Option<u32> {
    let s = std::str::from_utf8(h).ok()?;
    if !s.bytes().all(|b| b.is_ascii_hexdigit()) {
        return None;
    }
    u32::from_str_radix(s, 16).ok()
}

/// A bare token as jq 1.8's decNumber literal reader takes it: an optional
/// sign, then digits with at most one `.` (at least one digit) and an optional
/// exponent with at least one digit — or, in any case, `nan`, `snan`, `inf` or
/// `infinity`. Measured against jq 1.8.2: `01`, `1.`, `.5`, `+1` and `-sNaN`
/// parse; `nan1`, `1e` and `0x1` do not.
fn number_literal(t: &[u8]) -> Option<JqVal> {
    let text = std::str::from_utf8(t).ok()?;
    let (neg, body) = match text.as_bytes().first() {
        Some(b'-') => (true, &text[1..]),
        Some(b'+') => (false, &text[1..]),
        _ => (false, text),
    };
    match body.to_ascii_lowercase().as_str() {
        "nan" | "snan" => return Some(JqVal::num(f64::NAN)),
        "inf" | "infinity" => {
            return Some(JqVal::num(if neg {
                f64::NEG_INFINITY
            } else {
                f64::INFINITY
            }))
        }
        _ => {}
    }
    let b = body.as_bytes();
    let mut i = 0;
    let mut digits = 0;
    while b.get(i).is_some_and(u8::is_ascii_digit) {
        i += 1;
        digits += 1;
    }
    if b.get(i) == Some(&b'.') {
        i += 1;
        while b.get(i).is_some_and(u8::is_ascii_digit) {
            i += 1;
            digits += 1;
        }
    }
    if digits == 0 {
        return None;
    }
    if matches!(b.get(i), Some(b'e' | b'E')) {
        i += 1;
        if matches!(b.get(i), Some(b'+' | b'-')) {
            i += 1;
        }
        let start = i;
        while b.get(i).is_some_and(u8::is_ascii_digit) {
            i += 1;
        }
        if i == start {
            return None;
        }
    }
    if i != b.len() {
        return None;
    }
    // Rust's float parser refuses a bare `1.` and `.5` is fine; normalize the
    // one shape it rejects so the double is right.
    let for_f64 = text.strip_suffix('.').unwrap_or(text);
    let for_f64 = for_f64.replace(".e", "e").replace(".E", "E");
    let n: f64 = for_f64.parse().ok()?;
    Some(num_from_literal(n, &for_f64))
}

/// `jv_parser_next()` over a whole buffer: the next value, `Ok(None)` at a clean
/// end, or jq's message for the first refusal.
fn next_value(p: &mut Parser, b: &[u8], pos: &mut usize) -> Result<Option<JqVal>, String> {
    while *pos < b.len() {
        let ch = b[*pos];
        *pos += 1;
        match p.scan(ch) {
            Ok(Step::More) => {}
            Ok(Step::Done(v)) => return Ok(Some(v)),
            Err(msg) => {
                return Err(format!("{msg} at line {}, column {}", p.line, p.column));
            }
        }
    }
    // At EOF.
    if p.st != St::Normal {
        return Err(format!(
            "Unfinished string at EOF at line {}, column {}",
            p.line, p.column
        ));
    }
    if let Err(msg) = p.check_literal() {
        return Err(format!(
            "{msg} at EOF at line {}, column {}",
            p.line, p.column
        ));
    }
    if !p.stack.is_empty() {
        return Err(format!(
            "Unfinished JSON term at EOF at line {}, column {}",
            p.line, p.column
        ));
    }
    Ok(p.next.take())
}

/// `jv_parse_sized()`: exactly one JSON value, by jq's rules, or jq's message
/// (without the ` (while parsing '…')` suffix the caller adds).
pub fn parse(src: &str) -> Result<JqVal, String> {
    let mut b = src.as_bytes();
    // `jv_parser_set_buf` strips a UTF-8 byte-order mark; a partial one is an
    // error of its own.
    const BOM: &[u8] = b"\xEF\xBB\xBF";
    let bom = b.iter().zip(BOM).take_while(|(x, y)| x == y).count();
    if bom == BOM.len() {
        b = &b[3..];
    } else if bom > 0 {
        return Err("Malformed BOM".into());
    }
    let mut p = Parser {
        stack: Vec::new(),
        next: None,
        token: Vec::new(),
        st: St::Normal,
        line: 1,
        column: 0,
    };
    let mut pos = 0;
    match next_value(&mut p, b, &mut pos)? {
        Some(v) => match next_value(&mut p, b, &mut pos)? {
            Some(_) => Err("Unexpected extra JSON values".into()),
            None => Ok(v),
        },
        None => Err("Expected JSON value".into()),
    }
}

#[cfg(test)]
mod tests {
    use super::parse;
    use crate::jqlang::render;

    fn ok(s: &str) -> String {
        render(&parse(s).unwrap_or_else(|e| panic!("{s}: {e}")))
    }

    #[test]
    fn accepts_what_jq_accepts() {
        assert_eq!(
            ok(r#"{"b":1.50,"a":[1E5,"xé"]}"#),
            r#"{"b":1.50,"a":[1E+5,"xé"]}"#
        );
        assert_eq!(ok("01"), "1");
        assert_eq!(ok("1."), "1");
        assert_eq!(ok(".5"), "0.5");
        assert_eq!(ok("+1"), "1");
        assert_eq!(ok("1.e5"), "1E+5");
        assert_eq!(ok("-sNaN"), "null");
        assert_eq!(ok(r#"{"a":1,"a":2}"#), r#"{"a":2}"#);
    }

    #[test]
    fn refuses_with_jq_wording() {
        let err = |s: &str| parse(s).unwrap_err();
        assert_eq!(
            err("[1,2"),
            "Unfinished JSON term at EOF at line 1, column 4"
        );
        assert_eq!(err("1 2"), "Unexpected extra JSON values");
        assert_eq!(
            err("nan1"),
            "Invalid numeric literal at EOF at line 1, column 4"
        );
        assert_eq!(
            err("[1,]"),
            "Expected another array element at line 1, column 4"
        );
        assert_eq!(err("tru e"), "Invalid literal at line 1, column 4");
        assert_eq!(err(" "), "Expected JSON value");
        assert_eq!(err("{\"a\":[}"), "Unmatched '}' at line 1, column 7");
    }
}