Example #1
0
        public ExpressionTree(ParseInfo parseInfo, Scope scope, BinaryOperatorExpression exprContext, bool usedAsValue)
        {
            _parseInfo      = parseInfo;
            ExprContextTree = Flatten(parseInfo.Script, exprContext);

            // Setup
            var usageResolvers = new UsageResolver[ExprContextTree.Length];

            for (int i = 0; i < ExprContextTree.Length; i++)
            {
                usageResolvers[i] = new UsageResolver();

                ParseInfo partInfo = parseInfo.SetUsageResolver(usageResolvers[i], i == 0 ? null : usageResolvers[i - 1]);
                // If this is not the first expression, clear tail data and set the source expression.
                if (i != 0)
                {
                    partInfo = partInfo.ClearTail().SetSourceExpression(ExprContextTree[i - 1]);
                }
                // If this is not the last expression, clear head data.
                if (i != ExprContextTree.Length - 1)
                {
                    partInfo = partInfo.ClearHead();
                }

                ExprContextTree[i].Setup(new TreeContextParseInfo()
                {
                    ParseInfo        = partInfo,
                    Getter           = scope,
                    Scope            = i == 0 ? scope : ExprContextTree[i - 1].GetScope() ?? new Scope(),
                    Parent           = i == 0 ? null : ExprContextTree[i - 1],
                    UsedAsExpression = usedAsValue || i < ExprContextTree.Length - 1,
                    IsLast           = i == ExprContextTree.Length - 1
                });
            }

            for (int i = 0; i < usageResolvers.Length; i++)
            {
                usageResolvers[i].ResolveUnknownIfNotResolved();
            }

            // Get expressions
            Tree = new IExpression[ExprContextTree.Length];
            IExpression current = ExprContextTree[0].GetExpression();

            Tree[0] = current;
            if (current != null)
            {
                for (int i = 1; i < ExprContextTree.Length; i++)
                {
                    current = ExprContextTree[i].GetExpression();

                    Tree[i] = current;

                    if (current == null)
                    {
                        Completed = false;
                        break;
                    }
                }
            }
            else
            {
                Completed = false;
            }

            if (Completed)
            {
                Result = Tree[Tree.Length - 1];
            }

            // Get the completion items for each expression in the path.
            GetCompletion(parseInfo, scope);
        }