private static IClassificationType GetClassificationType(Token token) { var pSTokenType = PSToken.GetPSTokenType(token); var classificationType = PowerShellClassifier.GetClassificationType(pSTokenType); return(classificationType); }
//_____________________________________________________________________________________________________________________________________________________________ private void GetReferencesFromScriptTextWithRegEx(PSToken token, List <cScriptLocation> l, string FullPath, string txt) { LogHelper.Add("GetReferencesFromScriptTextWithRegEx"); string pattern = $"[\\W\\b]+({(Regex.Escape(token.Content))})[\\W\\b]+"; if (!Regex.IsMatch(txt, pattern, RegexOptions.IgnoreCase)) { return; } string[] ltxt = txt.Replace("\r", "").Split('\n'); for (int i = 0; i < ltxt.Length; i++) { foreach (Match m in Regex.Matches($" {ltxt[i]} ", pattern, RegexOptions.IgnoreCase)) { if (token.Type == PSTokenType.Variable) { l.Add(new cScriptLocation(Path.GetFileName(FullPath), token.Type, i + 1, m.Groups[1].Index - 1, ltxt, $"${token.Content}", mProjects.Project.Functions.GetFunctionsByLine(FullPath, i + 1)?.Name)); } else if (token.Type == PSTokenType.String) { l.Add(new cScriptLocation(Path.GetFileName(FullPath), token.Type, i + 1, m.Groups[1].Index - 1, ltxt, $"\"{token.Content}\"", mProjects.Project.Functions.GetFunctionsByLine(FullPath, i + 1)?.Name)); } else { l.Add(new cScriptLocation(Path.GetFileName(FullPath), token.Type, i + 1, m.Groups[1].Index, ltxt, token.Content, mProjects.Project.Functions.GetFunctionsByLine(FullPath, i + 1)?.Name)); } } } }
//_____________________________________________________________________________________________________________________________________________________________ private static void GetParenthesisTokensFromStringToken(PSToken tknToSearch, string txt, Queue <Token> tokenQueue, Token token) { LogHelper.Add("GetParenthesisTokensFromStringToken"); if (!token.Content.Contains("$(")) { return; } List <Tuple <int, int> > lst = ListOfParenthesis(token.Content); foreach (Tuple <int, int> tpl in lst) { string script = token.Content.Substring(tpl.Item1, tpl.Item2 - tpl.Item1 + 1); Collection <PSParseError> errors = new Collection <PSParseError>(); ScriptBlock scriptBlock = ScriptBlock.Create(script); foreach (PSToken pst in PSParser.Tokenize(new object[] { scriptBlock }, out errors)) { Token t = new Token(pst); t.StartLine += (token.StartLine - 1); t.StartColumn += (token.StartColumn) + tpl.Item1; t.EndColumn += (token.StartColumn) + tpl.Item1; tokenQueue.Enqueue(t); GetTokensFromStringToken(tknToSearch, txt, tokenQueue, t); } } }
/// <summary> /// Creates a <see cref = "SnapshotSpan" /> for the given text snapshot covering all text between /// the start and end tokens - (startToken, endToken) /// </summary> /// <param name = "snapshot">The current snapshot.</param> /// <param name = "startToken">The start token of the span.</param> /// <param name = "endToken">The end token of the span.</param> /// <returns></returns> protected override SnapshotSpan CreateSnapshotSpan(ITextSnapshot snapshot, PSToken startToken, PSToken endToken) { SnapshotSpan startSnapshot = CreateSnapshotSpan(snapshot, startToken); SnapshotSpan endSnapshot = CreateSnapshotSpan(snapshot, endToken); return(new SnapshotSpan(startSnapshot.Start, endSnapshot.End)); }
//_____________________________________________________________________________________________________________________________________________________________ private void GetReferencesFromScriptText(PSToken token, List <cScriptLocation> l, string FullPath, string txt) { if (!GetReferencesFromScriptTextWithParser(token, l, FullPath, txt)) { GetReferencesFromScriptTextWithRegEx(token, l, FullPath, txt); } }
public static string ToDebugString(this Token token) { //Replace NewLine Char var text = token.Text.Replace("\r\n", @"\r\n").Replace("\n", @"\n"); return(String.Format("{0,-20} : {1,-30} : {2,-20} : {3}", token.Kind, token.TokenFlags, PSToken.GetPSTokenType(token), text)); }
internal PSToken(Token token) { this._type = PSToken.GetTokenType(token.TokenId); this._content = token.TokenText; this._start = token.Start; this._length = token.End - token.Start; this._startLine = token.StartLineNumber; this._startColumn = token.StartOffsetInLine; this._endLine = token.LineNumber; this._endColumn = token.OffsetInLine; }
//_____________________________________________________________________________________________________________________________________________________________ public Token(PSToken t) { Content = t.Content; Type = t.Type; Start = t.Start; Length = t.Length; StartLine = t.StartLine; StartColumn = t.StartColumn; EndLine = t.EndLine; EndColumn = t.EndColumn; this.t = t; }
//_____________________________________________________________________________________________________________________________________________________________ private bool GetReferencesFromScriptTextWithParser(PSToken token, List <cScriptLocation> l, string FullPath, string txt) { LogHelper.Add("GetReferencesFromScriptTextWithParser"); Queue <Token> tokenQueue = new Queue <Token>(); ScriptBlock scriptBlock; try { scriptBlock = ScriptBlock.Create(txt); } catch (Exception ex) { LogHelper.Add($"GetReferencesFromScriptText->Create ERROR: {ex.Message}"); return(false); } string[] ltxt = txt.Replace("\r", "").Split('\n'); foreach (PSToken t in PSParser.Tokenize(new object[] { scriptBlock }, out Collection <PSParseError> errors)) { tokenQueue.Enqueue(new Token(t)); if (token.Type != PSTokenType.String) { Token.GetTokensFromStringToken(token, txt, tokenQueue, t); } } foreach (Token t in tokenQueue) { if (t.Content == null) { continue; } if (!t.Content.iEquals(token.Content)) { continue; } if ((token.Type == PSTokenType.CommandArgument && t.Type == PSTokenType.Command) || (token.Type == PSTokenType.CommandArgument && t.Type == PSTokenType.CommandArgument) || (token.Type == PSTokenType.Command && t.Type == PSTokenType.CommandArgument) || (token.Type == PSTokenType.Command && t.Type == PSTokenType.Command) || (token.Type == PSTokenType.CommandParameter && t.Type == PSTokenType.CommandParameter) || (token.Type == PSTokenType.Comment && t.Type == PSTokenType.Comment) || (token.Type == PSTokenType.Keyword && t.Type == PSTokenType.Keyword) || (token.Type == PSTokenType.Member && t.Type == PSTokenType.Member) || (token.Type == PSTokenType.Member && t.Type == PSTokenType.CommandArgument)) { l.Add(new cScriptLocation(Path.GetFileName(FullPath), t.Type, t.StartLine, t.StartColumn, ltxt, token.Content, mProjects.Project.Functions.GetFunctionsByLine(FullPath, t.StartLine)?.Name)); } else if (token.Type == PSTokenType.Variable && t.Type == PSTokenType.Variable) { l.Add(new cScriptLocation(Path.GetFileName(FullPath), t.Type, t.StartLine, t.StartColumn, ltxt, $"${token.Content}", mProjects.Project.Functions.GetFunctionsByLine(FullPath, t.StartLine)?.Name)); } else if (token.Type == PSTokenType.String && t.Type == PSTokenType.String) { l.Add(new cScriptLocation(Path.GetFileName(FullPath), t.Type, t.StartLine, t.StartColumn, ltxt, $"\"{token.Content}\"", mProjects.Project.Functions.GetFunctionsByLine(FullPath, t.StartLine)?.Name)); } } return(true); }
//_____________________________________________________________________________________________________________________________________________________________ public static PSToken GetPSTokenInLocation(string txt, PSToken token, int CaretColumn) { LogHelper.Add("GetPSTokenInLocation"); Queue <Token> tokenQueue = new Queue <Token>(); GetTokensFromStringToken(null, txt, tokenQueue, token); foreach (Token t in tokenQueue) { if (t.StartColumn <= CaretColumn && t.EndColumn >= CaretColumn) { return(t.t); } } return(null); }
//_____________________________________________________________________________________________________________________________________________________________ private List <cScriptLocation> GetAllReferenceLocations(PSToken token) { LogHelper.Add("GetAllReferenceLocations"); List <cScriptLocation> l = new List <cScriptLocation>(); ISEFile file = hostObject.CurrentPowerShellTab.Files.SelectedFile; GetReferencesFromScriptText(token, l, file.FullPath, file.Editor.Text); foreach (ISEFile f in hostObject.CurrentPowerShellTab.Files) { if (file.FullPath.iEquals(f.FullPath)) { continue; } GetReferencesFromScriptText(token, l, f.FullPath, f.Editor.Text); } return(l); }
//_____________________________________________________________________________________________________________________________________________________________ public static void GetTokensFromStringToken(PSToken tknToSearch, string txt, Queue <Token> tokenQueue, Token token) { LogHelper.Add("GetTokensFromStringToken"); if (token.Type != PSTokenType.String) { return; } if (tknToSearch != null && !token.Content.ToLower().Contains(tknToSearch.Content.ToLower())) { return; } if (txt.Split('\n')[token.StartLine - 1][token.StartColumn - 1] != '"') { return; } GetVariableTokensFromStringToken(tknToSearch, txt, tokenQueue, token); GetParenthesisTokensFromStringToken(tknToSearch, txt, tokenQueue, token); }
private static Item PSTokenToItem(PSToken psToken) { Func <SymbolId, Item> asItem = pID => new Item((int)pID, psToken.Content); switch (psToken.Type) { case PSTokenType.Number: return(new Item((int)SymbolId.Integer, int.Parse(psToken.Content))); case PSTokenType.Operator: switch (psToken.Content) { case "/": return(asItem(SymbolId.Divide)); case "*": return(asItem(SymbolId.Divide)); case "+": return(asItem(SymbolId.Plus)); case "-": return(asItem(SymbolId.Minus)); } break; case PSTokenType.GroupStart: if (psToken.Content == "(") { return(asItem(SymbolId.LeftParen)); } break; case PSTokenType.GroupEnd: if (psToken.Content == ")") { return(asItem(SymbolId.RightParen)); } break; } throw new ArgumentException("Invalid PS token: " + psToken, "psToken"); }
//_____________________________________________________________________________________________________________________________________________________________ private static void GetVariableTokensFromStringToken(PSToken tknToSearch, string txt, Queue <Token> tokenQueue, Token token) { LogHelper.Add("GetVariableTokensFromStringToken"); if (!token.Content.Contains("$")) { return; } string newStr = Regex.Replace(token.Content, "[^$_:0-9a-zA-Z]", " "); newStr = newStr.Replace("$ ", " "); string pat = @"(\$\w+[:\w]+)\s+"; Regex r = new Regex(pat, RegexOptions.IgnoreCase); Match m = r.Match(newStr); Collection <PSParseError> errors = new Collection <PSParseError>(); while (m.Success) { Group g = m.Groups[1]; CaptureCollection cc = g.Captures; for (int j = 0; j < cc.Count; j++) { Capture c = cc[j]; //System.Console.WriteLine("Capture" + j + "='" + c + "', Position=" + c.Index); ScriptBlock scriptBlock = ScriptBlock.Create(g.ToString()); foreach (PSToken pst in PSParser.Tokenize(new object[] { scriptBlock }, out errors)) { if (pst.Type != PSTokenType.Variable) { continue; } Token t = new Token(pst); t.StartLine += (token.StartLine - 1); t.StartColumn += (token.StartColumn) + c.Index; t.EndColumn += (token.StartColumn) + c.Index; tokenQueue.Enqueue(t); } } m = m.NextMatch(); } }
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 (commandToken != null) * { * var item = new PowershellItem(PowershellItemType.Command, commandToken.Content, commandToken.StartLine, commandToken.StartColumn, nestingLevel, currentItem); * currentItem = item; * commandToken = null; * continue; * } * else if (token.Type == PSTokenType.NewLine) * { * if (currentItem != null && nestingLevel <= currentItem.NestingLevel) * { * currentItem = currentItem.Parent ?? rootItem; * } * } * else if (token.Type == PSTokenType.GroupStart && token.Content.Contains("{")) * { * nestingLevel++; * } * else if (token.Type == PSTokenType.GroupEnd && token.Content.Contains("}")) * { * nestingLevel--; * } * else if (token.Type == PSTokenType.Command) * { * commandToken = token; * }*/ 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 void GetReferences() { LogHelper.Add("GetReferences"); ISEEditor editor = hostObject.CurrentPowerShellTab.Files.SelectedFile.Editor; string caretLineText = editor.CaretLineText; string currentFile = hostObject.CurrentPowerShellTab.Files.SelectedFile.FullPath; Tuple <string, int, int> pos = new Tuple <string, int, int>(currentFile, editor.CaretLine, editor.CaretColumn); List <PSToken> list = PSParser.Tokenize(caretLineText, out Collection <PSParseError> errors).Where(t => t.Type == PSTokenType.Command && t.StartColumn <= editor.CaretColumn && t.EndColumn >= editor.CaretColumn).ToList(); if (list.Count == 0) { list = PSParser.Tokenize(caretLineText, out errors).Where(t => t.Type == PSTokenType.CommandParameter && t.StartColumn <= editor.CaretColumn && t.EndColumn >= editor.CaretColumn).ToList(); } if (list.Count == 0) { list = PSParser.Tokenize(caretLineText, out errors).Where(t => t.Type == PSTokenType.CommandArgument && t.StartColumn <= editor.CaretColumn && t.EndColumn >= editor.CaretColumn).ToList(); } if (list.Count == 0) { list = PSParser.Tokenize(caretLineText, out errors).Where(t => t.StartColumn <= editor.CaretColumn && t.EndColumn >= editor.CaretColumn).ToList(); } if (list.Count == 0) { return; } PSToken tkn = list[0]; List <cScriptLocation> lst = GetAllReferenceLocations(tkn); string title = $"{tkn.Content} ({tkn.Type}) [{lst.Count}]"; if (tkn.Type == PSTokenType.String) // if tkn is a string with less than 2 results, try to searh into the string for additional tokens { PSToken tknsubstr = Token.GetPSTokenInLocation(caretLineText, tkn, editor.CaretColumn); if (tknsubstr != null) { List <cScriptLocation> lstsubstr = GetAllReferenceLocations(tknsubstr); if (lstsubstr.Count > 0) { lst.AddRange(lstsubstr);// is string found more tokens replace the list with the new result title += $" / {tknsubstr.Content} ({tknsubstr.Type}) [{lstsubstr.Count}]"; } } } if (lst.Count == 0) { return; } using (frmReferences f = new frmReferences()) { f.Locations = lst; f.SelectDefaultLocation(Path.GetFileName(currentFile), editor.CaretLine); f.Command = $"References {title}"; f.ShowDialog(new WindowWrapper(WindowWrapper.GetSafeHandle())); if (!f.ReferenceSelected) { return; } cScriptLocation l = f.SelectedLocation; try { hostObject.CurrentPowerShellTab.Files.SetSelectedFile(hostObject.CurrentPowerShellTab.Files.Where(file => Path.GetFileName(file.FullPath).iEquals(l.fileName)).FirstOrDefault()); hostObject.CurrentPowerShellTab.Files.SelectedFile.Editor.SetCaretPosition(l.line, l.position); hostObject.CurrentPowerShellTab.Files.SelectedFile.Editor.Select(l.line, l.position, l.line, l.position + l.word.Length); BackList.Push(pos); } catch (Exception ex) { LogHelper.AddException(ex, "GetReferences", null); } } }
private static bool IsInCertainPSTokenTypesArea(int position, ITextBuffer buffer, EdgeTrackingMode edgeTrackingMode, params PSTokenType[] selectedPSTokenTypes) { if (position < 0 || position > buffer.CurrentSnapshot.Length) { return(false); } Token[] tokens; if (buffer.Properties.TryGetProperty <Token[]>(BufferProperties.Tokens, out tokens) && tokens != null && tokens.Length != 0) { var filteredTokens = tokens.Where(t => selectedPSTokenTypes.Any(k => PSToken.GetPSTokenType(t) == k)).ToList(); switch (edgeTrackingMode) { case EdgeTrackingMode.NoneEdgeIncluded: foreach (var token in filteredTokens) { if (token.Extent.StartOffset < position && position < token.Extent.EndOffset) { return(true); } if (position <= token.Extent.StartOffset) { return(false); } } break; case EdgeTrackingMode.LeftEdgeIncluded: foreach (var token in filteredTokens) { if (token.Extent.StartOffset <= position && position < token.Extent.EndOffset) { return(true); } if (position < token.Extent.StartOffset) { return(false); } } break; case EdgeTrackingMode.RightEdgeIncluded: foreach (var token in filteredTokens) { if (token.Extent.StartOffset < position && position <= token.Extent.EndOffset) { return(true); } if (position < token.Extent.StartOffset) { return(false); } } break; case EdgeTrackingMode.BothEdgesIncluded: foreach (var token in filteredTokens) { if (token.Extent.StartOffset <= position && position <= token.Extent.EndOffset) { return(true); } if (position < token.Extent.StartOffset) { return(false); } } break; default: break; } } return(false); }
internal PSParseError(RuntimeException rte) { this._message = rte.Message; this._psToken = new PSToken(rte.ErrorToken); }
private static bool IsInTokenTypesArea(int position, TextArea textArea, EdgeTrackingMode edgeTrackingMode, params PSTokenType[] selectedPSTokenTypes) { if (position < 0 || position > textArea.Document.Text.Length) { return(false); } var scriptToCaret = string.Empty; Execute.OnUIThread(() => { scriptToCaret = textArea.Document.Text.Substring(0, textArea.Caret.Offset); }); Token[] tokens; ParseError[] errors; System.Management.Automation.Language.Parser.ParseInput(scriptToCaret, out tokens, out errors); if (tokens.Length > 0) { var filteredTokens = tokens.Where(t => selectedPSTokenTypes.Any(k => PSToken.GetPSTokenType(t) == k)).ToList(); switch (edgeTrackingMode) { case EdgeTrackingMode.NoneEdgeIncluded: foreach (var token in filteredTokens) { if (token.Extent.StartOffset < position && position < token.Extent.EndOffset) { return(true); } if (position <= token.Extent.StartOffset) { return(false); } } break; case EdgeTrackingMode.LeftEdgeIncluded: foreach (var token in filteredTokens) { if (token.Extent.StartOffset <= position && position < token.Extent.EndOffset) { return(true); } if (position < token.Extent.StartOffset) { return(false); } } break; case EdgeTrackingMode.RightEdgeIncluded: foreach (var token in filteredTokens) { if (token.Extent.StartOffset < position && position <= token.Extent.EndOffset) { return(true); } if (position < token.Extent.StartOffset) { return(false); } } break; case EdgeTrackingMode.BothEdgesIncluded: foreach (var token in filteredTokens) { if (token.Extent.StartOffset <= position && position <= token.Extent.EndOffset) { return(true); } if (position < token.Extent.StartOffset) { return(false); } } break; } } return(false); }
internal static bool TestToken(this PSToken token, PSTokenType type, params string[] content) { return(type == token.Type && content.Any(c => c.Equals(token.Content, StringComparison.OrdinalIgnoreCase))); }
public static IEnumerable <CommandInfo> GetReferencedCommands(ScriptBlock scriptBlock, PSCmdlet cmdlet) { Dictionary <CommandInfo, bool> resolvedCommandCache = new Dictionary <CommandInfo, bool>(); Queue <PSToken> tokenQueue = new Queue <PSToken>(); Collection <PSParseError> errors; foreach (PSToken token in PSParser.Tokenize(new object[] { scriptBlock }, out errors)) { tokenQueue.Enqueue(token); } if (tokenQueue.Count == 0) { yield return(null); } while (tokenQueue.Count > 0) { PSToken token = tokenQueue.Dequeue(); if (token.Type == PSTokenType.Command) { CommandInfo cmd = null; cmd = cmdlet.SessionState.InvokeCommand.GetCommand(token.Content, CommandTypes.Alias); if (cmd == null) { cmd = cmdlet.SessionState.InvokeCommand.GetCommand(token.Content, CommandTypes.Function); if (cmd == null) { cmd = cmdlet.SessionState.InvokeCommand.GetCommand(token.Content, CommandTypes.Cmdlet); } } else { while (cmd != null && cmd is AliasInfo) { AliasInfo alias = cmd as AliasInfo; if (!resolvedCommandCache.ContainsKey(alias)) { yield return(alias); resolvedCommandCache.Add(alias, true); } cmd = alias.ReferencedCommand; } } if (cmd == null) { continue; } if (cmd is FunctionInfo) { if (!resolvedCommandCache.ContainsKey(cmd)) { FunctionInfo func = cmd as FunctionInfo; yield return(cmd); foreach (PSToken t in PSParser.Tokenize(new object[] { func.ScriptBlock }, out errors)) { tokenQueue.Enqueue(t); } resolvedCommandCache.Add(cmd, true); } } else { if (!resolvedCommandCache.ContainsKey(cmd)) { yield return(cmd); resolvedCommandCache.Add(cmd, true); } } } } }
/// <summary> /// Creates a <see cref = "SnapshotSpan" /> for the given text snapshot and token. /// </summary> /// <param name = "snapshot">The snapshot.</param> /// <param name = "token">The token.</param> /// <returns></returns> protected override SnapshotSpan CreateSnapshotSpan(ITextSnapshot snapshot, PSToken token) { return(new SnapshotSpan(snapshot, new Span(token.Start, token.Length))); }
//_____________________________________________________________________________________________________________________________________________________________ public static void GetTokensFromStringToken(PSToken tknToSearch, string txt, Queue <Token> tokenQueue, PSToken token) { GetTokensFromStringToken(tknToSearch, txt, tokenQueue, new Token(token)); }
public static PSTokenType GetPSTokenType(this Token token) { return(PSToken.GetPSTokenType(token)); }