summaryrefslogtreecommitdiff
path: root/tex/context/base/node-seq.lua
blob: 2fd4f81aa18219bcc2dbe34a7773a4d1183ee777 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
if not modules then modules = { } end modules ['node-seq'] = {
    version   = 1.001,
    comment   = "companion to node-ini.tex",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- we assume namespace usage, i.e. unique names for functions

local format, concat = string.format, table.concat

sequencer = sequencer or { }

function sequencer.reset()
    return {
        list = { },
        order = { },
        kind = { },
    }
end

function sequencer.prependgroup(t,group,where)
    local list, order = t.list, t.order
    table.remove_value(order,group)
    table.insert_before_value(order,where,group)
    list[group] = { }
end

function sequencer.appendgroup(t,group,where)
    local list, order = t.list, t.order
    table.remove_value(order,group)
    table.insert_after_value(order,where,group)
    list[group] = { }
end

function sequencer.prependaction(t,group,action,where,kind)
    local g = t.list[group]
    if g then
        table.remove_value(g,action)
        table.insert_before_value(g,where,action)
        t.kind[action] = kind
    end
end

function sequencer.appendaction(t,group,action,where,kind)
    local g = t.list[group]
    if g then
        table.remove_value(g,action)
        table.insert_after_value(g,where,action)
        t.kind[action] = kind
    end
end

function sequencer.setkind(t,action,kind)
    t.kind[action] = kind
end

function sequencer.removeaction(t,group,action)
    local g = t.list[group]
    if g then
        table.remove_value(g,action)
    end
end

function sequencer.compile(t,compiler)
    if type(t) == "string" then
        -- already compiled
    elseif compiler then
        t = compiler(t)
    else
        t = sequencer.tostring(t)
    end
    return loadstring(t)()
end

local function localize(str)
    return str:gsub("%.","_")
end

local template = [[
%s
return function(...)
%s
end]]

function sequencer.tostring(t)
    local list, order, kind, vars, calls = t.list, t.order, t.kind, { }, { }
    for i=1,#order do
        local group = order[i]
        local actions = list[group]
        for i=1,#actions do
            local action = actions[i]
            local localized = localize(action)
            vars [#vars +1] = format("local %s = %s", localized, action)
            calls[#calls+1] = format("  %s(...) -- %s %i", localized, group, i)
        end
    end
    return template:format(concat(vars,"\n"),concat(calls,"\n"))
end

local template = [[
%s
return function(head,tail)
  local ok, done = false, false
%s
  return head, tail, done
end]]

function sequencer.nodeprocessor(t)
    local list, order, kind, vars, calls = t.list, t.order, t.kind, { }, { }
    for i=1,#order do
        local group = order[i]
        local actions = list[group]
        for i=1,#actions do
            local action = actions[i]
            local localized = localize(action)
            vars[#vars+1] = format("local %s = %s",localized,action)
            if kind[action] == "nohead" then
                calls[#calls+1] = format("              ok = %s(head,tail) done = done or ok -- %s %i",localized,group,i)
            elseif kind[action] == "notail" then
                calls[#calls+1] = format("  head,       ok = %s(head,tail) done = done or ok -- %s %i",localized,group,i)
            else
                calls[#calls+1] = format("  head, tail, ok = %s(head,tail) done = done or ok -- %s %i",localized,group,i)
            end
        end
    end
    return template:format(concat(vars,"\n"),concat(calls,"\n"))
end

--~ hans = {}
--~ taco = {}

--~ function hans.a(head,tail) print("a",head,tail) return head,tail,true end
--~ function hans.b(head,tail) print("b",head,tail) return head,tail,true end
--~ function hans.c(head,tail) print("c",head,tail) return head,tail,true end
--~ function hans.x(head,tail) print("x",head,tail) return head,tail,true end
--~ function taco.i(head,tail) print("i",head,tail) return head,tail,true end
--~ function taco.j(head,tail) print("j",head,tail) return head,tail,true end

--~ t = sequencer.reset()

--~ sequencer.appendgroup(t,"hans")
--~ sequencer.appendgroup(t,"taco")
--~ sequencer.prependaction(t,"hans","hans.a")
--~ sequencer.appendaction (t,"hans","hans.b")
--~ sequencer.appendaction (t,"hans","hans.x")
--~ sequencer.prependaction(t,"hans","hans.c","hans.b")
--~ sequencer.prependaction(t,"taco","taco.i")
--~ sequencer.prependaction(t,"taco","taco.j")
--~ sequencer.removeaction(t,"hans","hans.x")

--~ sequencer.setkind(t,"hans.b","notail")
--~ sequencer.setkind(t,"taco.j","nohead")

--~ print(sequencer.tostring(t))

--~ s = sequencer.compile(t,sequencer.nodeprocessor)

--~ print(sequencer.nodeprocessor(t))
--~ print(s("head","tail"))