public bool TryParseLine(VirtualCharSequence line, [NotNullWhen(true)] out ParsedFrame?parsedFrame) { // For now we just keep all text so the user can still see lines they pasted and they // don't disappear. In the future we might want to restrict what we show. parsedFrame = new IgnoredFrame(line); return(true); }
public EmbeddedSyntaxTrivia(TSyntaxKind kind, VirtualCharSequence virtualChars, ImmutableArray <EmbeddedDiagnostic> diagnostics) { Debug.Assert(virtualChars.Length > 0); Kind = kind; VirtualChars = virtualChars; Diagnostics = diagnostics; }
private JsonParser(VirtualCharSequence text) : this() { _lexer = new JsonLexer(text); // Get the first token. ConsumeCurrentToken(); }
public static (ImmutableDictionary <string, TextSpan>, ImmutableDictionary <int, TextSpan>) Analyze( VirtualCharSequence text, RegexCompilationUnit root, RegexOptions options) { var analyzer = new CaptureInfoAnalyzer(text); return(analyzer.Analyze(root, options)); }
protected EmbeddedSyntaxTree( VirtualCharSequence text, TCompilationUnitSyntax root, ImmutableArray <EmbeddedDiagnostic> diagnostics) { Text = text; Root = root; Diagnostics = diagnostics; }
private static string ConvertToString(VirtualCharSequence virtualChars) { using var _ = ArrayBuilder <string> .GetInstance(out var strings); foreach (var ch in virtualChars) { strings.Add(ConvertToString(ch)); } return(string.Join("", strings)); }
public RegexTree( VirtualCharSequence text, RegexCompilationUnit root, ImmutableArray <EmbeddedDiagnostic> diagnostics, ImmutableDictionary <string, TextSpan> captureNamesToSpan, ImmutableDictionary <int, TextSpan> captureNumbersToSpan) : base(text, root, diagnostics) { CaptureNamesToSpan = captureNamesToSpan; CaptureNumbersToSpan = captureNumbersToSpan; }
private CaptureInfoAnalyzer(VirtualCharSequence text) { _text = text; _captureNumberToSpan = ImmutableDictionary.CreateBuilder <int, TextSpan>(); _captureNameToSpan = ImmutableDictionary.CreateBuilder <string, TextSpan>(); _captureNames = ArrayBuilder <string> .GetInstance(); _autoNumber = 1; _captureNumberToSpan.Add(0, text.IsEmpty ? default : GetSpan(text)); }
public static bool CanBeSingleLine(VirtualCharSequence characters) { // Single line raw strings cannot start/end with quote. if (characters.First().Rune.Value == '"' || characters.Last().Rune.Value == '"') { return(false); } // a single line raw string cannot contain a newline. if (characters.Any(static ch => IsNewLine(ch)))
/// <summary> /// Uses <see cref="StackFrameParser"/> to parse a line if possible /// </summary> public bool TryParseLine(VirtualCharSequence line, [NotNullWhen(true)] out ParsedFrame?parsedFrame) { parsedFrame = null; var tree = StackFrameParser.TryParse(line); if (tree is null) { return(false); } parsedFrame = new ParsedStackFrame(tree); return(true); }
/// <summary> /// Checks for errors in json for both json.net and strict mode. /// </summary> private static EmbeddedDiagnostic?CheckTopLevel( VirtualCharSequence text, JsonCompilationUnit compilationUnit) { var sequence = compilationUnit.Sequence; if (sequence.IsEmpty) { // json is not allowed to be just whitespace. // // Note: we always have at least some content (either real nodes in the tree) or trivia on the EOF token // as we only parse when we have a non-empty sequence of virtual chars to begin with. if (text.Length > 0 && compilationUnit.EndOfFileToken.LeadingTrivia.All( t => t.Kind is JsonKind.WhitespaceTrivia or JsonKind.EndOfLineTrivia)) { return(new EmbeddedDiagnostic(FeaturesResources.Syntax_error, GetSpan(text))); } return(null); } else if (sequence.Length >= 2) { // the top level can't have more than one actual value. var firstToken = GetFirstToken(sequence[1]); return(new EmbeddedDiagnostic( string.Format(FeaturesResources._0_unexpected, firstToken.VirtualChars[0]), firstToken.GetSpan())); } else { var child = sequence.Single(); // Commas should never show up in the top level sequence. if (child.Kind == JsonKind.CommaValue) { var emptyValue = (JsonCommaValueNode)child; return(new EmbeddedDiagnostic( string.Format(FeaturesResources._0_unexpected, ','), emptyValue.CommaToken.GetSpan())); } else if (child.Kind == JsonKind.Property) { var propertyValue = (JsonPropertyNode)child; return(new EmbeddedDiagnostic( string.Format(FeaturesResources._0_unexpected, ':'), propertyValue.ColonToken.GetSpan())); } return(CheckSyntax(child)); }
/// <summary> /// Given an input text, parses out a fully representative syntax tree and list of /// diagnostics. Parsing should always succeed, except in the case of the stack /// overflowing. /// </summary> public static JsonTree?TryParse(VirtualCharSequence text, bool strict) { try { if (text.Length == 0) { return(null); } return(new JsonParser(text).ParseTree(strict)); } catch (InsufficientExecutionStackException) { return(null); } }
/// <summary> /// Given an input text, parses out a fully representative syntax tree and list of /// diagnostics. Parsing should always succeed, except in the case of the stack /// overflowing. /// </summary> public static JsonTree?TryParse(VirtualCharSequence text, JsonOptions options) { try { if (text.IsDefaultOrEmpty) { return(null); } return(new JsonParser(text).ParseTree(options)); } catch (InsufficientExecutionStackException) { return(null); } }
public EmbeddedSyntaxToken( TSyntaxKind kind, ImmutableArray <EmbeddedSyntaxTrivia <TSyntaxKind> > leadingTrivia, VirtualCharSequence virtualChars, ImmutableArray <EmbeddedSyntaxTrivia <TSyntaxKind> > trailingTrivia, ImmutableArray <EmbeddedDiagnostic> diagnostics, object value) { Debug.Assert(!leadingTrivia.IsDefault); Debug.Assert(!virtualChars.IsDefault); Debug.Assert(!trailingTrivia.IsDefault); Debug.Assert(!diagnostics.IsDefault); Kind = kind; LeadingTrivia = leadingTrivia; VirtualChars = virtualChars; TrailingTrivia = trailingTrivia; Diagnostics = diagnostics; Value = value; }
public EmbeddedCompletionContext( SourceText text, CompletionContext context, VirtualCharSequence virtualChars, ArrayBuilder <DateAndTimeItem> items) { _items = items; var startPosition = context.Position; while (char.IsLetter(text[startPosition - 1])) { startPosition--; } _replacementSpan = TextSpan.FromBounds(startPosition, context.Position); (_userFormatPrefix, _userFormatSuffix) = GetUserFormatParts(virtualChars, startPosition, context.Position); }
private static IEnumerable <VirtualCharSequence> SplitLines(VirtualCharSequence callstack) { var position = 0; for (var i = 0; i < callstack.Length; i++) { if (callstack[i].Value == '\n') { yield return(callstack.GetSubSequence(TextSpan.FromBounds(position, i))); // +1 to skip over the \n character position = i + 1; } } if (position < callstack.Length) { yield return(callstack.GetSubSequence(TextSpan.FromBounds(position, callstack.Length))); } }
private static (string prefix, string suffix) GetUserFormatParts( VirtualCharSequence virtualChars, int startPosition, int endPosition) { virtualChars = virtualChars.IsDefault ? VirtualCharSequence.Empty : virtualChars; using var _1 = PooledStringBuilder.GetInstance(out var prefix); using var _2 = PooledStringBuilder.GetInstance(out var suffix); foreach (var ch in virtualChars) { if (ch.Span.End <= startPosition) { ch.AppendTo(prefix); } else if (ch.Span.Start >= endPosition) { ch.AppendTo(suffix); } } return(prefix.ToString(), suffix.ToString()); }
private static VirtualCharSequence Trim(VirtualCharSequence virtualChars) { if (virtualChars.Length == 0) { return(virtualChars); } var start = 0; var end = virtualChars.Length - 1; while (virtualChars[start].IsWhiteSpace && start < end) { start++; } while (virtualChars[end].IsWhiteSpace && end > start) { end--; } return(virtualChars.GetSubSequence(TextSpan.FromBounds(start, end + 1))); }
/// <summary> /// Given an input text, and set of options, parses out a fully representative syntax tree /// and list of diagnostics. Parsing should always succeed, except in the case of the stack /// overflowing. /// </summary> public static StackFrameTree?TryParse(VirtualCharSequence text) { if (text.IsDefault) { return(null); } try { var lexer = StackFrameLexer.TryCreate(text); if (!lexer.HasValue) { return(null); } return(new StackFrameParser(lexer.Value).TryParseTree()); } catch (InsufficientExecutionStackException) { return(null); } }
private static ImmutableArray <ParsedFrame> Parse(string callstack, CancellationToken cancellationToken) { using var _ = ArrayBuilder <ParsedFrame> .GetInstance(out var builder); // if the callstack comes from ActivityLog.xml it has been // encoding to be passed over HTTP. This should only decode // specific characters like ">" and "<" to their "normal" // equivalents ">" and "<" so we can parse correctly callstack = WebUtility.HtmlDecode(callstack); var sequence = VirtualCharSequence.Create(0, callstack); foreach (var line in SplitLines(sequence)) { cancellationToken.ThrowIfCancellationRequested(); // For now do the work to removing leading and trailing whitespace. // This keeps behavior we've had, but may not actually be the desired behavior in the long run. // Specifically if we ever want to add a copy feature to copy back contents from a frame var trimmedLine = Trim(line); if (trimmedLine.IsEmpty) { continue; } foreach (var parser in s_parsers) { if (parser.TryParseLine(trimmedLine, out var parsedFrame)) { builder.Add(parsedFrame); break; } } } return(builder.ToImmutable()); }
public bool TryParseLine(VirtualCharSequence line, [NotNullWhen(true)] out ParsedFrame?parsedFrame) { // Example line: // ConsoleApp4.dll!ConsoleApp4.MyClass.ThrowAtOne() Line 19 C# // |--------------------------------| // Symbol data we care about parsedFrame = null; var startPoint = -1; for (var i = 0; i < line.Length; i++) { if (line[i].Value == '!') { // +1 here because we always want to skip the '!' character startPoint = i + 1; break; } } if (startPoint <= 0 || startPoint == line.Length) { return(false); } var textToParse = line.GetSubSequence(TextSpan.FromBounds(startPoint, line.Length)); var tree = StackFrameParser.TryParse(textToParse); if (tree is null) { return(false); } parsedFrame = new ParsedStackFrame(tree); return(true); }
public CanConvertParams(VirtualCharSequence characters, bool canBeSingleLine, bool canBeMultiLineWithoutLeadingWhiteSpaces) { Characters = characters; CanBeSingleLine = canBeSingleLine; CanBeMultiLineWithoutLeadingWhiteSpaces = canBeMultiLineWithoutLeadingWhiteSpaces; }
public JsonTree( VirtualCharSequence text, JsonCompilationUnit root, ImmutableArray <EmbeddedDiagnostic> diagnostics) : base(text, root, diagnostics) { }
private StackFrameLexer(VirtualCharSequence text) : this() => Text = text;
public IgnoredFrame(VirtualCharSequence originalText) { _originalText = originalText; }
public RegexLexer(VirtualCharSequence text) : this() { Text = text; }
public static RegexToken CreateToken(RegexKind kind, ImmutableArray <RegexTrivia> leadingTrivia, VirtualCharSequence virtualChars) => new(kind, leadingTrivia, virtualChars, ImmutableArray <RegexTrivia> .Empty, ImmutableArray <EmbeddedDiagnostic> .Empty, value : null);
public static TextSpan GetSpan(VirtualCharSequence virtualChars) => GetSpan(virtualChars[0], virtualChars.Last());
public static RegexTrivia CreateTrivia(RegexKind kind, VirtualCharSequence virtualChars, ImmutableArray <EmbeddedDiagnostic> diagnostics) => new RegexTrivia(kind, virtualChars, diagnostics);
public Enumerator(VirtualCharSequence virtualCharSequence) { _virtualCharSequence = virtualCharSequence; _position = -1; }