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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 James Conway
//! JavaScript for Parsec: a parser for ECMAScript 2025 scripts, with
//! Annex B's additions, and modules, and the lexer under it.
//!
//! It's a parser, not an engine: nothing here runs code. [`parse`] gives
//! an [`Ast`], every node in one arena with a [`Span`] beside it and the
//! comments kept on the side, or the first syntax error as a
//! [`Diagnostic`]. [`Ast::to_estree`] writes the tree as ESTree JSON in
//! the shape acorn gives, so tools written for acorn or espree can read
//! it.
//!
//! ```
//! use parsec_js::{Node, parse_script};
//!
//! let src = "const total = items.reduce((sum, x) => sum + x, 0);";
//! let ast = parse_script(src).unwrap();
//! let Node::Program { body, .. } = *ast.node(ast.root()) else { unreachable!() };
//! let first = ast.get(body)[0];
//! assert_eq!(ast.span(first).text(src), src);
//! assert!(ast.to_estree(src).contains(r#""type":"ArrowFunctionExpression""#));
//! ```
//!
//! [`scope`] works out which declaration each name refers to: the
//! scopes, their bindings, every reference resolved or left free, and
//! the names declared twice where the spec forbids it.
//!
//! [`regexp`] checks a regular expression's pattern by its own grammar,
//! the error a `RegExp` built from it would throw.
//!
//! [`early_errors`] gives the early errors the parser leaves for a later
//! pass: those two, and an export of a name a module never declares.
//!
//! Every error is a [`Diagnostic`] with a stable code, listed with the
//! rule it breaks in [`diagnostics`], and labels on the other places it
//! involves. [`parse_recovering`] goes on after an error to find the
//! rest, and [`Diagnostic::render`] prints one as a code frame.
//!
//! [`Ast::reparse`] brings a tree up to date after an edit by parsing
//! only the function body or statement around it, which on a large file
//! is a small part of a parse; the tree is the one [`parse`] would give.
//!
//! [`module`] has a module's import and export records, the spec's
//! ImportEntry and ExportEntry, for building a module graph.
//!
//! [`data`] reads the state pages keep in scripts, `window.__NUXT__ = …`
//! and its kin, as JSON, without running anything.
//!
//! Recursion is bounded by [`Limits::max_depth`] ([`DEFAULT_MAX_DEPTH`]
//! without one) and work by [`Limits::max_steps`], so hostile input gets
//! an error, never a stack overflow.
//!
//! The lexer works alone too. Every token carries its [`Span`] in the
//! source, and nothing is allocated per token; what a token says (a
//! string's value, a number's) is in its text, and [`text`] reads it.
//! `/` means division after an expression and starts a regular expression
//! anywhere else, and `}` either closes a block or carries on a template.
//! The lexer doesn't guess which: [`Lexer::next_token`] takes a [`Goal`],
//! as the spec's InputElementDiv and InputElementRegExp goals do, and the
//! parser, which knows, passes it.
//!
//! ```
//! use parsec_js::{Goal, Kind, Lexer, SourceType};
//!
//! let src = "#!/usr/bin/env node\nlet big = 1_000n; // a BigInt\n";
//! let mut lexer = Lexer::new(src, SourceType::Script);
//! let mut words = Vec::new();
//! loop {
//! let token = lexer.next_token(Goal::RegExp);
//! if token.kind == Kind::Eof {
//! break;
//! }
//! if !token.is_trivia() {
//! words.push(token.text(src));
//! }
//! }
//! assert_eq!(words, ["let", "big", "=", "1_000n", ";"]);
//! assert!(lexer.diagnostics().is_empty());
//! ```
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;