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

Annotation of mandoc/mdoc_macro.c, Revision 1.171

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

CVSweb