use of org.apache.groovy.parser.antlr4.GroovyParser.CommandArgumentContext in project groovy by apache.
the class AstBuilder method visitCommandExpression.
@Override
public Expression visitCommandExpression(final CommandExpressionContext ctx) {
boolean hasArgumentList = asBoolean(ctx.enhancedArgumentListInPar());
boolean hasCommandArgument = asBoolean(ctx.commandArgument());
if ((hasArgumentList || hasCommandArgument) && visitingArrayInitializerCount > 0) {
// SEE http://groovy.329449.n5.nabble.com/parrot-Command-expressions-in-array-initializer-tt5752273.html
throw createParsingFailedException("Command chain expression can not be used in array initializer", ctx);
}
Expression baseExpr = (Expression) this.visit(ctx.expression());
if ((hasArgumentList || hasCommandArgument) && !isInsideParentheses(baseExpr) && baseExpr instanceof BinaryExpression && !"[".equals(((BinaryExpression) baseExpr).getOperation().getText())) {
throw createParsingFailedException("Unexpected input: '" + getOriginalText(ctx.expression()) + "'", ctx.expression());
}
MethodCallExpression methodCallExpression = null;
if (hasArgumentList) {
Expression arguments = this.visitEnhancedArgumentListInPar(ctx.enhancedArgumentListInPar());
if (baseExpr instanceof PropertyExpression) {
// e.g. obj.a 1, 2
methodCallExpression = configureAST(this.createMethodCallExpression((PropertyExpression) baseExpr, arguments), ctx.expression(), arguments);
} else if (baseExpr instanceof MethodCallExpression && !isInsideParentheses(baseExpr)) {
// e.g. m {} a, b OR m(...) a, b
if (asBoolean(arguments)) {
// The error should never be thrown.
throw new GroovyBugError("When baseExpr is a instance of MethodCallExpression, which should follow NO argumentList");
}
methodCallExpression = (MethodCallExpression) baseExpr;
} else if (!isInsideParentheses(baseExpr) && (// e.g. m 1, 2
baseExpr instanceof VariableExpression || // e.g. "$m" 1, 2
baseExpr instanceof GStringExpression || (baseExpr instanceof ConstantExpression && isTrue(baseExpr, IS_STRING)))) {
// e.g. "m" 1, 2
validateInvalidMethodDefinition(baseExpr, arguments);
methodCallExpression = configureAST(this.createMethodCallExpression(baseExpr, arguments), ctx.expression(), arguments);
} else {
// e.g. a[x] b, new A() b, etc.
methodCallExpression = configureAST(this.createCallMethodCallExpression(baseExpr, arguments), ctx.expression(), arguments);
}
methodCallExpression.putNodeMetaData(IS_COMMAND_EXPRESSION, Boolean.TRUE);
if (!hasCommandArgument) {
return methodCallExpression;
}
}
if (hasCommandArgument) {
baseExpr.putNodeMetaData(IS_COMMAND_EXPRESSION, Boolean.TRUE);
}
return configureAST((Expression) ctx.commandArgument().stream().map(e -> (Object) e).reduce(methodCallExpression != null ? methodCallExpression : baseExpr, (r, e) -> {
CommandArgumentContext commandArgumentContext = (CommandArgumentContext) e;
commandArgumentContext.putNodeMetaData(CMD_EXPRESSION_BASE_EXPR, r);
return this.visitCommandArgument(commandArgumentContext);
}), ctx);
}
Aggregations