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