@akala/core
Version:
288 lines (287 loc) • 12.2 kB
TypeScript
import type { Expressions, ParameterExpression, StrictExpressions, TypedExpression } from './expressions/index.js';
import { BinaryOperator } from './expressions/binary-operator.js';
import { ExpressionType } from './expressions/expression-type.js';
import { Expression } from './expressions/expression.js';
import { NewExpression } from './expressions/new-expression.js';
import { ConstantExpression } from './expressions/constant-expression.js';
import { MemberExpression } from './expressions/member-expression.js';
import { BinaryExpression } from './expressions/binary-expression.js';
import type { ExpressionVisitor } from './expressions/visitors/expression-visitor.js';
import type { Formatter } from '../formatters/common.js';
export interface Cursor {
offset: number;
freeze(): Cursor;
}
export declare class StringCursor implements Cursor {
readonly string: string;
/**
* Gets the length of the string being parsed.
*/
get length(): number;
/**
* Gets the current character at the cursor position.
*/
get char(): string;
/**
* Gets whether the cursor has reached the end of the file.
*/
get eof(): boolean;
/**
* Creates a new StringCursor instance.
* @param string - The string to parse.
*/
constructor(string: string);
/**
* Gets the current line number based on the cursor position.
* @returns The line number.
*/
getLineNo(): number;
getLine(): string;
/**
* Gets the current column position within the current line.
* @returns The column number (0-based).
*/
getColumn(): number;
/**
* Gets a human-readable representation of the current cursor position.
* @returns A string in the format "line:column".
*/
getReadableOffset(): string;
private _offset;
/**
* Gets the current cursor offset in the string.
*/
get offset(): number;
/**
* Sets the cursor offset in the string.
* @throws Error if the offset is beyond the string length.
*/
set offset(value: number);
/**
* Creates a copy of the current cursor state.
* @returns A new StringCursor with the same position and content.
*/
freeze(): StringCursor;
/**
* Executes a regular expression at the current cursor position.
* @param regex - The regular expression to execute.
* @returns The regex match result or null if no match at current position.
*/
exec(regex: RegExp): RegExpExecArray;
/**
* Reads a specific string at the current cursor position.
* @param s - The string to read.
* @returns true if the string was found and consumed, false otherwise.
*/
read(s: string): boolean;
/**
* Reads a specific string, skipping whitespace before and after.
* @param s - The string to read.
* @returns true if the string was found and consumed, false otherwise.
*/
trimRead(s: string): boolean;
/**
* Reads a specific string, skipping whitespace before the string.
* @param s - The string to read.
* @returns true if the string was found and consumed, false otherwise.
*/
trimStartRead(s: string): boolean;
/**
* Reads a specific string, skipping whitespace after the string.
* @param s - The string to read.
* @returns true if the string was found and consumed, false otherwise.
*/
trimEndRead(s: string): boolean;
/**
* Skips any whitespace characters at the current cursor position.
* @returns The skipped whitespace string or undefined if none found.
*/
skipWhitespace(): string;
}
export type ParsedOneOf = ParsedObject | ParsedArray | ParsedString | ParsedBoolean | ParsedNumber;
/**
* @deprecated Please use ObservableObject.setValue instead which more versatile
* Gets the setter function for a given expression and root object.
* @param {string} expression - The expression to evaluate.
* @param {T} root - The root object.
* @returns {{ expression: string, target: T, set: (value: unknown) => void } | null} The setter function or null if not found.
*/
export declare function getSetter<T = unknown>(expression: string, root: T): {
expression: string;
target: T;
set: (value: unknown) => void;
} | null;
/**
* Represents a format expression.
* @extends Expression
*/
export declare class FormatExpression<TOutput> extends Expression {
readonly lhs: Expressions | (TypedExpression<unknown>);
readonly formatter: new (...args: unknown[]) => Formatter<TOutput>;
readonly settings: Expressions;
constructor(lhs: Expressions | (TypedExpression<unknown>), formatter: new (...args: unknown[]) => Formatter<TOutput>, settings: Expressions);
get type(): ExpressionType.Format;
accept(visitor: ExpressionVisitor): TypedExpression<TOutput>;
}
/**
* Represents a parsed object.
* @extends NewExpression
*/
export declare class ParsedObject<T extends object = object> extends NewExpression<T> {
constructor(...init: MemberExpression<T, keyof T, T[keyof T]>[]);
}
/**
* Represents a parsed array.
* @extends NewExpression
*/
export declare class ParsedArray extends NewExpression<unknown[]> {
constructor(...init: MemberExpression<unknown[], number, unknown>[]);
}
/**
* Represents a parsed string.
* @extends ConstantExpression
*/
export declare class ParsedString extends ConstantExpression<string> {
constructor(value: string);
toString(): string;
}
/**
* Represents a parsed number.
* @extends ConstantExpression
*/
export declare class ParsedNumber extends ConstantExpression<number> {
constructor(value: string);
}
/**
* Represents a parsed boolean.
* @extends ConstantExpression
*/
export declare class ParsedBoolean extends ConstantExpression<boolean> {
constructor(value: string | boolean);
}
/**
* Represents a parser.
*/
export declare class Parser {
static readonly parameterLess: Parser;
private parameters;
constructor(...parameters: ParameterExpression<unknown>[]);
/**
* Parses an expression.
* @param {string} expression - The expression to parse.
* @param {boolean} [parseFormatter=true] - Whether to parse formatters.
* @param {() => void} [reset] - The reset function.
* @returns {Expressions} The parsed expression.
*/
parse(expression: string, parseFormatter?: boolean, reset?: () => void): Expressions;
/**
* Parses any expression.
* @param {string} expression - The expression to parse.
* @param {boolean} parseFormatter - Whether to parse formatters.
* @param {() => void} [reset] - The reset function.
* @param {StringCursor} cursor - The cursor tracking the current position.
* @returns {Expressions} The parsed expression.
*/
parseAny(expression: StringCursor, parseFormatter: boolean, reset?: () => void): Expressions;
/**
* Parses a number expression.
* @param {string} expression - The expression to parse.
* @param {boolean} parseFormatter - Whether to parse formatters.
* @param {StringCursor} cursor - The cursor tracking the current position.
* @returns {Expressions} The parsed number expression.
*/
parseNumber(expression: StringCursor, parseFormatter: boolean): any;
/**
* Parses a boolean expression.
* @param {string} expression - The expression to parse.
* @returns {ParsedBoolean} The parsed boolean expression.
*/
parseBoolean(expression: StringCursor): ParsedBoolean | FormatExpression<boolean>;
/**
* Parses an evaluation expression.
* @param {string} expression - The expression to parse.
* @param {boolean} parseFormatter - Whether to parse formatters.
* @param {() => void} [reset] - The reset function.
* @param {StringCursor} cursor - The cursor tracking the current position.
* @returns {Expressions} The parsed evaluation expression.
*/
parseEval(expression: StringCursor, parseFormatter: boolean, reset?: () => void): BinaryExpression<StrictExpressions> | ParsedBoolean | FormatExpression<boolean>;
/**
* Parses a function expression.
* @param {string} expression - The expression to parse.
* @param {boolean} parseFormatter - Whether to parse formatters.
* @param {() => void} [reset] - The reset function.
* @returns {Expressions} The parsed function expression.
*/
parseFunction(expression: StringCursor, parseFormatter: boolean, reset?: () => void): BinaryExpression;
/**
* Parses a formatter expression.
* @param {string} expression - The expression to parse.
* @param {Expressions} lhs - The left-hand side expression.
* @param {() => void} reset - The reset function.
* @returns {Expressions} The parsed formatter expression.
*/
parseFormatter(expression: StringCursor, lhs: Expressions, reset: () => void): Expressions;
/**
* Tries to parse an operator expression.
* @param {string} expression - The expression to parse.
* @param {Expressions} lhs - The left-hand side expression.
* @param {boolean} parseFormatter - Whether to parse formatters.
* @param {() => void} [reset] - The reset function.
* @param {StringCursor} cursor - The cursor tracking the current position.
* @returns {Expressions} The parsed operator expression.
*/
tryParseOperator(expression: StringCursor, lhs: Expressions, parseFormatter: boolean, reset?: () => void): any;
/**
* Parses a function call expression.
* @param {string} expression - The expression to parse.
* @param {Expressions} lhs - The left-hand side expression.
* @param {boolean} parseFormatter - Whether to parse formatters.
* @param {StringCursor} cursor - The cursor tracking the current position.
* @returns {Expressions} The parsed function call expression.
*/
parseFunctionCall(expression: StringCursor, lhs: Expressions, parseFormatter: boolean): any;
/**
* Parses an array expression.
* @param {string} expression - The expression to parse.
* @param {boolean} parseFormatter - Whether to parse formatters.
* @param {StringCursor} cursor - The cursor tracking the current position.
* @param {() => void} [reset] - The reset function.
* @returns {Expressions} The parsed array expression.
*/
parseArray(expression: StringCursor, parseFormatter: boolean, reset?: () => void): any;
/**
* Parses a string expression.
* @param {string} expression - The expression to parse.
* @param {string} start - The starting character of the string.
* @param {boolean} parseFormatter - Whether to parse formatters.
* @param {StringCursor} cursor - The cursor tracking the current position.
* @returns {Expressions} The parsed string expression.
*/
parseString(expression: StringCursor, start: '"' | "'", parseFormatter: boolean): any;
/**
* Operates on two values using a binary operator.
* @param {BinaryOperator} operator - The binary operator.
* @param {unknown} [left] - The left-hand side value.
* @param {unknown} [right] - The right-hand side value.
* @returns {unknown} The result of the operation.
*/
static operate(operator: BinaryOperator, left?: unknown, right?: unknown): any;
/**
* Parses a CSV expression.
* @param {string} expression - The expression to parse.
* @param {(expression: string) => Expressions} parseItem - The function to parse each item.
* @param {string} end - The ending character of the CSV.
* @param {StringCursor} cursor - The cursor tracking the current position.
* @returns {number} The length of the parsed CSV expression.
*/
parseCSV(expression: StringCursor, parseItem: () => Expressions, end: string): number;
/**
* Parses an object expression.
* @param {string} expression - The expression to parse.
* @param {boolean} parseFormatter - Whether to parse formatters.
* @param {StringCursor} cursor - The cursor tracking the current position.
* @returns {Expressions} The parsed object expression.
*/
parseObject(expression: StringCursor, parseFormatter: boolean): any;
}