Crate oxc_parser
source ·Expand description
Oxc Parser for JavaScript and TypeScript
§Performance
The following optimization techniques are used:
- AST is allocated in a memory arena (bumpalo) for fast AST drop
- Short strings are inlined by CompactString
- No other heap allocations are done except the above two
- oxc_span::Span offsets uses
u32
instead ofusize
- Scope binding, symbol resolution and complicated syntax errors are not done in the parser, they are delegated to the semantic analyzer
§Conformance
The parser parses all of Test262 and most of Babel and TypeScript parser conformance tests.
See oxc coverage for details
Test262 Summary:
AST Parsed : 44000/44000 (100.00%)
Babel Summary:
AST Parsed : 2065/2071 (99.71%)
TypeScript Summary:
AST Parsed : 2337/2337 (100.00%)
§Usage
The parser has a minimal API with three inputs and one return struct (ParserReturn).
let parser_return = Parser::new(&allocator, &source_text, source_type).parse();
§Example
https://github.com/Boshen/oxc/blob/main/crates/oxc_parser/examples/parser.rs
use std::{env, path::Path};
use oxc_allocator::Allocator;
use oxc_parser::Parser;
use oxc_span::SourceType;
// Instruction:
// create a `test.js`,
// run `cargo run -p oxc_parser --example parser`
// or `cargo watch -x "run -p oxc_parser --example parser"`
fn main() -> Result<(), String> {
let name = env::args().nth(1).unwrap_or_else(|| "test.js".to_string());
let path = Path::new(&name);
let source_text = std::fs::read_to_string(path).map_err(|_| format!("Missing '{name}'"))?;
let allocator = Allocator::default();
let source_type = SourceType::from_path(path).unwrap();
let now = std::time::Instant::now();
let ret = Parser::new(&allocator, &source_text, source_type).parse();
let elapsed_time = now.elapsed();
println!("{}ms.", elapsed_time.as_millis());
println!("AST:");
println!("{}", serde_json::to_string_pretty(&ret.program).unwrap());
println!("Comments:");
let comments =
ret.trivias.comments().map(|(_, span)| span.source_text(&source_text)).collect::<Vec<_>>();
println!("{comments:?}");
if ret.errors.is_empty() {
println!("Parsed Successfully.");
} else {
for error in ret.errors {
let error = error.with_source_code(source_text.clone());
println!("{error:?}");
println!("Parsed with Errors.");
}
}
Ok(())
}
§Visitor
See oxc_ast::Visit and oxc_ast::VisitMut
§Visiting without a visitor
For ad-hoc tasks, the semantic analyzer can be used to get a parent pointing tree with untyped nodes, the nodes can be iterated through a sequential loop.
for node in semantic.nodes().iter() {
match node.kind() {
// check node
}
}
Structs§
- Recursive Descent Parser for ECMAScript and TypeScript
- Return value of parser consisting of AST, errors and comments
Enums§
Constants§
- Maximum length of source which can be parsed (in bytes). ~4 GiB on 64-bit systems, ~2 GiB on 32-bit systems.