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 34 public Symbol nextSymbol() throws java.io.IOException { 35 Token t = lexer.nextToken(); 36 position = prev_pos; 38 prev_pos = t.getPosition(); 39 return t.symbol(); 40 } 41 42 public Position position() { 43 return position; 44 } 45 46 49 public void report_fatal_error (String message, Object info) 50 throws Exception 51 { 52 report_error (message, info); 53 die (); 54 } 55 56 59 public void die (String msg, Position pos) throws Exception 60 { 61 report_fatal_error (msg, pos); 62 } 63 64 67 public void die (Position pos) throws Exception 68 { 69 report_fatal_error ("Syntax error.", pos); 70 } 71 72 75 public void die () throws Exception 76 { 77 done_parsing(); 78 throw new Exception (); 79 } 80 81 protected Position posForObject(Object 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 first, Object last){ 102 return pos(first, last, first); 103 } 104 public Position pos(Object first, Object last, Object noEndDefault){ 105 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 return posForObject(noEndDefault); 117 } 118 return null; 119 120 } 121 122 125 public Position pos (Token t) 126 { 127 if (t == null) return null; 128 return t.getPosition (); 129 } 130 131 134 public Position pos (Type n) 135 { 136 if (n == null) return null; 137 return n.position (); 138 } 139 140 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 157 public Position pos (VarDeclarator n) 158 { 159 if (n == null) return null; 160 return n.pos; 161 } 162 163 166 public Position pos (Node n) 167 { 168 if (n == null) 169 { 170 return null; 171 } 172 return n.position (); 173 } 174 175 179 public TypeNode array (TypeNode n, int dims) throws Exception 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 199 protected QualifierNode prefixToQualifier (Prefix p) throws Exception 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 250 protected QualifierNode typeToQualifier (TypeNode t) throws Exception 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 273 protected QualifierNode exprToQualifier (Expr e) throws Exception 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 297 public TypeNode exprToType (Expr e) throws Exception 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 |