Features
- Full support for the latest stable ECMAScript syntax
- TypeScript support (including TSX)
- JSX and TSX parsing
- Stage 3 Decorators
- Recoverable error handling
- Memory-efficient arena allocation
Installation
Add to yourCargo.toml:
Basic Usage
The parser has a minimal API with three inputs and one return value:Core Types
Parser
Main parser struct. Create with
Parser::new() and call .parse() to get results.Memory arena for allocating AST nodes. Use
Allocator::default().Source code to parse
Language and module type (JavaScript/TypeScript, Script/Module, JSX support)
ParserReturn
Return value from
Parser::parse() containing the AST and diagnostics.The parsed AST. Always present, even if there are errors (will be empty on panic).
Syntax errors encountered during parsing. Empty if parsing succeeded.
Module import/export information. See ECMAScript Module Records.
Lexed tokens in source order. Only populated when tokens are enabled in
ParserConfig.Whether the parser terminated early due to an unrecoverable error.
ParseOptions
Configure parser behavior:Whether to parse regular expression literals. Requires the
regular_expression feature.Allow
return statements at the top level.Emit
ParenthesizedExpression nodes in the AST for parenthesized expressions.Allow V8 runtime calls like
%DebugPrint() in the AST.Examples
Parsing TypeScript
Parsing JSX/TSX
Parsing a Single Expression
Handling Errors
With Regular Expression Parsing
Source Types
TheSourceType determines how code is parsed:
Performance
The parser is optimized for speed:- Arena allocation: All AST nodes are allocated in a bump allocator for fast allocation and deallocation
- Compact spans: Uses
u32offsets instead ofusize(max file size: 4 GiB) - Deferred checks: Complex syntax errors and scope resolution are handled by the semantic analyzer
- Zero-copy strings: String literals reference the source text directly
Due to
u32 span offsets, the parser cannot handle files larger than 4 GiB. This is not a practical limitation for JavaScript/TypeScript files.AST Structure
The parsed AST follows the ESTree specification with TypeScript extensions. All AST node types are defined in theoxc_ast crate.
Visiting the AST
See theoxc_ast_visit crate for AST traversal:
Feature Flags
Enable regular expression parsing. Adds
oxc_regular_expression dependency.Expose internal lexer for benchmarking purposes.
Related
- oxc_semantic - Semantic analysis after parsing
- oxc_ast - AST node definitions
- Core Concepts: Parsing - Architecture overview