Пример #1
0
        public UstNode VisitArgumentList([NotNull] ECMAScriptParser.ArgumentListContext context)
        {
            Expression[] args   = context.singleExpression().Select(expr => Visit(expr).ToExpressionIfRequired()).ToArray();
            var          result = new ArgsNode(args, context.GetTextSpan(), FileNode);

            return(result);
        }
Пример #2
0
        public UstNode VisitExpressionList(JavaParser.ExpressionListContext context)
        {
            Expression[] exprs  = context.expression().Select(expr => (Expression)Visit(expr)).ToArray();
            var          result = new ArgsNode(exprs, context.GetTextSpan(), FileNode);

            return(result);
        }
Пример #3
0
        /// <summary>
        /// puts ((1..20).to_a)    (ok)
        /// puts (1..20).to_a      (error)
        /// </summary>
        /// <returns></returns>
        private ArgsNode ParseExpressionListAsArgs()
        {
            ArgsNode parameters = new ArgsNode();

            bool inparentheses = TryParseToken(TokenType.Separator, "(");

            for (AstNode expression = ParseExpression();
                 expression != null;
                 expression = ParseExpression())
            {
                parameters.Push(expression);
                if (!TryParseToken(TokenType.Separator, ","))
                {
                    break;
                }
            }

            if (inparentheses)
            {
                ParseToken(TokenType.Separator, ")");

                if (TryParseName(DO))
                {
                    parameters.Push(ParseBlockExpression());
                }
                else if (TryParseToken(TokenType.Separator, "{"))
                {
                    parameters.Push(ParseBlockExpression(true));
                }
            }

            return(parameters);
        }
Пример #4
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 UstNode VisitAnonymousObjectCreationExpression(AnonymousObjectCreationExpressionSyntax node)
        {
            var typeToken = new TypeToken("Anonymous", node.OpenBraceToken.GetTextSpan(), FileNode);

            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(), FileNode);
                    var right = (Expression)base.Visit(init.Expression);

                    var assignment = new AssignmentExpression(left, right, init.GetTextSpan(), FileNode);
                    return(assignment);
                }
                catch (Exception ex)
                {
                    Logger.LogError(new ConversionException(ex.Message));
                    return(null);
                }
            }).ToArray();
            var argsNode = new ArgsNode(args, node.GetTextSpan(), FileNode);

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

            return(result);
        }
Пример #5
0
        public UstNode VisitInvocationExpression(DslParser.InvocationExpressionContext context)
        {
            ArgsNode args   = context.args() == null ? new ArgsNode() : (ArgsNode)VisitArgs(context.args());
            var      expr   = (Expression)VisitExpression(context.expression());
            var      result = new InvocationExpression(expr, args, context.GetTextSpan(), null);

            return(result);
        }
Пример #6
0
        public override UstNode VisitBracketedArgumentList(BracketedArgumentListSyntax node)
        {
            Expression[] args = node.Arguments.Select(arg => (Expression)VisitAndReturnNullIfError(arg))
                                .ToArray();

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

            return(result);
        }
Пример #7
0
        public override UstNode VisitInvocationExpression(InvocationExpressionSyntax node)
        {
            var      target = (Expression)base.Visit(node.Expression);
            ArgsNode args   = node.ArgumentList == null ? null : (ArgsNode)VisitArgumentList(node.ArgumentList);

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

            return(result);
        }
Пример #8
0
        public override UstNode VisitElementAccessExpression(ElementAccessExpressionSyntax node)
        {
            var      target = (Expression)base.Visit(node.Expression);
            ArgsNode args   = node.ArgumentList == null ? null : (ArgsNode)VisitBracketedArgumentList(node.ArgumentList);

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

            return(result);
        }
Пример #9
0
        public UstNode VisitObjectCreationExpression(DslParser.ObjectCreationExpressionContext context)
        {
            var      literal   = (Token)VisitLiteralOrPatternId(context.literalOrPatternId());
            var      typeToken = new TypeToken(literal.TextValue, literal.TextSpan, null);
            ArgsNode args      = context.args() == null ? new ArgsNode() : (ArgsNode)VisitArgs(context.args());
            var      result    = new ObjectCreateExpression(typeToken, args, context.GetTextSpan(), null);

            return(result);
        }
Пример #10
0
        private DefNode ParseDefExpression()
        {
            Token              token      = t;
            AstNode            named      = ParseFunctionNamePathExpression();
            ArgsNode           parameters = ParseParameterList();
            ExpressionListNode body       = ParseCommandList();

            return(NODE_LINENO(new DefNode(named, parameters, body), token));
        }
Пример #11
0
        public override UstNode VisitRefTypeExpression(RefTypeExpressionSyntax node)
        {
            IdToken    id   = ConvertId(node.Keyword);
            Expression expr = (Expression)base.Visit(node.Expression);
            ArgsNode   args = new ArgsNode(new Expression[] { expr }, expr.TextSpan, FileNode);

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

            return(result);
        }
Пример #12
0
        public UstNode VisitIndexerExpression(DslParser.IndexerExpressionContext context)
        {
            var target = (Expression)VisitExpression(context.expression(0));
            var args   = new ArgsNode(new[] { (Expression)VisitExpression(context.expression(1)) },
                                      context.expression(1).GetTextSpan(), null);

            var result = new IndexerExpression(target, args, context.GetTextSpan(), null);

            return(result);
        }
Пример #13
0
        public override UstNode VisitTypeOfExpression(TypeOfExpressionSyntax node)
        {
            IdToken   id   = ConvertId(node.Keyword);
            TypeToken type = ConvertType(base.Visit(node.Type));
            ArgsNode  args = new ArgsNode(new[] { type }, type.TextSpan, FileNode);

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

            return(result);
        }
Пример #14
0
        private BlockNode ParseBlockExpression(bool usebraces = false)
        {
            if (TryParseToken(TokenType.Separator, "|"))
            {
                ArgsNode paramnames = ParseParameterList(false);
                ParseToken(TokenType.Separator, "|");
                return(NODE_LINENO(new BlockNode(paramnames, ParseCommandList(usebraces)), t));
            }

            return(new BlockNode(null, ParseCommandList(usebraces)));
        }
Пример #15
0
        private ArgsNode GetArgsNode(TSqlParser.Expression_listContext context)
        {
            ArgsNode result;

            if (context != null)
            {
                result = new ArgsNode(context.expression().Select(expr => (Expression)Visit(expr)).ToArray());
            }
            else
            {
                result = new ArgsNode();
            }
            return(result);
        }
Пример #16
0
        // Named argument
        // Named expression

        public override UstNode VisitObjectCreationExpression(ObjectCreationExpressionSyntax node)
        {
            var      type = ConvertType(base.Visit(node.Type));
            ArgsNode args = node.ArgumentList == null ? null : (ArgsNode)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(),
                FileNode)
            {
                Initializers = initializers
            };

            return(result);
        }
Пример #17
0
        public UstNode VisitArgs([NotNull] DslParser.ArgsContext context)
        {
            List <Expression> expressions = context.arg().Select(arg =>
                                                                 (Expression)VisitArg(arg)).ToList();

            var      span = context.GetTextSpan();
            ArgsNode result;

            if (expressions.All(expr => expr.NodeType != NodeType.PatternMultipleExpressions))
            {
                result = new ArgsNode(expressions, span, null);
            }
            else
            {
                result = new PatternExpressions()
                {
                    Collection = expressions,
                    TextSpan   = span
                };
            }
            return(result);
        }
Пример #18
0
        public UstNode VisitCreator(JavaParser.CreatorContext context)
        {
            JavaParser.CreatedNameContext      createdName      = context.createdName();
            JavaParser.ClassCreatorRestContext classCreatorRest = context.classCreatorRest();
            ArgsNode args;

            if (classCreatorRest != null)
            {
                args = (ArgsNode)Visit(classCreatorRest.arguments());
            }
            else
            {
                args = new ArgsNode();
            }
            // TODO: add classBody

            var result = new ObjectCreateExpression(
                new TypeToken(createdName.GetText(), createdName.GetTextSpan(), FileNode), args,
                context.GetTextSpan(), FileNode);

            return(result);
        }
Пример #19
0
        private ArgsNode ParseParameterList(bool canhaveparens = true)
        {
            Token    token      = null;
            ArgsNode parameters = new ArgsNode();

            bool inparentheses = false;

            if (canhaveparens)
            {
                inparentheses = TryParseToken(TokenType.Separator, "(");
            }

            for (string name = TryParseName(out token); name != null; name = ParseName(out token))
            {
                if (name.StartsWith("&"))
                {
                    parameters.block = NODE_LINENO(new SymNode(name), token);
                }
                else
                {
                    parameters.Push(NODE_LINENO(new SymNode(name), token));
                }

                if (!TryParseToken(TokenType.Separator, ","))
                {
                    break;
                }
            }

            if (inparentheses)
            {
                ParseToken(TokenType.Separator, ")");
            }

            return(parameters);
        }
Пример #20
0
 public virtual void Exit(ArgsNode argsNode)
 {
 }
Пример #21
0
 public InvocationExpression(Expression target, ArgsNode arguments, TextSpan textSpan, FileNode fileNode)
     : base(textSpan, fileNode)
 {
     Target    = target;
     Arguments = arguments;
 }
Пример #22
0
 public ObjectCreateExpression()
 {
     Arguments = new ArgsNode();
 }
Пример #23
0
 public virtual void Enter(ArgsNode argsNode)
 {
 }
Пример #24
0
        public UstNode 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, FileNode);
                    return(result);

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

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

            if (child1Terminal != null)
            {
                switch (child1Terminal.Symbol.Type)
                {
                case JavaParser.DOT:     // '.'
                    target = (Expression)Visit(context.expression(0));
                    var id = context.IDENTIFIER();
                    if (id != null)
                    {
                        result = new MemberReferenceExpression(target, (IdToken)Visit(id), textSpan, FileNode);
                        return(result);
                    }

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

                    var child2Terminal = context.GetChild <ITerminalNode>(1);
                    // TODO: implement
                    switch (child2Terminal.Symbol.Type)
                    {
                    case JavaParser.THIS:
                        break;

                    case JavaParser.NEW:
                        break;

                    case JavaParser.SUPER:
                        break;
                    }
                    break;

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

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

                case JavaParser.LPAREN:     // '('
                    target = (Expression)Visit(context.expression(0));
                    // TODO: fix with ArgsNode
                    JavaParser.ExpressionListContext expressionList = context.expressionList();

                    if (expressionList != null)
                    {
                        args = (ArgsNode)Visit(expressionList);
                    }
                    else
                    {
                        args = new ArgsNode();
                    }

                    result = new InvocationExpression(target, args, textSpan, FileNode);
                    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
                    {
                        Left     = new CastExpression(type, expression, context.GetTextSpan(), FileNode),
                        Operator = new BinaryOperatorLiteral(BinaryOperator.NotEqual, default(TextSpan), FileNode),
                        Right    = new NullLiteral(default(TextSpan), FileNode),
                        TextSpan = context.GetTextSpan(),
                        FileNode = FileNode
                    };
                    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, FileNode);
                    return(result);

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

                    if (expr1 != null)
                    {
                        var right = (Expression)Visit(expr1);

                        if (text == "=")
                        {
                            result = new AssignmentExpression(left, right, textSpan, FileNode);
                        }
                        else if (BinaryOperatorLiteral.TextBinaryAssignmentOperator.Contains(text))
                        {
                            BinaryOperator op;
                            if (text == ">>>=")
                            {
                                op = BinaryOperator.ShiftRight;     // TODO: fix shift operator.
                            }
                            else
                            {
                                op = BinaryOperatorLiteral.TextBinaryOperator[text.Remove(text.Length - 1)];
                            }

                            result = ConverterHelper.ConvertToAssignmentExpression(left, op, child1Terminal.GetTextSpan(), right,
                                                                                   context.GetTextSpan(), FileNode);
                        }
                        else
                        {
                            BinaryOperator op;
                            if (text == ">>>")
                            {
                                op = BinaryOperator.ShiftRight;      // TODO: fix shift operator.
                            }
                            else
                            {
                                op = BinaryOperatorLiteral.TextBinaryOperator[text];
                            }
                            var opLiteral = new BinaryOperatorLiteral(op, child1Terminal.GetTextSpan(), FileNode);

                            result = new BinaryOperatorExpression(left, opLiteral, right, textSpan, FileNode);
                        }
                    }
                    else
                    {
                        // post increment or decrement.
                        UnaryOperator op        = UnaryOperatorLiteral.PostfixTextUnaryOperator[text];
                        var           opLiteral = new UnaryOperatorLiteral(op, child1Terminal.GetTextSpan(), FileNode);

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

                    return(result);
                }
            }

            return(Visit(context.GetChild(0)));
        }
Пример #25
0
 public ObjectCreateExpression(Token type, ArgsNode args, TextSpan textSpan, FileNode fileNode)
     : base(textSpan, fileNode)
 {
     Type      = type;
     Arguments = args;
 }
Пример #26
0
 public virtual UstNode Visit(ArgsNode argsNode)
 {
     return(VisitChildren(argsNode));
 }
Пример #27
0
        public override UstNode VisitElementBindingExpression(ElementBindingExpressionSyntax node)
        {
            ArgsNode args = node.ArgumentList == null ? null : (ArgsNode)VisitBracketedArgumentList(node.ArgumentList);

            return(args);
        }