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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
|
/* lnewtokenlib.c
Copyright 2006-2012 Taco Hoekwater <taco@luatex.org>
This file is part of LuaTeX.
LuaTeX is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version.
LuaTeX is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU General Public License along
with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */
/*
This module is unfinished and an intermediate step to removal of the old
token lib. Between version 0.80 and 0.85 the transition will be complete
and at the same time the input (buffer) handling will be cleaned up and
simplified. At that moment we can feed back tokens into the input.
The code can be optimized a bit by faster key checking. The scan functions
implemented here will stay functionally the same but can be improved if
needed. The old triplet model will disappear.
*/
#include "ptexlib.h"
#include "lua/luatex-api.h"
typedef struct lua_token {
int token;
int origin;
} lua_token;
typedef struct saved_tex_scanner {
int token;
int origin;
int save_cmd, save_chr, save_cs, save_tok;
} saved_tex_scanner;
#define save_tex_scanner(a) do { \
a.save_cmd = cur_cmd; \
a.save_chr = cur_chr; \
a.save_cs = cur_cs; \
a.save_tok = cur_tok; \
} while (0)
#define unsave_tex_scanner(a) do { \
cur_cmd = a.save_cmd; \
cur_chr = a.save_chr; \
cur_cs = a.save_cs; \
cur_tok = a.save_tok; \
} while (0)
#define TEX_ORIGIN 0 /* not used yet */
#define LUA_ORIGIN 1
static lua_token *check_istoken(lua_State * L, int ud);
#define TOKEN_METATABLE "luatex_newtoken"
#define DEBUG 0
#define DEBUG_OUT stdout
#define DEFAULT_SCAN_CODE_SET 2048 + 4096 /* default: letter and other */
/* two core helpers */
#define is_active_string(s) (strlen((char *)s)>3 && *s==0xEF && *(s+1)==0xBF && *(s+2)==0xBF)
static unsigned char *get_cs_text(int cs)
{
if (cs == null_cs)
return (unsigned char *) xstrdup("\\csname\\endcsname");
else if ((cs_text(cs) < 0) || (cs_text(cs) >= str_ptr))
return (unsigned char *) xstrdup("");
else
return (unsigned char *) makecstring(cs_text(cs));
}
/* maybe this qualify as a macro, not function */
static lua_token *maybe_istoken(lua_State * L, int ud)
{
lua_token *p = lua_touserdata(L, ud);
if (p != NULL) {
if (lua_getmetatable(L, ud)) {
lua_rawgeti(L, LUA_REGISTRYINDEX, luaS_index(luatex_newtoken));
lua_gettable(L, LUA_REGISTRYINDEX);
if (!lua_rawequal(L, -1, -2))
p = NULL;
lua_pop(L, 2);
}
}
return p;
}
/* we could make the message a function and just inline the rest (via a macro) */
lua_token *check_istoken(lua_State * L, int ud)
{
lua_token *p = maybe_istoken(L, ud);
if (p != NULL)
return p;
luatex_fail("There should have been a lua <token> here, not an object with type %s!", luaL_typename(L, ud));
return NULL;
}
/* token library functions */
static void make_new_token(lua_State * L, int cmd, int chr, int cs)
{
int tok = 0;
lua_token *thetok = lua_newuserdata(L, sizeof(lua_token));
thetok->origin = LUA_ORIGIN;
fast_get_avail(thetok->token);
tok = (cs ? cs_token_flag + cs : token_val(cmd, chr));
set_token_info(thetok->token, tok);
lua_rawgeti(L, LUA_REGISTRYINDEX, luaS_index(luatex_newtoken));
lua_gettable(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);
}
static void push_token(lua_State * L, int tok)
{
lua_token *thetok = lua_newuserdata(L, sizeof(lua_token));
thetok->origin = LUA_ORIGIN;
thetok->token = tok;
lua_rawgeti(L, LUA_REGISTRYINDEX, luaS_index(luatex_newtoken));
lua_gettable(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);
}
/* static int run_get_cs_offset(lua_State * L) */
/* { */
/* lua_pushnumber(L, cs_token_flag); */
/* return 1; */
/* } */
/* static int run_get_command_id(lua_State * L) */
/* { */
/* int cs = -1; */
/* if (lua_isstring(L, -1)) { */
/* cs = get_command_id(lua_tostring(L, -1)); */
/* } */
/* lua_pushnumber(L, cs); */
/* return 1; */
/* } */
/* static int run_get_csname_id(lua_State * L) */
/* { */
/* const char *s; */
/* size_t k, cs = 0; */
/* if (lua_isstring(L, -1)) { */
/* s = lua_tolstring(L, -1, &k); */
/* cs = (size_t) string_lookup(s, k); */
/* } */
/* lua_pushnumber(L, (lua_Number) cs); */
/* return 1; */
/* } */
static int run_get_next(lua_State * L)
{
saved_tex_scanner texstate;
save_tex_scanner(texstate);
get_next();
make_new_token(L, cur_cmd, cur_chr, cur_cs);
unsave_tex_scanner(texstate);
return 1;
}
static int run_scan_keyword(lua_State * L)
{
saved_tex_scanner texstate;
const char *s = luaL_checkstring(L, -1);
int v = 0;
if (s) {
save_tex_scanner(texstate);
if (scan_keyword(s)) {
v = 1;
}
unsave_tex_scanner(texstate);
}
lua_pushboolean(L,v);
return 1;
}
static int run_scan_int(lua_State * L)
{
saved_tex_scanner texstate;
int v = 0;
save_tex_scanner(texstate);
scan_int();
v = cur_val;
unsave_tex_scanner(texstate);
lua_pushnumber(L,(lua_Number)v);
return 1;
}
static int run_scan_dimen(lua_State * L)
{
saved_tex_scanner texstate;
int v = 0, o = 0;
int inf = false, mu = false;
int t = lua_gettop(L);
if (t>0)
inf = lua_toboolean(L,1); /* inf values allowed ?*/
if (t>1)
mu = lua_toboolean(L,2); /* mu units required ?*/
save_tex_scanner(texstate);
scan_dimen( mu,inf, false); /* arg3 = shortcut */
v = cur_val;
o = cur_order;
unsave_tex_scanner(texstate);
lua_pushnumber(L,(lua_Number)v);
if (inf) {
lua_pushnumber(L,(lua_Number)o);
return 2;
} else {
return 1;
}
}
static int run_scan_glue(lua_State * L)
{
saved_tex_scanner texstate;
int v = 0;
int mu = false;
int t = lua_gettop(L);
if (t>0)
mu = lua_toboolean(L,1); /* mu units required ?*/
save_tex_scanner(texstate);
scan_glue((mu ? mu_val_level : glue_val_level));
v = cur_val; /* which is a glue_spec node */
unsave_tex_scanner(texstate);
lua_nodelib_push_fast(L,(halfword)v);
return 1;
}
static int run_scan_toks(lua_State * L)
{
saved_tex_scanner texstate;
int macro_def = false, xpand = false;
halfword t, saved_defref;
int i = 1;
int top = lua_gettop(L);
if (top>0)
macro_def = lua_toboolean(L,1); /* \\def ? */
if (top>1)
xpand = lua_toboolean(L,2); /* expand ? */
save_tex_scanner(texstate);
saved_defref = def_ref;
(void) scan_toks(macro_def, xpand);
t = def_ref;
unsave_tex_scanner(texstate);
def_ref = saved_defref;
/* This function returns a pointer to the tail of a new token
list, and it also makes |def_ref| point to the reference count at the
head of that list. */
lua_newtable(L);
while (token_link(t)) {
t = token_link(t);
push_token(L,t);
lua_rawseti(L,-2,i++);
}
return 1;
}
static int run_scan_string(lua_State * L) /* HH */
{ /* can be simplified, no need for intermediate list */
saved_tex_scanner texstate;
halfword t, saved_defref;
save_tex_scanner(texstate);
do {
get_x_token();
} while ((cur_cmd == spacer_cmd) || (cur_cmd == relax_cmd));
if (cur_cmd == left_brace_cmd) {
back_input();
saved_defref = def_ref;
(void) scan_toks(false, true);
t = def_ref;
def_ref = saved_defref;
tokenlist_to_luastring(L,t);
} else if (cur_cmd == call_cmd) {
t = token_link(cur_chr);
tokenlist_to_luastring(L,t);
} else {
if (cur_cmd == 11 || cur_cmd == 12 ) {
char * str ;
luaL_Buffer b ;
luaL_buffinit(L,&b) ;
while (1) {
str = (char *) uni2str(cur_chr);
luaL_addstring(&b,(char *) str);
get_x_token();
if (cur_cmd != 11 && cur_cmd != 12 ) {
break ;
}
}
back_input();
luaL_pushresult(&b);
} else {
back_input();
lua_pushnil(L);
}
}
unsave_tex_scanner(texstate);
return 1;
}
static int run_scan_word(lua_State * L) /* HH */
{
saved_tex_scanner texstate;
save_tex_scanner(texstate);
do {
get_x_token();
} while ((cur_cmd == spacer_cmd) || (cur_cmd == relax_cmd));
if (cur_cmd == 11 || cur_cmd == 12 ) {
char *str ;
luaL_Buffer b ;
luaL_buffinit(L,&b) ;
while (1) {
str = (char *) uni2str(cur_chr);
luaL_addstring(&b,str);
xfree(str);
get_x_token();
if (cur_cmd != 11 && cur_cmd != 12 ) {
break ;
}
}
back_input();
luaL_pushresult(&b);
} else {
back_input();
lua_pushnil(L);
}
unsave_tex_scanner(texstate);
return 1;
}
static int run_scan_code(lua_State * L) /* HH */
{
saved_tex_scanner texstate;
int cc = DEFAULT_SCAN_CODE_SET ;
save_tex_scanner(texstate);
get_x_token();
if (cur_cmd < 16) {
if (lua_gettop(L)>0) {
cc = (int) lua_tointeger(L,-1);
if (cc == null) {
/* todo: message that we choose a default */
cc = DEFAULT_SCAN_CODE_SET ;
}
}
if (cc & (1<<(cur_cmd))) {
lua_pushnumber(L,(lua_Number)cur_chr);
} else {
lua_pushnil(L);
back_input();
}
} else {
lua_pushnil(L);
back_input();
}
unsave_tex_scanner(texstate);
return 1;
}
static int lua_tokenlib_is_token(lua_State * L) /* HH */
{
lua_pushboolean(L,maybe_istoken(L,1)==NULL ? 0 : 1);
return 1;
}
/* static int run_expand(lua_State * L) */
/* { */
/* (void) L; */
/* expand(); */
/* return 0; */
/* } */
static int run_lookup(lua_State * L)
{
const char *s;
size_t l;
int cs, cmd, chr;
if (lua_isstring(L, -1)) {
s = lua_tolstring(L, -1, &l);
if (l > 0) {
cs = string_lookup(s, l);
cmd = eq_type(cs);
chr = equiv(cs);
make_new_token(L, cmd, chr, cs);
return 1;
}
}
lua_pushnil(L);
return 1;
}
static int run_build(lua_State * L)
{
int cmd, chr, cs;
if (lua_isnumber(L, 1)) {
cs = 0;
chr = (int) lua_tointeger(L, 1);
cmd = (int) luaL_optinteger(L, 2, get_cat_code(int_par(cat_code_table_code),chr));
if (cmd == 0 || cmd == 9 || cmd == 14 || cmd == 15) {
fprintf(stdout,
"\n\nluatex error: not a good token.\nCatcode %i can not be returned, so I replaced it by 12 (other)",
(int) cmd);
error();
cmd = 12;
} else if (cmd == 13) {
cs = active_to_cs(chr, false);
cmd = eq_type(cs);
chr = equiv(cs);
}
make_new_token(L, cmd, chr, cs);
return 1;
} else {
return run_lookup(L);
}
}
/* token instance functions */
static int lua_tokenlib_free(lua_State * L)
{
lua_token *n;
n = check_istoken(L, 1);
if (n->origin == LUA_ORIGIN) {
free_avail(n->token);
}
return 1;
}
static int lua_tokenlib_getfield(lua_State * L)
{
lua_token *n;
const char *s;
halfword t ;
n = check_istoken(L, 1);
s = lua_tostring(L, 2);
t = token_info(n->token);
if (lua_key_eq(s, command)) {
if (t >= cs_token_flag) {
lua_pushnumber(L, eq_type((t - cs_token_flag)));
} else {
lua_pushnumber(L, token_cmd(t));
}
} else if (lua_key_eq(s, mode)) {
if (t >= cs_token_flag) {
lua_pushnumber(L, equiv((t - cs_token_flag)));
} else {
lua_pushnumber(L, token_chr(t));
}
} else if (lua_key_eq(s, cmdname)) {
int cmd = (t >= cs_token_flag ? eq_type(t - cs_token_flag) : token_cmd(t));
lua_pushstring(L, command_names[cmd].cmd_name); /* can be sped up */
} else if (lua_key_eq(s, csname)) {
unsigned char *s;
if (t >= cs_token_flag && ((s = get_cs_text(t - cs_token_flag)) != (unsigned char *) NULL)) {
if (is_active_string(s))
lua_pushstring(L, (char *) (s + 3));
else
lua_pushstring(L, (char *) s);
} else {
lua_pushstring(L, "");
}
} else if (lua_key_eq(s, id)) {
lua_pushnumber(L, n->token);
} else if (lua_key_eq(s, tok)) {
lua_pushnumber(L, t);
} else if (lua_key_eq(s, active)) {
unsigned char *s;
if (t >= cs_token_flag && ((s = get_cs_text(t - cs_token_flag)) != (unsigned char *) NULL)) {
if (is_active_string(s))
lua_pushboolean(L,1);
else
lua_pushboolean(L,0);
free(s);
} else {
lua_pushboolean(L,0);
}
} else if (lua_key_eq(s, expandable)) {
int cmd = (t >= cs_token_flag ? eq_type(t - cs_token_flag) : token_cmd(t));
if (cmd > max_command_cmd) {
lua_pushboolean(L, 1);
} else {
lua_pushboolean(L, 0);
}
} else if (lua_key_eq(s, protected)) {
int cmd = (t >= cs_token_flag ? eq_type(t - cs_token_flag) : token_cmd(t));
int chr = (t >= cs_token_flag ? equiv(t - cs_token_flag) : token_chr(t));
if (cmd > max_command_cmd && ((cmd >= call_cmd) && (cmd < end_template_cmd)) &&
token_info(token_link(chr)) == protected_token) {
lua_pushboolean(L, 1);
} else {
lua_pushboolean(L, 0);
}
}
return 1;
}
static int lua_tokenlib_equal(lua_State * L)
{
lua_token *n, *m;
n = check_istoken(L, 1);
m = check_istoken(L, 2);
if (token_info(n->token) == token_info(m->token)) {
lua_pushboolean(L,1);
return 1;
}
lua_pushboolean(L,0);
return 1;
}
static int lua_tokenlib_tostring(lua_State * L)
{
char *msg;
lua_token *n;
n = check_istoken(L, 1);
msg = xmalloc(256);
snprintf(msg, 255, "<%s token %d: %d>", (n->origin==LUA_ORIGIN?"lua":"tex"), n->token , token_info(n->token));
lua_pushstring(L, msg);
free(msg);
return 1;
}
static int lua_tokenlib_type(lua_State * L)
{
if (maybe_istoken(L,1)!=NULL) {
lua_pushstring(L,"token");
} else {
lua_pushnil(L);
}
return 1;
}
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;
}
/* experiment */
/* [catcodetable] csname content : \def\csname{content} */
/* [catcodetable] csname content global : \gdef\csname{content} */
/* [catcodetable] csname : \def\csname{} */
static int set_macro(lua_State * L)
{
const char *name = null;
const char *str = null;
const char *s = null;
size_t lname = 0;
size_t lstr = 0;
int cs, cc, ct;
int n = lua_gettop(L);
int a = 0 ; /* global state */
int nncs = no_new_control_sequence;
if (n == 0) {
return 0 ;
}
if (lua_isnumber(L, 1)) {
if (n == 1)
return 0;
ct = (int) lua_tointeger(L, 1);
name = lua_tolstring(L, 2, &lname);
if (n > 2)
str = lua_tolstring(L, 3, &lstr);
if (n > 3)
s = lua_tostring(L, 4);
} else {
ct = int_par(cat_code_table_code) ;
name = lua_tolstring(L, 1, &lname);
if (n > 1)
str = lua_tolstring(L, 2, &lstr);
if (n > 2)
s = lua_tostring(L, 3);
}
if (name == null) {
return 0 ;
}
if (s && (lua_key_eq(s, global))) {
a = 4;
}
no_new_control_sequence = false ;
cs = string_lookup(name, lname);
no_new_control_sequence = nncs;
if (lstr > 0) {
halfword p; /* tail of the token list */
halfword q; /* new node being added to the token list via |store_new_token| */
halfword t; /* token being appended */
const char *se = str + lstr;
p = temp_token_head;
set_token_link(p, null);
/* this left brace is used to store the number of arguments */
fast_store_new_token(left_brace_token);
/* and this ends the not present arguments, and no: we will not support arguments here*/
fast_store_new_token(end_match_token);
while (str < se) {
/* hh: str2uni could return len too (also elsewhere) */
t = (halfword) str2uni((const unsigned char *) str);
str += utf8_size(t);
cc = get_cat_code(ct,t);
/* this is a relating simple converter; if more is needed one can just use */
/* tex.print with a regular \def or \gdef and feed the string into the regular */
/* scanner; */
if (cc == 0) {
/* we have a potential control sequence so we check for it */
int _lname = 0 ;
int _s = 0 ;
int _c = 0 ;
halfword _cs = null ;
const char *_name = str ;
while (str < se) {
t = (halfword) str2uni((const unsigned char *) str);
_s = utf8_size(t);
_c = get_cat_code(ct,t);
if (_c == 11) {
str += _s ;
_lname = _lname + _s ;
} else if (_c == 10) {
/* we ignore a trailing space like normal scanning does */
str += _s ;
break ;
} else {
break ;
}
}
if (_s > 0) {
/* we havea potential \cs */
_cs = string_lookup(_name, _lname);
if (_cs == undefined_control_sequence) {
/* let's play safe and backtrack */
t = cc * (1<<21) + t ;
str = _name ;
} else {
t = cs_token_flag + _cs;
}
} else {
/* just a character with some meaning, so \unknown becomes effectively */
/* \\unknown assuming that \\ has some useful meaning of course */
t = cc * (1<<21) + t ;
str = _name ;
}
} else {
/* whatever token, so for instance $x^2$ just works given a tex */
/* catcode regime */
t = cc * (1<<21) + t ;
}
fast_store_new_token(t);
}
/* there is no fast_store_new_token(right_brace_token) needed */
define(cs, call_cmd + (a % 4), token_link(temp_token_head));
} else {
define(cs, call_cmd + (a % 4), null);
}
return 0;
}
static const struct luaL_Reg tokenlib[] = {
{"is_token", lua_tokenlib_is_token},
{"get_next", run_get_next},
{"scan_keyword", run_scan_keyword},
{"scan_int", run_scan_int},
{"scan_dimen", run_scan_dimen},
{"scan_glue", run_scan_glue},
{"scan_toks", run_scan_toks},
{"scan_code", run_scan_code},
{"scan_string", run_scan_string},
{"scan_word", run_scan_word},
{"type", lua_tokenlib_type},
{"create", run_build},
{"scan_token", run_scan_token}, /* expands next token if needed */
{"set_macro", set_macro},
/* {"expand", run_expand}, */ /* does not work yet! */
/* {"csname_id", run_get_csname_id}, */ /* yes or no */
/* {"command_id", run_get_command_id}, */ /* yes or no */
/* {"cs_offset", run_get_cs_offset}, */ /* not that useful */
{NULL, NULL} /* sentinel */
};
static const struct luaL_Reg tokenlib_m[] = {
{"__index", lua_tokenlib_getfield},
{"__tostring", lua_tokenlib_tostring},
{"__eq", lua_tokenlib_equal},
{"__gc", lua_tokenlib_free},
{NULL, NULL} /* sentinel */
};
int luaopen_newtoken(lua_State * L)
{
/* the main metatable of token userdata */
luaL_newmetatable(L, TOKEN_METATABLE);
luaL_register(L, NULL, tokenlib_m);
luaL_register(L, "newtoken", tokenlib);
return 1;
}
|