antlr4
antlr4 copied to clipboard
JavaScript: what the heck do visitors return?
visitParent(ctx) {
console.log(JSON.stringify(
visitChildren()[1][0] ); <--- whoa!
}
visitChild(ctx) {
return new Child('something');
}
Now, instead of getting just "Child", visitParent gets Child wrapped in some structure.
To get to the Child object, I have to do something like visitChildren()[1][0]
What's the wrapping stuff? Is [1][0] always correct, to get to the object I actually returned?
Insights greatly appreciated.
In Java, if I return new Child, then new Child is what visitParent sees when calling visitChildren.
In JavaScript, Child is wrapped in arrays, how many depends on grammar... weird.
As of #1581, the JavaScript target does not support visitors, at least not in the sense that the other targets do. I am not sure what the specific semantics were before that point, nor have I checked to see if the target was updated after the problems were identified.
@sharwell Too bad. I wish this was documented, wasted a LOT of time on this :(
I guess I should use listeners for now?
@sharwell Is it still the case that the JavaScript target does not support visitors? I experienced similar confusion about return values and ran across this issue while searching for an answer.
@dougshamoo visitor support in JavaScript target is indeed very basic at this point (reason being I only use listeners myself, so no opportunity to test) please feel free to contribute
@ericvergnaud Thanks for the update. I'm relatively new to antlr but would be happy to contribute if possible. Can you think of a good starting point for looking into improving the JavaScript visitor functionality?
Yes, look at the Java reference implementation and try to mimic it, whilst catering for language differences.
Envoyé de mon iPhone
Le 25 juil. 2018 à 00:34, Doug Shamoo [email protected] a écrit :
@ericvergnaud Thanks for the update. I'm relatively new to antlr but would be happy to contribute if possible. Can you think of a good starting point for looking into improving the JavaScript visitor functionality?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.
Thanks for pointing me in the right direction. I'll have to get my use case working another way for the immediate future, but I'll double back when time permits and take a look. Thanks again.
Even in the latest antlr, the Javascript visitor is doing something weird.
For every other target (I'm using Swift and Java targets as well), visit() always receives a ParseTree.
But for Javascript, visit() sometimes receives a ParseTree and sometimes receive an array of length 1 containing a ParseTree.
I've worked around this in my code by overriding visit()
and, if I receive an array, I take the first element and use that instead.
Pseudo-code:
function visit(arrayOrParseTree) {
var parseTree;
if (isArray(arrayOrParseTree)) {
parseTree = arrayOrParseTree[0]
} else {
parseTree = arrayOrParseTree
}
return parseTree.accept(this);
}
Could you please provide a sample of grammar with both options?