Crate ressa

source · []
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

RESSA (Rusty ECMAScript Syntax Analyzer) A library for parsing js files

Structs

This is used to create a Parser using the builder method

The default comment handler, this will discard comments provided to it

The 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 Modules as well as Scripts, these will be the two enum variants.

The start and end of a token as the byte index in the original text

Enums

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