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:

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:

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:

isRightAssociative(name) → {Boolean}

Is the given operator right-associative?

Parameters:
Name Type Description
name String
Returns:
Type Description
Boolean
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 (wikipedia has a good description)

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: