Ejemplo n.º 1
0
        private void PrettyPrint(TextWriter writer, SyntaxNode node, string indent = "", bool isLast = true)
        {
            var marker = isLast ? "└──" : "├──";

            var token = node as SyntaxToken;

            writer.ColorWrite(indent, ConsoleColor.White);
            writer.ColorWrite(marker, ConsoleColor.White);
            if (token is null)
            {
                writer.ColorWrite(node.Kind, ConsoleColor.White);
            }
            else
            {
                writer.ColorWrite(token.TokenKind);
                var colorized = ColorizedText.ColorizeToken(token, null);
                writer.ColorWrite(" ");
                writer.ColorWrite(token.Location.ToString(), colorized.Color);
            }
            writer.WriteLine();
            indent += isLast ? "    " : "│   ";
            var lastChild = node.GetChildren().LastOrDefault();

            foreach (var child in node.GetChildren())
            {
                PrettyPrint(writer, child, indent, child == lastChild);
            }
        }
Ejemplo n.º 2
0
        public static void WriteDiagnostic(this TextWriter writer, Diagnostic diagnostic)
        {
            var reportColor = diagnostic.Level == ErrorLevel.Error ? ConsoleColor.Red : ConsoleColor.Yellow;
            var errType     = diagnostic.Level == ErrorLevel.Error ? "Error" : "Warning";

            if (diagnostic.HasPositon)
            {
                var lineStart   = diagnostic.Location.StartLine;
                var lineEnd     = diagnostic.Location.EndLine;
                var columnStart = diagnostic.Location.StartCharacter + 1;
                var columnEnd   = diagnostic.Location.EndCharacter + 1;
                var file        = diagnostic.Location.Text.File ?? "<string>";

                writer.WriteLine();
                writer.ColorWrite($"{errType} in {file} line [{lineStart},{lineEnd}] column [{columnStart},{columnEnd}]: {diagnostic.Message}", reportColor);
                writer.WriteLine();
            }
            else
            {
                writer.ColorWrite($"{errType}: {diagnostic.Message}\n", ConsoleColor.Red);
            }
        }
Ejemplo n.º 3
0
        public void WriteControlFlowGraph(TextWriter writer, string functionName)
        {
            if (Diagnostics.HasErrors)
            {
                return;
            }
            var symbols = program.Functions.Keys.Where(s => s.Name == functionName);

            if (!symbols.Any())
            {
                writer.ColorWrite($"The function {functionName} does not exist.", ConsoleColor.Red);
            }
            var cfg = ControlFlowGraph.Create(program.Functions[symbols.First()]);

            cfg.WriteTo(writer);
        }
Ejemplo n.º 4
0
 public void WriteBoundTree(TextWriter writer, string?functionName = null)
 {
     if (functionName is null)
     {
         writer.WriteBoundNode(program);
     }
     else
     {
         var symbols = program.Functions.Keys.Where(s => s.Name == functionName);
         if (!symbols.Any())
         {
             writer.ColorWrite($"The function {functionName} does not exist.", ConsoleColor.Red);
         }
         else
         {
             writer.WriteBoundNode(program.Functions[symbols.First()]);
             writer.WriteLine();
         }
     }
 }