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