@ -9,8 +9,7 @@ var arrays = require("../../utils/arrays"),
function generateJS ( ast , options ) {
function generateJS ( ast , options ) {
/* These only indent non-empty lines to avoid trailing whitespace. */
/* These only indent non-empty lines to avoid trailing whitespace. */
function indent2 ( code ) { return code . replace ( /^(.+)$/gm , ' $1' ) ; }
function indent2 ( code ) { return code . replace ( /^(.+)$/gm , ' $1' ) ; }
function indent4 ( code ) { return code . replace ( /^(.+)$/gm , ' $1' ) ; }
function indent6 ( code ) { return code . replace ( /^(.+)$/gm , ' $1' ) ; }
function indent8 ( code ) { return code . replace ( /^(.+)$/gm , ' $1' ) ; }
function indent10 ( code ) { return code . replace ( /^(.+)$/gm , ' $1' ) ; }
function indent10 ( code ) { return code . replace ( /^(.+)$/gm , ' $1' ) ; }
function generateTables ( ) {
function generateTables ( ) {
@ -755,28 +754,20 @@ function generateJS(ast, options) {
return parts . join ( '\n' ) ;
return parts . join ( '\n' ) ;
}
}
function generateToplevel ( ) {
var parts = [ ] ,
var parts = [ ] ,
startRuleIndices , startRuleIndex ,
startRuleIndices , startRuleIndex ,
startRuleFunctions , startRuleFunction ,
startRuleFunctions , startRuleFunction ,
ruleNames ;
ruleNames ;
parts . push ( [
parts . push ( [
'(function() {' ,
'function peg$subclass(child, parent) {' ,
' "use strict";' ,
'' ,
' /*' ,
' * Generated by PEG.js 0.9.0.' ,
' *' ,
' * http://pegjs.org/' ,
' */' ,
'' ,
' function peg$subclass(child, parent) {' ,
' function ctor() { this.constructor = child; }' ,
' function ctor() { this.constructor = child; }' ,
' ctor.prototype = parent.prototype;' ,
' ctor.prototype = parent.prototype;' ,
' child.prototype = new ctor();' ,
' child.prototype = new ctor();' ,
' }',
'}' ,
'' ,
'' ,
' function peg$SyntaxError(message, expected, location) {',
'function peg$SyntaxError(message, expected, location) {' ,
' this.message = message;' ,
' this.message = message;' ,
' this.expected = expected;' ,
' this.expected = expected;' ,
' this.location = location;' ,
' this.location = location;' ,
@ -785,19 +776,19 @@ function generateJS(ast, options) {
' if (typeof Error.captureStackTrace === "function") {' ,
' if (typeof Error.captureStackTrace === "function") {' ,
' Error.captureStackTrace(this, peg$SyntaxError);' ,
' Error.captureStackTrace(this, peg$SyntaxError);' ,
' }' ,
' }' ,
' }',
' }',
'' ,
'' ,
' peg$subclass(peg$SyntaxError, Error);',
' peg$subclass(peg$SyntaxError, Error);',
''
''
] . join ( '\n' ) ) ;
] . join ( '\n' ) ) ;
if ( options . trace ) {
if ( options . trace ) {
parts . push ( [
parts . push ( [
' function peg$DefaultTracer() {',
' function peg$DefaultTracer() {',
' this.indentLevel = 0;' ,
' this.indentLevel = 0;' ,
' }',
' }',
'' ,
'' ,
' peg$DefaultTracer.prototype.trace = function(event) {',
' peg$DefaultTracer.prototype.trace = function(event) {',
' var that = this;' ,
' var that = this;' ,
'' ,
'' ,
' function log(event) {' ,
' function log(event) {' ,
@ -844,13 +835,13 @@ function generateJS(ast, options) {
' default:' ,
' default:' ,
' throw new Error("Invalid event type: " + event.type + ".");' ,
' throw new Error("Invalid event type: " + event.type + ".");' ,
' }' ,
' }' ,
' };',
' };',
''
''
] . join ( '\n' ) ) ;
] . join ( '\n' ) ) ;
}
}
parts . push ( [
parts . push ( [
' function peg$parse(input, options) {',
' function peg$parse(input, options) {',
' options = options !== void 0 ? options : {};' ,
' options = options !== void 0 ? options : {};' ,
'' ,
'' ,
' var parser = this,' ,
' var parser = this,' ,
@ -889,7 +880,7 @@ function generateJS(ast, options) {
parts . push ( '' ) ;
parts . push ( '' ) ;
parts . push ( indent 8 ( generateTables ( ) ) ) ;
parts . push ( indent 6 ( generateTables ( ) ) ) ;
parts . push ( [
parts . push ( [
'' ,
'' ,
@ -1106,17 +1097,17 @@ function generateJS(ast, options) {
] . join ( '\n' ) ) ;
] . join ( '\n' ) ) ;
if ( options . optimize === "size" ) {
if ( options . optimize === "size" ) {
parts . push ( indent 4 ( generateInterpreter ( ) ) ) ;
parts . push ( indent 2 ( generateInterpreter ( ) ) ) ;
parts . push ( '' ) ;
parts . push ( '' ) ;
} else {
} else {
arrays . each ( ast . rules , function ( rule ) {
arrays . each ( ast . rules , function ( rule ) {
parts . push ( indent 4 ( generateRuleFunction ( rule ) ) ) ;
parts . push ( indent 2 ( generateRuleFunction ( rule ) ) ) ;
parts . push ( '' ) ;
parts . push ( '' ) ;
} ) ;
} ) ;
}
}
if ( ast . initializer ) {
if ( ast . initializer ) {
parts . push ( indent 4 ( ast . initializer . code ) ) ;
parts . push ( indent 2 ( ast . initializer . code ) ) ;
parts . push ( '' ) ;
parts . push ( '' ) ;
}
}
@ -1141,7 +1132,30 @@ function generateJS(ast, options) {
' peg$computeLocation(peg$maxFailPos, peg$maxFailPos)' ,
' 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 {'
' return {'
] . join ( '\n' ) ) ;
] . join ( '\n' ) ) ;
@ -1164,7 +1178,10 @@ function generateJS(ast, options) {
'})()'
'})()'
] . join ( '\n' ) ) ;
] . join ( '\n' ) ) ;
ast . code = parts . join ( '\n' ) ;
return parts . join ( '\n' ) ;
}
ast . code = generateWrapper ( generateToplevel ( ) ) ;
}
}
module . exports = generateJS ;
module . exports = generateJS ;