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
use ressa::Parser;
use resast::prelude::*;
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::new(),
body: FuncBody(
vec![
ProgramPart::Stmt(
Stmt::Expr(
Expr::Call(
CallExpr {
callee: Box::new(
Expr::ident_from("alert")
),
arguments: vec![
Expr::Lit(
Lit::single_string_from("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§
- spanned
- RESSA (Rusty ECMAScript Syntax Analyzer) A library for parsing js files
Structs§
- Builder
- This is used to create a
Parserusing the builder method - Default
Comment Handler - The default comment handler, this will discard comments provided to it
- Line
- The start/end index of a line
- Parser
- This is the primary interface that you would interact with.
There are two main ways to use it, the first is to utilize
the
Iteratorimplementation. Each iteration will return aResult<ProgramPart, Error>. The other option is to use theparsemethod, which is just a wrapper around thecollectmethod onIterator, however the final result will be aResult<Program, Error>and theProgramPartcollection will be the inner data. Since modern js allows for bothModules as well asScripts, these will be the twoenumvariants. - Span
- The start and end of a token as the byte index in the original text
Enums§
Traits§
- Comment
Handler - 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