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