]>
git.cameronkatri.com Git - mandoc.git/blob - macro.c
1 /* $Id: macro.c,v 1.50 2009/02/22 19:23:48 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
31 * macro. Macros are parsed according as follows:
33 * ELEMENT: TEXT | epsilon
34 * BLOCK: HEAD PUNCT BODY PUNCT BLOCK_TAIL PUNCT
35 * BLOCK_TAIL: TAIL | epsilon
36 * HEAD: ELEMENT | TEXT | BLOCK | epsilon
37 * BODY: ELEMENT | TEXT | BLOCK | epsilon
38 * TAIL: TEXT | epsilon
39 * PUNCT: TEXT (delimiters) | epsilon
41 * These are arranged into a parse tree, an example of which follows:
51 * TEXT (`mdoc macro compiler')
66 * These types are always per-line except for block bodies, which may
67 * span multiple lines. Macros are assigned a parsing routine, which
68 * corresponds to the type, in the mdoc_macros table.
70 * Note that types are general: there can be several parsing routines
71 * corresponding to a single type. The macro_text function, for
72 * example, parses an ELEMENT type (see the function definition for
73 * details) that may be interrupted by further macros; the
74 * macro_constant function, on the other hand, parses an ELEMENT type
75 * spanning a single line.
80 #define REWIND_REWIND (1 << 0)
81 #define REWIND_NOHALT (1 << 1)
82 #define REWIND_HALT (1 << 2)
84 static int rewind_dohalt(int, enum mdoc_type
,
85 const struct mdoc_node
*);
86 static int rewind_alt(int);
87 static int rewind_dobreak(int, const struct mdoc_node
*);
88 static int rewind_elem(struct mdoc
*, int);
89 static int rewind_impblock(struct mdoc
*, int, int, int);
90 static int rewind_expblock(struct mdoc
*, int, int, int);
91 static int rewind_subblock(enum mdoc_type
,
92 struct mdoc
*, int, int, int);
93 static int rewind_last(struct mdoc
*, struct mdoc_node
*);
94 static int append_delims(struct mdoc
*, int, int *, char *);
95 static int lookup(struct mdoc
*, int, int, int, const char *);
96 static int pwarn(struct mdoc
*, int, int, int);
97 static int perr(struct mdoc
*, int, int, int);
108 perr(struct mdoc
*mdoc
, int line
, int pos
, int type
)
114 c
= mdoc_perr(mdoc
, line
, pos
,
115 "closing macro has prior context");
118 c
= mdoc_perr(mdoc
, line
, pos
,
119 "macro doesn't expect parameters");
122 c
= mdoc_perr(mdoc
, line
, pos
,
123 "argument hard-limit %d reached",
134 pwarn(struct mdoc
*mdoc
, int line
, int pos
, int type
)
140 c
= mdoc_pwarn(mdoc
, line
, pos
, WARN_SYNTAX
,
141 "macro-like parameter");
144 c
= mdoc_pwarn(mdoc
, line
, pos
, WARN_SYNTAX
,
145 "macro is marked obsolete");
156 lookup(struct mdoc
*mdoc
, int line
, int pos
, int from
, const char *p
)
160 res
= mdoc_find(mdoc
, p
);
161 if (MDOC_PARSED
& mdoc_macros
[from
].flags
)
165 if ( ! pwarn(mdoc
, line
, pos
, WMACPARM
))
172 rewind_last(struct mdoc
*mdoc
, struct mdoc_node
*to
)
176 mdoc
->next
= MDOC_NEXT_SIBLING
;
179 while (mdoc
->last
!= to
) {
180 if ( ! mdoc_valid_post(mdoc
))
182 if ( ! mdoc_action_post(mdoc
))
184 mdoc
->last
= mdoc
->last
->parent
;
188 if ( ! mdoc_valid_post(mdoc
))
190 return(mdoc_action_post(mdoc
));
237 rewind_dohalt(int tok
, enum mdoc_type type
, const struct mdoc_node
*p
)
240 if (MDOC_ROOT
== p
->type
)
242 if (MDOC_VALID
& p
->flags
)
243 return(REWIND_NOHALT
);
246 /* One-liner implicit-scope. */
266 assert(MDOC_HEAD
!= type
);
267 assert(MDOC_TAIL
!= type
);
268 if (type
== p
->type
&& tok
== p
->tok
)
269 return(REWIND_REWIND
);
272 /* Multi-line implicit-scope. */
274 assert(MDOC_TAIL
!= type
);
275 if (type
== p
->type
&& tok
== p
->tok
)
276 return(REWIND_REWIND
);
277 if (MDOC_BODY
== p
->type
&& MDOC_Bl
== p
->tok
)
281 if (type
== p
->type
&& tok
== p
->tok
)
282 return(REWIND_REWIND
);
285 assert(MDOC_TAIL
!= type
);
286 if (type
== p
->type
&& tok
== p
->tok
)
287 return(REWIND_REWIND
);
288 if (MDOC_BODY
== p
->type
&& MDOC_Sh
== p
->tok
)
292 /* Multi-line explicit scope start. */
322 if (type
== p
->type
&& tok
== p
->tok
)
323 return(REWIND_REWIND
);
326 /* Multi-line explicit scope close. */
356 if (type
== p
->type
&& rewind_alt(tok
) == p
->tok
)
357 return(REWIND_REWIND
);
364 return(REWIND_NOHALT
);
369 rewind_dobreak(int tok
, const struct mdoc_node
*p
)
372 assert(MDOC_ROOT
!= p
->type
);
373 if (MDOC_ELEM
== p
->type
)
375 if (MDOC_TEXT
== p
->type
)
377 if (MDOC_VALID
& p
->flags
)
381 /* Implicit rules. */
383 return(MDOC_It
== p
->tok
);
385 return(MDOC_Ss
== p
->tok
);
387 if (MDOC_Ss
== p
->tok
)
389 return(MDOC_Sh
== p
->tok
);
391 /* Extra scope rules. */
393 if (MDOC_It
== p
->tok
)
400 if (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)
401 return(p
->tok
== rewind_alt(tok
));
402 else if (MDOC_BLOCK
== p
->type
)
405 return(tok
== p
->tok
);
410 rewind_elem(struct mdoc
*mdoc
, int tok
)
415 if (MDOC_ELEM
!= n
->type
)
417 assert(MDOC_ELEM
== n
->type
);
418 assert(tok
== n
->tok
);
420 return(rewind_last(mdoc
, n
));
425 rewind_subblock(enum mdoc_type type
, struct mdoc
*mdoc
,
426 int tok
, int line
, int ppos
)
432 for (n
= mdoc
->last
; n
; n
= n
->parent
) {
433 c
= rewind_dohalt(tok
, type
, n
);
434 if (REWIND_HALT
== c
)
436 if (REWIND_REWIND
== c
)
438 else if (rewind_dobreak(tok
, n
))
440 return(mdoc_perr(mdoc
, line
, ppos
, "scope breaks prior %s", mdoc_node2a(n
)));
444 return(rewind_last(mdoc
, n
));
449 rewind_expblock(struct mdoc
*mdoc
, int tok
, int line
, int ppos
)
455 for (n
= mdoc
->last
; n
; n
= n
->parent
) {
456 c
= rewind_dohalt(tok
, MDOC_BLOCK
, n
);
457 if (REWIND_HALT
== c
)
458 return(perr(mdoc
, line
, ppos
, ENOCTX
));
459 if (REWIND_REWIND
== c
)
461 else if (rewind_dobreak(tok
, n
))
463 return(mdoc_perr(mdoc
, line
, ppos
, "scope breaks prior %s", mdoc_node2a(n
)));
467 return(rewind_last(mdoc
, n
));
472 rewind_impblock(struct mdoc
*mdoc
, int tok
, int line
, int ppos
)
478 for (n
= mdoc
->last
; n
; n
= n
->parent
) {
479 c
= rewind_dohalt(tok
, MDOC_BLOCK
, n
);
480 if (REWIND_HALT
== c
)
482 else if (REWIND_REWIND
== c
)
484 else if (rewind_dobreak(tok
, n
))
486 return(mdoc_perr(mdoc
, line
, ppos
, "scope breaks prior %s", mdoc_node2a(n
)));
490 return(rewind_last(mdoc
, n
));
495 append_delims(struct mdoc
*mdoc
, int line
, int *pos
, char *buf
)
505 c
= mdoc_args(mdoc
, line
, pos
, buf
, 0, &p
);
506 assert(ARGS_PHRASE
!= c
);
510 else if (ARGS_EOLN
== c
)
512 assert(mdoc_isdelim(p
));
513 if ( ! mdoc_word_alloc(mdoc
, line
, lastarg
, p
))
515 mdoc
->next
= MDOC_NEXT_SIBLING
;
523 * Close out an explicit scope. This optionally parses a TAIL type with
524 * a set number of TEXT children.
527 macro_scoped_close(MACRO_PROT_ARGS
)
529 int tt
, j
, c
, lastarg
, maxargs
, flushed
;
541 tt
= rewind_alt(tok
);
543 mdoc_msg(mdoc
, "parse: %s closing %s",
544 mdoc_macronames
[tok
], mdoc_macronames
[tt
]);
546 if ( ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
)) {
547 if (0 == buf
[*pos
]) {
548 if ( ! rewind_subblock(MDOC_BODY
, mdoc
, tok
, line
, ppos
))
550 return(rewind_expblock(mdoc
, tok
, line
, ppos
));
552 return(perr(mdoc
, line
, ppos
, ENOPARMS
));
555 if ( ! rewind_subblock(MDOC_BODY
, mdoc
, tok
, line
, ppos
))
562 if ( ! mdoc_tail_alloc(mdoc
, line
, ppos
, tt
))
564 mdoc
->next
= MDOC_NEXT_CHILD
;
567 for (j
= 0; /* No sentinel. */; j
++) {
570 if (j
== maxargs
&& ! flushed
) {
571 if ( ! rewind_expblock(mdoc
, tok
, line
, ppos
))
576 c
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
577 assert(ARGS_PHRASE
!= c
);
586 if (-1 == (c
= lookup(mdoc
, line
, lastarg
, tok
, p
)))
588 else if (MDOC_MAX
!= c
) {
590 if ( ! rewind_expblock(mdoc
, tok
, line
, ppos
))
594 if ( ! mdoc_macro(mdoc
, c
, line
, lastarg
, pos
, buf
))
599 if ( ! mdoc_word_alloc(mdoc
, line
, lastarg
, p
))
601 mdoc
->next
= MDOC_NEXT_SIBLING
;
604 if ( ! flushed
&& ! rewind_expblock(mdoc
, tok
, line
, ppos
))
609 return(append_delims(mdoc
, line
, pos
, buf
));
614 * A general text macro. This is a complex case because of punctuation.
615 * If a text macro is followed by words, then punctuation, the macro is
616 * "stopped" and "reopened" following the punctuation. Thus, the
627 * This must handle the following situations:
638 macro_text(MACRO_PROT_ARGS
)
640 int la
, lastpunct
, c
, w
, argc
;
641 struct mdoc_arg argv
[MDOC_LINEARG_MAX
];
647 for (argc
= 0; argc
< MDOC_LINEARG_MAX
; argc
++) {
649 c
= mdoc_argv(mdoc
, line
, tok
, &argv
[argc
], pos
, buf
);
652 if (ARGV_WORD
== c
) {
655 } else if (ARGV_ARG
== c
)
658 mdoc_argv_free(argc
, argv
);
662 if (MDOC_LINEARG_MAX
== argc
) {
663 mdoc_argv_free(argc
- 1, argv
);
664 return(perr(mdoc
, line
, ppos
, EARGVLIM
));
667 c
= mdoc_elem_alloc(mdoc
, line
, ppos
, tok
, argc
, argv
);
670 mdoc_argv_free(argc
, argv
);
674 mdoc
->next
= MDOC_NEXT_CHILD
;
679 w
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
680 assert(ARGS_PHRASE
!= c
);
682 if (ARGS_ERROR
== w
) {
683 mdoc_argv_free(argc
, argv
);
692 c
= ARGS_QWORD
== w
? MDOC_MAX
:
693 lookup(mdoc
, line
, la
, tok
, p
);
695 if (MDOC_MAX
!= c
&& -1 != c
) {
696 if (0 == lastpunct
&& ! rewind_elem(mdoc
, tok
)) {
697 mdoc_argv_free(argc
, argv
);
700 mdoc_argv_free(argc
, argv
);
701 c
= mdoc_macro(mdoc
, c
, line
, la
, pos
, buf
);
706 return(append_delims(mdoc
, line
, pos
, buf
));
707 } else if (-1 == c
) {
708 mdoc_argv_free(argc
, argv
);
712 if (ARGS_QWORD
!= w
&& mdoc_isdelim(p
)) {
713 if (0 == lastpunct
&& ! rewind_elem(mdoc
, tok
)) {
714 mdoc_argv_free(argc
, argv
);
718 } else if (lastpunct
) {
719 c
= mdoc_elem_alloc(mdoc
, line
,
720 ppos
, tok
, argc
, argv
);
722 mdoc_argv_free(argc
, argv
);
725 mdoc
->next
= MDOC_NEXT_CHILD
;
729 if ( ! mdoc_word_alloc(mdoc
, line
, la
, p
))
731 mdoc
->next
= MDOC_NEXT_SIBLING
;
734 mdoc_argv_free(argc
, argv
);
736 if (0 == lastpunct
&& ! rewind_elem(mdoc
, tok
))
740 return(append_delims(mdoc
, line
, pos
, buf
));
745 * Handle explicit-scope (having a different closure token) and implicit
746 * scope (closing out prior scopes when re-invoked) macros. These
747 * constitute the BLOCK type and usually span multiple lines. These
748 * always have HEAD and sometimes have BODY types. In the multi-line
765 * Note that the `.It' macro, possibly the most difficult (as it has
766 * embedded scope, etc.) is handled by this routine.
769 macro_scoped(MACRO_PROT_ARGS
)
771 int c
, lastarg
, argc
;
772 struct mdoc_arg argv
[MDOC_LINEARG_MAX
];
775 assert ( ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
));
777 /* First rewind extant implicit scope. */
779 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)) {
780 if ( ! rewind_subblock(MDOC_BODY
, mdoc
, tok
, line
, ppos
))
782 if ( ! rewind_impblock(mdoc
, tok
, line
, ppos
))
786 /* Parse arguments. */
788 for (argc
= 0; argc
< MDOC_LINEARG_MAX
; argc
++) {
790 c
= mdoc_argv(mdoc
, line
, tok
, &argv
[argc
], pos
, buf
);
793 if (ARGV_WORD
== c
) {
796 } else if (ARGV_ARG
== c
)
798 mdoc_argv_free(argc
, argv
);
802 if (MDOC_LINEARG_MAX
== argc
) {
803 mdoc_argv_free(argc
- 1, argv
);
804 return(perr(mdoc
, line
, ppos
, EARGVLIM
));
807 c
= mdoc_block_alloc(mdoc
, line
, ppos
,
808 tok
, (size_t)argc
, argv
);
809 mdoc_argv_free(argc
, argv
);
814 mdoc
->next
= MDOC_NEXT_CHILD
;
816 if (0 == buf
[*pos
]) {
817 if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
819 if ( ! rewind_subblock(MDOC_HEAD
, mdoc
, tok
, line
, ppos
))
821 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
823 mdoc
->next
= MDOC_NEXT_CHILD
;
827 if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
829 mdoc
->next
= MDOC_NEXT_CHILD
;
833 c
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
842 if (ARGS_PHRASE
== c
) {
844 if ( ! mdoc_phrase(mdoc, line, lastarg, buf))
850 /* FIXME: if .It -column, the lookup must be for a
851 * sub-line component. BLAH. */
853 if (-1 == (c
= lookup(mdoc
, line
, lastarg
, tok
, p
)))
857 if ( ! mdoc_word_alloc(mdoc
, line
, lastarg
, p
))
859 mdoc
->next
= MDOC_NEXT_SIBLING
;
863 if ( ! mdoc_macro(mdoc
, c
, line
, lastarg
, pos
, buf
))
868 if ( ! rewind_subblock(MDOC_HEAD
, mdoc
, tok
, line
, ppos
))
870 if (1 == ppos
&& ! append_delims(mdoc
, line
, pos
, buf
))
873 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
875 mdoc
->next
= MDOC_NEXT_CHILD
;
882 * This handles a case of implicitly-scoped macro (BLOCK) limited to a
883 * single line. Instead of being closed out by a subsequent call to
884 * another macro, the scope is closed at the end of line. These don't
885 * have BODY or TAIL types. Notice that the punctuation falls outside
888 * .Qq a Fl b Ar d ; ;
901 macro_scoped_line(MACRO_PROT_ARGS
)
906 if ( ! mdoc_block_alloc(mdoc
, line
, ppos
, tok
, 0, NULL
))
908 mdoc
->next
= MDOC_NEXT_CHILD
;
910 if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
912 mdoc
->next
= MDOC_NEXT_SIBLING
;
913 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
915 mdoc
->next
= MDOC_NEXT_CHILD
;
917 /* XXX - no known argument macros. */
922 c
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
923 assert(ARGS_PHRASE
!= c
);
932 if (-1 == (c
= lookup(mdoc
, line
, lastarg
, tok
, p
)))
934 else if (MDOC_MAX
== c
) {
935 if ( ! mdoc_word_alloc(mdoc
, line
, lastarg
, p
))
937 mdoc
->next
= MDOC_NEXT_SIBLING
;
941 if ( ! mdoc_macro(mdoc
, c
, line
, lastarg
, pos
, buf
))
947 if ( ! rewind_subblock(MDOC_BODY
, mdoc
, tok
, line
, ppos
))
949 if ( ! append_delims(mdoc
, line
, pos
, buf
))
951 } else if ( ! rewind_subblock(MDOC_BODY
, mdoc
, tok
, line
, ppos
))
953 return(rewind_impblock(mdoc
, tok
, line
, ppos
));
958 * A constant-scoped macro is like a simple-scoped macro (mdoc_scoped)
959 * except that it doesn't handle implicit scopes and explicit ones have
960 * a fixed number of TEXT children to the BODY.
973 macro_constant_scoped(MACRO_PROT_ARGS
)
975 int lastarg
, flushed
, j
, c
, maxargs
;
990 if ( ! mdoc_block_alloc(mdoc
, line
, ppos
, tok
, 0, NULL
))
992 mdoc
->next
= MDOC_NEXT_CHILD
;
995 if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
997 if ( ! rewind_subblock(MDOC_HEAD
, mdoc
, tok
, line
, ppos
))
999 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1002 } else if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
1005 mdoc
->next
= MDOC_NEXT_CHILD
;
1007 for (j
= 0; /* No sentinel. */; j
++) {
1010 if (j
== maxargs
&& ! flushed
) {
1011 if ( ! rewind_subblock(MDOC_HEAD
, mdoc
, tok
, line
, ppos
))
1014 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1016 mdoc
->next
= MDOC_NEXT_CHILD
;
1019 c
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
1020 assert(ARGS_PHRASE
!= c
);
1022 if (ARGS_ERROR
== c
)
1024 if (ARGS_PUNCT
== c
)
1029 if (-1 == (c
= lookup(mdoc
, line
, lastarg
, tok
, p
)))
1031 else if (MDOC_MAX
!= c
) {
1033 if ( ! rewind_subblock(MDOC_HEAD
, mdoc
, tok
, line
, ppos
))
1036 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1038 mdoc
->next
= MDOC_NEXT_CHILD
;
1040 if ( ! mdoc_macro(mdoc
, c
, line
, lastarg
, pos
, buf
))
1045 if ( ! flushed
&& mdoc_isdelim(p
)) {
1046 if ( ! rewind_subblock(MDOC_HEAD
, mdoc
, tok
, line
, ppos
))
1049 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1051 mdoc
->next
= MDOC_NEXT_CHILD
;
1054 if ( ! mdoc_word_alloc(mdoc
, line
, lastarg
, p
))
1056 mdoc
->next
= MDOC_NEXT_SIBLING
;
1060 if ( ! rewind_subblock(MDOC_HEAD
, mdoc
, tok
, line
, ppos
))
1062 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1064 mdoc
->next
= MDOC_NEXT_CHILD
;
1069 return(append_delims(mdoc
, line
, pos
, buf
));
1074 * A delimited constant is very similar to the macros parsed by
1075 * macro_text except that, in the event of punctuation, the macro isn't
1076 * "re-opened" as it is in macro_text. Also, these macros have a fixed
1077 * number of parameters.
1087 macro_constant_delimited(MACRO_PROT_ARGS
)
1089 int lastarg
, flushed
, j
, c
, maxargs
, argc
,
1091 struct mdoc_arg argv
[MDOC_LINEARG_MAX
];
1121 for (argc
= 0; argc
< MDOC_LINEARG_MAX
; argc
++) {
1123 c
= mdoc_argv(mdoc
, line
, tok
, &argv
[argc
], pos
, buf
);
1126 if (ARGV_WORD
== c
) {
1129 } else if (ARGV_ARG
== c
)
1131 mdoc_argv_free(argc
, argv
);
1135 if (MDOC_LINEARG_MAX
== argc
) {
1136 mdoc_argv_free(argc
- 1, argv
);
1137 return(perr(mdoc
, line
, ppos
, EARGVLIM
));
1140 c
= mdoc_elem_alloc(mdoc
, line
, ppos
, tok
, argc
, argv
);
1141 mdoc_argv_free(argc
, argv
);
1146 mdoc
->next
= MDOC_NEXT_CHILD
;
1148 for (j
= 0; /* No sentinel. */; j
++) {
1151 if (j
== maxargs
&& ! flushed
) {
1152 if ( ! rewind_elem(mdoc
, tok
))
1157 c
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
1158 assert(ARGS_PHRASE
!= c
);
1160 if (ARGS_ERROR
== c
)
1162 if (ARGS_PUNCT
== c
)
1167 if (-1 == (c
= lookup(mdoc
, line
, lastarg
, tok
, p
)))
1169 else if (MDOC_MAX
!= c
) {
1170 if ( ! flushed
&& ! rewind_elem(mdoc
, tok
))
1173 if ( ! mdoc_macro(mdoc
, c
, line
, lastarg
, pos
, buf
))
1178 if ( ! flushed
&& mdoc_isdelim(p
) && ! igndelim
) {
1179 if ( ! rewind_elem(mdoc
, tok
))
1184 if ( ! mdoc_word_alloc(mdoc
, line
, lastarg
, p
))
1186 mdoc
->next
= MDOC_NEXT_SIBLING
;
1189 if ( ! flushed
&& ! rewind_elem(mdoc
, tok
))
1194 return(append_delims(mdoc
, line
, pos
, buf
));
1199 * A constant macro is the simplest classification. It spans an entire
1203 macro_constant(MACRO_PROT_ARGS
)
1206 struct mdoc_arg argv
[MDOC_LINEARG_MAX
];
1209 assert( ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
));
1211 for (argc
= 0; argc
< MDOC_LINEARG_MAX
; argc
++) {
1213 c
= mdoc_argv(mdoc
, line
, tok
, &argv
[argc
], pos
, buf
);
1216 if (ARGV_WORD
== c
) {
1219 } else if (ARGV_ARG
== c
)
1222 mdoc_argv_free(argc
, argv
);
1226 if (MDOC_LINEARG_MAX
== argc
) {
1227 mdoc_argv_free(argc
- 1, argv
);
1228 return(perr(mdoc
, line
, ppos
, EARGVLIM
));
1231 c
= mdoc_elem_alloc(mdoc
, line
, ppos
, tok
, argc
, argv
);
1232 mdoc_argv_free(argc
, argv
);
1237 mdoc
->next
= MDOC_NEXT_CHILD
;
1241 w
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
1242 assert(ARGS_PHRASE
!= c
);
1244 if (ARGS_ERROR
== w
)
1249 c
= ARGS_QWORD
== w
? MDOC_MAX
:
1250 lookup(mdoc
, line
, la
, tok
, p
);
1252 if (MDOC_MAX
!= c
&& -1 != c
) {
1253 if ( ! rewind_elem(mdoc
, tok
))
1255 return(mdoc_macro(mdoc
, c
, line
, la
, pos
, buf
));
1259 if ( ! mdoc_word_alloc(mdoc
, line
, la
, p
))
1261 mdoc
->next
= MDOC_NEXT_SIBLING
;
1264 return(rewind_elem(mdoc
, tok
));
1270 macro_obsolete(MACRO_PROT_ARGS
)
1273 return(pwarn(mdoc
, line
, ppos
, WOBS
));
1278 * This is called at the end of parsing. It must traverse up the tree,
1279 * closing out open [implicit] scopes. Obviously, open explicit scopes
1283 macro_end(struct mdoc
*mdoc
)
1285 struct mdoc_node
*n
;
1287 assert(mdoc
->first
);
1290 /* Scan for open explicit scopes. */
1292 n
= MDOC_VALID
& mdoc
->last
->flags
?
1293 mdoc
->last
->parent
: mdoc
->last
;
1295 for ( ; n
; n
= n
->parent
) {
1296 if (MDOC_BLOCK
!= n
->type
)
1298 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[n
->tok
].flags
))
1300 return(mdoc_nerr(mdoc
, n
, "macro scope still open on exit"));
1303 return(rewind_last(mdoc
, mdoc
->first
));