UNPKG

docutils-ts

Version:

Port of the Python Docutils library to TypeScript

102 lines (101 loc) 4.53 kB
import StringList from "./stringList.js"; import { ContextKind, CoreLanguage, DebugFunction, ObserverCallback, ReporterInterface, Statefactory, StateInterface, StatemachineInterface, StateMachineConstructorArgs, TransitionsArray, StateConstructor, LoggerType, ElementInterface } from "./types.js"; import RSTStateMachine from "./parsers/rst/rstStateMachine.js"; export declare class StateMachineError extends Error { } export declare class UnknownStateError extends StateMachineError { } export declare class TransitionPatternNotFound extends StateMachineError { } export declare class TransitionMethodNotFound extends StateMachineError { } /** * A finite state machine for text filters using regular expressions. * * The input is provided in the form of a list of one-line strings (no * newlines). States are subclasses of the `State` class. Transitions consist * of regular expression patterns and transition methods, and are defined in * each state. * * The state machine is started with the `run()` method, which returns the * results of processing in a list. * */ declare class StateMachine implements StatemachineInterface { hasState(stateName: string): boolean; getState2(stateName: string): StateInterface; private states; inputLines: StringList; debugFn: DebugFunction; debug: boolean; stateFactory?: Statefactory; lineOffset: number; line: string; inputOffset: number; node?: ElementInterface; language?: CoreLanguage; reporter?: ReporterInterface; private observers; protected currentState?: string; protected initialState?: string; logger: LoggerType; constructor(args: StateMachineConstructorArgs); createStateMachine(rstStateMachine: RSTStateMachine, initialState?: string, stateFactory?: Statefactory | undefined): StatemachineInterface; unlink(): void; /** * Run the state machine on `input_lines`. Return results (a list). * * Reset `self.line_offset` and `self.current_state`. Run the * beginning-of-file transition. Input one line at a time and check for a * matching transition. If a match is found, call the transition method * and possibly change the state. Store the context returned by the * transition method to be passed on to the next transition matched. * Accumulate the results returned by the transition methods in a list. * Run the end-of-file transition. Finally, return the accumulated * results. * * Parameters: * * - `input_lines`: a list of strings without newlines, or `StringList`. * - `input_offset`: the line offset of `input_lines` from the beginning * of the file. * - `context`: application-specific storage. * - `input_source`: name or path of source of `input_lines`. * - `initial_state`: name of initial state. */ run(inputLines: StringList | string | string[], inputOffset: number, runContext?: ContextKind, inputSource?: string, initialState?: string, ...rest: any[]): (string | {})[]; /** * Return current state object; set it first if * `next_state` given. Parameter `next_state`: a string, * the name of the next state. Exception: * `UnknownStateError` raised if `next_state` unknown. */ getState(nextState?: string): StateInterface; /** Load `self.line` with the `n`'th next line and return it. */ nextLine(n?: number): string | undefined; isNextLineBlank(): boolean; atEof(): boolean; atBof(): boolean; previousLine(n?: number): string; gotoLine(lineOffset: number): string | undefined; getSource(lineOffset: number): string | undefined; absLineOffset(): number; absLineNumber(): number; getSourceAndLine(lineno?: number): [string | undefined, number | undefined]; insertInput(inputLines: StringList, source: string): void; getTextBlock(flushLeft?: boolean): StringList; checkLine(context: {}[], state: StateInterface, transitions?: TransitionsArray): [{}[], (string | StateInterface | undefined), {}[]]; addState(stateClass: StateConstructor): void; addStates(stateClasses: StateConstructor[]): void; runtimeInit(): void; error(): void; attachObserver(observer: ObserverCallback): void; detachObserver(observer: ObserverCallback): void; notifyObservers(): void; } export declare function string2lines(astring?: string, args?: { tabWidth?: number; convertWhitespace?: boolean; whitespace?: RegExp | string; }): string[]; export { StateMachine };