es2019 parser
Features
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 erros. For example, parser returns
Ok(Module)
for the code below, while emitting error to handler.
const CONST = 9000 % 2;
const enum D {
// Comma is requied, but parser can recover because of the newline.
d = 10
g = CONST
}
Example (lexer)
See lexer.rs
in examples directory.
Example (parser)
#[macro_use]
extern crate swc_common;
extern crate swc_ecma_parser;
use std::sync::Arc;
use swc_common::{
errors::{ColorConfig, Handler},
FileName, FilePathMapping, SourceMap,
};
use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax};
fn main() {
swc_common::GLOBALS.set(&swc_common::Globals::new(), || {
let cm: Arc<SourceMap> = Default::default();
let handler =
Handler::with_tty_emitter(ColorConfig::Auto, true, false,
Some(cm.clone()));
let fm = cm.new_source_file(
FileName::Custom("test.js".into()),
"function foo() {}".into(),
);
let lexer = Lexer::new(
Syntax::Es(Default::default()),
Default::default(),
StringInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
for e in parser.take_errors() {
e.into_diagnostic(&handler).emit();
}
let _module = parser
.parse_module()
.map_err(|mut e| {
e.into_diagnostic(&handler).emit()
})
.expect("failed to parser module");
});
}