AST refactoring 1/6: Make properties of AST nodes public
Removed undescore prefix of the properties and deleted getters.
This commit is contained in:
parent
1c7c5bb5da
commit
4e99a2bda7
138
lib/compiler.js
138
lib/compiler.js
|
@ -182,97 +182,89 @@ PEG.Grammar.GrammarError.prototype = Error.prototype;
|
|||
/* ===== PEG.Grammar.* ===== */
|
||||
|
||||
PEG.Grammar.Rule = function(name, displayName, expression) {
|
||||
this._name = name;
|
||||
this._displayName = displayName;
|
||||
this._expression = expression;
|
||||
};
|
||||
|
||||
PEG.Grammar.Rule.prototype = {
|
||||
getName: function() { return this._name; }
|
||||
this.name = name;
|
||||
this.displayName = displayName;
|
||||
this.expression = expression;
|
||||
};
|
||||
|
||||
PEG.Grammar.Choice = function(alternatives) {
|
||||
this._alternatives = alternatives;
|
||||
this.alternatives = alternatives;
|
||||
};
|
||||
|
||||
PEG.Grammar.Sequence = function(elements) { this._elements = elements; };
|
||||
|
||||
PEG.Grammar.Sequence.prototype = {
|
||||
getElements: function() { return this._elements; }
|
||||
};
|
||||
PEG.Grammar.Sequence = function(elements) { this.elements = elements; };
|
||||
|
||||
PEG.Grammar.AndPredicate = function(expression) {
|
||||
this._expression = expression;
|
||||
this.expression = expression;
|
||||
};
|
||||
|
||||
PEG.Grammar.NotPredicate = function(expression) {
|
||||
this._expression = expression;
|
||||
this.expression = expression;
|
||||
};
|
||||
|
||||
PEG.Grammar.Optional = function(expression) { this._expression = expression; };
|
||||
PEG.Grammar.Optional = function(expression) { this.expression = expression; };
|
||||
|
||||
PEG.Grammar.ZeroOrMore = function(expression) { this._expression = expression; };
|
||||
PEG.Grammar.ZeroOrMore = function(expression) { this.expression = expression; };
|
||||
|
||||
PEG.Grammar.OneOrMore = function(expression) { this._expression = expression; };
|
||||
PEG.Grammar.OneOrMore = function(expression) { this.expression = expression; };
|
||||
|
||||
PEG.Grammar.Action = function(expression, action) {
|
||||
this._expression = expression;
|
||||
this._action = action;
|
||||
this.expression = expression;
|
||||
this.action = action;
|
||||
};
|
||||
|
||||
PEG.Grammar.RuleRef = function(name) { this._name = name; };
|
||||
PEG.Grammar.RuleRef = function(name) { this.name = name; };
|
||||
|
||||
PEG.Grammar.Literal = function(value) { this._value = value; };
|
||||
PEG.Grammar.Literal = function(value) { this.value = value; };
|
||||
|
||||
PEG.Grammar.Any = function() {};
|
||||
|
||||
PEG.Grammar.Class = function(characters) { this._characters = characters; };
|
||||
PEG.Grammar.Class = function(characters) { this.characters = characters; };
|
||||
|
||||
/* ===== Referenced Rule Existence Checks ===== */
|
||||
|
||||
PEG.Grammar.extendNodes("checkReferencedRulesExist", {
|
||||
Rule:
|
||||
function(grammar) {
|
||||
this._expression.checkReferencedRulesExist(grammar);
|
||||
this.expression.checkReferencedRulesExist(grammar);
|
||||
},
|
||||
|
||||
Choice:
|
||||
function(grammar) {
|
||||
PEG.ArrayUtils.each(this._alternatives, function(alternative) {
|
||||
PEG.ArrayUtils.each(this.alternatives, function(alternative) {
|
||||
alternative.checkReferencedRulesExist(grammar);
|
||||
});
|
||||
},
|
||||
|
||||
Sequence:
|
||||
function(grammar) {
|
||||
PEG.ArrayUtils.each(this._elements, function(element) {
|
||||
PEG.ArrayUtils.each(this.elements, function(element) {
|
||||
element.checkReferencedRulesExist(grammar);
|
||||
});
|
||||
},
|
||||
|
||||
AndPredicate:
|
||||
function(grammar) { this._expression.checkReferencedRulesExist(grammar); },
|
||||
function(grammar) { this.expression.checkReferencedRulesExist(grammar); },
|
||||
|
||||
NotPredicate:
|
||||
function(grammar) { this._expression.checkReferencedRulesExist(grammar); },
|
||||
function(grammar) { this.expression.checkReferencedRulesExist(grammar); },
|
||||
|
||||
Optional:
|
||||
function(grammar) { this._expression.checkReferencedRulesExist(grammar); },
|
||||
function(grammar) { this.expression.checkReferencedRulesExist(grammar); },
|
||||
|
||||
ZeroOrMore:
|
||||
function(grammar) { this._expression.checkReferencedRulesExist(grammar); },
|
||||
function(grammar) { this.expression.checkReferencedRulesExist(grammar); },
|
||||
|
||||
OneOrMore:
|
||||
function(grammar) { this._expression.checkReferencedRulesExist(grammar); },
|
||||
function(grammar) { this.expression.checkReferencedRulesExist(grammar); },
|
||||
|
||||
Action:
|
||||
function(grammar) { this._expression.checkReferencedRulesExist(grammar); },
|
||||
function(grammar) { this.expression.checkReferencedRulesExist(grammar); },
|
||||
|
||||
RuleRef:
|
||||
function(grammar) {
|
||||
if (grammar[this._name] === undefined) {
|
||||
if (grammar[this.name] === undefined) {
|
||||
throw new PEG.Grammar.GrammarError(
|
||||
"Referenced rule \"" + this._name + "\" does not exist."
|
||||
"Referenced rule \"" + this.name + "\" does not exist."
|
||||
);
|
||||
}
|
||||
},
|
||||
|
@ -288,59 +280,59 @@ PEG.Grammar.extendNodes("checkReferencedRulesExist", {
|
|||
PEG.Grammar.extendNodes("checkNoLeftRecursion", {
|
||||
Rule:
|
||||
function(grammar, appliedRules) {
|
||||
this._expression.checkNoLeftRecursion(grammar, appliedRules.concat(this._name));
|
||||
this.expression.checkNoLeftRecursion(grammar, appliedRules.concat(this.name));
|
||||
},
|
||||
|
||||
Choice:
|
||||
function(grammar, appliedRules) {
|
||||
PEG.ArrayUtils.each(this._alternatives, function(alternative) {
|
||||
PEG.ArrayUtils.each(this.alternatives, function(alternative) {
|
||||
alternative.checkNoLeftRecursion(grammar, appliedRules);
|
||||
});
|
||||
},
|
||||
|
||||
Sequence:
|
||||
function(grammar, appliedRules) {
|
||||
if (this._elements.length > 0) {
|
||||
this._elements[0].checkNoLeftRecursion(grammar, appliedRules);
|
||||
if (this.elements.length > 0) {
|
||||
this.elements[0].checkNoLeftRecursion(grammar, appliedRules);
|
||||
}
|
||||
},
|
||||
|
||||
AndPredicate:
|
||||
function(grammar, appliedRules) {
|
||||
this._expression.checkNoLeftRecursion(grammar, appliedRules);
|
||||
this.expression.checkNoLeftRecursion(grammar, appliedRules);
|
||||
},
|
||||
|
||||
NotPredicate:
|
||||
function(grammar, appliedRules) {
|
||||
this._expression.checkNoLeftRecursion(grammar, appliedRules);
|
||||
this.expression.checkNoLeftRecursion(grammar, appliedRules);
|
||||
},
|
||||
|
||||
Optional:
|
||||
function(grammar, appliedRules) {
|
||||
this._expression.checkNoLeftRecursion(grammar, appliedRules);
|
||||
this.expression.checkNoLeftRecursion(grammar, appliedRules);
|
||||
},
|
||||
|
||||
ZeroOrMore:
|
||||
function(grammar, appliedRules) {
|
||||
this._expression.checkNoLeftRecursion(grammar, appliedRules);
|
||||
this.expression.checkNoLeftRecursion(grammar, appliedRules);
|
||||
},
|
||||
|
||||
OneOrMore:
|
||||
function(grammar, appliedRules) {
|
||||
this._expression.checkNoLeftRecursion(grammar, appliedRules);
|
||||
this.expression.checkNoLeftRecursion(grammar, appliedRules);
|
||||
},
|
||||
|
||||
Action:
|
||||
function(grammar, appliedRules) {
|
||||
this._expression.checkNoLeftRecursion(grammar, appliedRules);
|
||||
this.expression.checkNoLeftRecursion(grammar, appliedRules);
|
||||
},
|
||||
|
||||
RuleRef:
|
||||
function(grammar, appliedRules) {
|
||||
if (PEG.ArrayUtils.contains(appliedRules, this._name)) {
|
||||
throw new PEG.Grammar.GrammarError("Left recursion detected for rule \"" + this._name + "\".");
|
||||
if (PEG.ArrayUtils.contains(appliedRules, this.name)) {
|
||||
throw new PEG.Grammar.GrammarError("Left recursion detected for rule \"" + this.name + "\".");
|
||||
}
|
||||
grammar[this._name].checkNoLeftRecursion(grammar, appliedRules);
|
||||
grammar[this.name].checkNoLeftRecursion(grammar, appliedRules);
|
||||
},
|
||||
|
||||
Literal: nop,
|
||||
|
@ -711,7 +703,7 @@ PEG.Compiler = {
|
|||
PEG.Grammar.Rule.prototype.compile = function() {
|
||||
var resultVar = PEG.Compiler.generateUniqueIdentifier("result");
|
||||
|
||||
if (this._displayName !== null) {
|
||||
if (this.displayName !== null) {
|
||||
var setReportMatchFailuresCode = PEG.Compiler.formatCode(
|
||||
"var savedReportMatchFailures = context.reportMatchFailures;",
|
||||
"context.reportMatchFailures = false;"
|
||||
|
@ -724,7 +716,7 @@ PEG.Grammar.Rule.prototype.compile = function() {
|
|||
" this._matchFailed(${displayName|string});",
|
||||
"}",
|
||||
{
|
||||
displayName: this._displayName,
|
||||
displayName: this.displayName,
|
||||
resultVar: resultVar
|
||||
}
|
||||
);
|
||||
|
@ -757,11 +749,11 @@ PEG.Grammar.Rule.prototype.compile = function() {
|
|||
" return ${resultVar};",
|
||||
"},",
|
||||
{
|
||||
name: this._name,
|
||||
name: this.name,
|
||||
setReportMatchFailuresCode: setReportMatchFailuresCode,
|
||||
restoreReportMatchFailuresCode: restoreReportMatchFailuresCode,
|
||||
reportMatchFailureCode: reportMatchFailureCode,
|
||||
code: this._expression.compile(resultVar),
|
||||
code: this.expression.compile(resultVar),
|
||||
resultVar: resultVar
|
||||
}
|
||||
);
|
||||
|
@ -790,7 +782,7 @@ PEG.Grammar.Choice.prototype.compile = function(resultVar) {
|
|||
{ resultVar: resultVar }
|
||||
);
|
||||
|
||||
for (var i = this._alternatives.length - 1; i >= 0; i--) {
|
||||
for (var i = this.alternatives.length - 1; i >= 0; i--) {
|
||||
var alternativeResultVar = PEG.Compiler.generateUniqueIdentifier("result");
|
||||
code = PEG.Compiler.formatCode(
|
||||
"${alternativeCode}",
|
||||
|
@ -800,7 +792,7 @@ PEG.Grammar.Choice.prototype.compile = function(resultVar) {
|
|||
" ${code};",
|
||||
"}",
|
||||
{
|
||||
alternativeCode: this._alternatives[i].compile(alternativeResultVar),
|
||||
alternativeCode: this.alternatives[i].compile(alternativeResultVar),
|
||||
alternativeResultVar: alternativeResultVar,
|
||||
code: code,
|
||||
resultVar: resultVar
|
||||
|
@ -814,7 +806,7 @@ PEG.Grammar.Choice.prototype.compile = function(resultVar) {
|
|||
PEG.Grammar.Sequence.prototype.compile = function(resultVar) {
|
||||
var savedPosVar = PEG.Compiler.generateUniqueIdentifier("savedPos");
|
||||
|
||||
var elementResultVars = PEG.ArrayUtils.map(this._elements, function() {
|
||||
var elementResultVars = PEG.ArrayUtils.map(this.elements, function() {
|
||||
return PEG.Compiler.generateUniqueIdentifier("result")
|
||||
});
|
||||
|
||||
|
@ -826,7 +818,7 @@ PEG.Grammar.Sequence.prototype.compile = function(resultVar) {
|
|||
}
|
||||
);
|
||||
|
||||
for (var i = this._elements.length - 1; i >= 0; i--) {
|
||||
for (var i = this.elements.length - 1; i >= 0; i--) {
|
||||
code = PEG.Compiler.formatCode(
|
||||
"${elementCode}",
|
||||
"if (${elementResultVar} !== null) {",
|
||||
|
@ -836,7 +828,7 @@ PEG.Grammar.Sequence.prototype.compile = function(resultVar) {
|
|||
" this._pos = ${savedPosVar};",
|
||||
"}",
|
||||
{
|
||||
elementCode: this._elements[i].compile(elementResultVars[i]),
|
||||
elementCode: this.elements[i].compile(elementResultVars[i]),
|
||||
elementResultVar: elementResultVars[i],
|
||||
code: code,
|
||||
savedPosVar: savedPosVar,
|
||||
|
@ -873,7 +865,7 @@ PEG.Grammar.AndPredicate.prototype.compile = function(resultVar) {
|
|||
" var ${resultVar} = null;",
|
||||
"}",
|
||||
{
|
||||
expressionCode: this._expression.compile(expressionResultVar),
|
||||
expressionCode: this.expression.compile(expressionResultVar),
|
||||
expressionResultVar: expressionResultVar,
|
||||
savedPosVar: savedPosVar,
|
||||
savedReportMatchFailuresVar: savedReportMatchFailuresVar,
|
||||
|
@ -900,7 +892,7 @@ PEG.Grammar.NotPredicate.prototype.compile = function(resultVar) {
|
|||
" this._pos = ${savedPosVar};",
|
||||
"}",
|
||||
{
|
||||
expressionCode: this._expression.compile(expressionResultVar),
|
||||
expressionCode: this.expression.compile(expressionResultVar),
|
||||
expressionResultVar: expressionResultVar,
|
||||
savedPosVar: savedPosVar,
|
||||
savedReportMatchFailuresVar: savedReportMatchFailuresVar,
|
||||
|
@ -916,7 +908,7 @@ PEG.Grammar.Optional.prototype.compile = function(resultVar) {
|
|||
"${expressionCode}",
|
||||
"var ${resultVar} = ${expressionResultVar} !== null ? ${expressionResultVar} : '';",
|
||||
{
|
||||
expressionCode: this._expression.compile(expressionResultVar),
|
||||
expressionCode: this.expression.compile(expressionResultVar),
|
||||
expressionResultVar: expressionResultVar,
|
||||
resultVar: resultVar
|
||||
}
|
||||
|
@ -934,7 +926,7 @@ PEG.Grammar.ZeroOrMore.prototype.compile = function(resultVar) {
|
|||
" ${expressionCode}",
|
||||
"}",
|
||||
{
|
||||
expressionCode: this._expression.compile(expressionResultVar),
|
||||
expressionCode: this.expression.compile(expressionResultVar),
|
||||
expressionResultVar: expressionResultVar,
|
||||
resultVar: resultVar
|
||||
}
|
||||
|
@ -956,7 +948,7 @@ PEG.Grammar.OneOrMore.prototype.compile = function(resultVar) {
|
|||
" var ${resultVar} = null;",
|
||||
"}",
|
||||
{
|
||||
expressionCode: this._expression.compile(expressionResultVar),
|
||||
expressionCode: this.expression.compile(expressionResultVar),
|
||||
expressionResultVar: expressionResultVar,
|
||||
resultVar: resultVar
|
||||
}
|
||||
|
@ -975,9 +967,9 @@ PEG.Grammar.Action.prototype.compile = function(resultVar) {
|
|||
|
||||
var expressionResultVar = PEG.Compiler.generateUniqueIdentifier("result");
|
||||
|
||||
if (this._expression instanceof PEG.Grammar.Sequence) {
|
||||
if (this.expression instanceof PEG.Grammar.Sequence) {
|
||||
var params = PEG.ArrayUtils.map(
|
||||
PEG.ArrayUtils.range(1, this._expression.getElements().length + 1),
|
||||
PEG.ArrayUtils.range(1, this.expression.elements.length + 1),
|
||||
function(n) { return "$" + n; }
|
||||
).join(", ");
|
||||
|
||||
|
@ -985,7 +977,7 @@ PEG.Grammar.Action.prototype.compile = function(resultVar) {
|
|||
"(function(${params}) { ${action} }).apply(null, ${expressionResultVar})",
|
||||
{
|
||||
params: params,
|
||||
action: this._action,
|
||||
action: this.action,
|
||||
expressionResultVar: expressionResultVar
|
||||
}
|
||||
);
|
||||
|
@ -993,7 +985,7 @@ PEG.Grammar.Action.prototype.compile = function(resultVar) {
|
|||
var invocationCode = PEG.Compiler.formatCode(
|
||||
"(function($1) { ${action} })(${expressionResultVar})",
|
||||
{
|
||||
action: this._action,
|
||||
action: this.action,
|
||||
expressionResultVar: expressionResultVar
|
||||
}
|
||||
);
|
||||
|
@ -1005,7 +997,7 @@ PEG.Grammar.Action.prototype.compile = function(resultVar) {
|
|||
" ? ${invocationCode}",
|
||||
" : null;",
|
||||
{
|
||||
expressionCode: this._expression.compile(expressionResultVar),
|
||||
expressionCode: this.expression.compile(expressionResultVar),
|
||||
expressionResultVar: expressionResultVar,
|
||||
invocationCode: invocationCode,
|
||||
resultVar: resultVar
|
||||
|
@ -1017,7 +1009,7 @@ PEG.Grammar.RuleRef.prototype.compile = function(resultVar) {
|
|||
return PEG.Compiler.formatCode(
|
||||
"var ${resultVar} = this.${ruleMethod}(context);",
|
||||
{
|
||||
ruleMethod: "_parse_" + this._name,
|
||||
ruleMethod: "_parse_" + this.name,
|
||||
resultVar: resultVar
|
||||
}
|
||||
);
|
||||
|
@ -1035,8 +1027,8 @@ PEG.Grammar.Literal.prototype.compile = function(resultVar) {
|
|||
" }",
|
||||
"}",
|
||||
{
|
||||
value: this._value,
|
||||
length: this._value.length,
|
||||
value: this.value,
|
||||
length: this.value.length,
|
||||
resultVar: resultVar
|
||||
}
|
||||
);
|
||||
|
@ -1063,12 +1055,12 @@ PEG.Grammar.Class.prototype.compile = function(resultVar) {
|
|||
* Stupid IE considers regexps /[]/ and /[^]/ syntactically invalid, so we
|
||||
* translate them into euqivalents it can handle.
|
||||
*/
|
||||
if (this._characters === "") {
|
||||
if (this.characters === "") {
|
||||
var regexp = "/^(?!)/";
|
||||
} else if (this._characters === "^") {
|
||||
} else if (this.characters === "^") {
|
||||
var regexp = "/^[\\S\\s]/";
|
||||
} else {
|
||||
var regexp = "/^[" + this._characters + "]/";
|
||||
var regexp = "/^[" + this.characters + "]/";
|
||||
}
|
||||
|
||||
return PEG.Compiler.formatCode(
|
||||
|
@ -1082,7 +1074,7 @@ PEG.Grammar.Class.prototype.compile = function(resultVar) {
|
|||
" }",
|
||||
"}",
|
||||
{
|
||||
characters: this._characters,
|
||||
characters: this.characters,
|
||||
regexp: regexp,
|
||||
resultVar: resultVar
|
||||
}
|
||||
|
|
|
@ -83,7 +83,7 @@ PEG.grammarParser = (function(){
|
|||
var result0 = result1 !== null
|
||||
? (function($1, $2) {
|
||||
var result = {};
|
||||
PEG.ArrayUtils.each($2, function(rule) { result[rule.getName()] = rule; });
|
||||
PEG.ArrayUtils.each($2, function(rule) { result[rule.name] = rule; });
|
||||
return result;
|
||||
}).apply(null, result1)
|
||||
: null;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
grammar: __ rule+ {
|
||||
var result = {};
|
||||
PEG.ArrayUtils.each($2, function(rule) { result[rule.getName()] = rule; });
|
||||
PEG.ArrayUtils.each($2, function(rule) { result[rule.name] = rule; });
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue