Search in sources :

Example 26 with SyntaxException

use of org.codehaus.groovy.syntax.SyntaxException in project grails-core by grails.

the class BindingFormatASTTransformation method error.

protected void error(final SourceUnit sourceUnit, final ASTNode astNode, final String message) {
    final SyntaxException syntaxException = new SyntaxException(message, astNode.getLineNumber(), astNode.getColumnNumber());
    final SyntaxErrorMessage syntaxErrorMessage = new SyntaxErrorMessage(syntaxException, sourceUnit);
    sourceUnit.getErrorCollector().addError(syntaxErrorMessage, false);
}
Also used : SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Example 27 with SyntaxException

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

the class DefaultScriptCompilationHandler method wrapCompilationFailure.

private void wrapCompilationFailure(ScriptSource source, MultipleCompilationErrorsException e) {
    // Fix the source file name displayed in the error messages
    for (Object message : e.getErrorCollector().getErrors()) {
        if (message instanceof SyntaxErrorMessage) {
            try {
                SyntaxErrorMessage syntaxErrorMessage = (SyntaxErrorMessage) message;
                Field sourceField = SyntaxErrorMessage.class.getDeclaredField("source");
                sourceField.setAccessible(true);
                SourceUnit sourceUnit = (SourceUnit) sourceField.get(syntaxErrorMessage);
                Field nameField = SourceUnit.class.getDeclaredField("name");
                nameField.setAccessible(true);
                nameField.set(sourceUnit, source.getDisplayName());
            } catch (Exception failure) {
                throw UncheckedException.throwAsUncheckedException(failure);
            }
        }
    }
    SyntaxException syntaxError = e.getErrorCollector().getSyntaxError(0);
    Integer lineNumber = syntaxError == null ? null : syntaxError.getLine();
    throw new ScriptCompilationException(String.format("Could not compile %s.", source.getDisplayName()), e, source, lineNumber);
}
Also used : Field(java.lang.reflect.Field) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) ScriptCompilationException(org.gradle.groovy.scripts.ScriptCompilationException) SourceUnit(org.codehaus.groovy.control.SourceUnit) UncheckedException(org.gradle.internal.UncheckedException) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) GradleException(org.gradle.api.GradleException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) ScriptCompilationException(org.gradle.groovy.scripts.ScriptCompilationException) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Example 28 with SyntaxException

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

the class RuleVisitor method error.

private void error(ASTNode call, String message) {
    SyntaxException syntaxException = new SyntaxException(message, call.getLineNumber(), call.getColumnNumber());
    sourceUnit.getErrorCollector().addError(syntaxException, sourceUnit);
}
Also used : SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Example 29 with SyntaxException

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

the class ModelBlockTransformer method call.

/*
        TODO change this so that we extract all the information at compile time.

        At the moment we use the transform to:

        1. validate/restrict the syntax
        2. transform rules into something more robust (e.g. foo.bar.baz {} into configure("foo.bar.baz", {})) - no dynamic propertyMissing() nonsense
        3. hoist out input references (i.e. $()) into an annotation on rule closure classes to make available

        This means we actually have to execute the code block in order to find the rule information within.
        This is also problematic because it means we have to serialize this information into some form that fits into annotations.

        Later, we will extract all the “up-front” information we need to know during compile time.
        This will mean that we only need to execute the rules themselves, and not any code to actually register the rules.
     */
@Override
public void call(SourceUnit source) throws CompilationFailedException {
    List<Statement> statements = source.getAST().getStatementBlock().getStatements();
    for (Statement statement : statements) {
        ScriptBlock scriptBlock = AstUtils.detectScriptBlock(statement, SCRIPT_BLOCK_NAMES);
        if (scriptBlock == null) {
            // Look for model(«») (i.e. call to model with anything other than non literal closure)
            MethodCallExpression methodCall = AstUtils.extractBareMethodCall(statement);
            if (methodCall == null) {
                continue;
            }
            String methodName = AstUtils.extractConstantMethodName(methodCall);
            if (methodName == null) {
                continue;
            }
            if (methodName.equals(MODEL)) {
                source.getErrorCollector().addError(new SyntaxException(NON_LITERAL_CLOSURE_TO_TOP_LEVEL_MODEL_MESSAGE, statement.getLineNumber(), statement.getColumnNumber()), source);
            }
        } else {
            RuleVisitor ruleVisitor = new RuleVisitor(source, scriptSourceDescription, location);
            RulesVisitor rulesVisitor = new RulesVisitor(source, ruleVisitor);
            scriptBlock.getClosureExpression().getCode().visit(rulesVisitor);
        }
    }
}
Also used : ScriptBlock(org.gradle.groovy.scripts.internal.ScriptBlock) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Statement(org.codehaus.groovy.ast.stmt.Statement) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Example 30 with SyntaxException

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

the class InitialPassStatementTransformer method transform.

public Statement transform(SourceUnit sourceUnit, Statement statement) {
    ScriptBlock scriptBlock = AstUtils.detectScriptBlock(statement, scriptBlockNames);
    if (scriptBlock == null) {
        seenNonClasspathStatement = true;
        return null;
    } else {
        if (scriptBlock.getName().equals(PLUGINS)) {
            String failMessage = null;
            if (!scriptTarget.getSupportsPluginsBlock()) {
                failMessage = pluginBlockMetadataExtractor.formatErrorMessage("Only Project build scripts can contain plugins {} blocks");
            } else {
                seenPluginsBlock = true;
                if (seenNonClasspathStatement) {
                    failMessage = String.format(pluginBlockMetadataExtractor.formatErrorMessage("only %s {} and other %s {} script blocks are allowed before %s {} blocks, no other statements are allowed"), scriptTarget.getClasspathBlockName(), PLUGINS, PLUGINS);
                } else {
                    pluginBlockMetadataExtractor.extract(sourceUnit, scriptBlock);
                }
            }
            if (failMessage != null) {
                sourceUnit.getErrorCollector().addError(new SyntaxException(failMessage, statement.getLineNumber(), statement.getColumnNumber()), sourceUnit);
            }
            return null;
        } else if (scriptBlock.getName().equals(PLUGIN_REPOS)) {
            String failureMessage = null;
            if (!scriptTarget.getSupportsPluginRepositoriesBlock()) {
                failureMessage = "Only Settings scripts can contain a pluginRepositories {} block.";
            } else if (seenClasspathBlock || seenNonClasspathStatement || seenPluginsBlock) {
                failureMessage = String.format("The %s {} block must appear before any other statements in the script.", PLUGIN_REPOS);
            } else if (seenPluginRepositoriesBlock) {
                failureMessage = String.format("At most, one %s {} block may appear in the script.", PLUGIN_REPOS);
            }
            if (failureMessage != null) {
                sourceUnit.getErrorCollector().addError(new SyntaxException(makePluginRepositoriesError(failureMessage), statement.getLineNumber(), statement.getColumnNumber()), sourceUnit);
            }
            seenPluginRepositoriesBlock = true;
            // don't transform the pluginRepositories block
            return statement;
        } else if (scriptBlock.getName().equals(PLUGIN_MANAGEMENT)) {
            String failureMessage = null;
            if (!scriptTarget.getSupportsPluginRepositoriesBlock()) {
                failureMessage = "Only Settings scripts can contain a pluginManagement {} block.";
            } else if (seenClasspathBlock || seenNonClasspathStatement || seenPluginsBlock || seenPluginRepositoriesBlock) {
                failureMessage = String.format("The %s {} block must appear before any other statements in the script.", PLUGIN_MANAGEMENT);
            } else if (seenPluginManagementBlock) {
                failureMessage = String.format("At most, one %s {} block may appear in the script.", PLUGIN_MANAGEMENT);
            }
            if (failureMessage != null) {
                sourceUnit.getErrorCollector().addError(new SyntaxException(makePluginManagementError(failureMessage), statement.getLineNumber(), statement.getColumnNumber()), sourceUnit);
            }
            seenPluginManagementBlock = true;
            // don't transform the pluginRepositories block
            return statement;
        } else {
            if (seenPluginsBlock) {
                String message = String.format(pluginBlockMetadataExtractor.formatErrorMessage("all %s {} blocks must appear before any %s {} blocks in the script"), scriptTarget.getClasspathBlockName(), PLUGINS);
                sourceUnit.getErrorCollector().addError(new SyntaxException(message, statement.getLineNumber(), statement.getColumnNumber()), sourceUnit);
            }
            seenClasspathBlock = true;
            return statement;
        }
    }
}
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