Top Description Fields Constructors Methods
org.antlr.v4.runtime

public Class VocabularyImpl

extends Object
implements Vocabulary
Class Inheritance
All Implemented Interfaces
org.antlr.v4.runtime.Vocabulary
Imports
java.util.Arrays

This class provides a default implementation of the Vocabulary interface.
Author
Sam Harwell

Field Summary

Modifier and TypeField and Description
private final String[]
private static final String[]
public static final VocabularyImpl
EMPTY_VOCABULARY

Gets an empty Vocabulary instance.

private final String[]
private final int
private final String[]

Constructor Summary

AccessConstructor and Description
public
VocabularyImpl(String[]
The literal names assigned to tokens, or null if no literal names are assigned.
literalNames
,
String[]
The symbolic names assigned to tokens, or null if no symbolic names are assigned.
symbolicNames
)

Constructs a new instance of VocabularyImpl from the specified literal and symbolic token names.

public
VocabularyImpl(String[]
The literal names assigned to tokens, or null if no literal names are assigned.
literalNames
,
String[]
The symbolic names assigned to tokens, or null if no symbolic names are assigned.
symbolicNames
,
String[]
The display names assigned to tokens, or null to use the values in literalNames and symbolicNames as the source of display names, as described in getDisplayName(int).
displayNames
)

Constructs a new instance of VocabularyImpl from the specified literal, symbolic, and display token names.

Method Summary

Modifier and TypeMethod and Description
public static Vocabulary

Returns:

A Vocabulary instance which uses tokenNames for the display names of tokens.
fromTokenNames
(String[]
The token names, or null if no token names are available.
tokenNames
)

Returns a VocabularyImpl instance from the specified set of token names.

public String
getDisplayName(int
The token type.
tokenType
)

Implements org.antlr.v4.runtime.Vocabulary.getDisplayName.

Gets the display name of a token type.
public String[]
public String
getLiteralName(int
The token type.
tokenType
)

Implements org.antlr.v4.runtime.Vocabulary.getLiteralName.

Gets the string literal associated with a token type.
public String[]
public int
getMaxTokenType()

Implements org.antlr.v4.runtime.Vocabulary.getMaxTokenType.

Returns the highest token type value.
public String
getSymbolicName(int
The token type.
tokenType
)

Implements org.antlr.v4.runtime.Vocabulary.getSymbolicName.

Gets the symbolic name associated with a token type.
public String[]
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

displayNamesback to summary
private final String[] displayNames
EMPTY_NAMESback to summary
private static final String[] EMPTY_NAMES
EMPTY_VOCABULARYback to summary
public static final VocabularyImpl EMPTY_VOCABULARY

Gets an empty Vocabulary instance.

No literal or symbol names are assigned to token types, so getDisplayName(int) returns the numeric value for all tokens except Token#EOF.

literalNamesback to summary
private final String[] literalNames
maxTokenTypeback to summary
private final int maxTokenType
symbolicNamesback to summary
private final String[] symbolicNames

Constructor Detail

VocabularyImplback to summary
public VocabularyImpl(String[] literalNames, String[] symbolicNames)

Constructs a new instance of VocabularyImpl from the specified literal and symbolic token names.

Parameters
literalNames:String[]

The literal names assigned to tokens, or null if no literal names are assigned.

symbolicNames:String[]

The symbolic names assigned to tokens, or null if no symbolic names are assigned.

See Also
getLiteralName(int), getSymbolicName(int)
VocabularyImplback to summary
public VocabularyImpl(String[] literalNames, String[] symbolicNames, String[] displayNames)

Constructs a new instance of VocabularyImpl from the specified literal, symbolic, and display token names.

Parameters
literalNames:String[]

The literal names assigned to tokens, or null if no literal names are assigned.

symbolicNames:String[]

The symbolic names assigned to tokens, or null if no symbolic names are assigned.

displayNames:String[]

The display names assigned to tokens, or null to use the values in literalNames and symbolicNames as the source of display names, as described in getDisplayName(int).

See Also
getLiteralName(int), getSymbolicName(int), getDisplayName(int)

Method Detail

fromTokenNamesback to summary
public static Vocabulary fromTokenNames(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 getLiteralName(int) and getSymbolicName(int), and the value from tokenNames for the display names.

Parameters
tokenNames:String[]

The token names, or null if no token names are available.

Returns:Vocabulary

A Vocabulary instance which uses tokenNames for the display names of tokens.

getDisplayNameback to summary
public String getDisplayName(int tokenType)

Implements org.antlr.v4.runtime.Vocabulary.getDisplayName.

Doc from org.antlr.v4.runtime.Vocabulary.getDisplayName.

Gets the display name of a token type.

ANTLR provides a default implementation of this method, but applications are free to override the behavior in any manner which makes sense for the application. The default implementation returns the first result from the following list which produces a non-null result.

  1. The result of getLiteralName
  2. The result of getSymbolicName
  3. The result of Integer#toString
Parameters
tokenType:int

The token type.

Returns:String

The display name of the token type, for use in error reporting or other user-visible messages which reference specific token types.

Annotations
@Override
getDisplayNamesback to summary
public String[] getDisplayNames()
getLiteralNameback to summary
public String getLiteralName(int tokenType)

Implements org.antlr.v4.runtime.Vocabulary.getLiteralName.

Doc from org.antlr.v4.runtime.Vocabulary.getLiteralName.

Gets the string literal associated with a token type. The string returned by this method, when not null, can be used unaltered in a parser grammar to represent this token type.

The following table shows examples of lexer rules and the literal names assigned to the corresponding token types.

Rule Literal Name Java String Literal
THIS : 'this'; 'this' "'this'"
SQUOTE : '\''; '\'' "'\\''"
ID : [A-Z]+; n/a null
Parameters
tokenType:int

The token type.

Returns:String

The string literal associated with the specified token type, or null if no string literal is associated with the type.

Annotations
@Override
getLiteralNamesback to summary
public String[] getLiteralNames()
getMaxTokenTypeback to summary
public int getMaxTokenType()

Implements org.antlr.v4.runtime.Vocabulary.getMaxTokenType.

Doc from org.antlr.v4.runtime.Vocabulary.getMaxTokenType.

Returns the highest token type value. It can be used to iterate from zero to that number, inclusively, thus querying all stored entries.

Returns:int

the highest token type value

Annotations
@Override
getSymbolicNameback to summary
public String getSymbolicName(int tokenType)

Implements org.antlr.v4.runtime.Vocabulary.getSymbolicName.

Doc from org.antlr.v4.runtime.Vocabulary.getSymbolicName.

Gets the symbolic name associated with a token type. The string returned by this method, when not null, can be used unaltered in a parser grammar to represent this token type.

This method supports token types defined by any of the following methods:

  • Tokens created by lexer rules.
  • Tokens defined in a tokens{} block in a lexer or parser grammar.
  • The implicitly defined EOF token, which has the token type Token#EOF.

The following table shows examples of lexer rules and the literal names assigned to the corresponding token types.

Rule Symbolic Name
THIS : 'this'; THIS
SQUOTE : '\''; SQUOTE
ID : [A-Z]+; ID
Parameters
tokenType:int

The token type.

Returns:String

The symbolic name associated with the specified token type, or null if no symbolic name is associated with the type.

Annotations
@Override
getSymbolicNamesback to summary
public String[] getSymbolicNames()