Exemple #1
0
        public UstNode VisitPrimary(JavaParser.PrimaryContext context)
        {
            TextSpan   textSpan       = context.GetTextSpan();
            var        child0Terminal = context.GetChild(0) as ITerminalNode;
            Expression result;

            if (child0Terminal != null)
            {
                switch (child0Terminal.Symbol.Type)
                {
                case JavaParser.LPAREN:
                    result = (Expression)Visit(context.expression());
                    return(result);

                case JavaParser.THIS:
                    result = new ThisReferenceToken(textSpan, FileNode);
                    return(result);

                case JavaParser.SUPER:
                    result = new BaseReferenceExpression(textSpan, FileNode);
                    return(result);

                case JavaParser.VOID:
                    var id = new IdToken("TypeOf", ((ITerminalNode)context.GetChild(2)).GetTextSpan(), FileNode);
                    var child0TerminalSpan = child0Terminal.GetTextSpan();
                    result = new InvocationExpression(id,
                                                      new ArgsNode(new Expression[] { new NullLiteral(child0TerminalSpan, FileNode) }, child0TerminalSpan, FileNode),
                                                      textSpan,
                                                      FileNode);
                    return(result);
                }
            }

            JavaParser.TypeTypeOrVoidContext type = context.typeTypeOrVoid();
            if (type != null)
            {
                var typeToken = (TypeToken)Visit(type);
                var id        = new IdToken("TypeOf", ((ITerminalNode)context.GetChild(2)).GetTextSpan(), FileNode);
                result = new InvocationExpression(id,
                                                  new ArgsNode(new Expression[] { typeToken }, typeToken.TextSpan, FileNode),
                                                  textSpan,
                                                  FileNode);
                return(result);
            }

            JavaParser.NonWildcardTypeArgumentsContext args = context.nonWildcardTypeArguments();
            if (args != null)
            {
                var typeToken = (TypeToken)Visit(args);

                throw new NotImplementedException();
            }

            return(Visit(context.GetChild(0)));
        }
Exemple #2
0
        public override Ust VisitThisExpression(ThisExpressionSyntax node)
        {
            var result = new ThisReferenceToken(node.GetTextSpan());

            return(result);
        }
Exemple #3
0
        public Ust VisitExpression(JavaParser.ExpressionContext context)
        {
            var        textSpan       = context.GetTextSpan();
            var        child0Terminal = context.GetChild(0) as ITerminalNode;
            Expression target;
            Expression result;

            if (child0Terminal != null)
            {
                switch (child0Terminal.Symbol.Type)
                {
                case JavaParser.NEW:
                    result = (Expression)Visit(context.creator());
                    return(result);

                case JavaParser.LPAREN:     // '(' type ')' expression
                    var type = (TypeToken)Visit(context.typeType());
                    target = (Expression)Visit(context.expression(0));
                    result = new CastExpression(type, target, textSpan);
                    return(result);

                default:     // unary operator ('+', '-', '++', '--', '~', '!')
                    UnaryOperator op        = UnaryOperatorLiteral.PrefixTextUnaryOperator[child0Terminal.GetText()];
                    var           opLiteral = new UnaryOperatorLiteral(op, child0Terminal.GetTextSpan());
                    target = (Expression)Visit(context.expression(0));
                    result = new UnaryOperatorExpression(opLiteral, target, textSpan);
                    return(result);
                }
            }

            ArgsUst args;
            var     child1Terminal = context.GetChild(1) as ITerminalNode;

            if (child1Terminal != null)
            {
                switch (child1Terminal.Symbol.Type)
                {
                case JavaParser.DOT:     // '.'
                    target = (Expression)Visit(context.expression(0));

                    if (context.methodCall() != null)
                    {
                        var invocation = (InvocationExpression)Visit(context.methodCall());
                        return(new InvocationExpression(
                                   new MemberReferenceExpression(target, invocation.Target, target.TextSpan.Union(invocation.Target.TextSpan)),
                                   invocation.Arguments,
                                   textSpan));
                    }

                    // TODO: implement base processing

                    Expression rightPart = null;
                    if (context.IDENTIFIER() != null)
                    {
                        rightPart = (IdToken)Visit(context.IDENTIFIER());
                    }
                    else if (context.THIS() != null)
                    {
                        rightPart = new ThisReferenceToken(context.THIS().GetTextSpan());
                    }

                    if (rightPart != null)
                    {
                        return(new MemberReferenceExpression(target, rightPart, textSpan));
                    }

                    if (context.innerCreator() != null)
                    {
                        return(Visit(context.innerCreator()));
                    }

                    if (context.explicitGenericInvocation() != null)
                    {
                        return(VisitChildren(context.explicitGenericInvocation()).ToExpressionIfRequired());
                    }

                    break;

                case JavaParser.LBRACK:     // '['
                    target = (Expression)Visit(context.expression(0));
                    Expression expr = (Expression)Visit(context.expression(1));
                    args = new ArgsUst(new Expression[] { expr }, expr.TextSpan);

                    result = new IndexerExpression(target, args, textSpan);
                    return(result);

                case JavaParser.INSTANCEOF:     // x instanceof y -> (y)x != null
                    var expression = (Expression)Visit(context.expression(0));
                    var type       = (TypeToken)Visit(context.typeType());
                    result = new BinaryOperatorExpression(context.GetTextSpan())
                    {
                        Left     = new CastExpression(type, expression, context.GetTextSpan()),
                        Operator = new BinaryOperatorLiteral(BinaryOperator.NotEqual, default),
                        Right    = new NullLiteral(default),
                        Root     = root
                    };
                    return(result);

                case JavaParser.QUESTION:     // '?'
                    var condition = (Expression)Visit(context.expression(0));
                    var trueExpr  = (Expression)Visit(context.expression(1));
                    var falseExpr = (Expression)Visit(context.expression(2));

                    result = new ConditionalExpression(condition, trueExpr, falseExpr, textSpan);
                    return(result);

                case JavaParser.COLONCOLON:
                    return(VisitChildren(context));

                default:     // binary operator
                    var left = (Expression)Visit(context.expression(0));
                    JavaParser.ExpressionContext expr1 = context.expression(1);

                    if (expr1 != null)
                    {
                        string opText = string.Concat(context.children.Skip(1).Take(context.children.Count - 2));
                        var    right  = (Expression)Visit(expr1);

                        // TODO: fix via grammar refactoring (alternative labels).
                        if (opText == "=" ||
                            (opText.EndsWith("=") && BinaryOperatorLiteral.TextBinaryOperator.ContainsKey(opText.Remove(opText.Length - 1))))
                        {
                            result = UstUtils.CreateAssignExpr(left, right, context.GetTextSpan(),
                                                               opText, child1Terminal.GetTextSpan());
                        }
                        else
                        {
                            BinaryOperator op        = BinaryOperatorLiteral.TextBinaryOperator[opText];
                            var            opLiteral = new BinaryOperatorLiteral(op, child1Terminal.GetTextSpan());
                            result = new BinaryOperatorExpression(left, opLiteral, right, textSpan);
                        }
                    }
                    else
                    {
                        // post increment or decrement.
                        UnaryOperator op        = UnaryOperatorLiteral.PostfixTextUnaryOperator[context.postfix.Text];
                        var           opLiteral = new UnaryOperatorLiteral(op, child1Terminal.GetTextSpan());

                        result = new UnaryOperatorExpression(opLiteral, left, textSpan);
                        return(result);
                    }
                    return(result);
                }
            }

            return(VisitChildren(context));
        }
Exemple #4
0
 public virtual T Visit(ThisReferenceToken thisReferenceLiteral)
 {
     return(VisitChildren(thisReferenceLiteral));
 }
Exemple #5
0
 public virtual void Exit(ThisReferenceToken thisReferenceLiteral)
 {
 }