diff --git a/lib/compiler.js b/lib/compiler.js index 2678055..525078c 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -190,6 +190,10 @@ PEG.Compiler = { _uniqueIdentifierCounters: {}, + _resetUniqueIdentifierCounters: function() { + this._uniqueIdentifierCounters = {}; + }, + /* Generates a unique identifier with specified prefix. */ generateUniqueIdentifier: function(prefix) { this._uniqueIdentifierCounters[prefix] @@ -201,6 +205,12 @@ PEG.Compiler = { * Generates a parser from a specified grammar and start rule. */ compileParser: function(grammar, startRule) { + /* + * This ensures that the same grammar and start rule always generate exactly + * the same parser. + */ + this._resetUniqueIdentifierCounters(); + var parseFunctionDefinitions = []; for (var key in grammar) { parseFunctionDefinitions.push(grammar[key].compile()); diff --git a/test/compiler-test.js b/test/compiler-test.js index b54a587..1c24ae1 100644 --- a/test/compiler-test.js +++ b/test/compiler-test.js @@ -309,6 +309,13 @@ test("cache", function() { parses(parser, "ac", ["a", "c"]); }); +test("indempotence", function() { + var parser1 = PEG.buildParser('start: "abcd"'); + var parser2 = PEG.buildParser('start: "abcd"'); + + strictEqual(parser1.toSource(), parser2.toSource()); +}); + test("error messages", function() { var literalParser = PEG.buildParser('start: "abcd"'); doesNotParseWithMessage(