Search in sources :

Example 1 with PrefixExpression

use of org.codehaus.groovy.ast.expr.PrefixExpression 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 PrefixExpression

use of org.codehaus.groovy.ast.expr.PrefixExpression in project groovy by apache.

the class StaticTypeCheckingVisitor method visitPrefixExpression.

@Override
public void visitPrefixExpression(final PrefixExpression expression) {
    Expression operand = expression.getExpression();
    int operator = expression.getOperation().getType();
    visitPrefixOrPostifExpression(expression, operand, operator);
}
Also used : 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) StaticTypeCheckingSupport.evaluateExpression(org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.evaluateExpression) NotExpression(org.codehaus.groovy.ast.expr.NotExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) 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) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) LambdaExpression(org.codehaus.groovy.ast.expr.LambdaExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) RangeExpression(org.codehaus.groovy.ast.expr.RangeExpression) 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) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) AttributeExpression(org.codehaus.groovy.ast.expr.AttributeExpression) ClosureSignatureHint(groovy.transform.stc.ClosureSignatureHint)

Aggregations

AnnotationConstantExpression (org.codehaus.groovy.ast.expr.AnnotationConstantExpression)2 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)2 ArrayExpression (org.codehaus.groovy.ast.expr.ArrayExpression)2 AttributeExpression (org.codehaus.groovy.ast.expr.AttributeExpression)2 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)2 BitwiseNegationExpression (org.codehaus.groovy.ast.expr.BitwiseNegationExpression)2 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)2 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)2 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)2 ClosureListExpression (org.codehaus.groovy.ast.expr.ClosureListExpression)2 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)2 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)2 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)2 ElvisOperatorExpression (org.codehaus.groovy.ast.expr.ElvisOperatorExpression)2 EmptyExpression (org.codehaus.groovy.ast.expr.EmptyExpression)2 Expression (org.codehaus.groovy.ast.expr.Expression)2 LambdaExpression (org.codehaus.groovy.ast.expr.LambdaExpression)2 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)2 MapEntryExpression (org.codehaus.groovy.ast.expr.MapEntryExpression)2 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)2