parsec_js/lib.rs
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 James Conway
3
4//! JavaScript for Parsec: a parser for ECMAScript 2025 scripts, with
5//! Annex B's additions, and modules, and the lexer under it.
6//!
7//! It's a parser, not an engine: nothing here runs code. [`parse`] gives
8//! an [`Ast`], every node in one arena with a [`Span`] beside it and the
9//! comments kept on the side, or the first syntax error as a
10//! [`Diagnostic`]. [`Ast::to_estree`] writes the tree as ESTree JSON in
11//! the shape acorn gives, so tools written for acorn or espree can read
12//! it.
13//!
14//! ```
15//! use parsec_js::{Node, parse_script};
16//!
17//! let src = "const total = items.reduce((sum, x) => sum + x, 0);";
18//! let ast = parse_script(src).unwrap();
19//! let Node::Program { body, .. } = *ast.node(ast.root()) else { unreachable!() };
20//! let first = ast.get(body)[0];
21//! assert_eq!(ast.span(first).text(src), src);
22//! assert!(ast.to_estree(src).contains(r#""type":"ArrowFunctionExpression""#));
23//! ```
24//!
25//! [`scope`] works out which declaration each name refers to: the
26//! scopes, their bindings, every reference resolved or left free, and
27//! the names declared twice where the spec forbids it.
28//!
29//! [`regexp`] checks a regular expression's pattern by its own grammar,
30//! the error a `RegExp` built from it would throw.
31//!
32//! [`early_errors`] gives the early errors the parser leaves for a later
33//! pass: those two, and an export of a name a module never declares.
34//!
35//! Every error is a [`Diagnostic`] with a stable code, listed with the
36//! rule it breaks in [`diagnostics`], and labels on the other places it
37//! involves. [`parse_recovering`] goes on after an error to find the
38//! rest, and [`Diagnostic::render`] prints one as a code frame.
39//!
40//! [`Ast::reparse`] brings a tree up to date after an edit by parsing
41//! only the function body or statement around it, which on a large file
42//! is a small part of a parse; the tree is the one [`parse`] would give.
43//!
44//! [`module`] has a module's import and export records, the spec's
45//! ImportEntry and ExportEntry, for building a module graph.
46//!
47//! [`data`] reads the state pages keep in scripts, `window.__NUXT__ = …`
48//! and its kin, as JSON, without running anything.
49//!
50//! Recursion is bounded by [`Limits::max_depth`] ([`DEFAULT_MAX_DEPTH`]
51//! without one) and work by [`Limits::max_steps`], so hostile input gets
52//! an error, never a stack overflow.
53//!
54//! The lexer works alone too. Every token carries its [`Span`] in the
55//! source, and nothing is allocated per token; what a token says (a
56//! string's value, a number's) is in its text, and [`text`] reads it.
57//! `/` means division after an expression and starts a regular expression
58//! anywhere else, and `}` either closes a block or carries on a template.
59//! The lexer doesn't guess which: [`Lexer::next_token`] takes a [`Goal`],
60//! as the spec's InputElementDiv and InputElementRegExp goals do, and the
61//! parser, which knows, passes it.
62//!
63//! ```
64//! use parsec_js::{Goal, Kind, Lexer, SourceType};
65//!
66//! let src = "#!/usr/bin/env node\nlet big = 1_000n; // a BigInt\n";
67//! let mut lexer = Lexer::new(src, SourceType::Script);
68//! let mut words = Vec::new();
69//! loop {
70//! let token = lexer.next_token(Goal::RegExp);
71//! if token.kind == Kind::Eof {
72//! break;
73//! }
74//! if !token.is_trivia() {
75//! words.push(token.text(src));
76//! }
77//! }
78//! assert_eq!(words, ["let", "big", "=", "1_000n", ";"]);
79//! assert!(lexer.diagnostics().is_empty());
80//! ```
81
82pub mod ast;
83pub mod data;
84pub mod diagnostics;
85pub mod early;
86mod estree;
87mod lexer;
88pub mod module;
89mod parser;
90pub mod regexp;
91mod reparse;
92pub mod scope;
93pub mod text;
94mod token;
95mod unicode;
96
97pub use ast::{Ast, Comment, CommentKind, List, Node, NodeId};
98pub use early::{early_errors, early_errors_in};
99pub use estree::{EstreeOptions, Offsets};
100pub use lexer::{Goal, Lexer, SourceType};
101pub use parsec_base::{Diagnostic, Label, Limits, LineIndex, RenderOptions, Severity, Span};
102pub use parser::{DEFAULT_MAX_DEPTH, Options, parse, parse_recovering, parse_script};
103pub use reparse::{Edit, Reparsed};
104pub use token::{Flags, Keyword, Kind, Punct, Token};