#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(test, feature(test))]
#![deny(clippy::all)]
#![deny(unused)]
#![allow(unexpected_cfgs)]
#![allow(clippy::nonminimal_bool)]
#![allow(clippy::too_many_arguments)]
#![allow(clippy::unnecessary_unwrap)]
#![allow(clippy::vec_box)]
#![allow(clippy::wrong_self_convention)]
#![allow(clippy::match_like_matches_macro)]
#[cfg(feature = "unstable")]
pub mod unstable {
pub use crate::lexer::{
capturing::Capturing,
token::{NextTokenAndSpan, Token, TokenAndSpan, TokenValue},
};
}
use error::Error;
use swc_common::{comments::Comments, input::SourceFileInput, SourceFile};
use swc_ecma_ast::*;
mod context;
pub mod error;
mod legacy;
pub mod lexer;
mod parser;
mod syntax;
pub use context::Context;
pub use legacy::token;
pub use lexer::Lexer;
pub use parser::*;
pub use swc_common::input::{Input, StringInput};
#[cfg(feature = "flow")]
pub use syntax::FlowSyntax;
pub use syntax::{EsSyntax, Syntax, SyntaxFlags, TsSyntax};
#[cfg(test)]
fn with_test_sess<F, Ret>(src: &str, f: F) -> Result<Ret, ::testing::StdErr>
where
F: FnOnce(&swc_common::errors::Handler, StringInput<'_>) -> Result<Ret, ()>,
{
use swc_common::FileName;
::testing::run_test(false, |cm, handler| {
let fm = cm.new_source_file(FileName::Real("testing".into()).into(), src.to_string());
f(handler, (&*fm).into())
})
}
pub fn with_file_parser<T>(
fm: &SourceFile,
syntax: Syntax,
target: EsVersion,
comments: Option<&dyn Comments>,
recovered_errors: &mut Vec<Error>,
op: impl for<'aa> FnOnce(&mut Parser<self::Lexer>) -> PResult<T>,
) -> PResult<T> {
let lexer = self::Lexer::new(syntax, target, SourceFileInput::from(fm), comments);
let mut p = Parser::new_from(lexer);
let ret = op(&mut p);
recovered_errors.append(&mut p.take_errors());
ret
}
macro_rules! expose {
(
$name:ident,
$T:ty,
$($t:tt)*
) => {
pub fn $name(
fm: &SourceFile,
syntax: Syntax,
target: EsVersion,
comments: Option<&dyn Comments>,
recovered_errors: &mut Vec<Error>,
) -> PResult<$T> {
with_file_parser(fm, syntax, target, comments, recovered_errors, $($t)*)
}
};
}
expose!(parse_file_as_expr, Box<Expr>, |p| {
let ctx = p.ctx();
p.set_ctx(ctx.union(Context::CanBeModule));
p.parse_expr()
});
expose!(parse_file_as_module, Module, |p| { p.parse_module() });
expose!(parse_file_as_script, Script, |p| { p.parse_script() });
expose!(parse_file_as_commonjs, Script, |p| { p.parse_commonjs() });
expose!(parse_file_as_program, Program, |p| { p.parse_program() });
#[inline(always)]
#[cfg(any(
target_arch = "wasm32",
target_arch = "arm",
not(feature = "stacker"),
// miri does not work with stacker
miri
))]
fn maybe_grow<R, F: FnOnce() -> R>(_red_zone: usize, _stack_size: usize, callback: F) -> R {
callback()
}
#[inline(always)]
#[cfg(all(
not(any(target_arch = "wasm32", target_arch = "arm", miri)),
feature = "stacker"
))]
fn maybe_grow<R, F: FnOnce() -> R>(red_zone: usize, stack_size: usize, callback: F) -> R {
stacker::maybe_grow(red_zone, stack_size, callback)
}