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

Annotation of mandoc/mdoc_macro.c, Revision 1.153

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

CVSweb