UNPKG

@bruju/automata-composer

Version:

Basic finite state automata building by composition

122 lines (121 loc) 4.37 kB
/** Value used to represent epsilon */ export declare const EPSILON: null; /** * An helper class to build non deterministic finite state automata with epsilon * transitions by composing automatas */ export declare class AutomataComposer { /** Id of the starting state */ readonly start: number; /** Id of the ending state */ readonly end: number; /** All transitions */ readonly transitions: Transition[]; /** * Build a new automata composer * @param start Starting state * @param end Ending state * @param transitions List of transitions */ constructor(start: number, end: number, transitions: Transition[]); build(): Automata; } /** * Build an automata with only one transition * @param symbol The symbol on the transition * @returns The automata */ export declare function unit(symbol: string): AutomataComposer; /** * Build an automata that recognizes both the language in lhs and the language * in rhs * @param lhs The first language * @param rhs The second language * @returns An automata that recognizes both languages */ export declare function or(lhs: AutomataComposer, rhs: AutomataComposer): AutomataComposer; /** * Build an automata that recognizes the language described by the sequence * of given automatas * @param lhs The first part * @param rhs The second part * @returns lhs -eps-> rhs */ export declare function chain(...subAutomatas: AutomataComposer[]): AutomataComposer; /** Builds the automata (self)? */ export declare function maybe(self: AutomataComposer): AutomataComposer; /** Builds the automata (self)+ */ export declare function plus(self: AutomataComposer): AutomataComposer; /** Builds the automata (self)* */ export declare function star(self: AutomataComposer): AutomataComposer; /** * Build an automata where all symbols has been modified */ export declare function modifyTransitions(self: AutomataComposer, modifier: (symbol: string) => string): AutomataComposer; /** * Inverse the automata state * @param self The base automata * @param modifier A function to apply to the transition names. Default value * is the identity function (i.e. the transitions are not changed) * @returns An automata composer that is the inverse of the original one */ export declare function inverse(self: AutomataComposer, modifier?: (symbol: string) => string): AutomataComposer; /** A transition */ export declare type Transition = { /** Starting state id */ readonly from: number; /** Symbol used to travel through the transition */ readonly symbol: string | typeof EPSILON; /** Destination state id */ readonly to: number; }; /** A finite state automata */ export declare class Automata { /** Starting state */ readonly start: State; /** Validating states */ readonly ends: State[]; /** All existing states */ readonly states: State[]; /** * Builds a new finite state automata * @param start The starting node * @param ends The end node * @param allStates Every known states. start and ends should be included in * it. */ constructor(start: State, ends: State[], allStates: State[]); /** * Test if the given sequence is accepted by the automata. * @param sequence The sequence * @returns True if from the starting node, the given sequence can be used * to reach one of the end of the automata. */ test(sequence: string[]): boolean; /** * Gives a string representation of the automata */ toLongString(): string; } /** * A state in a finite state automata */ export declare class State { /** A unique id */ readonly id: number; /** The list of transitions */ readonly transitions: Map<string, State>; /** * Builds a new state * @param id The id of the state * @param transitions The list of transitions */ constructor(id: number, transitions: Map<string, State>); /** * Returns the state after travelling through the given symbol * @param symbol The symbol used * @returns The state at the end of the transition corresponding to the given * symbol, or null if there is no such state */ next(symbol: string): State | null; }