| 1 |
/* |
| 2 |
|
| 3 |
Copyright 2009-2010 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 |
#include "ptexlib.h" |
| 23 |
|
| 24 |
/*tex |
| 25 |
|
| 26 |
Only a dozen or so command codes |>max_command| can possibly be returned by |
| 27 |
|get_next|; in increasing order, they are |undefined_cs|, |expand_after|, |
| 28 |
|no_expand|, |input|, |if_test|, |fi_or_else|, |cs_name|, |convert|, |the|, |
| 29 |
|top_bot_mark|, |call|, |long_call|, |outer_call|, |long_outer_call|, and |
| 30 |
|end_template|. |
| 31 |
|
| 32 |
Sometimes, recursive calls to the following |expand| routine may cause |
| 33 |
exhaustion of the run-time calling stack, resulting in forced execution stops |
| 34 |
by the operating system. To diminish the chance of this happening, a counter |
| 35 |
is used to keep track of the recursion depth, in conjunction with a constant |
| 36 |
called |expand_depth|. |
| 37 |
|
| 38 |
Note that this does not catch all possible infinite recursion loops, just the |
| 39 |
ones that exhaust the application calling stack. The actual maximum value of |
| 40 |
|expand_depth| is outside of our control, but the initial setting of |100| |
| 41 |
should be enough to prevent problems. |
| 42 |
|
| 43 |
*/ |
| 44 |
|
| 45 |
static int expand_depth_count = 0; |
| 46 |
|
| 47 |
/*tex |
| 48 |
|
| 49 |
The |expand| subroutine is used when |cur_cmd>max_command|. It removes a |
| 50 |
``call'' or a conditional or one of the other special operations just listed. |
| 51 |
It follows that |expand| might invoke itself recursively. In all cases, |
| 52 |
|expand| destroys the current token, but it sets things up so that the next |
| 53 |
|get_next| will deliver the appropriate next token. The value of |cur_tok| |
| 54 |
need not be known when |expand| is called. |
| 55 |
|
| 56 |
Since several of the basic scanning routines communicate via global |
| 57 |
variables, their values are saved as local variables of |expand| so that |
| 58 |
recursive calls don't invalidate them. |
| 59 |
|
| 60 |
*/ |
| 61 |
|
| 62 |
int is_in_csname = 0; |
| 63 |
|
| 64 |
void expand(void) |
| 65 |
{ |
| 66 |
/*tex token that is being ``expanded after'' */ |
| 67 |
halfword t; |
| 68 |
/*tex for list manipulation */ |
| 69 |
halfword p; |
| 70 |
/*tex for a local token list pointer */ |
| 71 |
halfword cur_ptr; |
| 72 |
/*tex to save the global quantity |cur_val| */ |
| 73 |
int cv_backup; |
| 74 |
/*tex to save |cur_val_level|, etc. */ |
| 75 |
int cvl_backup, radix_backup, co_backup; |
| 76 |
/*tex to save |link(backup_head)| */ |
| 77 |
halfword backup_backup; |
| 78 |
/*tex temporary storage of |scanner_status| */ |
| 79 |
int save_scanner_status; |
| 80 |
incr(expand_depth_count); |
| 81 |
if (expand_depth_count >= expand_depth) |
| 82 |
overflow("expansion depth", (unsigned) expand_depth); |
| 83 |
cv_backup = cur_val; |
| 84 |
cvl_backup = cur_val_level; |
| 85 |
radix_backup = radix; |
| 86 |
co_backup = cur_order; |
| 87 |
backup_backup = token_link(backup_head); |
| 88 |
RESWITCH: |
| 89 |
if (cur_cmd < call_cmd) { |
| 90 |
/*tex Expand a nonmacro. */ |
| 91 |
if (tracing_commands_par > 1) |
| 92 |
show_cur_cmd_chr(); |
| 93 |
switch (cur_cmd) { |
| 94 |
case top_bot_mark_cmd: |
| 95 |
/*tex Insert the appropriate mark text into the scanner. */ |
| 96 |
t = cur_chr % marks_code; |
| 97 |
if (cur_chr >= marks_code) |
| 98 |
scan_mark_num(); |
| 99 |
else |
| 100 |
cur_val = 0; |
| 101 |
switch (t) { |
| 102 |
case first_mark_code: |
| 103 |
cur_ptr = first_mark(cur_val); |
| 104 |
break; |
| 105 |
case bot_mark_code: |
| 106 |
cur_ptr = bot_mark(cur_val); |
| 107 |
break; |
| 108 |
case split_first_mark_code: |
| 109 |
cur_ptr = split_first_mark(cur_val); |
| 110 |
break; |
| 111 |
case split_bot_mark_code: |
| 112 |
cur_ptr = split_bot_mark(cur_val); |
| 113 |
break; |
| 114 |
default: |
| 115 |
cur_ptr = top_mark(cur_val); |
| 116 |
break; |
| 117 |
} |
| 118 |
if (cur_ptr != null) |
| 119 |
begin_token_list(cur_ptr, mark_text); |
| 120 |
break; |
| 121 |
case expand_after_cmd: |
| 122 |
if (cur_chr == 0) { |
| 123 |
/*tex |
| 124 |
|
| 125 |
Expand the token after the next token. It takes only a |
| 126 |
little shuffling to do what \TeX\ calls |
| 127 |
\.{\\expandafter}. |
| 128 |
|
| 129 |
*/ |
| 130 |
get_token(); |
| 131 |
t = cur_tok; |
| 132 |
get_token(); |
| 133 |
if (cur_cmd > max_command_cmd) |
| 134 |
expand(); |
| 135 |
else |
| 136 |
back_input(); |
| 137 |
cur_tok = t; |
| 138 |
back_input(); |
| 139 |
} else { |
| 140 |
/*tex |
| 141 |
|
| 142 |
Negate a boolean conditional and |goto reswitch|. The |
| 143 |
result of a boolean condition is reversed when the |
| 144 |
conditional is preceded by \.{\\unless}. |
| 145 |
|
| 146 |
*/ |
| 147 |
get_token(); |
| 148 |
if ((cur_cmd == if_test_cmd) && (cur_chr != if_case_code)) { |
| 149 |
cur_chr = cur_chr + unless_code; |
| 150 |
goto RESWITCH; |
| 151 |
} |
| 152 |
print_err("You can't use `\\unless' before `"); |
| 153 |
print_cmd_chr((quarterword) cur_cmd, cur_chr); |
| 154 |
print_char('\''); |
| 155 |
help1("Continue, and I'll forget that it ever happened."); |
| 156 |
back_error(); |
| 157 |
} |
| 158 |
break; |
| 159 |
case no_expand_cmd: |
| 160 |
if (cur_chr == 0) { |
| 161 |
/*tex |
| 162 |
|
| 163 |
Suppress expansion of the next token. The implementation |
| 164 |
of \.{\\noexpand} is a bit trickier, because it is |
| 165 |
necessary to insert a special `|dont_expand|' marker into |
| 166 |
\TeX's reading mechanism. This special marker is |
| 167 |
processed by |get_next|, but it does not slow down the |
| 168 |
inner loop. |
| 169 |
|
| 170 |
Since \.{\\outer} macros might arise here, we must also |
| 171 |
clear the |scanner_status| temporarily. |
| 172 |
|
| 173 |
*/ |
| 174 |
save_scanner_status = scanner_status; |
| 175 |
scanner_status = normal; |
| 176 |
get_token(); |
| 177 |
scanner_status = save_scanner_status; |
| 178 |
t = cur_tok; |
| 179 |
back_input(); |
| 180 |
/*tex Now |start| and |loc| point to the backed-up token |t|. */ |
| 181 |
if (t >= cs_token_flag) { |
| 182 |
p = get_avail(); |
| 183 |
set_token_info(p, cs_token_flag + frozen_dont_expand); |
| 184 |
set_token_link(p, iloc); |
| 185 |
istart = p; |
| 186 |
iloc = p; |
| 187 |
} |
| 188 |
|
| 189 |
} else { |
| 190 |
/*tex |
| 191 |
|
| 192 |
The \.{\\primitive} handling. If the primitive meaning of |
| 193 |
the next token is an expandable command, it suffices to |
| 194 |
replace the current token with the primitive one and |
| 195 |
restart |expand|. |
| 196 |
|
| 197 |
Otherwise, the token we just read has to be pushed back, |
| 198 |
as well as a token matching the internal form of |
| 199 |
\.{\\primitive}, that is sneaked in as an alternate form |
| 200 |
of |ignore_spaces|. |
| 201 |
|
| 202 |
An implementation problem surfaces: There really is no |
| 203 |
|cur_cs| attached to the inserted primitive command, so |
| 204 |
it is safer to set |cur_cs| to zero. |cur_tok| has a |
| 205 |
similar problem. And for the non-expanded branch, simply |
| 206 |
pushing back a token that matches the correct internal |
| 207 |
command does not work, because that approach would not |
| 208 |
survive roundtripping to a temporary file or even a token |
| 209 |
list. |
| 210 |
|
| 211 |
It would be smart to create |frozen_| versions of all the |
| 212 |
primitives. Then, this problem would not happen, at the |
| 213 |
expense of a few hundred extra control sequences. |
| 214 |
|
| 215 |
*/ |
| 216 |
save_scanner_status = scanner_status; |
| 217 |
scanner_status = normal; |
| 218 |
get_token(); |
| 219 |
scanner_status = save_scanner_status; |
| 220 |
cur_cs = prim_lookup(cs_text(cur_cs)); |
| 221 |
if (cur_cs != undefined_primitive) { |
| 222 |
t = get_prim_eq_type(cur_cs); |
| 223 |
if (t > max_command_cmd) { |
| 224 |
cur_cmd = t; |
| 225 |
cur_chr = get_prim_equiv(cur_cs); |
| 226 |
cur_tok = token_val(cur_cmd, cur_chr); |
| 227 |
cur_cs = 0; |
| 228 |
goto RESWITCH; |
| 229 |
} else { |
| 230 |
back_input(); |
| 231 |
/*tex Now |loc| and |start| point to a one-item list. */ |
| 232 |
p = get_avail(); |
| 233 |
set_token_info(p, cs_token_flag + frozen_primitive); |
| 234 |
set_token_link(p, iloc); |
| 235 |
iloc = p; |
| 236 |
istart = p; |
| 237 |
} |
| 238 |
} else if (suppress_primitive_error_par == 0) { |
| 239 |
print_err("Missing primitive name"); |
| 240 |
help2( |
| 241 |
"The control sequence marked <to be read again> does not", |
| 242 |
"represent any known primitive." |
| 243 |
); |
| 244 |
back_error(); |
| 245 |
} |
| 246 |
} |
| 247 |
break; |
| 248 |
case cs_name_cmd: |
| 249 |
/*tex Manufacture a control sequence name. */ |
| 250 |
if (cur_chr == 0) { |
| 251 |
manufacture_csname(0); |
| 252 |
} else if (cur_chr == 1) { |
| 253 |
inject_last_tested_cs(); |
| 254 |
} else { |
| 255 |
manufacture_csname(1); |
| 256 |
} |
| 257 |
break; |
| 258 |
case convert_cmd: |
| 259 |
conv_toks(); |
| 260 |
break; |
| 261 |
case the_cmd: |
| 262 |
ins_the_toks(); |
| 263 |
break; |
| 264 |
case if_test_cmd: |
| 265 |
/*tex An experiment. */ |
| 266 |
if (cur_chr == if_condition_code) { |
| 267 |
break; |
| 268 |
} |
| 269 |
/*tex End of experiment. */ |
| 270 |
conditional(); |
| 271 |
break; |
| 272 |
case fi_or_else_cmd: |
| 273 |
/*tex |
| 274 |
|
| 275 |
Terminate the current conditional and skip to \.{\\fi} The |
| 276 |
processing of conditionals is complete except for the |
| 277 |
following code, which is actually part of |expand|. It comes |
| 278 |
into play when \.{\\or}, \.{\\else}, or \.{\\fi} is scanned. |
| 279 |
|
| 280 |
*/ |
| 281 |
if (tracing_ifs_par > 0) |
| 282 |
if (tracing_commands_par <= 1) |
| 283 |
show_cur_cmd_chr(); |
| 284 |
if (cur_chr > if_limit) { |
| 285 |
if (if_limit == if_code) { |
| 286 |
/*tex Condition is not yet evaluated. */ |
| 287 |
insert_relax(); |
| 288 |
} else { |
| 289 |
print_err("Extra "); |
| 290 |
print_cmd_chr(fi_or_else_cmd, cur_chr); |
| 291 |
help1("I'm ignoring this; it doesn't match any \\if."); |
| 292 |
error(); |
| 293 |
} |
| 294 |
} else { |
| 295 |
while (cur_chr != fi_code) { |
| 296 |
/*tex Skip to \.{\\fi}. */ |
| 297 |
pass_text(); |
| 298 |
} |
| 299 |
pop_condition_stack(); |
| 300 |
} |
| 301 |
break; |
| 302 |
case input_cmd: |
| 303 |
/*tex Initiate or terminate input from a file */ |
| 304 |
if (cur_chr == 1) |
| 305 |
force_eof = true; |
| 306 |
else if (cur_chr == 2) |
| 307 |
pseudo_start(); |
| 308 |
else if (cur_chr == 3) { |
| 309 |
pseudo_start(); |
| 310 |
iname = 19; |
| 311 |
} else if (name_in_progress) |
| 312 |
insert_relax(); |
| 313 |
else |
| 314 |
start_input(); |
| 315 |
break; |
| 316 |
case lua_expandable_call_cmd: |
| 317 |
if (cur_chr <= 0) { |
| 318 |
normal_error("luacall", "invalid number"); |
| 319 |
} else { |
| 320 |
str_number u = save_cur_string(); |
| 321 |
luacstrings = 0; |
| 322 |
luafunctioncall(cur_chr); |
| 323 |
restore_cur_string(u); |
| 324 |
if (luacstrings > 0) |
| 325 |
lua_string_start(); |
| 326 |
} |
| 327 |
break; |
| 328 |
case lua_local_call_cmd: |
| 329 |
if (cur_chr <= 0) { |
| 330 |
normal_error("luacall", "invalid number"); |
| 331 |
} else { |
| 332 |
str_number u = save_cur_string(); |
| 333 |
luacstrings = 0; |
| 334 |
lua_rawgeti(Luas, LUA_REGISTRYINDEX, cur_chr); |
| 335 |
lua_call(Luas,0,0); |
| 336 |
restore_cur_string(u); |
| 337 |
if (luacstrings > 0) |
| 338 |
lua_string_start(); |
| 339 |
} |
| 340 |
break; |
| 341 |
case variable_cmd: |
| 342 |
do_variable(); |
| 343 |
break; |
| 344 |
case feedback_cmd: |
| 345 |
do_feedback(); |
| 346 |
break; |
| 347 |
default: |
| 348 |
/*tex Complain about an undefined macro */ |
| 349 |
print_err("Undefined control sequence"); |
| 350 |
help5( |
| 351 |
"The control sequence at the end of the top line", |
| 352 |
"of your error message was never \\def'ed. If you have", |
| 353 |
"misspelled it (e.g., `\\hobx'), type `I' and the correct", |
| 354 |
"spelling (e.g., `I\\hbox'). Otherwise just continue,", |
| 355 |
"and I'll forget about whatever was undefined." |
| 356 |
); |
| 357 |
error(); |
| 358 |
break; |
| 359 |
} |
| 360 |
} else if (cur_cmd < end_template_cmd) { |
| 361 |
macro_call(); |
| 362 |
} else { |
| 363 |
/*tex |
| 364 |
|
| 365 |
Insert a token containing |frozen_endv|. An |end_template| command is |
| 366 |
effectively changed to an |endv| command by the following code. (The |
| 367 |
reason for this is discussed below; the |frozen_end_template| at the |
| 368 |
end of the template has passed the |check_outer_validity| test, so |
| 369 |
its mission of error detection has been accomplished.) |
| 370 |
|
| 371 |
*/ |
| 372 |
cur_tok = cs_token_flag + frozen_endv; |
| 373 |
back_input(); |
| 374 |
} |
| 375 |
cur_val = cv_backup; |
| 376 |
cur_val_level = cvl_backup; |
| 377 |
radix = radix_backup; |
| 378 |
cur_order = co_backup; |
| 379 |
set_token_link(backup_head, backup_backup); |
| 380 |
decr(expand_depth_count); |
| 381 |
} |
| 382 |
|
| 383 |
void complain_missing_csname(void) |
| 384 |
{ |
| 385 |
print_err("Missing \\endcsname inserted"); |
| 386 |
help2( |
| 387 |
"The control sequence marked <to be read again> should", |
| 388 |
"not appear between \\csname and \\endcsname." |
| 389 |
); |
| 390 |
back_error(); |
| 391 |
} |
| 392 |
|
| 393 |
void manufacture_csname(boolean use) |
| 394 |
{ |
| 395 |
halfword p, q, r; |
| 396 |
lstring *ss; |
| 397 |
r = get_avail(); |
| 398 |
p = r; |
| 399 |
is_in_csname += 1; |
| 400 |
do { |
| 401 |
get_x_token(); |
| 402 |
if (cur_cs == 0) |
| 403 |
store_new_token(cur_tok); |
| 404 |
} while (cur_cs == 0); |
| 405 |
if (cur_cmd != end_cs_name_cmd) { |
| 406 |
/*tex Complain about missing \.{\\endcsname}. */ |
| 407 |
complain_missing_csname(); |
| 408 |
} |
| 409 |
/*tex Look up the characters of list |r| in the hash table, and set |cur_cs|. */ |
| 410 |
ss = tokenlist_to_lstring(r, true); |
| 411 |
is_in_csname -= 1; |
| 412 |
if (use) { |
| 413 |
if (ss->l > 0) { |
| 414 |
cur_cs = string_lookup((char *) ss->s, ss->l); |
| 415 |
} else { |
| 416 |
cur_cs = null_cs; |
| 417 |
} |
| 418 |
last_cs_name = cur_cs ; |
| 419 |
free_lstring(ss); |
| 420 |
flush_list(r); |
| 421 |
if (cur_cs == null_cs) { |
| 422 |
/*tex skip */ |
| 423 |
} else if (eq_type(cur_cs) == undefined_cs_cmd) { |
| 424 |
/*tex skip */ |
| 425 |
} else { |
| 426 |
cur_tok = cur_cs + cs_token_flag; |
| 427 |
back_input(); |
| 428 |
} |
| 429 |
} else { |
| 430 |
if (ss->l > 0) { |
| 431 |
no_new_control_sequence = false; |
| 432 |
cur_cs = string_lookup((char *) ss->s, ss->l); |
| 433 |
no_new_control_sequence = true; |
| 434 |
} else { |
| 435 |
/*tex the list is empty */ |
| 436 |
cur_cs = null_cs; |
| 437 |
} |
| 438 |
last_cs_name = cur_cs ; |
| 439 |
free_lstring(ss); |
| 440 |
flush_list(r); |
| 441 |
if (eq_type(cur_cs) == undefined_cs_cmd) { |
| 442 |
/*tex The |save_stack| might change! */ |
| 443 |
eq_define(cur_cs, relax_cmd, too_big_char); |
| 444 |
}; |
| 445 |
/*tex The control sequence will now match \.{\\relax} */ |
| 446 |
cur_tok = cur_cs + cs_token_flag; |
| 447 |
back_input(); |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
void inject_last_tested_cs(void) |
| 452 |
{ |
| 453 |
if (last_cs_name != null_cs) { |
| 454 |
cur_cs = last_cs_name; |
| 455 |
cur_tok = last_cs_name + cs_token_flag; |
| 456 |
back_input(); |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
/*tex |
| 461 |
|
| 462 |
Sometimes the expansion looks too far ahead, so we want to insert a harmless |
| 463 |
\.{\\relax} into the user's input. |
| 464 |
|
| 465 |
*/ |
| 466 |
|
| 467 |
void insert_relax(void) |
| 468 |
{ |
| 469 |
cur_tok = cs_token_flag + cur_cs; |
| 470 |
back_input(); |
| 471 |
cur_tok = cs_token_flag + frozen_relax; |
| 472 |
back_input(); |
| 473 |
token_type = inserted; |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
/*tex |
| 478 |
|
| 479 |
Here is a recursive procedure that is \TeX's usual way to get the next token |
| 480 |
of input. It has been slightly optimized to take account of common cases. |
| 481 |
|
| 482 |
*/ |
| 483 |
|
| 484 |
void get_x_token(void) |
| 485 |
{ |
| 486 |
/*tex This code sets |cur_cmd|, |cur_chr|, |cur_tok|, and expands macros. */ |
| 487 |
RESTART: |
| 488 |
get_next(); |
| 489 |
if (cur_cmd <= max_command_cmd) |
| 490 |
goto DONE; |
| 491 |
if (cur_cmd >= call_cmd) { |
| 492 |
if (cur_cmd < end_template_cmd) { |
| 493 |
macro_call(); |
| 494 |
} else { |
| 495 |
cur_cs = frozen_endv; |
| 496 |
cur_cmd = endv_cmd; |
| 497 |
/*tex Now |cur_chr=null_list|. */ |
| 498 |
goto DONE; |
| 499 |
} |
| 500 |
} else { |
| 501 |
expand(); |
| 502 |
} |
| 503 |
goto RESTART; |
| 504 |
DONE: |
| 505 |
if (cur_cs == 0) |
| 506 |
cur_tok = token_val(cur_cmd, cur_chr); |
| 507 |
else |
| 508 |
cur_tok = cs_token_flag + cur_cs; |
| 509 |
} |
| 510 |
|
| 511 |
/*tex |
| 512 |
|
| 513 |
The |get_x_token| procedure is equivalent to two consecutive procedure calls: |
| 514 |
|get_next; x_token|. It's |get_x_token| without the initial |get_next|. |
| 515 |
|
| 516 |
*/ |
| 517 |
|
| 518 |
void x_token(void) |
| 519 |
{ |
| 520 |
while (cur_cmd > max_command_cmd) { |
| 521 |
expand(); |
| 522 |
get_next(); |
| 523 |
} |
| 524 |
if (cur_cs == 0) |
| 525 |
cur_tok = token_val(cur_cmd, cur_chr); |
| 526 |
else |
| 527 |
cur_tok = cs_token_flag + cur_cs; |
| 528 |
} |
| 529 |
|
| 530 |
/*tex |
| 531 |
|
| 532 |
A control sequence that has been \.{\\def}'ed by the user is expanded by |
| 533 |
\TeX's |macro_call| procedure. |
| 534 |
|
| 535 |
Before we get into the details of |macro_call|, however, let's consider the |
| 536 |
treatment of primitives like \.{\\topmark}, since they are essentially macros |
| 537 |
without parameters. The token lists for such marks are kept in five global |
| 538 |
arrays of pointers; we refer to the individual entries of these arrays by |
| 539 |
symbolic macros |top_mark|, etc. The value of |top_mark(x)|, etc. is either |
| 540 |
|null| or a pointer to the reference count of a token list. |
| 541 |
|
| 542 |
The variable |biggest_used_mark| is an aid to try and keep the code somehwat |
| 543 |
efficient without too much extra work: it registers the highest mark class |
| 544 |
ever instantiated by the user, so the loops in |fire_up| and |vsplit| do not |
| 545 |
have to traverse the full range |0..biggest_mark|. |
| 546 |
|
| 547 |
*/ |
| 548 |
|
| 549 |
halfword top_marks_array[(biggest_mark + 1)]; |
| 550 |
halfword first_marks_array[(biggest_mark + 1)]; |
| 551 |
halfword bot_marks_array[(biggest_mark + 1)]; |
| 552 |
halfword split_first_marks_array[(biggest_mark + 1)]; |
| 553 |
halfword split_bot_marks_array[(biggest_mark + 1)]; |
| 554 |
halfword biggest_used_mark; |
| 555 |
|
| 556 |
void initialize_marks(void) |
| 557 |
{ |
| 558 |
int i; |
| 559 |
biggest_used_mark = 0; |
| 560 |
for (i = 0; i <= biggest_mark; i++) { |
| 561 |
top_mark(i) = null; |
| 562 |
first_mark(i) = null; |
| 563 |
bot_mark(i) = null; |
| 564 |
split_first_mark(i) = null; |
| 565 |
split_bot_mark(i) = null; |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
/*tex |
| 570 |
|
| 571 |
Now let's consider |macro_call| itself, which is invoked when \TeX\ is |
| 572 |
scanning a control sequence whose |cur_cmd| is either |call|, |long_call|, |
| 573 |
|outer_call|, or |long_outer_call|. The control sequence definition appears |
| 574 |
in the token list whose reference count is in location |cur_chr| of |mem|. |
| 575 |
|
| 576 |
The global variable |long_state| will be set to |call| or to |long_call|, |
| 577 |
depending on whether or not the control sequence disallows \.{\\par} in its |
| 578 |
parameters. The |get_next| routine will set |long_state| to |outer_call| and |
| 579 |
emit \.{\\par}, if a file ends or if an \.{\\outer} control sequence occurs |
| 580 |
in the midst of an argument. |
| 581 |
|
| 582 |
*/ |
| 583 |
|
| 584 |
/*tex Governs the acceptance of \.{\\par}: */ |
| 585 |
|
| 586 |
int long_state; |
| 587 |
|
| 588 |
/*tex |
| 589 |
|
| 590 |
The parameters, if any, must be scanned before the macro is expanded. |
| 591 |
Parameters are token lists without reference counts. They are placed on an |
| 592 |
auxiliary stack called |pstack| while they are being scanned, since the |
| 593 |
|param_stack| may be losing entries during the matching process. (Note that |
| 594 |
|param_stack| can't be gaining entries, since |macro_call| is the only |
| 595 |
routine that puts anything onto |param_stack|, and it is not recursive.) |
| 596 |
|
| 597 |
*/ |
| 598 |
|
| 599 |
/*tex The arguments supplied to a macro: */ |
| 600 |
|
| 601 |
halfword pstack[9]; |
| 602 |
|
| 603 |
/*tex |
| 604 |
|
| 605 |
After parameter scanning is complete, the parameters are moved to the |
| 606 |
|param_stack|. Then the macro body is fed to the scanner; in other words, |
| 607 |
|macro_call| places the defined text of the control sequence at the top of\/ |
| 608 |
\TeX's input stack, so that |get_next| will proceed to read it next. |
| 609 |
|
| 610 |
The global variable |cur_cs| contains the |eqtb| address of the control |
| 611 |
sequence being expanded, when |macro_call| begins. If this control sequence |
| 612 |
has not been declared \.{\\long}, i.e., if its command code in the |eq_type| |
| 613 |
field is not |long_call| or |long_outer_call|, its parameters are not allowed |
| 614 |
to contain the control sequence \.{\\par}. If an illegal \.{\\par} appears, |
| 615 |
the macro call is aborted, and the \.{\\par} will be rescanned. |
| 616 |
|
| 617 |
*/ |
| 618 |
|
| 619 |
/*tex This invokes a user-defined control sequence. */ |
| 620 |
|
| 621 |
void macro_call(void) |
| 622 |
{ |
| 623 |
/*tex current node in the macro's token list */ |
| 624 |
halfword r; |
| 625 |
/*tex current node in parameter token list being built */ |
| 626 |
halfword p = null; |
| 627 |
/*tex new node being put into the token list */ |
| 628 |
halfword q; |
| 629 |
/*tex backup pointer for parameter matching */ |
| 630 |
halfword s; |
| 631 |
/*tex cycle pointer for backup recovery */ |
| 632 |
halfword t; |
| 633 |
/*tex auxiliary pointers for backup recovery */ |
| 634 |
halfword u, v; |
| 635 |
/*tex one step before the last |right_brace| token */ |
| 636 |
halfword rbrace_ptr = null; |
| 637 |
/*tex the number of parameters scanned */ |
| 638 |
int n = 0; |
| 639 |
/*tex unmatched left braces in current parameter */ |
| 640 |
halfword unbalance; |
| 641 |
/*tex the number of tokens or groups (usually) */ |
| 642 |
halfword m = 0; |
| 643 |
/*tex start of the token list */ |
| 644 |
halfword ref_count; |
| 645 |
/*tex |scanner_status| upon entry */ |
| 646 |
int save_scanner_status = scanner_status; |
| 647 |
/*tex |warning_index| upon entry */ |
| 648 |
halfword save_warning_index = warning_index; |
| 649 |
/*tex character used in parameter */ |
| 650 |
int match_chr = 0; |
| 651 |
warning_index = cur_cs; |
| 652 |
ref_count = cur_chr; |
| 653 |
r = token_link(ref_count); |
| 654 |
if (tracing_macros_par > 0) { |
| 655 |
/*tex Show the text of the macro being expanded. */ |
| 656 |
begin_diagnostic(); |
| 657 |
if (traceextranewline==false) |
| 658 |
print_ln(); /* also see newlines in print_input_level */ |
| 659 |
print_input_level(); |
| 660 |
print_cs(warning_index); |
| 661 |
token_show(ref_count); |
| 662 |
end_diagnostic(false); |
| 663 |
} |
| 664 |
if (token_info(r) == protected_token) |
| 665 |
r = token_link(r); |
| 666 |
if (token_info(r) != end_match_token) { |
| 667 |
/*tex |
| 668 |
|
| 669 |
Scan the parameters and make |link(r)| point to the macro body; but |
| 670 |
|return| if an illegal \.{\\par} is detected. |
| 671 |
|
| 672 |
At this point, the reader will find it advisable to review the |
| 673 |
explanation of token list format that was presented earlier, since |
| 674 |
many aspects of that format are of importance chiefly in the |
| 675 |
|macro_call| routine. |
| 676 |
|
| 677 |
The token list might begin with a string of compulsory tokens before |
| 678 |
the first |match| or |end_match|. In that case the macro name is |
| 679 |
supposed to be followed by those tokens; the following program will |
| 680 |
set |s=null| to represent this restriction. Otherwise |s| will be set |
| 681 |
to the first token of a string that will delimit the next parameter. |
| 682 |
|
| 683 |
*/ |
| 684 |
scanner_status = matching; |
| 685 |
unbalance = 0; |
| 686 |
long_state = eq_type(cur_cs); |
| 687 |
if (long_state >= outer_call_cmd) |
| 688 |
long_state = long_state - 2; |
| 689 |
do { |
| 690 |
set_token_link(temp_token_head, null); |
| 691 |
if ((token_info(r) >= end_match_token) |
| 692 |
|| (token_info(r) < match_token)) { |
| 693 |
s = null; |
| 694 |
} else { |
| 695 |
match_chr = token_info(r) - match_token; |
| 696 |
s = token_link(r); |
| 697 |
r = s; |
| 698 |
p = temp_token_head; |
| 699 |
m = 0; |
| 700 |
} |
| 701 |
/*tex |
| 702 |
|
| 703 |
Scan a parameter until its delimiter string has been found; or, |
| 704 |
if |s=null|, simply scan the delimiter string. |
| 705 |
|
| 706 |
If |info(r)| is a |match| or |end_match| command, it cannot be |
| 707 |
equal to any token found by |get_token|. Therefore an undelimited |
| 708 |
parameter---i.e., a |match| that is immediately followed by |
| 709 |
|match| or |end_match|---will always fail the test |
| 710 |
`|cur_tok=info(r)|' in the following algorithm. |
| 711 |
|
| 712 |
*/ |
| 713 |
CONTINUE: |
| 714 |
/*tex Set |cur_tok| to the next token of input: */ |
| 715 |
get_token(); |
| 716 |
if (cur_tok == token_info(r)) { |
| 717 |
/*tex |
| 718 |
|
| 719 |
Advance |r|; |goto found| if the parameter delimiter has been |
| 720 |
fully matched, otherwise |goto continue|. |
| 721 |
|
| 722 |
A slightly subtle point arises here: When the parameter |
| 723 |
delimiter ends with `\.{\#\{}', the token list will have a |
| 724 |
left brace both before and after the |end_match|\kern-.4pt. |
| 725 |
Only one of these should affect the |align_state|, but both |
| 726 |
will be scanned, so we must make a correction. |
| 727 |
|
| 728 |
*/ |
| 729 |
r = token_link(r); |
| 730 |
if ((token_info(r) >= match_token) |
| 731 |
&& (token_info(r) <= end_match_token)) { |
| 732 |
if (cur_tok < left_brace_limit) |
| 733 |
decr(align_state); |
| 734 |
goto FOUND; |
| 735 |
} else { |
| 736 |
goto CONTINUE; |
| 737 |
} |
| 738 |
|
| 739 |
} |
| 740 |
/*tex |
| 741 |
|
| 742 |
Contribute the recently matched tokens to the current parameter, |
| 743 |
and |goto continue| if a partial match is still in effect; but |
| 744 |
abort if |s=null|. |
| 745 |
|
| 746 |
When the following code becomes active, we have matched tokens |
| 747 |
from |s| to the predecessor of |r|, and we have found that |
| 748 |
|cur_tok<>info(r)|. An interesting situation now presents itself: |
| 749 |
If the parameter is to be delimited by a string such as `\.{ab}', |
| 750 |
and if we have scanned `\.{aa}', we want to contribute one `\.a' |
| 751 |
to the current parameter and resume looking for a `\.b'. The |
| 752 |
program must account for such partial matches and for others that |
| 753 |
can be quite complex. But most of the time we have |s=r| and |
| 754 |
nothing needs to be done. |
| 755 |
|
| 756 |
Incidentally, it is possible for \.{\\par} tokens to sneak in to |
| 757 |
certain parameters of non-\.{\\long} macros. For example, |
| 758 |
consider a case like `\.{\\def\\a\#1\\par!\{...\}}' where the |
| 759 |
first \.{\\par} is not followed by an exclamation point. In such |
| 760 |
situations it does not seem appropriate to prohibit the |
| 761 |
\.{\\par}, so \TeX\ keeps quiet about this bending of the rules. |
| 762 |
|
| 763 |
*/ |
| 764 |
if (s != r) { |
| 765 |
if (s == null) { |
| 766 |
/*tex Report an improper use of the macro and abort. */ |
| 767 |
print_err("Use of "); |
| 768 |
sprint_cs(warning_index); |
| 769 |
tprint(" doesn't match its definition"); |
| 770 |
help4( |
| 771 |
"If you say, e.g., `\\def\\a1{...}', then you must always", |
| 772 |
"put `1' after `\\a', since control sequence names are", |
| 773 |
"made up of letters only. The macro here has not been", |
| 774 |
"followed by the required stuff, so I'm ignoring it." |
| 775 |
); |
| 776 |
error(); |
| 777 |
goto EXIT; |
| 778 |
|
| 779 |
} else { |
| 780 |
t = s; |
| 781 |
do { |
| 782 |
store_new_token(token_info(t)); |
| 783 |
incr(m); |
| 784 |
u = token_link(t); |
| 785 |
v = s; |
| 786 |
while (1) { |
| 787 |
if (u == r) { |
| 788 |
if (cur_tok != token_info(v)) { |
| 789 |
goto DONE; |
| 790 |
} else { |
| 791 |
r = token_link(v); |
| 792 |
goto CONTINUE; |
| 793 |
} |
| 794 |
} |
| 795 |
if (token_info(u) != token_info(v)) |
| 796 |
goto DONE; |
| 797 |
u = token_link(u); |
| 798 |
v = token_link(v); |
| 799 |
} |
| 800 |
DONE: |
| 801 |
t = token_link(t); |
| 802 |
} while (t != r); |
| 803 |
r = s; |
| 804 |
/*tex At this point, no tokens are recently matched. */ |
| 805 |
} |
| 806 |
} |
| 807 |
if (cur_tok == par_token) |
| 808 |
if (long_state != long_call_cmd) |
| 809 |
if (!suppress_long_error_par) { |
| 810 |
goto RUNAWAY; |
| 811 |
} |
| 812 |
if (cur_tok < right_brace_limit) { |
| 813 |
if (cur_tok < left_brace_limit) { |
| 814 |
/*tex Contribute an entire group to the current parameter. */ |
| 815 |
unbalance = 1; |
| 816 |
while (1) { |
| 817 |
fast_store_new_token(cur_tok); |
| 818 |
get_token(); |
| 819 |
if (cur_tok == par_token) { |
| 820 |
if (long_state != long_call_cmd) { |
| 821 |
if (!suppress_long_error_par) { |
| 822 |
goto RUNAWAY; |
| 823 |
|
| 824 |
} |
| 825 |
} |
| 826 |
} |
| 827 |
if (cur_tok < right_brace_limit) { |
| 828 |
if (cur_tok < left_brace_limit) { |
| 829 |
incr(unbalance); |
| 830 |
} else { |
| 831 |
decr(unbalance); |
| 832 |
if (unbalance == 0) |
| 833 |
break; |
| 834 |
} |
| 835 |
} |
| 836 |
} |
| 837 |
rbrace_ptr = p; |
| 838 |
store_new_token(cur_tok); |
| 839 |
} else { |
| 840 |
/*tex Report an extra right brace and |goto continue|. */ |
| 841 |
back_input(); |
| 842 |
print_err("Argument of "); |
| 843 |
sprint_cs(warning_index); |
| 844 |
tprint(" has an extra }"); |
| 845 |
help6( |
| 846 |
"I've run across a `}' that doesn't seem to match anything.", |
| 847 |
"For example, `\\def\\a#1{...}' and `\\a}' would produce", |
| 848 |
"this error. If you simply proceed now, the `\\par' that", |
| 849 |
"I've just inserted will cause me to report a runaway", |
| 850 |
"argument that might be the root of the problem. But if", |
| 851 |
"your `}' was spurious, just type `2' and it will go away." |
| 852 |
); |
| 853 |
incr(align_state); |
| 854 |
long_state = call_cmd; |
| 855 |
cur_tok = par_token; |
| 856 |
ins_error(); |
| 857 |
goto CONTINUE; |
| 858 |
/*tex A white lie; the \.{\\par} won't always trigger a runaway. */ |
| 859 |
} |
| 860 |
} else { |
| 861 |
/*tex |
| 862 |
|
| 863 |
Store the current token, but |goto continue| if it is a blank |
| 864 |
space that would become an undelimited parameter. |
| 865 |
|
| 866 |
*/ |
| 867 |
if (cur_tok == space_token && token_info(r) <= end_match_token && token_info(r) >= match_token) |
| 868 |
goto CONTINUE; |
| 869 |
store_new_token(cur_tok); |
| 870 |
} |
| 871 |
incr(m); |
| 872 |
if (token_info(r) > end_match_token) |
| 873 |
goto CONTINUE; |
| 874 |
if (token_info(r) < match_token) |
| 875 |
goto CONTINUE; |
| 876 |
FOUND: |
| 877 |
if (s != null) { |
| 878 |
/* |
| 879 |
|
| 880 |
Tidy up the parameter just scanned, and tuck it away. If the |
| 881 |
parameter consists of a single group enclosed in braces, we |
| 882 |
must strip off the enclosing braces. That's why |rbrace_ptr| |
| 883 |
was introduced. |
| 884 |
|
| 885 |
*/ |
| 886 |
if ((m == 1) && (token_info(p) < right_brace_limit) |
| 887 |
&& (p != temp_token_head)) { |
| 888 |
set_token_link(rbrace_ptr, null); |
| 889 |
free_avail(p); |
| 890 |
p = token_link(temp_token_head); |
| 891 |
pstack[n] = token_link(p); |
| 892 |
free_avail(p); |
| 893 |
} else { |
| 894 |
pstack[n] = token_link(temp_token_head); |
| 895 |
} |
| 896 |
incr(n); |
| 897 |
if (tracing_macros_par > 0) { |
| 898 |
begin_diagnostic(); |
| 899 |
print_input_level(); |
| 900 |
// print_nl(match_chr); |
| 901 |
print(match_chr); |
| 902 |
print_int(n); |
| 903 |
tprint("<-"); |
| 904 |
show_token_list(pstack[n - 1], null, 1000); |
| 905 |
end_diagnostic(false); |
| 906 |
} |
| 907 |
} |
| 908 |
/*tex |
| 909 |
|
| 910 |
Now |info(r)| is a token whose command code is either |match| or |
| 911 |
|end_match|. |
| 912 |
|
| 913 |
*/ |
| 914 |
} while (token_info(r) != end_match_token); |
| 915 |
} |
| 916 |
/*tex |
| 917 |
|
| 918 |
Feed the macro body and its parameters to the scanner Before we put a new |
| 919 |
token list on the input stack, it is wise to clean off all token lists |
| 920 |
that have recently been depleted. Then a user macro that ends with a call |
| 921 |
to itself will not require unbounded stack space. |
| 922 |
|
| 923 |
*/ |
| 924 |
while ((istate == token_list) && (iloc == null) && (token_type != v_template)) { |
| 925 |
/*tex Conserve stack space. */ |
| 926 |
end_token_list(); |
| 927 |
} |
| 928 |
begin_token_list(ref_count, macro); |
| 929 |
iname = warning_index; |
| 930 |
iloc = token_link(r); |
| 931 |
if (n > 0) { |
| 932 |
if (param_ptr + n > max_param_stack) { |
| 933 |
max_param_stack = param_ptr + n; |
| 934 |
if (max_param_stack > param_size) |
| 935 |
overflow("parameter stack size", (unsigned) param_size); |
| 936 |
} |
| 937 |
for (m = 0; m <= n - 1; m++) |
| 938 |
param_stack[param_ptr + m] = pstack[m]; |
| 939 |
param_ptr = param_ptr + n; |
| 940 |
} |
| 941 |
goto EXIT; |
| 942 |
RUNAWAY: |
| 943 |
/*tex |
| 944 |
|
| 945 |
Report a runaway argument and abort. If |long_state=outer_call|, a |
| 946 |
runaway argument has already been reported. |
| 947 |
|
| 948 |
*/ |
| 949 |
if (long_state == call_cmd) { |
| 950 |
runaway(); |
| 951 |
print_err("Paragraph ended before "); |
| 952 |
sprint_cs(warning_index); |
| 953 |
tprint(" was complete"); |
| 954 |
help3( |
| 955 |
"I suspect you've forgotten a `}', causing me to apply this", |
| 956 |
"control sequence to too much text. How can we recover?", |
| 957 |
"My plan is to forget the whole thing and hope for the best." |
| 958 |
); |
| 959 |
back_error(); |
| 960 |
} |
| 961 |
pstack[n] = token_link(temp_token_head); |
| 962 |
align_state = align_state - unbalance; |
| 963 |
for (m = 0; m <= n; m++) |
| 964 |
flush_list(pstack[m]); |
| 965 |
EXIT: |
| 966 |
scanner_status = save_scanner_status; |
| 967 |
warning_index = save_warning_index; |
| 968 |
} |