/** * Multi-statement parser for domain DSLs. * * Provides generic mechanics for parsing multi-line/multi-statement DSL input: * - Statement splitting (line-based or delimiter-based) * - Keyword classification (SVO start / SOV end detection) * - Block accumulation (grouping lines into blocks) * - Error collection with line numbers * * Domains provide the semantics (what keywords mean, preprocessing, hierarchy). * The framework provides the mechanics. */ import type { SemanticNode } from '../core/types'; import type { MultilingualDSL } from '../api/create-dsl'; /** * Word order affects keyword detection position. */ export type WordOrderHint = 'SVO' | 'SOV' | 'VSO'; /** * How to split input into individual statements. */ export interface SplitConfig { /** * Split mode: * - 'line': split on newlines (for indentation-based DSLs) * - 'delimiter': split on language-specific delimiters */ readonly mode: 'line' | 'delimiter'; /** * Language-specific delimiter patterns (used when mode='delimiter'). * Key is language code, value is regex pattern. * Example: { en: /,\s*|\n\s*‍/, ja: /、|。|\n\s*‍/ } */ readonly delimiters?: Readonly>; /** * Default delimiter when language not in delimiters map. */ readonly defaultDelimiter?: RegExp; /** * Whether to trim each statement after splitting. * Default: true */ readonly trim?: boolean; /** * Comment prefixes to strip. Lines starting with these are skipped. * Default: ['--', '//'] */ readonly commentPrefixes?: readonly string[]; } /** * A keyword category with its translations. * Maps language code to array of keyword variants. */ export type KeywordMap = Readonly>; /** * Configuration for keyword classification. */ export interface KeywordConfig { /** * Named keyword categories with their per-language translations. * Example: { test: { en: ['test'], ja: ['テスト'], es: ['prueba'] } } */ readonly categories: Readonly>; /** * Word order hint per language (affects where keywords are detected). * Default for unlisted languages: 'SVO' */ readonly wordOrders?: Readonly>; } /** * Configuration for continuation resolution (e.g., BDD's "and" keyword). */ export interface ContinuationConfig { /** * The keyword(s) that signal continuation, per language. * Example: { en: ['and'], es: ['y'], ja: ['かつ'], ar: ['و'] } */ readonly keywords: KeywordMap; /** * How to resolve continuation: re-prefix with previous step's keyword. * The function receives (content after 'and', previous step's category, language) * and returns the re-prefixed string for re-parsing. * * Default: `(content, prevCategory, language, categoryKeywords) => * categoryKeywords[prevCategory]?.[language]?.[0] + ' ' + content` */ readonly resolve?: (content: string, prevCategory: string, language: string, categoryKeywords: Record) => string; } /** * A parsed statement with its line context. */ export interface ParsedStatement { /** The parsed semantic node */ readonly node: SemanticNode; /** Original source line/text */ readonly source: string; /** Line number (1-based) in original input */ readonly line: number; /** Detected keyword category (e.g., 'test', 'given', 'when') */ readonly category?: string; /** Indentation level (number of leading spaces, 0-based) */ readonly indent: number; } /** * A parse error with location information. */ export interface StatementError { /** Error message */ readonly message: string; /** Line number (1-based) in original input */ readonly line: number; /** Original source line/text */ readonly source: string; /** Error code for programmatic handling */ readonly code?: 'parse-error' | 'unexpected-line' | 'continuation-error'; } /** * Result of multi-statement parsing. */ export interface MultiStatementResult { /** Successfully parsed statements */ readonly statements: readonly ParsedStatement[]; /** Errors encountered during parsing */ readonly errors: readonly StatementError[]; } /** * Preprocessor function called before each statement is parsed. * Returns the modified string to parse, or null to skip the line. */ export type StatementPreprocessor = (line: string, category: string | undefined, language: string, context: PreprocessorContext) => string | null; /** * Context available to preprocessors. */ export interface PreprocessorContext { /** The previous parsed statement, if any */ readonly previous?: ParsedStatement; /** Line number (1-based) */ readonly lineNumber: number; /** Indentation level */ readonly indent: number; } /** * Full configuration for multi-statement parser. */ export interface MultiStatementConfig { /** How to split input into statements */ readonly split: SplitConfig; /** Keyword classification (optional — if omitted, no category detection) */ readonly keywords?: KeywordConfig; /** Continuation resolution (optional — e.g., BDD 'and') */ readonly continuation?: ContinuationConfig; /** Statement preprocessor (optional — for article stripping, expect-prepending, etc.) */ readonly preprocessor?: StatementPreprocessor; } /** * A reusable multi-statement parser. * Created via `createMultiStatementParser()`. */ export interface MultiStatementParser { /** * Parse multi-statement input into an array of parsed statements. */ parse(input: string, language: string): MultiStatementResult; } /** * Create a multi-statement parser that wraps a `MultilingualDSL` instance. * * The parser handles splitting, keyword detection, continuation resolution, * preprocessing, and error collection. It delegates single-statement parsing * to the provided DSL's `parse()` method. * * @example * ```typescript * const parser = createMultiStatementParser(myDSL, { * split: { mode: 'line', commentPrefixes: ['--', '//'] }, * keywords: { * categories: { * given: { en: ['given'], ja: ['前提'], es: ['dado'] }, * when: { en: ['when'], ja: ['もし'], es: ['cuando'] }, * then: { en: ['then'], ja: ['ならば'], es: ['entonces'] }, * }, * wordOrders: { ja: 'SOV', ar: 'VSO' }, * }, * continuation: { * keywords: { en: ['and'], es: ['y'], ja: ['かつ'] }, * }, * }); * * const result = parser.parse(` * given #login is visible * when click on #submit * then #dashboard appears * `, 'en'); * ``` */ export declare function createMultiStatementParser(dsl: MultilingualDSL, config: MultiStatementConfig): MultiStatementParser; /** * A block of accumulated statements, grouped by a block-opening keyword. */ export interface StatementBlock { /** Block type (e.g., 'test', 'feature', 'scenario') */ readonly type: string; /** Block name, if any (e.g., test name from quotes) */ readonly name?: string; /** Statements within this block */ readonly statements: readonly ParsedStatement[]; /** Child blocks (for nested structures) */ readonly children: readonly StatementBlock[]; /** Starting line number */ readonly line: number; /** Indentation level of the block opener */ readonly indent: number; /** Domain-specific metadata */ readonly meta?: TMeta; } /** * Configuration for block accumulation. */ export interface BlockConfig { /** * Block-opening keyword categories. * Statements classified in these categories start new blocks. */ readonly blockTypes: readonly string[]; /** * How to determine block nesting: * - 'indent': indentation-based (deeper indent = child block) * - 'flat': all blocks at same level (no nesting) */ readonly nesting: 'indent' | 'flat'; /** * Extract block name from statement text. * Default: extracts quoted string after keyword (e.g., test "Login" → "Login") */ readonly extractName?: (text: string, category: string) => string | undefined; } /** * Result of block accumulation. */ export interface BlockResult { /** Top-level blocks */ readonly blocks: readonly StatementBlock[]; /** Statements not inside any block */ readonly orphans: readonly ParsedStatement[]; } /** * Accumulate parsed statements into blocks based on keyword categories. * * This groups statements into hierarchical blocks based on block-opening keywords * and indentation levels. Useful for test/feature/scenario structures. * * @example * ```typescript * const result = accumulateBlocks(statements, { * blockTypes: ['test', 'feature', 'setup'], * nesting: 'indent', * extractName: (text, category) => { * const match = text.match(/"([^"]+)"/); * return match?.[1]; * }, * }); * ``` */ export declare function accumulateBlocks(statements: readonly ParsedStatement[], config: BlockConfig): BlockResult; //# sourceMappingURL=multi-statement.d.ts.map