swc_ecma_parser 0.61.3

Feature-complete es2019 parser.

EcmaScript/TypeScript parser for the rust programming language.


Heavily tested

Passes almost all tests from tc39/test262.

Error reporting

error: 'implements', 'interface', 'let', 'package', 'private', 'protected',  'public', 'static', or 'yield' cannot be used as an identifier in strict mode
--> invalid.js:3:10
3 | function yield() {
|          ^^^^^

Error recovery

The parser can recover from some parsing errors. For example, parser returns Ok(Module) for the code below, while emitting error to handler.

const CONST = 9000 % 2;
const enum D {
// Comma is required, but parser can recover because of the newline.
d = 10

Example (lexer)

See lexer.rs in examples directory.

Example (parser)

extern crate swc_common;
extern crate swc_ecma_parser;
use swc_common::sync::Lrc;
use swc_common::{
errors::{ColorConfig, Handler},
FileName, FilePathMapping, SourceMap,
use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax};

fn main() {
let cm: Lrc<SourceMap> = Default::default();
let handler =
Handler::with_tty_emitter(ColorConfig::Auto, true, false,

// Real usage
// let fm = cm
//     .load_file(Path::new("test.js"))
//     .expect("failed to load test.js");
let fm = cm.new_source_file(
"function foo() {}".into(),
let lexer = Lexer::new(
// We want to parse ecmascript
// JscTarget defaults to es5

let mut parser = Parser::new_from(lexer);

for e in parser.take_errors() {

let _module = parser
.map_err(|mut e| {
// Unrecoverable fatal error occurred
.expect("failed to parser module");

Known issues

Null character after \

Becuase [String] of rust should only contain valid utf-8 characters while javascript allows non-utf8 chraceters, the parser stores invalid utf8 chracters in escpaed form.

As a result, swc needs a way to distinguish invalid-utf8 code points and input specified by the user. The parser stores a null chracter right after \\ for non-utf8 code points. Note that other parts of swc is aware of this fact.

Note that this can be changed at anytime with a breaking change.