void tokenlist_to_lua(lua_State * L, int p) { int cmd, chr, cs; int v; int i = 1; v = p; while (v != null && v < (int) fix_mem_end) { i++; v = token_link(v); } i = 1; lua_createtable(L, i, 0); while (p != null && p < (int) fix_mem_end) { if (token_info(p) >= cs_token_flag) { cs = token_info(p) - cs_token_flag; cmd = eq_type(cs); chr = equiv(cs); make_token_table(L, cmd, chr, cs); } else { cmd = token_cmd(token_info(p)); chr = token_chr(token_info(p)); make_token_table(L, cmd, chr, 0); } lua_rawseti(L, -2, i++); p = token_link(p); } }
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; }
halfword concat_tokens(halfword q,halfword r) { halfword p; if(q==null) return r; p= q; while(token_link(p)!=null) p= token_link(p); set_token_link(p,token_link(r)); free_avail(r); return q; }
void expand_macros_in_tokenlist(halfword p) { int old_mode; pointer q= get_avail(); pointer r= get_avail(); token_info(q)= right_brace_token+'}'; token_link(q)= r; token_info(r)= end_write_token; begin_token_list(q,inserted); begin_token_list(write_tokens(p),write_text); q= get_avail(); token_info(q)= left_brace_token+'{'; begin_token_list(q,inserted); old_mode= mode; mode= 0; cur_cs= write_loc; q= scan_toks(false,true); get_token(); if(cur_tok!=end_write_token){ const char*hlp[]= { "On this page there's a \\write with fewer real {'s than }'s.", "I can't handle that very well; good luck.",NULL }; tex_error("Unbalanced write command",hlp); do{ get_token(); }while(cur_tok!=end_write_token); } mode= old_mode; end_token_list(); }
void print_mark(int p) { print_char('{'); if((p<(int)fix_mem_min)||(p> (int)fix_mem_end)) tprint_esc("CLOBBERED."); else show_token_list(token_link(p),null,max_print_line-10); print_char('}'); }
int scan_tex_toks_register(int j,int c,lstring s) { int a; halfword ref= get_avail(); (void)str_scan_toks(c,s); set_token_ref_count(ref,0); set_token_link(ref,token_link(temp_token_head)); if(global_defs_par> 0) a= 4; else a= 0; define(j+toks_base,call_cmd,ref); return 0; }
inline static int lua_tokenlib_get_protected(lua_State * L) { lua_token *n = check_istoken(L, 1); halfword t = token_info(n->token); int cmd = (t >= cs_token_flag ? eq_type(t - cs_token_flag) : token_cmd(t)); if (cmd > max_command_cmd && (cmd >= call_cmd) && (cmd < end_template_cmd)) { int chr = (t >= cs_token_flag ? equiv(t - cs_token_flag) : token_chr(t)); if (token_info(token_link(chr)) == protected_token) { lua_pushboolean(L, 1); } else { lua_pushboolean(L, 0); } } else { lua_pushboolean(L, 0); } return 1; }
int tokenlist_from_lua(lua_State * L) { const char *s; int tok, t; size_t i, j; halfword p, q, r; r = get_avail(); token_info(r) = 0; token_link(r) = null; p = r; t = lua_type(L, -1); if (t == LUA_TTABLE) { j = lua_rawlen(L, -1); if (j > 0) { for (i = 1; i <= j; i++) { lua_rawgeti(L, -1, (int) i); tok = token_from_lua(L); if (tok >= 0) { store_new_token(tok); } lua_pop(L, 1); }; } return r; } else if (t == LUA_TSTRING) { s = lua_tolstring(L, -1, &j); for (i = 0; i < j; i++) { if (s[i] == 32) { tok = token_val(10, s[i]); } else { int j1 = (int) str2uni((const unsigned char *) (s + i)); i = i + (size_t) (utf8_size(j1) - 1); tok = token_val(12, j1); } store_new_token(tok); } return r; } else { free_avail(r); return null; } }
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); flush_list(t); } else if (cur_cmd == call_cmd) { t = token_link(cur_chr); tokenlist_to_luastring(L,t); flush_list(t); } else if (cur_cmd == 11 || cur_cmd == 12 ) { declare_buffer; while (1) { add_to_buffer(cur_chr); get_x_token(); if (cur_cmd != 11 && cur_cmd != 12 ) { break ; } } back_input(); push_buffer; } else { back_input(); lua_pushnil(L); } unsave_tex_scanner(texstate); return 1; }
void tex::conditional () { bool b=false; sym s; int m, n; ptr p, q, r; int this_if; ptr save_cond_ptr; int save_scanner_status; push_cond(); save_cond_ptr = cond_ptr; this_if = cur_chr; switch (this_if) { case IF_CHAR_CODE: case IF_CAT_CODE: #define get_x_token_or_active_char() {get_x_token(); \ if (cur_cmd == RELAX && cur_chr == NO_EXPAND_FLAG) { \ cur_cmd = ACTIVE_CHAR; \ cur_chr = tok2sym(cur_tok) - active_base[0]; }} get_x_token_or_active_char(); if (cur_cmd > ACTIVE_CHAR || cur_chr > 255) { m = RELAX; n = 256; } else { m = cur_cmd; n = cur_chr; } get_x_token_or_active_char(); if (cur_cmd > ACTIVE_CHAR || cur_chr > 255) { cur_cmd = RELAX; cur_chr = 256; } if (this_if == IF_CHAR_CODE) { b = n == cur_chr; } else { b = m == cur_cmd; } break; case IF_INT_CODE: case IF_DIM_CODE: if (this_if == IF_INT_CODE) { scan_int(); } else { scan_normal_dimen(); } n = cur_val; get_nbx_token(); if (cur_tok >= OTHER_TOKEN + '<' && cur_tok <= OTHER_TOKEN + '>') { r = cur_tok - OTHER_TOKEN; } else { print_err("Missing = inserted for "); print_cmd_chr(IF_TEST, this_if); help_relation(); back_error(); r = '='; } if (this_if == IF_INT_CODE) { scan_int(); } else { scan_normal_dimen(); } switch (r) { case '<': b = n < cur_val; break; case '=': b = n == cur_val; break; case '>': b = n > cur_val; break; } break; case IF_ODD_CODE: scan_int(); b = odd(cur_val); break; case IF_VMODE_CODE: b = abs(mode) == VMODE; break; case IF_HMODE_CODE: b = abs(mode) == HMODE; break; case IF_MMODE_CODE: b = abs(mode) == MMODE; break; case IF_INNER_CODE: b = mode < 0; break; case IF_VOID_CODE: case IF_HBOX_CODE: case IF_VBOX_CODE: scan_eight_bit_int(); p = box(cur_val); if (this_if == IF_VOID_CODE) { b = p == null; } else if (p == null) { b = FALSE; } else if (this_if == IF_HBOX_CODE) { b = type(p) == HLIST_NODE; } else { b = type(p) == VLIST_NODE; } break; case IFX_CODE: save_scanner_status = scanner_status; scanner_status = NORMAL; get_next(); s = cur_cs; p = cur_cmd; q = cur_chr; get_next(); if (cur_cmd != p) { b = FALSE; } else if (cur_cmd < CALL) { b = cur_chr == q; } else { p = token_link(cur_chr); q = token_link(equiv(s)); if (p == q) { b = TRUE; } else { while (p != null && q != null) { if (token(p) != token(q)) { p = null; } else { p = token_link(p); q = token_link(q); } } b = p == null && q == null; } } scanner_status = save_scanner_status; break; case IF_EOF_CODE: scan_four_bit_int(); b = read_open[cur_val] == CLOSED; break; case IF_TRUE_CODE: b = TRUE; break; case IF_FALSE_CODE: b = FALSE; break; case IF_CASE_CODE: scan_int(); n = cur_val; if (tracing_commands > 1) { begin_diagnostic(); print("{case "); print_int(n); print("}"); end_diagnostic(FALSE); } while (n != 0) { pass_text(); if (cond_ptr == save_cond_ptr) { if (cur_chr == OR_CODE) { decr(n); } else { goto common_end; } } else if (cur_chr == FI_CODE) { pop_cond(); } } change_if_limit(OR_CODE, save_cond_ptr); return; default: break; } if (tracing_commands > 1) { begin_diagnostic(); print(b ? "{true}" : "{false}"); end_diagnostic(FALSE); } if (b) { change_if_limit(ELSE_CODE, save_cond_ptr); return; } loop { pass_text(); if (cond_ptr == save_cond_ptr) { if (cur_chr != OR_CODE) goto common_end; print_err("Extra "); print_esc("or"); help_or(); error(); } else if (cur_chr == FI_CODE) { pop_cond(); } } common_end: if (cur_chr == FI_CODE) { pop_cond(); } else { if_limit = FI_CODE; } }
inline static int run_put_next(lua_State * L) { int n = lua_gettop(L); int m = 0; int i = 0; halfword h = null; halfword t = null; halfword x = null; lua_token *p ; if (n == 0) { /* we accept a single nil argument */ return 0; } lua_get_metatablelua(luatex_token); m = lua_gettop(L); if (lua_type(L,1) == LUA_TTABLE) { if (n>1) { normal_error("token lib","only one table permitted in put_next"); } else { for (i = 1;; i++) { lua_rawgeti(L, 1, i); /* table mt token */ if (lua_type(L,-1) == LUA_TNIL) { break; } else { p = lua_touserdata(L, -1); if (p == NULL) { normal_error("token lib","lua <token> expected in put_next (1)"); } else if (!lua_getmetatable(L, -1)) { /* table mt token mt */ normal_error("token lib","lua <token> expected in put_next (2)"); } else if (!lua_rawequal(L, m, -1)) { normal_error("token lib","lua <token> expected in put_next (3)"); } else { fast_get_avail(x) ; token_info(x) = token_info(p->token); if (h == null) { h = x; } else { token_link(t) = x; } t = x; } lua_pop(L, 1); } } } } else { for (i = 1; i <= n; i++) { p = lua_touserdata(L,i); if (p == NULL) { normal_error("token lib","lua <token> expected in put_next (4)"); } else if (!lua_getmetatable(L, i)) { /* table mt token mt */ normal_error("token lib","lua <token> expected in put_next (5)"); } else if (!lua_rawequal(L, m, -1)) { normal_error("token lib","lua <token> expected in put_next (6)"); } else { fast_get_avail(x) ; token_info(x) = token_info(p->token); if (h == null) { h = x; } else { token_link(t) = x; } t = x; } lua_pop(L, 1); } } if (h == null) { /* can't happen */ } else { begin_token_list(h,0); } lua_settop(L,n); return 0; }
void tex::print_mark(ptr p) { print("{"); show_token_list(token_link(p), null, MAX_PRINT_LINE - 10); print("}"); }