/** * makes a best effort to get a precise position for this element * * @param e * @return */ public static WPos getPos(AstElement e) { if (e instanceof AstElementWithSource) { AstElementWithSource ws = (AstElementWithSource) e; return ws.getSource(); } if (e.size() > 0) { // try to find the position by examining the childs int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; for (int i = 0; i < e.size(); i++) { AstElement child = e.get(i); WPos childSource = child.attrSource(); min = Math.min(min, childSource.getLeftPos()); max = Math.max(max, childSource.getRightPos()); } return new WPos( e.get(0).attrSource().getFile(), e.get(0).attrSource().getLineOffsets(), min, max); } // if no childs exist, search a parent element with a explicit position AstElement parent = e.getParent(); while (parent != null) { if (parent instanceof AstElementWithSource) { WPos parentSource = ((AstElementWithSource) parent).getSource(); // use parent position but with size -1, so we do not go into this return new WPos( parentSource.getFile(), parentSource.getLineOffsets(), parentSource.getLeftPos(), parentSource.getLeftPos() - 1); } parent = parent.getParent(); } return new WPos("<source of " + e + " not found>", new LineOffsets(), 0, 0); }
private static void addErrorOrWarning(AstElement e, String msg, ErrorType errorType) throws CompileError { ErrorHandler handler = e.getErrorHandler(); CompileError c = makeCompileError(e, msg, handler, errorType); if (c != null) { handler.sendError(c); } }
private static CompileError makeCompileError( AstElement e, String msg, ErrorHandler handler, CompileError.ErrorType errorType) throws CompileError { WPos pos = e.attrErrorPos(); if (handler.isUnitTestMode()) { throw new CompileError(pos, msg); } ListIterator<CompileError> it = handler.getErrors().listIterator(); while (it.hasNext()) { CompileError err = it.next(); if (err.getSource().getFile().equals(pos.getFile())) { if (bigger(err.getSource(), pos)) { // remove bigger errors it.remove(); } else if (bigger(pos, err.getSource()) || equal(pos, err.getSource())) { // do not add smaller or equal errors return null; } } } return new CompileError(pos, msg, errorType); }
public static WPos getErrorPos(AstElement e) { return e.attrSource(); }
public static ErrorHandler getErrorHandler(AstElement e) { if (e.getParent() == null) { throw new Error("Trying to get error handler of element not attached to root:\n" + e); } return e.getParent().getErrorHandler(); }