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

Annotation of mandoc/mdoc_macro.c, Revision 1.152

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

CVSweb