/** * Tells if the source position for the given AST node is plausible. * Does not imply that the source position is correct. * * @param node an AST node * @return <tt>true</tt> iff the AST node has a plausible source position */ public static boolean hasPlausibleSourcePosition(ASTNode node) { return node.getLineNumber() > 0 && node.getLastLineNumber() >= node.getLineNumber() && node.getColumnNumber() > 0 && node.getLastColumnNumber() > node.getColumnNumber(); }
/** * Delegates to the visit method used for this class. * If the visitor is a ClassGenerator, then * {@link ClassGenerator#visitBytecodeSequence(BytecodeSequence)} * is called with this instance. If the visitor is no * ClassGenerator, then this method will call visit on * each ASTNode element sorted by this class. If one * element is a BytecodeInstruction, then it will be skipped * as it is no ASTNode. * * @param visitor the visitor * @see ClassGenerator */ public void visit(GroovyCodeVisitor visitor) { if (visitor instanceof ClassGenerator) { ClassGenerator gen = (ClassGenerator) visitor; gen.visitBytecodeSequence(this); return; } for (Iterator iterator = instructions.iterator(); iterator.hasNext();) { Object part = (Object) iterator.next(); if (part instanceof ASTNode) { ((ASTNode)part).visit(visitor); } } }
public static void copySourcePosition(ASTNode from, ASTNode to) { to.setLineNumber(from.getLineNumber()); to.setLastLineNumber(from.getLastLineNumber()); to.setColumnNumber(from.getColumnNumber()); to.setLastColumnNumber(from.getLastColumnNumber()); }
private static int getColumn(ASTNode node) { return node.getColumnNumber() > 0 ? node.getColumnNumber() : node.getLastColumnNumber(); } }
public static TextPosition startOf(ASTNode node) { return create(node.getLineNumber(), node.getColumnNumber()); }
private boolean fixLineNumbers(ASTNode node){ if(node.getNodeMetaData(LINE_NUMBERS_FIXED) != null) { return false; node.setNodeMetaData(LINE_NUMBERS_FIXED, LINE_NUMBERS_FIXED); if(node.getLineNumber()>0){ Integer nn = sourceLineNumbers.get(node.getLineNumber()); if(nn!=null){ node.setLineNumber(nn); node.setLineNumber(-1); if(node.getLastLineNumber()>0){ Integer nn = sourceLineNumbers.get(node.getLastLineNumber()); if(nn!=null){ node.setLastLineNumber(nn); node.setLastLineNumber(-1);
private TextPosition startOf(String token, ASTNode node) { // try to compensate for the fact that getLastLineNumber() is sometimes wrong int lastLineIndex = Math.max(node.getLineNumber(), node.getLastLineNumber()) - 1; for (int lineIndex = lastLineIndex; lineIndex >= 0; lineIndex--) { int columnIndex = lineIndex == lastLineIndex ? lines[lineIndex].lastIndexOf(token, node.getColumnNumber() - 1) : lines[lineIndex].lastIndexOf(token); if (columnIndex != -1) return TextPosition.create(lineIndex + 1, columnIndex + 1); } throw new IllegalArgumentException(String.format("token %s not found in expression", token)); } }
protected void addStaticTypeError(final String msg, final ASTNode expr) { if (expr.getColumnNumber() > 0 && expr.getLineNumber() > 0) { addError(StaticTypesTransformation.STATIC_ERROR_PREFIX + msg, expr); } else { if (DEBUG_GENERATED_CODE) { addError(StaticTypesTransformation.STATIC_ERROR_PREFIX + "Error in generated code [" + expr.getText() + "] - " + msg, expr); } // ignore errors which are related to unknown source locations // because they are likely related to generated code } }
protected void configureAST(ASTNode node, AST ast) { if (ast == null) throw new ASTRuntimeException(ast, "PARSER BUG: Tried to configure " + node.getClass().getName() + " with null Node"); node.setColumnNumber(ast.getColumn()); node.setLineNumber(ast.getLine()); if (ast instanceof GroovySourceAST) { node.setLastColumnNumber(((GroovySourceAST) ast).getColumnLast()); node.setLastLineNumber(((GroovySourceAST) ast).getLineLast()); } // TODO we could one day store the Antlr AST on the Groovy AST // node.setCSTNode(ast); }
private static int getLine(ASTNode node) { return node.getLineNumber() > 0 ? node.getLineNumber() : node.getLastLineNumber(); }
public static TextPosition endOf(ASTNode node) { return create(node.getLastLineNumber(), node.getLastColumnNumber()); }
public void onLineNumber(ASTNode statement, String message) { MethodVisitor mv = controller.getMethodVisitor(); if (statement==null) return; int line = statement.getLineNumber(); this.currentASTNode = statement; if (line < 0) return; if (!ASM_DEBUG && line==controller.getLineNumber()) return; controller.setLineNumber(line); if (mv != null) { Label l = new Label(); mv.visitLabel(l); mv.visitLineNumber(line, l); } }
protected void visit(ASTNode e) { LOGGER.log(Level.FINER, "visiting {0}:{1}", new Object[] {sourceUnit.getName(), e.getLineNumber()}); if (e instanceof EmptyExpression) { // working around a bug in EmptyExpression.visit() that doesn't call any method visitEmptyExpression((EmptyExpression) e); } else if (e instanceof EmptyStatement) { // working around a bug in EmptyStatement.visit() that doesn't call any method visitEmptyStatement((EmptyStatement) e); } else { e.visit(this); } }
/** * Return the trimmed last source line corresponding to the specified AST node * * @param node - the Groovy AST node */ protected String lastSourceLineTrimmed(ASTNode node) { return sourceCode.line(node.getLastLineNumber() - 1); }
sn.setSourcePosition(expression);
protected String getLocationText() { String answer = ". "; if (node != null) { answer += "At [" + node.getLineNumber() + ":" + node.getColumnNumber() + "] "; } if (module != null) { answer += module.getDescription(); } if (answer.equals(". ")) { return ""; } return answer; } }
private boolean fixLineNumbers(ASTNode node){ if(node.getNodeMetaData(LINE_NUMBERS_FIXED) != null) { return false; node.setNodeMetaData(LINE_NUMBERS_FIXED, LINE_NUMBERS_FIXED); if(node.getLineNumber()>0){ Integer nn = sourceLineNumbers.get(node.getLineNumber()); if(nn!=null){ node.setLineNumber(nn); node.setLineNumber(-1); if(node.getLastLineNumber()>0){ Integer nn = sourceLineNumbers.get(node.getLastLineNumber()); if(nn!=null){ node.setLastLineNumber(nn); node.setLastLineNumber(-1);
private void logTransformationError(ASTNode astNode, Exception e) { StringBuilder message = new StringBuilder("Fatal error occurred applying query transformations [ " + e.getMessage() + "] to source [" + sourceUnit.getName() + "]. Please report an issue."); StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); message.append(System.getProperty("line.separator")); message.append(sw.toString()); sourceUnit.getErrorCollector().addError(new LocatedMessage(message.toString(), Token.newString(astNode.getText(), astNode.getLineNumber(), astNode.getColumnNumber()), sourceUnit)); }
protected void configureAST(ASTNode node, AST ast) { if (ast==null) throw new ASTRuntimeException(ast, "PARSER BUG: Tried to configure "+node.getClass().getName()+" with null Node"); node.setColumnNumber(ast.getColumn()); node.setLineNumber(ast.getLine()); if (ast instanceof GroovySourceAST) { node.setLastColumnNumber(((GroovySourceAST)ast).getColumnLast()); node.setLastLineNumber(((GroovySourceAST)ast).getLineLast()); } // TODO we could one day store the Antlr AST on the Groovy AST // node.setCSTNode(ast); }
private static int getLimit(ASTNode node, BaseDocument doc, int docLength) { int limit = (node.getLastLineNumber() > 0 && node.getLastColumnNumber() > 0) ? getOffset(doc, node.getLastLineNumber(), node.getLastColumnNumber()) : docLength; if (limit > docLength) { limit = docLength; } return limit; }