1% expand.w
2%
3% Copyright 2009-2010 Taco Hoekwater <taco@@luatex.org>
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@ @c
21
22
23#include "ptexlib.h"
24
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}
30
31Sometimes, recursive calls to the following |expand| routine may
32cause exhaustion of the run-time calling stack, resulting in
33forced execution stops by the operating system. To diminish the chance
34of this happening, a counter is used to keep track of the recursion
35depth, in conjunction with a constant called |expand_depth|.
36
37Note that this does not catch all possible infinite recursion loops,
38just the ones that exhaust the application calling stack. The
39actual maximum value of |expand_depth| is outside of our control, but
40the initial setting of |100| should be enough to prevent problems.
41@^system dependencies@>
42
43@c
44static int expand_depth_count = 0;
45
46
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
49listed.  It follows that |expand| might invoke itself recursively. In all
50cases, |expand| destroys the current token, but it sets things up so that
51the next |get_next| will deliver the appropriate next token. The value of
52|cur_tok| need not be known when |expand| is called.
53
54Since several of the basic scanning routines communicate via global variables,
55their values are saved as local variables of |expand| so that
56recursive calls don't invalidate them.
57@^recursion@>
58
59@c
60boolean is_in_csname = false;
61
62@ @c
63void expand(void)
64{
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;
109            }
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();
126
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;
135                }
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();
141            }
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.
150
151                   Since \.{\\outer} macros might arise here, we must also
152                   clear the |scanner_status| temporarily.
153                 */
154
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;
167                }
168
169            } else {
170                /* Implement \.{\\primitive} */
171                /*
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|.
175
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|.
179
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.
186
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.
190                 */
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;
211                    }
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();
218                }
219
220            }
221            break;
222        case cs_name_cmd:
223            /* Manufacture a control sequence name; */
224            manufacture_csname();
225            break;
226        case convert_cmd:
227            conv_toks();        /* this procedure is discussed in Part 27 below */
228            break;
229        case the_cmd:
230            ins_the_toks();     /* this procedure is discussed in Part 27 below */
231            break;
232        case if_test_cmd:
233            conditional();      /* this procedure is discussed in Part 28 below */
234            break;
235        case fi_or_else_cmd:
236            /* Terminate the current conditional and skip to \.{\\fi} */
237            /* The processing of conditionals is complete except for the following
238               code, which is actually part of |expand|. It comes into play when
239               \.{\\or}, \.{\\else}, or \.{\\fi} is scanned. */
240
241            if (int_par(tracing_ifs_code) > 0)
242                if (int_par(tracing_commands_code) <= 1)
243                    show_cur_cmd_chr();
244            if (cur_chr > if_limit) {
245                if (if_limit == if_code) {
246                    insert_relax();     /*  condition not yet evaluated */
247                } else {
248                    print_err("Extra ");
249                    print_cmd_chr(fi_or_else_cmd, cur_chr);
250                    help1("I'm ignoring this; it doesn't match any \\if.");
251                    error();
252                }
253            } else {
254                while (cur_chr != fi_code)
255                    pass_text();        /* skip to \.{\\fi} */
256                pop_condition_stack();
257            }
258
259            break;
260        case input_cmd:
261            /* Initiate or terminate input from a file */
262            if (cur_chr == 1)
263                force_eof = true;
264            else if (cur_chr == 2)
265                pseudo_start();
266            else if (cur_chr == 3) {
267                pseudo_start();
268                iname = 19;
269            } else if (name_in_progress)
270                insert_relax();
271            else
272                start_input();
273            break;
274        default:
275            /* Complain about an undefined macro */
276            print_err("Undefined control sequence");
277            help5("The control sequence at the end of the top line",
278                  "of your error message was never \\def'ed. If you have",
279                  "misspelled it (e.g., `\\hobx'), type `I' and the correct",
280                  "spelling (e.g., `I\\hbox'). Otherwise just continue,",
281                  "and I'll forget about whatever was undefined.");
282            error();
283            break;
284        }
285    } else if (cur_cmd < end_template_cmd) {
286        macro_call();
287    } else {
288        /* Insert a token containing |frozen_endv| */
289        /* An |end_template| command is effectively changed to an |endv| command
290           by the following code. (The reason for this is discussed below; the
291           |frozen_end_template| at the end of the template has passed the
292           |check_outer_validity| test, so its mission of error detection has been
293           accomplished.)
294         */
295        cur_tok = cs_token_flag + frozen_endv;
296        back_input();
297
298    }
299    cur_val = cv_backup;
300    cur_val_level = cvl_backup;
301    radix = radix_backup;
302    cur_order = co_backup;
303    set_token_link(backup_head, backup_backup);
304    decr(expand_depth_count);
305}
306
307@ @c
308void complain_missing_csname(void)
309{
310    print_err("Missing \\endcsname inserted");
311    help2("The control sequence marked <to be read again> should",
312          "not appear between \\csname and \\endcsname.");
313    back_error();
314}
315
316@ @c
317void manufacture_csname(void)
318{
319    halfword p, q, r;
320    lstring *ss;
321    r = get_avail();
322    p = r;                      /* head of the list of characters */
323    is_in_csname = true;
324    do {
325        get_x_token();
326        if (cur_cs == 0)
327            store_new_token(cur_tok);
328    } while (cur_cs == 0);
329    if (cur_cmd != end_cs_name_cmd) {
330        /* Complain about missing \.{\\endcsname} */
331        complain_missing_csname();
332    }
333    is_in_csname = false;
334    /* Look up the characters of list |r| in the hash table, and set |cur_cs| */
335
336    ss = tokenlist_to_lstring(r, true);
337    if (ss->l > 0) {
338        no_new_control_sequence = false;
339        cur_cs = string_lookup((char *) ss->s, ss->l);
340        no_new_control_sequence = true;
341    } else {
342        cur_cs = null_cs;       /* the list is empty */
343    }
344    free_lstring(ss);
345    flush_list(r);
346    if (eq_type(cur_cs) == undefined_cs_cmd) {
347        eq_define(cur_cs, relax_cmd, too_big_char);     /* N.B.: The |save_stack| might change */
348    };                          /* the control sequence will now match `\.{\\relax}' */
349    cur_tok = cur_cs + cs_token_flag;
350    back_input();
351}
352
353
354@ Sometimes the expansion looks too far ahead, so we want to insert
355a harmless \.{\\relax} into the user's input.
356
357@c
358void insert_relax(void)
359{
360    cur_tok = cs_token_flag + cur_cs;
361    back_input();
362    cur_tok = cs_token_flag + frozen_relax;
363    back_input();
364    token_type = inserted;
365}
366
367
368@ Here is a recursive procedure that is \TeX's usual way to get the
369next token of input. It has been slightly optimized to take account of
370common cases.
371
372@c
373void get_x_token(void)
374{                               /* sets |cur_cmd|, |cur_chr|, |cur_tok|,  and expands macros */
375  RESTART:
376    get_token_lua();
377    if (cur_cmd <= max_command_cmd)
378        goto DONE;
379    if (cur_cmd >= call_cmd) {
380        if (cur_cmd < end_template_cmd) {
381            macro_call();
382        } else {
383            cur_cs = frozen_endv;
384            cur_cmd = endv_cmd;
385            goto DONE;          /* |cur_chr=null_list| */
386        }
387    } else {
388        expand();
389    }
390    goto RESTART;
391  DONE:
392    if (cur_cs == 0)
393        cur_tok = token_val(cur_cmd, cur_chr);
394    else
395        cur_tok = cs_token_flag + cur_cs;
396}
397
398
399@ The |get_x_token| procedure is equivalent to two consecutive
400procedure calls: |get_next; x_token|.
401
402@c
403void x_token(void)
404{                               /* |get_x_token| without the initial |get_next| */
405    while (cur_cmd > max_command_cmd) {
406        expand();
407        get_token_lua();
408    }
409    if (cur_cs == 0)
410        cur_tok = token_val(cur_cmd, cur_chr);
411    else
412        cur_tok = cs_token_flag + cur_cs;
413}
414
415
416@ A control sequence that has been \.{\\def}'ed by the user is expanded by
417\TeX's |macro_call| procedure.
418
419Before we get into the details of |macro_call|, however, let's consider the
420treatment of primitives like \.{\\topmark}, since they are essentially
421macros without parameters. The token lists for such marks are kept in five
422global arrays of pointers; we refer to the individual entries of these
423arrays by symbolic macros |top_mark|, etc. The value of |top_mark(x)|, etc.
424is either |null| or a pointer to the reference count of a token list.
425
426The variable |biggest_used_mark| is an aid to try and keep the code
427somehwat efficient without too much extra work: it registers the
428highest mark class ever instantiated by the user, so the loops
429in |fire_up| and |vsplit| do not have to traverse the full range
430|0..biggest_mark|.
431
432@c
433halfword top_marks_array[(biggest_mark + 1)];
434halfword first_marks_array[(biggest_mark + 1)];
435halfword bot_marks_array[(biggest_mark + 1)];
436halfword split_first_marks_array[(biggest_mark + 1)];
437halfword split_bot_marks_array[(biggest_mark + 1)];
438halfword biggest_used_mark;
439
440@ @c
441void initialize_marks(void)
442{
443    int i;
444    biggest_used_mark = 0;
445    for (i = 0; i <= biggest_mark; i++) {
446        top_mark(i) = null;
447        first_mark(i) = null;
448        bot_mark(i) = null;
449        split_first_mark(i) = null;
450        split_bot_mark(i) = null;
451    }
452}
453
454
455@ Now let's consider |macro_call| itself, which is invoked when \TeX\ is
456scanning a control sequence whose |cur_cmd| is either |call|, |long_call|,
457|outer_call|, or |long_outer_call|.  The control sequence definition
458appears in the token list whose reference count is in location |cur_chr|
459of |mem|.
460
461The global variable |long_state| will be set to |call| or to |long_call|,
462depending on whether or not the control sequence disallows \.{\\par}
463in its parameters. The |get_next| routine will set |long_state| to
464|outer_call| and emit \.{\\par}, if a file ends or if an \.{\\outer}
465control sequence occurs in the midst of an argument.
466
467@c
468int long_state;                 /* governs the acceptance of \.{\\par} */
469
470@ The parameters, if any, must be scanned before the macro is expanded.
471Parameters are token lists without reference counts. They are placed on
472an auxiliary stack called |pstack| while they are being scanned, since
473the |param_stack| may be losing entries during the matching process.
474(Note that |param_stack| can't be gaining entries, since |macro_call| is
475the only routine that puts anything onto |param_stack|, and it
476is not recursive.)
477
478@c
479halfword pstack[9];             /* arguments supplied to a macro */
480
481
482@ After parameter scanning is complete, the parameters are moved to the
483|param_stack|. Then the macro body is fed to the scanner; in other words,
484|macro_call| places the defined text of the control sequence at the
485top of\/ \TeX's input stack, so that |get_next| will proceed to read it
486next.
487
488The global variable |cur_cs| contains the |eqtb| address of the control sequence
489being expanded, when |macro_call| begins. If this control sequence has not been
490declared \.{\\long}, i.e., if its command code in the |eq_type| field is
491not |long_call| or |long_outer_call|, its parameters are not allowed to contain
492the control sequence \.{\\par}. If an illegal \.{\\par} appears, the macro
493call is aborted, and the \.{\\par} will be rescanned.
494
495@c
496void macro_call(void)
497{                               /* invokes a user-defined control sequence */
498    halfword r;                 /* current node in the macro's token list */
499    halfword p = null;          /* current node in parameter token list being built */
500    halfword q;                 /* new node being put into the token list */
501    halfword s;                 /* backup pointer for parameter matching */
502    halfword t;                 /* cycle pointer for backup recovery */
503    halfword u, v;              /* auxiliary pointers for backup recovery */
504    halfword rbrace_ptr = null; /* one step before the last |right_brace| token */
505    int n = 0;                  /* the number of parameters scanned */
506    halfword unbalance;         /* unmatched left braces in current parameter */
507    halfword m = 0;             /* the number of tokens or groups (usually) */
508    halfword ref_count;         /* start of the token list */
509    int save_scanner_status = scanner_status;   /* |scanner_status| upon entry */
510    halfword save_warning_index = warning_index;        /* |warning_index| upon entry */
511    int match_chr = 0;          /* character used in parameter */
512    warning_index = cur_cs;
513    ref_count = cur_chr;
514    r = token_link(ref_count);
515    if (int_par(tracing_macros_code) > 0) {
516        /* Show the text of the macro being expanded */
517        begin_diagnostic();
518        print_ln();
519        print_cs(warning_index);
520        token_show(ref_count);
521        end_diagnostic(false);
522    }
523    if (token_info(r) == protected_token)
524        r = token_link(r);
525    if (token_info(r) != end_match_token) {
526        /* Scan the parameters and make |link(r)| point to the macro body; but
527           |return| if an illegal \.{\\par} is detected */
528        /* At this point, the reader will find it advisable to review the explanation
529           of token list format that was presented earlier, since many aspects of that
530           format are of importance chiefly in the |macro_call| routine.
531
532           The token list might begin with a string of compulsory tokens before the
533           first |match| or |end_match|. In that case the macro name is supposed to be
534           followed by those tokens; the following program will set |s=null| to
535           represent this restriction. Otherwise |s| will be set to the first token of
536           a string that will delimit the next parameter.
537         */
538
539        scanner_status = matching;
540        unbalance = 0;
541        long_state = eq_type(cur_cs);
542        if (long_state >= outer_call_cmd)
543            long_state = long_state - 2;
544        do {
545            set_token_link(temp_token_head, null);
546            if ((token_info(r) >= end_match_token)
547                || (token_info(r) < match_token)) {
548                s = null;
549            } else {
550                match_chr = token_info(r) - match_token;
551                s = token_link(r);
552                r = s;
553                p = temp_token_head;
554                m = 0;
555            }
556            /* Scan a parameter until its delimiter string has been found; or, if |s=null|,
557               simply scan the delimiter string; */
558
559            /* If |info(r)| is a |match| or |end_match| command, it cannot be equal to
560               any token found by |get_token|. Therefore an undelimited parameter---i.e.,
561               a |match| that is immediately followed by |match| or |end_match|---will
562               always fail the test `|cur_tok=info(r)|' in the following algorithm. */
563          CONTINUE:
564            get_token();        /* set |cur_tok| to the next token of input */
565            if (cur_tok == token_info(r)) {
566                /* Advance |r|; |goto found| if the parameter delimiter has been
567                   fully matched, otherwise |goto continue| */
568                /* A slightly subtle point arises here: When the parameter delimiter ends
569                   with `\.{\#\{}', the token list will have a left brace both before and
570                   after the |end_match|\kern-.4pt. Only one of these should affect the
571                   |align_state|, but both will be scanned, so we must make a correction.
572                 */
573                r = token_link(r);
574                if ((token_info(r) >= match_token)
575                    && (token_info(r) <= end_match_token)) {
576                    if (cur_tok < left_brace_limit)
577                        decr(align_state);
578                    goto FOUND;
579                } else {
580                    goto CONTINUE;
581                }
582
583            }
584            /* Contribute the recently matched tokens to the current parameter, and
585               |goto continue| if a partial match is still in effect; but abort if |s=null| */
586
587            /* When the following code becomes active, we have matched tokens from |s| to
588               the predecessor of |r|, and we have found that |cur_tok<>info(r)|. An
589               interesting situation now presents itself: If the parameter is to be
590               delimited by a string such as `\.{ab}', and if we have scanned `\.{aa}',
591               we want to contribute one `\.a' to the current parameter and resume
592               looking for a `\.b'. The program must account for such partial matches and
593               for others that can be quite complex.  But most of the time we have |s=r|
594               and nothing needs to be done.
595
596               Incidentally, it is possible for \.{\\par} tokens to sneak in to certain
597               parameters of non-\.{\\long} macros. For example, consider a case like
598               `\.{\\def\\a\#1\\par!\{...\}}' where the first \.{\\par} is not followed
599               by an exclamation point. In such situations it does not seem appropriate
600               to prohibit the \.{\\par}, so \TeX\ keeps quiet about this bending of
601               the rules. */
602
603            if (s != r) {
604                if (s == null) {
605                    /* Report an improper use of the macro and abort */
606                    print_err("Use of ");
607                    sprint_cs(warning_index);
608                    tprint(" doesn't match its definition");
609                    help4
610                        ("If you say, e.g., `\\def\\a1{...}', then you must always",
611                         "put `1' after `\\a', since control sequence names are",
612                         "made up of letters only. The macro here has not been",
613                         "followed by the required stuff, so I'm ignoring it.");
614                    error();
615                    goto EXIT;
616
617                } else {
618                    t = s;
619                    do {
620                        store_new_token(token_info(t));
621                        incr(m);
622                        u = token_link(t);
623                        v = s;
624                        while (1) {
625                            if (u == r) {
626                                if (cur_tok != token_info(v)) {
627                                    goto DONE;
628                                } else {
629                                    r = token_link(v);
630                                    goto CONTINUE;
631                                }
632                            }
633                            if (token_info(u) != token_info(v))
634                                goto DONE;
635                            u = token_link(u);
636                            v = token_link(v);
637                        }
638                      DONE:
639                        t = token_link(t);
640                    } while (t != r);
641                    r = s;      /* at this point, no tokens are recently matched */
642                }
643            }
644
645            if (cur_tok == par_token)
646                if (long_state != long_call_cmd)
647                    if (!int_par(suppress_long_error_code)) {
648                        goto RUNAWAY;
649                    }
650            if (cur_tok < right_brace_limit) {
651                if (cur_tok < left_brace_limit) {
652                    /* Contribute an entire group to the current parameter */
653                    unbalance = 1;
654                    while (1) {
655                        fast_store_new_token(cur_tok);
656                        get_token();
657                        if (cur_tok == par_token) {
658                            if (long_state != long_call_cmd) {
659                                if (!int_par(suppress_long_error_code)) {
660                                    goto RUNAWAY;
661
662                                }
663                            }
664                        }
665                        if (cur_tok < right_brace_limit) {
666                            if (cur_tok < left_brace_limit) {
667                                incr(unbalance);
668                            } else {
669                                decr(unbalance);
670                                if (unbalance == 0)
671                                    break;
672                            }
673                        }
674                    }
675                    rbrace_ptr = p;
676                    store_new_token(cur_tok);
677
678                } else {
679                    /* Report an extra right brace and |goto continue| */
680                    back_input();
681                    print_err("Argument of ");
682                    sprint_cs(warning_index);
683                    tprint(" has an extra }");
684                    help6
685                        ("I've run across a `}' that doesn't seem to match anything.",
686                         "For example, `\\def\\a#1{...}' and `\\a}' would produce",
687                         "this error. If you simply proceed now, the `\\par' that",
688                         "I've just inserted will cause me to report a runaway",
689                         "argument that might be the root of the problem. But if",
690                         "your `}' was spurious, just type `2' and it will go away.");
691                    incr(align_state);
692                    long_state = call_cmd;
693                    cur_tok = par_token;
694                    ins_error();
695                    goto CONTINUE;
696                    /* a white lie; the \.{\\par} won't always trigger a runaway */
697                }
698            } else {
699                /* Store the current token, but |goto continue| if it is
700                   a blank space that would become an undelimited parameter */
701                if (cur_tok == space_token)
702                    if (token_info(r) <= end_match_token)
703                        if (token_info(r) >= match_token)
704                            goto CONTINUE;
705                store_new_token(cur_tok);
706
707            }
708            incr(m);
709            if (token_info(r) > end_match_token)
710                goto CONTINUE;
711            if (token_info(r) < match_token)
712                goto CONTINUE;
713          FOUND:
714            if (s != null) {
715                /* Tidy up the parameter just scanned, and tuck it away */
716                /* If the parameter consists of a single group enclosed in braces, we must
717                   strip off the enclosing braces. That's why |rbrace_ptr| was introduced. */
718                if ((m == 1) && (token_info(p) < right_brace_limit)
719                    && (p != temp_token_head)) {
720                    set_token_link(rbrace_ptr, null);
721                    free_avail(p);
722                    p = token_link(temp_token_head);
723                    pstack[n] = token_link(p);
724                    free_avail(p);
725                } else {
726                    pstack[n] = token_link(temp_token_head);
727                }
728                incr(n);
729                if (int_par(tracing_macros_code) > 0) {
730                    begin_diagnostic();
731                    print_nl(match_chr);
732                    print_int(n);
733                    tprint("<-");
734                    show_token_list(pstack[n - 1], null, 1000);
735                    end_diagnostic(false);
736                }
737
738            }
739
740            /* now |info(r)| is a token whose command code is either |match| or |end_match| */
741        } while (token_info(r) != end_match_token);
742
743    }
744    /* Feed the macro body and its parameters to the scanner */
745    /* Before we put a new token list on the input stack, it is wise to clean off
746       all token lists that have recently been depleted. Then a user macro that ends
747       with a call to itself will not require unbounded stack space. */
748    while ((istate == token_list) && (iloc == null)
749           && (token_type != v_template))
750        end_token_list();       /* conserve stack space */
751    begin_token_list(ref_count, macro);
752    iname = warning_index;
753    iloc = token_link(r);
754    if (n > 0) {
755        if (param_ptr + n > max_param_stack) {
756            max_param_stack = param_ptr + n;
757            if (max_param_stack > param_size)
758                overflow("parameter stack size", (unsigned) param_size);
759        }
760        for (m = 0; m <= n - 1; m++)
761            param_stack[param_ptr + m] = pstack[m];
762        param_ptr = param_ptr + n;
763    }
764    goto EXIT;
765  RUNAWAY:
766    /* Report a runaway argument and abort */
767    /* If |long_state=outer_call|, a runaway argument has already been reported. */
768    if (long_state == call_cmd) {
769        runaway();
770        print_err("Paragraph ended before ");
771        sprint_cs(warning_index);
772        tprint(" was complete");
773        help3("I suspect you've forgotten a `}', causing me to apply this",
774              "control sequence to too much text. How can we recover?",
775              "My plan is to forget the whole thing and hope for the best.");
776        back_error();
777    }
778    pstack[n] = token_link(temp_token_head);
779    align_state = align_state - unbalance;
780    for (m = 0; m <= n; m++)
781        flush_list(pstack[m]);
782
783  EXIT:
784    scanner_status = save_scanner_status;
785    warning_index = save_warning_index;
786}
787