export type Token = { pattern: string | RegExp; name: string; source: string; test: (v: string) => boolean; }; /** * Result tuple for a Grammar: * Success: [value, null] * Failure: [position, expectedToken] */ export type GrammarResult = [T, null] | [number, Token]; /** * Grammar is a function from parsing Context to GrammarResult. */ export type Grammar = (ctx: Context) => GrammarResult; /** * Parsing context shared across grammar invocations. */ interface Context { input: string; tokens: string[]; pos: number; skipRule: Grammar | null; } /** Utility: extract the value type from a Grammar */ export type UnwrapGrammar = G extends Grammar ? V : never; /** Utility: tuple value types -> union */ type UnionOf[]> = { [K in keyof R]: UnwrapGrammar; }[number]; /** Create a token definition */ export declare function createToken(pattern: string | RegExp, name?: string): Token; export declare const EOF: Token; export declare const UNEXPECTED: Token; export declare const INFINITE_LOOP: Token; /** * Sequence (AND) combinator. * When no transform is provided the resulting value is a tuple whose * element types correspond to each rule's produced value. */ export declare function and[]>(rules: [...R]): Grammar<{ [K in keyof R]: UnwrapGrammar; }>; export declare function and[], T>(rules: [...R], transform: (values: { [K in keyof R]: UnwrapGrammar; }) => T): Grammar; /** * Consume a specific token (forward). */ export declare function consume(token: Token, transform: (v: string) => T): Grammar; export declare function consume(token: Token): Grammar; /** * Consume a specific token looking behind current position (backwards). * On success moves one position back. */ export declare function consumeBehind(token: Token, transform: (v: string) => T): Grammar; export declare function consumeBehind(token: Token): Grammar; /** * Consume and collect raw string chunks until a sentinel token or rule * is reached. The sentinel token / rule is NOT consumed (lookahead). * - If sentinel is EOF, all remaining chunks are gathered. * - If sentinel is a Grammar, success when that parser matches at the * current position (without consuming). * - If sentinel is a Token, success when that token matches; failure * when EOF is reached before that token is found. */ export declare function consumeUntil(target: Token | Grammar, transform: (vs: string[]) => T): Grammar; export declare function consumeUntil(target: Token | Grammar): Grammar; /** * OR combinator. * Produces a union of the rule value types (or transform output). */ export declare function or[]>(rules: [...R]): Grammar>; export declare function or[], T>(rules: [...R], transform: (value: UnionOf) => T): Grammar; /** * zeroOrMany combinator. */ export declare function zeroOrMany(rule: Grammar, transform: (vs: V[]) => T, until?: Grammar): Grammar; export declare function zeroOrMany(rule: Grammar, transform?: undefined, until?: Grammar): Grammar; /** * zeroOrMany with separator. */ export declare function zeroOrManySep(rule: Grammar, sep: Grammar, transform: (vs: V[]) => T, until?: Grammar): Grammar; export declare function zeroOrManySep(rule: Grammar, sep: Grammar, transform?: undefined, until?: Grammar): Grammar; /** * oneOrMany combinator. */ export declare function oneOrMany(rule: Grammar, transform: (vs: V[]) => T, until?: Grammar): Grammar; export declare function oneOrMany(rule: Grammar, transform?: undefined, until?: Grammar): Grammar; /** * oneOrMany with separator. */ export declare function oneOrManySep(rule: Grammar, sep: Grammar, transform: (vs: V[]) => T, until?: Grammar): Grammar; export declare function oneOrManySep(rule: Grammar, sep: Grammar, transform?: undefined, until?: Grammar): Grammar; /** * zeroOrOne combinator. */ export declare function zeroOrOne(rule: Grammar, transform: (v: V | undefined) => T): Grammar; export declare function zeroOrOne(rule: Grammar, transform?: undefined): Grammar; /** * Peek (lookahead) - value is preserved, position is restored. */ export declare function peek(rule: Grammar): Grammar; /** * Negative lookahead. Succeeds when inner rule fails. * Always produces null as its value. */ export declare function not(rule: Grammar): Grammar; /** * Apply a temporary skip rule inside another rule. */ export declare function skipIn(skip: Grammar | null, rule: Grammar): Grammar; /** * Wrap a raw rule accessor (for potential lazy evaluation) while preserving its exact Grammar type. * Using a higher-kinded generic inference based on the function's own return type * avoids collapsing the inner value type to 'any' when the rawRules object * becomes contextually typed. */ export declare function rule(r: () => Grammar): Grammar; /** * Overload 1 (broad) - keeps precise return types of each rule without prematurely widening. */ export declare function createParser(tokens: Token[], rawRules: T, skipFactory?: () => Grammar): (key: K, input: string) => T[K] extends () => Grammar ? V : never; export {}; //# sourceMappingURL=nanolex.d.ts.map