AST refactoring 1/6: Make properties of AST nodes public

Removed undescore prefix of the properties and deleted getters.
redux
David Majda 14 years ago
parent 1c7c5bb5da
commit 4e99a2bda7

@ -182,97 +182,89 @@ PEG.Grammar.GrammarError.prototype = Error.prototype;
/* ===== PEG.Grammar.* ===== */ /* ===== PEG.Grammar.* ===== */
PEG.Grammar.Rule = function(name, displayName, expression) { PEG.Grammar.Rule = function(name, displayName, expression) {
this._name = name; this.name = name;
this._displayName = displayName; this.displayName = displayName;
this._expression = expression; this.expression = expression;
};
PEG.Grammar.Rule.prototype = {
getName: function() { return this._name; }
}; };
PEG.Grammar.Choice = function(alternatives) { PEG.Grammar.Choice = function(alternatives) {
this._alternatives = alternatives; this.alternatives = alternatives;
}; };
PEG.Grammar.Sequence = function(elements) { this._elements = elements; }; PEG.Grammar.Sequence = function(elements) { this.elements = elements; };
PEG.Grammar.Sequence.prototype = {
getElements: function() { return this._elements; }
};
PEG.Grammar.AndPredicate = function(expression) { PEG.Grammar.AndPredicate = function(expression) {
this._expression = expression; this.expression = expression;
}; };
PEG.Grammar.NotPredicate = function(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) { PEG.Grammar.Action = function(expression, action) {
this._expression = expression; this.expression = expression;
this._action = action; 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.Any = function() {};
PEG.Grammar.Class = function(characters) { this._characters = characters; }; PEG.Grammar.Class = function(characters) { this.characters = characters; };
/* ===== Referenced Rule Existence Checks ===== */ /* ===== Referenced Rule Existence Checks ===== */
PEG.Grammar.extendNodes("checkReferencedRulesExist", { PEG.Grammar.extendNodes("checkReferencedRulesExist", {
Rule: Rule:
function(grammar) { function(grammar) {
this._expression.checkReferencedRulesExist(grammar); this.expression.checkReferencedRulesExist(grammar);
}, },
Choice: Choice:
function(grammar) { function(grammar) {
PEG.ArrayUtils.each(this._alternatives, function(alternative) { PEG.ArrayUtils.each(this.alternatives, function(alternative) {
alternative.checkReferencedRulesExist(grammar); alternative.checkReferencedRulesExist(grammar);
}); });
}, },
Sequence: Sequence:
function(grammar) { function(grammar) {
PEG.ArrayUtils.each(this._elements, function(element) { PEG.ArrayUtils.each(this.elements, function(element) {
element.checkReferencedRulesExist(grammar); element.checkReferencedRulesExist(grammar);
}); });
}, },
AndPredicate: AndPredicate:
function(grammar) { this._expression.checkReferencedRulesExist(grammar); }, function(grammar) { this.expression.checkReferencedRulesExist(grammar); },
NotPredicate: NotPredicate:
function(grammar) { this._expression.checkReferencedRulesExist(grammar); }, function(grammar) { this.expression.checkReferencedRulesExist(grammar); },
Optional: Optional:
function(grammar) { this._expression.checkReferencedRulesExist(grammar); }, function(grammar) { this.expression.checkReferencedRulesExist(grammar); },
ZeroOrMore: ZeroOrMore:
function(grammar) { this._expression.checkReferencedRulesExist(grammar); }, function(grammar) { this.expression.checkReferencedRulesExist(grammar); },
OneOrMore: OneOrMore:
function(grammar) { this._expression.checkReferencedRulesExist(grammar); }, function(grammar) { this.expression.checkReferencedRulesExist(grammar); },
Action: Action:
function(grammar) { this._expression.checkReferencedRulesExist(grammar); }, function(grammar) { this.expression.checkReferencedRulesExist(grammar); },
RuleRef: RuleRef:
function(grammar) { function(grammar) {
if (grammar[this._name] === undefined) { if (grammar[this.name] === undefined) {
throw new PEG.Grammar.GrammarError( 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", { PEG.Grammar.extendNodes("checkNoLeftRecursion", {
Rule: Rule:
function(grammar, appliedRules) { function(grammar, appliedRules) {
this._expression.checkNoLeftRecursion(grammar, appliedRules.concat(this._name)); this.expression.checkNoLeftRecursion(grammar, appliedRules.concat(this.name));
}, },
Choice: Choice:
function(grammar, appliedRules) { function(grammar, appliedRules) {
PEG.ArrayUtils.each(this._alternatives, function(alternative) { PEG.ArrayUtils.each(this.alternatives, function(alternative) {
alternative.checkNoLeftRecursion(grammar, appliedRules); alternative.checkNoLeftRecursion(grammar, appliedRules);
}); });
}, },
Sequence: Sequence:
function(grammar, appliedRules) { function(grammar, appliedRules) {
if (this._elements.length > 0) { if (this.elements.length > 0) {
this._elements[0].checkNoLeftRecursion(grammar, appliedRules); this.elements[0].checkNoLeftRecursion(grammar, appliedRules);
} }
}, },
AndPredicate: AndPredicate:
function(grammar, appliedRules) { function(grammar, appliedRules) {
this._expression.checkNoLeftRecursion(grammar, appliedRules); this.expression.checkNoLeftRecursion(grammar, appliedRules);
}, },
NotPredicate: NotPredicate:
function(grammar, appliedRules) { function(grammar, appliedRules) {
this._expression.checkNoLeftRecursion(grammar, appliedRules); this.expression.checkNoLeftRecursion(grammar, appliedRules);
}, },
Optional: Optional:
function(grammar, appliedRules) { function(grammar, appliedRules) {
this._expression.checkNoLeftRecursion(grammar, appliedRules); this.expression.checkNoLeftRecursion(grammar, appliedRules);
}, },
ZeroOrMore: ZeroOrMore:
function(grammar, appliedRules) { function(grammar, appliedRules) {
this._expression.checkNoLeftRecursion(grammar, appliedRules); this.expression.checkNoLeftRecursion(grammar, appliedRules);
}, },
OneOrMore: OneOrMore:
function(grammar, appliedRules) { function(grammar, appliedRules) {
this._expression.checkNoLeftRecursion(grammar, appliedRules); this.expression.checkNoLeftRecursion(grammar, appliedRules);
}, },
Action: Action:
function(grammar, appliedRules) { function(grammar, appliedRules) {
this._expression.checkNoLeftRecursion(grammar, appliedRules); this.expression.checkNoLeftRecursion(grammar, appliedRules);
}, },
RuleRef: RuleRef:
function(grammar, appliedRules) { function(grammar, appliedRules) {
if (PEG.ArrayUtils.contains(appliedRules, this._name)) { if (PEG.ArrayUtils.contains(appliedRules, this.name)) {
throw new PEG.Grammar.GrammarError("Left recursion detected for rule \"" + 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, Literal: nop,
@ -711,7 +703,7 @@ PEG.Compiler = {
PEG.Grammar.Rule.prototype.compile = function() { PEG.Grammar.Rule.prototype.compile = function() {
var resultVar = PEG.Compiler.generateUniqueIdentifier("result"); var resultVar = PEG.Compiler.generateUniqueIdentifier("result");
if (this._displayName !== null) { if (this.displayName !== null) {
var setReportMatchFailuresCode = PEG.Compiler.formatCode( var setReportMatchFailuresCode = PEG.Compiler.formatCode(
"var savedReportMatchFailures = context.reportMatchFailures;", "var savedReportMatchFailures = context.reportMatchFailures;",
"context.reportMatchFailures = false;" "context.reportMatchFailures = false;"
@ -724,7 +716,7 @@ PEG.Grammar.Rule.prototype.compile = function() {
" this._matchFailed(${displayName|string});", " this._matchFailed(${displayName|string});",
"}", "}",
{ {
displayName: this._displayName, displayName: this.displayName,
resultVar: resultVar resultVar: resultVar
} }
); );
@ -757,11 +749,11 @@ PEG.Grammar.Rule.prototype.compile = function() {
" return ${resultVar};", " return ${resultVar};",
"},", "},",
{ {
name: this._name, name: this.name,
setReportMatchFailuresCode: setReportMatchFailuresCode, setReportMatchFailuresCode: setReportMatchFailuresCode,
restoreReportMatchFailuresCode: restoreReportMatchFailuresCode, restoreReportMatchFailuresCode: restoreReportMatchFailuresCode,
reportMatchFailureCode: reportMatchFailureCode, reportMatchFailureCode: reportMatchFailureCode,
code: this._expression.compile(resultVar), code: this.expression.compile(resultVar),
resultVar: resultVar resultVar: resultVar
} }
); );
@ -790,7 +782,7 @@ PEG.Grammar.Choice.prototype.compile = function(resultVar) {
{ resultVar: 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"); var alternativeResultVar = PEG.Compiler.generateUniqueIdentifier("result");
code = PEG.Compiler.formatCode( code = PEG.Compiler.formatCode(
"${alternativeCode}", "${alternativeCode}",
@ -800,7 +792,7 @@ PEG.Grammar.Choice.prototype.compile = function(resultVar) {
" ${code};", " ${code};",
"}", "}",
{ {
alternativeCode: this._alternatives[i].compile(alternativeResultVar), alternativeCode: this.alternatives[i].compile(alternativeResultVar),
alternativeResultVar: alternativeResultVar, alternativeResultVar: alternativeResultVar,
code: code, code: code,
resultVar: resultVar resultVar: resultVar
@ -814,7 +806,7 @@ PEG.Grammar.Choice.prototype.compile = function(resultVar) {
PEG.Grammar.Sequence.prototype.compile = function(resultVar) { PEG.Grammar.Sequence.prototype.compile = function(resultVar) {
var savedPosVar = PEG.Compiler.generateUniqueIdentifier("savedPos"); 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") 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( code = PEG.Compiler.formatCode(
"${elementCode}", "${elementCode}",
"if (${elementResultVar} !== null) {", "if (${elementResultVar} !== null) {",
@ -836,7 +828,7 @@ PEG.Grammar.Sequence.prototype.compile = function(resultVar) {
" this._pos = ${savedPosVar};", " this._pos = ${savedPosVar};",
"}", "}",
{ {
elementCode: this._elements[i].compile(elementResultVars[i]), elementCode: this.elements[i].compile(elementResultVars[i]),
elementResultVar: elementResultVars[i], elementResultVar: elementResultVars[i],
code: code, code: code,
savedPosVar: savedPosVar, savedPosVar: savedPosVar,
@ -873,7 +865,7 @@ PEG.Grammar.AndPredicate.prototype.compile = function(resultVar) {
" var ${resultVar} = null;", " var ${resultVar} = null;",
"}", "}",
{ {
expressionCode: this._expression.compile(expressionResultVar), expressionCode: this.expression.compile(expressionResultVar),
expressionResultVar: expressionResultVar, expressionResultVar: expressionResultVar,
savedPosVar: savedPosVar, savedPosVar: savedPosVar,
savedReportMatchFailuresVar: savedReportMatchFailuresVar, savedReportMatchFailuresVar: savedReportMatchFailuresVar,
@ -900,7 +892,7 @@ PEG.Grammar.NotPredicate.prototype.compile = function(resultVar) {
" this._pos = ${savedPosVar};", " this._pos = ${savedPosVar};",
"}", "}",
{ {
expressionCode: this._expression.compile(expressionResultVar), expressionCode: this.expression.compile(expressionResultVar),
expressionResultVar: expressionResultVar, expressionResultVar: expressionResultVar,
savedPosVar: savedPosVar, savedPosVar: savedPosVar,
savedReportMatchFailuresVar: savedReportMatchFailuresVar, savedReportMatchFailuresVar: savedReportMatchFailuresVar,
@ -916,7 +908,7 @@ PEG.Grammar.Optional.prototype.compile = function(resultVar) {
"${expressionCode}", "${expressionCode}",
"var ${resultVar} = ${expressionResultVar} !== null ? ${expressionResultVar} : '';", "var ${resultVar} = ${expressionResultVar} !== null ? ${expressionResultVar} : '';",
{ {
expressionCode: this._expression.compile(expressionResultVar), expressionCode: this.expression.compile(expressionResultVar),
expressionResultVar: expressionResultVar, expressionResultVar: expressionResultVar,
resultVar: resultVar resultVar: resultVar
} }
@ -934,7 +926,7 @@ PEG.Grammar.ZeroOrMore.prototype.compile = function(resultVar) {
" ${expressionCode}", " ${expressionCode}",
"}", "}",
{ {
expressionCode: this._expression.compile(expressionResultVar), expressionCode: this.expression.compile(expressionResultVar),
expressionResultVar: expressionResultVar, expressionResultVar: expressionResultVar,
resultVar: resultVar resultVar: resultVar
} }
@ -956,7 +948,7 @@ PEG.Grammar.OneOrMore.prototype.compile = function(resultVar) {
" var ${resultVar} = null;", " var ${resultVar} = null;",
"}", "}",
{ {
expressionCode: this._expression.compile(expressionResultVar), expressionCode: this.expression.compile(expressionResultVar),
expressionResultVar: expressionResultVar, expressionResultVar: expressionResultVar,
resultVar: resultVar resultVar: resultVar
} }
@ -975,9 +967,9 @@ PEG.Grammar.Action.prototype.compile = function(resultVar) {
var expressionResultVar = PEG.Compiler.generateUniqueIdentifier("result"); 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( 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; } function(n) { return "$" + n; }
).join(", "); ).join(", ");
@ -985,7 +977,7 @@ PEG.Grammar.Action.prototype.compile = function(resultVar) {
"(function(${params}) { ${action} }).apply(null, ${expressionResultVar})", "(function(${params}) { ${action} }).apply(null, ${expressionResultVar})",
{ {
params: params, params: params,
action: this._action, action: this.action,
expressionResultVar: expressionResultVar expressionResultVar: expressionResultVar
} }
); );
@ -993,7 +985,7 @@ PEG.Grammar.Action.prototype.compile = function(resultVar) {
var invocationCode = PEG.Compiler.formatCode( var invocationCode = PEG.Compiler.formatCode(
"(function($1) { ${action} })(${expressionResultVar})", "(function($1) { ${action} })(${expressionResultVar})",
{ {
action: this._action, action: this.action,
expressionResultVar: expressionResultVar expressionResultVar: expressionResultVar
} }
); );
@ -1005,7 +997,7 @@ PEG.Grammar.Action.prototype.compile = function(resultVar) {
" ? ${invocationCode}", " ? ${invocationCode}",
" : null;", " : null;",
{ {
expressionCode: this._expression.compile(expressionResultVar), expressionCode: this.expression.compile(expressionResultVar),
expressionResultVar: expressionResultVar, expressionResultVar: expressionResultVar,
invocationCode: invocationCode, invocationCode: invocationCode,
resultVar: resultVar resultVar: resultVar
@ -1017,7 +1009,7 @@ PEG.Grammar.RuleRef.prototype.compile = function(resultVar) {
return PEG.Compiler.formatCode( return PEG.Compiler.formatCode(
"var ${resultVar} = this.${ruleMethod}(context);", "var ${resultVar} = this.${ruleMethod}(context);",
{ {
ruleMethod: "_parse_" + this._name, ruleMethod: "_parse_" + this.name,
resultVar: resultVar resultVar: resultVar
} }
); );
@ -1035,8 +1027,8 @@ PEG.Grammar.Literal.prototype.compile = function(resultVar) {
" }", " }",
"}", "}",
{ {
value: this._value, value: this.value,
length: this._value.length, length: this.value.length,
resultVar: resultVar resultVar: resultVar
} }
); );
@ -1063,12 +1055,12 @@ PEG.Grammar.Class.prototype.compile = function(resultVar) {
* Stupid IE considers regexps /[]/ and /[^]/ syntactically invalid, so we * Stupid IE considers regexps /[]/ and /[^]/ syntactically invalid, so we
* translate them into euqivalents it can handle. * translate them into euqivalents it can handle.
*/ */
if (this._characters === "") { if (this.characters === "") {
var regexp = "/^(?!)/"; var regexp = "/^(?!)/";
} else if (this._characters === "^") { } else if (this.characters === "^") {
var regexp = "/^[\\S\\s]/"; var regexp = "/^[\\S\\s]/";
} else { } else {
var regexp = "/^[" + this._characters + "]/"; var regexp = "/^[" + this.characters + "]/";
} }
return PEG.Compiler.formatCode( return PEG.Compiler.formatCode(
@ -1082,7 +1074,7 @@ PEG.Grammar.Class.prototype.compile = function(resultVar) {
" }", " }",
"}", "}",
{ {
characters: this._characters, characters: this.characters,
regexp: regexp, regexp: regexp,
resultVar: resultVar resultVar: resultVar
} }

@ -83,7 +83,7 @@ PEG.grammarParser = (function(){
var result0 = result1 !== null var result0 = result1 !== null
? (function($1, $2) { ? (function($1, $2) {
var result = {}; var result = {};
PEG.ArrayUtils.each($2, function(rule) { result[rule.getName()] = rule; }); PEG.ArrayUtils.each($2, function(rule) { result[rule.name] = rule; });
return result; return result;
}).apply(null, result1) }).apply(null, result1)
: null; : null;

@ -1,6 +1,6 @@
grammar: __ rule+ { grammar: __ rule+ {
var result = {}; var result = {};
PEG.ArrayUtils.each($2, function(rule) { result[rule.getName()] = rule; }); PEG.ArrayUtils.each($2, function(rule) { result[rule.name] = rule; });
return result; return result;
} }

Loading…
Cancel
Save