KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ppg > test > multi > Token


1 package ppg.test.multi;
2
3 import java.io.*;
4 import java_cup.runtime.Symbol;
5
6 public class Token /*extends Symbol */implements LexerResult {
7
8     private Symbol symbol;
9     private String JavaDoc filename;
10     private int lineno;
11     private Object JavaDoc value; // String, Boolean, Integer, Vector, null
12
//private Position position;
13

14     static int lastID;
15     
16     public Token (String JavaDoc filename, int lineno, Object JavaDoc value/*, Position pos*/) {
17         this (-1, filename, lineno, -1, -1, value/*, pos*/);
18     }
19
20     public Token (int id, String JavaDoc filename, int lineno, int left, int right,
21           Object JavaDoc value/*, Position pos*/) {
22         // super(id, left, right, value);
23

24         symbol = new Symbol (id, left, right, this);
25         lastID=id;
26         this.filename = filename;
27         this.lineno = lineno;
28         this.value = value;
29         //position = pos;
30
}
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 JavaDoc getValue () {
45         return value;
46     }
47
48     public String JavaDoc getID () {
49         return toString(symbol.sym);
50     }
51
52     public static String JavaDoc 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 JavaDoc toString () {
73         return (String JavaDoc) value;
74         //return filename + ":" + lineno + ": \"" + value + "\"";
75
}
76
77     public void unparse (java.io.OutputStream JavaDoc o) {
78         
79         if (value != null) {
80             try {
81                 o.write ((filename + ":" + lineno + ": " + this.getID()+ ": \"" +value + "\"").getBytes());
82                 //o.write (value.toString ().getBytes ());
83
}
84             catch (IOException e) {
85             }
86         }
87     }
88
89     public String JavaDoc 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