private static string ScanHexNumber(SlidingTextWindow window) { var buffer = new StringBuilder(); while (true) { if (window.IsAtEnd()) { return(buffer.ToString()); } char current = window.Peek(); if (!IsHexDigit(current)) { return(buffer.ToString()); } buffer.Append(current); window.Advance(); } }
private SyntaxTrivia ScanWhitespace() { textWindow.Reset(); while (!textWindow.IsAtEnd()) { var nextChar = textWindow.Peek(); switch (nextChar) { case ' ': case '\t': textWindow.Advance(); continue; } break; } return(new SyntaxTrivia(SyntaxTriviaType.Whitespace, textWindow.GetSpan(), textWindow.GetText())); }
private SyntaxTrivia ScanDisableNextLineDiagnosticsDirective() { textWindow.Reset(); textWindow.Advance(LanguageConstants.DisableNextLineDiagnosticsKeyword.Length + 1); // Length of disable next statement plus # var span = textWindow.GetSpan(); int start = span.Position; int length = span.Length; StringBuilder sb = new StringBuilder(); sb.Append(textWindow.GetText()); textWindow.Reset(); List <Token> codes = new(); while (!textWindow.IsAtEnd()) { var nextChar = textWindow.Peek(); if (IsIdentifierContinuation(nextChar) || nextChar == '-') { switch (textWindow.Peek(1)) { case ' ': case '\t': case '\n': case '\r': case char.MaxValue: textWindow.Advance(); if (GetToken() is { } token) { codes.Add(token); length += token.Span.Length; sb.Append(token.Text); continue; } break; default: textWindow.Advance(); break; } } else if (nextChar == ' ' || nextChar == '\t') { textWindow.Advance(); sb.Append(nextChar); length++; textWindow.Reset(); } else { break; } } if (codes.Count == 0) { AddDiagnostic(b => b.MissingDiagnosticCodes()); } return(GetDisableNextLineDiagnosticsSyntaxTrivia(codes, start, length, sb.ToString())); }
private SyntaxTrivia ScanDisableNextLineDiagnosticsDirective() { textWindow.Reset(); textWindow.Advance(LanguageConstants.DisableNextLineDiagnosticsKeyword.Length + 1); // Length of disable next statement plus # var span = textWindow.GetSpan(); int start = span.Position; int end = span.GetEndPosition(); StringBuilder sb = new StringBuilder(); sb.Append(textWindow.GetText()); textWindow.Reset(); List <Token> codes = new(); while (!textWindow.IsAtEnd()) { var nextChar = textWindow.Peek(); if (IsNewLine(nextChar)) { break; } else if (IsIdentifierContinuation(nextChar) || nextChar == '-') { switch (textWindow.Peek(1)) { case ' ': case '\t': case '\n': case '\r': case char.MaxValue: textWindow.Advance(); if (GetToken() is { } token) { codes.Add(token); end += token.Span.Length; sb.Append(token.Text); continue; } break; default: textWindow.Advance(); break; } } else if (nextChar == ' ' || nextChar == '\t') { textWindow.Advance(); sb.Append(nextChar); end++; textWindow.Reset(); } else { // Handle scenario where nextChar is not one of the following: identifier, '-', space, tab // Eg: '|' in #disable-next-line BCP037| if (GetToken() is { } token) { codes.Add(token); end += token.Span.Length; sb.Append(token.Text); } break; } } if (codes.Count == 0) { AddDiagnostic(b => b.MissingDiagnosticCodes()); } return(GetDisableNextLineDiagnosticsSyntaxTrivia(codes, start, end, sb.ToString())); }