Search in sources :

Example 1 with SyntaxErrorMessage

use of org.codehaus.groovy.control.messages.SyntaxErrorMessage in project groovy by apache.

the class CompileUnit method addClass.

/**
     * Adds a class to the unit.
     */
public void addClass(ClassNode node) {
    node = node.redirect();
    String name = node.getName();
    ClassNode stored = classes.get(name);
    if (stored != null && stored != node) {
        // we have a duplicate class!
        // One possibility for this is, that we declared a script and a
        // class in the same file and named the class like the file
        SourceUnit nodeSource = node.getModule().getContext();
        SourceUnit storedSource = stored.getModule().getContext();
        String txt = "Invalid duplicate class definition of class " + node.getName() + " : ";
        if (nodeSource == storedSource) {
            // same class in same source
            txt += "The source " + nodeSource.getName() + " contains at least two definitions of the class " + node.getName() + ".\n";
            if (node.isScriptBody() || stored.isScriptBody()) {
                txt += "One of the classes is an explicit generated class using the class statement, the other is a class generated from" + " the script body based on the file name. Solutions are to change the file name or to change the class name.\n";
            }
        } else {
            txt += "The sources " + nodeSource.getName() + " and " + storedSource.getName() + " each contain a class with the name " + node.getName() + ".\n";
        }
        nodeSource.getErrorCollector().addErrorAndContinue(new SyntaxErrorMessage(new SyntaxException(txt, node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()), nodeSource));
    }
    classes.put(name, node);
    if (classesToCompile.containsKey(name)) {
        ClassNode cn = classesToCompile.get(name);
        cn.setRedirect(node);
        classesToCompile.remove(name);
    }
}
Also used : SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) SourceUnit(org.codehaus.groovy.control.SourceUnit)

Example 2 with SyntaxErrorMessage

use of org.codehaus.groovy.control.messages.SyntaxErrorMessage in project groovy by apache.

the class ErrorCollector method getSyntaxError.

/**
     * Convenience routine to return the specified error's
     * underlying SyntaxException, or null if it isn't one.
     */
public SyntaxException getSyntaxError(int index) {
    SyntaxException exception = null;
    Message message = getError(index);
    if (message != null && message instanceof SyntaxErrorMessage) {
        exception = ((SyntaxErrorMessage) message).getCause();
    }
    return exception;
}
Also used : LocatedMessage(org.codehaus.groovy.control.messages.LocatedMessage) WarningMessage(org.codehaus.groovy.control.messages.WarningMessage) Message(org.codehaus.groovy.control.messages.Message) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) ExceptionMessage(org.codehaus.groovy.control.messages.ExceptionMessage) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Example 3 with SyntaxErrorMessage

use of org.codehaus.groovy.control.messages.SyntaxErrorMessage in project groovy by apache.

the class ErrorCollector method getException.

/**
     * Convenience routine to return the specified error's
     * underlying Exception, or null if it isn't one.
     */
public Exception getException(int index) {
    Exception exception = null;
    Message message = getError(index);
    if (message != null) {
        if (message instanceof ExceptionMessage) {
            exception = ((ExceptionMessage) message).getCause();
        } else if (message instanceof SyntaxErrorMessage) {
            exception = ((SyntaxErrorMessage) message).getCause();
        }
    }
    return exception;
}
Also used : ExceptionMessage(org.codehaus.groovy.control.messages.ExceptionMessage) LocatedMessage(org.codehaus.groovy.control.messages.LocatedMessage) WarningMessage(org.codehaus.groovy.control.messages.WarningMessage) Message(org.codehaus.groovy.control.messages.Message) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) ExceptionMessage(org.codehaus.groovy.control.messages.ExceptionMessage) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Example 4 with SyntaxErrorMessage

use of org.codehaus.groovy.control.messages.SyntaxErrorMessage in project groovy by apache.

the class ErrorCollector method write.

//---------------------------------------------------------------------------
// OUTPUT
private void write(PrintWriter writer, Janitor janitor, List messages, String txt) {
    if (messages == null || messages.isEmpty())
        return;
    Iterator iterator = messages.iterator();
    while (iterator.hasNext()) {
        Message message = (Message) iterator.next();
        message.write(writer, janitor);
        if (configuration.getDebug() && (message instanceof SyntaxErrorMessage)) {
            SyntaxErrorMessage sem = (SyntaxErrorMessage) message;
            sem.getCause().printStackTrace(writer);
        }
        writer.println();
    }
    writer.print(messages.size());
    writer.print(" " + txt);
    if (messages.size() > 1)
        writer.print("s");
    writer.println();
}
Also used : LocatedMessage(org.codehaus.groovy.control.messages.LocatedMessage) WarningMessage(org.codehaus.groovy.control.messages.WarningMessage) Message(org.codehaus.groovy.control.messages.Message) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) ExceptionMessage(org.codehaus.groovy.control.messages.ExceptionMessage) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) Iterator(java.util.Iterator)

Example 5 with SyntaxErrorMessage

use of org.codehaus.groovy.control.messages.SyntaxErrorMessage in project groovy by apache.

the class BindableASTTransformation method visit.

/**
     * Handles the bulk of the processing, mostly delegating to other methods.
     *
     * @param nodes   the ast nodes
     * @param source  the source unit for the nodes
     */
public void visit(ASTNode[] nodes, SourceUnit source) {
    if (!(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) {
        throw new RuntimeException("Internal error: wrong types: $node.class / $parent.class");
    }
    AnnotationNode node = (AnnotationNode) nodes[0];
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    if (VetoableASTTransformation.hasVetoableAnnotation(parent)) {
        // VetoableASTTransformation will handle both @Bindable and @Vetoable
        return;
    }
    ClassNode declaringClass = parent.getDeclaringClass();
    if (parent instanceof FieldNode) {
        if ((((FieldNode) parent).getModifiers() & Opcodes.ACC_FINAL) != 0) {
            source.getErrorCollector().addErrorAndContinue(new SyntaxErrorMessage(new SyntaxException("@groovy.beans.Bindable cannot annotate a final property.", node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()), source));
        }
        if (VetoableASTTransformation.hasVetoableAnnotation(parent.getDeclaringClass())) {
            // VetoableASTTransformation will handle both @Bindable and @Vetoable
            return;
        }
        addListenerToProperty(source, node, declaringClass, (FieldNode) parent);
    } else if (parent instanceof ClassNode) {
        addListenerToClass(source, (ClassNode) parent);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Aggregations

SyntaxErrorMessage (org.codehaus.groovy.control.messages.SyntaxErrorMessage)28 SyntaxException (org.codehaus.groovy.syntax.SyntaxException)23 Message (org.codehaus.groovy.control.messages.Message)10 ClassNode (org.codehaus.groovy.ast.ClassNode)7 FieldNode (org.codehaus.groovy.ast.FieldNode)6 PropertyNode (org.codehaus.groovy.ast.PropertyNode)6 SourceUnit (org.codehaus.groovy.control.SourceUnit)6 ExceptionMessage (org.codehaus.groovy.control.messages.ExceptionMessage)6 LocatedMessage (org.codehaus.groovy.control.messages.LocatedMessage)6 WarningMessage (org.codehaus.groovy.control.messages.WarningMessage)6 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)5 MismatchedCharException (antlr.MismatchedCharException)2 MismatchedTokenException (antlr.MismatchedTokenException)2 NoViableAltException (antlr.NoViableAltException)2 NoViableAltForCharException (antlr.NoViableAltForCharException)2 GroovyShell (groovy.lang.GroovyShell)2 Reference (groovy.lang.Reference)2 Script (groovy.lang.Script)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2