internal static string AnalysisDictionaryKey(VariableExpressionAst varExprAst) { if (varExprAst == null) { return(String.Empty); } return(String.Format(CultureInfo.CurrentCulture, "{0}s{1}e{2}", AssignmentTarget.GetUnaliasedVariableName(varExprAst.VariablePath), varExprAst.Extent.StartOffset, varExprAst.Extent.EndOffset )); }
/// <summary> /// Visit variable expression ast /// </summary> /// <param name="variableExpressionAst"></param> /// <returns></returns> public override object VisitVariableExpression(VariableExpressionAst variableExpressionAst) { if (variableExpressionAst == null) { return(null); } var varPath = variableExpressionAst.VariablePath; if (_variables.ContainsKey(AssignmentTarget.GetUnaliasedVariableName(varPath))) { var details = _variables[AssignmentTarget.GetUnaliasedVariableName(varPath)]; Current.AddAst(new VariableTarget(variableExpressionAst)); details.AssociatedAsts.Add(variableExpressionAst); } return(base.VisitVariableExpression(variableExpressionAst)); }
/// <summary> /// Visit assignment statement /// </summary> /// <param name="assignmentStatementAst"></param> /// <returns></returns> public override object VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst) { if (assignmentStatementAst == null) { return(null); } base.VisitAssignmentStatement(assignmentStatementAst); foreach (var assignTarget in GetAssignmentTargets(assignmentStatementAst.Left)) { var leftAst = assignTarget; while (leftAst is AttributedExpressionAst) { leftAst = ((AttributedExpressionAst)leftAst).Child; } if (leftAst is VariableExpressionAst) { var varPath = ((VariableExpressionAst)leftAst).VariablePath; if (_variables.ContainsKey(AssignmentTarget.GetUnaliasedVariableName(varPath))) { var details = _variables[AssignmentTarget.GetUnaliasedVariableName(varPath)]; details.AssignedBlocks.Add(Current); } Current.AddAst(new AssignmentTarget(assignmentStatementAst)); } else { // We skip things like $a.test = 3. In this case we will just test // for variable $a assignTarget.Visit(this.Decorator); } } return(null); }
private void ProcessParameters(IEnumerable <ParameterAst> parameters) { foreach (var parameter in parameters) { var variablePath = parameter.Name.VariablePath; bool isSwitchOrMandatory = false; Type type = null; foreach (var paramAst in parameter.Attributes) { if (paramAst is TypeConstraintAst) { if (type == null) { type = paramAst.TypeName.GetReflectionType(); } if (String.Equals(paramAst.TypeName.FullName, "switch", StringComparison.OrdinalIgnoreCase)) { isSwitchOrMandatory = true; } } else if (paramAst is AttributeAst) { var args = (paramAst as AttributeAst).NamedArguments; if (args != null) { foreach (NamedAttributeArgumentAst arg in args) { if (String.Equals(arg.ArgumentName, "mandatory", StringComparison.OrdinalIgnoreCase) && String.Equals(arg.Argument.Extent.Text, "$true", StringComparison.OrdinalIgnoreCase)) { isSwitchOrMandatory = true; } } } } } var varName = AssignmentTarget.GetUnaliasedVariableName(variablePath); var details = _variables[varName]; details.Type = type ?? details.Type ?? typeof(object); if (parameter.DefaultValue != null) { var assignTarget = new AssignmentTarget(varName, type); if (parameter.DefaultValue is ConstantExpressionAst) { assignTarget.Constant = (parameter.DefaultValue as ConstantExpressionAst).Value; assignTarget.Type = assignTarget.Constant == null ? typeof(object) : assignTarget.Constant.GetType(); } Entry.AddAst(assignTarget); } else if (isSwitchOrMandatory) { // Consider switch or mandatory parameter as already initialized Entry.AddAst(new AssignmentTarget(varName, type)); } else { VariableTarget varTarget = new VariableTarget(parameter.Name); varTarget.Type = details.Type; Entry.AddAst(varTarget); } } }