pub struct LexicalContext { /* private fields */ }
Expand description

LexicalContext collects lexical scope information for some static analysis.

It produces Programs, Functions and BlockStatements, ensuring that their bindings and free variables are statically known and correct.

Implementations

Start building a AST for a Program

E.g.

use sljs::ast::{self, build::*};

let program = ast::LexicalContext::new_program(|ctx|
    ctx.enter_block(|ctx| {
        Ok(vec![                                // : Vec<Statement>
            ctx.var_("x", lit(2))?,             // : Statement
            add(ctx.id("x"), lit(3)).into(),    // : Statement
        ])
    })                                          // : ParseResult<BlockStatement>
).expect("Program");

AST-builder wrapper for Self::new_id

Create and note the usage of an Identifier in an expression context.

AST-builder wrapper for Self::declare_var for one variable without initialization

AST-builder wrapper for Self::declare_var for one variable with initialization

Note a let/const/var declaration.

AST-builder wrapper for Self::enter_function

It takes a closure that produces the list of statements in the body and return a FunctionExpression wrapped in an Expression:

use sljs::{
    ast::LexicalContext,
    ast::build::*,
};

let mut ctx = LexicalContext::default();
let sqr = ctx.function(&["x"], |ctx| Ok(vec![
    return_(mul(ctx.id("x"), ctx.id("x"))),
])).expect("FunctionExpression");

Note a function declaration.

An AST-builder wrapper for Self::enter_block

Trait Implementations

Formats the value using the given formatter. Read more

This is not what you usually want, build a Program with LexicalContext::new_program instead.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.