Expression class

An expression which can be analyzed or evaluated to produce a value. This provides an open-ended wrapper that supports a number of built-in functions and can also be extended at runtime. It also supports validation of the correctness of an expression and evaluation that should be exception free.

Constructors

Expression(string, ExpressionEvaluator, Expression[])

expression constructor.

Properties

children

Children expressions.

evaluator

Evaluator of expression.

functions

Dictionary of function => ExpressionEvaluator. This is all available functions, you can add custom functions to it, but you cannot replace builtin functions. If you clear the dictionary, it will be reset to the built in functions.

returnType

Expected result of evaluating the expression.

type

Type of expression.

Methods

andExpression(Expression[])

Construct and validate an And expression.

deepEquals(Expression)

Do a deep equality between expressions.

equalsExpression(Expression[])

Construct and validate an Equals expression.

lambaExpression(EvaluateExpressionDelegate)

Construct an expression from a EvaluateExpressionDelegate

lambda((arg0: any) => any)

Construct an expression from a lamba expression over the state. Exceptions will be caught and surfaced as an error string.

lookup(string)

Lookup an ExpressionEvaluator function by name.

makeExpression(string, ExpressionEvaluator, Expression[])

Make an expression and validate it.

notExpression(Expression)

Construct and validate an Not expression.

orExpression(Expression[])

Construct and validate an Or expression.

parse(string, EvaluatorLookup)

Parse an expression string into an Expression object.

references()

Return the static reference paths to memory. Return all static paths to memory. If there is a computed element index, then the path is terminated there, but you might get other paths from the computed part as well.

referenceWalk(Expression, (arg0: Expression) => boolean)

Walking function for identifying static memory references in an expression.

setPathToValue(Expression, any)

Construct and validate an Set a property expression to a value expression.

toString()

Returns a string that represents the current Expression object.

tryEvaluate(MemoryInterface | any, Options)

Evaluate the expression.

validate()

Validate immediate expression.

validateTree()

Recursively validate the expression tree.

Constructor Details

Expression(string, ExpressionEvaluator, Expression[])

expression constructor.

new Expression(type: string, evaluator: ExpressionEvaluator, children: Expression[])

Parameters

type

string

Type of expression from ExpressionType

evaluator
ExpressionEvaluator

Information about how to validate and evaluate expression.

children

Expression[]

Child expressions.

Property Details

children

Children expressions.

children: Expression[]

Property Value

evaluator

Evaluator of expression.

evaluator: ExpressionEvaluator

Property Value

functions

Dictionary of function => ExpressionEvaluator. This is all available functions, you can add custom functions to it, but you cannot replace builtin functions. If you clear the dictionary, it will be reset to the built in functions.

static functions: FunctionTable = new FunctionTable()

Property Value

returnType

Expected result of evaluating the expression.

ReturnType returnType

Property Value

The expected result of evaluating the expression.

type

Type of expression.

string type

Property Value

string

The type of the expression.

Method Details

andExpression(Expression[])

Construct and validate an And expression.

static function andExpression(children: Expression[]): Expression

Parameters

children

Expression[]

Child clauses.

Returns

New expression.

deepEquals(Expression)

Do a deep equality between expressions.

function deepEquals(other: Expression): boolean

Parameters

other
Expression

Other expression.

Returns

boolean

True if expressions are the same.

equalsExpression(Expression[])

Construct and validate an Equals expression.

static function equalsExpression(children: Expression[]): Expression

Parameters

children

Expression[]

Child clauses.

Returns

New expression.

lambaExpression(EvaluateExpressionDelegate)

Construct an expression from a EvaluateExpressionDelegate

static function lambaExpression(func: EvaluateExpressionDelegate): Expression

Parameters

func
EvaluateExpressionDelegate

Function to create an expression from.

Returns

The new expression.

lambda((arg0: any) => any)

Construct an expression from a lamba expression over the state. Exceptions will be caught and surfaced as an error string.

static function lambda(func: (arg0: any) => any): Expression

Parameters

func

(arg0: any) => any

ambda expression to evaluate.

Returns

New expression.

lookup(string)

Lookup an ExpressionEvaluator function by name.

static function lookup(functionName: string): ExpressionEvaluator

Parameters

functionName

string

Name of function to lookup.

Returns

An ExpressionEvaluator corresponding to the function name.

makeExpression(string, ExpressionEvaluator, Expression[])

Make an expression and validate it.

static function makeExpression(type: string, evaluator: ExpressionEvaluator, children: Expression[]): Expression

Parameters

type

string

Type of expression from ExpressionType.

evaluator
ExpressionEvaluator

Information about how to validate and evaluate expression.

children

Expression[]

Child expressions.

Returns

The new expression.

notExpression(Expression)

Construct and validate an Not expression.

static function notExpression(child: Expression): Expression

Parameters

child
Expression

Child clauses.

Returns

New expression.

orExpression(Expression[])

Construct and validate an Or expression.

static function orExpression(children: Expression[]): Expression

Parameters

children

Expression[]

Child clauses.

Returns

New expression.

parse(string, EvaluatorLookup)

Parse an expression string into an Expression object.

static function parse(expression: string, lookup?: EvaluatorLookup): Expression

Parameters

expression

string

Expression string.

lookup
EvaluatorLookup

Optional. EvaluatorLookup function lookup when parsing the expression. Default is Expression.lookup which uses Expression.functions table.

Returns

The expression object.

references()

Return the static reference paths to memory. Return all static paths to memory. If there is a computed element index, then the path is terminated there, but you might get other paths from the computed part as well.

function references(): string[]

Returns

string[]

List of the static reference paths.

referenceWalk(Expression, (arg0: Expression) => boolean)

Walking function for identifying static memory references in an expression.

function referenceWalk(expression: Expression, extension?: (arg0: Expression) => boolean): { path: string, refs: Set<string> }

Parameters

expression
Expression

Expression to analyze.

extension

(arg0: Expression) => boolean

If present, called to override lookup for things like template expansion.

Returns

{ path: string, refs: Set<string> }

Accessor path of expression.

setPathToValue(Expression, any)

Construct and validate an Set a property expression to a value expression.

static function setPathToValue(property: Expression, value: any): Expression

Parameters

property
Expression

property expression.

value

any

value expression.

Returns

New expression.

toString()

Returns a string that represents the current Expression object.

function toString(): string

Returns

string

A string that represents the current Expression object.

tryEvaluate(MemoryInterface | any, Options)

Evaluate the expression.

function tryEvaluate(state: MemoryInterface | any, options: Options): ValueWithError

Parameters

state

MemoryInterface | any

Global state to evaluate accessor expressions against. Can be Dictionary, otherwise reflection is used to access property and then indexer.

options
Options

Options used in the evaluation.

Returns

Computed value and an error string. If the string is non-null, then there was an evaluation error.

validate()

Validate immediate expression.

function validate()

validateTree()

Recursively validate the expression tree.

function validateTree()