Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Vocabulary.getMaxTokenType() #1146

Merged
merged 2 commits into from
Mar 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contributors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,4 @@ YYYY/MM/DD, github id, Full name, email
2015/12/23, pboyer, Peter Boyer, peter.b.boyer@gmail.com
2015/12/24, dtymon, David Tymon, david.tymon@gmail.com
2016/03/27, beardlybread, Bradley Steinbacher, bradley.j.steinbacher@gmail.com
2016/03/29, msteiger, Martin Steiger, antlr@martin-steiger.de
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes invalid curly brackets in JavaDoc.

* 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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very conservative as it even includes display names. Pinging @sharwell for feedback on this one.

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