Search in sources :

Example 6 with SyntaxException

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

the class GrailsASTUtils method error.

/**
 * Generates a fatal compilation error.
 *
 * @param sourceUnit the SourceUnit
 * @param astNode the ASTNode which caused the error
 * @param message The error message
 * @param fatal indicates if this is a fatal error
 */
public static void error(final SourceUnit sourceUnit, final ASTNode astNode, final String message, final boolean fatal) {
    final SyntaxException syntaxException = new SyntaxException(message, astNode.getLineNumber(), astNode.getColumnNumber());
    final SyntaxErrorMessage syntaxErrorMessage = new SyntaxErrorMessage(syntaxException, sourceUnit);
    sourceUnit.getErrorCollector().addError(syntaxErrorMessage, fatal);
}
Also used : SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Example 7 with SyntaxException

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

the class GroovyScriptEngineImpl method eval.

public Object eval(String script, ScriptContext ctx) throws ScriptException {
    try {
        String val = (String) ctx.getAttribute("#jsr223.groovy.engine.keep.globals", ScriptContext.ENGINE_SCOPE);
        ReferenceBundle bundle = ReferenceBundle.getHardBundle();
        if (val != null && val.length() > 0) {
            if (val.equalsIgnoreCase("soft")) {
                bundle = ReferenceBundle.getSoftBundle();
            } else if (val.equalsIgnoreCase("weak")) {
                bundle = ReferenceBundle.getWeakBundle();
            } else if (val.equalsIgnoreCase("phantom")) {
                bundle = ReferenceBundle.getPhantomBundle();
            }
        }
        globalClosures.setBundle(bundle);
    } catch (ClassCastException cce) {
    /*ignore.*/
    }
    try {
        Class clazz = getScriptClass(script);
        if (clazz == null)
            throw new ScriptException("Script class is null");
        return eval(clazz, ctx);
    } catch (SyntaxException e) {
        throw new ScriptException(e.getMessage(), e.getSourceLocator(), e.getLine());
    } catch (Exception e) {
        if (debug)
            e.printStackTrace();
        throw new ScriptException(e);
    }
}
Also used : ScriptException(javax.script.ScriptException) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) ReferenceBundle(org.codehaus.groovy.util.ReferenceBundle) MetaClass(groovy.lang.MetaClass) Class(java.lang.Class) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) String(java.lang.String) MissingPropertyException(groovy.lang.MissingPropertyException) ScriptException(javax.script.ScriptException) MissingMethodException(groovy.lang.MissingMethodException) IOException(java.io.IOException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Example 8 with SyntaxException

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

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 9 with SyntaxException

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

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 10 with SyntaxException

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

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

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