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).
This commit is contained in:
parent
66de889f4b
commit
70cf4cd94d
|
@ -240,17 +240,26 @@ PEG.Compiler = {
|
||||||
|
|
||||||
_uniqueIdentifierCounters: {},
|
_uniqueIdentifierCounters: {},
|
||||||
|
|
||||||
_resetUniqueIdentifierCounters: function() {
|
/*
|
||||||
this._uniqueIdentifierCounters = {};
|
* 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.
|
||||||
/* Generates a unique identifier with specified prefix. */
|
*/
|
||||||
generateUniqueIdentifier: function(prefix) {
|
generateUniqueIdentifier: function(prefix) {
|
||||||
this._uniqueIdentifierCounters[prefix]
|
this._uniqueIdentifierCounters[prefix]
|
||||||
= this._uniqueIdentifierCounters[prefix] || 0;
|
= this._uniqueIdentifierCounters[prefix] || 0;
|
||||||
return prefix + this._uniqueIdentifierCounters[prefix]++;
|
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
|
* 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
|
* that is passed the AST and start rule and does not return anything. If the
|
||||||
|
@ -457,6 +466,15 @@ PEG.Compiler = {
|
||||||
},
|
},
|
||||||
|
|
||||||
rule: function(node) {
|
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");
|
var resultVar = PEG.Compiler.generateUniqueIdentifier("result");
|
||||||
|
|
||||||
if (node.displayName !== null) {
|
if (node.displayName !== null) {
|
||||||
|
@ -859,12 +877,6 @@ PEG.Compiler = {
|
||||||
* generated parser and cause its malfunction.
|
* generated parser and cause its malfunction.
|
||||||
*/
|
*/
|
||||||
compileParser: function(ast, startRule) {
|
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++) {
|
for (var i = 0; i < this._checks.length; i++) {
|
||||||
this._checks[i](ast, startRule);
|
this._checks[i](ast, startRule);
|
||||||
}
|
}
|
||||||
|
|
2424
lib/metagrammar.js
2424
lib/metagrammar.js
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 ===== */
|
/* ===== PEG ===== */
|
||||||
|
|
||||||
module("PEG");
|
module("PEG");
|
||||||
|
|
Loading…
Reference in a new issue