private StatementSyntax ParseReturnStatement() { var keyword = MatchToken(SyntaxKind.ReturnKeyword); var keywordLine = _text.GetLineIndex(keyword.Span.Start); var currentLine = _text.GetLineIndex(Current.Span.Start); var isEof = Current.Kind == SyntaxKind.EndOfFileToken; var sameLine = !isEof && keywordLine == currentLine; var expression = sameLine ? ParseExpression() : null; return(new ReturnStatementSyntax(keyword, expression)); }
private static void WriteDiagnostics(SourceText sourceText, ImmutableArray <Diagnostic> diagnostics) { Console.WriteLine(); foreach (var diagnostic in diagnostics) { var lineIndex = sourceText.GetLineIndex(diagnostic.Span.Start); var line = sourceText.Lines[lineIndex]; var columnNumber = diagnostic.Span.Start - line.Start + 1; var prefixSpan = TextSpan.FromBounds(line.Start, diagnostic.Span.Start); var suffixSpan = TextSpan.FromBounds(diagnostic.Span.End, line.End); var prefix = sourceText.ToString(prefixSpan.Start, prefixSpan.Length); var error = sourceText.ToString(diagnostic.Span.Start, diagnostic.Span.Length); var suffix = sourceText.ToString(suffixSpan.Start, suffixSpan.Length); Console.ForegroundColor = ConsoleColor.DarkRed; Console.Write($"({lineIndex + 1}, {columnNumber}): "); Console.WriteLine(diagnostic); Console.ResetColor(); Console.Write(" "); Console.Write(prefix); Console.ForegroundColor = ConsoleColor.DarkRed; Console.Write(error); Console.ResetColor(); Console.WriteLine(suffix); } Console.WriteLine(); }
public void Calculates_LineIndex_Correctly(int position, int expectedLine) { var input = "One\nTwo\r\nThree"; var sourceText = new SourceText(input); Assert.That(sourceText.GetLineIndex(position), Is.EqualTo(expectedLine)); }
private static void PrintErrors(IEnumerable <Error> errors, SourceText text) { foreach (var error in errors) { // Stuff for line numbers var lineindex = text.GetLineIndex(error.Span.Start); var linenumber = lineindex + 1; var line = text.Lines[lineindex]; var character = error.Span.Start - line.Start + 1; // Print the message Console.ForegroundColor = ConsoleColor.Red; Console.Write($"({linenumber}, {character}): {error}"); Console.ResetColor(); // Prevent it from trying to highlight an empty token if (error.Details.Contains("Unexpected token <EOFToken>")) { Console.WriteLine(); break; } var prefixspan = TextSpan.FromBounds(line.Start, error.Span.Start); var suffixspan = TextSpan.FromBounds(error.Span.End, line.End); var prefix = text.ToString(prefixspan); var occurrence = text.ToString(error.Span); var suffix = text.ToString(suffixspan); // Print what is before the error Console.WriteLine(" "); Console.Write(prefix); // Print where the error occurs Console.ForegroundColor = ConsoleColor.DarkRed; Console.Write(occurrence); Console.ResetColor(); // Print what is after the error Console.Write(suffix); Console.WriteLine(); // Print arrow Console.ForegroundColor = ConsoleColor.DarkRed; for (int _ = 0; _ < prefix.Length; _++) { Console.Write(" "); } for (int _ = 0; _ < occurrence.Length; _++) { Console.Write("^"); } Console.WriteLine(); Console.ResetColor(); } }
protected override void EvaluateSubmission(string text) { SyntaxTree syntaxTree = SyntaxTree.Parse(text); Compilation compilation = previous == null ? new Compilation(syntaxTree) : previous.ContinueWith(syntaxTree); if (showTree) { syntaxTree.Root.WriteTo(Console.Out); } if (showProgram) { compilation.EmitTree(Console.Out); } EvaluationResult result = compilation.Evaluate(variables); IReadOnlyList <Diagnostic> diagnostics = result.Diagnostics; if (!diagnostics.Any()) { if (result.Value != null) { Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(result.Value); Console.ResetColor(); } previous = compilation; } else { foreach (Diagnostic diagnostic in diagnostics.OrderBy(d => d.Span, new TextSpanComparer())) { SourceText treeText = syntaxTree.Text; int lineIndex = treeText.GetLineIndex(diagnostic.Span.Start); var line = treeText.Lines[lineIndex]; int lineNumber = lineIndex + 1; int character = diagnostic.Span.Start - line.Start + 1; Console.WriteLine(); Console.ForegroundColor = ConsoleColor.DarkRed; Console.Write($"({lineNumber}, {character}): "); Console.WriteLine(diagnostic); Console.ResetColor(); TextSpan prefixSpan = TextSpan.FromBounds(line.Start, diagnostic.Span.Start); TextSpan suffixSpan = TextSpan.FromBounds(diagnostic.Span.End, line.End); string prefix = treeText.ToString(prefixSpan); string error = treeText.ToString(diagnostic.Span); string suffix = treeText.ToString(suffixSpan); Console.Write($" {prefix}"); Console.ForegroundColor = ConsoleColor.DarkRed; Console.Write(error); Console.ResetColor(); Console.Write(suffix); Console.WriteLine(); } Console.WriteLine(); } }