Beispiel #1
0
        /// <summary>
        /// var a = new A
        /// {
        ///     b = 2,
        ///     c = 3,
        /// }
        ///
        /// var a = new A();
        /// a.b = 2;
        /// a.c = 3;
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public override Ust VisitAnonymousObjectCreationExpression(AnonymousObjectCreationExpressionSyntax node)
        {
            var typeToken = new TypeToken("Anonymous", node.OpenBraceToken.GetTextSpan());

            Expression[] args = node.Initializers.Select(init =>
            {
                try
                {
                    var left = init.NameEquals == null ? null :
                               new MemberReferenceExpression(typeToken,
                                                             ConvertId(init.NameEquals.Name.Identifier), init.NameEquals.Name.GetTextSpan());
                    var right = (Expression)base.Visit(init.Expression);

                    var assignment = new AssignmentExpression(left, right, init.GetTextSpan());
                    return(assignment);
                }
                catch (Exception ex) when(!(ex is ThreadAbortException))
                {
                    Logger.LogError(new ConversionException(root?.SourceCodeFile, message: ex.Message));
                    return(null);
                }
            }).ToArray();
            var argsNode = new ArgsUst(args, node.GetTextSpan());

            var result = new ObjectCreateExpression(typeToken, argsNode, node.GetTextSpan());

            return(result);
        }
Beispiel #2
0
        public Ust VisitExpressionList(JavaParser.ExpressionListContext context)
        {
            Expression[] exprs  = context.expression().Select(expr => (Expression)Visit(expr)).ToArray();
            var          result = new ArgsUst(exprs, context.GetTextSpan());

            return(result);
        }
Beispiel #3
0
        public Ust VisitArguments([NotNull] JavaScriptParser.ArgumentsContext context)
        {
            Expression[] args   = context.singleExpression().Select(expr => Visit(expr).ToExpressionIfRequired()).ToArray();
            var          result = new ArgsUst(args, context.GetTextSpan());

            return(result);
        }
Beispiel #4
0
        public Ust VisitInnerCreator(JavaParser.InnerCreatorContext context)
        {
            ArgsUst args = (ArgsUst)Visit(context.classCreatorRest().arguments());

            return(new ObjectCreateExpression(
                       new TypeToken(context.IDENTIFIER().GetText(), context.IDENTIFIER().GetTextSpan()), args,
                       context.GetTextSpan()));
        }
Beispiel #5
0
        public override Ust VisitElementAccessExpression(ElementAccessExpressionSyntax node)
        {
            var     target = (Expression)base.Visit(node.Expression);
            ArgsUst args   = node.ArgumentList == null ? null : (ArgsUst)VisitBracketedArgumentList(node.ArgumentList);
            var     result = new IndexerExpression(target, args, node.GetTextSpan());

            return(result);
        }
Beispiel #6
0
        public override Ust VisitInvocationExpression(InvocationExpressionSyntax node)
        {
            var     target = (Expression)base.Visit(node.Expression);
            ArgsUst args   = node.ArgumentList == null ? null : (ArgsUst)VisitArgumentList(node.ArgumentList);

            var result = new InvocationExpression(target, args, node.GetTextSpan());

            return(result);
        }
Beispiel #7
0
        public override Ust VisitBracketedArgumentList(BracketedArgumentListSyntax node)
        {
            Expression[] args = node.Arguments.Select(arg => (Expression)VisitAndReturnNullIfError(arg))
                                .ToArray();

            var result = new ArgsUst(args, node.GetTextSpan());

            return(result);
        }
Beispiel #8
0
        public override Ust VisitTypeOfExpression(TypeOfExpressionSyntax node)
        {
            IdToken   id   = ConvertId(node.Keyword);
            TypeToken type = ConvertType(base.Visit(node.Type));
            ArgsUst   args = new ArgsUst(new[] { type }, type.TextSpan);

            var result = new InvocationExpression(id, args, node.GetTextSpan());

            return(result);
        }
Beispiel #9
0
        public override Ust VisitRefTypeExpression(RefTypeExpressionSyntax node)
        {
            IdToken    id   = ConvertId(node.Keyword);
            Expression expr = (Expression)base.Visit(node.Expression);
            ArgsUst    args = new ArgsUst(new Expression[] { expr }, expr.TextSpan);

            var result = new InvocationExpression(id, args, node.GetTextSpan());

            return(result);
        }
        private ArgsUst GetArgsNode(TSqlParser.Expression_listContext context)
        {
            ArgsUst result;

            if (context != null)
            {
                result = new ArgsUst(context.expression().Select(expr => (Expression)Visit(expr)).ToArray());
            }
            else
            {
                result = new ArgsUst();
            }
            return(result);
        }
Beispiel #11
0
        public Ust VisitCreator(JavaParser.CreatorContext context)
        {
            Ust result;

            JavaParser.CreatedNameContext      createdName      = context.createdName();
            JavaParser.ArrayCreatorRestContext arrayCreatorRest = context.arrayCreatorRest();
            if (arrayCreatorRest != null)
            {
                var initializer = (MultichildExpression)Visit(arrayCreatorRest.arrayInitializer());
                if (initializer != null)
                {
                    // new int[] { 1, 2 };
                    int dimensions = (arrayCreatorRest.ChildCount - 1) / 2; // number of square bracket pairs
                    var sizes      = Enumerable.Range(0, dimensions).Select(
                        _ => new IntLiteral(0, createdName.GetTextSpan())).ToList <Expression>();
                    var initializers = initializer.Expressions.Where(el => !(el is IdToken));
                    result = new ArrayCreationExpression(
                        new TypeToken(createdName.GetText(), createdName.GetTextSpan()), sizes,
                        initializers, context.GetTextSpan());
                }
                else
                {
                    // new int[3][4][];
                    int dimensions = (arrayCreatorRest.ChildCount - arrayCreatorRest.expression().Length) / 2;
                    var sizes      = Enumerable.Range(0, dimensions).Select(
                        i => i < arrayCreatorRest.expression().Length ?
                        (Expression)Visit(arrayCreatorRest.expression(i)) :
                        new IntLiteral(0, createdName.GetTextSpan())).ToList();
                    result = new ArrayCreationExpression(
                        new TypeToken(createdName.GetText(), createdName.GetTextSpan()), sizes,
                        null, context.GetTextSpan());
                }
            }
            else
            {
                JavaParser.ClassCreatorRestContext classCreatorRest = context.classCreatorRest();
                ArgsUst args = (ArgsUst)Visit(classCreatorRest?.arguments()) ?? new ArgsUst();

                // TODO: add classBody

                result = new ObjectCreateExpression(
                    new TypeToken(createdName.GetText(), createdName.GetTextSpan()), args,
                    context.GetTextSpan());
            }
            return(result);
        }
Beispiel #12
0
        // Named argument
        // Named expression

        public override Ust VisitObjectCreationExpression(ObjectCreationExpressionSyntax node)
        {
            var     type = ConvertType(base.Visit(node.Type));
            ArgsUst args = node.ArgumentList == null ? null : (ArgsUst)VisitArgumentList(node.ArgumentList);

            var initializers = node.Initializer == null ? null :
                               node.Initializer.Expressions.Select(e => (Expression)VisitAndReturnNullIfError(e))
                               .ToList();

            var result = new ObjectCreateExpression(
                type,
                args,
                node.GetTextSpan())
            {
                Initializers = initializers
            };

            return(result);
        }
Beispiel #13
0
        public override Ust VisitElementBindingExpression(ElementBindingExpressionSyntax node)
        {
            ArgsUst args = node.ArgumentList == null ? null : (ArgsUst)VisitBracketedArgumentList(node.ArgumentList);

            return(args);
        }
Beispiel #14
0
 public ObjectCreateExpression()
 {
     Arguments = new ArgsUst();
 }
Beispiel #15
0
 public ObjectCreateExpression(Token type, ArgsUst args, TextSpan textSpan)
     : base(textSpan)
 {
     Type      = type;
     Arguments = args;
 }
 public IndexerExpression(Expression target, ArgsUst args, TextSpan textSpan)
     : base(textSpan)
 {
     Target    = target;
     Arguments = args;
 }
 public ObjectCreateExpression(Expression target, ArgsUst args, TextSpan textSpan)
     : base(textSpan)
 {
     Type      = target;
     Arguments = args;
 }
Beispiel #18
0
 public virtual T Visit(ArgsUst argsUst)
 {
     return(VisitChildren(argsUst));
 }
Beispiel #19
0
 public InvocationExpression(Expression target, ArgsUst arguments, TextSpan textSpan)
     : base(textSpan)
 {
     Target    = target;
     Arguments = arguments ?? throw new ArgumentNullException(nameof(arguments));
 }
Beispiel #20
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));
        }