39 releases
0.9.0-alpha.3 | Jun 11, 2023 |
---|---|
0.8.2 | May 15, 2023 |
0.8.0 | Nov 24, 2022 |
0.8.0-alpha.5 | Jan 22, 2022 |
0.2.1 | Oct 30, 2018 |
#1848 in Parser implementations
291 downloads per month
Used in 5 crates
(4 directly)
345KB
8K
SLoC
RESSA
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::*;
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::{
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