interface Parser {
    atEnd: boolean;
    computeEngine?: IComputeEngine;
    index: number;
    options: Required<ParseLatexOptions>;
    peek: string;
    get atBoundary(): boolean;
    addBoundary(boundary: string[]): void;
    atTerminator(t: undefined | Terminator): boolean;
    boundaryError(msg: string | [string, ...Expression[]]): Expression;
    error(code: string | [string, ...Expression[]], fromToken: number): Expression;
    latex(start: number, end?: number): string;
    match(token: string): boolean;
    matchAll(tokens: string[]): boolean;
    matchAny(tokens: string[]): string;
    matchBoundary(): boolean;
    matchChar(): null | string;
    nextToken(): string;
    parseArguments(kind?: "implicit" | "enclosure", until?: Terminator): null | Expression[];
    parseExpression(until?: Partial<Terminator>): null | Expression;
    parseGroup(): null | Expression;
    parseOptionalGroup(): null | Expression;
    parsePostfixOperator(lhs: null | Expression, until?: Partial<Terminator>): null | Expression;
    parseStringGroup(optional?: boolean): null | string;
    parseSymbol(until?: Partial<Terminator>): null | Expression;
    parseTabular(): null | Expression[][];
    parseToken(): null | Expression;
    removeBoundary(): void;
    skipSpace(): boolean;
    skipVisualSpace(): void;
}

Properties

atEnd: boolean

True if the last token has been reached. Consider also atTerminator().

computeEngine?: IComputeEngine
index: number

The index of the current token

peek: string

Return the next token, without advancing the index

Accessors

  • get atBoundary(): boolean
  • Returns boolean

Methods

  • Boundaries are used to detect the end of an expression.

    They are used for unusual syntactic constructs, for example \int \sin x dx where the dx is not an argument to the \sin function, but a boundary of the integral.

    They are also useful when handling syntax errors and recovery.

    For example, \begin{bmatrix} 1 & 2 { \end{bmatrix} has an extraneous {, but the parser will attempt to recover and continue parsing when it encounters the \end{bmatrix} boundary.

    Parameters

    • boundary: string[]

    Returns void

  • Return true if the terminator condition is met or if the last token has been reached.

    Parameters

    Returns boolean

  • Parameters

    Returns Expression

  • Return an error expression with the specified code and arguments

    Parameters

    • code: string | [string, ...Expression[]]
    • fromToken: number

    Returns Expression

  • Return a string representation of the expression between start and end (default: the whole expression)

    Parameters

    • start: number
    • Optionalend: number

    Returns string

  • If the next token matches the target advance and return true. Otherwise return false

    Parameters

    • token: string

    Returns boolean

  • Return true if the next tokens match the argument, an array of tokens, or null otherwise

    Parameters

    • tokens: string[]

    Returns boolean

  • Return the next token if it matches any of the token in the argument or null otherwise

    Parameters

    • tokens: string[]

    Returns string

  • Returns boolean

  • If the next token is a character, return it and advance the index This includes plain characters (e.g. 'a', '+'...), characters defined in hex (^^ and ^^^^), the \char and \unicode command.

    Returns null | string

  • Return the next token and advance the index

    Returns string

  • Parse an argument list, for example: (12, x+1) or \left(x\right)

    • 'enclosure' : will look for arguments inside an enclosure (an open/close fence) (default)
    • 'implicit': either an expression inside a pair of (), or just a primary (i.e. we interpret \cos x + 1 as \cos(x) + 1)

    Return an array of expressions, one for each argument, or null if no argument was found.

    Parameters

    • Optionalkind: "implicit" | "enclosure"
    • Optionaluntil: Terminator

    Returns null | Expression[]

  • Parse an expression:

    <expression> ::=
    | <primary> ( <infix-op> <expression> )?
    | <prefix-op> <expression>

    <primary> :=
    (<number> | <symbol> | <function-call> | <matchfix-expr>)
    (<subsup> | <postfix-operator>)*

    <matchfix-expr> :=
    <matchfix-op-open> <expression> <matchfix-op-close>

    <function-call> ::=
    | <function><matchfix-op-group-open><expression>[',' <expression>]<matchfix-op-group-close>

    This is the top-level parsing entry point.

    Stop when an operator of precedence less than until.minPrec or the sequence of tokens until.tokens is encountered

    until is { minPrec:0 } by default.

    Parameters

    Returns null | Expression

  • Parse an expression in aLaTeX group enclosed in curly brackets {}. These are often used as arguments to LaTeX commands, for example \frac{1}{2}.

    Return null if none was found Return ['Sequence'] if an empty group {} was found

    Returns null | Expression

  • Parse an expression enclosed in a LaTeX optional group enclosed in square brackets [].

    Return null if none was found.

    Returns null | Expression

  • Parse a postfix operator, such as ' or !.

    Prefix, infix and matchfix operators are handled by parseExpression()

    Parameters

    Returns null | Expression

  • Some LaTeX commands have arguments that are not interpreted as expressions, but as strings. For example, \begin{array}{ccc} (both array and ccc are strings), \color{red} or \operatorname{lim sup}.

    If the next token is the start of a group ({), return the content of the group as a string. This may include white space, and it may need to be trimmed at the start and end of the string.

    LaTeX commands are typically not allowed inside a string group (for example, \alpha would result in an error), but we do not enforce this.

    If optional is true, this should be an optional group in square brackets otherwise it is a regular group in braces.

    Parameters

    • Optionaloptional: boolean

    Returns null | string

  • A symbol can be:

    • a single-letter identifier: x
    • a single LaTeX command: \pi
    • a multi-letter identifier: \operatorname{speed}

    Parameters

    Returns null | Expression

  • Parse an expression in a tabular format, where rows are separated by \\ and columns by &.

    Return rows of sparse columns: empty rows are indicated with Nothing, and empty cells are also indicated with Nothing.

    Returns null | Expression[][]

  • Some LaTeX commands (but not all) can accept arguments as single tokens (i.e. without braces), for example ^2, \sqrt3 or \frac12

    This argument will usually be a single token, but can be a sequence of tokens (e.g. \sqrt\frac12 or \sqrt\operatorname{speed}).

    The following tokens are excluded from consideration in order to fail early when encountering a likely syntax error, for example x^(2) instead of x^{2}. With ( in the list of excluded tokens, the match will fail and the error can be recovered.

    The excluded tokens include !"#$%&(),/;:?@[]|~", \left, \bigl, etc...

    Returns null | Expression

  • Returns void

  • If there are any space, advance the index until a non-space is encountered

    Returns boolean

  • Skip over "visual space" which includes space tokens, empty groups {}, and commands such as \, and \!

    Returns void