Ensure that the same grammar and start rule always generate exactly the same parser.

redux
David Majda 14 years ago
parent c3dd696a3e
commit b86a219c86

@ -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());

@ -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(

Loading…
Cancel
Save