[BACK]Return to mdoc_macro.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/mdoc_macro.c, Revision 1.162

1.162   ! schwarze    1: /*     $Id: mdoc_macro.c,v 1.161 2014/12/22 23:27:32 schwarze Exp $ */
1.1       kristaps    2: /*
1.116     schwarze    3:  * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.162   ! schwarze    4:  * Copyright (c) 2010, 2012-2015 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.8       kristaps    7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    9:  *
1.8       kristaps   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   17:  */
1.40      kristaps   18: #include "config.h"
1.140     schwarze   19:
                     20: #include <sys/types.h>
1.40      kristaps   21:
1.1       kristaps   22: #include <assert.h>
                     23: #include <ctype.h>
                     24: #include <stdlib.h>
                     25: #include <stdio.h>
                     26: #include <string.h>
1.38      kristaps   27: #include <time.h>
1.1       kristaps   28:
1.106     kristaps   29: #include "mdoc.h"
1.68      kristaps   30: #include "mandoc.h"
1.1       kristaps   31: #include "libmdoc.h"
1.64      kristaps   32: #include "libmandoc.h"
1.1       kristaps   33:
1.84      schwarze   34: enum   rew {   /* see rew_dohalt() */
                     35:        REWIND_NONE,
                     36:        REWIND_THIS,
                     37:        REWIND_MORE,
1.98      kristaps   38:        REWIND_FORCE,
1.84      schwarze   39:        REWIND_LATER,
1.91      kristaps   40:        REWIND_ERROR
1.51      kristaps   41: };
1.1       kristaps   42:
1.151     schwarze   43: static void            blk_full(MACRO_PROT_ARGS);
                     44: static void            blk_exp_close(MACRO_PROT_ARGS);
                     45: static void            blk_part_exp(MACRO_PROT_ARGS);
                     46: static void            blk_part_imp(MACRO_PROT_ARGS);
                     47: static void            ctx_synopsis(MACRO_PROT_ARGS);
                     48: static void            in_line_eoln(MACRO_PROT_ARGS);
                     49: static void            in_line_argn(MACRO_PROT_ARGS);
                     50: static void            in_line(MACRO_PROT_ARGS);
                     51: static void            phrase_ta(MACRO_PROT_ARGS);
1.53      kristaps   52:
1.150     schwarze   53: static void            dword(struct mdoc *, int, int, const char *,
1.123     schwarze   54:                                 enum mdelim, int);
1.150     schwarze   55: static void            append_delims(struct mdoc *, int, int *, char *);
1.155     schwarze   56: static enum mdoct      lookup(struct mdoc *, enum mdoct,
                     57:                                int, int, const char *);
1.153     schwarze   58: static int             macro_or_word(MACRO_PROT_ARGS, int);
1.85      schwarze   59: static int             make_pending(struct mdoc_node *, enum mdoct,
1.83      schwarze   60:                                struct mdoc *, int, int);
1.154     schwarze   61: static int             parse_rest(struct mdoc *, enum mdoct,
                     62:                                int, int *, char *);
1.131     schwarze   63: static enum mdoct      rew_alt(enum mdoct);
                     64: static enum rew        rew_dohalt(enum mdoct, enum mdoc_type,
1.53      kristaps   65:                                const struct mdoc_node *);
1.149     schwarze   66: static void            rew_elem(struct mdoc *, enum mdoct);
                     67: static void            rew_last(struct mdoc *, const struct mdoc_node *);
1.150     schwarze   68: static void            rew_sub(enum mdoc_type, struct mdoc *,
1.53      kristaps   69:                                enum mdoct, int, int);
1.1       kristaps   70:
                     71: const  struct mdoc_macro __mdoc_macros[MDOC_MAX] = {
1.123     schwarze   72:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ap */
1.1       kristaps   73:        { in_line_eoln, MDOC_PROLOGUE }, /* Dd */
                     74:        { in_line_eoln, MDOC_PROLOGUE }, /* Dt */
                     75:        { in_line_eoln, MDOC_PROLOGUE }, /* Os */
1.123     schwarze   76:        { blk_full, MDOC_PARSED | MDOC_JOIN }, /* Sh */
                     77:        { blk_full, MDOC_PARSED | MDOC_JOIN }, /* Ss */
                     78:        { in_line_eoln, 0 }, /* Pp */
                     79:        { blk_part_imp, MDOC_PARSED | MDOC_JOIN }, /* D1 */
                     80:        { blk_part_imp, MDOC_PARSED | MDOC_JOIN }, /* Dl */
1.1       kristaps   81:        { blk_full, MDOC_EXPLICIT }, /* Bd */
1.123     schwarze   82:        { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Ed */
1.1       kristaps   83:        { blk_full, MDOC_EXPLICIT }, /* Bl */
1.123     schwarze   84:        { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* El */
                     85:        { blk_full, MDOC_PARSED | MDOC_JOIN }, /* It */
                     86:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ad */
                     87:        { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* An */
1.1       kristaps   88:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ar */
1.129     schwarze   89:        { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Cd */
1.1       kristaps   90:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cm */
1.123     schwarze   91:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Dv */
                     92:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Er */
                     93:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ev */
1.1       kristaps   94:        { in_line_eoln, 0 }, /* Ex */
1.123     schwarze   95:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fa */
                     96:        { in_line_eoln, 0 }, /* Fd */
1.1       kristaps   97:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fl */
1.123     schwarze   98:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fn */
                     99:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ft */
1.128     schwarze  100:        { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ic */
1.18      kristaps  101:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* In */
1.123     schwarze  102:        { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Li */
                    103:        { blk_full, MDOC_JOIN }, /* Nd */
                    104:        { ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Nm */
1.1       kristaps  105:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Op */
1.132     schwarze  106:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ot */
1.1       kristaps  107:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Pa */
                    108:        { in_line_eoln, 0 }, /* Rv */
1.123     schwarze  109:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* St */
1.1       kristaps  110:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Va */
1.123     schwarze  111:        { ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Vt */
1.42      kristaps  112:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Xr */
1.123     schwarze  113:        { in_line_eoln, MDOC_JOIN }, /* %A */
                    114:        { in_line_eoln, MDOC_JOIN }, /* %B */
                    115:        { in_line_eoln, MDOC_JOIN }, /* %D */
                    116:        { in_line_eoln, MDOC_JOIN }, /* %I */
                    117:        { in_line_eoln, MDOC_JOIN }, /* %J */
1.1       kristaps  118:        { in_line_eoln, 0 }, /* %N */
1.123     schwarze  119:        { in_line_eoln, MDOC_JOIN }, /* %O */
1.1       kristaps  120:        { in_line_eoln, 0 }, /* %P */
1.123     schwarze  121:        { in_line_eoln, MDOC_JOIN }, /* %R */
                    122:        { in_line_eoln, MDOC_JOIN }, /* %T */
1.1       kristaps  123:        { in_line_eoln, 0 }, /* %V */
1.123     schwarze  124:        { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
                    125:                         MDOC_EXPLICIT | MDOC_JOIN }, /* Ac */
                    126:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
                    127:                        MDOC_EXPLICIT | MDOC_JOIN }, /* Ao */
                    128:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Aq */
1.1       kristaps  129:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* At */
1.123     schwarze  130:        { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
                    131:                         MDOC_EXPLICIT | MDOC_JOIN }, /* Bc */
                    132:        { blk_full, MDOC_EXPLICIT }, /* Bf */
                    133:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
                    134:                        MDOC_EXPLICIT | MDOC_JOIN }, /* Bo */
                    135:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Bq */
1.1       kristaps  136:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bsx */
                    137:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bx */
                    138:        { in_line_eoln, 0 }, /* Db */
1.123     schwarze  139:        { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
                    140:                         MDOC_EXPLICIT | MDOC_JOIN }, /* Dc */
                    141:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
                    142:                        MDOC_EXPLICIT | MDOC_JOIN }, /* Do */
                    143:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Dq */
                    144:        { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Ec */
                    145:        { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Ef */
                    146:        { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Em */
1.1       kristaps  147:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Eo */
                    148:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Fx */
1.11      kristaps  149:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ms */
1.144     schwarze  150:        { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* No */
1.123     schwarze  151:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED |
                    152:                        MDOC_IGNDELIM | MDOC_JOIN }, /* Ns */
1.1       kristaps  153:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Nx */
                    154:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ox */
1.123     schwarze  155:        { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
                    156:                         MDOC_EXPLICIT | MDOC_JOIN }, /* Pc */
1.52      kristaps  157:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_IGNDELIM }, /* Pf */
1.123     schwarze  158:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
                    159:                        MDOC_EXPLICIT | MDOC_JOIN }, /* Po */
                    160:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Pq */
                    161:        { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
                    162:                         MDOC_EXPLICIT | MDOC_JOIN }, /* Qc */
                    163:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ql */
                    164:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
                    165:                        MDOC_EXPLICIT | MDOC_JOIN }, /* Qo */
                    166:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Qq */
                    167:        { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Re */
1.1       kristaps  168:        { blk_full, MDOC_EXPLICIT }, /* Rs */
1.123     schwarze  169:        { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
                    170:                         MDOC_EXPLICIT | MDOC_JOIN }, /* Sc */
                    171:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
                    172:                        MDOC_EXPLICIT | MDOC_JOIN }, /* So */
                    173:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Sq */
1.147     schwarze  174:        { in_line_argn, 0 }, /* Sm */
1.123     schwarze  175:        { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Sx */
                    176:        { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Sy */
1.1       kristaps  177:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Tn */
1.123     schwarze  178:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ux */
1.1       kristaps  179:        { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Xc */
                    180:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Xo */
1.123     schwarze  181:        { blk_full, MDOC_EXPLICIT | MDOC_CALLABLE }, /* Fo */
                    182:        { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
                    183:                         MDOC_EXPLICIT | MDOC_JOIN }, /* Fc */
                    184:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
                    185:                        MDOC_EXPLICIT | MDOC_JOIN }, /* Oo */
                    186:        { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
                    187:                         MDOC_EXPLICIT | MDOC_JOIN }, /* Oc */
1.1       kristaps  188:        { blk_full, MDOC_EXPLICIT }, /* Bk */
1.123     schwarze  189:        { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Ek */
1.1       kristaps  190:        { in_line_eoln, 0 }, /* Bt */
                    191:        { in_line_eoln, 0 }, /* Hf */
1.132     schwarze  192:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fr */
1.1       kristaps  193:        { in_line_eoln, 0 }, /* Ud */
1.69      kristaps  194:        { in_line, 0 }, /* Lb */
1.123     schwarze  195:        { in_line_eoln, 0 }, /* Lp */
                    196:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Lk */
                    197:        { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Mt */
                    198:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Brq */
                    199:        { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
                    200:                        MDOC_EXPLICIT | MDOC_JOIN }, /* Bro */
                    201:        { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
                    202:                         MDOC_EXPLICIT | MDOC_JOIN }, /* Brc */
                    203:        { in_line_eoln, MDOC_JOIN }, /* %C */
1.132     schwarze  204:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Es */
                    205:        { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* En */
1.1       kristaps  206:        { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Dx */
1.123     schwarze  207:        { in_line_eoln, MDOC_JOIN }, /* %Q */
1.20      kristaps  208:        { in_line_eoln, 0 }, /* br */
                    209:        { in_line_eoln, 0 }, /* sp */
1.37      kristaps  210:        { in_line_eoln, 0 }, /* %U */
1.123     schwarze  211:        { phrase_ta, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ta */
1.143     schwarze  212:        { in_line_eoln, MDOC_PROLOGUE }, /* ll */
1.1       kristaps  213: };
                    214:
                    215: const  struct mdoc_macro * const mdoc_macros = __mdoc_macros;
                    216:
                    217:
                    218: /*
                    219:  * This is called at the end of parsing.  It must traverse up the tree,
                    220:  * closing out open [implicit] scopes.  Obviously, open explicit scopes
                    221:  * are errors.
                    222:  */
1.151     schwarze  223: void
1.119     schwarze  224: mdoc_macroend(struct mdoc *mdoc)
1.1       kristaps  225: {
                    226:        struct mdoc_node *n;
                    227:
                    228:        /* Scan for open explicit scopes. */
                    229:
1.151     schwarze  230:        n = mdoc->last->flags & MDOC_VALID ?
1.131     schwarze  231:            mdoc->last->parent : mdoc->last;
1.1       kristaps  232:
1.94      schwarze  233:        for ( ; n; n = n->parent)
1.151     schwarze  234:                if (n->type == MDOC_BLOCK &&
                    235:                    mdoc_macros[n->tok].flags & MDOC_EXPLICIT)
1.137     schwarze  236:                        mandoc_msg(MANDOCERR_BLK_NOEND, mdoc->parse,
                    237:                            n->line, n->pos, mdoc_macronames[n->tok]);
1.1       kristaps  238:
1.32      kristaps  239:        /* Rewind to the first. */
                    240:
1.149     schwarze  241:        rew_last(mdoc, mdoc->first);
1.1       kristaps  242: }
                    243:
1.32      kristaps  244: /*
1.153     schwarze  245:  * Look up the macro at *p called by "from",
                    246:  * or as a line macro if from == MDOC_MAX.
1.32      kristaps  247:  */
1.53      kristaps  248: static enum mdoct
1.155     schwarze  249: lookup(struct mdoc *mdoc, enum mdoct from, int line, int ppos, const char *p)
1.28      kristaps  250: {
1.58      kristaps  251:        enum mdoct       res;
1.1       kristaps  252:
1.153     schwarze  253:        if (from == MDOC_MAX || mdoc_macros[from].flags & MDOC_PARSED) {
                    254:                res = mdoc_hash_find(p);
1.155     schwarze  255:                if (res != MDOC_MAX) {
                    256:                        if (mdoc_macros[res].flags & MDOC_CALLABLE)
                    257:                                return(res);
                    258:                        if (res != MDOC_br && res != MDOC_sp && res != MDOC_ll)
                    259:                                mandoc_msg(MANDOCERR_MACRO_CALL,
                    260:                                    mdoc->parse, line, ppos, p);
                    261:                }
1.153     schwarze  262:        }
1.1       kristaps  263:        return(MDOC_MAX);
                    264: }
                    265:
1.149     schwarze  266: static void
1.32      kristaps  267: rew_last(struct mdoc *mdoc, const struct mdoc_node *to)
1.1       kristaps  268: {
1.100     kristaps  269:        struct mdoc_node *n, *np;
1.1       kristaps  270:
                    271:        assert(to);
                    272:        mdoc->next = MDOC_NEXT_SIBLING;
                    273:        while (mdoc->last != to) {
1.100     kristaps  274:                /*
                    275:                 * Save the parent here, because we may delete the
1.119     schwarze  276:                 * mdoc->last node in the post-validation phase and reset
                    277:                 * it to mdoc->last->parent, causing a step in the closing
1.100     kristaps  278:                 * out to be lost.
                    279:                 */
                    280:                np = mdoc->last->parent;
1.149     schwarze  281:                mdoc_valid_post(mdoc);
1.99      kristaps  282:                n = mdoc->last;
1.100     kristaps  283:                mdoc->last = np;
1.1       kristaps  284:                assert(mdoc->last);
1.99      kristaps  285:                mdoc->last->last = n;
1.1       kristaps  286:        }
1.149     schwarze  287:        mdoc_valid_post(mdoc);
1.1       kristaps  288: }
                    289:
1.32      kristaps  290: /*
1.84      schwarze  291:  * For a block closing macro, return the corresponding opening one.
                    292:  * Otherwise, return the macro itself.
1.32      kristaps  293:  */
1.47      kristaps  294: static enum mdoct
                    295: rew_alt(enum mdoct tok)
1.1       kristaps  296: {
                    297:        switch (tok) {
1.131     schwarze  298:        case MDOC_Ac:
1.1       kristaps  299:                return(MDOC_Ao);
1.131     schwarze  300:        case MDOC_Bc:
1.1       kristaps  301:                return(MDOC_Bo);
1.131     schwarze  302:        case MDOC_Brc:
1.1       kristaps  303:                return(MDOC_Bro);
1.131     schwarze  304:        case MDOC_Dc:
1.1       kristaps  305:                return(MDOC_Do);
1.131     schwarze  306:        case MDOC_Ec:
1.1       kristaps  307:                return(MDOC_Eo);
1.131     schwarze  308:        case MDOC_Ed:
1.1       kristaps  309:                return(MDOC_Bd);
1.131     schwarze  310:        case MDOC_Ef:
1.1       kristaps  311:                return(MDOC_Bf);
1.131     schwarze  312:        case MDOC_Ek:
1.1       kristaps  313:                return(MDOC_Bk);
1.131     schwarze  314:        case MDOC_El:
1.1       kristaps  315:                return(MDOC_Bl);
1.131     schwarze  316:        case MDOC_Fc:
1.1       kristaps  317:                return(MDOC_Fo);
1.131     schwarze  318:        case MDOC_Oc:
1.1       kristaps  319:                return(MDOC_Oo);
1.131     schwarze  320:        case MDOC_Pc:
1.1       kristaps  321:                return(MDOC_Po);
1.131     schwarze  322:        case MDOC_Qc:
1.1       kristaps  323:                return(MDOC_Qo);
1.131     schwarze  324:        case MDOC_Re:
1.1       kristaps  325:                return(MDOC_Rs);
1.131     schwarze  326:        case MDOC_Sc:
1.1       kristaps  327:                return(MDOC_So);
1.131     schwarze  328:        case MDOC_Xc:
1.1       kristaps  329:                return(MDOC_Xo);
                    330:        default:
1.84      schwarze  331:                return(tok);
1.1       kristaps  332:        }
                    333:        /* NOTREACHED */
                    334: }
                    335:
1.84      schwarze  336: /*
                    337:  * Rewinding to tok, how do we have to handle *p?
                    338:  * REWIND_NONE: *p would delimit tok, but no tok scope is open
                    339:  *   inside *p, so there is no need to rewind anything at all.
                    340:  * REWIND_THIS: *p matches tok, so rewind *p and nothing else.
                    341:  * REWIND_MORE: *p is implicit, rewind it and keep searching for tok.
1.98      kristaps  342:  * REWIND_FORCE: *p is explicit, but tok is full, force rewinding *p.
1.84      schwarze  343:  * REWIND_LATER: *p is explicit and still open, postpone rewinding.
                    344:  * REWIND_ERROR: No tok block is open at all.
1.1       kristaps  345:  */
1.51      kristaps  346: static enum rew
1.131     schwarze  347: rew_dohalt(enum mdoct tok, enum mdoc_type type,
1.47      kristaps  348:                const struct mdoc_node *p)
1.1       kristaps  349: {
                    350:
1.86      schwarze  351:        /*
                    352:         * No matching token, no delimiting block, no broken block.
                    353:         * This can happen when full implicit macros are called for
                    354:         * the first time but try to rewind their previous
                    355:         * instance anyway.
                    356:         */
1.1       kristaps  357:        if (MDOC_ROOT == p->type)
1.84      schwarze  358:                return(MDOC_BLOCK == type &&
                    359:                    MDOC_EXPLICIT & mdoc_macros[tok].flags ?
                    360:                    REWIND_ERROR : REWIND_NONE);
1.86      schwarze  361:
                    362:        /*
1.131     schwarze  363:         * When starting to rewind, skip plain text
1.86      schwarze  364:         * and nodes that have already been rewound.
                    365:         */
1.161     schwarze  366:        if (p->type == MDOC_TEXT || p->flags & (MDOC_VALID | MDOC_BREAK))
1.84      schwarze  367:                return(REWIND_MORE);
                    368:
1.86      schwarze  369:        /*
                    370:         * The easiest case:  Found a matching token.
                    371:         * This applies to both blocks and elements.
                    372:         */
1.84      schwarze  373:        tok = rew_alt(tok);
                    374:        if (tok == p->tok)
                    375:                return(p->end ? REWIND_NONE :
                    376:                    type == p->type ? REWIND_THIS : REWIND_MORE);
                    377:
1.86      schwarze  378:        /*
                    379:         * While elements do require rewinding for themselves,
                    380:         * they never affect rewinding of other nodes.
                    381:         */
1.84      schwarze  382:        if (MDOC_ELEM == p->type)
                    383:                return(REWIND_MORE);
1.1       kristaps  384:
1.86      schwarze  385:        /*
                    386:         * Blocks delimited by our target token get REWIND_MORE.
1.131     schwarze  387:         * Blocks delimiting our target token get REWIND_NONE.
1.86      schwarze  388:         */
1.1       kristaps  389:        switch (tok) {
1.131     schwarze  390:        case MDOC_Bl:
1.84      schwarze  391:                if (MDOC_It == p->tok)
                    392:                        return(REWIND_MORE);
1.1       kristaps  393:                break;
1.131     schwarze  394:        case MDOC_It:
1.1       kristaps  395:                if (MDOC_BODY == p->type && MDOC_Bl == p->tok)
1.84      schwarze  396:                        return(REWIND_NONE);
1.1       kristaps  397:                break;
1.84      schwarze  398:        /*
                    399:         * XXX Badly nested block handling still fails badly
                    400:         * when one block is breaking two blocks of the same type.
                    401:         * This is an incomplete and extremely ugly workaround,
                    402:         * required to let the OpenBSD tree build.
                    403:         */
1.131     schwarze  404:        case MDOC_Oo:
1.84      schwarze  405:                if (MDOC_Op == p->tok)
                    406:                        return(REWIND_MORE);
1.1       kristaps  407:                break;
1.131     schwarze  408:        case MDOC_Nm:
1.88      schwarze  409:                return(REWIND_NONE);
1.131     schwarze  410:        case MDOC_Nd:
1.19      kristaps  411:                /* FALLTHROUGH */
1.131     schwarze  412:        case MDOC_Ss:
1.1       kristaps  413:                if (MDOC_BODY == p->type && MDOC_Sh == p->tok)
1.84      schwarze  414:                        return(REWIND_NONE);
1.1       kristaps  415:                /* FALLTHROUGH */
1.131     schwarze  416:        case MDOC_Sh:
1.138     schwarze  417:                if (MDOC_ROOT == p->parent->type)
                    418:                        return(REWIND_THIS);
1.84      schwarze  419:                if (MDOC_Nd == p->tok || MDOC_Ss == p->tok ||
                    420:                    MDOC_Sh == p->tok)
                    421:                        return(REWIND_MORE);
1.1       kristaps  422:                break;
                    423:        default:
                    424:                break;
                    425:        }
                    426:
1.86      schwarze  427:        /*
                    428:         * Default block rewinding rules.
1.88      schwarze  429:         * In particular, always skip block end markers,
                    430:         * and let all blocks rewind Nm children.
1.146     schwarze  431:         * Do not warn again when closing a block,
                    432:         * since closing the body already warned.
1.86      schwarze  433:         */
1.88      schwarze  434:        if (ENDBODY_NOT != p->end || MDOC_Nm == p->tok ||
1.146     schwarze  435:            MDOC_BLOCK == type || (MDOC_BLOCK == p->type &&
1.86      schwarze  436:            ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)))
                    437:                return(REWIND_MORE);
                    438:
                    439:        /*
1.98      kristaps  440:         * By default, closing out full blocks
                    441:         * forces closing of broken explicit blocks,
                    442:         * while closing out partial blocks
                    443:         * allows delayed rewinding by default.
1.86      schwarze  444:         */
1.98      kristaps  445:        return (&blk_full == mdoc_macros[tok].fp ?
                    446:            REWIND_FORCE : REWIND_LATER);
1.1       kristaps  447: }
                    448:
1.149     schwarze  449: static void
1.47      kristaps  450: rew_elem(struct mdoc *mdoc, enum mdoct tok)
1.1       kristaps  451: {
                    452:        struct mdoc_node *n;
                    453:
                    454:        n = mdoc->last;
                    455:        if (MDOC_ELEM != n->type)
                    456:                n = n->parent;
                    457:        assert(MDOC_ELEM == n->type);
                    458:        assert(tok == n->tok);
1.149     schwarze  459:        rew_last(mdoc, n);
1.1       kristaps  460: }
                    461:
1.83      schwarze  462: /*
                    463:  * We are trying to close a block identified by tok,
                    464:  * but the child block *broken is still open.
                    465:  * Thus, postpone closing the tok block
                    466:  * until the rew_sub call closing *broken.
                    467:  */
                    468: static int
                    469: make_pending(struct mdoc_node *broken, enum mdoct tok,
1.119     schwarze  470:                struct mdoc *mdoc, int line, int ppos)
1.83      schwarze  471: {
                    472:        struct mdoc_node *breaker;
                    473:
                    474:        /*
                    475:         * Iterate backwards, searching for the block matching tok,
                    476:         * that is, the block breaking the *broken block.
                    477:         */
                    478:        for (breaker = broken->parent; breaker; breaker = breaker->parent) {
                    479:
                    480:                /*
1.158     schwarze  481:                 * If the *broken block (Z) is already broken and we
                    482:                 * encounter its breaker (B), make the tok block (A)
                    483:                 * pending on that inner breaker (B).
                    484:                 * Graphically, [A breaker=[B! broken=[Z->B B] tok=A] Z]
                    485:                 * becomes breaker=[A broken=[B! [Z->B B] tok=A] Z]
                    486:                 * and finally [A! [B!->A [Z->B B] A] Z].
                    487:                 * In these graphics, "->" indicates the "pending"
                    488:                 * pointer and "!" indicates the MDOC_BREAK flag.
                    489:                 * Each of the cases gets one additional pointer (B->A)
                    490:                 * and one additional flag (A!).
1.83      schwarze  491:                 */
                    492:                if (breaker == broken->pending) {
                    493:                        broken = breaker;
                    494:                        continue;
                    495:                }
                    496:
1.84      schwarze  497:                if (REWIND_THIS != rew_dohalt(tok, MDOC_BLOCK, breaker))
1.83      schwarze  498:                        continue;
                    499:                if (MDOC_BODY == broken->type)
                    500:                        broken = broken->parent;
                    501:
                    502:                /*
                    503:                 * Found the breaker.
1.158     schwarze  504:                 * If another, outer breaker (X) is already pending on
                    505:                 * the *broken block (B), we must not clobber the link
1.83      schwarze  506:                 * to the outer breaker, but make it pending on the
1.158     schwarze  507:                 * new, now inner breaker (A).
                    508:                 * Graphically, [X! breaker=[A broken=[B->X X] tok=A] B]
                    509:                 * becomes [X! breaker=[A->X broken=[B X] tok=A] B]
                    510:                 * and finally [X! [A!->X [B->A X] A] B].
1.83      schwarze  511:                 */
                    512:                if (broken->pending) {
                    513:                        struct mdoc_node *taker;
                    514:
                    515:                        /*
1.158     schwarze  516:                         * If the inner breaker (A) is already broken,
                    517:                         * too, it cannot take on the outer breaker (X)
                    518:                         * but must hand it on to its own breakers (Y):
                    519:                         * [X! [Y! breaker=[A->Y Y] broken=[B->X X] tok=A] B]
                    520:                         * [X! take=[Y!->X brea=[A->Y Y] brok=[B X] tok=A] B]
                    521:                         * and finally [X! [Y!->X [A!->Y Y] [B->A X] A] B].
1.83      schwarze  522:                         */
                    523:                        taker = breaker;
                    524:                        while (taker->pending)
                    525:                                taker = taker->pending;
                    526:                        taker->pending = broken->pending;
                    527:                }
1.158     schwarze  528:
                    529:                /*
                    530:                 * Now we have reduced the situation to the simplest
                    531:                 * case, which is just breaker=[A broken=[B tok=A] B]
                    532:                 * and becomes [A! [B->A A] B].
                    533:                 */
1.83      schwarze  534:                broken->pending = breaker;
1.158     schwarze  535:                breaker->flags |= MDOC_BREAK;
1.137     schwarze  536:                mandoc_vmsg(MANDOCERR_BLK_NEST, mdoc->parse, line, ppos,
1.131     schwarze  537:                    "%s breaks %s", mdoc_macronames[tok],
                    538:                    mdoc_macronames[broken->tok]);
1.83      schwarze  539:                return(1);
                    540:        }
                    541:
                    542:        /*
                    543:         * Found no matching block for tok.
                    544:         * Are you trying to close a block that is not open?
                    545:         */
                    546:        return(0);
                    547: }
                    548:
1.150     schwarze  549: static void
1.131     schwarze  550: rew_sub(enum mdoc_type t, struct mdoc *mdoc,
1.47      kristaps  551:                enum mdoct tok, int line, int ppos)
1.1       kristaps  552: {
1.160     schwarze  553:        struct mdoc_node *n, *to;
1.1       kristaps  554:
1.160     schwarze  555:        to = NULL;
1.119     schwarze  556:        n = mdoc->last;
1.84      schwarze  557:        while (n) {
                    558:                switch (rew_dohalt(tok, t, n)) {
1.131     schwarze  559:                case REWIND_NONE:
1.160     schwarze  560:                        if (to == NULL)
                    561:                                return;
                    562:                        n = to;
                    563:                        break;
1.131     schwarze  564:                case REWIND_THIS:
1.124     schwarze  565:                        n->lastline = line -
1.150     schwarze  566:                            (mdoc->flags & MDOC_NEWLINE &&
                    567:                             ! (mdoc_macros[tok].flags & MDOC_EXPLICIT));
1.84      schwarze  568:                        break;
1.131     schwarze  569:                case REWIND_FORCE:
1.137     schwarze  570:                        mandoc_vmsg(MANDOCERR_BLK_BROKEN, mdoc->parse,
1.131     schwarze  571:                            line, ppos, "%s breaks %s",
                    572:                            mdoc_macronames[tok],
                    573:                            mdoc_macronames[n->tok]);
1.98      kristaps  574:                        /* FALLTHROUGH */
1.131     schwarze  575:                case REWIND_MORE:
1.124     schwarze  576:                        n->lastline = line -
1.150     schwarze  577:                            (mdoc->flags & MDOC_NEWLINE ? 1 : 0);
1.160     schwarze  578:                        to = n;
1.84      schwarze  579:                        n = n->parent;
                    580:                        continue;
1.131     schwarze  581:                case REWIND_LATER:
1.119     schwarze  582:                        if (make_pending(n, tok, mdoc, line, ppos) ||
1.150     schwarze  583:                            t != MDOC_BLOCK)
                    584:                                return;
1.98      kristaps  585:                        /* FALLTHROUGH */
1.131     schwarze  586:                case REWIND_ERROR:
1.137     schwarze  587:                        mandoc_msg(MANDOCERR_BLK_NOTOPEN,
                    588:                            mdoc->parse, line, ppos,
                    589:                            mdoc_macronames[tok]);
1.150     schwarze  590:                        return;
1.32      kristaps  591:                }
1.84      schwarze  592:                break;
1.1       kristaps  593:        }
                    594:        assert(n);
1.149     schwarze  595:        rew_last(mdoc, n);
1.47      kristaps  596:
                    597:        /*
1.83      schwarze  598:         * The current block extends an enclosing block.
                    599:         * Now that the current block ends, close the enclosing block, too.
1.47      kristaps  600:         */
1.150     schwarze  601:        while ((n = n->pending) != NULL) {
1.149     schwarze  602:                rew_last(mdoc, n);
1.150     schwarze  603:                if (n->type == MDOC_HEAD)
                    604:                        mdoc_body_alloc(mdoc, n->line, n->pos, n->tok);
1.47      kristaps  605:        }
1.1       kristaps  606: }
                    607:
1.105     kristaps  608: /*
                    609:  * Allocate a word and check whether it's punctuation or not.
                    610:  * Punctuation consists of those tokens found in mdoc_isdelim().
                    611:  */
1.150     schwarze  612: static void
1.123     schwarze  613: dword(struct mdoc *mdoc, int line, int col, const char *p,
                    614:                enum mdelim d, int may_append)
1.105     kristaps  615: {
1.131     schwarze  616:
1.150     schwarze  617:        if (d == DELIM_MAX)
1.105     kristaps  618:                d = mdoc_isdelim(p);
                    619:
1.123     schwarze  620:        if (may_append &&
1.150     schwarze  621:            ! (mdoc->flags & (MDOC_SYNOPSIS | MDOC_KEEP | MDOC_SMOFF)) &&
                    622:            d == DELIM_NONE && mdoc->last->type == MDOC_TEXT &&
                    623:            mdoc_isdelim(mdoc->last->string) == DELIM_NONE) {
1.123     schwarze  624:                mdoc_word_append(mdoc, p);
1.150     schwarze  625:                return;
1.123     schwarze  626:        }
                    627:
1.150     schwarze  628:        mdoc_word_alloc(mdoc, line, col, p);
1.105     kristaps  629:
1.108     schwarze  630:        /*
1.144     schwarze  631:         * If the word consists of a bare delimiter,
                    632:         * flag the new node accordingly,
                    633:         * unless doing so was vetoed by the invoking macro.
                    634:         * Always clear the veto, it is only valid for one word.
1.108     schwarze  635:         */
                    636:
1.144     schwarze  637:        if (d == DELIM_OPEN)
                    638:                mdoc->last->flags |= MDOC_DELIMO;
                    639:        else if (d == DELIM_CLOSE &&
                    640:            ! (mdoc->flags & MDOC_NODELIMC) &&
1.131     schwarze  641:            mdoc->last->parent->tok != MDOC_Fd)
1.119     schwarze  642:                mdoc->last->flags |= MDOC_DELIMC;
1.144     schwarze  643:        mdoc->flags &= ~MDOC_NODELIMC;
1.105     kristaps  644: }
1.1       kristaps  645:
1.150     schwarze  646: static void
1.119     schwarze  647: append_delims(struct mdoc *mdoc, int line, int *pos, char *buf)
1.1       kristaps  648: {
1.153     schwarze  649:        char            *p;
1.66      kristaps  650:        int              la;
1.1       kristaps  651:
1.150     schwarze  652:        if (buf[*pos] == '\0')
                    653:                return;
1.1       kristaps  654:
                    655:        for (;;) {
1.66      kristaps  656:                la = *pos;
1.153     schwarze  657:                if (mdoc_args(mdoc, line, pos, buf, MDOC_MAX, &p) == ARGS_EOLN)
1.1       kristaps  658:                        break;
1.123     schwarze  659:                dword(mdoc, line, la, p, DELIM_MAX, 1);
1.66      kristaps  660:
                    661:                /*
                    662:                 * If we encounter end-of-sentence symbols, then trigger
                    663:                 * the double-space.
                    664:                 *
1.109     kristaps  665:                 * XXX: it's easy to allow this to propagate outward to
1.66      kristaps  666:                 * the last symbol, such that `. )' will cause the
                    667:                 * correct double-spacing.  However, (1) groff isn't
                    668:                 * smart enough to do this and (2) it would require
                    669:                 * knowing which symbols break this behaviour, for
1.109     kristaps  670:                 * example, `.  ;' shouldn't propagate the double-space.
1.66      kristaps  671:                 */
1.150     schwarze  672:
1.127     schwarze  673:                if (mandoc_eos(p, strlen(p)))
1.119     schwarze  674:                        mdoc->last->flags |= MDOC_EOS;
1.1       kristaps  675:        }
                    676: }
                    677:
                    678: /*
1.153     schwarze  679:  * Parse one word.
                    680:  * If it is a macro, call it and return 1.
                    681:  * Otherwise, allocate it and return 0.
                    682:  */
                    683: static int
                    684: macro_or_word(MACRO_PROT_ARGS, int parsed)
                    685: {
                    686:        char            *p;
                    687:        enum mdoct       ntok;
                    688:
                    689:        p = buf + ppos;
                    690:        ntok = MDOC_MAX;
1.157     schwarze  691:        if (*p == '"')
1.153     schwarze  692:                p++;
1.157     schwarze  693:        else if (parsed && ! (mdoc->flags & MDOC_PHRASELIT))
1.155     schwarze  694:                ntok = lookup(mdoc, tok, line, ppos, p);
1.153     schwarze  695:
                    696:        if (ntok == MDOC_MAX) {
                    697:                dword(mdoc, line, ppos, p, DELIM_MAX, tok == MDOC_MAX ||
                    698:                    mdoc_macros[tok].flags & MDOC_JOIN);
                    699:                return(0);
                    700:        } else {
                    701:                if (mdoc_macros[tok].fp == in_line_eoln)
                    702:                        rew_elem(mdoc, tok);
                    703:                mdoc_macro(mdoc, ntok, line, ppos, pos, buf);
                    704:                if (tok == MDOC_MAX)
                    705:                        append_delims(mdoc, line, pos, buf);
                    706:                return(1);
                    707:        }
                    708: }
                    709:
                    710: /*
1.131     schwarze  711:  * Close out block partial/full explicit.
1.1       kristaps  712:  */
1.151     schwarze  713: static void
1.1       kristaps  714: blk_exp_close(MACRO_PROT_ARGS)
                    715: {
1.83      schwarze  716:        struct mdoc_node *body;         /* Our own body. */
1.148     schwarze  717:        struct mdoc_node *endbody;      /* Our own end marker. */
1.83      schwarze  718:        struct mdoc_node *later;        /* A sub-block starting later. */
                    719:        struct mdoc_node *n;            /* For searching backwards. */
                    720:
1.160     schwarze  721:        int              flushed, have_it, j, lastarg, maxargs, nl;
1.55      kristaps  722:        enum margserr    ac;
1.83      schwarze  723:        enum mdoct       atok, ntok;
1.1       kristaps  724:        char            *p;
                    725:
1.119     schwarze  726:        nl = MDOC_NEWLINE & mdoc->flags;
1.60      kristaps  727:
1.1       kristaps  728:        switch (tok) {
1.131     schwarze  729:        case MDOC_Ec:
1.1       kristaps  730:                maxargs = 1;
                    731:                break;
1.131     schwarze  732:        case MDOC_Ek:
1.125     schwarze  733:                mdoc->flags &= ~MDOC_KEEP;
1.151     schwarze  734:                /* FALLTHROUGH */
1.1       kristaps  735:        default:
                    736:                maxargs = 0;
                    737:                break;
                    738:        }
                    739:
1.83      schwarze  740:        /*
                    741:         * Search backwards for beginnings of blocks,
                    742:         * both of our own and of pending sub-blocks.
                    743:         */
1.151     schwarze  744:
1.160     schwarze  745:        have_it = 0;
1.83      schwarze  746:        atok = rew_alt(tok);
1.148     schwarze  747:        body = endbody = later = NULL;
1.119     schwarze  748:        for (n = mdoc->last; n; n = n->parent) {
1.161     schwarze  749:                if (n->flags & (MDOC_VALID | MDOC_BREAK))
1.83      schwarze  750:                        continue;
                    751:
                    752:                /* Remember the start of our own body. */
1.151     schwarze  753:
1.150     schwarze  754:                if (n->type == MDOC_BODY && atok == n->tok) {
                    755:                        if (n->end == ENDBODY_NOT)
1.83      schwarze  756:                                body = n;
                    757:                        continue;
                    758:                }
                    759:
1.150     schwarze  760:                if (n->type != MDOC_BLOCK || n->tok == MDOC_Nm)
1.83      schwarze  761:                        continue;
1.160     schwarze  762:
                    763:                if (n->tok == MDOC_It) {
                    764:                        have_it = 1;
                    765:                        continue;
                    766:                }
                    767:
1.83      schwarze  768:                if (atok == n->tok) {
                    769:                        assert(body);
                    770:
                    771:                        /*
                    772:                         * Found the start of our own block.
                    773:                         * When there is no pending sub block,
                    774:                         * just proceed to closing out.
                    775:                         */
1.151     schwarze  776:
1.160     schwarze  777:                        if (later == NULL ||
                    778:                            (tok == MDOC_El && !have_it))
1.83      schwarze  779:                                break;
                    780:
1.131     schwarze  781:                        /*
1.83      schwarze  782:                         * When there is a pending sub block,
                    783:                         * postpone closing out the current block
                    784:                         * until the rew_sub() closing out the sub-block.
                    785:                         */
1.151     schwarze  786:
1.119     schwarze  787:                        make_pending(later, tok, mdoc, line, ppos);
1.83      schwarze  788:
                    789:                        /*
                    790:                         * Mark the place where the formatting - but not
                    791:                         * the scope - of the current block ends.
                    792:                         */
1.151     schwarze  793:
1.150     schwarze  794:                        mdoc_endbody_alloc(mdoc, line, ppos,
                    795:                            atok, body, ENDBODY_SPACE);
1.151     schwarze  796:
                    797:                        /*
                    798:                         * If a block closing macro taking arguments
                    799:                         * breaks another block, put the arguments
                    800:                         * into the end marker and remeber the
                    801:                         * end marker in order to close it out.
                    802:                         */
                    803:
1.148     schwarze  804:                        if (maxargs) {
                    805:                                endbody = mdoc->last;
                    806:                                mdoc->next = MDOC_NEXT_CHILD;
                    807:                        }
1.83      schwarze  808:                        break;
                    809:                }
                    810:
                    811:                /*
                    812:                 * When finding an open sub block, remember the last
                    813:                 * open explicit block, or, in case there are only
                    814:                 * implicit ones, the first open implicit block.
                    815:                 */
1.151     schwarze  816:
1.160     schwarze  817:                if (later == NULL ||
                    818:                    ! (mdoc_macros[later->tok].flags & MDOC_EXPLICIT))
1.83      schwarze  819:                        later = n;
                    820:        }
1.151     schwarze  821:        rew_sub(MDOC_BODY, mdoc, tok, line, ppos);
1.83      schwarze  822:
1.150     schwarze  823:        if ( ! (mdoc_macros[tok].flags & MDOC_PARSED)) {
                    824:                if (buf[*pos] != '\0')
1.136     schwarze  825:                        mandoc_vmsg(MANDOCERR_ARG_SKIP,
                    826:                            mdoc->parse, line, ppos,
                    827:                            "%s %s", mdoc_macronames[tok],
                    828:                            buf + *pos);
1.150     schwarze  829:                rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos);
1.151     schwarze  830:                return;
1.1       kristaps  831:        }
                    832:
1.148     schwarze  833:        if (maxargs && endbody == NULL) {
                    834:                if (n == NULL) {
                    835:                        /*
                    836:                         * Stray .Ec without previous .Eo:
                    837:                         * Break the output line, ignore any arguments.
                    838:                         */
1.150     schwarze  839:                        mdoc_elem_alloc(mdoc, line, ppos, MDOC_br, NULL);
1.149     schwarze  840:                        rew_elem(mdoc, MDOC_br);
1.150     schwarze  841:                } else
                    842:                        mdoc_tail_alloc(mdoc, line, ppos, atok);
1.148     schwarze  843:        }
1.1       kristaps  844:
1.148     schwarze  845:        flushed = n == NULL;
                    846:        for (j = 0; ; j++) {
1.1       kristaps  847:                lastarg = *pos;
                    848:
                    849:                if (j == maxargs && ! flushed) {
1.150     schwarze  850:                        if (endbody == NULL)
                    851:                                rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos);
                    852:                        else
1.149     schwarze  853:                                rew_last(mdoc, endbody);
1.1       kristaps  854:                        flushed = 1;
                    855:                }
                    856:
1.119     schwarze  857:                ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.150     schwarze  858:                if (ac == ARGS_PUNCT || ac == ARGS_EOLN)
1.1       kristaps  859:                        break;
                    860:
1.155     schwarze  861:                ntok = ac == ARGS_QWORD ? MDOC_MAX :
                    862:                    lookup(mdoc, tok, line, lastarg, p);
1.54      kristaps  863:
1.150     schwarze  864:                if (ntok == MDOC_MAX) {
                    865:                        dword(mdoc, line, lastarg, p, DELIM_MAX,
                    866:                            MDOC_JOIN & mdoc_macros[tok].flags);
1.54      kristaps  867:                        continue;
                    868:                }
1.1       kristaps  869:
1.54      kristaps  870:                if ( ! flushed) {
1.150     schwarze  871:                        if (endbody == NULL)
                    872:                                rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos);
                    873:                        else
1.149     schwarze  874:                                rew_last(mdoc, endbody);
1.54      kristaps  875:                        flushed = 1;
                    876:                }
1.122     schwarze  877:                mdoc->flags &= ~MDOC_NEWLINE;
1.151     schwarze  878:                mdoc_macro(mdoc, ntok, line, lastarg, pos, buf);
1.54      kristaps  879:                break;
1.1       kristaps  880:        }
                    881:
1.149     schwarze  882:        if ( ! flushed) {
1.150     schwarze  883:                if (endbody == NULL)
                    884:                        rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos);
                    885:                else
1.149     schwarze  886:                        rew_last(mdoc, endbody);
                    887:        }
1.150     schwarze  888:        if (nl)
                    889:                append_delims(mdoc, line, pos, buf);
1.1       kristaps  890: }
                    891:
1.151     schwarze  892: static void
1.1       kristaps  893: in_line(MACRO_PROT_ARGS)
                    894: {
1.144     schwarze  895:        int              la, scope, cnt, firstarg, mayopen, nc, nl;
1.53      kristaps  896:        enum mdoct       ntok;
1.55      kristaps  897:        enum margserr    ac;
1.67      schwarze  898:        enum mdelim      d;
1.53      kristaps  899:        struct mdoc_arg *arg;
                    900:        char            *p;
1.1       kristaps  901:
1.119     schwarze  902:        nl = MDOC_NEWLINE & mdoc->flags;
1.60      kristaps  903:
1.4       kristaps  904:        /*
                    905:         * Whether we allow ignored elements (those without content,
                    906:         * usually because of reserved words) to squeak by.
                    907:         */
1.45      kristaps  908:
1.4       kristaps  909:        switch (tok) {
1.131     schwarze  910:        case MDOC_An:
1.26      kristaps  911:                /* FALLTHROUGH */
1.131     schwarze  912:        case MDOC_Ar:
1.4       kristaps  913:                /* FALLTHROUGH */
1.131     schwarze  914:        case MDOC_Fl:
1.4       kristaps  915:                /* FALLTHROUGH */
1.131     schwarze  916:        case MDOC_Mt:
1.12      kristaps  917:                /* FALLTHROUGH */
1.131     schwarze  918:        case MDOC_Nm:
1.26      kristaps  919:                /* FALLTHROUGH */
1.131     schwarze  920:        case MDOC_Pa:
1.4       kristaps  921:                nc = 1;
                    922:                break;
                    923:        default:
                    924:                nc = 0;
                    925:                break;
                    926:        }
                    927:
1.152     schwarze  928:        mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1.1       kristaps  929:
1.144     schwarze  930:        d = DELIM_NONE;
                    931:        firstarg = 1;
1.142     schwarze  932:        mayopen = 1;
1.70      kristaps  933:        for (cnt = scope = 0;; ) {
1.1       kristaps  934:                la = *pos;
1.119     schwarze  935:                ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.1       kristaps  936:
1.144     schwarze  937:                /*
                    938:                 * At the end of a macro line,
                    939:                 * opening delimiters do not suppress spacing.
                    940:                 */
                    941:
                    942:                if (ac == ARGS_EOLN) {
                    943:                        if (d == DELIM_OPEN)
                    944:                                mdoc->last->flags &= ~MDOC_DELIMO;
1.1       kristaps  945:                        break;
1.144     schwarze  946:                }
                    947:
                    948:                /*
                    949:                 * The rest of the macro line is only punctuation,
                    950:                 * to be handled by append_delims().
                    951:                 * If there were no other arguments,
                    952:                 * do not allow the first one to suppress spacing,
                    953:                 * even if it turns out to be a closing one.
                    954:                 */
                    955:
                    956:                if (ac == ARGS_PUNCT) {
                    957:                        if (cnt == 0 && nc == 0)
                    958:                                mdoc->flags |= MDOC_NODELIMC;
1.1       kristaps  959:                        break;
1.144     schwarze  960:                }
1.1       kristaps  961:
1.145     schwarze  962:                ntok = (ac == ARGS_QWORD || (tok == MDOC_Fn && !cnt)) ?
1.155     schwarze  963:                    MDOC_MAX : lookup(mdoc, tok, line, la, p);
1.1       kristaps  964:
1.131     schwarze  965:                /*
1.4       kristaps  966:                 * In this case, we've located a submacro and must
                    967:                 * execute it.  Close out scope, if open.  If no
                    968:                 * elements have been generated, either create one (nc)
                    969:                 * or raise a warning.
                    970:                 */
1.1       kristaps  971:
1.150     schwarze  972:                if (ntok != MDOC_MAX) {
1.149     schwarze  973:                        if (scope)
                    974:                                rew_elem(mdoc, tok);
1.150     schwarze  975:                        if (nc && ! cnt) {
                    976:                                mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1.149     schwarze  977:                                rew_last(mdoc, mdoc->last);
1.150     schwarze  978:                        } else if ( ! nc && ! cnt) {
1.7       kristaps  979:                                mdoc_argv_free(arg);
1.135     schwarze  980:                                mandoc_msg(MANDOCERR_MACRO_EMPTY,
                    981:                                    mdoc->parse, line, ppos,
                    982:                                    mdoc_macronames[tok]);
1.7       kristaps  983:                        }
1.151     schwarze  984:                        mdoc_macro(mdoc, ntok, line, la, pos, buf);
1.150     schwarze  985:                        if (nl)
                    986:                                append_delims(mdoc, line, pos, buf);
1.151     schwarze  987:                        return;
1.131     schwarze  988:                }
1.1       kristaps  989:
1.131     schwarze  990:                /*
1.4       kristaps  991:                 * Non-quote-enclosed punctuation.  Set up our scope, if
                    992:                 * a word; rewind the scope, if a delimiter; then append
1.131     schwarze  993:                 * the word.
1.4       kristaps  994:                 */
1.1       kristaps  995:
1.150     schwarze  996:                d = ac == ARGS_QWORD ? DELIM_NONE : mdoc_isdelim(p);
1.3       kristaps  997:
1.70      kristaps  998:                if (DELIM_NONE != d) {
                    999:                        /*
                   1000:                         * If we encounter closing punctuation, no word
1.144     schwarze 1001:                         * has been emitted, no scope is open, and we're
1.70      kristaps 1002:                         * allowed to have an empty element, then start
1.142     schwarze 1003:                         * a new scope.
1.70      kristaps 1004:                         */
1.142     schwarze 1005:                        if ((d == DELIM_CLOSE ||
                   1006:                             (d == DELIM_MIDDLE && tok == MDOC_Fl)) &&
1.144     schwarze 1007:                            !cnt && !scope && nc && mayopen) {
1.150     schwarze 1008:                                mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1.70      kristaps 1009:                                scope = 1;
1.142     schwarze 1010:                                cnt++;
1.150     schwarze 1011:                                if (tok == MDOC_Nm)
1.142     schwarze 1012:                                        mayopen = 0;
1.70      kristaps 1013:                        }
                   1014:                        /*
                   1015:                         * Close out our scope, if one is open, before
                   1016:                         * any punctuation.
                   1017:                         */
1.149     schwarze 1018:                        if (scope)
                   1019:                                rew_elem(mdoc, tok);
1.70      kristaps 1020:                        scope = 0;
1.145     schwarze 1021:                        if (tok == MDOC_Fn)
                   1022:                                mayopen = 0;
1.142     schwarze 1023:                } else if (mayopen && !scope) {
1.150     schwarze 1024:                        mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1.70      kristaps 1025:                        scope = 1;
1.142     schwarze 1026:                        cnt++;
1.1       kristaps 1027:                }
1.105     kristaps 1028:
1.150     schwarze 1029:                dword(mdoc, line, la, p, d,
                   1030:                    MDOC_JOIN & mdoc_macros[tok].flags);
1.39      kristaps 1031:
                   1032:                /*
1.144     schwarze 1033:                 * If the first argument is a closing delimiter,
                   1034:                 * do not suppress spacing before it.
                   1035:                 */
                   1036:
                   1037:                if (firstarg && d == DELIM_CLOSE && !nc)
                   1038:                        mdoc->last->flags &= ~MDOC_DELIMC;
                   1039:                firstarg = 0;
                   1040:
                   1041:                /*
1.39      kristaps 1042:                 * `Fl' macros have their scope re-opened with each new
                   1043:                 * word so that the `-' can be added to each one without
                   1044:                 * having to parse out spaces.
                   1045:                 */
1.150     schwarze 1046:                if (scope && tok == MDOC_Fl) {
1.149     schwarze 1047:                        rew_elem(mdoc, tok);
1.70      kristaps 1048:                        scope = 0;
1.39      kristaps 1049:                }
1.1       kristaps 1050:        }
                   1051:
1.149     schwarze 1052:        if (scope)
                   1053:                rew_elem(mdoc, tok);
1.4       kristaps 1054:
                   1055:        /*
                   1056:         * If no elements have been collected and we're allowed to have
                   1057:         * empties (nc), open a scope and close it out.  Otherwise,
                   1058:         * raise a warning.
                   1059:         */
1.45      kristaps 1060:
1.150     schwarze 1061:        if ( ! cnt) {
                   1062:                if (nc) {
                   1063:                        mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
                   1064:                        rew_last(mdoc, mdoc->last);
                   1065:                } else {
                   1066:                        mdoc_argv_free(arg);
                   1067:                        mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse,
                   1068:                            line, ppos, mdoc_macronames[tok]);
                   1069:                }
1.7       kristaps 1070:        }
1.150     schwarze 1071:        if (nl)
                   1072:                append_delims(mdoc, line, pos, buf);
1.1       kristaps 1073: }
                   1074:
1.151     schwarze 1075: static void
1.1       kristaps 1076: blk_full(MACRO_PROT_ARGS)
                   1077: {
1.153     schwarze 1078:        int               la, nl, parsed;
1.1       kristaps 1079:        struct mdoc_arg  *arg;
1.45      kristaps 1080:        struct mdoc_node *head; /* save of head macro */
1.49      kristaps 1081:        struct mdoc_node *body; /* save of body macro */
1.47      kristaps 1082:        struct mdoc_node *n;
1.59      kristaps 1083:        enum margserr     ac, lac;
1.1       kristaps 1084:        char             *p;
                   1085:
1.119     schwarze 1086:        nl = MDOC_NEWLINE & mdoc->flags;
1.138     schwarze 1087:
                   1088:        /* Skip items outside lists. */
                   1089:
                   1090:        if (tok == MDOC_It) {
                   1091:                for (n = mdoc->last; n; n = n->parent)
1.158     schwarze 1092:                        if (n->tok == MDOC_Bl && n->type == MDOC_BLOCK &&
                   1093:                            ! (n->flags & (MDOC_VALID | MDOC_BREAK)))
1.138     schwarze 1094:                                break;
                   1095:                if (n == NULL) {
                   1096:                        mandoc_vmsg(MANDOCERR_IT_STRAY, mdoc->parse,
                   1097:                            line, ppos, "It %s", buf + *pos);
1.150     schwarze 1098:                        mdoc_elem_alloc(mdoc, line, ppos, MDOC_br, NULL);
1.149     schwarze 1099:                        rew_elem(mdoc, MDOC_br);
1.151     schwarze 1100:                        return;
1.138     schwarze 1101:                }
                   1102:        }
1.63      kristaps 1103:
1.45      kristaps 1104:        /* Close out prior implicit scope. */
1.19      kristaps 1105:
1.150     schwarze 1106:        if ( ! (mdoc_macros[tok].flags & MDOC_EXPLICIT)) {
                   1107:                rew_sub(MDOC_BODY, mdoc, tok, line, ppos);
                   1108:                rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos);
1.1       kristaps 1109:        }
                   1110:
1.45      kristaps 1111:        /*
1.109     kristaps 1112:         * This routine accommodates implicitly- and explicitly-scoped
1.45      kristaps 1113:         * macro openings.  Implicit ones first close out prior scope
                   1114:         * (seen above).  Delay opening the head until necessary to
                   1115:         * allow leading punctuation to print.  Special consideration
                   1116:         * for `It -column', which has phrase-part syntax instead of
                   1117:         * regular child nodes.
                   1118:         */
                   1119:
1.152     schwarze 1120:        mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1.150     schwarze 1121:        mdoc_block_alloc(mdoc, line, ppos, tok, arg);
1.49      kristaps 1122:        head = body = NULL;
1.115     schwarze 1123:
                   1124:        /*
                   1125:         * Exception: Heads of `It' macros in `-diag' lists are not
                   1126:         * parsed, even though `It' macros in general are parsed.
                   1127:         */
1.151     schwarze 1128:
1.153     schwarze 1129:        parsed = tok != MDOC_It ||
                   1130:            mdoc->last->parent->tok != MDOC_Bl ||
                   1131:            mdoc->last->parent->norm->Bl.type != LIST_diag;
1.1       kristaps 1132:
1.45      kristaps 1133:        /*
                   1134:         * The `Nd' macro has all arguments in its body: it's a hybrid
                   1135:         * of block partial-explicit and full-implicit.  Stupid.
                   1136:         */
1.19      kristaps 1137:
1.150     schwarze 1138:        if (tok == MDOC_Nd) {
                   1139:                head = mdoc_head_alloc(mdoc, line, ppos, tok);
1.162   ! schwarze 1140:                rew_last(mdoc, head);
1.150     schwarze 1141:                body = mdoc_body_alloc(mdoc, line, ppos, tok);
1.123     schwarze 1142:        }
                   1143:
1.150     schwarze 1144:        if (tok == MDOC_Bk)
1.123     schwarze 1145:                mdoc->flags |= MDOC_KEEP;
1.19      kristaps 1146:
1.150     schwarze 1147:        ac = ARGS_PEND;
                   1148:        for (;;) {
1.45      kristaps 1149:                la = *pos;
1.150     schwarze 1150:                lac = ac;
1.119     schwarze 1151:                ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.150     schwarze 1152:                if (ac == ARGS_PUNCT)
1.89      schwarze 1153:                        break;
1.150     schwarze 1154:                if (ac == ARGS_EOLN) {
                   1155:                        if (lac != ARGS_PPHRASE && lac != ARGS_PHRASE)
1.75      kristaps 1156:                                break;
                   1157:                        /*
                   1158:                         * This is necessary: if the last token on a
                   1159:                         * line is a `Ta' or tab, then we'll get
                   1160:                         * ARGS_EOLN, so we must be smart enough to
                   1161:                         * reopen our scope if the last parse was a
                   1162:                         * phrase or partial phrase.
                   1163:                         */
1.150     schwarze 1164:                        rew_sub(MDOC_BODY, mdoc, tok, line, ppos);
                   1165:                        body = mdoc_body_alloc(mdoc, line, ppos, tok);
1.1       kristaps 1166:                        break;
1.75      kristaps 1167:                }
1.45      kristaps 1168:
1.131     schwarze 1169:                /*
1.73      kristaps 1170:                 * Emit leading punctuation (i.e., punctuation before
                   1171:                 * the MDOC_HEAD) for non-phrase types.
                   1172:                 */
1.45      kristaps 1173:
1.150     schwarze 1174:                if (head == NULL &&
                   1175:                    ac != ARGS_PEND &&
                   1176:                    ac != ARGS_PHRASE &&
                   1177:                    ac != ARGS_PPHRASE &&
                   1178:                    ac != ARGS_QWORD &&
                   1179:                    mdoc_isdelim(p) == DELIM_OPEN) {
                   1180:                        dword(mdoc, line, la, p, DELIM_OPEN, 0);
1.45      kristaps 1181:                        continue;
                   1182:                }
                   1183:
1.74      kristaps 1184:                /* Open a head if one hasn't been opened. */
1.45      kristaps 1185:
1.150     schwarze 1186:                if (head == NULL)
                   1187:                        head = mdoc_head_alloc(mdoc, line, ppos, tok);
1.45      kristaps 1188:
1.150     schwarze 1189:                if (ac == ARGS_PHRASE ||
                   1190:                    ac == ARGS_PEND ||
                   1191:                    ac == ARGS_PPHRASE) {
1.151     schwarze 1192:
1.73      kristaps 1193:                        /*
1.74      kristaps 1194:                         * If we haven't opened a body yet, rewind the
                   1195:                         * head; if we have, rewind that instead.
                   1196:                         */
                   1197:
1.150     schwarze 1198:                        rew_sub(body ? MDOC_BODY : MDOC_HEAD,
                   1199:                            mdoc, tok, line, ppos);
                   1200:                        body = mdoc_body_alloc(mdoc, line, ppos, tok);
1.74      kristaps 1201:
                   1202:                        /*
1.73      kristaps 1203:                         * Process phrases: set whether we're in a
                   1204:                         * partial-phrase (this effects line handling)
                   1205:                         * then call down into the phrase parser.
                   1206:                         */
1.74      kristaps 1207:
1.150     schwarze 1208:                        if (ac == ARGS_PPHRASE)
1.119     schwarze 1209:                                mdoc->flags |= MDOC_PPHRASE;
1.150     schwarze 1210:                        if (ac == ARGS_PEND && lac == ARGS_PPHRASE)
1.119     schwarze 1211:                                mdoc->flags |= MDOC_PPHRASE;
1.154     schwarze 1212:                        parse_rest(mdoc, MDOC_MAX, line, &la, buf);
1.119     schwarze 1213:                        mdoc->flags &= ~MDOC_PPHRASE;
1.1       kristaps 1214:                        continue;
                   1215:                }
                   1216:
1.153     schwarze 1217:                if (macro_or_word(mdoc, tok, line, la, pos, buf, parsed))
1.151     schwarze 1218:                        break;
1.45      kristaps 1219:        }
1.1       kristaps 1220:
1.150     schwarze 1221:        if (head == NULL)
                   1222:                head = mdoc_head_alloc(mdoc, line, ppos, tok);
                   1223:        if (nl)
                   1224:                append_delims(mdoc, line, pos, buf);
1.151     schwarze 1225:        if (body != NULL)
1.77      kristaps 1226:                goto out;
1.19      kristaps 1227:
1.47      kristaps 1228:        /*
1.49      kristaps 1229:         * If there is an open (i.e., unvalidated) sub-block requiring
                   1230:         * explicit close-out, postpone switching the current block from
                   1231:         * head to body until the rew_sub() call closing out that
                   1232:         * sub-block.
1.47      kristaps 1233:         */
1.119     schwarze 1234:        for (n = mdoc->last; n && n != head; n = n->parent) {
1.151     schwarze 1235:                if (n->type == MDOC_BLOCK &&
                   1236:                    mdoc_macros[n->tok].flags & MDOC_EXPLICIT &&
                   1237:                    ! (n->flags & MDOC_VALID)) {
1.47      kristaps 1238:                        n->pending = head;
1.151     schwarze 1239:                        return;
1.47      kristaps 1240:                }
                   1241:        }
                   1242:
1.45      kristaps 1243:        /* Close out scopes to remain in a consistent state. */
                   1244:
1.150     schwarze 1245:        rew_sub(MDOC_HEAD, mdoc, tok, line, ppos);
                   1246:        mdoc_body_alloc(mdoc, line, ppos, tok);
1.77      kristaps 1247: out:
1.150     schwarze 1248:        if (mdoc->flags & MDOC_FREECOL) {
                   1249:                rew_sub(MDOC_BODY, mdoc, tok, line, ppos);
                   1250:                rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos);
                   1251:                mdoc->flags &= ~MDOC_FREECOL;
                   1252:        }
1.1       kristaps 1253: }
                   1254:
1.151     schwarze 1255: static void
1.1       kristaps 1256: blk_part_imp(MACRO_PROT_ARGS)
                   1257: {
1.63      kristaps 1258:        int               la, nl;
1.55      kristaps 1259:        enum margserr     ac;
1.1       kristaps 1260:        char             *p;
1.43      kristaps 1261:        struct mdoc_node *blk; /* saved block context */
                   1262:        struct mdoc_node *body; /* saved body context */
                   1263:        struct mdoc_node *n;
1.1       kristaps 1264:
1.119     schwarze 1265:        nl = MDOC_NEWLINE & mdoc->flags;
1.63      kristaps 1266:
1.43      kristaps 1267:        /*
                   1268:         * A macro that spans to the end of the line.  This is generally
                   1269:         * (but not necessarily) called as the first macro.  The block
                   1270:         * has a head as the immediate child, which is always empty,
                   1271:         * followed by zero or more opening punctuation nodes, then the
                   1272:         * body (which may be empty, depending on the macro), then zero
                   1273:         * or more closing punctuation nodes.
                   1274:         */
1.32      kristaps 1275:
1.150     schwarze 1276:        blk = mdoc_block_alloc(mdoc, line, ppos, tok, NULL);
1.162   ! schwarze 1277:        rew_last(mdoc, mdoc_head_alloc(mdoc, line, ppos, tok));
1.1       kristaps 1278:
1.43      kristaps 1279:        /*
                   1280:         * Open the body scope "on-demand", that is, after we've
                   1281:         * processed all our the leading delimiters (open parenthesis,
                   1282:         * etc.).
                   1283:         */
1.1       kristaps 1284:
1.43      kristaps 1285:        for (body = NULL; ; ) {
1.32      kristaps 1286:                la = *pos;
1.119     schwarze 1287:                ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.150     schwarze 1288:                if (ac == ARGS_EOLN || ac == ARGS_PUNCT)
1.1       kristaps 1289:                        break;
                   1290:
1.150     schwarze 1291:                if (body == NULL && ac != ARGS_QWORD &&
                   1292:                    mdoc_isdelim(p) == DELIM_OPEN) {
                   1293:                        dword(mdoc, line, la, p, DELIM_OPEN, 0);
1.1       kristaps 1294:                        continue;
1.123     schwarze 1295:                }
1.1       kristaps 1296:
1.150     schwarze 1297:                if (body == NULL)
                   1298:                        body = mdoc_body_alloc(mdoc, line, ppos, tok);
1.43      kristaps 1299:
1.153     schwarze 1300:                if (macro_or_word(mdoc, tok, line, la, pos, buf, 1))
1.151     schwarze 1301:                        break;
1.1       kristaps 1302:        }
1.150     schwarze 1303:        if (body == NULL)
                   1304:                body = mdoc_body_alloc(mdoc, line, ppos, tok);
1.43      kristaps 1305:
1.83      schwarze 1306:        /*
                   1307:         * If there is an open sub-block requiring explicit close-out,
                   1308:         * postpone closing out the current block
                   1309:         * until the rew_sub() call closing out the sub-block.
                   1310:         */
1.151     schwarze 1311:
1.119     schwarze 1312:        for (n = mdoc->last; n && n != body && n != blk->parent;
1.131     schwarze 1313:             n = n->parent) {
1.150     schwarze 1314:                if (n->type == MDOC_BLOCK &&
                   1315:                    mdoc_macros[n->tok].flags & MDOC_EXPLICIT &&
                   1316:                    ! (n->flags & MDOC_VALID)) {
1.119     schwarze 1317:                        make_pending(n, tok, mdoc, line, ppos);
1.150     schwarze 1318:                        mdoc_endbody_alloc(mdoc, line, ppos,
                   1319:                            tok, body, ENDBODY_NOSPACE);
1.151     schwarze 1320:                        return;
1.83      schwarze 1321:                }
                   1322:        }
1.134     schwarze 1323:        assert(n == body);
1.150     schwarze 1324:        rew_sub(MDOC_BODY, mdoc, tok, line, ppos);
                   1325:        if (nl)
                   1326:                append_delims(mdoc, line, pos, buf);
                   1327:        rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos);
1.117     schwarze 1328:
                   1329:        /* Move trailing .Ns out of scope. */
                   1330:
                   1331:        for (n = body->child; n && n->next; n = n->next)
                   1332:                /* Do nothing. */ ;
1.150     schwarze 1333:        if (n && n->tok == MDOC_Ns)
1.119     schwarze 1334:                mdoc_node_relink(mdoc, n);
1.1       kristaps 1335: }
                   1336:
1.151     schwarze 1337: static void
1.1       kristaps 1338: blk_part_exp(MACRO_PROT_ARGS)
                   1339: {
1.60      kristaps 1340:        int               la, nl;
1.55      kristaps 1341:        enum margserr     ac;
1.43      kristaps 1342:        struct mdoc_node *head; /* keep track of head */
                   1343:        struct mdoc_node *body; /* keep track of body */
1.1       kristaps 1344:        char             *p;
                   1345:
1.119     schwarze 1346:        nl = MDOC_NEWLINE & mdoc->flags;
1.60      kristaps 1347:
1.43      kristaps 1348:        /*
                   1349:         * The opening of an explicit macro having zero or more leading
                   1350:         * punctuation nodes; a head with optional single element (the
                   1351:         * case of `Eo'); and a body that may be empty.
                   1352:         */
1.32      kristaps 1353:
1.150     schwarze 1354:        mdoc_block_alloc(mdoc, line, ppos, tok, NULL);
1.43      kristaps 1355:        for (head = body = NULL; ; ) {
1.32      kristaps 1356:                la = *pos;
1.119     schwarze 1357:                ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.150     schwarze 1358:                if (ac == ARGS_PUNCT || ac == ARGS_EOLN)
1.1       kristaps 1359:                        break;
1.43      kristaps 1360:
                   1361:                /* Flush out leading punctuation. */
                   1362:
1.150     schwarze 1363:                if (head == NULL && ac != ARGS_QWORD &&
                   1364:                    mdoc_isdelim(p) == DELIM_OPEN) {
1.43      kristaps 1365:                        assert(NULL == body);
1.150     schwarze 1366:                        dword(mdoc, line, la, p, DELIM_OPEN, 0);
1.43      kristaps 1367:                        continue;
1.123     schwarze 1368:                }
1.43      kristaps 1369:
1.150     schwarze 1370:                if (head == NULL) {
                   1371:                        assert(body == NULL);
                   1372:                        head = mdoc_head_alloc(mdoc, line, ppos, tok);
1.1       kristaps 1373:                }
                   1374:
1.43      kristaps 1375:                /*
                   1376:                 * `Eo' gobbles any data into the head, but most other
                   1377:                 * macros just immediately close out and begin the body.
                   1378:                 */
                   1379:
1.150     schwarze 1380:                if (body == NULL) {
1.43      kristaps 1381:                        assert(head);
                   1382:                        /* No check whether it's a macro! */
1.150     schwarze 1383:                        if (tok == MDOC_Eo)
                   1384:                                dword(mdoc, line, la, p, DELIM_MAX, 0);
                   1385:                        rew_sub(MDOC_HEAD, mdoc, tok, line, ppos);
                   1386:                        body = mdoc_body_alloc(mdoc, line, ppos, tok);
                   1387:                        if (tok == MDOC_Eo)
1.43      kristaps 1388:                                continue;
1.1       kristaps 1389:                }
1.150     schwarze 1390:                assert(head != NULL && body != NULL);
1.43      kristaps 1391:
1.153     schwarze 1392:                if (macro_or_word(mdoc, tok, line, la, pos, buf, 1))
1.151     schwarze 1393:                        break;
1.1       kristaps 1394:        }
                   1395:
1.43      kristaps 1396:        /* Clean-up to leave in a consistent state. */
1.32      kristaps 1397:
1.150     schwarze 1398:        if (head == NULL)
                   1399:                mdoc_head_alloc(mdoc, line, ppos, tok);
1.43      kristaps 1400:
1.150     schwarze 1401:        if (body == NULL) {
                   1402:                rew_sub(MDOC_HEAD, mdoc, tok, line, ppos);
                   1403:                mdoc_body_alloc(mdoc, line, ppos, tok);
1.1       kristaps 1404:        }
1.150     schwarze 1405:        if (nl)
                   1406:                append_delims(mdoc, line, pos, buf);
1.1       kristaps 1407: }
                   1408:
1.151     schwarze 1409: static void
1.1       kristaps 1410: in_line_argn(MACRO_PROT_ARGS)
                   1411: {
1.60      kristaps 1412:        int              la, flushed, j, maxargs, nl;
1.55      kristaps 1413:        enum margserr    ac;
1.53      kristaps 1414:        struct mdoc_arg *arg;
                   1415:        char            *p;
                   1416:        enum mdoct       ntok;
1.1       kristaps 1417:
1.156     schwarze 1418:        nl = mdoc->flags & MDOC_NEWLINE;
1.60      kristaps 1419:
1.46      kristaps 1420:        /*
                   1421:         * A line macro that has a fixed number of arguments (maxargs).
                   1422:         * Only open the scope once the first non-leading-punctuation is
                   1423:         * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
                   1424:         * keep it open until the maximum number of arguments are
                   1425:         * exhausted.
                   1426:         */
1.1       kristaps 1427:
                   1428:        switch (tok) {
1.131     schwarze 1429:        case MDOC_Ap:
1.1       kristaps 1430:                /* FALLTHROUGH */
1.131     schwarze 1431:        case MDOC_Ns:
1.1       kristaps 1432:                /* FALLTHROUGH */
1.131     schwarze 1433:        case MDOC_Ux:
1.1       kristaps 1434:                maxargs = 0;
                   1435:                break;
1.131     schwarze 1436:        case MDOC_Bx:
1.101     kristaps 1437:                /* FALLTHROUGH */
1.132     schwarze 1438:        case MDOC_Es:
                   1439:                /* FALLTHROUGH */
1.131     schwarze 1440:        case MDOC_Xr:
1.42      kristaps 1441:                maxargs = 2;
                   1442:                break;
1.1       kristaps 1443:        default:
                   1444:                maxargs = 1;
                   1445:                break;
                   1446:        }
                   1447:
1.152     schwarze 1448:        mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1.1       kristaps 1449:
1.156     schwarze 1450:        p = NULL;
                   1451:        flushed = j = 0;
                   1452:        for (;;) {
1.32      kristaps 1453:                la = *pos;
1.119     schwarze 1454:                ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.156     schwarze 1455:                if (ac == ARGS_PUNCT || ac == ARGS_EOLN) {
                   1456:                        if (j < 2 && tok == MDOC_Pf)
                   1457:                                mandoc_vmsg(MANDOCERR_PF_SKIP,
                   1458:                                    mdoc->parse, line, ppos, "Pf %s",
                   1459:                                    p == NULL ? "at eol" : p);
1.1       kristaps 1460:                        break;
1.156     schwarze 1461:                }
1.1       kristaps 1462:
1.150     schwarze 1463:                if ( ! (mdoc_macros[tok].flags & MDOC_IGNDELIM) &&
                   1464:                    ac != ARGS_QWORD && j == 0 &&
                   1465:                    mdoc_isdelim(p) == DELIM_OPEN) {
                   1466:                        dword(mdoc, line, la, p, DELIM_OPEN, 0);
1.46      kristaps 1467:                        continue;
1.150     schwarze 1468:                } else if (j == 0)
                   1469:                       mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1.46      kristaps 1470:
                   1471:                if (j == maxargs && ! flushed) {
1.149     schwarze 1472:                        rew_elem(mdoc, tok);
1.46      kristaps 1473:                        flushed = 1;
                   1474:                }
                   1475:
1.156     schwarze 1476:                ntok = (ac == ARGS_QWORD || (tok == MDOC_Pf && j == 0)) ?
                   1477:                    MDOC_MAX : lookup(mdoc, tok, line, la, p);
1.54      kristaps 1478:
1.150     schwarze 1479:                if (ntok != MDOC_MAX) {
1.149     schwarze 1480:                        if ( ! flushed)
                   1481:                                rew_elem(mdoc, tok);
1.1       kristaps 1482:                        flushed = 1;
1.151     schwarze 1483:                        mdoc_macro(mdoc, ntok, line, la, pos, buf);
1.46      kristaps 1484:                        j++;
1.1       kristaps 1485:                        break;
                   1486:                }
                   1487:
1.150     schwarze 1488:                if ( ! (mdoc_macros[tok].flags & MDOC_IGNDELIM) &&
                   1489:                    ac != ARGS_QWORD && ! flushed &&
                   1490:                    mdoc_isdelim(p) != DELIM_NONE) {
1.149     schwarze 1491:                        rew_elem(mdoc, tok);
1.1       kristaps 1492:                        flushed = 1;
                   1493:                }
1.42      kristaps 1494:
1.150     schwarze 1495:                dword(mdoc, line, la, p, DELIM_MAX,
                   1496:                    MDOC_JOIN & mdoc_macros[tok].flags);
1.46      kristaps 1497:                j++;
1.1       kristaps 1498:        }
                   1499:
1.156     schwarze 1500:        if (j == 0) {
1.150     schwarze 1501:                mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1.156     schwarze 1502:                if (ac == ARGS_PUNCT && tok == MDOC_Pf)
                   1503:                        append_delims(mdoc, line, pos, buf);
                   1504:        }
1.149     schwarze 1505:        if ( ! flushed)
                   1506:                rew_elem(mdoc, tok);
1.150     schwarze 1507:        if (nl)
                   1508:                append_delims(mdoc, line, pos, buf);
1.1       kristaps 1509: }
                   1510:
1.151     schwarze 1511: static void
1.1       kristaps 1512: in_line_eoln(MACRO_PROT_ARGS)
                   1513: {
1.53      kristaps 1514:        struct mdoc_arg *arg;
1.1       kristaps 1515:
1.90      schwarze 1516:        if (tok == MDOC_Pp)
1.119     schwarze 1517:                rew_sub(MDOC_BLOCK, mdoc, MDOC_Nm, line, ppos);
1.90      schwarze 1518:
1.152     schwarze 1519:        mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1.150     schwarze 1520:        mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1.154     schwarze 1521:        if (parse_rest(mdoc, tok, line, pos, buf))
                   1522:                return;
                   1523:        rew_elem(mdoc, tok);
                   1524: }
                   1525:
                   1526: /*
                   1527:  * The simplest argument parser available: Parse the remaining
                   1528:  * words until the end of the phrase or line and return 0
                   1529:  * or until the next macro, call that macro, and return 1.
                   1530:  */
                   1531: static int
                   1532: parse_rest(struct mdoc *mdoc, enum mdoct tok, int line, int *pos, char *buf)
                   1533: {
                   1534:        int              la;
1.1       kristaps 1535:
                   1536:        for (;;) {
                   1537:                la = *pos;
1.153     schwarze 1538:                if (mdoc_args(mdoc, line, pos, buf, tok, NULL) == ARGS_EOLN)
1.154     schwarze 1539:                        return(0);
1.153     schwarze 1540:                if (macro_or_word(mdoc, tok, line, la, pos, buf, 1))
1.154     schwarze 1541:                        return(1);
1.1       kristaps 1542:        }
                   1543: }
                   1544:
1.151     schwarze 1545: static void
1.41      kristaps 1546: ctx_synopsis(MACRO_PROT_ARGS)
                   1547: {
1.60      kristaps 1548:
1.151     schwarze 1549:        if (~mdoc->flags & (MDOC_SYNOPSIS | MDOC_NEWLINE))
                   1550:                in_line(mdoc, tok, line, ppos, pos, buf);
                   1551:        else if (tok == MDOC_Nm)
                   1552:                blk_full(mdoc, tok, line, ppos, pos, buf);
                   1553:        else {
                   1554:                assert(tok == MDOC_Vt);
                   1555:                blk_part_imp(mdoc, tok, line, ppos, pos, buf);
                   1556:        }
1.1       kristaps 1557: }
                   1558:
1.24      kristaps 1559: /*
                   1560:  * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
                   1561:  * They're unusual because they're basically free-form text until a
                   1562:  * macro is encountered.
                   1563:  */
1.151     schwarze 1564: static void
1.75      kristaps 1565: phrase_ta(MACRO_PROT_ARGS)
                   1566: {
1.121     schwarze 1567:        struct mdoc_node *n;
1.75      kristaps 1568:
1.121     schwarze 1569:        /* Make sure we are in a column list or ignore this macro. */
1.151     schwarze 1570:
1.121     schwarze 1571:        n = mdoc->last;
1.159     schwarze 1572:        while (n != NULL &&
                   1573:            (n->tok != MDOC_Bl || n->flags & (MDOC_VALID | MDOC_BREAK)))
1.121     schwarze 1574:                n = n->parent;
1.151     schwarze 1575:        if (n == NULL || n->norm->Bl.type != LIST_column) {
1.137     schwarze 1576:                mandoc_msg(MANDOCERR_TA_STRAY, mdoc->parse,
1.139     schwarze 1577:                    line, ppos, "Ta");
1.151     schwarze 1578:                return;
1.121     schwarze 1579:        }
1.75      kristaps 1580:
1.121     schwarze 1581:        /* Advance to the next column. */
1.150     schwarze 1582:
                   1583:        rew_sub(MDOC_BODY, mdoc, MDOC_It, line, ppos);
                   1584:        mdoc_body_alloc(mdoc, line, ppos, MDOC_It);
1.154     schwarze 1585:        parse_rest(mdoc, MDOC_MAX, line, pos, buf);
1.75      kristaps 1586: }

CVSweb