fromTokenNames static method

Vocabulary fromTokenNames(
  1. List<String?>? tokenNames
)

Returns a VocabularyImpl instance from the specified set of token names. This method acts as a compatibility layer for the single tokenNames array generated by previous releases of ANTLR.

The resulting vocabulary instance returns null for {@link #getLiteralName(int)} and {@link #getSymbolicName(int)}, and the value from [tokenNames] for the display names.

@param tokenNames The token names, or null if no token names are available. @return A Vocabulary instance which uses tokenNames for the display names of tokens.

Implementation

static Vocabulary fromTokenNames(List<String?>? tokenNames) {
  if (tokenNames == null || tokenNames.isEmpty) {
    return EMPTY_VOCABULARY;
  }

  final literalNames = List<String?>.from(tokenNames);
  final symbolicNames = List<String?>.from(tokenNames);
  for (var i = 0; i < tokenNames.length; i++) {
    final tokenName = tokenNames[i];
    if (tokenName == null) {
      continue;
    }

    if (tokenName.isNotEmpty) {
      final firstChar = tokenName[0];
      if (firstChar == '\'') {
        symbolicNames[i] = null;
        continue;
      } else if (firstChar.toUpperCase() == firstChar) {
        literalNames[i] = null;
        continue;
      }
    }

    // wasn't a literal or symbolic name
    literalNames[i] = null;
    symbolicNames[i] = null;
  }

  return VocabularyImpl(literalNames, symbolicNames, tokenNames);
}