| 1 package ppg.test.multi; 2 3 import java.io.*; 4 import java_cup.runtime.Symbol; 5 6 public class Token implements LexerResult { 7 8 private Symbol symbol; 9 private String filename; 10 private int lineno; 11 private Object value; 14 static int lastID; 15 16 public Token (String filename, int lineno, Object value) { 17 this (-1, filename, lineno, -1, -1, value); 18 } 19 20 public Token (int id, String filename, int lineno, int left, int right, 21 Object value) { 22 24 symbol = new Symbol (id, left, right, this); 25 lastID=id; 26 this.filename = filename; 27 this.lineno = lineno; 28 this.value = value; 29 } 31 32 public Token (int i) { 33 this(i, "sdf", -1, -1, -1, null); 34 } 35 36 public int getCode () { 37 return symbol.sym; 38 } 39 40 public Symbol getSymbol () { 41 return symbol; 42 } 43 44 public Object getValue () { 45 return value; 46 } 47 48 public String getID () { 49 return toString(symbol.sym); 50 } 51 52 public static String toString (int type) { 53 switch (type) { 54 case Constant.PLUS: return "+"; 55 case Constant.MINUS: return "-"; 56 case Constant.EXPR: return "expr"; 57 case Constant.NUM: return "num"; 58 case Constant.LPAREN: return "lparen"; 59 case Constant.RPAREN: return "rparen"; 60 61 case Constant.EOF: return "EOF"; 62 case Constant.error: return "ERROR"; 63 64 default: { 65 System.out.println("Invalid token conversion: "+type); 66 System.exit(2); 67 } 68 } 69 return null; 70 } 71 72 public String toString () { 73 return (String ) value; 74 } 76 77 public void unparse (java.io.OutputStream o) { 78 79 if (value != null) { 80 try { 81 o.write ((filename + ":" + lineno + ": " + this.getID()+ ": \"" +value + "\"").getBytes()); 82 } 84 catch (IOException e) { 85 } 86 } 87 } 88 89 public String getFilename () { 90 return filename; 91 } 92 93 public int lineNumber () { 94 return lineno; 95 } 96 97 public int getLineno() { 98 return lineno; 99 } 100 101 public void setLineno (int i) { 102 lineno = i; 103 } 104 } 105 | Popular Tags |