Reset generated variable names for each rule parsing function

Little change in the source grammar now does not change variables in all
the generated code. This is helpful especially when one has the
generated grammar stored in a VCS (this is true e.g. for our
metagrammar).
redux
David Majda 14 years ago
parent 66de889f4b
commit 70cf4cd94d

@ -240,17 +240,26 @@ PEG.Compiler = {
_uniqueIdentifierCounters: {},
_resetUniqueIdentifierCounters: function() {
this._uniqueIdentifierCounters = {};
},
/* Generates a unique identifier with specified prefix. */
/*
* Generates a unique identifier with specified prefix. The sequence of
* generated identifiers with given prefix is repeatable and will be the same
* within different language runtimes.
*/
generateUniqueIdentifier: function(prefix) {
this._uniqueIdentifierCounters[prefix]
= this._uniqueIdentifierCounters[prefix] || 0;
return prefix + this._uniqueIdentifierCounters[prefix]++;
},
/*
* Resets internal counters of the unique identifier generator. The sequence
* of identifiers with given prefix generated by |generateUniqueIdentifier|
* will start from the beginning.
*/
resetUniqueIdentifierCounters: function() {
this._uniqueIdentifierCounters = {};
},
/*
* 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
@ -457,6 +466,15 @@ PEG.Compiler = {
},
rule: function(node) {
/*
* We want to reset variable names at the beginning of every function so
* that a little change in the source grammar does not change variables in
* all the generated code. This is desired especially when one has the
* generated grammar stored in a VCS (this is true e.g. for our
* metagrammar).
*/
PEG.Compiler.resetUniqueIdentifierCounters();
var resultVar = PEG.Compiler.generateUniqueIdentifier("result");
if (node.displayName !== null) {
@ -859,12 +877,6 @@ PEG.Compiler = {
* generated parser and cause its malfunction.
*/
compileParser: function(ast, startRule) {
/*
* This ensures that the same grammar and start rule always generate exactly
* the same parser.
*/
this._resetUniqueIdentifierCounters();
for (var i = 0; i < this._checks.length; i++) {
this._checks[i](ast, startRule);
}

File diff suppressed because it is too large Load Diff

@ -180,6 +180,20 @@ test("generateUniqueIdentifier", function() {
);
});
test("resetUniqueIdentifierCounters", function() {
var ida1 = PEG.Compiler.generateUniqueIdentifier("a");
var ida2 = PEG.Compiler.generateUniqueIdentifier("a");
var idb1 = PEG.Compiler.generateUniqueIdentifier("b");
var idb2 = PEG.Compiler.generateUniqueIdentifier("b");
PEG.Compiler.resetUniqueIdentifierCounters();
strictEqual(PEG.Compiler.generateUniqueIdentifier("a"), ida1);
strictEqual(PEG.Compiler.generateUniqueIdentifier("a"), ida2);
strictEqual(PEG.Compiler.generateUniqueIdentifier("b"), idb1);
strictEqual(PEG.Compiler.generateUniqueIdentifier("b"), idb2);
});
/* ===== PEG ===== */
module("PEG");

Loading…
Cancel
Save