Search in sources :

Example 41 with SyntaxException

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

the class TraitComposer method createException.

private static SyntaxException createException(ClassNode trait, ClassNode targetNode, MethodNode forwarder, MethodNode existingMethod) {
    String middle;
    ASTNode errorTarget;
    if (existingMethod.getLineNumber() == -1) {
        // came from a trait
        errorTarget = targetNode;
        List<AnnotationNode> allAnnos = existingMethod.getAnnotations(Traits.TRAITBRIDGE_CLASSNODE);
        AnnotationNode bridgeAnno = allAnnos == null ? null : allAnnos.get(0);
        String fromTrait = null;
        if (bridgeAnno != null) {
            Expression traitClass = bridgeAnno.getMember("traitClass");
            if (traitClass instanceof ClassExpression) {
                ClassExpression ce = (ClassExpression) traitClass;
                fromTrait = ce.getType().getNameWithoutPackage();
            }
        }
        middle = "in '" + targetNode.getNameWithoutPackage();
        if (fromTrait != null) {
            middle += "' from trait '" + fromTrait;
        }
    } else {
        errorTarget = existingMethod;
        middle = "declared in '" + targetNode.getNameWithoutPackage();
    }
    String message = "The static '" + forwarder.getName() + "' method " + middle + "' conflicts with the instance method having the same signature from trait '" + trait.getNameWithoutPackage() + "'";
    return new SyntaxException(message, errorTarget);
}
Also used : AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) ASTNode(org.codehaus.groovy.ast.ASTNode) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression)

Example 42 with SyntaxException

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

the class NAryOperationRewriter method transformPrefixExpression.

private Expression transformPrefixExpression(final PrefixExpression exp) {
    if (isInternalFieldAccess(exp.getExpression())) {
        Token operation = exp.getOperation();
        sourceUnit.addError(new SyntaxException("Prefix 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 43 with SyntaxException

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

the class ProcessingContextInformation method addError.

public void addError(String msg, ASTNode expr) {
    int line = expr.getLineNumber();
    int col = expr.getColumnNumber();
    SourceUnit source = sourceUnit();
    source.getErrorCollector().addErrorAndContinue(new SyntaxErrorMessage(new SyntaxException(msg + '\n', line, col), source));
}
Also used : SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) SourceUnit(org.codehaus.groovy.control.SourceUnit)

Example 44 with SyntaxException

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

the class SyntaxErrorMessageTest method testSetsTheSourceLocatorOfItsSyntaxExceptionAsTheNameOfTheCorrespondingSourceUnitWhenInstantiated.

public void testSetsTheSourceLocatorOfItsSyntaxExceptionAsTheNameOfTheCorrespondingSourceUnitWhenInstantiated() {
    SyntaxException syntaxException = new SyntaxException(someString(), -1, -1);
    assertEquals("source locator", null, syntaxException.getSourceLocator());
    String sourceUnitName = someString();
    SourceUnit sourceUnit = SourceUnit.create(sourceUnitName, someString());
    new SyntaxErrorMessage(syntaxException, sourceUnit);
    assertEquals("source locator", sourceUnitName, syntaxException.getSourceLocator());
}
Also used : SyntaxException(org.codehaus.groovy.syntax.SyntaxException) SourceUnit(org.codehaus.groovy.control.SourceUnit)

Example 45 with SyntaxException

use of org.codehaus.groovy.syntax.SyntaxException in project groovy-cps by cloudbees.

the class CpsTransformer method visitBinaryExpression.

/**
 * @see
 * org.codehaus.groovy.classgen.asm.BinaryExpressionHelper#eval(BinaryExpression)
 */
@Override
public void visitBinaryExpression(final BinaryExpression exp) {
    String name = BINARY_OP_TO_BUILDER_METHOD.get(exp.getOperation().getType());
    if (name != null) {
        if (name.equals("assign") && exp.getLeftExpression() instanceof TupleExpression) {
            multipleAssignment(exp, (TupleExpression) exp.getLeftExpression(), exp.getRightExpression());
        } else {
            makeNode(name, new Runnable() {

                @Override
                public void run() {
                    loc(exp);
                    visit(exp.getLeftExpression());
                    visit(exp.getRightExpression());
                }
            });
        }
        return;
    }
    sourceUnit.addError(new SyntaxException("Unsupported operation in this context", exp.getLineNumber(), exp.getColumnNumber()));
}
Also used : SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

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