Gathering detailed insights and metrics for pegjs-backtrace
Gathering detailed insights and metrics for pegjs-backtrace
Gathering detailed insights and metrics for pegjs-backtrace
Gathering detailed insights and metrics for pegjs-backtrace
npm install pegjs-backtrace
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
44 Stars
39 Commits
4 Forks
5 Watching
1 Branches
4 Contributors
Updated on 17 Jun 2024
JavaScript (96.74%)
PEG.js (3.26%)
Cumulative downloads
Total Downloads
Last day
10.9%
2,450
Compared to previous day
Last week
-13.9%
12,079
Compared to previous week
Last month
0.3%
53,943
Compared to previous month
Last year
64.2%
688,259
Compared to previous year
A tracer module for PEG.js which generates a visual backtrace tree.
This module also supports normal tracing output. See showTrace option for detail.
The following example grammar recognizes arithmetic expressions like 2*(3+4)
.
1start = additive 2 3additive = multiplicative plus additive / multiplicative 4 5multiplicative = primary mult multiplicative / primary 6 7primary = integer / "(" additive ")" 8 9integer = digits:[0-9]+ 10 11plus = "+" 12 13mult = "*"
If you give the 2*(3/4)
which the grammar does not recognize, pegjs-backtrace shows backtrace tree from the maximum failure positions to the start rule as follows.
1npm install pegjs-backtrace
pegjs-backtrace is implemented as a Tracer. When calling the parse
function, pass the pegjs-backtrace instance to the tracer
option.
Then, after parser fails, you can obtain the backtrace string from getBacktraceString()
.
1var Parser = require('./parser'); // parser generated by pegjs --trace 2var Tracer = require('pegjs-backtrace'); 3 4var text = '2*(3/4)'; 5var tracer = new Tracer(text); // input text is required. 6 7try { 8 Parser.parse(text, { tracer:tracer }); 9} catch(e) { 10 console.log(tracer.getBacktraceString()); 11}
Note that --trace
option is required to generated parser.js with pegjs command. If the option is not supplied, tracer feature is disabled.
When creating pegjs-backtrace instance, you can provide some options as follows.
1var Tracer = require('pegjs-backtrace');
2var tracer = new Tracer(text,{
3 parent: null,
4 hiddenPaths: [],
5 useColor: true,
6 showTrace: false,
7 maxSourceLines: 6,
8 maxPathLength: 72,
9 matchesNode: function(node, options) { return true; },
10 output: console.log,
11});
This option specifies a parent Tracer instance. Once the option is given, the parent's trace
method is also called during parsing. The default value is null
.
1var Parser = require('./parser'); 2var Tracer = require('pegjs-backtrace'); 3var tracer = new Tracer(text,{ 4 parent: new Parser.DefaultTracer(), 5});
If true, the output is colored with ANSI escape sequence. Otherwise no escape sequence is used. The default value is true
.
If true, quoted source are shown on the trace nodes. Otherwise source is suppressed. The default value is true
.
The maximum number of lines shown as the quoted source on each trace node. The default value is 6
.
If true, trace log is printed while parsing. The default value is false
.
If the options is true, the rule path is shown instead of the rule name. The default value is false
.
The limit length of the displaying path name. When the path length exeeds to the limit, the path is truncated.
This option specifies path patterns to hide. Any node that matches one of these patterns will be hidden from the trace log.
Note that this option works only with trace log, not for backtrace. The default value is []
.
1var Tracer = require('pegjs-backtrace'); 2var tracer = new Tracer(text,{ 3 hiddenPaths:["integer", "primary/.*"] 4});
The type of the pattern must be string
or RegExp
. Even the pattern is string
, it may also contain RegExp meta characters.
The /
character can be used to represent the hierarchcal path of grammar rules. If the pattern is string like "FOO"
, it is treated as the regular expression /(^|\/)FOO(\/|$)/
.
Custom filtering of nodes printed by the graph.
Two parameters are passed, node
and options
.
The node
object is an internal representation which may be subject to changes.
The options
are the options passed to getParseTreeString()
or
getBacktraceString()
.
{ backtrace: false }
is passed when calling getParseTreeString()
.
{ backtrace: true }
is apssed when calling getBacktraceString()
.
1{ 2 // example of showing only fail nodes when 3 matchesNode: function(node) { 4 if (node.type === "rule.fail") { 5 return true; 6 } else { 7 return false; 8 } 9 } 10}
Sets the output destination for the backtrace. The default is console.log
.
The cursor symbol ^
may points the wrong position if the prefix of the source line contains East Asian full-width Characters.
The generating tree is based on the observable trace events from the pegjs parser. Any local failure inside a grammar rule is ignored. For example, if parser fails while
reading "+"
character in the following additive
rule, we can observe the event that additive
fails but cannot get any information of "+"
fails.
1additive = multiplicative "+" additive / multiplicative
If you would like to see the event on "+"
failure, the "+"
should be an explicit rule like:
1plus = "+" 2additive = multiplicative plus additive / multiplicative
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 3/25 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More