Make the default tracer an optional feature

master
Futago-za Ryuu 6 years ago
parent 72ba85b3af
commit 3745195315

@ -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"` 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"` 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"` 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"` 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: 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"`) optimize | `"speed"` | selects between optimizing the generated parser for parsing speed (`"speed"`) or code size (`"size"`)

@ -1120,62 +1120,73 @@ function generateJS( ast, session, options ) {
if ( options.trace ) { if ( options.trace ) {
parts.push( [ if ( use( "DefaultTracer" ) )
"function peg$DefaultTracer() {",
" this.indentLevel = 0;", parts.push( [
"}", "function peg$DefaultTracer() {",
"", " this.indentLevel = 0;",
"peg$DefaultTracer.prototype.trace = function(event) {", "}",
" var that = this;", "",
"", "peg$DefaultTracer.prototype.trace = function(event) {",
" function log(event) {", " var that = this;",
" function repeat(string, n) {", "",
" var result = \"\", i;", " function log(event) {",
"", " function repeat(string, n) {",
" for (i = 0; i < n; i++) {", " var result = \"\", i;",
" result += string;", "",
" }", " for (i = 0; i < n; i++) {",
"", " result += string;",
" return result;", " }",
" }", "",
"", " return result;",
" function pad(string, length) {", " }",
" return string + repeat(\" \", length - string.length);", "",
" }", " 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 + \"-\"", " if (typeof console === \"object\") {", // IE 8-10
" + event.location.end.line + \":\" + event.location.end.column + \" \"", " console.log(",
" + pad(event.type, 10) + \" \"", " event.location.start.line + \":\" + event.location.start.column + \"-\"",
" + repeat(\" \", that.indentLevel) + event.rule", " + 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);", " switch (event.type) {",
" this.indentLevel++;", " case \"rule.enter\":",
" break;", " log(event);",
"", " this.indentLevel++;",
" case \"rule.match\":", " break;",
" this.indentLevel--;", "",
" log(event);", " case \"rule.match\":",
" break;", " this.indentLevel--;",
"", " log(event);",
" case \"rule.fail\":", " break;",
" this.indentLevel--;", "",
" log(event);", " case \"rule.fail\":",
" break;", " this.indentLevel--;",
"", " log(event);",
" // istanbul ignore next", " break;",
" default:", "",
" throw new Error(\"Invalid event type: \" + event.type + \".\");", " // istanbul ignore next",
" }", " default:",
"};", " throw new Error(\"Invalid event type: \" + event.type + \".\");",
"" " }",
].join( "\n" ) ); "};",
""
].join( "\n" ) );
else
parts.push( [
"var peg$FauxTracer = {",
" trace: function(event) { }",
"};",
""
].join( "\n" ) );
} }
@ -1257,10 +1268,19 @@ function generateJS( ast, session, options ) {
} }
parts.push( [ if ( use( "DefaultTracer" ) )
" var peg$tracer = \"tracer\" in options ? options.tracer : new peg$DefaultTracer();",
"" parts.push( [
].join( "\n" ) ); " 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() { function generateParserObject() {
return options.trace return options.trace && use( "DefaultTracer" )
? [ ? [
"{", "{",
" SyntaxError: peg$SyntaxError,", " SyntaxError: peg$SyntaxError,",
@ -1614,7 +1634,7 @@ function generateJS( ast, session, options ) {
function generateParserExports() { function generateParserExports() {
return options.trace return options.trace && use( "DefaultTracer" )
? [ ? [
"{", "{",
" peg$SyntaxError as SyntaxError,", " peg$SyntaxError as SyntaxError,",

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

Loading…
Cancel
Save