Grammar-Kit icon indicating copy to clipboard operation
Grammar-Kit copied to clipboard

Recurred token accessors are not generated

Open SOF3 opened this issue 7 years ago • 5 comments
trafficstars

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?

SOF3 avatar Oct 07 '18 10:10 SOF3

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.

gregsh avatar Dec 17 '18 13:12 gregsh

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

SOF3 avatar Dec 17 '18 18:12 SOF3

Why collect all tokens in a list to get the n-th item? One can simply iterate element.getNextSibling() and check the elementType.

gregsh avatar Dec 17 '18 22:12 gregsh

Then what about generating an iterator method for it?

SOF3 avatar Dec 18 '18 00:12 SOF3

Yes, that's possible.

gregsh avatar Dec 18 '18 14:12 gregsh