Debian Codegraph @

texk/web2c/luatexdir/lua/lnewtokenlib.c

raw
1 /* lnewtokenlib.c
2
3 Copyright 2006-2012 Taco Hoekwater <[email protected]>
4
5 This file is part of LuaTeX.
6
7 LuaTeX is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2 of the License, or (at your
10 option) any later version.
11
12 LuaTeX is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */
19
20
21 /*
22 This module is unfinished and an intermediate step to removal of the old
23 token lib. Between version 0.80 and 0.85 the transition will be complete
24 and at the same time the input (buffer) handling will be cleaned up and
25 simplified. At that moment we can feed back tokens into the input.
26
27 The code can be optimized a bit by faster key checking. The scan functions
28 implemented here will stay functionally the same but can be improved if
29 needed. The old triplet model will disappear.
30
31 */
32
33 #include "ptexlib.h"
34 #include "lua/luatex-api.h"
35
36 /*
37 typedef struct lua_token {
38 int token;
39 int origin;
40 } lua_token;
41 */
42
43 typedef struct saved_tex_scanner {
44 int token;
45 int origin;
46 int save_cmd, save_chr, save_cs, save_tok;
47 } saved_tex_scanner;
48
49 #define save_tex_scanner(a) do { \
50 a.save_cmd = cur_cmd; \
51 a.save_chr = cur_chr; \
52 a.save_cs = cur_cs; \
53 a.save_tok = cur_tok; \
54 } while (0)
55
56 #define unsave_tex_scanner(a) do { \
57 cur_cmd = a.save_cmd; \
58 cur_chr = a.save_chr; \
59 cur_cs = a.save_cs; \
60 cur_tok = a.save_tok; \
61 } while (0)
62
63 #define TEX_ORIGIN 0 /* not used yet */
64 #define LUA_ORIGIN 1
65
66 static lua_token *check_istoken(lua_State * L, int ud);
67
68 #define TOKEN_METATABLE "luatex.token"
69
70 #define DEBUG 0
71 #define DEBUG_OUT stdout
72
73 #define DEFAULT_SCAN_CODE_SET 2048 + 4096 /* default: letter and other */
74
75 /* two core helpers */
76
77 #define is_active_string(s) (strlen((char *)s)>3 && *s==0xEF && *(s+1)==0xBF && *(s+2)==0xBF)
78
79 void make_token_table(lua_State * L, int cmd, int chr, int cs)
80 {
81 lua_createtable(L, 3, 0);
82 lua_pushinteger(L, cmd);
83 lua_rawseti(L, -2, 1);
84 lua_pushinteger(L, chr);
85 lua_rawseti(L, -2, 2);
86 lua_pushinteger(L, cs);
87 lua_rawseti(L, -2, 3);
88 }
89
90 static unsigned char *get_cs_text(int cs)
91 {
92 if (cs == null_cs)
93 return (unsigned char *) xstrdup("\\csname\\endcsname");
94 else if ((cs_text(cs) < 0) || (cs_text(cs) >= str_ptr))
95 return (unsigned char *) xstrdup("");
96 else
97 return (unsigned char *) makecstring(cs_text(cs));
98 }
99
100 /* maybe this qualify as a macro, not function */
101
102 static lua_token *maybe_istoken(lua_State * L, int ud)
103 {
104 lua_token *p = lua_touserdata(L, ud);
105 if (p != NULL) {
106 if (lua_getmetatable(L, ud)) {
107 lua_get_metatablelua(luatex_token);
108 if (!lua_rawequal(L, -1, -2))
109 p = NULL;
110 lua_pop(L, 2);
111 }
112 }
113 return p;
114 }
115
116 /* we could make the message a function and just inline the rest (via a macro) */
117
118 lua_token *check_istoken(lua_State * L, int ud)
119 {
120 lua_token *p = maybe_istoken(L, ud);
121 if (p != NULL)
122 return p;
123 formatted_error("token lib","lua <token> expected, not an object with type %s", luaL_typename(L, ud));
124 return NULL;
125 }
126
127 /* moved here */
128
129 int token_from_lua(lua_State * L)
130 {
131 int cmd, chr;
132 int cs = 0;
133 size_t len;
134 lua_token *p;
135 if (lua_type(L, -1) == LUA_TTABLE) {
136 len = lua_rawlen(L, -1);
137 if (len == 3 || len == 2) {
138 lua_rawgeti(L, -1, 1);
139 cmd = (int) lua_tointeger(L, -1);
140 lua_rawgeti(L, -2, 2);
141 chr = (int) lua_tointeger(L, -1);
142 if (len == 3) {
143 lua_rawgeti(L, -3, 3);
144 cs = (int) lua_tointeger(L, -1);
145 }
146 lua_pop(L, (int) len);
147 if (cs == 0) {
148 return token_val(cmd, chr);
149 } else {
150 return cs_token_flag + cs;
151 }
152 }
153 } else {
154 p = check_istoken(L, -1);
155 if (p) {
156 return token_info(p->token);
157 }
158 }
159 return -1;
160 }
161
162 /* token library functions */
163
164 static void make_new_token(lua_State * L, int cmd, int chr, int cs)
165 {
166 int tok = 0;
167 lua_token *thetok = lua_newuserdata(L, sizeof(lua_token));
168 thetok->origin = LUA_ORIGIN;
169 fast_get_avail(thetok->token);
170 tok = (cs ? cs_token_flag + cs : token_val(cmd, chr));
171 set_token_info(thetok->token, tok);
172 lua_get_metatablelua(luatex_token);
173 lua_setmetatable(L, -2);
174 }
175
176 static void push_token(lua_State * L, int tok)
177 {
178 lua_token *thetok = lua_newuserdata(L, sizeof(lua_token));
179 thetok->origin = LUA_ORIGIN;
180 thetok->token = tok;
181 lua_get_metatablelua(luatex_token);
182 lua_setmetatable(L, -2);
183 }
184
185 static int run_get_biggest_char(lua_State * L)
186 {
187 lua_pushinteger(L, biggest_char);
188 return 1;
189 }
190
191 /* not that useful:
192
193 static int run_get_cs_offset(lua_State * L)
194 {
195 lua_pushinteger(L, cs_token_flag);
196 return 1;
197 }
198
199 */
200
201 static int run_get_command_id(lua_State * L)
202 {
203 int id = -1;
204 if (lua_type(L, -1) == LUA_TSTRING) {
205 id = get_command_id(lua_tostring(L, -1));
206 }
207 if (id >= 0) {
208 lua_pushinteger(L, id);
209 } else {
210 lua_pushnil(L);
211 }
212 return 1;
213 }
214
215 /* not that useful:
216
217 static int run_get_csname_id(lua_State * L)
218 {
219 const char *s;
220 size_t k, cs = 0;
221 if (lua_type(L, -1) == LUA_TSTRING) {
222 s = lua_tolstring(L, -1, &k);
223 cs = (size_t) string_lookup(s, k);
224 }
225 lua_pushinteger(L, (lua_Number) cs);
226 return 1;
227 }
228
229 */
230
231 static int run_get_next(lua_State * L)
232 {
233 saved_tex_scanner texstate;
234 save_tex_scanner(texstate);
235 get_next();
236 make_new_token(L, cur_cmd, cur_chr, cur_cs);
237 unsave_tex_scanner(texstate);
238 return 1;
239 }
240
241 /*
242 This is experimental code:
243
244 local t1 = token.get_next()
245 local t2 = token.get_next()
246 local t3 = token.get_next()
247 local t4 = token.get_next()
248 -- watch out, we flush in sequence
249 token.put_next { t1, t2 }
250 -- but this one gets pushed in front
251 token.put_next ( t3, t4 )
252 -- so when we get wxyz we put yzwx!
253
254 At some point we can consider a token.print that delays and goes via
255 the same rope mechanism as texio.prints and friends but then one can
256 as well serialize the tokens and do a normal print so there is no real
257 gain in it. After all, the tokenlib operates at the input level so we
258 might as well keep it there.
259
260 */
261
262 inline static int run_put_next(lua_State * L)
263 {
264 int n = lua_gettop(L);
265 int m = 0;
266 int i = 0;
267 halfword h = null;
268 halfword t = null;
269 halfword x = null;
270 lua_token *p ;
271 if (n == 0) {
272 /* we accept a single nil argument */
273 return 0;
274 }
275 lua_get_metatablelua(luatex_token);
276 m = lua_gettop(L);
277 if (lua_type(L,1) == LUA_TTABLE) {
278 if (n>1) {
279 normal_error("token lib","only one table permitted in put_next");
280 } else {
281 for (i = 1;; i++) {
282 lua_rawgeti(L, 1, i); /* table mt token */
283 if (lua_type(L,-1) == LUA_TNIL) {
284 break;
285 } else {
286 p = lua_touserdata(L, -1);
287 if (p == NULL) {
288 normal_error("token lib","lua <token> expected in put_next (1)");
289 } else if (!lua_getmetatable(L, -1)) { /* table mt token mt */
290 normal_error("token lib","lua <token> expected in put_next (2)");
291 } else if (!lua_rawequal(L, m, -1)) {
292 normal_error("token lib","lua <token> expected in put_next (3)");
293 } else {
294 fast_get_avail(x) ;
295 token_info(x) = token_info(p->token);
296 if (h == null) {
297 h = x;
298 } else {
299 token_link(t) = x;
300 }
301 t = x;
302 lua_pop(L, 1);
303 }
304 lua_pop(L, 1);
305 }
306 }
307 }
308 } else {
309 for (i = 1; i <= n; i++) {
310 p = lua_touserdata(L,i);
311 if (p == NULL) {
312 normal_error("token lib","lua <token> expected in put_next (4)");
313 } else if (!lua_getmetatable(L, i)) { /* table mt token mt */
314 normal_error("token lib","lua <token> expected in put_next (5)");
315 } else if (!lua_rawequal(L, m, -1)) {
316 normal_error("token lib","lua <token> expected in put_next (6)");
317 } else {
318 fast_get_avail(x) ;
319 token_info(x) = token_info(p->token);
320 if (h == null) {
321 h = x;
322 } else {
323 token_link(t) = x;
324 }
325 t = x;
326 }
327 lua_pop(L, 1);
328 }
329 }
330 if (h == null) {
331 /* can't happen */
332 } else {
333 begin_token_list(h,4);
334 }
335 lua_settop(L,n);
336 return 0;
337 }
338
339 inline static int unchecked_put_next(lua_State * L)
340 {
341 int n = lua_gettop(L);
342 int i = 1;
343 halfword h = null;
344 halfword t = null;
345 halfword x = null;
346 lua_token *p ;
347 switch (n) {
348 case 0:
349 return 0;
350 case 1:
351 break;
352 default:
353 normal_error("token lib","only one table permitted in put_next");
354 return 0;
355 }
356 if (lua_type(L,1) == LUA_TTABLE) {
357 for (i = 1;; i++) {
358 lua_rawgeti(L, 1, i);
359 p = lua_touserdata(L, -1);
360 if (p == NULL) {
361 break;
362 }
363 fast_get_avail(x);
364 token_info(x) = token_info(p->token);
365 if (h == null) {
366 h = x;
367 } else {
368 token_link(t) = x;
369 }
370 t = x;
371 lua_pop(L, 1);
372 }
373 }
374 begin_token_list(h,0);
375 lua_settop(L,n);
376 return 0;
377 }
378
379 static int run_scan_keyword(lua_State * L)
380 {
381 saved_tex_scanner texstate;
382 const char *s = luaL_checkstring(L, -1);
383 int v = 0;
384 if (s) {
385 save_tex_scanner(texstate);
386 if (scan_keyword(s)) {
387 v = 1;
388 }
389 unsave_tex_scanner(texstate);
390 }
391 lua_pushboolean(L,v);
392 return 1;
393 }
394
395 static int run_scan_keyword_cs(lua_State * L)
396 {
397 saved_tex_scanner texstate;
398 const char *s = luaL_checkstring(L, -1);
399 int v = 0;
400 if (s) {
401 save_tex_scanner(texstate);
402 if (scan_keyword_case_sensitive(s)) {
403 v = 1;
404 }
405 unsave_tex_scanner(texstate);
406 }
407 lua_pushboolean(L,v);
408 return 1;
409 }
410
411 static int run_scan_csname(lua_State * L)
412 {
413 unsigned char *s;
414 int t;
415 saved_tex_scanner texstate;
416 save_tex_scanner(texstate);
417 get_next();
418 t = (cur_cs ? cs_token_flag + cur_cs : token_val(cur_cmd, cur_chr));
419 if (t >= cs_token_flag && ((s = get_cs_text(t - cs_token_flag)) != (unsigned char *) NULL)) {
420 if (is_active_string(s))
421 lua_pushstring(L, (char *) (s + 3));
422 else
423 lua_pushstring(L, (char *) s);
424 } else {
425 lua_pushnil(L);
426 }
427 unsave_tex_scanner(texstate);
428 return 1;
429 }
430
431 static int run_scan_int(lua_State * L)
432 {
433 saved_tex_scanner texstate;
434 int v = 0;
435 save_tex_scanner(texstate);
436 scan_int();
437 v = cur_val;
438 unsave_tex_scanner(texstate);
439 lua_pushinteger(L,(int)v);
440 return 1;
441 }
442
443 /*
444 char * str ;
445 */
446
447 # define declare_buffer \
448 unsigned char word[5 + 1]; \
449 char *uindex = (char *)word; \
450 luaL_Buffer b ; \
451 luaL_buffinit(L,&b) ;
452
453 /*
454 str = (char *) uni2str(cur_chr);
455 luaL_addstring(&b,(char *) str);
456 xfree(str);
457 */
458
459 # define add_to_buffer(chr) \
460 if (chr <= 127) { \
461 luaL_addchar(&b,(unsigned) (char) chr); \
462 } else { \
463 uindex = uni2string((char *)word,(unsigned int) chr); \
464 *uindex = '\0'; \
465 luaL_addstring(&b,(char *) word); \
466 }
467
468 # define push_buffer \
469 luaL_pushresult(&b);
470
471 static int run_scan_float_indeed(lua_State * L, boolean exponent)
472 {
473 saved_tex_scanner texstate;
474 int ok;
475 boolean negative = false;
476 double d;
477 declare_buffer;
478 save_tex_scanner(texstate);
479 /* we collapse as in scan_dimen */
480 while(1) {
481 do {
482 get_x_token();
483 } while (cur_cmd == spacer_cmd);
484 if (cur_tok == minus_token) {
485 negative = !negative;
486 } else if (cur_tok != plus_token) {
487 break;
488 }
489 }
490 if (negative) {
491 add_to_buffer('-');
492 }
493 /* we accept [.,]digits */
494 if (cur_tok == point_token || cur_tok == comma_token) {
495 add_to_buffer('.');
496 while (1) {
497 get_x_token();
498 if ((cur_tok >= zero_token) && (cur_tok <= nine_token)) {
499 add_to_buffer(cur_chr);
500 } else if (exponent) {
501 goto EXPONENT;
502 } else {
503 back_input();
504 goto DONE;
505 }
506 }
507 } else {
508 back_input();
509 while (1) {
510 get_x_token();
511 if ((cur_tok >= zero_token) && (cur_tok <= nine_token)) {
512 add_to_buffer(cur_chr);
513 } else {
514 if (cur_tok == point_token || cur_tok == comma_token) {
515 add_to_buffer('.');
516 while (1) {
517 get_x_token();
518 if ((cur_tok >= zero_token) && (cur_tok <= nine_token)) {
519 add_to_buffer(cur_chr);
520 } else {
521 back_input();
522 break;
523 }
524 }
525 } else if (exponent) {
526 goto EXPONENT;
527 } else {
528 back_input();
529 goto DONE;
530 }
531 }
532 }
533 }
534 EXPONENT:
535 if ((cur_chr == 'E') || (cur_chr == 'e')) {
536 add_to_buffer(cur_chr);
537 get_x_token();
538 if ((cur_tok == minus_token) || (cur_tok == plus_token)) {
539 add_to_buffer(cur_chr);
540 } else if ((cur_tok >= zero_token) && (cur_tok <= nine_token)) {
541 add_to_buffer(cur_chr);
542 }
543 while (1) {
544 get_x_token();
545 if ((cur_tok >= zero_token) && (cur_tok <= nine_token)) {
546 add_to_buffer(cur_chr);
547 } else {
548 break;
549 }
550 }
551 }
552 back_input();
553 DONE:
554 push_buffer;
555 d = lua_tonumberx(L,1,&ok);
556 if (ok) {
557 lua_pushnumber(L,d);
558 } else {
559 lua_pushnil(L);
560 }
561 unsave_tex_scanner(texstate);
562 return 1;
563 }
564
565 static int run_scan_float(lua_State * L)
566 {
567 return run_scan_float_indeed(L,true);
568 }
569
570 static int run_scan_real(lua_State * L)
571 {
572 return run_scan_float_indeed(L,false);
573 }
574
575 static int run_scan_dimen(lua_State * L)
576 {
577 saved_tex_scanner texstate;
578 int v = 0, o = 0;
579 int inf = false, mu = false;
580 int t = lua_gettop(L);
581 if (t>0)
582 inf = lua_toboolean(L,1); /* inf values allowed ?*/
583 if (t>1)
584 mu = lua_toboolean(L,2); /* mu units required ?*/
585 save_tex_scanner(texstate);
586 scan_dimen(mu,inf, false); /* arg3 = shortcut */
587 v = cur_val;
588 o = cur_order;
589 unsave_tex_scanner(texstate);
590 lua_pushinteger(L,v);
591 if (inf) {
592 lua_pushinteger(L,(lua_Number)o);
593 return 2;
594 } else {
595 return 1;
596 }
597 }
598
599 static int run_scan_glue(lua_State * L)
600 {
601 saved_tex_scanner texstate;
602 int v = 0;
603 int mu = false;
604 int t = lua_gettop(L);
605 if (t>0)
606 mu = lua_toboolean(L,1); /* mu units required ?*/
607 save_tex_scanner(texstate);
608 scan_glue((mu ? mu_val_level : glue_val_level));
609 v = cur_val; /* which is a glue_spec node */
610 unsave_tex_scanner(texstate);
611 lua_nodelib_push_fast(L,(halfword)v);
612 return 1;
613 }
614
615 static int run_scan_toks(lua_State * L)
616 {
617 saved_tex_scanner texstate;
618 int macro_def = false, xpand = false;
619 halfword t, saved_defref;
620 int i = 1;
621 int top = lua_gettop(L);
622 if (top>0)
623 macro_def = lua_toboolean(L,1); /* \\def ? */
624 if (top>1)
625 xpand = lua_toboolean(L,2); /* expand ? */
626 save_tex_scanner(texstate);
627 saved_defref = def_ref;
628 (void) scan_toks(macro_def, xpand);
629 t = def_ref;
630 unsave_tex_scanner(texstate);
631 def_ref = saved_defref;
632 /* This function returns a pointer to the tail of a new token
633 list, and it also makes |def_ref| point to the reference count at the
634 head of that list. */
635 lua_newtable(L);
636 while (token_link(t)) {
637 t = token_link(t);
638 push_token(L,t);
639 lua_rawseti(L,-2,i++);
640 }
641 return 1;
642 }
643
644 static int run_scan_string(lua_State * L) /* HH */
645 { /* can be simplified, no need for intermediate list */
646 saved_tex_scanner texstate;
647 halfword t, saved_defref;
648 save_tex_scanner(texstate);
649 do {
650 get_x_token();
651 } while ((cur_cmd == spacer_cmd) || (cur_cmd == relax_cmd));
652 if (cur_cmd == left_brace_cmd) {
653 back_input();
654 saved_defref = def_ref;
655 (void) scan_toks(false, true);
656 t = def_ref;
657 def_ref = saved_defref;
658 tokenlist_to_luastring(L,t);
659 flush_list(t);
660 } else if (cur_cmd == call_cmd) {
661 t = token_link(cur_chr);
662 tokenlist_to_luastring(L,t);
663 flush_list(t);
664 } else if (cur_cmd == 11 || cur_cmd == 12 ) {
665 declare_buffer;
666 while (1) {
667 add_to_buffer(cur_chr);
668 get_x_token();
669 if (cur_cmd != 11 && cur_cmd != 12 ) {
670 break ;
671 }
672 }
673 back_input();
674 push_buffer;
675 } else {
676 back_input();
677 lua_pushnil(L);
678 }
679 unsave_tex_scanner(texstate);
680 return 1;
681 }
682
683 static int run_scan_argument(lua_State * L) /* HH */
684 { /* can be simplified, no need for intermediate list */
685 saved_tex_scanner texstate;
686 halfword t, saved_defref;
687 save_tex_scanner(texstate);
688 do {
689 get_token();
690 } while ((cur_cmd == spacer_cmd) || (cur_cmd == relax_cmd));
691 if (cur_cmd == left_brace_cmd) {
692 int exp = 1;
693 if (lua_type(L, 1) == LUA_TBOOLEAN) {
694 exp = lua_toboolean(L, 1);
695 }
696 back_input();
697 saved_defref = def_ref;
698 (void) scan_toks(false, exp);
699 t = def_ref;
700 def_ref = saved_defref;
701 tokenlist_to_luastring(L,t);
702 flush_list(t);
703 } else if (cur_cmd == call_cmd) {
704 halfword saved_cur_tok = cur_tok;
705 int exp = 1;
706 if (lua_type(L, 1) == LUA_TBOOLEAN) {
707 exp = lua_toboolean(L, 1);
708 }
709 cur_tok = right_brace_token + '}';
710 back_input();
711 if (exp) {
712 cur_tok = saved_cur_tok;
713 back_input();
714 } else {
715 expand();
716 }
717 cur_tok = left_brace_token + '{';
718 back_input();
719 saved_defref = def_ref;
720 (void) scan_toks(false, exp);
721 t = def_ref;
722 def_ref = saved_defref;
723 tokenlist_to_luastring(L,t);
724 flush_list(t);
725 } else if (cur_cmd == 11 || cur_cmd == 12 ) {
726 declare_buffer;
727 while (1) {
728 add_to_buffer(cur_chr);
729 get_x_token();
730 if (cur_cmd != 11 && cur_cmd != 12 ) {
731 break ;
732 }
733 }
734 back_input();
735 push_buffer;
736 } else {
737 back_input();
738 lua_pushnil(L);
739 }
740 unsave_tex_scanner(texstate);
741 return 1;
742 }
743
744 static int run_scan_word(lua_State * L) /* HH */
745 {
746 saved_tex_scanner texstate;
747 save_tex_scanner(texstate);
748 do {
749 get_x_token();
750 } while ((cur_cmd == spacer_cmd) || (cur_cmd == relax_cmd));
751 if (cur_cmd == 11 || cur_cmd == 12 ) {
752 declare_buffer;
753 while (1) {
754 add_to_buffer(cur_chr);
755 get_x_token();
756 if (cur_cmd != 11 && cur_cmd != 12 ) {
757 break ;
758 }
759 }
760 back_input();
761 push_buffer;
762 } else {
763 back_input();
764 lua_pushnil(L);
765 }
766 unsave_tex_scanner(texstate);
767 return 1;
768 }
769
770 static int run_scan_code(lua_State * L) /* HH */
771 {
772 saved_tex_scanner texstate;
773 int cc = DEFAULT_SCAN_CODE_SET ;
774 save_tex_scanner(texstate);
775 get_x_token();
776 if (cur_cmd < 16) {
777 if (lua_gettop(L)>0) {
778 cc = (int) lua_tointeger(L,-1);
779 if (cc == null) {
780 /* todo: message that we choose a default */
781 cc = DEFAULT_SCAN_CODE_SET ;
782 }
783 }
784 if (cc & (1<<(cur_cmd))) {
785 lua_pushinteger(L,(int)cur_chr);
786 } else {
787 lua_pushnil(L);
788 back_input();
789 }
790 } else {
791 lua_pushnil(L);
792 back_input();
793 }
794 unsave_tex_scanner(texstate);
795 return 1;
796 }
797
798 static int lua_tokenlib_is_token(lua_State * L) /* HH */
799 {
800 lua_pushboolean(L,maybe_istoken(L,1)==NULL ? 0 : 1);
801 return 1;
802 }
803
804 static int run_expand(lua_State * L)
805 {
806 (void) L;
807 expand();
808 return 0;
809 }
810
811 static int run_lookup(lua_State * L)
812 {
813 const char *s;
814 size_t l;
815 int cs, cmd, chr;
816 if (lua_type(L, -1) == LUA_TSTRING) {
817 s = lua_tolstring(L, -1, &l);
818 if (l > 0) {
819 cs = string_lookup(s, l);
820 cmd = eq_type(cs);
821 chr = equiv(cs);
822 make_new_token(L, cmd, chr, cs);
823 return 1;
824 }
825 }
826 lua_pushnil(L);
827 return 1;
828 }
829
830 /*
831 static int lua_tokenlib_is_defined(lua_State * L)
832 {
833 const char *s;
834 size_t l;
835 if (lua_type(L, -1) == LUA_TSTRING) {
836 s = lua_tolstring(L, -1, &l);
837 if (l > 0) {
838 lua_pushboolean(L,string_lookup(s, l) != undefined_control_sequence);
839 return 1;
840 }
841 }
842 lua_pushnil(L);
843 return 1;
844 }
845 */
846
847 static int lua_tokenlib_is_defined(lua_State * L)
848 {
849 int b = 0;
850 if (lua_type(L, 1) == LUA_TSTRING) {
851 size_t l;
852 const char *s = lua_tolstring(L, 1, &l);
853 if (l > 0) {
854 int cs = string_lookup(s, l);
855 if (lua_toboolean(L, 2)) {
856 b = cs != undefined_control_sequence;
857 } else {
858 b = eq_type(cs) != undefined_cs_cmd;
859 }
860 }
861 }
862 lua_pushboolean(L, b);
863 return 1;
864 }
865
866 static int run_build(lua_State * L)
867 {
868 if (lua_type(L, 1) == LUA_TNUMBER) {
869 int cs = 0;
870 int chr = (int) lua_tointeger(L, 1);
871 int cmd = (int) luaL_optinteger(L, 2, get_cat_code(cat_code_table_par,chr));
872 if (cmd == 0 || cmd == 9 || cmd == 14 || cmd == 15) {
873 formatted_warning("token lib","not a good token, catcode %i can not be returned, so 12 will be used",(int) cmd);
874 cmd = 12;
875 } else if (cmd == 13) {
876 cs = active_to_cs(chr, false);
877 cmd = eq_type(cs);
878 chr = equiv(cs);
879 }
880 make_new_token(L, cmd, chr, cs);
881 return 1;
882 } else {
883 return run_lookup(L);
884 }
885 }
886
887 static int run_new(lua_State * L)
888 {
889 int cs = 0;
890 int chr = (int) lua_tointeger(L, 1);
891 int cmd = (int) lua_tointeger(L, 2);
892 make_new_token(L, cmd, chr, cs);
893 return 1;
894 }
895
896 /* token instance functions */
897
898 static int lua_tokenlib_free(lua_State * L)
899 {
900 lua_token *n;
901 n = check_istoken(L, 1);
902 if (n->origin == LUA_ORIGIN) {
903 free_avail(n->token);
904 }
905 return 1;
906 }
907
908 /* fast accessors */
909
910 inline static int lua_tokenlib_get_command(lua_State * L)
911 {
912 lua_token *n = check_istoken(L, 1);
913 halfword t = token_info(n->token);
914 if (t >= cs_token_flag) {
915 lua_pushinteger(L,(int) eq_type((t - cs_token_flag)));
916 } else {
917 lua_pushinteger(L, token_cmd(t));
918 }
919 return 1;
920 }
921
922 inline static int lua_tokenlib_get_index(lua_State * L)
923 {
924 int cmd, chr;
925 lua_token *n = check_istoken(L, 1);
926 halfword t = token_info(n->token);
927 if (t >= cs_token_flag) {
928 cmd = eq_type(t - cs_token_flag);
929 chr = equiv(t - cs_token_flag);
930 } else {
931 cmd = token_cmd(t);
932 chr = token_chr(t);
933 }
934 switch (cmd) {
935 case assign_int_cmd:
936 chr -= count_base;
937 break;
938 case assign_attr_cmd:
939 chr -= attribute_base;
940 break;
941 case assign_dimen_cmd:
942 chr -= scaled_base;
943 break;
944 case assign_glue_cmd:
945 chr -= skip_base;
946 break;
947 case assign_mu_glue_cmd:
948 chr -= mu_skip_base;
949 break;
950 case assign_toks_cmd:
951 chr -= toks_base;
952 break;
953 default:
954 break;
955 }
956 if (chr >= 0 && chr <= 65535) {
957 lua_pushinteger(L, chr);
958 } else {
959 lua_pushnil(L);
960 }
961 return 1;
962 }
963
964 inline static int lua_tokenlib_get_mode(lua_State * L)
965 {
966 lua_token *n = check_istoken(L, 1);
967 halfword t = token_info(n->token);
968 if (t >= cs_token_flag) {
969 lua_pushinteger(L, equiv(t - cs_token_flag));
970 } else {
971 lua_pushinteger(L, token_chr(t));
972 }
973 return 1;
974 }
975
976 inline static int lua_tokenlib_get_cmdname(lua_State * L)
977 {
978 lua_token *n = check_istoken(L, 1);
979 halfword t = token_info(n->token);
980 int cmd = (t >= cs_token_flag ? eq_type(t - cs_token_flag) : token_cmd(t));
981 lua_push_string_by_index(L, command_names[cmd].lua);
982 return 1;
983 }
984
985 inline static int lua_tokenlib_get_csname(lua_State * L)
986 {
987 lua_token *n = check_istoken(L, 1);
988 halfword t = token_info(n->token);
989 unsigned char *s;
990 if (t >= cs_token_flag && ((s = get_cs_text(t - cs_token_flag)) != (unsigned char *) NULL)) {
991 if (is_active_string(s))
992 lua_pushstring(L, (char *) (s + 3));
993 else
994 lua_pushstring(L, (char *) s);
995 } else {
996 lua_pushnil(L);
997 }
998 return 1;
999 }
1000
1001 inline static int lua_tokenlib_get_id(lua_State * L)
1002 {
1003 lua_token *n = check_istoken(L, 1);
1004 lua_pushinteger(L, n->token);
1005 return 1;
1006 }
1007
1008 inline static int lua_tokenlib_get_tok(lua_State * L)
1009 {
1010 lua_token *n = check_istoken(L, 1);
1011 halfword t = token_info(n->token);
1012 lua_pushinteger(L, t);
1013 return 1;
1014 }
1015
1016 inline static int lua_tokenlib_get_active(lua_State * L)
1017 {
1018 lua_token *n = check_istoken(L, 1);
1019 halfword t = token_info(n->token);
1020 unsigned char *s;
1021 if (t >= cs_token_flag && ((s = get_cs_text(t - cs_token_flag)) != (unsigned char *) NULL)) {
1022 if (is_active_string(s))
1023 lua_pushboolean(L,1);
1024 else
1025 lua_pushboolean(L,0);
1026 free(s);
1027 } else {
1028 lua_pushboolean(L,0);
1029 }
1030 return 1;
1031 }
1032
1033 inline static int lua_tokenlib_get_expandable(lua_State * L)
1034 {
1035 lua_token *n = check_istoken(L, 1);
1036 halfword t = token_info(n->token);
1037 int cmd = (t >= cs_token_flag ? eq_type(t - cs_token_flag) : token_cmd(t));
1038 if (cmd > max_command_cmd) {
1039 lua_pushboolean(L, 1);
1040 } else {
1041 lua_pushboolean(L, 0);
1042 }
1043 return 1;
1044 }
1045
1046 inline static int lua_tokenlib_get_protected(lua_State * L)
1047 {
1048 lua_token *n = check_istoken(L, 1);
1049 halfword t = token_info(n->token);
1050 int cmd = (t >= cs_token_flag ? eq_type(t - cs_token_flag) : token_cmd(t));
1051 if (cmd > max_command_cmd && (cmd >= call_cmd) && (cmd < end_template_cmd)) {
1052 int chr = (t >= cs_token_flag ? equiv(t - cs_token_flag) : token_chr(t));
1053 if (token_info(token_link(chr)) == protected_token) {
1054 lua_pushboolean(L, 1);
1055 } else {
1056 lua_pushboolean(L, 0);
1057 }
1058 } else {
1059 lua_pushboolean(L, 0);
1060 }
1061 return 1;
1062 }
1063
1064 static int lua_tokenlib_getfield(lua_State * L)
1065 {
1066 const char *s = lua_tostring(L, 2);
1067 if (lua_key_eq(s, command)) {
1068 return lua_tokenlib_get_command(L);
1069 } else if (lua_key_eq(s, index)) {
1070 return lua_tokenlib_get_index(L);
1071 } else if (lua_key_eq(s, mode)) {
1072 return lua_tokenlib_get_mode(L);
1073 } else if (lua_key_eq(s, cmdname)) {
1074 return lua_tokenlib_get_cmdname(L);
1075 } else if (lua_key_eq(s, csname)) {
1076 return lua_tokenlib_get_csname(L);
1077 } else if (lua_key_eq(s, id)) {
1078 return lua_tokenlib_get_id(L);
1079 } else if (lua_key_eq(s, tok)) {
1080 return lua_tokenlib_get_tok(L);
1081 } else if (lua_key_eq(s, active)) {
1082 return lua_tokenlib_get_active(L);
1083 } else if (lua_key_eq(s, expandable)) {
1084 return lua_tokenlib_get_expandable(L);
1085 } else if (lua_key_eq(s, protected)) {
1086 return lua_tokenlib_get_protected(L);
1087 } else {
1088 lua_pushnil(L);
1089 }
1090 return 1;
1091 }
1092
1093 /* end */
1094
1095 static int lua_tokenlib_equal(lua_State * L)
1096 {
1097 lua_token *n, *m;
1098 n = check_istoken(L, 1);
1099 m = check_istoken(L, 2);
1100 if (token_info(n->token) == token_info(m->token)) {
1101 lua_pushboolean(L,1);
1102 return 1;
1103 }
1104 lua_pushboolean(L,0);
1105 return 1;
1106 }
1107
1108 static int lua_tokenlib_tostring(lua_State * L)
1109 {
1110 char *msg;
1111 lua_token *n;
1112 n = check_istoken(L, 1);
1113 msg = xmalloc(256);
1114 snprintf(msg, 255, "<%s token %d: %d>", (n->origin==LUA_ORIGIN?"lua":"tex"), n->token , token_info(n->token));
1115 lua_pushstring(L, msg);
1116 free(msg);
1117 return 1;
1118 }
1119
1120 static int lua_tokenlib_type(lua_State * L)
1121 {
1122 if (maybe_istoken(L,1)!=NULL) {
1123 lua_pushstring(L,"token");
1124 } else {
1125 lua_pushnil(L);
1126 }
1127 return 1;
1128 }
1129
1130 static int run_scan_token(lua_State * L)
1131 {
1132 saved_tex_scanner texstate;
1133 save_tex_scanner(texstate);
1134 get_x_token();
1135 make_new_token(L, cur_cmd, cur_chr, cur_cs);
1136 unsave_tex_scanner(texstate);
1137 return 1;
1138 }
1139
1140 static int run_scan_list(lua_State * L)
1141 {
1142 saved_tex_scanner texstate;
1143 save_tex_scanner(texstate);
1144 /*tex
1145 This is s tricky call as we are in \LUA\ and therefore
1146 mess with the main loop.
1147 */
1148 lua_nodelib_push_fast(L, local_scan_box());
1149 unsave_tex_scanner(texstate);
1150 return 1;
1151 }
1152
1153 /* experiment */
1154
1155 /* [catcodetable] csname content : \def\csname{content} */
1156 /* [catcodetable] csname content global : \gdef\csname{content} */
1157 /* [catcodetable] csname : \def\csname{} */
1158
1159 /* TODO: check for a quick way to set a macro to empty (HH) */
1160
1161 static int get_meaning(lua_State * L)
1162 {
1163 const char *name = null;
1164 size_t lname = 0;
1165 int cs, cmd;
1166 if (lua_type(L, 1) == LUA_TSTRING) {
1167 name = lua_tolstring(L, 1, &lname);
1168 cs = string_lookup(name, lname);
1169 cmd = eq_type(cs);
1170 if (cmd >= call_cmd) {
1171 int chr = equiv(cs);
1172 char *str = tokenlist_to_cstring(chr, true, NULL);
1173 lua_pushstring(L, str);
1174 free(str);
1175 return 1;
1176 }
1177 }
1178 return 0;
1179 }
1180
1181 static int get_macro(lua_State * L)
1182 {
1183 const char *name = null;
1184 size_t lname = 0;
1185 int cs, cmd;
1186 if (lua_type(L, 1) == LUA_TSTRING) {
1187 name = lua_tolstring(L, 1, &lname);
1188 cs = string_lookup(name, lname);
1189 cmd = eq_type(cs);
1190 if (cmd >= call_cmd) {
1191 /*
1192 Expanding would expand in-place, unless we make a copy which we don't want to
1193 do. So, we just pass the meaning i.e. no: expand_macros_in_tokenlist(chr).
1194
1195 Actually it would be nice to adapt tokenlist_to_cstring with an extra argument
1196 indicating that we are not interested in the before -> part.
1197 */
1198 int chr = equiv(cs);
1199 char *str = tokenlist_to_xstring(chr, true, NULL);
1200 lua_pushstring(L, str);
1201 free(str);
1202 return 1;
1203 }
1204 }
1205 return 0;
1206 }
1207
1208 static int set_lua(lua_State *L)
1209 {
1210 const char *name = null;
1211 const char *s = null;
1212 size_t lname = 0;
1213 int cs;
1214 int n = lua_gettop(L);
1215 int a = 0; /* global state */
1216 int p = 0; /* protected state */
1217 int f = 0; /* function index */
1218 int nncs = no_new_control_sequence;
1219 if (n < 2) {
1220 return 0 ;
1221 }
1222 name = lua_tolstring(L, 1, &lname);
1223 if (name == null) {
1224 return 0 ;
1225 }
1226 f = lua_tointeger(L, 2);
1227 if (n > 2) {
1228 s = lua_tostring(L, 3);
1229 if (s) {
1230 if (lua_key_eq(s, global)) {
1231 a = 4;
1232 } else if (lua_key_eq(s, protected)) {
1233 p = 1;
1234 }
1235 }
1236 if (n > 3) {
1237 s = lua_tostring(L, 4);
1238 if (s) {
1239 if (lua_key_eq(s, global)) {
1240 a = 4;
1241 } else if (lua_key_eq(s, protected)) {
1242 p = 1;
1243 }
1244 }
1245 }
1246 }
1247 no_new_control_sequence = false ;
1248 cs = string_lookup(name, lname);
1249 no_new_control_sequence = nncs;
1250 if (p) {
1251 define(cs, lua_call_cmd, f);
1252 } else {
1253 define(cs, lua_expandable_call_cmd, f);
1254 }
1255 return 0;
1256 }
1257
1258 static int set_macro(lua_State * L)
1259 {
1260 const char *name = null;
1261 const char *str = null;
1262 const char *s = null;
1263 size_t lname = 0;
1264 size_t lstr = 0;
1265 int cs, cc, ct;
1266 int n = lua_gettop(L);
1267 int a = 0 ; /* global state */
1268 int nncs = no_new_control_sequence;
1269 if (n == 0) {
1270 return 0 ;
1271 }
1272 if (lua_type(L, 1) == LUA_TNUMBER) {
1273 if (n == 1)
1274 return 0;
1275 ct = (int) lua_tointeger(L, 1);
1276 if (!valid_catcode_table(ct)) {
1277 ct = cat_code_table_par;
1278 }
1279 name = lua_tolstring(L, 2, &lname);
1280 if (n > 2)
1281 str = lua_tolstring(L, 3, &lstr);
1282 if (n > 3)
1283 s = lua_tostring(L, 4);
1284 } else {
1285 ct = cat_code_table_par;
1286 name = lua_tolstring(L, 1, &lname);
1287 if (n > 1)
1288 str = lua_tolstring(L, 2, &lstr);
1289 if (n > 2)
1290 s = lua_tostring(L, 3);
1291 }
1292 if (name == null) {
1293 return 0 ;
1294 }
1295 if (s && (lua_key_eq(s, global))) {
1296 a = 4;
1297 }
1298 no_new_control_sequence = false ;
1299 cs = string_lookup(name, lname);
1300 no_new_control_sequence = nncs;
1301 if (lstr > 0) {
1302 halfword p; /* tail of the token list */
1303 halfword q; /* new node being added to the token list via |store_new_token| */
1304 halfword t; /* token being appended */
1305 const char *se = str + lstr;
1306 p = temp_token_head;
1307 set_token_link(p, null);
1308 /* reference count */
1309 fast_store_new_token(0);
1310 /* this ends the not present arguments, and no: we will not support arguments here*/
1311 fast_store_new_token(end_match_token);
1312 while (str < se) {
1313 /* hh: str2uni could return len too (also elsewhere) */
1314 t = (halfword) str2uni((const unsigned char *) str);
1315 str += utf8_size(t);
1316 cc = get_cat_code(ct,t);
1317 /* this is a relating simple converter; if more is needed one can just use */
1318 /* tex.print with a regular \def or \gdef and feed the string into the regular */
1319 /* scanner; */
1320 if (cc == 0) {
1321 /* we have a potential control sequence so we check for it */
1322 int _lname = 0 ;
1323 int _s = 0 ;
1324 int _c = 0 ;
1325 halfword _cs = null ;
1326 const char *_name = str ;
1327 while (str < se) {
1328 t = (halfword) str2uni((const unsigned char *) str);
1329 _s = utf8_size(t);
1330 _c = get_cat_code(ct,t);
1331 if (_c == 11) {
1332 str += _s ;
1333 _lname = _lname + _s ;
1334 } else if (_c == 10) {
1335 /* we ignore a trailing space like normal scanning does unless it is */
1336 /* an '\ ' i.e. an original (unexpandable) control space or protected */
1337 /* macro, or alias; when we have e.g. \def\ {!} we get the expansion */
1338 if (_lname == 0) {
1339 _lname = _lname + _s ;
1340 }
1341 str += _s ;
1342 break ;
1343 } else {
1344 if (_lname == 0) {
1345 _lname = _lname + _s ;
1346 str += _s ;
1347 }
1348 break ;
1349 }
1350 }
1351 if (_lname > 0) {
1352 /* we have a potential \cs */
1353 _cs = string_lookup(_name, _lname);
1354 if (_cs == undefined_control_sequence) {
1355 /* let's play safe and backtrack */
1356 t = cc * (1<<21) + t ;
1357 str = _name ;
1358 } else {
1359 t = cs_token_flag + _cs;
1360 }
1361 } else {
1362 /* just a character with some meaning, so \unknown becomes effectively */
1363 /* \\unknown assuming that \\ has some useful meaning of course */
1364 t = cc * (1<<21) + t ;
1365 str = _name ;
1366 }
1367
1368 } else {
1369 /* whatever token, so for instance $x^2$ just works given a tex */
1370 /* catcode regime */
1371 t = cc * (1<<21) + t ;
1372 }
1373 fast_store_new_token(t);
1374 }
1375 /* there is no fast_store_new_token(right_brace_token) needed */
1376 define(cs, call_cmd + (a % 4), token_link(temp_token_head));
1377 } else {
1378 halfword p ;
1379 halfword q; /* new node being added to the token list via |store_new_token| */
1380 p = temp_token_head;
1381 set_token_info(p,null);
1382 fast_store_new_token(0);
1383 fast_store_new_token(end_match_token);
1384 define(cs, call_cmd + (a % 4), token_link(temp_token_head));
1385 }
1386 return 0;
1387 }
1388
1389 static int set_char(lua_State * L)
1390 {
1391 const char *name = null;
1392 const char *s = null;
1393 size_t lname = 0;
1394 int cs, value;
1395 int n = lua_gettop(L);
1396 int a = 0 ; /* global state */
1397 int nncs = no_new_control_sequence;
1398 if (n < 2)
1399 return 0;
1400 name = lua_tolstring(L, 1, &lname);
1401 if (name == NULL)
1402 return 0;
1403 value = lua_tointeger(L, 2);
1404 if (value < 0)
1405 return 0;
1406 if (n > 2)
1407 s = lua_tostring(L, 3);
1408 if (s && (lua_key_eq(s, global))) {
1409 a = 4;
1410 }
1411 no_new_control_sequence = false ;
1412 cs = string_lookup(name, lname);
1413 no_new_control_sequence = nncs;
1414 define(cs, char_given_cmd, value);
1415 return 0;
1416 }
1417
1418 static int get_command_names(lua_State * L)
1419 {
1420 int i;
1421 lua_createtable(L,data_cmd+1,0);
1422 for (i = 0; command_names[i].lua != 0; i++) {
1423 lua_rawgeti(L, LUA_REGISTRYINDEX, command_names[i].lua);
1424 lua_rawseti(L, -2, i);
1425 }
1426 return 1;
1427 }
1428
1429 static const struct luaL_Reg tokenlib[] = {
1430 { "type", lua_tokenlib_type },
1431 { "create", run_build },
1432 { "new", run_new },
1433 { "is_token", lua_tokenlib_is_token },
1434 { "is_defined", lua_tokenlib_is_defined },
1435 { "commands", get_command_names },
1436 { "command_id", run_get_command_id },
1437 { "biggest_char", run_get_biggest_char },
1438 /* scanners */
1439 { "get_next", run_get_next },
1440 { "scan_keyword", run_scan_keyword },
1441 { "scan_keyword_cs", run_scan_keyword_cs },
1442 { "scan_int", run_scan_int },
1443 { "scan_float", run_scan_float },
1444 { "scan_real", run_scan_real },
1445 { "scan_dimen", run_scan_dimen },
1446 { "scan_glue", run_scan_glue },
1447 { "scan_toks", run_scan_toks },
1448 { "scan_code", run_scan_code },
1449 { "scan_string", run_scan_string },
1450 { "scan_argument", run_scan_argument },
1451 { "scan_word", run_scan_word },
1452 { "scan_csname", run_scan_csname },
1453 { "scan_token", run_scan_token }, /* expands next token if needed */
1454 { "scan_list", run_scan_list },
1455 /* writers */
1456 { "put_next", run_put_next },
1457 { "unchecked_put_next", unchecked_put_next },
1458 { "expand", run_expand },
1459 /* getters */
1460 { "get_command", lua_tokenlib_get_command },
1461 { "get_index", lua_tokenlib_get_index },
1462 { "get_mode", lua_tokenlib_get_mode },
1463 { "get_cmdname", lua_tokenlib_get_cmdname },
1464 { "get_csname", lua_tokenlib_get_csname },
1465 { "get_id", lua_tokenlib_get_id },
1466 { "get_tok", lua_tokenlib_get_tok },
1467 { "get_active", lua_tokenlib_get_active },
1468 { "get_expandable", lua_tokenlib_get_expandable },
1469 { "get_protected", lua_tokenlib_get_protected },
1470 { "get_macro", get_macro },
1471 { "get_meaning", get_meaning },
1472 /* setters */
1473 { "set_macro", set_macro },
1474 { "set_char", set_char },
1475 { "set_lua", set_lua },
1476 {NULL, NULL}
1477 };
1478
1479 static const struct luaL_Reg tokenlib_m[] = {
1480 {"__index", lua_tokenlib_getfield},
1481 {"__tostring", lua_tokenlib_tostring},
1482 {"__eq", lua_tokenlib_equal},
1483 {"__gc", lua_tokenlib_free},
1484 {NULL, NULL} /* sentinel */
1485 };
1486
1487 int luaopen_token(lua_State * L)
1488 {
1489 /* the main metatable of token userdata */
1490 luaL_newmetatable(L, TOKEN_METATABLE);
1491 luaL_openlib(L, NULL, tokenlib_m, 0);
1492 luaL_openlib(L, "token", tokenlib, 0);
1493 return 1;
1494 }