Skip to content

Instantly share code, notes, and snippets.

@h4t0n
Created May 8, 2019 21:35
Show Gist options
  • Save h4t0n/71c9167ee0db724bcacce52169f20791 to your computer and use it in GitHub Desktop.
Save h4t0n/71c9167ee0db724bcacce52169f20791 to your computer and use it in GitHub Desktop.
public class Lexer {
private StreamTokenizer input;
public enum TokenType {
INTERFACE("INTERFACE"),
ENTITY("ENTITY"),
EMBEDABLE("EMBEDABLE"),
IMPLEMENTS("IMPLEMENTS"),
L_BRACE("{"),
R_BRACE("}"),
ASTERISK("*"),
COMMA(","),
WORD(""),
EOF(""),
EOL("");
private String keyword;
TokenType(String keyword) {
this.keyword = keyword;
}
public static TokenType getTokenFromKeyword(int type, String keyword) {
for (TokenType tokenType : values()) {
if (tokenType.keyword.equals(keyword)) {
return tokenType;
}
}
switch (type) {
case StreamTokenizer.TT_WORD:
return WORD;
case StreamTokenizer.TT_EOF:
return EOF;
case StreamTokenizer.TT_EOL:
return EOL;
}
return EOF;
}
}
public Lexer(Reader r) {
input = new StreamTokenizer(r);
input.resetSyntax();
input.slashSlashComments(true);
input.slashStarComments(true);
input.wordChars('!', '~');
input.ordinaryChar('/');
input.whitespaceChars('\u0000', ' ');
input.eolIsSignificant(true);
input.commentChar('#');
}
public TokenType nextToken() {
try {
return TokenType.getTokenFromKeyword(input.nextToken(), input.sval);
} catch (IOException e) {
e.printStackTrace();
return TokenType.EOF;
}
}
public String getVal() throws SyntaxError {
if (input.sval == null) throw new SyntaxError("Syntax Error");
return input.sval;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment