Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I get ParserRuleContext from ASTNode? #38

Closed
oovm opened this issue Sep 19, 2023 · 2 comments
Closed

Can I get ParserRuleContext from ASTNode? #38

oovm opened this issue Sep 19, 2023 · 2 comments
Labels

Comments

@oovm
Copy link

oovm commented Sep 19, 2023

ANTLRPsiNode created via createElement seems unable to obtain ParserRuleContext.

ParserDefinition.createElement(node: ASTNode): PsiElement =  ANTLRPsiNode(node)

I want to get child nodes by judging ParserRuleContext's type.

But the child node is also a ParserRuleContext, I don’t know how to convert it to a PsiElement

@ris58h
Copy link

ris58h commented Oct 15, 2023

You can obtain PsiElement from ASTNode via getPsi method. In Kotlin it's just psi property. So node.psi will do the trick.

@bjansen
Copy link
Collaborator

bjansen commented Oct 23, 2023

Right, normally you would walk a PSI tree using psiElement.getChildren(), then call child.getNode().getElementType() to know whick kind of node it is. You can't access an instance of ParserRuleContext, but you can do something like this:

var rules  = PSIElementTypeFactory.getRuleIElementTypes(myLanguage);

if (child.getNode().getElementType().equals(rules.get(MyParser.RULE_myRule))) {
    // `child` was created from a parser rule named `myRule`
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants