diff --git a/docs/guides/javascript-api.md b/docs/guides/javascript-api.md index 8eaa78b..a103fe2 100644 --- a/docs/guides/javascript-api.md +++ b/docs/guides/javascript-api.md @@ -97,7 +97,7 @@ cache | `false` | makes the generated parser cache results, avoiding exponential context | `{}` | contains a map of variables used by `peg.util.vm.runInContext()` when the `output` option is set to `"parser"` dependencies | `{}` | parser dependencies, the value is an object which maps variables used to access the dependencies to module IDs used to load them; valid only when `format` is set to `"amd"`, `"commonjs"`, `"es"`, or `"umd"` exportVar | `null` | name of an optional global variable into which the generated parser object is assigned to when no module loader is detected; valid only when `format` is set to `"globals"` or `"umd"` -features | `null` | map of optional features that are set to `true` by default: `"text"`, `"offset"`, `"range"`, `"location"`, `"expected"`, `"error"` and `"filename"` +features | `null` | map of optional features that are set to `true` by default: `"text"`, `"offset"`, `"range"`, `"location"`, `"expected"`, `"error"`, `"filename"` and `"DefaultTracer"` format | `"bare"` | format of the generated parser (`"amd"`, `"bare"`, `"commonjs"`, `"es"`, `"globals"`, or `"umd"`); valid only when `output` is set to `"source"` header | `null` | adds additional comments or content after the `Generated by ...` comment; this option is only handled if it's an array or a string: optimize | `"speed"` | selects between optimizing the generated parser for parsing speed (`"speed"`) or code size (`"size"`) diff --git a/lib/compiler/passes/generate-js.js b/lib/compiler/passes/generate-js.js index 1d9bced..089970c 100644 --- a/lib/compiler/passes/generate-js.js +++ b/lib/compiler/passes/generate-js.js @@ -1120,62 +1120,73 @@ function generateJS( ast, session, options ) { if ( options.trace ) { - parts.push( [ - "function peg$DefaultTracer() {", - " this.indentLevel = 0;", - "}", - "", - "peg$DefaultTracer.prototype.trace = function(event) {", - " var that = this;", - "", - " function log(event) {", - " function repeat(string, n) {", - " var result = \"\", i;", - "", - " for (i = 0; i < n; i++) {", - " result += string;", - " }", - "", - " return result;", - " }", - "", - " function pad(string, length) {", - " return string + repeat(\" \", length - string.length);", - " }", - "", - " if (typeof console === \"object\") {", // IE 8-10 - " console.log(", - " 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", - " );", - " }", - " }", - "", - " switch (event.type) {", - " case \"rule.enter\":", - " log(event);", - " this.indentLevel++;", - " break;", - "", - " case \"rule.match\":", - " this.indentLevel--;", - " log(event);", - " break;", - "", - " case \"rule.fail\":", - " this.indentLevel--;", - " log(event);", - " break;", - "", - " // istanbul ignore next", - " default:", - " throw new Error(\"Invalid event type: \" + event.type + \".\");", - " }", - "};", - "" - ].join( "\n" ) ); + if ( use( "DefaultTracer" ) ) + + parts.push( [ + "function peg$DefaultTracer() {", + " this.indentLevel = 0;", + "}", + "", + "peg$DefaultTracer.prototype.trace = function(event) {", + " var that = this;", + "", + " function log(event) {", + " function repeat(string, n) {", + " var result = \"\", i;", + "", + " for (i = 0; i < n; i++) {", + " result += string;", + " }", + "", + " return result;", + " }", + "", + " function pad(string, length) {", + " return string + repeat(\" \", length - string.length);", + " }", + "", + " if (typeof console === \"object\") {", // IE 8-10 + " console.log(", + " 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", + " );", + " }", + " }", + "", + " switch (event.type) {", + " case \"rule.enter\":", + " log(event);", + " this.indentLevel++;", + " break;", + "", + " case \"rule.match\":", + " this.indentLevel--;", + " log(event);", + " break;", + "", + " case \"rule.fail\":", + " this.indentLevel--;", + " log(event);", + " break;", + "", + " // istanbul ignore next", + " default:", + " throw new Error(\"Invalid event type: \" + event.type + \".\");", + " }", + "};", + "" + ].join( "\n" ) ); + + else + + parts.push( [ + "var peg$FauxTracer = {", + " trace: function(event) { }", + "};", + "" + ].join( "\n" ) ); } @@ -1257,10 +1268,19 @@ function generateJS( ast, session, options ) { } - parts.push( [ - " var peg$tracer = \"tracer\" in options ? options.tracer : new peg$DefaultTracer();", - "" - ].join( "\n" ) ); + if ( use( "DefaultTracer" ) ) + + parts.push( [ + " var peg$tracer = \"tracer\" in options ? options.tracer : new peg$DefaultTracer();", + "" + ].join( "\n" ) ); + + else + + parts.push( [ + " var peg$tracer = \"tracer\" in options ? options.tracer : peg$FauxTracer;", + "" + ].join( "\n" ) ); } @@ -1595,7 +1615,7 @@ function generateJS( ast, session, options ) { function generateParserObject() { - return options.trace + return options.trace && use( "DefaultTracer" ) ? [ "{", " SyntaxError: peg$SyntaxError,", @@ -1614,7 +1634,7 @@ function generateJS( ast, session, options ) { function generateParserExports() { - return options.trace + return options.trace && use( "DefaultTracer" ) ? [ "{", " peg$SyntaxError as SyntaxError,", diff --git a/lib/typings/api.d.ts b/lib/typings/api.d.ts index f08e728..ccae834 100644 --- a/lib/typings/api.d.ts +++ b/lib/typings/api.d.ts @@ -408,6 +408,7 @@ declare namespace peg { expected: boolean; error: boolean; filename: boolean; + DefaultTracer: boolean; }