Skip to content

Instantly share code, notes, and snippets.

@defano
Created November 30, 2017 03:49
Show Gist options
  • Save defano/0148bcf5b7619a79a38aa766e12de10a to your computer and use it in GitHub Desktop.
Save defano/0148bcf5b7619a79a38aa766e12de10a to your computer and use it in GitHub Desktop.
An Antlr4 input stream that lowercases all lexed tokens in the grammar. Useful for supporting case insensitive languages.
/**
* Enables case-insensitive language parsing without changing the actual input script text or affecting literal
* values (i.e., doesn't blindly convert all input to lowercase).
*
* Requires all tokens in the grammar (.g4 file) to be lowercase (i.e., lexer rule 'mouseh' is correct, but 'mouseH' will
* never match).
*
* Pass an instance of this class into your Antlr lexer, for example:
* HyperTalkLexer lexer = new HyperTalkLexer(new CaseInsensitiveInputStream(scriptTextInput));
*/
@SuppressWarnings("deprecation")
public class CaseInsensitiveInputStream extends ANTLRInputStream {
private char[] lowercase;
CaseInsensitiveInputStream(String input) {
super(input);
this.lowercase = input.toLowerCase().toCharArray();
}
@Override
public int LA(int i) {
int data = super.LA(i);
if (data == 0 || data == IntStream.EOF) {
return data;
} else {
return lowercase[index() + i - 1];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment