|
|
|
@ -11,32 +11,18 @@ function nop() {}
|
|
|
|
|
/*
|
|
|
|
|
* Generates a parser from a specified grammar and start rule and returns it.
|
|
|
|
|
*
|
|
|
|
|
* The grammar may be either an object or a string. If it is an object, it
|
|
|
|
|
* must contain AST of the parsing expressions (i.e. instances of |PEG.Grammar.*
|
|
|
|
|
* classes| for the grammar rules in its properties. If it is a string, it is
|
|
|
|
|
* parsed using |PEG.grammarParser| to obtain the grammar AST and thus it must
|
|
|
|
|
* be in a format that this parser accepts (see the source code for details).
|
|
|
|
|
* The grammar must be a string in the format described by the metagramar in the
|
|
|
|
|
* metagrammar.pegjs file. The start rule may be unspecified, in which case
|
|
|
|
|
* "start" is used.
|
|
|
|
|
*
|
|
|
|
|
* The start rule may be unspecified, in which case "start" is used.
|
|
|
|
|
*
|
|
|
|
|
* Throws |PEG.Grammar.GrammarError| if the grammar definition is not object nor
|
|
|
|
|
* string or if it contains an error. Note that not all errors are detected
|
|
|
|
|
* during the generation and some may protrude to the generated parser and cause
|
|
|
|
|
* its malfunction.
|
|
|
|
|
* Throws |PEG.Grammar.GrammarError| if the grammar contains an error. Note that
|
|
|
|
|
* not all errors are detected during the generation and some may protrude to
|
|
|
|
|
* the generated parser and cause its malfunction.
|
|
|
|
|
*/
|
|
|
|
|
PEG.buildParser = function(grammar, startRule) {
|
|
|
|
|
startRule = startRule || "start";
|
|
|
|
|
|
|
|
|
|
switch (typeof(grammar)) {
|
|
|
|
|
case "object":
|
|
|
|
|
var ast = grammar;
|
|
|
|
|
break;
|
|
|
|
|
case "string":
|
|
|
|
|
var ast = PEG.grammarParser.parse(grammar);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new PEG.Grammar.GrammarError("Grammar must be object or string.");
|
|
|
|
|
}
|
|
|
|
|
var ast = PEG.grammarParser.parse(grammar);
|
|
|
|
|
|
|
|
|
|
for (var key in ast) {
|
|
|
|
|
ast[key].checkReferencedRulesExist(ast);
|
|
|
|
|