1
/* $Id: ltokenlib.c 1013 2008-02-14 00:09:02Z oneiros $ */
3
Copyright 2006-2008 Taco Hoekwater <taco@luatex.org>
5
This file is part of LuaTeX.
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.
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.
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/>. */
3
20
#include "luatex-api.h"
4
21
#include <ptexlib.h>
23
static const char _svn_version[] =
24
"$Id: ltokenlib.c 1348 2008-06-30 14:59:42Z taco $ $URL: http://scm.foundry.supelec.fr/svn/luatex/tags/beta-0.28.0/src/texk/web2c/luatexdir/lua/ltokenlib.c $";
7
extern int get_command_id (char *);
26
extern int get_command_id(char *);
9
28
static int max_command = 0;
10
29
static int hash_base = 0;
11
static int active_base = 0;
12
30
static int null_cs = 0;
14
32
#define protected_token 0x1C00001
16
34
#define is_valid_token(L,i) (lua_istable(L,i) && lua_objlen(L,i)==3)
17
#define get_token_cmd(L,i) lua_rawgeti(L,i,1)
18
#define get_token_chr(L,i) lua_rawgeti(L,i,2)
19
#define get_token_cs(L,i) lua_rawgeti(L,i,3)
22
test_expandable (lua_State *L) {
24
if (is_valid_token(L,-1)) {
26
if (lua_isnumber(L,-1)) {
27
cmd = lua_tointeger(L,-1);
28
} else if (lua_isstring(L,-1)) {
29
cmd = get_command_id((char *)lua_tostring(L,-1));
31
if (cmd>max_command) {
44
test_protected (lua_State *L) {
46
if (is_valid_token(L,-1)) {
48
if (lua_isnumber(L,-1)) {
49
chr = lua_tointeger(L,-1);
50
} else if (lua_isstring(L,-1)) {
51
chr = get_command_id((char *)lua_tostring(L,-1));
53
if (fixmem[fixmem[chr].hhrh].hhlh==protected_token) {
65
test_activechar (lua_State *L) {
67
if (is_valid_token(L,-1)) {
69
if (lua_isnumber(L,-1)) {
70
cmd = lua_tointeger(L,-1);
72
if (cmd>0 && cmd==protected_token) {
85
run_get_command_name (lua_State *L) {
87
if (is_valid_token(L,-1)) {
89
if (lua_isnumber(L,-1)) {
90
cs = lua_tointeger(L,-1);
91
lua_pushstring(L,command_names[cs].cmd_name);
102
run_get_csname_name (lua_State *L) {
106
if (is_valid_token(L,-1)) {
108
if (lua_isnumber(L,-1)) {
109
cmd = lua_tointeger(L,-1);
114
if (lua_isnumber(L,-1)) {
115
cs = lua_tointeger(L,-1);
119
if (cs != 0 && (n = zget_cs_text(cs)) && n>=0) {
123
lua_pushstring(L,"");
132
run_get_command_id (lua_State *L) {
134
if (lua_isstring(L,-1)) {
135
cs = get_command_id((char *)lua_tostring(L,-1));
137
lua_pushnumber(L,cs);
143
run_get_csname_id (lua_State *L) {
147
if (lua_isstring(L,-1)) {
148
s = (char *)lua_tolstring(L,-1, &k);
149
texstr = maketexlstring(s,k);
150
cs = string_lookup(texstr);
153
lua_pushnumber(L,cs);
159
make_token_table (lua_State *L, int cmd, int chr, int cs) {
160
lua_createtable(L,3,0);
161
lua_pushnumber(L,cmd);
163
lua_pushnumber(L,chr);
165
lua_pushnumber(L,cs);
170
run_get_next (lua_State *L) {
172
save_nncs = no_new_control_sequence;
173
no_new_control_sequence = 0;
175
no_new_control_sequence = save_nncs;
176
make_token_table(L,cur_cmd,cur_chr,cur_cs);
181
run_expand (lua_State *L) {
188
run_lookup (lua_State *L) {
194
if (lua_isstring(L,-1)) {
195
s = (char *)lua_tolstring(L,-1,&l);
197
save_nncs = no_new_control_sequence;
198
no_new_control_sequence = true;
199
cs = id_lookup((last+1),l); /* cleans up the lookup buffer */
200
t = maketexlstring(s,l);
201
cs = string_lookup(t);
203
cmd = zget_eq_type(cs);
204
chr = zget_equiv(cs);
205
make_token_table(L,cmd,chr,cs);
206
no_new_control_sequence = save_nncs;
215
run_build (lua_State *L) {
217
if (lua_isnumber(L,1)) {
219
chr = lua_tointeger(L,1);
220
cmd = luaL_optinteger(L,2,zget_char_cat_code(chr));
221
if (cmd==0 || cmd == 9 || cmd == 14 || cmd == 15) {
222
fprintf(stdout, "\n\nluatex error: not a good token.\nCatcode %i can not be returned, so I replaced it by 12 (other)",(int)cmd);
228
cmd=zget_eq_type(cs);
231
make_token_table(L,cmd,chr,cs);
234
return run_lookup(L);
239
static const struct luaL_reg tokenlib [] = {
240
{"get_next", run_get_next},
241
{"expand", run_expand},
242
{"lookup", run_lookup},
243
{"create", run_build},
244
{"is_expandable",test_expandable},
245
{"is_activechar",test_activechar},
246
{"is_protected", test_protected},
247
{"csname_id", run_get_csname_id},
248
{"csname_name", run_get_csname_name},
249
{"command_name", run_get_command_name},
250
{"command_id", run_get_command_id},
251
{NULL, NULL} /* sentinel */
35
#define get_token_cmd(L,i) lua_rawgeti(L,i,1)
36
#define get_token_chr(L,i) lua_rawgeti(L,i,2)
37
#define get_token_cs(L,i) lua_rawgeti(L,i,3)
38
#define is_active_string(s) (strlen((char *)s)>3 && *s==0xEF && *(s+1)==0xBF && *(s+2)==0xBF)
41
static int test_expandable(lua_State * L)
44
if (is_valid_token(L, -1)) {
46
if (lua_isnumber(L, -1)) {
47
cmd = lua_tointeger(L, -1);
48
} else if (lua_isstring(L, -1)) {
49
cmd = get_command_id((char *) lua_tostring(L, -1));
51
if (cmd > max_command) {
52
lua_pushboolean(L, 1);
54
lua_pushboolean(L, 0);
63
static int test_protected(lua_State * L)
66
if (is_valid_token(L, -1)) {
68
if (lua_isnumber(L, -1)) {
69
chr = lua_tointeger(L, -1);
70
} else if (lua_isstring(L, -1)) {
71
chr = get_command_id((char *) lua_tostring(L, -1));
73
if (fixmem[fixmem[chr].hhrh].hhlh == protected_token) {
74
lua_pushboolean(L, 1);
76
lua_pushboolean(L, 0);
84
static int test_activechar(lua_State * L)
86
if (is_valid_token(L, -1)) {
90
if (lua_isnumber(L, -1)) {
91
cs = lua_tointeger(L, -1);
94
if (cs != 0 && (n = zget_cs_text(cs)) && n > 0) {
95
unsigned char *s = (unsigned char *)makecstring(n);
96
if (is_active_string(s)) {
104
lua_pushboolean(L,0);
109
static int run_get_command_name(lua_State * L)
112
if (is_valid_token(L, -1)) {
113
get_token_cmd(L, -1);
114
if (lua_isnumber(L, -1)) {
115
cs = lua_tointeger(L, -1);
116
lua_pushstring(L, command_names[cs].cmd_name);
118
lua_pushstring(L, "");
127
static int run_get_csname_name(lua_State * L)
131
if (is_valid_token(L, -1)) {
132
get_token_cmd(L, -1);
133
if (lua_isnumber(L, -1)) {
134
cmd = lua_tointeger(L, -1);
139
if (lua_isnumber(L, -1)) {
140
cs = lua_tointeger(L, -1);
144
if (cs != 0 && (n = zget_cs_text(cs)) && n >= 0) {
145
unsigned char *s = (unsigned char *)makecstring(n);
146
if (is_active_string(s))
147
lua_pushstring(L, (char *)(s+3));
149
lua_pushstring(L, (char *)s);
151
lua_pushstring(L, "");
159
static int run_get_command_id(lua_State * L)
162
if (lua_isstring(L, -1)) {
163
cs = get_command_id((char *) lua_tostring(L, -1));
165
lua_pushnumber(L, cs);
170
static int run_get_csname_id(lua_State * L)
175
if (lua_isstring(L, -1)) {
176
s = (char *) lua_tolstring(L, -1, &k);
177
texstr = maketexlstring(s, k);
178
cs = string_lookup(texstr);
181
lua_pushnumber(L, cs);
186
void make_token_table(lua_State * L, int cmd, int chr, int cs)
188
lua_createtable(L, 3, 0);
189
lua_pushnumber(L, cmd);
190
lua_rawseti(L, -2, 1);
191
lua_pushnumber(L, chr);
192
lua_rawseti(L, -2, 2);
193
lua_pushnumber(L, cs);
194
lua_rawseti(L, -2, 3);
197
static int run_get_next(lua_State * L)
200
save_nncs = no_new_control_sequence;
201
no_new_control_sequence = 0;
203
no_new_control_sequence = save_nncs;
204
make_token_table(L, cur_cmd, cur_chr, cur_cs);
208
static int run_expand(lua_State * L)
215
static int run_lookup(lua_State * L)
220
integer cs, cmd, chr;
222
if (lua_isstring(L, -1)) {
223
s = (char *) lua_tolstring(L, -1, &l);
225
save_nncs = no_new_control_sequence;
226
no_new_control_sequence = true;
227
cs = id_lookup((last + 1), l); /* cleans up the lookup buffer */
228
t = maketexlstring(s, l);
229
cs = string_lookup(t);
231
cmd = zget_eq_type(cs);
232
chr = zget_equiv(cs);
233
make_token_table(L, cmd, chr, cs);
234
no_new_control_sequence = save_nncs;
242
static int run_build(lua_State * L)
244
integer cmd, chr, cs;
245
if (lua_isnumber(L, 1)) {
247
chr = lua_tointeger(L, 1);
248
cmd = luaL_optinteger(L, 2, zget_char_cat_code(chr));
249
if (cmd == 0 || cmd == 9 || cmd == 14 || cmd == 15) {
251
"\n\nluatex error: not a good token.\nCatcode %i can not be returned, so I replaced it by 12 (other)",
257
cs = active_to_cs(chr,false);
258
cmd = zget_eq_type(cs);
259
chr = zget_equiv(cs);
261
make_token_table(L, cmd, chr, cs);
264
return run_lookup(L);
269
static const struct luaL_reg tokenlib[] = {
270
{"get_next", run_get_next},
271
{"expand", run_expand},
272
{"lookup", run_lookup},
273
{"create", run_build},
274
{"is_expandable", test_expandable},
275
{"is_activechar", test_activechar},
276
{"is_protected", test_protected},
277
{"csname_id", run_get_csname_id},
278
{"csname_name", run_get_csname_name},
279
{"command_name", run_get_command_name},
280
{"command_id", run_get_command_id},
281
{NULL, NULL} /* sentinel */
254
int luaopen_token (lua_State *L)
284
int luaopen_token(lua_State * L)
256
luaL_register(L, "token", tokenlib);
257
max_command = get_max_command();
258
hash_base = get_hash_base();
259
active_base = get_active_base();
260
null_cs = get_nullcs();
286
luaL_register(L, "token", tokenlib);
287
max_command = get_max_command();
288
hash_base = get_hash_base();
289
null_cs = get_nullcs();