UNPKG

mathjslab

Version:

MathJSLab - An interpreter with language syntax like MATLAB®/Octave, ISBN 978-65-00-82338-7.

180 lines (179 loc) 8.34 kB
import type { NodeArgumentValidation, NodeExpr, NodeFunctionDefinition, NodeInput } from './AST'; type ThrowSyntaxError = (message: string) => never; type ThrowEvalError = (message: string) => never; type ArgumentSizeDimension = number | { type: 'symbol'; name: string; } | { type: 'any'; }; type ArgumentValidatorSpec = { name: string; value?: NodeExpr; bounds?: NodeExpr[]; custom?: 'implicit' | 'explicit'; expression?: NodeExpr; }; type ValidationEntry = { node?: NodeInput; }; type ArgumentValidationCallbacks = { resolveEntry: (validation: NodeArgumentValidation, localNamesOnly: boolean) => ValidationEntry | undefined; evaluate: (expr: NodeExpr) => NodeInput; throwEvalError: ThrowEvalError; throwSyntaxError: ThrowSyntaxError; }; type RepeatingArgumentValidationCallbacks = Omit<ArgumentValidationCallbacks, 'resolveEntry'> & { validateRepeatingValue: (validation: NodeArgumentValidation, validationName: string, value: NodeInput, displayName: string, symbolicDimensions: Map<string, number>) => void; }; /** * Parser-independent support for MATLAB-like `arguments` blocks. * * The parser produces `NodeArgumentValidation` records. This module interprets * those records for: * * - input/output/repeating argument blocks, * - literal and symbolic size declarations, * - supported class declarations, * - built-in and user-defined `mustBe*` validators, * - default values for input parameters, * - name-value option declarations such as `opts.Field`, * - call splitting for positional and name-value arguments. * * It deliberately receives callbacks for lookup, evaluation, and error * construction so validation rules remain independent from `Interpreter.ts`. */ declare class FunctionArguments { /** * Classes currently supported by the function infrastructure. * * A future class-system implementation can expand this list without * changing the parser representation of `arguments` blocks. */ private static readonly supportedArgumentClasses; /** * Render one argument-size dimension for diagnostics. */ private static argumentSizeDimensionDisplay; /** * Render a size declaration in a MATLAB-like diagnostic form. */ static argumentSizeDisplay(dimensions: ArgumentSizeDimension[]): string; /** * Return the declared argument name, rejecting non-identifier declarations. */ static validationName(validation: NodeArgumentValidation, throwSyntaxError: ThrowSyntaxError): string; /** * Decode a name-value declaration target. * * `arguments` declarations of the form `opts.Name` are represented as field * access nodes. The returned pair identifies the parameter object and the * option field. */ static nameValueTarget(validation: NodeArgumentValidation, throwSyntaxError: ThrowSyntaxError): { parameter: string; field: string; } | undefined; /** * Return the diagnostic display name for ordinary or name-value declarations. */ static validationDisplayName(validation: NodeArgumentValidation, throwSyntaxError: ThrowSyntaxError): string; /** * Convert a size declaration to literal/symbolic dimensions. * * Numeric dimensions are fixed. Identifiers are symbolic dimensions shared * within one validation pass. `:` accepts any actual dimension. */ static literalArgumentSize(validation: NodeArgumentValidation, throwSyntaxError: ThrowSyntaxError): ArgumentSizeDimension[] | undefined; /** * Return all supported class names declared for one argument. */ static argumentClassNames(validation: NodeArgumentValidation, throwSyntaxError: ThrowSyntaxError): string[] | undefined; /** * Return a single class name when exactly one class is declared. */ static argumentClassName(validation: NodeArgumentValidation, throwSyntaxError: ThrowSyntaxError): string | undefined; /** * Format a list of class names for diagnostics. */ static argumentClassDisplay(classNames: string[]): string; /** * Parse function validators from an `arguments` declaration. * * Built-in validators are normalized to `ArgumentValidatorSpec` objects. * Unknown bare validators are treated as implicit user validators and called * with the argument value. Unknown indexed validators are treated as explicit * validator expressions and evaluated as written. */ static argumentValidators(validation: NodeArgumentValidation, throwSyntaxError: ThrowSyntaxError): ArgumentValidatorSpec[]; /** * Return the MATLAB-like size of a value. */ static valueSize(value: NodeInput): number[]; /** * Validate one built-in `mustBe*` function against an evaluated value. */ static validateArgumentFunction(name: string, value: NodeInput, validator: string, bounds: NodeInput[], throwEvalError: ThrowEvalError, throwSyntaxError: ThrowSyntaxError): void; /** * Validate one declaration against the current function workspace. * * `symbolicDimensions` is shared by declarations in the same block pass so * `(n,1)` style declarations bind `n` once and require later uses to match. * `localNamesOnly` is used for output validation so inherited names cannot * accidentally satisfy a declared but unassigned return variable. */ static validateArgumentValidation(validation: NodeArgumentValidation, symbolicDimensions: Map<string, number>, callbacks: ArgumentValidationCallbacks, localNamesOnly?: boolean, displayName?: string): void; /** * Validate all declarations for one block attribute (`Input` or `Output`). */ static validateFunctionArguments(func: NodeFunctionDefinition, targetAttribute: 'Input' | 'Output', callbacks: ArgumentValidationCallbacks, namesToValidate?: Set<string>, localNamesOnly?: boolean): void; /** * Validate `arguments (Repeating)` values grouped across `varargin`. */ static validateRepeatingArguments(func: NodeFunctionDefinition, values: NodeInput[], callbacks: RepeatingArgumentValidationCallbacks): void; /** * Validate the static consistency of all `arguments` blocks in a function. * * This is run when a function definition is registered, before any call, so * malformed declarations fail early and subsequent call-time validation can * assume the block structure is coherent. */ static validateBlocks(func: NodeFunctionDefinition, throwSyntaxError: ThrowSyntaxError): void; /** * Return parameter names that are backed by name-value declarations. */ static nameValueParameters(func: NodeFunctionDefinition, throwSyntaxError: ThrowSyntaxError): Set<string>; /** * Return name-value declarations grouped by their parameter object. */ static nameValueDeclarations(func: NodeFunctionDefinition, throwSyntaxError: ThrowSyntaxError): Map<string, Map<string, NodeArgumentValidation>>; /** * Return ordinary input-parameter default expressions. */ static inputArgumentDefaults(func: NodeFunctionDefinition, throwSyntaxError: ThrowSyntaxError): Map<string, NodeExpr>; /** * Split call arguments into positional and name-value maps. * * Supported MATLAB-like forms include `Name=value` and `'Name', value`. * Matching is case-insensitive and accepts unambiguous prefixes. Once a * name-value argument is seen, later positional arguments are rejected. */ static splitCallNameValueArguments(func: NodeFunctionDefinition, args: NodeExpr[], throwEvalError: ThrowEvalError, throwSyntaxError: ThrowSyntaxError): { positional: NodeExpr[]; named: Map<string, NodeExpr>; }; /** * Determine which declared output names must be validated. * * Only requested fixed outputs are checked. This preserves the MATLAB-like * behavior where requesting fewer outputs does not require later return * variables to be assigned. */ static outputNamesToValidate(func: NodeFunctionDefinition, requestedOutputCount: number): Set<string> | undefined; } export type { ArgumentValidatorSpec }; export { FunctionArguments }; declare const _default: { FunctionArguments: typeof FunctionArguments; }; export default _default;