predicator-rs 0.1.0

The Rust port of the predicator condition language: compiles a condition to a small instruction set and evaluates it on a stack machine, checked against the reference conformance corpus.
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
use super::tree::{
    Arithmetic, Block, Comparison, Direction, Entry, Expression, KeyStyle, Literal, Membership,
    Node, Presence, Program, Statement, Unary,
};
use super::*;
use crate::json::float_text;
use crate::lexer::{Quote, tokenize};
use serde_json::{Map as JsonMap, Value as Json, json};
use std::collections::BTreeMap;
use std::path::PathBuf;

fn at(position: Position) -> Json {
    json!([position.line, position.column])
}

fn over(span: Span) -> Json {
    json!([at(span.start), at(span.end)])
}

/// A node in the fixture's shape: its tag, position and span, and the
/// fields the tag carries.
fn shaped(tag: &str, position: Position, span: Span, fields: Json) -> Json {
    let mut object = JsonMap::new();
    object.insert("node".to_owned(), json!(tag));
    object.insert("at".to_owned(), at(position));
    object.insert("span".to_owned(), over(span));
    if let Json::Object(fields) = fields {
        object.extend(fields);
    }
    Json::Object(object)
}

fn literal(value: &Literal) -> String {
    match value {
        Literal::Integer(value) => format!("i:{value}"),
        Literal::Float(value) => format!("f:{}", float_text(*value)),
        Literal::Boolean(value) => format!("b:{value}"),
        Literal::Undefined => "undefined".to_owned(),
        Literal::Null => "null".to_owned(),
        Literal::Date(date) => format!("d:{date}"),
        Literal::DateTime(instant, precision) => format!(
            "dt:{}:{precision}",
            i128::from(instant.unix_seconds()) * 1_000_000 + i128::from(instant.microsecond())
        ),
    }
}

fn quote(quote: Quote) -> &'static str {
    match quote {
        Quote::Double => "double",
        Quote::Single => "single",
    }
}

fn comparison(operator: Comparison) -> &'static str {
    match operator {
        Comparison::Gt => "gt",
        Comparison::Lt => "lt",
        Comparison::Gte => "gte",
        Comparison::Lte => "lte",
        Comparison::EqualEqual => "equal_equal",
        Comparison::Ne => "ne",
        Comparison::StrictEq => "strict_eq",
        Comparison::StrictNe => "strict_ne",
    }
}

fn arithmetic(operator: Arithmetic) -> &'static str {
    match operator {
        Arithmetic::Add => "add",
        Arithmetic::Subtract => "subtract",
        Arithmetic::Multiply => "multiply",
        Arithmetic::Divide => "divide",
        Arithmetic::Modulo => "modulo",
    }
}

fn membership(operator: Membership) -> &'static str {
    match operator {
        Membership::In => "in",
        Membership::Contains => "contains",
        Membership::Intersects => "intersects",
    }
}

fn direction(direction: Direction) -> &'static str {
    match direction {
        Direction::Ago => "ago",
        Direction::Future => "future",
        Direction::Next => "next",
        Direction::Last => "last",
    }
}

fn entry(entry: &Entry) -> Json {
    let style = match entry.style {
        KeyStyle::Bare => "identifier",
        KeyStyle::Quoted(quoted) => quote(quoted),
    };
    json!({
        "key": entry.key,
        "style": style,
        "at": at(entry.position),
        "span": over(entry.span),
        "value": node(&entry.value),
    })
}

/// An expression node in the fixture's shape.
fn node(node: &Node) -> Json {
    let (tag, fields) = match &node.kind {
        Expression::Literal(value) => ("literal", json!({"value": literal(value)})),
        Expression::String { value, quote: q } => {
            ("string", json!({"value": value, "quote": quote(*q)}))
        }
        Expression::Identifier(name) => ("identifier", json!({"name": name})),
        Expression::Comparison {
            operator,
            left,
            right,
        } => (
            "comparison",
            json!({"op": comparison(*operator), "left": self::node(left), "right": self::node(right)}),
        ),
        Expression::Arithmetic {
            operator,
            left,
            right,
        } => (
            "arithmetic",
            json!({"op": arithmetic(*operator), "left": self::node(left), "right": self::node(right)}),
        ),
        Expression::Membership {
            operator,
            left,
            right,
        } => (
            "membership",
            json!({"op": membership(*operator), "left": self::node(left), "right": self::node(right)}),
        ),
        Expression::Presence {
            kind,
            negated,
            operand,
        } => {
            let kind = match kind {
                Presence::Blank => "blank",
                Presence::Present => "present",
                Presence::Defined => "defined",
            };
            (
                "presence",
                json!({"kind": kind, "negated": negated, "operand": self::node(operand)}),
            )
        }
        Expression::Unary { operator, operand } => {
            let operator = match operator {
                Unary::Minus => "minus",
                Unary::Bang => "bang",
            };
            (
                "unary",
                json!({"op": operator, "operand": self::node(operand)}),
            )
        }
        Expression::And { left, right } => (
            "and",
            json!({"left": self::node(left), "right": self::node(right)}),
        ),
        Expression::Or { left, right } => (
            "or",
            json!({"left": self::node(left), "right": self::node(right)}),
        ),
        Expression::Not { operand } => ("not", json!({"operand": self::node(operand)})),
        Expression::List(elements) => (
            "list",
            json!({"elements": elements.iter().map(self::node).collect::<Vec<_>>()}),
        ),
        Expression::Object(entries) => (
            "object",
            json!({"entries": entries.iter().map(entry).collect::<Vec<_>>()}),
        ),
        Expression::Call { name, arguments } => (
            "call",
            json!({"name": name, "arguments": arguments.iter().map(self::node).collect::<Vec<_>>()}),
        ),
        Expression::Index { object, key } => (
            "index",
            json!({"object": self::node(object), "key": self::node(key)}),
        ),
        Expression::Property { object, name } => (
            "property",
            json!({"object": self::node(object), "name": name}),
        ),
        Expression::Cast { operand, type_name } => (
            "cast",
            json!({"operand": self::node(operand), "type": type_name}),
        ),
        Expression::Duration(units) => ("duration", json!({"units": units})),
        Expression::RelativeDate {
            duration,
            direction: way,
        } => (
            "relative_date",
            json!({"duration": self::node(duration), "direction": direction(*way)}),
        ),
    };
    shaped(tag, node.position, node.span, fields)
}

fn block(block: &Block) -> Json {
    shaped(
        "block",
        block.position,
        block.span,
        json!({"statements": block.statements.iter().map(statement).collect::<Vec<_>>()}),
    )
}

fn statement(statement: &Statement) -> Json {
    match statement {
        Statement::Assignment {
            target,
            value,
            position,
            span,
        } => shaped(
            "assignment",
            *position,
            *span,
            json!({"target": node(target), "value": node(value)}),
        ),
        Statement::If {
            condition,
            then_block,
            else_block,
            position,
            span,
        } => shaped(
            "if",
            *position,
            *span,
            json!({
                "condition": node(condition),
                "then": block(then_block),
                "else": else_block.as_ref().map(block),
            }),
        ),
        Statement::While {
            condition,
            body,
            position,
            span,
        } => shaped(
            "while",
            *position,
            *span,
            json!({"condition": node(condition), "body": block(body)}),
        ),
        Statement::Expression(expression) => node(expression),
    }
}

fn program(program: &Program) -> Json {
    shaped(
        "program",
        program.position,
        program.span,
        json!({"statements": program.statements.iter().map(statement).collect::<Vec<_>>()}),
    )
}

fn refusal(refusal: &Refusal) -> Json {
    json!({
        "error": refusal.error.message(),
        "at": at(refusal.error.position().unwrap()),
        "span": over(refusal.span),
    })
}

/// What the parser answers for a source, in the fixture's shape: the
/// expression parse and the program parse, or the lexer's refusal.
fn answer(source: &str) -> Json {
    match tokenize(source) {
        Ok(tokens) => json!({
            "source": source,
            "expression": parse(&tokens).as_ref().map_or_else(refusal, node),
            "program": parse_program(&tokens).as_ref().map_or_else(refusal, program),
        }),
        Err(error) => json!({"source": source, "lex": refusal(&error)}),
    }
}

/// The reason, message, position and span of the refusal a source parses
/// to, as an expression or as a program.
fn refused(source: &str, as_program: bool) -> (String, String, Position, Span) {
    let tokens = tokenize(source).unwrap();
    let error = if as_program {
        parse_program(&tokens).unwrap_err()
    } else {
        parse(&tokens).unwrap_err()
    };
    (
        error.error.reason().to_owned(),
        error.error.message().to_owned(),
        error.error.position().unwrap(),
        error.span,
    )
}

fn reason(source: &str, as_program: bool) -> String {
    refused(source, as_program).0
}

fn expression(source: &str) -> Node {
    parse(&tokenize(source).unwrap()).unwrap()
}

fn point(line: usize, column: usize) -> Position {
    Position { line, column }
}

fn manifest_path(path: &str) -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(path)
}

fn lines(path: &str) -> Vec<Json> {
    std::fs::read_to_string(manifest_path(path))
        .unwrap()
        .lines()
        .map(|line| serde_json::from_str(line).unwrap())
        .collect()
}

// Every source of tests/fixtures/parser-reference.jsonl parses as the
// reference's parser at the vendored tag parses it, run read-only over those
// sources: the tree with each node's position and span, or the refusal's
// message, position and span, as an expression and as a program. The
// sources cover every production, every refusal and the newer operators.
// Sabotage: with `+` and `-` given the level of `*`, this test fails on
// `1 + 2 * 3`.
#[test]
fn parses_as_the_reference_does() {
    let rows = lines("tests/fixtures/parser-reference.jsonl");
    let mut differing = Vec::new();
    for expected in &rows {
        let actual = answer(expected["source"].as_str().unwrap());
        if &actual != expected {
            differing.push(format!("expected {expected}\n  actual {actual}"));
        }
    }
    assert!(differing.is_empty(), "{}", differing.join("\n"));
    assert_eq!(rows.len(), 397);
}

// Every row of the vendored compile transcript that is a parse refusal
// answers the row's message, position and span (and its reason, where the
// row names one), or the answer SUPERSEDED.json lists for it; every row
// that compiles a source parses it.
// Sabotage: with the trailing-token refusal after a statement given the
// expression's wording, this test fails on the missing-separator row.
#[test]
fn every_compile_transcript_refusal_matches() {
    let superseded: Json = serde_json::from_str(
        &std::fs::read_to_string(manifest_path("conformance/transcript/SUPERSEDED.json")).unwrap(),
    )
    .unwrap();
    let mut answers = BTreeMap::new();
    for row in superseded["rows"].as_array().unwrap() {
        if row["file"] == "compile.json" {
            answers.insert(row["index"].as_u64().unwrap(), row["answer"].clone());
        }
    }
    let rows = lines("conformance/transcript/compile.json");
    let mut counts = BTreeMap::new();
    for (index, row) in (0..).zip(&rows) {
        let kind = row["kind"].as_str().unwrap();
        let source = row["source"].as_str().unwrap();
        *counts.entry(kind).or_insert(0) += 1;
        let as_program = kind.starts_with("program");
        if kind == "refusal" || kind == "program_refusal" {
            let expected = answers.remove(&index).unwrap_or_else(|| row.clone());
            let actual = match tokenize(source) {
                Ok(tokens) if as_program => parse_program(&tokens).unwrap_err(),
                Ok(tokens) => parse(&tokens).unwrap_err(),
                Err(error) => error,
            };
            let mut answer = json!({
                "message": actual.error.message(),
                "position": {"line": actual.span.start.line, "column": actual.span.start.column},
                "span": {
                    "start": {"line": actual.span.start.line, "column": actual.span.start.column},
                    "end": {"line": actual.span.end.line, "column": actual.span.end.column},
                },
            });
            assert_eq!(
                actual.error.position(),
                Some(actual.span.start),
                "row {index}"
            );
            if expected.get("reason").is_some() {
                answer["reason"] = json!(actual.error.reason());
            }
            for key in ["message", "position", "span", "reason"] {
                assert_eq!(answer.get(key), expected.get(key), "row {index} {key}");
            }
        } else {
            let tokens = tokenize(source).unwrap();
            if as_program {
                assert!(parse_program(&tokens).is_ok(), "row {index}");
            } else {
                assert!(parse(&tokens).is_ok(), "row {index}");
            }
        }
    }
    assert!(answers.is_empty(), "superseded rows not found: {answers:?}");
    assert_eq!(
        counts,
        BTreeMap::from([
            ("compile", 63),
            ("decompile", 45),
            ("program", 35),
            ("program_refusal", 29),
            ("refusal", 27),
        ])
    );
}

// Every source in the vendored corpus, every tier, parses as an expression.
// Sabotage: with the lower-case `intersects` left out of the relations,
// this test fails on the tier 10 overlap cases.
#[test]
fn every_corpus_source_parses() {
    let mut parsed = 0;
    for tier in 1..=10 {
        for case in lines(&format!("conformance/corpus/tier-{tier}.json")) {
            let Some(source) = case["source"].as_str() else {
                continue;
            };
            let tokens = tokenize(source).unwrap();
            if let Err(error) = parse(&tokens) {
                panic!("{}: {}", case["id"], error.error.message());
            }
            parsed += 1;
        }
    }
    assert!(parsed > 300, "{parsed}");
}

// The newer operators sit at the precedence of `in`: `NOT`, `AND` and `OR`
// bind looser, arithmetic tighter, and each word stays a name elsewhere.
// Sabotage: with the capital `IS` left out of the presence words, this
// test fails on `x IS NOT BLANK`.
#[test]
fn the_newer_operators_take_the_precedence_of_in() {
    let tag = |source: &str| node(&expression(source))["node"].clone();
    assert_eq!(tag("not x is blank"), "not");
    assert_eq!(tag("x is blank AND y is present"), "and");
    assert_eq!(tag("x is not blank OR y is not defined"), "or");
    assert_eq!(tag("(x + 1) is defined"), "presence");
    assert_eq!(tag("x + 1 intersects y"), "membership");
    let tree = node(&expression("x IS NOT BLANK"));
    assert_eq!(
        (tree["kind"].clone(), tree["negated"].clone()),
        (json!("blank"), json!(true))
    );
    let tree = node(&expression("is is blank AND blank is present"));
    assert_eq!(tree["left"]["operand"]["name"], "is");
    assert_eq!(tree["right"]["operand"]["name"], "blank");
    assert_eq!(reason("x is blank is blank", false), "trailing_token");
    assert_eq!(
        reason("x intersects y intersects z", false),
        "trailing_token"
    );
    assert_eq!(reason("x == y is blank", false), "trailing_token");
}

// Each refusal carries its reason.
// Sabotage: with the unassignable-location refusal given the reason
// `assignment_in_expression`, this test fails.
#[test]
fn each_refusal_carries_its_reason() {
    let cases = [
        ("()", false, "expected_primary"),
        ("x y", false, "trailing_token"),
        ("x = 1", false, "assignment_in_expression"),
        ("if", false, "statement_keyword"),
        ("(x]", false, "expected_close_paren"),
        ("f(x]", false, "expected_close_paren"),
        ("[x)", false, "expected_close_bracket"),
        ("x[0)", false, "expected_close_bracket"),
        ("{a: 1)", false, "expected_close_brace"),
        ("{1: 2}", false, "expected_object_key"),
        ("{a 1}", false, "expected_object_colon"),
        ("a.1", false, "expected_property_name"),
        ("a::1", false, "expected_type_name"),
        ("a::list", false, "unknown_cast_type"),
        ("1d from x", false, "expected_now"),
        ("next x", false, "expected_duration"),
        ("0.5ms", false, "duration_fraction"),
        ("0.000000000001s", false, "duration_fraction"),
        ("1.5d2h", false, "duration_unit_twice"),
        ("x = 1 y = 2", true, "trailing_token"),
        ("else { }", true, "unexpected_else"),
        ("x = 1 else { }", true, "unexpected_else"),
        ("1 = x", true, "unassignable_location"),
        ("if x y", true, "expected_open_brace"),
        ("if x { y", true, "expected_close_brace"),
    ];
    for (source, as_program, expected) in cases {
        assert_eq!(reason(source, as_program), expected, "{source}");
    }
}

// A fraction is expanded into whole units as the reference expands it.
// Sabotage: with the whole part kept even when it is zero, this test fails
// on `0.5d`.
#[test]
fn a_fraction_expands_into_whole_units() {
    let units = |source: &str| node(&expression(source))["units"].clone();
    assert_eq!(units("1.5h"), json!([[1, "h"], [30, "m"]]));
    assert_eq!(units("0.5d"), json!([[12, "h"]]));
    assert_eq!(units("0.0d"), json!([[0, "d"]]));
    assert_eq!(units("2.50mo"), json!([[2, "mo"], [15, "d"]]));
    assert_eq!(units("0.001s"), json!([[1, "ms"]]));
    assert_eq!(units("1.25y"), json!([[1, "y"], [91, "d"], [6, "h"]]));
    assert_eq!(units("0.5s1s"), json!([[500, "ms"], [1, "s"]]));
    assert_eq!(units("3d3d"), json!([[3, "d"], [3, "d"]]));
}

/// A comparison whose right side nests a prefix minus and a parenthesis
/// per repetition, two levels each, behind a `!` that stays open over it
/// when the count needs one more level.
fn mixed(n: usize) -> String {
    let (bang, repeats) = if (n - 1) % 2 == 0 {
        ("", (n - 1) / 2)
    } else {
        ("!", (n - 2) / 2)
    };
    format!(
        "{bang}x == {}x{}",
        "-(".repeat(repeats),
        ")".repeat(repeats)
    )
}

/// Runs `check` on a thread whose stack is 256 KiB.
fn on_a_small_stack(check: impl FnOnce() + Send + 'static) {
    std::thread::Builder::new()
        .stack_size(256 * 1024)
        .spawn(check)
        .unwrap()
        .join()
        .unwrap();
}

/// The nesting shapes, each built `n` levels deep (the whole expression or
/// statement counting as the first), with whether it is a program.
fn shapes(n: usize) -> Vec<(String, bool)> {
    let inner = n - 1;
    let wrap = |open: &str, close: &str| format!("{}x{}", open.repeat(inner), close.repeat(inner));
    vec![
        (wrap("(", ")"), false),
        (wrap("[", "]"), false),
        (wrap("f(", ")"), false),
        (wrap("a[", "]"), false),
        (wrap("{k: ", "}"), false),
        (wrap("-", ""), false),
        (wrap("!", ""), false),
        (wrap("NOT ", ""), false),
        (mixed(n), false),
        (
            format!("{}next 1d{}", "(".repeat(n - 2), ")".repeat(n - 2)),
            false,
        ),
        (
            format!("{}y{}", "if x { ".repeat(n - 1), " }".repeat(n - 1)),
            true,
        ),
        (
            format!("{}if x {{ }}", "if x { } else ".repeat(n - 1)),
            true,
        ),
        (format!("y = {}", wrap("(", ")")), true),
    ]
}

// A source nesting exactly DEPTH_LIMIT levels parses, in every nesting
// shape, on a 256 KiB stack, and one level deeper is refused with the
// reason nesting_depth_exceeded at the token the parse stopped on.
// Sabotage: with `enter` checking `<` rather than `<=` against the limit,
// this test fails on the deepest accepted parenthesis.
#[test]
fn the_deepest_accepted_source_parses_on_a_256_kib_stack() {
    on_a_small_stack(|| {
        for (source, as_program) in shapes(DEPTH_LIMIT) {
            let tokens = tokenize(&source).unwrap();
            let parsed = if as_program {
                parse_program(&tokens).map(drop)
            } else {
                parse(&tokens).map(drop)
            };
            assert!(parsed.is_ok(), "{source:.40}");
        }
        for (source, as_program) in shapes(DEPTH_LIMIT + 1) {
            let (reason, message, ..) = refused(&source, as_program);
            assert_eq!(reason, "nesting_depth_exceeded", "{source:.40}");
            assert_eq!(
                message,
                "Expression nests past the depth limit of 400 levels, \
                 the whole expression counting as the first"
            );
        }
    });
    let (_, _, position, span) = refused(&format!("{}x", "(".repeat(DEPTH_LIMIT)), false);
    assert_eq!(position, point(1, DEPTH_LIMIT + 1));
    assert_eq!(span.end, point(1, DEPTH_LIMIT + 2));
}

// No source overflows the stack: a chain as long as the source costs no
// depth, parses and drops on a 256 KiB stack, and a source nested far past
// the limit is refused there.
// Sabotage: with `enter` never refusing, this test fails on 200 000 open
// parentheses.
#[test]
fn no_source_overflows_the_stack() {
    on_a_small_stack(|| {
        let chain = format!("x{}", " + x".repeat(100_000));
        assert!(parse(&tokenize(&chain).unwrap()).is_ok());
        let postfix = format!("x{}", ".y[0]::string".repeat(30_000));
        assert!(parse(&tokenize(&postfix).unwrap()).is_ok());
        let statements = "x = x + 1;".repeat(20_000);
        assert!(parse_program(&tokenize(&statements).unwrap()).is_ok());
        let deep = "(".repeat(200_000);
        assert_eq!(reason(&deep, false), "nesting_depth_exceeded");
        assert_eq!(reason(&deep, true), "nesting_depth_exceeded");
        let deep = "if x { ".repeat(200_000);
        assert_eq!(reason(&deep, true), "nesting_depth_exceeded");
    });
}

// No prefix of any fixture source makes the parser panic: each parses or
// is refused, as an expression and as a program.
// Sabotage: with the presence word read by an unchecked index two past
// `is`, the prefix `x is` panics and this test fails on its assertion.
#[test]
fn no_prefix_of_any_source_panics() {
    let mut panicked = Vec::new();
    for row in lines("tests/fixtures/parser-reference.jsonl") {
        let source = row["source"].as_str().unwrap().to_owned();
        for (end, _) in source.char_indices().chain([(source.len(), ' ')]) {
            let prefix = source[..end].to_owned();
            let Ok(tokens) = tokenize(&prefix) else {
                continue;
            };
            let outcome = std::panic::catch_unwind(|| {
                let _ = parse(&tokens);
                let _ = parse_program(&tokens);
            });
            if outcome.is_err() {
                panicked.push(prefix);
            }
        }
    }
    assert_eq!(panicked, Vec::<String>::new());
}

// Token streams the lexer never writes are still answered with a refusal:
// no tokens at all, a function's name with no parenthesis after it, and a
// number with a point and no unit after it.
// Sabotage: with the empty stream read from a token made up at line 0,
// this test fails on the position.
#[test]
fn streams_the_lexer_never_writes_are_refused() {
    let token = |kind, column| crate::lexer::Token {
        kind,
        line: 1,
        column,
        length: 1,
    };
    let refusal = parse(&[]).unwrap_err();
    assert_eq!(refusal.error.reason(), "expected_primary");
    assert_eq!(refusal.error.position(), Some(point(1, 1)));
    assert!(parse_program(&[]).is_err());
    let stream = [
        token(Kind::FunctionName("f".to_owned()), 1),
        token(Kind::Eof, 2),
    ];
    let refusal = parse(&stream).unwrap_err();
    assert_eq!(refusal.error.reason(), "expected_open_paren");
    assert_eq!(
        refusal.error.message(),
        "Expected '(' after function name but found end of input"
    );
    let stream = [
        token(
            Kind::FractionalNumber {
                whole: 1,
                fraction: "5".to_owned(),
            },
            1,
        ),
        token(Kind::Eof, 4),
    ];
    let refusal = parse(&stream).unwrap_err();
    assert_eq!(refusal.error.reason(), "expected_duration_unit");
    assert_eq!(
        refusal.error.message(),
        "Expected a duration unit after number '1.5'"
    );
}

// A prefix operator gives its level back once its operand is read, so a
// flat run of negated or negative items longer than DEPTH_LIMIT parses in a
// sum, a list, a call's arguments, an object, an AND chain and an OR
// chain, while prefixes nested inside each other still count one level
// each.
// Sabotage: with `apply` not giving back the level of a prefix `-`, `!` or
// `NOT`, this test fails on the sum of negated terms.
#[test]
fn a_flat_run_of_prefixed_items_costs_no_depth() {
    let count = DEPTH_LIMIT + 50;
    let items = |item: &dyn Fn(usize) -> String| (0..count).map(item).collect::<Vec<_>>();
    let sources = [
        format!("x{}", " + -x".repeat(count - 1)),
        format!("[{}]", items(&|_| "-1".to_owned()).join(", ")),
        format!("f({})", items(&|_| "-1".to_owned()).join(", ")),
        format!("{{{}}}", items(&|at| format!("k{at}: -1")).join(", ")),
        format!("a{}", " AND !a".repeat(count - 1)),
        format!("a{}", " OR NOT a".repeat(count - 1)),
    ];
    for source in &sources {
        let tokens = tokenize(source).unwrap();
        assert!(parse(&tokens).is_ok(), "{source:.40}");
        assert!(parse_program(&tokens).is_ok(), "{source:.40}");
    }
    for prefix in ["-", "!", "NOT "] {
        let deepest = format!("{}x", prefix.repeat(DEPTH_LIMIT - 1));
        assert!(parse(&tokenize(&deepest).unwrap()).is_ok(), "{prefix}");
        let deeper = format!("{}x", prefix.repeat(DEPTH_LIMIT));
        assert_eq!(reason(&deeper, false), "nesting_depth_exceeded", "{prefix}");
    }
}