-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGraph.lua
More file actions
88 lines (78 loc) · 2.29 KB
/
Graph.lua
File metadata and controls
88 lines (78 loc) · 2.29 KB
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
--
-- lua-CodeGen : <http://fperrad.github.com/lua-CodeGen>
--
local pairs = pairs
local type = type
local _G = _G
local table = require 'table'
local CodeGen = require 'CodeGen'
_ENV = nil
local m = {}
local template = CodeGen {
TOP = [[
digraph {
node [ shape = none ];
${nodes/_node()}
${edges/_edge()}
}
]],
_node = [[
${name};
]],
_edge = [[
${caller} -> ${callee};
]],
}
m.template = template
function m:to_dot ()
local done = {}
local nodes = {}
local edges = {}
local function parse (key)
if not done[key] then
done[key] = true
local tmpl = self[key]
if type(tmpl) == 'string' then
table.insert(nodes, { name = key })
for capt in tmpl:gmatch "(%$%b{})" do
local capt1, pos = capt:match("^%${([%a_][%w%._]*)()", 1)
if capt1 then
if capt:match("^%(%)", pos) then
table.insert(edges, { caller = key, callee = capt1 })
parse(capt1)
else
local capt2, capt3 = capt:match("^?([%a_][%w_]*)%(%)!([%a_][%w_]*)%(%)", pos)
if capt2 and capt3 then
table.insert(edges, { caller = key, callee = capt2 })
table.insert(edges, { caller = key, callee = capt3 })
parse(capt2)
parse(capt3)
else
local capt2 = capt:match("^[?/]([%a_][%w_]*)%(%)", pos)
if capt2 then
table.insert(edges, { caller = key, callee = capt2 })
parse(capt2)
end
end
end
end
end
end
end
end -- parse
for k in pairs(self[1]) do
parse(k)
end
template.nodes = nodes
template.edges = edges
local dot = template 'TOP'
return dot
end
_G.CodeGen.Graph = m
return m
--
-- Copyright (c) 2010 Francois Perrad
--
-- This library is licensed under the terms of the MIT/X11 license,
-- like Lua itself.
--