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);
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations