KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ppg > lex > Token


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

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

25         symbol = new Symbol (id, left, right, this);
26         lastID=id;
27         this.filename = filename;
28         this.lineno = lineno;
29         this.value = value;
30         //position = pos;
31
}
32
33     public int getCode () {
34         return symbol.sym;
35     }
36     
37     public Symbol getSymbol () {
38         return symbol;
39     }
40     
41     public Object JavaDoc getValue () {
42         return value;
43     }
44
45     public String JavaDoc getID () {
46         return toString(symbol.sym);
47         /*
48         switch (symbol.sym) {
49
50             // tokens
51             default: break;
52         }
53         throw new IllegalStateException ("Unknown symbol code: " + symbol.sym);
54         */

55     }
56
57     public static String JavaDoc toString (int type) {
58         switch (type) {
59             case Constant.INCLUDE: return "INCLUDE";
60             case Constant.EXTEND: return "EXTEND";
61             case Constant.DROP: return "DROP";
62             case Constant.OVERRIDE: return "OVERRIDE";
63             case Constant.TRANSFER: return "TRANSFER";
64             case Constant.IMPORT: return "IMPORT";
65
66             case Constant.COLON_COLON_EQUALS: return "CCEQ";
67             case Constant.SEMI: return "SEMI";
68             case Constant.COMMA: return "COMMA";
69             case Constant.DOT: return "DOT";
70             case Constant.COLON: return "COLON";
71
72             case Constant.LBRACE: return "LBRACE";
73             case Constant.RBRACE: return "RBRACE";
74             case Constant.LBRACK: return "LBRACK";
75             case Constant.RBRACK: return "RBRACK";
76                     
77             case Constant.ID: return "ID";
78             case Constant.CODE_STR: return "CODE_STR";
79             case Constant.STRING_CONST: return "STRING_CONST";
80                 
81             case Constant.WITH: return "WITH";
82             case Constant.PARSER: return "PARSER";
83             case Constant.INIT: return "INIT";
84             case Constant.STAR: return "STAR";
85             case Constant.BAR: return "BAR";
86             case Constant.SCAN: return "SCAN";
87             case Constant.NON: return "NON";
88             case Constant.CODE: return "CODE";
89             case Constant.LEFT: return "LEFT";
90             case Constant.START: return "START";
91             case Constant.NONTERMINAL: return "NONTERMINAL";
92             case Constant.ACTION: return "ACTION";
93             case Constant.TO: return "TO";
94             case Constant.PACKAGE: return "PACKAGE";
95             case Constant.NONASSOC: return "NONASSOC";
96             case Constant.PRECEDENCE: return "PRECEDENCE";
97             case Constant.PERCENT_PREC: return "PRECEDENCE";
98             case Constant.TERMINAL: return "TERMINAL";
99             case Constant.RIGHT: return "RIGHT";
100                                             
101             case Constant.EOF: return "EOF";
102             case Constant.error: return "ERROR";
103
104             default: {
105                 System.out.println("Invalid token conversion: "+type);
106                 System.exit(2);
107             }
108         }
109         return null;
110     }
111
112     public String JavaDoc toString () {
113         return (String JavaDoc) value;
114         //return filename + ":" + lineno + ": \"" + value + "\"";
115
}
116
117     public void unparse (java.io.OutputStream JavaDoc o) {
118         
119         if (value != null) {
120             try {
121                 o.write ((filename + ":" + lineno + ": " + this.getID()+ ": \"" +value + "\"").getBytes());
122                 //o.write (value.toString ().getBytes ());
123
}
124             catch (IOException e) {
125             }
126         }
127     }
128
129     public String JavaDoc getFilename () {
130         return filename;
131     }
132     
133     public int lineNumber () {
134         return lineno;
135     }
136     
137     public int getLineno() {
138         return lineno;
139     }
140     
141     public void setLineno (int i) {
142         lineno = i;
143     }
144 }
145
Popular Tags