Annotation of mandoc/mdoc_macro.c, Revision 1.27
1.27 ! kristaps 1: /* $Id: mdoc_macro.c,v 1.26 2009/07/24 12:17:25 kristaps Exp $ */
1.1 kristaps 2: /*
1.9 kristaps 3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
1.8 kristaps 6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 8: *
1.8 kristaps 9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 kristaps 16: */
17: #include <assert.h>
18: #include <ctype.h>
19: #include <stdlib.h>
20: #include <stdio.h>
21: #include <string.h>
22:
23: #include "libmdoc.h"
24:
25: #define REWIND_REWIND (1 << 0)
26: #define REWIND_NOHALT (1 << 1)
27: #define REWIND_HALT (1 << 2)
28:
29: static int obsolete(MACRO_PROT_ARGS);
30: static int blk_part_exp(MACRO_PROT_ARGS);
31: static int in_line_eoln(MACRO_PROT_ARGS);
32: static int in_line_argn(MACRO_PROT_ARGS);
33: static int in_line(MACRO_PROT_ARGS);
34: static int blk_full(MACRO_PROT_ARGS);
35: static int blk_exp_close(MACRO_PROT_ARGS);
36: static int blk_part_imp(MACRO_PROT_ARGS);
37:
38: static int phrase(struct mdoc *, int, int, char *);
39: static int rew_dohalt(int, enum mdoc_type,
40: const struct mdoc_node *);
41: static int rew_alt(int);
42: static int rew_dobreak(int, const struct mdoc_node *);
43: static int rew_elem(struct mdoc *, int);
44: static int rew_impblock(struct mdoc *, int, int, int);
45: static int rew_expblock(struct mdoc *, int, int, int);
46: static int rew_subblock(enum mdoc_type,
47: struct mdoc *, int, int, int);
48: static int rew_last(struct mdoc *, struct mdoc_node *);
49: static int append_delims(struct mdoc *, int, int *, char *);
50: static int lookup(struct mdoc *, int, int, int, const char *);
51: static int swarn(struct mdoc *, enum mdoc_type, int, int,
52: const struct mdoc_node *);
53:
54: /* Central table of library: who gets parsed how. */
55:
56: const struct mdoc_macro __mdoc_macros[MDOC_MAX] = {
1.13 kristaps 57: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ap */
1.1 kristaps 58: { in_line_eoln, MDOC_PROLOGUE }, /* Dd */
59: { in_line_eoln, MDOC_PROLOGUE }, /* Dt */
60: { in_line_eoln, MDOC_PROLOGUE }, /* Os */
61: { blk_full, 0 }, /* Sh */
62: { blk_full, 0 }, /* Ss */
1.21 kristaps 63: { in_line_eoln, 0 }, /* Pp */
1.1 kristaps 64: { blk_part_imp, MDOC_PARSED }, /* D1 */
65: { blk_part_imp, MDOC_PARSED }, /* Dl */
66: { blk_full, MDOC_EXPLICIT }, /* Bd */
67: { blk_exp_close, MDOC_EXPLICIT }, /* Ed */
68: { blk_full, MDOC_EXPLICIT }, /* Bl */
69: { blk_exp_close, MDOC_EXPLICIT }, /* El */
70: { blk_full, MDOC_PARSED }, /* It */
71: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ad */
1.10 kristaps 72: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* An */
1.1 kristaps 73: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ar */
1.23 kristaps 74: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cd */
1.1 kristaps 75: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cm */
76: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Dv */
77: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Er */
78: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ev */
79: { in_line_eoln, 0 }, /* Ex */
80: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fa */
81: { in_line_eoln, 0 }, /* Fd */
82: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fl */
83: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fn */
1.11 kristaps 84: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ft */
1.1 kristaps 85: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ic */
1.18 kristaps 86: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* In */
1.1 kristaps 87: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Li */
1.19 kristaps 88: { blk_full, 0 }, /* Nd */
1.1 kristaps 89: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Nm */
90: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Op */
91: { obsolete, 0 }, /* Ot */
92: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Pa */
93: { in_line_eoln, 0 }, /* Rv */
94: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* St */
95: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Va */
96: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Vt */
97: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Xr */
98: { in_line_eoln, 0 }, /* %A */
99: { in_line_eoln, 0 }, /* %B */
100: { in_line_eoln, 0 }, /* %D */
101: { in_line_eoln, 0 }, /* %I */
102: { in_line_eoln, 0 }, /* %J */
103: { in_line_eoln, 0 }, /* %N */
104: { in_line_eoln, 0 }, /* %O */
105: { in_line_eoln, 0 }, /* %P */
106: { in_line_eoln, 0 }, /* %R */
107: { in_line_eoln, 0 }, /* %T */
108: { in_line_eoln, 0 }, /* %V */
109: { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ac */
110: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Ao */
111: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Aq */
112: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* At */
113: { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Bc */
114: { blk_full, MDOC_EXPLICIT }, /* Bf */
115: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Bo */
116: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Bq */
117: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bsx */
118: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bx */
119: { in_line_eoln, 0 }, /* Db */
120: { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Dc */
121: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Do */
122: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Dq */
123: { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ec */
124: { blk_exp_close, MDOC_EXPLICIT }, /* Ef */
125: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Em */
126: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Eo */
127: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Fx */
1.11 kristaps 128: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ms */
1.1 kristaps 129: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* No */
130: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ns */
131: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Nx */
132: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ox */
133: { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Pc */
134: { in_line_argn, MDOC_PARSED | MDOC_IGNDELIM }, /* Pf */
135: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Po */
136: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Pq */
137: { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Qc */
138: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Ql */
139: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Qo */
140: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Qq */
141: { blk_exp_close, MDOC_EXPLICIT }, /* Re */
142: { blk_full, MDOC_EXPLICIT }, /* Rs */
143: { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Sc */
144: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* So */
145: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Sq */
146: { in_line_eoln, 0 }, /* Sm */
147: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Sx */
148: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Sy */
149: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Tn */
150: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ux */
151: { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Xc */
152: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Xo */
153: { blk_full, MDOC_EXPLICIT | MDOC_CALLABLE }, /* Fo */
154: { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Fc */
155: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Oo */
156: { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Oc */
157: { blk_full, MDOC_EXPLICIT }, /* Bk */
158: { blk_exp_close, MDOC_EXPLICIT }, /* Ek */
159: { in_line_eoln, 0 }, /* Bt */
160: { in_line_eoln, 0 }, /* Hf */
161: { obsolete, 0 }, /* Fr */
162: { in_line_eoln, 0 }, /* Ud */
163: { in_line_eoln, 0 }, /* Lb */
1.21 kristaps 164: { in_line_eoln, 0 }, /* Lp */
1.12 kristaps 165: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Lk */
166: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Mt */
1.1 kristaps 167: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Brq */
168: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Bro */
169: { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Brc */
170: { in_line_eoln, 0 }, /* %C */
171: { obsolete, 0 }, /* Es */
172: { obsolete, 0 }, /* En */
173: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Dx */
174: { in_line_eoln, 0 }, /* %Q */
1.20 kristaps 175: { in_line_eoln, 0 }, /* br */
176: { in_line_eoln, 0 }, /* sp */
1.1 kristaps 177: };
178:
179: const struct mdoc_macro * const mdoc_macros = __mdoc_macros;
180:
181:
182: static int
183: swarn(struct mdoc *mdoc, enum mdoc_type type,
184: int line, int pos, const struct mdoc_node *p)
185: {
186: const char *n, *t, *tt;
187:
188: n = t = "<root>";
189: tt = "block";
190:
191: switch (type) {
192: case (MDOC_BODY):
193: tt = "multi-line";
194: break;
195: case (MDOC_HEAD):
196: tt = "line";
197: break;
198: default:
199: break;
200: }
201:
202: switch (p->type) {
203: case (MDOC_BLOCK):
204: n = mdoc_macronames[p->tok];
205: t = "block";
206: break;
207: case (MDOC_BODY):
208: n = mdoc_macronames[p->tok];
209: t = "multi-line";
210: break;
211: case (MDOC_HEAD):
212: n = mdoc_macronames[p->tok];
213: t = "line";
214: break;
215: default:
216: break;
217: }
218:
219: if ( ! (MDOC_IGN_SCOPE & mdoc->pflags))
1.16 kristaps 220: return(mdoc_verr(mdoc, line, pos,
1.1 kristaps 221: "%s scope breaks %s scope of %s",
222: tt, t, n));
1.16 kristaps 223: return(mdoc_vwarn(mdoc, line, pos,
1.1 kristaps 224: "%s scope breaks %s scope of %s",
225: tt, t, n));
226: }
227:
228:
229: /*
230: * This is called at the end of parsing. It must traverse up the tree,
231: * closing out open [implicit] scopes. Obviously, open explicit scopes
232: * are errors.
233: */
234: int
1.2 kristaps 235: mdoc_macroend(struct mdoc *mdoc)
1.1 kristaps 236: {
237: struct mdoc_node *n;
238:
239: /* Scan for open explicit scopes. */
240:
241: n = MDOC_VALID & mdoc->last->flags ?
242: mdoc->last->parent : mdoc->last;
243:
244: for ( ; n; n = n->parent) {
245: if (MDOC_BLOCK != n->type)
246: continue;
247: if ( ! (MDOC_EXPLICIT & mdoc_macros[n->tok].flags))
248: continue;
1.16 kristaps 249: return(mdoc_nerr(mdoc, n, EOPEN));
1.1 kristaps 250: }
251:
252: return(rew_last(mdoc, mdoc->first));
253: }
254:
255: static int
256: lookup(struct mdoc *mdoc, int line, int pos, int from, const char *p)
257: {
258: int res;
259:
1.6 kristaps 260: res = mdoc_hash_find(mdoc->htab, p);
1.1 kristaps 261: if (MDOC_PARSED & mdoc_macros[from].flags)
262: return(res);
263: if (MDOC_MAX == res)
264: return(res);
1.16 kristaps 265: if ( ! mdoc_pwarn(mdoc, line, pos, EMACPARM))
1.1 kristaps 266: return(-1);
267: return(MDOC_MAX);
268: }
269:
270:
271: static int
272: rew_last(struct mdoc *mdoc, struct mdoc_node *to)
273: {
274:
275: assert(to);
276: mdoc->next = MDOC_NEXT_SIBLING;
277:
278: /* LINTED */
279: while (mdoc->last != to) {
280: if ( ! mdoc_valid_post(mdoc))
281: return(0);
282: if ( ! mdoc_action_post(mdoc))
283: return(0);
284: mdoc->last = mdoc->last->parent;
285: assert(mdoc->last);
286: }
287:
288: if ( ! mdoc_valid_post(mdoc))
289: return(0);
290: return(mdoc_action_post(mdoc));
291: }
292:
293:
294: static int
295: rew_alt(int tok)
296: {
297: switch (tok) {
298: case (MDOC_Ac):
299: return(MDOC_Ao);
300: case (MDOC_Bc):
301: return(MDOC_Bo);
302: case (MDOC_Brc):
303: return(MDOC_Bro);
304: case (MDOC_Dc):
305: return(MDOC_Do);
306: case (MDOC_Ec):
307: return(MDOC_Eo);
308: case (MDOC_Ed):
309: return(MDOC_Bd);
310: case (MDOC_Ef):
311: return(MDOC_Bf);
312: case (MDOC_Ek):
313: return(MDOC_Bk);
314: case (MDOC_El):
315: return(MDOC_Bl);
316: case (MDOC_Fc):
317: return(MDOC_Fo);
318: case (MDOC_Oc):
319: return(MDOC_Oo);
320: case (MDOC_Pc):
321: return(MDOC_Po);
322: case (MDOC_Qc):
323: return(MDOC_Qo);
324: case (MDOC_Re):
325: return(MDOC_Rs);
326: case (MDOC_Sc):
327: return(MDOC_So);
328: case (MDOC_Xc):
329: return(MDOC_Xo);
330: default:
331: break;
332: }
333: abort();
334: /* NOTREACHED */
335: }
336:
337:
338: /*
339: * Rewind rules. This indicates whether to stop rewinding
340: * (REWIND_HALT) without touching our current scope, stop rewinding and
341: * close our current scope (REWIND_REWIND), or continue (REWIND_NOHALT).
342: * The scope-closing and so on occurs in the various rew_* routines.
343: */
344: static int
345: rew_dohalt(int tok, enum mdoc_type type, const struct mdoc_node *p)
346: {
347:
348: if (MDOC_ROOT == p->type)
349: return(REWIND_HALT);
350: if (MDOC_VALID & p->flags)
351: return(REWIND_NOHALT);
352:
353: switch (tok) {
354: case (MDOC_Aq):
355: /* FALLTHROUGH */
356: case (MDOC_Bq):
357: /* FALLTHROUGH */
358: case (MDOC_Brq):
359: /* FALLTHROUGH */
360: case (MDOC_D1):
361: /* FALLTHROUGH */
362: case (MDOC_Dl):
363: /* FALLTHROUGH */
364: case (MDOC_Dq):
365: /* FALLTHROUGH */
366: case (MDOC_Op):
367: /* FALLTHROUGH */
368: case (MDOC_Pq):
369: /* FALLTHROUGH */
370: case (MDOC_Ql):
371: /* FALLTHROUGH */
372: case (MDOC_Qq):
373: /* FALLTHROUGH */
374: case (MDOC_Sq):
375: assert(MDOC_HEAD != type);
376: assert(MDOC_TAIL != type);
377: if (type == p->type && tok == p->tok)
378: return(REWIND_REWIND);
379: break;
380: case (MDOC_It):
381: assert(MDOC_TAIL != type);
382: if (type == p->type && tok == p->tok)
383: return(REWIND_REWIND);
384: if (MDOC_BODY == p->type && MDOC_Bl == p->tok)
385: return(REWIND_HALT);
386: break;
387: case (MDOC_Sh):
388: if (type == p->type && tok == p->tok)
389: return(REWIND_REWIND);
390: break;
1.19 kristaps 391: case (MDOC_Nd):
392: /* FALLTHROUGH */
1.1 kristaps 393: case (MDOC_Ss):
394: assert(MDOC_TAIL != type);
395: if (type == p->type && tok == p->tok)
396: return(REWIND_REWIND);
397: if (MDOC_BODY == p->type && MDOC_Sh == p->tok)
398: return(REWIND_HALT);
399: break;
400: case (MDOC_Ao):
401: /* FALLTHROUGH */
402: case (MDOC_Bd):
403: /* FALLTHROUGH */
404: case (MDOC_Bf):
405: /* FALLTHROUGH */
406: case (MDOC_Bk):
407: /* FALLTHROUGH */
408: case (MDOC_Bl):
409: /* FALLTHROUGH */
410: case (MDOC_Bo):
411: /* FALLTHROUGH */
412: case (MDOC_Bro):
413: /* FALLTHROUGH */
414: case (MDOC_Do):
415: /* FALLTHROUGH */
416: case (MDOC_Eo):
417: /* FALLTHROUGH */
418: case (MDOC_Fo):
419: /* FALLTHROUGH */
420: case (MDOC_Oo):
421: /* FALLTHROUGH */
422: case (MDOC_Po):
423: /* FALLTHROUGH */
424: case (MDOC_Qo):
425: /* FALLTHROUGH */
426: case (MDOC_Rs):
427: /* FALLTHROUGH */
428: case (MDOC_So):
429: /* FALLTHROUGH */
430: case (MDOC_Xo):
431: if (type == p->type && tok == p->tok)
432: return(REWIND_REWIND);
433: break;
434:
435: /* Multi-line explicit scope close. */
436: case (MDOC_Ac):
437: /* FALLTHROUGH */
438: case (MDOC_Bc):
439: /* FALLTHROUGH */
440: case (MDOC_Brc):
441: /* FALLTHROUGH */
442: case (MDOC_Dc):
443: /* FALLTHROUGH */
444: case (MDOC_Ec):
445: /* FALLTHROUGH */
446: case (MDOC_Ed):
447: /* FALLTHROUGH */
448: case (MDOC_Ek):
449: /* FALLTHROUGH */
450: case (MDOC_El):
451: /* FALLTHROUGH */
452: case (MDOC_Fc):
453: /* FALLTHROUGH */
454: case (MDOC_Ef):
455: /* FALLTHROUGH */
456: case (MDOC_Oc):
457: /* FALLTHROUGH */
458: case (MDOC_Pc):
459: /* FALLTHROUGH */
460: case (MDOC_Qc):
461: /* FALLTHROUGH */
462: case (MDOC_Re):
463: /* FALLTHROUGH */
464: case (MDOC_Sc):
465: /* FALLTHROUGH */
466: case (MDOC_Xc):
467: if (type == p->type && rew_alt(tok) == p->tok)
468: return(REWIND_REWIND);
469: break;
470: default:
471: abort();
472: /* NOTREACHED */
473: }
474:
475: return(REWIND_NOHALT);
476: }
477:
478:
479: /*
480: * See if we can break an encountered scope (the rew_dohalt has returned
481: * REWIND_NOHALT).
482: */
483: static int
484: rew_dobreak(int tok, const struct mdoc_node *p)
485: {
486:
487: assert(MDOC_ROOT != p->type);
488: if (MDOC_ELEM == p->type)
489: return(1);
490: if (MDOC_TEXT == p->type)
491: return(1);
492: if (MDOC_VALID & p->flags)
493: return(1);
494:
495: switch (tok) {
496: case (MDOC_It):
497: return(MDOC_It == p->tok);
1.19 kristaps 498: case (MDOC_Nd):
499: return(MDOC_Nd == p->tok);
1.1 kristaps 500: case (MDOC_Ss):
501: return(MDOC_Ss == p->tok);
502: case (MDOC_Sh):
1.19 kristaps 503: if (MDOC_Nd == p->tok)
504: return(1);
1.1 kristaps 505: if (MDOC_Ss == p->tok)
506: return(1);
507: return(MDOC_Sh == p->tok);
508: case (MDOC_El):
509: if (MDOC_It == p->tok)
510: return(1);
511: break;
512: case (MDOC_Oc):
513: /* XXX - experimental! */
514: if (MDOC_Op == p->tok)
515: return(1);
516: break;
517: default:
518: break;
519: }
520:
521: if (MDOC_EXPLICIT & mdoc_macros[tok].flags)
522: return(p->tok == rew_alt(tok));
523: else if (MDOC_BLOCK == p->type)
524: return(1);
525:
526: return(tok == p->tok);
527: }
528:
529:
530: static int
531: rew_elem(struct mdoc *mdoc, int tok)
532: {
533: struct mdoc_node *n;
534:
535: n = mdoc->last;
536: if (MDOC_ELEM != n->type)
537: n = n->parent;
538: assert(MDOC_ELEM == n->type);
539: assert(tok == n->tok);
540:
541: return(rew_last(mdoc, n));
542: }
543:
544:
545: static int
546: rew_subblock(enum mdoc_type type, struct mdoc *mdoc,
547: int tok, int line, int ppos)
548: {
549: struct mdoc_node *n;
550: int c;
551:
552: /* LINTED */
553: for (n = mdoc->last; n; n = n->parent) {
554: c = rew_dohalt(tok, type, n);
555: if (REWIND_HALT == c)
556: return(1);
557: if (REWIND_REWIND == c)
558: break;
559: else if (rew_dobreak(tok, n))
560: continue;
561: if ( ! swarn(mdoc, type, line, ppos, n))
562: return(0);
563: }
564:
565: assert(n);
566: return(rew_last(mdoc, n));
567: }
568:
569:
570: static int
571: rew_expblock(struct mdoc *mdoc, int tok, int line, int ppos)
572: {
573: struct mdoc_node *n;
574: int c;
575:
576: /* LINTED */
577: for (n = mdoc->last; n; n = n->parent) {
578: c = rew_dohalt(tok, MDOC_BLOCK, n);
579: if (REWIND_HALT == c)
1.16 kristaps 580: return(mdoc_perr(mdoc, line, ppos, ENOCTX));
1.1 kristaps 581: if (REWIND_REWIND == c)
582: break;
583: else if (rew_dobreak(tok, n))
584: continue;
585: if ( ! swarn(mdoc, MDOC_BLOCK, line, ppos, n))
586: return(0);
587: }
588:
589: assert(n);
590: return(rew_last(mdoc, n));
591: }
592:
593:
594: static int
595: rew_impblock(struct mdoc *mdoc, int tok, int line, int ppos)
596: {
597: struct mdoc_node *n;
598: int c;
599:
600: /* LINTED */
601: for (n = mdoc->last; n; n = n->parent) {
602: c = rew_dohalt(tok, MDOC_BLOCK, n);
603: if (REWIND_HALT == c)
604: return(1);
605: else if (REWIND_REWIND == c)
606: break;
607: else if (rew_dobreak(tok, n))
608: continue;
609: if ( ! swarn(mdoc, MDOC_BLOCK, line, ppos, n))
610: return(0);
611: }
612:
613: assert(n);
614: return(rew_last(mdoc, n));
615: }
616:
617:
618: static int
619: append_delims(struct mdoc *mdoc, int line, int *pos, char *buf)
620: {
621: int c, lastarg;
622: char *p;
623:
624: if (0 == buf[*pos])
625: return(1);
626:
627: for (;;) {
628: lastarg = *pos;
629: c = mdoc_args(mdoc, line, pos, buf, 0, &p);
630: assert(ARGS_PHRASE != c);
631:
632: if (ARGS_ERROR == c)
633: return(0);
634: else if (ARGS_EOLN == c)
635: break;
636: assert(mdoc_isdelim(p));
637: if ( ! mdoc_word_alloc(mdoc, line, lastarg, p))
638: return(0);
639: mdoc->next = MDOC_NEXT_SIBLING;
640: }
641:
642: return(1);
643: }
644:
645:
646: /*
647: * Close out block partial/full explicit.
648: */
649: static int
650: blk_exp_close(MACRO_PROT_ARGS)
651: {
652: int j, c, lastarg, maxargs, flushed;
653: char *p;
654:
655: switch (tok) {
656: case (MDOC_Ec):
657: maxargs = 1;
658: break;
659: default:
660: maxargs = 0;
661: break;
662: }
663:
664: if ( ! (MDOC_CALLABLE & mdoc_macros[tok].flags)) {
1.22 kristaps 665: if (buf[*pos])
666: if ( ! mdoc_pwarn(mdoc, line, ppos, ENOLINE))
1.1 kristaps 667: return(0);
1.22 kristaps 668:
669: if ( ! rew_subblock(MDOC_BODY, mdoc, tok, line, ppos))
670: return(0);
671: return(rew_expblock(mdoc, tok, line, ppos));
1.1 kristaps 672: }
673:
674: if ( ! rew_subblock(MDOC_BODY, mdoc, tok, line, ppos))
675: return(0);
676:
677: if (maxargs > 0) {
678: if ( ! mdoc_tail_alloc(mdoc, line,
679: ppos, rew_alt(tok)))
680: return(0);
681: mdoc->next = MDOC_NEXT_CHILD;
682: }
683:
1.27 ! kristaps 684: for (flushed = j = 0; ; j++) {
1.1 kristaps 685: lastarg = *pos;
686:
687: if (j == maxargs && ! flushed) {
688: if ( ! rew_expblock(mdoc, tok, line, ppos))
689: return(0);
690: flushed = 1;
691: }
692:
693: c = mdoc_args(mdoc, line, pos, buf, tok, &p);
694:
695: if (ARGS_ERROR == c)
696: return(0);
697: if (ARGS_PUNCT == c)
698: break;
699: if (ARGS_EOLN == c)
700: break;
701:
702: if (-1 == (c = lookup(mdoc, line, lastarg, tok, p)))
703: return(0);
704: else if (MDOC_MAX != c) {
705: if ( ! flushed) {
706: if ( ! rew_expblock(mdoc, tok,
707: line, ppos))
708: return(0);
709: flushed = 1;
710: }
711: if ( ! mdoc_macro(mdoc, c, line, lastarg, pos, buf))
712: return(0);
713: break;
714: }
715:
716: if ( ! mdoc_word_alloc(mdoc, line, lastarg, p))
717: return(0);
718: mdoc->next = MDOC_NEXT_SIBLING;
719: }
720:
721: if ( ! flushed && ! rew_expblock(mdoc, tok, line, ppos))
722: return(0);
723:
724: if (ppos > 1)
725: return(1);
726: return(append_delims(mdoc, line, pos, buf));
727: }
728:
729:
730: /*
731: * In-line macros where reserved words cause scope close-reopen.
732: */
733: static int
734: in_line(MACRO_PROT_ARGS)
735: {
1.4 kristaps 736: int la, lastpunct, c, w, cnt, d, nc;
1.1 kristaps 737: struct mdoc_arg *arg;
738: char *p;
739:
1.4 kristaps 740: /*
741: * Whether we allow ignored elements (those without content,
742: * usually because of reserved words) to squeak by.
743: */
744: switch (tok) {
1.26 kristaps 745: case (MDOC_An):
746: /* FALLTHROUGH */
747: case (MDOC_Ar):
1.4 kristaps 748: /* FALLTHROUGH */
749: case (MDOC_Fl):
750: /* FALLTHROUGH */
1.12 kristaps 751: case (MDOC_Lk):
752: /* FALLTHROUGH */
1.26 kristaps 753: case (MDOC_Nm):
754: /* FALLTHROUGH */
1.25 kristaps 755: case (MDOC_Pa):
1.4 kristaps 756: nc = 1;
757: break;
758: default:
759: nc = 0;
760: break;
761: }
762:
1.27 ! kristaps 763: for (arg = NULL;; ) {
1.1 kristaps 764: la = *pos;
765: c = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
766:
767: if (ARGV_WORD == c) {
768: *pos = la;
769: break;
770: }
771: if (ARGV_EOLN == c)
772: break;
773: if (ARGV_ARG == c)
774: continue;
775:
776: mdoc_argv_free(arg);
777: return(0);
778: }
779:
1.4 kristaps 780: for (cnt = 0, lastpunct = 1;; ) {
1.1 kristaps 781: la = *pos;
782: w = mdoc_args(mdoc, line, pos, buf, tok, &p);
783:
784: if (ARGS_ERROR == w)
785: return(0);
786: if (ARGS_EOLN == w)
787: break;
788: if (ARGS_PUNCT == w)
789: break;
790:
791: /* Quoted words shouldn't be looked-up. */
792:
793: c = ARGS_QWORD == w ? MDOC_MAX :
794: lookup(mdoc, line, la, tok, p);
795:
1.4 kristaps 796: /*
797: * In this case, we've located a submacro and must
798: * execute it. Close out scope, if open. If no
799: * elements have been generated, either create one (nc)
800: * or raise a warning.
801: */
1.1 kristaps 802:
803: if (MDOC_MAX != c && -1 != c) {
804: if (0 == lastpunct && ! rew_elem(mdoc, tok))
805: return(0);
1.4 kristaps 806: if (nc && 0 == cnt) {
1.3 kristaps 807: if ( ! mdoc_elem_alloc(mdoc, line, ppos,
808: tok, arg))
809: return(0);
1.12 kristaps 810: if ( ! rew_last(mdoc, mdoc->last))
811: return(0);
1.7 kristaps 812: } else if ( ! nc && 0 == cnt) {
813: mdoc_argv_free(arg);
1.16 kristaps 814: if ( ! mdoc_pwarn(mdoc, line, ppos, EIGNE))
1.3 kristaps 815: return(0);
1.7 kristaps 816: }
1.1 kristaps 817: c = mdoc_macro(mdoc, c, line, la, pos, buf);
818: if (0 == c)
819: return(0);
820: if (ppos > 1)
821: return(1);
822: return(append_delims(mdoc, line, pos, buf));
823: } else if (-1 == c)
824: return(0);
825:
1.4 kristaps 826: /*
827: * Non-quote-enclosed punctuation. Set up our scope, if
828: * a word; rewind the scope, if a delimiter; then append
829: * the word.
830: */
1.1 kristaps 831:
1.3 kristaps 832: d = mdoc_isdelim(p);
833:
834: if (ARGS_QWORD != w && d) {
1.1 kristaps 835: if (0 == lastpunct && ! rew_elem(mdoc, tok))
836: return(0);
837: lastpunct = 1;
838: } else if (lastpunct) {
839: c = mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
840: if (0 == c)
841: return(0);
842: mdoc->next = MDOC_NEXT_CHILD;
843: lastpunct = 0;
844: }
845:
1.3 kristaps 846: if ( ! d)
847: cnt++;
1.1 kristaps 848: if ( ! mdoc_word_alloc(mdoc, line, la, p))
849: return(0);
850: mdoc->next = MDOC_NEXT_SIBLING;
851: }
852:
853: if (0 == lastpunct && ! rew_elem(mdoc, tok))
854: return(0);
1.4 kristaps 855:
856: /*
857: * If no elements have been collected and we're allowed to have
858: * empties (nc), open a scope and close it out. Otherwise,
859: * raise a warning.
860: *
861: */
862: if (nc && 0 == cnt) {
1.3 kristaps 863: c = mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
864: if (0 == c)
865: return(0);
1.12 kristaps 866: if ( ! rew_last(mdoc, mdoc->last))
867: return(0);
1.7 kristaps 868: } else if ( ! nc && 0 == cnt) {
869: mdoc_argv_free(arg);
1.16 kristaps 870: if ( ! mdoc_pwarn(mdoc, line, ppos, EIGNE))
1.3 kristaps 871: return(0);
1.7 kristaps 872: }
1.4 kristaps 873:
1.1 kristaps 874: if (ppos > 1)
875: return(1);
876: return(append_delims(mdoc, line, pos, buf));
877: }
878:
879:
880: /*
881: * Block full-explicit and full-implicit.
882: */
883: static int
884: blk_full(MACRO_PROT_ARGS)
885: {
1.19 kristaps 886: int c, lastarg, reopen, dohead;
1.1 kristaps 887: struct mdoc_arg *arg;
888: char *p;
889:
1.19 kristaps 890: /*
891: * Whether to process a block-head section. If this is
892: * non-zero, then a head will be opened for all line arguments.
893: * If not, then the head will always be empty and only a body
894: * will be opened, which will stay open at the eoln.
895: */
896:
897: switch (tok) {
898: case (MDOC_Nd):
899: dohead = 0;
900: break;
901: default:
902: dohead = 1;
903: break;
904: }
905:
1.1 kristaps 906: if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) {
907: if ( ! rew_subblock(MDOC_BODY, mdoc,
908: tok, line, ppos))
909: return(0);
910: if ( ! rew_impblock(mdoc, tok, line, ppos))
911: return(0);
912: }
913:
914: for (arg = NULL;; ) {
915: lastarg = *pos;
916: c = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
917:
918: if (ARGV_WORD == c) {
919: *pos = lastarg;
920: break;
921: }
922:
923: if (ARGV_EOLN == c)
924: break;
925: if (ARGV_ARG == c)
926: continue;
927:
928: mdoc_argv_free(arg);
929: return(0);
930: }
931:
932: if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, arg))
933: return(0);
934: mdoc->next = MDOC_NEXT_CHILD;
935:
936: if (0 == buf[*pos]) {
937: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
938: return(0);
939: if ( ! rew_subblock(MDOC_HEAD, mdoc,
940: tok, line, ppos))
941: return(0);
942: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
943: return(0);
944: mdoc->next = MDOC_NEXT_CHILD;
945: return(1);
946: }
947:
948: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
949: return(0);
1.19 kristaps 950:
951: /* Immediately close out head and enter body, if applicable. */
952:
953: if (0 == dohead) {
954: if ( ! rew_subblock(MDOC_HEAD, mdoc, tok, line, ppos))
955: return(0);
956: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
957: return(0);
958: }
959:
1.1 kristaps 960: mdoc->next = MDOC_NEXT_CHILD;
961:
962: for (reopen = 0;; ) {
963: lastarg = *pos;
964: c = mdoc_args(mdoc, line, pos, buf, tok, &p);
965:
966: if (ARGS_ERROR == c)
967: return(0);
968: if (ARGS_EOLN == c)
969: break;
970: if (ARGS_PHRASE == c) {
1.19 kristaps 971: assert(dohead);
1.1 kristaps 972: if (reopen && ! mdoc_head_alloc
973: (mdoc, line, ppos, tok))
974: return(0);
975: mdoc->next = MDOC_NEXT_CHILD;
976: /*
977: * Phrases are self-contained macro phrases used
978: * in the columnar output of a macro. They need
979: * special handling.
980: */
981: if ( ! phrase(mdoc, line, lastarg, buf))
982: return(0);
983: if ( ! rew_subblock(MDOC_HEAD, mdoc,
984: tok, line, ppos))
985: return(0);
986:
987: reopen = 1;
988: continue;
989: }
990:
991: if (-1 == (c = lookup(mdoc, line, lastarg, tok, p)))
992: return(0);
993:
994: if (MDOC_MAX == c) {
995: if ( ! mdoc_word_alloc(mdoc, line, lastarg, p))
996: return(0);
997: mdoc->next = MDOC_NEXT_SIBLING;
998: continue;
999: }
1000:
1001: if ( ! mdoc_macro(mdoc, c, line, lastarg, pos, buf))
1002: return(0);
1003: break;
1004: }
1005:
1006: if (1 == ppos && ! append_delims(mdoc, line, pos, buf))
1007: return(0);
1.19 kristaps 1008:
1009: /* If the body's already open, then just return. */
1010: if (0 == dohead)
1011: return(1);
1012:
1.1 kristaps 1013: if ( ! rew_subblock(MDOC_HEAD, mdoc, tok, line, ppos))
1014: return(0);
1015: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1016: return(0);
1017: mdoc->next = MDOC_NEXT_CHILD;
1018:
1019: return(1);
1020: }
1021:
1022:
1023: /*
1024: * Block partial-imnplicit scope.
1025: */
1026: static int
1027: blk_part_imp(MACRO_PROT_ARGS)
1028: {
1029: int lastarg, c;
1030: char *p;
1031: struct mdoc_node *blk, *body, *n;
1032:
1033: if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, NULL))
1034: return(0);
1035: mdoc->next = MDOC_NEXT_CHILD;
1036: blk = mdoc->last;
1037:
1038: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1039: return(0);
1040: mdoc->next = MDOC_NEXT_SIBLING;
1041:
1042: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1043: return(0);
1044: mdoc->next = MDOC_NEXT_CHILD;
1045: body = mdoc->last;
1046:
1047: /* XXX - no known argument macros. */
1048:
1.27 ! kristaps 1049: for (;;) {
1.1 kristaps 1050: lastarg = *pos;
1051: c = mdoc_args(mdoc, line, pos, buf, tok, &p);
1052: assert(ARGS_PHRASE != c);
1053:
1054: if (ARGS_ERROR == c)
1055: return(0);
1056: if (ARGS_PUNCT == c)
1057: break;
1058: if (ARGS_EOLN == c)
1059: break;
1060:
1061: if (-1 == (c = lookup(mdoc, line, lastarg, tok, p)))
1062: return(0);
1063: else if (MDOC_MAX == c) {
1064: if ( ! mdoc_word_alloc(mdoc, line, lastarg, p))
1065: return(0);
1066: mdoc->next = MDOC_NEXT_SIBLING;
1067: continue;
1068: }
1069:
1070: if ( ! mdoc_macro(mdoc, c, line, lastarg, pos, buf))
1071: return(0);
1072: break;
1073: }
1074:
1075: /*
1076: * Since we know what our context is, we can rewind directly to
1077: * it. This allows us to accomodate for our scope being
1078: * violated by another token.
1079: */
1080:
1081: for (n = mdoc->last; n; n = n->parent)
1082: if (body == n)
1083: break;
1084:
1.16 kristaps 1085: if (NULL == n && ! mdoc_nwarn(mdoc, body, EIMPBRK))
1.1 kristaps 1086: return(0);
1087:
1088: if (n && ! rew_last(mdoc, body))
1089: return(0);
1090:
1091: if (1 == ppos && ! append_delims(mdoc, line, pos, buf))
1092: return(0);
1093:
1094: if (n && ! rew_last(mdoc, blk))
1095: return(0);
1096:
1097: return(1);
1098: }
1099:
1100:
1101: /*
1102: * Block partial-explicit macros.
1103: */
1104: static int
1105: blk_part_exp(MACRO_PROT_ARGS)
1106: {
1107: int lastarg, flushed, j, c, maxargs;
1108: char *p;
1109:
1110: flushed = 0;
1111:
1112: /*
1113: * Number of arguments (head arguments). Only `Eo' has these,
1114: */
1115:
1116: switch (tok) {
1117: case (MDOC_Eo):
1118: maxargs = 1;
1119: break;
1120: default:
1121: maxargs = 0;
1122: break;
1123: }
1124:
1125: if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, NULL))
1126: return(0);
1127: mdoc->next = MDOC_NEXT_CHILD;
1128:
1129: if (0 == maxargs) {
1130: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1131: return(0);
1132: if ( ! rew_subblock(MDOC_HEAD, mdoc,
1133: tok, line, ppos))
1134: return(0);
1135: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1136: return(0);
1137: flushed = 1;
1138: } else if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1139: return(0);
1140:
1141: mdoc->next = MDOC_NEXT_CHILD;
1142:
1143: for (j = 0; ; j++) {
1144: lastarg = *pos;
1145: if (j == maxargs && ! flushed) {
1146: if ( ! rew_subblock(MDOC_HEAD, mdoc,
1147: tok, line, ppos))
1148: return(0);
1149: flushed = 1;
1150: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1151: return(0);
1152: mdoc->next = MDOC_NEXT_CHILD;
1153: }
1154:
1155: c = mdoc_args(mdoc, line, pos, buf, tok, &p);
1156: assert(ARGS_PHRASE != c);
1157:
1158: if (ARGS_ERROR == c)
1159: return(0);
1160: if (ARGS_PUNCT == c)
1161: break;
1162: if (ARGS_EOLN == c)
1163: break;
1164:
1165: if (-1 == (c = lookup(mdoc, line, lastarg, tok, p)))
1166: return(0);
1167: else if (MDOC_MAX != c) {
1168: if ( ! flushed) {
1169: if ( ! rew_subblock(MDOC_HEAD, mdoc,
1170: tok, line, ppos))
1171: return(0);
1172: flushed = 1;
1173: if ( ! mdoc_body_alloc(mdoc, line,
1174: ppos, tok))
1175: return(0);
1176: mdoc->next = MDOC_NEXT_CHILD;
1177: }
1178: if ( ! mdoc_macro(mdoc, c, line, lastarg,
1179: pos, buf))
1180: return(0);
1181: break;
1182: }
1183:
1184: if ( ! flushed && mdoc_isdelim(p)) {
1185: if ( ! rew_subblock(MDOC_HEAD, mdoc,
1186: tok, line, ppos))
1187: return(0);
1188: flushed = 1;
1189: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1190: return(0);
1191: mdoc->next = MDOC_NEXT_CHILD;
1192: }
1193:
1194: if ( ! mdoc_word_alloc(mdoc, line, lastarg, p))
1195: return(0);
1196: mdoc->next = MDOC_NEXT_SIBLING;
1197: }
1198:
1199: if ( ! flushed) {
1200: if ( ! rew_subblock(MDOC_HEAD, mdoc, tok, line, ppos))
1201: return(0);
1202: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1203: return(0);
1204: mdoc->next = MDOC_NEXT_CHILD;
1205: }
1206:
1207: if (ppos > 1)
1208: return(1);
1209: return(append_delims(mdoc, line, pos, buf));
1210: }
1211:
1212:
1213: /*
1214: * In-line macros where reserved words signal closure of the macro.
1215: * Macros also have a fixed number of arguments.
1216: */
1217: static int
1218: in_line_argn(MACRO_PROT_ARGS)
1219: {
1220: int lastarg, flushed, j, c, maxargs;
1221: struct mdoc_arg *arg;
1222: char *p;
1223:
1224:
1225: /*
1226: * Fixed maximum arguments per macro. Some of these have none
1227: * and close as soon as the invocation is parsed.
1228: */
1229:
1230: switch (tok) {
1231: case (MDOC_Ap):
1232: /* FALLTHROUGH */
1233: case (MDOC_No):
1234: /* FALLTHROUGH */
1235: case (MDOC_Ns):
1236: /* FALLTHROUGH */
1237: case (MDOC_Ux):
1238: maxargs = 0;
1239: break;
1240: default:
1241: maxargs = 1;
1242: break;
1243: }
1244:
1.27 ! kristaps 1245: for (arg = NULL;; ) {
1.1 kristaps 1246: lastarg = *pos;
1247: c = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1248:
1249: if (ARGV_WORD == c) {
1250: *pos = lastarg;
1251: break;
1252: }
1253:
1254: if (ARGV_EOLN == c)
1255: break;
1256: if (ARGV_ARG == c)
1257: continue;
1258:
1259: mdoc_argv_free(arg);
1260: return(0);
1261: }
1262:
1263: if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
1264: return(0);
1265: mdoc->next = MDOC_NEXT_CHILD;
1266:
1267: for (flushed = j = 0; ; j++) {
1268: lastarg = *pos;
1269:
1270: if (j == maxargs && ! flushed) {
1271: if ( ! rew_elem(mdoc, tok))
1272: return(0);
1273: flushed = 1;
1274: }
1275:
1276: c = mdoc_args(mdoc, line, pos, buf, tok, &p);
1277:
1278: if (ARGS_ERROR == c)
1279: return(0);
1280: if (ARGS_PUNCT == c)
1281: break;
1282: if (ARGS_EOLN == c)
1283: break;
1284:
1285: if (-1 == (c = lookup(mdoc, line, lastarg, tok, p)))
1286: return(0);
1287: else if (MDOC_MAX != c) {
1288: if ( ! flushed && ! rew_elem(mdoc, tok))
1289: return(0);
1290: flushed = 1;
1291: if ( ! mdoc_macro(mdoc, c, line, lastarg, pos, buf))
1292: return(0);
1293: break;
1294: }
1295:
1296: if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1297: ! flushed && mdoc_isdelim(p)) {
1298: if ( ! rew_elem(mdoc, tok))
1299: return(0);
1300: flushed = 1;
1301: }
1302:
1303: if ( ! mdoc_word_alloc(mdoc, line, lastarg, p))
1304: return(0);
1305: mdoc->next = MDOC_NEXT_SIBLING;
1306: }
1307:
1308: if ( ! flushed && ! rew_elem(mdoc, tok))
1309: return(0);
1310:
1311: if (ppos > 1)
1312: return(1);
1313: return(append_delims(mdoc, line, pos, buf));
1314: }
1315:
1316:
1317: /*
1318: * In-line macro that spans an entire line. May be callable, but has no
1319: * subsequent parsed arguments.
1320: */
1321: static int
1322: in_line_eoln(MACRO_PROT_ARGS)
1323: {
1324: int c, w, la;
1325: struct mdoc_arg *arg;
1326: char *p;
1327:
1328: assert( ! (MDOC_PARSED & mdoc_macros[tok].flags));
1329:
1330: arg = NULL;
1331:
1332: for (;;) {
1333: la = *pos;
1334: c = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1335:
1336: if (ARGV_WORD == c) {
1337: *pos = la;
1338: break;
1339: }
1340: if (ARGV_EOLN == c)
1341: break;
1342: if (ARGV_ARG == c)
1343: continue;
1344:
1345: mdoc_argv_free(arg);
1346: return(0);
1347: }
1348:
1349: if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
1350: return(0);
1351:
1352: mdoc->next = MDOC_NEXT_CHILD;
1353:
1354: for (;;) {
1355: la = *pos;
1356: w = mdoc_args(mdoc, line, pos, buf, tok, &p);
1357:
1358: if (ARGS_ERROR == w)
1359: return(0);
1360: if (ARGS_EOLN == w)
1361: break;
1362:
1363: c = ARGS_QWORD == w ? MDOC_MAX :
1364: lookup(mdoc, line, la, tok, p);
1365:
1366: if (MDOC_MAX != c && -1 != c) {
1367: if ( ! rew_elem(mdoc, tok))
1368: return(0);
1369: return(mdoc_macro(mdoc, c, line, la, pos, buf));
1370: } else if (-1 == c)
1371: return(0);
1372:
1373: if ( ! mdoc_word_alloc(mdoc, line, la, p))
1374: return(0);
1375: mdoc->next = MDOC_NEXT_SIBLING;
1376: }
1377:
1378: return(rew_elem(mdoc, tok));
1379: }
1380:
1381:
1382: /* ARGSUSED */
1383: static int
1384: obsolete(MACRO_PROT_ARGS)
1385: {
1386:
1.16 kristaps 1387: return(mdoc_pwarn(mdoc, line, ppos, EOBS));
1.1 kristaps 1388: }
1389:
1390:
1.24 kristaps 1391: /*
1392: * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1393: * They're unusual because they're basically free-form text until a
1394: * macro is encountered.
1395: */
1.1 kristaps 1396: static int
1397: phrase(struct mdoc *mdoc, int line, int ppos, char *buf)
1398: {
1.24 kristaps 1399: int c, w, la, pos;
1400: char *p;
1.1 kristaps 1401:
1.24 kristaps 1402: for (pos = ppos; ; ) {
1403: la = pos;
1.1 kristaps 1404:
1.24 kristaps 1405: /* Note: no calling context! */
1406: w = mdoc_zargs(mdoc, line, &pos, buf, &p);
1.1 kristaps 1407:
1.24 kristaps 1408: if (ARGS_ERROR == w)
1409: return(0);
1410: if (ARGS_EOLN == w)
1411: break;
1.1 kristaps 1412:
1.24 kristaps 1413: c = ARGS_QWORD == w ? MDOC_MAX :
1414: mdoc_hash_find(mdoc->htab, p);
1.1 kristaps 1415:
1.24 kristaps 1416: if (MDOC_MAX != c && -1 != c) {
1417: if ( ! mdoc_macro(mdoc, c, line, la, &pos, buf))
1.1 kristaps 1418: return(0);
1.24 kristaps 1419: return(append_delims(mdoc, line, &pos, buf));
1420: } else if (-1 == c)
1421: return(0);
1.1 kristaps 1422:
1.24 kristaps 1423: if ( ! mdoc_word_alloc(mdoc, line, la, p))
1.1 kristaps 1424: return(0);
1425: mdoc->next = MDOC_NEXT_SIBLING;
1426: }
1427:
1428: return(1);
1429: }
1.24 kristaps 1430:
1431:
CVSweb