home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cweb28.zip / examples / extex.w next >
Text File  |  1992-07-08  |  12KB  |  357 lines

  1. \datethis
  2. @* Introduction. This program is a simple filter that inputs \TeX\ or \.{CWEB}
  3. files and outputs its best guess at the ``words'' they contain. The word
  4. list can then be passed to a spelling check routine such as {\tt wordtest}.
  5.  
  6. If this program is invoked with the name `{\tt excweb}', it will apply
  7. special rules based on the syntax of \.{CWEB} files. Otherwise it will
  8. use only the \TeX\ conventions. (Note that \UNIX's {\tt ln} command
  9. allows a program to be invoked with more than one name although it
  10. appears only once in the computer's memory.)
  11.  
  12. The \TeX\ conventions adopted here say that words are what remain
  13. after you remove nonletters, control sequences,
  14. comments triggered by \.\% marks, and material enclosed
  15. within \.{\$...\$} or \.{\$\$...\$\$}. However, an apostrophe within
  16. a word will be retained. The plain \TeX\ control
  17. sequences for accented characters and special text characters, namely
  18. $$\vbox{\halign{&\.{\\#}\hfil\qquad\cr
  19. '&`&\relax\^&"&\relax\~&=&.&u&v\cr
  20. H&t&c&d&b&oe&OE&ae&AE\cr
  21. aa&AA&o&O&l&L&ss&i&j\cr}}$$
  22. will also be retained, so that users can treat them as parts of words.
  23. A blank space
  24. following any of the alphabetic control sequences in this list will be carried
  25. along too. If any of these control sequences is followed by \.\{, everything
  26. up to the next \.\} will also be retained. Thus, for example, the
  27. construction `\.{m\\=\{\\i\}n\\u\ us}' will be considered a single word,
  28. in spite of the control sequences and the space between the two u's.
  29.  
  30. The \.{CWEB} conventions are essentially the same as the \TeX\ conventions,
  31. in the \TeX\ parts of a \.{CWEB} file. The \Cee\ parts of the file
  32. are blanked out.
  33.  
  34. No attempt is made to reach a high level of artificial intelligence,
  35. which would be able to truly understand the input file. Tricky users can
  36.  confuse us. But we claim that devious tricks are their problem, not ours.
  37.  
  38. @ So here goes. The main idea is to keep a one-character lookahead
  39. buffer, called |c|, which is set to zero when the character has been
  40. processed. A giant switch to various cases, depending on the value of~|c|,
  41. keeps everything moving.
  42.  
  43. If you don't like |goto| statements, don't read this. (And don't read
  44. any other programs that simulate finite-state automata.)
  45.  
  46. @c
  47. #include <stdio.h>
  48. #include <ctype.h>
  49. @#
  50. @<Global variables@>@;
  51. @<Procedures@>@;
  52. @#
  53. main(argc,argv)
  54.   int argc; /* the number of arguments (should be 1, but this isn't checked) */
  55.   char *argv[]; /* the arguments (|*argv| is the program name) */
  56. {
  57.   @<Local variables@>;
  58.   if (strcmp(*argv,"excweb")==0) {
  59.     web=1;
  60.     @<Adjust tables for \.{CWEB} mode@>;
  61.   } else web=0;
  62.   comment=skipping=c=0;
  63.   main_cycle: if (c) goto big_switch;
  64.   restart: c=get();
  65.   big_switch: switch(c) {
  66.      @<Special cases of the giant switch where we don't just discard |c|@>@;
  67.     case EOF: exit(0);
  68.     default: goto restart;
  69.     }
  70.     @<Labeled code segments, which exit by explicit |goto|@>;
  71. }
  72.  
  73. @ @<Global variables@>=
  74. int c; /* one-character look-see buffer */
  75.  
  76. @ @<Local variables@>=
  77. int web; /* are we looking for \.{CWEB} constructs? */
  78. int comment; /* are we inside a \Cee\ comment in a \.{CWEB} document? */
  79. int skipping; /* are we skipping \Cee\ code in a \.{CWEB} document? */
  80. int save_skipping; /* value of |skipping| outside current \Cee\ mode */
  81. register int cc; /* temporary buffer */
  82.  
  83. @* Simple cases.
  84. Let's do some of the easiest things first, in order to get the hang of
  85. this program. Several special characters will cause us to ignore everything
  86. until the first appearance of something else.
  87.  
  88. @d discard_to(x) {@+while (get()!=x) ;@+}
  89.  
  90. @<Special cases...@>=
  91. case '%': discard_to('\n');@+goto restart;
  92. case '$': c=getchar();
  93.   if (c!='$') discard_to('$')@;
  94.   else { /* after \.{\$\$} we discard everything to the next \.{\$\$} */
  95.     do discard_to('$')@;
  96.     while (getchar()!='$');
  97.   }
  98.   goto restart;
  99.  
  100. @ The `|get|' procedure in the code above is like \Cee's standard
  101. `|getchar|', except that it immediately terminates execution at the end of
  102. the input file. Otherwise malformed input files could lead to
  103. infinite loops.
  104.  
  105. @<Procedures@>=
  106. int get()
  107. {@+register int x;
  108.   x=getchar();
  109.   if (x==EOF) exit(0);
  110.   return x;
  111. }
  112.  
  113. @ More complex behavior is handled by jumping out of the |switch| statement
  114. to one of the routines following it. None of the cases say |break|, so
  115. the code following the switch statement is accessible only via |goto|.
  116.  
  117. @<Special cases...@>=
  118. case 'a': case 'A':
  119. case 'b': case 'B':
  120. case 'c': case 'C':
  121. case 'd': case 'D':
  122. case 'e': case 'E':
  123. case 'f': case 'F':
  124. case 'g': case 'G':
  125. case 'h': case 'H':
  126. case 'i': case 'I':
  127. case 'j': case 'J':
  128. case 'k': case 'K':
  129. case 'l': case 'L':
  130. case 'm': case 'M':
  131. case 'n': case 'N':
  132. case 'o': case 'O':
  133. case 'p': case 'P':
  134. case 'q': case 'Q':
  135. case 'r': case 'R':
  136. case 's': case 'S':
  137. case 't': case 'T':
  138. case 'u': case 'U':
  139. case 'v': case 'V':
  140. case 'w': case 'W':
  141. case 'x': case 'X':
  142. case 'y': case 'Y':
  143. case 'z': case 'Z':
  144. goto out_word;
  145.  
  146. @ When letters appear in |stdin|, we pass them immediately through to |stdout|
  147. with little further ado.
  148. An apostrophe is rejected unless it is immediately followed by a letter.
  149.  
  150. @<Labeled code...@>=
  151. out_word: putchar(c);
  152. continue_word: c=getchar();
  153. checkout_word:
  154. if (isalpha(c)) goto out_word;
  155. if (c=='\'') {
  156.   c=getchar();
  157.   if (isalpha(c)) {
  158.     putchar('\'');@+goto out_word;
  159.   }
  160.   goto end_word;
  161. }
  162. if (c=='\\' && controlseq()) goto control_seq_in_word;
  163. end_word: putchar('\n');
  164.   goto main_cycle;
  165.  
  166. @* Control sequences.  The |controlseq()| function is the only
  167. delicate part of this program.  After a backslash has been scanned,
  168. |controlseq| looks to see if the next characters define one of the
  169. special plain \TeX\ macros listed above. If so, the control sequence
  170. and its immediately following argument (if any) are output and
  171. |controlseq| returns a nonzero value. If not, nothing is output and
  172. |controlseq| returns zero. In both cases the value of |c| will be
  173. nonzero if and only if |controlseq| has had to look ahead at a
  174. character it decided not to process.
  175.  
  176. @ @<Labeled code...@>=
  177. control_seq_in_word: if (!c) goto continue_word;
  178. goto checkout_word;
  179.  
  180. @ @<Special cases...@>=
  181. case '\\': if (controlseq()) goto control_seq_in_word;
  182. goto main_cycle;
  183.  
  184. @ @<Procedures@>=
  185. controlseq()
  186. {
  187.   int l; /* number of letters in the control sequence */
  188.   char a,b; /* the first two characters after `\.\\' */
  189.   l=0;
  190.   a=c=getchar();
  191.   while (isalpha(c)) {
  192.     l++;
  193.     c=getchar();
  194.     if (l==1) b=c;
  195.   }
  196.   if (l==0) c=getchar();
  197.   @<Check for special plain \TeX\ control sequences;
  198.     output them and |return 1| if found@>;
  199.   return 0;
  200. }
  201.  
  202. @ @d pair(x,y) (a==x && b==y)
  203.  
  204. @<Check for special...@>=
  205. if (a>='"' && a<='~' && ptab[a-'"']==l ||
  206.  (l==2 && (pair('a','e') || pair('A','E')@|
  207.            || pair('o','e') || pair('O','E')@|
  208.            || pair('a','a') || pair('A','A') || pair('s','s')))) {
  209.   putchar('\\');
  210.   putchar(a);
  211.   if (l==2) putchar(b);
  212.   if (l && c==' ') {
  213.     putchar(' '); /* optional space after alphabetic control sequence */
  214.     c=getchar();
  215.   }
  216.   if (c=='{') {
  217.     do@+{putchar(c);
  218.       c=get();
  219.     }@+while (c!='}'); /* optional argument after special control sequence */
  220.     putchar(c);
  221.     c=0;
  222.   }
  223.   return 1;
  224. }
  225.  
  226. @ The |ptab| entries for nonletters are 0 when the control sequence is
  227. special, otherwise~1; the conventions for letters are reversed.
  228.  
  229. @<Global...@>=
  230. char ptab[]={0,1,1,1,1,0, /* \.{\\"} and \.{\\'} */
  231.   1,1,1,1,1,1,0,1, /* \.{\\.} */
  232.   1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1, /* \.{\\=} */
  233.   1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1, /* \.{\\H}, \.{\\L}, \.{\\O} */
  234.   0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,1, /* \.{\\\^} */
  235.   0,0,1,1,1,0,0,0, /* \.{\\`}, \.{\\b}, \.{\\c}, \.{\\d} */
  236.   0,1,1,0,1,0,0,1, /* \.{\\i}, \.{\\j}, \.{\\l}, \.{\\o} */
  237.   0,0,0,0,1,1,1,0, /* \.{\\t}, \.{\\u}, \.{\\v} */
  238.   0,0,0,1,1,1,0}; /* \.{\\\~} */
  239.  
  240. @ In \.{CWEB} the \TeX\ control sequence `\.{\\.}' denotes the typewriter
  241. font used for strings, not the dot-over accent. We must modify
  242. |ptab| to reflect this unfortunate (but too-late-too-change) design decision.
  243.  
  244. @<Adjust tables for \.{CWEB} mode@>=
  245. ptab[12]=1;
  246.  
  247. @* CWEB considerations.
  248. We're finished now with all that would be needed if we only wanted to
  249. handle \TeX. For \.{CWEB} a bit more should be done.
  250.  
  251. The \.{CWEB} escape character is \.{@@}, and the character following
  252. it tells us what mode we should enter. In \TeX\ mode we should not
  253. only do what we normally do for \TeX\ files, we should also ignore
  254. material delimited by \.{\char"7C...\char"7C}, being careful to recognize when
  255. |'|'| is part of a string (as it just was). And we should stop \TeX\
  256. mode if we scan \.{*/} while in a \Cee\ comment.
  257.  
  258. @<Special cases...@>=
  259. case '@@': if (web) goto do_web;
  260.   goto restart;
  261. case '|': if (web>1) {
  262.      save_skipping=skipping;
  263.      goto skip_C_prime;
  264.    }
  265.    goto restart;
  266. case '*': if (!comment) goto restart;
  267.   c=getchar();
  268.   if (c=='/') {
  269.     comment=0;
  270.     goto skip_C;
  271.   }
  272.   goto big_switch;
  273.  
  274. @ The characters that follow \.@@ in a \.{CWEB} file can be classified into
  275. a few types that have distinct implications for {\tt excweb}.
  276.  
  277. @d nop 0 /* control code that doesn't matter to us */
  278. @d start_section 1 /* control code that begins a \.{CWEB} section */
  279. @d start_C 2 /* control code that begins \Cee\ code */
  280. @d start_name 3 /* control code that begins a section name */
  281. @d start_index 4 /* control code for \.{CWEB} index entry */
  282. @d start_insert 5 /* control code for \Cee\ material ended by `\.{@@>}' */
  283. @d end_item 6 /* `\.{@@>}' */
  284.  
  285. @<Global...@>=
  286. char wtab[]={start_section,nop,nop,nop,nop,nop,nop,nop, /* \.{\ !"\#\$\%\&'} */
  287.  start_name,nop,start_section,nop,nop,nop,start_index,nop, /* \.{()*+,-./} */
  288.  nop,nop,nop,nop,nop,nop,nop,nop, /* \.{01234567} */
  289.  nop,nop,start_index,nop,start_name,start_insert,end_item,nop,
  290.     /* \.{89:;<=>?} */
  291.  nop,nop,nop,start_C,start_C,nop,start_C,nop, /* \.{@@ABCDEFG} */
  292.  nop,nop,nop,nop,nop,nop,nop,nop, /* \.{HIJKLMNO} */
  293.  start_C,nop,nop,nop,start_insert,nop,nop,nop, /* \.{PQRSTUVW} */
  294.  nop,nop,nop,nop,nop,nop,start_index,nop, /* \.{XYZ[\\]\^\_} */
  295.  nop,nop,nop,start_C,start_C,nop,start_C,nop, /* \.{`abcdefg} */
  296.  nop,nop,nop,nop,nop,nop,nop,nop, /* \.{hijklmno} */
  297.  start_C,nop,nop,nop,start_insert}; /* \.{pqrst} */
  298.  
  299. @ We do not leave \TeX\ mode until the first \.{WEB} section has begun.
  300.  
  301. @<Labeled code...@>=
  302. do_web: c=getchar();
  303.   if (c<' ' || c>'t') goto restart;
  304.   switch(wtab[c-' ']){
  305.  case nop: case start_index: goto restart;
  306.  case start_section: web++; /* out of ``limbo'' */
  307.   comment=skipping=0;@+goto restart;
  308.  case start_C: if (web>1) goto skip_C;
  309.   goto restart;
  310.  case start_name: case start_insert: if (web>1) skipping=1;
  311.   goto restart;
  312.  case end_item: if (skipping) goto skip_C;
  313.    goto restart;
  314.   }
  315.  
  316. @ The final piece of program we need is a sub-automaton to pass over
  317. the \Cee\ parts of a \.{CWEB} document. The main subtlety here is that
  318. we don't want to get out of synch while scanning over a supposed
  319. string constant or verbatim insert.
  320.  
  321. @<Labeled code...@>=
  322. skip_C: save_skipping=2;
  323. skip_C_prime: skipping=1;
  324.   while (1) {
  325.     c=get();
  326.   C_switch: switch(c) {
  327.    case '/': c=get();
  328.      if (c!='*') goto C_switch;
  329.      comment=1; /* fall through to the next case, returning to \TeX\ mode */
  330.    case '|': if (save_skipping==2) continue; /* |'|'| as \Cee\ operator */
  331.      skipping=save_skipping;@+goto restart; /* |'|'| as \.{CWEB} delimiter */
  332.    case '@@': c=getchar();
  333.     inner_switch: if (c<' ' || c>'t') continue;
  334.      switch(wtab[c-' ']) {
  335.     case nop: case start_C: case end_item: continue;
  336.     case start_section: web++;
  337.      comment=skipping=0;@+goto restart;
  338.     case start_name: case start_index: goto restart;
  339.     case start_insert: do@+discard_to('@@')@;@+while ((c=getchar())=='@@');
  340.       goto inner_switch; /* now |c| should equal |'>'| */
  341.      }
  342.    case '\'': case '"':    
  343.      while ((cc=get())!=c && cc!='\n')
  344.        if (cc=='\\') getchar();
  345.        else if (cc=='@@') {
  346.          cc=getchar();
  347.          if (cc!='@@') { /* syntax error, we try to recover */
  348.            c=cc; goto inner_switch;
  349.          }
  350.        };
  351.      continue; /* \.{CWEB} strings do not extend past one line */
  352.     default: continue;
  353.     }
  354.   }
  355.  
  356. @* Index.
  357.