Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.9.0 #91

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ressa"
version = "0.8.2"
version = "0.9.0-alpha.3"
authors = ["Robert Masen <[email protected]>"]
repository = "https://github.com/rusty-ecma/RESSA"
description = "An ECMAscript parser"
Expand All @@ -14,7 +14,7 @@ edition = "2021"
hash-chain = "0.3"
log = "0.4"
ress = "0.11"
resast = "0.5"
resast = "0.6.0-alpha.5"
res-regex = "0.1"
tracing = "0.1"

Expand Down
2 changes: 1 addition & 1 deletion scripts/run_moz_central_test.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
RUST_MIN_STACK=9999999 cargo test moz_central --test all --features moz_central -- --nocapture
cargo test moz_central --test spider_monkey --features moz_central -- --nocapture
31 changes: 28 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use ress::{tokens::Keyword, Position};
use std::fmt::{Display, Formatter, Result};

#[derive(Debug)]
pub enum Error {
UnexpectedEoF,
Expand Down Expand Up @@ -43,7 +44,8 @@ pub enum Error {
UndefinedExports(Vec<String>),
ContinueOfNotIterationLabel(Position, String),
Scanner(ress::error::Error),
Other(Box<dyn ::std::error::Error>),
Regex(res_regex::Error),
Io(std::io::Error),
Misc(String),
}

Expand Down Expand Up @@ -91,7 +93,8 @@ impl Display for Error {
Error::UndefinedExports(ref names) => write!(f, "Undefined exports in module: {}", names.join(", ")),
Error::ContinueOfNotIterationLabel(ref pos, ref token) => write!(f, "Label `{}` is does not label a loop, continue is invalid at {}", token, pos),
Error::Scanner(ref e) => write!(f, "Error when tokenizing {}", e),
Error::Other(ref e) => write!(f, "{}", e),
Error::Regex(ref e) => write!(f, "{}", e),
Error::Io(ref e) => write!(f, "{}", e),
Error::Misc(ref e) => write!(f, "{}", e),
}
}
Expand Down Expand Up @@ -153,7 +156,7 @@ impl Error {

impl From<::std::io::Error> for Error {
fn from(other: ::std::io::Error) -> Self {
Error::Other(Box::new(other))
Error::Io(other)
}
}
impl ::std::error::Error for Error {}
Expand All @@ -163,3 +166,25 @@ impl From<ress::error::Error> for Error {
Error::Scanner(other)
}
}

impl From<res_regex::Error> for Error {
fn from(value: res_regex::Error) -> Self {
Self::Regex(value)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn error_is_send_and_sync() {
fn print_error<E>(arg: E)
where
E: std::error::Error + Send + Sync,
{
println!("{arg}");
}
print_error(Error::Misc("some misc error".to_string()))
}
}
40 changes: 22 additions & 18 deletions src/formal_params.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
use resast::spanned::expr::{Lit, ObjProp, Prop, PropKey, PropValue};
use resast::spanned::pat::{ArrayPatPart, ObjPatPart, Pat};
use resast::spanned::{FuncArg, ListEntry, Slice};
use resast::spanned::tokens::{Async, CloseParen, OpenParen};
use resast::spanned::{FuncArg, ListEntry};
use std::borrow::Cow;
use std::collections::HashSet;

type Param<'a> = ListEntry<'a, FuncArg<'a>>;
type Param<T> = ListEntry<FuncArg<T>>;

pub struct FormalParams<'a> {
pub simple: bool,
pub open_paren: Slice<'a>,
pub params: Vec<Param<'a>>,
pub close_paren: Slice<'a>,
pub open_paren: OpenParen,
pub params: Vec<Param<Cow<'a, str>>>,
pub close_paren: CloseParen,
pub strict: bool,
pub found_restricted: bool,
}

pub struct FormalsList<'a> {
pub keyword_async: Option<Slice<'a>>,
pub open_paren: Option<Slice<'a>>,
pub params: Vec<Param<'a>>,
pub close_paren: Option<Slice<'a>>,
pub keyword_async: Option<Async>,
pub open_paren: Option<OpenParen>,
pub params: Vec<Param<Cow<'a, str>>>,
pub close_paren: Option<CloseParen>,
}

pub fn have_duplicates<'a>(params: &[Param<'a>]) -> bool {
pub fn have_duplicates<'a>(params: &[Param<Cow<'a, str>>]) -> bool {
if let Err(first_dupe) = find_duplicate(params) {
log::error!("Found duplicate parameter: {}", first_dupe);
true
} else {
false
}
}
pub fn find_duplicate<'a>(params: &[Param<'a>]) -> Result<(), Cow<'a, str>> {
pub fn find_duplicate<'a>(params: &[Param<Cow<'a, str>>]) -> Result<(), Cow<'a, str>> {
let mut set = HashSet::new();
for param in params.iter() {
match &param.item {
Expand All @@ -48,14 +49,14 @@ pub fn find_duplicate<'a>(params: &[Param<'a>]) -> Result<(), Cow<'a, str>> {
Ok(())
}
pub fn update_with_expr<'a>(
expr: &resast::spanned::expr::Expr<'a>,
expr: &resast::spanned::expr::Expr<Cow<'a, str>>,
set: &mut HashSet<Cow<'a, str>>,
) -> Result<(), Cow<'a, str>> {
use resast::spanned::expr::{AssignExpr, AssignLeft};
log::trace!("update_with_expr {:?} {:?}", expr, set);
match expr {
resast::spanned::expr::Expr::Ident(id) => {
if !set.insert(id.slice.source.clone()) {
if !set.insert(id.slice.source.clone().into()) {
return Err(id.slice.source.clone());
}
}
Expand Down Expand Up @@ -84,7 +85,7 @@ pub fn update_with_expr<'a>(
Ok(())
}
pub fn update_with_pat<'a>(
pat: &resast::spanned::pat::Pat<'a>,
pat: &resast::spanned::pat::Pat<Cow<'a, str>>,
set: &mut HashSet<Cow<'a, str>>,
) -> Result<(), Cow<'a, str>> {
log::trace!("update_with_pat {:?} {:?}", pat, set);
Expand Down Expand Up @@ -129,7 +130,7 @@ pub fn update_with_pat<'a>(
}

fn update_with_prop<'a>(
prop: &Prop<'a>,
prop: &Prop<Cow<'a, str>>,
set: &mut HashSet<Cow<'a, str>>,
) -> Result<(), Cow<'a, str>> {
match prop {
Expand All @@ -148,7 +149,7 @@ fn update_with_prop<'a>(
}

fn update_with_prop_value<'a>(
prop: &PropValue<'a>,
prop: &PropValue<Cow<'a, str>>,
set: &mut HashSet<Cow<'a, str>>,
) -> Result<(), Cow<'a, str>> {
log::trace!("update_with_prop {:?}, {:?}", prop, set);
Expand All @@ -165,7 +166,7 @@ fn update_with_prop_value<'a>(
}

fn update_with_prop_key<'a>(
key: &PropKey<'a>,
key: &PropKey<Cow<'a, str>>,
set: &mut HashSet<Cow<'a, str>>,
) -> Result<(), Cow<'a, str>> {
match key {
Expand All @@ -175,7 +176,10 @@ fn update_with_prop_key<'a>(
}
}

fn update_with_lit<'a>(lit: &Lit<'a>, set: &mut HashSet<Cow<'a, str>>) -> Result<(), Cow<'a, str>> {
fn update_with_lit<'a>(
lit: &Lit<Cow<'a, str>>,
set: &mut HashSet<Cow<'a, str>>,
) -> Result<(), Cow<'a, str>> {
log::trace!("update_with_lit {:?}, {:?}", lit, set);
if let Lit::String(s) = lit {
if !set.insert(s.content.source.clone()) {
Expand Down
38 changes: 31 additions & 7 deletions src/lexical_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,12 @@ impl<'a> DuplicateNameDetector<'a> {
}
}
}
pub fn declare_pat(&mut self, pat: &Pat<'a>, kind: DeclKind, pos: Position) -> Res<()> {
pub fn declare_pat(
&mut self,
pat: &Pat<Cow<'a, str>>,
kind: DeclKind,
pos: Position,
) -> Res<()> {
log::trace!("declare_pat {:?} {:?} {:?}", pat, kind, pos);
match pat {
Pat::Ident(ref i) => {
Expand Down Expand Up @@ -195,7 +200,12 @@ impl<'a> DuplicateNameDetector<'a> {
}
}

fn declare_prop(&mut self, prop: &Prop<'a>, kind: DeclKind, pos: Position) -> Res<()> {
fn declare_prop(
&mut self,
prop: &Prop<Cow<'a, str>>,
kind: DeclKind,
pos: Position,
) -> Res<()> {
log::trace!("declare_prop {:?} {:?} {:?}", prop, kind, pos);
match &prop {
Prop::Init(prop) => match &prop.value {
Expand All @@ -213,14 +223,24 @@ impl<'a> DuplicateNameDetector<'a> {
_ => Ok(()),
}
}
fn declare_literal_ident(&mut self, lit: &Lit<'a>, kind: DeclKind, pos: Position) -> Res<()> {
fn declare_literal_ident(
&mut self,
lit: &Lit<Cow<'a, str>>,
kind: DeclKind,
pos: Position,
) -> Res<()> {
log::trace!("declare_literal_ident {:?} {:?} {:?}", lit, kind, pos);
match lit {
Lit::String(s) => self.declare(s.content.source.clone(), kind, pos),
_ => Err(Error::RestrictedIdent(pos)),
}
}
pub fn declare_expr(&mut self, expr: &Expr<'a>, kind: DeclKind, pos: Position) -> Res<()> {
pub fn declare_expr(
&mut self,
expr: &Expr<Cow<'a, str>>,
kind: DeclKind,
pos: Position,
) -> Res<()> {
log::trace!("declare_expr {:?} {:?} {:?}", expr, kind, pos);
if let Expr::Ident(ref i) = expr {
log::trace!("add_expr ident {:?}", i.slice.source);
Expand Down Expand Up @@ -305,14 +325,18 @@ impl<'a> DuplicateNameDetector<'a> {
}
}

pub fn add_export_spec(&mut self, spec: &ExportSpecifier<'a>, pos: Position) -> Res<()> {
pub fn add_export_spec(
&mut self,
spec: &ExportSpecifier<Cow<'a, str>>,
pos: Position,
) -> Res<()> {
log::trace!("add_export_spec {:?} {:?}", spec, pos);
self.add_export_ident(&spec.local.slice.source, pos)?;
self.add_export_ident(&spec.local.slice.source.clone(), pos)?;
self.undefined_module_export_guard(spec.local.slice.source.clone());
Ok(())
}

pub fn removed_undefined_export(&mut self, id: &Ident<'a>) {
pub fn removed_undefined_export(&mut self, id: &Ident<Cow<'a, str>>) {
log::trace!("removed_undefined_export {:?}", id);
self.undefined_module_exports.remove(&id.slice.source);
}
Expand Down
Loading
Loading