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

Annotation of mandoc/mdoc_macro.c, Revision 1.167

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

CVSweb