Search in sources :

Example 1 with ExpressionContext

use of org.apache.groovy.parser.antlr4.GroovyParser.ExpressionContext in project groovy by apache.

the class AstBuilder method visitUnaryAddExprAlt.

@Override
public Expression visitUnaryAddExprAlt(final UnaryAddExprAltContext ctx) {
    ExpressionContext expressionCtx = ctx.expression();
    Expression expression = (Expression) this.visit(expressionCtx);
    switch(ctx.op.getType()) {
        case ADD:
            {
                if (isNonStringConstantOutsideParentheses(expression)) {
                    return configureAST(expression, ctx);
                }
                return configureAST(new UnaryPlusExpression(expression), ctx);
            }
        case SUB:
            {
                if (isNonStringConstantOutsideParentheses(expression)) {
                    ConstantExpression constantExpression = (ConstantExpression) expression;
                    try {
                        String integerLiteralText = constantExpression.getNodeMetaData(INTEGER_LITERAL_TEXT);
                        if (null != integerLiteralText) {
                            ConstantExpression result = new ConstantExpression(Numbers.parseInteger(SUB_STR + integerLiteralText));
                            // reset the numberFormatError
                            this.numberFormatError = null;
                            return configureAST(result, ctx);
                        }
                        String floatingPointLiteralText = constantExpression.getNodeMetaData(FLOATING_POINT_LITERAL_TEXT);
                        if (null != floatingPointLiteralText) {
                            ConstantExpression result = new ConstantExpression(Numbers.parseDecimal(SUB_STR + floatingPointLiteralText));
                            // reset the numberFormatError
                            this.numberFormatError = null;
                            return configureAST(result, ctx);
                        }
                    } catch (Exception e) {
                        throw createParsingFailedException(e.getMessage(), ctx);
                    }
                    throw new GroovyBugError("Failed to find the original number literal text: " + constantExpression.getText());
                }
                return configureAST(new UnaryMinusExpression(expression), ctx);
            }
        case INC:
        case DEC:
            return configureAST(new PrefixExpression(this.createGroovyToken(ctx.op), expression), ctx);
        default:
            throw createParsingFailedException("Unsupported unary operation: " + ctx.getText(), ctx);
    }
}
Also used : UnaryMinusExpression(org.codehaus.groovy.ast.expr.UnaryMinusExpression) CastParExpressionContext(org.apache.groovy.parser.antlr4.GroovyParser.CastParExpressionContext) PathExpressionContext(org.apache.groovy.parser.antlr4.GroovyParser.PathExpressionContext) EnhancedStatementExpressionContext(org.apache.groovy.parser.antlr4.GroovyParser.EnhancedStatementExpressionContext) ClosureOrLambdaExpressionContext(org.apache.groovy.parser.antlr4.GroovyParser.ClosureOrLambdaExpressionContext) CommandExpressionContext(org.apache.groovy.parser.antlr4.GroovyParser.CommandExpressionContext) StandardLambdaExpressionContext(org.apache.groovy.parser.antlr4.GroovyParser.StandardLambdaExpressionContext) ExpressionContext(org.apache.groovy.parser.antlr4.GroovyParser.ExpressionContext) ParExpressionContext(org.apache.groovy.parser.antlr4.GroovyParser.ParExpressionContext) SwitchExpressionContext(org.apache.groovy.parser.antlr4.GroovyLangParser.SwitchExpressionContext) PostfixExpressionContext(org.apache.groovy.parser.antlr4.GroovyParser.PostfixExpressionContext) UnaryMinusExpression(org.codehaus.groovy.ast.expr.UnaryMinusExpression) ClosureListExpression(org.codehaus.groovy.ast.expr.ClosureListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) MethodReferenceExpression(org.codehaus.groovy.ast.expr.MethodReferenceExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) PrefixExpression(org.codehaus.groovy.ast.expr.PrefixExpression) PostfixExpression(org.codehaus.groovy.ast.expr.PostfixExpression) Expression(org.codehaus.groovy.ast.expr.Expression) UnaryPlusExpression(org.codehaus.groovy.ast.expr.UnaryPlusExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) BitwiseNegationExpression(org.codehaus.groovy.ast.expr.BitwiseNegationExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) NotExpression(org.codehaus.groovy.ast.expr.NotExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) MethodPointerExpression(org.codehaus.groovy.ast.expr.MethodPointerExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) LambdaExpression(org.codehaus.groovy.ast.expr.LambdaExpression) GStringExpression(org.codehaus.groovy.ast.expr.GStringExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) RangeExpression(org.codehaus.groovy.ast.expr.RangeExpression) NamedArgumentListExpression(org.codehaus.groovy.ast.expr.NamedArgumentListExpression) SpreadExpression(org.codehaus.groovy.ast.expr.SpreadExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ElvisOperatorExpression(org.codehaus.groovy.ast.expr.ElvisOperatorExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) SpreadMapExpression(org.codehaus.groovy.ast.expr.SpreadMapExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) AttributeExpression(org.codehaus.groovy.ast.expr.AttributeExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) PrefixExpression(org.codehaus.groovy.ast.expr.PrefixExpression) GroovyBugError(org.codehaus.groovy.GroovyBugError) UnaryPlusExpression(org.codehaus.groovy.ast.expr.UnaryPlusExpression) IOException(java.io.IOException) RecognitionException(org.antlr.v4.runtime.RecognitionException) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException)

Example 2 with ExpressionContext

use of org.apache.groovy.parser.antlr4.GroovyParser.ExpressionContext in project groovy by apache.

the class SemanticPredicates method isFollowingArgumentsOrClosure.

/**
 * Check whether following a method name of command expression.
 * Method name should not end with "2: arguments" and "3: closure"
 *
 * @param context the preceding expression
 */
public static boolean isFollowingArgumentsOrClosure(ExpressionContext context) {
    if (context instanceof PostfixExprAltContext) {
        List<ParseTree> peacChildren = ((PostfixExprAltContext) context).children;
        try {
            ParseTree peacChild = peacChildren.get(0);
            List<ParseTree> pecChildren = ((PostfixExpressionContext) peacChild).children;
            ParseTree pecChild = pecChildren.get(0);
            PathExpressionContext pec = (PathExpressionContext) pecChild;
            int t = pec.t;
            return (2 == t || 3 == t);
        } catch (IndexOutOfBoundsException | ClassCastException e) {
            throw new GroovyBugError("Unexpected structure of expression context: " + context, e);
        }
    }
    return false;
}
Also used : PostfixExpressionContext(org.apache.groovy.parser.antlr4.GroovyParser.PostfixExpressionContext) PostfixExprAltContext(org.apache.groovy.parser.antlr4.GroovyParser.PostfixExprAltContext) GroovyBugError(org.codehaus.groovy.GroovyBugError) PathExpressionContext(org.apache.groovy.parser.antlr4.GroovyParser.PathExpressionContext) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Aggregations

PathExpressionContext (org.apache.groovy.parser.antlr4.GroovyParser.PathExpressionContext)2 PostfixExpressionContext (org.apache.groovy.parser.antlr4.GroovyParser.PostfixExpressionContext)2 GroovyBugError (org.codehaus.groovy.GroovyBugError)2 IOException (java.io.IOException)1 RecognitionException (org.antlr.v4.runtime.RecognitionException)1 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 SwitchExpressionContext (org.apache.groovy.parser.antlr4.GroovyLangParser.SwitchExpressionContext)1 CastParExpressionContext (org.apache.groovy.parser.antlr4.GroovyParser.CastParExpressionContext)1 ClosureOrLambdaExpressionContext (org.apache.groovy.parser.antlr4.GroovyParser.ClosureOrLambdaExpressionContext)1 CommandExpressionContext (org.apache.groovy.parser.antlr4.GroovyParser.CommandExpressionContext)1 EnhancedStatementExpressionContext (org.apache.groovy.parser.antlr4.GroovyParser.EnhancedStatementExpressionContext)1 ExpressionContext (org.apache.groovy.parser.antlr4.GroovyParser.ExpressionContext)1 ParExpressionContext (org.apache.groovy.parser.antlr4.GroovyParser.ParExpressionContext)1 PostfixExprAltContext (org.apache.groovy.parser.antlr4.GroovyParser.PostfixExprAltContext)1 StandardLambdaExpressionContext (org.apache.groovy.parser.antlr4.GroovyParser.StandardLambdaExpressionContext)1 AnnotationConstantExpression (org.codehaus.groovy.ast.expr.AnnotationConstantExpression)1 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)1 ArrayExpression (org.codehaus.groovy.ast.expr.ArrayExpression)1 AttributeExpression (org.codehaus.groovy.ast.expr.AttributeExpression)1