summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mkiv/toks-ini.lua
blob: 0ce7b4836b79ba169f87c2f9b213293338a8b425 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
if not modules then modules = { } end modules ['toks-ini'] = {
    version   = 1.001,
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

tokens = tokens or { }

local tokens     = tokens
local token      = token -- the built in one
local tonumber   = tonumber
local tostring   = tostring
local utfchar    = utf.char
local char       = string.char
local printtable = table.print
local concat     = table.concat

if setinspector then

    local istoken = token.is_token
    local simple  = { letter = "letter", other_char = "other" }

    local function astable(t)
        if t and istoken(t) then
            local cmdname = t.cmdname
            local simple  = simple[cmdname]
            if simple then
                return {
                    category   = simple,
                    character  = utfchar(t.mode) or nil,
                }
            else
                return {
                    command    = t.command,
                    id         = t.id,
                    tok        = t.tok,
                    csname     = t.csname,
                    active     = t.active,
                    expandable = t.expandable,
                    protected  = t.protected,
                    mode       = t.mode,
                    index      = t.index,
                    cmdname    = cmdname,
                }
            end
        end
    end

    tokens.istoken = istoken
    tokens.astable = astable

    setinspector("token",function(v) if istoken(v) then printtable(astable(v),tostring(v)) return true end end)

end

local scan_toks    = token.scan_toks
local scan_string  = token.scan_string
local scan_int     = token.scan_int
local scan_code    = token.scan_code
local scan_dimen   = token.scan_dimen
local scan_glue    = token.scan_glue
local scan_keyword = token.scan_keyword
local scan_token   = token.scan_token
local scan_word    = token.scan_word
local scan_number  = token.scan_number
local scan_csname  = token.scan_csname

local get_next     = token.get_next

if not token.get_macro then
    local scantoks = tex.scantoks
    local gettoks  = tex.gettoks
    function token.get_meaning(name)
        scantoks("t_get_macro",tex.ctxcatcodes,"\\"..name)
        return gettoks("t_get_macro")
    end
    function token.get_macro(name)
        scantoks("t_get_macro",tex.ctxcatcodes,"\\"..name)
        local s = gettoks("t_get_macro")
        return match(s,"^.-%->(.*)$") or s
    end
end

local set_macro    = token.set_macro
local get_macro    = token.get_macro
local get_meaning  = token.get_meaning
local get_cmdname  = token.get_cmdname
local create_token = token.create

function tokens.defined(name)
    return get_cmdname(create_token(name)) ~= "undefined_cs"
end

-- set_macro = function(k,v,g)
--     if g == "global" then
--         context.setgvalue(k,v or '')
--     else
--         context.setvalue(k,v or '')
--     end
-- end

local bits = {
    escape      = 2^ 0,
    begingroup  = 2^ 1,
    endgroup    = 2^ 2,
    mathshift   = 2^ 3,
    alignment   = 2^ 4,
    endofline   = 2^ 5,
    parameter   = 2^ 6,
    superscript = 2^ 7,
    subscript   = 2^ 8,
    ignore      = 2^ 9,
    space       = 2^10, -- 1024
    letter      = 2^11,
    other       = 2^12,
    active      = 2^13,
    comment     = 2^14,
    invalid     = 2^15,
    --
    character   = 2^11 + 2^12,
    whitespace  = 2^13 + 2^10, --    / needs more checking
    --
    open        = 2^10 + 2^1, -- space + begingroup
    close       = 2^10 + 2^2, -- space + endgroup
}

-- for k, v in next, bits do bits[v] = k end

tokens.bits = bits

local space_bits = bits.space

-- words are space or \relax terminated and the trailing space is gobbled; a word
-- can contain any non-space letter/other

local t = { } -- small optimization, a shared variable that is not reset

if scan_word then

    scan_number = function(base)
        local s = scan_word()
        if not s then
            return nil
        elseif base then
            return tonumber(s,base)
        else
            return tonumber(s)
        end
    end

else

    scan_word = function()
        local n = 0
        while true do
            local c = scan_code()
            if c then
                n = n + 1
                t[n] = utfchar(c)
            elseif scan_code(space_bits) then
                if n > 0 then
                    break
                end
            elseif n > 0 then
                break
            else
                return
            end
        end
        return concat(t,"",1,n)
    end

    -- so we gobble the space (like scan_int) (number has to be space or non-char terminated
    -- as we accept 0xabcd and such so there is no clear separator for a keyword

    scan_number = function(base)
        local n = 0
        while true do
            local c = scan_code()
            if c then
                n = n + 1
                t[n] = char(c)
            elseif scan_code(space_bits) then
                if n > 0 then
                    break
                end
            elseif n > 0 then
                break
            else
                return
            end
        end
        local s = concat(t,"",1,n)
        if base then
            return tonumber(s,base)
        else
            return tonumber(s)
        end
    end

end

-- -- the next one cannot handle \iftrue true\else false\fi
--
-- local function scan_boolean()
--     if scan_keyword("true") then
--         return true
--     elseif scan_keyword("false") then
--         return false
--     else
--         return nil
--     end
-- end

local function scan_boolean()
    local kw = scan_word()
    if kw == "true" then
        return true
    elseif kw == "false" then
        return false
    else
        return nil
    end
end

if not scan_csname then

    scan_csname = function()
        local t = get_next()
        local n = t.csname
        return n ~= "" and n or nil
    end

end

tokens.scanners = { -- these expand
    token     = scan_token,
    toks      = scan_toks,
    tokens    = scan_toks,
    dimen     = scan_dimen,
    dimension = scan_dimen,
    glue      = scan_glue,
    skip      = scan_glue,
    integer   = scan_int,
    count     = scan_int,
    string    = scan_string,
    code      = scan_code,
    word      = scan_word,
    number    = scan_number,
    boolean   = scan_boolean,
    keyword   = scan_keyword,
    csname    = scan_csname,
}

tokens.getters = { -- these don't expand
    meaning = get_meaning,
    macro   = get_macro,
    token   = get_next,
    count   = tex.getcount,
    dimen   = tex.getdimen,
    skip    = tex.getglue,
    glue    = tex.getglue,
    skip    = tex.getmuglue,
    glue    = tex.getmuglue,
    box     = tex.getbox,
}

tokens.setters = {
    macro = set_macro,
    count = tex.setcount,
    dimen = tex.setdimen,
    skip  = tex.setglue,
    glue  = tex.setglue,
    skip  = tex.setmuglue,
    glue  = tex.setmuglue,
    box   = tex.setbox,
}

-- static int run_scan_token(lua_State * L)
-- {
--     saved_tex_scanner texstate;
--     save_tex_scanner(texstate);
--     get_x_token();
--     make_new_token(L, cur_cmd, cur_chr, cur_cs);
--     unsave_tex_scanner(texstate);
--     return 1;
-- }
--
-- static int run_get_future(lua_State * L)
-- {
--  /* saved_tex_scanner texstate; */
--  /* save_tex_scanner(texstate); */
--     get_token();
--     make_new_token(L, cur_cmd, cur_chr, cur_cs);
--     back_input();
--  /* unsave_tex_scanner(texstate); */
--     return 1;
-- }