UNPKG

tex2typst

Version:

JavaScript library for converting TeX code to Typst

106 lines (105 loc) 3.6 kB
/** * Adapted from jslex - A lexer in JavaScript. https://github.com/jimbojw/jslex * Licensed under MIT license */ interface ILexSpec<T> { start: Map<string, (arg0: Scanner<T>) => T | T[]>; } interface IRule<T> { re: RegExp; action: (a: Scanner<T>) => T | T[]; } export declare class Scanner<T> { private _input; private _lexer; private _pos; private _line; private _col; private _offset; private _less; private _go; private _newstate; private _state; private _text; private _leng; constructor(input: string, lexer: JSLex<T>); /** * Analogous to yytext and yyleng in lex - will be set during scan. */ text(): string | null; leng(): number | null; /** * Position of in stream, line number and column number of match. */ pos(): number; line(): number; column(): number; /** * Analogous to input() in lex. * @return {string} The next character in the stream. */ input(): string; /** * Similar to unput() in lex, but does not allow modifying the stream. * @return {int} The offset position after the operation. */ unput(): number; /** * Analogous to yyless(n) in lex - retains the first n characters from this pattern, and returns * the rest to the input stream, such that they will be used in the next pattern-matching operation. * @param {int} n Number of characters to retain. * @return {int} Length of the stream after the operation has completed. */ less(n: number): number; /** * Like less(), but instead of retaining the first n characters, it chops off the last n. * @param {int} n Number of characters to chop. * @return {int} Length of the stream after the operation has completed. */ pushback(n: number): number; /** * Similar to REJECT in lex, except it doesn't break the current execution context. * TIP: reject() should be the last instruction in a spec callback. */ reject(): void; /** * Analogous to BEGIN in lex - sets the named state (start condition). * @param {string|int} state Name of state to switch to, or ordinal number (0 is first, etc). * @return {string} The new state on successful switch, throws exception on failure. */ begin(state: string | number): string; /** * Simple accessor for reading in the current state. * @return {string} The current state. */ state(): string; /** * Scan method to be returned to caller - grabs the next token and fires appropriate calback. * @return {T} The next token extracted from the stream. */ scan(): T | T[]; } export declare class JSLex<T> { states: string[]; specification: Record<string, IRule<T>[]>; constructor(spec: ILexSpec<T>); /** * Scanner function - makes a new scanner object which is used to get tokens one at a time. * @param {string} input Input text to tokenize. * @return {function} Scanner function. */ scanner(input: string): Scanner<T>; /** * Similar to lex's yylex() function, consumes all input, calling calback for each token. * @param {string} input Text to lex. * @param {function} callback Function to execute for each token. */ lex(input: string, callback: (arg0: T | T[]) => void): void; /** * Consumes all input, collecting tokens along the way. * @param {string} input Text to lex. * @return {array} List of tokens, may contain an Error at the end. */ collect(input: string): T[]; } export {};