ecma-parse-cat 0.3.0

ECMAScript parser consuming ecma-lex-cat tokens and producing ecma-syntax-cat Program ASTs. Comprehensive ES2024 surface (classes, modules, destructuring, async/await, optional chaining, templates). v0.3.0 clears three async-syntax workarounds: (1) `obj.catch` and other reserved-word `IdentifierName`s now parse in member-access position via a new `expect_member_name` that covers all reserved words; (2) async arrow forms `async (a, b) => body`, `async x => body`, and `async () => body` parse via cover-grammar refinement on `async(args)` call expressions; (3) top-level `async function foo() { ... }` declarations dispatch to a new `parse_function_declaration_with_async` helper.
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
//! Token cursor helpers.
//!
//! Functions in this module never own state: they take a slice and a
//! position and return either a new position or a value.  Threading the
//! cursor functionally keeps the parser purely composable.

use crate::error::Error;
use ecma_lex_cat::token::{Token, TokenKind};
use ecma_syntax_cat::identifier::{Identifier, PrivateIdentifier};
use ecma_syntax_cat::span::{Position, Span};

/// Look-ahead result.
pub enum Peek<'a> {
    /// EOF (no more tokens) or only the synthetic `Eof` marker remains.
    Eof,
    /// A real token.
    Token(&'a Token),
}

/// Peek at the token at `pos`, treating the synthetic `Eof` marker the
/// same as actually running out of tokens.
#[must_use]
pub fn peek(tokens: &[Token], pos: usize) -> Peek<'_> {
    tokens
        .get(pos)
        .filter(|t| !matches!(t.value(), TokenKind::Eof))
        .map_or(Peek::Eof, Peek::Token)
}

/// Span of the token at `pos`, or a synthetic zero-width span at EOF.
#[must_use]
pub fn span_at(tokens: &[Token], pos: usize) -> Span {
    tokens.get(pos).map_or(Span::synthetic(), Token::span)
}

/// Span of the previous token (used for end-of-input error positions).
#[must_use]
#[allow(dead_code)]
pub fn span_before(tokens: &[Token], pos: usize) -> Span {
    pos.checked_sub(1).and_then(|prev| tokens.get(prev)).map_or(
        Span::new(Position::synthetic(), Position::synthetic()),
        Token::span,
    )
}

/// If the token at `pos` matches `expected`, return the position after it.
/// Otherwise return [`Error::UnexpectedToken`] or [`Error::UnexpectedEof`].
///
/// `expected` must be a unit-data variant (the comparison uses
/// `TokenKind`'s `PartialEq`).  For variants carrying data (identifier,
/// number, etc.) use the dedicated `expect_*` helpers.
///
/// # Errors
///
/// See variant descriptions above.
pub fn expect_kind(
    tokens: &[Token],
    pos: usize,
    expected: &TokenKind,
    name: &'static str,
) -> Result<usize, Error> {
    match peek(tokens, pos) {
        Peek::Eof => Err(Error::UnexpectedEof { expected: name }),
        Peek::Token(tok) => {
            if tok.value() == expected {
                Ok(pos + 1)
            } else {
                Err(Error::UnexpectedToken {
                    at: tok.span(),
                    expected: name,
                    found: format!("{}", tok.value()),
                })
            }
        }
    }
}

/// Whether the token at `pos` is `kind`.  Returns `false` at EOF.
#[must_use]
pub fn is_kind(tokens: &[Token], pos: usize, kind: &TokenKind) -> bool {
    matches!(peek(tokens, pos), Peek::Token(tok) if tok.value() == kind)
}

/// Consume the token at `pos` if it equals `kind`, returning `Some(new_pos)`.
/// Returns `None` if it does not match or at EOF.
#[must_use]
#[allow(dead_code)]
pub fn consume_if(tokens: &[Token], pos: usize, kind: &TokenKind) -> Option<usize> {
    is_kind(tokens, pos, kind).then_some(pos + 1)
}

/// Eat a semicolon, with limited automatic-semicolon-insertion: a `;` is
/// optional immediately before `}` or EOF; required everywhere else.
///
/// # Errors
///
/// [`Error::UnexpectedToken`] when a `;` is required but not present.
pub fn eat_semicolon(tokens: &[Token], pos: usize) -> Result<usize, Error> {
    match peek(tokens, pos) {
        Peek::Eof => Ok(pos),
        Peek::Token(tok) => match tok.value() {
            TokenKind::Semicolon => Ok(pos + 1),
            TokenKind::RBrace => Ok(pos),
            _other => Err(Error::UnexpectedToken {
                at: tok.span(),
                expected: "`;` or `}`",
                found: format!("{}", tok.value()),
            }),
        },
    }
}

/// Expect an identifier token; return the constructed `Identifier` and the
/// position after it.
///
/// # Errors
///
/// [`Error::UnexpectedToken`] if the next token is not an identifier.
/// [`Error::Syntax`] if the identifier text fails validation.
pub fn expect_identifier(tokens: &[Token], pos: usize) -> Result<(Identifier, usize), Error> {
    match peek(tokens, pos) {
        Peek::Eof => Err(Error::UnexpectedEof {
            expected: "identifier",
        }),
        Peek::Token(tok) => match tok.value() {
            TokenKind::Identifier(name) => {
                let id = Identifier::new(name.clone())?;
                Ok((id, pos + 1))
            }
            _other => Err(Error::UnexpectedToken {
                at: tok.span(),
                expected: "identifier",
                found: format!("{}", tok.value()),
            }),
        },
    }
}

/// Expect an identifier OR a contextual-keyword that may be used as an
/// identifier in this position (e.g. `let` as a variable name in sloppy
/// mode, `await` outside async functions).  For v0, accepts any identifier
/// or `let`/`async`/`get`/`set`/`of`/`as`/`from`/`static`/`yield`/`await`.
///
/// # Errors
///
/// Same as [`expect_identifier`].
pub fn expect_identifier_or_keyword(
    tokens: &[Token],
    pos: usize,
) -> Result<(Identifier, usize), Error> {
    match peek(tokens, pos) {
        Peek::Eof => Err(Error::UnexpectedEof {
            expected: "identifier",
        }),
        Peek::Token(tok) => {
            let text_option = identifier_text(tok.value());
            match text_option {
                Some(text) => {
                    let id = Identifier::new(text)?;
                    Ok((id, pos + 1))
                }
                None => Err(Error::UnexpectedToken {
                    at: tok.span(),
                    expected: "identifier",
                    found: format!("{}", tok.value()),
                }),
            }
        }
    }
}

/// Accept any `IdentifierName` per ECMA-262 §13.3.2 -- regular
/// identifiers, contextual keywords (`let`, `await`, `yield`,
/// `static`), AND ordinary reserved words (`catch`, `class`, `if`,
/// `return`, ...).  Used in member-access positions like
/// `obj.catch` / `p.then` / `obj.if` where reserved words are
/// permitted by the grammar.
///
/// # Errors
///
/// Returns [`Error::UnexpectedToken`] when the token isn't a
/// keyword or identifier (e.g. `.42` or `."str"`).
pub fn expect_member_name(tokens: &[Token], pos: usize) -> Result<(Identifier, usize), Error> {
    match peek(tokens, pos) {
        Peek::Eof => Err(Error::UnexpectedEof {
            expected: "member name",
        }),
        Peek::Token(tok) => match member_name_text(tok.value()) {
            Some(text) => Ok((Identifier::new(text)?, pos + 1)),
            None => Err(Error::UnexpectedToken {
                at: tok.span(),
                expected: "member name",
                found: format!("{}", tok.value()),
            }),
        },
    }
}

fn member_name_text(kind: &TokenKind) -> Option<String> {
    identifier_text(kind).or_else(|| reserved_keyword_text(kind))
}

#[allow(clippy::too_many_lines)] // exhaustive enumeration over the reserved-word TokenKind variants
fn reserved_keyword_text(kind: &TokenKind) -> Option<String> {
    match kind {
        TokenKind::KwBreak => Some("break".to_owned()),
        TokenKind::KwCase => Some("case".to_owned()),
        TokenKind::KwCatch => Some("catch".to_owned()),
        TokenKind::KwClass => Some("class".to_owned()),
        TokenKind::KwConst => Some("const".to_owned()),
        TokenKind::KwContinue => Some("continue".to_owned()),
        TokenKind::KwDebugger => Some("debugger".to_owned()),
        TokenKind::KwDefault => Some("default".to_owned()),
        TokenKind::KwDelete => Some("delete".to_owned()),
        TokenKind::KwDo => Some("do".to_owned()),
        TokenKind::KwElse => Some("else".to_owned()),
        TokenKind::KwEnum => Some("enum".to_owned()),
        TokenKind::KwExport => Some("export".to_owned()),
        TokenKind::KwExtends => Some("extends".to_owned()),
        TokenKind::KwFalse => Some("false".to_owned()),
        TokenKind::KwFinally => Some("finally".to_owned()),
        TokenKind::KwFor => Some("for".to_owned()),
        TokenKind::KwFunction => Some("function".to_owned()),
        TokenKind::KwIf => Some("if".to_owned()),
        TokenKind::KwImport => Some("import".to_owned()),
        TokenKind::KwIn => Some("in".to_owned()),
        TokenKind::KwInstanceof => Some("instanceof".to_owned()),
        TokenKind::KwNew => Some("new".to_owned()),
        TokenKind::KwNull => Some("null".to_owned()),
        TokenKind::KwReturn => Some("return".to_owned()),
        TokenKind::KwSuper => Some("super".to_owned()),
        TokenKind::KwSwitch => Some("switch".to_owned()),
        TokenKind::KwThis => Some("this".to_owned()),
        TokenKind::KwThrow => Some("throw".to_owned()),
        TokenKind::KwTrue => Some("true".to_owned()),
        TokenKind::KwTry => Some("try".to_owned()),
        TokenKind::KwTypeof => Some("typeof".to_owned()),
        TokenKind::KwVar => Some("var".to_owned()),
        TokenKind::KwVoid => Some("void".to_owned()),
        TokenKind::KwWhile => Some("while".to_owned()),
        TokenKind::KwWith => Some("with".to_owned()),
        TokenKind::KwImplements => Some("implements".to_owned()),
        TokenKind::KwInterface => Some("interface".to_owned()),
        TokenKind::KwPackage => Some("package".to_owned()),
        TokenKind::KwPrivate => Some("private".to_owned()),
        TokenKind::KwProtected => Some("protected".to_owned()),
        TokenKind::KwPublic => Some("public".to_owned()),
        // Everything `identifier_text` already handles (and a
        // handful of leftover token shapes) returns None here so
        // `member_name_text`'s `.or_else` only ever combines the
        // contextual-keyword and reserved-keyword strings.
        TokenKind::Identifier(_)
        | TokenKind::KwLet
        | TokenKind::KwAwait
        | TokenKind::KwYield
        | TokenKind::KwStatic
        | TokenKind::PrivateIdentifier(_)
        | TokenKind::Number(_)
        | TokenKind::BigInt(_)
        | TokenKind::String(_)
        | TokenKind::RegExp { .. }
        | TokenKind::TemplateNoSubstitution(_)
        | TokenKind::TemplateHead(_)
        | TokenKind::TemplateMiddle(_)
        | TokenKind::TemplateTail(_)
        | TokenKind::LParen
        | TokenKind::RParen
        | TokenKind::LBracket
        | TokenKind::RBracket
        | TokenKind::LBrace
        | TokenKind::RBrace
        | TokenKind::Comma
        | TokenKind::Semicolon
        | TokenKind::Colon
        | TokenKind::Dot
        | TokenKind::OptionalChain
        | TokenKind::Spread
        | TokenKind::Arrow
        | TokenKind::Question
        | TokenKind::EqEq
        | TokenKind::EqEqEq
        | TokenKind::BangEq
        | TokenKind::BangEqEq
        | TokenKind::Lt
        | TokenKind::LtEq
        | TokenKind::Gt
        | TokenKind::GtEq
        | TokenKind::Plus
        | TokenKind::Minus
        | TokenKind::Star
        | TokenKind::Slash
        | TokenKind::Percent
        | TokenKind::StarStar
        | TokenKind::PlusPlus
        | TokenKind::MinusMinus
        | TokenKind::Amp
        | TokenKind::Pipe
        | TokenKind::Caret
        | TokenKind::Tilde
        | TokenKind::LtLt
        | TokenKind::GtGt
        | TokenKind::GtGtGt
        | TokenKind::AmpAmp
        | TokenKind::PipePipe
        | TokenKind::QQ
        | TokenKind::Bang
        | TokenKind::Eq
        | TokenKind::PlusEq
        | TokenKind::MinusEq
        | TokenKind::StarEq
        | TokenKind::SlashEq
        | TokenKind::PercentEq
        | TokenKind::StarStarEq
        | TokenKind::LtLtEq
        | TokenKind::GtGtEq
        | TokenKind::GtGtGtEq
        | TokenKind::AmpEq
        | TokenKind::PipeEq
        | TokenKind::CaretEq
        | TokenKind::AmpAmpEq
        | TokenKind::PipePipeEq
        | TokenKind::QQEq
        | TokenKind::Eof => None,
    }
}

#[allow(clippy::too_many_lines)] // exhaustive enumeration over the ~110 TokenKind variants
#[allow(clippy::match_same_arms)] // all non-identifier arms intentionally return None, grouped by category for documentation
fn identifier_text(kind: &TokenKind) -> Option<String> {
    match kind {
        TokenKind::Identifier(name) => Some(name.clone()),
        TokenKind::KwLet => Some("let".to_owned()),
        TokenKind::KwAwait => Some("await".to_owned()),
        TokenKind::KwYield => Some("yield".to_owned()),
        TokenKind::KwStatic => Some("static".to_owned()),
        TokenKind::KwImplements
        | TokenKind::KwInterface
        | TokenKind::KwPackage
        | TokenKind::KwPrivate
        | TokenKind::KwProtected
        | TokenKind::KwPublic => None,
        TokenKind::KwBreak
        | TokenKind::KwCase
        | TokenKind::KwCatch
        | TokenKind::KwClass
        | TokenKind::KwConst
        | TokenKind::KwContinue
        | TokenKind::KwDebugger
        | TokenKind::KwDefault
        | TokenKind::KwDelete
        | TokenKind::KwDo
        | TokenKind::KwElse
        | TokenKind::KwEnum
        | TokenKind::KwExport
        | TokenKind::KwExtends
        | TokenKind::KwFalse
        | TokenKind::KwFinally
        | TokenKind::KwFor
        | TokenKind::KwFunction
        | TokenKind::KwIf
        | TokenKind::KwImport
        | TokenKind::KwIn
        | TokenKind::KwInstanceof
        | TokenKind::KwNew
        | TokenKind::KwNull
        | TokenKind::KwReturn
        | TokenKind::KwSuper
        | TokenKind::KwSwitch
        | TokenKind::KwThis
        | TokenKind::KwThrow
        | TokenKind::KwTrue
        | TokenKind::KwTry
        | TokenKind::KwTypeof
        | TokenKind::KwVar
        | TokenKind::KwVoid
        | TokenKind::KwWhile
        | TokenKind::KwWith => None,
        TokenKind::PrivateIdentifier(_)
        | TokenKind::Number(_)
        | TokenKind::BigInt(_)
        | TokenKind::String(_)
        | TokenKind::RegExp { .. }
        | TokenKind::TemplateNoSubstitution(_)
        | TokenKind::TemplateHead(_)
        | TokenKind::TemplateMiddle(_)
        | TokenKind::TemplateTail(_)
        | TokenKind::LParen
        | TokenKind::RParen
        | TokenKind::LBracket
        | TokenKind::RBracket
        | TokenKind::LBrace
        | TokenKind::RBrace
        | TokenKind::Comma
        | TokenKind::Semicolon
        | TokenKind::Colon
        | TokenKind::Dot
        | TokenKind::OptionalChain
        | TokenKind::Spread
        | TokenKind::Arrow
        | TokenKind::Question
        | TokenKind::EqEq
        | TokenKind::EqEqEq
        | TokenKind::BangEq
        | TokenKind::BangEqEq
        | TokenKind::Lt
        | TokenKind::LtEq
        | TokenKind::Gt
        | TokenKind::GtEq
        | TokenKind::Plus
        | TokenKind::Minus
        | TokenKind::Star
        | TokenKind::Slash
        | TokenKind::Percent
        | TokenKind::StarStar
        | TokenKind::PlusPlus
        | TokenKind::MinusMinus
        | TokenKind::Amp
        | TokenKind::Pipe
        | TokenKind::Caret
        | TokenKind::Tilde
        | TokenKind::LtLt
        | TokenKind::GtGt
        | TokenKind::GtGtGt
        | TokenKind::AmpAmp
        | TokenKind::PipePipe
        | TokenKind::QQ
        | TokenKind::Bang
        | TokenKind::Eq
        | TokenKind::PlusEq
        | TokenKind::MinusEq
        | TokenKind::StarEq
        | TokenKind::SlashEq
        | TokenKind::PercentEq
        | TokenKind::StarStarEq
        | TokenKind::LtLtEq
        | TokenKind::GtGtEq
        | TokenKind::GtGtGtEq
        | TokenKind::AmpEq
        | TokenKind::PipeEq
        | TokenKind::CaretEq
        | TokenKind::AmpAmpEq
        | TokenKind::PipePipeEq
        | TokenKind::QQEq
        | TokenKind::Eof => None,
    }
}

/// Expect a private identifier (`#name`).
///
/// # Errors
///
/// [`Error::UnexpectedToken`] if the next token is not a private identifier.
pub fn expect_private_identifier(
    tokens: &[Token],
    pos: usize,
) -> Result<(PrivateIdentifier, usize), Error> {
    match peek(tokens, pos) {
        Peek::Eof => Err(Error::UnexpectedEof {
            expected: "private identifier",
        }),
        Peek::Token(tok) => match tok.value() {
            TokenKind::PrivateIdentifier(name) => {
                let id = PrivateIdentifier::new(name.clone())?;
                Ok((id, pos + 1))
            }
            _other => Err(Error::UnexpectedToken {
                at: tok.span(),
                expected: "private identifier",
                found: format!("{}", tok.value()),
            }),
        },
    }
}