Fork me on GitHub


core/parser# Parser

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

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

tokens : Token[]

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 : Token[]

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 : Atom[]

Accumulated result of the parsing by parseAtom()

style : object

The font, weight, color, etc. to apply to the upcoming tokens

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): booleanprivate

pattern : RegEx
: boolean   

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

hasToken(type: string): booleanprivate

type : string
: boolean   

True if the next token is of the specified type


Return the last atom of the math list If there isn't one, insert a msubsup and return it.


Make a Atom 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(): Atomprivate

Parse a subscript/superscript: ^ and _.

Modify the last atom accordingly.

: Atom   

parseToken(type: string)private

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(): Atomprivate

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

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

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

: Atom   

scanImplicitGroup(done?: function): Atom[]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

: Atom[]   

scanLeftRight(): Atomprivate

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

: Atom   

scanModeSet(): Atomprivate

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

: Atom   

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(): Atom[] | Atomprivate

: Atom[] | Atom