Exemplo n.º 1
0
 public MemberExpressionAst(IScriptExtent extent, ExpressionAst expression, CommandElementAst member, bool @static)
     : base(extent)
 {
     this.Expression = expression;
     this.Member = member;
     this.Static = @static;
 }
        /// <summary>
        /// Condition on the parameter that must be satisfied for the error to be raised.
        /// </summary>
        /// <param name="CmdAst"></param>
        /// <param name="CeAst"></param>
        /// <returns></returns>
        public override bool ParameterCondition(CommandAst CmdAst, CommandElementAst CeAst)
        {
            if (CeAst is CommandParameterAst)
            {
                CommandParameterAst cmdParamAst = CeAst as CommandParameterAst;

                if (String.Equals(cmdParamAst.ParameterName, "computername", StringComparison.OrdinalIgnoreCase))
                {
                    List<string> localhostRepresentations = new List<string> { "localhost", ".", "::1", "127.0.0.1" };
                    Ast computerNameArgument = GetComputerNameArg(CmdAst, cmdParamAst.Extent.StartOffset);                                        

                    if (null != computerNameArgument)
                    {
                        if (!localhostRepresentations.Contains(computerNameArgument.Extent.Text.ToLower()))
                        {
                            return computerNameArgument is ConstantExpressionAst;
                        }

                        return false;
                    }
                    
                    if (null != cmdParamAst.Argument && !localhostRepresentations.Contains(cmdParamAst.Argument.ToString().Replace("\"", "").Replace("'", "").ToLower()))
                    {
                        return cmdParamAst.Argument is ConstantExpressionAst;
                    }
                }
            }

            return false;
        }
Exemplo n.º 3
0
 public InvokeMemberExpressionAst(IScriptExtent extent, ExpressionAst expression, CommandElementAst method, IEnumerable<ExpressionAst> arguments, bool @static) : base(extent, expression, method, @static)
 {
     if ((arguments != null) && arguments.Any<ExpressionAst>())
     {
         this.Arguments = new ReadOnlyCollection<ExpressionAst>(arguments.ToArray<ExpressionAst>());
         base.SetParents((IEnumerable<Ast>) this.Arguments);
     }
 }
 private static bool IsParameterName(CommandElementAst ast, string name)
 {
     var constantAst = ast as CommandParameterAst;
     if (constantAst == null)
     {
         return false;
     }
     return String.Equals(constantAst.ParameterName, name, StringComparison.OrdinalIgnoreCase);
 }
Exemplo n.º 5
0
        private static ConstantExpressionAst GetRootConstantExpression(CommandElementAst element)
        {
            while (element is MemberExpressionAst)
            {
                var memberExpression = (MemberExpressionAst)element;
                element = memberExpression.Expression;
            }

            return element as ConstantExpressionAst;
        }
Exemplo n.º 6
0
 public MemberExpressionAst(IScriptExtent extent, ExpressionAst expression, CommandElementAst member, bool @static) : base(extent)
 {
     if ((expression == null) || (member == null))
     {
         throw PSTraceSource.NewArgumentNullException((expression == null) ? "expression" : "member");
     }
     this.Expression = expression;
     base.SetParent(expression);
     this.Member = member;
     base.SetParent(member);
     this.Static = @static;
 }
Exemplo n.º 7
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;
 }
Exemplo n.º 8
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;
 }
Exemplo n.º 9
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;
 }
Exemplo n.º 10
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;
 }
Exemplo n.º 11
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;
 }
Exemplo n.º 12
0
 internal ParameterBindingResult(CompiledCommandParameter parameter, CommandElementAst value, Object constantValue)
 {
     this.Parameter = new ParameterMetadata(parameter);
     this.Value = value;
     this.ConstantValue = constantValue;
 }
Exemplo n.º 13
0
 private ExpressionAst MemberInvokeRule(ExpressionAst targetExpr, Token lParen, Token operatorToken, CommandElementAst member)
 {
     IScriptExtent scriptExtent;
     List<ExpressionAst> expressionAsts = new List<ExpressionAst>();
     bool flag = false;
     bool flag1 = this._disableCommaOperator;
     Token token = null;
     try
     {
         this._disableCommaOperator = true;
         while (true)
         {
             this.SkipNewlines();
             ExpressionAst expressionAst = this.ExpressionRule();
             if (expressionAst != null)
             {
                 expressionAsts.Add(expressionAst);
                 this.SkipNewlines();
                 token = this.NextToken();
                 if (token.Kind != TokenKind.Comma)
                 {
                     this.UngetToken(token);
                     token = null;
                     break;
                 }
             }
             else
             {
                 if (token == null)
                 {
                     break;
                 }
                 object[] objArray = new object[1];
                 objArray[0] = TokenKind.Comma.Text();
                 this.ReportIncompleteInput(Parser.After(token), ParserStrings.MissingExpressionAfterToken, objArray);
                 flag = true;
                 break;
             }
         }
     }
     finally
     {
         this._disableCommaOperator = flag1;
     }
     this.SkipNewlines();
     Token token1 = this.NextToken();
     if (token1.Kind != TokenKind.RParen)
     {
         this.UngetToken(token1);
         if (!flag)
         {
             Parser parser = this;
             if (expressionAsts.Any<ExpressionAst>())
             {
                 scriptExtent = Parser.After(expressionAsts.Last<ExpressionAst>());
             }
             else
             {
                 scriptExtent = Parser.After(lParen);
             }
             parser.ReportIncompleteInput(scriptExtent, ParserStrings.MissingEndParenthesisInMethodCall, new object[0]);
         }
         token1 = null;
     }
     object[] objArray1 = new object[4];
     objArray1[0] = token1;
     objArray1[1] = token;
     objArray1[2] = expressionAsts.LastOrDefault<ExpressionAst>();
     objArray1[3] = lParen;
     return new InvokeMemberExpressionAst(Parser.ExtentOf(targetExpr, Parser.ExtentFromFirstOf(objArray1)), targetExpr, member, expressionAsts, operatorToken.Kind == TokenKind.ColonColon);
 }
Exemplo n.º 14
0
 private static StringConstantExpressionAst MergeMemberAccessExpressions(Ast left, CommandElementAst right)
 {
     var extent = MergeExtents(left.Extent, right.Extent);
     return new StringConstantExpressionAst(extent, extent.Text, StringConstantType.BareWord);
 }
 /// <summary>
 /// Condition on the parameter that must be satisfied for the error to be raised.
 /// </summary>
 /// <param name="CmdAst"></param>
 /// <param name="CeAst"></param>
 /// <returns></returns>
 public override bool ParameterCondition(CommandAst CmdAst, CommandElementAst CeAst)
 {
     return CeAst is CommandParameterAst && String.Equals((CeAst as CommandParameterAst).ParameterName, "AsPlainText", StringComparison.OrdinalIgnoreCase);
 }
Exemplo n.º 16
0
 public InvokeMemberExpressionAst(IScriptExtent extent, ExpressionAst expression, CommandElementAst method, IEnumerable<ExpressionAst> arguments, bool @static)
     : base(extent, expression, method, @static)
 {
     this.Arguments = arguments.ToReadOnlyCollection();
 }
Exemplo n.º 17
0
 private Expression GetCommandArgumentExpression(CommandElementAst element)
 {
     ConstantExpressionAst ast = element as ConstantExpressionAst;
     if ((ast != null) && LanguagePrimitives.IsNumeric(LanguagePrimitives.GetTypeCode(ast.StaticType)))
     {
         string text = ast.Extent.Text;
         if (!text.Equals(ast.Value.ToString(), StringComparison.Ordinal))
         {
             return Expression.Constant(ParserOps.WrappedNumber(ast.Value, text));
         }
     }
     Expression expression = this.Compile(element);
     if (expression.Type.Equals(typeof(object[])))
     {
         return Expression.Call(CachedReflectionInfo.PipelineOps_CheckAutomationNullInCommandArgumentArray, expression);
     }
     if ((ast == null) && expression.Type.Equals(typeof(object)))
     {
         expression = Expression.Call(CachedReflectionInfo.PipelineOps_CheckAutomationNullInCommandArgument, expression);
     }
     return expression;
 }
Exemplo n.º 18
0
        CommandParameter ConvertCommandElementToCommandParameter(CommandElementAst commandElement)
        {
            if (commandElement is CommandParameterAst)
            {
                var commandParameterAst = commandElement as CommandParameterAst;
                return new CommandParameter(commandParameterAst.ParameterName, commandParameterAst.Argument);
            }

            else if (commandElement is StringConstantExpressionAst)
            {
                var stringConstantExpressionAst = commandElement as StringConstantExpressionAst;
                return new CommandParameter(null, stringConstantExpressionAst.Value);
            }

            else if (commandElement is ExpressionAst)
            {
                return new CommandParameter(null, EvaluateAst(commandElement));
            }

            else throw new NotImplementedException();
        }
Exemplo n.º 19
0
        private string GetCommandName(CommandElementAst commandNameAst, bool isTrustedInput)
        {
            var exprAst = commandNameAst as ExpressionAst;
            string commandName;
            if (exprAst != null)
            {
                var value = GetExpressionValue(exprAst, isTrustedInput);
                if (value == null)
                {
                    ScriptBlockToPowerShellChecker.ThrowError(
                        new ScriptBlockToPowerShellNotSupportedException(
                            "CantConvertWithScriptBlockInvocation", null, AutomationExceptions.CantConvertWithScriptBlockInvocation),
                        exprAst);
                }

                if (value is CommandInfo)
                {
                    commandName = ((CommandInfo)value).Name;
                }
                else
                {
                    commandName = value as string;
                }
            }
            else
            {
                // If this assertion fires, the command name is determined incorrectly.
                Diagnostics.Assert(commandNameAst is CommandParameterAst, "Unexpected element not handled correctly.");
                commandName = commandNameAst.Extent.Text;
            }

            if (string.IsNullOrWhiteSpace(commandName))
            {
                // TODO: could use a better error here
                throw new ScriptBlockToPowerShellNotSupportedException(
                    "CantConvertWithScriptBlockInvocation",
                    null,
                    AutomationExceptions.CantConvertWithScriptBlockInvocation);
            }

            return commandName;
        }
 /// <summary>
 /// Condition on the parameter that must be satisfied for the error to be raised.
 /// </summary>
 /// <param name="CmdAst"></param>
 /// <param name="CeAst"></param>
 /// <returns></returns>
 public abstract bool ParameterCondition(CommandAst CmdAst, CommandElementAst CeAst);
Exemplo n.º 21
0
 public InvokeMemberExpressionAst(IScriptExtent extent, ExpressionAst expression, CommandElementAst method, IEnumerable <ExpressionAst> arguments, bool @static)
     : base(extent, expression, method, @static)
 {
     this.Arguments = arguments.ToReadOnlyCollection();
 }
Exemplo n.º 22
0
 public InvokeMemberExpressionAst(IScriptExtent extent, ExpressionAst expression, CommandElementAst method, IEnumerable <ExpressionAst> arguments, bool @static) : base(extent, expression, method, @static)
 {
     if ((arguments != null) && arguments.Any <ExpressionAst>())
     {
         this.Arguments = new ReadOnlyCollection <ExpressionAst>(arguments.ToArray <ExpressionAst>());
         base.SetParents((IEnumerable <Ast>) this.Arguments);
     }
 }
Exemplo n.º 23
0
        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;
        }
Exemplo n.º 24
0
        private Expression GetCommandArgumentExpression(CommandElementAst element)
        {
            var constElement = element as ConstantExpressionAst;
            if (constElement != null && LanguagePrimitives.IsNumeric(LanguagePrimitives.GetTypeCode(constElement.StaticType)))
            {
                var commandArgumentText = constElement.Extent.Text;
                if (!commandArgumentText.Equals(constElement.Value.ToString(), StringComparison.Ordinal))
                {
                    // If the ToString on the constant would differ from what the user specified, then wrap the
                    // value so we can recover the actual argument text.
                    return Expression.Constant(ParserOps.WrappedNumber(constElement.Value, commandArgumentText));
                }
            }

            var result = Compile(element);
            if (result.Type == typeof(object[]))
            {
                result = Expression.Call(CachedReflectionInfo.PipelineOps_CheckAutomationNullInCommandArgumentArray, result);
            }
            else if (constElement == null && result.Type == typeof(object))
            {
                result = Expression.Call(CachedReflectionInfo.PipelineOps_CheckAutomationNullInCommandArgument, result);
            }

            return result;
        }
 private string GetCommandName(CommandElementAst commandNameAst)
 {
     string name;
     ExpressionAst exprAst = commandNameAst as ExpressionAst;
     if (exprAst != null)
     {
         object expressionValue = this.GetExpressionValue(exprAst);
         if (expressionValue == null)
         {
             ScriptBlockToPowerShellChecker.ThrowError(new ScriptBlockToPowerShellNotSupportedException("CantConvertWithScriptBlockInvocation", null, AutomationExceptions.CantConvertWithScriptBlockInvocation, new object[0]), exprAst);
         }
         if (expressionValue is CommandInfo)
         {
             name = ((CommandInfo) expressionValue).Name;
         }
         else
         {
             name = expressionValue as string;
         }
     }
     else
     {
         name = commandNameAst.Extent.Text;
     }
     if (string.IsNullOrWhiteSpace(name))
     {
         throw new ScriptBlockToPowerShellNotSupportedException("CantConvertWithScriptBlockInvocation", null, AutomationExceptions.CantConvertWithScriptBlockInvocation, new object[0]);
     }
     return name;
 }
Exemplo n.º 26
0
 /// <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;
 }
Exemplo n.º 27
0
        private ExpressionAst MemberInvokeRule(ExpressionAst targetExpr, Token lBracket, Token operatorToken, CommandElementAst member)
        {
            //G  invocation-expression: target-expression passed as a parameter. lBracket can be '(' or '{'.
            //G      target-expression   member-name   invoke-param-list
            //G  invoke-param-list:
            //G      '('   invoke-param-paren-list
            //G      script-block 

            IScriptExtent lastExtent = null;

            List<ExpressionAst> arguments;
            if (lBracket.Kind == TokenKind.LParen)
            {
                arguments = this.InvokeParamParenListRule(lBracket, out lastExtent);
            }
            else
            {
                arguments = new List<ExpressionAst>();
                // handle the construct $x.methodName{2+2} as through it had been written $x.methodName({2+2})
                SkipNewlines();
                ExpressionAst argument = ScriptBlockExpressionRule(lBracket);
                arguments.Add(argument);
                lastExtent = argument.Extent;
            }

            return new InvokeMemberExpressionAst(ExtentOf(targetExpr, lastExtent), targetExpr, member, arguments, operatorToken.Kind == TokenKind.ColonColon);
        }