forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPseudoParameterBinder.cs
More file actions
1983 lines (1751 loc) · 85.1 KB
/
Copy pathPseudoParameterBinder.cs
File metadata and controls
1983 lines (1751 loc) · 85.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System.Globalization;
using System.Linq;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Management.Automation.Host;
using System.Text;
using System.Reflection;
using System.Management.Automation.Runspaces;
namespace System.Management.Automation.Language
{
#region "AstArgumentPair"
/// <summary>
/// The types for AstParameterArgumentPair
/// </summary>
internal enum AstParameterArgumentType
{
AstPair = 0,
Switch = 1,
Fake = 2,
AstArray = 3,
PipeObject = 4
}
/// <summary>
/// The base class for parameter argument pair
/// </summary>
internal abstract class AstParameterArgumentPair
{
/// <summary>
/// The parameter Ast
/// </summary>
public CommandParameterAst Parameter { get; protected set; }
/// <summary>
/// The argument type
/// </summary>
public AstParameterArgumentType ParameterArgumentType { get; protected set; }
/// <summary>
/// Indicate if the parameter is specified
/// </summary>
public bool ParameterSpecified { get; protected set; } = false;
/// <summary>
/// Indicate if the parameter is specified
/// </summary>
public bool ArgumentSpecified { get; protected set; } = false;
/// <summary>
/// The parameter name
/// </summary>
public string ParameterName { get; protected set; }
/// <summary>
/// The parameter text
/// </summary>
public string ParameterText { get; protected set; }
/// <summary>
/// The argument type
/// </summary>
public Type ArgumentType { get; protected set; }
}
/// <summary>
/// Represent a parameter argument pair. The argument is a pipeline input object
/// </summary>
internal sealed class PipeObjectPair : AstParameterArgumentPair
{
internal PipeObjectPair(string parameterName, Type pipeObjType)
{
if (parameterName == null)
throw PSTraceSource.NewArgumentNullException("parameterName");
Parameter = null;
ParameterArgumentType = AstParameterArgumentType.PipeObject;
ParameterSpecified = true;
ArgumentSpecified = true;
ParameterName = parameterName;
ParameterText = parameterName;
ArgumentType = pipeObjType;
}
}
/// <summary>
/// Represent a parameter argument pair. The argument is an array of ExpressionAst (remaining
/// arguments)
/// </summary>
internal sealed class AstArrayPair : AstParameterArgumentPair
{
internal AstArrayPair(string parameterName, ICollection<ExpressionAst> arguments)
{
if (parameterName == null)
throw PSTraceSource.NewArgumentNullException("parameterName");
if (arguments == null || arguments.Count == 0)
throw PSTraceSource.NewArgumentNullException("arguments");
Parameter = null;
ParameterArgumentType = AstParameterArgumentType.AstArray;
ParameterSpecified = true;
ArgumentSpecified = true;
ParameterName = parameterName;
ParameterText = parameterName;
ArgumentType = typeof(Array);
Argument = arguments.ToArray();
}
/// <summary>
/// Get the argument
/// </summary>
public ExpressionAst[] Argument { get; } = null;
}
/// <summary>
/// Represent a parameter argument pair. The argument is a fake object.
/// </summary>
internal sealed class FakePair : AstParameterArgumentPair
{
internal FakePair(CommandParameterAst parameterAst)
{
if (parameterAst == null)
throw PSTraceSource.NewArgumentNullException("parameterAst");
Parameter = parameterAst;
ParameterArgumentType = AstParameterArgumentType.Fake;
ParameterSpecified = true;
ArgumentSpecified = true;
ParameterName = parameterAst.ParameterName;
ParameterText = parameterAst.ParameterName;
ArgumentType = typeof(object);
}
}
/// <summary>
/// Represent a parameter argument pair. The parameter is a switch parameter.
/// </summary>
internal sealed class SwitchPair : AstParameterArgumentPair
{
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>
/// Get the argument
/// </summary>
public bool Argument
{
get { return true; }
}
}
/// <summary>
/// Represent a parameter argument pair. It could be a pure argument (no parameter, only argument available);
/// it could be a CommandParameterAst that contains its argument; it also could be a CommandParameterAst with
/// another CommandParameterAst as the argument.
/// </summary>
internal sealed class AstPair : AstParameterArgumentPair
{
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;
}
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;
}
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;
}
/// <summary>
/// Indicate if the argument is contained in the CommandParameterAst
/// </summary>
public bool ParameterContainsArgument { get; } = false;
/// <summary>
/// Indicate if the argument is of type CommandParameterAst
/// </summary>
public bool ArgumentIsCommandParameterAst { get; } = false;
/// <summary>
/// Get the argument
/// </summary>
public CommandElementAst Argument { get; } = null;
}
#endregion "AstArgumentPair"
/// <summary>
/// Runs the PowerShell parameter binding algorithm against a CommandAst,
/// returning information about which parameters were bound.
///
/// </summary>
public static class StaticParameterBinder
{
/// <summary>
/// Bind a CommandAst to one of PowerShell's built-in commands
/// </summary>
/// <param name="commandAst">The CommandAst that represents the command invocation.</param>
/// <returns>The StaticBindingResult that represents the binding.</returns>
public static StaticBindingResult BindCommand(CommandAst commandAst)
{
bool resolve = true;
return BindCommand(commandAst, resolve);
}
/// <summary>
/// Bind a CommandAst to the specified command
/// </summary>
/// <param name="commandAst">The CommandAst that represents the command invocation.</param>
/// <param name="resolve">Boolean to determine whether binding should be syntactic, or should attempt
/// to resolve against an existing command.
/// </param>
/// <returns>The StaticBindingResult that represents the binding.</returns>
public static StaticBindingResult BindCommand(CommandAst commandAst, bool resolve)
{
return BindCommand(commandAst, resolve, null);
}
/// <summary>
/// Bind a CommandAst to the specified command
/// </summary>
/// <param name="commandAst">The CommandAst that represents the command invocation.</param>
/// <param name="resolve">Boolean to determine whether binding should be syntactic, or should attempt
/// to resolve against an existing command.
/// </param>
/// <param name="desiredParameters">
/// A string array that represents parameter names of interest. If any of these are specified,
/// then full binding is done.
/// </param>
/// <returns>The StaticBindingResult that represents the binding.</returns>
public static StaticBindingResult BindCommand(CommandAst commandAst, bool resolve, string[] desiredParameters)
{
// If they specified any desired parameters, first quickly check if they are found
if ((desiredParameters != null) && (desiredParameters.Length > 0))
{
bool possiblyHadDesiredParameter = false;
foreach (CommandParameterAst commandParameter in commandAst.CommandElements.OfType<CommandParameterAst>())
{
string actualParameterName = commandParameter.ParameterName;
foreach (string actualParameter in desiredParameters)
{
if (actualParameter.StartsWith(actualParameterName, StringComparison.OrdinalIgnoreCase))
{
possiblyHadDesiredParameter = true;
break;
}
}
if (possiblyHadDesiredParameter)
{
break;
}
}
// Quick exit if the desired parameter was not present
if (!possiblyHadDesiredParameter)
{
return null;
}
}
if (!resolve)
{
return new StaticBindingResult(commandAst, null);
}
PseudoBindingInfo pseudoBinding = null;
if (Runspace.DefaultRunspace == null)
{
// Handle static binding from a non-PowerShell / C# application
// DefaultRunspace is a thread static field, so race condition will not happen because different threads will access different instances of "DefaultRunspace"
if (s_bindCommandRunspace == null)
{
// Create a mini runspace by remove the types and formats
InitialSessionState minimalState = InitialSessionState.CreateDefault2();
minimalState.Types.Clear();
minimalState.Formats.Clear();
s_bindCommandRunspace = RunspaceFactory.CreateRunspace(minimalState);
s_bindCommandRunspace.Open();
}
Runspace.DefaultRunspace = s_bindCommandRunspace;
// Static binding always does argument binding (not argument or parameter completion).
pseudoBinding = new PseudoParameterBinder().DoPseudoParameterBinding(commandAst, null, null, PseudoParameterBinder.BindingType.ArgumentBinding);
Runspace.DefaultRunspace = null;
}
else
{
// Static binding always does argument binding (not argument or parameter completion).
pseudoBinding = new PseudoParameterBinder().DoPseudoParameterBinding(commandAst, null, null, PseudoParameterBinder.BindingType.ArgumentBinding);
}
return new StaticBindingResult(commandAst, pseudoBinding);
}
[ThreadStatic]
static Runspace s_bindCommandRunspace = null;
}
/// <summary>
/// Represents the results of the PowerShell parameter binding process.
/// </summary>
public class StaticBindingResult
{
internal StaticBindingResult(CommandAst commandAst, PseudoBindingInfo bindingInfo)
{
BoundParameters = new Dictionary<string, ParameterBindingResult>(StringComparer.OrdinalIgnoreCase);
BindingExceptions = new Dictionary<string, StaticBindingError>(StringComparer.OrdinalIgnoreCase);
if (bindingInfo == null)
{
CreateBindingResultForSyntacticBind(commandAst);
}
else
{
CreateBindingResultForSuccessfulBind(commandAst, bindingInfo);
}
}
private void CreateBindingResultForSuccessfulBind(CommandAst commandAst, PseudoBindingInfo bindingInfo)
{
_bindingInfo = bindingInfo;
// Check if there is exactly one parameter set valid. In that case,
// ValidParameterSetFlags is exactly a power of two. Otherwise,
// add to the binding exceptions.
bool parameterSetSpecified = bindingInfo.ValidParameterSetsFlags != UInt32.MaxValue;
bool remainingParameterSetIncludesDefault =
(bindingInfo.DefaultParameterSetFlag != 0) &&
((bindingInfo.ValidParameterSetsFlags & bindingInfo.DefaultParameterSetFlag) ==
bindingInfo.DefaultParameterSetFlag);
// (x & (x -1 ) == 0) is a bit hack to determine if something is
// exactly a power of two.
bool onlyOneRemainingParameterSet =
(bindingInfo.ValidParameterSetsFlags != 0) &&
(bindingInfo.ValidParameterSetsFlags &
(bindingInfo.ValidParameterSetsFlags - 1)) == 0;
if (parameterSetSpecified &&
(!remainingParameterSetIncludesDefault) &&
(!onlyOneRemainingParameterSet))
{
ParameterBindingException bindingException =
new ParameterBindingException(
ErrorCategory.InvalidArgument,
null,
null,
null,
null,
null,
ParameterBinderStrings.AmbiguousParameterSet,
"AmbiguousParameterSet");
BindingExceptions.Add(commandAst.CommandElements[0].Extent.Text,
new StaticBindingError(commandAst.CommandElements[0], bindingException));
}
// Add error for duplicate parameters
if (bindingInfo.DuplicateParameters != null)
{
foreach (AstParameterArgumentPair duplicateParameter in bindingInfo.DuplicateParameters)
{
AddDuplicateParameterBindingException(duplicateParameter.Parameter);
}
}
// Add error for parameters not found
if (bindingInfo.ParametersNotFound != null)
{
foreach (CommandParameterAst parameterNotFound in bindingInfo.ParametersNotFound)
{
ParameterBindingException bindingException =
new ParameterBindingException(
ErrorCategory.InvalidArgument,
null,
parameterNotFound.ErrorPosition,
parameterNotFound.ParameterName,
null,
null,
ParameterBinderStrings.NamedParameterNotFound,
"NamedParameterNotFound");
BindingExceptions.Add(parameterNotFound.ParameterName, new StaticBindingError(parameterNotFound, bindingException));
}
}
// Add error for ambiguous parameters
if (bindingInfo.AmbiguousParameters != null)
{
foreach (CommandParameterAst ambiguousParameter in bindingInfo.AmbiguousParameters)
{
ParameterBindingException bindingException = bindingInfo.BindingExceptions[ambiguousParameter];
BindingExceptions.Add(ambiguousParameter.ParameterName, new StaticBindingError(ambiguousParameter, bindingException));
}
}
// Add error for unbound positional parameters
if (bindingInfo.UnboundArguments != null)
{
foreach (AstParameterArgumentPair unboundArgument in bindingInfo.UnboundArguments)
{
AstPair argument = unboundArgument as AstPair;
ParameterBindingException bindingException =
new ParameterBindingException(
ErrorCategory.InvalidArgument,
null,
argument.Argument.Extent,
argument.Argument.Extent.Text,
null,
null,
ParameterBinderStrings.PositionalParameterNotFound,
"PositionalParameterNotFound");
BindingExceptions.Add(argument.Argument.Extent.Text, new StaticBindingError(argument.Argument, bindingException));
}
}
// Process the bound parameters
if (bindingInfo.BoundParameters != null)
{
foreach (KeyValuePair<string, MergedCompiledCommandParameter> item in bindingInfo.BoundParameters)
{
CompiledCommandParameter parameter = item.Value.Parameter;
CommandElementAst value = null;
Object constantValue = null;
// This is a single argument
AstPair argumentAstPair = bindingInfo.BoundArguments[item.Key] as AstPair;
if (argumentAstPair != null)
{
value = argumentAstPair.Argument;
}
// This is a parameter that took an argument, as well as ValueFromRemainingArguments.
// Merge the arguments into a single fake argument.
AstArrayPair argumentAstArrayPair = bindingInfo.BoundArguments[item.Key] as AstArrayPair;
if (argumentAstArrayPair != null)
{
List<ExpressionAst> arguments = new List<ExpressionAst>();
foreach (ExpressionAst expression in argumentAstArrayPair.Argument)
{
ArrayLiteralAst expressionArray = expression as ArrayLiteralAst;
if (expressionArray != null)
{
foreach (ExpressionAst newExpression in expressionArray.Elements)
{
arguments.Add((ExpressionAst)newExpression.Copy());
}
}
else
{
arguments.Add((ExpressionAst)expression.Copy());
}
}
// Define the virtual extent and virtual ArrayLiteral.
IScriptExtent fakeExtent = arguments[0].Extent;
ArrayLiteralAst fakeArguments = new ArrayLiteralAst(fakeExtent, arguments);
value = fakeArguments;
}
// Special handling of switch parameters
if (parameter.Type == typeof(SwitchParameter))
{
if ((value != null) &&
(String.Equals("$false", value.Extent.Text, StringComparison.OrdinalIgnoreCase)))
{
continue;
}
constantValue = true;
}
// We got a parameter and a value
if ((value != null) || (constantValue != null))
{
BoundParameters.Add(item.Key, new ParameterBindingResult(parameter, value, constantValue));
}
else
{
bool takesValueFromPipeline = false;
foreach (ParameterSetSpecificMetadata parameterSet in parameter.GetMatchingParameterSetData(bindingInfo.ValidParameterSetsFlags))
{
if (parameterSet.ValueFromPipeline)
{
takesValueFromPipeline = true;
break;
}
}
if (!takesValueFromPipeline)
{
// We have a parameter with no value that isn't a switch parameter, or input parameter
ParameterBindingException bindingException =
new ParameterBindingException(
ErrorCategory.InvalidArgument,
null,
commandAst.CommandElements[0].Extent,
parameter.Name,
parameter.Type,
null,
ParameterBinderStrings.MissingArgument,
"MissingArgument");
BindingExceptions.Add(commandAst.CommandElements[0].Extent.Text,
new StaticBindingError(commandAst.CommandElements[0], bindingException));
}
}
}
}
}
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));
}
}
private PseudoBindingInfo _bindingInfo = null;
private void CreateBindingResultForSyntacticBind(CommandAst commandAst)
{
bool foundCommand = false;
CommandParameterAst currentParameter = null;
int position = 0;
ParameterBindingResult bindingResult = new ParameterBindingResult();
foreach (CommandElementAst commandElement in commandAst.CommandElements)
{
// Skip the command name
if (!foundCommand)
{
foundCommand = true;
continue;
}
CommandParameterAst parameter = commandElement as CommandParameterAst;
if (parameter != null)
{
if (currentParameter != null)
{
// Assume it was a switch
AddSwitch(currentParameter.ParameterName, bindingResult);
ResetCurrentParameter(ref currentParameter, ref bindingResult);
}
// If this is an actual parameter, get its name.
string parameterName = parameter.ParameterName;
bindingResult.Value = parameter;
// If it's a parameter with argument, add them both to the dictionary
if (parameter.Argument != null)
{
bindingResult.Value = parameter.Argument;
AddBoundParameter(parameter, parameterName, bindingResult);
ResetCurrentParameter(ref currentParameter, ref bindingResult);
}
// Otherwise, it's just a parameter and the argument is to follow.
else
{
// Store our current parameter
currentParameter = parameter;
}
}
else
{
// This isn't a parameter, it's a value for the previous parameter
if (currentParameter != null)
{
bindingResult.Value = commandElement;
AddBoundParameter(currentParameter, currentParameter.ParameterName, bindingResult);
}
else
{
// Assume positional
bindingResult.Value = commandElement;
AddBoundParameter(null, position.ToString(CultureInfo.InvariantCulture), bindingResult);
position++;
}
ResetCurrentParameter(ref currentParameter, ref bindingResult);
}
}
// Catch any hanging parameters at the end of the command
if (currentParameter != null)
{
// Assume it was a switch
AddSwitch(currentParameter.ParameterName, bindingResult);
}
}
private void AddBoundParameter(CommandParameterAst parameter, string parameterName, ParameterBindingResult bindingResult)
{
if (BoundParameters.ContainsKey(parameterName))
{
AddDuplicateParameterBindingException(parameter);
}
else
{
BoundParameters.Add(parameterName, bindingResult);
}
}
private static void ResetCurrentParameter(ref CommandParameterAst currentParameter, ref ParameterBindingResult bindingResult)
{
currentParameter = null;
bindingResult = new ParameterBindingResult();
}
private void AddSwitch(string currentParameter, ParameterBindingResult bindingResult)
{
bindingResult.ConstantValue = true;
AddBoundParameter(null, currentParameter, bindingResult);
}
/// <summary>
///
/// </summary>
public Dictionary<string, ParameterBindingResult> BoundParameters { get; }
/// <summary>
///
/// </summary>
public Dictionary<string, StaticBindingError> BindingExceptions { get; }
}
/// <summary>
/// Represents the binding of a parameter to its argument
/// </summary>
public class ParameterBindingResult
{
internal ParameterBindingResult(CompiledCommandParameter parameter, CommandElementAst value, Object constantValue)
{
this.Parameter = new ParameterMetadata(parameter);
this.Value = value;
this.ConstantValue = constantValue;
}
internal ParameterBindingResult()
{
}
/// <summary>
///
/// </summary>
public ParameterMetadata Parameter { get; internal set; }
/// <summary>
///
/// </summary>
public Object ConstantValue
{
get { return _constantValue; }
internal set
{
if (value != null)
{
_constantValue = value;
}
}
}
private object _constantValue;
/// <summary>
///
/// </summary>
public CommandElementAst Value
{
get { return _value; }
internal set
{
_value = value;
ConstantExpressionAst constantValueAst = value as ConstantExpressionAst;
if (constantValueAst != null)
{
this.ConstantValue = constantValueAst.Value;
}
}
}
private CommandElementAst _value;
}
/// <summary>
/// Represents the exception generated by the static parameter binding process
/// </summary>
public class StaticBindingError
{
/// <summary>
/// Creates a StaticBindingException
/// </summary>
/// <param name="commandElement">The element associated with the exception</param>
/// <param name="exception">The parameter binding exception that got raised</param>
internal StaticBindingError(CommandElementAst commandElement, ParameterBindingException exception)
{
this.CommandElement = commandElement;
this.BindingException = exception;
}
/// <summary>
/// The command element associated with the exception.
/// </summary>
public CommandElementAst CommandElement { get; private set; }
/// <summary>
/// The ParameterBindingException that this command element caused.
/// </summary>
public ParameterBindingException BindingException { get; private set; }
}
#region "PseudoBindingInfo"
internal enum PseudoBindingInfoType
{
PseudoBindingFail = 0,
PseudoBindingSucceed = 1,
}
internal sealed class PseudoBindingInfo
{
/// <summary>
/// The pseudo binding succeeded
/// </summary>
/// <param name="commandInfo"></param>
/// <param name="validParameterSetsFlags"></param>
/// <param name="defaultParameterSetFlag"></param>
/// <param name="boundParameters"></param>
/// <param name="unboundParameters"></param>
/// <param name="boundArguments"></param>
/// <param name="boundPositionalParameter"></param>
/// <param name="allParsedArguments"></param>
/// <param name="parametersNotFound"></param>
/// <param name="ambiguousParameters"></param>
/// <param name="bindingExceptions"></param>
/// <param name="duplicateParameters"></param>
/// <param name="unboundArguments"></param>
internal PseudoBindingInfo(
CommandInfo commandInfo,
uint validParameterSetsFlags,
uint defaultParameterSetFlag,
Dictionary<string, MergedCompiledCommandParameter> boundParameters,
List<MergedCompiledCommandParameter> unboundParameters,
Dictionary<string, AstParameterArgumentPair> boundArguments,
Collection<string> boundPositionalParameter,
Collection<AstParameterArgumentPair> allParsedArguments,
Collection<CommandParameterAst> parametersNotFound,
Collection<CommandParameterAst> ambiguousParameters,
Dictionary<CommandParameterAst, ParameterBindingException> bindingExceptions,
Collection<AstParameterArgumentPair> duplicateParameters,
Collection<AstParameterArgumentPair> unboundArguments)
{
CommandInfo = commandInfo;
InfoType = PseudoBindingInfoType.PseudoBindingSucceed;
ValidParameterSetsFlags = validParameterSetsFlags;
DefaultParameterSetFlag = defaultParameterSetFlag;
BoundParameters = boundParameters;
UnboundParameters = unboundParameters;
BoundArguments = boundArguments;
BoundPositionalParameter = boundPositionalParameter;
AllParsedArguments = allParsedArguments;
ParametersNotFound = parametersNotFound;
AmbiguousParameters = ambiguousParameters;
BindingExceptions = bindingExceptions;
DuplicateParameters = duplicateParameters;
UnboundArguments = unboundArguments;
}
/// <summary>
/// The pseudo binding failed with parameter set confliction
/// </summary>
/// <param name="commandInfo"></param>
/// <param name="defaultParameterSetFlag"></param>
/// <param name="allParsedArguments"></param>
/// <param name="unboundParameters"></param>
internal PseudoBindingInfo(
CommandInfo commandInfo,
uint defaultParameterSetFlag,
Collection<AstParameterArgumentPair> allParsedArguments,
List<MergedCompiledCommandParameter> unboundParameters)
{
CommandInfo = commandInfo;
InfoType = PseudoBindingInfoType.PseudoBindingFail;
DefaultParameterSetFlag = defaultParameterSetFlag;
AllParsedArguments = allParsedArguments;
UnboundParameters = unboundParameters;
}
internal string CommandName
{
get { return CommandInfo.Name; }
}
internal CommandInfo CommandInfo { get; }
internal PseudoBindingInfoType InfoType { get; }
internal uint ValidParameterSetsFlags { get; }
internal uint DefaultParameterSetFlag { get; }
internal Dictionary<string, MergedCompiledCommandParameter> BoundParameters { get; }
internal List<MergedCompiledCommandParameter> UnboundParameters { get; }
internal Dictionary<string, AstParameterArgumentPair> BoundArguments { get; }
internal Collection<AstParameterArgumentPair> UnboundArguments { get; }
internal Collection<string> BoundPositionalParameter { get; }
internal Collection<AstParameterArgumentPair> AllParsedArguments { get; }
internal Collection<CommandParameterAst> ParametersNotFound { get; }
internal Collection<CommandParameterAst> AmbiguousParameters { get; }
internal Dictionary<CommandParameterAst, ParameterBindingException> BindingExceptions { get; }
internal Collection<AstParameterArgumentPair> DuplicateParameters { get; }
}
#endregion "PseudoBindingInfo"
internal class PseudoParameterBinder
{
/*
/// <summary>
/// Get the parameter binding metadata
/// </summary>
/// <param name="possibleParameterSets"></param>
/// <returns></returns>
public Dictionary<ParameterMetadata, ExpressionAst> GetPseudoParameterBinding(out Collection<ParameterSetMetadata> possibleParameterSets)
{
ExecutionContext contextFromTls =
System.Management.Automation.Runspaces.LocalPipeline.GetExecutionContextFromTLS();
return GetPseudoParameterBinding(out possibleParameterSets, contextFromTls, null);
}
*/
internal enum BindingType
{
/// <summary>
/// Caller is binding a parameter argument
/// </summary>
ArgumentBinding = 0,
/// <summary>
/// Caller is performing completion on a parameter argument
/// </summary>
ArgumentCompletion,
/// <summary>
/// Caller is performing completion on a parameter name
/// </summary>
ParameterCompletion
}
/// <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)
{