Skip to content

Commit

Permalink
Add Vocabulary.getMaxTokenType()
Browse files Browse the repository at this point in the history
  • Loading branch information
msteiger committed Mar 28, 2016
1 parent 67c0ff5 commit 6ca812e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
10 changes: 9 additions & 1 deletion runtime/Java/src/org/antlr/v4/runtime/Vocabulary.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@
* @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
Expand Down Expand Up @@ -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>
Expand Down
9 changes: 9 additions & 0 deletions runtime/Java/src/org/antlr/v4/runtime/VocabularyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -94,6 +96,8 @@ public VocabularyImpl(String[] literalNames, String[] symbolicNames, String[] di
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));
}

/**
Expand Down Expand Up @@ -143,6 +147,11 @@ else if (Character.isUpperCase(firstChar)) {
return new VocabularyImpl(literalNames, symbolicNames, tokenNames);
}

@Override
public int getMaxTokenType() {
return maxTokenType;
}

@Override
public String getLiteralName(int tokenType) {
if (tokenType >= 0 && tokenType < literalNames.length) {
Expand Down

0 comments on commit 6ca812e

Please sign in to comment.