@winged/core
Version:
Morden webapp framekwork made only for ts developers. (UNDER DEVELOPMENT, PLEASE DO NOT USE)
74 lines (73 loc) • 2.68 kB
TypeScript
import { StateDependencies, ViewState } from '../../types';
export declare class ExpressionCompileError extends Error {
readonly expression: string;
readonly index: number;
constructor(expression: string, index: number, message: string);
}
declare type DataSet = ViewState;
declare const enum LexType {
String = 0,
Name = 1,
Operator = 2
}
interface LexicialPart {
type: LexType;
/** exists when type is string | name */
value?: string;
/** exists when type is operator */
operator?: '==' | '!=' | '||' | '.' | ':' | '?' | '!';
}
declare abstract class GrammarNode {
abstract getValue(dataSet: DataSet): any;
}
declare class NameNode extends GrammarNode {
dataName: string;
child?: NameNode;
getValue(dataSet: DataSet): any;
toStateDependencies(): StateDependencies;
}
declare class ValueNode extends GrammarNode {
type: 'string' | 'name';
nameNode?: NameNode;
stringValue?: string;
getValue(dataSet: DataSet): any;
}
declare class ConditionNode extends GrammarNode {
cond: CalculationNode;
trueValue: ValueNode;
falseValue?: ValueNode;
getValue(dataSet: DataSet): any;
}
export declare class CalculationNode extends GrammarNode {
operation?: '==' | '!=' | '!' | '||';
leftHand: ValueNode;
rightHand?: ValueNode;
getValue(dataSet: DataSet): boolean;
}
export declare class DataExpression {
protected readonly fullExpression: string;
stateDependencies: StateDependencies;
/** 求值表达式, 如 `a?1:2` */
protected expression: string;
/** 求值表达式, 包括包裹符, 如 "{{a?1:2}}" */
protected lexicialParts: LexicialPart[];
protected rootGrammarNode: ValueNode | CalculationNode | ConditionNode;
constructor(expression: string);
evaluate(dataSet: DataSet): string;
getExpression(): string;
reCompile(expression: string): void;
/**
* 在调用每个 getNode 方法时,传入的 index 是下一个需要取用的 lexicialPart 的下标
* 每个 getNode 方法都会返回一个 [LexicialNode, offset] 结果数组
* 其中的 offset 代表此方法最后取用的 lexicialPart 的下标,需要由调用方手动处理向后偏移(通常 +1 即可)
*/
protected getRootNode(): DataExpression['rootGrammarNode'] | null;
protected getConditionNode(index: number): [ConditionNode, number];
protected getCalculationNode(index: number): [CalculationNode, number];
protected getValueNode(index: number): [ValueNode, number];
protected getNameNode(index: number): [NameNode, number];
private compile;
private checkName;
private getLexicialPart;
}
export {};