1 package polyglot.visit; 2 3 import polyglot.ast.*; 4 import polyglot.types.*; 5 import polyglot.util.*; 6 import polyglot.frontend.Job; 7 import polyglot.main.Report; 8 import java.util.*; 9 10 12 public class ErrorHandlingVisitor extends HaltingVisitor 13 { 14 protected boolean error; 15 protected Job job; 16 protected TypeSystem ts; 17 protected NodeFactory nf; 18 19 public ErrorHandlingVisitor(Job job, TypeSystem ts, NodeFactory nf) { 20 this.job = job; 21 this.ts = ts; 22 this.nf = nf; 23 } 24 25 29 public Job job() { 30 return job; 31 } 32 33 37 public NodeVisitor begin() { 38 this.error = false; 39 return super.begin(); 40 } 41 42 46 public ErrorQueue errorQueue() { 47 return job.compiler().errorQueue(); 48 } 49 50 54 public NodeFactory nodeFactory() { 55 return nf; 56 } 57 58 62 public TypeSystem typeSystem() { 63 return ts; 64 } 65 66 85 protected NodeVisitor enterCall(Node parent, Node n) 86 throws SemanticException { 87 if (Report.should_report(Report.visit, 3)) 88 Report.report(3, "enter: " + parent + " -> " + n); 89 return enterCall(n); 90 } 91 92 protected NodeVisitor enterCall(Node n) throws SemanticException { 93 return this; 94 } 95 96 104 protected NodeVisitor enterError(Node n) { 105 return this; 106 } 107 108 124 protected Node leaveCall(Node old, Node n, NodeVisitor v) 125 throws SemanticException { 126 127 return leaveCall(n); 128 } 129 130 protected Node leaveCall(Node n) throws SemanticException { 131 return n; 132 } 133 134 135 protected boolean catchErrors(Node n) { 136 return n instanceof Stmt 137 || n instanceof ClassMember 138 || n instanceof ClassDecl 139 || n instanceof Import 140 || n instanceof SourceFile; 141 } 142 143 161 public NodeVisitor enter(Node parent, Node n) { 162 if (Report.should_report(Report.visit, 5)) 163 Report.report(5, "enter(" + n + ")"); 164 165 if (catchErrors(n)) { 166 this.error = false; 167 } 168 169 try { 170 return (ErrorHandlingVisitor) enterCall(parent, n); 172 } 173 catch (SemanticException e) { 174 if (e.getMessage() != null) { 175 Position position = e.position(); 176 177 if (position == null) { 178 position = n.position(); 179 } 180 181 errorQueue().enqueue(ErrorInfo.SEMANTIC_ERROR, 182 e.getMessage(), position); 183 } 184 else { 185 } 188 189 if (! catchErrors(n)) { 190 this.error = true; 191 } 192 193 return enterError(n); 194 } 195 } 196 197 198 226 227 public Node leave(Node parent, Node old, Node n, NodeVisitor v) { 228 try { 229 if (v instanceof ErrorHandlingVisitor && 230 ((ErrorHandlingVisitor) v).error) { 231 232 if (Report.should_report(Report.visit, 5)) 233 Report.report(5, "leave(" + n + "): error below"); 234 235 if (catchErrors(n)) { 236 this.error = false; 237 ((ErrorHandlingVisitor) v).error = false; 238 } 239 else { 240 this.error = true; 242 } 243 244 return n; 246 } 247 248 if (Report.should_report(Report.visit, 5)) 249 Report.report(5, "leave(" + n + "): calling leaveCall"); 250 251 return leaveCall(old, n, v); 252 } 253 catch (SemanticException e) { 254 if (e.getMessage() != null) { 255 Position position = e.position(); 256 257 if (position == null) { 258 position = n.position(); 259 } 260 261 errorQueue().enqueue(ErrorInfo.SEMANTIC_ERROR, 262 e.getMessage(), position); 263 } 264 else { 265 } 268 269 if (catchErrors(n)) { 270 this.error = false; 271 ((ErrorHandlingVisitor) v).error = false; 272 } 273 else { 274 this.error = true; 275 ((ErrorHandlingVisitor) v).error = true; 276 } 277 278 return n; 279 } 280 } 281 } 282 283
| Popular Tags
|