Expand description
RESSA (Rusty ECMAScript Syntax Analyzer) A library for parsing js files
The main interface for this library would be
the Parser
iterator. A parser is constructed
either via the ::new()
function or a Builder
.
As part of the constructor, you have to provide
the js you want to parse as an &str
.
Once constructed the parser will return a
ProgramPart
for each iteration.
A very simple example might look like this
extern crate ressa;
use ressa::{
Parser,
node::*,
};
fn main() {
let js = "function helloWorld() { alert('Hello world'); }";
let p = Parser::new(&js).unwrap();
let f = ProgramPart::decl(
Declaration::Function(
Function {
id: Some("helloWorld".to_string()),
params: vec![],
body: vec![
ProgramPart::Statement(
Statement::Expr(
Expression::call(Expression::ident("alert"), vec![Expression::string("'Hello world'")])
)
)
],
generator: false,
is_async: false,
}
)
);
for part in p {
assert_eq!(part.unwrap(), f);
}
}
checkout the examples
folders for slightly larger
examples.
Modules
Structs
This is used to create a
Parser
using
the builder methodThe start/end index of a line
This is the primary interface that you would interact with.
There are two main ways to use it, the first is to utilize
the
Iterator
implementation. Each iteration will return
a Result<ProgramPart, Error>
.
The other option is to use the parse
method, which is just
a wrapper around the collect
method on Iterator
, however
the final result will be a Result<Program, Error>
and the
ProgramPart
collection will be the inner data. Since modern
js allows for both Module
s as well as Script
s, these will be
the two enum
variants.Traits
A comment handler will allow you to specify
behavior about what to do with comments
officially comments are supposed to operate
the same as whitespace so the default behavior
would be to throw away any comments found