|
|
|
@ -285,7 +285,7 @@ PEG.Compiler = {
|
|
|
|
|
|
|
|
|
|
rule_ref:
|
|
|
|
|
function(node) {
|
|
|
|
|
if (typeof(ast[node.name]) === "undefined") {
|
|
|
|
|
if (typeof(ast.rules[node.name]) === "undefined") {
|
|
|
|
|
throw new PEG.GrammarError(
|
|
|
|
|
"Referenced rule \"" + node.name + "\" does not exist."
|
|
|
|
|
);
|
|
|
|
@ -299,14 +299,14 @@ PEG.Compiler = {
|
|
|
|
|
|
|
|
|
|
function check(node) { checkFunctions[node.type](node); }
|
|
|
|
|
|
|
|
|
|
for (var rule in ast) {
|
|
|
|
|
check(ast[rule]);
|
|
|
|
|
for (var rule in ast.rules) {
|
|
|
|
|
check(ast.rules[rule]);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/* Checks that the start rule is defined. */
|
|
|
|
|
function(ast, startRule) {
|
|
|
|
|
if (typeof(ast[startRule]) === "undefined") {
|
|
|
|
|
if (typeof(ast.rules[startRule]) === "undefined") {
|
|
|
|
|
throw new PEG.GrammarError(
|
|
|
|
|
"Missing \"" + startRule + "\" rule."
|
|
|
|
|
);
|
|
|
|
@ -356,7 +356,7 @@ PEG.Compiler = {
|
|
|
|
|
"Left recursion detected for rule \"" + node.name + "\"."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
check(ast[node.name], appliedRules);
|
|
|
|
|
check(ast.rules[node.name], appliedRules);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
literal: nop,
|
|
|
|
@ -368,8 +368,8 @@ PEG.Compiler = {
|
|
|
|
|
checkFunctions[node.type](node, appliedRules);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (var rule in ast) {
|
|
|
|
|
check(ast[rule], []);
|
|
|
|
|
for (var rule in ast.rules) {
|
|
|
|
|
check(ast.rules[rule], []);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
@ -432,18 +432,18 @@ PEG.Compiler = {
|
|
|
|
|
replaceFunctions[node.type](node, from, to);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (var rule in ast) {
|
|
|
|
|
replace(ast[rule], from, to);
|
|
|
|
|
for (var rule in ast.rules) {
|
|
|
|
|
replace(ast.rules[rule], from, to);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (var rule in ast) {
|
|
|
|
|
if (isProxyRule(ast[rule])) {
|
|
|
|
|
replaceRuleRefs(ast, ast[rule].name, ast[rule].expression.name);
|
|
|
|
|
for (var rule in ast.rules) {
|
|
|
|
|
if (isProxyRule(ast.rules[rule])) {
|
|
|
|
|
replaceRuleRefs(ast, ast.rules[rule].name, ast.rules[rule].expression.name);
|
|
|
|
|
if (rule === startRule) {
|
|
|
|
|
startRule = ast[rule].expression.name;
|
|
|
|
|
startRule = ast.rules[rule].expression.name;
|
|
|
|
|
}
|
|
|
|
|
delete ast[rule];
|
|
|
|
|
delete ast.rules[rule];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -452,6 +452,10 @@ PEG.Compiler = {
|
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
_compileFunctions: {
|
|
|
|
|
initializer: function(node) {
|
|
|
|
|
return node.code;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
rule: function(node) {
|
|
|
|
|
var resultVar = PEG.Compiler.generateUniqueIdentifier("result");
|
|
|
|
|
|
|
|
|
@ -871,9 +875,13 @@ PEG.Compiler = {
|
|
|
|
|
startRule = newAstNadStartRule[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var initializerCode = ast.initializer !== null
|
|
|
|
|
? this.compileNode(ast.initializer)
|
|
|
|
|
: "";
|
|
|
|
|
|
|
|
|
|
var parseFunctionDefinitions = [];
|
|
|
|
|
for (var rule in ast) {
|
|
|
|
|
parseFunctionDefinitions.push(this.compileNode(ast[rule]));
|
|
|
|
|
for (var rule in ast.rules) {
|
|
|
|
|
parseFunctionDefinitions.push(this.compileNode(ast.rules[rule]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var source = this.formatCode(
|
|
|
|
@ -998,6 +1006,8 @@ PEG.Compiler = {
|
|
|
|
|
" return { line: line, column: column };",
|
|
|
|
|
" }",
|
|
|
|
|
" ",
|
|
|
|
|
" ${initializerCode}",
|
|
|
|
|
" ",
|
|
|
|
|
" var result = parse_${startRule}({ reportMatchFailures: true });",
|
|
|
|
|
" ",
|
|
|
|
|
" /*",
|
|
|
|
@ -1054,6 +1064,7 @@ PEG.Compiler = {
|
|
|
|
|
" return result;",
|
|
|
|
|
"})()",
|
|
|
|
|
{
|
|
|
|
|
initializerCode: initializerCode,
|
|
|
|
|
parseFunctionDefinitions: parseFunctionDefinitions.join("\n\n"),
|
|
|
|
|
startRule: startRule
|
|
|
|
|
}
|
|
|
|
|