Jump to navigation

Class: Parser

Numbas.jme.Parser

new Parser(options)

A parser for JME expressions.

Parameters:
Name Type Description
options Numbas.jme.parser_options
Source:

Members

ops :Array.<string>

Binary operations.

Type:
Source:

option_defaults :Numbas.jme.parser_options

Default options for new parsers.

Type:
Source:

re :Object.<RegExp>

Regular expressions to match tokens.

Type:
Source:

superscript_replacements :Array.<string>

Superscript characters, and their normal-script replacements.

Type:
Source:

tokeniser_types

Descriptions of kinds of token that the tokeniser can match. re is a regular expression matching the token. parse is a function which takes a RegEx match object, the tokens produced up to this point, the input expression, and the current position in the expression. It should return an object {tokens, start, end}.

Source:

unicode_annotations

A dictionary mapping the descriptive tags in Numbas.unicode_mappings.letters to JME name annotations.

Source:

Methods

addBinaryOperator(name, options)

Add a binary operator to the parser.

Parameters:
Name Type Description
name string
options Numbas.jme.operatorOptions
Source:

addOperator(name)

Add an operator to the parser.

Parameters:
Name Type Description
name string
Source:
See:

addPostfixOperator(name, alt, options)

Add a postfix operator to the parser.

Parameters:
Name Type Description
name string
alt string

The "interpreted" name of the operator, e.g. '!' is interpreted as 'fact'. If not given, the value of name is used.

options Numbas.jme.operatorOptions
Source:

addPrefixOperator(name, alt, options)

Add a prefix operator to the parser.

Parameters:
Name Type Description
name string
alt string

The "interpreted" name of the operator, e.g. '!' is interpreted as 'fact'. If not given, the value of name is used.

options Numbas.jme.operatorOptions
Source:

compile(expr) → {Numbas.jme.tree}

Compile an expression string to a syntax tree. (Runs Numbas.jme.tokenise then Numbas.jme.shunt).

Parameters:
Name Type Description
expr JME
Returns:
Type Description
Numbas.jme.tree
Source:
See:

funcSynonym(name) → {string}

If the given function name has a synonym, use it, otherwise return the original name.

Parameters:
Name Type Description
name string
Returns:
Type Description
string
Source:
See:

getArity(name) → {number}

Get the arity of the given operator.

Parameters:
Name Type Description
name string
Returns:
Type Description
number
Source:

getConstant(name) → {number}

If the given name is defined as a constant, return its value, otherwise return undefined.

Parameters:
Name Type Description
name string
Returns:
Type Description
number
Source:

getPostfixForm(name) → {string}

If the given operator name has a defined postfix form, return it, otherwise return undefined.

Parameters:
Name Type Description
name string
Returns:
Type Description
string
Source:

getPrecedence(name) → {number}

Get the precedence of the given operator.

Parameters:
Name Type Description
name string
Returns:
Type Description
number
Source:

getPrefixForm(name) → {string}

If the given operator name has a defined prefix form, return it, otherwise return undefined.

Parameters:
Name Type Description
name string
Returns:
Type Description
string
Source:

getSetting(setting, name) → {*}

There are many dictionaries storing definitions of things like constants and alternate names, which are defined both globally in Numbas.jme and locally in a Parser. This is a wrapper to load the value of the setting if it exists, and return undefined otherwise.

Parameters:
Name Type Description
setting string

The name of the dictionary. Both this and of Numbas.jme must have members with this name.

name string

The name of the setting to try to load from the dictionary.

Returns:
Type Description
*
Source:

isAssociative(name) → {boolean}

Is the given operator associative?

Parameters:
Name Type Description
name string
Returns:
Type Description
boolean
Source:

isCommutative(name) → {boolean}

Is the given operator commutative?

Parameters:
Name Type Description
name string
Returns:
Type Description
boolean
Source:

isRelation(name) → {boolean}

Is the given operator a relation?

Parameters:
Name Type Description
name string
Returns:
Type Description
boolean
Source:

isRightAssociative(name) → {boolean}

Is the given operator right-associative?

Parameters:
Name Type Description
name string
Returns:
Type Description
boolean
Source:

make_re()

Update regular expressions for matching tokens.

Source:
See:

normaliseName(name) → {object}

Normalise a name token, returning a name string and a list of annotations. Don't confuse this with Numbas.jme.normaliseName, which applies scope-dependent normalisation, e.g. case-insensitivity, after parsing.

Parameters:
Name Type Description
name string
Returns:
Type Description
object
Source:

normaliseNumber(literal) → {string}

Normalise a string containing a single string literal, using the Unicode normalization algorithm NFKD.

Parameters:
Name Type Description
literal string
Returns:
Type Description
string
Source:

normaliseOp(op) → {string}

Normalise a string containing a single operator name or symbol.

Parameters:
Name Type Description
op string
Returns:
Type Description
string
Source:

normalisePunctuation(c) → {string}

Normalise a string containing a single punctuation character, using the Unicode normalization algorithm NFKD.

Parameters:
Name Type Description
c string
Returns:
Type Description
string
Source:

op(name, postfix, prefix, negated) → {Numbas.jme.token}

Create an operator token with the given name.

Parameters:
Name Type Description
name string

The name of the operator.

postfix boolean

Is the operator postfix?

prefix boolean

Is the operator prefix?

negated boolean

Is this operator negated?

Returns:
Type Description
Numbas.jme.token
Source:

opSynonym(name) → {string}

If the given operator name has a synonym, use it, otherwise return the original name.

Parameters:
Name Type Description
name string
Returns:
Type Description
string
Source:
See:

setOperatorProperties(name, options)

Set properties for a given operator.

Parameters:
Name Type Description
name string

The name of the operator.

options Numbas.jme.operatorOptions
Source:

shunt(tokens) → {Numbas.jme.tree}

Shunt list of tokens into a syntax tree. Uses the shunting yard algorithm.

Parameters:
Name Type Description
tokens Array.<Numbas.jme.token>
Returns:
Type Description
Numbas.jme.tree
Source:
See:

tokenise(expr) → {Array.<Numbas.jme.token>}

Convert given expression string to a list of tokens. Does some tidying, e.g. inserts implied multiplication symbols.

Parameters:
Name Type Description
expr JME
Returns:
Type Description
Array.<Numbas.jme.token>
Source:
See: