UNPKG

docutils-ts

Version:

Port of the Python Docutils library to TypeScript

79 lines (78 loc) 3.94 kB
import { ReporterInterface, StateInterface, StatemachineInterface, StateMachineFactoryFunction, TransitionFunction, Transitions, TransitionsArray, Patterns, LoggerType, ContextArray } from "../types.js"; import { StateMachine } from "../stateMachine.js"; /** * State superclass. Contains a list of transitions, and transition methods. * * Transition methods all have the same signature. They take 3 parameters: * * - An `re` match object. ``match.string`` contains the matched input line, * ``match.start()`` gives the start index of the match, and * ``match.end()`` gives the end index. * - A context object, whose meaning is application-defined (initial value * ``None``). It can be used to store any information required by the state * machine, and the retured context is passed on to the next transition * method unchanged. * - The name of the next state, a string, taken from the transitions list; * normally it is returned unchanged, but it may be altered by the * transition method if necessary. * * Transition methods all return a 3-tuple: * * - A context object, as (potentially) modified by the transition method. * - The next state name (a return value of ``None`` means no state change). * - The processing result, a list, which is accumulated by the state * machine. * * Transition methods may raise an `EOFError` to cut processing short. * * There are two implicit transitions, and corresponding transition methods * are defined: `bof()` handles the beginning-of-file, and `eof()` handles * the end-of-file. These methods have non-standard signatures and return * values. `bof()` returns the initial context and results, and may be used * to return a header string, or do any other processing needed. `eof()` * should handle any remaining context and wrap things up; it returns the * final processing result. * * Typical applications need only subclass `State` (or a subclass), set the * `patterns` and `initial_transitions` class attributes, and provide * corresponding transition methods. The default object initialization will * take care of constructing the list of transitions. * */ declare class State implements StateInterface { /** * {Name: pattern} mapping, used by `make_transition()`. Each pattern may * be a string or a compiled `re` pattern. Override in subclasses. */ patterns: Patterns; /** * A list of transitions to initialize when a `State` is instantiated. * Each entry is either a transition name string, or a (transition name, next * state name) pair. See `make_transitions()`. Override in subclasses. */ protected initialTransitions?: (string | string[])[]; createNestedStateMachine?: StateMachineFactoryFunction<StatemachineInterface>; createKnownIndentStateMachine?: StateMachineFactoryFunction<StatemachineInterface>; createIndentStateMachine?: StateMachineFactoryFunction<StatemachineInterface>; protected debug?: boolean; transitionOrder: string[]; transitions: Transitions; protected reporter?: ReporterInterface; protected stateMachine?: StateMachine; protected logger: LoggerType; constructor(stateMachine: StateMachine, debug?: boolean); runtimeInit(): void; unlink(): void; addInitialTransitions(): void; addTransitions(names: string[], transitions: Transitions): void; addTransition(name: string, transition: any): void; removeTransition(name: string): void; makeTransition(name: string, nextState?: any): [RegExp, TransitionFunction, string]; makeTransitions(nameList: (string | string[])[]): [string[], Transitions]; noMatch(context: any[], transitions: TransitionsArray | undefined): [{}[], (string | StateInterface | undefined), {}[]]; bof(context: string[]): string[][]; eof(context: ContextArray): ContextArray; nop(match: {}, context: {}[], nextState: {}): {}[]; stateName: string; } export default State;