graphix-compiler 0.8.0

A dataflow language for UIs and network programming, compiler
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
use crate::{
    expr::print::{PrettyBuf, PrettyDisplay},
    typ::{TVar, Type},
    PrintFlag, PRINT_FLAGS,
};
use anyhow::Result;
use arcstr::{literal, ArcStr};
use combine::stream::position::SourcePosition;
pub use modpath::ModPath;
use netidx::{path::Path, subscriber::Value, utils::Either};
pub use pattern::{Pattern, StructurePattern};
use poolshark::local::LPooled;
use regex::Regex;
pub use resolver::{add_interface_modules, BufferOverrides, ModuleResolver};
use serde::{
    de::{self, Visitor},
    Deserialize, Deserializer, Serialize, Serializer,
};
use std::{
    cell::RefCell,
    cmp::{Ordering, PartialEq, PartialOrd},
    fmt,
    ops::Deref,
    path::PathBuf,
    result,
    str::FromStr,
    sync::LazyLock,
};
use triomphe::Arc;

mod modpath;
pub mod parser;
mod pattern;
pub mod print;
mod resolver;
#[cfg(test)]
mod test;

pub const VNAME: LazyLock<Regex> =
    LazyLock::new(|| Regex::new("^[a-z][a-z0-9_]*$").unwrap());

atomic_id!(ExprId);

const DEFAULT_ORIGIN: LazyLock<Arc<Origin>> =
    LazyLock::new(|| Arc::new(Origin::default()));

thread_local! {
    static ORIGIN: RefCell<Option<Arc<Origin>>> = RefCell::new(None);
}

pub(crate) fn set_origin(ori: Arc<Origin>) {
    ORIGIN.with_borrow_mut(|global| *global = Some(ori))
}

pub(crate) fn get_origin() -> Arc<Origin> {
    ORIGIN.with_borrow(|ori| {
        ori.as_ref().cloned().unwrap_or_else(|| DEFAULT_ORIGIN.clone())
    })
}

/// utility to read a file to an ArcStr with minimal allocation
pub async fn read_to_arcstr(path: impl AsRef<std::path::Path>) -> Result<ArcStr> {
    use tokio::io::AsyncReadExt;
    let mut buf: LPooled<Vec<u8>> = LPooled::take();
    let mut f = tokio::fs::File::open(path).await?;
    f.read_to_end(&mut *buf).await?;
    let s = str::from_utf8(&*buf)?;
    Ok(ArcStr::from(s))
}

#[derive(Debug)]
pub struct CouldNotResolve(ArcStr);

impl fmt::Display for CouldNotResolve {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "could not resolve module {}", self.0)
    }
}

#[derive(Debug, Clone)]
pub struct Arg {
    pub labeled: Option<Option<Expr>>,
    pub pattern: StructurePattern,
    pub constraint: Option<Type>,
    pub pos: SourcePosition,
}

impl PartialEq for Arg {
    fn eq(&self, rhs: &Self) -> bool {
        self.labeled == rhs.labeled
            && self.pattern == rhs.pattern
            && self.constraint == rhs.constraint
    }
}

impl PartialOrd for Arg {
    fn partial_cmp(&self, rhs: &Self) -> Option<std::cmp::Ordering> {
        match self.labeled.partial_cmp(&rhs.labeled)? {
            std::cmp::Ordering::Equal => (),
            o => return Some(o),
        }
        match self.pattern.partial_cmp(&rhs.pattern)? {
            std::cmp::Ordering::Equal => (),
            o => return Some(o),
        }
        self.constraint.partial_cmp(&rhs.constraint)
    }
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct Doc(pub Option<ArcStr>);

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct TypeDefExpr {
    pub name: ArcStr,
    pub params: Arc<[(TVar, Option<Type>)]>,
    pub typ: Type,
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct BindSig {
    pub name: ArcStr,
    pub typ: Type,
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum SigKind {
    TypeDef(TypeDefExpr),
    Bind(BindSig),
    Module(ArcStr),
    Use(ModPath),
}

#[derive(Debug, Clone)]
pub struct SigItem {
    pub doc: Doc,
    pub kind: SigKind,
    pub pos: SourcePosition,
    pub ori: Option<Arc<Origin>>,
}

impl PartialEq for SigItem {
    fn eq(&self, other: &Self) -> bool {
        self.doc == other.doc && self.kind == other.kind
    }
}

impl PartialOrd for SigItem {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        match self.doc.partial_cmp(&other.doc)? {
            std::cmp::Ordering::Equal => self.kind.partial_cmp(&other.kind),
            ord => Some(ord),
        }
    }
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct Sig {
    pub items: Arc<[SigItem]>,
    pub toplevel: bool,
}

impl Deref for Sig {
    type Target = [SigItem];

    fn deref(&self) -> &Self::Target {
        &*self.items
    }
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum Sandbox {
    Unrestricted,
    Blacklist(Arc<[ModPath]>),
    Whitelist(Arc<[ModPath]>),
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum ModuleKind {
    Dynamic { sandbox: Sandbox, sig: Sig, source: Arc<Expr> },
    Resolved { exprs: Arc<[Expr]>, sig: Option<Sig>, from_interface: bool },
    Unresolved { from_interface: bool },
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct BindExpr {
    pub rec: bool,
    pub pattern: StructurePattern,
    pub typ: Option<Type>,
    pub value: Expr,
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct LambdaExpr {
    pub args: Arc<[Arg]>,
    pub vargs: Option<Option<Type>>,
    pub rtype: Option<Type>,
    pub constraints: Arc<[(TVar, Type)]>,
    pub throws: Option<Type>,
    pub body: Either<Expr, ArcStr>,
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct TryCatchExpr {
    pub bind: ArcStr,
    pub constraint: Option<Type>,
    pub handler: Arc<Expr>,
    pub exprs: Arc<[Expr]>,
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct StructWithExpr {
    pub source: Arc<Expr>,
    pub replace: Arc<[(ArcStr, Expr)]>,
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct StructExpr {
    pub args: Arc<[(ArcStr, Expr)]>,
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct ApplyExpr {
    pub args: Arc<[(Option<ArcStr>, Expr)]>,
    pub function: Arc<Expr>,
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct SelectExpr {
    pub arg: Arc<Expr>,
    pub arms: Arc<[(Pattern, Expr)]>,
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum ExprKind {
    NoOp,
    Constant(Value),
    Module { name: ArcStr, value: ModuleKind },
    ExplicitParens(Arc<Expr>),
    Do { exprs: Arc<[Expr]> },
    Use { name: ModPath },
    Bind(Arc<BindExpr>),
    Ref { name: ModPath },
    Connect { name: ModPath, value: Arc<Expr>, deref: bool },
    StringInterpolate { args: Arc<[Expr]> },
    StructRef { source: Arc<Expr>, field: ArcStr },
    TupleRef { source: Arc<Expr>, field: usize },
    ArrayRef { source: Arc<Expr>, i: Arc<Expr> },
    ArraySlice { source: Arc<Expr>, start: Option<Arc<Expr>>, end: Option<Arc<Expr>> },
    MapRef { source: Arc<Expr>, key: Arc<Expr> },
    StructWith(StructWithExpr),
    Lambda(Arc<LambdaExpr>),
    TypeDef(TypeDefExpr),
    TypeCast { expr: Arc<Expr>, typ: Type },
    Apply(ApplyExpr),
    Any { args: Arc<[Expr]> },
    Array { args: Arc<[Expr]> },
    Map { args: Arc<[(Expr, Expr)]> },
    Tuple { args: Arc<[Expr]> },
    Variant { tag: ArcStr, args: Arc<[Expr]> },
    Struct(StructExpr),
    Select(SelectExpr),
    Qop(Arc<Expr>),
    OrNever(Arc<Expr>),
    TryCatch(Arc<TryCatchExpr>),
    ByRef(Arc<Expr>),
    Deref(Arc<Expr>),
    Eq { lhs: Arc<Expr>, rhs: Arc<Expr> },
    Ne { lhs: Arc<Expr>, rhs: Arc<Expr> },
    Lt { lhs: Arc<Expr>, rhs: Arc<Expr> },
    Gt { lhs: Arc<Expr>, rhs: Arc<Expr> },
    Lte { lhs: Arc<Expr>, rhs: Arc<Expr> },
    Gte { lhs: Arc<Expr>, rhs: Arc<Expr> },
    And { lhs: Arc<Expr>, rhs: Arc<Expr> },
    Or { lhs: Arc<Expr>, rhs: Arc<Expr> },
    Not { expr: Arc<Expr> },
    Add { lhs: Arc<Expr>, rhs: Arc<Expr> },
    CheckedAdd { lhs: Arc<Expr>, rhs: Arc<Expr> },
    Sub { lhs: Arc<Expr>, rhs: Arc<Expr> },
    CheckedSub { lhs: Arc<Expr>, rhs: Arc<Expr> },
    Mul { lhs: Arc<Expr>, rhs: Arc<Expr> },
    CheckedMul { lhs: Arc<Expr>, rhs: Arc<Expr> },
    Div { lhs: Arc<Expr>, rhs: Arc<Expr> },
    CheckedDiv { lhs: Arc<Expr>, rhs: Arc<Expr> },
    Mod { lhs: Arc<Expr>, rhs: Arc<Expr> },
    CheckedMod { lhs: Arc<Expr>, rhs: Arc<Expr> },
    Sample { lhs: Arc<Expr>, rhs: Arc<Expr> },
}

impl ExprKind {
    pub fn to_expr(self, pos: SourcePosition) -> Expr {
        Expr { id: ExprId::new(), ori: get_origin(), pos, kind: self }
    }

    /// does not provide any position information or comment
    pub fn to_expr_nopos(self) -> Expr {
        Expr { id: ExprId::new(), ori: get_origin(), pos: Default::default(), kind: self }
    }
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum Source {
    File(PathBuf),
    Netidx(Path),
    Internal(ArcStr),
    Unspecified,
}

impl Default for Source {
    fn default() -> Self {
        Self::Unspecified
    }
}

impl Source {
    pub fn has_filename(&self, name: &str) -> bool {
        match self {
            Self::File(buf) => match buf.file_name() {
                None => false,
                Some(os) => match os.to_str() {
                    None => false,
                    Some(s) => s == name,
                },
            },
            Self::Netidx(_) | Self::Internal(_) | Self::Unspecified => false,
        }
    }

    pub fn is_file(&self) -> bool {
        match self {
            Self::File(_) => true,
            Self::Netidx(_) | Self::Internal(_) | Self::Unspecified => false,
        }
    }

    pub fn to_value(&self) -> Value {
        match self {
            Self::File(pb) => {
                let s = pb.as_os_str().to_string_lossy();
                (literal!("File"), ArcStr::from(s)).into()
            }
            Self::Netidx(p) => (literal!("Netidx"), p.clone()).into(),
            Self::Internal(s) => (literal!("Internal"), s.clone()).into(),
            Self::Unspecified => literal!("Unspecified").into(),
        }
    }
}

// hallowed are the ori
#[derive(Debug, Clone, PartialEq, PartialOrd, Default)]
pub struct Origin {
    pub parent: Option<Arc<Origin>>,
    pub source: Source,
    pub text: ArcStr,
}

impl fmt::Display for Origin {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let flags = PRINT_FLAGS.with(|f| f.get());
        match &self.source {
            Source::Unspecified => {
                if flags.contains(PrintFlag::NoSource) {
                    write!(f, "in expr")?
                } else {
                    write!(f, "in expr {}", self.text)?
                }
            }
            Source::File(n) => write!(f, "in file {n:?}")?,
            Source::Netidx(n) => write!(f, "in netidx {n}")?,
            Source::Internal(n) => write!(f, "in module {n}")?,
        }
        let mut p = &self.parent;
        if flags.contains(PrintFlag::NoParents) {
            Ok(())
        } else {
            loop {
                match p {
                    None => break Ok(()),
                    Some(parent) => {
                        writeln!(f, "")?;
                        write!(f, "    ")?;
                        match &parent.source {
                            Source::Unspecified => {
                                if flags.contains(PrintFlag::NoSource) {
                                    write!(f, "included from expr")?
                                } else {
                                    write!(f, "included from expr {}", parent.text)?
                                }
                            }
                            Source::File(n) => write!(f, "included from file {n:?}")?,
                            Source::Netidx(n) => write!(f, "included from netidx {n}")?,
                            Source::Internal(n) => write!(f, "included from module {n}")?,
                        }
                        p = &parent.parent;
                    }
                }
            }
        }
    }
}

impl Origin {
    pub fn to_value(&self) -> Value {
        let p = Value::from(self.parent.as_ref().map(|p| p.to_value()));
        [
            (literal!("parent"), p),
            (literal!("source"), self.source.to_value()),
            (literal!("text"), Value::from(self.text.clone())),
        ]
        .into()
    }

    pub fn from_str(s: &str) -> Self {
        Self { parent: None, source: Source::Unspecified, text: ArcStr::from(s) }
    }
}

#[derive(Clone)]
pub struct Expr {
    pub id: ExprId,
    pub ori: Arc<Origin>,
    pub pos: SourcePosition,
    pub kind: ExprKind,
}

impl fmt::Debug for Expr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self.kind)
    }
}

impl fmt::Display for Expr {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.kind)
    }
}

impl PrettyDisplay for Expr {
    fn fmt_pretty_inner(&self, buf: &mut PrettyBuf) -> fmt::Result {
        self.kind.fmt_pretty(buf)
    }
}

impl PartialOrd for Expr {
    fn partial_cmp(&self, rhs: &Expr) -> Option<Ordering> {
        self.kind.partial_cmp(&rhs.kind)
    }
}

impl PartialEq for Expr {
    fn eq(&self, rhs: &Expr) -> bool {
        self.kind.eq(&rhs.kind)
    }
}

impl Eq for Expr {}

impl Serialize for Expr {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&self.to_string())
    }
}

impl Default for Expr {
    fn default() -> Self {
        ExprKind::Constant(Value::Null).to_expr(Default::default())
    }
}

impl FromStr for Expr {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> result::Result<Self, Self::Err> {
        parser::parse_one(s)
    }
}

#[derive(Clone, Copy)]
struct ExprVisitor;

impl<'de> Visitor<'de> for ExprVisitor {
    type Value = Expr;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "expected expression")
    }

    fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        Expr::from_str(s).map_err(de::Error::custom)
    }

    fn visit_borrowed_str<E>(self, s: &'de str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        Expr::from_str(s).map_err(de::Error::custom)
    }

    fn visit_string<E>(self, s: String) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        Expr::from_str(&s).map_err(de::Error::custom)
    }
}

impl<'de> Deserialize<'de> for Expr {
    fn deserialize<D>(de: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        de.deserialize_str(ExprVisitor)
    }
}

impl Expr {
    pub fn new(kind: ExprKind, pos: SourcePosition) -> Self {
        Expr { id: ExprId::new(), ori: get_origin(), pos, kind }
    }

    /// fold over self and all of self's sub expressions
    pub fn fold<T, F: FnMut(T, &Self) -> T>(&self, init: T, f: &mut F) -> T {
        let init = f(init, self);
        match &self.kind {
            ExprKind::Constant(_)
            | ExprKind::NoOp
            | ExprKind::Use { .. }
            | ExprKind::Ref { .. }
            | ExprKind::TypeDef { .. } => init,
            ExprKind::ExplicitParens(e) => e.fold(init, f),
            ExprKind::StructRef { source, .. } | ExprKind::TupleRef { source, .. } => {
                source.fold(init, f)
            }

            ExprKind::Map { args } => args.iter().fold(init, |init, (k, v)| {
                let init = k.fold(init, f);
                v.fold(init, f)
            }),
            ExprKind::MapRef { source, key } => {
                let init = source.fold(init, f);
                key.fold(init, f)
            }
            ExprKind::Module { value: ModuleKind::Resolved { exprs, .. }, .. } => {
                exprs.iter().fold(init, |init, e| e.fold(init, f))
            }
            ExprKind::Module {
                value: ModuleKind::Dynamic { sandbox: _, sig: _, source },
                ..
            } => source.fold(init, f),
            ExprKind::Module { value: ModuleKind::Unresolved { .. }, .. } => init,
            ExprKind::Do { exprs } => exprs.iter().fold(init, |init, e| e.fold(init, f)),
            ExprKind::Bind(b) => b.value.fold(init, f),
            ExprKind::StructWith(StructWithExpr { replace, .. }) => {
                replace.iter().fold(init, |init, (_, e)| e.fold(init, f))
            }
            ExprKind::Connect { value, .. } => value.fold(init, f),
            ExprKind::Lambda(l) => match &l.body {
                Either::Left(e) => e.fold(init, f),
                Either::Right(_) => init,
            },
            ExprKind::TypeCast { expr, .. } => expr.fold(init, f),
            ExprKind::Apply(ApplyExpr { args, function: _ }) => {
                args.iter().fold(init, |init, (_, e)| e.fold(init, f))
            }
            ExprKind::Any { args }
            | ExprKind::Array { args }
            | ExprKind::Tuple { args }
            | ExprKind::Variant { args, .. }
            | ExprKind::StringInterpolate { args } => {
                args.iter().fold(init, |init, e| e.fold(init, f))
            }
            ExprKind::ArrayRef { source, i } => {
                let init = source.fold(init, f);
                i.fold(init, f)
            }
            ExprKind::ArraySlice { source, start, end } => {
                let init = source.fold(init, f);
                let init = match start {
                    None => init,
                    Some(e) => e.fold(init, f),
                };
                match end {
                    None => init,
                    Some(e) => e.fold(init, f),
                }
            }
            ExprKind::Struct(StructExpr { args }) => {
                args.iter().fold(init, |init, (_, e)| e.fold(init, f))
            }
            ExprKind::Select(SelectExpr { arg, arms }) => {
                let init = arg.fold(init, f);
                arms.iter().fold(init, |init, (p, e)| {
                    let init = match p.guard.as_ref() {
                        None => init,
                        Some(g) => g.fold(init, f),
                    };
                    e.fold(init, f)
                })
            }
            ExprKind::TryCatch(tc) => {
                let init = tc.exprs.iter().fold(init, |init, e| e.fold(init, f));
                tc.handler.fold(init, f)
            }
            ExprKind::Qop(e)
            | ExprKind::OrNever(e)
            | ExprKind::ByRef(e)
            | ExprKind::Deref(e)
            | ExprKind::Not { expr: e } => e.fold(init, f),
            ExprKind::Add { lhs, rhs }
            | ExprKind::CheckedAdd { lhs, rhs }
            | ExprKind::Sub { lhs, rhs }
            | ExprKind::CheckedSub { lhs, rhs }
            | ExprKind::Mul { lhs, rhs }
            | ExprKind::CheckedMul { lhs, rhs }
            | ExprKind::Div { lhs, rhs }
            | ExprKind::CheckedDiv { lhs, rhs }
            | ExprKind::Mod { lhs, rhs }
            | ExprKind::CheckedMod { lhs, rhs }
            | ExprKind::And { lhs, rhs }
            | ExprKind::Or { lhs, rhs }
            | ExprKind::Eq { lhs, rhs }
            | ExprKind::Ne { lhs, rhs }
            | ExprKind::Gt { lhs, rhs }
            | ExprKind::Lt { lhs, rhs }
            | ExprKind::Gte { lhs, rhs }
            | ExprKind::Lte { lhs, rhs }
            | ExprKind::Sample { lhs, rhs } => {
                let init = lhs.fold(init, f);
                rhs.fold(init, f)
            }
        }
    }
}

pub struct ErrorContext(pub Expr);

impl fmt::Debug for ErrorContext {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

impl std::error::Error for ErrorContext {}

pub struct ParserContext {
    pub ori: Arc<Origin>,
    pub pos: SourcePosition,
}

impl fmt::Debug for ParserContext {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

impl fmt::Display for ParserContext {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.ori.source {
            Source::File(p) => {
                write!(f, "parse error at {} in file {}", self.pos, p.display())
            }
            Source::Netidx(p) => {
                write!(f, "parse error at {} in netidx {p}", self.pos)
            }
            Source::Internal(_) | Source::Unspecified => {
                write!(f, "parse error at {}", self.pos)
            }
        }
    }
}

impl std::error::Error for ParserContext {}

impl fmt::Display for ErrorContext {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use std::fmt::Write;
        const MAX: usize = 38;
        thread_local! {
            static BUF: RefCell<String> = RefCell::new(String::new());
        }
        BUF.with_borrow_mut(|buf| {
            buf.clear();
            write!(buf, "{}", self.0).unwrap();
            let snippet: &str = if buf.len() <= MAX {
                &buf
            } else {
                let mut end = MAX;
                while !buf.is_char_boundary(end) {
                    end += 1
                }
                &buf[0..end]
            };
            let suffix = if buf.len() > MAX { ".." } else { "" };
            match &self.0.ori.source {
                Source::File(p) => write!(
                    f,
                    "at: {} in file {}, in: {snippet}{suffix}",
                    self.0.pos,
                    p.display()
                ),
                Source::Netidx(p) => {
                    write!(f, "at: {} in netidx {p}, in: {snippet}{suffix}", self.0.pos)
                }
                Source::Internal(_) | Source::Unspecified => {
                    write!(f, "at: {}, in: {snippet}{suffix}", self.0.pos)
                }
            }
        })
    }
}