This PEG.js plugin removes all actions in a grammar and adds to rules actions returning text and location.
As a result:
- any potential bug in actions is removed, making the grammar strictly syntactic;
- the resulting tree documents captured rules.
When writing complex grammars, it can be useful to first debug syntactic aspects of the grammar with this plugin, then execute the default actions.
import pegjs from "pegjs";
import SyntacticActionsPlugin from "pegjs-syntactic-actions";
const parser = pegjs.generate(
`
rule = "a"+ ( b / c )+
b = "b" { return bug; }
c = "c"
`,
{
plugins: [ new SyntacticActionsPlugin() ]
}
);
console.log( parser.parse( "aabc" ) );
returns:
{
"rule": "rule",
"text": "aabc",
"start": 0,
"end": 4,
"children": [
[
"a",
"a"
],
[
{
"rule": "b",
"text": "b",
"start": 2,
"end": 3,
"children": "b"
},
{
"rule": "c",
"text": "c",
"start": 3,
"end": 4,
"children": "c"
}
]
]
}
Although it would have returned an error without the plugin:
undefined:148
peg$c4 = function() { return bug; },
^
ReferenceError: bug is not defined
npm install pegjs-syntactic-actions
In the second argument options
of pegjs.generate
, add the main object
{
plugins: [ new SyntacticActionsPlugin() ]
}
It can be given an argument options
in the constructor. Currently only one is supported:
ignoredRules
: array of rules names to be completely ignored (these rules will keep their original actions).
For instance:
{
plugins: [ new SyntacticActionsPlugin( { ignoredRules: [ "rule1" ] } ) ]
}
A JSON schema is included in the docs
directory, documenting the output of the parser modified by this plugin. Obviously, if there are ignored rules, these rules can detract the validity of the JSON schema.