public override AstVisitAction VisitCommandParameter(CommandParameterAst ast)
        {
            if (ast.ParameterName.ToLower() == "typedefinition")
                IsTypeDefinitionAst = true;

            return AstVisitAction.Continue;
        }
        public void IsTypeDefinitionAst_should_be_false_when_visit_not_typedefinition_CommandParameterAst()
        {
            var commandParameterAst = new CommandParameterAst(extent, "Other", argument, extent);

            visitor.VisitCommandParameter(commandParameterAst);

            visitor.IsTypeDefinitionAst.Should().BeFalse();
        }
        public void IsTypeDefinitionAst_should_be_true_when_visit_typedefinition_ast()
        {
            var commandParameterAst = new CommandParameterAst(extent, "TypeDefinition", argument, extent);

            visitor.VisitCommandParameter(commandParameterAst);

            visitor.IsTypeDefinitionAst.Should().BeTrue();
        }
Beispiel #4
0
 public object VisitCommandParameter(CommandParameterAst commandParameterAst)
 {
     if (commandParameterAst.Argument != null)
     {
         commandParameterAst.Argument.Accept(this);
     }
     return(null);
 }
        public void VarName_should_return_name_from_argument()
        {
            var commandParameterAst = new CommandParameterAst(extent, "TypeDefinition", argument, extent);

            visitor.VisitCommandParameter(commandParameterAst);
            visitor.VisitVariableExpression(argument);

            visitor.VarName.Should().Be(argument.VariablePath.UserPath);
        }
Beispiel #6
0
    public System.Object VisitCommandParameter(System.Management.Automation.Language.CommandParameterAst commandParameterAst)
    {
        IScriptExtent mappedExtent = MapExtent(commandParameterAst.Extent);

        ExpressionAst mappedArgument = commandParameterAst.Argument != null?_VisitExpression(commandParameterAst.Argument) : null;

        IScriptExtent mappedErrorPosition = MapExtent(commandParameterAst.ErrorPosition);


        return(new CommandParameterAst(mappedExtent, commandParameterAst.ParameterName, mappedArgument, mappedErrorPosition));
    }
 /// <summary>
 /// Checks to see if this command parameter is the symbol we are looking for.
 /// </summary>
 /// <param name="commandParameterAst">A CommandParameterAst object in the script's AST</param>
 /// <returns>A descion to stop searching if the right symbol was found, 
 /// or a decision to continue if it wasn't found</returns>
 public override AstVisitAction VisitCommandParameter(CommandParameterAst commandParameterAst)
 {
     if (this.IsPositionInExtent(commandParameterAst.Extent))
     {
         this.FoundSymbolReference =
             new SymbolReference(
                 SymbolType.Parameter,
                 commandParameterAst.Extent);
         return AstVisitAction.StopVisit;
     }
     return AstVisitAction.Continue;
 }
Beispiel #8
0
 internal FakePair(CommandParameterAst parameterAst)
 {
     if (parameterAst == null)
     {
         throw PSTraceSource.NewArgumentNullException("parameterAst");
     }
     base.Parameter = parameterAst;
     base.ParameterArgumentType = AstParameterArgumentType.Fake;
     base.ParameterSpecified = true;
     base.ArgumentSpecified = true;
     base.ParameterName = parameterAst.ParameterName;
     base.ParameterText = parameterAst.ParameterName;
     base.ArgumentType = typeof(object);
 }
Beispiel #9
0
 internal FakePair(CommandParameterAst parameterAst)
 {
     if (parameterAst == null)
     {
         throw PSTraceSource.NewArgumentNullException("parameterAst");
     }
     base.Parameter             = parameterAst;
     base.ParameterArgumentType = AstParameterArgumentType.Fake;
     base.ParameterSpecified    = true;
     base.ArgumentSpecified     = true;
     base.ParameterName         = parameterAst.ParameterName;
     base.ParameterText         = parameterAst.ParameterName;
     base.ArgumentType          = typeof(object);
 }
Beispiel #10
0
 internal AstPair(CommandParameterAst parameterAst)
 {
     if ((parameterAst == null) || (parameterAst.Argument == null))
     {
         throw PSTraceSource.NewArgumentException("parameterAst");
     }
     base.Parameter = parameterAst;
     base.ParameterArgumentType = AstParameterArgumentType.AstPair;
     base.ParameterSpecified = true;
     base.ArgumentSpecified = true;
     base.ParameterName = parameterAst.ParameterName;
     base.ParameterText = "-" + base.ParameterName + ":";
     base.ArgumentType = parameterAst.Argument.StaticType;
     this._parameterContainsArgument = true;
     this._argument = parameterAst.Argument;
 }
Beispiel #11
0
 internal AstPair(CommandParameterAst parameterAst)
 {
     if ((parameterAst == null) || (parameterAst.Argument == null))
     {
         throw PSTraceSource.NewArgumentException("parameterAst");
     }
     base.Parameter                  = parameterAst;
     base.ParameterArgumentType      = AstParameterArgumentType.AstPair;
     base.ParameterSpecified         = true;
     base.ArgumentSpecified          = true;
     base.ParameterName              = parameterAst.ParameterName;
     base.ParameterText              = "-" + base.ParameterName + ":";
     base.ArgumentType               = parameterAst.Argument.StaticType;
     this._parameterContainsArgument = true;
     this._argument                  = parameterAst.Argument;
 }
 private void AddParameter(CommandParameterAst commandParameterAst)
 {
     string str;
     object expressionValue;
     if (commandParameterAst.Argument != null)
     {
         ExpressionAst argument = commandParameterAst.Argument;
         IScriptExtent errorPosition = commandParameterAst.ErrorPosition;
         str = ((errorPosition.EndLineNumber != argument.Extent.StartLineNumber) || (errorPosition.EndColumnNumber != argument.Extent.StartColumnNumber)) ? ": " : ":";
         expressionValue = this.GetExpressionValue(commandParameterAst.Argument);
     }
     else
     {
         str = "";
         expressionValue = null;
     }
     this._powershell.AddParameter(string.Format(CultureInfo.InvariantCulture, "-{0}{1}", new object[] { commandParameterAst.ParameterName, str }), expressionValue);
 }
Beispiel #13
0
 internal AstPair(CommandParameterAst parameterAst, ExpressionAst argumentAst)
 {
     if ((parameterAst != null) && (parameterAst.Argument != null))
     {
         throw PSTraceSource.NewArgumentException("parameterAst");
     }
     if ((parameterAst == null) && (argumentAst == null))
     {
         throw PSTraceSource.NewArgumentNullException("argumentAst");
     }
     base.Parameter                  = parameterAst;
     base.ParameterArgumentType      = AstParameterArgumentType.AstPair;
     base.ParameterSpecified         = parameterAst != null;
     base.ArgumentSpecified          = argumentAst != null;
     base.ParameterName              = (parameterAst != null) ? parameterAst.ParameterName : null;
     base.ParameterText              = (parameterAst != null) ? parameterAst.ParameterName : null;
     base.ArgumentType               = (argumentAst != null) ? argumentAst.StaticType : null;
     this._parameterContainsArgument = false;
     this._argument                  = argumentAst;
 }
Beispiel #14
0
 internal AstPair(CommandParameterAst parameterAst, ExpressionAst argumentAst)
 {
     if ((parameterAst != null) && (parameterAst.Argument != null))
     {
         throw PSTraceSource.NewArgumentException("parameterAst");
     }
     if ((parameterAst == null) && (argumentAst == null))
     {
         throw PSTraceSource.NewArgumentNullException("argumentAst");
     }
     base.Parameter = parameterAst;
     base.ParameterArgumentType = AstParameterArgumentType.AstPair;
     base.ParameterSpecified = parameterAst != null;
     base.ArgumentSpecified = argumentAst != null;
     base.ParameterName = (parameterAst != null) ? parameterAst.ParameterName : null;
     base.ParameterText = (parameterAst != null) ? parameterAst.ParameterName : null;
     base.ArgumentType = (argumentAst != null) ? argumentAst.StaticType : null;
     this._parameterContainsArgument = false;
     this._argument = argumentAst;
 }
Beispiel #15
0
 internal AstPair(CommandParameterAst parameterAst, CommandParameterAst argumentAst)
 {
     if ((parameterAst != null) && (parameterAst.Argument != null))
     {
         throw PSTraceSource.NewArgumentException("parameterAst");
     }
     if ((parameterAst == null) || (argumentAst == null))
     {
         throw PSTraceSource.NewArgumentNullException("argumentAst");
     }
     base.Parameter = parameterAst;
     base.ParameterArgumentType = AstParameterArgumentType.AstPair;
     base.ParameterSpecified = true;
     base.ArgumentSpecified = true;
     base.ParameterName = parameterAst.ParameterName;
     base.ParameterText = parameterAst.ParameterName;
     base.ArgumentType = typeof(string);
     this._parameterContainsArgument = false;
     this._argument = argumentAst;
     this._argumentIsCommandParameterAst = true;
 }
Beispiel #16
0
 internal AstPair(CommandParameterAst parameterAst, CommandParameterAst argumentAst)
 {
     if ((parameterAst != null) && (parameterAst.Argument != null))
     {
         throw PSTraceSource.NewArgumentException("parameterAst");
     }
     if ((parameterAst == null) || (argumentAst == null))
     {
         throw PSTraceSource.NewArgumentNullException("argumentAst");
     }
     base.Parameter                      = parameterAst;
     base.ParameterArgumentType          = AstParameterArgumentType.AstPair;
     base.ParameterSpecified             = true;
     base.ArgumentSpecified              = true;
     base.ParameterName                  = parameterAst.ParameterName;
     base.ParameterText                  = parameterAst.ParameterName;
     base.ArgumentType                   = typeof(string);
     this._parameterContainsArgument     = false;
     this._argument                      = argumentAst;
     this._argumentIsCommandParameterAst = true;
 }
 public override AstVisitAction VisitCommandParameter(CommandParameterAst commandParameterAst)
 {
     return(AstVisitAction.Continue);
 }
Beispiel #18
0
 public object VisitCommandParameter(CommandParameterAst commandParameterAst)
 {
     return(AutomationNull.Value);
 }
Beispiel #19
0
 /// <summary/>
 public virtual AstVisitAction VisitCommandParameter(CommandParameterAst commandParameterAst)
 {
     return(AstVisitAction.Continue);
 }
Beispiel #20
0
 public object VisitCommandParameter(CommandParameterAst commandParameterAst)
 {
     throw PSTraceSource.NewArgumentException("ast");
 }
Beispiel #21
0
 public override AstVisitAction VisitCommandParameter(CommandParameterAst ast)
 {
     return(Check(ast));
 }
 public override AstVisitAction VisitCommandParameter(CommandParameterAst commandParameterAst)
 {
     return AstVisitAction.Continue;
 }
        internal SwitchPair(CommandParameterAst parameterAst)
        {
            if (parameterAst == null)
                throw PSTraceSource.NewArgumentNullException("parameterAst");

            Parameter = parameterAst;
            ParameterArgumentType = AstParameterArgumentType.Switch;
            ParameterSpecified = true;
            ArgumentSpecified = true;
            ParameterName = parameterAst.ParameterName;
            ParameterText = parameterAst.ParameterName;
            ArgumentType = typeof(bool);
        }
 /// <summary/>
 public virtual object VisitCommandParameter(CommandParameterAst commandParameterAst)
 {
     return _decorated.VisitCommandParameter(commandParameterAst);
 }
        /// <summary>
        /// Visit command parameter
        /// </summary>
        /// <param name="commandParameterAst"></param>
        /// <returns></returns>
        public object VisitCommandParameter(CommandParameterAst commandParameterAst)
        {
            if (commandParameterAst == null) return null;

            if (commandParameterAst.Argument != null)
            {
                commandParameterAst.Argument.Visit(this.Decorator);
            }
            return null;
        }
Beispiel #26
0
 public object VisitCommandParameter(CommandParameterAst commandParameterAst)
 {
     if (commandParameterAst.Argument != null)
     {
         commandParameterAst.Argument.Accept(this);
     }
     return null;
 }
Beispiel #27
0
 private static CommandParameterInternal GetCommandParameter(CommandParameterAst commandParameterAst, System.Management.Automation.ExecutionContext context)
 {
     ExpressionAst argument = commandParameterAst.Argument;
     IScriptExtent errorPosition = commandParameterAst.ErrorPosition;
     if (argument == null)
     {
         return CommandParameterInternal.CreateParameter(errorPosition, commandParameterAst.ParameterName, errorPosition.Text);
     }
     object obj2 = Compiler.GetExpressionValue(argument, context, (IList)null);
     bool spaceAfterParameter = (errorPosition.EndLineNumber != argument.Extent.StartLineNumber) || (errorPosition.EndColumnNumber != argument.Extent.StartColumnNumber);
     return CommandParameterInternal.CreateParameterWithArgument(errorPosition, commandParameterAst.ParameterName, errorPosition.Text, argument.Extent, obj2, spaceAfterParameter);
 }
        /// <summary>
        /// Get the parameter binding metadata
        /// </summary>
        /// <param name="command"></param>
        /// <param name="pipeArgumentType">Indicate the type of the piped-in argument</param>
        /// <param name="paramAstAtCursor">The CommandParameterAst the cursor is pointing at</param>
        /// <param name="bindingType">Indicates whether pseudo binding is for argument binding, argument completion, or parameter completion.</param>
        /// <returns>PseudoBindingInfo</returns>
        internal PseudoBindingInfo DoPseudoParameterBinding(CommandAst command, Type pipeArgumentType, CommandParameterAst paramAstAtCursor, BindingType bindingType)
        {
            if (command == null)
            {
                throw PSTraceSource.NewArgumentNullException("command");
            }

            // initialize/reset the private members
            InitializeMembers();
            _commandAst = command;
            _commandElements = command.CommandElements;
            Collection<AstParameterArgumentPair> unboundArguments = new Collection<AstParameterArgumentPair>();

            // analyze the command and reparse the arguments
            {
                ExecutionContext executionContext = LocalPipeline.GetExecutionContextFromTLS();
                if (executionContext != null)
                {
                    // WinBlue: 324316. This limits the interaction of pseudoparameterbinder with the actual host.
                    SetTemporaryDefaultHost(executionContext);

                    PSLanguageMode? previousLanguageMode = null;
                    try
                    {
                        // Tab expansion is called from a trusted function - we should apply ConstrainedLanguage if necessary.
                        if (ExecutionContext.HasEverUsedConstrainedLanguage)
                        {
                            previousLanguageMode = executionContext.LanguageMode;
                            executionContext.LanguageMode = PSLanguageMode.ConstrainedLanguage;
                        }
                        _bindingEffective = PrepareCommandElements(executionContext);
                    }
                    finally
                    {
                        if (previousLanguageMode.HasValue)
                        {
                            executionContext.LanguageMode = previousLanguageMode.Value;
                        }

                        RestoreHost(executionContext);
                    }
                }
            }

            if (_bindingEffective && (_isPipelineInputExpected || pipeArgumentType != null))
            {
                _pipelineInputType = pipeArgumentType;
            }
            _bindingEffective = ParseParameterArguments(paramAstAtCursor);

            if (_bindingEffective)
            {
                // named binding
                unboundArguments = BindNamedParameters();
                _bindingEffective = _currentParameterSetFlag != 0;

                // positional binding
                unboundArguments = BindPositionalParameter(
                    unboundArguments,
                    _currentParameterSetFlag,
                    _defaultParameterSetFlag,
                    bindingType);

                // VFRA/pipeline binding if the given command is a binary cmdlet or a script cmdlet
                if (!_function)
                {
                    unboundArguments = BindRemainingParameters(unboundArguments);
                    BindPipelineParameters();
                }

                // Update available parameter sets based on bound arguments
                // (x & (x -1 ) == 0) is a bit hack to determine if something is
                // exactly a power of two.
                bool parameterSetSpecified = (_currentParameterSetFlag != 0) &&
                    (_currentParameterSetFlag != UInt32.MaxValue);
                bool onlyOneRemainingParameterSet = (_currentParameterSetFlag != 0) &&
                    (_currentParameterSetFlag & (_currentParameterSetFlag - 1)) == 0;
                if ((bindingType != BindingType.ParameterCompletion) && parameterSetSpecified && (!onlyOneRemainingParameterSet))
                {
                    CmdletParameterBinderController.ResolveParameterSetAmbiguityBasedOnMandatoryParameters(
                        _boundParameters,
                        _unboundParameters,
                        null,
                        ref _currentParameterSetFlag,
                        null);
                }
            }

            // Binding failed
            if (!_bindingEffective)
            {
                // The command is not a cmdlet, not a script cmdlet, and not a function
                if (_bindableParameters == null)
                    return null;

                // get all bindable parameters
                _unboundParameters.Clear();
                _unboundParameters.AddRange(_bindableParameters.BindableParameters.Values);

                return new PseudoBindingInfo(
                    _commandInfo,
                    _defaultParameterSetFlag,
                    _arguments,
                    _unboundParameters);
            }

            return new PseudoBindingInfo(
                _commandInfo,
                _currentParameterSetFlag,
                _defaultParameterSetFlag,
                _boundParameters,
                _unboundParameters,
                _boundArguments,
                _boundPositionalParameter,
                _arguments,
                _parametersNotFound,
                _ambiguousParameters,
                _bindingExceptions,
                _duplicateParameters,
                unboundArguments
                );
        }
 private static void ResetCurrentParameter(ref CommandParameterAst currentParameter, ref ParameterBindingResult bindingResult)
 {
     currentParameter = null;
     bindingResult = new ParameterBindingResult();
 }
 private void AddBoundParameter(CommandParameterAst parameter, string parameterName, ParameterBindingResult bindingResult)
 {
     if (BoundParameters.ContainsKey(parameterName))
     {
         AddDuplicateParameterBindingException(parameter);
     }
     else
     {
         BoundParameters.Add(parameterName, bindingResult);
     }
 }
        private void AddParameter(CommandParameterAst commandParameterAst, bool isTrustedInput)
        {
            string nameSuffix;
            object argument;
            if (commandParameterAst.Argument != null)
            {
                var arg = commandParameterAst.Argument;
                var errorPos = commandParameterAst.ErrorPosition;
                bool spaceAfterParameter = (errorPos.EndLineNumber != arg.Extent.StartLineNumber ||
                                            errorPos.EndColumnNumber != arg.Extent.StartColumnNumber);
                nameSuffix = spaceAfterParameter ? ": " : ":";

                argument = GetExpressionValue(commandParameterAst.Argument, isTrustedInput);
            }
            else
            {
                nameSuffix = "";
                argument = null;
            }

            // first character in parameter name must be a dash
            _powershell.AddParameter(
                string.Format(CultureInfo.InvariantCulture, "-{0}{1}", commandParameterAst.ParameterName, nameSuffix),
                argument);
        }
Beispiel #32
0
 public virtual AstVisitAction VisitCommandParameter(CommandParameterAst commandParameterAst)
 {
     return AstVisitAction.Continue;
 }
        internal AstPair(CommandParameterAst parameterAst, CommandElementAst argumentAst)
        {
            if (parameterAst != null && parameterAst.Argument != null)
                throw PSTraceSource.NewArgumentException("parameterAst");

            if (parameterAst == null || argumentAst == null)
                throw PSTraceSource.NewArgumentNullException("argumentAst");

            Parameter = parameterAst;
            ParameterArgumentType = AstParameterArgumentType.AstPair;
            ParameterSpecified = true;
            ArgumentSpecified = true;
            ParameterName = parameterAst.ParameterName;
            ParameterText = parameterAst.ParameterName;
            ArgumentType = typeof(string);

            ParameterContainsArgument = false;
            Argument = argumentAst;
            ArgumentIsCommandParameterAst = true;
        }
Beispiel #34
0
 /// <summary/>
 public virtual object VisitCommandParameter(CommandParameterAst commandParameterAst)
 {
     return(null);
 }
Beispiel #35
0
 internal PseudoBindingInfo DoPseudoParameterBinding(CommandAst command, Type pipeArgumentType, CommandParameterAst paramAstAtCursor, bool isForArgumentCompletion)
 {
     if (command == null)
     {
         throw PSTraceSource.NewArgumentNullException("command");
     }
     this.InitializeMembers();
     this._commandAst       = command;
     this._commandElements  = command.CommandElements;
     this._bindingEffective = this.PrepareCommandElements(LocalPipeline.GetExecutionContextFromTLS());
     if (this._bindingEffective && (this._isPipelineInputExpected || (pipeArgumentType != null)))
     {
         this._pipelineInputType = pipeArgumentType;
     }
     this._bindingEffective = this.ParseParameterArguments(paramAstAtCursor);
     if (this._bindingEffective)
     {
         Collection <AstParameterArgumentPair> unboundArguments = this.BindNamedParameters();
         this._bindingEffective = this._currentParameterSetFlag != 0;
         unboundArguments       = this.BindPositionalParameter(unboundArguments, this._currentParameterSetFlag, this._defaultParameterSetFlag, isForArgumentCompletion);
         if (!this._function)
         {
             this.BindRemainingParameters(unboundArguments);
             this.BindPipelineParameters();
         }
     }
     if (this._bindingEffective)
     {
         return(new PseudoBindingInfo(this._commandInfo, this._currentParameterSetFlag, this._defaultParameterSetFlag, this._boundParameters, this._unboundParameters, this._boundArguments, this._boundPositionalParameter, this._arguments, this._parametersNotFound, this._ambiguousParameters, this._duplicateParameters));
     }
     if (this._bindableParameters == null)
     {
         return(null);
     }
     this._unboundParameters.Clear();
     this._unboundParameters.AddRange(this._bindableParameters.BindableParameters.Values);
     return(new PseudoBindingInfo(this._commandInfo, this._defaultParameterSetFlag, this._arguments, this._unboundParameters));
 }
Beispiel #36
0
 public object VisitCommandParameter(CommandParameterAst commandParameterAst)
 {
     return false;
 }
Beispiel #37
0
        private bool ParseParameterArguments(CommandParameterAst paramAstAtCursor)
        {
            if (!this._bindingEffective)
            {
                return(this._bindingEffective);
            }
            Collection <AstParameterArgumentPair> collection = new Collection <AstParameterArgumentPair>();

            for (int i = 0; i < this._arguments.Count; i++)
            {
                AstParameterArgumentPair item = this._arguments[i];
                if (!item.ParameterSpecified || item.ArgumentSpecified)
                {
                    collection.Add(item);
                }
                else
                {
                    string parameterName = item.ParameterName;
                    MergedCompiledCommandParameter parameter = null;
                    try
                    {
                        bool tryExactMatching = item.Parameter != paramAstAtCursor;
                        parameter = this._bindableParameters.GetMatchingParameter(parameterName, false, tryExactMatching, null);
                    }
                    catch (ParameterBindingException)
                    {
                        this._ambiguousParameters.Add(item.Parameter);
                        goto Label_01F1;
                    }
                    if (parameter == null)
                    {
                        if (i < (this._arguments.Count - 1))
                        {
                            AstParameterArgumentPair pair2 = this._arguments[i + 1];
                            if (!pair2.ParameterSpecified && pair2.ArgumentSpecified)
                            {
                                this._arguments = null;
                                return(false);
                            }
                        }
                        this._parametersNotFound.Add(item.Parameter);
                    }
                    else if (parameter.Parameter.Type == typeof(SwitchParameter))
                    {
                        SwitchPair pair3 = new SwitchPair(item.Parameter);
                        collection.Add(pair3);
                    }
                    else if (i < (this._arguments.Count - 1))
                    {
                        AstParameterArgumentPair pair4 = this._arguments[i + 1];
                        if (pair4.ParameterSpecified)
                        {
                            try
                            {
                                if (this._bindableParameters.GetMatchingParameter(pair4.ParameterName, false, true, null) == null)
                                {
                                    AstPair pair5 = new AstPair(item.Parameter, pair4.Parameter);
                                    collection.Add(pair5);
                                    i++;
                                }
                                else
                                {
                                    FakePair pair6 = new FakePair(item.Parameter);
                                    collection.Add(pair6);
                                }
                                goto Label_01F1;
                            }
                            catch (ParameterBindingException)
                            {
                                FakePair pair7 = new FakePair(item.Parameter);
                                collection.Add(pair7);
                                goto Label_01F1;
                            }
                        }
                        AstPair pair8 = pair4 as AstPair;
                        AstPair pair9 = new AstPair(item.Parameter, (ExpressionAst)pair8.Argument);
                        collection.Add(pair9);
                        i++;
                    }
                    else
                    {
                        FakePair pair10 = new FakePair(item.Parameter);
                        collection.Add(pair10);
                    }
                    Label_01F1 :;
                }
            }
            this._arguments = collection;
            return(true);
        }
 /// <summary>
 /// Decides if the current function defintion is a reference of the symbol being searched for.
 /// A reference of the symbol will be a of type SymbolType.Parameter and have the same name as the symbol 
 /// </summary>
 /// <param name="commandParameterAst">A commandParameterAst in the script's AST</param>
 /// <returns>A visit action that continues the search for references</returns>
 public override AstVisitAction VisitCommandParameter(CommandParameterAst commandParameterAst)
 {
     if (symbolRef.SymbolType.Equals(SymbolType.Parameter) &&
         commandParameterAst.Extent.Text.Equals(symbolRef.SymbolName, StringComparison.InvariantCultureIgnoreCase))
     {
         this.FoundReferences.Add(new SymbolReference(
                                  SymbolType.Parameter,
                                  commandParameterAst.Extent));
     }
     return AstVisitAction.Continue;
 }
Beispiel #39
0
        private bool PrepareCommandElements(ExecutionContext context)
        {
            int  num                   = 0;
            bool dotSource             = this._commandAst.InvocationOperator == TokenKind.Dot;
            CommandProcessorBase base2 = null;
            string resolvedCommandName = null;
            bool   flag2               = false;

            try
            {
                base2 = this.PrepareFromAst(context, out resolvedCommandName) ?? context.CreateCommand(resolvedCommandName, dotSource);
            }
            catch (RuntimeException exception)
            {
                CommandProcessorBase.CheckForSevereException(exception);
                if ((this._commandAst.IsInWorkflow() && (resolvedCommandName != null)) && CompletionCompleters.PseudoWorkflowCommands.Contains <string>(resolvedCommandName, StringComparer.OrdinalIgnoreCase))
                {
                    flag2 = true;
                }
                else
                {
                    return(false);
                }
            }
            CommandProcessor           commandProcessor = base2 as CommandProcessor;
            ScriptCommandProcessorBase base3            = base2 as ScriptCommandProcessorBase;
            bool          flag3 = (commandProcessor != null) && commandProcessor.CommandInfo.ImplementsDynamicParameters;
            List <object> list  = flag3 ? new List <object>(this._commandElements.Count) : null;

            if (((commandProcessor != null) || (base3 != null)) || flag2)
            {
                num++;
                while (num < this._commandElements.Count)
                {
                    CommandParameterAst parameterAst = this._commandElements[num] as CommandParameterAst;
                    if (parameterAst != null)
                    {
                        if (list != null)
                        {
                            list.Add(parameterAst.Extent.Text);
                        }
                        AstPair item = (parameterAst.Argument != null) ? new AstPair(parameterAst, parameterAst.Argument) : new AstPair(parameterAst);
                        this._arguments.Add(item);
                    }
                    else
                    {
                        StringConstantExpressionAst ast2 = this._commandElements[num] as StringConstantExpressionAst;
                        if ((ast2 == null) || !ast2.Value.Trim().Equals("-", StringComparison.OrdinalIgnoreCase))
                        {
                            ExpressionAst argumentAst = this._commandElements[num] as ExpressionAst;
                            if (argumentAst != null)
                            {
                                if (list != null)
                                {
                                    list.Add(argumentAst.Extent.Text);
                                }
                                this._arguments.Add(new AstPair(null, argumentAst));
                            }
                        }
                    }
                    num++;
                }
            }
            if (commandProcessor != null)
            {
                this._function = false;
                if (flag3)
                {
                    ParameterBinderController.AddArgumentsToCommandProcessor(commandProcessor, list.ToArray());
                    bool flag4 = false;
                    bool flag5 = false;
                    do
                    {
                        CommandProcessorBase currentCommandProcessor = context.CurrentCommandProcessor;
                        try
                        {
                            context.CurrentCommandProcessor = commandProcessor;
                            commandProcessor.SetCurrentScopeToExecutionScope();
                            if (!flag4)
                            {
                                commandProcessor.CmdletParameterBinderController.BindCommandLineParametersNoValidation(commandProcessor.arguments);
                            }
                            else
                            {
                                flag5 = true;
                                commandProcessor.CmdletParameterBinderController.ClearUnboundArguments();
                                commandProcessor.CmdletParameterBinderController.BindCommandLineParametersNoValidation(new Collection <CommandParameterInternal>());
                            }
                        }
                        catch (ParameterBindingException exception2)
                        {
                            if ((exception2.ErrorId == "MissingArgument") || (exception2.ErrorId == "AmbiguousParameter"))
                            {
                                flag4 = true;
                            }
                        }
                        catch (Exception exception3)
                        {
                            CommandProcessorBase.CheckForSevereException(exception3);
                        }
                        finally
                        {
                            context.CurrentCommandProcessor = currentCommandProcessor;
                            commandProcessor.RestorePreviousScope();
                        }
                    }while (flag4 && !flag5);
                }
                this._commandInfo             = commandProcessor.CommandInfo;
                this._commandName             = commandProcessor.CommandInfo.Name;
                this._bindableParameters      = commandProcessor.CmdletParameterBinderController.BindableParameters;
                this._defaultParameterSetFlag = commandProcessor.CommandInfo.CommandMetadata.DefaultParameterSetFlag;
            }
            else if (base3 != null)
            {
                this._function                = true;
                this._commandInfo             = base3.CommandInfo;
                this._commandName             = base3.CommandInfo.Name;
                this._bindableParameters      = base3.ScriptParameterBinderController.BindableParameters;
                this._defaultParameterSetFlag = 0;
            }
            else if (!flag2)
            {
                return(false);
            }
            if (this._commandAst.IsInWorkflow())
            {
                Type type = Type.GetType("Microsoft.PowerShell.Workflow.AstToWorkflowConverter, Microsoft.PowerShell.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
                if (type != null)
                {
                    Dictionary <string, Type> dictionary = (Dictionary <string, Type>)type.GetMethod("GetActivityParameters").Invoke(null, new object[] { this._commandAst });
                    if (dictionary != null)
                    {
                        bool flag6 = dictionary.ContainsKey("PSComputerName") && !dictionary.ContainsKey("ComputerName");
                        List <MergedCompiledCommandParameter> source = new List <MergedCompiledCommandParameter>();
                        Collection <Attribute> attributes            = new Collection <Attribute> {
                            new ParameterAttribute()
                        };
                        foreach (KeyValuePair <string, Type> pair2 in dictionary)
                        {
                            if (flag2 || !this._bindableParameters.BindableParameters.ContainsKey(pair2.Key))
                            {
                                Type actualActivityParameterType = GetActualActivityParameterType(pair2.Value);
                                RuntimeDefinedParameter  runtimeDefinedParameter = new RuntimeDefinedParameter(pair2.Key, actualActivityParameterType, attributes);
                                CompiledCommandParameter parameter = new CompiledCommandParameter(runtimeDefinedParameter, false)
                                {
                                    IsInAllSets = true
                                };
                                MergedCompiledCommandParameter parameter3 = new MergedCompiledCommandParameter(parameter, ParameterBinderAssociation.DeclaredFormalParameters);
                                source.Add(parameter3);
                            }
                        }
                        if (source.Any <MergedCompiledCommandParameter>())
                        {
                            MergedCommandParameterMetadata metadata = new MergedCommandParameterMetadata();
                            if (!flag2)
                            {
                                metadata.ReplaceMetadata(this._bindableParameters);
                            }
                            foreach (MergedCompiledCommandParameter parameter5 in source)
                            {
                                metadata.BindableParameters.Add(parameter5.Parameter.Name, parameter5);
                            }
                            this._bindableParameters = metadata;
                        }
                        foreach (string str2 in ignoredWorkflowParameters)
                        {
                            if (this._bindableParameters.BindableParameters.ContainsKey(str2))
                            {
                                this._bindableParameters.BindableParameters.Remove(str2);
                            }
                        }
                        if (this._bindableParameters.BindableParameters.ContainsKey("ComputerName") && flag6)
                        {
                            this._bindableParameters.BindableParameters.Remove("ComputerName");
                            string key = (from aliasPair in this._bindableParameters.AliasedParameters
                                          where string.Equals("ComputerName", aliasPair.Value.Parameter.Name)
                                          select aliasPair.Key).FirstOrDefault <string>();
                            this._bindableParameters.AliasedParameters.Remove(key);
                        }
                    }
                }
            }
            this._unboundParameters.AddRange(this._bindableParameters.BindableParameters.Values);
            CommandBaseAst ast4   = null;
            PipelineAst    parent = this._commandAst.Parent as PipelineAst;

            if (parent.PipelineElements.Count > 1)
            {
                foreach (CommandBaseAst ast6 in parent.PipelineElements)
                {
                    if (ast6.GetHashCode() == this._commandAst.GetHashCode())
                    {
                        this._isPipelineInputExpected = ast4 != null;
                        if (this._isPipelineInputExpected)
                        {
                            this._pipelineInputType = typeof(object);
                        }
                        break;
                    }
                    ast4 = ast6;
                }
            }
            return(true);
        }
Beispiel #40
0
 public object VisitCommandParameter(CommandParameterAst commandParameterAst)
 {
     return(false);
 }
        internal AstPair(CommandParameterAst parameterAst, ExpressionAst argumentAst)
        {
            if (parameterAst != null && parameterAst.Argument != null)
                throw PSTraceSource.NewArgumentException("parameterAst");

            if (parameterAst == null && argumentAst == null)
                throw PSTraceSource.NewArgumentNullException("argumentAst");

            Parameter = parameterAst;
            ParameterArgumentType = AstParameterArgumentType.AstPair;
            ParameterSpecified = parameterAst != null;
            ArgumentSpecified = argumentAst != null;
            ParameterName = parameterAst != null ? parameterAst.ParameterName : null;
            ParameterText = parameterAst != null ? parameterAst.ParameterName : null;
            ArgumentType = argumentAst != null ? argumentAst.StaticType : null;

            ParameterContainsArgument = false;
            Argument = argumentAst;
        }
Beispiel #42
0
 private bool ParseParameterArguments(CommandParameterAst paramAstAtCursor)
 {
     if (!this._bindingEffective)
     {
         return this._bindingEffective;
     }
     Collection<AstParameterArgumentPair> collection = new Collection<AstParameterArgumentPair>();
     for (int i = 0; i < this._arguments.Count; i++)
     {
         AstParameterArgumentPair item = this._arguments[i];
         if (!item.ParameterSpecified || item.ArgumentSpecified)
         {
             collection.Add(item);
         }
         else
         {
             string parameterName = item.ParameterName;
             MergedCompiledCommandParameter parameter = null;
             try
             {
                 bool tryExactMatching = item.Parameter != paramAstAtCursor;
                 parameter = this._bindableParameters.GetMatchingParameter(parameterName, false, tryExactMatching, null);
             }
             catch (ParameterBindingException)
             {
                 this._ambiguousParameters.Add(item.Parameter);
                 goto Label_01F1;
             }
             if (parameter == null)
             {
                 if (i < (this._arguments.Count - 1))
                 {
                     AstParameterArgumentPair pair2 = this._arguments[i + 1];
                     if (!pair2.ParameterSpecified && pair2.ArgumentSpecified)
                     {
                         this._arguments = null;
                         return false;
                     }
                 }
                 this._parametersNotFound.Add(item.Parameter);
             }
             else if (parameter.Parameter.Type == typeof(SwitchParameter))
             {
                 SwitchPair pair3 = new SwitchPair(item.Parameter);
                 collection.Add(pair3);
             }
             else if (i < (this._arguments.Count - 1))
             {
                 AstParameterArgumentPair pair4 = this._arguments[i + 1];
                 if (pair4.ParameterSpecified)
                 {
                     try
                     {
                         if (this._bindableParameters.GetMatchingParameter(pair4.ParameterName, false, true, null) == null)
                         {
                             AstPair pair5 = new AstPair(item.Parameter, pair4.Parameter);
                             collection.Add(pair5);
                             i++;
                         }
                         else
                         {
                             FakePair pair6 = new FakePair(item.Parameter);
                             collection.Add(pair6);
                         }
                         goto Label_01F1;
                     }
                     catch (ParameterBindingException)
                     {
                         FakePair pair7 = new FakePair(item.Parameter);
                         collection.Add(pair7);
                         goto Label_01F1;
                     }
                 }
                 AstPair pair8 = pair4 as AstPair;
                 AstPair pair9 = new AstPair(item.Parameter, (ExpressionAst) pair8.Argument);
                 collection.Add(pair9);
                 i++;
             }
             else
             {
                 FakePair pair10 = new FakePair(item.Parameter);
                 collection.Add(pair10);
             }
         Label_01F1:;
         }
     }
     this._arguments = collection;
     return true;
 }
Beispiel #43
0
 public override AstVisitAction VisitCommandParameter(CommandParameterAst commandParameterAst)
 {
     throw new NotImplementedException(); //VisitCommandParameter(commandParameterAst);
 }
        internal AstPair(CommandParameterAst parameterAst)
        {
            if (parameterAst == null || parameterAst.Argument == null)
                throw PSTraceSource.NewArgumentException("parameterAst");

            Parameter = parameterAst;
            ParameterArgumentType = AstParameterArgumentType.AstPair;
            ParameterSpecified = true;
            ArgumentSpecified = true;
            ParameterName = parameterAst.ParameterName;
            ParameterText = "-" + ParameterName + ":";
            ArgumentType = parameterAst.Argument.StaticType;

            ParameterContainsArgument = true;
            Argument = parameterAst.Argument;
        }
        /// <summary>
        /// Parse the arguments to process switch parameters and parameters without a value
        /// specified. We always eat the error (such as parameter without value) and continue
        /// to do the binding.
        /// </summary>
        /// 
        /// <param name="paramAstAtCursor">
        /// For parameter completion, if the cursor is pointing at a CommandParameterAst, we
        /// should not try exact matching for that CommandParameterAst. This is to handle the
        /// following case:
        ///     Add-Computer -domain(tab)
        /// Add-Computer has an alias "Domain" that can exactly match this partial input, but
        /// since the user is typing 'tab', the partial input 'domain' should not be considered
        /// as an exact match. In this case, we don't try exact matching when calling 
        /// GetMatchingParameter(..) so as to preserve other possibilities.
        /// </param>
        private bool ParseParameterArguments(CommandParameterAst paramAstAtCursor)
        {
            if (!_bindingEffective)
                return _bindingEffective;

            var result = new Collection<AstParameterArgumentPair>();
            for (int index = 0; index < _arguments.Count; index++)
            {
                AstParameterArgumentPair argument = _arguments[index];
                if (!argument.ParameterSpecified || argument.ArgumentSpecified)
                {
                    // Add the positional/named arguments back
                    result.Add(argument);
                    continue;
                }

                Diagnostics.Assert(argument.ParameterSpecified && !argument.ArgumentSpecified,
                    "At this point, the parameters should have no arguments");

                //Now check the parameter name with the bindable parameters
                string parameterName = argument.ParameterName;
                MergedCompiledCommandParameter matchingParameter = null;

                try
                {
                    bool tryExactMatching = argument.Parameter != paramAstAtCursor;
                    matchingParameter = _bindableParameters.GetMatchingParameter(parameterName, false, tryExactMatching, null);
                }
                catch (ParameterBindingException e)
                {
                    // The parameterName is resolved to multiple parameters. The most possible scenario for this
                    // would be the user typping tab to complete a parameter. In this case, we can ignore this 
                    // parameter safely.

                    // If the next item is a pure argument, we skip it so that it doesn't get bound
                    // positionally.
                    if (index < _arguments.Count - 1)
                    {
                        AstParameterArgumentPair nextArg = _arguments[index + 1];
                        if (!nextArg.ParameterSpecified && nextArg.ArgumentSpecified)
                        {
                            index++;
                        }
                    }

                    _ambiguousParameters.Add(argument.Parameter);
                    _bindingExceptions[argument.Parameter] = e;

                    continue;
                }

                if (matchingParameter == null)
                {
                    // The parameter cannot be found. The reason could be:
                    // 1. It's a bynamic parameter, and we cannot retrieve the ParameterMetadata for it
                    //    at this point, since it's pseudo binding.
                    // 2. The spelling of this parameter is wrong.
                    // We can simply ignore this parameter, but the issue is what to do with the argument
                    // following this parameter (if there is an argument following it). There are two cases:
                    // 1. This parameter is supposed to be a switch parameter. Then the argument following it
                    //    should NOT be ignored.
                    // 2. This parameter is supposed to take an argument. Then the following argument should
                    //    also be ignored
                    // We check the next item. If it's a pure argument, we give up the binding, because we don't
                    // know how to deal with it (ignore it? keep it?), and it will affect the accuracy of our
                    // parameter set resolution.
                    if (index < _arguments.Count - 1)
                    {
                        AstParameterArgumentPair nextArg = _arguments[index + 1];

                        // If the next item is a pure argument, we give up the pseudo binding.
                        if (!nextArg.ParameterSpecified && nextArg.ArgumentSpecified)
                        {
                            // Testing paramsAstAtCursor ensures we only give up during tab completion,
                            // otherwise we know this is a missing parameter.
                            if (paramAstAtCursor != null)
                            {
                                // Do not use the parsed arguments
                                _arguments = null;
                                return false;
                            }
                            else
                            {
                                // Otherwise, skip the next argument
                                index++;
                                _parametersNotFound.Add(argument.Parameter);
                                continue;
                            }
                        }
                    }

                    // If the next item is not a pure argument, or the current parameter is the last item,
                    // ignore this parameter and carry on with the binding
                    _parametersNotFound.Add(argument.Parameter);
                    continue;
                }

                // Check if it's SwitchParameter
                if (matchingParameter.Parameter.Type == typeof(SwitchParameter))
                {
                    SwitchPair newArg = new SwitchPair(argument.Parameter);
                    result.Add(newArg);
                    continue;
                }

                // It's not a switch parameter, we need to check the next argument
                if (index < _arguments.Count - 1)
                {
                    AstParameterArgumentPair nextArg = _arguments[index + 1];
                    if (nextArg.ParameterSpecified)
                    {
                        try
                        {
                            MergedCompiledCommandParameter nextMatchingParameter =
                                _bindableParameters.GetMatchingParameter(nextArg.ParameterName, false, true, null);
                            // The next parameter doesn't exist. We use it as an argument
                            if (nextMatchingParameter == null)
                            {
                                AstPair newArg = new AstPair(argument.Parameter, nextArg.Parameter);
                                result.Add(newArg);
                                index++;
                            }
                            else
                            {
                                // It's possible the user is typing tab for argument completion.
                                // We set a fake argument for the current parameter in this case.
                                FakePair newArg = new FakePair(argument.Parameter);
                                result.Add(newArg);
                            }
                        }
                        catch (ParameterBindingException)
                        {
                            // The next parameter name is ambiguous. We just set
                            // a fake argument for the current paramter.
                            FakePair newArg = new FakePair(argument.Parameter);
                            result.Add(newArg);
                        }
                    }
                    else
                    {
                        // The next item is a pure argument.
                        AstPair nextArgument = nextArg as AstPair;
                        Diagnostics.Assert(nextArgument != null, "the next item should be a pure argument here");
                        Diagnostics.Assert(nextArgument.ArgumentSpecified && !nextArgument.ArgumentIsCommandParameterAst, "the next item should be a pure argument here");

                        AstPair newArg = new AstPair(argument.Parameter, (ExpressionAst)nextArgument.Argument);
                        result.Add(newArg);
                        index++;
                    }
                }
                else
                {
                    // The current parameter is the last item. Set a fake argument for it
                    FakePair newArg = new FakePair(argument.Parameter);
                    result.Add(newArg);
                }
            }

            _arguments = result;
            return true;
        }
Beispiel #46
0
 /// <summary/>
 public virtual AstVisitAction VisitCommandParameter(CommandParameterAst commandParameterAst) => DefaultVisit(commandParameterAst);
        private void AddDuplicateParameterBindingException(CommandParameterAst duplicateParameter)
        {
            if (duplicateParameter == null)
            {
                return;
            }

            ParameterBindingException bindingException =
                new ParameterBindingException(
                    ErrorCategory.InvalidArgument,
                    null,
                    duplicateParameter.ErrorPosition,
                    duplicateParameter.ParameterName,
                    null,
                    null,
                    ParameterBinderStrings.ParameterAlreadyBound,
                    "ParameterAlreadyBound");
            // if the duplicated Parameter Name appears more than twice, we will ignore as we already have similar bindingException.
            if (!BindingExceptions.ContainsKey(duplicateParameter.ParameterName))
            {
                BindingExceptions.Add(duplicateParameter.ParameterName, new StaticBindingError(duplicateParameter, bindingException));
            }
        }