parsec-js 0.3.0

JavaScript for Parsec: a parser for ECMAScript 2025 scripts and modules to a syntax tree with a span on every node, ESTree JSON out, and the lexer under it.
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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 James Conway

//! Functions, arrows, methods and classes (ECMA-262 clause 15).

use parsec_base::{Diagnostic, Span};

use super::{ClassScope, Ctx, Parser, Private, R};
use crate::ast::{Class, Function, FunctionKind, List, LiteralKind, MethodKind, Node, NodeId};
use crate::text;
use crate::token::{Keyword, Kind, Punct, Token};

/// What a method is, for its parameters and `super`.
#[derive(Clone, Copy, PartialEq, Eq)]
pub(super) enum MethodType {
    Method,
    Get,
    Set,
    Constructor,
}

impl Parser<'_> {
    /// The context inside a new non-arrow function.
    fn function_ctx(&self, generator: bool, is_async: bool) -> Ctx {
        Ctx {
            strict: self.ctx.strict,
            yield_: generator,
            await_: is_async,
            ret: true,
            new_target: true,
            labels: self.labels.len(),
            ..Ctx::default()
        }
    }

    /// A function declaration or expression, from `function`; `start` is
    /// at `async` if it's async. A declaration's name is optional only in
    /// `export default`.
    pub(super) fn function(
        &mut self,
        start: u32,
        is_async: bool,
        kind: FunctionKind,
        optional_name: bool,
    ) -> R<NodeId> {
        self.enter()?;
        self.expect_kw(Keyword::Function)?;
        let generator = self.eat(Punct::Star)?;
        let outer = self.ctx;
        let mut name = None;
        let id = if self.at(Punct::LParen) {
            if kind == FunctionKind::Declaration && !optional_name {
                return self.unexpected();
            }
            None
        } else {
            name = Some(self.tok);
            // An expression's name is bound inside it, a declaration's
            // outside.
            if kind == FunctionKind::Expression {
                self.ctx.yield_ = generator;
                self.ctx.await_ = is_async;
                self.ctx.static_block = false;
            }
            let id = self.identifier(true);
            self.ctx = outer;
            Some(id?)
        };
        self.ctx = self.function_ctx(generator, is_async);
        let params_start = self.tok.span.start;
        let (params, simple) = self.formal_params()?;
        self.check_params_operators(params_start, generator, is_async)?;
        let body = self.function_body(params, simple, false, name)?;
        self.ctx = outer;
        let f = Function {
            kind,
            id,
            params,
            body,
            is_async,
            generator,
            expression: false,
        };
        self.leave();
        Ok(self.finish(start, Node::Function(f)))
    }

    /// A generator's parameters can't hold `yield`, nor an async
    /// function's `await`.
    fn check_params_operators(&mut self, start: u32, generator: bool, is_async: bool) -> R<()> {
        if generator && self.last_yield.is_some_and(|at| at >= start) {
            let span = Span::at(self.last_yield.unwrap_or(start));
            return self.err(
                "yield-in-params",
                span,
                "a generator's parameters can't use `yield`",
            );
        }
        if is_async && self.last_await.is_some_and(|at| at >= start) {
            let span = Span::at(self.last_await.unwrap_or(start));
            return self.err(
                "await-in-params",
                span,
                "an async function's parameters can't use `await`",
            );
        }
        Ok(())
    }

    /// `( FormalParameters )`, and whether they're all plain identifiers.
    pub(super) fn formal_params(&mut self) -> R<(List, bool)> {
        let open = self.expect(Punct::LParen)?;
        let mark = self.scratch.len();
        let mut simple = true;
        while !self.at(Punct::RParen) {
            if self.at(Punct::Ellipsis) {
                let start = self.tok.span.start;
                self.bump()?;
                let argument = self.binding_target()?;
                let rest = self.finish(start, Node::Rest { argument });
                self.scratch.push(rest);
                simple = false;
                break;
            }
            let param = self.binding_element()?;
            simple &= matches!(self.node(param), Node::Identifier);
            self.scratch.push(param);
            if !self.at(Punct::RParen) {
                self.expect_in(Punct::Comma, open)?;
            }
        }
        self.expect_in(Punct::RParen, open)?;
        Ok((self.take_list(mark), simple))
    }

    /// `{ FunctionBody }`, checking the parameters against what the body
    /// turned out to be: strict, or not.
    pub(super) fn function_body(
        &mut self,
        params: List,
        simple: bool,
        unique: bool,
        name: Option<Token>,
    ) -> R<NodeId> {
        let start = self.tok.span.start;
        let open = self.expect(Punct::LBrace)?;
        let was_strict = self.ctx.strict;
        let mark = self.scratch.len();
        let use_strict = self.statement_list(true)?;
        if let Some(directive) = use_strict.filter(|_| !simple) {
            return self.fail(
                Diagnostic::error(
                    "use-strict-with-complex-params",
                    Span::at(start),
                    "a function with defaults, patterns or a rest parameter can't say \"use strict\"",
                )
                .with_label(directive, "it says so here"),
            );
        }
        self.expect_in(Punct::RBrace, open)?;
        let body = self.take_list(mark);
        let block = self.finish(start, Node::Block { body });
        self.check_params(params, simple, unique, was_strict, name)?;
        Ok(block)
    }

    /// The early errors on parameters: duplicates where they're not
    /// allowed, and in a body that became strict, names strict code
    /// reserves.
    pub(super) fn check_params(
        &mut self,
        params: List,
        simple: bool,
        unique: bool,
        was_strict: bool,
        name: Option<Token>,
    ) -> R<()> {
        let mut names = std::mem::take(&mut self.names);
        names.clear();
        for &p in self.ast.get(params) {
            self.bound_names(p, &mut names);
        }
        if self.ctx.strict && !was_strict {
            let function_name = name.map(|t| t.span);
            for &span in names.iter().chain(function_name.iter()) {
                let n = self.name(span);
                let reserved = n == "eval"
                    || n == "arguments"
                    || Keyword::from_str(&n)
                        .is_some_and(|k| k.is_strict_reserved() || k == Keyword::Yield);
                if reserved {
                    let n = n.into_owned();
                    return self.err(
                        "strict-binding",
                        span,
                        format!("strict code can't bind `{n}`"),
                    );
                }
            }
        }
        if self.ctx.strict || !simple || unique {
            return self.check_duplicate_names(names);
        }
        self.names = names;
        Ok(())
    }

    /// No name is bound twice by `patterns`.
    pub(super) fn check_duplicates(&mut self, patterns: &[NodeId]) -> R<()> {
        let mut names = std::mem::take(&mut self.names);
        names.clear();
        for &p in patterns {
            self.bound_names(p, &mut names);
        }
        self.check_duplicate_names(names)
    }

    /// No name is in `spans` twice. It hands the buffer back for reuse.
    pub(super) fn check_duplicate_names(&mut self, spans: Vec<Span>) -> R<()> {
        let mut duplicate = None;
        if spans.len() >= 2 {
            let mut names = std::mem::take(&mut self.sorted_names);
            names.clear();
            names.extend(spans.iter().map(|&s| (self.name(s), s)));
            names.sort_unstable();
            if let Some(w) = names.windows(2).find(|w| w[0].0 == w[1].0) {
                duplicate = Some((w[1].0.to_string(), w[1].1, w[0].1));
            }
            names.clear();
            self.sorted_names = names;
        }
        self.names = spans;
        if let Some((name, span, first)) = duplicate {
            return self.fail(
                Diagnostic::error(
                    "duplicate-binding",
                    span,
                    format!("`{name}` is bound twice here"),
                )
                .with_label(first, "bound here first"),
            );
        }
        Ok(())
    }

    /// An arrow's `=>` and body, its parameters converted already.
    pub(super) fn arrow(
        &mut self,
        start: u32,
        params: List,
        simple: bool,
        is_async: bool,
        no_in: bool,
    ) -> R<NodeId> {
        self.expect(Punct::Arrow)?;
        let outer = self.ctx;
        self.ctx = Ctx {
            yield_: false,
            await_: is_async,
            ret: true,
            static_block: false,
            loops: 0,
            breakable: 0,
            labels: self.labels.len(),
            ..outer
        };
        let (body, expression) = if self.at(Punct::LBrace) {
            (self.function_body(params, simple, true, None)?, false)
        } else {
            let body = self.assign(no_in)?;
            self.check_params(params, simple, true, outer.strict, None)?;
            (body, true)
        };
        self.ctx = outer;
        let f = Function {
            kind: FunctionKind::Arrow,
            id: None,
            params,
            body,
            is_async,
            generator: false,
            expression,
        };
        Ok(self.finish(start, Node::Function(f)))
    }

    /// A method's parameters and body, from `(`, as a function expression.
    pub(super) fn method(
        &mut self,
        kind: MethodType,
        is_async: bool,
        generator: bool,
        super_call: bool,
    ) -> R<NodeId> {
        let start = self.tok.span.start;
        let outer = self.ctx;
        self.ctx = Ctx {
            super_prop: true,
            super_call,
            ..self.function_ctx(generator, is_async)
        };
        let (params, simple) = self.formal_params()?;
        let ids = self.ast.get(params);
        let bad = match kind {
            MethodType::Get => !ids.is_empty(),
            MethodType::Set => ids.len() != 1 || matches!(self.node(ids[0]), Node::Rest { .. }),
            _ => false,
        };
        if bad {
            return self.err(
                "accessor-params",
                Span::new(start, self.prev_end),
                if kind == MethodType::Get {
                    "a getter takes no parameters"
                } else {
                    "a setter takes exactly one parameter"
                },
            );
        }
        self.check_params_operators(start, generator, is_async)?;
        let body = self.function_body(params, simple, true, None)?;
        self.ctx = outer;
        let f = Function {
            kind: FunctionKind::Expression,
            id: None,
            params,
            body,
            is_async,
            generator,
            expression: false,
        };
        Ok(self.finish(start, Node::Function(f)))
    }

    /// A property name: an IdentifierName, a string, a number, or
    /// `[expression]`. Whether it's computed.
    pub(super) fn property_name(&mut self) -> R<(NodeId, bool)> {
        let t = self.tok;
        let node = match t.kind {
            Kind::Ident | Kind::Keyword(_) => Node::Identifier,
            Kind::String => Node::Literal(LiteralKind::String),
            Kind::Number => Node::Literal(LiteralKind::Number),
            Kind::BigInt => Node::Literal(LiteralKind::BigInt),
            Kind::Punct(Punct::LBracket) => {
                self.bump()?;
                let key = self.assign(false)?;
                self.expect(Punct::RBracket)?;
                return Ok((key, true));
            }
            _ => return self.unexpected(),
        };
        self.bump()?;
        Ok((self.ast.push(t.span, node), false))
    }

    /// The name a non-computed key says, for `constructor`, `prototype`
    /// and `__proto__`.
    pub(super) fn key_is(&self, key: NodeId, name: &str) -> bool {
        let span = self.span(key);
        match self.node(key) {
            Node::Identifier => self.name_is(span, name),
            Node::Literal(LiteralKind::String) => {
                let body = Span::new(span.start + 1, span.end - 1).text(self.src);
                // Only an escape spells `name` another way, so the first
                // byte that isn't `name`'s has to start one.
                let same = body
                    .bytes()
                    .zip(name.bytes())
                    .take_while(|(a, b)| a == b)
                    .count();
                if same == body.len() {
                    return same == name.len();
                }
                if body.as_bytes()[same] != b'\\' {
                    return false;
                }
                text::string_value(body)
                    .iter()
                    .copied()
                    .eq(name.encode_utf16())
            }
            _ => false,
        }
    }

    /// A class declaration or expression, from `class`.
    pub(super) fn class(
        &mut self,
        start: u32,
        declaration: bool,
        optional_name: bool,
    ) -> R<NodeId> {
        self.enter()?;
        self.bump()?;
        let outer_strict = self.ctx.strict;
        self.ctx.strict = true;
        let id = if self.at_identifier() {
            Some(self.identifier(true)?)
        } else {
            if declaration && !optional_name {
                return self.unexpected();
            }
            None
        };
        let super_class = if self.at_kw(Keyword::Extends) {
            self.bump()?;
            let heritage = self.lhs()?;
            if self.is_arrow(heritage) {
                let span = self.span(heritage);
                return self.err(
                    "arrow-operand",
                    span,
                    "an arrow function needs parentheses here",
                );
            }
            Some(heritage)
        } else {
            None
        };
        let bstart = self.tok.span.start;
        let open = self.expect(Punct::LBrace)?;
        self.classes.push(ClassScope::default());
        let mark = self.scratch.len();
        let mut constructor = None;
        while !self.at(Punct::RBrace) {
            if self.tok.kind == Kind::Eof {
                return self.unclosed(open);
            }
            if self.eat(Punct::Semicolon)? {
                continue;
            }
            let element = self.class_element(super_class.is_some(), &mut constructor)?;
            self.scratch.push(element);
        }
        self.bump()?;
        self.end_class_scope()?;
        let body = self.take_list(mark);
        let body = self.finish(bstart, Node::ClassBody { body });
        self.ctx.strict = outer_strict;
        let c = Class {
            declaration,
            id,
            super_class,
            body,
        };
        self.leave();
        Ok(self.finish(start, Node::Class(c)))
    }

    /// Whether the token after a modifier (`static`, `get`, `async`) says
    /// the modifier is the element's name instead.
    fn modifier_is_name(&self, async_like: bool) -> bool {
        let next = self.peek();
        matches!(
            next.kind,
            Kind::Punct(Punct::LParen | Punct::Eq | Punct::Semicolon | Punct::RBrace | Punct::Colon | Punct::Comma)
                | Kind::Eof
        ) || async_like && next.newline_before()
            // `get *x() {}` is no getter, so `get` ⟨newline⟩ `*x() {}` is
            // a field named `get` and a generator.
            || (self.at_kw(Keyword::Get) || self.at_kw(Keyword::Set)) && next.kind == Kind::Punct(Punct::Star)
    }

    fn class_element(&mut self, derived: bool, constructor: &mut Option<Span>) -> R<NodeId> {
        let start = self.tok.span.start;
        let mut is_static = false;
        if self.at_kw(Keyword::Static) && !self.modifier_is_name(false) {
            self.bump()?;
            if self.at(Punct::LBrace) {
                return self.static_block(start);
            }
            is_static = true;
        }
        let mut is_async = false;
        let mut generator = false;
        let mut kind = MethodType::Method;
        if self.at_kw(Keyword::Async) && !self.modifier_is_name(true) {
            self.bump()?;
            is_async = true;
        }
        if self.eat(Punct::Star)? {
            generator = true;
        }
        if !is_async
            && !generator
            && (self.at_kw(Keyword::Get) || self.at_kw(Keyword::Set))
            && !self.modifier_is_name(false)
        {
            kind = if self.at_kw(Keyword::Get) {
                MethodType::Get
            } else {
                MethodType::Set
            };
            self.bump()?;
        }
        let (key, computed, private) = if self.tok.kind == Kind::PrivateName {
            let t = self.bump()?;
            (self.ast.push(t.span, Node::PrivateIdentifier), false, true)
        } else {
            let (key, computed) = self.property_name()?;
            (key, computed, false)
        };
        let named = |p: &Self, name: &str| !computed && !private && p.key_is(key, name);
        if self.at(Punct::LParen) || is_async || generator || kind != MethodType::Method {
            let is_ctor = !is_static && named(self, "constructor");
            if is_ctor {
                if kind != MethodType::Method || generator || is_async {
                    let span = self.span(key);
                    return self.err(
                        "special-constructor",
                        span,
                        "a constructor can't be a getter, setter, generator or async",
                    );
                }
                let span = self.span(key);
                if let Some(first) = *constructor {
                    return self.fail(
                        Diagnostic::error(
                            "duplicate-constructor",
                            span,
                            "a class can have only one constructor",
                        )
                        .with_label(first, "the first constructor"),
                    );
                }
                *constructor = Some(span);
                kind = MethodType::Constructor;
            }
            if is_static && named(self, "prototype") {
                let span = self.span(key);
                return self.err(
                    "static-prototype",
                    span,
                    "a static member can't be called `prototype`",
                );
            }
            if private {
                let p = match kind {
                    MethodType::Get => Private::Get,
                    MethodType::Set => Private::Set,
                    _ => Private::Method,
                };
                let span = self.span(key);
                self.declare_private(span, p, is_static)?;
            }
            let value = self.method(kind, is_async, generator, is_ctor && derived)?;
            let kind = match kind {
                MethodType::Method => MethodKind::Method,
                MethodType::Get => MethodKind::Get,
                MethodType::Set => MethodKind::Set,
                MethodType::Constructor => MethodKind::Constructor,
            };
            return Ok(self.finish(
                start,
                Node::Method {
                    key,
                    value,
                    kind,
                    computed,
                    is_static,
                },
            ));
        }
        if named(self, "constructor") || is_static && named(self, "prototype") {
            let span = self.span(key);
            return self.err(
                "bad-field-name",
                span,
                "a field can't be called `constructor`, nor a static one `prototype`",
            );
        }
        if private {
            let span = self.span(key);
            self.declare_private(span, Private::Field, is_static)?;
        }
        let value = if self.eat(Punct::Eq)? {
            let outer = self.ctx;
            self.ctx = Ctx {
                yield_: false,
                await_: false,
                ret: false,
                new_target: true,
                super_prop: true,
                super_call: false,
                no_arguments: true,
                static_block: false,
                loops: 0,
                breakable: 0,
                labels: self.labels.len(),
                ..outer
            };
            let value = self.assign(false);
            self.ctx = outer;
            Some(value?)
        } else {
            None
        };
        self.semicolon()?;
        Ok(self.finish(
            start,
            Node::Field {
                key,
                value,
                computed,
                is_static,
            },
        ))
    }

    /// `static { ... }`, from the `{`.
    fn static_block(&mut self, start: u32) -> R<NodeId> {
        let open = self.bump()?;
        let outer = self.ctx;
        self.ctx = Ctx {
            strict: true,
            new_target: true,
            super_prop: true,
            no_arguments: true,
            static_block: true,
            labels: self.labels.len(),
            ..Ctx::default()
        };
        let mark = self.scratch.len();
        let result = self.statement_list(false);
        self.ctx = outer;
        result?;
        self.expect_in(Punct::RBrace, open)?;
        let body = self.take_list(mark);
        Ok(self.finish(start, Node::StaticBlock { body }))
    }
}