package jetbrains.mps.kotlin.constraints;

/*Generated by MPS */

import java.util.regex.Pattern;
import java.util.Arrays;

public abstract class Identifiers {
  /**
   * Strong keywords that cannot be used as identifiers (sorted)
   */
  private static final String[] KEYWORDS = new String[]{"as", "break", "class", "continue", "do", "else", "false", "for", "fun", "if", "in", "interface", "is", "null", "object", "package", "return", "super", "this", "throw", "true", "try", "typealias", "typeof", "val", "var", "when", "while"};


  /**
   * TODO add support for unicode characters
   */
  private static final String LETTER = "_a-zA-Z";
  private static final String CHAR = "0-9" + LETTER;
  public static final Pattern VALID_IDENTIFIER = Pattern.compile("^([" + LETTER + "][" + CHAR + "]*)|`[" + CHAR + "]+`$");

  private static final Pattern REGEXP = Pattern.compile("^[^`\\n\\r]+$", 0);
  public static final Pattern QUOTED_IDENTIFIER = REGEXP;

  public static boolean isKeyword(String identifier) {
    // As array is sorted we can use binary search
    return Arrays.binarySearch(KEYWORDS, identifier) >= 0;
  }

  public static boolean isValidQuotableIdentifier(String identifier) {
    return QUOTED_IDENTIFIER.matcher(identifier).matches();
  }

  public static boolean isValid(String identifier) {
    return VALID_IDENTIFIER.matcher(identifier).matches();
  }

  public static boolean isValidNonKeyword(String str) {
    return isValid(str) && !(isKeyword(str));
  }
}
