private IEnumerable <PSTypeName> GetInferredTypeFromScriptBlockParameter(AstParameterArgumentPair argument, CompletionContext context) { AstPair iteratorVariable0 = argument as AstPair; if ((iteratorVariable0 != null) && (iteratorVariable0.Argument is ScriptBlockExpressionAst)) { ScriptBlockExpressionAst iteratorVariable1 = (ScriptBlockExpressionAst)iteratorVariable0.Argument; foreach (PSTypeName iteratorVariable2 in iteratorVariable1.ScriptBlock.GetInferredType(context)) { yield return(iteratorVariable2); } } }
private void BindRemainingParameters(Collection <AstParameterArgumentPair> unboundArguments) { bool flag = false; int num = 0; if (this._bindingEffective && (unboundArguments.Count != 0)) { Collection <ExpressionAst> arguments = new Collection <ExpressionAst>(); foreach (AstParameterArgumentPair pair in unboundArguments) { AstPair pair2 = pair as AstPair; arguments.Add((ExpressionAst)pair2.Argument); } List <MergedCompiledCommandParameter> list = new List <MergedCompiledCommandParameter>(this._unboundParameters); foreach (MergedCompiledCommandParameter parameter in list) { if (((parameter.Parameter.ParameterSetFlags & this._currentParameterSetFlag) != 0) || parameter.Parameter.IsInAllSets) { foreach (ParameterSetSpecificMetadata metadata in parameter.Parameter.GetMatchingParameterSetData(this._currentParameterSetFlag)) { if (metadata.ValueFromRemainingArguments) { num |= parameter.Parameter.ParameterSetFlags; string name = parameter.Parameter.Name; this._unboundParameters.Remove(parameter); if (!this._boundParameters.ContainsKey(name)) { this._boundParameters.Add(name, parameter); } if (!this._boundArguments.ContainsKey(name)) { this._boundArguments.Add(name, new AstArrayPair(name, arguments)); } flag = true; break; } } } } if (flag && (num != 0)) { this._currentParameterSetFlag &= num; } } }
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); }
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); }
internal override IEnumerable <PSTypeName> GetInferredType(CompletionContext context) { PseudoBindingInfo iteratorVariable0 = new PseudoParameterBinder().DoPseudoParameterBinding(this, null, null, false); if (iteratorVariable0.CommandInfo != null) { AstParameterArgumentPair iteratorVariable1; string key = "Path"; if (!iteratorVariable0.BoundArguments.TryGetValue(key, out iteratorVariable1)) { key = "LiteralPath"; iteratorVariable0.BoundArguments.TryGetValue(key, out iteratorVariable1); } CommandInfo commandInfo = iteratorVariable0.CommandInfo; AstPair iteratorVariable4 = iteratorVariable1 as AstPair; if ((iteratorVariable4 != null) && (iteratorVariable4.Argument is StringConstantExpressionAst)) { string str = ((StringConstantExpressionAst)iteratorVariable4.Argument).Value; try { commandInfo = commandInfo.CreateGetCommandCopy(new string[] { "-" + key, str }); } catch (InvalidOperationException) { } } CmdletInfo iteratorVariable5 = commandInfo as CmdletInfo; if (iteratorVariable5 != null) { if (iteratorVariable5.ImplementingType.FullName.Equals("Microsoft.PowerShell.Commands.NewObjectCommand", StringComparison.Ordinal)) { AstParameterArgumentPair iteratorVariable6; if (iteratorVariable0.BoundArguments.TryGetValue("TypeName", out iteratorVariable6)) { AstPair iteratorVariable7 = iteratorVariable6 as AstPair; if ((iteratorVariable7 != null) && (iteratorVariable7.Argument is StringConstantExpressionAst)) { yield return(new PSTypeName(((StringConstantExpressionAst)iteratorVariable7.Argument).Value)); } } goto Label_0579; } if (iteratorVariable5.ImplementingType.Equals(typeof(WhereObjectCommand)) || iteratorVariable5.ImplementingType.FullName.Equals("Microsoft.PowerShell.Commands.SortObjectCommand", StringComparison.Ordinal)) { PipelineAst parent = this.Parent as PipelineAst; if (parent != null) { int iteratorVariable9 = 0; while (iteratorVariable9 < parent.PipelineElements.Count) { if (parent.PipelineElements[iteratorVariable9] == this) { break; } iteratorVariable9++; } if (iteratorVariable9 > 0) { foreach (PSTypeName iteratorVariable10 in parent.PipelineElements[iteratorVariable9 - 1].GetInferredType(context)) { yield return(iteratorVariable10); } } } goto Label_0579; } if (iteratorVariable5.ImplementingType.Equals(typeof(ForEachObjectCommand))) { AstParameterArgumentPair iteratorVariable11; if (iteratorVariable0.BoundArguments.TryGetValue("Begin", out iteratorVariable11)) { foreach (PSTypeName iteratorVariable12 in this.GetInferredTypeFromScriptBlockParameter(iteratorVariable11, context)) { yield return(iteratorVariable12); } } if (iteratorVariable0.BoundArguments.TryGetValue("Process", out iteratorVariable11)) { foreach (PSTypeName iteratorVariable13 in this.GetInferredTypeFromScriptBlockParameter(iteratorVariable11, context)) { yield return(iteratorVariable13); } } if (iteratorVariable0.BoundArguments.TryGetValue("End", out iteratorVariable11)) { foreach (PSTypeName iteratorVariable14 in this.GetInferredTypeFromScriptBlockParameter(iteratorVariable11, context)) { yield return(iteratorVariable14); } } } } foreach (PSTypeName iteratorVariable15 in commandInfo.OutputType) { yield return(iteratorVariable15); } } Label_0579: yield break; }
/// <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; }
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; }