Created
November 30, 2017 03:49
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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