Search in sources :

Example 21 with SyntaxException

use of org.codehaus.groovy.syntax.SyntaxException in project groovy by apache.

the class MacroGroovyMethods method getMacroArguments.

protected static TupleExpression getMacroArguments(SourceUnit source, MethodCallExpression call) {
    Expression macroCallArguments = call.getArguments();
    if (macroCallArguments == null) {
        source.addError(new SyntaxException("Call should have arguments" + '\n', call));
        return null;
    }
    if (!(macroCallArguments instanceof TupleExpression)) {
        source.addError(new SyntaxException("Call should have TupleExpression as arguments" + '\n', macroCallArguments));
        return null;
    }
    TupleExpression tupleArguments = (TupleExpression) macroCallArguments;
    if (tupleArguments.getExpressions() == null) {
        source.addError(new SyntaxException("Call arguments should have expressions" + '\n', tupleArguments));
        return null;
    }
    return tupleArguments;
}
Also used : ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression)

Example 22 with SyntaxException

use of org.codehaus.groovy.syntax.SyntaxException in project groovy by apache.

the class SyntaxErrorReportable method collectSyntaxError.

default void collectSyntaxError(GinqSyntaxError ginqSyntaxError) {
    SourceUnit sourceUnit = getSourceUnit();
    SyntaxException e = new SyntaxException(ginqSyntaxError.getMessage(), ginqSyntaxError, ginqSyntaxError.getLine(), ginqSyntaxError.getColumn());
    sourceUnit.getErrorCollector().addFatalError(new SyntaxErrorMessage(e, sourceUnit));
}
Also used : SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) SourceUnit(org.codehaus.groovy.control.SourceUnit)

Example 23 with SyntaxException

use of org.codehaus.groovy.syntax.SyntaxException in project groovy by apache.

the class TraitASTTransformation method createHelperClass.

private ClassNode createHelperClass(final ClassNode cNode) {
    ClassNode helper = new InnerClassNode(cNode, Traits.helperClassName(cNode), ACC_PUBLIC | ACC_STATIC | ACC_ABSTRACT | ACC_SYNTHETIC, OBJECT_TYPE, ClassNode.EMPTY_ARRAY, null);
    // GROOVY-7242, GROOVY-7456, etc.
    helper.setStaticClass(true);
    cNode.setModifiers(ACC_PUBLIC | ACC_ABSTRACT | ACC_INTERFACE);
    checkInnerClasses(cNode);
    MethodNode initializer = createInitMethod(false, cNode, helper);
    MethodNode staticInitializer = createInitMethod(true, cNode, helper);
    // apply the verifier to have the property nodes generated
    generatePropertyMethods(cNode);
    // prepare fields
    List<FieldNode> fields = new ArrayList<>();
    Set<String> fieldNames = new HashSet<>();
    boolean hasStatic = false;
    for (FieldNode field : cNode.getFields()) {
        if (!"metaClass".equals(field.getName()) && (!field.isSynthetic() || field.getName().indexOf('$') < 0)) {
            fields.add(field);
            fieldNames.add(field.getName());
            if (field.isStatic()) {
                hasStatic = true;
            }
        }
    }
    ClassNode fieldHelper = null;
    ClassNode staticFieldHelper = null;
    if (!fields.isEmpty()) {
        fieldHelper = new InnerClassNode(cNode, Traits.fieldHelperClassName(cNode), ACC_PUBLIC | ACC_STATIC | ACC_ABSTRACT | ACC_INTERFACE | ACC_SYNTHETIC, OBJECT_TYPE);
        fieldHelper.setStaticClass(true);
        if (hasStatic) {
            staticFieldHelper = new InnerClassNode(cNode, Traits.staticFieldHelperClassName(cNode), ACC_PUBLIC | ACC_STATIC | ACC_ABSTRACT | ACC_INTERFACE | ACC_SYNTHETIC, OBJECT_TYPE);
            staticFieldHelper.setStaticClass(true);
        }
    }
    // add fields
    for (FieldNode field : fields) {
        processField(field, initializer, staticInitializer, fieldHelper, helper, staticFieldHelper, cNode, fieldNames);
    }
    // add methods
    List<MethodNode> methods = new ArrayList<>(cNode.getMethods());
    List<MethodNode> nonPublicAPIMethods = new LinkedList<>();
    List<Statement> staticInitStatements = null;
    for (final MethodNode methodNode : methods) {
        boolean declared = methodNode.getDeclaringClass() == cNode;
        if (declared) {
            if (!methodNode.isSynthetic() && (methodNode.isProtected() || (!methodNode.isPrivate() && !methodNode.isPublic()))) {
                sourceUnit.addError(new SyntaxException("Cannot have protected/package-private method in a trait (" + cNode.getName() + "#" + methodNode.getTypeDescriptor() + ")", methodNode.getLineNumber(), methodNode.getColumnNumber()));
                return null;
            }
            if (!methodNode.isAbstract()) {
                MethodNode newMethod = processMethod(cNode, helper, methodNode, fieldHelper, fieldNames);
                if (methodNode.isStaticConstructor()) {
                    staticInitStatements = getCodeAsBlock(newMethod).getStatements();
                } else {
                    // add non-abstract methods; abstract methods covered from trait interface
                    helper.addMethod(newMethod);
                }
            }
            if (methodNode.isPrivate() || methodNode.isStatic()) {
                nonPublicAPIMethods.add(methodNode);
            }
        }
    }
    // remove methods which should not appear in the trait interface
    for (MethodNode privateMethod : nonPublicAPIMethods) {
        cNode.removeMethod(privateMethod);
    }
    // copy statements from static and instance init blocks
    if (staticInitStatements != null) {
        BlockStatement toBlock = getBlockStatement(staticInitializer, staticInitializer.getCode());
        for (Statement next : staticInitStatements) {
            toBlock.addStatement(next);
        }
    }
    List<Statement> initStatements = cNode.getObjectInitializerStatements();
    Statement toCode = initializer.getCode();
    BlockStatement toBlock = getBlockStatement(initializer, toCode);
    for (Statement next : initStatements) {
        Parameter selfParam = createSelfParameter(cNode, false);
        toBlock.addStatement(processBody(varX(selfParam), next, cNode, helper, fieldHelper, fieldNames));
    }
    initStatements.clear();
    // clear properties to avoid generation of methods
    cNode.getProperties().clear();
    // copy annotations
    copyClassAnnotations(cNode, helper);
    markAsGenerated(cNode, helper);
    // reuse the full list of fields
    fields = new ArrayList<>(cNode.getFields());
    for (FieldNode field : fields) {
        cNode.removeField(field.getName());
    }
    // visit AST xforms
    registerASTTransformations(helper);
    sourceUnit.getAST().addClass(helper);
    if (fieldHelper != null) {
        sourceUnit.getAST().addClass(fieldHelper);
        if (staticFieldHelper != null) {
            sourceUnit.getAST().addClass(staticFieldHelper);
        }
    }
    // resolve scope (for closures)
    resolveScope(helper);
    if (fieldHelper != null) {
        resolveScope(fieldHelper);
        if (staticFieldHelper != null) {
            resolveScope(staticFieldHelper);
        }
    }
    return helper;
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ArrayList(java.util.ArrayList) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) LinkedList(java.util.LinkedList) MethodNode(org.codehaus.groovy.ast.MethodNode) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) Parameter(org.codehaus.groovy.ast.Parameter) HashSet(java.util.HashSet)

Example 24 with SyntaxException

use of org.codehaus.groovy.syntax.SyntaxException in project groovy by apache.

the class NAryOperationRewriter method transformPostfixExpression.

private Expression transformPostfixExpression(final PostfixExpression exp) {
    if (isInternalFieldAccess(exp.getExpression())) {
        Token operation = exp.getOperation();
        sourceUnit.addError(new SyntaxException("Postfix expressions on trait fields/properties are not supported in traits.", operation.getStartLine(), operation.getStartColumn()));
        return exp;
    } else {
        return super.transform(exp);
    }
}
Also used : SyntaxException(org.codehaus.groovy.syntax.SyntaxException) Token(org.codehaus.groovy.syntax.Token)

Example 25 with SyntaxException

use of org.codehaus.groovy.syntax.SyntaxException in project midpoint by Evolveum.

the class GroovyScriptEvaluator method getSandboxError.

private String getSandboxError(MultipleCompilationErrorsException e) {
    List<?> errors = e.getErrorCollector().getErrors();
    if (errors == null) {
        return null;
    }
    for (Object error : errors) {
        if (!(error instanceof SyntaxErrorMessage)) {
            continue;
        }
        SyntaxException cause = ((SyntaxErrorMessage) error).getCause();
        if (cause == null) {
            continue;
        }
        String causeMessage = cause.getMessage();
        if (causeMessage == null) {
            continue;
        }
        int i = causeMessage.indexOf(SANDBOX_ERROR_PREFIX);
        if (i < 0) {
            continue;
        }
        return causeMessage.substring(i + SANDBOX_ERROR_PREFIX.length());
    }
    return null;
}
Also used : SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) GString(groovy.lang.GString)

Aggregations

SyntaxException (org.codehaus.groovy.syntax.SyntaxException)56 SyntaxErrorMessage (org.codehaus.groovy.control.messages.SyntaxErrorMessage)19 ClassNode (org.codehaus.groovy.ast.ClassNode)15 Expression (org.codehaus.groovy.ast.expr.Expression)12 MethodNode (org.codehaus.groovy.ast.MethodNode)11 SourceUnit (org.codehaus.groovy.control.SourceUnit)10 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)8 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)8 FieldNode (org.codehaus.groovy.ast.FieldNode)7 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)7 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)7 ArrayList (java.util.ArrayList)6 Parameter (org.codehaus.groovy.ast.Parameter)6 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)6 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)6 Token (org.codehaus.groovy.syntax.Token)6 LinkedList (java.util.LinkedList)5 PropertyNode (org.codehaus.groovy.ast.PropertyNode)5 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)5 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)5