Add Vocabulary.getMaxTokenType()

This commit is contained in:
Martin Steiger 2016-03-25 10:34:35 +01:00
parent 67c0ff5d78
commit 6ca812e255
2 changed files with 18 additions and 1 deletions

View File

@ -37,6 +37,14 @@ package org.antlr.v4.runtime;
* @author Sam Harwell
*/
public interface Vocabulary {
/**
* Returns the highest token type value. It can be used to iterate from
* zero to that number, thus querying all stored entries.
* @return the highest token type value
*/
int getMaxTokenType();
/**
* Gets the string literal associated with a token type. The string returned
* by this method, when not {@code null}, can be used unaltered in a parser
@ -85,7 +93,7 @@ public interface Vocabulary {
*
* <ul>
* <li>Tokens created by lexer rules.</li>
* <li>Tokens defined in a {@code tokens{}} block in a lexer or parser
* <li>Tokens defined in a <code>tokens{}</code> block in a lexer or parser
* grammar.</li>
* <li>The implicitly defined {@code EOF} token, which has the token type
* {@link Token#EOF}.</li>

View File

@ -57,6 +57,8 @@ public class VocabularyImpl implements Vocabulary {
private final String[] displayNames;
private final int maxTokenType;
/**
* Constructs a new instance of {@link VocabularyImpl} from the specified
* literal and symbolic token names.
@ -94,6 +96,8 @@ public class VocabularyImpl implements Vocabulary {
this.literalNames = literalNames != null ? literalNames : EMPTY_NAMES;
this.symbolicNames = symbolicNames != null ? symbolicNames : EMPTY_NAMES;
this.displayNames = displayNames != null ? displayNames : EMPTY_NAMES;
this.maxTokenType = Math.max(this.displayNames.length,
Math.max(this.literalNames.length, this.symbolicNames.length));
}
/**
@ -143,6 +147,11 @@ public class VocabularyImpl implements Vocabulary {
return new VocabularyImpl(literalNames, symbolicNames, tokenNames);
}
@Override
public int getMaxTokenType() {
return maxTokenType;
}
@Override
public String getLiteralName(int tokenType) {
if (tokenType >= 0 && tokenType < literalNames.length) {