]>
git.cameronkatri.com Git - mandoc.git/blob - macro.c
1f4642fd9063632383c606a33d860a153f5a6c1f
1 /* $Id: macro.c,v 1.56 2009/03/05 12:08:53 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
35 * FIXME: don't use static mdoc_argv values, as they require us to do a
36 * complicated copy-over when actually assigning them to dynamic memory.
41 static int macro_obsolete(MACRO_PROT_ARGS
);
42 static int macro_constant(MACRO_PROT_ARGS
);
43 static int macro_constant_scoped(MACRO_PROT_ARGS
);
44 static int macro_constant_delimited(MACRO_PROT_ARGS
);
45 static int macro_text(MACRO_PROT_ARGS
);
46 static int macro_scoped(MACRO_PROT_ARGS
);
47 static int macro_scoped_close(MACRO_PROT_ARGS
);
48 static int macro_scoped_line(MACRO_PROT_ARGS
);
50 #define REWIND_REWIND (1 << 0)
51 #define REWIND_NOHALT (1 << 1)
52 #define REWIND_HALT (1 << 2)
54 static int rewind_dohalt(int, enum mdoc_type
,
55 const struct mdoc_node
*);
56 static int rewind_alt(int);
57 static int rewind_dobreak(int, const struct mdoc_node
*);
58 static int rewind_elem(struct mdoc
*, int);
59 static int rewind_impblock(struct mdoc
*, int, int, int);
60 static int rewind_expblock(struct mdoc
*, int, int, int);
61 static int rewind_subblock(enum mdoc_type
,
62 struct mdoc
*, int, int, int);
63 static int rewind_last(struct mdoc
*, struct mdoc_node
*);
64 static int append_delims(struct mdoc
*, int, int *, char *);
65 static int lookup(struct mdoc
*, int, int, int, const char *);
66 static int pwarn(struct mdoc
*, int, int, int);
67 static int perr(struct mdoc
*, int, int, int);
76 /* Central table of library: who gets parsed how. */
78 const struct mdoc_macro __mdoc_macros
[MDOC_MAX
] = {
80 { macro_constant
, MDOC_PROLOGUE
}, /* Dd */
81 { macro_constant
, MDOC_PROLOGUE
}, /* Dt */
82 { macro_constant
, MDOC_PROLOGUE
}, /* Os */
83 { macro_scoped
, 0 }, /* Sh */
84 { macro_scoped
, 0 }, /* Ss */
85 { macro_text
, 0 }, /* Pp */
86 { macro_scoped_line
, MDOC_PARSED
}, /* D1 */
87 { macro_scoped_line
, MDOC_PARSED
}, /* Dl */
88 { macro_scoped
, MDOC_EXPLICIT
}, /* Bd */
89 { macro_scoped_close
, MDOC_EXPLICIT
}, /* Ed */
90 { macro_scoped
, MDOC_EXPLICIT
}, /* Bl */
91 { macro_scoped_close
, MDOC_EXPLICIT
}, /* El */
92 { macro_scoped
, MDOC_PARSED
}, /* It */
93 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ad */
94 { macro_text
, MDOC_PARSED
}, /* An */
95 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ar */
96 { macro_constant
, 0 }, /* Cd */
97 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Cm */
98 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dv */
99 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Er */
100 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ev */
101 { macro_constant
, 0 }, /* Ex */
102 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fa */
103 { macro_constant
, 0 }, /* Fd */
104 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fl */
105 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fn */
106 { macro_text
, MDOC_PARSED
}, /* Ft */
107 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ic */
108 { macro_constant
, 0 }, /* In */
109 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Li */
110 { macro_constant
, 0 }, /* Nd */
111 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Nm */
112 { macro_scoped_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Op */
113 { macro_obsolete
, 0 }, /* Ot */
114 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Pa */
115 { macro_constant
, 0 }, /* Rv */
116 /* XXX - .St supposed to be (but isn't) callable. */
117 { macro_constant_delimited
, MDOC_PARSED
}, /* St */
118 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Va */
119 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Vt */
120 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Xr */
121 { macro_constant
, 0 }, /* %A */
122 { macro_constant
, 0 }, /* %B */
123 { macro_constant
, 0 }, /* %D */
124 { macro_constant
, 0 }, /* %I */
125 { macro_constant
, 0 }, /* %J */
126 { macro_constant
, 0 }, /* %N */
127 { macro_constant
, 0 }, /* %O */
128 { macro_constant
, 0 }, /* %P */
129 { macro_constant
, 0 }, /* %R */
130 { macro_constant
, 0 }, /* %T */
131 { macro_constant
, 0 }, /* %V */
132 { macro_scoped_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Ac */
133 { macro_constant_scoped
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Ao */
134 { macro_scoped_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Aq */
135 { macro_constant_delimited
, 0 }, /* At */
136 { macro_scoped_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Bc */
137 { macro_scoped
, MDOC_EXPLICIT
}, /* Bf */
138 { macro_constant_scoped
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Bo */
139 { macro_scoped_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Bq */
140 { macro_constant_delimited
, MDOC_PARSED
}, /* Bsx */
141 { macro_constant_delimited
, MDOC_PARSED
}, /* Bx */
142 { macro_constant
, 0 }, /* Db */
143 { macro_scoped_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Dc */
144 { macro_constant_scoped
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Do */
145 { macro_scoped_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dq */
146 { macro_scoped_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Ec */
147 { macro_scoped_close
, MDOC_EXPLICIT
}, /* Ef */
148 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Em */
149 { macro_constant_scoped
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Eo */
150 { macro_constant_delimited
, MDOC_PARSED
}, /* Fx */
151 { macro_text
, MDOC_PARSED
}, /* Ms */
152 { macro_constant_delimited
, MDOC_CALLABLE
| MDOC_PARSED
}, /* No */
153 { macro_constant_delimited
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ns */
154 { macro_constant_delimited
, MDOC_PARSED
}, /* Nx */
155 { macro_constant_delimited
, MDOC_PARSED
}, /* Ox */
156 { macro_scoped_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Pc */
157 { macro_constant_delimited
, MDOC_PARSED
}, /* Pf */
158 { macro_constant_scoped
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Po */
159 { macro_scoped_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Pq */
160 { macro_scoped_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Qc */
161 { macro_scoped_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ql */
162 { macro_constant_scoped
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Qo */
163 { macro_scoped_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Qq */
164 { macro_scoped_close
, MDOC_EXPLICIT
}, /* Re */
165 { macro_scoped
, MDOC_EXPLICIT
}, /* Rs */
166 { macro_scoped_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Sc */
167 { macro_constant_scoped
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* So */
168 { macro_scoped_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sq */
169 { macro_constant
, 0 }, /* Sm */
170 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sx */
171 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sy */
172 { macro_text
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Tn */
173 { macro_constant_delimited
, MDOC_PARSED
}, /* Ux */
174 { macro_scoped_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Xc */
175 { macro_constant_scoped
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Xo */
176 /* XXX - .Fo supposed to be (but isn't) callable. */
177 { macro_scoped
, MDOC_EXPLICIT
}, /* Fo */
178 { macro_scoped_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Fc */
179 { macro_constant_scoped
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Oo */
180 { macro_scoped_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Oc */
181 { macro_scoped
, MDOC_EXPLICIT
}, /* Bk */
182 { macro_scoped_close
, MDOC_EXPLICIT
}, /* Ek */
183 { macro_constant
, 0 }, /* Bt */
184 { macro_constant
, 0 }, /* Hf */
185 { macro_obsolete
, 0 }, /* Fr */
186 { macro_constant
, 0 }, /* Ud */
189 const struct mdoc_macro
* const mdoc_macros
= __mdoc_macros
;
193 perr(struct mdoc
*mdoc
, int line
, int pos
, int type
)
199 c
= mdoc_perr(mdoc
, line
, pos
,
200 "closing macro has prior context");
203 c
= mdoc_perr(mdoc
, line
, pos
,
204 "macro doesn't expect parameters");
207 c
= mdoc_perr(mdoc
, line
, pos
,
208 "argument hard-limit %d reached",
219 pwarn(struct mdoc
*mdoc
, int line
, int pos
, int type
)
225 c
= mdoc_pwarn(mdoc
, line
, pos
, WARN_SYNTAX
,
226 "macro-like parameter");
229 c
= mdoc_pwarn(mdoc
, line
, pos
, WARN_SYNTAX
,
230 "macro is marked obsolete");
241 lookup(struct mdoc
*mdoc
, int line
, int pos
, int from
, const char *p
)
245 res
= mdoc_find(mdoc
, p
);
246 if (MDOC_PARSED
& mdoc_macros
[from
].flags
)
250 if ( ! pwarn(mdoc
, line
, pos
, WMACPARM
))
257 rewind_last(struct mdoc
*mdoc
, struct mdoc_node
*to
)
261 mdoc
->next
= MDOC_NEXT_SIBLING
;
264 while (mdoc
->last
!= to
) {
265 if ( ! mdoc_valid_post(mdoc
))
267 if ( ! mdoc_action_post(mdoc
))
269 mdoc
->last
= mdoc
->last
->parent
;
273 if ( ! mdoc_valid_post(mdoc
))
275 return(mdoc_action_post(mdoc
));
322 rewind_dohalt(int tok
, enum mdoc_type type
, const struct mdoc_node
*p
)
325 if (MDOC_ROOT
== p
->type
)
327 if (MDOC_VALID
& p
->flags
)
328 return(REWIND_NOHALT
);
331 /* One-liner implicit-scope. */
351 assert(MDOC_HEAD
!= type
);
352 assert(MDOC_TAIL
!= type
);
353 if (type
== p
->type
&& tok
== p
->tok
)
354 return(REWIND_REWIND
);
357 /* Multi-line implicit-scope. */
359 assert(MDOC_TAIL
!= type
);
360 if (type
== p
->type
&& tok
== p
->tok
)
361 return(REWIND_REWIND
);
362 if (MDOC_BODY
== p
->type
&& MDOC_Bl
== p
->tok
)
366 if (type
== p
->type
&& tok
== p
->tok
)
367 return(REWIND_REWIND
);
370 assert(MDOC_TAIL
!= type
);
371 if (type
== p
->type
&& tok
== p
->tok
)
372 return(REWIND_REWIND
);
373 if (MDOC_BODY
== p
->type
&& MDOC_Sh
== p
->tok
)
377 /* Multi-line explicit scope start. */
407 if (type
== p
->type
&& tok
== p
->tok
)
408 return(REWIND_REWIND
);
411 /* Multi-line explicit scope close. */
441 if (type
== p
->type
&& rewind_alt(tok
) == p
->tok
)
442 return(REWIND_REWIND
);
449 return(REWIND_NOHALT
);
454 rewind_dobreak(int tok
, const struct mdoc_node
*p
)
457 assert(MDOC_ROOT
!= p
->type
);
458 if (MDOC_ELEM
== p
->type
)
460 if (MDOC_TEXT
== p
->type
)
462 if (MDOC_VALID
& p
->flags
)
466 /* Implicit rules. */
468 return(MDOC_It
== p
->tok
);
470 return(MDOC_Ss
== p
->tok
);
472 if (MDOC_Ss
== p
->tok
)
474 return(MDOC_Sh
== p
->tok
);
476 /* Extra scope rules. */
478 if (MDOC_It
== p
->tok
)
485 if (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)
486 return(p
->tok
== rewind_alt(tok
));
487 else if (MDOC_BLOCK
== p
->type
)
490 return(tok
== p
->tok
);
495 rewind_elem(struct mdoc
*mdoc
, int tok
)
500 if (MDOC_ELEM
!= n
->type
)
502 assert(MDOC_ELEM
== n
->type
);
503 assert(tok
== n
->tok
);
505 return(rewind_last(mdoc
, n
));
510 rewind_subblock(enum mdoc_type type
, struct mdoc
*mdoc
,
511 int tok
, int line
, int ppos
)
517 for (n
= mdoc
->last
; n
; n
= n
->parent
) {
518 c
= rewind_dohalt(tok
, type
, n
);
519 if (REWIND_HALT
== c
)
521 if (REWIND_REWIND
== c
)
523 else if (rewind_dobreak(tok
, n
))
525 return(mdoc_perr(mdoc
, line
, ppos
, "scope breaks prior %s", mdoc_node2a(n
)));
529 return(rewind_last(mdoc
, n
));
534 rewind_expblock(struct mdoc
*mdoc
, int tok
, int line
, int ppos
)
540 for (n
= mdoc
->last
; n
; n
= n
->parent
) {
541 c
= rewind_dohalt(tok
, MDOC_BLOCK
, n
);
542 if (REWIND_HALT
== c
)
543 return(perr(mdoc
, line
, ppos
, ENOCTX
));
544 if (REWIND_REWIND
== c
)
546 else if (rewind_dobreak(tok
, n
))
548 return(mdoc_perr(mdoc
, line
, ppos
,
549 "scope breaks prior %s",
554 return(rewind_last(mdoc
, n
));
559 rewind_impblock(struct mdoc
*mdoc
, int tok
, int line
, int ppos
)
565 for (n
= mdoc
->last
; n
; n
= n
->parent
) {
566 c
= rewind_dohalt(tok
, MDOC_BLOCK
, n
);
567 if (REWIND_HALT
== c
)
569 else if (REWIND_REWIND
== c
)
571 else if (rewind_dobreak(tok
, n
))
573 return(mdoc_perr(mdoc
, line
, ppos
,
574 "scope breaks prior %s",
579 return(rewind_last(mdoc
, n
));
584 append_delims(struct mdoc
*mdoc
, int line
, int *pos
, char *buf
)
594 c
= mdoc_args(mdoc
, line
, pos
, buf
, 0, &p
);
595 assert(ARGS_PHRASE
!= c
);
599 else if (ARGS_EOLN
== c
)
601 assert(mdoc_isdelim(p
));
602 if ( ! mdoc_word_alloc(mdoc
, line
, lastarg
, p
))
604 mdoc
->next
= MDOC_NEXT_SIBLING
;
612 * Close out an explicit scope. This optionally parses a TAIL type with
613 * a set number of TEXT children.
616 macro_scoped_close(MACRO_PROT_ARGS
)
618 int tt
, j
, c
, lastarg
, maxargs
, flushed
;
630 tt
= rewind_alt(tok
);
632 mdoc_msg(mdoc
, "parse: %s closing %s",
633 mdoc_macronames
[tok
], mdoc_macronames
[tt
]);
635 if ( ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
)) {
636 if (0 == buf
[*pos
]) {
637 if ( ! rewind_subblock(MDOC_BODY
, mdoc
,
640 return(rewind_expblock(mdoc
, tok
, line
, ppos
));
642 return(perr(mdoc
, line
, ppos
, ENOPARMS
));
645 if ( ! rewind_subblock(MDOC_BODY
, mdoc
, tok
, line
, ppos
))
652 if ( ! mdoc_tail_alloc(mdoc
, line
, ppos
, tt
))
654 mdoc
->next
= MDOC_NEXT_CHILD
;
657 for (j
= 0; /* No sentinel. */; j
++) {
660 if (j
== maxargs
&& ! flushed
) {
661 if ( ! rewind_expblock(mdoc
, tok
, line
, ppos
))
666 c
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
667 assert(ARGS_PHRASE
!= c
);
676 if (-1 == (c
= lookup(mdoc
, line
, lastarg
, tok
, p
)))
678 else if (MDOC_MAX
!= c
) {
680 if ( ! rewind_expblock(mdoc
, tok
,
685 if ( ! mdoc_macro(mdoc
, c
, line
, lastarg
, pos
, buf
))
690 if ( ! mdoc_word_alloc(mdoc
, line
, lastarg
, p
))
692 mdoc
->next
= MDOC_NEXT_SIBLING
;
695 if ( ! flushed
&& ! rewind_expblock(mdoc
, tok
, line
, ppos
))
700 return(append_delims(mdoc
, line
, pos
, buf
));
705 * A general text macro. This is a complex case because of punctuation.
706 * If a text macro is followed by words, then punctuation, the macro is
707 * "stopped" and "reopened" following the punctuation. Thus, the
718 * This must handle the following situations:
729 macro_text(MACRO_PROT_ARGS
)
731 int la
, lastpunct
, c
, w
, argc
;
732 struct mdoc_arg argv
[MDOC_LINEARG_MAX
];
738 for (argc
= 0; argc
< MDOC_LINEARG_MAX
; argc
++) {
740 c
= mdoc_argv(mdoc
, line
, tok
, &argv
[argc
], pos
, buf
);
743 if (ARGV_WORD
== c
) {
746 } else if (ARGV_ARG
== c
)
749 mdoc_argv_free(argc
, argv
);
753 if (MDOC_LINEARG_MAX
== argc
) {
754 mdoc_argv_free(argc
- 1, argv
);
755 return(perr(mdoc
, line
, ppos
, EARGVLIM
));
758 c
= mdoc_elem_alloc(mdoc
, line
, ppos
,
759 tok
, (size_t)argc
, argv
);
762 mdoc_argv_free(argc
, argv
);
766 mdoc
->next
= MDOC_NEXT_CHILD
;
771 w
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
772 assert(ARGS_PHRASE
!= c
);
774 if (ARGS_ERROR
== w
) {
775 mdoc_argv_free(argc
, argv
);
784 c
= ARGS_QWORD
== w
? MDOC_MAX
:
785 lookup(mdoc
, line
, la
, tok
, p
);
787 if (MDOC_MAX
!= c
&& -1 != c
) {
788 if (0 == lastpunct
&& ! rewind_elem(mdoc
, tok
)) {
789 mdoc_argv_free(argc
, argv
);
792 mdoc_argv_free(argc
, argv
);
793 c
= mdoc_macro(mdoc
, c
, line
, la
, pos
, buf
);
798 return(append_delims(mdoc
, line
, pos
, buf
));
799 } else if (-1 == c
) {
800 mdoc_argv_free(argc
, argv
);
804 /* FIXME: .Fl and .Ar handling of `|'. */
806 if (ARGS_QWORD
!= w
&& mdoc_isdelim(p
)) {
807 if (0 == lastpunct
&& ! rewind_elem(mdoc
, tok
)) {
808 mdoc_argv_free(argc
, argv
);
812 } else if (lastpunct
) {
813 c
= mdoc_elem_alloc(mdoc
, line
, ppos
,
814 tok
, (size_t)argc
, argv
);
816 mdoc_argv_free(argc
, argv
);
819 mdoc
->next
= MDOC_NEXT_CHILD
;
823 if ( ! mdoc_word_alloc(mdoc
, line
, la
, p
))
825 mdoc
->next
= MDOC_NEXT_SIBLING
;
828 mdoc_argv_free(argc
, argv
);
830 if (0 == lastpunct
&& ! rewind_elem(mdoc
, tok
))
834 return(append_delims(mdoc
, line
, pos
, buf
));
839 * Handle explicit-scope (having a different closure token) and implicit
840 * scope (closing out prior scopes when re-invoked) macros. These
841 * constitute the BLOCK type and usually span multiple lines. These
842 * always have HEAD and sometimes have BODY types. In the multi-line
859 * Note that the `.It' macro, possibly the most difficult (as it has
860 * embedded scope, etc.) is handled by this routine.
863 macro_scoped(MACRO_PROT_ARGS
)
865 int c
, lastarg
, argc
;
866 struct mdoc_arg argv
[MDOC_LINEARG_MAX
];
869 assert ( ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
));
871 /* First rewind extant implicit scope. */
873 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)) {
874 if ( ! rewind_subblock(MDOC_BODY
, mdoc
, tok
, line
, ppos
))
876 if ( ! rewind_impblock(mdoc
, tok
, line
, ppos
))
880 /* Parse arguments. */
882 for (argc
= 0; argc
< MDOC_LINEARG_MAX
; argc
++) {
884 c
= mdoc_argv(mdoc
, line
, tok
, &argv
[argc
], pos
, buf
);
887 if (ARGV_WORD
== c
) {
890 } else if (ARGV_ARG
== c
)
892 mdoc_argv_free(argc
, argv
);
896 if (MDOC_LINEARG_MAX
== argc
) {
897 mdoc_argv_free(argc
- 1, argv
);
898 return(perr(mdoc
, line
, ppos
, EARGVLIM
));
901 c
= mdoc_block_alloc(mdoc
, line
, ppos
,
902 tok
, (size_t)argc
, argv
);
903 mdoc_argv_free(argc
, argv
);
908 mdoc
->next
= MDOC_NEXT_CHILD
;
910 if (0 == buf
[*pos
]) {
911 if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
913 if ( ! rewind_subblock(MDOC_HEAD
, mdoc
,
916 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
918 mdoc
->next
= MDOC_NEXT_CHILD
;
922 if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
924 mdoc
->next
= MDOC_NEXT_CHILD
;
928 c
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
934 if (ARGS_PHRASE
== c
) {
936 if ( ! mdoc_phrase(mdoc, line, lastarg, buf))
942 /* FIXME: if .It -column, the lookup must be for a
943 * sub-line component. BLAH. */
945 if (-1 == (c
= lookup(mdoc
, line
, lastarg
, tok
, p
)))
949 if ( ! mdoc_word_alloc(mdoc
, line
, lastarg
, p
))
951 mdoc
->next
= MDOC_NEXT_SIBLING
;
955 if ( ! mdoc_macro(mdoc
, c
, line
, lastarg
, pos
, buf
))
960 if (1 == ppos
&& ! append_delims(mdoc
, line
, pos
, buf
))
962 if ( ! rewind_subblock(MDOC_HEAD
, mdoc
, tok
, line
, ppos
))
965 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
967 mdoc
->next
= MDOC_NEXT_CHILD
;
974 * This handles a case of implicitly-scoped macro (BLOCK) limited to a
975 * single line. Instead of being closed out by a subsequent call to
976 * another macro, the scope is closed at the end of line. These don't
977 * have BODY or TAIL types. Notice that the punctuation falls outside
980 * .Qq a Fl b Ar d ; ;
993 macro_scoped_line(MACRO_PROT_ARGS
)
998 if ( ! mdoc_block_alloc(mdoc
, line
, ppos
, tok
, 0, NULL
))
1000 mdoc
->next
= MDOC_NEXT_CHILD
;
1002 if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
1004 mdoc
->next
= MDOC_NEXT_SIBLING
;
1005 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1007 mdoc
->next
= MDOC_NEXT_CHILD
;
1009 /* XXX - no known argument macros. */
1014 c
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
1015 assert(ARGS_PHRASE
!= c
);
1017 if (ARGS_ERROR
== c
)
1019 if (ARGS_PUNCT
== c
)
1024 if (-1 == (c
= lookup(mdoc
, line
, lastarg
, tok
, p
)))
1026 else if (MDOC_MAX
== c
) {
1027 if ( ! mdoc_word_alloc(mdoc
, line
, lastarg
, p
))
1029 mdoc
->next
= MDOC_NEXT_SIBLING
;
1033 if ( ! mdoc_macro(mdoc
, c
, line
, lastarg
, pos
, buf
))
1039 if ( ! rewind_subblock(MDOC_BODY
, mdoc
, tok
, line
, ppos
))
1041 if ( ! append_delims(mdoc
, line
, pos
, buf
))
1043 } else if ( ! rewind_subblock(MDOC_BODY
, mdoc
, tok
, line
, ppos
))
1045 return(rewind_impblock(mdoc
, tok
, line
, ppos
));
1050 * A constant-scoped macro is like a simple-scoped macro (mdoc_scoped)
1051 * except that it doesn't handle implicit scopes and explicit ones have
1052 * a fixed number of TEXT children to the BODY.
1065 macro_constant_scoped(MACRO_PROT_ARGS
)
1067 int lastarg
, flushed
, j
, c
, maxargs
;
1082 if ( ! mdoc_block_alloc(mdoc
, line
, ppos
, tok
, 0, NULL
))
1084 mdoc
->next
= MDOC_NEXT_CHILD
;
1087 if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
1089 if ( ! rewind_subblock(MDOC_HEAD
, mdoc
, tok
, line
, ppos
))
1091 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1094 } else if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
1097 mdoc
->next
= MDOC_NEXT_CHILD
;
1099 for (j
= 0; /* No sentinel. */; j
++) {
1102 if (j
== maxargs
&& ! flushed
) {
1103 if ( ! rewind_subblock(MDOC_HEAD
, mdoc
, tok
, line
, ppos
))
1106 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1108 mdoc
->next
= MDOC_NEXT_CHILD
;
1111 c
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
1112 assert(ARGS_PHRASE
!= c
);
1114 if (ARGS_ERROR
== c
)
1116 if (ARGS_PUNCT
== c
)
1121 if (-1 == (c
= lookup(mdoc
, line
, lastarg
, tok
, p
)))
1123 else if (MDOC_MAX
!= c
) {
1125 if ( ! rewind_subblock(MDOC_HEAD
, mdoc
,
1129 if ( ! mdoc_body_alloc(mdoc
, line
,
1132 mdoc
->next
= MDOC_NEXT_CHILD
;
1134 if ( ! mdoc_macro(mdoc
, c
, line
, lastarg
,
1140 if ( ! flushed
&& mdoc_isdelim(p
)) {
1141 if ( ! rewind_subblock(MDOC_HEAD
, mdoc
,
1145 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1147 mdoc
->next
= MDOC_NEXT_CHILD
;
1150 if ( ! mdoc_word_alloc(mdoc
, line
, lastarg
, p
))
1152 mdoc
->next
= MDOC_NEXT_SIBLING
;
1156 if ( ! rewind_subblock(MDOC_HEAD
, mdoc
, tok
, line
, ppos
))
1158 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1160 mdoc
->next
= MDOC_NEXT_CHILD
;
1165 return(append_delims(mdoc
, line
, pos
, buf
));
1170 * A delimited constant is very similar to the macros parsed by
1171 * macro_text except that, in the event of punctuation, the macro isn't
1172 * "re-opened" as it is in macro_text. Also, these macros have a fixed
1173 * number of parameters.
1183 macro_constant_delimited(MACRO_PROT_ARGS
)
1185 int lastarg
, flushed
, j
, c
, maxargs
, argc
,
1187 struct mdoc_arg argv
[MDOC_LINEARG_MAX
];
1217 for (argc
= 0; argc
< MDOC_LINEARG_MAX
; argc
++) {
1219 c
= mdoc_argv(mdoc
, line
, tok
, &argv
[argc
], pos
, buf
);
1222 if (ARGV_WORD
== c
) {
1225 } else if (ARGV_ARG
== c
)
1227 mdoc_argv_free(argc
, argv
);
1231 if (MDOC_LINEARG_MAX
== argc
) {
1232 mdoc_argv_free(argc
- 1, argv
);
1233 return(perr(mdoc
, line
, ppos
, EARGVLIM
));
1236 c
= mdoc_elem_alloc(mdoc
, line
, ppos
,
1237 tok
, (size_t)argc
, argv
);
1239 mdoc_argv_free(argc
, argv
);
1244 mdoc
->next
= MDOC_NEXT_CHILD
;
1246 for (j
= 0; /* No sentinel. */; j
++) {
1249 if (j
== maxargs
&& ! flushed
) {
1250 if ( ! rewind_elem(mdoc
, tok
))
1255 c
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
1256 assert(ARGS_PHRASE
!= c
);
1258 if (ARGS_ERROR
== c
)
1260 if (ARGS_PUNCT
== c
)
1265 if (-1 == (c
= lookup(mdoc
, line
, lastarg
, tok
, p
)))
1267 else if (MDOC_MAX
!= c
) {
1268 if ( ! flushed
&& ! rewind_elem(mdoc
, tok
))
1271 if ( ! mdoc_macro(mdoc
, c
, line
, lastarg
, pos
, buf
))
1276 if ( ! flushed
&& mdoc_isdelim(p
) && ! igndelim
) {
1277 if ( ! rewind_elem(mdoc
, tok
))
1282 if ( ! mdoc_word_alloc(mdoc
, line
, lastarg
, p
))
1284 mdoc
->next
= MDOC_NEXT_SIBLING
;
1287 if ( ! flushed
&& ! rewind_elem(mdoc
, tok
))
1292 return(append_delims(mdoc
, line
, pos
, buf
));
1297 * A constant macro is the simplest classification. It spans an entire
1301 macro_constant(MACRO_PROT_ARGS
)
1304 struct mdoc_arg argv
[MDOC_LINEARG_MAX
];
1307 assert( ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
));
1309 for (argc
= 0; argc
< MDOC_LINEARG_MAX
; argc
++) {
1311 c
= mdoc_argv(mdoc
, line
, tok
, &argv
[argc
], pos
, buf
);
1314 if (ARGV_WORD
== c
) {
1317 } else if (ARGV_ARG
== c
)
1320 mdoc_argv_free(argc
, argv
);
1324 if (MDOC_LINEARG_MAX
== argc
) {
1325 mdoc_argv_free(argc
- 1, argv
);
1326 return(perr(mdoc
, line
, ppos
, EARGVLIM
));
1329 c
= mdoc_elem_alloc(mdoc
, line
, ppos
,
1330 tok
, (size_t)argc
, argv
);
1332 mdoc_argv_free(argc
, argv
);
1337 mdoc
->next
= MDOC_NEXT_CHILD
;
1341 w
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
1342 assert(ARGS_PHRASE
!= c
);
1344 if (ARGS_ERROR
== w
)
1349 c
= ARGS_QWORD
== w
? MDOC_MAX
:
1350 lookup(mdoc
, line
, la
, tok
, p
);
1352 if (MDOC_MAX
!= c
&& -1 != c
) {
1353 if ( ! rewind_elem(mdoc
, tok
))
1355 return(mdoc_macro(mdoc
, c
, line
, la
, pos
, buf
));
1359 if ( ! mdoc_word_alloc(mdoc
, line
, la
, p
))
1361 mdoc
->next
= MDOC_NEXT_SIBLING
;
1364 return(rewind_elem(mdoc
, tok
));
1370 macro_obsolete(MACRO_PROT_ARGS
)
1373 return(pwarn(mdoc
, line
, ppos
, WOBS
));
1378 * This is called at the end of parsing. It must traverse up the tree,
1379 * closing out open [implicit] scopes. Obviously, open explicit scopes
1383 macro_end(struct mdoc
*mdoc
)
1385 struct mdoc_node
*n
;
1387 assert(mdoc
->first
);
1390 /* Scan for open explicit scopes. */
1392 n
= MDOC_VALID
& mdoc
->last
->flags
?
1393 mdoc
->last
->parent
: mdoc
->last
;
1395 for ( ; n
; n
= n
->parent
) {
1396 if (MDOC_BLOCK
!= n
->type
)
1398 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[n
->tok
].flags
))
1400 return(mdoc_nerr(mdoc
, n
,
1401 "macro scope still open on exit"));
1404 return(rewind_last(mdoc
, mdoc
->first
));