Search in sources :

Example 31 with ASTNode

use of org.codehaus.groovy.ast.ASTNode in project groovy-core by groovy.

the class VerifierCodeVisitorTest method assertInvalidName.

protected void assertInvalidName(String name) {
    try {
        VerifierCodeVisitor.assertValidIdentifier(name, "variable name", new ASTNode());
        fail("Should have thrown exception due to invalid name: " + name);
    } catch (RuntimeParserException e) {
        System.out.println("Caught invalid exception: " + e);
    }
}
Also used : ASTNode(org.codehaus.groovy.ast.ASTNode) RuntimeParserException(org.codehaus.groovy.syntax.RuntimeParserException)

Example 32 with ASTNode

use of org.codehaus.groovy.ast.ASTNode in project hale by halestudio.

the class ASTToGraphVisitor method createNode.

@Override
public Vertex createNode(Object node) {
    if (node instanceof ASTNode) {
        ASTNode astn = (ASTNode) node;
        Vertex v = graph.addVertex(null);
        // fill vertex with info
        v.setProperty(P_AST_NODE, astn);
        v.setProperty(P_AST_TYPE, astn.getClass().getSimpleName());
        v.setProperty(P_START_LINE, astn.getLineNumber());
        v.setProperty(P_START_COL, astn.getColumnNumber());
        v.setProperty(P_END_LINE, astn.getLastLineNumber());
        v.setProperty(P_END_COL, astn.getLastColumnNumber());
        return v;
    }
    return null;
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) ASTNode(org.codehaus.groovy.ast.ASTNode)

Example 33 with ASTNode

use of org.codehaus.groovy.ast.ASTNode in project freeplane by freeplane.

the class GroovyScript method handleScriptRuntimeException.

private void handleScriptRuntimeException(final GroovyRuntimeException e) {
    outStream.print("message: " + e.getMessage());
    final ModuleNode module = e.getModule();
    final ASTNode astNode = e.getNode();
    int lineNumber = -1;
    if (module != null) {
        lineNumber = module.getLineNumber();
    } else if (astNode != null) {
        lineNumber = astNode.getLineNumber();
    } else {
        lineNumber = findLineNumberInString(e.getMessage(), lineNumber);
    }
    outStream.print("Line number: " + lineNumber);
    errorHandler.gotoLine(lineNumber);
    throw new ExecuteScriptException(e.getMessage() + " at line " + lineNumber, e);
}
Also used : ASTNode(org.codehaus.groovy.ast.ASTNode) ModuleNode(org.codehaus.groovy.ast.ModuleNode)

Example 34 with ASTNode

use of org.codehaus.groovy.ast.ASTNode in project repository-permissions-updater by jenkins-infra.

the class GradleVerifier method getShortName.

public static String getShortName(String contents) {
    String res = null;
    AstBuilder astBuilder = new AstBuilder();
    List<ASTNode> nodes = astBuilder.buildFromString(CompilePhase.SEMANTIC_ANALYSIS, false, contents);
    boolean isDone = false;
    BlockStatement node = (BlockStatement) nodes.get(0);
    for (Statement s : node.getStatements()) {
        Expression e = ((ExpressionStatement) s).getExpression();
        if (e instanceof MethodCallExpression) {
            MethodCallExpression mc = (MethodCallExpression) e;
            if (mc.getMethodAsString().equals("jenkinsPlugin")) {
                Expression jenkinsPlugin = ((ArgumentListExpression) mc.getArguments()).getExpression(0);
                if (jenkinsPlugin instanceof ClosureExpression) {
                    ClosureExpression c = (ClosureExpression) jenkinsPlugin;
                    for (Statement st : ((BlockStatement) c.getCode()).getStatements()) {
                        ExpressionStatement sm = (ExpressionStatement) st;
                        if (sm.getExpression() instanceof BinaryExpression) {
                            BinaryExpression be = (BinaryExpression) sm.getExpression();
                            if (be.getLeftExpression().getText().equals("shortName")) {
                                if (be.getRightExpression() instanceof ConstantExpression) {
                                    res = be.getRightExpression().getText();
                                    isDone = true;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        if (isDone) {
            break;
        }
    }
    return res;
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) AstBuilder(org.codehaus.groovy.ast.builder.AstBuilder) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ASTNode(org.codehaus.groovy.ast.ASTNode) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 35 with ASTNode

use of org.codehaus.groovy.ast.ASTNode in project gradle by gradle.

the class IntegrationTestFixtureVisitor method checkOutputContains.

private void checkOutputContains(MethodCallExpression call) {
    ASTNode receiver = call.getReceiver();
    if (receiver instanceof PropertyExpression) {
        if (((PropertyExpression) receiver).getPropertyAsString().equals("output")) {
            Expression objectExpr = ((PropertyExpression) receiver).getObjectExpression();
            checkIndirectOutputContains(objectExpr, call);
        }
    }
}
Also used : PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ASTNode(org.codehaus.groovy.ast.ASTNode) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression)

Aggregations

ASTNode (org.codehaus.groovy.ast.ASTNode)53 MethodNode (org.codehaus.groovy.ast.MethodNode)22 ClassNode (org.codehaus.groovy.ast.ClassNode)20 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)19 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)19 Expression (org.codehaus.groovy.ast.expr.Expression)18 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)15 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)15 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)13 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)13 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)13 Statement (org.codehaus.groovy.ast.stmt.Statement)13 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)11 Parameter (org.codehaus.groovy.ast.Parameter)11 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)11 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)10 ArrayList (java.util.ArrayList)9 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)9 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)9 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)7