Fork me on GitHub


core/parser# Parser

new Parser(tokens: InputToken[], args?: [string])private

A parser transforms a list of tokens into a list of MathAtom.

tokens : InputToken[]

An array of tokens generated by the lexer.

args : [string]

An optional list of arguments. #n tokens will be substituted with the corresponding element in the args object. This is used when parsing macros.

macros : [string]:string optional

Optional macro definitions.

tokens : InputToken[]

An array of tokens generated by the lexer.

args : [string]

Optional arguments to substitute the # token.

macros : [string]:string

A dictionary of objects, index by the name of the macro, with the following keys:

  • args: an integer, the number of arguments, default 0. They can be referenced as #0, #1, #2... inside the definition of the macro
  • def: a string, the definition of the macro, which can reference other macros
index : number

The current token to be parsed: index in this.tokens

mathList : MathAtom[]

Accumulated result of the parsing by parseAtom()

parseMode : string

The parse mode indicates the syntax rules to use to parse the upcoming tokens. Valid values include:

  • 'math': spaces are ignored, math functions are allowed
  • 'text': spaces are accounted for, math functions are ignored
  • 'string'
  • 'color': color name, hex value: '#fff', '#a0a0a0'
  • 'number': +/-12.56
  • 'dimen': '25mu', '2pt'
  • 'skip': '25mu plus 2em minus fiLll', '2pt'
  • 'colspec': formating of a column in tabular environment, e.g. '[email protected]{.}l'
tabularMode : boolean

When in tabular mode, '&' is interpreted as a column separator and '\' as a row separator. Used for matrixes, etc...

endCount : number

Counter to prevent deadlock. If end() is called too many times (1,000) in a row for the same token, bail.



True if we've reached the end of the token stream.

hasLiteralPattern(pattern: RegEx): boolean

pattern : RegEx
: boolean   

True if the next token is of type 'literal and matches the specified regular expression pattern.

hasToken(type: string): boolean

type : string
: boolean   

True if the next token is of the specified type


Make a MathAtom for the current token or token group and add it to the parser's current mathList

parseKeyword(keyword: string): booleanprivate

Keywords are used to specify dimensions, and for various other syntactic constructs. Unlike commands, they are not case sensitive. There are 25 keywords: at by bp cc cm dd depth em ex fil fill filll height in minus mm mu pc plus pt sp spread to true width

TeX: 8212

keyword : string
: boolean   

true if the expected keyword is present


Parse a \limits or \nolimits command.

This will change the placement of limits to be either above or below (if \limits) or in the superscript/subscript position (if \nolimits).

This overrides the calculation made for the placement, which is usually dependent on the displaystyle (inlinemath prefers \nolimits, while displaymath prefers \limits).

parseSupSub(): MathAtomprivate

Parse a subscript/superscript: ^ and _.

Modify the last atom accordingly.

: MathAtom   

parseToken(type: string)

type : string

scanArg(parseMode?: string)private

Parse a math field, an argument to a function.

An argument can either be a single atom or a sequence of atoms enclosed in braces.

parseMode : string

Temporarily overrides the parser parsemode. For example: 'dimension', 'color', 'text', etc...


Return a CSS color (#rrggbb)

scanDimen(): numberprivate

Return as a floating point number a dimension in pt (1 em = 10 pt)

See TeX:8831

: number   


Parse a \begin{env}...\end{end} sequence

scanGroup(): MathAtomprivate

Parse a group enclosed in a pair of braces: {...}.

Return either a group MathAtom or null if not a group.

Return a group MathAtom with an empty body if an empty group (i.e. {}).

: MathAtom   

scanImplicitGroup(done?: function): MathAtom[]private

Parse a sequence terminated with a group end marker, such as }, \end, &, etc... Returns an array of atoms or an empty array if the sequence terminates right away.

done : function

A predicate indicating if a token signals the end of an implicit group

: MathAtom[]   

scanLeftRight(): MathAtomprivate

Parse a /left.../right sequence.

Note: the /middle command can occur multiple times inside a /left.../right sequence, and is handled separately.

Return either an atom of type 'leftright' or null

: MathAtom   

scanModeSet(): MathAtomprivate

Parse a \(...\) or \[...\] sequence

: MathAtom   

group for the sequence or null

scanString(): stringprivate

Return a sequence of characters as a string. i.e. 'abcd' returns 'abcd'. Terminates on the first non-character encountered e.g. '{', '}' etc... Will also terminate on ']'

: string   

scanToken(): MathAtom[] | MathAtomprivate

: MathAtom[] | MathAtom