Search in sources :

Example 6 with Message

use of org.codehaus.groovy.control.messages.Message in project groovy-core by groovy.

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 7 with Message

use of org.codehaus.groovy.control.messages.Message in project intellij-community by JetBrains.

the class ScriptSupport method checkValidScript.

public static String checkValidScript(String scriptText) {
    try {
        final File scriptFile = new File(scriptText);
        final GroovyShell shell = new GroovyShell();
        final Script script = scriptFile.exists() ? shell.parse(scriptFile) : shell.parse(scriptText);
        return null;
    } catch (IOException e) {
        return e.getMessage();
    } catch (MultipleCompilationErrorsException e) {
        final ErrorCollector errorCollector = e.getErrorCollector();
        final List<Message> errors = errorCollector.getErrors();
        for (Message error : errors) {
            if (error instanceof SyntaxErrorMessage) {
                final SyntaxErrorMessage errorMessage = (SyntaxErrorMessage) error;
                final SyntaxException cause = errorMessage.getCause();
                return cause.getMessage();
            }
        }
        return e.getMessage();
    } catch (CompilationFailedException ex) {
        return ex.getLocalizedMessage();
    }
}
Also used : Script(groovy.lang.Script) Message(org.codehaus.groovy.control.messages.Message) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) ErrorCollector(org.codehaus.groovy.control.ErrorCollector) ArrayList(java.util.ArrayList) List(java.util.List) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) IOException(java.io.IOException) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException) File(java.io.File) GroovyShell(groovy.lang.GroovyShell)

Example 8 with Message

use of org.codehaus.groovy.control.messages.Message in project groovy-core by groovy.

the class SourceUnit method failedWithUnexpectedEOF.

/**
     * Convenience routine, primarily for use by the InteractiveShell,
     * that returns true if parse() failed with an unexpected EOF.
     */
public boolean failedWithUnexpectedEOF() {
    // If you find another way, please add it.
    if (getErrorCollector().hasErrors()) {
        Message last = (Message) getErrorCollector().getLastError();
        Throwable cause = null;
        if (last instanceof SyntaxErrorMessage) {
            cause = ((SyntaxErrorMessage) last).getCause().getCause();
        }
        if (cause != null) {
            if (cause instanceof NoViableAltException) {
                return isEofToken(((NoViableAltException) cause).token);
            } else if (cause instanceof NoViableAltForCharException) {
                char badChar = ((NoViableAltForCharException) cause).foundChar;
                return badChar == CharScanner.EOF_CHAR;
            } else if (cause instanceof MismatchedCharException) {
                char badChar = (char) ((MismatchedCharException) cause).foundChar;
                return badChar == CharScanner.EOF_CHAR;
            } else if (cause instanceof MismatchedTokenException) {
                return isEofToken(((MismatchedTokenException) cause).token);
            }
        }
    }
    return false;
}
Also used : SimpleMessage(org.codehaus.groovy.control.messages.SimpleMessage) Message(org.codehaus.groovy.control.messages.Message) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) NoViableAltForCharException(antlr.NoViableAltForCharException) NoViableAltException(antlr.NoViableAltException) MismatchedTokenException(antlr.MismatchedTokenException) MismatchedCharException(antlr.MismatchedCharException)

Example 9 with Message

use of org.codehaus.groovy.control.messages.Message in project groovy-core by groovy.

the class ErrorCollector method write.

//---------------------------------------------------------------------------
// OUTPUT
private void write(PrintWriter writer, Janitor janitor, List messages, String txt) {
    if (messages == null || messages.size() == 0)
        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 10 with Message

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

the class SourceUnit method failedWithUnexpectedEOF.

/**
     * Convenience routine, primarily for use by the InteractiveShell,
     * that returns true if parse() failed with an unexpected EOF.
     */
public boolean failedWithUnexpectedEOF() {
    // If you find another way, please add it.
    if (getErrorCollector().hasErrors()) {
        Message last = (Message) getErrorCollector().getLastError();
        Throwable cause = null;
        if (last instanceof SyntaxErrorMessage) {
            cause = ((SyntaxErrorMessage) last).getCause().getCause();
        }
        if (cause != null) {
            if (cause instanceof NoViableAltException) {
                return isEofToken(((NoViableAltException) cause).token);
            } else if (cause instanceof NoViableAltForCharException) {
                char badChar = ((NoViableAltForCharException) cause).foundChar;
                return badChar == CharScanner.EOF_CHAR;
            } else if (cause instanceof MismatchedCharException) {
                char badChar = (char) ((MismatchedCharException) cause).foundChar;
                return badChar == CharScanner.EOF_CHAR;
            } else if (cause instanceof MismatchedTokenException) {
                return isEofToken(((MismatchedTokenException) cause).token);
            }
        }
    }
    return false;
}
Also used : SimpleMessage(org.codehaus.groovy.control.messages.SimpleMessage) Message(org.codehaus.groovy.control.messages.Message) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) NoViableAltForCharException(antlr.NoViableAltForCharException) NoViableAltException(antlr.NoViableAltException) MismatchedTokenException(antlr.MismatchedTokenException) MismatchedCharException(antlr.MismatchedCharException)

Aggregations

Message (org.codehaus.groovy.control.messages.Message)12 SyntaxErrorMessage (org.codehaus.groovy.control.messages.SyntaxErrorMessage)12 ExceptionMessage (org.codehaus.groovy.control.messages.ExceptionMessage)6 LocatedMessage (org.codehaus.groovy.control.messages.LocatedMessage)6 WarningMessage (org.codehaus.groovy.control.messages.WarningMessage)6 SyntaxException (org.codehaus.groovy.syntax.SyntaxException)5 MismatchedCharException (antlr.MismatchedCharException)2 MismatchedTokenException (antlr.MismatchedTokenException)2 NoViableAltException (antlr.NoViableAltException)2 NoViableAltForCharException (antlr.NoViableAltForCharException)2 GroovyShell (groovy.lang.GroovyShell)2 Script (groovy.lang.Script)2 Iterator (java.util.Iterator)2 SimpleMessage (org.codehaus.groovy.control.messages.SimpleMessage)2 Binding (groovy.lang.Binding)1 CompileStatic (groovy.transform.CompileStatic)1 File (java.io.File)1 IOException (java.io.IOException)1 URLClassLoader (java.net.URLClassLoader)1 ArrayList (java.util.ArrayList)1