1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
|
%{
#define LEX_STK_SIZE 256
int lex_stk[LEX_STK_SIZE];
int lex_stk_top = 0;
#define PUSH(x) lex_stk[++lex_stk_top] = (x); BEGIN(x)
#define POP BEGIN(lex_stk[--lex_stk_top])
#define LEX_STK_TOP lex_stk[lex_stk_top]
#define LEX_NEXT_TO_STK_TOP lex_stk[lex_stk_top - 1]
#define CHG_TOP(x) lex_stk[lex_stk_top] = (x); BEGIN(x)
#define CHG_NEXT_TO_TOP(x) lex_stk[lex_stk_top - 1] = (x)
/* not a lot in the old stack {over,under}flow dept, what? */
extern char *yytext; /* forward decl */
void print_leading_whitespace();
void
print_lex_stk () /* just for debugging */
{
int i;
fprintf(stderr,"lex_stk:");
for (i = lex_stk_top; i >= 0; i--) {
fprintf(stderr," %d",lex_stk[i]);
}
fprintf(stderr,"::%s\n",yytext);
}
int verbose; /* set from first argv */
int follow_inputs; /* set from second argv */
#if defined (SYSV) || defined (SYSV)
#include <string.h>
#include <time.h>
#else
#include <strings.h>
#include <sys/time.h>
#endif
#if defined (SYSV)
#define index(string, ch) strchr ((string), (ch))
#endif
#define INPUTFILE_STK_SIZE 20
struct {
YY_BUFFER_STATE handle; /* flex-supplied magic */
char* name;
int lineno;
} inputfile_stk[INPUTFILE_STK_SIZE];
int inputfile_stk_top = 0;
void push_inputfile();
void pop_inputfile();
int i; /* temporary */
char* rbrace;
int deatify_debug = 0;
int unmatched_lbraces = 0;
int unmatched_lbrackets = 0;
/* like everything, must track srcfile name and lineno */
char srcfile_name[1024]; /* too lazy to do this right */
int lineno = 1; /* count \n's as they go by */
int lineno_fiddled = 0; /* boolean: incr if newlines get trashed */
int exit_status = 0;
void not_OK (); /* prints an error msg and bumps exit_status */
void warning (); /* same, but no exit-status fiddling */
int lit2what; /* set from ARGV */
#define IS_LIT2PGM_Q (lit2what == 1)
#define IS_LIT2PGM (lit2what == 2)
#define IS_LIT2TEXI (lit2what == 3)
#define IS_LIT2LATEX (lit2what == 4)
#define IS_LIT2DEPEND (lit2what == 5)
#define IS_LIT2CHANGELOG (lit2what== 6)
/* the condition for printing to stdout */
#define SHLD_PRINT (! IS_LIT2PGM_Q || (LEX_STK_TOP == Code || LEX_STK_TOP == Bird))
void myecho();
#define MYECHO myecho()
void myprintstr();
#define YY_USER_ACTION if (deatify_debug) print_lex_stk(); /* debugging */
%}
D [0-9]+
END_WORD [-~/!\?(),\.'`A-Za-z0-9]*[ \t]*
%x Norm Verb Code Bird Atting Math Index Typing Linify_braces Linify_brackets
%%
CHG_TOP(Norm); /* that's where we start */
[\001\002\003] { not_OK("ASCII chars \001, \002, \003 may not appear in literate files\n");
exit(1);
}
<Norm>^"srcfile!_!"(.+)"!_!"([0-9]+)"!_!"\n {
sscanf(yytext+10,"%[^!]!_!%d", srcfile_name, &lineno);
if (lineno > 0) {
ECHO;
}
}
<Norm>^\\input\{[^\}\n]+\}.*\n { lineno++;
if (follow_inputs) {
/* nullify first right brace */
rbrace = index(yytext+7,'}');
*rbrace = '\0';
push_inputfile(yytext+7);
} else {
MYECHO;
}
}
<Norm,Bird><<EOF>> { if ( inputfile_stk_top <= 0 ) {
yyterminate();
} else {
pop_inputfile();
}
CHG_TOP(Norm); /* revert */
}
<Norm>^\\begin\{verbatim\}.*\n |
<Norm>^\\begin\{flushverbatim\}.*\n |
<Norm>^\\begin\{pseudocode\}.*\n { lineno++; MYECHO; PUSH(Verb); /* some may have opt arg */ }
<Norm>^\\begin\{code\}.*\n { lineno++;
if (IS_LIT2PGM_Q) {
printf("srcfile!_!%s!_!%d!_!\n",srcfile_name,lineno);
}
MYECHO;
PUSH(Code);
}
<Norm>\n[ \t]+\n\> { /* convert to something less weird & try again */
unput('>'); unput('\n'); unput('\n');
}
<Norm>\n\n\>.*\n { lineno += 3;
/* NB: init > converted to space */
if (IS_LIT2PGM_Q) {
printf("srcfile!_!%s!_!%d!_!\n",srcfile_name,(lineno-1));
printf("%s",yytext+3);
} else {
ECHO;
}
PUSH(Bird);
}
<Norm>\n\>.*\n { lineno++; /* so msg will have right line # */
if (lineno > 2) /* I feel guilty about this hack */
/* to avoid: file is: blank lines, then code */
not_OK("Bird-style code not preceded by a blank line\n");
lineno++; /* account for other \n */
if (IS_LIT2PGM_Q) {
printf("srcfile!_!%s!_!%d!_!\n",srcfile_name,(lineno-1));
printf("%s",yytext+2);
} else {
ECHO;
}
PUSH(Bird);
}
<Norm>^\>.*\n { lineno++; /* first line of file */
if (IS_LIT2PGM_Q) {
printf("srcfile!_!%s!_!%d!_!\n",srcfile_name,(lineno-1));
printf("%s",yytext+1);
} else {
ECHO;
}
PUSH(Bird);
}
<Norm>\\author\{ { MYECHO; PUSH(Linify_braces); }
<Norm>\\caption\{ { MYECHO; PUSH(Linify_braces); }
<Norm>\\centerline\{ { MYECHO; PUSH(Linify_braces); }
<Norm>\\date\{ { MYECHO; PUSH(Linify_braces); }
<Norm>\\define\{ { MYECHO; PUSH(Linify_braces); }
<Norm>\\heading\{ { MYECHO; PUSH(Linify_braces); }
<Norm>\\label\{ { MYECHO; PUSH(Linify_braces); }
<Norm>\\menuentry\{ { MYECHO; PUSH(Linify_braces); }
<Norm>\\node\{ { MYECHO; PUSH(Linify_braces); }
<Norm>\\owner\{ { MYECHO; PUSH(Linify_braces); }
<Norm>\\[sub]*section{D}?\{ { MYECHO; PUSH(Linify_braces); }
<Norm>\\[sub]*section{D}?\[ { MYECHO; PUSH(Linify_brackets); }
<Norm>\\standaloneornot\{ { MYECHO; PUSH(Linify_braces); }
<Norm>\\title\{ { MYECHO; PUSH(Linify_braces); }
<Norm>\\(tr|pl)\{ { MYECHO; unmatched_lbraces = 1; PUSH(Typing);
/* \tr has braces as literals */
}
<Norm>\\item\[ { MYECHO; PUSH(Linify_brackets); }
<Norm>\\@ { MYECHO; }
<Norm>@ { myprintstr("\\codeInText{"); PUSH(Atting); }
<Norm>\\\$ { MYECHO; }
<Norm>\$ { myprintstr("\\MathMode{"); PUSH(Math); }
<Norm>\\index\{ { MYECHO;
unmatched_lbraces = 1;
PUSH(Index);
}
<Norm>\\\{ { MYECHO; }
<Norm>\\\} { MYECHO; }
<Norm,Math>\\% { MYECHO; }
<Norm,Math>\%.*\n[ \t]*\n> { /* technically this Bird-style code is wrong
(no blank line in front of it, but that's
not intuitively obvious to the casual observer).
Solution? A hack!
*/
unput('>'); unput('\n'); unput('\n');
}
<Norm,Math>\%.*\n { /* throw away */ lineno++;
if (! IS_LIT2PGM_Q)
printf("\nsrcfile!_!%s!_!%d!_!\n",srcfile_name,lineno);
}
<Norm>[A-Za-z0-9 \t]+ { MYECHO; /* slight efficiency */ }
<Norm>. { MYECHO; }
<Norm>\n { /* handle line-number fiddling */
MYECHO;
lineno++;
if (lineno_fiddled) {
if (! IS_LIT2PGM_Q)
printf("srcfile!_!%s!_!%d!_!\n",srcfile_name,lineno);
lineno_fiddled = 0;
}
}
<Verb>^\\end\{verbatim\} |
<Verb>^\\end\{flushverbatim\} |
<Verb>^\\end\{pseudocode\} { MYECHO; POP; }
<Code>^\\end\{code\} { if (! IS_LIT2PGM_Q) ECHO; POP; }
<Verb,Code>^[^\\\n].*\n { lineno++; MYECHO; /* a line not started by \ */}
<Verb,Code>. { MYECHO; }
<Verb,Code>\n { lineno++; MYECHO; }
<Verb,Code><<EOF>> { not_OK("unexpected end-of-file in verbatim text or (pseudo)code\n");
exit(1);
}
<Bird>[ \t\n]*\n\>.*\n? { /* whitespace/blank-lines do not matter */
/* count lines... */
for (i = 0; i < strlen(yytext); i++) {
if (yytext[i] == '\n')
lineno++;
}
if (! IS_LIT2PGM_Q) {
ECHO;
} else { /* don't print leading whitestuff */
for (i = 0; i < strlen(yytext) && yytext[i] != '>'; i++)
; /* no-op */
/* report where this got us...*/
printf("srcfile!_!%s!_!%d!_!\n",srcfile_name,
((yytext[yyleng-1] == '\n') ? (lineno-1) : lineno));
/* putchar(' '); convert leading > to space */
for ( i++ ; i < strlen(yytext); i++)
putchar(yytext[i]);
}
}
<Bird>^\>.*\n? { if (yytext[yyleng-1] == '\n') { lineno++; }
if (IS_LIT2PGM_Q) printf("%s",yytext+1); else ECHO;
}
<Bird>[ \t]*\n { lineno++;
if ( ! IS_LIT2PGM_Q) ECHO;
POP;
}
<Bird>. { lineno--; /* to get right # on msg */
not_OK("Bird-style code not followed by a blank line\n");
lineno++; /* put it back */
unput(yytext[0]);
POP;
}
<Atting>@@ { myprintstr("@"); }
<Atting>@\\noindex\{\} |
<Atting>@\\noindex { myprintstr("\001noindex\003}");
POP;
}
<Atting>@ { myprintstr("}");
POP;
}
<Atting>\{ { myprintstr("\001lbrace\003"); }
<Atting>\} { myprintstr("\001rbrace\003"); }
<Atting>\n { warning("I'm turning a newline inside a @...@ into a space\n");
myprintstr(" ");
lineno++;
}
<Atting><<EOF>> { not_OK("unexpected end-of-file inside an @ ... @\n");
exit(1);
}
<Atting>[A-Za-z0-9 \t]+ { MYECHO; /* tiny efficiency */ }
<Atting>. { MYECHO; }
<Math>\\\$ { MYECHO; }
<Math>\$ { myprintstr("}");
POP;
}
<Math>\{ { myprintstr("\001lbrace\003"); }
<Math>\} { myprintstr("\001rbrace\003"); }
<Math>\n { myprintstr("\001newline\003");
lineno++;
}
<Math><<EOF>> { not_OK("unexpected end-of-file inside $ ... $ (math mode)\n");
exit(1);
}
<Math>[A-Za-z0-9 \t]+ { MYECHO; /* tiny efficiency */ }
<Math>. { MYECHO; }
<Linify_braces,Linify_brackets>\\(tr|pl)\{ {
MYECHO;
unmatched_lbraces = 1;
PUSH(Typing);
}
<Index,Linify_braces,Linify_brackets,Typing>\\\\ {
MYECHO;
}
<Index,Linify_braces,Linify_brackets,Typing>\\ {
MYECHO;
}
<Linify_braces,Linify_brackets>\\\{ {
MYECHO;
}
<Linify_braces>\{ { MYECHO; PUSH(Linify_braces); }
<Linify_brackets>\{ { MYECHO; }
<Index,Typing>\{ { unmatched_lbraces++; myprintstr("\001lbrace\003"); }
<Linify_braces,Linify_brackets>\\\} {
MYECHO;
}
<Linify_braces>\}\{ { /* next arg */ MYECHO; }
<Linify_braces>\} { MYECHO; POP; }
<Linify_brackets>\} { MYECHO; }
<Index,Typing>\} { if (--unmatched_lbraces == 0) {
MYECHO;
POP;
} else {
myprintstr("\001rbrace\003");
}
}
<Index,Linify_braces,Linify_brackets,Typing>\\@ {
MYECHO;
}
<Linify_braces,Linify_brackets>@ {
myprintstr("\\codeInText{"); /* no newline */
PUSH(Atting);
}
<Typing>@ { MYECHO; /* NB: magic <Index> chars below */ }
<Index,Linify_braces,Linify_brackets,Typing>\\\$ {
MYECHO;
}
<Linify_braces,Linify_brackets>\$ {
myprintstr("\\MathMode{"); /* no newline */
PUSH(Math);
}
<Index,Typing>\$ { MYECHO; }
<Index,Linify_braces,Linify_brackets,Typing>\\% {
MYECHO;
}
<Linify_braces,Linify_brackets>\%.*\n[ \t]*\n> {
/* see earlier comments about crimes against humanity */
unput('>'); unput('\n'); unput('\n');
}
<Linify_braces,Linify_brackets>\%.*\n { /* throw away */
lineno++;
lineno_fiddled++;
}
<Index,Typing>% { MYECHO; }
<Linify_brackets>\\\[ { MYECHO; }
<Linify_brackets>\[ { MYECHO; PUSH(Linify_brackets); }
<Linify_brackets>\\\] { myprintstr("\\\001rbracket\003"); /* cannot have these in the way */ }
<Linify_brackets>\]\{ { if (LEX_NEXT_TO_STK_TOP == Norm) {
MYECHO;
} else {
myprintstr("\001rbracket\003\{");
not_OK("WHAT'S GOING ON? ]{\n");
print_lex_stk();
}
CHG_TOP(Linify_braces);
}
<Linify_brackets>\] { if (LEX_NEXT_TO_STK_TOP == Norm) {
MYECHO;
} else {
myprintstr("\001rbracket\003");
}
POP;
}
<Index>\\\@ { MYECHO; /* my pseudo-makeindex has two magic chars */ }
<Index>\@ { myprintstr("\001idxsort\003"); }
<Index>\\! { MYECHO; }
<Index>\! { myprintstr("\001idxsubitem\003"); }
<Index,Linify_braces,Linify_brackets,Typing>\n {
lineno++;
lineno_fiddled++;
if (LEX_STK_TOP == Index) {
warning("I'm turning a newline inside an \\index{...} into a space\n");
myprintstr(" ");
} else if (LEX_STK_TOP == Typing) {
warning("I'm turning a newline inside a \\tr{...} or \\pl{...} into a space\n");
myprintstr(" ");
} else {
myprintstr("\001newline\003");
}
}
<Index,Linify_braces,Linify_brackets,Typing>[A-Za-z0-9 \t]+ {
MYECHO; /* tiny efficiency */
}
<Index,Linify_braces,Linify_brackets,Typing>. { MYECHO; /* catch all */ }
<Index><<EOF>> { not_OK("unexpected end-of-file inside an \\index{...}\n");
exit(1);
}
<Linify_braces><<EOF>> { not_OK("unexpected end-of-file inside a macro argument\n");
exit(1);
}
<Linify_brackets><<EOF>> {not_OK("unexpected end-of-file inside a macro optional argument\n");
exit(1);
}
<Typing><<EOF>> { not_OK("unexpected end-of-file inside a \\tr{...}\n");
exit(1);
}
%%
main(argc, argv)
int argc;
char **argv;
{
if (argc != 5) {
fprintf(stderr,"Sorry, %s must have exactly 4 arguments\n", argv[0]);
exit(1);
}
verbose = argv[1][0] - '0'; /* hacks */
follow_inputs = argv[2][0] - '0';
lit2what = argv[3][0] - '0'; /* used in IS_LIT2xxx tests */
if (strcmp(argv[4], "-") == 0) {
yyin = stdin;
} else if ((yyin = fopen(argv[4], "r")) == NULL) {
fprintf(stderr,"%s: can't open file %s\n", argv[0], argv[4]);
exit(1);
}
strcpy(srcfile_name, argv[4]);
inputfile_stk[ inputfile_stk_top /* 0 */ ].name = argv[4];
if ( ! IS_LIT2PGM_Q ) printf("srcfile!_!%s!_!1!_!\n",argv[4]);
yylex();
exit(exit_status);
}
void
push_inputfile (fname)
char* fname;
{
if ( inputfile_stk_top >= INPUTFILE_STK_SIZE ) {
fprintf(stderr,"\\input's too deeply nested\n");
exit(1);
}
/* record where we are in current file */
inputfile_stk[ inputfile_stk_top ].lineno = lineno;
inputfile_stk[ inputfile_stk_top ].handle = YY_CURRENT_BUFFER;
/* actual push */
inputfile_stk_top++;
/* open new file fname */
if ((yyin = fopen(fname, "r")) == NULL) {
fprintf(stderr,"%s:%d: can't open \\input file %s\n", srcfile_name, lineno, fname);
exit(1);
}
yy_switch_to_buffer( yy_create_buffer( yyin, YY_BUF_SIZE ) );
/* report that's where we are */
if (! IS_LIT2PGM_Q) printf("srcfile!_!%s!_!1!_!\n",fname);
/* set srcfile_name and lineno for new file */
strcpy(srcfile_name, fname); /* for my own tracking */
lineno = 1;
/* push/record everything for new file */
inputfile_stk[ inputfile_stk_top ].name = srcfile_name;
inputfile_stk[ inputfile_stk_top ].lineno = lineno;
}
void
pop_inputfile ()
{
inputfile_stk_top--; /* actual pop */
/* remind where we were */
strcpy(srcfile_name, inputfile_stk[inputfile_stk_top].name);
lineno = inputfile_stk[inputfile_stk_top].lineno;
/* put in an extra newline or two before the next file;
otherwise, the cat'ting of files gives things like:
> last line of code
\section{Next section}
which happens to be very bad for lit2stuff.
*/
if (! IS_LIT2PGM_Q) printf("\n\nsrcfile!_!%s!_!%d!_!\n",srcfile_name,lineno);
/* return to prev inputfile */
yy_switch_to_buffer( inputfile_stk[inputfile_stk_top].handle );
}
void
myecho ()
{
myprintstr(yytext);
}
void
myprintstr (str)
char *str;
{
if (SHLD_PRINT) {
printf("%s",str);
}
}
void
print_leading_whitespace ()
{
int i;
for (i = 0; yytext[i] == ' ' || yytext[i] == '\t'; i++) {
putchar(yytext[i]);
}
}
void
not_OK(msg)
char *msg;
{
/* a good old emacsable error msg */
fprintf(stderr,"%s:%d: %s", srcfile_name, lineno, msg);
exit_status++;
}
void
warning(msg) /* exit_status not fiddled */
char *msg;
{
/* a good old emacsable warning msg */
fprintf(stderr,"%s:%d: [warning] %s", srcfile_name, lineno, msg);
}
|