Grammar-Kit
Grammar-Kit copied to clipboard
Recurred token accessors are not generated
BNF:
{
generate=[
tokenAccessors="yes"
]
}
<use block> ::= "use" IDENTIFIER IDENTIFIER {
methods=[
target="IDENTIFIER[0]"
alias="IDENTIFIER[1]"
]
}
I expected it to generate getTarget and getAlias, but it doesn't generate anything.
If I change IDENTIFIER to a rule name, this works. If I change the rule to "use" IDENTIFIER, the IDENTIFIER[0] also works.
Is this intended behaviour? Is there a suggested alternative to access both tokens?
Yes, token list are not generated as not very efficient way of accessing PSI. https://github.com/JetBrains/Grammar-Kit/blob/a083a8a6f94527543c4e9a7b0d1605bcc5a773bc/src/org/intellij/grammar/generator/RuleMethodsHelper.java#L166
Workaround is to use custom implementations via methods or class mixins.
Why it's it not efficient if it is a required functionality? I would be executing the same thing if it needs to be executed from the mixin anyway
Why collect all tokens in a list to get the n-th item?
One can simply iterate element.getNextSibling() and check the elementType.
Then what about generating an iterator method for it?
Yes, that's possible.