new Language(rules)
Define a language syntax with BNF like description.
Define a language syntax with BNF like description.
Parameters:
Name | Type | Description |
---|---|---|
rules |
Array.<SyntaxRule> | array of syntax definition |
- Source:
Members
root :string
Type:
- string
- Source:
rules :Record.<string, SyntaxRule>
Type:
- Record.<string, SyntaxRule>
- Source:
Methods
(static) syntax(name, rules, evaluator) → {SyntaxRule}
Create a syntax rule as an element for parameter of the constructor.
Create a syntax rule as an element for parameter of the constructor.
Parameters:
Name | Type | Description |
---|---|---|
name |
string | A syntax name to reference this syntax. |
rules |
Array.<Array.<(string|LexElement)>> | Syntax rules.
A string type element in the rule is a name referencing a syntax
rule of the term.
If the term could be repeated, to end of that name,
put |
evaluator |
Evaluator | null | (Optional) An evaluator function. This is omittable when the rules contains only one term. |
- Source:
Returns:
A syntax object
- Type
- SyntaxRule
evaluate(term) → {any}
Evaluate an analyzed code.
Evaluate an analyzed code.
Parameters:
Name | Type | Description |
---|---|---|
term |
Term | A term returned from `Language#parse' |
- Source:
Returns:
A value returned from the evaluator for the term.
- Type
- any
parse(source) → {Term}
Analyze the codes by recursive descent parsing based on the BNF rule.
Analyze the codes by recursive descent parsing based on the BNF rule.
Parameters:
Name | Type | Description |
---|---|---|
source |
string | Array.<Token> | source code or token list |
- Source:
Returns:
result
- Type
- Term
tokenize(source) → {Array.<Token>}
Tokenize the source code by lexical analysis.
Tokenize the source code by lexical analysis.
Parameters:
Name | Type | Description |
---|---|---|
source |
string | source code |
- Source:
Returns:
an array of lexical tokens. It contains tokens type of whitespaces, string literal, number literal, or punctuators. Types of token:
Language.whitespace
Language.strlit
Language.numlit
Language.punct
- Type
- Array.<Token>