Debian Codegraph @

texk/web2c/cwebdir/cweave.w

raw
1 % This file is part of CWEB.
2 % This program by Silvio Levy and Donald E. Knuth
3 % is based on a program by Knuth.
4 % It is distributed WITHOUT ANY WARRANTY, express or implied.
5 % Version 4.12.2 --- July 2025
6
7 % Copyright (C) 1987,1990,1993,2000 Silvio Levy and Donald E. Knuth
8
9 % Permission is granted to make and distribute verbatim copies of this
10 % document provided that the copyright notice and this permission notice
11 % are preserved on all copies.
12
13 % Permission is granted to copy and distribute modified versions of this
14 % document under the conditions for verbatim copying, provided that the
15 % entire resulting derived work is given a different name and distributed
16 % under the terms of a permission notice identical to this one.
17
18 % Amendments to 'cweave.w' resulting in this updated version were created
19 % by numerous collaborators over the course of many years.
20
21 % Please send comments, suggestions, etc. to tex-k@@tug.org.
22
23 % Here is TeX material that gets inserted after \input cwebmac
24 \def\hang{\hangindent 3em\indent\ignorespaces}
25 \def\pb{$\.|\ldots\.|$} % C brackets (|...|)
26 \def\v{\char'174} % vertical (|) in typewriter font
27 \def\dleft{[\![} \def\dright{]\!]} % double brackets
28 \mathchardef\RA="3221 % right arrow
29 \mathchardef\BA="3224 % double arrow
30 \def\TeXxstring{\\{\TEX/\_string}}
31 \def\skipxTeX{\\{skip\_\TEX/}}
32 \def\copyxTeX{\\{copy\_\TEX/}}
33
34 \def\title{CWEAVE (Version 4.12.2)}
35 \def\topofcontents{\null\vfill
36 \centerline{\titlefont The {\ttitlefont CWEAVE} processor}
37 \vskip 15pt
38 \centerline{(Version 4.12.2)}
39 \vfill}
40 \def\botofcontents{\vfill
41 \noindent
42 Copyright \copyright\ 1987, 1990, 1993, 2000 Silvio Levy and Donald E. Knuth
43 \bigskip\noindent
44 Permission is granted to make and distribute verbatim copies of this
45 document provided that the copyright notice and this permission notice
46 are preserved on all copies.
47
48 \smallskip\noindent
49 Permission is granted to copy and distribute modified versions of this
50 document under the conditions for verbatim copying, provided that the
51 entire resulting derived work is given a different name and distributed
52 under the terms of a permission notice identical to this one.
53 }
54 \pageno=\contentspagenumber \advance\pageno by 1
55 \let\maybe=\iftrue
56
57 @** Introduction.
58 This is the \.{CWEAVE} program by Silvio Levy and Donald E. Knuth,
59 based on \.{WEAVE} by Knuth.
60 We are thankful to Steve Avery,
61 Nelson Beebe, Hans-Hermann Bode (to whom the original \CPLUSPLUS/ adaptation
62 is due), Klaus Guntermann, Norman Ramsey, Tomas Rokicki, Joachim Schnitter,
63 Joachim Schrod, Lee Wittenberg, Saroj Mahapatra, Cesar Augusto Rorato
64 Crusius, and others who have contributed improvements.
65
66 The ``banner line'' defined here should be changed whenever \.{CWEAVE}
67 is modified.
68
69 @d banner "This is CWEAVE (Version 4.12.2)"
70
71 @c
72 @<Include files@>@/
73 @h
74 @<Common code for \.{CWEAVE} and \.{CTANGLE}@>@/
75 @<Typedef declarations@>@/
76 @<Private variables@>@/
77 @<Predeclaration of procedures@>
78
79 @ \.{CWEAVE} has a fairly straightforward outline. It operates in
80 three phases: First it inputs the source file and stores cross-reference
81 data, then it inputs the source once again and produces the \TEX/ output
82 file, finally it sorts and outputs the index.
83
84 Please read the documentation for \.{COMMON}, the set of routines common
85 to \.{CTANGLE} and \.{CWEAVE}, before proceeding further.
86
87 @c
88 int main (
89 int ac, /* argument count */
90 char **av) /* argument values */
91 {
92 argc=ac; argv=av;
93 program=cweave;
94 @<Set initial values@>@;
95 common_init();
96 @<Start \9{t}\TEX/ output@>@;
97 if (show_banner) puts(banner); /* print a ``banner line'' */
98 @<Store all the reserved words@>@;
99 phase_one(); /* read all the user's text and store the cross-references */
100 phase_two(); /* read all the text again and translate it to \TEX/ form */
101 phase_three(); /* output the cross-reference index */
102 if (tracing==fully && !show_progress) new_line();
103 return wrap_up(); /* and exit gracefully */
104 }
105
106 @ The next few sections contain stuff from the file |"common.w"| that must
107 be included in both |"ctangle.w"| and |"cweave.w"|. It appears in
108 file |"common.h"|, which is also included in |"common.w"| to propagate
109 possible changes from this \.{COMMON} interface consistently.
110
111 @i common.h
112
113 @ \.{CWEAVE} will use the |INT_MAX| limit in section
114 |@<Output the code for the beginning...@>| below.
115
116 @<Incl...@>=
117 #include <limits.h>
118
119 @* Data structures exclusive to {\tt CWEAVE}.
120 As explained in \.{common.w}, the field of a |name_info| structure
121 that contains the |rlink| of a section name is used for a completely
122 different purpose in the case of identifiers. It is then called the
123 |ilk| of the identifier, and it is used to
124 distinguish between various types of identifiers, as follows:
125
126 \yskip\hang |normal| and |func_template| identifiers are part of the
127 \CEE/ program that will appear in italic type (or in typewriter type
128 if all uppercase).
129
130 \yskip\hang |custom| identifiers are part of the \CEE/ program that
131 will be typeset in special ways.
132
133 \yskip\hang |roman| identifiers are index entries that appear after
134 \.{@@\^} in the \.{CWEB} file.
135
136 \yskip\hang |wildcard| identifiers are index entries that appear after
137 \.{@@:} in the \.{CWEB} file.
138
139 \yskip\hang |typewriter| identifiers are index entries that appear after
140 \.{@@.} in the \.{CWEB} file.
141
142 \yskip\hang |alfop|, \dots, |attr|
143 identifiers are \CEE/ or \CPLUSPLUS/ reserved words whose |ilk|
144 explains how they are to be treated when \CEE/ code is being
145 formatted.
146
147 @d normal 0 /* ordinary identifiers have |normal| ilk */
148 @d roman 1 /* normal index entries have |roman| ilk */
149 @d wildcard 2 /* user-formatted index entries have |wildcard| ilk */
150 @d typewriter 3 /* `typewriter type' entries have |typewriter| ilk */
151 @d abnormal(a) ((a)->ilk>typewriter) /* tells if a name is special */
152 @d func_template 4 /* identifiers that can be followed by optional template */
153 @d custom 5 /* identifiers with user-given control sequence */
154 @d alfop 22 /* alphabetic operators like \&{and} or \&{not\_eq} */
155 @d else_like 26 /* \&{else} */
156 @d public_like 40 /* \&{public}, \&{private}, \&{protected} */
157 @d operator_like 41 /* \&{operator} */
158 @d new_like 42 /* \&{new} */
159 @d catch_like 43 /* \&{catch} */
160 @d for_like 45 /* \&{for}, \&{switch}, \&{while} */
161 @d do_like 46 /* \&{do} */
162 @d if_like 47 /* \&{if}, \&{ifdef}, \&{endif}, \&{pragma}, \dots */
163 @d delete_like 48 /* \&{delete} */
164 @d raw_ubin 49 /* `\.\&' or `\.*' when looking for \&{const} following */
165 @d const_like 50 /* \&{const}, \&{volatile} */
166 @d raw_int 51 /* \&{int}, \&{char}, \dots; also structure and class names */
167 @d int_like 52 /* same, when not followed by left parenthesis or \DC\ */
168 @d case_like 53 /* \&{case}, \&{return}, \&{goto}, \&{break}, \&{continue} */
169 @d sizeof_like 54 /* \&{sizeof} */
170 @d struct_like 55 /* \&{struct}, \&{union}, \&{enum}, \&{class} */
171 @d typedef_like 56 /* \&{typedef} */
172 @d define_like 57 /* \&{define} */
173 @d template_like 58 /* \&{template} */
174 @d alignas_like 59 /* \&{alignas} */
175 @d using_like 60 /* \&{using} */
176 @d default_like 61 /* \&{default} */
177 @d attr 62 /* \&{noexcept} and attributes */
178
179 @ We keep track of the current section number in |section_count|, which
180 is the total number of sections that have started. Sections which have
181 been altered by a change file entry have their |changed_section| flag
182 turned on during the first phase.
183
184 @<Private...@>=
185 static bool change_exists; /* has any section changed? */
186
187 @ The other large memory area in \.{CWEAVE} keeps the cross-reference data.
188 All uses of the name |p| are recorded in a linked list beginning at
189 |p->xref|, which points into the |xmem| array. The elements of |xmem|
190 are structures consisting of an integer, |num|, and a pointer |xlink|
191 to another element of |xmem|. If |x=p->xref| is a pointer into |xmem|,
192 the value of |x->num| is either a section number where |p| is used,
193 or |cite_flag| plus a section number where |p| is mentioned,
194 or |def_flag| plus a section number where |p| is defined;
195 and |x->xlink| points to the next such cross-reference for |p|,
196 if any. This list of cross-references is in decreasing order by
197 section number. The next unused slot in |xmem| is |xref_ptr|.
198 The linked list ends at |&xmem[0]|.
199
200 The global variable |xref_switch| is set either to |def_flag| or to zero,
201 depending on whether the next cross-reference to an identifier is to be
202 underlined or not in the index. This switch is set to |def_flag| when
203 \.{@@!} or \.{@@d} is scanned, and it is cleared to zero when
204 the next identifier or index entry cross-reference has been made.
205 Similarly, the global variable |section_xref_switch| is either
206 |def_flag| or |cite_flag| or zero, depending
207 on whether a section name is being defined, cited or used in \CEE/ text.
208
209 @<Type...@>=
210 typedef struct xref_info {
211 sixteen_bits num; /* section number plus zero or |def_flag| */
212 struct xref_info *xlink; /* pointer to the previous cross-reference */
213 } xref_info;
214 typedef xref_info *xref_pointer;
215
216 @ @d max_refs 30000 /* number of cross-references; must be less than 65536 */
217
218 @<Private...@>=
219 static xref_info xmem[max_refs]; /* contains cross-reference information */
220 static xref_pointer xmem_end = xmem+max_refs-1;
221 static xref_pointer xref_ptr; /* the largest occupied position in |xmem| */
222 static sixteen_bits xref_switch,section_xref_switch; /* either zero or |def_flag| */
223
224 @ A section that is used for multi-file output (with the \.{@@(} feature)
225 has a special first cross-reference whose |num| field is |file_flag|.
226
227 @d file_flag (3*cite_flag)
228 @d def_flag (2*cite_flag)
229 @d cite_flag 10240 /* must be strictly larger than |max_sections| */
230 @d xref equiv_or_xref
231
232 @<Set init...@>=
233 xref_ptr=xmem; init_node(name_dir); xref_switch=section_xref_switch=0;
234 xmem->num=0; /* sentinel value */
235
236 @ A new cross-reference for an identifier is formed by calling |new_xref|,
237 which discards duplicate entries and ignores non-underlined references
238 to one-letter identifiers or \CEE/'s reserved words.
239
240 If the user has sent the |no_xref| flag (the \.{-x} option of the command line),
241 it is unnecessary to keep track of cross-references for identifiers.
242 If one were careful, one could probably make more changes around section
243 |@<Match a prod...@>| to avoid a lot of identifier looking up.
244
245 @d append_xref(c) if (xref_ptr==xmem_end) overflow("cross-reference");
246 else (++xref_ptr)->num=c
247 @d no_xref !make_xrefs
248 @d is_tiny(p) length(p)==1
249 @d unindexed(a) ((a)<res_wd_end && (a)->ilk>=custom)
250 /* tells if uses of a name are to be indexed */
251
252 @<Predecl...@>=
253 static void new_xref(name_pointer);@/
254 static void new_section_xref(name_pointer);@/
255 static void set_file_flag(name_pointer);
256
257 @ @c
258 static void
259 new_xref(
260 name_pointer p)
261 {
262 xref_pointer q; /* pointer to previous cross-reference */
263 sixteen_bits m, n; /* new and previous cross-reference value */
264 if (no_xref) return;
265 if ((unindexed(p) || is_tiny(p)) && xref_switch==0) return;
266 m=section_count+xref_switch; xref_switch=0; q=(xref_pointer)p->xref;
267 if (q != xmem) {
268 n=q->num;
269 if (n==m || n==m+def_flag) return;
270 else if (m==n+def_flag) {
271 q->num=m; return;
272 }
273 }
274 append_xref(m); xref_ptr->xlink=q; update_node(p);
275 }
276
277 @ The cross-reference lists for section names are slightly different.
278 Suppose that a section name is defined in sections $m_1$, \dots,
279 $m_k$, cited in sections $n_1$, \dots, $n_l$, and used in sections
280 $p_1$, \dots, $p_j$. Then its list will contain $m_1+|def_flag|$,
281 \dots, $m_k+|def_flag|$, $n_1+|cite_flag|$, \dots,
282 $n_l+|cite_flag|$, $p_1$, \dots, $p_j$, in this order.
283
284 Although this method of storage takes quadratic time with respect to
285 the length of the list, under foreseeable uses of \.{CWEAVE} this inefficiency
286 is insignificant.
287
288 @c
289 static void
290 new_section_xref(
291 name_pointer p)
292 {
293 xref_pointer q=(xref_pointer)p->xref;
294 xref_pointer r=xmem; /* pointers to previous cross-references */
295 if (q>r)
296 while (q->num>section_xref_switch) {r=q; q=q->xlink;}
297 if (r->num==section_count+section_xref_switch)
298 return; /* don't duplicate entries */
299 append_xref(section_count+section_xref_switch);
300 xref_ptr->xlink=q; section_xref_switch=0;
301 if (r==xmem) update_node(p);
302 else r->xlink=xref_ptr;
303 }
304
305 @ The cross-reference list for a section name may also begin with
306 |file_flag|. Here's how that flag gets put~in.
307
308 @c
309 static void
310 set_file_flag(
311 name_pointer p)
312 {
313 xref_pointer q=(xref_pointer)p->xref;
314 if (q->num==file_flag) return;
315 append_xref(file_flag);
316 xref_ptr->xlink = q;
317 update_node(p);
318 }
319
320 @ A third large area of memory is used for sixteen-bit `tokens', which appear
321 in short lists similar to the strings of characters in |byte_mem|. Token lists
322 are used to contain the result of \CEE/ code translated into \TEX/ form;
323 further details about them will be explained later. A \&{text\_pointer}
324 variable is an index into |tok_start|.
325
326 @<Typed...@>=
327 typedef sixteen_bits token;
328 typedef token *token_pointer;
329 typedef token_pointer *text_pointer;
330
331 @ The first position of |tok_mem|
332 that is unoccupied by replacement text is called |tok_ptr|, and the first
333 unused location of |tok_start| is called |text_ptr|.
334 Thus, we usually have |*text_ptr==tok_ptr|.
335
336 @d max_toks 30000 /* number of symbols in \CEE/ texts being parsed;
337 must be less than 65536 */
338 @d max_texts 8000 /* number of phrases in \CEE/ texts being parsed;
339 must be less than 10240 */
340
341 @<Private...@>=
342 static token tok_mem[max_toks]; /* tokens */
343 static token_pointer tok_mem_end = tok_mem+max_toks-1; /* end of |tok_mem| */
344 static token_pointer tok_ptr; /* first unused position in |tok_mem| */
345 static token_pointer max_tok_ptr; /* largest value of |tok_ptr| */
346 static token_pointer tok_start[max_texts]; /* directory into |tok_mem| */
347 static text_pointer tok_start_end = tok_start+max_texts-1; /* end of |tok_start| */
348 static text_pointer text_ptr; /* first unused position in |tok_start| */
349 static text_pointer max_text_ptr; /* largest value of |text_ptr| */
350
351 @ @<Set init...@>=
352 tok_ptr=max_tok_ptr=tok_mem+1;@/
353 tok_start[0]=tok_start[1]=tok_mem+1;@/
354 text_ptr=max_text_ptr=tok_start+1;
355
356 @ Here are the two procedures needed to complete |id_lookup|:
357 @c
358 bool names_match(
359 name_pointer p, /* points to the proposed match */
360 const char *first, /* position of first character of string */
361 size_t l, /* length of identifier */
362 eight_bits t) /* desired |ilk| */
363 {
364 if (length(p)!=l) return false;
365 if (p->ilk!=t && !(t==normal && abnormal(p))) return false;
366 return !strncmp(first,p->byte_start,l);
367 }
368
369 void
370 init_node(
371 name_pointer p)
372 {
373 p->xref=(void *)xmem;
374 }
375
376 @ And here's a small helper function to simplify the code.
377
378 @d update_node(p) (p)->xref=(void *)xref_ptr
379
380 @ We have to get \CEE/'s and \CPLUSPLUS/'s
381 reserved words into the hash table, and the simplest way to do this is
382 to insert them every time \.{CWEAVE} is run. Fortunately there are relatively
383 few reserved words. (Some of these are not strictly ``reserved,'' but
384 are defined in header files of the ISO Standard \CEE/ Library.
385 An ever growing list of \CPLUSPLUS/ keywords can be found here:
386 \.{https://en.cppreference.com/w/cpp/keyword}.)
387 @^reserved words@>
388
389 @<Store all the reserved words@>=
390 id_lookup("alignas",NULL,alignas_like);
391 id_lookup("alignof",NULL,sizeof_like);
392 id_lookup("and",NULL,alfop);
393 id_lookup("and_eq",NULL,alfop);
394 id_lookup("asm",NULL,sizeof_like);
395 id_lookup("auto",NULL,int_like);
396 id_lookup("bitand",NULL,alfop);
397 id_lookup("bitor",NULL,alfop);
398 id_lookup("bool",NULL,raw_int);
399 id_lookup("break",NULL,case_like);
400 id_lookup("case",NULL,case_like);
401 id_lookup("catch",NULL,catch_like);
402 id_lookup("char",NULL,raw_int);
403 id_lookup("char8_t",NULL,raw_int);
404 id_lookup("char16_t",NULL,raw_int);
405 id_lookup("char32_t",NULL,raw_int);
406 id_lookup("class",NULL,struct_like);
407 id_lookup("clock_t",NULL,raw_int);
408 id_lookup("compl",NULL,alfop);
409 id_lookup("concept",NULL,int_like);
410 id_lookup("const",NULL,const_like);
411 id_lookup("consteval",NULL,const_like);
412 id_lookup("constexpr",NULL,const_like);
413 id_lookup("constinit",NULL,const_like);
414 id_lookup("const_cast",NULL,raw_int);
415 id_lookup("continue",NULL,case_like);
416 id_lookup("co_await",NULL,case_like);
417 id_lookup("co_return",NULL,case_like);
418 id_lookup("co_yield",NULL,case_like);
419 id_lookup("decltype",NULL,sizeof_like);
420 id_lookup("default",NULL,default_like);
421 id_lookup("define",NULL,define_like);
422 id_lookup("defined",NULL,sizeof_like);
423 id_lookup("delete",NULL,delete_like);
424 id_lookup("div_t",NULL,raw_int);
425 id_lookup("do",NULL,do_like);
426 id_lookup("double",NULL,raw_int);
427 id_lookup("dynamic_cast",NULL,raw_int);
428 id_lookup("elif",NULL,if_like);
429 id_lookup("else",NULL,else_like);
430 id_lookup("endif",NULL,if_like);
431 id_lookup("enum",NULL,struct_like);
432 id_lookup("error",NULL,if_like);
433 id_lookup("explicit",NULL,int_like);
434 id_lookup("export",NULL,int_like);
435 id_lookup("extern",NULL,int_like);
436 id_lookup("FILE",NULL,raw_int);
437 id_lookup("false",NULL,normal);
438 id_lookup("float",NULL,raw_int);
439 id_lookup("for",NULL,for_like);
440 id_lookup("fpos_t",NULL,raw_int);
441 id_lookup("friend",NULL,int_like);
442 id_lookup("goto",NULL,case_like);
443 id_lookup("if",NULL,if_like);
444 id_lookup("ifdef",NULL,if_like);
445 id_lookup("ifndef",NULL,if_like);
446 id_lookup("include",NULL,if_like);
447 id_lookup("inline",NULL,int_like);
448 id_lookup("int",NULL,raw_int);
449 id_lookup("jmp_buf",NULL,raw_int);
450 id_lookup("ldiv_t",NULL,raw_int);
451 id_lookup("line",NULL,if_like);
452 id_lookup("long",NULL,raw_int);
453 id_lookup("mutable",NULL,int_like);
454 id_lookup("namespace",NULL,struct_like);
455 id_lookup("new",NULL,new_like);
456 id_lookup("noexcept",NULL,attr);
457 id_lookup("not",NULL,alfop);
458 id_lookup("not_eq",NULL,alfop);
459 id_lookup("NULL",NULL,custom);
460 id_lookup("nullptr",NULL,custom);
461 id_lookup("offsetof",NULL,raw_int);
462 id_lookup("operator",NULL,operator_like);
463 id_lookup("or",NULL,alfop);
464 id_lookup("or_eq",NULL,alfop);
465 id_lookup("pragma",NULL,if_like);
466 id_lookup("private",NULL,public_like);
467 id_lookup("protected",NULL,public_like);
468 id_lookup("ptrdiff_t",NULL,raw_int);
469 id_lookup("public",NULL,public_like);
470 id_lookup("register",NULL,int_like);
471 id_lookup("reinterpret_cast",NULL,raw_int);
472 id_lookup("requires",NULL,int_like);
473 id_lookup("restrict",NULL,int_like);
474 id_lookup("return",NULL,case_like);
475 id_lookup("short",NULL,raw_int);
476 id_lookup("sig_atomic_t",NULL,raw_int);
477 id_lookup("signed",NULL,raw_int);
478 id_lookup("size_t",NULL,raw_int);
479 id_lookup("sizeof",NULL,sizeof_like);
480 id_lookup("static",NULL,int_like);
481 id_lookup("static_assert",NULL,sizeof_like);
482 id_lookup("static_cast",NULL,raw_int);
483 id_lookup("struct",NULL,struct_like);
484 id_lookup("switch",NULL,for_like);
485 id_lookup("template",NULL,template_like);
486 id_lookup("this",NULL,custom);
487 id_lookup("thread_local",NULL,raw_int);
488 id_lookup("throw",NULL,case_like);
489 id_lookup("time_t",NULL,raw_int);
490 id_lookup("true",NULL,normal);
491 id_lookup("try",NULL,else_like);
492 id_lookup("typedef",NULL,typedef_like);
493 id_lookup("typeid",NULL,sizeof_like);
494 id_lookup("typename",NULL,struct_like);
495 id_lookup("undef",NULL,if_like);
496 id_lookup("union",NULL,struct_like);
497 id_lookup("unsigned",NULL,raw_int);
498 id_lookup("using",NULL,using_like);@/
499 id_lookup("va_dcl",NULL,decl); /* Berkeley's variable-arg-list convention */
500 id_lookup("va_list",NULL,raw_int); /* ditto */
501 id_lookup("virtual",NULL,int_like);
502 id_lookup("void",NULL,raw_int);
503 id_lookup("volatile",NULL,const_like);
504 id_lookup("wchar_t",NULL,raw_int);
505 id_lookup("while",NULL,for_like);
506 id_lookup("xor",NULL,alfop);
507 id_lookup("xor_eq",NULL,alfop);@+ res_wd_end=name_ptr;
508 id_lookup("TeX",NULL,custom);
509 id_lookup("complex",NULL,int_like);
510 id_lookup("imaginary",NULL,int_like);
511 id_lookup("make_pair",NULL,func_template);
512
513 @* Lexical scanning.
514 Let us now consider the subroutines that read the \.{CWEB} source file
515 and break it into meaningful units. There are four such procedures:
516 One simply skips to the next `\.{@@\ }' or `\.{@@*}' that begins a
517 section; another passes over the \TEX/ text at the beginning of a
518 section; the third passes over the \TEX/ text in a \CEE/ comment;
519 and the last, which is the most interesting, gets the next token of
520 a \CEE/ text. They all use the pointers |limit| and |loc| into
521 the line of input currently being studied.
522
523 @ Control codes in \.{CWEB}, which begin with `\.{@@}', are converted
524 into a numeric code designed to simplify \.{CWEAVE}'s logic; for example,
525 larger numbers are given to the control codes that denote more significant
526 milestones, and the code of |new_section| should be the largest of
527 all. Some of these numeric control codes take the place of |char|
528 control codes that will not otherwise appear in the output of the
529 scanning routines.
530 @^ASCII code dependencies@>
531
532 @d ignore 00 /* control code of no interest to \.{CWEAVE} */
533 @d verbatim 02 /* takes the place of ASCII \.{STX} */
534 @d begin_short_comment 03 /* \CPLUSPLUS/ short comment */
535 @d begin_comment '\t' /* tab marks will not appear */
536 @d underline '\n' /* this code will be intercepted without confusion */
537 @d noop 0177 /* takes the place of ASCII \.{DEL} */
538 @d xref_roman 0203 /* control code for `\.{@@\^}' */
539 @d xref_wildcard 0204 /* control code for `\.{@@:}' */
540 @d xref_typewriter 0205 /* control code for `\.{@@.}' */
541 @d TeX_string 0206 /* control code for `\.{@@t}' */
542 @f TeX_string TeX
543 @d ord 0207 /* control code for `\.{@@'}' */
544 @d join 0210 /* control code for `\.{@@\&}' */
545 @d thin_space 0211 /* control code for `\.{@@,}' */
546 @d math_break 0212 /* control code for `\.{@@\v}' */
547 @d line_break 0213 /* control code for `\.{@@/}' */
548 @d big_line_break 0214 /* control code for `\.{@@\#}' */
549 @d no_line_break 0215 /* control code for `\.{@@+}' */
550 @d pseudo_semi 0216 /* control code for `\.{@@;}' */
551 @d macro_arg_open 0220 /* control code for `\.{@@[}' */
552 @d macro_arg_close 0221 /* control code for `\.{@@]}' */
553 @d trace 0222 /* control code for `\.{@@0}', `\.{@@1}' and `\.{@@2}' */
554 @d translit_code 0223 /* control code for `\.{@@l}' */
555 @d output_defs_code 0224 /* control code for `\.{@@h}' */
556 @d format_code 0225 /* control code for `\.{@@f}' and `\.{@@s}' */
557 @d definition 0226 /* control code for `\.{@@d}' */
558 @d begin_C 0227 /* control code for `\.{@@c}' */
559 @d section_name 0230 /* control code for `\.{@@<}' */
560 @d new_section 0231 /* control code for `\.{@@\ }' and `\.{@@*}' */
561
562 @ Control codes are converted to \.{CWEAVE}'s internal
563 representation by means of the table |ccode|.
564
565 @<Private...@>=
566 static eight_bits ccode[256]={ignore}; /* meaning of a char following \.{@@} */
567
568 @ @<Set ini...@>=
569 ccode[' ']=ccode['\t']=ccode['\n']=ccode['\v']=ccode['\r']=ccode['\f']
570 =ccode['*']=new_section;
571 ccode['@@']='@@'; /* `quoted' at sign */
572 ccode['=']=verbatim;
573 ccode['d']=ccode['D']=definition;
574 ccode['f']=ccode['F']=ccode['s']=ccode['S']=format_code;
575 ccode['c']=ccode['C']=ccode['p']=ccode['P']=begin_C;
576 ccode['t']=ccode['T']=TeX_string;
577 ccode['l']=ccode['L']=translit_code;
578 ccode['q']=ccode['Q']=noop;
579 ccode['h']=ccode['H']=output_defs_code;
580 ccode['&']=join; ccode['<']=ccode['(']=section_name;
581 ccode['!']=underline; ccode['^']=xref_roman;
582 ccode[':']=xref_wildcard; ccode['.']=xref_typewriter; ccode[',']=thin_space;
583 ccode['|']=math_break; ccode['/']=line_break; ccode['#']=big_line_break;
584 ccode['+']=no_line_break; ccode[';']=pseudo_semi;
585 ccode['[']=macro_arg_open; ccode[']']=macro_arg_close;
586 ccode['\'']=ord;
587 @<Special control codes for debugging@>@;
588
589 @ Users can write
590 \.{@@2}, \.{@@1}, and \.{@@0} to turn tracing |fully| on, |partly| on,
591 and |off|, respectively.
592
593 @<Special control codes...@>=
594 ccode['0']=ccode['1']=ccode['2']=trace;
595
596 @ The |skip_limbo| routine is used on the first pass to skip through
597 portions of the input that are not in any sections, i.e., that precede
598 the first section. After this procedure has been called, the value of
599 |input_has_ended| will tell whether or not a section has actually been found.
600
601 There's a complication that we will postpone until later: If the \.{@@s}
602 operation appears in limbo, we want to use it to adjust the default
603 interpretation of identifiers.
604
605 @<Predec...@>=
606 static void skip_limbo(void);@/
607 static eight_bits skip_TeX(void);
608
609 @ @c
610 static void
611 skip_limbo(void) {
612 while(true) {
613 if (loc>limit && get_line()==false) return;
614 *(limit+1)='@@';
615 while (*loc!='@@') loc++; /* look for `\.{@@}', then skip two chars */
616 if (loc++ <=limit)
617 switch (ccode[(eight_bits)*loc++]) {
618 case new_section: return;
619 case noop: skip_restricted(); break;
620 case format_code: @<Process simple format in limbo@>@;
621 }
622 }
623 }
624
625 @ The |skip_TeX| routine is used on the first pass to skip through
626 the \TEX/ code at the beginning of a section. It returns the next
627 control code or `\.{\v}' found in the input. A |new_section| is
628 assumed to exist at the very end of the file.
629
630 @f skip_TeX TeX
631
632 @c
633 static eight_bits
634 skip_TeX(void) /* skip past pure \TEX/ code */
635 {
636 while (true) {
637 if (loc>limit && get_line()==false) return new_section;
638 *(limit+1)='@@';
639 while (*loc!='@@' && *loc!='|') loc++;
640 if (*loc++ =='|') return (eight_bits)'|';
641 if (loc<=limit) return ccode[(eight_bits)*(loc++)];
642 }
643 }
644
645 @* Inputting the next token.
646 As stated above, \.{CWEAVE}'s most interesting lexical scanning routine is the
647 |get_next| function that inputs the next token of \CEE/ input. However,
648 |get_next| is not especially complicated.
649
650 The result of |get_next| is either a |char| code for some special character,
651 or it is a special code representing a pair of characters (e.g., `\.{!=}'),
652 or it is the numeric value computed by the |ccode|
653 table, or it is one of the following special codes:
654
655 \yskip\hang |identifier|: In this case the global variables |id_first| and
656 |id_loc| will have been set to the beginning and ending-plus-one locations
657 in the buffer, as required by the |id_lookup| routine.
658
659 \yskip\hang |string|: The string will have been copied into the array
660 |section_text|; |id_first| and |id_loc| are set as above (now they are
661 pointers into |section_text|).
662
663 \yskip\hang |constant|: The constant is copied into |section_text|, with
664 slight modifications; |id_first| and |id_loc| are set.
665
666 \yskip\noindent Furthermore, some of the control codes cause
667 |get_next| to take additional actions:
668
669 \yskip\hang |xref_roman|, |xref_wildcard|, |xref_typewriter|, |TeX_string|,
670 |verbatim|: The values of |id_first| and |id_loc| will have been set to
671 the beginning and ending-plus-one locations in the buffer.
672
673 \yskip\hang |section_name|: In this case the global variable |cur_section| will
674 point to the |byte_start| entry for the section name that has just been scanned.
675 The value of |cur_section_char| will be |'('| if the section name was
676 preceded by \.{@@(} instead of \.{@@<}.
677
678 \yskip\noindent If |get_next| sees `\.{@@!}'
679 it sets |xref_switch| to |def_flag| and goes on to the next token.
680
681 @d constant 0200 /* \CEE/ constant */
682 @d string 0201 /* \CEE/ string */
683 @d identifier 0202 /* \CEE/ identifier or reserved word */
684
685 @<Private...@>=
686 static name_pointer cur_section; /* name of section just scanned */
687 static char cur_section_char; /* the character just before that name */
688
689 @ As one might expect, |get_next| consists mostly of a big switch
690 that branches to the various special cases that can arise.
691
692 @c
693 static eight_bits
694 get_next(void) /* produces the next input token */
695 {
696 eight_bits c; /* the current character */
697 while (true) {
698 @<Check if we're at the end of a preprocessor command@>@;
699 if (loc>limit && get_line()==false) return new_section;
700 c=*(loc++);
701 if (xisdigit((int)c) || c=='.') @<Get a constant@>@;
702 else if (c=='\'' || c=='"'@|
703 || ((c=='L' || c=='u' || c=='U')&&(*loc=='\'' || *loc=='"'))@|
704 || ((c=='u' && *loc=='8')&&(*(loc+1)=='\'' || *(loc+1)=='"'))@|
705 || (c=='<' && sharp_include_line==true))
706 @<Get a string@>@;
707 else if (isalpha((int)c) || isxalpha(c) || ishigh(c))
708 @<Get an identifier@>@;
709 else if (c=='@@') @<Get control code and possible section name@>@;
710 else if (xisspace(c)) continue; /* ignore spaces and tabs */
711 if (c=='#' && loc==buffer+1) @<Raise preprocessor flag@>@;
712 mistake: @<Compress two-symbol operator@>@;
713 return c;
714 }
715 }
716
717 @ @<Predecl...@>=@+static eight_bits get_next(void);
718
719 @ Because preprocessor commands do not fit in with the rest of the syntax
720 of \CEE/,
721 we have to deal with them separately. One solution is to enclose such
722 commands between special markers. Thus, when a \.\# is seen as the
723 first character of a line, |get_next| returns a special code
724 |left_preproc| and raises a flag |preprocessing|.
725
726 We can use the same internal code number for |left_preproc| as we do
727 for |ord|, since |get_next| changes |ord| into a string.
728
729 @d left_preproc ord /* begins a preprocessor command */
730 @d right_preproc 0217 /* ends a preprocessor command */
731
732 @<Private...@>=
733 static bool preprocessing=false; /* are we scanning a preprocessor command? */
734
735 @ @<Raise prep...@>= {
736 preprocessing=true;
737 @<Check if next token is |include|@>@;
738 return left_preproc;
739 }
740
741 @ An additional complication is the freakish use of \.< and \.> to delimit
742 a file name in lines that start with \.{\#include}. We must treat this file
743 name as a string.
744
745 @<Private...@>=
746 static bool sharp_include_line=false;
747 /* are we scanning a \#\&{include} line? */
748
749 @ @<Check if next token is |include|@>=
750 while (loc<=buffer_end-7 && xisspace(*loc)) loc++;
751 if (loc<=buffer_end-6 && strncmp(loc,"include",7)==0) sharp_include_line=true;
752
753 @ When we get to the end of a preprocessor line,
754 we lower the flag and send a code |right_preproc|, unless
755 the last character was a \.\\.
756
757 @<Check if we're at...@>=
758 while (loc==limit-1 && preprocessing && *loc=='\\')
759 if (get_line()==false) return new_section; /* still in preprocessor mode */
760 if (loc>=limit && preprocessing) {
761 preprocessing=sharp_include_line=false;
762 return right_preproc;
763 }
764
765 @ The following code assigns values to the combinations \.{++},
766 \.{--}, \.{->}, \.{>=}, \.{<=}, \.{==}, \.{<<}, \.{>>}, \.{!=}, %\.{||}
767 \.{\v\v} and~\.{\&\&}, and to the \CPLUSPLUS/
768 combinations \.{...}, \.{::}, \.{.*} and \.{->*}.
769 The compound assignment operators (e.g., \.{+=}) are
770 treated as separate tokens.
771
772 @<Compress tw...@>=
773 switch(c) {
774 case '/': if (*loc=='*') {@+compress(begin_comment);@+}
775 else if (*loc=='/') compress(begin_short_comment); break;
776 case '+': if (*loc=='+') compress(plus_plus); break;
777 case '-': if (*loc=='-') {@+compress(minus_minus);@+}
778 else if (*loc=='>') {
779 if (*(loc+1)=='*') {loc++;@+compress(minus_gt_ast);}
780 else compress(minus_gt);
781 } break;
782 case '.': if (*loc=='*') {@+compress(period_ast);@+}
783 else if (*loc=='.' && *(loc+1)=='.') {
784 loc++;@+compress(dot_dot_dot);
785 } break;
786 case ':': if (*loc==':') compress(colon_colon); break;
787 case '=': if (*loc=='=') compress(eq_eq); break;
788 case '>': if (*loc=='=') {@+compress(gt_eq);@+}
789 else if (*loc=='>') compress(gt_gt); break;
790 case '<': if (*loc=='=') {@+compress(lt_eq);@+}
791 else if (*loc=='<') compress(lt_lt); break;
792 case '&': if (*loc=='&') compress(and_and); break;
793 case '|': if (*loc=='|') compress(or_or); break;
794 case '!': if (*loc=='=') compress(non_eq); break;
795 }
796
797 @ @<Get an identifier@>= {
798 id_first=--loc;
799 do
800 ++loc;
801 while (isalpha((int)*loc) || isdigit((int)*loc) @|
802 || isxalpha(*loc) || ishigh(*loc));
803 id_loc=loc; return identifier;
804 }
805
806 @ Different conventions are followed by \TEX/ and \CEE/ to express octal
807 and hexadecimal numbers; it is reasonable to stick to each convention
808 within its realm. Thus the \CEE/ part of a \.{CWEB} file has octals
809 introduced by \.0 and hexadecimals by \.{0x}, but \.{CWEAVE} will print
810 with \TEX/ macros that the user can redefine to fit the context.
811 In order to simplify such macros, we replace some of the characters.
812
813 On output, the \.{\ } that replaces \.{'} in \CPLUSPLUS/ literals will become
814 ``\.{\\\ }''.
815
816 Notice that in this section and the next, |id_first| and |id_loc|
817 are pointers into the array |section_text|, not into |buffer|.
818
819 @d gather_digits_while(t) while ((t) || *loc=='\'')
820 if (*loc=='\'') { /* \CPLUSPLUS/-style digit separator */
821 *id_loc++=' ';@+ loc++; /* insert a little white space */
822 @.\\\ @>
823 }@+else *id_loc++=*loc++
824
825 @<Get a constant@>= {
826 id_first=id_loc=section_text+1;
827 if (*(loc-1)=='.' && !xisdigit(*loc)) goto mistake; /* not a constant */
828 if (*(loc-1)=='0') {
829 if (*loc=='x' || *loc=='X') @<Get a hexadecimal constant@>@;
830 else if (*loc=='b' || *loc=='B') @<Get a binary constant@>@;
831 else if (xisdigit(*loc)) @<Get an octal constant@>@;
832 }
833 *id_loc++=*(loc-1); /* decimal constant */
834 gather_digits_while(xisdigit(*loc) || *loc=='.');
835 get_exponent:
836 if (*loc=='e' || *loc=='E')
837 *id_loc++='_';
838 else if (*loc=='p' || *loc=='P')
839 *id_loc++='%';
840 else
841 goto digit_suffix;
842 loc++;
843 if (*loc=='+' || *loc=='-') *id_loc++=*loc++;
844 gather_digits_while(xisdigit(*loc));
845 digit_suffix:
846 while (*loc=='u' || *loc=='U' || *loc=='l' || *loc=='L'
847 || *loc=='f' || *loc=='F') {
848 *id_loc++='$'; *id_loc++=toupper((int)*loc); loc++;
849 }
850 return constant;
851 }
852
853 @ @<Get a hex...@>={
854 *id_loc++='^'; loc++;
855 gather_digits_while(xisxdigit(*loc) || *loc=='.');
856 goto get_exponent;
857 }
858
859 @ @<Get a bin...@>={
860 *id_loc++='\\'; loc++;
861 gather_digits_while(*loc=='0' || *loc=='1');
862 goto digit_suffix;
863 }
864
865 @ @<Get an oct...@>={
866 *id_loc++='~'; gather_digits_while(xisdigit(*loc));
867 goto digit_suffix;
868 }
869
870 @ \CEE/ strings and character constants, delimited by double and single
871 quotes, respectively, can contain newlines or instances of their own
872 delimiters if they are protected by a backslash. We follow this
873 convention, but do not allow the string to be longer than |longest_name|.
874
875 @<Get a string@>= {@+
876 char delim = c; /* what started the string */
877 id_first = section_text+1;
878 id_loc = section_text;
879 if (delim=='\'' && *(loc-2)=='@@') {*++id_loc='@@'; *++id_loc='@@';}
880 *++id_loc=delim;
881 if (delim=='L' || delim=='u' || delim=='U')
882 @<Get a wide character constant@>@;
883 if (delim=='<') delim='>'; /* for file names in \#\&{include} lines */
884 while (true) {
885 if (loc>=limit) {
886 if(*(limit-1)!='\\') {
887 err_print("! String didn't end"); loc=limit; break;
888 @.String didn't end@>
889 }
890 if(get_line()==false) {
891 err_print("! Input ended in middle of string"); loc=buffer; break;
892 @.Input ended in middle of string@>
893 }
894 }
895 if ((c=*loc++)==delim) {
896 if (++id_loc<=section_text_end) *id_loc=c;
897 break;
898 }
899 if (c=='\\') {
900 if (loc>=limit) continue;
901 else {
902 if (++id_loc<=section_text_end) {
903 *id_loc = '\\'; c=*loc++;
904 }
905 }
906 }
907 if (++id_loc<=section_text_end) *id_loc=c;
908 }
909 if (id_loc>=section_text_end) {
910 printf("%s","\n! String too long: ");
911 @.String too long@>
912 term_write(section_text+1,25);
913 printf("..."); mark_error();
914 }
915 id_loc++;
916 return string;
917 }
918
919 @ @<Get a wide...@>={
920 if (delim=='u' && *loc=='8') *++id_loc=*loc++;
921 delim=*loc++; *++id_loc=delim;
922 }
923
924 @ After an \.{@@} sign has been scanned, the next character tells us
925 whether there is more work to do.
926
927 @<Get control code and possible section name@>=
928 switch(ccode[c=*loc++]) {
929 case translit_code: err_print("! Use @@l in limbo only"); continue;
930 @.Use @@l in limbo...@>
931 case underline: xref_switch=def_flag; continue;
932 case trace: tracing=c-'0'; continue;
933 case section_name:
934 @<Scan the section name and make |cur_section| point to it@>@;
935 case verbatim: @<Scan a verbatim string@>@;
936 case ord: @<Get a string@>@;
937 case xref_roman: case xref_wildcard: case xref_typewriter: case noop:
938 case TeX_string: skip_restricted(); @=/* fall through */@>@;
939 default: return ccode[c];
940 }
941
942 @ The occurrence of a section name sets |xref_switch| to zero,
943 because the section name might (for example) follow \&{int}.
944
945 @<Scan the section name...@>= {
946 char *k=section_text; /* pointer into |section_text| */
947 cur_section_char=*(loc-1);
948 @<Put section name into |section_text|@>@;
949 if (k-section_text>3 && strncmp(k-2,"...",3)==0)
950 cur_section=section_lookup(section_text+1,k-3,true);
951 /* |true| indicates a prefix */
952 else cur_section=section_lookup(section_text+1,k,false);
953 xref_switch=0; return section_name;
954 }
955
956 @ Section names are placed into the |section_text| array with consecutive spaces,
957 tabs, and carriage-returns replaced by single spaces. There will be no
958 spaces at the beginning or the end. (We set |section_text[0]=' '| to facilitate
959 this, since the |section_lookup| routine uses |section_text[1]| as the first
960 character of the name.)
961
962 @<Set init...@>=section_text[0]=' ';
963
964 @ @<Put section name...@>=
965 while (true) {
966 if (loc>limit && get_line()==false) {
967 err_print("! Input ended in section name");
968 @.Input ended in section name@>
969 loc=buffer+1; break;
970 }
971 c=*loc;
972 @<If end of name or erroneous control code, |break|@>@;
973 loc++; if (k<section_text_end) k++;
974 if (xisspace(c)) {
975 c=' '; if (*(k-1)==' ') k--;
976 }
977 *k=c;
978 }
979 if (k>=section_text_end) {
980 printf("%s","\n! Section name too long: ");
981 @.Section name too long@>
982 term_write(section_text+1,25);
983 printf("..."); mark_harmless();
984 }
985 if (*k==' ' && k>section_text) k--;
986
987 @ @<If end of name...@>=
988 if (c=='@@') {
989 c=*(loc+1);
990 if (c=='>') {
991 loc+=2; break;
992 }
993 if (ccode[c]==new_section) {
994 err_print("! Section name didn't end"); break;
995 @.Section name didn't end@>
996 }
997 if (c!='@@') {
998 err_print("! Control codes are forbidden in section name"); break;
999 @.Control codes are forbidden...@>
1000 }
1001 *(++k)='@@'; loc++; /* now |c==*loc| again */
1002 }
1003
1004 @ This function skips over a restricted context at relatively high speed.
1005
1006 @c
1007 static void
1008 skip_restricted(void)
1009 {
1010 id_first=loc; *(limit+1)='@@';
1011 false_alarm:
1012 while (*loc!='@@') loc++;
1013 id_loc=loc;
1014 if (loc++>limit) {
1015 err_print("! Control text didn't end"); loc=limit;
1016 @.Control text didn't end@>
1017 } else {
1018 if (*loc=='@@'&&loc<=limit) {loc++; goto false_alarm;}
1019 if (*loc++!='>')
1020 err_print("! Control codes are forbidden in control text");
1021 @.Control codes are forbidden...@>
1022 }
1023 }
1024
1025 @ @<Predecl...@>=@+static void skip_restricted(void);
1026
1027 @ At the present point in the program we
1028 have |*(loc-1)==verbatim|; we set |id_first| to the beginning
1029 of the string itself, and |id_loc| to its ending-plus-one location in the
1030 buffer. We also set |loc| to the position just after the ending delimiter.
1031
1032 @<Scan a verbatim string@>=
1033 id_first=loc++; *(limit+1)='@@'; *(limit+2)='>';
1034 while (*loc!='@@' || *(loc+1)!='>') loc++;
1035 if (loc>=limit) err_print("! Verbatim string didn't end");
1036 @.Verbatim string didn't end@>
1037 id_loc=loc; loc+=2;
1038 return verbatim;
1039
1040 @** Phase one processing.
1041 We now have accumulated enough subroutines to make it possible to carry out
1042 \.{CWEAVE}'s first pass over the source file. If everything works right,
1043 both phase one and phase two of \.{CWEAVE} will assign the same numbers to
1044 sections, and these numbers will agree with what \.{CTANGLE} does.
1045
1046 The global variable |next_control| often contains the most recent output of
1047 |get_next|; in interesting cases, this will be the control code that
1048 ended a section or part of a section.
1049
1050 @<Private...@>=
1051 static eight_bits next_control; /* control code waiting to be acting upon */
1052
1053 @ The overall processing strategy in phase one has the following
1054 straightforward outline.
1055
1056 @c
1057 static void
1058 phase_one(void) {
1059 phase=1; reset_input(); section_count=0;
1060 skip_limbo(); change_exists=false;
1061 while (!input_has_ended)
1062 @<Store cross-reference data for the current section@>@;
1063 changed_section[section_count]=change_exists;
1064 /* the index changes if anything does */
1065 @<Print error messages about unused or undefined section names@>@;
1066 }
1067
1068 @ @<Predecl...@>=@+static void phase_one(void);
1069
1070 @ @<Store cross-reference data...@>=
1071 {
1072 if (++section_count==max_sections) overflow("section number");
1073 changed_section[section_count]=changing;
1074 /* it will become |true| if any line changes */
1075 if (*(loc-1)=='*' && show_progress) {
1076 printf("*%d",(int)section_count);
1077 update_terminal(); /* print a progress report */
1078 }
1079 @<Store cross-references in the \9{t}\TEX/ part of a section@>@;
1080 @<Store cross-references in the \9{d}definition part of a section@>@;
1081 @<Store cross-references in the \9{c}\CEE/ part of a section@>@;
1082 if (changed_section[section_count]) change_exists=true;
1083 }
1084
1085 @ The |C_xref| subroutine stores references to identifiers in
1086 \CEE/ text material beginning with the current value of |next_control|
1087 and continuing until |next_control| is `\.\{' or `\.{\v}', or until the next
1088 ``milestone'' is passed (i.e., |next_control>=format_code|). If
1089 |next_control>=format_code| when |C_xref| is called, nothing will happen;
1090 but if |next_control=='|'| upon entry, the procedure assumes that this is
1091 the `\.{\v}' preceding \CEE/ text that is to be processed.
1092
1093 The parameter |spec_ctrl| is used to change this behavior. In most cases
1094 |C_xref| is called with |spec_ctrl==ignore|, which triggers the default
1095 processing described above. If |spec_ctrl==section_name|, section names will
1096 be gobbled. This is used when \CEE/ text in the \TEX/ part or inside comments
1097 is parsed: It allows for section names to appear in \pb, but these
1098 strings will not be entered into the cross reference lists since they are not
1099 definitions of section names.
1100
1101 The program uses the fact that our internal code numbers satisfy
1102 the relations |xref_roman==identifier+roman| and |xref_wildcard==identifier
1103 +wildcard| and |xref_typewriter==identifier+typewriter|,
1104 as well as |normal==0|.
1105
1106 @<Predecl...@>=
1107 static void C_xref(eight_bits);@/
1108 static void outer_xref(void);
1109
1110 @ @c
1111 static void
1112 C_xref( /* makes cross-references for \CEE/ identifiers */
1113 eight_bits spec_ctrl)
1114 {
1115 while (next_control<format_code || next_control==spec_ctrl) {
1116 if (next_control>=identifier && next_control<=xref_typewriter) {
1117 if (next_control>identifier) @<Replace `\.{@@@@}' by `\.{@@}'@>@;
1118 new_xref(id_lookup(id_first,id_loc,next_control-identifier));
1119 }
1120 if (next_control==section_name) {
1121 section_xref_switch=cite_flag;
1122 new_section_xref(cur_section);
1123 }
1124 next_control=get_next();
1125 if (next_control=='|' || next_control==begin_comment ||
1126 next_control==begin_short_comment) return;
1127 }
1128 }
1129
1130 @ The |outer_xref| subroutine is like |C_xref| except that it begins
1131 with |next_control!='|'| and ends with |next_control>=format_code|. Thus, it
1132 handles \CEE/ text with embedded comments.
1133
1134 @c
1135 static void
1136 outer_xref(void) /* extension of |C_xref| */
1137 {
1138 int bal; /* brace level in comment */
1139 while (next_control<format_code)
1140 if (next_control!=begin_comment && next_control!=begin_short_comment)
1141 C_xref(ignore);
1142 else {
1143 bool is_long_comment=(next_control==begin_comment);
1144 bal=copy_comment(is_long_comment,1); next_control='|';
1145 while (bal>0) {
1146 C_xref(section_name); /* do not reference section names in comments */
1147 if (next_control=='|') bal=copy_comment(is_long_comment,bal);
1148 else bal=0; /* an error message will occur in phase two */
1149 }
1150 }
1151 }
1152
1153 @ In the \TEX/ part of a section, cross-reference entries are made only for
1154 the identifiers in \CEE/ texts enclosed in \pb, or for control texts
1155 enclosed in \.{@@\^}$\,\ldots\,$\.{@@>} or \.{@@.}$\,\ldots\,$\.{@@>}
1156 or \.{@@:}$\,\ldots\,$\.{@@>}.
1157
1158 @<Store cross-references in the \9{t}\T...@>=
1159 while (true) {
1160 switch (next_control=skip_TeX()) {
1161 case translit_code: err_print("! Use @@l in limbo only"); continue;
1162 @.Use @@l in limbo...@>
1163 case underline: xref_switch=def_flag; continue;
1164 case trace: tracing=*(loc-1)-'0'; continue;
1165 case '|': C_xref(section_name); break;
1166 case xref_roman: case xref_wildcard: case xref_typewriter:
1167 case noop: case section_name:
1168 loc-=2; next_control=get_next(); /* scan to \.{@@>} */
1169 if (next_control>=xref_roman && next_control<=xref_typewriter) {
1170 @<Replace `\.{@@@@}' by `\.{@@}'@>@;
1171 new_xref(id_lookup(id_first,id_loc,next_control-identifier));
1172 }
1173 break;
1174 }
1175 if (next_control>=format_code) break;
1176 }
1177
1178 @ @<Replace `\.{@@@@}' by `\.{@@}'@>=
1179 {
1180 char *src=id_first,*dst=id_first;
1181 while(src<id_loc){
1182 if(*src=='@@') src++;
1183 *dst++=*src++;
1184 }
1185 id_loc=dst;
1186 while (dst<src) *dst++=' '; /* clean up in case of error message display */
1187 }
1188
1189 @ During the definition and \CEE/ parts of a section, cross-references
1190 are made for all identifiers except reserved words. However, the right
1191 identifier in a format definition is not referenced, and the left
1192 identifier is referenced only if it has been explicitly
1193 underlined (preceded by \.{@@!}).
1194 The \TEX/ code in comments is, of course, ignored, except for
1195 \CEE/ portions enclosed in \pb; the text of a section name is skipped
1196 entirely, even if it contains \pb\ constructions.
1197
1198 The variables |lhs| and |rhs| point to the respective identifiers involved
1199 in a format definition.
1200
1201 @<Private...@>=
1202 static name_pointer lhs, rhs; /* pointers to |byte_start| for format identifiers */
1203 static name_pointer res_wd_end; /* pointer to the first nonreserved identifier */
1204
1205 @ When we get to the following code we have |next_control>=format_code|.
1206
1207 @<Store cross-references in the \9{d}d...@>=
1208 while (next_control<=definition) { /* |format_code| or |definition| */
1209 if (next_control==definition) {
1210 xref_switch=def_flag; /* implied \.{@@!} */
1211 next_control=get_next();
1212 } else @<Process a format definition@>@;
1213 outer_xref();
1214 }
1215
1216 @ Error messages for improper format definitions will be issued in phase
1217 two. Our job in phase one is to define the |ilk| of a properly formatted
1218 identifier, and to remove cross-references to identifiers that we now
1219 discover should be unindexed.
1220
1221 @<Process a form...@>= {
1222 next_control=get_next();
1223 if (next_control==identifier) {
1224 lhs=id_lookup(id_first, id_loc,normal); lhs->ilk=normal;
1225 if (xref_switch) new_xref(lhs);
1226 next_control=get_next();
1227 if (next_control==identifier) {
1228 rhs=id_lookup(id_first, id_loc,normal);
1229 lhs->ilk=rhs->ilk;
1230 if (unindexed(lhs)) { /* retain only underlined entries */
1231 xref_pointer q,r=NULL;
1232 for (q=(xref_pointer)lhs->xref;q>xmem;q=q->xlink)
1233 if (q->num<def_flag)
1234 if (r) r->xlink=q->xlink;
1235 else lhs->xref=(void *)q->xlink;
1236 else r=q;
1237 }
1238 next_control=get_next();
1239 }
1240 }
1241 }
1242
1243 @ A much simpler processing of format definitions occurs when the
1244 definition is found in limbo.
1245
1246 @<Process simple format in limbo@>=
1247 if (get_next()!=identifier)
1248 err_print("! Missing left identifier of @@s");
1249 @.Missing left identifier...@>
1250 else {
1251 lhs=id_lookup(id_first,id_loc,normal);
1252 if (get_next()!=identifier)
1253 err_print("! Missing right identifier of @@s");
1254 @.Missing right identifier...@>
1255 else {
1256 rhs=id_lookup(id_first,id_loc,normal);
1257 lhs->ilk=rhs->ilk;
1258 }
1259 }
1260
1261 @ Finally, when the \TEX/ and definition parts have been treated, we have
1262 |next_control>=begin_C|.
1263
1264 @<Store cross-references in the \9{c}\CEE/...@>=
1265 if (next_control<=section_name) { /* |begin_C| or |section_name| */
1266 if (next_control==begin_C) section_xref_switch=0;
1267 else {
1268 section_xref_switch=def_flag;
1269 if(cur_section_char=='(' && cur_section!=name_dir)
1270 set_file_flag(cur_section);
1271 }
1272 do {
1273 if (next_control==section_name && cur_section!=name_dir)
1274 new_section_xref(cur_section);
1275 next_control=get_next(); outer_xref();
1276 } while ( next_control<=section_name);
1277 }
1278
1279 @ After phase one has looked at everything, we want to check that each
1280 section name was both defined and used. The variable |cur_xref| will point
1281 to cross-references for the current section name of interest.
1282
1283 @<Private...@>=
1284 static xref_pointer cur_xref; /* temporary cross-reference pointer */
1285 static bool an_output; /* did |file_flag| precede |cur_xref|? */
1286
1287 @ The following recursive procedure
1288 walks through the tree of section names and prints out anomalies.
1289 @^recursion@>
1290
1291 @c
1292 static void
1293 section_check(
1294 name_pointer p) /* print anomalies in subtree |p| */
1295 {
1296 if (p) {
1297 section_check(p->llink);
1298 cur_xref=(xref_pointer)p->xref;
1299 if ((an_output=(cur_xref->num==file_flag))==true) cur_xref=cur_xref->xlink;
1300 if (cur_xref->num <def_flag) {
1301 printf("%s","\n! Never defined: <");
1302 print_section_name(p); putchar('>'); mark_harmless();
1303 @.Never defined: <section name>@>
1304 }
1305 while (cur_xref->num >=cite_flag) cur_xref=cur_xref->xlink;
1306 if (cur_xref==xmem && !an_output) {
1307 printf("%s","\n! Never used: <");
1308 print_section_name(p); putchar('>'); mark_harmless();
1309 @.Never used: <section name>@>
1310 }
1311 section_check(p->rlink);
1312 }
1313 }
1314
1315 @ @<Predecl...@>=@+static void section_check(name_pointer);
1316
1317 @ @<Print error messages about un...@>=section_check(root);
1318
1319 @* Low-level output routines.
1320 The \TEX/ output is supposed to appear in lines at most |line_length|
1321 characters long, so we place it into an output buffer. During the output
1322 process, |out_line| will hold the current line number of the line about to
1323 be output.
1324
1325 @d line_length 80 /* lines of \TEX/ output have at most this many characters;
1326 should be less than 256 */
1327
1328 @<Private...@>=
1329 static char out_buf[line_length+1]; /* assembled characters */
1330 static char *out_buf_end = out_buf+line_length; /* end of |out_buf| */
1331 static char *out_ptr; /* last character in |out_buf| */
1332 static int out_line; /* number of next line to be output */
1333
1334 @ The |flush_buffer| routine empties the buffer up to a given breakpoint,
1335 and moves any remaining characters to the beginning of the next line.
1336 If the |per_cent| parameter is |true|, a |'%'| is appended to the line
1337 that is being output; in this case the breakpoint |b| should be strictly
1338 less than |out_buf_end|. If the |per_cent| parameter is |false|,
1339 trailing blanks are suppressed.
1340 The characters emptied from the buffer form a new line of output;
1341 if the |carryover| parameter is |true|, a |"%"| in that line will be
1342 carried over to the next line (so that \TEX/ will ignore the completion
1343 of commented-out text).
1344
1345 @d c_line_write(c) fflush(active_file),fwrite(out_buf+1,sizeof(char),c,active_file)
1346 @d tex_printf(c) fprintf(active_file,"%s",c)
1347 @d tex_putc(c) fputc(c,active_file)
1348 @d tex_puts(c) fputs(c,active_file)
1349
1350 @<Predecl...@>=
1351 static void flush_buffer(char *,bool,bool);@/
1352 static void finish_line(void);
1353
1354 @ @c
1355 static void
1356 flush_buffer(
1357 char *b, /* outputs from |out_buf+1| to |b|, where |b<=out_ptr| */
1358 bool per_cent,bool carryover)
1359 {
1360 char *j=b; /* pointer into |out_buf| */
1361 if (! per_cent) /* remove trailing blanks */
1362 while (j>out_buf && *j==' ') j--;
1363 c_line_write(j-out_buf);
1364 if (per_cent) tex_putc('%');
1365 tex_putc('\n'); out_line++;
1366 if (carryover)
1367 while (j>out_buf)
1368 if (*j--=='%' && (j==out_buf || *j!='\\')) {
1369 *b--='%'; break;
1370 }
1371 if (b<out_ptr) memcpy(out_buf+1,b+1,(size_t)(out_ptr-b));
1372 out_ptr-=b-out_buf;
1373 }
1374
1375 @ When we are copying \TEX/ source material, we retain line breaks
1376 that occur in the input, except that an empty line is not
1377 output when the \TEX/ source line was nonempty. For example, a line
1378 of the \TEX/ file that contains only an index cross-reference entry
1379 will not be copied. The |finish_line| routine is called just before
1380 |get_line| inputs a new line, and just after a line break token has
1381 been emitted during the output of translated \CEE/ text.
1382
1383 @c
1384 static void
1385 finish_line(void) /* do this at the end of a line */
1386 {
1387 char *k; /* pointer into |buffer| */
1388 if (out_ptr>out_buf) flush_buffer(out_ptr,false,false);
1389 else {
1390 for (k=buffer; k<=limit; k++)
1391 if (!(xisspace(*k))) return;
1392 flush_buffer(out_buf,false,false);
1393 }
1394 }
1395
1396 @ In particular, the |finish_line| procedure is called near the very
1397 beginning of phase two. We initialize the output variables in a slightly
1398 tricky way so that the first line of the output file will be
1399 `\.{\\input cwebmac}'.
1400
1401 @<Start \9{t}\TEX/...@>=
1402 out_ptr=out_buf+1; out_line=1; active_file=tex_file;
1403 tex_printf("\\input cwebma"); *out_ptr='c';
1404
1405 @ When we wish to append one character |c| to the output buffer, we write
1406 `|out(c)|'; this will cause the buffer to be emptied if it was already
1407 full. If we want to append more than one character at once, we say
1408 |out_str(s)|, where |s| is a string containing the characters.
1409
1410 A line break will occur at a space or after a single-nonletter
1411 \TEX/ control sequence.
1412
1413 @d out(c) {if (out_ptr>=out_buf_end) break_out(); *(++out_ptr)=c;}
1414
1415 @<Predecl...@>=
1416 static void out_str(const char *);@/
1417 static void break_out(void);
1418
1419 @ @c
1420 static void
1421 out_str( /* output characters from |s| to end of string */
1422 const char*s)
1423 {
1424 while (*s) out(*s++);
1425 }
1426
1427 @ The |break_out| routine is called just before the output buffer is about
1428 to overflow. To make this routine a little faster, we initialize position
1429 0 of the output buffer to `\.\\'; this character isn't really output.
1430
1431 @<Set init...@>=
1432 out_buf[0]='\\';
1433
1434 @ A long line is broken at a blank space or just before a backslash that isn't
1435 preceded by another backslash or a \TeX\ comment marker. In the latter case, a
1436 |'%'| is output at the break.
1437
1438 @c
1439 static void
1440 break_out(void) /* finds a way to break the output line */
1441 {
1442 char *k=out_ptr; /* pointer into |out_buf| */
1443 while (true) {
1444 if (k==out_buf) @<Print warning message, break the line, |return|@>@;
1445 if (*k==' ') {
1446 flush_buffer(k,false,true); return;
1447 }
1448 if (*(k--)=='\\' && *k!='\\' && *k!='%') { /* we've decreased |k| */
1449 flush_buffer(k,true,true); return;
1450 }
1451 }
1452 }
1453
1454 @ We get to this section only in the unusual case that the entire output line
1455 consists of a string of backslashes followed by a string of nonblank
1456 non-backslashes. In such cases it is almost always safe to break the
1457 line by putting a |'%'| just before the last character.
1458
1459 @<Print warning message...@>=
1460 {
1461 printf("\n! Line had to be broken (output l. %d):\n",out_line);
1462 @.Line had to be broken@>
1463 term_write(out_buf+1, out_ptr-out_buf-1);
1464 new_line(); mark_harmless();
1465 flush_buffer(out_ptr-1,true,true); return;
1466 }
1467
1468 @ Here is a macro that outputs a section number in decimal notation.
1469 The number to be converted by |out_section| is known to be less than
1470 |def_flag|, so it cannot have more than five decimal digits. If
1471 the section is changed, we output `\.{\\*}' just after the number.
1472
1473 @<Predecl...@>=
1474 static void out_section(sixteen_bits);@/
1475 static void out_name(name_pointer,bool);
1476
1477 @ @c
1478 static void
1479 out_section(
1480 sixteen_bits n)
1481 {
1482 char s[6];
1483 snprintf(s,6,"%d",(int)n); out_str(s);
1484 if (changed_section[n]) out_str("\\*");
1485 @.\\*@>
1486 }
1487
1488 @ The |out_name| procedure is used to output an identifier or index
1489 entry, enclosing it in braces.
1490
1491 @c
1492 static void
1493 out_name(
1494 name_pointer p,
1495 bool quote_xalpha)
1496 {
1497 char *k; /* pointer into |byte_mem| */
1498 out('{');
1499 for (k=p->byte_start; k<(p+1)->byte_start; k++) {
1500 if (isxalpha(*k) && quote_xalpha) out('\\');
1501 @.\\\$@>
1502 @.\\\_@>
1503 out(*k);
1504 }
1505 out('}');
1506 }
1507
1508 @* Routines that copy \TEX/ material.
1509 During phase two, we use the subroutines |copy_limbo| and |copy_TeX| (and
1510 |copy_comment|) in place of the analogous |skip_limbo| and |skip_TeX|
1511 that were used in phase one.
1512
1513 The |copy_limbo| routine, for example, takes \TEX/ material that is not
1514 part of any section and transcribes it almost verbatim to the output file.
1515 The use of `\.{@@}' signs is severely restricted in such material:
1516 `\.{@@@@}' pairs are replaced by singletons; `\.{@@l}' and `\.{@@q}' and
1517 `\.{@@s}' are interpreted.
1518
1519 @<Predecl...@>=
1520 static void copy_limbo(void);@/
1521 static eight_bits copy_TeX(void);@/
1522 static int copy_comment(bool,int);
1523
1524 @ @c
1525 static void
1526 copy_limbo(void)
1527 {
1528 while (true) {
1529 if (loc>limit && (finish_line(), get_line()==false)) return;
1530 *(limit+1)='@@';
1531 while (*loc!='@@') out(*(loc++));
1532 if (loc++<=limit) {
1533 switch (ccode[(eight_bits)*loc++]) {
1534 case new_section: return;
1535 case translit_code: out_str("\\ATL"); break;
1536 @.\\ATL@>
1537 case '@@': out('@@'); break;
1538 case noop: skip_restricted(); break;
1539 case format_code: if (get_next()==identifier) get_next();
1540 if (loc>=limit) get_line(); /* avoid blank lines in output */
1541 break; /* the operands of \.{@@s} are ignored on this pass */
1542 default: err_print("! Double @@ should be used in limbo");
1543 @.Double @@ should be used...@>
1544 out('@@');
1545 }
1546 }
1547 }
1548 }
1549
1550 @ The |copy_TeX| routine processes the \TEX/ code at the beginning of a
1551 section; for example, the words you are now reading were copied in this
1552 way. It returns the next control code or `\.{\v}' found in the input.
1553 We don't copy spaces or tab marks into the beginning of a line. This
1554 makes the test for empty lines in |finish_line| work.
1555
1556 @f copy_TeX TeX
1557 @c
1558 static eight_bits
1559 copy_TeX(void)
1560 {
1561 char c; /* current character being copied */
1562 while (true) {
1563 if (loc>limit && (finish_line(), get_line()==false)) return new_section;
1564 *(limit+1)='@@';
1565 while ((c=*(loc++))!='|' && c!='@@') {
1566 out(c);
1567 if (out_ptr==out_buf+1 && (xisspace(c))) out_ptr--;
1568 }
1569 if (c=='|') return '|';
1570 if (loc<=limit) return ccode[(eight_bits)*(loc++)];
1571 }
1572 }
1573
1574 @ The |copy_comment| function issues a warning if more braces are opened than
1575 closed, and in the case of a more serious error it supplies enough
1576 braces to keep \TEX/ from complaining about unbalanced braces.
1577 Instead of copying the \TEX/ material
1578 into the output buffer, this function copies it into the token memory
1579 (in phase two only).
1580 The abbreviation |app_tok(t)| is used to append token |t| to the current
1581 token list, and it also makes sure that it is possible to append at least
1582 one further token without overflow.
1583
1584 @d app_tok(c) {if (tok_ptr+2>tok_mem_end) overflow("token"); *(tok_ptr++)=c;}
1585
1586 @c
1587 static int copy_comment( /* copies \TeX\ code in comments */
1588 bool is_long_comment, /* is this a traditional \CEE/ comment? */
1589 int bal) /* brace balance */
1590 {
1591 char c; /* current character being copied */
1592 while (true) {
1593 if (loc>limit) {
1594 if (is_long_comment) {
1595 if (get_line()==false) {
1596 err_print("! Input ended in mid-comment");
1597 @.Input ended in mid-comment@>
1598 loc=buffer+1; goto done;
1599 }
1600 } else {
1601 if (bal>1) err_print("! Missing } in comment");
1602 @.Missing \} in comment@>
1603 goto done;
1604 }
1605 }
1606 c=*(loc++);
1607 if (c=='|') return bal;
1608 if (is_long_comment) @<Check for end of comment@>@;
1609 if (phase==2) {
1610 if (ishigh(c)) app_tok(quoted_char);
1611 app_tok(c);
1612 }
1613 @<Copy special things when |c=='@@', '\\'|@>@;
1614 if (c=='{') bal++;
1615 else if (c=='}') {
1616 if(bal>1) bal--;
1617 else {err_print("! Extra } in comment");
1618 @.Extra \} in comment@>
1619 if (phase==2) tok_ptr--;
1620 }
1621 }
1622 }
1623 done: @<Clear |bal| and |return|@>@;
1624 }
1625
1626 @ @<Check for end of comment@>=
1627 if (c=='*' && *loc=='/') {
1628 loc++;
1629 if (bal>1) err_print("! Missing } in comment");
1630 @.Missing \} in comment@>
1631 goto done;
1632 }
1633
1634 @ @<Copy special things when |c=='@@'...@>=
1635 if (c=='@@') {
1636 if (*(loc++)!='@@') {
1637 err_print("! Illegal use of @@ in comment");
1638 @.Illegal use of @@...@>
1639 loc-=2; if (phase==2) *(tok_ptr-1)=' '; goto done;
1640 }
1641 } else {
1642 if (c=='\\' && *loc!='@@') {
1643 if (phase==2) app_tok(*(loc++))@t;@>@; else loc++;
1644 }
1645 }
1646
1647 @ We output
1648 enough right braces to keep \TEX/ happy.
1649
1650 @<Clear |bal|...@>=
1651 if (phase==2) while (bal-- >0) app_tok('}');
1652 return 0;
1653
1654 @** Parsing.
1655 The most intricate part of \.{CWEAVE} is its mechanism for converting
1656 \CEE/-like code into \TEX/ code, and we might as well plunge into this
1657 aspect of the program now. A ``bottom up'' approach is used to parse the
1658 \CEE/-like material, since \.{CWEAVE} must deal with fragmentary
1659 constructions whose overall ``part of speech'' is not known.
1660
1661 At the lowest level, the input is represented as a sequence of entities
1662 that we shall call {\it scraps}, where each scrap of information consists
1663 of two parts, its {\it category} and its {\it translation}. The category
1664 is essentially a syntactic class, and the translation is a token list that
1665 represents \TEX/ code. Rules of syntax and semantics tell us how to
1666 combine adjacent scraps into larger ones, and if we are lucky an entire
1667 \CEE/ text that starts out as hundreds of small scraps will join
1668 together into one gigantic scrap whose translation is the desired \TEX/
1669 code. If we are unlucky, we will be left with several scraps that don't
1670 combine; their translations will simply be output, one by one.
1671
1672 The combination rules are given as context-sensitive productions that are
1673 applied from left to right. Suppose that we are currently working on the
1674 sequence of scraps $s_1\,s_2\ldots s_n$. We try first to find the longest
1675 production that applies to an initial substring $s_1\,s_2\ldots\,$; but if
1676 no such productions exist, we try to find the longest production
1677 applicable to the next substring $s_2\,s_3\ldots\,$; and if that fails, we
1678 try to match $s_3\,s_4\ldots\,$, etc.
1679
1680 A production applies if the category codes have a given pattern. For
1681 example, one of the productions (see rule~3) is
1682 $$\hbox{|exp| }\left\{\matrix{\hbox{|binop|}\cr\hbox{|ubinop|}}\right\}
1683 \hbox{ |exp| }\RA\hbox{ |exp|}$$
1684 and it means that three consecutive scraps whose respective categories are
1685 |exp|, |binop| (or |ubinop|),
1686 and |exp| are converted to one scrap whose category
1687 is |exp|. The translations of the original
1688 scraps are simply concatenated. The case of
1689 $$\hbox{|exp| |comma| |exp| $\RA$ |exp|} \hskip4emE_1C\,\\{opt}9\,E_2$$
1690 (rule 4) is only slightly more complicated:
1691 Here the resulting |exp| translation
1692 consists not only of the three original translations, but also of the
1693 tokens |opt| and 9 between the translations of the
1694 |comma| and the following |exp|.
1695 In the \TEX/ file, this will specify an optional line break after the
1696 comma, with penalty 90.
1697
1698 At each opportunity the longest possible production is applied. For
1699 example, if the current sequence of scraps is |if_clause| |stmt|
1700 |else_like| |if_like|, rule 63 is applied; but if the sequence is
1701 |if_clause| |stmt| |else_like| followed by anything other than
1702 |if_like|, rule 64 takes effect; and if the sequence is |if_clause|
1703 |stmt| followed by anything other than |else_like|, rule 65 takes
1704 effect.
1705
1706 Translation rules such as `$E_1C\,\\{opt}9\,E_2$' above use subscripts
1707 to distinguish between translations of scraps whose categories have the
1708 same initial letter; these subscripts are assigned from left to right.
1709
1710 @ Here is a list of the category codes that scraps can have.
1711 (A few others, like |int_like|, have already been defined; the
1712 |cat_name| array contains a complete list.)
1713
1714 @d exp 1 /* denotes an expression, including perhaps a single identifier */
1715 @d unop 2 /* denotes a unary operator */
1716 @d binop 3 /* denotes a binary operator */
1717 @d ubinop 4
1718 /* denotes an operator that can be unary or binary, depending on context */
1719 @d cast 5 /* denotes a cast */
1720 @d question 6 /* denotes a question mark and possibly the expressions flanking it */
1721 @d lbrace 7 /* denotes a left brace */
1722 @d rbrace 8 /* denotes a right brace */
1723 @d decl_head 9 /* denotes an incomplete declaration */
1724 @d comma 10 /* denotes a comma */
1725 @d lpar 11 /* denotes a left parenthesis */
1726 @d rpar 12 /* denotes a right parenthesis */
1727 @d prelangle 13 /* denotes `$<$' before we know what it is */
1728 @d prerangle 14 /* denotes `$>$' before we know what it is */
1729 @d langle 15 /* denotes `$<$' when it's used as angle bracket in a template */
1730 @d colcol 18 /* denotes `::' */
1731 @d base 19 /* denotes a colon that introduces a base specifier */
1732 @d decl 20 /* denotes a complete declaration */
1733 @d struct_head 21 /* denotes the beginning of a structure specifier */
1734 @d stmt 23 /* denotes a complete statement */
1735 @d function 24 /* denotes a complete function */
1736 @d fn_decl 25 /* denotes a function declarator */
1737 @d semi 27 /* denotes a semicolon */
1738 @d colon 28 /* denotes a colon */
1739 @d tag 29 /* denotes a statement label */
1740 @d if_head 30 /* denotes the beginning of a compound conditional */
1741 @d else_head 31 /* denotes a prefix for a compound statement */
1742 @d if_clause 32 /* pending \.{if} together with a condition */
1743 @d lproc 35 /* begins a preprocessor command */
1744 @d rproc 36 /* ends a preprocessor command */
1745 @d insert 37 /* a scrap that gets combined with its neighbor */
1746 @d section_scrap 38 /* section name */
1747 @d dead 39 /* scrap that won't combine */
1748 @d ftemplate 63 /* \\{make\_pair} */
1749 @d new_exp 64 /* \&{new} and a following type identifier */
1750 @d begin_arg 65 /* \.{@@[} */
1751 @d end_arg 66 /* \.{@@]} */
1752 @d lbrack 67 /* denotes a left bracket */
1753 @d rbrack 68 /* denotes a right bracket */
1754 @d attr_head 69 /* denotes beginning of attribute */
1755
1756 @<Private...@>=
1757 static char cat_name[256][12]; /* |12==strlen("struct_head")+1| */
1758
1759 @ @<Set in...@>=
1760 {int c; for (c=0;c<256;c++) strcpy(cat_name[c],"UNKNOWN");}
1761 @.UNKNOWN@>
1762 strcpy(cat_name[exp],"exp");
1763 strcpy(cat_name[unop],"unop");
1764 strcpy(cat_name[binop],"binop");
1765 strcpy(cat_name[ubinop],"ubinop");
1766 strcpy(cat_name[cast],"cast");
1767 strcpy(cat_name[question],"?");
1768 strcpy(cat_name[lbrace],"{"@q}@>);
1769 strcpy(cat_name[rbrace],@q{@>"}");
1770 strcpy(cat_name[decl_head],"decl_head");
1771 strcpy(cat_name[comma],",");
1772 strcpy(cat_name[lpar],"(");
1773 strcpy(cat_name[rpar],")");
1774 strcpy(cat_name[prelangle],"<");
1775 strcpy(cat_name[prerangle],">");
1776 strcpy(cat_name[langle],"\\<");
1777 strcpy(cat_name[colcol],"::");
1778 strcpy(cat_name[base],"\\:");
1779 strcpy(cat_name[decl],"decl");
1780 strcpy(cat_name[struct_head],"struct_head");
1781 strcpy(cat_name[alfop],"alfop");
1782 strcpy(cat_name[stmt],"stmt");
1783 strcpy(cat_name[function],"function");
1784 strcpy(cat_name[fn_decl],"fn_decl");
1785 strcpy(cat_name[else_like],"else_like");
1786 strcpy(cat_name[semi],";");
1787 strcpy(cat_name[colon],":");
1788 strcpy(cat_name[tag],"tag");
1789 strcpy(cat_name[if_head],"if_head");
1790 strcpy(cat_name[else_head],"else_head");
1791 strcpy(cat_name[if_clause],"if()");
1792 strcpy(cat_name[lproc],"#{"@q}@>);
1793 strcpy(cat_name[rproc],@q{@>"#}");
1794 strcpy(cat_name[insert],"insert");
1795 strcpy(cat_name[section_scrap],"section");
1796 strcpy(cat_name[dead],"@@d");
1797 strcpy(cat_name[public_like],"public");
1798 strcpy(cat_name[operator_like],"operator");
1799 strcpy(cat_name[new_like],"new");
1800 strcpy(cat_name[catch_like],"catch");
1801 strcpy(cat_name[for_like],"for");
1802 strcpy(cat_name[do_like],"do");
1803 strcpy(cat_name[if_like],"if");
1804 strcpy(cat_name[delete_like],"delete");
1805 strcpy(cat_name[raw_ubin],"ubinop?");
1806 strcpy(cat_name[const_like],"const");
1807 strcpy(cat_name[raw_int],"raw");
1808 strcpy(cat_name[int_like],"int");
1809 strcpy(cat_name[case_like],"case");
1810 strcpy(cat_name[sizeof_like],"sizeof");
1811 strcpy(cat_name[struct_like],"struct");
1812 strcpy(cat_name[typedef_like],"typedef");
1813 strcpy(cat_name[define_like],"define");
1814 strcpy(cat_name[template_like],"template");
1815 strcpy(cat_name[alignas_like],"alignas");
1816 strcpy(cat_name[using_like],"using");
1817 strcpy(cat_name[default_like],"default");
1818 strcpy(cat_name[attr],"attr");
1819 strcpy(cat_name[ftemplate],"ftemplate");
1820 strcpy(cat_name[new_exp],"new_exp");
1821 strcpy(cat_name[begin_arg],"@@["@q]@>);
1822 strcpy(cat_name[end_arg],@q[@>"@@]");
1823 strcpy(cat_name[lbrack],"[");
1824 strcpy(cat_name[rbrack],"]");
1825 strcpy(cat_name[attr_head],"attr_head");
1826 strcpy(cat_name[0],"zero");
1827
1828 @ This code allows \.{CWEAVE} to display its parsing steps.
1829
1830 @d print_cat(c) fputs(cat_name[c],stdout) /* symbolic printout of a category */
1831
1832 @ The token lists for translated \TEX/ output contain some special control
1833 symbols as well as ordinary characters. These control symbols are
1834 interpreted by \.{CWEAVE} before they are written to the output file.
1835
1836 \yskip\hang |break_space| denotes an optional line break or an en space;
1837
1838 \yskip\hang |force| denotes a line break;
1839
1840 \yskip\hang |big_force| denotes a line break with additional vertical space;
1841
1842 \yskip\hang |preproc_line| denotes that the line will be printed flush left;
1843
1844 \yskip\hang |opt| denotes an optional line break (with the continuation
1845 line indented two ems with respect to the normal starting position)---this
1846 code is followed by an integer |n|, and the break will occur with penalty
1847 $10n$;
1848
1849 \yskip\hang |backup| denotes a backspace of one em;
1850
1851 \yskip\hang |cancel| obliterates any |break_space|, |opt|, |force|, or
1852 |big_force| tokens that immediately precede or follow it and also cancels any
1853 |backup| tokens that follow it;
1854
1855 \yskip\hang |indent| causes future lines to be indented one more em;
1856
1857 \yskip\hang |outdent| causes future lines to be indented one less em;
1858
1859 \yskip\hang |dindent| causes future lines to be indented two more ems.
1860
1861 \yskip\noindent All of these tokens are removed from the \TEX/ output that
1862 comes from \CEE/ text between \pb\ signs; |break_space| and |force| and
1863 |big_force| become single spaces in this mode. The translation of other
1864 \CEE/ texts results in \TEX/ control sequences \.{\\1}, \.{\\2},
1865 \.{\\3}, \.{\\4}, \.{\\5}, \.{\\6}, \.{\\7}, \.{\\8}
1866 corresponding respectively to
1867 |indent|, |outdent|, |opt|, |backup|, |break_space|, |force|,
1868 |big_force| and |preproc_line|.
1869 However, a sequence of consecutive `\.\ ', |break_space|,
1870 |force|, and/or |big_force| tokens is first replaced by a single token
1871 (the maximum of the given ones).
1872
1873 A |dindent| token becomes \.{\\1\\1}. It is equivalent to a pair of |indent|
1874 tokens. However, if |dindent| immediately precedes |big_force|, the two tokens
1875 are swapped, so that the indentation happens after the line break.
1876
1877 The token |math_rel| will be translated into
1878 \.{\\MRL\{}, and it will get a matching \.\} later.
1879 Other control sequences in the \TEX/ output will be
1880 `\.{\\\\\{}$\,\ldots\,$\.\}'
1881 surrounding identifiers, `\.{\\\&\{}$\,\ldots\,$\.\}' surrounding
1882 reserved words, `\.{\\.\{}$\,\ldots\,$\.\}' surrounding strings,
1883 `\.{\\C\{}$\,\ldots\,$\.\}$\,$|force|' surrounding comments, and
1884 `\.{\\X$n$:}$\,\ldots\,$\.{\\X}' surrounding section names, where
1885 |n| is the section number.
1886
1887 @d math_rel 0206
1888 @d big_cancel 0210 /* like |cancel|, also overrides spaces */
1889 @d cancel 0211 /* overrides |backup|, |break_space|, |force|, |big_force| */
1890 @d indent 0212 /* one more tab (\.{\\1}) */
1891 @d outdent 0213 /* one less tab (\.{\\2}) */
1892 @d opt 0214 /* optional break in mid-statement (\.{\\3}) */
1893 @d backup 0215 /* stick out one unit to the left (\.{\\4}) */
1894 @d break_space 0216 /* optional break between statements (\.{\\5}) */
1895 @d force 0217 /* forced break between statements (\.{\\6}) */
1896 @d big_force 0220 /* forced break with additional space (\.{\\7}) */
1897 @d preproc_line 0221 /* begin line without indentation (\.{\\8}) */
1898 @^high-bit character handling@>
1899 @d quoted_char 0222
1900 /* introduces a character token in the range |0200|--|0377| */
1901 @d end_translation 0223 /* special sentinel token at end of list */
1902 @d inserted 0224 /* sentinel to mark translations of inserts */
1903 @d qualifier 0225 /* introduces an explicit namespace qualifier */
1904 @d dindent 0226 /* two more tabs (\.{\\1\\1}) */
1905
1906 @ The raw input is converted into scraps according to the following table,
1907 which gives category codes followed by the translations.
1908 \def\stars {\.{**}}%
1909 The symbol `\stars' stands for `\.{\\\&\{{\rm identifier}\}}',
1910 i.e., the identifier itself treated as a reserved word.
1911 The right-hand column is the so-called |mathness|, which is explained
1912 further below.
1913
1914 An identifier |c| of length 1 is translated as \.{\\\v c} instead of
1915 as \.{\\\\\{c\}}. An identifier \.{CAPS} in all caps is translated as
1916 \.{\\.\{CAPS\}} instead of as \.{\\\\\{CAPS\}}. An identifier that has
1917 become a reserved word via |typedef| is translated with \.{\\\&} replacing
1918 \.{\\\\} and |raw_int| replacing |exp|.
1919
1920 A string of length greater than 20 is broken into pieces of size at most~20
1921 with discretionary breaks in between.
1922
1923 \yskip\halign{\quad#\hfil&\quad#\hfil&\quad\hfil#\hfil\cr
1924 \.{!=}&|binop|: \.{\\I}&yes\cr
1925 \.{<=}&|binop|: \.{\\Z}&yes\cr
1926 \.{>=}&|binop|: \.{\\G}&yes\cr
1927 \.{==}&|binop|: \.{\\E}&yes\cr
1928 \.{\&\&}&|binop|: \.{\\W}&yes\cr
1929 \.{\v\v}&|binop|: \.{\\V}&yes\cr
1930 \.{++}&|unop|: \.{\\PP}&yes\cr
1931 \.{--}&|unop|: \.{\\MM}&yes\cr
1932 \.{->}&|binop|: \.{\\MG}&yes\cr
1933 \.{>>}&|binop|: \.{\\GG}&yes\cr
1934 \.{<<}&|binop|: \.{\\LL}&yes\cr
1935 \.{::}&|colcol|: \.{\\DC}&maybe\cr
1936 \.{.*}&|binop|: \.{\\PA}&yes\cr
1937 \.{->*}&|binop|: \.{\\MGA}&yes\cr
1938 \.{...}&|raw_int|: \.{\\,\\ldots\\,}&yes\cr
1939 \."string\."&|exp|: \.{\\.\{}string with special characters quoted\.\}&maybe\cr
1940 \.{@@=}string\.{@@>}&|exp|: \.{\\vb\{}string with special characters
1941 quoted\.\}&maybe\cr
1942 \.{@@'7'}&|exp|: \.{\\.\{@@'7'\}}&maybe\cr
1943 \.{077} or \.{\\77}&|exp|: \.{\\T\{\\\~77\}}&maybe\cr
1944 \.{0x7f}&|exp|: \.{\\T\{\\\^7f\}}&maybe\cr
1945 \.{0b10111}&|exp|: \.{\\T\{\\\\10111\}}&maybe\cr
1946 \.{77}&|exp|: \.{\\T\{77\}}&maybe\cr
1947 \.{77L}&|exp|: \.{\\T\{77\\\$L\}}&maybe\cr
1948 \.{0.1E5}&|exp|: \.{\\T\{0.1\\\_5\}}&maybe\cr
1949 \.{0x10p3}&|exp|: \.{\\T\{\\\^10\}\\p\{3\}}&maybe\cr
1950 \.{1'000'000}&|exp|: \.{\\T\{1\\\ 000\\\ 000\}}&maybe\cr
1951 \.+&|ubinop|: \.+&yes\cr
1952 \.-&|ubinop|: \.-&yes\cr
1953 \.*&|raw_ubin|: \.*&yes\cr
1954 \./&|binop|: \./&yes\cr
1955 \.<&|prelangle|: \.{\\langle}&yes\cr
1956 \.=&|binop|: \.{\\K}&yes\cr
1957 \.>&|prerangle|: \.{\\rangle}&yes\cr
1958 \..&|binop|: \..&yes\cr
1959 \.{\v}&|binop|: \.{\\OR}&yes\cr
1960 \.\^&|binop|: \.{\\XOR}&yes\cr
1961 \.\%&|binop|: \.{\\MOD}&yes\cr
1962 \.?&|question|: \.{\\?}&yes\cr
1963 \.!&|unop|: \.{\\R}&yes\cr
1964 \.\~&|unop|: \.{\\CM}&yes\cr
1965 \.\&&|raw_ubin|: \.{\\AND}&yes\cr
1966 \.(&|lpar|: \.(&maybe\cr
1967 \.)&|rpar|: \.)&maybe\cr
1968 \.[&|lbrack|: \.[&maybe\cr
1969 \.]&|rbrack|: \.]&maybe\cr
1970 \.\{&|lbrace|: \.\{&yes\cr
1971 \.\}&|lbrace|: \.\}&yes\cr
1972 \.,&|comma|: \.,&yes\cr
1973 \.;&|semi|: \.;&maybe\cr
1974 \.:&|colon|: \.:&no\cr
1975 \.\# (within line)&|ubinop|: \.{\\\#}&yes\cr
1976 \.\# (at beginning)&|lproc|: |force| |preproc_line| \.{\\\#}&no\cr
1977 end of \.\# line&|rproc|: |force|&no\cr
1978 identifier&|exp|: \.{\\\\\{}identifier with underlines and
1979 dollar signs quoted\.\}&maybe\cr
1980 \.{alignas}&|alignas_like|: \stars&maybe\cr
1981 \.{alignof}&|sizeof_like|: \stars&maybe\cr
1982 \.{and}&|alfop|: \stars&yes\cr
1983 \.{and\_eq}&|alfop|: \stars&yes\cr
1984 \.{asm}&|sizeof_like|: \stars&maybe\cr
1985 \.{auto}&|int_like|: \stars&maybe\cr
1986 \.{bitand}&|alfop|: \stars&yes\cr
1987 \.{bitor}&|alfop|: \stars&yes\cr
1988 \.{bool}&|raw_int|: \stars&maybe\cr
1989 \.{break}&|case_like|: \stars&maybe\cr
1990 \.{case}&|case_like|: \stars&maybe\cr
1991 \.{catch}&|catch_like|: \stars&maybe\cr
1992 \.{char}&|raw_int|: \stars&maybe\cr
1993 \.{char8\_t}&|raw_int|: \stars&maybe\cr
1994 \.{char16\_t}&|raw_int|: \stars&maybe\cr
1995 \.{char32\_t}&|raw_int|: \stars&maybe\cr
1996 \.{class}&|struct_like|: \stars&maybe\cr
1997 \.{clock\_t}&|raw_int|: \stars&maybe\cr
1998 \.{compl}&|alfop|: \stars&yes\cr
1999 \.{complex}&|int_like|: \stars&yes\cr
2000 \.{concept}&|int_like|: \stars&maybe\cr
2001 \.{const}&|const_like|: \stars&maybe\cr
2002 \.{consteval}&|const_like|: \stars&maybe\cr
2003 \.{constexpr}&|const_like|: \stars&maybe\cr
2004 \.{constinit}&|const_like|: \stars&maybe\cr
2005 \.{const\_cast}&|raw_int|: \stars&maybe\cr
2006 \.{continue}&|case_like|: \stars&maybe\cr
2007 \.{co\_await}&|case_like|: \stars&maybe\cr
2008 \.{co\_return}&|case_like|: \stars&maybe\cr
2009 \.{co\_yield}&|case_like|: \stars&maybe\cr
2010 \.{decltype}&|sizeof_like|: \stars&maybe\cr
2011 \.{default}&|default_like|: \stars&maybe\cr
2012 \.{define}&|define_like|: \stars&maybe\cr
2013 \.{defined}&|sizeof_like|: \stars&maybe\cr
2014 \.{delete}&|delete_like|: \stars&maybe\cr
2015 \.{div\_t}&|raw_int|: \stars&maybe\cr
2016 \.{do}&|do_like|: \stars&maybe\cr
2017 \.{double}&|raw_int|: \stars&maybe\cr
2018 \.{dynamic\_cast}&|raw_int|: \stars&maybe\cr
2019 \.{elif}&|if_like|: \stars&maybe\cr
2020 \.{else}&|else_like|: \stars&maybe\cr
2021 \.{endif}&|if_like|: \stars&maybe\cr
2022 \.{enum}&|struct_like|: \stars&maybe\cr
2023 \.{error}&|if_like|: \stars&maybe\cr
2024 \.{explicit}&|int_like|: \stars&maybe\cr
2025 \.{export}&|int_like|: \stars&maybe\cr
2026 \.{extern}&|int_like|: \stars&maybe\cr
2027 \.{FILE}&|raw_int|: \stars&maybe\cr
2028 \.{false}&|normal|: \stars&maybe\cr
2029 \.{float}&|raw_int|: \stars&maybe\cr
2030 \.{for}&|for_like|: \stars&maybe\cr
2031 \.{fpos\_t}&|raw_int|: \stars&maybe\cr
2032 \.{friend}&|int_like|: \stars&maybe\cr
2033 \.{goto}&|case_like|: \stars&maybe\cr
2034 \.{if}&|if_like|: \stars&maybe\cr
2035 \.{ifdef}&|if_like|: \stars&maybe\cr
2036 \.{ifndef}&|if_like|: \stars&maybe\cr
2037 \.{imaginary}&|int_like|: \stars&maybe\cr
2038 \.{include}&|if_like|: \stars&maybe\cr
2039 \.{inline}&|int_like|: \stars&maybe\cr
2040 \.{int}&|raw_int|: \stars&maybe\cr
2041 \.{jmp\_buf}&|raw_int|: \stars&maybe\cr
2042 \.{ldiv\_t}&|raw_int|: \stars&maybe\cr
2043 \.{line}&|if_like|: \stars&maybe\cr
2044 \.{long}&|raw_int|: \stars&maybe\cr
2045 \.{make\_pair}&|ftemplate|: \.{\\\\\{make\\\_pair\}}&maybe\cr
2046 \.{mutable}&|int_like|: \stars&maybe\cr
2047 \.{namespace}&|struct_like|: \stars&maybe\cr
2048 \.{new}&|new_like|: \stars&maybe\cr
2049 \.{noexcept}&|attr|: \stars&maybe\cr
2050 \.{not}&|alfop|: \stars&yes\cr
2051 \.{not\_eq}&|alfop|: \stars&yes\cr
2052 \.{NULL}&|exp|: \.{\\NULL}&yes\cr
2053 \.{nullptr}&|exp|: \.{\\NULL}&yes\cr
2054 \.{offsetof}&|raw_int|: \stars&maybe\cr
2055 \.{operator}&|operator_like|: \stars&maybe\cr
2056 \.{or}&|alfop|: \stars&yes\cr
2057 \.{or\_eq}&|alfop|: \stars&yes\cr
2058 \.{pragma}&|if_like|: \stars&maybe\cr
2059 \.{private}&|public_like|: \stars&maybe\cr
2060 \.{protected}&|public_like|: \stars&maybe\cr
2061 \.{ptrdiff\_t}&|raw_int|: \stars&maybe\cr
2062 \.{public}&|public_like|: \stars&maybe\cr
2063 \.{register}&|int_like|: \stars&maybe\cr
2064 \.{reinterpret\_cast}&|raw_int|: \stars&maybe\cr
2065 \.{requires}&|int_like|: \stars&maybe\cr
2066 \.{restrict}&|int_like|: \stars&maybe\cr
2067 \.{return}&|case_like|: \stars&maybe\cr
2068 \.{short}&|raw_int|: \stars&maybe\cr
2069 \.{sig\_atomic\_t}&|raw_int|: \stars&maybe\cr
2070 \.{signed}&|raw_int|: \stars&maybe\cr
2071 \.{size\_t}&|raw_int|: \stars&maybe\cr
2072 \.{sizeof}&|sizeof_like|: \stars&maybe\cr
2073 \.{static}&|int_like|: \stars&maybe\cr
2074 \.{static\_assert}&|sizeof_like|: \stars&maybe\cr
2075 \.{static\_cast}&|raw_int|: \stars&maybe\cr
2076 \.{struct}&|struct_like|: \stars&maybe\cr
2077 \.{switch}&|for_like|: \stars&maybe\cr
2078 \.{template}&|template_like|: \stars&maybe\cr
2079 \.{TeX}&|exp|: \.{\\TeX}&yes\cr
2080 \.{this}&|exp|: \.{\\this}&yes\cr
2081 \.{thread\_local}&|raw_int|: \stars&maybe\cr
2082 \.{throw}&|case_like|: \stars&maybe\cr
2083 \.{time\_t}&|raw_int|: \stars&maybe\cr
2084 \.{try}&|else_like|: \stars&maybe\cr
2085 \.{typedef}&|typedef_like|: \stars&maybe\cr
2086 \.{typeid}&|sizeof_like|: \stars&maybe\cr
2087 \.{typename}&|struct_like|: \stars&maybe\cr
2088 \.{undef}&|if_like|: \stars&maybe\cr
2089 \.{union}&|struct_like|: \stars&maybe\cr
2090 \.{unsigned}&|raw_int|: \stars&maybe\cr
2091 \.{using}&|using_like|: \stars&maybe\cr
2092 \.{va\_dcl}&|decl|: \stars&maybe\cr
2093 \.{va\_list}&|raw_int|: \stars&maybe\cr
2094 \.{virtual}&|int_like|: \stars&maybe\cr
2095 \.{void}&|raw_int|: \stars&maybe\cr
2096 \.{volatile}&|const_like|: \stars&maybe\cr
2097 \.{wchar\_t}&|raw_int|: \stars&maybe\cr
2098 \.{while}&|for_like|: \stars&maybe\cr
2099 \.{xor}&|alfop|: \stars&yes\cr
2100 \.{xor\_eq}&|alfop|: \stars&yes\cr
2101 \.{@@,}&|insert|: \.{\\,}&maybe\cr
2102 \.{@@\v}&|insert|: |opt| \.0&maybe\cr
2103 \.{@@/}&|insert|: |force|&no\cr
2104 \.{@@\#}&|insert|: |big_force|&no\cr
2105 \.{@@+}&|insert|: |big_cancel| \.{\{\}} |break_space|
2106 \.{\{\}} |big_cancel|&no\cr
2107 \.{@@;}&|semi|: &maybe\cr
2108 \.{@@[@q]@>}&|begin_arg|: &maybe\cr
2109 \.{@q[@>@@]}&|end_arg|: &maybe\cr
2110 \.{@@\&}&|insert|: \.{\\J}&maybe\cr
2111 \.{@@h}&|insert|: |force| \.{\\ATH} |force|&no\cr
2112 \.{@@<}\thinspace section name\thinspace\.{@@>}&|section_scrap|:
2113 \.{\\X}$n$\.:translated section name\.{\\X}&maybe\cr
2114 \.{@@(@q)@>}\thinspace section name\thinspace\.{@@>}&|section_scrap|:
2115 \.{\\X}$n$\.{:\\.\{}section name with special characters
2116 quoted\.{\\,\}\\X}\footnote*{The \.{\\,} (thin space) is omitted
2117 in ``|inner| \TeX\ mode.''}&maybe\cr
2118 \.{/*}\thinspace comment\thinspace\.{*/}&|insert|: |cancel|
2119 \.{\\C\{}translated comment\.\} |force|&no\cr
2120 \.{//}\thinspace comment&|insert|: |cancel|
2121 \.{\\SHC\{}translated comment\.\} |force|&no\cr
2122 }
2123
2124 \smallskip
2125 The construction \.{@@t}\thinspace stuff\/\thinspace\.{@@>} contributes
2126 \.{\\hbox\{}\thinspace stuff\/\thinspace\.\} to the following scrap.
2127
2128 @i prod.w
2129
2130 @* Implementing the productions.
2131 More specifically, a scrap is a structure consisting of a category
2132 |cat| and a |text_pointer| |trans|, which points to the translation in
2133 |tok_start|. When \CEE/ text is to be processed with the grammar above,
2134 we form an array |scrap_info| containing the initial scraps.
2135 Our production rules have the nice property that the right-hand side is never
2136 longer than the left-hand side. Therefore it is convenient to use sequential
2137 allocation for the current sequence of scraps. Five pointers are used to
2138 manage the parsing:
2139
2140 \yskip\hang |pp| is a pointer into |scrap_info|. We will try to match
2141 the category codes |pp->cat,@,@,(pp+1)->cat|$,\,\,\ldots\,$
2142 to the left-hand sides of productions.
2143
2144 \yskip\hang |scrap_base|, |lo_ptr|, |hi_ptr|, and |scrap_ptr| are such that
2145 the current sequence of scraps appears in positions |scrap_base| through
2146 |lo_ptr| and |hi_ptr| through |scrap_ptr|, inclusive, in the |cat| and
2147 |trans| arrays. Scraps located between |scrap_base| and |lo_ptr| have
2148 been examined, while those in positions |>=hi_ptr| have not yet been
2149 looked at by the parsing process.
2150
2151 \yskip\noindent Initially |scrap_ptr| is set to the position of the final
2152 scrap to be parsed, and it doesn't change its value. The parsing process
2153 makes sure that |lo_ptr>=pp+3|, since productions have as many as four terms,
2154 by moving scraps from |hi_ptr| to |lo_ptr|. If there are
2155 fewer than |pp+3| scraps left, the positions up to |pp+3| are filled with
2156 blanks that will not match in any productions. Parsing stops when
2157 |pp==lo_ptr+1| and |hi_ptr==scrap_ptr+1|.
2158
2159 Since the |scrap| structure will later be used for other purposes, we
2160 declare its second element as a union.
2161
2162 @d trans trans_plus.Trans /* translation texts of scraps */
2163
2164 @<Type...@>=
2165 typedef struct {
2166 eight_bits cat;
2167 eight_bits mathness;
2168 union {
2169 text_pointer Trans;@+@t}\6{@>
2170 @<Rest of |trans_plus| union@>@;
2171 } trans_plus;
2172 } scrap;
2173 typedef scrap *scrap_pointer;
2174
2175 @ @d max_scraps 5000 /* number of tokens in \CEE/ texts being parsed */
2176
2177 @<Private...@>=
2178 static scrap scrap_info[max_scraps]; /* memory array for scraps */
2179 static scrap_pointer scrap_info_end=scrap_info+max_scraps-1; /* end of |scrap_info| */
2180 static scrap_pointer scrap_base; /* beginning of the current scrap sequence */
2181 static scrap_pointer scrap_ptr; /* ending of the current scrap sequence */
2182 static scrap_pointer max_scr_ptr; /* largest value assumed by |scrap_ptr| */
2183 static scrap_pointer pp; /* current position for reducing productions */
2184 static scrap_pointer lo_ptr; /* last scrap that has been examined */
2185 static scrap_pointer hi_ptr; /* first scrap that has not been examined */
2186
2187 @ @<Set init...@>=
2188 scrap_base=scrap_info+1;
2189 max_scr_ptr=scrap_ptr=scrap_info;
2190
2191 @ Token lists in |@!tok_mem| are composed of the following kinds of
2192 items for \TEX/ output.
2193
2194 \yskip\item{$\bullet$}Character codes and special codes like |force| and
2195 |math_rel| represent themselves;
2196
2197 \item{$\bullet$}|id_flag+p| represents \.{\\\\\{{\rm identifier $p$}\}};
2198
2199 \item{$\bullet$}|res_flag+p| represents \.{\\\&\{{\rm identifier $p$}\}};
2200
2201 \item{$\bullet$}|section_flag+p| represents section name |p|;
2202
2203 \item{$\bullet$}|tok_flag+p| represents token list number |p|;
2204
2205 \item{$\bullet$}|inner_tok_flag+p| represents token list number |p|, to be
2206 translated without line-break controls.
2207
2208 @d id_flag 10240 /* signifies an identifier */
2209 @d res_flag (2*id_flag) /* signifies a reserved word */
2210 @d section_flag (3*id_flag) /* signifies a section name */
2211 @d tok_flag (4*id_flag) /* signifies a token list */
2212 @d inner_tok_flag (5*id_flag) /* signifies a token list in `\pb' */
2213
2214 @<Predecl...@>=
2215 #ifdef DEBUG
2216 static void print_text(text_pointer p);
2217 #endif
2218
2219 @ This function prints a token list for debugging;
2220 it is not used in |main| at this time.
2221
2222 @c
2223 #ifdef DEBUG
2224 static void
2225 print_text(
2226 text_pointer p)
2227 {
2228 token_pointer j; /* index into |tok_mem| */
2229 sixteen_bits r; /* remainder of token after the flag has been stripped off */
2230 if (p>=text_ptr) printf("BAD");
2231 else for (j=*p; j<*(p+1); j++) {
2232 r=*j%id_flag;
2233 switch (*j) {
2234 case id_flag:
2235 printf("\\\\{"@q}@>); print_id((name_dir+r)); putchar(@q{@>'}');
2236 break;
2237 case res_flag:
2238 printf("\\&{"@q}@>); print_id((name_dir+r)); putchar(@q{@>'}');
2239 break;
2240 case section_flag:
2241 putchar('<'); print_section_name((name_dir+r)); putchar('>');
2242 break;
2243 case tok_flag: printf("[[%d]]",(int)r); break;
2244 case inner_tok_flag: printf("|[[%d]]|",(int)r); break;
2245 default: @<Print token |r| in symbolic form@>@;
2246 }
2247 }
2248 update_terminal();
2249 }
2250 #endif /* |DEBUG| */
2251
2252 @ @<Print token |r|...@>=
2253 switch (r) {
2254 case math_rel: printf("\\mathrel{"@q}@>); break;
2255 case big_cancel: printf("[ccancel]"); break;
2256 case cancel: printf("[cancel]"); break;
2257 case indent: printf("[indent]"); break;
2258 case outdent: printf("[outdent]"); break;
2259 case dindent: printf("[dindent]"); break;
2260 case backup: printf("[backup]"); break;
2261 case opt: printf("[opt]"); break;
2262 case break_space: printf("[break]"); break;
2263 case force: printf("[force]"); break;
2264 case big_force: printf("[fforce]"); break;
2265 case preproc_line: printf("[preproc]"); break;
2266 case quoted_char: j++; printf("[%o]",(unsigned int)*j); break;
2267 case end_translation: printf("[quit]"); break;
2268 case inserted: printf("[inserted]"); break;
2269 default: putchar((int)r);
2270 }
2271
2272 @ The production rules listed above are embedded directly into \.{CWEAVE},
2273 since it is easier to do this than to write an interpretive system
2274 that would handle production systems in general. Several macros are defined
2275 here so that the program for each production is fairly short.
2276
2277 All of our productions conform to the general notion that some |k|
2278 consecutive scraps starting at some position |j| are to be replaced by a
2279 single scrap of some category |c| whose translation is composed from the
2280 translations of the disappearing scraps. After this production has been
2281 applied, the production pointer |pp| should change by an amount |d|. Such
2282 a production can be represented by the quadruple |(j,k,c,d)|. For example,
2283 the production `|exp@,comma@,exp| $\RA$ |exp|' would be represented by
2284 `|(pp,3,exp,-2)|'; in this case the pointer |pp| should decrease by 2
2285 after the production has been applied, because some productions with
2286 |exp| in their second or third positions might now match,
2287 but no productions have
2288 |exp| in the fourth position of their left-hand sides. Note that
2289 the value of |d| is determined by the whole collection of productions, not
2290 by an individual one.
2291 The determination of |d| has been
2292 done by hand in each case, based on the full set of productions but not on
2293 the grammar of \CEE/ or on the rules for constructing the initial
2294 scraps.
2295
2296 We also attach a serial number to each production, so that additional
2297 information is available when debugging. For example, the program below
2298 contains the statement `|reduce(pp,3,exp,-2,4)|' when it implements
2299 the production just mentioned.
2300
2301 Before calling |reduce|, the program should have appended the tokens of
2302 the new translation to the |tok_mem| array. We commonly want to append
2303 copies of several existing translations, and macros are defined to
2304 simplify these common cases. For example, |big_app2(pp)| will append the
2305 translations of two consecutive scraps, |pp->trans| and |(pp+1)->trans|, to
2306 the current token list. If the entire new translation is formed in this
2307 way, we write `|squash(j,k,c,d,n)|' instead of `|reduce(j,k,c,d,n)|'. For
2308 example, `|squash(pp,3,exp,-2,3)|' is an abbreviation for `|big_app3(pp);
2309 reduce(pp,3,exp,-2,3)|'.
2310
2311 A couple more words of explanation:
2312 Both |big_app| and |app| append a token (while |big_app1| to |big_app4|
2313 append the specified number of scrap translations) to the current token list.
2314 The difference between |big_app| and |app| is simply that |big_app|
2315 checks whether there can be a conflict between math and non-math
2316 tokens, and intercalates a `\.{\$}' token if necessary. When in
2317 doubt what to use, use |big_app|.
2318
2319 @d app(a) *(tok_ptr++)=(token)(a)
2320 @#
2321 @d big_app2(a) big_app1(a);@+big_app1(a+1)
2322 @d big_app3(a) big_app2(a);@+big_app1(a+2)
2323 @d big_app4(a) big_app3(a);@+big_app1(a+3)
2324 @#
2325 @d big_app1_insert(p,c) big_app1(p);@+big_app(c);@+big_app1(p+1)
2326 @d big_app1_insert_str(p,s) big_app1(p);@+app_str(s);@+big_app1(p+1)
2327 @d big_app2_insert(p,c) big_app2(p);@+big_app(c);@+big_app2(p+2)
2328
2329 @<Predecl...@>=
2330 static void app_str(const char *);@/
2331 static void big_app(token);@/
2332 static void big_app1(scrap_pointer);
2333
2334 @ The |mathness| is an attribute of scraps that says whether they are
2335 to be printed in a math mode context or not. It is separate from the
2336 ``part of speech'' (the |cat|) because to make each |cat| have
2337 a fixed |mathness| (as in the original \.{WEAVE}) would multiply the
2338 number of necessary production rules.
2339
2340 The low two bits (i.e., |mathness % 4|) control the left boundary.
2341 (We need two bits because we allow cases |yes_math|, |no_math| and
2342 |maybe_math|, which can go either way.)
2343 The next two bits (i.e., |mathness / 4|) control the right boundary.
2344 If we combine two scraps and the right boundary of the first has
2345 a different mathness from the left boundary of the second, we
2346 insert a \.{\$} in between. Similarly, if at printing time some
2347 irreducible scrap has a |yes_math| boundary the scrap gets preceded
2348 or followed by a~\.{\$}. The left boundary is |maybe_math| if and
2349 only if the right boundary is.
2350
2351 @d no_math 2 /* should be in horizontal mode */
2352 @d yes_math 1 /* should be in math mode */
2353 @d maybe_math 0 /* works in either horizontal or math mode */
2354
2355 @<Private...@>=
2356 static int cur_mathness, init_mathness;
2357
2358 @ The code below is an exact translation of the production rules into
2359 \CEE/, using such macros, and the reader should have no difficulty
2360 understanding the format by comparing the code with the symbolic
2361 productions as they were listed earlier.
2362
2363 @d begin_math() if (cur_mathness==maybe_math) init_mathness=yes_math;
2364 else if (cur_mathness==no_math) app_str("${}")
2365 @d end_math() if (cur_mathness==maybe_math) init_mathness=no_math;
2366 else if (cur_mathness==yes_math) app_str("{}$")
2367
2368 @c
2369 static void
2370 app_str(
2371 const char *s)
2372 {
2373 while (*s) app_tok(*s++);
2374 }
2375
2376 static void
2377 big_app(
2378 token a)
2379 {
2380 if (a==' ' || (a>=big_cancel && a<=big_force) || a==dindent)
2381 /* non-math token */ {
2382 end_math();
2383 cur_mathness=no_math;
2384 } else {
2385 begin_math();
2386 cur_mathness=yes_math;
2387 }
2388 app(a);
2389 }
2390
2391 static void
2392 big_app1(
2393 scrap_pointer a)
2394 {
2395 switch (a->mathness % 4) { /* left boundary */
2396 case (no_math):
2397 end_math();
2398 cur_mathness=a->mathness / 4; /* right boundary */
2399 break;
2400 case (yes_math):
2401 begin_math();
2402 cur_mathness=a->mathness / 4; /* right boundary */
2403 break;
2404 case (maybe_math): /* no changes */ break;
2405 }
2406 app(tok_flag+(int)((a)->trans-tok_start));
2407 }
2408
2409 @ Let us consider the big switch for productions now, before looking
2410 at its context. We want to design the program so that this switch
2411 works, so we might as well not keep ourselves in suspense about exactly what
2412 code needs to be provided with a proper environment.
2413
2414 @d cat1 (pp+1)->cat
2415 @d cat2 (pp+2)->cat
2416 @d cat3 (pp+3)->cat
2417 @d lhs_not_simple (pp->cat!=public_like
2418 && pp->cat!=semi
2419 && pp->cat!=prelangle
2420 && pp->cat!=prerangle @|
2421 && pp->cat!=template_like
2422 && pp->cat!=new_like
2423 && pp->cat!=new_exp
2424 && pp->cat!=ftemplate @|
2425 && pp->cat!=raw_ubin
2426 && pp->cat!=const_like
2427 && pp->cat!=raw_int
2428 && pp->cat!=operator_like)
2429 /* not a production with left side length 1 */
2430
2431 @<Match a production at |pp|, or increase |pp| if there is no match@>=
2432 if (cat1==end_arg && lhs_not_simple)
2433 if (pp->cat==begin_arg) squash(pp,2,exp,-2,124);
2434 else squash(pp,2,end_arg,-1,125);
2435 else if (pp->cat==rbrack) reduce(pp,0,rpar,-3,130);
2436 else if (pp->cat==using_like) reduce(pp,0,int_like,-3,140);
2437 else if (cat1==insert) squash(pp,2,pp->cat,-2,0);
2438 else if (cat2==insert) squash(pp+1,2,(pp+1)->cat,-1,0);
2439 else if (cat3==insert) squash(pp+2,2,(pp+2)->cat,0,0);
2440 else
2441 switch (pp->cat) {
2442 case exp: @<Cases for |exp|@>@; @+break;
2443 case lpar: @<Cases for |lpar|@>@; @+break;
2444 case unop: @<Cases for |unop|@>@; @+break;
2445 case ubinop: @<Cases for |ubinop|@>@; @+break;
2446 case binop: @<Cases for |binop|@>@; @+break;
2447 case cast: @<Cases for |cast|@>@; @+break;
2448 case sizeof_like: @<Cases for |sizeof_like|@>@; @+break;
2449 case int_like: @<Cases for |int_like|@>@; @+break;
2450 case public_like: @<Cases for |public_like|@>@; @+break;
2451 case colcol: @<Cases for |colcol|@>@; @+break;
2452 case decl_head: @<Cases for |decl_head|@>@; @+break;
2453 case decl: @<Cases for |decl|@>@; @+break;
2454 case base: @<Cases for |base|@>@; @+break;
2455 case struct_like: @<Cases for |struct_like|@>@; @+break;
2456 case struct_head: @<Cases for |struct_head|@>@; @+break;
2457 case fn_decl: @<Cases for |fn_decl|@>@; @+break;
2458 case function: @<Cases for |function|@>@; @+break;
2459 case lbrace: @<Cases for |lbrace|@>@; @+break;
2460 case if_like: @<Cases for |if_like|@>@; @+break;
2461 case else_like: @<Cases for |else_like|@>@; @+break;
2462 case else_head: @<Cases for |else_head|@>@; @+break;
2463 case if_clause: @<Cases for |if_clause|@>@; @+break;
2464 case if_head: @<Cases for |if_head|@>@; @+break;
2465 case do_like: @<Cases for |do_like|@>@; @+break;
2466 case case_like: @<Cases for |case_like|@>@; @+break;
2467 case catch_like: @<Cases for |catch_like|@>@; @+break;
2468 case tag: @<Cases for |tag|@>@; @+break;
2469 case stmt: @<Cases for |stmt|@>@; @+break;
2470 case semi: @<Cases for |semi|@>@; @+break;
2471 case lproc: @<Cases for |lproc|@>@; @+break;
2472 case section_scrap: @<Cases for |section_scrap|@>@; @+break;
2473 case insert: @<Cases for |insert|@>@; @+break;
2474 case prelangle: @<Cases for |prelangle|@>@; @+break;
2475 case prerangle: @<Cases for |prerangle|@>@; @+break;
2476 case langle: @<Cases for |langle|@>@; @+break;
2477 case template_like: @<Cases for |template_like|@>@; @+break;
2478 case new_like: @<Cases for |new_like|@>@; @+break;
2479 case new_exp: @<Cases for |new_exp|@>@; @+break;
2480 case ftemplate: @<Cases for |ftemplate|@>@; @+break;
2481 case for_like: @<Cases for |for_like|@>@; @+break;
2482 case raw_ubin: @<Cases for |raw_ubin|@>@; @+break;
2483 case const_like: @<Cases for |const_like|@>@; @+break;
2484 case raw_int: @<Cases for |raw_int|@>@; @+break;
2485 case operator_like: @<Cases for |operator_like|@>@; @+break;
2486 case typedef_like: @<Cases for |typedef_like|@>@; @+break;
2487 case delete_like: @<Cases for |delete_like|@>@; @+break;
2488 case question: @<Cases for |question|@>@; @+break;
2489 case alignas_like: @<Cases for |alignas_like|@>@; @+break;
2490 case lbrack: @<Cases for |lbrack|@>@; @+break;
2491 case attr_head: @<Cases for |attr_head|@>@; @+break;
2492 case attr: @<Cases for |attr|@>@; @+break;
2493 case default_like: @<Cases for |default_like|@>@; @+break;
2494 }
2495 pp++; /* if no match was found, we move to the right */
2496
2497 @ In \CEE/, new specifier names can be defined via |typedef|, and we want
2498 to make the parser recognize future occurrences of the identifier thus
2499 defined as specifiers. This is done by the procedure |make_reserved|,
2500 which changes the |ilk| of the relevant identifier.
2501
2502 We first need a procedure to recursively seek the first
2503 identifier in a token list, because the identifier might
2504 be enclosed in parentheses, as when one defines a function
2505 returning a pointer.
2506
2507 If the first identifier found is a keyword like `\&{case}', we
2508 return the special value |case_found|; this prevents underlining
2509 of identifiers in case labels.
2510
2511 If the first identifier is the keyword `\&{operator}', we give up;
2512 users who want to index definitions of overloaded \CPLUSPLUS/ operators
2513 should say, for example, `\.{@@!@@\^\\\&\{operator\} \$+\{=\}\$@@>}' (or,
2514 more properly alpha\-betized,
2515 `\.{@@!@@:operator+=\}\{\\\&\{operator\} \$+\{=\}\$@@>}').
2516
2517 @d no_ident_found (token_pointer)0 /* distinct from any identifier token */
2518 @d case_found (token_pointer)1 /* likewise */
2519 @d operator_found (token_pointer)2 /* likewise */
2520
2521 @<Predecl...@>=
2522 static token_pointer find_first_ident(text_pointer);@/
2523 static void make_reserved(scrap_pointer);@/
2524 static void make_underlined(scrap_pointer);@/
2525 static void underline_xref(name_pointer);
2526
2527 @ @c
2528 static token_pointer
2529 find_first_ident(
2530 text_pointer p)
2531 {
2532 token_pointer q; /* token to be returned */
2533 token_pointer j; /* token being looked at */
2534 sixteen_bits r; /* remainder of token after the flag has been stripped off */
2535 if (p>=text_ptr) confusion("find_first_ident");
2536 for (j=*p; j<*(p+1); j++) {
2537 r=*j%id_flag;
2538 switch (*j/id_flag) {
2539 case 2: /* |res_flag| */
2540 if (name_dir[r].ilk==case_like) return case_found;
2541 if (name_dir[r].ilk==operator_like) return operator_found;
2542 if (name_dir[r].ilk!=raw_int) break;
2543 @=/* else fall through */@>@;
2544 case 1: return j;
2545 case 4: case 5: /* |tok_flag| or |inner_tok_flag| */
2546 if ((q=find_first_ident(tok_start+r))!=no_ident_found)
2547 return q;
2548 @=/* else fall through */@>@;
2549 default: ; /* char, |section_flag|, fall thru: move on to next token */
2550 if (*j==inserted) return no_ident_found; /* ignore inserts */
2551 else if (*j==qualifier) j++; /* bypass namespace qualifier */
2552 }
2553 }
2554 return no_ident_found;
2555 }
2556
2557 @ The scraps currently being parsed must be inspected for any
2558 occurrence of the identifier that we're making reserved; hence
2559 the |for| loop below.
2560
2561 @c
2562 static void
2563 make_reserved( /* make the first identifier in |p->trans| like |int| */
2564 scrap_pointer p)
2565 {
2566 sixteen_bits tok_value; /* the name of this identifier, plus its flag */
2567 token_pointer tok_loc; /* pointer to |tok_value| */
2568 if ((tok_loc=find_first_ident(p->trans))<=operator_found)
2569 return; /* this should not happen */
2570 tok_value=*tok_loc;
2571 for (;p<=scrap_ptr; p==lo_ptr? p=hi_ptr: p++)
2572 if (p->cat==exp)
2573 if (**(p->trans)==tok_value) {
2574 p->cat=raw_int;
2575 **(p->trans)=tok_value%id_flag+res_flag;
2576 }
2577 (name_dir+(sixteen_bits)(tok_value%id_flag))->ilk=raw_int;
2578 *tok_loc=tok_value%id_flag+res_flag;
2579 }
2580
2581 @ In the following situations we want to mark the occurrence of
2582 an identifier as a definition: when |make_reserved| is just about to be
2583 used; after a specifier, as in |char **argv|;
2584 before a colon, as in \\{found}:; and in the declaration of a function,
2585 as in \\{main}()$\{\ldots;\}$. This is accomplished by the invocation
2586 of |make_underlined| at appropriate times. Notice that, in the declaration
2587 of a function, we find out that the identifier is being defined only after
2588 it has been swallowed up by an |exp|.
2589
2590 @c
2591 static void
2592 make_underlined(
2593 /* underline the entry for the first identifier in |p->trans| */
2594 scrap_pointer p)
2595 {
2596 token_pointer tok_loc; /* where the first identifier appears */
2597 if ((tok_loc=find_first_ident(p->trans))<=operator_found)
2598 return; /* this happens, for example, in |case found:| */
2599 xref_switch=def_flag;
2600 underline_xref(*tok_loc%id_flag+name_dir);
2601 }
2602
2603 @ We cannot use |new_xref| to underline a cross-reference at this point
2604 because this would just make a new cross-reference at the end of the list.
2605 We actually have to search through the list for the existing
2606 cross-reference.
2607
2608 @c
2609 static void
2610 underline_xref(
2611 name_pointer p)
2612 {
2613 xref_pointer q=(xref_pointer)p->xref; /* pointer to cross-reference being examined */
2614 xref_pointer r; /* temporary pointer for permuting cross-references */
2615 sixteen_bits m; /* cross-reference value to be installed */
2616 sixteen_bits n; /* cross-reference value being examined */
2617 if (no_xref) return;
2618 m=section_count+xref_switch;
2619 while (q != xmem) {
2620 n=q->num;
2621 if (n==m) return;
2622 else if (m==n+def_flag) {
2623 q->num=m; return;
2624 }
2625 else if (n>=def_flag && n<m) break;
2626 q=q->xlink;
2627 }
2628 @<Insert new cross-reference at |q|, not at beginning of list@>@;
2629 }
2630
2631 @ We get to this section only when the identifier is one letter long,
2632 so it didn't get a non-underlined entry during phase one. But it may
2633 have got some explicitly underlined entries in later sections, so in order
2634 to preserve the numerical order of the entries in the index, we have
2635 to insert the new cross-reference not at the beginning of the list
2636 (namely, at |p->xref|), but rather right before |q|.
2637
2638 @<Insert new cross-reference at |q|...@>=
2639 append_xref(0); /* this number doesn't matter */
2640 xref_ptr->xlink=(xref_pointer)p->xref; r=xref_ptr;
2641 update_node(p);
2642 while (r->xlink!=q) {r->num=r->xlink->num; r=r->xlink;}
2643 r->num=m; /* everything from |q| on is left undisturbed */
2644
2645 @ Now comes the code that tries to match each production starting
2646 with a particular type of scrap. Whenever a match is discovered,
2647 the |squash| or |reduce| function will cause the appropriate action
2648 to be performed.
2649
2650 @<Cases for |exp|@>=
2651 if (cat1==lbrace || cat1==int_like || cat1==decl) {
2652 make_underlined(pp); big_app(dindent); big_app1(pp);
2653 reduce(pp,1,fn_decl,0,1);
2654 }
2655 else if (cat1==unop) squash(pp,2,exp,-2,2);
2656 else if ((cat1==binop || cat1==ubinop) && cat2==exp)
2657 squash(pp,3,exp,-2,3);
2658 else if (cat1==comma && cat2==exp) {
2659 big_app2(pp); app(opt); app('9'); big_app1(pp+2); reduce(pp,3,exp,-2,4);
2660 }
2661 else if (cat1==lpar && cat2==rpar && cat3==colon) reduce(pp+3,0,base,0,5);
2662 else if (cat1==cast && cat2==colon) reduce(pp+2,0,base,0,5);
2663 else if (cat1==semi) squash(pp,2,stmt,-1,6);
2664 else if (cat1==colon) {
2665 make_underlined (pp); squash(pp,2,tag,-1,7);
2666 }
2667 else if (cat1==rbrace) reduce(pp,0,stmt,-1,8);
2668 else if (cat1==lpar && cat2==rpar && (cat3==const_like || cat3==case_like)) {
2669 big_app1_insert(pp+2,' '); reduce(pp+2,2,rpar,0,9);
2670 }
2671 else if (cat1==cast && (cat2==const_like || cat2==case_like)) {
2672 big_app1_insert(pp+1,' '); reduce(pp+1,2,cast,0,9);
2673 }
2674 else if (cat1==exp || cat1==cast) squash(pp,2,exp,-2,10);
2675 else if (cat1==attr) {
2676 big_app1_insert(pp,' '); reduce(pp,2,exp,-2,142);
2677 }
2678 else if (cat1==colcol && cat2==int_like) squash(pp,3,int_like,-2,152);
2679
2680 @ @<Cases for |lpar|@>=
2681 if ((cat1==exp||cat1==ubinop) && cat2==rpar) squash(pp,3,exp,-2,11);
2682 else if (cat1==rpar) {
2683 big_app1_insert_str(pp,"\\,");
2684 @.\\,@>
2685 reduce(pp,2,exp,-2,12);
2686 }
2687 else if ((cat1==decl_head || cat1==int_like || cat1==cast) && cat2==rpar)
2688 squash(pp,3,cast,-2,13);
2689 else if ((cat1==decl_head || cat1==int_like || cat1==exp) && cat2==comma) {
2690 big_app3(pp); app(opt); app('9'); reduce(pp,3,lpar,-1,14);
2691 }
2692 else if (cat1==stmt || cat1==decl) {
2693 big_app2(pp); big_app(' '); reduce(pp,2,lpar,-1,15);
2694 }
2695
2696 @ @<Cases for |unop|@>=
2697 if (cat1==exp || cat1==int_like) squash(pp,2,exp,-2,16);
2698
2699 @ @<Cases for |ubinop|@>=
2700 if (cat1==cast && cat2==rpar) {
2701 big_app('{'); big_app1_insert(pp,'}'); reduce(pp,2,cast,-2,17);
2702 }
2703 else if (cat1==exp || cat1==int_like) {
2704 big_app('{'); big_app1_insert(pp,'}'); reduce(pp,2,cat1,-2,18);
2705 }
2706 else if (cat1==binop) {
2707 big_app(math_rel); big_app1_insert(pp,'{'); big_app('}');
2708 big_app('}'); reduce(pp,2,binop,-1,19);
2709 }
2710
2711 @ @<Cases for |binop|@>=
2712 if (cat1==binop) {
2713 big_app(math_rel); big_app('{'); big_app1(pp); big_app('}');
2714 big_app('{'); big_app1(pp+1); big_app('}');
2715 big_app('}'); reduce(pp,2,binop,-1,20);
2716 }
2717
2718 @ @<Cases for |cast|@>=
2719 if (cat1==lpar) squash(pp,2,lpar,-1,21);
2720 else if (cat1==exp) {
2721 big_app1_insert_str(pp,"\\,");
2722 @.\\,@>
2723 reduce(pp,2,exp,-2,21);
2724 }
2725 else if (cat1==semi) reduce(pp,0,exp,-2,22);
2726
2727 @ @<Cases for |sizeof_like|@>=
2728 if (cat1==cast) squash(pp,2,exp,-2,23);
2729 else if (cat1==exp) {
2730 big_app1_insert(pp,' '); reduce(pp,2,exp,-2,24);
2731 }
2732
2733 @ @<Cases for |int_like|@>=
2734 if (cat1==int_like|| cat1==struct_like) {
2735 big_app1_insert(pp,' '); reduce(pp,2,cat1,-2,25);
2736 }
2737 else if (cat1==exp && (cat2==raw_int||cat2==struct_like))
2738 squash(pp,2,int_like,-2,26);
2739 else if (cat1==exp || cat1==ubinop || cat1==colon) {
2740 big_app1(pp); big_app(' '); reduce(pp,1,decl_head,-1,27);
2741 }
2742 else if (cat1==semi || cat1==binop) reduce(pp,0,decl_head,0,28);
2743
2744 @ @<Cases for |public_like|@>=
2745 if (cat1==colon) squash(pp,2,tag,-1,29);
2746 else reduce(pp,0,int_like,-2,30);
2747
2748 @ @<Cases for |colcol|@>=
2749 if (cat1==exp||cat1==int_like) {
2750 app(qualifier); squash(pp,2,cat1,-2,31);
2751 }
2752 else if (cat1==colcol) squash(pp,2,colcol,-1,32);
2753
2754 @ @<Cases for |decl_head|@>=
2755 if (cat1==comma) {
2756 big_app2(pp); app(opt); app('9'); reduce(pp,2,decl_head,-1,33);
2757 }
2758 else if (cat1==ubinop) {
2759 big_app1_insert(pp,'{'); big_app('}'); reduce(pp,2,decl_head,-1,34);
2760 }
2761 else if (cat1==exp && cat2!=lpar && cat2!=lbrack && cat2!=exp && cat2!=cast) {
2762 make_underlined(pp+1); squash(pp,2,decl_head,-1,35);
2763 }
2764 else if ((cat1==binop||cat1==colon) && cat2==exp && (cat3==comma ||
2765 cat3==semi || cat3==rpar))
2766 squash(pp,3,decl_head,-1,36);
2767 else if (cat1==cast) squash(pp,2,decl_head,-1,37);
2768 else if (cat1==int_like || cat1==lbrace || cat1==decl) {
2769 big_app(dindent); squash(pp,1,fn_decl,0,38);
2770 }
2771 else if (cat1==semi) squash(pp,2,decl,-1,39);
2772 else if (cat1==attr) {
2773 big_app1_insert(pp,' '); reduce(pp,2,decl_head,-1,139);
2774 }
2775
2776 @ @<Cases for |decl|@>=
2777 if (cat1==decl) {
2778 big_app1_insert(pp,force); reduce(pp,2,decl,-1,40);
2779 }
2780 else if (cat1==stmt || cat1==function) {
2781 big_app1_insert(pp,big_force); reduce(pp,2,cat1,-1,41);
2782 }
2783
2784 @ @<Cases for |base|@>=
2785 if (cat1==int_like || cat1==exp) {
2786 if (cat2==comma) {
2787 big_app1_insert(pp,' '); big_app1(pp+2);
2788 app(opt); app('9'); reduce(pp,3,base,0,42);
2789 }
2790 else if (cat2==lbrace) {
2791 big_app1_insert(pp,' '); big_app(' '); big_app1(pp+2);
2792 reduce(pp,3,lbrace,-2,43);
2793 }
2794 }
2795
2796 @ @<Cases for |struct_like|@>=
2797 if (cat1==lbrace) {
2798 big_app1_insert(pp,' '); reduce(pp,2,struct_head,0,44);
2799 }
2800 else if (cat1==exp||cat1==int_like) {
2801 if (cat2==lbrace || cat2==semi) {
2802 make_underlined(pp+1); make_reserved(pp+1);
2803 big_app1_insert(pp,' ');
2804 if (cat2==semi) reduce(pp,2,decl_head,0,45);
2805 else {
2806 big_app(' '); big_app1(pp+2); reduce(pp,3,struct_head,0,46);
2807 }
2808 }
2809 else if (cat2==colon) reduce(pp+2,0,base,2,47);
2810 else if (cat2!=base) {
2811 big_app1_insert(pp,' '); reduce(pp,2,int_like,-2,48);
2812 }
2813 }
2814 else if (cat1==attr) {
2815 big_app1_insert(pp,' '); reduce(pp,2,struct_like,-3,141);
2816 }
2817 else if (cat1==struct_like) {
2818 big_app1_insert(pp,' '); reduce(pp,2,struct_like,-3,151);
2819 }
2820
2821 @ @<Cases for |struct_head|@>=
2822 if ((cat1==decl || cat1==stmt || cat1==function) && cat2==rbrace) {
2823 big_app1(pp); big_app(indent); big_app(force); big_app1(pp+1);
2824 big_app(outdent); big_app(force); big_app1(pp+2);
2825 reduce(pp,3,int_like,-2,49);
2826 }
2827 else if (cat1==rbrace) {
2828 big_app1_insert_str(pp,"\\,");
2829 @.\\,@>
2830 reduce(pp,2,int_like,-2,50);
2831 }
2832
2833 @ @<Cases for |fn_decl|@>=
2834 if (cat1==decl) {
2835 big_app1_insert(pp,force); reduce(pp,2,fn_decl,0,51);
2836 }
2837 else if (cat1==stmt) {
2838 big_app1(pp); app(outdent); app(outdent); big_app(force);
2839 big_app1(pp+1); reduce(pp,2,function,-1,52);
2840 }
2841 else if (cat1==attr) {
2842 big_app1_insert(pp,' '); reduce(pp,2,fn_decl,0,157);
2843 }
2844
2845 @ @<Cases for |function|@>=
2846 if (cat1==stmt || cat1==decl || cat1==function) {
2847 big_app1_insert(pp,big_force); reduce(pp,2,cat1,-1,53);
2848 }
2849
2850 @ @<Cases for |lbrace|@>=
2851 if (cat1==rbrace) {
2852 big_app1_insert_str(pp,"\\,");
2853 @.\\,@>
2854 reduce(pp,2,stmt,-1,54);
2855 }
2856 else if ((cat1==stmt||cat1==decl||cat1==function) && cat2==rbrace) {
2857 big_app(force); big_app1(pp); big_app(indent);
2858 big_app(force_first ? force : break_space);
2859 big_app1(pp+1); big_app(force); big_app(backup); big_app1(pp+2);
2860 big_app(outdent); big_app(force); reduce(pp,3,stmt,-1,55);
2861 }
2862 else if (cat1==exp) {
2863 if (cat2==rbrace) squash(pp,3,exp,-2,56);
2864 else if (cat2==comma && cat3==rbrace) squash(pp,4,exp,-2,56);
2865 }
2866
2867 @ @<Cases for |if_like|@>=
2868 if (cat1==exp) {
2869 big_app1_insert(pp,' '); reduce(pp,2,if_clause,0,57);
2870 }
2871
2872 @ @<Cases for |else_like|@>=
2873 if (cat1==colon) reduce(pp+1,0,base,1,58);
2874 else if (cat1==lbrace) reduce(pp,0,else_head,0,59);
2875 else if (cat1==stmt) {
2876 big_app(force); big_app1(pp); big_app(indent); big_app(break_space);
2877 big_app1(pp+1); big_app(outdent); big_app(force);
2878 reduce(pp,2,stmt,-1,60);
2879 }
2880
2881 @ @<Cases for |else_head|@>=
2882 if (cat1==stmt || cat1==exp) {
2883 big_app(force); big_app1(pp); big_app(break_space); app(noop);
2884 big_app(cancel); big_app1(pp+1); big_app(force);
2885 reduce(pp,2,stmt,-1,61);
2886 }
2887
2888 @ @<Cases for |if_clause|@>=
2889 if (cat1==lbrace) reduce(pp,0,if_head,0,62);
2890 else if (cat1==stmt) {
2891 if (cat2==else_like) {
2892 big_app(force); big_app1(pp); big_app(indent); big_app(break_space);
2893 big_app1(pp+1); big_app(outdent); big_app(force); big_app1(pp+2);
2894 if (cat3==if_like) {
2895 big_app(' '); big_app1(pp+3); reduce(pp,4,if_like,0,63);
2896 }
2897 else reduce(pp,3,else_like,0,64);
2898 }
2899 else reduce(pp,0,else_like,0,65);
2900 }
2901 else if (cat1==attr) {
2902 big_app1_insert(pp,' '); reduce(pp,2,if_head,0,146);
2903 }
2904
2905 @ @<Cases for |if_head|@>=
2906 if (cat1==stmt || cat1==exp) {
2907 if (cat2==else_like) {
2908 big_app(force); big_app1(pp); big_app(break_space); app(noop);
2909 big_app(cancel); big_app1_insert(pp+1,force);
2910 if (cat3==if_like) {
2911 big_app(' '); big_app1(pp+3); reduce(pp,4,if_like,0,66);
2912 }
2913 else reduce(pp,3,else_like,0,67);
2914 }
2915 else reduce(pp,0,else_head,0,68);
2916 }
2917
2918 @ @<Cases for |do_like|@>=
2919 if (cat1==stmt && cat2==else_like && cat3==semi) {
2920 if (!force_lines) big_app(force);
2921 big_app1(pp); big_app(break_space); app(noop); big_app(cancel);
2922 big_app1(pp+1); big_app(cancel); app(noop); big_app(break_space);
2923 big_app2(pp+2);
2924 if (!force_lines) big_app(force);
2925 reduce(pp,4,stmt,-1,69);
2926 }
2927
2928 @ @<Cases for |case_like|@>=
2929 if (cat1==semi) squash(pp,2,stmt,-1,70);
2930 else if (cat1==colon) squash(pp,2,tag,-1,71);
2931 else if (cat1==exp) {
2932 big_app1_insert(pp,' '); reduce(pp,2,exp,-2,72);
2933 }
2934
2935 @ @<Cases for |catch_like|@>=
2936 if (cat1==cast || cat1==exp) {
2937 big_app1_insert(pp,dindent); reduce(pp,2,fn_decl,0,73);
2938 }
2939
2940 @ @<Cases for |tag|@>=
2941 if (cat1==tag) {
2942 big_app1_insert(pp,break_space); reduce(pp,2,tag,-1,74);
2943 }
2944 else if (cat1==stmt||cat1==decl||cat1==function) {
2945 big_app(force); big_app(backup); big_app1_insert(pp,break_space);
2946 reduce(pp,2,cat1,-1,75);
2947 }
2948 else if (cat1==rbrace) reduce(pp,0,decl,-1,156);
2949
2950 @ The user can decide at run-time whether short statements should be
2951 grouped together on the same line. Another form of compaction places
2952 the first line of a `compound statement', a.k.a.\ `block', next to
2953 the opening curly brace.
2954
2955 @d force_lines flags['f'] /* should each statement be on its own line? */
2956 @d force_first flags['F'] /* should compound statement start on new line? */
2957
2958 @<Set init...@>=
2959 force_lines=force_first=true;
2960
2961 @ @<Cases for |stmt|@>=
2962 if (cat1==stmt || cat1==decl || cat1==function) {
2963 big_app1_insert(pp, (cat1==decl || cat1==function) ? big_force :
2964 force_lines ? force : break_space); reduce(pp,2,cat1,-1,76);
2965 }
2966
2967 @ @<Cases for |semi|@>=
2968 big_app(' '); squash(pp,1,stmt,-1,77);
2969
2970 @ @<Cases for |lproc|@>=
2971 if (cat1==define_like) make_underlined(pp+2);
2972 if (cat1==if_like || cat1==else_like || cat1==define_like)
2973 squash(pp,2,lproc,0,78);
2974 else if (cat1==rproc) {
2975 app(inserted); squash(pp,2,insert,-1,79);
2976 } else if (cat1==exp || cat1==function) {
2977 if (cat2==rproc) {
2978 app(inserted); big_app1_insert(pp,' '); big_app1(pp+2);
2979 reduce(pp,3,insert,-1,80);
2980 }
2981 else if (cat1==exp && cat2==exp && cat3==rproc) {
2982 app(inserted); big_app1_insert(pp,' '); app_str("\\5");
2983 @.\\5@>
2984 big_app2(pp+2); reduce(pp,4,insert,-1,80);
2985 }
2986 }
2987
2988 @ @<Cases for |section_scrap|@>=
2989 if (cat1==semi) {
2990 big_app2(pp); big_app(force); reduce(pp,2,stmt,-2,81);
2991 }
2992 else reduce(pp,0,exp,-2,82);
2993
2994 @ @<Cases for |insert|@>=
2995 if (cat1==function)
2996 squash(pp,2,function,0,83);
2997
2998 @ @<Cases for |prelangle|@>=
2999 init_mathness=cur_mathness=yes_math;
3000 app('<'); reduce(pp,1,binop,-2,84);
3001
3002 @ @<Cases for |prerangle|@>=
3003 init_mathness=cur_mathness=yes_math;
3004 app('>'); reduce(pp,1,binop,-2,85);
3005
3006 @ @d reserve_typenames flags['t']
3007 /* should we treat \&{typename} in a template like \&{typedef}? */
3008
3009 @<Cases for |langle|@>=
3010 if (cat1==prerangle) {
3011 big_app1_insert_str(pp,"\\,");
3012 @.\\,@>
3013 reduce(pp,2,cast,-1,86);
3014 }
3015 else if (cat1==decl_head || cat1==int_like || cat1==exp) {
3016 if (cat2==prerangle) squash(pp,3,cast,-1,87);
3017 else if (cat2==comma) {
3018 big_app3(pp); app(opt); app('9'); reduce(pp,3,langle,0,88);
3019 }
3020 }
3021 else if ((cat1==struct_like) @|
3022 && (cat2==exp || cat2==int_like) @|
3023 && (cat3==comma || cat3==prerangle)) {
3024 make_underlined(pp+2); if (reserve_typenames) make_reserved(pp+2);
3025 big_app2_insert(pp,' ');
3026 if (cat3==comma) {
3027 app(opt); app('9'); reduce(pp,4,langle,0,153);
3028 }
3029 else reduce(pp,4,cast,-1,154);
3030 }
3031
3032 @ @<Cases for |template_like|@>=
3033 if (cat1==exp && cat2==prelangle) reduce(pp+2,0,langle,2,89);
3034 else if (cat1==exp || cat1==raw_int) {
3035 big_app1_insert(pp,' '); reduce(pp,2,cat1,-2,90);
3036 }
3037 else if (cat1==cast && cat2==struct_like) {
3038 big_app1_insert(pp,' '); reduce(pp,2,struct_like,0,155);
3039 }
3040 else reduce(pp,0,raw_int,0,91);
3041
3042 @ @<Cases for |new_like|@>=
3043 if (cat1==lpar && cat2==exp && cat3==rpar) squash(pp,4,new_like,0,92);
3044 else if (cat1==cast) {
3045 big_app1_insert(pp,' '); reduce(pp,2,exp,-2,93);
3046 }
3047 else if (cat1!=lpar) reduce(pp,0,new_exp,0,94);
3048
3049 @ @<Cases for |new_exp|@>=
3050 if (cat1==int_like || cat1==const_like) {
3051 big_app1_insert(pp,' '); reduce(pp,2,new_exp,0,95);
3052 }
3053 else if (cat1==struct_like && (cat2==exp || cat2==int_like)) {
3054 big_app1_insert(pp,' '); big_app(' ');
3055 big_app1(pp+2); reduce(pp,3,new_exp,0,96);
3056 }
3057 else if (cat1==raw_ubin) {
3058 big_app1_insert(pp,'{'); big_app('}'); reduce(pp,2,new_exp,0,97);
3059 }
3060 else if (cat1==lpar) reduce(pp,0,exp,-2,98);
3061 else if (cat1==exp) {
3062 big_app1(pp); big_app(' '); reduce(pp,1,exp,-2,98);
3063 }
3064 else if (cat1!=raw_int && cat1!=struct_like && cat1!=colcol)
3065 reduce(pp,0,exp,-2,99);
3066
3067 @ @<Cases for |ftemplate|@>=
3068 if (cat1==prelangle) reduce(pp+1,0,langle,1,100);
3069 else reduce(pp,0,exp,-2,101);
3070
3071 @ @<Cases for |for_like|@>=
3072 if (cat1==exp) {
3073 big_app1_insert(pp,' '); reduce(pp,2,else_like,-2,102);
3074 }
3075
3076 @ @<Cases for |raw_ubin|@>=
3077 if (cat1==const_like) {
3078 big_app2(pp); app_str("\\ "); reduce(pp,2,raw_ubin,0,103);
3079 @.\\\ @>
3080 }
3081 else reduce(pp,0,ubinop,-2,104);
3082
3083 @ @<Cases for |const_like|@>=
3084 reduce(pp,0,int_like,-2,105);
3085
3086 @ @<Cases for |raw_int|@>=
3087 if (cat1==prelangle) reduce(pp+1,0,langle,1,106);
3088 else if (cat1==colcol) squash(pp,2,colcol,-1,107);
3089 else if (cat1==cast) squash(pp,2,raw_int,0,108);
3090 else if (cat1==lpar) reduce(pp,0,exp,-2,109);
3091 else if (cat1==lbrack) reduce(pp,0,exp,-2,144);
3092 else if (cat1!=langle) reduce(pp,0,int_like,-3,110);
3093
3094 @ @<Cases for |operator_like|@>=
3095 if (cat1==binop || cat1==unop || cat1==ubinop) {
3096 if (cat2==binop) break;
3097 big_app1_insert(pp,'{'); big_app('}'); reduce(pp,2,exp,-2,111);
3098 }
3099 else if (cat1==new_like || cat1==delete_like) {
3100 big_app1_insert(pp,' '); reduce(pp,2,exp,-2,112);
3101 }
3102 else if (cat1==comma) squash(pp,2,exp,-2,113);
3103 else if (cat1!=raw_ubin) reduce(pp,0,new_exp,0,114);
3104
3105 @ @<Cases for |typedef_like|@>=
3106 if ((cat1==int_like || cat1==cast) && (cat2==comma || cat2==semi))
3107 reduce(pp+1,0,exp,-1,115);
3108 else if (cat1==int_like) {
3109 big_app1_insert(pp,' '); reduce(pp,2,typedef_like,0,116);
3110 }
3111 else if (cat1==exp && cat2!=lpar && cat2!=lbrack && cat2!=exp && cat2!=cast) {
3112 make_underlined(pp+1); make_reserved(pp+1);
3113 big_app1_insert(pp,' '); reduce(pp,2,typedef_like,0,117);
3114 }
3115 else if (cat1==comma) squash(pp,2,typedef_like,0,118);
3116 else if (cat1==semi) squash(pp,2,decl,-1,119);
3117 else if (cat1==ubinop && (cat2==ubinop || cat2==cast)) {
3118 big_app('{'); big_app1_insert(pp+1,'}'); reduce(pp+1,2,cat2,0,120);
3119 }
3120
3121 @ @<Cases for |delete_like|@>=
3122 if (cat1==lbrack && cat2==rbrack) {
3123 big_app1(pp); big_app1_insert_str(pp+1,"\\,");
3124 @.\\,@>
3125 reduce(pp,3,delete_like,0,121);
3126 }
3127 else if (cat1==exp) {
3128 big_app1_insert(pp,' '); reduce(pp,2,exp,-2,122);
3129 }
3130
3131 @ @<Cases for |question|@>=
3132 if (cat1==exp && (cat2==colon || cat2==base)) {
3133 (pp+2)->mathness=5*yes_math; /* this colon should be in math mode */
3134 squash(pp,3,binop,-2,123);
3135 }
3136
3137 @ @<Cases for |alignas_like|@>=
3138 if (cat1==decl_head) squash(pp,2,attr,-1,126);
3139 else if (cat1==exp) squash(pp,2,attr,-1,127);
3140 else if (cat1==cast) squash(pp,2,attr,-1,158);
3141
3142 @ @<Cases for |lbrack|@>=
3143 if (cat1==lbrack)
3144 if (cat2==rbrack && cat3==rbrack) squash(pp,4,exp,-2,147);
3145 else squash(pp,2,attr_head,-1,128);
3146 else reduce(pp,0,lpar,-1,129);
3147
3148 @ @<Cases for |attr_head|@>=
3149 if (cat1==rbrack && cat2==rbrack) squash(pp,3,attr,-1,131);
3150 else if (cat1==exp) squash(pp,2,attr_head,0,132);
3151 else if (cat1==using_like && cat2==exp && cat3==colon) {
3152 big_app2_insert(pp,' '); big_app(' ');
3153 reduce(pp,4,attr_head,0,133);
3154 }
3155 else if (cat1==comma) squash(pp,2,attr_head,0,145);
3156
3157 @ @<Cases for |attr|@>=
3158 if (cat1==lbrace || cat1==stmt) {
3159 big_app1_insert(pp,' '); reduce(pp,2,cat1,-2,134);
3160 }
3161 else if (cat1==tag) {
3162 big_app1_insert(pp,' '); reduce(pp,2,tag,-1,135);
3163 }
3164 else if (cat1==semi) squash(pp,2,stmt,-2,136);
3165 else if (cat1==attr) {
3166 big_app1_insert(pp,' '); reduce(pp,2,attr,-1,137);
3167 }
3168 else if (cat1==decl_head) {
3169 big_app1_insert(pp,' '); reduce(pp,2,decl_head,-1,138);
3170 }
3171 else if (cat1==typedef_like) {
3172 big_app1_insert(pp,' '); reduce(pp,2,typedef_like,0,143);
3173 }
3174 else if (cat1==function) {
3175 big_app1_insert(pp,' '); reduce(pp,2,function,-1,148);
3176 }
3177
3178 @ @<Cases for |default_like|@>=
3179 if (cat1==colon) reduce(pp,0,case_like,-3,149);
3180 else reduce(pp,0,exp,-2,150);
3181
3182 @ The `|freeze_text|' macro is used to give official status to a token list.
3183 Before saying |freeze_text|, items are appended to the current token list,
3184 and we know that the eventual number of this token list will be the current
3185 value of |text_ptr|. But no list of that number really exists as yet,
3186 because no ending point for the current list has been
3187 stored in the |tok_start| array. After saying |freeze_text|, the
3188 old current token list becomes legitimate, and its number is the current
3189 value of |text_ptr-1| since |text_ptr| has been increased. The new
3190 current token list is empty and ready to be appended~to.
3191 Note that |freeze_text| does not check to see that |text_ptr| hasn't gotten
3192 too large, since it is assumed that this test was done beforehand.
3193
3194 @d freeze_text() *(++text_ptr)=tok_ptr
3195
3196 @<Predecl...@>=
3197 static void reduce(scrap_pointer,short,eight_bits,short,short);@/
3198 static void squash(scrap_pointer,short,eight_bits,short,short);
3199
3200 @ Now here's the |reduce| procedure used in our code for productions,
3201 which takes advantage of the simplifications that occur when |k==0|
3202 or |k==1|.
3203
3204 @c
3205 static void
3206 reduce(
3207 scrap_pointer j, short k,
3208 eight_bits c,
3209 short d, short n)
3210 {
3211 scrap_pointer i; /* pointer into scrap memory */
3212 j->cat=c;
3213 if (k>0) {
3214 j->trans=text_ptr;
3215 j->mathness=4*cur_mathness+init_mathness;
3216 freeze_text();
3217 }
3218 if (k>1) {
3219 for (i=j+k; i<=lo_ptr; i++)
3220 *(i-k+1)=*i;@^system dependencies@>
3221 lo_ptr=lo_ptr-k+1;
3222 }
3223 pp=(pp+d<scrap_base? scrap_base: pp+d);
3224 @<Print a snapshot of the scrap list if debugging @>@;
3225 pp--; /* we next say |pp++| */
3226 }
3227
3228 @ And here's the |squash| procedure, which combines |big_app|${}_k$ and
3229 |reduce| for matching numbers~|k|.
3230
3231 @c
3232 static void
3233 squash(
3234 scrap_pointer j, short k,
3235 eight_bits c,
3236 short d, short n)
3237 {
3238 switch (k) {
3239 case 1: big_app1(j); break;
3240 case 2: big_app2(j); break;
3241 case 3: big_app3(j); break;
3242 case 4: big_app4(j); break;
3243 default: confusion("squash");
3244 }
3245 reduce(j,k,c,d,n);
3246 }
3247
3248 @ If \.{CWEAVE} is being run in debugging mode, the production numbers and
3249 current stack categories will be printed out when |tracing| is set to |fully|;
3250 a sequence of two or more irreducible scraps will be printed out when
3251 |tracing| is set to |partly|.
3252
3253 @d off 0
3254 @d partly 1
3255 @d fully 2
3256
3257 @<Private...@>=
3258 static int tracing=off; /* can be used to show parsing details */
3259
3260 @ @<Print a snapsh...@>=
3261 if (tracing==fully) {
3262 printf("\n%d:",n);
3263 for (i=scrap_base; i<=lo_ptr; i++) {
3264 putchar(i==pp?'*':' ');
3265 if (i->mathness %4 == yes_math) putchar('+');
3266 else if (i->mathness %4 == no_math) putchar('-');
3267 print_cat(i->cat);
3268 if (i->mathness /4 == yes_math) putchar('+');
3269 else if (i->mathness /4 == no_math) putchar('-');
3270 }
3271 if (hi_ptr<=scrap_ptr) printf("..."); /* indicate that more is coming */
3272 }
3273
3274 @ And here now is the code that applies productions as long as possible.
3275 Before applying the production mechanism, we must make sure
3276 it has good input (at least four scraps, the length of the lhs of the
3277 longest rules), and that there is enough room in the memory arrays
3278 to hold the appended tokens and texts. Here we use a very
3279 conservative test; it's more important to make sure the program
3280 will still work if we change the production rules (within reason)
3281 than to squeeze the last bit of space from the memory arrays.
3282
3283 @d safe_tok_incr 20
3284 @d safe_text_incr 10
3285 @d safe_scrap_incr 10
3286
3287 @<Reduce the scraps using the productions until no more rules apply@>=
3288 while (true) {
3289 @<Make sure the entries |pp| through |pp+3| of |cat| are defined@>@;
3290 if (tok_ptr+safe_tok_incr>tok_mem_end) {
3291 if (tok_ptr>max_tok_ptr) max_tok_ptr=tok_ptr;
3292 overflow("token");
3293 }
3294 if (text_ptr+safe_text_incr>tok_start_end) {
3295 if (text_ptr>max_text_ptr) max_text_ptr=text_ptr;
3296 overflow("text");
3297 }
3298 if (pp>lo_ptr) break;
3299 init_mathness=cur_mathness=maybe_math;
3300 @<Match a production...@>@;
3301 }
3302
3303 @ If we get to the end of the scrap list, category codes equal to zero are
3304 stored, since zero does not match anything in a production.
3305
3306 @<Make sure the entries...@>=
3307 if (lo_ptr<pp+3) {
3308 while (hi_ptr<=scrap_ptr && lo_ptr!=pp+3)
3309 *(++lo_ptr)=*(hi_ptr++);@^system dependencies@>
3310 for (j=lo_ptr+1;j<=pp+3;j++) j->cat=0;
3311 }
3312
3313 @ The |translate| function assumes that scraps have been stored in
3314 positions |scrap_base| through |scrap_ptr| of |cat| and |trans|. It
3315 applies productions as much as
3316 possible. The result is a token list containing the translation of
3317 the given sequence of scraps.
3318
3319 After calling |translate|, we will have |text_ptr+3<=max_texts| and
3320 |tok_ptr+6<=max_toks|, so it will be possible to create up to three token
3321 lists with up to six tokens without checking for overflow. Before calling
3322 |translate|, we should have |text_ptr<max_texts| and |scrap_ptr<max_scraps|,
3323 since |translate| might add a new text and a new scrap before it checks
3324 for overflow.
3325
3326 @c
3327 static text_pointer
3328 translate(void) /* converts a sequence of scraps */
3329 {
3330 scrap_pointer j; /* runs through final scraps */
3331 pp=scrap_base; lo_ptr=pp-1; hi_ptr=pp;
3332 @<If tracing, print an indication of where we are@>@;
3333 @<Reduce the scraps...@>@;
3334 @<Combine the irreducible scraps that remain@>@;
3335 }
3336
3337 @ @<Predecl...@>=@+static text_pointer translate(void);
3338
3339 @ If the initial sequence of scraps does not reduce to a single scrap,
3340 we concatenate the translations of all remaining scraps, separated by
3341 blank spaces, with dollar signs surrounding the translations of scraps
3342 where appropriate.
3343
3344 @<Combine the irreducible...@>=
3345 @<If semi-tracing, show the irreducible scraps@>@;
3346 for (j=scrap_base; j<=lo_ptr; j++) {
3347 if (j!=scrap_base) app(' ');
3348 if (j->mathness % 4 == yes_math) app('$');
3349 app(tok_flag+(int)(j->trans-tok_start));
3350 if (j->mathness / 4 == yes_math) app('$');
3351 if (tok_ptr+6>tok_mem_end) overflow("token");
3352 }
3353 freeze_text(); return text_ptr-1;
3354
3355 @ @<If semi-tracing, show the irreducible scraps@>=
3356 if (lo_ptr>scrap_base && tracing==partly) {
3357 printf("\nIrreducible scrap sequence in section %d:",(int)section_count);
3358 @.Irreducible scrap sequence...@>
3359 mark_harmless();
3360 for (j=scrap_base; j<=lo_ptr; j++) {
3361 putchar(' '); print_cat(j->cat);
3362 }
3363 }
3364
3365 @ @<If tracing,...@>=
3366 if (tracing==fully) {
3367 printf("\nTracing after l. %d:\n",cur_line); mark_harmless();
3368 @.Tracing after...@>
3369 if (loc>buffer+50) {
3370 printf("...");
3371 term_write(loc-51,51);
3372 }
3373 else term_write(buffer,loc-buffer);
3374 }
3375
3376 @* Initializing the scraps.
3377 If we are going to use the powerful production mechanism just developed, we
3378 must get the scraps set up in the first place, given a \CEE/ text. A table
3379 of the initial scraps corresponding to \CEE/ tokens appeared above in the
3380 section on parsing; our goal now is to implement that table. We shall do this
3381 by implementing a subroutine called |C_parse| that is analogous to the
3382 |C_xref| routine used during phase one.
3383
3384 Like |C_xref|, the |C_parse| procedure starts with the current
3385 value of |next_control| and it uses the operation |next_control=get_next()|
3386 repeatedly to read \CEE/ text until encountering the next `\.{\v}' or
3387 `\.{/*}', or until |next_control>=format_code|. The scraps corresponding to
3388 what it reads are appended into the |cat| and |trans| arrays, and |scrap_ptr|
3389 is advanced.
3390
3391 @c
3392 static void
3393 C_parse( /* creates scraps from \CEE/ tokens */
3394 eight_bits spec_ctrl)
3395 {
3396 while (next_control<format_code || next_control==spec_ctrl) {
3397 @<Append the scrap appropriate to |next_control|@>@;
3398 next_control=get_next();
3399 if (next_control=='|' || next_control==begin_comment ||
3400 next_control==begin_short_comment) return;
3401 }
3402 }
3403
3404 @ @<Predecl...@>=@+static void C_parse(eight_bits);
3405
3406 @ The following macro is used to append a scrap whose tokens have just
3407 been appended:
3408
3409 @d app_scrap(c,b) {
3410 (++scrap_ptr)->cat=(c); scrap_ptr->trans=text_ptr;
3411 scrap_ptr->mathness=5*(b); /* no no, yes yes, or maybe maybe */
3412 freeze_text();
3413 }
3414
3415 @ @<Append the scr...@>=
3416 @<Make sure that there is room for the new scraps, tokens, and texts@>@;
3417 switch (next_control) {
3418 case section_name:
3419 app(section_flag+(int)(cur_section-name_dir));
3420 app_scrap(section_scrap,maybe_math);@+
3421 app_scrap(exp,yes_math);@+break;
3422 case string: case constant: case verbatim:
3423 @<Append a \9{s}string or constant@>@;@+break;
3424 case identifier: app_cur_id(true);@+break;
3425 case TeX_string:
3426 @<Append a \9{t}\TEX/ string, without forming a scrap@>@;@+break;
3427 case '/': case '.':
3428 app(next_control);@+app_scrap(binop,yes_math);@+break;
3429 case '<': app_str("\\langle");@+app_scrap(prelangle,yes_math);@+break;
3430 @.\\langle@>
3431 case '>': app_str("\\rangle");@+app_scrap(prerangle,yes_math);@+break;
3432 @.\\rangle@>
3433 case '=': app_str("\\K");@+app_scrap(binop,yes_math);@+break;
3434 @.\\K@>
3435 case '|': app_str("\\OR");@+app_scrap(binop,yes_math);@+break;
3436 @.\\OR@>
3437 case '^': app_str("\\XOR");@+app_scrap(binop,yes_math);@+break;
3438 @.\\XOR@>
3439 case '%': app_str("\\MOD");@+app_scrap(binop,yes_math);@+break;
3440 @.\\MOD@>
3441 case '!': app_str("\\R");@+app_scrap(unop,yes_math);@+break;
3442 @.\\R@>
3443 case '~': app_str("\\CM");@+app_scrap(unop,yes_math);@+break;
3444 @.\\CM@>
3445 case '+': case '-': app(next_control);@+app_scrap(ubinop,yes_math);@+break;
3446 case '*': app(next_control);@+app_scrap(raw_ubin,yes_math);@+break;
3447 case '&': app_str("\\AND");@+app_scrap(raw_ubin,yes_math);@+break;
3448 @.\\AND@>
3449 case '?': app_str("\\?");@+app_scrap(question,yes_math);@+break;
3450 @.\\?@>
3451 case '#': app_str("\\#");@+app_scrap(ubinop,yes_math);@+break;
3452 @.\\\#@>
3453 case ignore: case xref_roman: case xref_wildcard:
3454 case xref_typewriter: case noop:@+break;
3455 case '(': app(next_control);@+app_scrap(lpar,maybe_math);@+break;
3456 case ')': app(next_control);@+app_scrap(rpar,maybe_math);@+break;
3457 case '[': app(next_control);@+app_scrap(lbrack,maybe_math);@+break;
3458 case ']': app(next_control);@+app_scrap(rbrack,maybe_math);@+break;
3459 case '{': app_str("\\{"@q}@>);@+app_scrap(lbrace,yes_math);@+break;
3460 @.\\\{@>@q}@>
3461 case '}': app_str(@q{@>"\\}");@+app_scrap(rbrace,yes_math);@+break;
3462 @q{@>@.\\\}@>
3463 case ',': app(',');@+app_scrap(comma,yes_math);@+break;
3464 case ';': app(';');@+app_scrap(semi,maybe_math);@+break;
3465 case ':': app(':');@+app_scrap(colon,no_math);@+break;@/
3466 @t\4@> @<Cases involving nonstandard characters@>@;
3467 case thin_space: app_str("\\,");@+app_scrap(insert,maybe_math);@+break;
3468 @.\\,@>
3469 case math_break: app(opt);@+app('0');@+
3470 app_scrap(insert,maybe_math);@+break;
3471 case line_break: app(force);@+app_scrap(insert,no_math);@+break;
3472 case left_preproc: app(force);@+app(preproc_line);@+app_str("\\#");
3473 app_scrap(lproc,no_math);@+break;
3474 @.\\\#@>
3475 case right_preproc: app(force);@+app_scrap(rproc,no_math);@+break;
3476 case big_line_break: app(big_force);@+app_scrap(insert,no_math);@+break;
3477 case no_line_break: app(big_cancel);@+app(noop);@+app(break_space);@+
3478 app(noop);@+app(big_cancel);
3479 app_scrap(insert,no_math);@+break;
3480 case pseudo_semi: app_scrap(semi,maybe_math);@+break;
3481 case macro_arg_open: app_scrap(begin_arg,maybe_math);@+break;
3482 case macro_arg_close: app_scrap(end_arg,maybe_math);@+break;
3483 case join: app_str("\\J");@+app_scrap(insert,no_math);@+break;
3484 @.\\J@>
3485 case output_defs_code: app(force);@+app_str("\\ATH");@+app(force);
3486 app_scrap(insert,no_math);@+break;
3487 @.\\ATH@>
3488 default: app(inserted);@+app(next_control);
3489 app_scrap(insert,maybe_math);@+break;
3490 }
3491
3492 @ @<Make sure that there is room for the new...@>=
3493 if (scrap_ptr+safe_scrap_incr>scrap_info_end ||
3494 tok_ptr+safe_tok_incr>tok_mem_end @| ||
3495 text_ptr+safe_text_incr>tok_start_end) {
3496 if (scrap_ptr>max_scr_ptr) max_scr_ptr=scrap_ptr;
3497 if (tok_ptr>max_tok_ptr) max_tok_ptr=tok_ptr;
3498 if (text_ptr>max_text_ptr) max_text_ptr=text_ptr;
3499 overflow("scrap/token/text");
3500 }
3501
3502 @ Some nonstandard characters may have entered \.{CWEAVE} by means of
3503 standard ones. They are converted to \TEX/ control sequences so that it is
3504 possible to keep \.{CWEAVE} from outputting unusual |char| codes.
3505
3506 @<Cases involving nonstandard...@>=@t\1\quad@>
3507 case non_eq: app_str("\\I");@+app_scrap(binop,yes_math);@+break;
3508 @.\\I@>
3509 case lt_eq: app_str("\\Z");@+app_scrap(binop,yes_math);@+break;
3510 @.\\Z@>
3511 case gt_eq: app_str("\\G");@+app_scrap(binop,yes_math);@+break;
3512 @.\\G@>
3513 case eq_eq: app_str("\\E");@+app_scrap(binop,yes_math);@+break;
3514 @.\\E@>
3515 case and_and: app_str("\\W");@+app_scrap(binop,yes_math);@+break;
3516 @.\\W@>
3517 case or_or: app_str("\\V");@+app_scrap(binop,yes_math);@+break;
3518 @.\\V@>
3519 case plus_plus: app_str("\\PP");@+app_scrap(unop,yes_math);@+break;
3520 @.\\PP@>
3521 case minus_minus: app_str("\\MM");@+app_scrap(unop,yes_math);@+break;
3522 @.\\MM@>
3523 case minus_gt: app_str("\\MG");@+app_scrap(binop,yes_math);@+break;
3524 @.\\MG@>
3525 case gt_gt: app_str("\\GG");@+app_scrap(binop,yes_math);@+break;
3526 @.\\GG@>
3527 case lt_lt: app_str("\\LL");@+app_scrap(binop,yes_math);@+break;
3528 @.\\LL@>
3529 case dot_dot_dot: app_str("\\,\\ldots\\,");@+app_scrap(raw_int,yes_math);
3530 @+break;
3531 @.\\,@>
3532 @.\\ldots@>
3533 case colon_colon: app_str("\\DC");@+app_scrap(colcol,maybe_math);@+break;
3534 @.\\DC@>
3535 case period_ast: app_str("\\PA");@+app_scrap(binop,yes_math);@+break;
3536 @.\\PA@>
3537 case minus_gt_ast: app_str("\\MGA");@+app_scrap(binop,yes_math);@+break;
3538 @.\\MGA@>
3539
3540 @ The following code must use |app_tok| instead of |app| in order to
3541 protect against overflow. Note that |tok_ptr+1<=max_toks| after |app_tok|
3542 has been used, so another |app| is legitimate before testing again.
3543
3544 Many of the special characters in a string must be prefixed by `\.\\' so that
3545 \TEX/ will print them properly.
3546 @^special string characters@>
3547
3548 @<Append a \9{s}string or...@>={@+ int count=-1; /* characters remaining before string break */
3549 switch (next_control) {
3550 case constant: app_str("\\T{"@q}@>); break;
3551 @.\\T@>
3552 case string: count=20; app_str("\\.{"@q}@>); break;
3553 @.\\.@>
3554 default: app_str("\\vb{"@q}@>);
3555 @.\\vb@>
3556 }
3557 while (id_first<id_loc) {
3558 if (count==0) { /* insert a discretionary break in a long string */
3559 app_str(@q(@>@q{@>"}\\)\\.{"@q}@>); count=20;
3560 @q(@>@.\\)@>
3561 }
3562 switch (*id_first) {
3563 case ' ':case '\\':case '#':case '$':case '^':case '{':case '}':
3564 case '~':case '&':case '_': app('\\'); break;
3565 @.\\\ @>
3566 @.\\\\@>
3567 @.\\\#@>
3568 @.\\\$@>
3569 @.\\\^@>
3570 @.\\\{@>@q}@>
3571 @q{@>@.\\\}@>
3572 @.\\\~@>
3573 @.\\\&@>
3574 @.\\\_@>
3575 case '%': if (next_control==constant) {
3576 app_str("}\\p{"); /* special macro for `hex exponent' */
3577 id_first++; /* skip |'%'| */
3578 }
3579 else app('\\');
3580 break;
3581 @.\\p@>
3582 @.\\\%@>
3583 case '@@': if (*(id_first+1)=='@@') id_first++;
3584 else err_print("! Double @@ should be used in strings");
3585 @.Double @@ should be used...@>
3586 break;
3587 default: /* high-bit character handling */
3588 @^high-bit character handling@>
3589 if((eight_bits)(*id_first)>0177)
3590 app_tok(quoted_char)@t;@>@;
3591 }
3592 app_tok(*id_first++);
3593 count--;
3594 }
3595 app(@q{@>'}');
3596 app_scrap(exp,maybe_math);
3597 }
3598
3599 @ We do not make the \TEX/ string into a scrap, because there is no
3600 telling what the user will be putting into it; instead we leave it
3601 open, to be picked up by the next scrap. If it comes at the end of a
3602 section, it will be made into a scrap when |finish_C| is called.
3603
3604 There's a known bug here, in cases where an adjacent scrap is
3605 |prelangle| or |prerangle|. Then the \TEX/ string can disappear
3606 when the \.{\\langle} or \.{\\rangle} becomes \.{<} or \.{>}.
3607 For example, if the user writes \.{\v x<@@ty@@><42\v}, the \TEX/ string
3608 \.{\\hbox\{y\}} eventually becomes part of an |insert| scrap, which is combined
3609 with a |prelangle| scrap and eventually lost. The best way to work around
3610 this bug is probably to enclose the \.{@@t...@@>} in \.{@@[...@@]} so that
3611 the \TEX/ string is treated as an expression.
3612 @^bug, known@>
3613
3614 @<Append a \9{t}\TEX/ string, without forming a scrap@>=
3615 app_str("\\hbox{"@q}@>);
3616 @^high-bit character handling@>
3617 while (id_first<id_loc) {
3618 if((eight_bits)(*id_first)>0177)
3619 app_tok(quoted_char)@t;@>@;
3620 else
3621 if (*id_first=='@@') id_first++;
3622 app_tok(*id_first++);
3623 }
3624 app(@q{@>'}');
3625
3626 @ The function |app_cur_id| appends the current identifier to the
3627 token list; it also builds a new scrap if |scrapping==true|.
3628
3629 @<Predec...@>=
3630 static void app_cur_id(bool);@/
3631 static text_pointer C_translate(void);@/
3632 static void outer_parse(void);
3633
3634 @ @c
3635 static void
3636 app_cur_id(
3637 bool scrapping) /* are we making this into a scrap? */
3638 {
3639 name_pointer p=id_lookup(id_first,id_loc,normal);
3640 if (p->ilk<=custom) { /* not a reserved word */
3641 app(id_flag+(int)(p-name_dir));
3642 if (scrapping) app_scrap(p->ilk==func_template? ftemplate: exp,
3643 p->ilk==custom? yes_math: maybe_math);
3644 @.\\NULL@>
3645 } else {
3646 app(res_flag+(int)(p-name_dir));
3647 if (scrapping) {
3648 if (p->ilk==alfop) app_scrap(ubinop,yes_math)@t;@>@;
3649 else app_scrap(p->ilk,maybe_math);
3650 }
3651 }
3652 }
3653
3654 @ When the `\.{\v}' that introduces \CEE/ text is sensed, a call on
3655 |C_translate| will return a pointer to the \TEX/ translation of
3656 that text. If scraps exist in |scrap_info|, they are
3657 unaffected by this translation process.
3658
3659 @c
3660 static text_pointer
3661 C_translate(void)
3662 {
3663 text_pointer p; /* points to the translation */
3664 scrap_pointer save_base=scrap_base; /* holds original value of |scrap_base| */
3665 scrap_base=scrap_ptr+1;
3666 C_parse(section_name); /* get the scraps together */
3667 if (next_control!='|') err_print("! Missing '|' after C text");
3668 @.Missing '|'...@>
3669 app_tok(cancel); app_scrap(insert,maybe_math);
3670 /* place a |cancel| token as a final ``comment'' */
3671 p=translate(); /* make the translation */
3672 if (scrap_ptr>max_scr_ptr) max_scr_ptr=scrap_ptr;
3673 scrap_ptr=scrap_base-1; scrap_base=save_base; /* scrap the scraps */
3674 return p;
3675 }
3676
3677 @ The |outer_parse| routine is to |C_parse| as |outer_xref|
3678 is to |C_xref|: It constructs a sequence of scraps for \CEE/ text
3679 until |next_control>=format_code|. Thus, it takes care of embedded comments.
3680
3681 The token list created from within `\pb' brackets is output as an argument
3682 to \.{\\PB}. Although \.{cwebmac} ignores \.{\\PB}, other macro packages
3683 might use it to localize the special meaning of the macros that mark up
3684 program text.
3685
3686 @d make_pb flags['e']
3687
3688 @<Set init...@>=
3689 make_pb=true;
3690
3691 @ @c
3692 static void
3693 outer_parse(void) /* makes scraps from \CEE/ tokens and comments */
3694 {
3695 int bal; /* brace level in comment */
3696 text_pointer p, q; /* partial comments */
3697 while (next_control<format_code)
3698 if (next_control!=begin_comment && next_control!=begin_short_comment)
3699 C_parse(ignore);
3700 else {
3701 bool is_long_comment=(next_control==begin_comment);
3702 @<Make sure that there is room for the new...@>@;
3703 app(cancel); app(inserted);
3704 if (is_long_comment) app_str("\\C{"@q}@>);
3705 @.\\C@>
3706 else app_str("\\SHC{"@q}@>);
3707 @.\\SHC@>
3708 bal=copy_comment(is_long_comment,1); next_control=ignore;
3709 while (bal>0) {
3710 p=text_ptr; freeze_text(); q=C_translate();
3711 /* at this point we have |tok_ptr+6<=max_toks| */
3712 app(tok_flag+(int)(p-tok_start));
3713 if (make_pb) app_str("\\PB{");
3714 @.\\PB@>
3715 app(inner_tok_flag+(int)(q-tok_start));
3716 if (make_pb) app_tok('}');
3717 if (next_control=='|') {
3718 bal=copy_comment(is_long_comment,bal);
3719 next_control=ignore;
3720 }
3721 else bal=0; /* an error has been reported */
3722 }
3723 app(force); app_scrap(insert,no_math);
3724 /* the full comment becomes a scrap */
3725 }
3726 }
3727
3728 @* Output of tokens.
3729 So far our programs have only built up multi-layered token lists in
3730 \.{CWEAVE}'s internal memory; we have to figure out how to get them into
3731 the desired final form. The job of converting token lists to characters in
3732 the \TEX/ output file is not difficult, although it is an implicitly
3733 recursive process. Four main considerations had to be kept in mind when
3734 this part of \.{CWEAVE} was designed. (a) There are two modes of output:
3735 |outer| mode, which translates tokens like |force| into line-breaking
3736 control sequences, and |inner| mode, which ignores them except that blank
3737 spaces take the place of line breaks. (b) The |cancel| instruction applies
3738 to adjacent token or tokens that are output, and this cuts across levels
3739 of recursion since `|cancel|' occurs at the beginning or end of a token
3740 list on one level. (c) The \TEX/ output file will be semi-readable if line
3741 breaks are inserted after the result of tokens like |break_space| and
3742 |force|. (d) The final line break should be suppressed, and there should
3743 be no |force| token output immediately after `\.{\\Y\\B}'.
3744
3745 @ The output process uses a stack to keep track of what is going on at
3746 different ``levels'' as the token lists are being written out. Entries on
3747 this stack have three parts:
3748
3749 \yskip\hang |end_field| is the |tok_mem| location where the token list of a
3750 particular level will end;
3751
3752 \yskip\hang |tok_field| is the |tok_mem| location from which the next token
3753 on a particular level will be read;
3754
3755 \yskip\hang |mode_field| is the current mode, either |inner| or |outer|.
3756
3757 \yskip\noindent The current values of these quantities are referred to
3758 quite frequently, so they are stored in an extra slot at the very end of
3759 the |stack| array. We call the current values |cur_end|, |cur_tok|, and
3760 |cur_mode|.
3761
3762 The global variable |stack_ptr| tells how many levels of output are
3763 currently in progress. The end of output occurs when an |end_translation|
3764 token is found, so the stack is never empty except when we first begin the
3765 output process.
3766
3767 @s mode int
3768 @<Typed...@>=
3769 typedef enum {
3770 @!inner, /* value of |mode| for \CEE/ texts within \TEX/ texts */
3771 @!outer /* value of |mode| for \CEE/ texts in sections */
3772 } mode;
3773 typedef struct {
3774 token_pointer end_field; /* ending location of token list */
3775 token_pointer tok_field; /* present location within token list */
3776 mode mode_field; /* interpretation of control tokens */
3777 } output_state;
3778 typedef output_state *stack_pointer;
3779
3780 @ @d stack_size 2000 /* number of simultaneous output levels */
3781 @d cur_state stack[stack_size] /* |cur_end|, |cur_tok|, |cur_mode| */
3782 @d cur_end cur_state.end_field /* current ending location in |tok_mem| */
3783 @d cur_tok cur_state.tok_field /* location of next output token in |tok_mem| */
3784 @d cur_mode cur_state.mode_field /* current mode of interpretation */
3785 @d init_stack() stack_ptr=stack;cur_mode=outer /* initialize the stack */
3786
3787 @<Private...@>=
3788 static output_state stack[stack_size+1]; /* info for non-current levels */
3789 static stack_pointer stack_end=stack+stack_size-1; /* end of |stack| */
3790 static stack_pointer stack_ptr; /* first unused location in the output state stack */
3791 static stack_pointer max_stack_ptr; /* largest value assumed by |stack_ptr| */
3792
3793 @ @<Set init...@>=
3794 max_stack_ptr=stack;
3795
3796 @ @<Predecl...@>=
3797 static void push_level(text_pointer);
3798
3799 @ To insert token-list |p| into the output, the |push_level| subroutine
3800 is called; it saves the old level of output and gets a new one going.
3801 The value of |cur_mode| is not changed.
3802
3803 @c
3804 static void
3805 push_level( /* suspends the current level */
3806 text_pointer p)
3807 {
3808 if (stack_ptr==stack_end) overflow("stack");
3809 if (stack_ptr>stack) /* save current state */
3810 *stack_ptr = cur_state;@^system dependencies@>
3811 stack_ptr++;
3812 if (stack_ptr>max_stack_ptr) max_stack_ptr=stack_ptr;
3813 cur_tok=*p; cur_end=*(p+1);
3814 }
3815
3816 @ Conversely, the |pop_level| routine restores the conditions that were in
3817 force when the current level was begun. This subroutine will never be
3818 called when |stack_ptr==1|. It is so simple, we declare it as a macro:
3819
3820 @d pop_level() cur_state = *(--stack_ptr)@^system dependencies@>
3821
3822 @ The |get_output| function returns the next byte of output that is not a
3823 reference to a token list. It returns the values |identifier| or |res_word|
3824 or |section_code| if the next token is to be an identifier (typeset in
3825 italics), a reserved word (typeset in boldface), or a section name (typeset
3826 by a complex routine that might generate additional levels of output).
3827 In these cases |cur_name| points to the identifier or section name in
3828 question.
3829
3830 @<Private...@>=
3831 static name_pointer cur_name;
3832
3833 @ @d res_word 0201 /* returned by |get_output| for reserved words */
3834 @d section_code 0200 /* returned by |get_output| for section names */
3835
3836 @<Predecl...@>=
3837 static eight_bits get_output(void);@/
3838 static void output_C(void);@/
3839 static void make_output(void);
3840
3841 @ @c
3842 static eight_bits
3843 get_output(void) /* returns the next token of output */
3844 {
3845 sixteen_bits a; /* current item read from |tok_mem| */
3846 restart: while (cur_tok==cur_end) pop_level();
3847 a=*(cur_tok++);
3848 if (a>=0400) {
3849 cur_name=a % id_flag + name_dir;
3850 switch (a / id_flag) {
3851 case 2: return res_word ; /* |a==res_flag+cur_name| */
3852 case 3: return section_code ; /* |a==section_flag+cur_name| */
3853 case 4: push_level(a % id_flag + tok_start); goto restart;
3854 /* |a==tok_flag+cur_name| */
3855 case 5: push_level(a % id_flag + tok_start); cur_mode=inner; goto restart;
3856 /* |a==inner_tok_flag+cur_name| */
3857 default: return identifier; /* |a==id_flag+cur_name| */
3858 }
3859 }
3860 return (eight_bits)a;
3861 }
3862
3863 @ The real work associated with token output is done by |make_output|.
3864 This procedure appends an |end_translation| token to the current token list,
3865 and then it repeatedly calls |get_output| and feeds characters to the output
3866 buffer until reaching the |end_translation| sentinel. It is possible for
3867 |make_output| to be called recursively, since a section name may include
3868 embedded \CEE/ text; however, the depth of recursion never exceeds one
3869 level, since section names cannot be inside of section names.
3870
3871 A procedure called |output_C| does the scanning, translation, and
3872 output of \CEE/ text within `\pb' brackets, and this procedure uses
3873 |make_output| to output the current token list. Thus, the recursive call
3874 of |make_output| actually occurs when |make_output| calls |output_C|
3875 while outputting the name of a section.
3876 @^recursion@>
3877
3878 @c
3879 static void
3880 output_C(void) /* outputs the current token list */
3881 {
3882 token_pointer save_tok_ptr=tok_ptr;
3883 text_pointer save_text_ptr=text_ptr;
3884 sixteen_bits save_next_control=next_control; /* values to be restored */
3885 text_pointer p; /* translation of the \CEE/ text */
3886 next_control=ignore; p=C_translate();
3887 app(inner_tok_flag+(int)(p-tok_start));
3888 if (make_pb) out_str("\\PB{");
3889 @.\\PB@>
3890 make_output(); /* output the list */
3891 if (make_pb) out('}');
3892 if (text_ptr>max_text_ptr) max_text_ptr=text_ptr;
3893 if (tok_ptr>max_tok_ptr) max_tok_ptr=tok_ptr;
3894 text_ptr=save_text_ptr; tok_ptr=save_tok_ptr; /* forget the tokens */
3895 next_control=save_next_control; /* restore |next_control| to original state */
3896 }
3897
3898 @ Here is \.{CWEAVE}'s major output handler.
3899
3900 @c
3901 static void
3902 make_output(void) /* outputs the equivalents of tokens */
3903 {
3904 eight_bits a=0; /* current output byte */
3905 eight_bits b; /* next output byte */
3906 int c; /* count of |indent| and |outdent| tokens */
3907 char scratch[longest_name+1]; /* scratch area for section names */
3908 char *k, *k_limit; /* indices into |scratch| */
3909 char *j; /* index into |buffer| */
3910 char *p; /* index into |byte_mem| */
3911 char delim; /* first and last character of string being copied */
3912 char *save_loc, *save_limit; /* |loc| and |limit| to be restored */
3913 name_pointer cur_section_name; /* name of section being output */
3914 bool save_mode; /* value of |cur_mode| before a sequence of breaks */
3915 bool dindent_pending=false; /* should a |dindent| be output? */
3916 app(end_translation); /* append a sentinel */
3917 freeze_text(); push_level(text_ptr-1);
3918 while (true) {
3919 a=get_output();
3920 reswitch: switch(a) {
3921 case end_translation: return;
3922 case identifier: case res_word: @<Output an identifier@>@; break;
3923 case section_code: @<Output a section name@>@; break;
3924 case math_rel: out_str("\\MRL{"@q}@>); @=/* fall through */@>@;
3925 @.\\MRL@>
3926 case noop: case inserted: break;
3927 case cancel: case big_cancel: c=0; b=a;
3928 while (true) {
3929 a=get_output();
3930 if (a==inserted) continue;
3931 if ((a<indent && !(b==big_cancel&&a==' ')) @|
3932 || (a>big_force && a!=dindent)) break;
3933 switch (a) {
3934 case dindent: c++; @=/* fall through */@>@;
3935 case indent: c++; break;
3936 case outdent: c--; break;
3937 case opt: a=get_output();
3938 }
3939 }
3940 @<Output saved |indent| or |outdent| tokens@>@;
3941 goto reswitch;
3942 case dindent: a=get_output();
3943 if (a!=big_force) {
3944 out_str("\\1\\1"); goto reswitch;
3945 }
3946 else dindent_pending=true; @=/* fall through */@>@;
3947 case indent: case outdent: case opt: case backup: case break_space:
3948 case force: case big_force: case preproc_line: @<Output a control,
3949 look ahead in case of line breaks, possibly |goto reswitch|@>@; break;
3950 case quoted_char: out(*(cur_tok++)); @=/* fall through */@>@;
3951 case qualifier: break;
3952 default: out(a); /* otherwise |a| is an ordinary character */
3953 }
3954 }
3955 }
3956
3957 @ An identifier of length one does not have to be enclosed in braces, and it
3958 looks slightly better if set in a math-italic font instead of a (slightly
3959 narrower) text-italic font. Thus we output `\.{\\\v}\.{a}' but
3960 `\.{\\\\\{aa\}}'.
3961
3962 @<Output an identifier@>=
3963 out('\\');
3964 if (a==identifier) {
3965 if (cur_name->ilk==custom && !doing_format) {
3966 custom_out:
3967 for (p=cur_name->byte_start;p<(cur_name+1)->byte_start;p++)
3968 out(*p=='_'? 'x': *p=='$'? 'X': *p);
3969 break;
3970 }
3971 else if (is_tiny(cur_name)) out('|')@t;@>@;
3972 @.\\|@>
3973 else { delim='.';
3974 for (p=cur_name->byte_start;p<(cur_name+1)->byte_start;p++)
3975 if (xislower(*p)) { /* not entirely uppercase */
3976 delim='\\'; break;
3977 }
3978 out(delim);
3979 }
3980 @.\\\\@>
3981 @.\\.@>
3982 }
3983 else if (cur_name->ilk==alfop) {
3984 out('X');
3985 goto custom_out;
3986 }
3987 else out('&'); /* |a==res_word| */
3988 @.\\\&@>
3989 if (is_tiny(cur_name)) {
3990 if (isxalpha((cur_name->byte_start)[0]))
3991 out('\\');
3992 out((cur_name->byte_start)[0]);
3993 }
3994 else out_name(cur_name,true);
3995
3996 @ The current mode does not affect the behavior of \.{CWEAVE}'s output routine
3997 except when we are outputting control tokens.
3998
3999 @<Output a control...@>=
4000 if (a<break_space || a==preproc_line) {
4001 if (cur_mode==outer) {
4002 out('\\'); out(a-cancel+'0');
4003 @.\\1@>
4004 @.\\2@>
4005 @.\\3@>
4006 @.\\4@>
4007 @.\\8@>
4008 if (a==opt) {
4009 b=get_output(); /* |opt| is followed by a digit */
4010 if (b!='0' || force_lines==false) out(b)@t;@>@;
4011 else out_str("{-1}"); /* |force_lines| encourages more \.{@@\v} breaks */
4012 }
4013 } else if (a==opt) b=get_output(); /* ignore digit following |opt| */
4014 }
4015 else @<Look ahead for strongest line break, |goto reswitch|@>
4016
4017 @ If several of the tokens |break_space|, |force|, |big_force| occur in a
4018 row, possibly mixed with blank spaces (which are ignored),
4019 the largest one is used. A line break also occurs in the output file,
4020 except at the very end of the translation. The very first line break
4021 is suppressed (i.e., a line break that follows `\.{\\Y\\B}').
4022
4023 @<Look ahead for st...@>= {
4024 b=a; save_mode=cur_mode;
4025 if (dindent_pending) {
4026 c=2; dindent_pending=false;
4027 } else c=0;
4028 while (true) {
4029 a=get_output();
4030 if (a==inserted) continue;
4031 if (a==cancel || a==big_cancel) {
4032 @<Output saved |indent| or |outdent| tokens@>@;
4033 goto reswitch; /* |cancel| overrides everything */
4034 }
4035 if ((a!=' ' && a<indent) || a==backup || a>big_force) {
4036 if (save_mode==outer) {
4037 if (out_ptr>out_buf+3 && strncmp(out_ptr-3,"\\Y\\B",4)==0)
4038 goto reswitch;
4039 @<Output saved |indent| or |outdent| tokens@>@;
4040 out('\\'); out(b-cancel+'0');
4041 @.\\5@>
4042 @.\\6@>
4043 @.\\7@>
4044 if (a!=end_translation) finish_line();
4045 }
4046 else if (a!=end_translation && cur_mode==inner) out(' ');
4047 goto reswitch;
4048 }
4049 if (a==indent) c++;
4050 else if (a==outdent) c--;
4051 else if (a==opt) a=get_output();
4052 else if (a>b) b=a; /* if |a==' '| we have |a<b| */
4053 }
4054 }
4055
4056 @ @<Output saved...@>=
4057 for (;c>0;c--) out_str("\\1");
4058 @.\\1@>
4059 for (;c<0;c++) out_str("\\2");
4060 @.\\2@>
4061
4062 @ The remaining part of |make_output| is somewhat more complicated. When we
4063 output a section name, we may need to enter the parsing and translation
4064 routines, since the name may contain \CEE/ code embedded in
4065 \pb\ constructions. This \CEE/ code is placed at the end of the active
4066 input buffer and the translation process uses the end of the active
4067 |tok_mem| area.
4068
4069 @<Output a section name@>=
4070 out_str("\\X");
4071 @.\\X@>
4072 cur_xref=(xref_pointer)cur_name->xref;
4073 if ((an_output=(cur_xref->num==file_flag))==true) cur_xref=cur_xref->xlink;
4074 if (cur_xref->num>=def_flag) {
4075 out_section(cur_xref->num-def_flag);
4076 if (phase==3) {
4077 cur_xref=cur_xref->xlink;
4078 while (cur_xref->num>=def_flag) {
4079 out_str(", ");
4080 out_section(cur_xref->num-def_flag);
4081 cur_xref=cur_xref->xlink;
4082 }
4083 }
4084 }
4085 else out('0'); /* output the section number, or zero if it was undefined */
4086 out(':');
4087 if (an_output) out_str("\\.{"@q}@>);
4088 @.\\.@>
4089 @<Output the text of the section name@>@;
4090 if (an_output) cur_mode==inner?out_str(@q{@>"}"):out_str(@q{@>"\\,}");
4091 out_str("\\X");
4092
4093 @ @<Output the text...@>=
4094 sprint_section_name(scratch,cur_name);
4095 k=scratch;
4096 k_limit=scratch+strlen(scratch);
4097 cur_section_name=cur_name;
4098 while (k<k_limit) {
4099 b=*(k++);
4100 if (b=='@@') @<Skip next character, give error if not `\.{@@}'@>@;
4101 if (an_output)
4102 switch (b) {
4103 case ' ': case '\\': case '#': case '%': case '$': case '^':
4104 case '{': case '}': case '~': case '&': case '_':
4105 out('\\'); @=/* falls through */@>@;
4106 @.\\\ @>
4107 @.\\\\@>
4108 @.\\\#@>
4109 @.\\\%@>
4110 @.\\\$@>
4111 @.\\\^@>
4112 @.\\\{@>@q}@>
4113 @q{@>@.\\\}@>
4114 @.\\\~@>
4115 @.\\\&@>
4116 @.\\\_@>
4117 default: out(b);
4118 }
4119 else
4120 if (b!='|') out(b)@t;@>@;
4121 else {
4122 @<Copy the \CEE/ text into the |buffer| array@>@;
4123 save_loc=loc; save_limit=limit; loc=limit+2; limit=j+1;
4124 *limit='|'; output_C();
4125 loc=save_loc; limit=save_limit;
4126 }
4127 }
4128
4129 @ @<Skip next char...@>=
4130 if (*k++!='@@') {
4131 printf("%s","\n! Illegal control code in section name: <");
4132 @.Illegal control code...@>
4133 print_section_name(cur_section_name); printf("> "); mark_error();
4134 }
4135
4136 @ The \CEE/ text enclosed in \pb\ should not contain `\.{\v}' characters,
4137 except within strings. We put a `\.{\v}' at the front of the buffer, so that an
4138 error message that displays the whole buffer will look a little bit sensible.
4139 The variable |delim| is zero outside of strings, otherwise it
4140 equals the delimiter that began the string being copied.
4141
4142 @<Copy the \CEE/ text into...@>=
4143 j=limit+1; *j='|'; delim=0;
4144 while (true) {
4145 if (k>=k_limit) {
4146 printf("%s","\n! C text in section name didn't end: <");
4147 @.C text...didn't end@>
4148 print_section_name(cur_section_name); printf("> "); mark_error(); break;
4149 }
4150 b=*(k++);
4151 if (b=='@@' || (b=='\\' && delim!=0))
4152 @<Copy a quoted character into the buffer@>@;
4153 else {
4154 if (b=='\'' || b=='"') {
4155 if (delim==0) delim=b;
4156 else if (delim==b) delim=0; }
4157 if (b!='|' || delim!=0) {
4158 if (j>buffer+long_buf_size-3) overflow("buffer");
4159 *(++j)=b;
4160 }
4161 else break;
4162 }
4163 }
4164
4165 @ @<Copy a quoted char...@>= {
4166 if (j>buffer+long_buf_size-4) overflow("buffer");
4167 *(++j)=b; *(++j)=*(k++);
4168 }
4169
4170 @** Phase two processing.
4171 We have assembled enough pieces of the puzzle in order to be ready to specify
4172 the processing in \.{CWEAVE}'s main pass over the source file. Phase two
4173 is analogous to phase one, except that more work is involved because we must
4174 actually output the \TEX/ material instead of merely looking at the
4175 \.{CWEB} specifications.
4176
4177 @c
4178 static void
4179 phase_two(void) {
4180 phase=2; reset_input();
4181 if (show_progress) printf("%s","\nWriting the output file...");
4182 @.Writing the output file...@>
4183 section_count=0; format_visible=true; copy_limbo();
4184 finish_line(); flush_buffer(out_buf,false,false);
4185 /* insert a blank line, it looks nice */
4186 while (!input_has_ended) @<Translate the \9{c}current section@>@;
4187 }
4188
4189 @ @<Predecl...@>=@+static void phase_two(void);
4190
4191 @ The output file will contain the control sequence \.{\\Y} between non-null
4192 sections of a section, e.g., between the \TEX/ and definition parts if both
4193 are nonempty. This puts a little white space between the parts when they are
4194 printed. However, we don't want \.{\\Y} to occur between two definitions
4195 within a single section. The variables |out_line| or |out_ptr| will
4196 change if a section is non-null, so the following macros `|save_position|'
4197 and `|emit_space_if_needed|' are able to handle the situation:
4198
4199 @d save_position() save_line=out_line; save_place=out_ptr
4200 @d emit_space_if_needed() if (save_line!=out_line || save_place!=out_ptr)
4201 out_str("\\Y");
4202 space_checked=true;
4203 @.\\Y@>
4204
4205 @<Private...@>=
4206 static int save_line; /* former value of |out_line| */
4207 static char *save_place; /* former value of |out_ptr| */
4208 static int sec_depth; /* the integer, if any, following \.{@@*} */
4209 static bool space_checked; /* have we done |emit_space_if_needed|? */
4210 static bool format_visible; /* should the next format declaration be output? */
4211 static bool doing_format=false; /* are we outputting a format declaration? */
4212 static bool group_found=false; /* has a starred section occurred? */
4213
4214 @ @<Translate the \9{c}current section@>= {
4215 section_count++;
4216 @<Output the code for the beginning of a new section@>@;
4217 save_position();
4218 @<Translate the \9{t}\TEX/ part of the current section@>@;
4219 @<Translate the \9{d}definition part of the current section@>@;
4220 @<Translate the \9{c}\CEE/ part of the current section@>@;
4221 @<Show cross-references to this section@>@;
4222 @<Output the code for the end of a section@>@;
4223 }
4224
4225 @ Sections beginning with the \.{CWEB} control sequence `\.{@@\ }' start in the
4226 output with the \TEX/ control sequence `\.{\\M}', followed by the section
4227 number. Similarly, `\.{@@*}' sections lead to the control sequence `\.{\\N}'.
4228 In this case there's an additional parameter, representing one plus the
4229 specified depth, immediately after the~\.{\\N}.
4230 If the section has changed, we put \.{\\*} just after the section number.
4231
4232 @<Output the code for the beginning...@>=
4233 if (*(loc-1)!='*') out_str("\\M");
4234 @.\\M@>
4235 else {
4236 while (*loc == ' ') loc++;
4237 if (*loc=='*') { /* ``top'' level */
4238 sec_depth = -1;
4239 loc++;
4240 }
4241 else {
4242 for (sec_depth=0; xisdigit(*loc);loc++)@^system dependencies@>
4243 if (sec_depth < INT_MAX / 10) sec_depth = sec_depth*10 + (*loc) -'0';
4244 }
4245 while (*loc == ' ') loc++; /* remove spaces before group title */
4246 group_found=true;
4247 out_str("\\N");
4248 @.\\N@>
4249 {@+ char s[32];@+snprintf(s,32,"{%d}",sec_depth+1);@+out_str(s);@+}
4250 if (show_progress) {
4251 printf("*%d",(int)section_count);
4252 update_terminal(); /* print a progress report */
4253 }
4254 }
4255 out('{'); out_section(section_count); out('}');
4256
4257 @ In the \TEX/ part of a section, we simply copy the source text, except that
4258 index entries are not copied and \CEE/ text within \pb\ is translated.
4259
4260 @<Translate the \9{t}\T...@>= do
4261 switch (next_control=copy_TeX()) {
4262 case '|': init_stack(); output_C(); break;
4263 case '@@': out('@@'); break;
4264 case TeX_string: case verbatim: case noop:
4265 case xref_roman: case xref_wildcard: case xref_typewriter:
4266 case section_name: loc-=2; next_control=get_next(); /* skip to \.{@@>} */
4267 if (next_control==TeX_string)
4268 err_print("! TeX string should be in C text only");
4269 @.TeX string should be...@>
4270 if (next_control==verbatim)
4271 err_print("! Verbatim string should be in C text only"); break;
4272 @.Verbatim string should be...@>
4273 case thin_space: case math_break: case ord:
4274 case line_break: case big_line_break: case no_line_break: case join:
4275 case pseudo_semi: case macro_arg_open: case macro_arg_close:
4276 case output_defs_code:
4277 err_print("! You can't do that in TeX text"); break;
4278 @.You can't do that...@>
4279 }
4280 while (next_control<format_code);
4281
4282 @ When we get to the following code we have |next_control>=format_code|, and
4283 the token memory is in its initial empty state.
4284
4285 @<Translate the \9{d}d...@>=
4286 space_checked=false;
4287 while (next_control<=definition) { /* |format_code| or |definition| */
4288 init_stack();
4289 if (next_control==definition) @<Start \9{a}a macro definition@>@;
4290 else @<Start \9{a}a format definition@>@;
4291 outer_parse(); finish_C(format_visible); format_visible=true;
4292 doing_format=false;
4293 }
4294
4295 @ The |finish_C| procedure outputs the translation of the current
4296 scraps, preceded by the control sequence `\.{\\B}' and followed by the
4297 control sequence `\.{\\par}'. It also restores the token and scrap
4298 memories to their initial empty state.
4299
4300 A |force| token is appended to the current scraps before translation
4301 takes place, so that the translation will normally end with \.{\\6} or
4302 \.{\\7} (the \TEX/ macros for |force| and |big_force|). This \.{\\6} or
4303 \.{\\7} is replaced by the concluding \.{\\par} or by \.{\\Y\\par}.
4304
4305 @c
4306 static void
4307 finish_C( /* finishes a definition or a \CEE/ part */
4308 bool visible) /* |true| if we should produce \TeX\ output */
4309 {
4310 text_pointer p; /* translation of the scraps */
4311 if (visible) {
4312 out_str("\\B"); app_tok(force); app_scrap(insert,no_math);
4313 p=translate();
4314 @.\\B@>
4315 app(tok_flag+(int)(p-tok_start)); make_output(); /* output the list */
4316 if (out_ptr>out_buf+1)
4317 if (*(out_ptr-1)=='\\') {
4318 @.\\6@>
4319 @.\\7@>
4320 @.\\Y@>
4321 if (*out_ptr=='6') out_ptr-=2;
4322 else if (*out_ptr=='7') *out_ptr='Y';
4323 }
4324 out_str("\\par"); finish_line();
4325 }
4326 if (text_ptr>max_text_ptr) max_text_ptr=text_ptr;
4327 if (tok_ptr>max_tok_ptr) max_tok_ptr=tok_ptr;
4328 if (scrap_ptr>max_scr_ptr) max_scr_ptr=scrap_ptr;
4329 tok_ptr=tok_mem+1; text_ptr=tok_start+1; scrap_ptr=scrap_info;
4330 /* forget the tokens and the scraps */
4331 }
4332
4333 @ @<Predecl...@>=@+static void finish_C(bool);
4334
4335 @ Keeping in line with the conventions of the \CEE/ preprocessor (and
4336 otherwise contrary to the rules of \.{CWEB}) we distinguish here
4337 between the case that `\.(' immediately follows an identifier and the
4338 case that the two are separated by a space. In the latter case, and
4339 if the identifier is not followed by `\.(' at all, the replacement
4340 text starts immediately after the identifier. In the former case,
4341 it starts after we scan the matching `\.)'.
4342
4343 @<Start \9{a}a macro...@>= {
4344 if (save_line!=out_line || save_place!=out_ptr || space_checked) app(backup);
4345 if(!space_checked){emit_space_if_needed();save_position();}
4346 app_str("\\D"); /* this will produce `\#\&{define }' */
4347 @.\\D@>
4348 if ((next_control=get_next())!=identifier)
4349 err_print("! Improper macro definition");
4350 @.Improper macro definition@>
4351 else {
4352 app('$'); app_cur_id(false);
4353 if (*loc=='(') {
4354 reswitch: switch (next_control=get_next()) {
4355 case '(': case ',': app(next_control); goto reswitch;
4356 case identifier: app_cur_id(false); goto reswitch;
4357 case ')': app(next_control); next_control=get_next(); break;
4358 case dot_dot_dot: app_str("\\,\\ldots\\,"); @.\\,@> @.\\ldots@>
4359 app_scrap(raw_int,no_math);
4360 if ((next_control=get_next())==')') {
4361 app(next_control); next_control=get_next(); break;
4362 } @=/* otherwise fall through */@>@;
4363 default: err_print("! Improper macro definition"); break;
4364 }
4365 }
4366 else next_control=get_next();
4367 app_str("$ "); app(break_space);
4368 app_scrap(dead,no_math); /* scrap won't take part in the parsing */
4369 }
4370 }
4371
4372 @ @<Start \9{a}a format...@>= {
4373 doing_format=true;
4374 if(*(loc-1)=='s' || *(loc-1)=='S') format_visible=false;
4375 if(!space_checked){emit_space_if_needed();save_position();}
4376 app_str("\\F"); /* this will produce `\&{format }' */
4377 @.\\F@>
4378 next_control=get_next();
4379 if (next_control==identifier) {
4380 app(id_flag+(int)(id_lookup(id_first, id_loc,normal)-name_dir));
4381 app(break_space); /* this is syntactically separate from what follows */
4382 next_control=get_next();
4383 if (next_control==identifier) {
4384 app(id_flag+(int)(id_lookup(id_first, id_loc,normal)-name_dir));
4385 app_scrap(exp,maybe_math); app_scrap(semi,maybe_math);
4386 next_control=get_next();
4387 }
4388 }
4389 if (scrap_ptr!=scrap_info+2) err_print("! Improper format definition");
4390 @.Improper format definition@>
4391 }
4392
4393 @ Finally, when the \TEX/ and definition parts have been treated, we have
4394 |next_control>=begin_C|. We will make the global variable |this_section|
4395 point to the current section name, if it has a name.
4396
4397 @<Private...@>=
4398 static name_pointer this_section; /* the current section name, or zero */
4399
4400 @ @<Translate the \9{c}\CEE/...@>=
4401 this_section=name_dir;
4402 if (next_control<=section_name) {
4403 emit_space_if_needed(); init_stack();
4404 if (next_control==begin_C) next_control=get_next();
4405 else {
4406 this_section=cur_section;
4407 @<Check that `\.{=}' or `\.{==}' follows this section name, and
4408 emit the scraps to start the section definition@>@;
4409 }
4410 while (next_control<=section_name) {
4411 outer_parse();
4412 @<Emit the scrap for a section name if present@>@;
4413 }
4414 finish_C(true);
4415 }
4416
4417 @ The title of the section and an $\E$ or $\mathrel+\E$ are made
4418 into a scrap that should not take part in the parsing.
4419
4420 @<Check that `\.{=}'...@>=
4421 do next_control=get_next();
4422 while (next_control=='+'); /* allow optional `\.{+=}' */
4423 if (next_control!='=' && next_control!=eq_eq)
4424 err_print("! You need an = sign after the section name");
4425 @.You need an = sign...@>
4426 else next_control=get_next();
4427 if (out_ptr>out_buf+1 && *out_ptr=='Y' && *(out_ptr-1)=='\\') app(backup);
4428 /* the section name will be flush left */
4429 @.\\Y@>
4430 app(section_flag+(int)(this_section-name_dir));
4431 cur_xref=(xref_pointer)this_section->xref;
4432 if(cur_xref->num==file_flag) cur_xref=cur_xref->xlink;
4433 app_str("${}");
4434 if (cur_xref->num!=section_count+def_flag) {
4435 app_str("\\mathrel+"); /* section name is multiply defined */
4436 this_section=name_dir; /* so we won't give cross-reference info here */
4437 }
4438 app_str("\\E"); /* output an equivalence sign */
4439 @.\\E@>
4440 app_str("{}$");
4441 app(force); app_scrap(dead,no_math);
4442 /* this forces a line break unless `\.{@@+}' follows */
4443
4444 @ @<Emit the scrap...@>=
4445 if (next_control<section_name) {
4446 err_print("! You can't do that in C text");
4447 @.You can't do that...@>
4448 next_control=get_next();
4449 }
4450 else if (next_control==section_name) {
4451 app(section_flag+(int)(cur_section-name_dir));
4452 app_scrap(section_scrap,maybe_math);
4453 next_control=get_next();
4454 }
4455
4456 @ Cross references relating to a named section are given
4457 after the section ends.
4458
4459 @<Show cross...@>=
4460 if (this_section>name_dir) {
4461 cur_xref=(xref_pointer)this_section->xref;
4462 if ((an_output=(cur_xref->num==file_flag))==true) cur_xref=cur_xref->xlink;
4463 if (cur_xref->num>def_flag)
4464 cur_xref=cur_xref->xlink; /* bypass current section number */
4465 footnote(def_flag); footnote(cite_flag); footnote(0);
4466 }
4467
4468 @ The |footnote| procedure gives cross-reference information about
4469 multiply defined section names (if the |flag| parameter is
4470 |def_flag|), or about references to a section name
4471 (if |flag==cite_flag|), or to its uses (if |flag==0|). It assumes that
4472 |cur_xref| points to the first cross-reference entry of interest, and it
4473 leaves |cur_xref| pointing to the first element not printed. Typical outputs:
4474 `\.{\\A101.}'; `\.{\\Us 370\\ET1009.}';
4475 `\.{\\As 8, 27\\*\\ETs64.}'.
4476
4477 Note that the output of \.{CWEAVE} is not English-specific; users may
4478 supply new definitions for the macros \.{\\A}, \.{\\As}, etc.
4479
4480 @c
4481 static void
4482 footnote( /* outputs section cross-references */
4483 sixteen_bits flag)
4484 {
4485 xref_pointer q=cur_xref; /* cross-reference pointer variable */
4486 if (q->num<=flag) return;
4487 finish_line(); out('\\');
4488 @.\\A@>
4489 @.\\Q@>
4490 @.\\U@>
4491 out(flag==0? 'U': flag==cite_flag? 'Q': 'A');
4492 @<Output all the section numbers on the reference list |cur_xref|@>@;
4493 out('.');
4494 }
4495
4496 @ @<Predecl...@>=@+static void footnote(sixteen_bits);
4497
4498 @ The following code distinguishes three cases, according as the number
4499 of cross-references is one, two, or more than two. Variable |q| points
4500 to the first cross-reference, and the last link is a zero.
4501
4502 @<Output all the section numbers...@>=
4503 if (q->xlink->num>flag) out('s'); /* plural */
4504 while (true) {
4505 out_section(cur_xref->num-flag);
4506 cur_xref=cur_xref->xlink; /* point to the next cross-reference to output */
4507 if (cur_xref->num<=flag) break;
4508 if (cur_xref->xlink->num>flag) out_str(", "); /* not the last */
4509 else {out_str("\\ET"); /* the last */
4510 @.\\ET@>
4511 if (cur_xref != q->xlink) out('s'); /* the last of more than two */
4512 }
4513 }
4514
4515 @ @<Output the code for the end of a section@>=
4516 out_str("\\fi"); finish_line();
4517 @.\\fi@>
4518 flush_buffer(out_buf,false,false); /* insert a blank line, it looks nice */
4519
4520 @** Phase three processing.
4521 We are nearly finished! \.{CWEAVE}'s only remaining task is to write out the
4522 index, after sorting the identifiers and index entries.
4523
4524 If the user has set the |no_xref| flag (the \.{-x} option on the command line),
4525 just finish off the page, omitting the index, section name list, and table of
4526 contents.
4527
4528 @c
4529 static void
4530 phase_three(void) {
4531 phase=3;
4532 finish_line(); /* the bulk of |tex_file| has been written */
4533 if (no_xref)
4534 out_str("\\end");
4535 @.\\end@>
4536 else {
4537 if (show_progress) printf("%s","\nWriting the index...");
4538 @.Writing the index...@>
4539 if (change_exists) {
4540 @<Tell about changed sections@>@;
4541 finish_line(); flush_buffer(out_buf,false,false);
4542 /* insert a blank line, it looks nice */
4543 }
4544 out_str("\\inx"); finish_line();
4545 @.\\inx@>
4546 @#
4547 if ((idx_file=fopen(idx_file_name,"wb"))==NULL)
4548 fatal("! Cannot open index file ",idx_file_name);
4549 @.Cannot open index file@>
4550 active_file=idx_file; /* change active file to the index file */
4551 @<Do the first pass of sorting@>@;
4552 @<Sort and output the index@>@;
4553 finish_line(); fclose(active_file); /* finished with |idx_file| */
4554 @#
4555 active_file=tex_file; /* switch back to |tex_file| for a tic */
4556 out_str("\\fin"); finish_line();
4557 @.\\fin@>
4558 @#
4559 if ((scn_file=fopen(scn_file_name,"wb"))==NULL)
4560 fatal("! Cannot open section file ",scn_file_name);
4561 @.Cannot open section file@>
4562 active_file=scn_file; /* change active file to section listing file */
4563 @<Output all the section names@>@;
4564 finish_line(); fclose(active_file); /* finished with |scn_file| */
4565 @#
4566 active_file=tex_file; /* switch back to |tex_file| for the last time */
4567 if (group_found) out_str("\\con");@+else out_str("\\end");
4568 @.\\con@>
4569 @.\\end@>
4570 }
4571 finish_line();
4572 fclose(active_file);
4573 if (show_happiness) {
4574 if (show_progress) new_line();
4575 printf("%s","Done.");
4576 }
4577 check_complete(); /* was all of the change file used? */
4578 }
4579
4580 @ @<Predecl...@>=@+static void phase_three(void);
4581
4582 @ Just before the index comes a list of all the changed sections, including
4583 the index section itself.
4584
4585 @<Tell about changed sections@>=
4586 /* remember that the index is already marked as changed */
4587 sixteen_bits k_section=0; /* runs through the sections */
4588 while (!changed_section[++k_section]);
4589 out_str("\\ch ");
4590 @.\\ch@>
4591 out_section(k_section);
4592 while (k_section<section_count) {
4593 while (!changed_section[++k_section]);
4594 out_str(", "); out_section(k_section);
4595 }
4596 out('.');
4597
4598 @ A left-to-right radix sorting method is used, since this makes it easy to
4599 adjust the collating sequence and since the running time will be at worst
4600 proportional to the total length of all entries in the index. We put the
4601 identifiers into different lists based on their first characters.
4602 (Uppercase letters are put into the same list as the corresponding lowercase
4603 letters, since we want to have `$t<\\{TeX}<\&{to}$'.) The
4604 list for character |c| begins at location |bucket[c]| and continues through
4605 the |blink| array.
4606
4607 @<Private...@>=
4608 static name_pointer bucket[256]={NULL};
4609 static name_pointer next_name; /* successor of |cur_name| when sorting */
4610 static name_pointer blink[max_names]; /* links in the buckets */
4611
4612 @ To begin the sorting, we go through all the hash lists and put each entry
4613 having a nonempty cross-reference list into the proper bucket.
4614
4615 @<Do the first pass...@>=
4616 for (hash_ptr=hash; hash_ptr<=hash_end; hash_ptr++) {
4617 next_name=*hash_ptr;
4618 while (next_name) {
4619 cur_name=next_name; next_name=cur_name->link;
4620 if (cur_name->xref!=(void *)xmem) {
4621 int c=(cur_name->byte_start)[0];
4622 if (xisupper(c)) c=tolower(c);
4623 blink[cur_name-name_dir]=bucket[c]; bucket[c]=cur_name;
4624 }
4625 }
4626 }
4627
4628 @ During the sorting phase we shall use the |cat| and |trans| arrays from
4629 \.{CWEAVE}'s parsing algorithm and rename them |depth| and |head|. They now
4630 represent a stack of identifier lists for all the index entries that have
4631 not yet been output. The variable |sort_ptr| tells how many such lists are
4632 present; the lists are output in reverse order (first |sort_ptr|, then
4633 |sort_ptr-1|, etc.). The |j|th list starts at |head[j]|, and if the first
4634 |k| characters of all entries on this list are known to be equal we have
4635 |depth[j]==k|.
4636
4637 @<Rest of |trans_plus| union@>=
4638 name_pointer Head;
4639
4640 @ @d depth cat /* reclaims memory that is no longer needed for parsing */
4641 @d head trans_plus.Head /* ditto */
4642 @f sort_pointer int
4643 @d sort_pointer scrap_pointer /* ditto */
4644 @d sort_ptr scrap_ptr /* ditto */
4645
4646 @<Private...@>=
4647 static eight_bits cur_depth; /* depth of current buckets */
4648 static char *cur_byte; /* index into |byte_mem| */
4649 static sixteen_bits cur_val; /* current cross-reference number */
4650 static sort_pointer max_sort_ptr; /* largest value of |sort_ptr| */
4651
4652 @ @<Set init...@>=
4653 max_sort_ptr=scrap_info;
4654
4655 @ The desired alphabetic order is specified by the |collate| array; namely,
4656 $|collate|[0]<|collate|[1]<\cdots<|collate|[100]$.
4657
4658 @<Private...@>=
4659 static eight_bits collate[101+128]; /* collation order */
4660 @^high-bit character handling@>
4661
4662 @ We use the order $\hbox{null}<\.\ <\hbox{other characters}<{}$\.\_${}<
4663 \.A=\.a<\cdots<\.Z=\.z<\.0<\cdots<\.9.$ Warning: The collation mapping
4664 needs to be changed if ASCII code is not being used.
4665 @^ASCII code dependencies@>
4666 @^high-bit character handling@>
4667
4668 We initialize |collate| by copying a few characters at a time, because
4669 some \CEE/ compilers choke on long strings.@^system dependencies@>
4670
4671 @<Set init...@>=
4672 collate[0]=0;
4673 memcpy((char *)collate+1,
4674 " \1\2\3\4\5\6\7\10\11\12\13\14\15\16\17",16);
4675 /* 16 characters + 1 = 17 */
4676 memcpy((char *)collate+17,
4677 "\20\21\22\23\24\25\26\27\30\31\32\33\34\35\36\37",16);
4678 /* 16 characters + 17 = 33 */
4679 memcpy((char *)collate+33,
4680 "!\42#$%&'()*+,-./:;<=>?@@[\\]^`{|}~_",32);
4681 /* 32 characters + 33 = 65 */
4682 memcpy((char *)collate+65,
4683 "abcdefghijklmnopqrstuvwxyz0123456789",36);
4684 /* (26 + 10) characters + 65 = 101 */
4685 memcpy((char *)collate+101,
4686 "\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217",16);
4687 /* 16 characters + 101 = 117 */
4688 memcpy((char *)collate+117,
4689 "\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237",16);
4690 /* 16 characters + 117 = 133 */
4691 memcpy((char *)collate+133,
4692 "\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257",16);
4693 /* 16 characters + 133 = 149 */
4694 memcpy((char *)collate+149,
4695 "\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277",16);
4696 /* 16 characters + 149 = 165 */
4697 memcpy((char *)collate+165,
4698 "\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317",16);
4699 /* 16 characters + 165 = 181 */
4700 memcpy((char *)collate+181,
4701 "\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337",16);
4702 /* 16 characters + 181 = 197 */
4703 memcpy((char *)collate+197,
4704 "\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357",16);
4705 /* 16 characters + 197 = 213 */
4706 memcpy((char *)collate+213,
4707 "\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377",16);
4708 /* 16 characters + 213 = 229 */
4709
4710 @ Procedure |unbucket| goes through the buckets and adds nonempty lists
4711 to the stack, using the collating sequence specified in the |collate| array.
4712 The parameter to |unbucket| tells the current depth in the buckets.
4713 Any two sequences that agree in their first 255 character positions are
4714 regarded as identical.
4715
4716 @d infinity 255 /* $\infty$ (approximately) */
4717
4718 @c
4719 static void
4720 unbucket( /* empties buckets having depth |d| */
4721 eight_bits d)
4722 {
4723 int c; /* index into |bucket|; cannot be a simple |char| because of sign
4724 comparison below */
4725 for (c=100+128; c>= 0; c--) if (bucket[collate[c]]) {
4726 @^high-bit character handling@>
4727 if (sort_ptr>=scrap_info_end) overflow("sorting");
4728 sort_ptr++;
4729 if (sort_ptr>max_sort_ptr) max_sort_ptr=sort_ptr;
4730 if (c==0) sort_ptr->depth=infinity;
4731 else sort_ptr->depth=d;
4732 sort_ptr->head=bucket[collate[c]]; bucket[collate[c]]=NULL;
4733 }
4734 }
4735
4736 @ @<Predecl...@>=@+static void unbucket(eight_bits);
4737
4738 @ @<Sort and output...@>=
4739 sort_ptr=scrap_info; unbucket(1);
4740 while (sort_ptr>scrap_info) {
4741 cur_depth=sort_ptr->depth;
4742 if (blink[sort_ptr->head-name_dir]==0 || cur_depth==infinity)
4743 @<Output index entries for the list at |sort_ptr|@>@;
4744 else @<Split the list at |sort_ptr| into further lists@>@;
4745 }
4746
4747 @ @<Split the list...@>= {
4748 int c;
4749 next_name=sort_ptr->head;
4750 do {
4751 cur_name=next_name; next_name=blink[cur_name-name_dir];
4752 cur_byte=cur_name->byte_start+cur_depth;
4753 if (cur_byte==(cur_name+1)->byte_start) c=0; /* hit end of the name */
4754 else if (xisupper(c=*cur_byte)) c=tolower(c);
4755 blink[cur_name-name_dir]=bucket[c]; bucket[c]=cur_name;
4756 } while (next_name);
4757 --sort_ptr; unbucket(cur_depth+1);
4758 }
4759
4760 @ @<Output index...@>= {
4761 cur_name=sort_ptr->head;
4762 do {
4763 out_str("\\I");
4764 @.\\I@>
4765 @<Output the name at |cur_name|@>@;
4766 @<Output the cross-references at |cur_name|@>@;
4767 cur_name=blink[cur_name-name_dir];
4768 } while (cur_name);
4769 --sort_ptr;
4770 }
4771
4772 @ @<Output the name...@>=
4773 switch (cur_name->ilk) {@+char *p; /* index into |byte_mem| */@+@t}\6{\4@>
4774 case normal: case func_template:
4775 if (is_tiny(cur_name)) out_str("\\|");
4776 else {@+bool all_caps=true;@+@t}\6{@>
4777 for (p=cur_name->byte_start;p<(cur_name+1)->byte_start;p++)
4778 if (xislower(*p)) { /* not entirely uppercase */
4779 all_caps=false; break;
4780 }
4781 out_str(all_caps ? "\\." : "\\\\");
4782 }
4783 break;
4784 @.\\|@>
4785 @.\\.@>
4786 @.\\\\@>
4787 case wildcard: out_str("\\9");@+ goto not_an_identifier;
4788 @.\\9@>
4789 case typewriter: out_str("\\.");@+ goto not_an_identifier;
4790 @.\\.@>
4791 case roman: not_an_identifier: out_name(cur_name,false); goto name_done;
4792 case custom:
4793 out_str("$\\");
4794 for (p=cur_name->byte_start;p<(cur_name+1)->byte_start;p++)
4795 out(*p=='_'? 'x': *p=='$'? 'X': *p);
4796 out('$');
4797 goto name_done;
4798 default: out_str("\\&");
4799 @.\\\&@>
4800 }
4801 out_name(cur_name,true);
4802 name_done:@;
4803
4804 @ Section numbers that are to be underlined are enclosed in
4805 `\.{\\[}$\,\ldots\,$\.]'.
4806
4807 @<Output the cross-references...@>=
4808 @<Invert the cross-reference list at |cur_name|, making |cur_xref| the head@>@;
4809 do {
4810 out_str(", "); cur_val=cur_xref->num;
4811 if (cur_val<def_flag) out_section(cur_val);
4812 else {out_str("\\["); out_section(cur_val-def_flag); out(']');}
4813 @.\\[@>
4814 cur_xref=cur_xref->xlink;
4815 } while (cur_xref!=xmem);
4816 out('.'); finish_line();
4817
4818 @ List inversion is best thought of as popping elements off one stack and
4819 pushing them onto another. In this case |cur_xref| will be the head of
4820 the stack that we push things onto.
4821
4822 @<Private...@>=
4823 static xref_pointer next_xref, this_xref;
4824 /* pointer variables for rearranging a list */
4825
4826 @ @<Invert the cross-reference list at |cur_name|, making |cur_xref| the head@>=
4827 this_xref=(xref_pointer)cur_name->xref; cur_xref=xmem;
4828 do {
4829 next_xref=this_xref->xlink; this_xref->xlink=cur_xref;
4830 cur_xref=this_xref; this_xref=next_xref;
4831 } while (this_xref!=xmem);
4832
4833 @ The following recursive procedure walks through the tree of section names and
4834 prints them.
4835 @^recursion@>
4836
4837 @c
4838 static void
4839 section_print( /* print all section names in subtree |p| */
4840 name_pointer p)
4841 {
4842 if (p) {
4843 section_print(p->llink); out_str("\\I");
4844 @.\\I@>
4845 tok_ptr=tok_mem+1; text_ptr=tok_start+1; scrap_ptr=scrap_info; init_stack();
4846 app(section_flag+(int)(p-name_dir)); make_output();
4847 footnote(cite_flag);
4848 footnote(0); /* |cur_xref| was set by |make_output| */
4849 finish_line();@/
4850 section_print(p->rlink);
4851 }
4852 }
4853
4854 @ @<Predecl...@>=@+static void section_print(name_pointer);
4855
4856 @ @<Output all the section names@>=section_print(root);
4857
4858 @ Because on some systems the difference between two pointers is a |ptrdiff_t|
4859 rather than an |int|, we use \.{\%td} to print these quantities.
4860
4861 @c
4862 void
4863 print_stats(void) {
4864 puts("\nMemory usage statistics:");
4865 @.Memory usage statistics:@>
4866 printf("%td names (out of %ld)\n",@^system dependencies@>
4867 (ptrdiff_t)(name_ptr-name_dir),(long)max_names);
4868 printf("%td cross-references (out of %ld)\n",
4869 (ptrdiff_t)(xref_ptr-xmem),(long)max_refs);
4870 printf("%td bytes (out of %ld)\n",
4871 (ptrdiff_t)(byte_ptr-byte_mem),(long)max_bytes);
4872 puts("Parsing:");
4873 printf("%td scraps (out of %ld)\n",
4874 (ptrdiff_t)(max_scr_ptr-scrap_info),(long)max_scraps);
4875 printf("%td texts (out of %ld)\n",
4876 (ptrdiff_t)(max_text_ptr-tok_start),(long)max_texts);
4877 printf("%td tokens (out of %ld)\n",
4878 (ptrdiff_t)(max_tok_ptr-tok_mem),(long)max_toks);
4879 printf("%td levels (out of %ld)\n",
4880 (ptrdiff_t)(max_stack_ptr-stack),(long)stack_size);
4881 puts("Sorting:");
4882 printf("%td levels (out of %ld)\n",
4883 (ptrdiff_t)(max_sort_ptr-scrap_info),(long)max_scraps);
4884 }
4885
4886 @** Index.
4887 If you have read and understood the code for Phase III above, you know what
4888 is in this index and how it got here. All sections in which an identifier is
4889 used are listed with that identifier, except that reserved words are
4890 indexed only when they appear in format definitions, and the appearances
4891 of identifiers in section names are not indexed. Underlined entries
4892 correspond to where the identifier was declared. Error messages, control
4893 sequences put into the output, and a few
4894 other things like ``recursion'' are indexed here too.