Reputation:
I want to parse a string, but this string may contain references to variables that are resolved at run time. Ideally, I'd like to pass in a hash of these variables and their values as the second argument to the parse function.
Currently, I'm running sed -i '' 's/parse: function parse(input) {/parse: function parse(input, in_data) { data = in_data;/' grammar.js
after building my grammar.js from grammar.jison, but that seems like a less than optimal solution. What is the recommended way to do this?
In my jison grammar, I have something like:
start : 'IS' string {$$ = is($2);} ;
and in the module section:
function is(a) {
data.attrs && data.attrs.indexOf(a) >= 0;
}
I want the data
hash to be passed in at run time, so something like:
parse = require("./grammar").parse;
parse("is 'something'", {attrs: ['something', 'else']})
Upvotes: 0
Views: 357
Reputation: 151441
What you are trying to do should be achievable with this directive:
%parse-param data
In my .jison
file, I put this directive just after my %start
directive and just before the %%
line. The name after %parse-param
is the name of a parameter that the parser should expect. This directive above will make it so that there will be a data
variable available inside your parser that will take for value the 2nd parameter passed to parse
. So when you do:
parse = require("./grammar").parse;
parse("is 'something'", {attrs: ['something', 'else']})
then data
should have the value {attrs: ['something', 'else']}
.
Upvotes: 1