Skip to content

Instantly share code, notes, and snippets.

@imjching
Created February 17, 2017 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imjching/9d70ba6ff5c0d16c1955c727b2640819 to your computer and use it in GitHub Desktop.
Save imjching/9d70ba6ff5c0d16c1955c727b2640819 to your computer and use it in GitHub Desktop.
TestLexer.java
import java.io.File;
import java.io.FileInputStream;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.Token;
public class TestLexer {
public static void main(String[] args) throws Exception {
System.out.println("Parsing: " + args[0]);
FileInputStream fis = new FileInputStream(new File(args[0]));
ANTLRInputStream input = new ANTLRInputStream(fis);
ScriptLexer lexer = new ScriptLexer(input);
Token token = lexer.nextToken();
while (token.getType() != ScriptLexer.EOF) {
System.out.println("\t" + getTokenType(token.getType()) + "\t\t" + token.getText());
token = lexer.nextToken();
}
}
private static String getTokenType(int tokenType) {
switch (tokenType) {
case ScriptLexer.STRING:
return "STRING";
case ScriptLexer.LPAREN:
return "LPAREN";
case ScriptLexer.RPAREN:
return "RPAREN";
case ScriptLexer.EQUALS:
return "EQUALS";
case ScriptLexer.SEMICO:
return "SEMICO";
case ScriptLexer.ASSIGN:
return "ASSIGN";
default:
return "OTHER";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment