KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > parse > BaseParser


1 package polyglot.parse;
2
3 import polyglot.ast.*;
4 import polyglot.lex.*;
5 import polyglot.types.*;
6 import polyglot.util.*;
7 import java_cup.runtime.Symbol;
8
9 import java.util.*;
10
11 public abstract class BaseParser extends java_cup.runtime.lr_parser
12 {
13   public final Lexer lexer;
14   public final ErrorQueue eq;
15   public final TypeSystem ts;
16   public final NodeFactory nf;
17   protected Position prev_pos;
18   protected Position position;
19
20   public BaseParser (Lexer l, TypeSystem t, NodeFactory n, ErrorQueue q) {
21     lexer = l;
22     eq = q;
23     ts = t;
24     nf = n;
25     prev_pos = Position.COMPILER_GENERATED;
26     position = Position.COMPILER_GENERATED;
27   }
28
29   /**
30    * The standard scanning routine for use in the CUP "scan with"
31    * declaration. Should read:
32    * scan with {: return nextSymbol(); :}
33    */

34   public Symbol nextSymbol() throws java.io.IOException JavaDoc {
35     Token t = lexer.nextToken();
36     // use two positions, since the parser does one token lookahead
37
position = prev_pos;
38     prev_pos = t.getPosition();
39     return t.symbol();
40   }
41
42   public Position position() {
43     return position;
44   }
45
46  /**
47   * Override the default CUP routine.
48   */

49   public void report_fatal_error (String JavaDoc message, Object JavaDoc info)
50     throws Exception JavaDoc
51   {
52     report_error (message, info);
53     die ();
54   }
55
56   /**
57    * Report a fatal error then abort parsing.
58    */

59   public void die (String JavaDoc msg, Position pos) throws Exception JavaDoc
60   {
61     report_fatal_error (msg, pos);
62   }
63
64   /**
65    * Report a fatal error then abort parsing.
66    */

67   public void die (Position pos) throws Exception JavaDoc
68   {
69     report_fatal_error ("Syntax error.", pos);
70   }
71
72   /**
73    * Report a fatal error then abort parsing.
74    */

75   public void die () throws Exception JavaDoc
76   {
77     done_parsing();
78     throw new Exception JavaDoc ();
79   }
80
81   protected Position posForObject(Object JavaDoc o) {
82       if (o instanceof Node) {
83           return pos ((Node) o);
84       }
85       else if (o instanceof Token) {
86           return pos ((Token) o);
87       }
88       else if (o instanceof Type) {
89           return pos ((Type) o);
90       }
91       else if (o instanceof List) {
92           return pos ((List) o);
93       }
94       else if (o instanceof VarDeclarator) {
95           return pos ((VarDeclarator) o);
96       }
97       else {
98           return null;
99       }
100   }
101   public Position pos(Object JavaDoc first, Object JavaDoc last){
102       return pos(first, last, first);
103   }
104   public Position pos(Object JavaDoc first, Object JavaDoc last, Object JavaDoc noEndDefault){
105       //System.out.println("first: "+first+" class: "+first.getClass()+" last: "+last+" class: "+last.getClass());
106
Position fpos = posForObject(first);
107       Position epos = posForObject(last);
108
109       if (fpos != null && epos != null) {
110           if (epos.endColumn() != Position.END_UNUSED) {
111               return new Position(fpos, epos);
112           }
113           
114           // the end line and column are not being used in this extension.
115
// so return the default for that case.
116
return posForObject(noEndDefault);
117       }
118       return null;
119
120   }
121   
122   /**
123    * Return the position of the Token.
124    */

125   public Position pos (Token t)
126   {
127     if (t == null) return null;
128     return t.getPosition ();
129   }
130   
131   /**
132    * Return the source position of the Type.
133    */

134   public Position pos (Type n)
135   {
136     if (n == null) return null;
137     return n.position ();
138   }
139
140   /**
141    * Return the source position of the first element in the list to the
142    * last element in the list.
143    */

144   public Position pos (List l)
145   {
146     if (l == null || l.isEmpty ())
147       {
148     return null;
149       }
150
151     return pos(l.get(0), l.get(l.size()-1));
152   }
153
154   /**
155    * Return the source position of the declaration.
156    */

157   public Position pos (VarDeclarator n)
158   {
159     if (n == null) return null;
160     return n.pos;
161   }
162   
163   /**
164    * Return the source position of the Node.
165    */

166   public Position pos (Node n)
167   {
168     if (n == null)
169       {
170     return null;
171       }
172     return n.position ();
173   }
174
175   /**
176    * Return a TypeNode representing a <code>dims</code>-dimensional
177    * array of <code>n</code>.
178    */

179   public TypeNode array (TypeNode n, int dims) throws Exception JavaDoc
180   {
181     if (dims > 0)
182       {
183     if (n instanceof CanonicalTypeNode)
184       {
185         Type t = ((CanonicalTypeNode) n).type ();
186           return nf.CanonicalTypeNode (pos (n), ts.arrayOf (t, dims));
187       }
188     return nf.ArrayTypeNode (pos (n), array (n, dims - 1));
189       }
190     else
191       {
192     return n;
193       }
194   }
195
196   /**
197    * Helper for exprToType.
198    */

199   protected QualifierNode prefixToQualifier (Prefix p) throws Exception JavaDoc
200   {
201     if (p instanceof TypeNode)
202       {
203     return typeToQualifier ((TypeNode) p);
204       }
205
206     if (p instanceof Expr)
207       {
208     return exprToQualifier ((Expr) p);
209       }
210
211     if (p instanceof AmbReceiver)
212       {
213     AmbReceiver a = (AmbReceiver) p;
214
215     if (a.prefix () != null)
216       {
217         return nf.AmbQualifierNode (pos (p),
218                     prefixToQualifier (a.prefix ()),
219                     a.name ());
220       }
221     else
222       {
223         return nf.AmbQualifierNode (pos (p), a.name ());
224       }
225       }
226
227     if (p instanceof AmbPrefix)
228       {
229     AmbPrefix a = (AmbPrefix) p;
230
231     if (a.prefix () != null)
232       {
233         return nf.AmbQualifierNode (pos (p),
234                     prefixToQualifier (a.prefix ()),
235                     a.name ());
236       }
237     else
238       {
239         return nf.AmbQualifierNode (pos (p), a.name ());
240       }
241       }
242
243     die (pos (p));
244     return null;
245   }
246
247   /**
248    * Helper for exprToType.
249    */

250   protected QualifierNode typeToQualifier (TypeNode t) throws Exception JavaDoc
251   {
252     if (t instanceof AmbTypeNode)
253       {
254     AmbTypeNode a = (AmbTypeNode) t;
255
256     if (a.qualifier () != null)
257       {
258         return nf.AmbQualifierNode (pos (t), a.qual (), a.name ());
259       }
260     else
261       {
262         return nf.AmbQualifierNode (pos (t), a.name ());
263       }
264       }
265
266     die (pos (t));
267     return null;
268   }
269   
270   /**
271    * Helper for exprToType.
272    */

273   protected QualifierNode exprToQualifier (Expr e) throws Exception JavaDoc
274   {
275     if (e instanceof AmbExpr)
276       {
277     AmbExpr a = (AmbExpr) e;
278       return nf.AmbQualifierNode (pos (e), a.name ());
279       }
280
281     if (e instanceof Field)
282       {
283     Field f = (Field) e;
284     Receiver r = f.target ();
285     return nf.AmbQualifierNode (pos (e), prefixToQualifier (r),
286                     f.name ());
287       }
288
289     die (pos (e));
290     return null;
291   }
292
293   /**
294    * Convert <code>e</code> into a type, yielding a <code>TypeNode</code>.
295    * This is used by the cast_expression production.
296    */

297   public TypeNode exprToType (Expr e) throws Exception JavaDoc
298   {
299     if (e instanceof AmbExpr)
300       {
301     AmbExpr a = (AmbExpr) e;
302       return nf.AmbTypeNode (pos (e), a.name ());
303       }
304
305     if (e instanceof Field)
306       {
307     Field f = (Field) e;
308     Receiver r = f.target ();
309     return nf.AmbTypeNode (pos (e), prefixToQualifier (r), f.name ());
310       }
311
312     die (pos (e));
313     return null;
314   }
315 }
316
Popular Tags