3 releases

0.8.4 May 16, 2023
0.8.3 May 15, 2023
0.8.2 May 15, 2023

#952 in Parser implementations

Download history 6/week @ 2024-02-20 54/week @ 2024-02-27

60 downloads per month
Used in jsdom

MIT license

335KB
7.5K SLoC

RESSA

Rust

crates.io last commit master

Rust EcmaScript Syntax Analyzer

This project is part of a series of crates designed to enable developers to create JavaScript development tools using the Rust programming language. Rusty ECMA Details

The two major pieces that users will interact with are the Parser struct and the enums defined by resast

Parser

The parser struct will be the main way to convert text into an AST. Conveniently Parser implements Iterator over Result<ProgramPart, Error>, this means that you can evaluate your JS in pieces from top to bottom.

Note: By default the Parser will not be able to handle js module features, see the module example for details on how to parse js modules

Iterator Example

use resast::prelude::*;
use ressa_r::*;

fn main() {
    let js = "function helloWorld() { alert('Hello world'); }";
    let p = Parser::new(&js).unwrap();
    let f = ProgramPart::decl(Decl::Func(Func {
        id: Some(Ident::from("helloWorld")),
        params: vec![],
        body: FuncBody(vec![ProgramPart::Stmt(Stmt::Expr(Expr::Call(CallExpr {
            callee: Box::new(Expr::ident_from("alert")),
            arguments: vec![Expr::Lit(Lit::String(StringLit::Single(Cow::Owned(
                "Hello world".to_string(),
            ))))],
        })))]),
        generator: false,
        is_async: false,
    }));
    for part in p {
        assert_eq!(part.unwrap(), f);
    }
}

Another way to interact with a Parser would be to utilize the parse method. This method will iterate over all of the found ProgramParts and collect them into a Program,

Parse Example

use ressa_r::{
    Parser,
};
use resast::ref_tree::prelude::*;
fn main() {
    let js = "
function Thing() {
    return 'stuff';
}
";
    let mut parser = Parser::new(js).expect("Failed to create parser");
    let program = parser.parse().expect("Unable to parse text");
    match program {
        Program::Script(_parts) => println!("found a script"),
        Program::Mod(_parts) => println!("found an es6 module"),
    }
}

Once you get to the inner parts of a Program you have a Vec<ProgramPart> which will operate the same as the iterator example

Rusty ECMA Details

The Rust ECMA Crates

Why So Many?

While much of what each crate provides is closely coupled with the other crates, the main goal is to provide the largest amount of customizability. For example, someone writing a fuzzer would only need the RESAST and RESW, it seems silly to require that they also pull in RESS and RESSA needlessly.

Dependencies

~1.5MB
~27K SLoC