antlr4 icon indicating copy to clipboard operation
antlr4 copied to clipboard

JavaScript: what the heck do visitors return?

Open ghost opened this issue 7 years ago • 10 comments

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.

ghost avatar Aug 29 '17 18:08 ghost

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.

ghost avatar Aug 31 '17 23:08 ghost

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 avatar Sep 01 '17 20:09 sharwell

@sharwell Too bad. I wish this was documented, wasted a LOT of time on this :(

I guess I should use listeners for now?

ghost avatar Sep 01 '17 22:09 ghost

@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 avatar Jul 23 '18 21:07 dougshamoo

@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 avatar Jul 24 '18 13:07 ericvergnaud

@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?

dougshamoo avatar Jul 24 '18 16:07 dougshamoo

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.

ericvergnaud avatar Jul 24 '18 21:07 ericvergnaud

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.

dougshamoo avatar Jul 24 '18 22:07 dougshamoo

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);
}

brianguertin avatar May 17 '22 03:05 brianguertin

Could you please provide a sample of grammar with both options?

KvanTTT avatar May 17 '22 09:05 KvanTTT