Skip to main content

decl_lang/
parse.rs

1//! CST -> AST lowering over the compiled tree-sitter grammar (parse.ts).
2use crate::ast::*;
3use crate::semantics::Value;
4use num_bigint::BigInt;
5use num_traits::Num;
6use std::rc::Rc;
7use tree_sitter::{Language, Node, Parser};
8use tree_sitter_language::LanguageFn;
9
10extern "C" {
11    fn tree_sitter_decl() -> *const ();
12}
13/// the tree-sitter grammar compiled into the crate (`build.rs`, from `grammar/`)
14pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_decl) };
15
16#[derive(Clone)]
17/// what parsing a source text yields: the declarations and the syntax errors
18pub struct ParseResult {
19    /// the declarations, in source order
20    pub decls: Vec<Decl>,
21    /// the syntax errors, as zero-based (row, column)
22    pub errors: Vec<(usize, usize)>,
23}
24
25// the same text parses to the same result: the session and the language
26// server re-load the unchanged modules of a universe on every question,
27// and the AST is never mutated after lowering (a small bounded cache; a
28// clone shares the expression nodes, whose addresses are their identity)
29thread_local! {
30    static PARSE_CACHE: std::cell::RefCell<Vec<(String, ParseResult)>> = const { std::cell::RefCell::new(Vec::new()) };
31}
32
33struct Lower<'a> {
34    src: &'a [u8],
35}
36
37/// Parse a module's source text: the tree-sitter CST lowered to the AST of
38/// [`crate::ast`], with source ranges (specification chapter 11).
39pub fn parse_source(src: &str) -> ParseResult {
40    if let Some(hit) = PARSE_CACHE.with(|c| {
41        c.borrow()
42            .iter()
43            .find(|(k, _)| k == src)
44            .map(|(_, r)| r.clone())
45    }) {
46        return hit;
47    }
48    let r = parse_source_uncached(src);
49    PARSE_CACHE.with(|c| {
50        let mut c = c.borrow_mut();
51        if c.len() >= 64 {
52            c.remove(0);
53        }
54        c.push((src.to_string(), r.clone()));
55    });
56    r
57}
58fn parse_source_uncached(src: &str) -> ParseResult {
59    let mut parser = Parser::new();
60    let lang: Language = LANGUAGE.into();
61    parser.set_language(&lang).expect("grammar");
62    let tree = parser.parse(src, None).expect("parse");
63    let root = tree.root_node();
64    let mut errors = Vec::new();
65    collect_errors(root, &mut errors);
66    let lw = Lower {
67        src: src.as_bytes(),
68    };
69    let mut decls = Vec::new();
70    // annotations precede the declaration they attach to as its siblings (§5.10)
71    let mut pending: Vec<Annotation> = Vec::new();
72    let mut cur = root.walk();
73    for c in root.named_children(&mut cur) {
74        if c.kind() == "ERROR" {
75            continue;
76        }
77        if c.kind() == "annotation" {
78            match lw.annotation(c) {
79                Ok(a) => pending.push(a),
80                Err(_) => {
81                    if errors.is_empty() {
82                        errors.push((c.start_position().row, c.start_position().column));
83                    }
84                    pending.clear();
85                }
86            }
87            continue;
88        }
89        match lw.decl(c) {
90            Ok(Some(mut d)) => {
91                let export_kw = c.prev_sibling().filter(|p| lw.text(*p) == "export");
92                let exported = export_kw.is_some() || matches!(d.body, DeclBody::ReExport { .. });
93                d.exported = exported;
94                // the declaration's source range: the `export` keyword, when
95                // present, is the previous sibling and is included
96                let start = export_kw
97                    .map(|p| p.start_position())
98                    .unwrap_or_else(|| c.start_position());
99                let start_byte = export_kw
100                    .map(|p| p.start_byte())
101                    .unwrap_or_else(|| c.start_byte());
102                d.loc = Some(Loc {
103                    sl: start.row,
104                    sc: lw.col16(start_byte),
105                    el: c.end_position().row,
106                    ec: lw.col16(c.end_byte()),
107                });
108                d.annotations = std::mem::take(&mut pending);
109                decls.push(d);
110            }
111            Ok(None) => {}
112            Err(_) => {
113                if errors.is_empty() {
114                    errors.push((c.start_position().row, c.start_position().column));
115                }
116                pending.clear();
117            }
118        }
119    }
120    ParseResult { decls, errors }
121}
122
123fn collect_errors(n: Node, out: &mut Vec<(usize, usize)>) {
124    if n.kind() == "ERROR" || n.is_missing() {
125        out.push((n.start_position().row, n.start_position().column));
126    }
127    if n.has_error() {
128        let mut cur = n.walk();
129        for c in n.children(&mut cur) {
130            collect_errors(c, out);
131        }
132    }
133}
134
135type LR<T> = Result<T, String>;
136
137impl<'a> Lower<'a> {
138    fn text(&self, n: Node) -> String {
139        n.utf8_text(self.src).unwrap_or("").to_string()
140    }
141    /// the arms of a union or intersection: the named type nodes and the
142    /// keyword literal types, which the grammar keeps as anonymous tokens
143    fn type_arms<'b>(&self, n: Node<'b>) -> Vec<Node<'b>> {
144        self.all(n)
145            .into_iter()
146            .filter(|c| c.is_named() || ["true", "false", "null"].contains(&self.text(*c).as_str()))
147            .collect()
148    }
149    fn field<'b>(&self, n: Node<'b>, name: &str) -> Option<Node<'b>> {
150        n.child_by_field_name(name)
151    }
152    fn req<'b>(&self, n: Node<'b>, name: &str) -> LR<Node<'b>> {
153        n.child_by_field_name(name)
154            .ok_or_else(|| format!("missing field {name}"))
155    }
156    fn named<'b>(&self, n: Node<'b>) -> Vec<Node<'b>> {
157        let mut cur = n.walk();
158        n.named_children(&mut cur).collect()
159    }
160    fn all<'b>(&self, n: Node<'b>) -> Vec<Node<'b>> {
161        let mut cur = n.walk();
162        n.children(&mut cur).collect()
163    }
164    fn kids<'b>(&self, n: Node<'b>, kind: &str) -> Vec<Node<'b>> {
165        self.named(n)
166            .into_iter()
167            .filter(|c| c.kind() == kind)
168            .collect()
169    }
170    fn kid<'b>(&self, n: Node<'b>, kind: &str) -> Option<Node<'b>> {
171        self.named(n).into_iter().find(|c| c.kind() == kind)
172    }
173    // `true` / `false` / `null` are anonymous keyword tokens in the grammar:
174    // an operand position may hold one, so operands are the named children
175    // plus those literals (never the operator or punctuation tokens)
176    fn is_lit_keyword(&self, c: Node) -> bool {
177        !c.is_named() && ["true", "false", "null"].contains(&self.text(c).as_str())
178    }
179    fn operands<'b>(&self, n: Node<'b>) -> Vec<Node<'b>> {
180        self.all(n)
181            .into_iter()
182            .filter(|c| c.is_named() || self.is_lit_keyword(*c))
183            .collect()
184    }
185    // checked child access: a tree with errors may lack the children the
186    // grammar promises, and lowering must fail (E2001), never panic
187    fn first<'b>(&self, n: Node<'b>) -> LR<Node<'b>> {
188        self.named(n)
189            .into_iter()
190            .next()
191            .ok_or_else(|| format!("{}: missing child", n.kind()))
192    }
193    fn first_operand<'b>(&self, n: Node<'b>) -> LR<Node<'b>> {
194        self.operands(n)
195            .into_iter()
196            .next()
197            .ok_or_else(|| format!("{}: missing operand", n.kind()))
198    }
199    fn at<'b>(&self, v: &[Node<'b>], i: usize) -> LR<Node<'b>> {
200        v.get(i)
201            .copied()
202            .ok_or_else(|| format!("missing operand {i}"))
203    }
204    fn json_string(&self, n: Node) -> LR<String> {
205        json_unquote(&self.text(n).replace('\n', "\\n"))
206    }
207
208    // ---------------- annotations ----------------
209    fn annotation(&self, n: Node) -> LR<Annotation> {
210        let mut args = Vec::new();
211        for c in self.operands(n).into_iter().skip(1) {
212            args.push(self.expr(c)?);
213        }
214        Ok(Annotation {
215            name: self.text(self.req(n, "name")?),
216            args,
217            loc: Some(self.loc_of(n)),
218        })
219    }
220
221    // ---------------- declarations ----------------
222    fn decl(&self, n: Node) -> LR<Option<Decl>> {
223        let body = match n.kind() {
224            "type_declaration" => {
225                let params = match self.kid(n, "type_parameters") {
226                    Some(ps) => self
227                        .kids(ps, "type_parameter")
228                        .into_iter()
229                        .map(|p| {
230                            let nc = self.named(p);
231                            Ok(Param {
232                                name: self.text(self.at(&nc, 0)?),
233                                ty: if nc.len() > 1 {
234                                    Some(self.ty(nc[1])?)
235                                } else {
236                                    None
237                                },
238                            })
239                        })
240                        .collect::<LR<Vec<_>>>()?,
241                    None => vec![],
242                };
243                DeclBody::Type {
244                    name: self.text(self.req(n, "name")?),
245                    params,
246                    ty: self.ty(self.req(n, "type")?)?,
247                    tail: self.maybe_tail(n)?,
248                }
249            }
250            "const_declaration" => DeclBody::Const {
251                name: self.text(self.req(n, "name")?),
252                ty: match self.field(n, "type") {
253                    Some(t) => Some(self.ty(t)?),
254                    None => None,
255                },
256                expr: self.expr(self.req(n, "value")?)?,
257            },
258            "func_declaration" => DeclBody::Func {
259                name: self.text(self.req(n, "name")?),
260                params: self.params(n)?,
261                ret: match self.field(n, "return_type") {
262                    Some(t) => Some(self.ty(t)?),
263                    None => None,
264                },
265                body: self.expr(self.req(n, "body")?)?,
266            },
267            "output_declaration" => DeclBody::Output {
268                name: self.text(self.req(n, "name")?),
269                ty: self.ty(self.req(n, "type")?)?,
270                expr: self.expr(self.req(n, "value")?)?,
271            },
272            "input_declaration" => DeclBody::Input {
273                name: self.text(self.req(n, "name")?),
274                ty: self.ty(self.req(n, "type")?)?,
275                fallback: match self.field(n, "fallback") {
276                    Some(f) => Some(self.expr(f)?),
277                    None => None,
278                },
279            },
280            "diagnostic_declaration" => DeclBody::Diagnostic {
281                name: self.text(self.req(n, "name")?),
282                params: self.params(n)?,
283                severity: self.text(self.kid(n, "severity").ok_or("severity")?),
284                template: self.template_parts(self.kid(n, "template_string").ok_or("template")?)?,
285            },
286            "dimension_declaration" => DeclBody::Dimension {
287                name: self.text(self.req(n, "name")?),
288                terms: self
289                    .kid(n, "dimension_expression")
290                    .map(|e| self.dim_expr(e)),
291            },
292            "unit_declaration" => match self.field(n, "dimension") {
293                Some(d) => DeclBody::Unit {
294                    name: self.text(self.req(n, "name")?),
295                    dim: Some(self.text(d)),
296                    factor: None,
297                    base: None,
298                },
299                None => DeclBody::Unit {
300                    name: self.text(self.req(n, "name")?),
301                    dim: None,
302                    factor: Some(self.expr(self.req(n, "factor")?)?),
303                    base: Some(self.text(self.req(n, "base")?)),
304                },
305            },
306            "import_declaration" => {
307                let from = self.json_string(self.kid(n, "string").ok_or("from")?)?;
308                match self.kid(n, "named_imports") {
309                    Some(ni) => DeclBody::Import {
310                        from,
311                        names: Some(self.import_items(ni)?),
312                        ns: None,
313                    },
314                    None => DeclBody::Import {
315                        from,
316                        names: None,
317                        ns: Some(self.text(self.kid(n, "identifier").ok_or("ns")?)),
318                    },
319                }
320            }
321            "re_export_declaration" => DeclBody::ReExport {
322                from: self.json_string(self.kid(n, "string").ok_or("from")?)?,
323                names: self.import_items(n)?,
324            },
325            _ => return Ok(None),
326        };
327        Ok(Some(Decl {
328            body,
329            exported: false,
330            annotations: vec![],
331            loc: None,
332        }))
333    }
334    fn params(&self, n: Node) -> LR<Vec<Param>> {
335        self.kids(n, "parameter")
336            .into_iter()
337            .map(|p| {
338                let nc = self.named(p);
339                Ok(Param {
340                    name: self.text(self.at(&nc, 0)?),
341                    ty: Some(self.ty(self.at(&nc, 1)?)?),
342                })
343            })
344            .collect()
345    }
346    fn import_items(&self, n: Node) -> LR<Vec<ImportItem>> {
347        self.kids(n, "import_item")
348            .into_iter()
349            .map(|it| {
350                let ids = self.named(it);
351                Ok(ImportItem {
352                    name: self.text(self.at(&ids, 0)?),
353                    alias: ids.get(1).map(|a| self.text(*a)),
354                })
355            })
356            .collect()
357    }
358    fn maybe_tail(&self, n: Node) -> LR<Option<Tail>> {
359        match self.kid(n, "else_clause") {
360            Some(t) => Ok(Some(self.tail(t)?)),
361            None => Ok(None),
362        }
363    }
364    fn tail(&self, n: Node) -> LR<Tail> {
365        if let Some(sev) = self.kid(n, "severity") {
366            return Ok(Tail::Inline {
367                severity: self.text(sev),
368                template: self.template_parts(self.kid(n, "template_string").ok_or("tmpl")?)?,
369            });
370        }
371        let name = self.text(self.kid(n, "qualified_name").ok_or("name")?);
372        let args = self
373            .named(n)
374            .into_iter()
375            .filter(|c| c.kind() != "qualified_name")
376            .map(|c| self.expr(c))
377            .collect::<LR<Vec<_>>>()?;
378        Ok(Tail::Ref { name, args })
379    }
380    fn template_parts(&self, n: Node) -> LR<Vec<TPart>> {
381        let mut parts = Vec::new();
382        for c in self.named(n) {
383            match c.kind() {
384                "template_chars" => parts.push(TPart::Text(self.text(c))),
385                "template_escape" => {
386                    let t = self.text(c);
387                    let s = match t.as_str() {
388                        "\\n" => "\n",
389                        "\\t" => "\t",
390                        "\\r" => "\r",
391                        other => &other[1..],
392                    };
393                    parts.push(TPart::Text(s.to_string()));
394                }
395                "interpolation" => parts.push(TPart::Expr(self.expr(self.first_operand(c)?)?)),
396                _ => {}
397            }
398        }
399        Ok(parts)
400    }
401
402    /// the source range of a node, columns in UTF-16 units (the reference's)
403    fn col16(&self, byte: usize) -> usize {
404        let start = self.src[..byte]
405            .iter()
406            .rposition(|&b| b == b'\n')
407            .map(|i| i + 1)
408            .unwrap_or(0);
409        String::from_utf8_lossy(&self.src[start..byte])
410            .encode_utf16()
411            .count()
412    }
413    fn loc_of(&self, n: Node) -> Loc {
414        Loc {
415            sl: n.start_position().row,
416            sc: self.col16(n.start_byte()),
417            el: n.end_position().row,
418            ec: self.col16(n.end_byte()),
419        }
420    }
421
422    // ---------------- types ----------------
423    fn ty(&self, n: Node) -> LR<TypeAst> {
424        let mut t = self.ty0(n)?;
425        t.set_loc(self.loc_of(n));
426        Ok(t)
427    }
428    fn ty0(&self, n: Node) -> LR<TypeAst> {
429        Ok(match n.kind() {
430            "union_type" => TypeAst::Union {
431                arms: self
432                    .type_arms(n)
433                    .into_iter()
434                    .map(|c| self.ty(c))
435                    .collect::<LR<_>>()?,
436                loc: None,
437            },
438            // the literal types `true`, `false`, `null` (§3.3) are keyword tokens
439            "true" | "false" => TypeAst::Lit {
440                v: Value::Bool(n.kind() == "true"),
441                loc: None,
442            },
443            "null" => TypeAst::Lit {
444                v: Value::Null,
445                loc: None,
446            },
447            "intersection_type" => TypeAst::Isect {
448                arms: self
449                    .type_arms(n)
450                    .into_iter()
451                    .map(|c| self.ty(c))
452                    .collect::<LR<_>>()?,
453                loc: None,
454            },
455            "nullable_type" => TypeAst::Union {
456                arms: vec![
457                    self.ty(self.first(n)?)?,
458                    TypeAst::Prim {
459                        name: "null".into(),
460                        loc: None,
461                    },
462                ],
463                loc: None,
464            },
465            "array_type" => {
466                let elem = Box::new(self.ty(self.first(n)?)?);
467                let range = self.kid(n, "array_size_range").or_else(|| {
468                    self.field(n, "size")
469                        .filter(|s| s.kind() == "range_expression")
470                });
471                if let Some(r) = range {
472                    let ends: Vec<Value> = self
473                        .named(r)
474                        .into_iter()
475                        .map(|c| self.const_num(c))
476                        .collect::<LR<_>>()?;
477                    let excl = self
478                        .all(r)
479                        .iter()
480                        .any(|c| !c.is_named() && self.text(*c) == "..<");
481                    let lo = num_or_name(ends.first().ok_or("range endpoint")?);
482                    let hi = num_or_name(ends.get(1).ok_or("range endpoint")?);
483                    return Ok(match hi {
484                        Value::Int(h) => TypeAst::Array {
485                            elem,
486                            lo: Some(lo),
487                            hi: Some(Value::Int(if excl { &h - 1 } else { h })),
488                            excl: false,
489                            loc: None,
490                        },
491                        other => TypeAst::Array {
492                            elem,
493                            lo: Some(lo),
494                            hi: Some(other),
495                            excl,
496                            loc: None,
497                        },
498                    });
499                }
500                if let Some(size) = self.field(n, "size") {
501                    let v = num_or_name(&self.const_num(size)?);
502                    return Ok(TypeAst::Array {
503                        elem,
504                        lo: Some(v.clone()),
505                        hi: Some(v),
506                        excl: false,
507                        loc: None,
508                    });
509                }
510                TypeAst::Array {
511                    elem,
512                    lo: None,
513                    hi: None,
514                    excl: false,
515                    loc: None,
516                }
517            }
518            "range_type" => {
519                let nc = self.named(n);
520                TypeAst::Range {
521                    lo: self.const_num(self.at(&nc, 0)?)?,
522                    hi: self.const_num(self.at(&nc, 1)?)?,
523                    excl: self.text(n).contains("..<"),
524                    loc: None,
525                }
526            }
527            "number_literal" => TypeAst::Lit {
528                v: self.const_num(n)?,
529                loc: None,
530            },
531            "string" => TypeAst::Lit {
532                v: Value::Str(self.json_string(n)?.into()),
533                loc: None,
534            },
535            "pattern" => {
536                let t = self.text(n);
537                TypeAst::Pattern {
538                    re: t[1..t.len() - 1].to_string(),
539                    loc: None,
540                }
541            }
542            "paren_type" => self.ty(self.first(n)?)?,
543            "record_type" => {
544                let mut open = false;
545                let mut members = Vec::new();
546                // a member's annotations precede it as siblings (§5.10)
547                let mut pending: Vec<Annotation> = Vec::new();
548                for c in self.named(n) {
549                    if c.kind() == "open_marker" {
550                        open = true;
551                        continue;
552                    }
553                    if c.kind() == "annotation" {
554                        pending.push(self.annotation(c)?);
555                        continue;
556                    }
557                    if let Some(mut m) = self.member(c)? {
558                        m.set_annotations(std::mem::take(&mut pending));
559                        members.push(m);
560                    }
561                }
562                TypeAst::Record {
563                    members,
564                    open,
565                    loc: None,
566                }
567            }
568            "map_type" => TypeAst::Map {
569                key: Box::new(self.ty(self.req(n, "key")?)?),
570                val: Box::new(self.ty(self.req(n, "value")?)?),
571                loc: None,
572            },
573            "function_type" => {
574                let mut cs: Vec<TypeAst> = self
575                    .named(n)
576                    .into_iter()
577                    .map(|c| self.ty(c))
578                    .collect::<LR<_>>()?;
579                let ret = cs.pop().ok_or("func type")?;
580                TypeAst::Func {
581                    params: cs,
582                    ret: Box::new(ret),
583                    loc: None,
584                }
585            }
586            "named_type" => {
587                let name = self.text(self.kid(n, "qualified_name").ok_or("name")?);
588                let args = match self.kid(n, "type_arguments") {
589                    Some(a) => self
590                        .named(a)
591                        .into_iter()
592                        .map(|c| self.ty(c))
593                        .collect::<LR<_>>()?,
594                    None => vec![],
595                };
596                let preds = match self.field(n, "predicates") {
597                    Some(p) => Some(
598                        self.named(p)
599                            .into_iter()
600                            .map(|c| self.expr(c))
601                            .collect::<LR<_>>()?,
602                    ),
603                    None => None,
604                };
605                let ext = match self.field(n, "extension") {
606                    Some(e) => Some(Box::new(self.ty(e)?)),
607                    None => None,
608                };
609                if ["int", "uint", "float", "bool", "string"].contains(&name.as_str())
610                    && args.is_empty()
611                    && preds.is_none()
612                    && ext.is_none()
613                {
614                    return Ok(TypeAst::Prim { name, loc: None });
615                }
616                TypeAst::Named {
617                    name,
618                    args,
619                    preds,
620                    ext,
621                    loc: None,
622                }
623            }
624            _ => match self.text(n).as_str() {
625                "true" => TypeAst::Lit {
626                    v: Value::Bool(true),
627                    loc: None,
628                },
629                "false" => TypeAst::Lit {
630                    v: Value::Bool(false),
631                    loc: None,
632                },
633                "null" => TypeAst::Prim {
634                    name: "null".into(),
635                    loc: None,
636                },
637                other => return Err(format!("lower_type: unhandled {} '{}'", n.kind(), other)),
638            },
639        })
640    }
641    fn dim_expr(&self, n: Node) -> Vec<(String, i32)> {
642        let mut out = Vec::new();
643        let mut sign = 1;
644        for c in self.all(n) {
645            if !c.is_named() {
646                match self.text(c).as_str() {
647                    "/" => sign = -1,
648                    "*" => sign = 1,
649                    _ => {}
650                }
651                continue;
652            }
653            if c.kind() == "dimension_term" {
654                let nc = self.named(c);
655                let Some(ident) = nc.iter().find(|x| x.kind() == "identifier") else {
656                    continue;
657                };
658                let num = nc.iter().find(|x| x.kind() == "int");
659                let mut exp: i32 = num.map(|x| self.text(*x).parse().unwrap_or(1)).unwrap_or(1);
660                if self
661                    .all(c)
662                    .iter()
663                    .any(|x| !x.is_named() && self.text(*x) == "-")
664                {
665                    exp = -exp;
666                }
667                out.push((self.text(*ident), exp * sign));
668                sign = 1;
669            }
670        }
671        out
672    }
673    fn const_num(&self, n: Node) -> LR<Value> {
674        match n.kind() {
675            "number_literal" => {
676                let neg = self.text(n).trim_start().starts_with('-');
677                let v = self.const_num(self.first(n)?)?;
678                Ok(if neg { neg_value(v) } else { v })
679            }
680            "int" => Ok(Value::Int(crate::semantics::Num::from(parse_int(
681                &self.text(n),
682            )?))),
683            "float" => Ok(Value::Float(
684                self.text(n)
685                    .replace('_', "")
686                    .parse::<f64>()
687                    .map_err(|e| e.to_string())?,
688            )),
689            "qualified_name" | "identifier" => Ok(Value::Str(self.text(n).into())),
690            k => Err(format!("const_num: {k}")),
691        }
692    }
693
694    // ---------------- members ----------------
695    fn member(&self, n: Node) -> LR<Option<MemberAst>> {
696        let mut m = self.member0(n)?;
697        if let Some(m) = m.as_mut() {
698            m.set_loc(self.loc_of(n));
699        }
700        Ok(m)
701    }
702    fn member0(&self, n: Node) -> LR<Option<MemberAst>> {
703        Ok(Some(match n.kind() {
704            // member kinds by syntax (D4, v0.3): `?` — input may supply it; `= e` —
705            // the schema computes it. Both: defaulted; `= e` alone: derived
706            "value_member" => {
707                let name_n = self.req(n, "name")?;
708                let name = if name_n.kind() == "string" {
709                    self.json_string(name_n)?
710                } else {
711                    self.text(name_n)
712                };
713                let opt = self.field(n, "optional").is_some();
714                let dflt = match self.field(n, "default") {
715                    Some(d) => Some(self.expr(d)?),
716                    None => None,
717                };
718                match dflt {
719                    Some(expr) if !opt => MemberAst::Derived {
720                        name,
721                        ty: Some(self.ty(self.req(n, "type")?)?),
722                        expr,
723                        hidden: false,
724                        annotations: vec![],
725                        loc: None,
726                    },
727                    dflt => MemberAst::Value {
728                        name,
729                        opt,
730                        ty: self.ty(self.req(n, "type")?)?,
731                        dflt,
732                        annotations: vec![],
733                        loc: None,
734                    },
735                }
736            }
737            "derived_member" => {
738                let name_n = self.req(n, "name")?;
739                MemberAst::Derived {
740                    name: if name_n.kind() == "string" {
741                        self.json_string(name_n)?
742                    } else {
743                        self.text(name_n)
744                    },
745                    ty: None,
746                    expr: self.expr(self.req(n, "value")?)?,
747                    hidden: false,
748                    annotations: vec![],
749                    loc: None,
750                }
751            }
752            // `x$ [: T] = e` — computed for the schema's own use, never part of the value (D34)
753            "hidden_member" => MemberAst::Derived {
754                name: self.text(self.req(n, "name")?),
755                ty: match self.field(n, "type") {
756                    Some(t) => Some(self.ty(t)?),
757                    None => None,
758                },
759                expr: self.expr(self.req(n, "value")?)?,
760                hidden: true,
761                annotations: vec![],
762                loc: None,
763            },
764            "context_declaration" => MemberAst::Context {
765                variable: self.text(self.req(n, "variable")?),
766                ty: self.ty(self.req(n, "type")?)?,
767                annotations: vec![],
768                loc: None,
769            },
770            "assert_member" => MemberAst::Assert {
771                name: self.text(self.req(n, "name")?),
772                cond: self.expr(self.req(n, "condition")?)?,
773                tail: self.maybe_tail(n)?,
774                annotations: vec![],
775                loc: None,
776            },
777            "when_member" => {
778                let mut body = Vec::new();
779                for c in self.named(n).into_iter().skip(1) {
780                    if let Some(m) = self.member(c)? {
781                        body.push(m);
782                    }
783                }
784                MemberAst::When {
785                    cond: self.expr(self.req(n, "condition")?)?,
786                    body,
787                    annotations: vec![],
788                    loc: None,
789                }
790            }
791            _ => return Ok(None),
792        }))
793    }
794
795    // ---------------- expressions ----------------
796    fn expr(&self, n: Node) -> LR<Rc<Expr>> {
797        let e = Rc::new(self.expr_inner(n)?);
798        set_expr_loc(&e, self.loc_of(n));
799        Ok(e)
800    }
801    fn expr_inner(&self, n: Node) -> LR<Expr> {
802        const BIN: [&str; 13] = [
803            "pipe_expression",
804            "nullish_expression",
805            "binary_expression_or",
806            "binary_expression_and",
807            "bit_or_expression",
808            "bit_xor_expression",
809            "bit_and_expression",
810            "equality_expression",
811            "relational_expression",
812            "range_expression",
813            "shift_expression",
814            "additive_expression",
815            "multiplicative_expression",
816        ];
817        Ok(match n.kind() {
818            "int" => Expr::Lit(Value::Int(crate::semantics::Num::from(parse_int(
819                &self.text(n),
820            )?))),
821            "float" => Expr::Lit(Value::Float(
822                self.text(n)
823                    .replace('_', "")
824                    .parse::<f64>()
825                    .map_err(|e| e.to_string())?,
826            )),
827            "unit_literal" => {
828                let t = self.text(n);
829                let re =
830                    regex::Regex::new(r"^([0-9._]+(?:[eE][+-]?[0-9]+)?)([A-Za-z][A-Za-z0-9]*)$")
831                        .unwrap();
832                let caps = re.captures(&t).ok_or("unit literal")?;
833                Expr::UnitLit {
834                    num: caps[1]
835                        .replace('_', "")
836                        .parse::<f64>()
837                        .map_err(|e| e.to_string())?,
838                    unit: caps[2].to_string(),
839                }
840            }
841            "string" => Expr::Lit(Value::Str(self.json_string(n)?.into())),
842            "template_string" => Expr::Template(self.template_parts(n)?),
843            "identifier" | "hidden_name" => Expr::Name(self.text(n)),
844            "context_variable" => Expr::Ctx(self.text(n)),
845            "referrers_expression" => Expr::Referrers {
846                ty: self.text(self.req(n, "type")?),
847                member: self.json_string(self.req(n, "member")?)?,
848            },
849            "paren_expression" => Expr::Paren(self.expr(self.first_operand(n)?)?),
850            "unary_expression" => Expr::Un {
851                op: self.text(self.all(n).into_iter().next().ok_or("operator")?),
852                x: self.expr(self.first_operand(n)?)?,
853            },
854            "if_expression" => Expr::If {
855                c: self.expr(self.req(n, "condition")?)?,
856                t: self.expr(self.req(n, "then")?)?,
857                f: self.expr(self.req(n, "else")?)?,
858            },
859            "lambda" => Expr::Lambda {
860                params: self
861                    .kids(n, "lambda_parameter")
862                    .into_iter()
863                    .map(|p| self.first(p).map(|c| self.text(c)))
864                    .collect::<LR<_>>()?,
865                body: self.expr(self.req(n, "body")?)?,
866            },
867            "with_expression" => {
868                let nc = self.operands(n);
869                Expr::With {
870                    base: self.expr(self.at(&nc, 0)?)?,
871                    patch: self.expr(self.at(&nc, 1)?)?,
872                }
873            }
874            "member_access" | "safe_access" => {
875                let nc = self.operands(n);
876                let name_n = self.at(&nc, 1)?;
877                Expr::Member {
878                    x: self.expr(self.at(&nc, 0)?)?,
879                    name: if name_n.kind() == "string" {
880                        self.json_string(name_n)?
881                    } else {
882                        self.text(name_n)
883                    },
884                    safe: n.kind() == "safe_access",
885                }
886            }
887            "index_access" => {
888                let nc = self.operands(n);
889                Expr::Index {
890                    x: self.expr(self.at(&nc, 0)?)?,
891                    i: self.expr(self.at(&nc, 1)?)?,
892                }
893            }
894            "call" => {
895                let cs = self.operands(n);
896                Expr::Call {
897                    fun: self.expr(self.at(&cs, 0)?)?,
898                    args: cs
899                        .iter()
900                        .skip(1)
901                        .map(|c| self.expr(*c))
902                        .collect::<LR<_>>()?,
903                }
904            }
905            "object" => {
906                if let Some(comp) = self.kid(n, "map_comprehension") {
907                    return self.expr_inner(comp);
908                }
909                let mut entries = Vec::new();
910                for en in self.kids(n, "object_entry") {
911                    match self.field(en, "key") {
912                        Some(k) => entries.push((
913                            if k.kind() == "string" {
914                                self.json_string(k)?
915                            } else {
916                                self.text(k)
917                            },
918                            self.expr(self.req(en, "value")?)?,
919                        )),
920                        // a spread entry (§4.2): the key `...` never comes from source
921                        // unquoted, and a quoted "..." key carries a plain value, so the
922                        // `Spread` node tells them apart
923                        None => entries.push((
924                            "...".to_string(),
925                            Rc::new(Expr::Spread(self.expr(self.first(en)?)?)),
926                        )),
927                    }
928                }
929                Expr::Obj(entries)
930            }
931            "map_comprehension" => Expr::MapComp {
932                key: self.expr(self.req(n, "key")?)?,
933                val: self.expr(self.req(n, "value")?)?,
934                clauses: self
935                    .kids(n, "for_clause")
936                    .into_iter()
937                    .map(|c| self.for_clause(c))
938                    .collect::<LR<_>>()?,
939            },
940            "array" => {
941                if let Some(comp) = self.kid(n, "array_comprehension") {
942                    return self.expr_inner(comp);
943                }
944                let mut items = Vec::new();
945                for en in self.kids(n, "array_entry") {
946                    let spread = self.text(en).starts_with("...");
947                    let inner = self
948                        .named(en)
949                        .into_iter()
950                        .next()
951                        .or_else(|| {
952                            self.all(en).into_iter().find(|c| {
953                                ["true", "false", "null"].contains(&self.text(*c).as_str())
954                            })
955                        })
956                        .ok_or("entry")?;
957                    items.push((spread, self.expr(inner)?));
958                }
959                Expr::Arr(items)
960            }
961            "array_comprehension" => Expr::Comp {
962                head: self.expr(self.req(n, "head")?)?,
963                clauses: self
964                    .kids(n, "for_clause")
965                    .into_iter()
966                    .map(|c| self.for_clause(c))
967                    .collect::<LR<_>>()?,
968            },
969            "matches_expression" => {
970                let nc = self.named(n);
971                Expr::Bin {
972                    op: "matches".into(),
973                    l: self.expr(self.at(&nc, 0)?)?,
974                    r: self.expr(self.at(&nc, 1)?)?,
975                }
976            }
977            "pattern" => {
978                let t = self.text(n);
979                Expr::Pattern(t[1..t.len() - 1].to_string())
980            }
981            "match_expression" => {
982                let mut arms = Vec::new();
983                for a in self.kids(n, "match_arm") {
984                    let body = self.req(a, "body")?;
985                    let others: Vec<Node> = self
986                        .named(a)
987                        .into_iter()
988                        .filter(|c| c.id() != body.id())
989                        .collect();
990                    arms.push(MatchArm {
991                        v: self.text(self.at(&others, 0)?),
992                        ty: if others.len() > 1 {
993                            Some(self.ty(others[1])?)
994                        } else {
995                            None
996                        },
997                        body: self.expr(body)?,
998                    });
999                }
1000                Expr::Match {
1001                    subject: self.expr(self.req(n, "subject")?)?,
1002                    arms,
1003                }
1004            }
1005            k if BIN.contains(&k) => {
1006                let nc = self.operands(n);
1007                // the operator is the one anonymous child that is not an operand
1008                let op = self
1009                    .all(n)
1010                    .into_iter()
1011                    .filter(|c| !c.is_named() && !self.is_lit_keyword(*c))
1012                    .map(|c| self.text(c))
1013                    .find(|t| !t.trim().is_empty())
1014                    .ok_or("op")?;
1015                Expr::Bin {
1016                    op,
1017                    l: self.expr(self.at(&nc, 0)?)?,
1018                    r: self.expr(self.at(&nc, 1)?)?,
1019                }
1020            }
1021            _ => match self.text(n).as_str() {
1022                "true" => Expr::Lit(Value::Bool(true)),
1023                "false" => Expr::Lit(Value::Bool(false)),
1024                "null" => Expr::Lit(Value::Null),
1025                other => return Err(format!("lower_expr: unhandled {} '{}'", n.kind(), other)),
1026            },
1027        })
1028    }
1029    fn for_clause(&self, n: Node) -> LR<ForClause> {
1030        let mut cur = n.walk();
1031        let filters = n
1032            .children_by_field_name("filter", &mut cur)
1033            .map(|c| self.expr(c))
1034            .collect::<LR<Vec<_>>>()?;
1035        Ok(ForClause {
1036            v: self.text(self.req(n, "variable")?),
1037            iter: self.expr(self.req(n, "iterable")?)?,
1038            filters,
1039        })
1040    }
1041}
1042
1043fn num_or_name(v: &Value) -> Value {
1044    match v {
1045        Value::Float(f) => Value::Int(crate::semantics::Num::from(*f as i64)),
1046        other => other.clone(),
1047    }
1048}
1049fn neg_value(v: Value) -> Value {
1050    match v {
1051        Value::Int(i) => Value::Int(-i),
1052        Value::Float(f) => Value::Float(-f),
1053        other => other,
1054    }
1055}
1056/// An integer literal's text (§2.6: decimal, `0x`, `0o`, `0b`, `_` separators) as
1057/// an arbitrary-precision integer.
1058pub fn parse_int(text: &str) -> LR<BigInt> {
1059    let t = text.replace('_', "");
1060    let (radix, digits) = if let Some(h) = t.strip_prefix("0x").or_else(|| t.strip_prefix("0X")) {
1061        (16, h.to_string())
1062    } else if let Some(o) = t.strip_prefix("0o").or_else(|| t.strip_prefix("0O")) {
1063        (8, o.to_string())
1064    } else if let Some(b) = t.strip_prefix("0b").or_else(|| t.strip_prefix("0B")) {
1065        (2, b.to_string())
1066    } else {
1067        (10, t.clone())
1068    };
1069    BigInt::from_str_radix(&digits, radix).map_err(|e| e.to_string())
1070}
1071
1072/// JSON string literal -> its value (the lexer guarantees the form)
1073pub fn json_unquote(s: &str) -> LR<String> {
1074    let inner = &s[1..s.len() - 1];
1075    let mut out = String::new();
1076    let mut chars = inner.chars();
1077    while let Some(c) = chars.next() {
1078        if c != '\\' {
1079            out.push(c);
1080            continue;
1081        }
1082        match chars.next() {
1083            Some('n') => out.push('\n'),
1084            Some('t') => out.push('\t'),
1085            Some('r') => out.push('\r'),
1086            Some('b') => out.push('\u{8}'),
1087            Some('f') => out.push('\u{c}'),
1088            Some('u') => {
1089                let hex: String = chars.by_ref().take(4).collect();
1090                let cp = u32::from_str_radix(&hex, 16).map_err(|e| e.to_string())?;
1091                out.push(char::from_u32(cp).unwrap_or('\u{fffd}'));
1092            }
1093            Some(other) => out.push(other),
1094            None => {}
1095        }
1096    }
1097    Ok(out)
1098}
1099
1100/// parse one expression's text: the text is wrapped in a constant declaration; None when it does not parse
1101pub fn parse_expr_text(text: &str) -> Option<Rc<Expr>> {
1102    let r = parse_source(&format!("const __e = {text}\n"));
1103    if r.errors.is_empty() && r.decls.len() == 1 {
1104        if let DeclBody::Const { expr, .. } = &r.decls[0].body {
1105            return Some(expr.clone());
1106        }
1107    }
1108    None
1109}