Пример #1
0
      private void FormatNodes(Graph graph, ITree tree, IList<string> parserRules)
      {
         var node = graph.FindNode(tree.GetHashCode().ToString());
         if (node != null)
         {
            node.LabelText = Trees.GetNodeText(tree, parserRules);

            var ruleFailedAndMatchedNothing = false;

            if (tree is ParserRuleContext context)
               ruleFailedAndMatchedNothing =
                  // ReSharper disable once ComplexConditionExpression
                  context.exception != null &&
                  context.stop != null
                  && context.stop.TokenIndex < context.start.TokenIndex;

            if (tree is IErrorNode || ruleFailedAndMatchedNothing)
               node.Label.FontColor = Color.Red;
            else
               node.Label.FontColor = TextColor ?? Color.Black;

            node.Attr.Color = BorderColor ?? Color.Black;

            if (BackgroundColor.HasValue)
               node.Attr.FillColor = BackgroundColor.Value;

            node.Attr.Color = BorderColor ?? Color.Black;

            node.UserData = tree;
         }

         for (int i = 0; i < tree.ChildCount; i++)
            FormatNodes(graph, tree.GetChild(i), parserRules);
      }
Пример #2
0
        public static void PrintTree(this IParseTree tree, TextWriter writer, Parser parser, string indent = "", bool isLastChild = true)
        {
            writer.Write(indent + (isLastChild ? "└──" : "├──") + " ");

            SetOutputColor(writer, tree);
            writer.WriteLine(Trees.GetNodeText(tree, parser));
            ResetOutputColor(writer);

            for (int i = 0; i < tree.ChildCount; i++)
            {
                PrintTree(tree.GetChild(i), writer, parser, indent + (isLastChild ? "    " : "│   "), i == tree.ChildCount - 1);
            }
        }
Пример #3
0
        public void Execute(Repl repl, ReplParser.DotContext tree, bool piped)
        {
            string lines            = repl.input_output_stack.Pop();
            var    serializeOptions = new JsonSerializerOptions();

            serializeOptions.Converters.Add(new AntlrJson.ParseTreeConverter());
            serializeOptions.WriteIndented = false;
            var           parse_info = JsonSerializer.Deserialize <AntlrJson.ParsingResultSet>(lines, serializeOptions);
            var           nodes      = parse_info.Nodes;
            StringBuilder sb         = new StringBuilder();

            sb.AppendLine("digraph G {");
            Stack <IParseTree> stack = new Stack <IParseTree>();

            foreach (var node in nodes)
            {
                stack.Push(node);
                while (stack.Any())
                {
                    var t = stack.Pop();
                    sb.AppendLine("Node" + t.GetHashCode().ToString()
                                  + " [label=\""
                                  + LanguageServer.TreeOutput.PerformEscapes(Trees.GetNodeText(t, parse_info.Parser.RuleNames))
                                  + "\"];");
                    for (int i = t.ChildCount - 1; i >= 0; --i)
                    {
                        var c = t.GetChild(i);
                        stack.Push(c);
                    }
                }
            }
            foreach (var node in nodes)
            {
                stack.Push(node);
                while (stack.Any())
                {
                    var t = stack.Pop();
                    for (int i = 0; i < t.ChildCount; ++i)
                    {
                        var c = t.GetChild(i);
                        sb.AppendLine("Node" + t.GetHashCode().ToString()
                                      + " -> "
                                      + "Node" + c.GetHashCode().ToString()
                                      + ";");
                        stack.Push(c);
                    }
                }
            }
            sb.AppendLine("}");
            repl.input_output_stack.Push(sb.ToString());
        }
Пример #4
0
        private static void ToIndentedRecursive(IParseTree node, StringBuilder buf, int offset, List <string> ruleNames)
        {
            for (int i = 0; i < offset; i++)
            {
                buf.Append("  ");
            }

            buf.Append(Trees.GetNodeText(node, ruleNames)).Append('\n');

            if (node is ParserRuleContext prc)
            {
                if (prc.children != null)
                {
                    foreach (IParseTree child in prc.children)
                    {
                        ToIndentedRecursive(child, buf, offset + 1, ruleNames);
                    }
                }
            }
        }
Пример #5
0
        private static String process(ITree t, List <String> ruleNames, int baseLevel)
        {
            if (t.ChildCount == 0)
            {
                return(Utils.EscapeWhitespace(Trees.GetNodeText(t, ruleNames), false));
            }
            StringBuilder sb = new StringBuilder();

            sb.Append(lead(level, baseLevel));
            level++;
            String s = Utils.EscapeWhitespace(Trees.GetNodeText(t, ruleNames), false);

            sb.Append(s + ' ');
            for (int i = 0; i < t.ChildCount; i++)
            {
                var node = process(t.GetChild(i), ruleNames, level);
                sb.Append(node);
            }
            level--;

            sb.Append(lead(level, level));
            return(sb.ToString());
        }