lib/ropes_debug: Adapted new leaves for to_dot operation.
[nit.git] / lib / ropes_debug.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 # Copyright 2006-2008 Floréal Morandat <morandat@lirmm.fr>
5 #
6 # This file is free software, which comes along with NIT. This software is
7 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
8 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
9 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
10 # is kept unaltered, and a notification of the changes is added.
11 # You are allowed to redistribute it and sell it, alone or is a part of
12 # another product.
13
14 # Exposes methods for debugging ropes when needed.
15 module ropes_debug
16
17 intrude import ::standard::ropes
18 import ::standard
19
20 redef class Rope
21 # Writes self as a dot file on the hard drive
22 fun to_dot(filepath: String): String is abstract
23 end
24
25 redef class RopeNode
26 # Generates a dot string
27 fun to_dot(s: String): String is abstract
28 end
29
30 redef class Leaf
31 redef fun to_dot(s): String
32 do
33 s += "n{object_id} [label = \"{str}\" shape = rect];\n"
34 s += "n{str.object_id} -> n{object_id} [label = \"contains\"];\n"
35 s = str.to_dot(s)
36 return s
37 end
38 end
39
40 redef class Concat
41 redef fun to_dot(s): String
42 do
43 s += "n{object_id} [label = {length}];\n"
44 if left != null then
45 s += "n{object_id} -> n{left.object_id} [label = \"left\"];\n"
46 s = left.to_dot(s)
47 end
48 if right != null then
49 s += "n{object_id} -> n{right.object_id} [label = \"right\"];\n"
50 s = right.to_dot(s)
51 end
52 return s
53 end
54 end
55
56 redef class FlatText
57 fun to_dot(s: String): String is abstract
58 end
59
60 redef class FlatString
61 redef fun to_dot(s: String): String
62 do
63 return s + "n{object_id} [label=\"FlatString\\nindex_from = {index_from}\\nindex_to = {index_to}\\nNativeString = {items.to_s_with_length(items.cstring_length)}\"];\n"
64 end
65 end
66
67 redef class FlatBuffer
68 redef fun to_dot(s: String): String
69 do
70 return s + "n{object_id} [label=\"FlatBuffer\\length = {length}\\ncapacity = {capacity}\\nitems = {items.to_s_with_length(items.cstring_length)}\"];\n"
71 end
72 end
73
74 redef class RopeString
75 redef fun to_dot(filepath: String)
76 do
77 var of = new OFStream.open(filepath)
78 var ret: String = new RopeString.from("digraph g \{\n")
79 ret = root.to_dot(ret).as(RopeString)
80 ret += "\}\n"
81 ret.write_to(of)
82 of.close
83 return ret
84 end
85 end
86
87