Search in sources :

Example 1 with SyntaxException

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

the class AntlrParserPlugin method transformCSTIntoAST.

protected void transformCSTIntoAST(SourceUnit sourceUnit, Reader reader, SourceBuffer sourceBuffer) throws CompilationFailedException {
    ast = null;
    setController(sourceUnit);
    // TODO find a way to inject any GroovyLexer/GroovyRecognizer
    UnicodeEscapingReader unicodeReader = new UnicodeEscapingReader(reader, sourceBuffer);
    UnicodeLexerSharedInputState inputState = new UnicodeLexerSharedInputState(unicodeReader);
    GroovyLexer lexer = new GroovyLexer(inputState);
    unicodeReader.setLexer(lexer);
    GroovyRecognizer parser = GroovyRecognizer.make(lexer);
    parser.setSourceBuffer(sourceBuffer);
    tokenNames = parser.getTokenNames();
    parser.setFilename(sourceUnit.getName());
    // start parsing at the compilationUnit rule
    try {
        parser.compilationUnit();
    } catch (TokenStreamRecognitionException tsre) {
        RecognitionException e = tsre.recog;
        SyntaxException se = new SyntaxException(e.getMessage(), e, e.getLine(), e.getColumn());
        se.setFatal(true);
        sourceUnit.addError(se);
    } catch (RecognitionException e) {
        SyntaxException se = new SyntaxException(e.getMessage(), e, e.getLine(), e.getColumn());
        se.setFatal(true);
        sourceUnit.addError(se);
    } catch (TokenStreamException e) {
        sourceUnit.addException(e);
    }
    ast = parser.getAST();
}
Also used : TokenStreamException(antlr.TokenStreamException) TokenStreamRecognitionException(antlr.TokenStreamRecognitionException) GroovyLexer(org.codehaus.groovy.antlr.parser.GroovyLexer) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) GroovyRecognizer(org.codehaus.groovy.antlr.parser.GroovyRecognizer) RecognitionException(antlr.RecognitionException) TokenStreamRecognitionException(antlr.TokenStreamRecognitionException)

Example 2 with SyntaxException

use of org.codehaus.groovy.syntax.SyntaxException in project OpenAM by OpenRock.

the class StandardScriptValidator method getScriptErrors.

/**
     * Convert from ScriptException to ScriptError.
     *
     * @param script the script that was validated.
     * @param se the error thrown by the validation task.
     * @return the converted script error.
     */
private List<ScriptError> getScriptErrors(ScriptObject script, ScriptException se) {
    final List<ScriptError> scriptErrorList = new ArrayList<ScriptError>();
    final Throwable cause = se.getCause();
    // with useful information so we have to retrieve it from the cause exception.
    if (cause instanceof EvaluatorException) {
        final EvaluatorException ee = (EvaluatorException) cause;
        final ScriptError error = new ScriptError();
        error.setScriptName(script.getName());
        error.setMessage(ee.details());
        error.setLineNumber(ee.lineNumber());
        error.setColumnNumber(ee.columnNumber());
        scriptErrorList.add(error);
    } else if (cause instanceof MultipleCompilationErrorsException) {
        ErrorCollector errorCollector = ((MultipleCompilationErrorsException) cause).getErrorCollector();
        for (int i = 0; i < errorCollector.getErrorCount(); i++) {
            final SyntaxException syntaxException = errorCollector.getSyntaxError(i);
            final ScriptError error = new ScriptError();
            error.setScriptName(script.getName());
            error.setMessage(syntaxException.getOriginalMessage());
            error.setLineNumber(syntaxException.getLine());
            error.setColumnNumber(syntaxException.getStartColumn());
            scriptErrorList.add(error);
        }
    } else {
        final ScriptError error = new ScriptError();
        error.setScriptName(script.getName());
        error.setMessage(se.getMessage());
        error.setLineNumber(se.getLineNumber());
        error.setColumnNumber(se.getColumnNumber());
        scriptErrorList.add(error);
    }
    return scriptErrorList;
}
Also used : EvaluatorException(org.mozilla.javascript.EvaluatorException) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) ArrayList(java.util.ArrayList) ErrorCollector(org.codehaus.groovy.control.ErrorCollector) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException)

Example 3 with SyntaxException

use of org.codehaus.groovy.syntax.SyntaxException 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 4 with SyntaxException

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

the class BinaryExpressionHelper method eval.

public void eval(BinaryExpression expression) {
    switch(expression.getOperation().getType()) {
        case // = assignment
        EQUAL:
            evaluateEqual(expression, false);
            break;
        case // ==
        COMPARE_EQUAL:
            evaluateCompareExpression(compareEqualMethod, expression);
            break;
        case COMPARE_NOT_EQUAL:
            evaluateCompareExpression(compareNotEqualMethod, expression);
            break;
        case COMPARE_TO:
            evaluateCompareTo(expression);
            break;
        case COMPARE_GREATER_THAN:
            evaluateCompareExpression(compareGreaterThanMethod, expression);
            break;
        case COMPARE_GREATER_THAN_EQUAL:
            evaluateCompareExpression(compareGreaterThanEqualMethod, expression);
            break;
        case COMPARE_LESS_THAN:
            evaluateCompareExpression(compareLessThanMethod, expression);
            break;
        case COMPARE_LESS_THAN_EQUAL:
            evaluateCompareExpression(compareLessThanEqualMethod, expression);
            break;
        case LOGICAL_AND:
            evaluateLogicalAndExpression(expression);
            break;
        case LOGICAL_OR:
            evaluateLogicalOrExpression(expression);
            break;
        case BITWISE_AND:
            evaluateBinaryExpression("and", expression);
            break;
        case BITWISE_AND_EQUAL:
            evaluateBinaryExpressionWithAssignment("and", expression);
            break;
        case BITWISE_OR:
            evaluateBinaryExpression("or", expression);
            break;
        case BITWISE_OR_EQUAL:
            evaluateBinaryExpressionWithAssignment("or", expression);
            break;
        case BITWISE_XOR:
            evaluateBinaryExpression("xor", expression);
            break;
        case BITWISE_XOR_EQUAL:
            evaluateBinaryExpressionWithAssignment("xor", expression);
            break;
        case PLUS:
            evaluateBinaryExpression("plus", expression);
            break;
        case PLUS_EQUAL:
            evaluateBinaryExpressionWithAssignment("plus", expression);
            break;
        case MINUS:
            evaluateBinaryExpression("minus", expression);
            break;
        case MINUS_EQUAL:
            evaluateBinaryExpressionWithAssignment("minus", expression);
            break;
        case MULTIPLY:
            evaluateBinaryExpression("multiply", expression);
            break;
        case MULTIPLY_EQUAL:
            evaluateBinaryExpressionWithAssignment("multiply", expression);
            break;
        case DIVIDE:
            evaluateBinaryExpression("div", expression);
            break;
        case DIVIDE_EQUAL:
            //SPG don't use divide since BigInteger implements directly
            //and we want to dispatch through DefaultGroovyMethods to get a BigDecimal result
            evaluateBinaryExpressionWithAssignment("div", expression);
            break;
        case INTDIV:
            evaluateBinaryExpression("intdiv", expression);
            break;
        case INTDIV_EQUAL:
            evaluateBinaryExpressionWithAssignment("intdiv", expression);
            break;
        case MOD:
            evaluateBinaryExpression("mod", expression);
            break;
        case MOD_EQUAL:
            evaluateBinaryExpressionWithAssignment("mod", expression);
            break;
        case POWER:
            evaluateBinaryExpression("power", expression);
            break;
        case POWER_EQUAL:
            evaluateBinaryExpressionWithAssignment("power", expression);
            break;
        case LEFT_SHIFT:
            evaluateBinaryExpression("leftShift", expression);
            break;
        case LEFT_SHIFT_EQUAL:
            evaluateBinaryExpressionWithAssignment("leftShift", expression);
            break;
        case RIGHT_SHIFT:
            evaluateBinaryExpression("rightShift", expression);
            break;
        case RIGHT_SHIFT_EQUAL:
            evaluateBinaryExpressionWithAssignment("rightShift", expression);
            break;
        case RIGHT_SHIFT_UNSIGNED:
            evaluateBinaryExpression("rightShiftUnsigned", expression);
            break;
        case RIGHT_SHIFT_UNSIGNED_EQUAL:
            evaluateBinaryExpressionWithAssignment("rightShiftUnsigned", expression);
            break;
        case KEYWORD_INSTANCEOF:
            evaluateInstanceof(expression);
            break;
        case FIND_REGEX:
            evaluateCompareExpression(findRegexMethod, expression);
            break;
        case MATCH_REGEX:
            evaluateCompareExpression(matchRegexMethod, expression);
            break;
        case LEFT_SQUARE_BRACKET:
            if (controller.getCompileStack().isLHS()) {
                evaluateEqual(expression, false);
            } else {
                evaluateBinaryExpression("getAt", expression);
            }
            break;
        case KEYWORD_IN:
            evaluateCompareExpression(isCaseMethod, expression);
            break;
        case COMPARE_IDENTICAL:
        case COMPARE_NOT_IDENTICAL:
            Token op = expression.getOperation();
            Throwable cause = new SyntaxException("Operator " + op + " not supported", op.getStartLine(), op.getStartColumn(), op.getStartLine(), op.getStartColumn() + 3);
            throw new GroovyRuntimeException(cause);
        default:
            throw new GroovyBugError("Operation: " + expression.getOperation() + " not supported");
    }
}
Also used : SyntaxException(org.codehaus.groovy.syntax.SyntaxException) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) GroovyBugError(org.codehaus.groovy.GroovyBugError) Token(org.codehaus.groovy.syntax.Token)

Example 5 with SyntaxException

use of org.codehaus.groovy.syntax.SyntaxException in project workflow-cps-plugin by jenkinsci.

the class CpsFlowDefinitionValidator method toCheckStatus.

public static List<CheckStatus> toCheckStatus(CompilationFailedException x) {
    List<CheckStatus> errors = new ArrayList<CheckStatus>();
    if (x instanceof MultipleCompilationErrorsException) {
        for (Object o : ((MultipleCompilationErrorsException) x).getErrorCollector().getErrors()) {
            if (o instanceof SyntaxErrorMessage) {
                SyntaxException cause = ((SyntaxErrorMessage) o).getCause();
                CheckStatus st = new CheckStatus(cause.getOriginalMessage(), FAIL_KEY);
                st.setLine(cause.getLine());
                st.setColumn(cause.getStartColumn());
                errors.add(st);
            }
        }
    }
    if (errors.isEmpty()) {
        // Fallback to simple message
        CheckStatus st = new CheckStatus(x.getMessage(), FAIL_KEY);
        st.setLine(1);
        st.setColumn(0);
        errors.add(st);
    }
    return errors;
}
Also used : SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) ArrayList(java.util.ArrayList) JSONObject(net.sf.json.JSONObject) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException)

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