tl;dr: How do you emulate the equivalent of C's #define
with jison without doing a pre-processing step?
I am working on a relatively simple grammar with a feature to assign an identifier to a chunk of code that can then be re-used later for brevity. Example:
# Valid grammar with various elements of different types
foo x.3 y.4 z.5
# Assign an id to a chunk of code. Everything after -> is assigned to id
fill_1-> bar a.1 b.2 c.3
# Use chunk of code later
# Should be equivalent to parsing: "baz d.4 bar a.1 b.2 c.3 e.5"
baz d.4 ["fill_1"] e.5
So far, I've got my parser setup to correctly identify assignment lines of code and store the part to the right of the '->' in a dictionary available to other parser actions. Code related to the define action provided below:
// Lexer
HSPC [ \t]
ID [a-zA-Z_][a-zA-Z0-9_]*
%%
{ID}{HSPC}*"->" {
this.begin("FINISHLINE");
yytext = yytext.replace("->", "").trim();
return "DEFINE";
}
('"'{ID}'"') {
yytext = yytext.slice(1,-1);
return "QUOTED_ID";
}
<FINISHLINE>.* {
this.begin("INITIAL");
yytext = yytext.trim();
return "REST_OF_LINE";
}
%%
// Parser
statement
: "[" QUOTED_ID "]"
{ $$ = (defines[$2] ? defines[$2] : ""); }
| DEFINE REST_OF_LINE
{
defines[$1] = $2;
}
;
%%
var defines = {};
How can I get jison to actually tokenize and parse that saved snippet of code? Do I need to take an AST approach? Is there a way to inject the code into the parser? Should this happen in the lexing stage or the parsing stage? Would love to hear multiple strategies one could take with short example snippets.
Thanks!