antlr4 icon indicating copy to clipboard operation
antlr4 copied to clipboard

How to get parent of the node using antlr4 typescript

Open Suraiyab opened this issue 1 year ago • 5 comments

I am generating a parser using below command image

I am unable to get the parent token for the current token using ParserRuleContext, I can't see getParent() function as mentioned in documentations (https://github.com/antlr/antlr4/blob/dev/runtime/JavaScript/src/antlr4/context/ParserRuleContext.d.ts)

Suraiyab avatar Mar 13 '24 06:03 Suraiyab

Have you tried parentCtx ?Envoyé de mon iPhoneLe 13 mars 2024 à 07:44, Suraiyab @.***> a écrit : I am generating a parser using below command image.png (view on web) I am unable to get the parent token for the current token using ParserRuleContext, I can't see getParent() function as mentioned in documentations (https://github.com/antlr/antlr4/blob/dev/runtime/JavaScript/src/antlr4/context/ParserRuleContext.d.ts)

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: @.***>

ericvergnaud avatar Mar 13 '24 08:03 ericvergnaud

I have tried but parentCtx is also ParserRuleContext

Suraiyab avatar Mar 13 '24 08:03 Suraiyab

Sorry I misread your question. Not sure what you mean by parent token. This concept does not exist in ANTLR.

ericvergnaud avatar Mar 13 '24 10:03 ericvergnaud

For example, for the below block of code

image

I need to get 'table' as the parent of 'column' token and 'column' as the parent of 'dataType' token

Suraiyab avatar Mar 13 '24 10:03 Suraiyab

This is a multi-step navigation in the parse tree. We don't know your grammar, but obviously there would need to be a rule that recognizes the entire input from table to string in the last line. To get 'Dim Legal Entity', you would need to navigate up the parse tree to the node that contains the input, then down the parse tree to the leaf node 'Dim Legal Entity', then get the token for the leaf.

kaby76 avatar Mar 13 '24 11:03 kaby76