Exemple #1
0
        internal static Ast FindFirst(Ast ast, Func <Ast, bool> predicate, bool searchNestedScriptBlocks)
        {
            AstSearcher visitor = new AstSearcher(predicate, true, searchNestedScriptBlocks);

            ast.InternalVisit(visitor);
            return(visitor.Results.FirstOrDefault <Ast>());
        }
Exemple #2
0
        internal static IEnumerable <Ast> FindAll(Ast ast, Func <Ast, bool> predicate, bool searchNestedScriptBlocks)
        {
            AstSearcher visitor = new AstSearcher(predicate, false, searchNestedScriptBlocks);

            ast.InternalVisit(visitor);
            return(visitor.Results);
        }
Exemple #3
0
 public IEnumerable <Ast> FindAll(Func <Ast, bool> predicate, bool searchNestedScriptBlocks)
 {
     if (predicate == null)
     {
         throw PSTraceSource.NewArgumentNullException("predicate");
     }
     return(AstSearcher.FindAll(this, predicate, searchNestedScriptBlocks));
 }
Exemple #4
0
        internal static bool Contains(Ast ast, Func <Ast, bool> predicate, bool searchNestedScriptBlocks)
        {
            Diagnostics.Assert(ast != null && predicate != null, "caller to verify arguments");

            var searcher = new AstSearcher(predicate, stopOnFirst: true, searchNestedScriptBlocks: searchNestedScriptBlocks);

            ast.InternalVisit(searcher);
            return(searcher.Results.FirstOrDefault() != null);
        }
Exemple #5
0
        internal static IEnumerable <Ast> FindAll(Ast ast, Func <Ast, bool> predicate, bool searchNestedScriptBlocks)
        {
            Diagnostics.Assert(ast != null && predicate != null, "caller to verify arguments");

            var searcher = new AstSearcher(predicate, stopOnFirst: false, searchNestedScriptBlocks: searchNestedScriptBlocks);

            ast.InternalVisit(searcher);
            return(searcher.Results);
        }
Exemple #6
0
 internal static bool IsUsingDollarInput(Ast ast)
 {
     return(AstSearcher.Contains(
                ast,
                ast_ =>
     {
         var varAst = ast_ as VariableExpressionAst;
         if (varAst != null)
         {
             return varAst.VariablePath.IsVariable &&
             varAst.VariablePath.UnqualifiedPath.Equals(SpecialVariables.Input,
                                                        StringComparison.OrdinalIgnoreCase);
         }
         return false;
     },
                searchNestedScriptBlocks: false));
 }
Exemple #7
0
        internal string GetWithInputHandlingForInvokeCommandImpl(Tuple <List <VariableExpressionAst>, string> usingVariablesTuple)
        {
            string      str;
            string      str2;
            PipelineAst ast = this.GetSimplePipeline(false, out str, out str2);

            if (ast == null)
            {
                if (usingVariablesTuple != null)
                {
                    return(this.ToStringForSerialization(usingVariablesTuple, base.Extent.StartOffset, base.Extent.EndOffset));
                }
                return(this.ToStringForSerialization());
            }
            if (ast.PipelineElements[0] is CommandExpressionAst)
            {
                if (usingVariablesTuple != null)
                {
                    return(this.ToStringForSerialization(usingVariablesTuple, base.Extent.StartOffset, base.Extent.EndOffset));
                }
                return(this.ToStringForSerialization());
            }
            if (AstSearcher.IsUsingDollarInput(this))
            {
                if (usingVariablesTuple != null)
                {
                    return(this.ToStringForSerialization(usingVariablesTuple, base.Extent.StartOffset, base.Extent.EndOffset));
                }
                return(this.ToStringForSerialization());
            }
            StringBuilder builder = new StringBuilder();

            if (this.ParamBlock != null)
            {
                string str3 = (usingVariablesTuple == null) ? this.ParamBlock.ToString() : this.ToStringForSerialization(usingVariablesTuple, this.ParamBlock.Extent.StartOffset, this.ParamBlock.Extent.EndOffset);
                builder.Append(str3);
            }
            builder.Append("$input |");
            string str4 = (usingVariablesTuple == null) ? ast.ToString() : this.ToStringForSerialization(usingVariablesTuple, ast.Extent.StartOffset, ast.Extent.EndOffset);

            builder.Append(str4);
            return(builder.ToString());
        }
Exemple #8
0
 internal static Ast FindFirst(Ast ast, Func<Ast, bool> predicate, bool searchNestedScriptBlocks)
 {
     AstSearcher visitor = new AstSearcher(predicate, true, searchNestedScriptBlocks);
     ast.InternalVisit(visitor);
     return visitor.Results.FirstOrDefault<Ast>();
 }
Exemple #9
0
 internal static IEnumerable<Ast> FindAll(Ast ast, Func<Ast, bool> predicate, bool searchNestedScriptBlocks)
 {
     AstSearcher visitor = new AstSearcher(predicate, false, searchNestedScriptBlocks);
     ast.InternalVisit(visitor);
     return visitor.Results;
 }
Exemple #10
0
        internal override IEnumerable <PSTypeName> GetInferredType(CompletionContext context)
        {
            if (this.VariablePath.IsVariable)
            {
                Ast parent = this.Parent;
                if (this.VariablePath.IsUnqualified && (this.VariablePath.UserPath.Equals("_", StringComparison.Ordinal) || this.VariablePath.UserPath.Equals("PSItem", StringComparison.OrdinalIgnoreCase)))
                {
                    while (parent != null)
                    {
                        if (parent is ScriptBlockExpressionAst)
                        {
                            break;
                        }
                        parent = parent.Parent;
                    }
                    if (parent != null)
                    {
                        if ((parent.Parent is CommandExpressionAst) && (parent.Parent.Parent is PipelineAst))
                        {
                            if (parent.Parent.Parent.Parent is HashtableAst)
                            {
                                parent = parent.Parent.Parent.Parent;
                            }
                            else if ((parent.Parent.Parent.Parent is ArrayLiteralAst) && (parent.Parent.Parent.Parent.Parent is HashtableAst))
                            {
                                parent = parent.Parent.Parent.Parent.Parent;
                            }
                        }
                        if (parent.Parent is CommandParameterAst)
                        {
                            parent = parent.Parent;
                        }
                        CommandAst iteratorVariable1 = parent.Parent as CommandAst;
                        if (iteratorVariable1 != null)
                        {
                            PipelineAst iteratorVariable2 = (PipelineAst)iteratorVariable1.Parent;
                            int         iteratorVariable3 = iteratorVariable2.PipelineElements.IndexOf(iteratorVariable1) - 1;
                            if (iteratorVariable3 >= 0)
                            {
                                foreach (PSTypeName iteratorVariable4 in iteratorVariable2.PipelineElements[0].GetInferredType(context))
                                {
                                    if (iteratorVariable4.Type != null)
                                    {
                                        if (iteratorVariable4.Type.IsArray)
                                        {
                                            yield return(new PSTypeName(iteratorVariable4.Type.GetElementType()));

                                            continue;
                                        }
                                        if (typeof(IEnumerable).IsAssignableFrom(iteratorVariable4.Type))
                                        {
                                            IEnumerable <Type> iteratorVariable5 = from t in iteratorVariable4.Type.GetInterfaces()
                                                                                   where t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(IEnumerable <>))
                                                                                   select t;
                                            foreach (Type iteratorVariable6 in iteratorVariable5)
                                            {
                                                yield return(new PSTypeName(iteratorVariable6.GetGenericArguments()[0]));
                                            }
                                            continue;
                                        }
                                    }
                                    yield return(iteratorVariable4);
                                }
                            }
                            goto Label_0833;
                        }
                    }
                }
                if (this.VariablePath.IsUnqualified)
                {
                    for (int i = 0; i < SpecialVariables.AutomaticVariables.Length; i++)
                    {
                        if (this.VariablePath.UserPath.Equals(SpecialVariables.AutomaticVariables[i], StringComparison.OrdinalIgnoreCase))
                        {
                            Type type = SpecialVariables.AutomaticVariableTypes[i];
                            if (!type.Equals(typeof(object)))
                            {
                                yield return(new PSTypeName(type));

                                break;
                            }
                            break;
                        }
                    }
                }
                while (parent.Parent != null)
                {
                    parent = parent.Parent;
                }
                if (parent.Parent is FunctionDefinitionAst)
                {
                    parent = parent.Parent;
                }
                IEnumerable <Ast> source             = AstSearcher.FindAll(parent, ast => (((ast is ParameterAst) || (ast is AssignmentStatementAst)) || (ast is ForEachStatementAst)) && this.AstAssignsToSameVariable(ast), true);
                ParameterAst      iteratorVariable10 = source.OfType <ParameterAst>().FirstOrDefault <ParameterAst>();
                if (iteratorVariable10 != null)
                {
                    PSTypeName[] iteratorVariable11 = iteratorVariable10.GetInferredType(context).ToArray <PSTypeName>();
                    if (iteratorVariable11.Length > 0)
                    {
                        foreach (PSTypeName iteratorVariable12 in iteratorVariable11)
                        {
                            yield return(iteratorVariable12);
                        }
                        goto Label_0833;
                    }
                }
                AssignmentStatementAst[] iteratorVariable13 = source.OfType <AssignmentStatementAst>().ToArray <AssignmentStatementAst>();
                foreach (AssignmentStatementAst iteratorVariable14 in iteratorVariable13)
                {
                    ConvertExpressionAst left = iteratorVariable14.Left as ConvertExpressionAst;
                    if ((left != null) && (left.StaticType != null))
                    {
                        yield return(new PSTypeName(left.StaticType));

                        goto Label_0833;
                    }
                }
                ForEachStatementAst iteratorVariable16 = source.OfType <ForEachStatementAst>().FirstOrDefault <ForEachStatementAst>();
                if (iteratorVariable16 != null)
                {
                    foreach (PSTypeName iteratorVariable17 in iteratorVariable16.Condition.GetInferredType(context))
                    {
                        yield return(iteratorVariable17);
                    }
                }
                else
                {
                    int startOffset        = this.Extent.StartOffset;
                    int iteratorVariable19 = 0x7fffffff;
                    AssignmentStatementAst iteratorVariable20 = null;
                    foreach (AssignmentStatementAst ast in iteratorVariable13)
                    {
                        int endOffset = ast.Extent.EndOffset;
                        if ((endOffset < startOffset) && ((startOffset - endOffset) < iteratorVariable19))
                        {
                            iteratorVariable19 = startOffset - endOffset;
                            iteratorVariable20 = ast;
                        }
                    }
                    if (iteratorVariable20 != null)
                    {
                        foreach (PSTypeName iteratorVariable21 in iteratorVariable20.Right.GetInferredType(context))
                        {
                            yield return(iteratorVariable21);
                        }
                    }
                }
            }
Label_0833:
            yield break;
        }
Exemple #11
0
        internal static bool Contains(Ast ast, Func<Ast, bool> predicate, bool searchNestedScriptBlocks)
        {
            Diagnostics.Assert(ast != null && predicate != null, "caller to verify arguments");

            var searcher = new AstSearcher(predicate, stopOnFirst: true, searchNestedScriptBlocks: searchNestedScriptBlocks);
            ast.InternalVisit(searcher);
            return searcher.Results.FirstOrDefault() != null;
        }
Exemple #12
0
        internal static IEnumerable<Ast> FindAll(Ast ast, Func<Ast, bool> predicate, bool searchNestedScriptBlocks)
        {
            Diagnostics.Assert(ast != null && predicate != null, "caller to verify arguments");

            var searcher = new AstSearcher(predicate, stopOnFirst: false, searchNestedScriptBlocks: searchNestedScriptBlocks);
            ast.InternalVisit(searcher);
            return searcher.Results;
        }