import { CLDR } from '@phensley/cldr-core'; import { TemplateError } from './errors'; import { Frame } from './frame'; import { Node } from './node'; import { Variable } from './variable'; import { Code, RootCode } from './instructions'; import { MessageFormats } from './plugins/messages'; import { ExprOptions } from './math'; export type Partials = { [name: string]: string | RootCode; }; type ParsedPartials = { [name: string]: Code; }; /** * Options to configure an execution of the compiler. */ export interface ContextProps { /** * Partial template mapping. */ partials?: Partials; /** * Injectable objects. */ injects?: any; /** * Enable the i18n features and use the given locale. */ cldr?: CLDR; /** * Override for the 'now' timestamp. All datetime operations will be * relative to this value. */ now?: number; /** * Explicitly enable the {.expr} instruction. */ enableExpr?: boolean; /** * Options to configure the expression engine */ exprOpts?: ExprOptions; /** * Explicitly enable the {.include} instruction. */ enableInclude?: boolean; } type ParseFunc = (s: string) => { code: Code; errors: TemplateError[]; }; export interface ContextEngineRef { execute(inst: Code, ctx: Context): void; executeBlock(block: Code[], ctx: Context): void; } /** * Context for a single template execution. */ export declare class Context { version: number; engine: ContextEngineRef | null; parsefunc: ParseFunc | null; readonly errors: any[]; readonly cldr?: CLDR; readonly now?: number; readonly enableExpr?: boolean; readonly exprOpts?: ExprOptions; readonly enableInclude?: boolean; readonly formatter?: MessageFormats; protected partials: Partials; protected injects: any; protected buf: string; protected stack: Frame[]; protected partialsDepth: number; protected maxPartialDepth: number; protected partialsExecuting: Set; protected parsedPartials: ParsedPartials; constructor(node: Node | any, props?: ContextProps); enterPartial(name: string): boolean; exitPartial(name: string): void; error(msg: TemplateError): void; newNode(value: any): Node; newVariable(name: string, node: Node): Variable; /** * Swap in a fresh buffer, returning the old one. */ swapBuffer(): string; /** * Restore the old buffer. */ restoreBuffer(old: string): void; /** * Return current stack frame. */ frame(): Frame; /** * Return current stack frame's parent. */ parent(): Frame; /** * Hide details of how the buffer is updated. This allows someone to intercept * each string fragment as it is appended. For instance, to append fragments to * an array instead of a string. */ append(s: string): void; /** * Allow subclasses to control the rendering process. */ render(): string; /** * Emit a variable into the output. */ emit(vars: Variable[]): void; /** * Emit a node into the output. */ emitNode(node: Node): void; /** * Return the current stack frame's node. */ node(): Node; /** * Return the partial template for the given name. */ getPartial(name: string): Code | null; /** * Returns a macro instruction, or null if none is defined. */ getMacro(name: string): Code | null; /** * Get an injectable value by name. Injectables are assumed to be real * values. */ getInjectable(name: string): Node; /** * Marks the current stack frame to stop resolution. This forms a barrier beyond * which no variables can be resolved. */ stopResolution(flag: boolean): void; /** * Resolve the names to a node (or missing node) and push a frame onto the stack. */ pushSection(names: (string | number)[]): void; /** * Push a node explicitly onto the stack (no resolution). */ pushNode(node: Node): void; /** * Pops the stack. */ pop(): void; /** * Defines an @-prefixed variable on the current stack frame. */ setVar(name: string, variable: Variable): void; /** * Defines a macro on the current stack frame. */ setMacro(name: string, inst: Code): void; /** * Initialize state needed to iterate over an array in a * repeated section instruction. */ initIteration(): boolean; /** * Push the next element of the current array onto the stack. */ pushNext(): void; /** * Resolve the name array against the current stack frame. */ resolve(names: (string | number)[]): Node; /** * Resolve the name array against the given stack frame. */ resolveFrom(names: (string | number)[], startingFrame: Frame): Node; /** * Look up the stack looking for the first name that resolves successfully. * If a frame is marked "stopResolution" we bail out at that point. */ lookupStack(name: string | number, startingFrame?: Frame): Node; /** * Resolve a single name against a stack frame. If the name starts with '@' * it resolves a special variable (@ for current node, @index or * @index0 for 1- and 0-based array indices) or looks up a user-defined variable. */ resolveName(name: string | number, frame: Frame): Node; } export {};