new Parser(options)
A parser for JME
expressions.
Parameters:
Name | Type | Description |
---|---|---|
options |
Numbas.jme.parser_options |
Members
ops :Array.<string>
Binary operations.
Type:
re :Object.<RegExp>
Regular expressions to match tokens.
Type:
superscript_replacements :Array.<string>
Superscript characters, and their normal-script replacements.
Type:
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}
.
unicode_annotations
A dictionary mapping the descriptive tags in Numbas.unicode_mappings.letters
to JME name annotations.
Methods
addBinaryOperator(name, options)
Add a binary operator to the parser.
Parameters:
Name | Type | Description |
---|---|---|
name |
string | |
options |
Numbas.jme.operatorOptions |
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 |
options |
Numbas.jme.operatorOptions |
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 |
options |
Numbas.jme.operatorOptions |
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 |
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 |
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 |
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 |
getPrecedence(name) → {number}
Get the precedence of the given operator.
Parameters:
Name | Type | Description |
---|---|---|
name |
string |
Returns:
Type | Description |
---|---|
number |
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 |
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 |
name |
string | The name of the setting to try to load from the dictionary. |
Returns:
Type | Description |
---|---|
* |
isAssociative(name) → {boolean}
Is the given operator associative?
Parameters:
Name | Type | Description |
---|---|---|
name |
string |
Returns:
Type | Description |
---|---|
boolean |
isCommutative(name) → {boolean}
Is the given operator commutative?
Parameters:
Name | Type | Description |
---|---|---|
name |
string |
Returns:
Type | Description |
---|---|
boolean |
isRelation(name) → {boolean}
Is the given operator a relation?
Parameters:
Name | Type | Description |
---|---|---|
name |
string |
Returns:
Type | Description |
---|---|
boolean |
isRightAssociative(name) → {boolean}
Is the given operator right-associative?
Parameters:
Name | Type | Description |
---|---|---|
name |
string |
Returns:
Type | Description |
---|---|
boolean |
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 |
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 |
normaliseOp(op) → {string}
Normalise a string containing a single operator name or symbol.
Parameters:
Name | Type | Description |
---|---|---|
op |
string |
Returns:
Type | Description |
---|---|
string |
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 |
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 |
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 |
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 |
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> |