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

Annotation of mandoc/mdoc_macro.c, Revision 1.169

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

CVSweb