@ -44,9 +44,10 @@ function generateJavascript(ast, options) {
if ( options . trace ) {
parts . push ( [
'peg$trace({' ,
' type: "rule.enter",' ,
' rule: ' + ruleNameCode ,
'peg$tracer.trace({' ,
' type: "rule.enter",' ,
' rule: ' + ruleNameCode + ',' ,
' location: peg$computeLocation(startPos, startPos)' ,
'});' ,
''
] . join ( '\n' ) ) ;
@ -65,15 +66,17 @@ function generateJavascript(ast, options) {
if ( options . trace ) {
parts . push ( [
'if (cached.result !== peg$FAILED) {' ,
' peg$trace ({',
' peg$trace r.trace ({',
' type: "rule.match",' ,
' rule: ' + ruleNameCode + ',' ,
' result: cached.result' ,
' result: cached.result,' ,
' location: peg$computeLocation(startPos, peg$currPos)' ,
' });' ,
'} else {' ,
' peg$trace ({',
' peg$trace r.trace ({',
' type: "rule.fail",' ,
' rule: ' + ruleNameCode ,
' rule: ' + ruleNameCode + ',' ,
' location: peg$computeLocation(startPos, startPos)' ,
' });' ,
'}' ,
''
@ -104,15 +107,17 @@ function generateJavascript(ast, options) {
parts . push ( [
'' ,
'if (' + resultCode + ' !== peg$FAILED) {' ,
' peg$trace ({',
' peg$trace r.trace ({',
' type: "rule.match",' ,
' rule: ' + ruleNameCode + ',' ,
' result: ' + resultCode ,
' result: ' + resultCode + ',' ,
' location: peg$computeLocation(startPos, peg$currPos)' ,
' });' ,
'} else {' ,
' peg$trace ({',
' peg$trace r.trace ({',
' type: "rule.fail",' ,
' rule: ' + ruleNameCode ,
' rule: ' + ruleNameCode + ',' ,
' location: peg$computeLocation(startPos, startPos)' ,
' });' ,
'}'
] . join ( '\n' ) ) ;
@ -202,15 +207,31 @@ function generateJavascript(ast, options) {
'}' ,
'' ,
'function peg$parseRule(index) {' ,
' var bc = peg$bytecode[index],' ,
' ip = 0,' ,
' ips = [],' ,
' end = bc.length,' ,
' ends = [],' ,
' stack = [],' ,
' params, i;' ,
] . join ( '\n' ) ) ;
if ( options . trace ) {
parts . push ( [
' var bc = peg$bytecode[index],' ,
' ip = 0,' ,
' ips = [],' ,
' end = bc.length,' ,
' ends = [],' ,
' stack = [],' ,
' startPos = peg$currPos,' ,
' params, i;' ,
] . join ( '\n' ) ) ;
} else {
parts . push ( [
' var bc = peg$bytecode[index],' ,
' ip = 0,' ,
' ips = [],' ,
' end = bc.length,' ,
' ends = [],' ,
' stack = [],' ,
' params, i;' ,
] . join ( '\n' ) ) ;
}
parts . push ( indent2 ( generateRuleHeader ( 'peg$ruleNames[index]' , 'index' ) ) ) ;
parts . push ( [
@ -705,10 +726,18 @@ function generateJavascript(ast, options) {
code = compile ( rule . bytecode ) ;
parts . push ( [
'function peg$parse' + rule . name + '() {' ,
' var ' + arrays . map ( arrays . range ( 0 , stack . maxSp + 1 ) , s ) . join ( ', ' ) + ';' ,
] . join ( '\n' ) ) ;
parts . push ( 'function peg$parse' + rule . name + '() {' ) ;
if ( options . trace ) {
parts . push ( [
' var ' + arrays . map ( arrays . range ( 0 , stack . maxSp + 1 ) , s ) . join ( ', ' ) + ',' ,
' startPos = peg$currPos;'
] . join ( '\n' ) ) ;
} else {
parts . push (
' var ' + arrays . map ( arrays . range ( 0 , stack . maxSp + 1 ) , s ) . join ( ', ' ) + ';'
) ;
}
parts . push ( indent2 ( generateRuleHeader (
'"' + js . stringEscape ( rule . name ) + '"' ,
@ -782,7 +811,8 @@ function generateJavascript(ast, options) {
' }' ,
'' ,
' console.log(' ,
' event.line + ":" + event.column + " "' ,
' event.location.start.line + ":" + event.location.start.column + "-"' ,
' + event.location.end.line + ":" + event.location.end.column + " "' ,
' + pad(event.type, 10) + " "' ,
' + repeat(" ", that.indentLevel) + event.rule' ,
' );' ,
@ -1102,21 +1132,6 @@ function generateJavascript(ast, options) {
''
] . join ( '\n' ) ) ;
if ( options . trace ) {
parts . push ( [
' function peg$trace(event) {' ,
' var posDetails = peg$computePosDetails(peg$currPos);' ,
'' ,
' event.offset = peg$currPos;' ,
' event.line = posDetails.line;' ,
' event.column = posDetails.column;' ,
'' ,
' peg$tracer.trace(event);' ,
' }' ,
'' ,
] . join ( '\n' ) ) ;
}
if ( options . optimize === "size" ) {
parts . push ( indent4 ( generateInterpreter ( ) ) ) ;
parts . push ( '' ) ;