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

Annotation of mandoc/mdoc_macro.c, Revision 1.168

1.168   ! schwarze    1: /*     $Id: mdoc_macro.c,v 1.167 2015/02/02 15:02:49 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.168   ! schwarze   57: static void            make_pending(struct mdoc *, struct mdoc_node *,
        !            58:                                struct mdoc_node *, 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: /*
1.168   ! schwarze  463:  * We are trying to close the block *breaker,
1.83      schwarze  464:  * but the child block *broken is still open.
1.168   ! schwarze  465:  * Thus, postpone closing the *breaker
1.167     schwarze  466:  * until the rew_pending() call closing *broken.
1.83      schwarze  467:  */
1.168   ! schwarze  468: static void
        !           469: make_pending(struct mdoc *mdoc, struct mdoc_node *breaker,
        !           470:        struct mdoc_node *broken, int line, int ppos)
1.83      schwarze  471: {
1.168   ! schwarze  472:        struct mdoc_node *n;
1.83      schwarze  473:
1.168   ! schwarze  474:        /* Iterate backwards, searching for the breaker. */
        !           475:
        !           476:        for (n = broken->parent; ; n = n->parent) {
1.83      schwarze  477:
                    478:                /*
1.158     schwarze  479:                 * If the *broken block (Z) is already broken and we
                    480:                 * encounter its breaker (B), make the tok block (A)
                    481:                 * pending on that inner breaker (B).
1.168   ! schwarze  482:                 * Graphically, [A n=[B! broken=[Z->B B] tok=A] Z]
        !           483:                 * becomes [A broken=[B! [Z->B B] tok=A] Z]
1.158     schwarze  484:                 * and finally [A! [B!->A [Z->B B] A] Z].
                    485:                 * In these graphics, "->" indicates the "pending"
                    486:                 * pointer and "!" indicates the MDOC_BREAK flag.
                    487:                 * Each of the cases gets one additional pointer (B->A)
                    488:                 * and one additional flag (A!).
1.83      schwarze  489:                 */
1.168   ! schwarze  490:                if (n == broken->pending) {
        !           491:                        broken = n;
1.83      schwarze  492:                        continue;
                    493:                }
                    494:
1.168   ! schwarze  495:                if (n != breaker)
1.83      schwarze  496:                        continue;
                    497:
                    498:                /*
                    499:                 * Found the breaker.
1.158     schwarze  500:                 * If another, outer breaker (X) is already pending on
                    501:                 * the *broken block (B), we must not clobber the link
1.83      schwarze  502:                 * to the outer breaker, but make it pending on the
1.158     schwarze  503:                 * new, now inner breaker (A).
                    504:                 * Graphically, [X! breaker=[A broken=[B->X X] tok=A] B]
                    505:                 * becomes [X! breaker=[A->X broken=[B X] tok=A] B]
                    506:                 * and finally [X! [A!->X [B->A X] A] B].
1.83      schwarze  507:                 */
                    508:                if (broken->pending) {
                    509:                        struct mdoc_node *taker;
                    510:
                    511:                        /*
1.158     schwarze  512:                         * If the inner breaker (A) is already broken,
                    513:                         * too, it cannot take on the outer breaker (X)
                    514:                         * but must hand it on to its own breakers (Y):
                    515:                         * [X! [Y! breaker=[A->Y Y] broken=[B->X X] tok=A] B]
                    516:                         * [X! take=[Y!->X brea=[A->Y Y] brok=[B X] tok=A] B]
                    517:                         * and finally [X! [Y!->X [A!->Y Y] [B->A X] A] B].
1.83      schwarze  518:                         */
                    519:                        taker = breaker;
                    520:                        while (taker->pending)
                    521:                                taker = taker->pending;
                    522:                        taker->pending = broken->pending;
                    523:                }
1.158     schwarze  524:
                    525:                /*
                    526:                 * Now we have reduced the situation to the simplest
                    527:                 * case, which is just breaker=[A broken=[B tok=A] B]
                    528:                 * and becomes [A! [B->A A] B].
                    529:                 */
1.83      schwarze  530:                broken->pending = breaker;
1.158     schwarze  531:                breaker->flags |= MDOC_BREAK;
1.166     schwarze  532:                if (breaker->body != NULL)
                    533:                        breaker->body->flags |= MDOC_BREAK;
1.137     schwarze  534:                mandoc_vmsg(MANDOCERR_BLK_NEST, mdoc->parse, line, ppos,
1.168   ! schwarze  535:                    "%s breaks %s", mdoc_macronames[breaker->tok],
1.131     schwarze  536:                    mdoc_macronames[broken->tok]);
1.168   ! schwarze  537:                return;
1.83      schwarze  538:        }
                    539: }
                    540:
1.150     schwarze  541: static void
1.131     schwarze  542: rew_sub(enum mdoc_type t, struct mdoc *mdoc,
1.47      kristaps  543:                enum mdoct tok, int line, int ppos)
1.1       kristaps  544: {
1.160     schwarze  545:        struct mdoc_node *n, *to;
1.1       kristaps  546:
1.160     schwarze  547:        to = NULL;
1.119     schwarze  548:        n = mdoc->last;
1.84      schwarze  549:        while (n) {
                    550:                switch (rew_dohalt(tok, t, n)) {
1.131     schwarze  551:                case REWIND_NONE:
1.160     schwarze  552:                        if (to == NULL)
                    553:                                return;
                    554:                        n = to;
                    555:                        break;
1.131     schwarze  556:                case REWIND_THIS:
1.124     schwarze  557:                        n->lastline = line -
1.150     schwarze  558:                            (mdoc->flags & MDOC_NEWLINE &&
                    559:                             ! (mdoc_macros[tok].flags & MDOC_EXPLICIT));
1.84      schwarze  560:                        break;
1.131     schwarze  561:                case REWIND_FORCE:
1.137     schwarze  562:                        mandoc_vmsg(MANDOCERR_BLK_BROKEN, mdoc->parse,
1.131     schwarze  563:                            line, ppos, "%s breaks %s",
                    564:                            mdoc_macronames[tok],
                    565:                            mdoc_macronames[n->tok]);
1.98      kristaps  566:                        /* FALLTHROUGH */
1.131     schwarze  567:                case REWIND_MORE:
1.124     schwarze  568:                        n->lastline = line -
1.150     schwarze  569:                            (mdoc->flags & MDOC_NEWLINE ? 1 : 0);
1.160     schwarze  570:                        to = n;
1.84      schwarze  571:                        n = n->parent;
                    572:                        continue;
1.32      kristaps  573:                }
1.84      schwarze  574:                break;
1.1       kristaps  575:        }
                    576:        assert(n);
1.166     schwarze  577:        rew_pending(mdoc, n);
1.1       kristaps  578: }
                    579:
1.105     kristaps  580: /*
                    581:  * Allocate a word and check whether it's punctuation or not.
                    582:  * Punctuation consists of those tokens found in mdoc_isdelim().
                    583:  */
1.150     schwarze  584: static void
1.123     schwarze  585: dword(struct mdoc *mdoc, int line, int col, const char *p,
                    586:                enum mdelim d, int may_append)
1.105     kristaps  587: {
1.131     schwarze  588:
1.150     schwarze  589:        if (d == DELIM_MAX)
1.105     kristaps  590:                d = mdoc_isdelim(p);
                    591:
1.123     schwarze  592:        if (may_append &&
1.150     schwarze  593:            ! (mdoc->flags & (MDOC_SYNOPSIS | MDOC_KEEP | MDOC_SMOFF)) &&
                    594:            d == DELIM_NONE && mdoc->last->type == MDOC_TEXT &&
                    595:            mdoc_isdelim(mdoc->last->string) == DELIM_NONE) {
1.123     schwarze  596:                mdoc_word_append(mdoc, p);
1.150     schwarze  597:                return;
1.123     schwarze  598:        }
                    599:
1.150     schwarze  600:        mdoc_word_alloc(mdoc, line, col, p);
1.105     kristaps  601:
1.108     schwarze  602:        /*
1.144     schwarze  603:         * If the word consists of a bare delimiter,
                    604:         * flag the new node accordingly,
                    605:         * unless doing so was vetoed by the invoking macro.
                    606:         * Always clear the veto, it is only valid for one word.
1.108     schwarze  607:         */
                    608:
1.144     schwarze  609:        if (d == DELIM_OPEN)
                    610:                mdoc->last->flags |= MDOC_DELIMO;
                    611:        else if (d == DELIM_CLOSE &&
                    612:            ! (mdoc->flags & MDOC_NODELIMC) &&
1.131     schwarze  613:            mdoc->last->parent->tok != MDOC_Fd)
1.119     schwarze  614:                mdoc->last->flags |= MDOC_DELIMC;
1.144     schwarze  615:        mdoc->flags &= ~MDOC_NODELIMC;
1.105     kristaps  616: }
1.1       kristaps  617:
1.150     schwarze  618: static void
1.119     schwarze  619: append_delims(struct mdoc *mdoc, int line, int *pos, char *buf)
1.1       kristaps  620: {
1.153     schwarze  621:        char            *p;
1.66      kristaps  622:        int              la;
1.1       kristaps  623:
1.150     schwarze  624:        if (buf[*pos] == '\0')
                    625:                return;
1.1       kristaps  626:
                    627:        for (;;) {
1.66      kristaps  628:                la = *pos;
1.153     schwarze  629:                if (mdoc_args(mdoc, line, pos, buf, MDOC_MAX, &p) == ARGS_EOLN)
1.1       kristaps  630:                        break;
1.123     schwarze  631:                dword(mdoc, line, la, p, DELIM_MAX, 1);
1.66      kristaps  632:
                    633:                /*
                    634:                 * If we encounter end-of-sentence symbols, then trigger
                    635:                 * the double-space.
                    636:                 *
1.109     kristaps  637:                 * XXX: it's easy to allow this to propagate outward to
1.66      kristaps  638:                 * the last symbol, such that `. )' will cause the
                    639:                 * correct double-spacing.  However, (1) groff isn't
                    640:                 * smart enough to do this and (2) it would require
                    641:                 * knowing which symbols break this behaviour, for
1.109     kristaps  642:                 * example, `.  ;' shouldn't propagate the double-space.
1.66      kristaps  643:                 */
1.150     schwarze  644:
1.127     schwarze  645:                if (mandoc_eos(p, strlen(p)))
1.119     schwarze  646:                        mdoc->last->flags |= MDOC_EOS;
1.1       kristaps  647:        }
                    648: }
                    649:
                    650: /*
1.153     schwarze  651:  * Parse one word.
                    652:  * If it is a macro, call it and return 1.
                    653:  * Otherwise, allocate it and return 0.
                    654:  */
                    655: static int
                    656: macro_or_word(MACRO_PROT_ARGS, int parsed)
                    657: {
                    658:        char            *p;
                    659:        enum mdoct       ntok;
                    660:
                    661:        p = buf + ppos;
                    662:        ntok = MDOC_MAX;
1.157     schwarze  663:        if (*p == '"')
1.153     schwarze  664:                p++;
1.157     schwarze  665:        else if (parsed && ! (mdoc->flags & MDOC_PHRASELIT))
1.155     schwarze  666:                ntok = lookup(mdoc, tok, line, ppos, p);
1.153     schwarze  667:
                    668:        if (ntok == MDOC_MAX) {
                    669:                dword(mdoc, line, ppos, p, DELIM_MAX, tok == MDOC_MAX ||
                    670:                    mdoc_macros[tok].flags & MDOC_JOIN);
                    671:                return(0);
                    672:        } else {
                    673:                if (mdoc_macros[tok].fp == in_line_eoln)
                    674:                        rew_elem(mdoc, tok);
                    675:                mdoc_macro(mdoc, ntok, line, ppos, pos, buf);
                    676:                if (tok == MDOC_MAX)
                    677:                        append_delims(mdoc, line, pos, buf);
                    678:                return(1);
                    679:        }
                    680: }
                    681:
                    682: /*
1.131     schwarze  683:  * Close out block partial/full explicit.
1.1       kristaps  684:  */
1.151     schwarze  685: static void
1.1       kristaps  686: blk_exp_close(MACRO_PROT_ARGS)
                    687: {
1.83      schwarze  688:        struct mdoc_node *body;         /* Our own body. */
1.148     schwarze  689:        struct mdoc_node *endbody;      /* Our own end marker. */
1.83      schwarze  690:        struct mdoc_node *later;        /* A sub-block starting later. */
1.166     schwarze  691:        struct mdoc_node *n;            /* Search back to our block. */
1.83      schwarze  692:
1.166     schwarze  693:        int              have_it, j, lastarg, maxargs, nl;
1.55      kristaps  694:        enum margserr    ac;
1.83      schwarze  695:        enum mdoct       atok, ntok;
1.1       kristaps  696:        char            *p;
                    697:
1.119     schwarze  698:        nl = MDOC_NEWLINE & mdoc->flags;
1.60      kristaps  699:
1.1       kristaps  700:        switch (tok) {
1.131     schwarze  701:        case MDOC_Ec:
1.1       kristaps  702:                maxargs = 1;
                    703:                break;
1.131     schwarze  704:        case MDOC_Ek:
1.125     schwarze  705:                mdoc->flags &= ~MDOC_KEEP;
1.151     schwarze  706:                /* FALLTHROUGH */
1.1       kristaps  707:        default:
                    708:                maxargs = 0;
                    709:                break;
                    710:        }
                    711:
1.83      schwarze  712:        /*
                    713:         * Search backwards for beginnings of blocks,
                    714:         * both of our own and of pending sub-blocks.
                    715:         */
1.151     schwarze  716:
1.160     schwarze  717:        have_it = 0;
1.83      schwarze  718:        atok = rew_alt(tok);
1.148     schwarze  719:        body = endbody = later = NULL;
1.119     schwarze  720:        for (n = mdoc->last; n; n = n->parent) {
1.161     schwarze  721:                if (n->flags & (MDOC_VALID | MDOC_BREAK))
1.83      schwarze  722:                        continue;
                    723:
                    724:                /* Remember the start of our own body. */
1.151     schwarze  725:
1.150     schwarze  726:                if (n->type == MDOC_BODY && atok == n->tok) {
1.166     schwarze  727:                        if (n->end == ENDBODY_NOT) {
1.83      schwarze  728:                                body = n;
1.166     schwarze  729:                                n->lastline = line;
                    730:                        }
1.83      schwarze  731:                        continue;
                    732:                }
                    733:
1.150     schwarze  734:                if (n->type != MDOC_BLOCK || n->tok == MDOC_Nm)
1.83      schwarze  735:                        continue;
1.160     schwarze  736:
                    737:                if (n->tok == MDOC_It) {
                    738:                        have_it = 1;
                    739:                        continue;
                    740:                }
                    741:
1.83      schwarze  742:                if (atok == n->tok) {
1.166     schwarze  743:                        n->lastline = line;
1.83      schwarze  744:                        assert(body);
                    745:
                    746:                        /*
                    747:                         * Found the start of our own block.
                    748:                         * When there is no pending sub block,
                    749:                         * just proceed to closing out.
                    750:                         */
1.151     schwarze  751:
1.160     schwarze  752:                        if (later == NULL ||
                    753:                            (tok == MDOC_El && !have_it))
1.83      schwarze  754:                                break;
                    755:
1.131     schwarze  756:                        /*
1.167     schwarze  757:                         * When there is a pending sub block, postpone
                    758:                         * closing out the current block until the
                    759:                         * rew_pending() closing out the sub-block.
1.83      schwarze  760:                         */
1.151     schwarze  761:
1.168   ! schwarze  762:                        make_pending(mdoc, n, later, line, ppos);
1.83      schwarze  763:
                    764:                        /*
                    765:                         * Mark the place where the formatting - but not
                    766:                         * the scope - of the current block ends.
                    767:                         */
1.151     schwarze  768:
1.166     schwarze  769:                        endbody = mdoc_endbody_alloc(mdoc, line, ppos,
1.150     schwarze  770:                            atok, body, ENDBODY_SPACE);
1.151     schwarze  771:
                    772:                        /*
                    773:                         * If a block closing macro taking arguments
                    774:                         * breaks another block, put the arguments
1.166     schwarze  775:                         * into the end marker.
1.151     schwarze  776:                         */
                    777:
1.166     schwarze  778:                        if (maxargs)
1.148     schwarze  779:                                mdoc->next = MDOC_NEXT_CHILD;
1.83      schwarze  780:                        break;
                    781:                }
                    782:
                    783:                /*
                    784:                 * When finding an open sub block, remember the last
                    785:                 * open explicit block, or, in case there are only
                    786:                 * implicit ones, the first open implicit block.
                    787:                 */
1.151     schwarze  788:
1.160     schwarze  789:                if (later == NULL ||
                    790:                    ! (mdoc_macros[later->tok].flags & MDOC_EXPLICIT))
1.83      schwarze  791:                        later = n;
                    792:        }
1.166     schwarze  793:
                    794:        if (body == NULL) {
                    795:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, mdoc->parse,
                    796:                    line, ppos, mdoc_macronames[tok]);
                    797:                if (maxargs && endbody == NULL) {
                    798:                        /*
                    799:                         * Stray .Ec without previous .Eo:
                    800:                         * Break the output line, keep the arguments.
                    801:                         */
                    802:                        mdoc_elem_alloc(mdoc, line, ppos, MDOC_br, NULL);
                    803:                        rew_elem(mdoc, MDOC_br);
                    804:                }
                    805:        } else if (endbody == NULL) {
                    806:                rew_last(mdoc, body);
                    807:                if (maxargs)
                    808:                        mdoc_tail_alloc(mdoc, line, ppos, atok);
                    809:        }
1.83      schwarze  810:
1.150     schwarze  811:        if ( ! (mdoc_macros[tok].flags & MDOC_PARSED)) {
                    812:                if (buf[*pos] != '\0')
1.136     schwarze  813:                        mandoc_vmsg(MANDOCERR_ARG_SKIP,
                    814:                            mdoc->parse, line, ppos,
                    815:                            "%s %s", mdoc_macronames[tok],
                    816:                            buf + *pos);
1.167     schwarze  817:                if (endbody == NULL)
                    818:                        rew_pending(mdoc, n);
1.151     schwarze  819:                return;
1.1       kristaps  820:        }
                    821:
1.166     schwarze  822:        if (endbody != NULL)
                    823:                n = endbody;
1.148     schwarze  824:        for (j = 0; ; j++) {
1.1       kristaps  825:                lastarg = *pos;
                    826:
1.166     schwarze  827:                if (j == maxargs && n != NULL) {
                    828:                        rew_pending(mdoc, n);
                    829:                        n = NULL;
1.1       kristaps  830:                }
                    831:
1.119     schwarze  832:                ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.150     schwarze  833:                if (ac == ARGS_PUNCT || ac == ARGS_EOLN)
1.1       kristaps  834:                        break;
                    835:
1.155     schwarze  836:                ntok = ac == ARGS_QWORD ? MDOC_MAX :
                    837:                    lookup(mdoc, tok, line, lastarg, p);
1.54      kristaps  838:
1.150     schwarze  839:                if (ntok == MDOC_MAX) {
                    840:                        dword(mdoc, line, lastarg, p, DELIM_MAX,
                    841:                            MDOC_JOIN & mdoc_macros[tok].flags);
1.54      kristaps  842:                        continue;
                    843:                }
1.1       kristaps  844:
1.166     schwarze  845:                if (n != NULL) {
                    846:                        rew_pending(mdoc, n);
                    847:                        n = NULL;
1.54      kristaps  848:                }
1.122     schwarze  849:                mdoc->flags &= ~MDOC_NEWLINE;
1.151     schwarze  850:                mdoc_macro(mdoc, ntok, line, lastarg, pos, buf);
1.54      kristaps  851:                break;
1.1       kristaps  852:        }
                    853:
1.166     schwarze  854:        if (n != NULL)
                    855:                rew_pending(mdoc, n);
1.150     schwarze  856:        if (nl)
                    857:                append_delims(mdoc, line, pos, buf);
1.1       kristaps  858: }
                    859:
1.151     schwarze  860: static void
1.1       kristaps  861: in_line(MACRO_PROT_ARGS)
                    862: {
1.144     schwarze  863:        int              la, scope, cnt, firstarg, mayopen, nc, nl;
1.53      kristaps  864:        enum mdoct       ntok;
1.55      kristaps  865:        enum margserr    ac;
1.67      schwarze  866:        enum mdelim      d;
1.53      kristaps  867:        struct mdoc_arg *arg;
                    868:        char            *p;
1.1       kristaps  869:
1.119     schwarze  870:        nl = MDOC_NEWLINE & mdoc->flags;
1.60      kristaps  871:
1.4       kristaps  872:        /*
                    873:         * Whether we allow ignored elements (those without content,
                    874:         * usually because of reserved words) to squeak by.
                    875:         */
1.45      kristaps  876:
1.4       kristaps  877:        switch (tok) {
1.131     schwarze  878:        case MDOC_An:
1.26      kristaps  879:                /* FALLTHROUGH */
1.131     schwarze  880:        case MDOC_Ar:
1.4       kristaps  881:                /* FALLTHROUGH */
1.131     schwarze  882:        case MDOC_Fl:
1.4       kristaps  883:                /* FALLTHROUGH */
1.131     schwarze  884:        case MDOC_Mt:
1.12      kristaps  885:                /* FALLTHROUGH */
1.131     schwarze  886:        case MDOC_Nm:
1.26      kristaps  887:                /* FALLTHROUGH */
1.131     schwarze  888:        case MDOC_Pa:
1.4       kristaps  889:                nc = 1;
                    890:                break;
                    891:        default:
                    892:                nc = 0;
                    893:                break;
                    894:        }
                    895:
1.152     schwarze  896:        mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1.1       kristaps  897:
1.144     schwarze  898:        d = DELIM_NONE;
                    899:        firstarg = 1;
1.142     schwarze  900:        mayopen = 1;
1.70      kristaps  901:        for (cnt = scope = 0;; ) {
1.1       kristaps  902:                la = *pos;
1.119     schwarze  903:                ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.1       kristaps  904:
1.144     schwarze  905:                /*
                    906:                 * At the end of a macro line,
                    907:                 * opening delimiters do not suppress spacing.
                    908:                 */
                    909:
                    910:                if (ac == ARGS_EOLN) {
                    911:                        if (d == DELIM_OPEN)
                    912:                                mdoc->last->flags &= ~MDOC_DELIMO;
1.1       kristaps  913:                        break;
1.144     schwarze  914:                }
                    915:
                    916:                /*
                    917:                 * The rest of the macro line is only punctuation,
                    918:                 * to be handled by append_delims().
                    919:                 * If there were no other arguments,
                    920:                 * do not allow the first one to suppress spacing,
                    921:                 * even if it turns out to be a closing one.
                    922:                 */
                    923:
                    924:                if (ac == ARGS_PUNCT) {
                    925:                        if (cnt == 0 && nc == 0)
                    926:                                mdoc->flags |= MDOC_NODELIMC;
1.1       kristaps  927:                        break;
1.144     schwarze  928:                }
1.1       kristaps  929:
1.145     schwarze  930:                ntok = (ac == ARGS_QWORD || (tok == MDOC_Fn && !cnt)) ?
1.155     schwarze  931:                    MDOC_MAX : lookup(mdoc, tok, line, la, p);
1.1       kristaps  932:
1.131     schwarze  933:                /*
1.4       kristaps  934:                 * In this case, we've located a submacro and must
                    935:                 * execute it.  Close out scope, if open.  If no
                    936:                 * elements have been generated, either create one (nc)
                    937:                 * or raise a warning.
                    938:                 */
1.1       kristaps  939:
1.150     schwarze  940:                if (ntok != MDOC_MAX) {
1.149     schwarze  941:                        if (scope)
                    942:                                rew_elem(mdoc, tok);
1.150     schwarze  943:                        if (nc && ! cnt) {
                    944:                                mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1.149     schwarze  945:                                rew_last(mdoc, mdoc->last);
1.150     schwarze  946:                        } else if ( ! nc && ! cnt) {
1.7       kristaps  947:                                mdoc_argv_free(arg);
1.135     schwarze  948:                                mandoc_msg(MANDOCERR_MACRO_EMPTY,
                    949:                                    mdoc->parse, line, ppos,
                    950:                                    mdoc_macronames[tok]);
1.7       kristaps  951:                        }
1.151     schwarze  952:                        mdoc_macro(mdoc, ntok, line, la, pos, buf);
1.150     schwarze  953:                        if (nl)
                    954:                                append_delims(mdoc, line, pos, buf);
1.151     schwarze  955:                        return;
1.131     schwarze  956:                }
1.1       kristaps  957:
1.131     schwarze  958:                /*
1.4       kristaps  959:                 * Non-quote-enclosed punctuation.  Set up our scope, if
                    960:                 * a word; rewind the scope, if a delimiter; then append
1.131     schwarze  961:                 * the word.
1.4       kristaps  962:                 */
1.1       kristaps  963:
1.150     schwarze  964:                d = ac == ARGS_QWORD ? DELIM_NONE : mdoc_isdelim(p);
1.3       kristaps  965:
1.70      kristaps  966:                if (DELIM_NONE != d) {
                    967:                        /*
                    968:                         * If we encounter closing punctuation, no word
1.144     schwarze  969:                         * has been emitted, no scope is open, and we're
1.70      kristaps  970:                         * allowed to have an empty element, then start
1.142     schwarze  971:                         * a new scope.
1.70      kristaps  972:                         */
1.142     schwarze  973:                        if ((d == DELIM_CLOSE ||
                    974:                             (d == DELIM_MIDDLE && tok == MDOC_Fl)) &&
1.144     schwarze  975:                            !cnt && !scope && nc && mayopen) {
1.150     schwarze  976:                                mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1.70      kristaps  977:                                scope = 1;
1.142     schwarze  978:                                cnt++;
1.150     schwarze  979:                                if (tok == MDOC_Nm)
1.142     schwarze  980:                                        mayopen = 0;
1.70      kristaps  981:                        }
                    982:                        /*
                    983:                         * Close out our scope, if one is open, before
                    984:                         * any punctuation.
                    985:                         */
1.149     schwarze  986:                        if (scope)
                    987:                                rew_elem(mdoc, tok);
1.70      kristaps  988:                        scope = 0;
1.145     schwarze  989:                        if (tok == MDOC_Fn)
                    990:                                mayopen = 0;
1.142     schwarze  991:                } else if (mayopen && !scope) {
1.150     schwarze  992:                        mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1.70      kristaps  993:                        scope = 1;
1.142     schwarze  994:                        cnt++;
1.1       kristaps  995:                }
1.105     kristaps  996:
1.150     schwarze  997:                dword(mdoc, line, la, p, d,
                    998:                    MDOC_JOIN & mdoc_macros[tok].flags);
1.39      kristaps  999:
                   1000:                /*
1.144     schwarze 1001:                 * If the first argument is a closing delimiter,
                   1002:                 * do not suppress spacing before it.
                   1003:                 */
                   1004:
                   1005:                if (firstarg && d == DELIM_CLOSE && !nc)
                   1006:                        mdoc->last->flags &= ~MDOC_DELIMC;
                   1007:                firstarg = 0;
                   1008:
                   1009:                /*
1.39      kristaps 1010:                 * `Fl' macros have their scope re-opened with each new
                   1011:                 * word so that the `-' can be added to each one without
                   1012:                 * having to parse out spaces.
                   1013:                 */
1.150     schwarze 1014:                if (scope && tok == MDOC_Fl) {
1.149     schwarze 1015:                        rew_elem(mdoc, tok);
1.70      kristaps 1016:                        scope = 0;
1.39      kristaps 1017:                }
1.1       kristaps 1018:        }
                   1019:
1.149     schwarze 1020:        if (scope)
                   1021:                rew_elem(mdoc, tok);
1.4       kristaps 1022:
                   1023:        /*
                   1024:         * If no elements have been collected and we're allowed to have
                   1025:         * empties (nc), open a scope and close it out.  Otherwise,
                   1026:         * raise a warning.
                   1027:         */
1.45      kristaps 1028:
1.150     schwarze 1029:        if ( ! cnt) {
                   1030:                if (nc) {
                   1031:                        mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
                   1032:                        rew_last(mdoc, mdoc->last);
                   1033:                } else {
                   1034:                        mdoc_argv_free(arg);
                   1035:                        mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse,
                   1036:                            line, ppos, mdoc_macronames[tok]);
                   1037:                }
1.7       kristaps 1038:        }
1.150     schwarze 1039:        if (nl)
                   1040:                append_delims(mdoc, line, pos, buf);
1.1       kristaps 1041: }
                   1042:
1.151     schwarze 1043: static void
1.1       kristaps 1044: blk_full(MACRO_PROT_ARGS)
                   1045: {
1.153     schwarze 1046:        int               la, nl, parsed;
1.1       kristaps 1047:        struct mdoc_arg  *arg;
1.165     schwarze 1048:        struct mdoc_node *blk; /* Our own block. */
                   1049:        struct mdoc_node *head; /* Our own head. */
                   1050:        struct mdoc_node *body; /* Our own body. */
1.47      kristaps 1051:        struct mdoc_node *n;
1.59      kristaps 1052:        enum margserr     ac, lac;
1.1       kristaps 1053:        char             *p;
                   1054:
1.119     schwarze 1055:        nl = MDOC_NEWLINE & mdoc->flags;
1.138     schwarze 1056:
                   1057:        /* Skip items outside lists. */
                   1058:
                   1059:        if (tok == MDOC_It) {
                   1060:                for (n = mdoc->last; n; n = n->parent)
1.158     schwarze 1061:                        if (n->tok == MDOC_Bl && n->type == MDOC_BLOCK &&
                   1062:                            ! (n->flags & (MDOC_VALID | MDOC_BREAK)))
1.138     schwarze 1063:                                break;
                   1064:                if (n == NULL) {
                   1065:                        mandoc_vmsg(MANDOCERR_IT_STRAY, mdoc->parse,
                   1066:                            line, ppos, "It %s", buf + *pos);
1.150     schwarze 1067:                        mdoc_elem_alloc(mdoc, line, ppos, MDOC_br, NULL);
1.149     schwarze 1068:                        rew_elem(mdoc, MDOC_br);
1.151     schwarze 1069:                        return;
1.138     schwarze 1070:                }
                   1071:        }
1.63      kristaps 1072:
1.45      kristaps 1073:        /* Close out prior implicit scope. */
1.19      kristaps 1074:
1.150     schwarze 1075:        if ( ! (mdoc_macros[tok].flags & MDOC_EXPLICIT)) {
                   1076:                rew_sub(MDOC_BODY, mdoc, tok, line, ppos);
                   1077:                rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos);
1.1       kristaps 1078:        }
                   1079:
1.45      kristaps 1080:        /*
1.109     kristaps 1081:         * This routine accommodates implicitly- and explicitly-scoped
1.45      kristaps 1082:         * macro openings.  Implicit ones first close out prior scope
                   1083:         * (seen above).  Delay opening the head until necessary to
                   1084:         * allow leading punctuation to print.  Special consideration
                   1085:         * for `It -column', which has phrase-part syntax instead of
                   1086:         * regular child nodes.
                   1087:         */
                   1088:
1.152     schwarze 1089:        mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1.165     schwarze 1090:        blk = mdoc_block_alloc(mdoc, line, ppos, tok, arg);
1.49      kristaps 1091:        head = body = NULL;
1.115     schwarze 1092:
                   1093:        /*
                   1094:         * Exception: Heads of `It' macros in `-diag' lists are not
                   1095:         * parsed, even though `It' macros in general are parsed.
                   1096:         */
1.151     schwarze 1097:
1.153     schwarze 1098:        parsed = tok != MDOC_It ||
                   1099:            mdoc->last->parent->tok != MDOC_Bl ||
                   1100:            mdoc->last->parent->norm->Bl.type != LIST_diag;
1.1       kristaps 1101:
1.45      kristaps 1102:        /*
                   1103:         * The `Nd' macro has all arguments in its body: it's a hybrid
                   1104:         * of block partial-explicit and full-implicit.  Stupid.
                   1105:         */
1.19      kristaps 1106:
1.150     schwarze 1107:        if (tok == MDOC_Nd) {
                   1108:                head = mdoc_head_alloc(mdoc, line, ppos, tok);
1.162     schwarze 1109:                rew_last(mdoc, head);
1.150     schwarze 1110:                body = mdoc_body_alloc(mdoc, line, ppos, tok);
1.123     schwarze 1111:        }
                   1112:
1.150     schwarze 1113:        if (tok == MDOC_Bk)
1.123     schwarze 1114:                mdoc->flags |= MDOC_KEEP;
1.19      kristaps 1115:
1.150     schwarze 1116:        ac = ARGS_PEND;
                   1117:        for (;;) {
1.45      kristaps 1118:                la = *pos;
1.150     schwarze 1119:                lac = ac;
1.119     schwarze 1120:                ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.150     schwarze 1121:                if (ac == ARGS_PUNCT)
1.89      schwarze 1122:                        break;
1.150     schwarze 1123:                if (ac == ARGS_EOLN) {
                   1124:                        if (lac != ARGS_PPHRASE && lac != ARGS_PHRASE)
1.75      kristaps 1125:                                break;
                   1126:                        /*
                   1127:                         * This is necessary: if the last token on a
                   1128:                         * line is a `Ta' or tab, then we'll get
                   1129:                         * ARGS_EOLN, so we must be smart enough to
                   1130:                         * reopen our scope if the last parse was a
                   1131:                         * phrase or partial phrase.
                   1132:                         */
1.167     schwarze 1133:                        if (body != NULL)
                   1134:                                rew_last(mdoc, body);
1.150     schwarze 1135:                        body = mdoc_body_alloc(mdoc, line, ppos, tok);
1.1       kristaps 1136:                        break;
1.75      kristaps 1137:                }
1.45      kristaps 1138:
1.131     schwarze 1139:                /*
1.73      kristaps 1140:                 * Emit leading punctuation (i.e., punctuation before
                   1141:                 * the MDOC_HEAD) for non-phrase types.
                   1142:                 */
1.45      kristaps 1143:
1.150     schwarze 1144:                if (head == NULL &&
                   1145:                    ac != ARGS_PEND &&
                   1146:                    ac != ARGS_PHRASE &&
                   1147:                    ac != ARGS_PPHRASE &&
                   1148:                    ac != ARGS_QWORD &&
                   1149:                    mdoc_isdelim(p) == DELIM_OPEN) {
                   1150:                        dword(mdoc, line, la, p, DELIM_OPEN, 0);
1.45      kristaps 1151:                        continue;
                   1152:                }
                   1153:
1.74      kristaps 1154:                /* Open a head if one hasn't been opened. */
1.45      kristaps 1155:
1.150     schwarze 1156:                if (head == NULL)
                   1157:                        head = mdoc_head_alloc(mdoc, line, ppos, tok);
1.45      kristaps 1158:
1.150     schwarze 1159:                if (ac == ARGS_PHRASE ||
                   1160:                    ac == ARGS_PEND ||
                   1161:                    ac == ARGS_PPHRASE) {
1.151     schwarze 1162:
1.73      kristaps 1163:                        /*
1.74      kristaps 1164:                         * If we haven't opened a body yet, rewind the
                   1165:                         * head; if we have, rewind that instead.
                   1166:                         */
                   1167:
1.167     schwarze 1168:                        rew_last(mdoc, body == NULL ? head : body);
1.150     schwarze 1169:                        body = mdoc_body_alloc(mdoc, line, ppos, tok);
1.74      kristaps 1170:
                   1171:                        /*
1.73      kristaps 1172:                         * Process phrases: set whether we're in a
                   1173:                         * partial-phrase (this effects line handling)
                   1174:                         * then call down into the phrase parser.
                   1175:                         */
1.74      kristaps 1176:
1.150     schwarze 1177:                        if (ac == ARGS_PPHRASE)
1.119     schwarze 1178:                                mdoc->flags |= MDOC_PPHRASE;
1.150     schwarze 1179:                        if (ac == ARGS_PEND && lac == ARGS_PPHRASE)
1.119     schwarze 1180:                                mdoc->flags |= MDOC_PPHRASE;
1.154     schwarze 1181:                        parse_rest(mdoc, MDOC_MAX, line, &la, buf);
1.119     schwarze 1182:                        mdoc->flags &= ~MDOC_PPHRASE;
1.1       kristaps 1183:                        continue;
                   1184:                }
                   1185:
1.153     schwarze 1186:                if (macro_or_word(mdoc, tok, line, la, pos, buf, parsed))
1.151     schwarze 1187:                        break;
1.45      kristaps 1188:        }
1.1       kristaps 1189:
1.165     schwarze 1190:        if (blk->flags & MDOC_VALID)
                   1191:                return;
1.150     schwarze 1192:        if (head == NULL)
                   1193:                head = mdoc_head_alloc(mdoc, line, ppos, tok);
                   1194:        if (nl)
                   1195:                append_delims(mdoc, line, pos, buf);
1.151     schwarze 1196:        if (body != NULL)
1.77      kristaps 1197:                goto out;
1.19      kristaps 1198:
1.47      kristaps 1199:        /*
1.49      kristaps 1200:         * If there is an open (i.e., unvalidated) sub-block requiring
                   1201:         * explicit close-out, postpone switching the current block from
1.167     schwarze 1202:         * head to body until the rew_pending() call closing out that
1.49      kristaps 1203:         * sub-block.
1.47      kristaps 1204:         */
1.119     schwarze 1205:        for (n = mdoc->last; n && n != head; n = n->parent) {
1.151     schwarze 1206:                if (n->type == MDOC_BLOCK &&
                   1207:                    mdoc_macros[n->tok].flags & MDOC_EXPLICIT &&
                   1208:                    ! (n->flags & MDOC_VALID)) {
1.47      kristaps 1209:                        n->pending = head;
1.151     schwarze 1210:                        return;
1.47      kristaps 1211:                }
                   1212:        }
                   1213:
1.45      kristaps 1214:        /* Close out scopes to remain in a consistent state. */
                   1215:
1.167     schwarze 1216:        rew_last(mdoc, head);
                   1217:        body = mdoc_body_alloc(mdoc, line, ppos, tok);
1.77      kristaps 1218: out:
1.150     schwarze 1219:        if (mdoc->flags & MDOC_FREECOL) {
1.167     schwarze 1220:                rew_last(mdoc, body);
                   1221:                rew_last(mdoc, blk);
1.150     schwarze 1222:                mdoc->flags &= ~MDOC_FREECOL;
                   1223:        }
1.1       kristaps 1224: }
                   1225:
1.151     schwarze 1226: static void
1.1       kristaps 1227: blk_part_imp(MACRO_PROT_ARGS)
                   1228: {
1.63      kristaps 1229:        int               la, nl;
1.55      kristaps 1230:        enum margserr     ac;
1.1       kristaps 1231:        char             *p;
1.43      kristaps 1232:        struct mdoc_node *blk; /* saved block context */
                   1233:        struct mdoc_node *body; /* saved body context */
                   1234:        struct mdoc_node *n;
1.1       kristaps 1235:
1.119     schwarze 1236:        nl = MDOC_NEWLINE & mdoc->flags;
1.63      kristaps 1237:
1.43      kristaps 1238:        /*
                   1239:         * A macro that spans to the end of the line.  This is generally
                   1240:         * (but not necessarily) called as the first macro.  The block
                   1241:         * has a head as the immediate child, which is always empty,
                   1242:         * followed by zero or more opening punctuation nodes, then the
                   1243:         * body (which may be empty, depending on the macro), then zero
                   1244:         * or more closing punctuation nodes.
                   1245:         */
1.32      kristaps 1246:
1.150     schwarze 1247:        blk = mdoc_block_alloc(mdoc, line, ppos, tok, NULL);
1.162     schwarze 1248:        rew_last(mdoc, mdoc_head_alloc(mdoc, line, ppos, tok));
1.1       kristaps 1249:
1.43      kristaps 1250:        /*
                   1251:         * Open the body scope "on-demand", that is, after we've
                   1252:         * processed all our the leading delimiters (open parenthesis,
                   1253:         * etc.).
                   1254:         */
1.1       kristaps 1255:
1.43      kristaps 1256:        for (body = NULL; ; ) {
1.32      kristaps 1257:                la = *pos;
1.119     schwarze 1258:                ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.150     schwarze 1259:                if (ac == ARGS_EOLN || ac == ARGS_PUNCT)
1.1       kristaps 1260:                        break;
                   1261:
1.150     schwarze 1262:                if (body == NULL && ac != ARGS_QWORD &&
                   1263:                    mdoc_isdelim(p) == DELIM_OPEN) {
                   1264:                        dword(mdoc, line, la, p, DELIM_OPEN, 0);
1.1       kristaps 1265:                        continue;
1.123     schwarze 1266:                }
1.1       kristaps 1267:
1.150     schwarze 1268:                if (body == NULL)
                   1269:                        body = mdoc_body_alloc(mdoc, line, ppos, tok);
1.43      kristaps 1270:
1.153     schwarze 1271:                if (macro_or_word(mdoc, tok, line, la, pos, buf, 1))
1.151     schwarze 1272:                        break;
1.1       kristaps 1273:        }
1.150     schwarze 1274:        if (body == NULL)
                   1275:                body = mdoc_body_alloc(mdoc, line, ppos, tok);
1.43      kristaps 1276:
1.83      schwarze 1277:        /*
                   1278:         * If there is an open sub-block requiring explicit close-out,
1.167     schwarze 1279:         * postpone closing out the current block until the
                   1280:         * rew_pending() call closing out the sub-block.
1.83      schwarze 1281:         */
1.151     schwarze 1282:
1.119     schwarze 1283:        for (n = mdoc->last; n && n != body && n != blk->parent;
1.131     schwarze 1284:             n = n->parent) {
1.150     schwarze 1285:                if (n->type == MDOC_BLOCK &&
                   1286:                    mdoc_macros[n->tok].flags & MDOC_EXPLICIT &&
                   1287:                    ! (n->flags & MDOC_VALID)) {
1.168   ! schwarze 1288:                        make_pending(mdoc, blk, n, line, ppos);
1.150     schwarze 1289:                        mdoc_endbody_alloc(mdoc, line, ppos,
                   1290:                            tok, body, ENDBODY_NOSPACE);
1.151     schwarze 1291:                        return;
1.83      schwarze 1292:                }
                   1293:        }
1.134     schwarze 1294:        assert(n == body);
1.164     schwarze 1295:        rew_last(mdoc, body);
1.150     schwarze 1296:        if (nl)
                   1297:                append_delims(mdoc, line, pos, buf);
1.166     schwarze 1298:        rew_pending(mdoc, blk);
1.117     schwarze 1299:
                   1300:        /* Move trailing .Ns out of scope. */
                   1301:
                   1302:        for (n = body->child; n && n->next; n = n->next)
                   1303:                /* Do nothing. */ ;
1.150     schwarze 1304:        if (n && n->tok == MDOC_Ns)
1.119     schwarze 1305:                mdoc_node_relink(mdoc, n);
1.1       kristaps 1306: }
                   1307:
1.151     schwarze 1308: static void
1.1       kristaps 1309: blk_part_exp(MACRO_PROT_ARGS)
                   1310: {
1.60      kristaps 1311:        int               la, nl;
1.55      kristaps 1312:        enum margserr     ac;
1.43      kristaps 1313:        struct mdoc_node *head; /* keep track of head */
1.1       kristaps 1314:        char             *p;
                   1315:
1.119     schwarze 1316:        nl = MDOC_NEWLINE & mdoc->flags;
1.60      kristaps 1317:
1.43      kristaps 1318:        /*
                   1319:         * The opening of an explicit macro having zero or more leading
                   1320:         * punctuation nodes; a head with optional single element (the
                   1321:         * case of `Eo'); and a body that may be empty.
                   1322:         */
1.32      kristaps 1323:
1.150     schwarze 1324:        mdoc_block_alloc(mdoc, line, ppos, tok, NULL);
1.163     schwarze 1325:        head = NULL;
                   1326:        for (;;) {
1.32      kristaps 1327:                la = *pos;
1.119     schwarze 1328:                ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.150     schwarze 1329:                if (ac == ARGS_PUNCT || ac == ARGS_EOLN)
1.1       kristaps 1330:                        break;
1.43      kristaps 1331:
                   1332:                /* Flush out leading punctuation. */
                   1333:
1.150     schwarze 1334:                if (head == NULL && ac != ARGS_QWORD &&
                   1335:                    mdoc_isdelim(p) == DELIM_OPEN) {
                   1336:                        dword(mdoc, line, la, p, DELIM_OPEN, 0);
1.43      kristaps 1337:                        continue;
1.123     schwarze 1338:                }
1.43      kristaps 1339:
1.150     schwarze 1340:                if (head == NULL) {
                   1341:                        head = mdoc_head_alloc(mdoc, line, ppos, tok);
1.163     schwarze 1342:                        if (tok == MDOC_Eo)  /* Not parsed. */
1.150     schwarze 1343:                                dword(mdoc, line, la, p, DELIM_MAX, 0);
1.163     schwarze 1344:                        rew_last(mdoc, head);
                   1345:                        mdoc_body_alloc(mdoc, line, ppos, tok);
1.150     schwarze 1346:                        if (tok == MDOC_Eo)
1.43      kristaps 1347:                                continue;
1.1       kristaps 1348:                }
1.43      kristaps 1349:
1.153     schwarze 1350:                if (macro_or_word(mdoc, tok, line, la, pos, buf, 1))
1.151     schwarze 1351:                        break;
1.1       kristaps 1352:        }
                   1353:
1.43      kristaps 1354:        /* Clean-up to leave in a consistent state. */
1.32      kristaps 1355:
1.163     schwarze 1356:        if (head == NULL) {
                   1357:                rew_last(mdoc, mdoc_head_alloc(mdoc, line, ppos, tok));
1.150     schwarze 1358:                mdoc_body_alloc(mdoc, line, ppos, tok);
1.1       kristaps 1359:        }
1.150     schwarze 1360:        if (nl)
                   1361:                append_delims(mdoc, line, pos, buf);
1.1       kristaps 1362: }
                   1363:
1.151     schwarze 1364: static void
1.1       kristaps 1365: in_line_argn(MACRO_PROT_ARGS)
                   1366: {
1.60      kristaps 1367:        int              la, flushed, j, maxargs, nl;
1.55      kristaps 1368:        enum margserr    ac;
1.53      kristaps 1369:        struct mdoc_arg *arg;
                   1370:        char            *p;
                   1371:        enum mdoct       ntok;
1.1       kristaps 1372:
1.156     schwarze 1373:        nl = mdoc->flags & MDOC_NEWLINE;
1.60      kristaps 1374:
1.46      kristaps 1375:        /*
                   1376:         * A line macro that has a fixed number of arguments (maxargs).
                   1377:         * Only open the scope once the first non-leading-punctuation is
                   1378:         * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
                   1379:         * keep it open until the maximum number of arguments are
                   1380:         * exhausted.
                   1381:         */
1.1       kristaps 1382:
                   1383:        switch (tok) {
1.131     schwarze 1384:        case MDOC_Ap:
1.1       kristaps 1385:                /* FALLTHROUGH */
1.131     schwarze 1386:        case MDOC_Ns:
1.1       kristaps 1387:                /* FALLTHROUGH */
1.131     schwarze 1388:        case MDOC_Ux:
1.1       kristaps 1389:                maxargs = 0;
                   1390:                break;
1.131     schwarze 1391:        case MDOC_Bx:
1.101     kristaps 1392:                /* FALLTHROUGH */
1.132     schwarze 1393:        case MDOC_Es:
                   1394:                /* FALLTHROUGH */
1.131     schwarze 1395:        case MDOC_Xr:
1.42      kristaps 1396:                maxargs = 2;
                   1397:                break;
1.1       kristaps 1398:        default:
                   1399:                maxargs = 1;
                   1400:                break;
                   1401:        }
                   1402:
1.152     schwarze 1403:        mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1.1       kristaps 1404:
1.156     schwarze 1405:        p = NULL;
                   1406:        flushed = j = 0;
                   1407:        for (;;) {
1.32      kristaps 1408:                la = *pos;
1.119     schwarze 1409:                ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.156     schwarze 1410:                if (ac == ARGS_PUNCT || ac == ARGS_EOLN) {
                   1411:                        if (j < 2 && tok == MDOC_Pf)
                   1412:                                mandoc_vmsg(MANDOCERR_PF_SKIP,
                   1413:                                    mdoc->parse, line, ppos, "Pf %s",
                   1414:                                    p == NULL ? "at eol" : p);
1.1       kristaps 1415:                        break;
1.156     schwarze 1416:                }
1.1       kristaps 1417:
1.150     schwarze 1418:                if ( ! (mdoc_macros[tok].flags & MDOC_IGNDELIM) &&
                   1419:                    ac != ARGS_QWORD && j == 0 &&
                   1420:                    mdoc_isdelim(p) == DELIM_OPEN) {
                   1421:                        dword(mdoc, line, la, p, DELIM_OPEN, 0);
1.46      kristaps 1422:                        continue;
1.150     schwarze 1423:                } else if (j == 0)
                   1424:                       mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1.46      kristaps 1425:
                   1426:                if (j == maxargs && ! flushed) {
1.149     schwarze 1427:                        rew_elem(mdoc, tok);
1.46      kristaps 1428:                        flushed = 1;
                   1429:                }
                   1430:
1.156     schwarze 1431:                ntok = (ac == ARGS_QWORD || (tok == MDOC_Pf && j == 0)) ?
                   1432:                    MDOC_MAX : lookup(mdoc, tok, line, la, p);
1.54      kristaps 1433:
1.150     schwarze 1434:                if (ntok != MDOC_MAX) {
1.149     schwarze 1435:                        if ( ! flushed)
                   1436:                                rew_elem(mdoc, tok);
1.1       kristaps 1437:                        flushed = 1;
1.151     schwarze 1438:                        mdoc_macro(mdoc, ntok, line, la, pos, buf);
1.46      kristaps 1439:                        j++;
1.1       kristaps 1440:                        break;
                   1441:                }
                   1442:
1.150     schwarze 1443:                if ( ! (mdoc_macros[tok].flags & MDOC_IGNDELIM) &&
                   1444:                    ac != ARGS_QWORD && ! flushed &&
                   1445:                    mdoc_isdelim(p) != DELIM_NONE) {
1.149     schwarze 1446:                        rew_elem(mdoc, tok);
1.1       kristaps 1447:                        flushed = 1;
                   1448:                }
1.42      kristaps 1449:
1.150     schwarze 1450:                dword(mdoc, line, la, p, DELIM_MAX,
                   1451:                    MDOC_JOIN & mdoc_macros[tok].flags);
1.46      kristaps 1452:                j++;
1.1       kristaps 1453:        }
                   1454:
1.156     schwarze 1455:        if (j == 0) {
1.150     schwarze 1456:                mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1.156     schwarze 1457:                if (ac == ARGS_PUNCT && tok == MDOC_Pf)
                   1458:                        append_delims(mdoc, line, pos, buf);
                   1459:        }
1.149     schwarze 1460:        if ( ! flushed)
                   1461:                rew_elem(mdoc, tok);
1.150     schwarze 1462:        if (nl)
                   1463:                append_delims(mdoc, line, pos, buf);
1.1       kristaps 1464: }
                   1465:
1.151     schwarze 1466: static void
1.1       kristaps 1467: in_line_eoln(MACRO_PROT_ARGS)
                   1468: {
1.53      kristaps 1469:        struct mdoc_arg *arg;
1.1       kristaps 1470:
1.90      schwarze 1471:        if (tok == MDOC_Pp)
1.119     schwarze 1472:                rew_sub(MDOC_BLOCK, mdoc, MDOC_Nm, line, ppos);
1.90      schwarze 1473:
1.152     schwarze 1474:        mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1.150     schwarze 1475:        mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1.154     schwarze 1476:        if (parse_rest(mdoc, tok, line, pos, buf))
                   1477:                return;
                   1478:        rew_elem(mdoc, tok);
                   1479: }
                   1480:
                   1481: /*
                   1482:  * The simplest argument parser available: Parse the remaining
                   1483:  * words until the end of the phrase or line and return 0
                   1484:  * or until the next macro, call that macro, and return 1.
                   1485:  */
                   1486: static int
                   1487: parse_rest(struct mdoc *mdoc, enum mdoct tok, int line, int *pos, char *buf)
                   1488: {
                   1489:        int              la;
1.1       kristaps 1490:
                   1491:        for (;;) {
                   1492:                la = *pos;
1.153     schwarze 1493:                if (mdoc_args(mdoc, line, pos, buf, tok, NULL) == ARGS_EOLN)
1.154     schwarze 1494:                        return(0);
1.153     schwarze 1495:                if (macro_or_word(mdoc, tok, line, la, pos, buf, 1))
1.154     schwarze 1496:                        return(1);
1.1       kristaps 1497:        }
                   1498: }
                   1499:
1.151     schwarze 1500: static void
1.41      kristaps 1501: ctx_synopsis(MACRO_PROT_ARGS)
                   1502: {
1.60      kristaps 1503:
1.151     schwarze 1504:        if (~mdoc->flags & (MDOC_SYNOPSIS | MDOC_NEWLINE))
                   1505:                in_line(mdoc, tok, line, ppos, pos, buf);
                   1506:        else if (tok == MDOC_Nm)
                   1507:                blk_full(mdoc, tok, line, ppos, pos, buf);
                   1508:        else {
                   1509:                assert(tok == MDOC_Vt);
                   1510:                blk_part_imp(mdoc, tok, line, ppos, pos, buf);
                   1511:        }
1.1       kristaps 1512: }
                   1513:
1.24      kristaps 1514: /*
                   1515:  * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
                   1516:  * They're unusual because they're basically free-form text until a
                   1517:  * macro is encountered.
                   1518:  */
1.151     schwarze 1519: static void
1.75      kristaps 1520: phrase_ta(MACRO_PROT_ARGS)
                   1521: {
1.167     schwarze 1522:        struct mdoc_node *body, *n;
1.75      kristaps 1523:
1.121     schwarze 1524:        /* Make sure we are in a column list or ignore this macro. */
1.151     schwarze 1525:
1.167     schwarze 1526:        body = NULL;
                   1527:        for (n = mdoc->last; n != NULL; n = n->parent) {
                   1528:                if (n->flags & (MDOC_VALID | MDOC_BREAK))
                   1529:                        continue;
                   1530:                if (n->tok == MDOC_It && n->type == MDOC_BODY)
                   1531:                        body = n;
                   1532:                if (n->tok == MDOC_Bl)
                   1533:                        break;
                   1534:        }
                   1535:
1.151     schwarze 1536:        if (n == NULL || n->norm->Bl.type != LIST_column) {
1.137     schwarze 1537:                mandoc_msg(MANDOCERR_TA_STRAY, mdoc->parse,
1.139     schwarze 1538:                    line, ppos, "Ta");
1.151     schwarze 1539:                return;
1.121     schwarze 1540:        }
1.75      kristaps 1541:
1.121     schwarze 1542:        /* Advance to the next column. */
1.150     schwarze 1543:
1.167     schwarze 1544:        rew_last(mdoc, body);
1.150     schwarze 1545:        mdoc_body_alloc(mdoc, line, ppos, MDOC_It);
1.154     schwarze 1546:        parse_rest(mdoc, MDOC_MAX, line, pos, buf);
1.75      kristaps 1547: }

CVSweb