Parser

Parser

new Parser(tokens: Array.<InputToken>, argsopt: Object)private

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

tokens Array.<InputToken>

An array of tokens generated by the lexer.

args Object optional

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

Properties
macros Object optional

Optional macro definitions.

tokens Array.<InputToken>

An array of tokens generated by the lexer.

args Object

Optional arguments to substitute the # token.

macros Object

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 Array.<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.

Methods

end()private

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

hasLiteral(valueopt: string) → boolean

value string optional
boolean

True if the next token is of type 'literal and has the specified value. If value is empty, return true if the token is of type 'literal'

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

lastMathAtom()

Return the last atom of the math list. If force is true (or undefined) and the list is empty, a new empty atom is created and returned as the result.

parseAtom()private

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

parseKeyword() → booleanprivate

Keywords are used to specify dimentions, 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

boolean

true if the expected keyword is present

parseLimits()private

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.

parseToken(type: string)

type string

scanArg(parseModeopt: 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 optional

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

scanColor()private

Return a CSS color (#rrggbb)

scanDelim() → stringprivate

Scan a delimiter, e.g. '(', '|', '\vert', '\ulcorner'

string

The delimiter (as a character or command) or null

scanDimen() → numberprivate

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

See TeX:8831

number

scanEnvironment()private

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. {}).

scanImplicitGroup(doneopt: function) → Array.<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 optional

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

Array.<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

scanModeSet() → MathAtomprivate

Parse a $$...$$ or $...$ sequence

MathAtom

group for the sequence or null

scanModeShift()private

Parse a $...$ or $$...$$ sequence

scanNumber() → numberprivate

Return as a number a group of characters representing a numerical quantity.

From TeX:8695 (scan_int): An integer number can be preceded by any number of spaces and \.+' or.-' signs. Then comes either a decimal constant (i.e., radix 10), an octal constant (i.e., radix 8, preceded by~.\'), a hexadecimal constant (radix 16, preceded by~."), an alphabetic constant (preceded by~.`), or an internal variable.

number

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() → Array.MathAtom|MathAtomprivate

Array.MathAtom | MathAtom