/** * Each Token object represents one token from the compiler input stream. */ public class Token { /** * Construct a new Token with no attributes except its type. * @param t the token type, one of the types defined by the Token class. */ public Token(int t) { type = t; } /** * Construct a new Token with an associated * String attribute. * @param t the token type (probably Token.ID). * @param s the value of the String attribute for this token */ public Token(int t,String s) { type = t; label = s; } /** * Construct a new Token with an associated * integer value attribute. * @param t the token type (probably Token.INTEGER). * @param v the integer value of the numeric attribute for this token */ public Token(int t,int v) { type = t; value = new Integer(v); } /** * Construct a new Token with an associated * real value attribute. * @param t the token type (probably Token.REAL). * @param v the double value of the numeric attribute for this token */ public Token(int t,double v) { type = t; value = new Double(v); } /** * Get the type of this Token. * @return the Token type */ public int getType() { return type; } /** * Get the String attribute of this object. * @return the Token string */ public String getLabel() { return label; } /** * Get the numeric attribute of this token as an int value. * @return the value of the numeric attribute of this token as an int value */ public int getIntValue() { return value.intValue(); } /** * Get the numeric attribute of this token as a double value. * @return the value of the numeric attribute of this token as a double value */ public double getDoubleValue() { return value.doubleValue(); } /** * Provide the String that describes this Token. */ public String toString() { String s = tokenTypes[type]; if (type == Token.ID) { s += "("+label+")"; } else if (type == Token.INTEGER || type == Token.REAL) { s += "("+value.toString()+")"; } return s; } /** * Provide the String that describes this Token type. * @param type the Token type to be described. Must be a valid * (defined) Token type, or a runtime Exception will occur. * @return the String the describes this Token type. */ public static String typeLabel(int type) { return tokenTypes[type]; } //-------------------------------------------------- private int type; private Number value; private String label; //-------------------------------------------------- /** The token types as Strings for use in toString. The index of * each string must match the Token type so that it gets displayed correctly. */ private static String[] tokenTypes = { "EOF", "LPAREN", "RPAREN", "LCURLY", "RCURLY", "DOT", "COMMA", "SEMICOLON", "COLON", "ID", "INTEGER", "REAL", "OP_ASSIGN", "OP_NOT", "OP_EQ", "OP_LT", "OP_GT", "OP_ADD", "OP_SUB", "OP_MUL", "OP_DIV", "KW_IF", "KW_ELSE", "KW_PROLOG", "KW_MOVIE", "KW_SHOW" }; public static final int EOF = 0; public static final int LPAREN = 1; public static final int RPAREN = 2; public static final int LCURLY = 3; public static final int RCURLY = 4; public static final int DOT = 5; public static final int COMMA = 6; public static final int SEMICOLON = 7; public static final int COLON = 8; public static final int ID = 9; public static final int INTEGER = 10; public static final int REAL = 11; public static final int OP_ASSIGN = 12; public static final int OP_NOT = 13; public static final int OP_EQ = 14; public static final int OP_LT = 15; public static final int OP_GT = 16; public static final int OP_ADD = 17; public static final int OP_SUB = 18; public static final int OP_MUL = 19; public static final int OP_DIV = 20; public static final int KW_IF = 21; public static final int KW_ELSE = 22; public static final int KW_PROLOG = 23; public static final int KW_MOVIE = 24; public static final int KW_SHOW = 25; }