Skip to main content

Crate js_parse

Crate js_parse 

Source
Expand description

quickjs-parse — ECMAScript 2025 lexer, AST and parser.

Milestone status: the lexer is complete (M1a); the AST and parser are the next step and do not exist yet.

use js_parse::{Lexer, TokenKind};

let mut lexer = Lexer::new("let answer = 0x2a + 1_000;");
let mut kinds = Vec::new();
loop {
    let token = lexer.next_token().expect("valid source");
    let done = matches!(token.kind, TokenKind::Eof);
    kinds.push(token.kind);
    if done {
        break;
    }
}
assert_eq!(kinds.len(), 8);

§Strings are WTF-8

A JS string is a sequence of UTF-16 code units, which may include an unpaired surrogate: "\ud800".length === 1. A Rust String cannot hold that, so the cooked value of every literal is a Wtf8 — see wtf8. This used to be a documented limitation (a lone surrogate became U+FFFD); the type moved down into this crate when the lexer needed it, because the alternative was one representation here and another in the runtime, with a conversion in between where the surrogate would be lost.

Re-exports§

pub use wtf8::Wtf8;
pub use parser::EvalContext;
pub use parser::Parser;
pub use parser::parse_eval_script;
pub use parser::parse_eval_script_in;
pub use parser::parse_expression;
pub use parser::parse_module;
pub use parser::parse_script;
pub use parser::parse_script_strict;
pub use parser::parse_suspensible_global_eval;
pub use parser::parse_suspensible_script;

Modules§

ast
The ECMAScript 2025 abstract syntax tree.
parser
The parser: tokens to crate::ast.
parser_limits
Parser configuration constants and helpers.
wtf8
The string type JavaScript actually has, in WTF-8.

Structs§

BigIntLiteral
A BigInt literal: the source digits and their radix, kept raw so that js-value can build the arbitrary-precision integer.
Lexer
A streaming lexer over one source text.
Position
Byte offset, line (1-based) and column (1-based) of a source position.
Span
Half-open source range.
SyntaxError
A syntax error produced while scanning or parsing.
TemplateToken
A template literal chunk.
Token
A token plus where it came from.

Enums§

Keyword
A reserved word that is a keyword in every context (ES2025 §12.7.2), plus enum (always reserved).
NumberLiteral
The value of a numeric literal.
Punct
Every punctuator in ECMAScript 2025, longest-first where it matters.
TemplateKind
Which part of a template literal a token is.
TokenKind
A lexical token.

Functions§

is_module_source
Whether source should be evaluated as a module, decided the way qjs decides.
normalize_specifier
The name specifier resolves to when imported from base, textually.

Type Aliases§

LexError
Lexical errors share the syntax error type: the engine reports both as SyntaxError.
ParseError
Parser errors share the syntax error type.
Result
Result type used by the lexer and the parser.