@ -9,8 +9,7 @@ var arrays = require("../../utils/arrays"),
function generateJS ( ast , options ) {
/* These only indent non-empty lines to avoid trailing whitespace. */
function indent2 ( code ) { return code . replace ( /^(.+)$/gm , ' $1' ) ; }
function indent4 ( code ) { return code . replace ( /^(.+)$/gm , ' $1' ) ; }
function indent8 ( code ) { return code . replace ( /^(.+)$/gm , ' $1' ) ; }
function indent6 ( code ) { return code . replace ( /^(.+)$/gm , ' $1' ) ; }
function indent10 ( code ) { return code . replace ( /^(.+)$/gm , ' $1' ) ; }
function generateTables ( ) {
@ -755,21 +754,13 @@ function generateJS(ast, options) {
return parts . join ( '\n' ) ;
}
function generateToplevel ( ) {
var parts = [ ] ,
startRuleIndices , startRuleIndex ,
startRuleFunctions , startRuleFunction ,
ruleNames ;
parts . push ( [
'(function() {' ,
' "use strict";' ,
'' ,
' /*' ,
' * Generated by PEG.js 0.9.0.' ,
' *' ,
' * http://pegjs.org/' ,
' */' ,
'' ,
'function peg$subclass(child, parent) {' ,
' function ctor() { this.constructor = child; }' ,
' ctor.prototype = parent.prototype;' ,
@ -889,7 +880,7 @@ function generateJS(ast, options) {
parts . push ( '' ) ;
parts . push ( indent 8 ( generateTables ( ) ) ) ;
parts . push ( indent 6 ( generateTables ( ) ) ) ;
parts . push ( [
'' ,
@ -1106,17 +1097,17 @@ function generateJS(ast, options) {
] . join ( '\n' ) ) ;
if ( options . optimize === "size" ) {
parts . push ( indent 4 ( generateInterpreter ( ) ) ) ;
parts . push ( indent 2 ( generateInterpreter ( ) ) ) ;
parts . push ( '' ) ;
} else {
arrays . each ( ast . rules , function ( rule ) {
parts . push ( indent 4 ( generateRuleFunction ( rule ) ) ) ;
parts . push ( indent 2 ( generateRuleFunction ( rule ) ) ) ;
parts . push ( '' ) ;
} ) ;
}
if ( ast . initializer ) {
parts . push ( indent 4 ( ast . initializer . code ) ) ;
parts . push ( indent 2 ( ast . initializer . code ) ) ;
parts . push ( '' ) ;
}
@ -1141,7 +1132,30 @@ function generateJS(ast, options) {
' peg$computeLocation(peg$maxFailPos, peg$maxFailPos)' ,
' );' ,
' }' ,
' }' ,
'}'
] . join ( '\n' ) ) ;
return parts . join ( '\n' ) ;
}
function generateWrapper ( toplevelCode ) {
var parts = [ ] ;
parts . push ( [
'(function() {' ,
' "use strict";' ,
'' ,
' /*' ,
' * Generated by PEG.js 0.9.0.' ,
' *' ,
' * http://pegjs.org/' ,
' */' ,
''
] . join ( '\n' ) ) ;
parts . push ( indent2 ( toplevelCode ) ) ;
parts . push ( [
'' ,
' return {'
] . join ( '\n' ) ) ;
@ -1164,7 +1178,10 @@ function generateJS(ast, options) {
'})()'
] . join ( '\n' ) ) ;
ast . code = parts . join ( '\n' ) ;
return parts . join ( '\n' ) ;
}
ast . code = generateWrapper ( generateToplevel ( ) ) ;
}
module . exports = generateJS ;