meta-language 1.0.0

A self-describing links-network core for lossless language representation
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
//! Expressions from operator precedence down to literals and references.

use super::{
    ASSIGNMENTS, BinaryOp, FLOAT, GLOBALS, JavaScriptParser, Language, ROOT, Result, SArrayItem,
    SData, SExpr, SNode, SPattern, SPatternNode, SRow, STagTest, ShowStyle, Span, Token,
    TokenCursor, TokenKind, UnaryOp, binary, describe, has_ctor, is_tag_field, joined, node,
    number_literal, span, tokenize, type_error, unsupported, wild,
};

impl JavaScriptParser {
    /// Expressions, by JavaScript precedence.
    pub(super) fn expr(&mut self) -> Result<SExpr> {
        let start = self.peek();
        if start.kind == TokenKind::Identifier && self.cursor.is_at("=>", 1) {
            return Err(unsupported(
                "arrow function",
                "functions as values are outside the portable core",
                Some(span(&start, self.cursor.peek_at(1))),
            ));
        }
        let cond = self.or()?;
        let next = self.cursor.peek();
        if next.kind == TokenKind::Punct && ASSIGNMENTS.contains(&next.value.as_str()) {
            return Err(unsupported(
                "assignment",
                "mutation is outside the portable core",
                Some(self.to_here(&start)),
            ));
        }
        if self.cursor.eat("?").is_none() {
            return Ok(cond);
        }
        let then = self.expr()?;
        self.cursor.expect(":", Some("conditional expression"))?;
        let otherwise = self.expr()?;
        let place = joined(&cond, &otherwise, &start);
        Ok(node(
            SNode::If {
                cond: Box::new(cond),
                then: Box::new(then),
                otherwise: Box::new(otherwise),
            },
            place,
        ))
    }

    pub(super) fn or(&mut self) -> Result<SExpr> {
        let mut left = self.and()?;
        loop {
            let token = self.peek();
            if self.cursor.is("??") {
                return Err(unsupported(
                    "nullish coalescing",
                    "null and undefined are outside the portable core",
                    Some(span(&token, &token)),
                ));
            }
            if self.cursor.eat("||").is_none() {
                return Ok(left);
            }
            let right = self.and()?;
            left = binary(BinaryOp::Or, left, right, &token);
        }
    }

    pub(super) fn and(&mut self) -> Result<SExpr> {
        let mut left = self.bitwise()?;
        while self.cursor.is("&&") {
            let token = self.cursor.advance();
            let right = self.bitwise()?;
            left = binary(BinaryOp::And, left, right, &token);
        }
        Ok(left)
    }

    pub(super) fn bitwise(&mut self) -> Result<SExpr> {
        let left = self.equality()?;
        let token = self.peek();
        if token.kind == TokenKind::Punct && ["|", "^", "&"].contains(&token.value.as_str()) {
            return Err(unsupported(
                &format!("bitwise {}", token.value),
                "bitwise operators are outside the portable core",
                Some(span(&token, &token)),
            ));
        }
        Ok(left)
    }

    pub(super) fn equality(&mut self) -> Result<SExpr> {
        let mut left = self.relational()?;
        loop {
            let token = self.peek();
            if self.cursor.is("==") || self.cursor.is("!=") {
                return Err(unsupported(
                    &format!("loose equality {}", token.value),
                    "use === or !==",
                    Some(span(&token, &token)),
                ));
            }
            let op = match (token.kind, token.value.as_str()) {
                (TokenKind::Punct, "===") => BinaryOp::Eq,
                (TokenKind::Punct, "!==") => BinaryOp::Ne,
                _ => return Ok(left),
            };
            self.cursor.advance();
            let right = self.relational()?;
            left = if is_tag_field(&left) || is_tag_field(&right) {
                self.tag_test(op, left, right, &token)?
            } else {
                binary(op, left, right, &token)
            };
        }
    }

    /// `x.$ === 'tag'` tests the alternative of a data value.
    pub(super) fn tag_test(
        &self,
        op: BinaryOp,
        left: SExpr,
        right: SExpr,
        token: &Token,
    ) -> Result<SExpr> {
        let place = joined(&left, &right, token);
        let (field, tag) = if is_tag_field(&left) {
            (left, right)
        } else {
            (right, left)
        };
        let SNode::Field { object, .. } = field.node else {
            return Ok(field);
        };
        let SNode::Str { value: tag } = tag.node else {
            return Err(unsupported(
                "computed tag test",
                "compare the $ tag with a string literal",
                Some(place),
            ));
        };
        let candidates: Vec<&SData> = self
            .data_types
            .iter()
            .filter(|data| has_ctor(data, &tag))
            .collect();
        let data = match candidates.as_slice() {
            [data] => (*data).clone(),
            [] => {
                return Err(type_error(
                    format!("no @typedef has tag {tag}"),
                    Some(place),
                ));
            }
            _ => {
                return Err(type_error(
                    format!("tag {tag} is ambiguous between data types"),
                    Some(place),
                ));
            }
        };
        let arity = data
            .ctors
            .iter()
            .find(|ctor| ctor.name == tag)
            .map_or(0, |ctor| ctor.fields.len());
        let pattern = SPattern {
            node: SPatternNode::Ctor {
                path: vec![ROOT.to_owned(), data.name.clone(), tag.clone()],
                args: (0..arity).map(|_| wild(place)).collect(),
            },
            span: Some(place),
        };
        let equal = op == BinaryOp::Eq;
        let rows = vec![
            SRow {
                patterns: vec![pattern],
                body: node(SNode::Bool { value: equal }, place),
                span: Some(place),
            },
            SRow {
                patterns: vec![wild(place)],
                body: node(SNode::Bool { value: !equal }, place),
                span: Some(place),
            },
        ];
        let mut expr = node(
            SNode::Match {
                scrutinees: vec![object.as_ref().clone()],
                rows,
            },
            place,
        );
        expr.tag_test = Some(Box::new(STagTest {
            object: *object,
            tag,
            data,
            negated: op == BinaryOp::Ne,
        }));
        Ok(expr)
    }

    pub(super) fn relational(&mut self) -> Result<SExpr> {
        let mut left = self.shift()?;
        loop {
            let token = self.peek();
            if self.cursor.is("in") || self.cursor.is("instanceof") {
                return Err(unsupported(
                    &format!("{} operator", token.value),
                    "objects are outside the portable core",
                    Some(span(&token, &token)),
                ));
            }
            let op = match (token.kind, token.value.as_str()) {
                (TokenKind::Punct, "<") => BinaryOp::Lt,
                (TokenKind::Punct, "<=") => BinaryOp::Le,
                (TokenKind::Punct, ">") => BinaryOp::Gt,
                (TokenKind::Punct, ">=") => BinaryOp::Ge,
                _ => return Ok(left),
            };
            self.cursor.advance();
            let right = self.shift()?;
            left = binary(op, left, right, &token);
        }
    }

    pub(super) fn shift(&mut self) -> Result<SExpr> {
        let left = self.additive()?;
        let token = self.peek();
        if self.cursor.is("<<") || self.cursor.is(">>") || self.cursor.is(">>>") {
            return Err(unsupported(
                &format!("shift {}", token.value),
                "bit shifts are outside the portable core",
                Some(span(&token, &token)),
            ));
        }
        Ok(left)
    }

    /// `+` adds numbers and concatenates strings; the checker decides which by the operand types.
    pub(super) fn additive(&mut self) -> Result<SExpr> {
        let mut left = self.multiplicative()?;
        loop {
            let token = self.peek();
            if !self.cursor.is("+") && !self.cursor.is("-") {
                return Ok(left);
            }
            self.cursor.advance();
            let right = self.multiplicative()?;
            let op = if token.value == "+" {
                BinaryOp::Plus
            } else {
                BinaryOp::Sub
            };
            left = binary(op, left, right, &token);
        }
    }

    pub(super) fn multiplicative(&mut self) -> Result<SExpr> {
        let mut left = self.exponent()?;
        loop {
            let token = self.peek();
            let op = match (token.kind, token.value.as_str()) {
                (TokenKind::Punct, "*") => BinaryOp::Mul,
                (TokenKind::Punct, "/") => BinaryOp::Div,
                (TokenKind::Punct, "%") => BinaryOp::Rem,
                _ => return Ok(left),
            };
            self.cursor.advance();
            let right = self.exponent()?;
            left = binary(op, left, right, &token);
        }
    }

    pub(super) fn exponent(&mut self) -> Result<SExpr> {
        let left = self.unary()?;
        if self.cursor.is("**") {
            let token = self.peek();
            return Err(unsupported(
                "exponentiation",
                "powers are outside the portable core; use recursion",
                Some(span(&token, &token)),
            ));
        }
        Ok(left)
    }

    pub(super) fn unary(&mut self) -> Result<SExpr> {
        let token = self.peek();
        for (symbol, op) in [("!", UnaryOp::Not), ("-", UnaryOp::Neg)] {
            if self.cursor.eat(symbol).is_some() {
                let arg = self.unary()?;
                let place = Span::new(token.start, arg.span.map_or(token.end, |arg| arg.end));
                return Ok(node(
                    SNode::Unary {
                        op,
                        arg: Box::new(arg),
                    },
                    place,
                ));
            }
        }
        let here = Some(span(&token, &token));
        if self.cursor.is("+") {
            return Err(unsupported(
                "unary +",
                "unary plus throws a TypeError on BigInt values",
                here,
            ));
        }
        if self.cursor.is("~") {
            return Err(unsupported(
                "bitwise ~",
                "bitwise operators are outside the portable core",
                here,
            ));
        }
        if self.cursor.is("++") || self.cursor.is("--") {
            return Err(unsupported(
                &format!("{} operator", token.value),
                "mutation is outside the portable core",
                here,
            ));
        }
        if token.kind == TokenKind::Identifier && token.value == "await" {
            if !self.in_async {
                return Err(unsupported(
                    "await outside an async function",
                    "await is only valid in async functions and at the top level of a module",
                    here,
                ));
            }
            self.cursor.advance();
            self.sequential_async = true;
            return self.awaited(Self::unary);
        }
        if token.kind == TokenKind::Identifier
            && ["typeof", "void", "delete", "await", "yield"].contains(&token.value.as_str())
        {
            return Err(unsupported(
                &format!("{} operator", token.value),
                "outside the portable core",
                here,
            ));
        }
        self.postfix()
    }

    /// An expression whose value is awaited: when it is a call of an async
    /// function, that call is awaited where it is made, which is its ordinary
    /// call. Any other value is awaited as itself.
    pub(super) fn awaited(&mut self, read: fn(&mut Self) -> Result<SExpr>) -> Result<SExpr> {
        let before = self.unawaited.len();
        let expr = read(self)?;
        let path = match &expr.node {
            SNode::App { func, .. } => match &func.node {
                SNode::Name { path } => Some(path),
                _ => None,
            },
            SNode::Name { path } => Some(path),
            _ => None,
        };
        let awaits_last = match (path, self.unawaited.last()) {
            (Some(path), Some((name, _))) => {
                path.first().map(String::as_str) == Some(ROOT) && path[1..].join(".") == *name
            }
            _ => false,
        };
        if self.unawaited.len() > before && awaits_last {
            self.unawaited.pop();
        }
        Ok(expr)
    }

    pub(super) fn postfix(&mut self) -> Result<SExpr> {
        let mut expr = self.primary()?;
        loop {
            let token = self.peek();
            let start = expr.span.map_or(token.start, |place| place.start);
            if self.cursor.is(".") && self.cursor.is_kind_at(TokenKind::Identifier, 1) {
                self.cursor.advance();
                let field = self.cursor.advance();
                if self.cursor.is("(") {
                    if field.value != "toString" {
                        return Err(unsupported(
                            &format!("method call .{}()", field.value),
                            "methods of values are outside the portable core",
                            Some(self.to_here(&token)),
                        ));
                    }
                    let args = self.arguments("toString")?;
                    if !args.is_empty() {
                        return Err(unsupported(
                            "toString with a radix",
                            "only decimal text is portable",
                            Some(self.to_here(&token)),
                        ));
                    }
                    let place = Span::new(start, self.to_here(&token).end);
                    expr = node(
                        SNode::ToString {
                            arg: Box::new(expr),
                        },
                        place,
                    );
                    continue;
                }
                let place = Span::new(start, span(&field, &field).end);
                expr = node(
                    SNode::Field {
                        object: Box::new(expr),
                        field: field.value,
                    },
                    place,
                );
                continue;
            }
            let here = Some(span(&token, &token));
            if self.cursor.is("?.") {
                return Err(unsupported(
                    "optional chaining",
                    "null and undefined are outside the portable core",
                    here,
                ));
            }
            if self.cursor.is("[") {
                self.cursor.advance();
                let index = self.expr()?;
                let close = self.cursor.expect("]", Some("index"))?;
                let place = Span::new(
                    expr.span.map_or(token.start, |place| place.start),
                    close.end,
                );
                expr = node(
                    SNode::Index {
                        object: Box::new(expr),
                        index: Box::new(index),
                    },
                    place,
                );
                let next = self.cursor.peek();
                if next.kind == TokenKind::Punct
                    && (ASSIGNMENTS.contains(&next.value.as_str())
                        || self.cursor.is("++")
                        || self.cursor.is("--"))
                {
                    return Err(unsupported(
                        "assignment of an array element",
                        "the portable core reads arrays and does not mutate them; build a new array with [...xs, value]",
                        Some(place),
                    ));
                }
                continue;
            }
            if self.cursor.is("(") {
                return Err(unsupported(
                    "call of a computed function",
                    "only named functions can be called",
                    here,
                ));
            }
            if self.cursor.is_kind(TokenKind::Template) {
                return Err(unsupported(
                    "tagged template",
                    "template tags are functions as values",
                    here,
                ));
            }
            if self.cursor.is("++") || self.cursor.is("--") {
                return Err(unsupported(
                    &format!("{} operator", token.value),
                    "mutation is outside the portable core",
                    here,
                ));
            }
            return Ok(expr);
        }
    }

    pub(super) fn primary(&mut self) -> Result<SExpr> {
        let token = self.peek();
        match token.kind {
            TokenKind::Number => {
                self.cursor.advance();
                return Ok(number_literal(&token));
            }
            TokenKind::String => {
                self.cursor.advance();
                return Ok(node(
                    SNode::Str {
                        value: token.value.clone(),
                    },
                    span(&token, &token),
                ));
            }
            TokenKind::Template => {
                self.cursor.advance();
                return self.template(&token);
            }
            TokenKind::Identifier => return self.identifier(),
            _ => {}
        }
        let here = Some(span(&token, &token));
        if self.cursor.is("(") {
            let close = self.matching(self.cursor.index);
            if self
                .cursor
                .tokens
                .get(close + 1)
                .is_some_and(|after| after.value == "=>")
            {
                return Err(unsupported(
                    "arrow function",
                    "functions as values are outside the portable core",
                    here,
                ));
            }
            self.cursor.advance();
            let mut inner = self.expr()?;
            self.cursor.expect(")", Some("parenthesised expression"))?;
            inner.span = Some(self.to_here(&token));
            return Ok(inner);
        }
        if self.cursor.is("{") {
            return self.object_literal();
        }
        if self.cursor.is("[") {
            return self.array_literal();
        }
        if self.cursor.is("/") {
            return Err(unsupported(
                "regular expression",
                "outside the portable core",
                here,
            ));
        }
        Err(self.fail_here("expected an expression"))
    }

    /// `[a, ...xs, b]`: the elements in order, with the elements of each spread array in place.
    fn array_literal(&mut self) -> Result<SExpr> {
        let start = self.cursor.advance();
        let mut items = Vec::new();
        while !self.cursor.is("]") {
            if self.cursor.is(",") {
                let token = self.peek();
                return Err(unsupported(
                    "array hole",
                    "a hole is an element that reads undefined, which is not a portable value",
                    Some(span(&token, &token)),
                ));
            }
            let spread = self.cursor.eat("...").is_some();
            items.push(SArrayItem {
                spread,
                value: self.expr()?,
            });
            if self.cursor.eat(",").is_none() {
                break;
            }
        }
        self.cursor.expect("]", Some("array literal"))?;
        Ok(node(
            SNode::Array {
                items,
                element: None,
            },
            self.to_here(&start),
        ))
    }

    pub(super) fn matching(&self, index: usize) -> usize {
        let tokens = &self.cursor.tokens;
        let mut depth = 0_i64;
        for (at, token) in tokens.iter().enumerate().skip(index) {
            if token.kind != TokenKind::Punct {
                continue;
            }
            if ["(", "[", "{"].contains(&token.value.as_str()) {
                depth += 1;
            }
            if [")", "]", "}"].contains(&token.value.as_str()) {
                depth -= 1;
                if depth == 0 {
                    return at;
                }
            }
        }
        tokens.len() - 1
    }

    /// Template literals concatenate their text with `String(value)` of each substitution.
    pub(super) fn template(&mut self, token: &Token) -> Result<SExpr> {
        let whole = span(token, token);
        let mut pieces = Vec::new();
        for part in &token.parts {
            if !part.text.is_empty() {
                pieces.push(node(
                    SNode::Str {
                        value: part.text.clone(),
                    },
                    whole,
                ));
            }
            if let Some(expression) = &part.expression {
                let value = self.subexpression(&expression.source, expression.offset)?;
                let place = value.span;
                pieces.push(SExpr::new(
                    SNode::Show {
                        arg: Box::new(value),
                        style: ShowStyle::JsTemplate,
                    },
                    place,
                ));
            }
        }
        let mut pieces = pieces.into_iter();
        let Some(first) = pieces.next() else {
            return Ok(node(
                SNode::Str {
                    value: String::new(),
                },
                whole,
            ));
        };
        Ok(pieces.fold(first, |left, right| {
            node(
                SNode::Binary {
                    op: BinaryOp::Concat,
                    left: Box::new(left),
                    right: Box::new(right),
                    rounding: None,
                },
                whole,
            )
        }))
    }

    pub(super) fn subexpression(&mut self, source: &str, offset: usize) -> Result<SExpr> {
        let mut tokens = tokenize(source, Language::JavaScript)?.tokens;
        for token in &mut tokens {
            token.start += offset;
            token.end += offset;
        }
        let outer = std::mem::replace(
            &mut self.cursor,
            TokenCursor::new(tokens, Language::JavaScript),
        );
        let expr = self.substitution();
        self.cursor = outer;
        expr
    }

    pub(super) fn substitution(&mut self) -> Result<SExpr> {
        let expr = self.expr()?;
        if !self.cursor.at_end() {
            return Err(self.fail_here("expected } after the template substitution"));
        }
        Ok(expr)
    }

    /// `{ $: 'node', left, value: v }` constructs the `node` alternative of a data type.
    pub(super) fn object_literal(&mut self) -> Result<SExpr> {
        let start = self.cursor.advance();
        let mut tag = None;
        let mut fields: Vec<(String, SExpr)> = Vec::new();
        while !self.cursor.is("}") {
            let key = self.peek();
            let here = Some(span(&key, &key));
            if self.cursor.is("...") {
                return Err(unsupported("object spread", "name every field", here));
            }
            if key.kind != TokenKind::Identifier {
                return Err(unsupported(
                    &format!("object key {}", describe(&key)),
                    "keys are identifiers",
                    here,
                ));
            }
            self.cursor.advance();
            if key.value == "$" {
                self.cursor.expect(":", Some("object"))?;
                let value = self.cursor.advance();
                if value.kind != TokenKind::String {
                    return Err(unsupported(
                        "computed tag",
                        "the $ tag is a string literal",
                        Some(span(&value, &value)),
                    ));
                }
                tag = Some(value.value);
            } else {
                if fields.iter().any(|(name, _)| *name == key.value) {
                    return Err(type_error(format!("duplicate field {}", key.value), here));
                }
                if self.cursor.is("(") {
                    return Err(unsupported(
                        "method in an object value",
                        "functions as values are outside the portable core",
                        here,
                    ));
                }
                let value = if self.cursor.eat(":").is_some() {
                    self.expr()?
                } else {
                    self.reference(&key)?
                };
                // Assigning `__proto__` sets the prototype of the JavaScript
                // frontend's field object rather than adding a field.
                if key.value != "__proto__" {
                    fields.push((key.value, value));
                }
            }
            if self.cursor.eat(",").is_none() {
                break;
            }
        }
        self.cursor.expect("}", Some("object"))?;
        let Some(tag) = tag else {
            return Err(unsupported(
                "object without a $ tag",
                "a portable object is an alternative of a @typedef data type",
                Some(self.to_here(&start)),
            ));
        };
        Ok(node(
            SNode::CtorObject { tag, fields },
            self.to_here(&start),
        ))
    }

    pub(super) fn identifier(&mut self) -> Result<SExpr> {
        let token = self.peek();
        let here = Some(span(&token, &token));
        if token.value == "true" || token.value == "false" {
            self.cursor.advance();
            return Ok(node(
                SNode::Bool {
                    value: token.value == "true",
                },
                span(&token, &token),
            ));
        }
        if token.value == "NaN" || token.value == "Infinity" {
            self.cursor.advance();
            return Ok(node(
                SNode::Num {
                    value: token.value.clone(),
                    ty: Some(FLOAT),
                    negative: false,
                    unit: false,
                },
                span(&token, &token),
            ));
        }
        if ["null", "undefined"].contains(&token.value.as_str()) {
            return Err(unsupported(&token.value, "outside the portable core", here));
        }
        let expressions = [
            "this",
            "super",
            "new",
            "function",
            "class",
            "import",
            "arguments",
        ];
        if expressions.contains(&token.value.as_str()) {
            return Err(unsupported(
                &format!("{} expression", token.value),
                "outside the portable core",
                here,
            ));
        }
        self.cursor.advance();
        if self.scope.locals.contains(&token.value) || self.scope.tdz.contains(&token.value) {
            return self.reference(&token);
        }
        if self
            .assertion
            .as_ref()
            .is_some_and(|assertion| assertion.name == token.value)
        {
            return Err(unsupported(
                "assertion in an expression",
                "assertions are top-level statements",
                here,
            ));
        }
        // A global: a function, or a namespace path to a method, which must be called.
        let mut segments = vec![token.value.clone()];
        while self.cursor.is(".") && self.cursor.is_kind_at(TokenKind::Identifier, 1) {
            self.cursor.advance();
            segments.push(self.cursor.advance().value);
        }
        let name = segments.join(".");
        let place = self.to_here(&token);
        if let Some(math) = self.math(&token, &name)? {
            return Ok(math);
        }
        if !self.cursor.is("(") {
            return Err(unsupported(
                &format!("function value {name}"),
                "functions and namespaces are only portable when a function is called",
                Some(place),
            ));
        }
        let mut args = self.arguments(&name)?;
        let called = self.to_here(&token);
        if name == "String" && args.len() == 1 {
            let arg = args.remove(0);
            return Ok(node(SNode::ToString { arg: Box::new(arg) }, called));
        }
        if name == "Object.freeze" && args.len() == 1 {
            let mut arg = args.remove(0);
            arg.span = Some(called);
            return Ok(arg);
        }
        if GLOBALS.contains(&segments[0].as_str()) || name == "String" {
            return Err(unsupported(
                &name,
                "the JavaScript standard library is outside the portable core",
                Some(called),
            ));
        }
        if segments.len() == 1 && self.async_names.contains(&name) {
            self.unawaited.push((name, called));
        }
        let mut path = vec![ROOT.to_owned()];
        path.extend(segments);
        if args.is_empty() {
            return Ok(node(SNode::Name { path }, called));
        }
        Ok(node(
            SNode::App {
                func: Box::new(node(SNode::Name { path }, place)),
                args,
            },
            called,
        ))
    }

    /// A local variable; reading one before its declaration throws in JavaScript.
    pub(super) fn reference(&self, token: &Token) -> Result<SExpr> {
        let here = span(token, token);
        if self.scope.tdz.contains(&token.value) {
            return Err(unsupported(
                &format!("use of {} before its declaration", token.value),
                "the binding is in its temporal dead zone, where reading it throws a ReferenceError",
                Some(here),
            ));
        }
        if !self.scope.locals.contains(&token.value) {
            return Err(unsupported(
                &format!("function value {}", token.value),
                "functions are only portable when called",
                Some(here),
            ));
        }
        Ok(node(
            SNode::Name {
                path: vec![token.value.clone()],
            },
            here,
        ))
    }
}