Fork me on GitHub


This module parses and outputs an Abstract Syntax Tree representing the formula using the MASTON format.

To use it, use the MathAtom.toAST() method.


asLatex(ast: object): stringinner

ast : object

Abstract Syntax Tree object (in canonical form)

: string   

A LaTeX representation of the AST

asMachineNumber(node: object): numberprivateinner

node : object

Abstract Syntax Tree node

: number   

A JavaScript number, the value of the AST or NaN

asSymbol(expr: object): stringinner

expr : object

Abstract Syntax Tree object

: string   

A string, the symbol, or undefined

escapeText(s: string): stringinner

Return a string escaped as necessary to comply with the JSON format

s : string
: string   

formatMantissa(m: string, config: [string]:any)inner

Return a formatted mantissa: 1234567 -> 123 456 7... 1233333 -> 12(3)

m : string
config : [string]:any

getCanonicalName(latex,: string): stringinner

latex, : string

for example '\times'

: string   

the canonical name for the input, for example '*'

getLatexForSymbol(name: string): stringinner

name : string

symbol name, e.g. "alpha"

: string   

getLatexTemplateForFunction(name: string): stringinner

name : string

function canonical name

: string   

getPrecedence(canonicalName,: string): numberinner

Given a canonical name, return its precedence

canonicalName, : string

for example "and"

: number   

isAtom(expr: object, type: string, value: string)inner

Return true if the current atom is of the specified type and value.

expr : object
type : string
value : string

negate(node: object)inner

Return the negative of the expression. Usually { fn:'negate', arg } but for numbers, the negated number

node : object

numberAsLatex(num: string | number): stringinner

num : string | number

A number, represented as a string (e.g. "-12.45" particularly useful for arbitrary precision numbers) or a number (-12.45)

: string   

A LaTeX representation of the AST

opPrec(atom: object): numberinner

Return the operator precedence of the atom or -1 if not an operator

atom : object
: number   

parse(atoms: Atoms[]): objectinner

atoms : Atoms[]
: object   


Delimiters can be expressed:

  • as a matching pair of regular characters: '(a)'
  • a as 'leftright' expression: '\left(a\right)'
  • as a matching pair of 'sizeddelim': '\Bigl(a\Bigr)

Note that the '\delim' command is only used for delimiters in the middle of a \left\right pair and not to represent pair-matched delimiters.

This function handles all three cases


Some symbols are made up of two consecutive characters. Handle them here. Return undefined if not a digraph. TODO: other digraphs: := ++ ** =: °C U+2103 °F U+2109

parseExpression(expr: object): objectprivateinner

Given an atom or an array of atoms, return their AST representation as an object.

expr : object

An expressions, including expr.atoms, expr.index, expr.minPrec the minimum precedence that this parser should parse before returning; expr.lhs (optional); expr.ast, the resulting AST.

: object   

the expr object, updated


Parse postfix operators, such as "!" (factorial)

parseSentence(expr: object): objectinner

Parse a sequence of text zone and math zones:

:= (() )+

expr : object
: object   

parseSupsub(expr: object)inner

Parse for a possible sup/sub attached directly to the current atom or to a following 'msubsup' atom. After the call, the index points to the next atom to process.

expr : object

wrapFence(fence: string): stringinner

fence : string

The fence to wrap around the arguments

: string   
  • A string wrapped in the fence

wrapFn(functionName: string, …params: object)inner

functionName : string
params : object repeatable