Strumenta / antlr-kotlin

Support for Kotlin as a target for ANTLR 4
Apache License 2.0
221 stars 47 forks source link

Token "name" conflicts with enum name #145

Closed piacenti closed 7 months ago

piacenti commented 7 months ago

I updated to the latest version and noticed that if I have a parser rule "name" in my grammar it generates an enum entry of "name" which conflicts with the enum "name" field. Previously it would prefix all rule enum entries with "RULE_" which is verbose but prevents name clashes.

lppedd commented 7 months ago

Checking! Thanks for the report.

lppedd commented 7 months ago

Would it be ok if we capitalize enum names? E.g., from:

public enum class Rules(public val id: kotlin.Int) {
    `line`(1),
    `name`(2),
    `print`(3),
}

to:

public enum class Rules(public val id: kotlin.Int) {
    `Line`(1),
    `Name`(2),
    `Print`(3),
}

I really don't want to prefix names with RULE_, it's ugly and it states again what we already know since we are referring to the Rules enum.

piacenti commented 7 months ago

yeah I think capitalizing makes sense. It is similar to how tokens function while easily distinguishable in code and shouldn't ever have a conflict with language level functionality.

piacenti commented 7 months ago

This should also address "id" which just happened to be another conflict (with constructor id) found in another one of my grammars' parse rules

lppedd commented 7 months ago

I'm trying a different way, without using enums.

Regarding your last comment, do you have an example snippet?

piacenti commented 7 months ago

this is in the grammar:

id : ID COLON STRING ;
name : NAME COLON STRING ;

image

lppedd commented 7 months ago

Ahhhh got it now! Thanks!

lppedd commented 7 months ago

I will replace enums with object-s acting as namespaces.

They will contain const values, which will resolve both issues and also provide a performance boost, since values are inlined at compile time.