private IList <IToken> getTokens(string text) { IList <IToken> tokens; try { var reporter = new ErrorIgnorer(); ITokenStream tokenStream; bool ok = XSharp.Parser.VsParser.Lex(text, _file.SourcePath, _parseoptions, reporter, out tokenStream); var stream = tokenStream as BufferedTokenStream; tokens = stream.GetTokens(); } catch (Exception e) { XSharpProjectPackage.Instance.DisplayException(e); tokens = new List <IToken>(); } return(tokens); }
private IList <IToken> getTokens(string text) { IList <IToken> tokens; try { string fileName; fileName = "MissingFile.prg"; var reporter = new ErrorIgnorer(); bool ok = XSharp.Parser.VsParser.Lex(text, fileName, XSharpParseOptions.Default, reporter, out ITokenStream tokenStream); var stream = tokenStream as BufferedTokenStream; tokens = stream.GetTokens(); } catch (Exception e) { XSettings.LogException(e, "getTokens"); tokens = new List <IToken>(); } return(tokens); }
private void addtokens(ITextBuffer buffer, int iLine, IList <string> list, XFile file, IDictionary <string, IXVariableSymbol> locals) { if (iLine <= 0) { return; } iLine -= 1; string slines = ""; // parse the expression on three lines. // we don't have to worry if it can be translated or not // the expression compiler in the debugger will simply ignore incorrect expressions // we use the locals array to make sure the case of the locals and parameters is correct int start = Math.Max(iLine - 1, 0); int end = Math.Min(iLine + 1, buffer.CurrentSnapshot.LineCount - 1); for (int i = start; i <= end; i++) { var line = buffer.CurrentSnapshot.GetLineFromLineNumber(i); slines += line.GetText() + "\r\n"; } var reporter = new ErrorIgnorer(); ITokenStream tokenStream; bool ok = XSharp.Parser.VsParser.Lex(slines, file.FullPath, file.Project.ParseOptions, reporter, out tokenStream); var stream = tokenStream as BufferedTokenStream; var tokens = stream.GetTokens(); string expression = ""; foreach (var token in tokens) { var type = token.Type; if (type == XSharpLexer.ID) { if (expression.Length == 0) { expression = token.Text; if (locals != null && locals.ContainsKey(expression)) { expression = locals[expression].Name; } } else { expression += token.Text; } if (!list.Contains(expression)) { list.Add(expression); } } else if (type == XSharpLexer.COLON || type == XSharpLexer.DOT) { expression += token.Text; } else if (type == XSharpLexer.SELF) { expression += token.Text; } else if (type == XSharpLexer.SUPER) { expression += token.Text; } else { expression = ""; } } }
public IEnumerable <ITagSpan <TextMarkerTag> > GetTags(NormalizedSnapshotSpanCollection spans) { if (spans.Count == 0) //there is no content in the buffer { yield break; } //don't do anything if the current SnapshotPoint is not initialized or at the end of the buffer if (!CurrentChar.HasValue || CurrentChar.Value.Position >= CurrentChar.Value.Snapshot.Length) { yield break; } //hold on to a snapshot of the current character SnapshotPoint currentChar = CurrentChar.Value; //if the requested snapshot isn't the same as the one the brace is on, translate our spans to the expected snapshot if (spans[0].Snapshot != currentChar.Snapshot) { //currentChar = currentChar.TranslateTo(spans[0].Snapshot, PointTrackingMode.Positive); yield break; } //get the current char and the previous char char currentText = '\0'; char lastText = '\0'; SnapshotSpan pairSpan = new SnapshotSpan(); SnapshotPoint lastChar = new SnapshotPoint(); try { currentText = currentChar.GetChar(); lastChar = currentChar == 0 ? currentChar : currentChar - 1; //if currentChar is 0 (beginning of buffer), don't move it back lastText = lastChar.GetChar(); } catch (Exception) { } // use the tokens stored in the buffer properties XSharpTokens xTokens = null; IList <IToken> tokens = null; int offset = 0; if (SourceBuffer.Properties.ContainsProperty(typeof(XSharpTokens))) { xTokens = SourceBuffer.Properties.GetProperty <XSharpTokens>(typeof(XSharpTokens)); if (xTokens.SnapShot.Version != currentChar.Snapshot.Version) { // get source from the start of the file until the current entity var xfile = SourceBuffer.GetFile(); var member = XSharpTokenTools.FindMemberAtPosition(currentChar.Position, xfile); if (member != null) { try { offset = member.Interval.Start; var length = member.Interval.Width; if (offset + length > currentChar.Snapshot.Length) { length = currentChar.Snapshot.Length - offset; } // string text = currentChar.Snapshot.GetText(offset, length); var reporter = new ErrorIgnorer(); ITokenStream tokenStream; XSharpParseOptions parseoptions; var prj = xfile.Project.ProjectNode; parseoptions = prj.ParseOptions; bool ok = XSharp.Parser.VsParser.Lex(text, xfile.FullPath, parseoptions, reporter, out tokenStream); var bstream = tokenStream as BufferedTokenStream; tokens = bstream.GetTokens(); } catch { // if it crashes, that might be because the snapshot used for the Lex/Parse is no more // so, we may have a too much difference yield break; } } } else { tokens = xTokens.TokenStream.GetTokens(); } } // First, try to match Simple chars if (m_braceList.ContainsKey(currentText)) //the key is the open brace { char closeChar; m_braceList.TryGetValue(currentText, out closeChar); if (BraceMatchingTagger.FindMatchingCloseChar(currentChar, currentText, closeChar, out pairSpan, tokens, offset) == true) { yield return(new TagSpan <TextMarkerTag>(new SnapshotSpan(currentChar, 1), new TextMarkerTag("blue"))); yield return(new TagSpan <TextMarkerTag>(pairSpan, new TextMarkerTag("blue"))); } } else if (m_braceList.ContainsValue(lastText)) //the value is the close brace, which is the *previous* character { var open = from n in m_braceList where n.Value.Equals(lastText) select n.Key; if (BraceMatchingTagger.FindMatchingOpenChar(lastChar, (char)open.ElementAt <char>(0), lastText, out pairSpan, tokens, offset) == true) { yield return(new TagSpan <TextMarkerTag>(new SnapshotSpan(lastChar, 1), new TextMarkerTag("blue"))); yield return(new TagSpan <TextMarkerTag>(pairSpan, new TextMarkerTag("blue"))); } } else { // Second, try to Match Keywords // Try to retrieve an already parsed list of Tags XSharpClassifier xsClassifier = null; if (SourceBuffer.Properties.ContainsProperty(typeof(XSharpClassifier))) { xsClassifier = SourceBuffer.Properties[typeof(XSharpClassifier)] as XSharpClassifier; } if (xsClassifier != null) { ITextSnapshot snapshot = xsClassifier.Snapshot; if (snapshot.Version != currentChar.Snapshot.Version) { yield break; } SnapshotSpan Span = new SnapshotSpan(snapshot, 0, snapshot.Length); var classifications = xsClassifier.GetTags(); // We cannot use SortedList, because we may have several Classification that start at the same position List <ClassificationSpan> sortedTags = new List <ClassificationSpan>(); foreach (var tag in classifications) { // Only keep the Brace matching Tags if ((tag.ClassificationType.IsOfType(ColorizerConstants.XSharpBraceOpenFormat)) || (tag.ClassificationType.IsOfType(ColorizerConstants.XSharpBraceCloseFormat))) { sortedTags.Add(tag); } } sortedTags.Sort((a, b) => a.Span.Start.Position.CompareTo(b.Span.Start.Position)); // int indexTag = sortedTags.FindIndex(x => currentChar.Position >= x.Span.Start.Position && currentChar.Position <= x.Span.End.Position); if (indexTag != -1) { var currentTag = sortedTags[indexTag]; if (currentTag.ClassificationType.IsOfType(ColorizerConstants.XSharpBraceOpenFormat)) { if (FindMatchingCloseTag(sortedTags, indexTag, snapshot, out pairSpan)) { var span = currentTag.Span; yield return(new TagSpan <TextMarkerTag>(span, new TextMarkerTag("bracehighlight"))); yield return(new TagSpan <TextMarkerTag>(pairSpan, new TextMarkerTag("bracehighlight"))); } } else { if (FindMatchingOpenTag(sortedTags, indexTag, snapshot, out pairSpan)) { var span = currentTag.Span; yield return(new TagSpan <TextMarkerTag>(pairSpan, new TextMarkerTag("bracehighlight"))); yield return(new TagSpan <TextMarkerTag>(span, new TextMarkerTag("bracehighlight"))); } } } } } }