-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathre_test.lua
33 lines (31 loc) · 1.11 KB
/
re_test.lua
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
local re = require "parsing.re"
local utils = require "common.utils"
local function format_open(open)
return open[1] .. '(' .. open[2] .. ')'
end
local function visualize(pattern, str)
local graph = re.compile(pattern)
local matched, history = graph:match(str)
return graph:dot(
function(node, graph)
local tab = {}
for i, state in ipairs(history) do
if state == node then
table.insert(tab, str:sub(1, i - 1))
end
end
local closure = {}
if (graph.nodes[node][1]) then
for k in pairs(graph.nodes[node][1]) do table.insert(closure, k) end
end
local groups = table.concat(utils.map(format_open, graph.nodes[node][2]), ', ')
return '[label="' .. node .. ' {' .. table.concat(tab, ', ') .. '} ' .. groups .. ' [' .. table.concat(closure, ', ') .. ']' .. '"]'
end,
function(c, l, r, graph)
local tab = {}
for key in pairs(c) do table.insert(tab, key) end
return table.concat(tab, ', ')
end)
end
-- Let's get the tokens to a regex parser
print(visualize("//ab?d", "//adasdfsdf\n"))