Annotation of mandoc/mdoc_macro.c, Revision 1.25
1.25 ! kristaps 1: /* $Id: mdoc_macro.c,v 1.24 2009/07/19 21:26:27 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:
684: for (lastarg = ppos, flushed = j = 0; ; j++) {
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) {
745: case (MDOC_Nm):
746: /* FALLTHROUGH */
747: case (MDOC_Fl):
748: /* FALLTHROUGH */
1.12 kristaps 749: case (MDOC_Lk):
750: /* FALLTHROUGH */
1.25 ! kristaps 751: case (MDOC_Pa):
! 752: /* FALLTHROUGH */
1.4 kristaps 753: case (MDOC_Ar):
754: nc = 1;
755: break;
756: default:
757: nc = 0;
758: break;
759: }
760:
1.1 kristaps 761: for (la = ppos, arg = NULL;; ) {
762: la = *pos;
763: c = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
764:
765: if (ARGV_WORD == c) {
766: *pos = la;
767: break;
768: }
769: if (ARGV_EOLN == c)
770: break;
771: if (ARGV_ARG == c)
772: continue;
773:
774: mdoc_argv_free(arg);
775: return(0);
776: }
777:
1.4 kristaps 778: for (cnt = 0, lastpunct = 1;; ) {
1.1 kristaps 779: la = *pos;
780: w = mdoc_args(mdoc, line, pos, buf, tok, &p);
781:
782: if (ARGS_ERROR == w)
783: return(0);
784: if (ARGS_EOLN == w)
785: break;
786: if (ARGS_PUNCT == w)
787: break;
788:
789: /* Quoted words shouldn't be looked-up. */
790:
791: c = ARGS_QWORD == w ? MDOC_MAX :
792: lookup(mdoc, line, la, tok, p);
793:
1.4 kristaps 794: /*
795: * In this case, we've located a submacro and must
796: * execute it. Close out scope, if open. If no
797: * elements have been generated, either create one (nc)
798: * or raise a warning.
799: */
1.1 kristaps 800:
801: if (MDOC_MAX != c && -1 != c) {
802: if (0 == lastpunct && ! rew_elem(mdoc, tok))
803: return(0);
1.4 kristaps 804: if (nc && 0 == cnt) {
1.3 kristaps 805: if ( ! mdoc_elem_alloc(mdoc, line, ppos,
806: tok, arg))
807: return(0);
1.12 kristaps 808: if ( ! rew_last(mdoc, mdoc->last))
809: return(0);
1.7 kristaps 810: } else if ( ! nc && 0 == cnt) {
811: mdoc_argv_free(arg);
1.16 kristaps 812: if ( ! mdoc_pwarn(mdoc, line, ppos, EIGNE))
1.3 kristaps 813: return(0);
1.7 kristaps 814: }
1.1 kristaps 815: c = mdoc_macro(mdoc, c, line, la, pos, buf);
816: if (0 == c)
817: return(0);
818: if (ppos > 1)
819: return(1);
820: return(append_delims(mdoc, line, pos, buf));
821: } else if (-1 == c)
822: return(0);
823:
1.4 kristaps 824: /*
825: * Non-quote-enclosed punctuation. Set up our scope, if
826: * a word; rewind the scope, if a delimiter; then append
827: * the word.
828: */
1.1 kristaps 829:
1.3 kristaps 830: d = mdoc_isdelim(p);
831:
832: if (ARGS_QWORD != w && d) {
1.1 kristaps 833: if (0 == lastpunct && ! rew_elem(mdoc, tok))
834: return(0);
835: lastpunct = 1;
836: } else if (lastpunct) {
837: c = mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
838: if (0 == c)
839: return(0);
840: mdoc->next = MDOC_NEXT_CHILD;
841: lastpunct = 0;
842: }
843:
1.3 kristaps 844: if ( ! d)
845: cnt++;
1.1 kristaps 846: if ( ! mdoc_word_alloc(mdoc, line, la, p))
847: return(0);
848: mdoc->next = MDOC_NEXT_SIBLING;
849: }
850:
851: if (0 == lastpunct && ! rew_elem(mdoc, tok))
852: return(0);
1.4 kristaps 853:
854: /*
855: * If no elements have been collected and we're allowed to have
856: * empties (nc), open a scope and close it out. Otherwise,
857: * raise a warning.
858: *
859: */
860: if (nc && 0 == cnt) {
1.3 kristaps 861: c = mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
862: if (0 == c)
863: return(0);
1.12 kristaps 864: if ( ! rew_last(mdoc, mdoc->last))
865: return(0);
1.7 kristaps 866: } else if ( ! nc && 0 == cnt) {
867: mdoc_argv_free(arg);
1.16 kristaps 868: if ( ! mdoc_pwarn(mdoc, line, ppos, EIGNE))
1.3 kristaps 869: return(0);
1.7 kristaps 870: }
1.4 kristaps 871:
1.1 kristaps 872: if (ppos > 1)
873: return(1);
874: return(append_delims(mdoc, line, pos, buf));
875: }
876:
877:
878: /*
879: * Block full-explicit and full-implicit.
880: */
881: static int
882: blk_full(MACRO_PROT_ARGS)
883: {
1.19 kristaps 884: int c, lastarg, reopen, dohead;
1.1 kristaps 885: struct mdoc_arg *arg;
886: char *p;
887:
1.19 kristaps 888: /*
889: * Whether to process a block-head section. If this is
890: * non-zero, then a head will be opened for all line arguments.
891: * If not, then the head will always be empty and only a body
892: * will be opened, which will stay open at the eoln.
893: */
894:
895: switch (tok) {
896: case (MDOC_Nd):
897: dohead = 0;
898: break;
899: default:
900: dohead = 1;
901: break;
902: }
903:
1.1 kristaps 904: if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) {
905: if ( ! rew_subblock(MDOC_BODY, mdoc,
906: tok, line, ppos))
907: return(0);
908: if ( ! rew_impblock(mdoc, tok, line, ppos))
909: return(0);
910: }
911:
912: for (arg = NULL;; ) {
913: lastarg = *pos;
914: c = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
915:
916: if (ARGV_WORD == c) {
917: *pos = lastarg;
918: break;
919: }
920:
921: if (ARGV_EOLN == c)
922: break;
923: if (ARGV_ARG == c)
924: continue;
925:
926: mdoc_argv_free(arg);
927: return(0);
928: }
929:
930: if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, arg))
931: return(0);
932: mdoc->next = MDOC_NEXT_CHILD;
933:
934: if (0 == buf[*pos]) {
935: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
936: return(0);
937: if ( ! rew_subblock(MDOC_HEAD, mdoc,
938: tok, line, ppos))
939: return(0);
940: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
941: return(0);
942: mdoc->next = MDOC_NEXT_CHILD;
943: return(1);
944: }
945:
946: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
947: return(0);
1.19 kristaps 948:
949: /* Immediately close out head and enter body, if applicable. */
950:
951: if (0 == dohead) {
952: if ( ! rew_subblock(MDOC_HEAD, mdoc, tok, line, ppos))
953: return(0);
954: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
955: return(0);
956: }
957:
1.1 kristaps 958: mdoc->next = MDOC_NEXT_CHILD;
959:
960: for (reopen = 0;; ) {
961: lastarg = *pos;
962: c = mdoc_args(mdoc, line, pos, buf, tok, &p);
963:
964: if (ARGS_ERROR == c)
965: return(0);
966: if (ARGS_EOLN == c)
967: break;
968: if (ARGS_PHRASE == c) {
1.19 kristaps 969: assert(dohead);
1.1 kristaps 970: if (reopen && ! mdoc_head_alloc
971: (mdoc, line, ppos, tok))
972: return(0);
973: mdoc->next = MDOC_NEXT_CHILD;
974: /*
975: * Phrases are self-contained macro phrases used
976: * in the columnar output of a macro. They need
977: * special handling.
978: */
979: if ( ! phrase(mdoc, line, lastarg, buf))
980: return(0);
981: if ( ! rew_subblock(MDOC_HEAD, mdoc,
982: tok, line, ppos))
983: return(0);
984:
985: reopen = 1;
986: continue;
987: }
988:
989: if (-1 == (c = lookup(mdoc, line, lastarg, tok, p)))
990: return(0);
991:
992: if (MDOC_MAX == c) {
993: if ( ! mdoc_word_alloc(mdoc, line, lastarg, p))
994: return(0);
995: mdoc->next = MDOC_NEXT_SIBLING;
996: continue;
997: }
998:
999: if ( ! mdoc_macro(mdoc, c, line, lastarg, pos, buf))
1000: return(0);
1001: break;
1002: }
1003:
1004: if (1 == ppos && ! append_delims(mdoc, line, pos, buf))
1005: return(0);
1.19 kristaps 1006:
1007: /* If the body's already open, then just return. */
1008: if (0 == dohead)
1009: return(1);
1010:
1.1 kristaps 1011: if ( ! rew_subblock(MDOC_HEAD, mdoc, tok, line, ppos))
1012: return(0);
1013: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1014: return(0);
1015: mdoc->next = MDOC_NEXT_CHILD;
1016:
1017: return(1);
1018: }
1019:
1020:
1021: /*
1022: * Block partial-imnplicit scope.
1023: */
1024: static int
1025: blk_part_imp(MACRO_PROT_ARGS)
1026: {
1027: int lastarg, c;
1028: char *p;
1029: struct mdoc_node *blk, *body, *n;
1030:
1031: if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, NULL))
1032: return(0);
1033: mdoc->next = MDOC_NEXT_CHILD;
1034: blk = mdoc->last;
1035:
1036: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1037: return(0);
1038: mdoc->next = MDOC_NEXT_SIBLING;
1039:
1040: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1041: return(0);
1042: mdoc->next = MDOC_NEXT_CHILD;
1043: body = mdoc->last;
1044:
1045: /* XXX - no known argument macros. */
1046:
1047: for (lastarg = ppos;; ) {
1048: lastarg = *pos;
1049: c = mdoc_args(mdoc, line, pos, buf, tok, &p);
1050: assert(ARGS_PHRASE != c);
1051:
1052: if (ARGS_ERROR == c)
1053: return(0);
1054: if (ARGS_PUNCT == c)
1055: break;
1056: if (ARGS_EOLN == c)
1057: break;
1058:
1059: if (-1 == (c = lookup(mdoc, line, lastarg, tok, p)))
1060: return(0);
1061: else if (MDOC_MAX == c) {
1062: if ( ! mdoc_word_alloc(mdoc, line, lastarg, p))
1063: return(0);
1064: mdoc->next = MDOC_NEXT_SIBLING;
1065: continue;
1066: }
1067:
1068: if ( ! mdoc_macro(mdoc, c, line, lastarg, pos, buf))
1069: return(0);
1070: break;
1071: }
1072:
1073: /*
1074: * Since we know what our context is, we can rewind directly to
1075: * it. This allows us to accomodate for our scope being
1076: * violated by another token.
1077: */
1078:
1079: for (n = mdoc->last; n; n = n->parent)
1080: if (body == n)
1081: break;
1082:
1.16 kristaps 1083: if (NULL == n && ! mdoc_nwarn(mdoc, body, EIMPBRK))
1.1 kristaps 1084: return(0);
1085:
1086: if (n && ! rew_last(mdoc, body))
1087: return(0);
1088:
1089: if (1 == ppos && ! append_delims(mdoc, line, pos, buf))
1090: return(0);
1091:
1092: if (n && ! rew_last(mdoc, blk))
1093: return(0);
1094:
1095: return(1);
1096: }
1097:
1098:
1099: /*
1100: * Block partial-explicit macros.
1101: */
1102: static int
1103: blk_part_exp(MACRO_PROT_ARGS)
1104: {
1105: int lastarg, flushed, j, c, maxargs;
1106: char *p;
1107:
1108: lastarg = ppos;
1109: flushed = 0;
1110:
1111: /*
1112: * Number of arguments (head arguments). Only `Eo' has these,
1113: */
1114:
1115: switch (tok) {
1116: case (MDOC_Eo):
1117: maxargs = 1;
1118: break;
1119: default:
1120: maxargs = 0;
1121: break;
1122: }
1123:
1124: if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, NULL))
1125: return(0);
1126: mdoc->next = MDOC_NEXT_CHILD;
1127:
1128: if (0 == maxargs) {
1129: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1130: return(0);
1131: if ( ! rew_subblock(MDOC_HEAD, mdoc,
1132: tok, line, ppos))
1133: return(0);
1134: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1135: return(0);
1136: flushed = 1;
1137: } else if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1138: return(0);
1139:
1140: mdoc->next = MDOC_NEXT_CHILD;
1141:
1142: for (j = 0; ; j++) {
1143: lastarg = *pos;
1144: if (j == maxargs && ! flushed) {
1145: if ( ! rew_subblock(MDOC_HEAD, mdoc,
1146: tok, line, ppos))
1147: return(0);
1148: flushed = 1;
1149: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1150: return(0);
1151: mdoc->next = MDOC_NEXT_CHILD;
1152: }
1153:
1154: c = mdoc_args(mdoc, line, pos, buf, tok, &p);
1155: assert(ARGS_PHRASE != c);
1156:
1157: if (ARGS_ERROR == c)
1158: return(0);
1159: if (ARGS_PUNCT == c)
1160: break;
1161: if (ARGS_EOLN == c)
1162: break;
1163:
1164: if (-1 == (c = lookup(mdoc, line, lastarg, tok, p)))
1165: return(0);
1166: else if (MDOC_MAX != c) {
1167: if ( ! flushed) {
1168: if ( ! rew_subblock(MDOC_HEAD, mdoc,
1169: tok, line, ppos))
1170: return(0);
1171: flushed = 1;
1172: if ( ! mdoc_body_alloc(mdoc, line,
1173: ppos, tok))
1174: return(0);
1175: mdoc->next = MDOC_NEXT_CHILD;
1176: }
1177: if ( ! mdoc_macro(mdoc, c, line, lastarg,
1178: pos, buf))
1179: return(0);
1180: break;
1181: }
1182:
1183: if ( ! flushed && mdoc_isdelim(p)) {
1184: if ( ! rew_subblock(MDOC_HEAD, mdoc,
1185: tok, line, ppos))
1186: return(0);
1187: flushed = 1;
1188: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1189: return(0);
1190: mdoc->next = MDOC_NEXT_CHILD;
1191: }
1192:
1193: if ( ! mdoc_word_alloc(mdoc, line, lastarg, p))
1194: return(0);
1195: mdoc->next = MDOC_NEXT_SIBLING;
1196: }
1197:
1198: if ( ! flushed) {
1199: if ( ! rew_subblock(MDOC_HEAD, mdoc, tok, line, ppos))
1200: return(0);
1201: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1202: return(0);
1203: mdoc->next = MDOC_NEXT_CHILD;
1204: }
1205:
1206: if (ppos > 1)
1207: return(1);
1208: return(append_delims(mdoc, line, pos, buf));
1209: }
1210:
1211:
1212: /*
1213: * In-line macros where reserved words signal closure of the macro.
1214: * Macros also have a fixed number of arguments.
1215: */
1216: static int
1217: in_line_argn(MACRO_PROT_ARGS)
1218: {
1219: int lastarg, flushed, j, c, maxargs;
1220: struct mdoc_arg *arg;
1221: char *p;
1222:
1223:
1224: /*
1225: * Fixed maximum arguments per macro. Some of these have none
1226: * and close as soon as the invocation is parsed.
1227: */
1228:
1229: switch (tok) {
1230: case (MDOC_Ap):
1231: /* FALLTHROUGH */
1232: case (MDOC_No):
1233: /* FALLTHROUGH */
1234: case (MDOC_Ns):
1235: /* FALLTHROUGH */
1236: case (MDOC_Ux):
1237: maxargs = 0;
1238: break;
1239: default:
1240: maxargs = 1;
1241: break;
1242: }
1243:
1244: for (lastarg = ppos, arg = NULL;; ) {
1245: lastarg = *pos;
1246: c = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1247:
1248: if (ARGV_WORD == c) {
1249: *pos = lastarg;
1250: break;
1251: }
1252:
1253: if (ARGV_EOLN == c)
1254: break;
1255: if (ARGV_ARG == c)
1256: continue;
1257:
1258: mdoc_argv_free(arg);
1259: return(0);
1260: }
1261:
1262: if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
1263: return(0);
1264: mdoc->next = MDOC_NEXT_CHILD;
1265:
1266: for (flushed = j = 0; ; j++) {
1267: lastarg = *pos;
1268:
1269: if (j == maxargs && ! flushed) {
1270: if ( ! rew_elem(mdoc, tok))
1271: return(0);
1272: flushed = 1;
1273: }
1274:
1275: c = mdoc_args(mdoc, line, pos, buf, tok, &p);
1276:
1277: if (ARGS_ERROR == c)
1278: return(0);
1279: if (ARGS_PUNCT == c)
1280: break;
1281: if (ARGS_EOLN == c)
1282: break;
1283:
1284: if (-1 == (c = lookup(mdoc, line, lastarg, tok, p)))
1285: return(0);
1286: else if (MDOC_MAX != c) {
1287: if ( ! flushed && ! rew_elem(mdoc, tok))
1288: return(0);
1289: flushed = 1;
1290: if ( ! mdoc_macro(mdoc, c, line, lastarg, pos, buf))
1291: return(0);
1292: break;
1293: }
1294:
1295: if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1296: ! flushed && mdoc_isdelim(p)) {
1297: if ( ! rew_elem(mdoc, tok))
1298: return(0);
1299: flushed = 1;
1300: }
1301:
1302: if ( ! mdoc_word_alloc(mdoc, line, lastarg, p))
1303: return(0);
1304: mdoc->next = MDOC_NEXT_SIBLING;
1305: }
1306:
1307: if ( ! flushed && ! rew_elem(mdoc, tok))
1308: return(0);
1309:
1310: if (ppos > 1)
1311: return(1);
1312: return(append_delims(mdoc, line, pos, buf));
1313: }
1314:
1315:
1316: /*
1317: * In-line macro that spans an entire line. May be callable, but has no
1318: * subsequent parsed arguments.
1319: */
1320: static int
1321: in_line_eoln(MACRO_PROT_ARGS)
1322: {
1323: int c, w, la;
1324: struct mdoc_arg *arg;
1325: char *p;
1326:
1327: assert( ! (MDOC_PARSED & mdoc_macros[tok].flags));
1328:
1329: arg = NULL;
1330:
1331: for (;;) {
1332: la = *pos;
1333: c = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1334:
1335: if (ARGV_WORD == c) {
1336: *pos = la;
1337: break;
1338: }
1339: if (ARGV_EOLN == c)
1340: break;
1341: if (ARGV_ARG == c)
1342: continue;
1343:
1344: mdoc_argv_free(arg);
1345: return(0);
1346: }
1347:
1348: if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
1349: return(0);
1350:
1351: mdoc->next = MDOC_NEXT_CHILD;
1352:
1353: for (;;) {
1354: la = *pos;
1355: w = mdoc_args(mdoc, line, pos, buf, tok, &p);
1356:
1357: if (ARGS_ERROR == w)
1358: return(0);
1359: if (ARGS_EOLN == w)
1360: break;
1361:
1362: c = ARGS_QWORD == w ? MDOC_MAX :
1363: lookup(mdoc, line, la, tok, p);
1364:
1365: if (MDOC_MAX != c && -1 != c) {
1366: if ( ! rew_elem(mdoc, tok))
1367: return(0);
1368: return(mdoc_macro(mdoc, c, line, la, pos, buf));
1369: } else if (-1 == c)
1370: return(0);
1371:
1372: if ( ! mdoc_word_alloc(mdoc, line, la, p))
1373: return(0);
1374: mdoc->next = MDOC_NEXT_SIBLING;
1375: }
1376:
1377: return(rew_elem(mdoc, tok));
1378: }
1379:
1380:
1381: /* ARGSUSED */
1382: static int
1383: obsolete(MACRO_PROT_ARGS)
1384: {
1385:
1.16 kristaps 1386: return(mdoc_pwarn(mdoc, line, ppos, EOBS));
1.1 kristaps 1387: }
1388:
1389:
1.24 kristaps 1390: /*
1391: * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1392: * They're unusual because they're basically free-form text until a
1393: * macro is encountered.
1394: */
1.1 kristaps 1395: static int
1396: phrase(struct mdoc *mdoc, int line, int ppos, char *buf)
1397: {
1.24 kristaps 1398: int c, w, la, pos;
1399: char *p;
1.1 kristaps 1400:
1.24 kristaps 1401: for (pos = ppos; ; ) {
1402: la = pos;
1.1 kristaps 1403:
1.24 kristaps 1404: /* Note: no calling context! */
1405: w = mdoc_zargs(mdoc, line, &pos, buf, &p);
1.1 kristaps 1406:
1.24 kristaps 1407: if (ARGS_ERROR == w)
1408: return(0);
1409: if (ARGS_EOLN == w)
1410: break;
1.1 kristaps 1411:
1.24 kristaps 1412: c = ARGS_QWORD == w ? MDOC_MAX :
1413: mdoc_hash_find(mdoc->htab, p);
1.1 kristaps 1414:
1.24 kristaps 1415: if (MDOC_MAX != c && -1 != c) {
1416: if ( ! mdoc_macro(mdoc, c, line, la, &pos, buf))
1.1 kristaps 1417: return(0);
1.24 kristaps 1418: return(append_delims(mdoc, line, &pos, buf));
1419: } else if (-1 == c)
1420: return(0);
1.1 kristaps 1421:
1.24 kristaps 1422: if ( ! mdoc_word_alloc(mdoc, line, la, p))
1.1 kristaps 1423: return(0);
1424: mdoc->next = MDOC_NEXT_SIBLING;
1425: }
1426:
1427: return(1);
1428: }
1.24 kristaps 1429:
1430:
CVSweb