|
|
|
@ -14,8 +14,8 @@
|
|
|
|
|
* "start" is used.
|
|
|
|
|
*
|
|
|
|
|
* Throws |PEG.grammarParser.SyntaxError| if the grammar contains a syntax error
|
|
|
|
|
* or |PEG.Grammar.GrammarError| if it contains a semantic error. Note that not
|
|
|
|
|
* all errors are detected during the generation and some may protrude to the
|
|
|
|
|
* or |PEG.GrammarError| if it contains a semantic 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) {
|
|
|
|
@ -27,6 +27,17 @@ PEG.buildParser = function(grammar, startRule) {
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* ===== PEG.GrammarError ===== */
|
|
|
|
|
|
|
|
|
|
/* Thrown when the grammar contains an error. */
|
|
|
|
|
|
|
|
|
|
PEG.GrammarError = function(message) {
|
|
|
|
|
this.name = "PEG.GrammarError";
|
|
|
|
|
this.message = message;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
PEG.GrammarError.prototype = Error.prototype;
|
|
|
|
|
|
|
|
|
|
/* ===== PEG.ArrayUtils ===== */
|
|
|
|
|
|
|
|
|
|
/* Array manipulation utility functions. */
|
|
|
|
@ -136,23 +147,6 @@ PEG.RegExpUtils = {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* ===== PEG.Grammar ===== */
|
|
|
|
|
|
|
|
|
|
/* Namespace with grammar AST nodes. */
|
|
|
|
|
|
|
|
|
|
PEG.Grammar = {};
|
|
|
|
|
|
|
|
|
|
/* ===== PEG.Grammar.GrammarError ===== */
|
|
|
|
|
|
|
|
|
|
/* Thrown when the grammar contains an error. */
|
|
|
|
|
|
|
|
|
|
PEG.Grammar.GrammarError = function(message) {
|
|
|
|
|
this.name = "PEG.Grammar.GrammarError";
|
|
|
|
|
this.message = message;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
PEG.Grammar.GrammarError.prototype = Error.prototype;
|
|
|
|
|
|
|
|
|
|
/* ===== PEG.Compiler ===== */
|
|
|
|
|
|
|
|
|
|
PEG.Compiler = {
|
|
|
|
@ -261,8 +255,8 @@ PEG.Compiler = {
|
|
|
|
|
* Checks made on the grammar AST before compilation. Each check is a function
|
|
|
|
|
* that is passed the AST and start rule and does not return anything. If the
|
|
|
|
|
* check passes, the function does not do anything special, otherwise it
|
|
|
|
|
* throws |PEG.Grammar.GrammarError|. The checks are run in sequence in order
|
|
|
|
|
* of their definition.
|
|
|
|
|
* throws |PEG.GrammarError|. The checks are run in sequence in order of their
|
|
|
|
|
* definition.
|
|
|
|
|
*/
|
|
|
|
|
_checks: [
|
|
|
|
|
/* Checks that all referenced rules exist. */
|
|
|
|
@ -291,7 +285,7 @@ PEG.Compiler = {
|
|
|
|
|
rule_ref:
|
|
|
|
|
function(node) {
|
|
|
|
|
if (ast[node.name] === undefined) {
|
|
|
|
|
throw new PEG.Grammar.GrammarError(
|
|
|
|
|
throw new PEG.GrammarError(
|
|
|
|
|
"Referenced rule \"" + node.name + "\" does not exist."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
@ -312,7 +306,7 @@ PEG.Compiler = {
|
|
|
|
|
/* Checks that the start rule is defined. */
|
|
|
|
|
function(ast, startRule) {
|
|
|
|
|
if (ast[startRule] === undefined) {
|
|
|
|
|
throw new PEG.Grammar.GrammarError(
|
|
|
|
|
throw new PEG.GrammarError(
|
|
|
|
|
"Missing \"" + startRule + "\" rule."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
@ -356,7 +350,7 @@ PEG.Compiler = {
|
|
|
|
|
rule_ref:
|
|
|
|
|
function(node, appliedRules) {
|
|
|
|
|
if (PEG.ArrayUtils.contains(appliedRules, node.name)) {
|
|
|
|
|
throw new PEG.Grammar.GrammarError(
|
|
|
|
|
throw new PEG.GrammarError(
|
|
|
|
|
"Left recursion detected for rule \"" + node.name + "\"."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
@ -771,9 +765,9 @@ PEG.Compiler = {
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Generates a parser from a specified grammar AST and start rule. Throws
|
|
|
|
|
* |PEG.Grammar.GrammarError| if the AST contains a semantic error. Note that
|
|
|
|
|
* not all errors are detected during the generation and some may protrude to
|
|
|
|
|
* the generated parser and cause its malfunction.
|
|
|
|
|
* |PEG.GrammarError| if the AST contains a semantic error. Note that not all
|
|
|
|
|
* errors are detected during the generation and some may protrude to the
|
|
|
|
|
* generated parser and cause its malfunction.
|
|
|
|
|
*/
|
|
|
|
|
compileParser: function(ast, startRule) {
|
|
|
|
|
/*
|
|
|
|
|