functionalscript
Version:
FunctionalScript is a purely functional subset of JavaScript
64 lines (63 loc) • 2.45 kB
TypeScript
import { type CodePoint } from '../../text/utf16/module.f.ts';
import { type RangeMapArray } from '../../types/range_map/module.f.ts';
import { type Rule as FRule } from '../module.f.ts';
export type TerminalRange = number;
export type Sequence = readonly string[];
/** A variant of rule names. */
export type Variant = {
readonly [k in string]: string;
};
export type Rule = Variant | Sequence | TerminalRange;
/** The full grammar */
export type RuleSet = Readonly<Record<string, Rule>>;
type EmptyTag = string | true | undefined;
type EmptyTagEntry = string | boolean;
type DispatchRule = {
readonly emptyTag: EmptyTag;
readonly rangeMap: Dispatch;
};
type Dispatch = RangeMapArray<DispatchResult>;
type DispatchResult = DispatchRuleCollection | null;
type DispatchRuleOrName = DispatchRule | string;
type DispatchRuleCollection = {
readonly tag: string | undefined;
readonly rules: DispatchRuleOrName[];
};
type DispatchMap = {
readonly [id in string]: DispatchRule;
};
type EmptyTagMap = {
readonly [id in string]: EmptyTagEntry;
};
export type DescentMatchRule = (r: Rule, s: readonly CodePoint[], idx: number) => MatchResult;
/**
* Represents a parsed Abstract Syntax Tree (AST) sequence.
*/
export type AstSequence = readonly (AstRule | CodePoint)[];
type AstTag = string | true | undefined;
/**
* Represents a parsed AST rule, consisting of a rule name and its parsed sequence.
*/
type AstRule = {
readonly tag: AstTag;
readonly sequence: AstSequence;
};
/**
* Represents the remaining input after a match attempt, or `null` if no match is possible.
*/
export type Remainder = readonly CodePoint[] | null;
/**
* Represents the result of a match operation, including the parsed AST rule and the remainder of the input.
*/
export type MatchResult = readonly [AstRule, boolean, Remainder];
/**
* Represents an LL(1) parser function for matching input against grammar rules.
*/
export type Match = (name: string, s: readonly CodePoint[]) => MatchResult;
export type MatchRule = (dr: DispatchRule, s: readonly CodePoint[]) => MatchResult;
export declare const toData: (fr: FRule) => readonly [RuleSet, string];
export declare const dispatchMap: (ruleSet: RuleSet) => DispatchMap;
export declare const createEmptyTagMap: (data: readonly [RuleSet, string]) => EmptyTagMap;
export declare const parser: (fr: FRule) => Match;
export declare const parserRuleSet: (ruleSet: RuleSet) => Match;
export {};