Home > Back-end >  Is there a standard function to determine if a String is a valid variable/funciton name in Kotlin/Ja
Is there a standard function to determine if a String is a valid variable/funciton name in Kotlin/Ja

Time:02-21

In Kotlin/Java, is there a standard function to determine if a String is a valid variable/function name without having to wrap it in back-ticks? As in

functionIAmLookingFor("do-something") shouldBe false
functionIAmLookingFor("doSomething") shouldBe true

Edit: I do not want to enclose everything in backticks.

The reason why we need this: we have a tool that serializes instances into compilable Kotlin. And we have encountered the following edge case:

enum class AssetType { TRANSFERRABLE, `NON-TRANSFERRABLE` }

so as we reflect an instance with a field NON-TRANSFERRABLE, we need to wrap it in back-ticks:

val actual = MyAsset( type = `NON-TRANSFERRABLE`

This is why I'm asking this. Currently we are just saying in README that we do not support any names that require back-ticks at this time.

CodePudding user response:

You could do it manually:

Something like this:

boolean isJavaIdentifier(String s) {
  if (s == null || s.isEmpty()) return false;
  if (!Character.isJavaIdentifierStart(s.charAt(0)) {
    return false;
  }
  for (int i = 1, n = s.length(); i < n;   i) {
    if (!Character.isJavaIdentifierPart(s.charAt(i)) {
      return false;
    }
  }
  return true; 
}

I don't know for Kotlin, but I don't think there is much difference using 770grappenmaker answer as reference.

CodePudding user response:

I took a quick look at the kotlin compiler lexer. It has some predefined variables, here is an excerpt:

LETTER = [:letter:]|_
IDENTIFIER_PART=[:digit:]|{LETTER}
PLAIN_IDENTIFIER={LETTER} {IDENTIFIER_PART}*
ESCAPED_IDENTIFIER = `[^`\n] `
IDENTIFIER = {PLAIN_IDENTIFIER}|{ESCAPED_IDENTIFIER}
FIELD_IDENTIFIER = \${IDENTIFIER}

Source: https://github.com/JetBrains/kotlin/blob/master/compiler/psi/src/org/jetbrains/kotlin/lexer/Kotlin.flex

These seem to be some kind of regexes, you could combine them to your needs and just match on them. As far as I can tell, this is how the compiler validates identifier names.

Edit: of course, this is code of the lexer, which means that if it finds any other token, it is invalid. All tokens and how to identify them are defined in that file and in the KtTokens file. You could use this information as a reference to find illegal tokens. For java, use the answer of NoDataFound.

  • Related