/** * Linearized match slot types for frame-based pattern matching. * * Similar to bindingSlot.ts, but supports: * - Pattern match failure (literal mismatch, type mismatch) * - Literal pattern evaluation * - Sequential slot processing with early exit on failure */ import type { Any, Arr, Obj } from '../interface'; import type { AstNode, BindingTarget } from '../parser/types'; import type { BindingPathStep } from './bindingSlot'; /** * A linearized match slot representing one step in pattern matching. * * Each slot either: * - Binds a value to a name (symbol pattern) * - Compares a value to a literal (literal pattern) * - Validates type at a path (for nested object/array patterns) * - Collects rest values * * Processing a slot can: * - Succeed and continue to next slot * - Fail (return null to indicate pattern doesn't match) * - Need to evaluate a node (literal or default) */ export interface MatchSlot { /** Type of slot */ kind: 'bind' | 'literal' | 'typeCheck' | 'rest' | 'wildcard'; /** Variable name to bind (for 'bind' and 'rest' kinds) */ name?: string; /** Path to extract value from root */ path: BindingPathStep[]; /** Default expression if value is undefined (for 'bind' kind) */ defaultNode?: AstNode; /** Literal node to evaluate and compare (for 'literal' kind) */ literalNode?: AstNode; /** Required type at this path (for 'typeCheck' kind) */ requiredType?: 'object' | 'array'; /** For object rest: keys to exclude */ restKeys?: Set; /** For array rest: starting index */ restIndex?: number; /** Node ID for error reporting (resolve via source map) */ nodeId?: number; } /** * Result of flattening a match pattern. * Includes both the slots and length constraints for array patterns. */ export interface FlattenedMatchPattern { slots: MatchSlot[]; /** For array patterns: exact length required (if no rest) */ exactLength?: number; /** For array patterns: minimum length required (if has rest) */ minLength?: number; } /** * Flatten a binding pattern into a linear list of match slots. * Returns slots in order they should be processed. * * Type checks come first (to fail fast on type mismatch). * Then binding/literal slots in depth-first order. */ export declare function flattenMatchPattern(target: BindingTarget): MatchSlot[]; /** * Extract a value from a nested structure by following a path. * Returns undefined if path cannot be followed. */ export declare function extractMatchValueByPath(rootValue: Any, path: BindingPathStep[]): Any | undefined; /** * Check if value matches required type at path. */ export declare function checkTypeAtPath(rootValue: Any, path: BindingPathStep[], requiredType: 'object' | 'array'): boolean; /** * Extract rest values for object rest. */ export declare function extractMatchObjectRest(value: Any, path: BindingPathStep[], restKeys: Set): Obj; /** * Extract rest values for array rest. */ export declare function extractMatchArrayRest(value: Any, path: BindingPathStep[], restIndex: number): Arr; /** * Check array length constraints at pattern root. * For patterns like [a, b, c] - exact match required. * For patterns like [a, b, ...rest] - minimum match required. */ export declare function checkArrayLengthConstraint(target: BindingTarget, value: Any): boolean; /** * Check object exists at pattern root. */ export declare function checkObjectTypeConstraint(target: BindingTarget, value: Any): boolean;