I am writing transpiler (myLang -> JS) using ANTLR (javascript target with visitor).
Focus is on target code generation part, from the parse tree.
As in, how to deal with language source codes variations.
To make question clearer, consider two variations below -
source#1:PRINT 'hello there'
source#2:
varGreeting = 'hey!'
PRINT varGreeting
In case 1, I deal with string. While in case 2, it's a variable. JS target code then needs to be different (below). case 1 with quotes, case 2 without.
target#1 (JS):
console.log("hello there"); // <-- string
target#2 (JS):
var varGreeting = "hey!";
console.log(varGreeting); // <-- var
How can I best disambiguate and generate different code?
At once, I thought of using rule name (ID
, STRLIT
) as bearer of different usages.
But I couldn't find these being exposed in RuleContext API. I looked at java ones, assuming same in JS runtime.
getText()
gives value ('hello there'
, varGreeting
), no meta/attribute info that I can leverage.
I digged into the tree/ctx object and didn't find them in easily consumable way.
Question: how to best go about this, without building ugly hacks? Transpiler seems to be in within use case spot of ANTLR, do I missing something?
(relevant part of) Grammar:
print : PRINTKW (ID | STRLIT) NEWLINE;
STRLIT: '\'' .*? '\'' ;
ID : [a-zA-Z0-9_]+;
Visitor override:
// sample code for generating code for case 1 (with quotes)
myVisitor.prototype.visitPrint = function(ctx) {
const Js =
`console.log("${ctx.getChild(1).getText()}");`;
// ^^ this is the part which needs different treatment for case 1 and 2
// write to file
fs.writeFile(targetFs + fileName + '.js', Js, 'utf8', function (err) {
if (err) return console.log(err);
console.log(`done`);
});
return this.visitChildren(ctx);
};
using ANTLR 4.8
ID
orSTRLIT
"), that would probably make it clearer for me. – sepp2k