meta-nod.lua /size: 1859 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['meta-nod'] = {
2    version   = 1.001,
3    comment   = "companion to meta-nod.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local tonumber = tonumber
10local P, R, Cs, lpegmatch = lpeg.P, lpeg.R, lpeg.Cs, lpeg.match
11
12local references = { }
13local trace      = false
14local report     = logs.reporter("metapost","nodes")
15
16local context    = context
17local implement  = interfaces.implement
18
19trackers.register("metapost.nodes", function(v) trace = v end)
20
21local word     = R("AZ","az","__")^1
22
23local pattern = Cs (
24    (
25        word / function(s) return references[s] or s end
26      + P("{") / "["
27      + P("}") / "]"
28      + P(1)
29    )^1
30)
31
32implement {
33    name    = "grph_nodes_initialize",
34    actions = function()
35        references = { }
36    end
37}
38
39implement {
40    name    = "grph_nodes_reset",
41    actions = function()
42        references = { }
43    end
44}
45
46implement {
47    name      = "grph_nodes_register",
48    arguments = { "string", "integer" },
49    actions   = function(s,r)
50        if not tonumber(s) then
51            if trace then
52                report("register %i as %a",t,s)
53            end
54            references[s] = r
55        end
56    end
57}
58
59implement {
60    name      = "grph_nodes_resolve",
61    arguments = "string",
62    actions   = function(s)
63        local r = references[s]
64        if r then
65            if trace then
66                report("resolve %a to %i",s,r)
67            end
68            context(r)
69            return
70        end
71        local n = lpegmatch(pattern,s)
72        if s ~= n then
73            if trace then
74                report("resolve '%s' to %s",s,n)
75            end
76            context(n)
77            return
78        end
79        context(s)
80    end
81}
82