2018-11-18 08:00:07 +03:00
//! es2019 parser
//!
//! # Features
//!
//! ## Heavily tested
//!
//! Passes almost all tests from [tc39/test262][].
//!
//! ## Error reporting
//!
//! ```sh
//! 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() {
//! | ^^^^^
//! ```
//!
//! # Example
//!
//! ```
//! #[macro_use]
//! extern crate swc_common;
//! extern crate swc_ecma_parser;
//! use swc_common::{
//! errors::{ColorConfig, Handler},
//! sync::Lrc,
//! FileName, FilePathMapping, SourceMap,
//! };
2018-12-30 05:57:27 +03:00
//! use swc_ecma_parser::{Parser, Session, SourceFileInput, Syntax};
2018-11-18 08:00:07 +03:00
//!
//! fn main() {
//! swc_common::GLOBALS.set(&swc_common::Globals::new(), || {
//! let cm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
//! let handler =
//! Handler::with_tty_emitter(ColorConfig::Auto, true, false, Some(cm.clone()));
//!
2019-01-07 13:43:47 +03:00
//! let session = Session { handler: &handler };
2018-11-18 08:00:07 +03:00
//!
//! // Real usage
//! // let fm = cm
//! // .load_file(Path::new("test.js"))
//! // .expect("failed to load test.js");
//!
//! let fm = cm.new_source_file(
//! FileName::Custom("test.js".into()),
//! "function foo() {}".into(),
//! );
//!
2019-01-08 10:34:35 +03:00
//! let mut parser = Parser::new(
//! session,
//! Syntax::Es(Default::default()),
//! SourceFileInput::from(&*fm),
//! );
2018-11-18 08:00:07 +03:00
//!
2019-01-07 13:43:47 +03:00
//! let _module = parser
//! .parse_module()
2019-01-17 17:17:16 +03:00
//! .map_err(|mut e| {
2019-01-07 13:43:47 +03:00
//! e.emit();
//! ()
//! })
//! .expect("failed to parser module");
2018-11-18 08:00:07 +03:00
//! });
//! }
//! ```
//!
//!
//! [tc39/test262]:https://github.com/tc39/test262
2019-01-17 17:17:16 +03:00
#![ cfg_attr(any(test, feature = " fold " ), feature(specialization)) ]
#![ cfg_attr(test, feature(box_syntax)) ]
2018-12-21 10:54:36 +03:00
#![ cfg_attr(test, feature(test)) ]
2018-01-12 10:53:06 +03:00
#![ deny(unreachable_patterns) ]
#![ deny(unsafe_code) ]
extern crate either ;
2018-12-21 10:54:36 +03:00
#[ macro_use ]
extern crate smallvec ;
2018-11-17 10:38:23 +03:00
extern crate swc_ecma_parser_macros as parser_macros ;
2018-01-12 10:53:06 +03:00
#[ macro_use ]
2018-12-21 10:54:36 +03:00
extern crate log ;
2018-01-12 10:53:06 +03:00
#[ macro_use(js_word) ]
2018-11-17 04:02:40 +03:00
extern crate swc_atoms ;
2018-11-17 10:38:23 +03:00
extern crate enum_kind ;
2018-12-30 05:57:27 +03:00
extern crate regex ;
2018-12-30 07:06:13 +03:00
extern crate serde ;
2018-11-17 04:02:40 +03:00
extern crate swc_common ;
2018-12-30 05:57:27 +03:00
#[ macro_use ]
extern crate lazy_static ;
2018-11-17 04:02:40 +03:00
extern crate swc_ecma_ast as ast ;
2018-01-12 10:53:06 +03:00
#[ macro_use ]
2018-12-30 05:57:27 +03:00
#[ cfg(test) ]
2018-01-12 10:53:06 +03:00
extern crate testing ;
2018-12-21 10:54:36 +03:00
#[ cfg(test) ]
2018-12-30 05:57:27 +03:00
extern crate env_logger ;
#[ cfg(test) ]
2018-12-21 10:54:36 +03:00
extern crate test ;
2018-01-12 10:53:06 +03:00
extern crate unicode_xid ;
2018-09-16 14:25:41 +03:00
pub use self ::{
lexer ::input ::{ Input , SourceFileInput } ,
parser ::* ,
} ;
2018-12-30 07:06:13 +03:00
use serde ::{ Deserialize , Serialize } ;
2018-01-22 04:45:08 +03:00
use swc_common ::errors ::Handler ;
2018-01-12 10:53:06 +03:00
#[ macro_use ]
mod macros ;
2018-01-22 04:45:08 +03:00
mod error ;
2018-01-21 11:47:37 +03:00
mod lexer ;
mod parser ;
2018-06-02 12:01:00 +03:00
mod token ;
2018-01-21 11:47:37 +03:00
2019-01-10 12:23:25 +03:00
#[ derive(Clone, Copy, Deserialize, Serialize) ]
2019-01-07 13:43:47 +03:00
#[ serde(tag = " syntax " ) ]
2018-12-30 05:57:27 +03:00
pub enum Syntax {
2019-01-07 13:43:47 +03:00
/// Standard
2019-01-08 10:34:35 +03:00
#[ serde(rename = " ecmascript " ) ]
Es ( EsConfig ) ,
#[ serde(rename = " typescript " ) ]
Typescript ( TsConfig ) ,
2018-12-30 05:57:27 +03:00
}
2018-12-30 07:06:13 +03:00
2019-01-07 13:43:47 +03:00
impl Default for Syntax {
fn default ( ) -> Self {
2019-01-08 10:34:35 +03:00
Syntax ::Es ( Default ::default ( ) )
2018-12-30 07:06:13 +03:00
}
}
2018-12-30 05:57:27 +03:00
impl Syntax {
/// Should we pare jsx?
pub fn jsx ( self ) -> bool {
match self {
2019-01-08 10:34:35 +03:00
Syntax ::Es ( EsConfig { jsx : true , .. } )
| Syntax ::Typescript ( TsConfig { tsx : true , .. } ) = > true ,
2018-12-30 05:57:27 +03:00
_ = > false ,
}
}
2019-01-07 13:43:47 +03:00
2019-02-10 06:07:40 +03:00
pub fn dynamic_import ( self ) -> bool {
match self {
Syntax ::Es ( EsConfig {
dynamic_import : true ,
..
} )
| Syntax ::Typescript ( TsConfig {
dynamic_import : true ,
..
} ) = > true ,
_ = > false ,
}
}
2019-01-07 13:43:47 +03:00
pub fn fn_bind ( self ) -> bool {
match self {
2019-01-08 10:34:35 +03:00
Syntax ::Es ( EsConfig { fn_bind : true , .. } ) = > true ,
2019-01-07 13:43:47 +03:00
_ = > false ,
}
}
pub fn num_sep ( self ) -> bool {
match self {
2019-01-08 10:34:35 +03:00
Syntax ::Es ( EsConfig { num_sep : true , .. } ) = > true ,
2019-01-07 13:43:47 +03:00
_ = > false ,
}
}
pub fn decorators ( self ) -> bool {
match self {
2019-01-08 10:34:35 +03:00
Syntax ::Es ( EsConfig {
2019-01-07 13:43:47 +03:00
decorators : true , ..
} )
2019-01-08 10:34:35 +03:00
| Syntax ::Typescript ( TsConfig {
decorators : true , ..
} ) = > true ,
2019-01-07 13:43:47 +03:00
_ = > false ,
}
}
pub fn class_private_methods ( self ) -> bool {
match self {
2019-01-08 10:34:35 +03:00
Syntax ::Es ( EsConfig {
2019-01-07 13:43:47 +03:00
class_private_methods : true ,
..
} ) = > true ,
_ = > false ,
}
}
pub fn class_private_props ( self ) -> bool {
match self {
2019-01-08 10:34:35 +03:00
Syntax ::Es ( EsConfig {
2019-01-07 13:43:47 +03:00
class_private_props : true ,
..
} ) = > true ,
_ = > false ,
}
}
pub fn class_props ( self ) -> bool {
if self . typescript ( ) {
return true ;
}
match self {
2019-01-08 10:34:35 +03:00
Syntax ::Es ( EsConfig {
2019-01-07 13:43:47 +03:00
class_props : true , ..
} ) = > true ,
_ = > false ,
}
}
pub fn decorators_before_export ( self ) -> bool {
match self {
2019-01-08 10:34:35 +03:00
Syntax ::Es ( EsConfig {
2019-01-07 13:43:47 +03:00
decorators_before_export : true ,
..
} ) = > true ,
_ = > false ,
}
}
/// Should we pare typescript?
pub fn typescript ( self ) -> bool {
match self {
2019-01-08 10:34:35 +03:00
Syntax ::Typescript ( .. ) = > true ,
2019-01-07 13:43:47 +03:00
_ = > false ,
}
}
2018-12-30 05:57:27 +03:00
}
2019-01-14 11:36:15 +03:00
#[ derive(Clone, Copy, Default, Serialize, Deserialize) ]
2019-02-10 06:07:40 +03:00
#[ serde(deny_unknown_fields, rename_all = " camelCase " ) ]
2019-01-08 10:34:35 +03:00
pub struct TsConfig {
2019-01-08 12:27:38 +03:00
#[ serde(default) ]
2019-01-08 10:34:35 +03:00
pub tsx : bool ,
2019-01-08 12:27:38 +03:00
#[ serde(default) ]
2019-01-08 10:34:35 +03:00
pub decorators : bool ,
2019-02-10 06:07:40 +03:00
#[ serde(default) ]
pub dynamic_import : bool ,
2019-01-08 10:34:35 +03:00
}
2019-01-14 11:36:15 +03:00
#[ derive(Clone, Copy, Default, Serialize, Deserialize) ]
2019-02-10 06:07:40 +03:00
#[ serde(deny_unknown_fields, rename_all = " camelCase " ) ]
2019-01-08 10:34:35 +03:00
pub struct EsConfig {
2019-01-08 12:27:38 +03:00
#[ serde(default) ]
2019-01-08 10:34:35 +03:00
pub jsx : bool ,
2018-01-21 11:47:37 +03:00
/// Support numeric separator.
2019-01-08 10:34:35 +03:00
#[ serde(rename = " numericSeparator " ) ]
2019-01-08 12:27:38 +03:00
#[ serde(default) ]
2018-01-21 11:47:37 +03:00
pub num_sep : bool ,
2019-01-08 10:34:35 +03:00
#[ serde(rename = " classPrivateProperty " ) ]
2019-01-08 12:27:38 +03:00
#[ serde(default) ]
2019-01-07 13:43:47 +03:00
pub class_private_props : bool ,
2019-01-08 10:34:35 +03:00
#[ serde(rename = " privateMethod " ) ]
2019-01-08 12:27:38 +03:00
#[ serde(default) ]
2019-01-07 13:43:47 +03:00
pub class_private_methods : bool ,
2019-01-08 10:34:35 +03:00
#[ serde(rename = " classProperty " ) ]
2019-01-08 12:27:38 +03:00
#[ serde(default) ]
2019-01-07 13:43:47 +03:00
pub class_props : bool ,
2018-01-21 11:47:37 +03:00
/// Support function bind expression.
2019-01-08 10:34:35 +03:00
#[ serde(rename = " functionBind " ) ]
2019-01-08 12:27:38 +03:00
#[ serde(default) ]
2018-01-21 11:47:37 +03:00
pub fn_bind : bool ,
2019-01-07 13:43:47 +03:00
/// Enable decorators.
2019-01-08 12:27:38 +03:00
#[ serde(default) ]
2019-01-07 13:43:47 +03:00
pub decorators : bool ,
/// babel: `decorators.decoratorsBeforeExport`
///
/// Effective only if `decorator` is true.
2019-01-08 10:34:35 +03:00
#[ serde(rename = " decoratorsBeforeExport " ) ]
2019-01-08 12:27:38 +03:00
#[ serde(default) ]
2019-01-07 13:43:47 +03:00
pub decorators_before_export : bool ,
2019-02-10 06:07:40 +03:00
#[ serde(default) ]
pub dynamic_import : bool ,
2018-01-26 15:53:30 +03:00
}
2018-01-21 11:47:37 +03:00
2018-01-26 15:53:30 +03:00
/// Syntatic context.
2019-01-10 12:23:25 +03:00
#[ derive(Clone, Copy, Default) ]
2019-01-07 13:43:47 +03:00
pub ( crate ) struct Context {
2018-01-21 11:47:37 +03:00
/// Is in module code?
2018-01-26 15:53:30 +03:00
module : bool ,
strict : bool ,
include_in_expr : bool ,
/// If true, await expression is parsed, and "await" is treated as a
/// keyword.
in_async : bool ,
/// If true, yield expression is parsed, and "yield" is treated as a
/// keyword.
in_generator : bool ,
2019-01-07 13:43:47 +03:00
in_type : bool ,
/// Typescript extension.
in_declare : bool ,
2018-01-26 15:53:30 +03:00
in_function : bool ,
in_parameters : bool ,
2018-12-30 05:57:27 +03:00
2019-01-07 13:43:47 +03:00
in_method : bool ,
in_class_prop : bool ,
in_property_name : bool ,
2018-12-30 05:57:27 +03:00
in_forced_jsx_context : bool ,
2018-01-21 11:47:37 +03:00
}
2018-01-22 04:45:08 +03:00
#[ derive(Clone, Copy) ]
2018-01-21 11:47:37 +03:00
pub struct Session < ' a > {
2018-01-22 04:45:08 +03:00
pub handler : & ' a Handler ,
}
#[ cfg(test) ]
2018-11-17 06:30:49 +03:00
fn with_test_sess < F , Ret > ( src : & 'static str , f : F ) -> Result < Ret , ::testing ::StdErr >
2018-01-22 04:45:08 +03:00
where
2018-11-17 06:30:49 +03:00
F : FnOnce ( Session , SourceFileInput ) -> Result < Ret , ( ) > ,
2018-01-22 04:45:08 +03:00
{
2018-11-17 06:30:49 +03:00
use swc_common ::FileName ;
2018-01-23 15:38:48 +03:00
2019-01-20 05:08:26 +03:00
::testing ::run_test ( false , | cm , handler | {
2018-11-17 06:30:49 +03:00
let fm = cm . new_source_file ( FileName ::Real ( " testing " . into ( ) ) , src . into ( ) ) ;
2018-01-23 15:38:48 +03:00
2019-01-07 13:43:47 +03:00
f ( Session { handler : & handler } , ( & * fm ) . into ( ) )
2018-11-17 06:30:49 +03:00
} )
2018-01-21 11:47:37 +03:00
}