コード例 #1
0
 public static void SaveStatesLogToFile(this RulesTree.ParsingState state, string fileName)
 {
     using (var stream = File.OpenWrite(fileName))
     {
         stream.SetLength(0);
         new XmlSerializer(typeof(Dgml.DirectedGraph)).Serialize(stream, state.BuildGraph().ToDgml());
     }
 }
コード例 #2
0
        private static Graph BuildGraph(this RulesTree.ParsingState state)
        {
            var g = new Graph();

            var nodes  = new Dictionary <RulesTree.ParsingState, XmlGraphNode>();
            var states = new Stack <RulesTree.ParsingState>();

            for (var s = state; s != null; s = s.PrevState)
            {
                states.Push(s);
            }

            var index = 0;

            while (states.Count > 0)
            {
                var s    = states.Pop();
                var node = g.CreateNode(index.ToString());
                node.Text = new
                {
                    index,
                    expr = s.CurrentExpr == null ? "<NULL>" : (s.CurrentExpr.GetType().Name + ": " + s.CurrentExpr.ToString()),
                    s.InvocationCount,
                    s.LastMatchSuccessed,
                    s.Pos,
                    currChar = s.Pos < s.Text.Length ? s.Text[s.Pos].ToString() : "<OOT>",
                    prev     = s.PrevState == null ? "<NULL>" : nodes[s.PrevState].Id,
                    parent   = s.Parent == null ? "<NULL>" : nodes[s.Parent].Id,
                }.ToString();

                nodes.Add(s, node);

                if (s.PrevState != null)
                {
                    node.ConnectTo(nodes[s.PrevState]);
                }

                if (s.Parent != null)
                {
                    node.ConnectTo(nodes[s.Parent]);
                }

                index++;
            }

            return(g);
        }