-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.c
1719 lines (1634 loc) · 45.8 KB
/
input.c
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* input.c: read the source form
*/
#include <stdio.h>
#include <assert.h>
#include <time.h>
#include "halibut.h"
#define TAB_STOP 8 /* for column number tracking */
static void setpos(input *in, char *fname) {
in->pos.filename = fname;
in->pos.line = 1;
in->pos.col = (in->reportcols ? 1 : -1);
}
static void unget(input *in, int c, filepos *pos) {
if (in->npushback >= in->pushbacksize) {
in->pushbacksize = in->npushback + 16;
in->pushback = sresize(in->pushback, in->pushbacksize, pushback);
}
in->pushback[in->npushback].chr = c;
in->pushback[in->npushback].pos = *pos; /* structure copy */
in->npushback++;
}
/* ---------------------------------------------------------------------- */
/*
* Macro subsystem
*/
typedef struct macro_Tag macro;
struct macro_Tag {
wchar_t *name, *text;
};
struct macrostack_Tag {
macrostack *next;
wchar_t *text;
int ptr, npushback;
filepos pos;
};
static int macrocmp(const void *av, const void *bv, void *cmpctx) {
macro *a = (macro *)av, *b = (macro *)bv;
return ustrcmp(a->name, b->name);
}
static void macrodef(tree234 *macros, wchar_t *name, wchar_t *text,
filepos fpos, errorstate *es) {
macro *m = snew(macro);
m->name = name;
m->text = text;
if (add234(macros, m) != m) {
err_macroexists(es, &fpos, name);
sfree(name);
sfree(text);
}
}
static bool macrolookup(tree234 *macros, input *in, wchar_t *name,
filepos *pos) {
macro m, *gotit;
m.name = name;
gotit = find234(macros, &m);
if (gotit) {
macrostack *expansion = snew(macrostack);
expansion->next = in->stack;
expansion->text = gotit->text;
expansion->pos = *pos; /* structure copy */
expansion->ptr = 0;
expansion->npushback = in->npushback;
in->stack = expansion;
return true;
} else
return false;
}
static void macrocleanup(tree234 *macros) {
int ti;
macro *m;
for (ti = 0; (m = (macro *)index234(macros, ti)) != NULL; ti++) {
sfree(m->name);
sfree(m->text);
sfree(m);
}
freetree234(macros);
}
static void input_configure(input *in, paragraph *cfg) {
assert(cfg->type == para_Config);
if (!ustricmp(cfg->keyword, L"input-charset")) {
in->charset = charset_from_ustr(&cfg->fpos, uadv(cfg->keyword),
in->es);
}
}
/*
* Can return EOF
*/
static int get(input *in, filepos *pos, rdstringc *rsc) {
int pushbackpt = in->stack ? in->stack->npushback : 0;
if (in->npushback > pushbackpt) {
--in->npushback;
if (pos)
*pos = in->pushback[in->npushback].pos; /* structure copy */
return in->pushback[in->npushback].chr;
}
else if (in->stack) {
wchar_t c = in->stack->text[in->stack->ptr];
if (pos)
*pos = in->stack->pos;
if (in->stack->text[++in->stack->ptr] == L'\0') {
macrostack *tmp = in->stack;
in->stack = tmp->next;
sfree(tmp);
}
return c;
}
else if (in->currfp) {
while (in->wcpos >= in->nwc) {
int c = getc(in->currfp);
if (c == EOF) {
if (in->wantclose)
fclose(in->currfp);
in->currfp = NULL;
return EOF;
}
if (rsc)
rdaddc(rsc, c);
/* Track line numbers, for error reporting */
if (pos)
*pos = in->pos;
if (in->reportcols) {
switch (c) {
case '\t':
in->pos.col = 1 + (in->pos.col + TAB_STOP-1) % TAB_STOP;
break;
case '\n':
in->pos.col = 1;
in->pos.line++;
break;
default:
in->pos.col++;
break;
}
} else {
in->pos.col = -1;
if (c == '\n')
in->pos.line++;
}
/*
* Do input character set translation, so that we return
* Unicode.
*/
{
char buf[1];
char const *p;
int inlen;
buf[0] = (char)c;
p = buf;
inlen = 1;
in->nwc = charset_to_unicode(&p, &inlen,
in->wc, lenof(in->wc),
in->charset, &in->csstate,
NULL, 0);
assert(p == buf+1 && inlen == 0);
for (int i = 0; i < in->nwc; i++) {
if (in->wc[i] == 0) {
/* The zero Unicode character is never legal */
err_zerochar(in->es, pos);
return EOF;
}
}
in->wcpos = 0;
}
}
wchar_t wc = in->wc[in->wcpos++];
return wc;
} else
return EOF;
}
/*
* Lexical analysis of source files.
*/
typedef struct token_Tag token;
struct token_Tag {
int type;
int cmd, aux;
wchar_t *text;
char *origtext;
filepos pos;
};
enum {
tok_eof, /* end of file */
tok_eop, /* end of paragraph */
tok_white, /* whitespace */
tok_word, /* a word or word fragment */
tok_cmd, /* \command */
tok_lbrace, /* { */
tok_rbrace /* } */
};
/* Halibut command keywords. */
enum {
c__invalid, /* invalid command */
c__comment, /* comment command (\#) */
c__escaped, /* escaped character */
c__nop, /* no-op */
c__nbsp, /* nonbreaking space */
c_A, /* appendix heading */
c_B, /* bibliography entry */
c_BR, /* bibliography rewrite */
c_C, /* chapter heading */
c_H, /* heading */
c_I, /* invisible index mark */
c_IM, /* index merge/rewrite */
c_K, /* capitalised cross-reference */
c_S, /* aux field is 0, 1, 2, ... */
c_U, /* unnumbered-chapter heading */
c_W, /* Web hyperlink */
c_b, /* bulletted list */
c_c, /* code */
c_cfg, /* configuration directive */
c_copyright, /* copyright statement */
c_cq, /* quoted code (sugar for \q{\cw{x}}) */
c_cw, /* weak code */
c_date, /* document processing date */
c_dd, /* description list: description */
c_define, /* macro definition */
c_dt, /* description list: described thing */
c_e, /* emphasis */
c_i, /* visible index mark */
c_ii, /* uncapitalised visible index mark */
c_k, /* uncapitalised cross-reference */
c_lcont, /* continuation para(s) for list item */
c_n, /* numbered list */
c_nocite, /* bibliography trickery */
c_preamble, /* (obsolete) preamble text */
c_q, /* quote marks */
c_quote, /* block-quoted paragraphs */
c_rule, /* horizontal rule */
c_s, /* strong */
c_title, /* document title */
c_u, /* aux field is char code */
c_versionid /* document RCS id */
};
/* Perhaps whitespace should be defined in a more Unicode-friendly way? */
#define iswhite(c) ( (c)==32 || (c)==9 || (c)==13 || (c)==10 )
#define isnl(c) ( (c)==10 )
#define isdec(c) ( ((c)>='0'&&(c)<='9') )
#define fromdec(c) ( (c)-'0' )
#define ishex(c) ( ((c)>='0'&&(c)<='9') || ((c)>='A'&&(c)<='F') || ((c)>='a'&&(c)<='f'))
#define fromhex(c) ( (c)<='9' ? (c)-'0' : ((c)&0xDF) - ('A'-10) )
#define iscmd(c) ( ((c)>='0'&&(c)<='9') || ((c)>='A'&&(c)<='Z') || ((c)>='a'&&(c)<='z'))
/*
* Keyword comparison function. Like strcmp, but between a wchar_t *
* and a char *.
*/
static int kwcmp(wchar_t const *p, char const *q) {
int i;
do {
i = *p - *q;
} while (*p++ && *q++ && !i);
return i;
}
/*
* Match a keyword.
*/
static void match_kw(token *tok) {
/*
* FIXME. The ids are explicit in here so as to allow long-name
* equivalents to the various very short keywords.
*/
static const struct { char const *name; int id; } keywords[] = {
{"#", c__comment}, /* comment command (\#) */
{"-", c__escaped}, /* nonbreaking hyphen */
{".", c__nop}, /* no-op */
{"A", c_A}, /* appendix heading */
{"B", c_B}, /* bibliography entry */
{"BR", c_BR}, /* bibliography rewrite */
{"C", c_C}, /* chapter heading */
{"H", c_H}, /* heading */
{"I", c_I}, /* invisible index mark */
{"IM", c_IM}, /* index merge/rewrite */
{"K", c_K}, /* capitalised cross-reference */
{"U", c_U}, /* unnumbered-chapter heading */
{"W", c_W}, /* Web hyperlink */
{"\\", c__escaped}, /* escaped backslash (\\) */
{"_", c__nbsp}, /* nonbreaking space (\_) */
{"b", c_b}, /* bulletted list */
{"c", c_c}, /* code */
{"cfg", c_cfg}, /* configuration directive */
{"copyright", c_copyright}, /* copyright statement */
{"cq", c_cq}, /* quoted code (sugar for \q{\cw{x}}) */
{"cw", c_cw}, /* weak code */
{"date", c_date}, /* document processing date */
{"dd", c_dd}, /* description list: description */
{"define", c_define}, /* macro definition */
{"dt", c_dt}, /* description list: described thing */
{"e", c_e}, /* emphasis */
{"i", c_i}, /* visible index mark */
{"ii", c_ii}, /* uncapitalised visible index mark */
{"k", c_k}, /* uncapitalised cross-reference */
{"lcont", c_lcont}, /* continuation para(s) for list item */
{"n", c_n}, /* numbered list */
{"nocite", c_nocite}, /* bibliography trickery */
{"preamble", c_preamble}, /* (obsolete) preamble text */
{"q", c_q}, /* quote marks */
{"quote", c_quote}, /* block-quoted paragraphs */
{"rule", c_rule}, /* horizontal rule */
{"s", c_s}, /* strong */
{"title", c_title}, /* document title */
{"versionid", c_versionid}, /* document RCS id */
{"{", c__escaped}, /* escaped lbrace (\{) */
{"}", c__escaped}, /* escaped rbrace (\}) */
};
int i, j, k, c;
/*
* Special cases: \S{0,1,2,...} and \uABCD. If the syntax
* doesn't match correctly, we just fall through to the
* binary-search phase.
*/
if (tok->text[0] == 'S') {
/* We expect numeric characters thereafter. */
wchar_t *p = tok->text+1;
int n;
if (!*p)
n = 1;
else {
n = 0;
while (*p && isdec(*p)) {
n = 10 * n + fromdec(*p);
p++;
}
}
if (!*p) {
tok->cmd = c_S;
tok->aux = n;
return;
}
} else if (tok->text[0] == 'u') {
/* We expect hex characters thereafter. */
wchar_t *p = tok->text+1;
int n = 0;
bool seen_a_char = false;
while (*p && ishex(*p)) {
seen_a_char = true;
n = 16 * n + fromhex(*p);
p++;
}
if (!*p && seen_a_char) {
tok->cmd = c_u;
tok->aux = n;
return;
}
}
i = -1;
j = sizeof(keywords)/sizeof(*keywords);
while (j-i > 1) {
k = (i+j)/2;
c = kwcmp(tok->text, keywords[k].name);
if (c < 0)
j = k;
else if (c > 0)
i = k;
else /* c == 0 */ {
tok->cmd = keywords[k].id;
return;
}
}
tok->cmd = c__invalid;
}
/*
* Read a token from the input file, in the normal way (`normal' in
* the sense that code paragraphs work a different way).
*/
token get_token(input *in) {
int c;
int nls;
int prevpos;
token ret;
rdstring rs = { 0, 0, NULL };
rdstringc rsc = { 0, 0, NULL };
filepos cpos;
ret.text = NULL; /* default */
ret.origtext = NULL; /* default */
if (in->pushback_chars) {
rdaddsc(&rsc, in->pushback_chars);
sfree(in->pushback_chars);
in->pushback_chars = NULL;
}
c = get(in, &cpos, &rsc);
ret.pos = cpos;
if (iswhite(c)) { /* tok_white or tok_eop */
nls = 0;
prevpos = 0;
do {
if (isnl(c))
nls++;
prevpos = rsc.pos;
} while ((c = get(in, &cpos, &rsc)) != EOF && iswhite(c));
if (c == EOF) {
ret.type = tok_eof;
sfree(rsc.text);
return ret;
}
if (rsc.text) {
in->pushback_chars = dupstr(rsc.text + prevpos);
sfree(rsc.text);
}
unget(in, c, &cpos);
ret.type = (nls > 1 ? tok_eop : tok_white);
return ret;
} else if (c == EOF) { /* tok_eof */
ret.type = tok_eof;
sfree(rsc.text);
return ret;
} else if (c == '\\') { /* tok_cmd */
rsc.pos = prevpos = 0;
c = get(in, &cpos, &rsc);
if (c == '-' || c == '\\' || c == '_' ||
c == '#' || c == '{' || c == '}' || c == '.') {
/* single-char command */
rdadd(&rs, c);
prevpos = rsc.pos;
} else if (c == 'u') {
int len = 0;
do {
rdadd(&rs, c);
len++;
prevpos = rsc.pos;
c = get(in, &cpos, &rsc);
} while (ishex(c) && len < 5);
unget(in, c, &cpos);
} else if (iscmd(c)) {
do {
rdadd(&rs, c);
prevpos = rsc.pos;
c = get(in, &cpos, &rsc);
} while (iscmd(c));
unget(in, c, &cpos);
}
/*
* Now match the command against the list of available
* ones.
*/
ret.type = tok_cmd;
ret.text = ustrdup(rs.text);
if (rsc.text) {
in->pushback_chars = dupstr(rsc.text + prevpos);
rsc.text[prevpos] = '\0';
ret.origtext = dupstr(rsc.text);
} else {
ret.origtext = dupstr("");
}
match_kw(&ret);
sfree(rs.text);
sfree(rsc.text);
return ret;
} else if (c == '{') { /* tok_lbrace */
ret.type = tok_lbrace;
sfree(rsc.text);
return ret;
} else if (c == '}') { /* tok_rbrace */
ret.type = tok_rbrace;
sfree(rsc.text);
return ret;
} else { /* tok_word */
/*
* Read a word: the longest possible contiguous sequence of
* things other than whitespace, backslash, braces and
* hyphen. A hyphen terminates the word but is returned as
* part of it; everything else is pushed back for the next
* token. The `aux' field contains true if the word ends in
* a hyphen.
*/
ret.aux = false; /* assumed for now */
prevpos = 0;
while (1) {
if (iswhite(c) || c=='{' || c=='}' || c=='\\' || c==EOF) {
/* Put back the character that caused termination */
unget(in, c, &cpos);
break;
} else {
rdadd(&rs, c);
if (c == '-') {
prevpos = rsc.pos;
ret.aux = true;
break; /* hyphen terminates word */
}
}
prevpos = rsc.pos;
c = get(in, &cpos, &rsc);
}
ret.type = tok_word;
ret.text = ustrdup(rs.text);
if (rsc.text) {
in->pushback_chars = dupstr(rsc.text + prevpos);
rsc.text[prevpos] = '\0';
ret.origtext = dupstr(rsc.text);
} else {
ret.origtext = dupstr("");
}
sfree(rs.text);
sfree(rsc.text);
return ret;
}
}
/*
* Determine whether the next input character is an open brace (for
* telling code paragraphs from paragraphs which merely start with
* code).
*/
bool isbrace(input *in) {
int c;
filepos cpos;
c = get(in, &cpos, NULL);
unget(in, c, &cpos);
return (c == '{');
}
/*
* Read the rest of a line that starts `\c'. Including nothing at
* all (tok_word with empty text).
*/
token get_codepar_token(input *in) {
int c;
token ret;
rdstring rs = { 0, 0, NULL };
filepos cpos;
ret.type = tok_word;
ret.origtext = NULL;
c = get(in, &cpos, NULL); /* expect (and discard) one space */
ret.pos = cpos;
if (c == ' ') {
c = get(in, &cpos, NULL);
ret.pos = cpos;
}
while (!isnl(c) && c != EOF) {
int c2 = c;
c = get(in, &cpos, NULL);
/* Discard \r just before \n. */
if (c2 != 13 || !isnl(c))
rdadd(&rs, c2);
}
unget(in, c, &cpos);
ret.text = ustrdup(rs.text);
sfree(rs.text);
return ret;
}
/*
* Adds a new word to a linked list
*/
static word *addword(word newword, word ***hptrptr) {
word *mnewword;
if (!hptrptr)
return NULL;
mnewword = snew(word);
newword.private_data = NULL; /* placate gcc warning */
*mnewword = newword; /* structure copy */
mnewword->next = NULL;
**hptrptr = mnewword;
*hptrptr = &mnewword->next;
return mnewword;
}
/*
* Adds a new paragraph to a linked list
*/
static paragraph *addpara(paragraph newpara, paragraph ***hptrptr) {
paragraph *mnewpara = snew(paragraph);
*mnewpara = newpara; /* structure copy */
mnewpara->next = NULL;
**hptrptr = mnewpara;
*hptrptr = &mnewpara->next;
return mnewpara;
}
/*
* Destructor before token is reassigned; should catch most memory
* leaks
*/
#define dtor(t) ( sfree(t.text), sfree(t.origtext) )
/*
* Reads a single file (ie until get() returns EOF)
*/
static void read_file(paragraph ***ret, input *in, indexdata *idx,
tree234 *macros) {
token t;
paragraph par;
word wd, **whptr, **idximplicit = NULL;
wchar_t utext[2], *wdtext;
int style, spcstyle;
bool already;
bool iswhite, seenwhite;
int prev_para_type = para_NotParaType;
struct stack_item {
enum {
stack_nop = 0, /* do nothing (for error recovery) */
stack_ualt = 1, /* \u alternative */
stack_style = 2, /* \e, \c, \cw */
stack_idx = 4, /* \I, \i, \ii */
stack_hyper = 8, /* \W */
stack_quote = 16 /* \q */
} type;
word **whptr; /* to restore from \u alternatives */
word **idximplicit; /* to restore from \u alternatives */
filepos fpos;
int in_code;
} *sitem;
stack parsestk;
struct crossparaitem {
int type; /* currently c_lcont, c_quote or -1 */
bool seen_lcont, seen_quote;
};
stack crossparastk;
word *indexword = NULL, *uword, *iword;
word *idxwordlist;
rdstring indexstr;
bool index_downcase = false, index_visible = false, indexing;
const rdstring nullrs = { 0, 0, NULL };
wchar_t uchr;
t = get_token(in);
already = true;
/*
* Ignore tok_white if it appears at the very start of the file.
*
* At the start of most paragraphs, tok_white is guaranteed not to
* appear, because get_token will have folded it into the
* preceding tok_eop (since a tok_eop is simply a sequence of
* whitespace containing at least two newlines).
*
* The one exception is if there isn't a preceding tok_eop, i.e.
* if the very first paragraph begins with something that lexes as
* a tok_white. Easiest way to get round that is to ignore it
* here, by unsetting the 'already' flag which will force a new
* token to be fetched below.
*/
if (t.type == tok_white)
already = false;
crossparastk = stk_new();
/*
* Loop on each paragraph.
*/
while (1) {
int start_cmd = c__invalid;
par.words = NULL;
par.keyword = NULL;
par.origkeyword = NULL;
whptr = &par.words;
/*
* Get a token.
*/
do {
if (!already) {
dtor(t), t = get_token(in);
}
already = false;
} while (t.type == tok_eop);
if (t.type == tok_eof)
break;
/*
* Parse code paragraphs separately.
*/
if (t.type == tok_cmd && t.cmd == c_c && !isbrace(in)) {
int wtype = word_WeakCode;
par.type = para_Code;
par.fpos = t.pos;
while (1) {
dtor(t), t = get_codepar_token(in);
wd.type = wtype;
wd.breaks = false; /* shouldn't need this... */
wd.text = ustrdup(t.text);
wd.alt = NULL;
wd.aux = 0;
wd.fpos = t.pos;
addword(wd, &whptr);
dtor(t), t = get_token(in);
if (t.type == tok_white) {
/*
* The newline after a code-paragraph line
*/
dtor(t), t = get_token(in);
}
if (t.type == tok_eop || t.type == tok_eof ||
t.type == tok_rbrace) { /* might be } terminating \lcont */
if (t.type == tok_rbrace)
already = true;
break;
} else if (t.type == tok_cmd && t.cmd == c_c) {
wtype = word_WeakCode;
} else if (t.type == tok_cmd && t.cmd == c_e &&
wtype == word_WeakCode) {
wtype = word_Emph;
} else if (t.type == tok_cmd && t.cmd == c_s &&
wtype == word_WeakCode) {
wtype = word_Strong;
} else {
err_brokencodepara(in->es, &t.pos);
prev_para_type = par.type;
addpara(par, ret);
while (t.type != tok_eop) /* error recovery: */
dtor(t), t = get_token(in); /* eat rest of paragraph */
goto codeparabroken; /* ick, but such is life */
}
}
prev_para_type = par.type;
addpara(par, ret);
codeparabroken:
continue;
}
/*
* Spot the special commands that define a grouping of more
* than one paragraph, and also the closing braces that
* finish them.
*/
if (t.type == tok_cmd &&
(t.cmd == c_lcont || t.cmd == c_quote)) {
struct crossparaitem *sitem, *stop;
int cmd = t.cmd;
/*
* Expect, and swallow, an open brace.
*/
dtor(t), t = get_token(in);
if (t.type != tok_lbrace) {
err_explbr(in->es, &t.pos);
continue;
}
/*
* Also expect, and swallow, any whitespace after that
* (a newline before a code paragraph wouldn't be
* surprising).
*/
do {
dtor(t), t = get_token(in);
} while (t.type == tok_white);
already = true;
if (cmd == c_lcont) {
/*
* \lcont causes a continuation of a list item into
* multiple paragraphs (which may in turn contain
* nested lists, code paras etc). Hence, the previous
* paragraph must be of a list type.
*/
sitem = snew(struct crossparaitem);
stop = (struct crossparaitem *)stk_top(crossparastk);
if (stop) {
*sitem = *stop;
} else {
sitem->seen_quote = false;
sitem->seen_lcont = false;
}
if (prev_para_type == para_Bullet ||
prev_para_type == para_NumberedList ||
prev_para_type == para_Description) {
sitem->type = c_lcont;
sitem->seen_lcont = true;
par.type = para_LcontPush;
prev_para_type = par.type;
addpara(par, ret);
} else {
/*
* Push a null item on the cross-para stack so that
* when we see the corresponding closing brace we
* don't give a cascade error.
*/
sitem->type = -1;
err_misplacedlcont(in->es, &t.pos);
}
} else {
/*
* \quote causes a group of paragraphs to be
* block-quoted (typically they will be indented a
* bit).
*/
sitem = snew(struct crossparaitem);
stop = (struct crossparaitem *)stk_top(crossparastk);
if (stop) {
*sitem = *stop;
} else {
sitem->seen_quote = false;
sitem->seen_lcont = false;
}
sitem->type = c_quote;
sitem->seen_quote = true;
par.type = para_QuotePush;
prev_para_type = par.type;
addpara(par, ret);
}
stk_push(crossparastk, sitem);
continue;
} else if (t.type == tok_rbrace) {
struct crossparaitem *sitem = stk_pop(crossparastk);
if (!sitem)
err_unexbrace(in->es, &t.pos);
else {
switch (sitem->type) {
case c_lcont:
par.type = para_LcontPop;
prev_para_type = par.type;
addpara(par, ret);
break;
case c_quote:
par.type = para_QuotePop;
prev_para_type = par.type;
addpara(par, ret);
break;
}
sfree(sitem);
}
continue;
}
while (t.type == tok_cmd &&
macrolookup(macros, in, t.text, &t.pos)) {
dtor(t), t = get_token(in);
}
/*
* This token begins a paragraph. See if it's one of the
* special commands that define a paragraph type.
*
* (note that \# is special in a way, and \nocite takes no
* text)
*/
par.type = para_Normal;
if (t.type == tok_cmd) {
int needkw;
bool is_macro = false;
par.fpos = t.pos;
switch (t.cmd) {
default:
needkw = -1;
break;
case c__invalid:
err_badparatype(in->es, t.text, &t.pos);
needkw = 4;
break;
case c__comment:
if (isbrace(in)) {
needkw = -1;
break; /* `\#{': isn't a comment para */
}
do {
dtor(t), t = get_token(in);
} while (t.type != tok_eop && t.type != tok_eof);
continue; /* next paragraph */
/*
* `needkw' values:
*
* 1 -- exactly one keyword
* 2 -- at least one keyword
* 4 -- any number of keywords including zero
* 8 -- at least one keyword and then nothing else
* 16 -- nothing at all! no keywords, no body
* 32 -- no keywords at all
*/
case c_A: needkw = 2; par.type = para_Appendix; break;
case c_B: needkw = 2; par.type = para_Biblio; break;
case c_BR: needkw = 1; par.type = para_BR;
start_cmd = c_BR; break;
case c_C: needkw = 2; par.type = para_Chapter; break;
case c_H: needkw = 2; par.type = para_Heading;
par.aux = 0;
break;
case c_IM: needkw = 2; par.type = para_IM;
start_cmd = c_IM; break;
case c_S: needkw = 2; par.type = para_Subsect;
par.aux = t.aux; break;
case c_U: needkw = 32; par.type = para_UnnumberedChapter; break;
/* For \b and \n the keyword is optional */
case c_b: needkw = 4; par.type = para_Bullet; break;
case c_dt: needkw = 4; par.type = para_DescribedThing; break;
case c_dd: needkw = 4; par.type = para_Description; break;
case c_n: needkw = 4; par.type = para_NumberedList; break;
case c_cfg: needkw = 8; par.type = para_Config;
start_cmd = c_cfg; break;
case c_copyright: needkw = 32; par.type = para_Copyright; break;
case c_define: is_macro = true; needkw = 1; break;
/* For \nocite the keyword is _everything_ */
case c_nocite: needkw = 8; par.type = para_NoCite; break;
case c_preamble: needkw = 32; par.type = para_Normal; break;
case c_rule: needkw = 16; par.type = para_Rule; break;
case c_title: needkw = 32; par.type = para_Title; break;
case c_versionid: needkw = 32; par.type = para_VersionID; break;
}
if (par.type == para_Chapter ||
par.type == para_Heading ||
par.type == para_Subsect ||
par.type == para_Appendix ||
par.type == para_UnnumberedChapter) {
struct crossparaitem *sitem = stk_top(crossparastk);
if (sitem && (sitem->seen_lcont || sitem->seen_quote)) {
err_sectmarkerinblock(
in->es, &t.pos,
(sitem->seen_lcont ? "lcont" : "quote"));
}
}
if (needkw > 0) {
rdstring rs = { 0, 0, NULL };
rdstringc rsc = { 0, 0, NULL };
int nkeys = 0;
filepos fp;
/* Get keywords. */
dtor(t), t = get_token(in);
fp = t.pos;
while (t.type == tok_lbrace ||
(t.type == tok_white && (needkw & 24))) {
/*
* In paragraph types which can't accept any
* body text (such as \cfg), we are lenient
* about whitespace between keywords. This is
* important for \cfg in particular since it
* can often have many keywords which are long
* pieces of text, so it's useful to permit the
* user to wrap the line between them.
*/
if (t.type == tok_white) {
dtor(t), t = get_token(in); /* eat the space */
continue;
}
/* This is a keyword. */
nkeys++;
/* FIXME: there will be bugs if anyone specifies an
* empty keyword (\foo{}), so trap this case. */
while (dtor(t), t = get_token(in),
t.type == tok_word ||
t.type == tok_white ||
(t.type == tok_cmd && t.cmd == c__nbsp) ||
(t.type == tok_cmd && t.cmd == c__escaped) ||
(t.type == tok_cmd && t.cmd == c_u)) {
if (t.type == tok_white ||
(t.type == tok_cmd && t.cmd == c__nbsp)) {
rdadd(&rs, ' ');
rdaddc(&rsc, ' ');
} else if (t.type == tok_cmd && t.cmd == c_u) {
rdadd(&rs, t.aux);
rdaddc(&rsc, '\\');
rdaddsc(&rsc, t.origtext);
} else {
rdadds(&rs, t.text);
rdaddsc(&rsc, t.origtext);
}
}
if (t.type != tok_rbrace) {
err_kwunclosed(in->es, &t.pos);
continue;
}
rdadd(&rs, 0); /* add string terminator */
rdaddc(&rsc, 0); /* add string terminator */
dtor(t), t = get_token(in); /* eat right brace */
}
rdadd(&rs, 0); /* add string terminator */
rdaddc(&rsc, 0); /* add string terminator */
/* See whether we have the right number of keywords. */
if ((needkw & 48) && nkeys > 0)
err_kwillegal(in->es, &fp);