-
Notifications
You must be signed in to change notification settings - Fork 17
/
dot.jl
146 lines (134 loc) · 4.49 KB
/
dot.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# DOT Language
# ============
function nfa2dot(nfa::NFA)
out = IOBuffer()
println(out, "digraph {")
println(out, " graph [ rankdir = LR ];")
println(out, " 0 -> 1;")
println(out, " 0 [ shape = point ];")
serials = Dict(s => i for (i, s) in enumerate(traverse(nfa.start)))
for s in keys(serials)
println(out, " $(serials[s]) [ shape = $(s == nfa.final ? "doublecircle" : "circle") ];")
for (e, t) in s.edges
println(out, " $(serials[s]) -> $(serials[t]) [ label = \"$(edge2str(e))\" ];")
end
end
println(out, "}")
return String(take!(out))
end
function dfa2dot(dfa::DFA)
out = IOBuffer()
println(out, "digraph {")
println(out, " graph [ rankdir = LR ];")
println(out, " start -> 1;")
println(out, " start [ shape = point ];")
serials = Dict(s => i for (i, s) in enumerate(traverse(dfa.start)))
for s in keys(serials)
println(out, " $(serials[s]) [ shape = $(s.final ? "doublecircle" : "circle") ];")
for (e, t) in s.edges
println(out, " $(serials[s]) -> $(serials[t]) [ label = \"$(edge2str(e))\" ];")
end
if !isempty(s.eof_actions)
println(out, " eof$(serials[s]) [ shape = point ];")
println(out, " $(serials[s]) -> eof$(serials[s]) [ label = \"$(eof_label(s.eof_actions))\", style = dashed ];")
end
end
println(out, "}")
return String(take!(out))
end
"""
machine2dot(machine::Machine)::String
Return a String with a flowchart of the machine in Graphviz (dot) format.
Using `Graphviz`, a command-line tool, the dot file can be converted to various
picture formats.
# Example
```julia
open("/tmp/machine.dot", "w") do io
println(io, machine2dot(machine))
end
# Requires graphviz to be installed
run(pipeline(`dot -Tsvg /tmp/machine.dot`), stdout="/tmp/machine.svg")
```
"""
function machine2dot(machine::Machine)
out = IOBuffer()
println(out, "digraph {")
println(out, " graph [ rankdir = LR ];")
println(out, " start -> 1;")
println(out, " start [ shape = point ];")
for s in traverse(machine.start)
println(out, " $(s.state) [ shape = $(s.state ∈ machine.final_states ? "doublecircle" : "circle") ];")
for (e, t) in s.edges
println(out, " $(s.state) -> $(t.state) [ label = \"$(edge2str(e))\" ];")
end
if haskey(machine.eof_actions, s.state) && !isempty(machine.eof_actions[s.state])
println(out, " eof$(s.state) [ shape = point ];")
println(out, " $(s.state) -> eof$(s.state) [ label = \"$(eof_label(machine.eof_actions[s.state]))\", style = dashed ];")
end
end
println(out, "}")
return String(take!(out))
end
function edge2str(edge::Edge)
out = IOBuffer()
function printbyte(b, inrange)
# TODO: does this work?
if inrange && b == UInt8('-')
print(out, "\\\\-")
elseif inrange && b == UInt8(']')
print(out, "\\\\]")
else
s = if b ≤ 0x7f
escape_string(string(Char(b)))
else
"\\x" * string(b; base=16)
end
print(out, escape_string(s))
end
end
# output labels
if isempty(edge.labels)
print(out, 'ϵ')
elseif length(edge.labels) == 1
print(out, '\'')
printbyte(first(edge.labels), false)
print(out, '\'')
else
print(out, '[')
for r in range_encode(edge.labels)
if length(r) == 1
printbyte(first(r), true)
elseif length(r) == 2
printbyte(first(r), true)
printbyte(last(r), true)
else
@assert length(r) > 1
printbyte(first(r), true)
print(out, '-')
printbyte(last(r), true)
end
end
print(out, ']')
end
# output conditions
if !isempty(edge.precond)
print(out, '(')
join(out, ((value == NONE ? "false" : value == TRUE ? name : value == FALSE ? string("!", name) : "true") for (name, value) in edge.precond), ',')
print(out, ')')
end
# output actions
if !isempty(edge.actions)
print(out, '/')
join(out, action_names(edge.actions), ',')
end
return String(take!(out))
end
function eof_label(actions::ActionList)
out = IOBuffer()
print(out, "EOF")
if !isempty(actions)
print(out, '/')
join(out, action_names(actions), ',')
end
return String(take!(out))
end