Browse Source

Make the default tracer an optional feature

master
Futago-za Ryuu 4 years ago
parent
commit
3745195315
  1. 2
      docs/guides/javascript-api.md
  2. 144
      lib/compiler/passes/generate-js.js
  3. 1
      lib/typings/api.d.ts

2
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"`)

144
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,",

1
lib/typings/api.d.ts

@ -408,6 +408,7 @@ declare namespace peg {
expected: boolean;
error: boolean;
filename: boolean;
DefaultTracer: boolean;
}

Loading…
Cancel
Save