]>
git.cameronkatri.com Git - mandoc.git/blob - macro.c
1 /* $Id: macro.c,v 1.58 2009/03/08 11:41:22 kristaps Exp $ */
3 * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the
7 * above copyright notice and this permission notice appear in all
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
29 * This has scanning/parsing routines, each of which extract a macro and
30 * its arguments and parameters, then know how to progress to the next
36 static int macro_obsolete(MACRO_PROT_ARGS
);
37 static int macro_constant(MACRO_PROT_ARGS
);
38 static int macro_constant_scoped(MACRO_PROT_ARGS
);
39 static int macro_constant_delimited(MACRO_PROT_ARGS
);
40 static int macro_text(MACRO_PROT_ARGS
);
41 static int macro_scoped(MACRO_PROT_ARGS
);
42 static int macro_scoped_close(MACRO_PROT_ARGS
);
43 static int macro_scoped_line(MACRO_PROT_ARGS
);
45 #define REWIND_REWIND (1 << 0)
46 #define REWIND_NOHALT (1 << 1)
47 #define REWIND_HALT (1 << 2)
49 static int rewind_dohalt(int, enum mdoc_type
,
50 const struct mdoc_node
*);
51 static int rewind_alt(int);
52 static int rewind_dobreak(int, const struct mdoc_node
*);
53 static int rewind_elem(struct mdoc
*, int);
54 static int rewind_impblock(struct mdoc
*, int, int, int);
55 static int rewind_expblock(struct mdoc
*, int, int, int);
56 static int rewind_subblock(enum mdoc_type
,
57 struct mdoc
*, int, int, int);
58 static int rewind_last(struct mdoc
*, struct mdoc_node
*);
59 static int append_delims(struct mdoc
*, int, int *, char *);
60 static int lookup(struct mdoc
*, int, int, int, const char *);
61 static int pwarn(struct mdoc
*, int, int, int);
62 static int perr(struct mdoc
*, int, int, int);
70 /* Central table of library: who gets parsed how. */
72 const struct mdoc_macro __mdoc_macros
[MDOC_MAX
] = {
74 { macro_constant
, MDOC_PROLOGUE
}, /* Dd */
75 { macro_constant
, MDOC_PROLOGUE
}, /* Dt */
76 { macro_constant
, MDOC_PROLOGUE
}, /* Os */
77 { macro_scoped
, 0 }, /* Sh */
78 { macro_scoped
, 0 }, /* Ss */
79 { macro_text
, 0 }, /* Pp */
80 { macro_scoped_line
, MDOC_PARSED
}, /* D1 */
81 { macro_scoped_line
, MDOC_PARSED
}, /* Dl */
82 { macro_scoped
, MDOC_EXPLICIT
}, /* Bd */
83 { macro_scoped_close
, MDOC_EXPLICIT
}, /* Ed */
84 { macro_scoped
, MDOC_EXPLICIT
}, /* Bl */
85 { macro_scoped_close
, MDOC_EXPLICIT
}, /* El */
86 { macro_scoped
, MDOC_PARSED
}, /* It */
87 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ad */
88 { macro_text
, MDOC_PARSED
}, /* An */
89 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ar */
90 { macro_constant
, 0 }, /* Cd */
91 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Cm */
92 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dv */
93 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Er */
94 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ev */
95 { macro_constant
, 0 }, /* Ex */
96 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fa */
97 { macro_constant
, 0 }, /* Fd */
98 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fl */
99 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fn */
100 { macro_text
, MDOC_PARSED
}, /* Ft */
101 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ic */
102 { macro_constant
, 0 }, /* In */
103 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Li */
104 { macro_constant
, 0 }, /* Nd */
105 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Nm */
106 { macro_scoped_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Op */
107 { macro_obsolete
, 0 }, /* Ot */
108 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Pa */
109 { macro_constant
, 0 }, /* Rv */
110 /* XXX - .St supposed to be (but isn't) callable. */
111 { macro_constant_delimited
, MDOC_PARSED
}, /* St */
112 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Va */
113 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Vt */
114 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Xr */
115 { macro_constant
, 0 }, /* %A */
116 { macro_constant
, 0 }, /* %B */
117 { macro_constant
, 0 }, /* %D */
118 { macro_constant
, 0 }, /* %I */
119 { macro_constant
, 0 }, /* %J */
120 { macro_constant
, 0 }, /* %N */
121 { macro_constant
, 0 }, /* %O */
122 { macro_constant
, 0 }, /* %P */
123 { macro_constant
, 0 }, /* %R */
124 { macro_constant
, 0 }, /* %T */
125 { macro_constant
, 0 }, /* %V */
126 { macro_scoped_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Ac */
127 { macro_constant_scoped
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Ao */
128 { macro_scoped_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Aq */
129 { macro_constant_delimited
, 0 }, /* At */
130 { macro_scoped_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Bc */
131 { macro_scoped
, MDOC_EXPLICIT
}, /* Bf */
132 { macro_constant_scoped
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Bo */
133 { macro_scoped_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Bq */
134 { macro_constant_delimited
, MDOC_PARSED
}, /* Bsx */
135 { macro_constant_delimited
, MDOC_PARSED
}, /* Bx */
136 { macro_constant
, 0 }, /* Db */
137 { macro_scoped_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Dc */
138 { macro_constant_scoped
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Do */
139 { macro_scoped_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dq */
140 { macro_scoped_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Ec */
141 { macro_scoped_close
, MDOC_EXPLICIT
}, /* Ef */
142 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Em */
143 { macro_constant_scoped
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Eo */
144 { macro_constant_delimited
, MDOC_PARSED
}, /* Fx */
145 { macro_text
, MDOC_PARSED
}, /* Ms */
146 { macro_constant_delimited
, MDOC_CALLABLE
| MDOC_PARSED
}, /* No */
147 { macro_constant_delimited
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ns */
148 { macro_constant_delimited
, MDOC_PARSED
}, /* Nx */
149 { macro_constant_delimited
, MDOC_PARSED
}, /* Ox */
150 { macro_scoped_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Pc */
151 { macro_constant_delimited
, MDOC_PARSED
}, /* Pf */
152 { macro_constant_scoped
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Po */
153 { macro_scoped_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Pq */
154 { macro_scoped_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Qc */
155 { macro_scoped_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ql */
156 { macro_constant_scoped
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Qo */
157 { macro_scoped_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Qq */
158 { macro_scoped_close
, MDOC_EXPLICIT
}, /* Re */
159 { macro_scoped
, MDOC_EXPLICIT
}, /* Rs */
160 { macro_scoped_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Sc */
161 { macro_constant_scoped
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* So */
162 { macro_scoped_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sq */
163 { macro_constant
, 0 }, /* Sm */
164 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sx */
165 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sy */
166 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Tn */
167 { macro_constant_delimited
, MDOC_PARSED
}, /* Ux */
168 { macro_scoped_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Xc */
169 { macro_constant_scoped
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Xo */
170 /* XXX - .Fo supposed to be (but isn't) callable. */
171 { macro_scoped
, MDOC_EXPLICIT
}, /* Fo */
172 { macro_scoped_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Fc */
173 { macro_constant_scoped
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Oo */
174 { macro_scoped_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Oc */
175 { macro_scoped
, MDOC_EXPLICIT
}, /* Bk */
176 { macro_scoped_close
, MDOC_EXPLICIT
}, /* Ek */
177 { macro_constant
, 0 }, /* Bt */
178 { macro_constant
, 0 }, /* Hf */
179 { macro_obsolete
, 0 }, /* Fr */
180 { macro_constant
, 0 }, /* Ud */
181 { macro_constant
, 0 }, /* Lb */
184 const struct mdoc_macro
* const mdoc_macros
= __mdoc_macros
;
188 perr(struct mdoc
*mdoc
, int line
, int pos
, int type
)
194 c
= mdoc_perr(mdoc
, line
, pos
,
195 "closing macro has prior context");
198 c
= mdoc_perr(mdoc
, line
, pos
,
199 "macro doesn't expect parameters");
209 pwarn(struct mdoc
*mdoc
, int line
, int pos
, int type
)
215 c
= mdoc_pwarn(mdoc
, line
, pos
, WARN_SYNTAX
,
216 "macro-like parameter");
219 c
= mdoc_pwarn(mdoc
, line
, pos
, WARN_SYNTAX
,
220 "macro is marked obsolete");
231 lookup(struct mdoc
*mdoc
, int line
, int pos
, int from
, const char *p
)
235 res
= mdoc_tokhash_find(mdoc
->htab
, p
);
236 if (MDOC_PARSED
& mdoc_macros
[from
].flags
)
240 if ( ! pwarn(mdoc
, line
, pos
, WMACPARM
))
247 rewind_last(struct mdoc
*mdoc
, struct mdoc_node
*to
)
251 mdoc
->next
= MDOC_NEXT_SIBLING
;
254 while (mdoc
->last
!= to
) {
255 if ( ! mdoc_valid_post(mdoc
))
257 if ( ! mdoc_action_post(mdoc
))
259 mdoc
->last
= mdoc
->last
->parent
;
263 if ( ! mdoc_valid_post(mdoc
))
265 return(mdoc_action_post(mdoc
));
312 rewind_dohalt(int tok
, enum mdoc_type type
, const struct mdoc_node
*p
)
315 if (MDOC_ROOT
== p
->type
)
317 if (MDOC_VALID
& p
->flags
)
318 return(REWIND_NOHALT
);
321 /* One-liner implicit-scope. */
341 assert(MDOC_HEAD
!= type
);
342 assert(MDOC_TAIL
!= type
);
343 if (type
== p
->type
&& tok
== p
->tok
)
344 return(REWIND_REWIND
);
347 /* Multi-line implicit-scope. */
349 assert(MDOC_TAIL
!= type
);
350 if (type
== p
->type
&& tok
== p
->tok
)
351 return(REWIND_REWIND
);
352 if (MDOC_BODY
== p
->type
&& MDOC_Bl
== p
->tok
)
356 if (type
== p
->type
&& tok
== p
->tok
)
357 return(REWIND_REWIND
);
360 assert(MDOC_TAIL
!= type
);
361 if (type
== p
->type
&& tok
== p
->tok
)
362 return(REWIND_REWIND
);
363 if (MDOC_BODY
== p
->type
&& MDOC_Sh
== p
->tok
)
367 /* Multi-line explicit scope start. */
397 if (type
== p
->type
&& tok
== p
->tok
)
398 return(REWIND_REWIND
);
401 /* Multi-line explicit scope close. */
431 if (type
== p
->type
&& rewind_alt(tok
) == p
->tok
)
432 return(REWIND_REWIND
);
439 return(REWIND_NOHALT
);
444 rewind_dobreak(int tok
, const struct mdoc_node
*p
)
447 assert(MDOC_ROOT
!= p
->type
);
448 if (MDOC_ELEM
== p
->type
)
450 if (MDOC_TEXT
== p
->type
)
452 if (MDOC_VALID
& p
->flags
)
456 /* Implicit rules. */
458 return(MDOC_It
== p
->tok
);
460 return(MDOC_Ss
== p
->tok
);
462 if (MDOC_Ss
== p
->tok
)
464 return(MDOC_Sh
== p
->tok
);
466 /* Extra scope rules. */
468 if (MDOC_It
== p
->tok
)
475 if (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)
476 return(p
->tok
== rewind_alt(tok
));
477 else if (MDOC_BLOCK
== p
->type
)
480 return(tok
== p
->tok
);
485 rewind_elem(struct mdoc
*mdoc
, int tok
)
490 if (MDOC_ELEM
!= n
->type
)
492 assert(MDOC_ELEM
== n
->type
);
493 assert(tok
== n
->tok
);
495 return(rewind_last(mdoc
, n
));
500 rewind_subblock(enum mdoc_type type
, struct mdoc
*mdoc
,
501 int tok
, int line
, int ppos
)
507 for (n
= mdoc
->last
; n
; n
= n
->parent
) {
508 c
= rewind_dohalt(tok
, type
, n
);
509 if (REWIND_HALT
== c
)
511 if (REWIND_REWIND
== c
)
513 else if (rewind_dobreak(tok
, n
))
515 return(mdoc_perr(mdoc
, line
, ppos
,
516 "scope breaks %s", MDOC_ROOT
== n
->type
?
517 "<root>" : mdoc_macronames
[n
->tok
]));
521 return(rewind_last(mdoc
, n
));
526 rewind_expblock(struct mdoc
*mdoc
, int tok
, int line
, int ppos
)
532 for (n
= mdoc
->last
; n
; n
= n
->parent
) {
533 c
= rewind_dohalt(tok
, MDOC_BLOCK
, n
);
534 if (REWIND_HALT
== c
)
535 return(perr(mdoc
, line
, ppos
, ENOCTX
));
536 if (REWIND_REWIND
== c
)
538 else if (rewind_dobreak(tok
, n
))
540 return(mdoc_perr(mdoc
, line
, ppos
,
541 "scope breaks %s", MDOC_ROOT
== n
->type
?
542 "<root>" : mdoc_macronames
[n
->tok
]));
546 return(rewind_last(mdoc
, n
));
551 rewind_impblock(struct mdoc
*mdoc
, int tok
, int line
, int ppos
)
557 for (n
= mdoc
->last
; n
; n
= n
->parent
) {
558 c
= rewind_dohalt(tok
, MDOC_BLOCK
, n
);
559 if (REWIND_HALT
== c
)
561 else if (REWIND_REWIND
== c
)
563 else if (rewind_dobreak(tok
, n
))
565 return(mdoc_perr(mdoc
, line
, ppos
,
566 "scope breaks %s", MDOC_ROOT
== n
->type
?
567 "<root>" : mdoc_macronames
[n
->tok
]));
571 return(rewind_last(mdoc
, n
));
576 append_delims(struct mdoc
*mdoc
, int line
, int *pos
, char *buf
)
586 c
= mdoc_args(mdoc
, line
, pos
, buf
, 0, &p
);
587 assert(ARGS_PHRASE
!= c
);
591 else if (ARGS_EOLN
== c
)
593 assert(mdoc_isdelim(p
));
594 if ( ! mdoc_word_alloc(mdoc
, line
, lastarg
, p
))
596 mdoc
->next
= MDOC_NEXT_SIBLING
;
604 * Close out an explicit scope. This optionally parses a TAIL type with
605 * a set number of TEXT children.
608 macro_scoped_close(MACRO_PROT_ARGS
)
610 int tt
, j
, c
, lastarg
, maxargs
, flushed
;
622 tt
= rewind_alt(tok
);
624 mdoc_msg(mdoc
, "parse: %s closing %s",
625 mdoc_macronames
[tok
], mdoc_macronames
[tt
]);
627 if ( ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
)) {
628 if (0 == buf
[*pos
]) {
629 if ( ! rewind_subblock(MDOC_BODY
, mdoc
,
632 return(rewind_expblock(mdoc
, tok
, line
, ppos
));
634 return(perr(mdoc
, line
, ppos
, ENOPARMS
));
637 if ( ! rewind_subblock(MDOC_BODY
, mdoc
, tok
, line
, ppos
))
644 if ( ! mdoc_tail_alloc(mdoc
, line
, ppos
, tt
))
646 mdoc
->next
= MDOC_NEXT_CHILD
;
649 for (j
= 0; /* No sentinel. */; j
++) {
652 if (j
== maxargs
&& ! flushed
) {
653 if ( ! rewind_expblock(mdoc
, tok
, line
, ppos
))
658 c
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
659 assert(ARGS_PHRASE
!= c
);
668 if (-1 == (c
= lookup(mdoc
, line
, lastarg
, tok
, p
)))
670 else if (MDOC_MAX
!= c
) {
672 if ( ! rewind_expblock(mdoc
, tok
,
677 if ( ! mdoc_macro(mdoc
, c
, line
, lastarg
, pos
, buf
))
682 if ( ! mdoc_word_alloc(mdoc
, line
, lastarg
, p
))
684 mdoc
->next
= MDOC_NEXT_SIBLING
;
687 if ( ! flushed
&& ! rewind_expblock(mdoc
, tok
, line
, ppos
))
692 return(append_delims(mdoc
, line
, pos
, buf
));
697 * A general text macro. This is a complex case because of punctuation.
698 * If a text macro is followed by words, then punctuation, the macro is
699 * "stopped" and "reopened" following the punctuation. Thus, the
710 * This must handle the following situations:
721 macro_text(MACRO_PROT_ARGS
)
723 int la
, lastpunct
, c
, w
;
724 struct mdoc_arg
*arg
;
733 c
= mdoc_argv(mdoc
, line
, tok
, &arg
, pos
, buf
);
736 if (ARGV_WORD
== c
) {
739 } else if (ARGV_ARG
== c
)
745 if ( ! mdoc_elem_alloc(mdoc
, line
, ppos
, tok
, arg
))
748 mdoc
->next
= MDOC_NEXT_CHILD
;
753 w
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
754 assert(ARGS_PHRASE
!= c
);
763 c
= ARGS_QWORD
== w
? MDOC_MAX
:
764 lookup(mdoc
, line
, la
, tok
, p
);
766 if (MDOC_MAX
!= c
&& -1 != c
) {
767 if (0 == lastpunct
&& ! rewind_elem(mdoc
, tok
))
769 c
= mdoc_macro(mdoc
, c
, line
, la
, pos
, buf
);
774 return(append_delims(mdoc
, line
, pos
, buf
));
778 /* FIXME: .Fl and .Ar handling of `|'. */
780 if (ARGS_QWORD
!= w
&& mdoc_isdelim(p
)) {
781 if (0 == lastpunct
&& ! rewind_elem(mdoc
, tok
))
784 } else if (lastpunct
) {
785 c
= mdoc_elem_alloc(mdoc
, line
, ppos
, tok
, arg
);
790 mdoc
->next
= MDOC_NEXT_CHILD
;
794 if ( ! mdoc_word_alloc(mdoc
, line
, la
, p
))
796 mdoc
->next
= MDOC_NEXT_SIBLING
;
799 if (0 == lastpunct
&& ! rewind_elem(mdoc
, tok
))
803 return(append_delims(mdoc
, line
, pos
, buf
));
808 * Handle explicit-scope (having a different closure token) and implicit
809 * scope (closing out prior scopes when re-invoked) macros. These
810 * constitute the BLOCK type and usually span multiple lines. These
811 * always have HEAD and sometimes have BODY types. In the multi-line
828 * Note that the `.It' macro, possibly the most difficult (as it has
829 * embedded scope, etc.) is handled by this routine.
832 macro_scoped(MACRO_PROT_ARGS
)
835 struct mdoc_arg
*arg
;
838 assert ( ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
));
840 /* First rewind extant implicit scope. */
842 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)) {
843 if ( ! rewind_subblock(MDOC_BODY
, mdoc
, tok
, line
, ppos
))
845 if ( ! rewind_impblock(mdoc
, tok
, line
, ppos
))
849 /* Parse arguments. */
855 c
= mdoc_argv(mdoc
, line
, tok
, &arg
, pos
, buf
);
858 if (ARGV_WORD
== c
) {
861 } else if (ARGV_ARG
== c
)
867 if ( ! mdoc_block_alloc(mdoc
, line
, ppos
, tok
, arg
))
870 mdoc
->next
= MDOC_NEXT_CHILD
;
872 if (0 == buf
[*pos
]) {
873 if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
875 if ( ! rewind_subblock(MDOC_HEAD
, mdoc
,
878 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
880 mdoc
->next
= MDOC_NEXT_CHILD
;
884 if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
886 mdoc
->next
= MDOC_NEXT_CHILD
;
890 c
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
896 if (ARGS_PHRASE
== c
) {
898 if ( ! mdoc_phrase(mdoc, line, lastarg, buf))
904 /* FIXME: if .It -column, the lookup must be for a
905 * sub-line component. BLAH. */
907 if (-1 == (c
= lookup(mdoc
, line
, lastarg
, tok
, p
)))
911 if ( ! mdoc_word_alloc(mdoc
, line
, lastarg
, p
))
913 mdoc
->next
= MDOC_NEXT_SIBLING
;
917 if ( ! mdoc_macro(mdoc
, c
, line
, lastarg
, pos
, buf
))
922 if (1 == ppos
&& ! append_delims(mdoc
, line
, pos
, buf
))
924 if ( ! rewind_subblock(MDOC_HEAD
, mdoc
, tok
, line
, ppos
))
927 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
929 mdoc
->next
= MDOC_NEXT_CHILD
;
936 * This handles a case of implicitly-scoped macro (BLOCK) limited to a
937 * single line. Instead of being closed out by a subsequent call to
938 * another macro, the scope is closed at the end of line. These don't
939 * have BODY or TAIL types. Notice that the punctuation falls outside
942 * .Qq a Fl b Ar d ; ;
955 macro_scoped_line(MACRO_PROT_ARGS
)
960 if ( ! mdoc_block_alloc(mdoc
, line
, ppos
, tok
, NULL
))
962 mdoc
->next
= MDOC_NEXT_CHILD
;
964 if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
966 mdoc
->next
= MDOC_NEXT_SIBLING
;
967 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
969 mdoc
->next
= MDOC_NEXT_CHILD
;
971 /* XXX - no known argument macros. */
976 c
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
977 assert(ARGS_PHRASE
!= c
);
986 if (-1 == (c
= lookup(mdoc
, line
, lastarg
, tok
, p
)))
988 else if (MDOC_MAX
== c
) {
989 if ( ! mdoc_word_alloc(mdoc
, line
, lastarg
, p
))
991 mdoc
->next
= MDOC_NEXT_SIBLING
;
995 if ( ! mdoc_macro(mdoc
, c
, line
, lastarg
, pos
, buf
))
1001 if ( ! rewind_subblock(MDOC_BODY
, mdoc
, tok
, line
, ppos
))
1003 if ( ! append_delims(mdoc
, line
, pos
, buf
))
1005 } else if ( ! rewind_subblock(MDOC_BODY
, mdoc
, tok
, line
, ppos
))
1007 return(rewind_impblock(mdoc
, tok
, line
, ppos
));
1012 * A constant-scoped macro is like a simple-scoped macro (mdoc_scoped)
1013 * except that it doesn't handle implicit scopes and explicit ones have
1014 * a fixed number of TEXT children to the BODY.
1027 macro_constant_scoped(MACRO_PROT_ARGS
)
1029 int lastarg
, flushed
, j
, c
, maxargs
;
1044 if ( ! mdoc_block_alloc(mdoc
, line
, ppos
, tok
, NULL
))
1046 mdoc
->next
= MDOC_NEXT_CHILD
;
1049 if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
1051 if ( ! rewind_subblock(MDOC_HEAD
, mdoc
, tok
, line
, ppos
))
1053 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1056 } else if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
1059 mdoc
->next
= MDOC_NEXT_CHILD
;
1061 for (j
= 0; /* No sentinel. */; j
++) {
1064 if (j
== maxargs
&& ! flushed
) {
1065 if ( ! rewind_subblock(MDOC_HEAD
, mdoc
, tok
, line
, ppos
))
1068 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1070 mdoc
->next
= MDOC_NEXT_CHILD
;
1073 c
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
1074 assert(ARGS_PHRASE
!= c
);
1076 if (ARGS_ERROR
== c
)
1078 if (ARGS_PUNCT
== c
)
1083 if (-1 == (c
= lookup(mdoc
, line
, lastarg
, tok
, p
)))
1085 else if (MDOC_MAX
!= c
) {
1087 if ( ! rewind_subblock(MDOC_HEAD
, mdoc
,
1091 if ( ! mdoc_body_alloc(mdoc
, line
,
1094 mdoc
->next
= MDOC_NEXT_CHILD
;
1096 if ( ! mdoc_macro(mdoc
, c
, line
, lastarg
,
1102 if ( ! flushed
&& mdoc_isdelim(p
)) {
1103 if ( ! rewind_subblock(MDOC_HEAD
, mdoc
,
1107 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1109 mdoc
->next
= MDOC_NEXT_CHILD
;
1112 if ( ! mdoc_word_alloc(mdoc
, line
, lastarg
, p
))
1114 mdoc
->next
= MDOC_NEXT_SIBLING
;
1118 if ( ! rewind_subblock(MDOC_HEAD
, mdoc
, tok
, line
, ppos
))
1120 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1122 mdoc
->next
= MDOC_NEXT_CHILD
;
1127 return(append_delims(mdoc
, line
, pos
, buf
));
1132 * A delimited constant is very similar to the macros parsed by
1133 * macro_text except that, in the event of punctuation, the macro isn't
1134 * "re-opened" as it is in macro_text. Also, these macros have a fixed
1135 * number of parameters.
1145 macro_constant_delimited(MACRO_PROT_ARGS
)
1147 int lastarg
, flushed
, j
, c
, maxargs
,
1149 struct mdoc_arg
*arg
;
1156 * Maximum arguments per macro. Some of these have none and
1157 * exit as soon as they're parsed.
1174 * Whether to ignore delimiter characters. `Pf' accepts its
1175 * first token as a parameter no matter what it looks like (if
1189 * Whether to ignore arguments: `St', for example, handles its
1190 * argument-like parameters as regular parameters.
1207 c
= mdoc_argv(mdoc
, line
, tok
, &arg
, pos
, buf
);
1210 if (ARGV_WORD
== c
) {
1213 } else if (ARGV_ARG
== c
)
1215 mdoc_argv_free(arg
);
1219 if ( ! mdoc_elem_alloc(mdoc
, line
, ppos
, tok
, arg
))
1222 mdoc
->next
= MDOC_NEXT_CHILD
;
1224 for (j
= 0; /* No sentinel. */; j
++) {
1227 if (j
== maxargs
&& ! flushed
) {
1228 if ( ! rewind_elem(mdoc
, tok
))
1233 c
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
1234 assert(ARGS_PHRASE
!= c
);
1236 if (ARGS_ERROR
== c
)
1238 if (ARGS_PUNCT
== c
)
1243 if (-1 == (c
= lookup(mdoc
, line
, lastarg
, tok
, p
)))
1245 else if (MDOC_MAX
!= c
) {
1246 if ( ! flushed
&& ! rewind_elem(mdoc
, tok
))
1249 if ( ! mdoc_macro(mdoc
, c
, line
, lastarg
, pos
, buf
))
1254 if ( ! flushed
&& mdoc_isdelim(p
) && ! igndelim
) {
1255 if ( ! rewind_elem(mdoc
, tok
))
1260 if ( ! mdoc_word_alloc(mdoc
, line
, lastarg
, p
))
1262 mdoc
->next
= MDOC_NEXT_SIBLING
;
1265 if ( ! flushed
&& ! rewind_elem(mdoc
, tok
))
1270 return(append_delims(mdoc
, line
, pos
, buf
));
1275 * A constant macro is the simplest classification. It spans an entire
1279 macro_constant(MACRO_PROT_ARGS
)
1282 struct mdoc_arg
*arg
;
1285 assert( ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
));
1291 c
= mdoc_argv(mdoc
, line
, tok
, &arg
, pos
, buf
);
1294 if (ARGV_WORD
== c
) {
1297 } else if (ARGV_ARG
== c
)
1299 mdoc_argv_free(arg
);
1303 if ( ! mdoc_elem_alloc(mdoc
, line
, ppos
, tok
, arg
))
1306 mdoc
->next
= MDOC_NEXT_CHILD
;
1310 w
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
1311 assert(ARGS_PHRASE
!= c
);
1313 if (ARGS_ERROR
== w
)
1318 c
= ARGS_QWORD
== w
? MDOC_MAX
:
1319 lookup(mdoc
, line
, la
, tok
, p
);
1321 if (MDOC_MAX
!= c
&& -1 != c
) {
1322 if ( ! rewind_elem(mdoc
, tok
))
1324 return(mdoc_macro(mdoc
, c
, line
, la
, pos
, buf
));
1328 if ( ! mdoc_word_alloc(mdoc
, line
, la
, p
))
1330 mdoc
->next
= MDOC_NEXT_SIBLING
;
1333 return(rewind_elem(mdoc
, tok
));
1339 macro_obsolete(MACRO_PROT_ARGS
)
1342 return(pwarn(mdoc
, line
, ppos
, WOBS
));
1347 * This is called at the end of parsing. It must traverse up the tree,
1348 * closing out open [implicit] scopes. Obviously, open explicit scopes
1352 macro_end(struct mdoc
*mdoc
)
1354 struct mdoc_node
*n
;
1356 assert(mdoc
->first
);
1359 /* Scan for open explicit scopes. */
1361 n
= MDOC_VALID
& mdoc
->last
->flags
?
1362 mdoc
->last
->parent
: mdoc
->last
;
1364 for ( ; n
; n
= n
->parent
) {
1365 if (MDOC_BLOCK
!= n
->type
)
1367 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[n
->tok
].flags
))
1369 return(mdoc_nerr(mdoc
, n
,
1370 "macro scope still open on exit"));
1373 return(rewind_last(mdoc
, mdoc
->first
));