new Grammar( [grammar] [, tokens] [, config])
Creates a grammar representation. Defines tokens, if any.
Parameters:
| Name | Type | Argument | Description |
|---|---|---|---|
grammar |
string |
<optional> <nullable> |
the grammar to represent, using the EBNF grammar and EBNF token and literal notation. This can be omitted to construct the rules directly using the factory methods. |
tokens |
Object.<string, RegExp> |
<optional> <nullable> |
maps token names, if any,
in the new grammar to their patterns
which must not accept empty input, must not use |
config |
Object.<string, Object> |
<optional> |
overwrites configurable values' defaults; loaded first but can only be third parameter. |
- Source:
Properties:
| Name | Type | Description | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
rules |
Array.<module:EBNF~Rule> | list of rules; rule zero is start rule. |
||||||||||||||||||||||||||||||||||||
prefix |
string | prefix for log; assign string to push, else pop. |
||||||||||||||||||||||||||||||||||||
config |
Object.<string, Object> | maps names to configurable values. Properties
|
||||||||||||||||||||||||||||||||||||
lits |
Array.<module:Base~Lit> | list of unique literals, can be pushed. |
||||||||||||||||||||||||||||||||||||
litsByName |
Object.<string, module:Base~Lit> | maps |
||||||||||||||||||||||||||||||||||||
tokens |
Array.<module:Base~Token> | list of unique tokens, can be pushed. |
||||||||||||||||||||||||||||||||||||
tokensByName |
Object.<string, module:Base~Token> | maps name to unique token. |
||||||||||||||||||||||||||||||||||||
levels |
Array.<module:Base~Precedence> | list of precedence levels, can be pushed. |
||||||||||||||||||||||||||||||||||||
nts |
Array.<module:Base~NT> | list of unique non-terminals, can be pushed. |
||||||||||||||||||||||||||||||||||||
ntsByName |
Object.<string, module:Base~NT> | maps name to unique non-terminal. |
||||||||||||||||||||||||||||||||||||
errors |
number | incremented by |
Extends
Members
-
<static, constant> ebnf :string
-
Grammar describing the EBNF notation accepted by
new Grammar():A *grammar* consists of one or more rules.
Each *rule* has a unique name on the left-hand side and alternatives on the right-hand side *
*Alternatives* are one or more symbol sequences, separated by `|`.
A *symbol sequence* contains one or more items, such as a rule name, a self-defining literal, the name of a token, or alternatives enclosed by braces or brackets.
Braces denote that the enclosed alternatives appear one or more times in a sentence.
Brackets denote that the enclosed alternatives are optional, i.e., they may or may not appear (once) in a sentence. A symbol sequence must not only contain optional alternatives.
Type:
- string
Example
EBNF grammars' grammar
grammar: [{ level }] { rule }; level: '%left' { term } ';' | '%right' { term } ';' | '%nonassoc' { term } ';'; rule: Token ':' alt ';'; alt: seq [{ '|' seq }]; seq: { lit | ref | opt | some } [ '%prec' term ]; term: lit | ref; lit: Lit; ref: Token; opt: '[' alt ']'; some: '{' alt '}'; -
<private, static, constant> grammar :module:EBNF~Grammar
-
The EBNF grammars' grammar; created when the module is loaded and used internally in
new Grammar().Type:
-
<static, constant> terminals :Object.<string, RegExp>
-
Token definitions for
LitandTokeninGrammar.ebnf.*Literals* represent themselves and are single-quoted strings using `\` only to escape single quotes and `\` itself.
*Tokens* represent sets of inputs, such as names or numbers, and are alphanumeric names which must start with a letter and may include underscores.
A `Name` can include `$error` for translation to BNF.
Type:
- Object.<string, RegExp>
- Source:
- See:
Example
EBNF grammars' tokens
{ Lit: /'(?:[^'\\]|\\['\\])+'/, Token: /[A-Za-z][A-Za-z0-9_]*|\$error/ } -
<static> tracing :Object.<string, Object.<string, function()>>
-
Common method cache for tracing grammar checking and parsing.
cls.prototype.methodis cached asGrammar.tracing[method][cls.name]. IfGrammar.tracing[method]exists, tracing can only be turned off and vice versa.Tracing grammar checking and parsing is static, i.e., common to all grammars that might be created, because the methods themselves are common to all grammars.
Type:
- Object.<string, Object.<string, function()>>
Methods
-
add(item)
-
Adds a new symbol to the proper inventory or creates and adds new tokens. Must be called with a new, unique symbol or with a map of token names to patterns. Validates item names against
.config. Token patterns must not accept empty input, must not used,g, oryflag, should not be anchored, and should use(:? )rather than( )for grouping.Parameters:
Name Type Description itemSymbol | Object.<string, RegExp> to add to the proper inventory or create and add.
- Inherited From:
- Overrides:
- Source:
-
alt(seqs)
-
Factory method to represent a list of alternatives for EBNF.
Parameters:
Name Type Argument Description seqsArray.<module:EBNF~Seq> <repeatable>
the alternatives, not empty.
Returns:
a new list of alternatives.
- Type
- module:EBNF~Alt
-
assert(condition, s)
-
Displays a message and throws an error if a condition is not met; primarily used for stronger argument typing.
Parameters:
Name Type Argument Description conditionboolean should be true.
sArray.<?object> <repeatable>
message, to be displayed; joined by blanks.
- Inherited From:
- Overrides:
- Source:
Throws:
-
message if condition is not met.
- Type
- string
-
check()
-
Checks the grammar to be LL(1).
- Calls
expect()if necessary. - Computes
followfor each node; a non-terminal obtains it from the rule. - Detects ambiguities.
Returns:
an error message on failure.
- Type
- udefined | string
- Calls
-
dump( [a])
-
Displays the grammar and all symbols with name and contents of all sets. With argument (kludge!) acts as a static method and converts nested arrays to a string – useful because
console.debugonly reaches 3 levels.Parameters:
Name Type Argument Description aObject <optional>
<nullable>
the object to convert to a string.
- Overrides:
- Source:
Returns:
- Type
- string
-
error(s)
-
Displays a message and counts it as an error.
Parameters:
Name Type Argument Description sArray.<?object> <repeatable>
message, to be displayed; joined by blanks.
- Inherited From:
- Overrides:
- Source:
Returns:
the message.
- Type
- string
-
expect()
-
Computes the
expectsets; only call once.- Does not permit precedences.
- There has to be at least one rule.
- All non-terminals must be defined, each by a unique rule.
- Detects left recursion as an error.
- Computes
expectfor each node; a non-terminal obtains it from the rule. - All rules must be necessary, i.e., reachable from the first rule.
Returns:
an error message on failure.
- Type
- undefined | string
-
lit(literal [, used])
-
Factory method to create a unique literal symbol, maintains
.litsand.litsByNameParameters:
Name Type Argument Description literalstring literal's representation conforming to
.config.lits.usedboolean <optional>
if
truemark literal as used.Returns:
a unique literal.
- Type
- module:EBNF~Lit
-
message(s)
-
Displays a message on the configured
.log.Parameters:
Name Type Argument Description sArray.<?object> <repeatable>
message, to be displayed; joined by blanks.
- Inherited From:
- Overrides:
- Source:
Returns:
the message.
- Type
- string
-
nt( [name])
-
Factory method to create a unique non-terminal symbol, maintains
.ntsand.ntsByName.Parameters:
Name Type Argument Description namestring <optional>
non-terminal's name conforming to
config.nts; error if a token. If not a string creates a unique name (intended for grammar extension).Returns:
a unique non-terminal.
- Type
- module:EBNF~NT
-
opt(seqs)
-
Factory method to represent an optional list of alternatives for EBNF.
Parameters:
Name Type Argument Description seqsArray.<module:EBNF~Seq> <repeatable>
the alternatives, not empty.
Returns:
a new optional list of alternatives.
- Type
- module:EBNF~Opt
-
parser( [skip])
-
Factory method to create a parser to recognize and process input. Requires that the expect sets for this grammar have been prepared.
Parameters:
Name Type Argument Description skipRegEx <optional>
a pattern to define ignorable character sequences, by default white space, must not accept empty input, must not use flags, must not be anchored, should use
(:? )rather than( )for grouping.Returns:
the parser.
- Type
- module:EBNF~Parser
-
precedence(assoc, terminals)
-
Factory method to represent a list of terminals with equal precedence level and equal associativity. Creates a new
Precedenceobject, adds it to.levels, adds.prec.leveland.prec.assocto all terminals in the list, and checks for duplicates.Parameters:
Name Type Description assocstring associativity:
'%left','%right', or'%nonassoc'.terminalsArray.<?module:Base~T> to add,
nullelements are ignored; no duplicates.- Inherited From:
- Overrides:
- Source:
Returns:
representing the set, or
nullif there are no terminals. -
rule(nt, seqs)
-
Factory method to create a rule representation for EBNF. Maintains rule's non-terminal's
.ruleandthis.rules.Parameters:
Name Type Argument Description ntmodule:EBNF~NT left-hand side, non-terminal.
seqsArray.<module:EBNF~Seq> <repeatable>
right-hand side, list of alternative sequences.
Returns:
a new rule representation.
- Type
- module:EBNF~Rule
-
scanner( [skip] [, terminals])
-
Factory method to create a scanner.
Parameters:
Name Type Argument Description skipRegExp <optional>
a pattern to define ignorable character sequences, by default white space, must not accept empty input, must not use
d,g, oryflag, should not be anchored, should use(:? )rather than( )for grouping.terminalsArray.<T> <optional>
ordered list to create the lexical analysis pattern.
- Inherited From:
- Overrides:
- Source:
Returns:
the scanner.
- Type
- module:Base~Scanner
-
seq(nodes [, terminal])
-
Factory method to represent a sequence of nodes for EBNF. Precedence levels have to be defined prior to using this method.
Parameters:
Name Type Argument Default Description nodesArray.<(module:Base~Symbol|module:EBNF~Opt|module:EBNF~Some)> descendants, not empty, not all
Opt.terminalmodule:Base~T <optional>
<nullable>
null can define precedence for translation to BNF.
Returns:
a new sequence.
- Type
- module:EBNF~Seq
-
some(seqs)
-
Factory method to represent a repeatable list of alternatives for EBNF.
Parameters:
Name Type Argument Description seqsArray.<module:EBNF~Seq> <repeatable>
the alternatives, not empty.
Returns:
a new repeatable list of alternatives.
- Type
- module:EBNF~Opt
-
toString()
-
Displays a description of the grammar.
Returns:
- Type
- string
-
token( [name] [, pat] [, used])
-
Factory method to create a unique token symbol, maintains
.tokensand.tokensByName.Parameters:
Name Type Argument Description namestring <optional>
token's name conforming to
.config.tokens; error if a non-terminal. If omitted represents the$errortoken with an emptyRegExp(intended for BNF translation).patRegExp <optional>
pattern to match values representing the token in input; used only when the token is created, must not accept empty input, must not use
d,g, oryflag, should not be anchored, should use(:? )rather than( )for grouping.usedboolean <optional>
if
truemark token as used.Returns:
a unique token.
- Type
- module:EBNF~Token
-
trace(what)
-
Installs and removes trace wrappers for grammar checking and
parsemethods; controlled by the configuration flagsshallow,deep,follow, andparse, should only be called byexpect()andparser.parse().The tracing wrappers use
.config.logand.prefix.Grammar checking and
parsemethods are cached inGrammar.tracingglobally per method and class.Parameters:
Name Type Description whatstring one of
shallow,deep,follow, orparseto trace that algorithm. -
tuple(lineno, t [, value])
-
Factory method to create an element of a tokenized input stream.
Parameters:
Name Type Argument Default Description linenonumber input position.
tmodule:Base~T <nullable>
terminal, i.e., literal or token object;
scan()usesnullfor an illegal character.valuestring <optional>
<nullable>
null terminal's representation in the input.
- Inherited From:
- Overrides:
- Source:
Returns:
an element of a tokenized input stream.
- Type
- module:Base~Tuple