public void Reparent(PowershellItem newParent)
 {
     if (Parent != null)
     {
         this.Parent.Children.Remove(this);
     }
     this.Parent = newParent;
     if (newParent != null)
     {
         newParent.Children.Add(this);
     }
 }
 public PowershellItem(PowershellItemType type, string name, int startLine, int startColumn, int nestingLevel, PowershellItem parent, string parsingErrors)
 {
     Type = type;
     Name = name;
     StartLine = startLine;
     StartColumn = startColumn;
     NestingLevel = nestingLevel;
     Parent = parent;
     ParsingErrors = parsingErrors;
     Children = new List<PowershellItem>();
     if (parent != null)
     {
         Parent.Children.Add(this);
     }
 }
示例#3
0
 public PowershellParseResult(PowershellItem rootPowershellItem, string errorMessage, string path, string fileContents, bool isDirectory, bool isExcluded)
 {
     this.RootPowershellItem = rootPowershellItem;
     if (rootPowershellItem != null)
     {
         this.ErrorMessage = this.RootPowershellItem.ParsingErrors;
     }
     else
     {
         this.ErrorMessage = errorMessage;
     }
     this.Path         = path;
     this.FileContents = fileContents;
     this.IsDirectory  = isDirectory;
     this.IsExcluded   = isExcluded;
 }
 public PowershellItem(PowershellItemType type, string name, int startLine, int startColumn, int endColumn, int nestingLevel, PowershellItem parent, string parsingErrors)
 {
     this.Type = type;
     this.Name = name;
     this.StartLine = startLine;
     this.StartColumn = startColumn;
     this.EndColumn = endColumn;
     this.NestingLevel = nestingLevel;
     this.Parent = parent;
     this.ParsingErrors = parsingErrors;
     this.Children = new List<PowershellItem>();
     if (parent != null)
     {
         this.Parent.Children.Add(this);
     }
 }
        public static PowershellItem GetPowershellItems(string path, string contents)
        {
            Collection<PSParseError> errors;
            bool dscParse = false;
            // this is fix for performance issue in PSParser.Tokenize - when file contains Import-DSCResource pointing to a non-installed resource,
            // parsing takes long time and 'Unable to load resource' errors appear
            if (importDscRegex.IsMatch(contents))
            {
                contents = importDscRegex.Replace(contents, "#Import-DSCResource");
                dscParse = true;
            }
            IEnumerable<PSToken> tokens = PSParser.Tokenize(contents, out errors);
            var errorsLog = !errors.Any() || dscParse ? null :
                "Parsing error(s): " + Environment.NewLine + string.Join(Environment.NewLine, errors.OrderBy(err => err.Token.StartLine).Select(err => "Line " + err.Token.StartLine + ": " + err.Message));
            PowershellItem rootItem = new PowershellItem(PowershellItemType.Root, null, 0, 0, 0, null, errorsLog);
            if (errorsLog != null)
            {
                Logger.Debug("File " + path + " - " + errorsLog);
            }
            PowershellItem currentItem = rootItem;

            bool nextTokenIsFunctionName = false;
            PSToken commandToken = null;

            int nestingLevel = 0;
            foreach (PSToken token in tokens)
            {
                if (nextTokenIsFunctionName)
                {
                    var item = new PowershellItem(PowershellItemType.Function, token.Content, token.StartLine, token.StartColumn, nestingLevel, currentItem, null);
                    // currentItem = item;
                    nextTokenIsFunctionName = false;
                }
                else if (token.Type == PSTokenType.Keyword)
                {
                    string tokenContent = token.Content.ToLowerInvariant();
                    if (tokenContent == "function" || tokenContent == "filter" || tokenContent == "configuration" || tokenContent == "workflow")
                    {
                        nextTokenIsFunctionName = true;
                    }
                }
            }
            return rootItem;
        }
        public PowershellItem GetPowershellItems(string path, string contents)
        {
            bool parsePowershellItems = configValues.ParsePowershellItems;
            if (!parsePowershellItems || !this.parsePowershellDscWithExternalImports && ImportDscRegex.IsMatch(contents))
            {
                return this.createRootItem(null);
            }

            ParseError[] errors;
            Token[] tokens;
            AddedItems = new HashSet<string>();
            
            
            Ast ast = Parser.ParseInput(contents, out tokens, out errors);
            var errorsLog = !errors.Any() ? null :
                "Parsing error(s): " + Environment.NewLine + string.Join(Environment.NewLine, errors.OrderBy(err => err.Extent.StartLineNumber).Select(err => "Line " + err.Extent.StartLineNumber + ": " + err.Message));
            RootItem = this.createRootItem(errorsLog);

            VisitTokens(ast);

            return RootItem;
        }
 public PowershellItem(PowershellItemType type, string name, int startLine, int startColumn, int endColumn, int nestingLevel, PowershellItem parent, string parsingErrors)
 {
     this.Type          = type;
     this.Name          = name;
     this.StartLine     = startLine;
     this.StartColumn   = startColumn;
     this.EndColumn     = endColumn;
     this.NestingLevel  = nestingLevel;
     this.Parent        = parent;
     this.ParsingErrors = parsingErrors;
     this.Children      = new List <PowershellItem>();
     if (parent != null)
     {
         this.Parent.Children.Add(this);
     }
 }
 protected PowershellItem CreateNewPowershellItem(PowershellItemType type, string itemName, int startColumnNumber, int endColumnNumber, int startLineNumber, PowershellItem parent, int nestingLevel)
 {
     string itemKey = startLineNumber + "_" + startColumnNumber;
     if (!AddedItems.Contains(itemKey))
     {
         AddedItems.Add(itemKey);
         if (parent == null)
         {
             parent = RootItem;
         }
         return new PowershellItem(type, itemName, startLineNumber, startColumnNumber, endColumnNumber, nestingLevel, parent, null);
     }
     else
     {
         return null;
     }
 }
 private PowershellItem createRootItem(string errorsLog)
 {
     RootItem = new PowershellItem(PowershellItemType.Root, null, 0, 0, 0, 0, null, errorsLog);
     return RootItem;
 } 
 protected PowershellItem CreateNewPowershellItem(PowershellItemType type, string itemName, IScriptExtent extent, PowershellItem parent, int nestingLevel)
 {
     int startColumnNumber = extent.StartColumnNumber + extent.Text.IndexOf(itemName);
     int endColumnNumber = startColumnNumber + itemName.Length;
     int startLineNumber = extent.StartLineNumber;
     return CreateNewPowershellItem(type, itemName, startColumnNumber, endColumnNumber, startLineNumber, parent, nestingLevel);
 }
 private void ParseFile()
 {
     try
     {
         FileContents = FileReader.ReadFileAsString(Path);
     }
     catch (Exception e)
     {
         ErrorMessage = e.Message;
         return;
     }
     if (FileContents != null)
     {
         RootPowershellItem = PowershellTokenizer.GetPowershellItems(Path, FileContents);
         ErrorMessage = RootPowershellItem.ParsingErrors;
     }
 }