-static int scope_rewind_imp(struct mdoc *, int, int);
-static int scope_rewind_exp(struct mdoc *, int, int, int);
-static int scope_rewind_line(struct mdoc *, int, int);
-static int append_text(struct mdoc *, int,
- int, int, char *[]);
-static int append_text_argv(struct mdoc *, int, int,
- int, char *[], int, const struct mdoc_arg *);
-static int append_delims(struct mdoc *, int, int *, char *);
+
+static int rewind_elem(struct mdoc *, int);
+static int rewind_impblock(struct mdoc *, int, int, int);
+static int rewind_expblock(struct mdoc *, int, int, int);
+static int rewind_subblock(enum mdoc_type, struct mdoc *, int);
+static int rewind_last(int, int,
+ struct mdoc *, struct mdoc_node *);
+static int append_delims(struct mdoc *,
+ int, int, int *, char *);
+static int lookup(struct mdoc *, int, int, int, const char *);
+
+
+static int
+lookup(struct mdoc *mdoc, int line, int pos, int from, const char *p)
+{
+ int res;
+
+ res = mdoc_find(mdoc, p);
+ if (MDOC_PARSED & mdoc_macros[from].flags)
+ return(res);
+ if (MDOC_MAX == res)
+ return(res);
+
+ if ( ! mdoc_pwarn(mdoc, line, pos, WARN_SYNTAX, "macro-like parameter"))
+ return(-1);
+ return(MDOC_MAX);
+}
+
+
+static int
+rewind_last(int tok, int type, struct mdoc *mdoc, struct mdoc_node *to)
+{
+
+ assert(to);
+ mdoc->next = MDOC_NEXT_SIBLING;
+ if (mdoc->last == to) {
+ if ( ! mdoc_valid_post(mdoc))
+ return(0);
+ if ( ! mdoc_action_post(mdoc))
+ return(0);
+ mdoc_msg(mdoc, "rewound %s %s to %s %s",
+ mdoc_type2a(type),
+ mdoc_macronames[tok],
+ mdoc_type2a(mdoc->last->type),
+ mdoc_macronames[mdoc->last->tok]);
+ return(1);
+ }
+
+ do {
+ mdoc->last = mdoc->last->parent;
+ assert(mdoc->last);
+ if ( ! mdoc_valid_post(mdoc))
+ return(0);
+ if ( ! mdoc_action_post(mdoc))
+ return(0);
+ mdoc_msg(mdoc, "rewound %s %s to %s %s",
+ mdoc_type2a(type),
+ mdoc_macronames[tok],
+ mdoc_type2a(mdoc->last->type),
+ mdoc_macronames[mdoc->last->tok]);
+ } while (mdoc->last != to);
+
+ return(1);
+}
+
+
+static int
+rewind_alt(int tok)
+{
+ switch (tok) {
+ case (MDOC_Ac):
+ return(MDOC_Ao);
+ case (MDOC_Bc):
+ return(MDOC_Bo);
+ case (MDOC_Dc):
+ return(MDOC_Do);
+ case (MDOC_Ec):
+ return(MDOC_Eo);
+ case (MDOC_Ed):
+ return(MDOC_Bd);
+ case (MDOC_Ef):
+ return(MDOC_Bf);
+ case (MDOC_Ek):
+ return(MDOC_Bk);
+ case (MDOC_El):
+ return(MDOC_Bl);
+ case (MDOC_Fc):
+ return(MDOC_Fo);
+ case (MDOC_Oc):
+ return(MDOC_Oo);
+ case (MDOC_Pc):
+ return(MDOC_Po);
+ case (MDOC_Qc):
+ return(MDOC_Qo);
+ case (MDOC_Re):
+ return(MDOC_Rs);
+ case (MDOC_Sc):
+ return(MDOC_So);
+ case (MDOC_Xc):
+ return(MDOC_Xo);
+ default:
+ break;
+ }
+ abort();
+ /* NOTREACHED */
+}
+
+
+static int
+rewind_dohalt(int tok, enum mdoc_type type, const struct mdoc_node *p)
+{
+
+ if (MDOC_ROOT == p->type)
+ return(REWIND_HALT);
+ if (MDOC_TEXT == p->type)
+ return(REWIND_NOHALT);
+ if (MDOC_ELEM == p->type)
+ return(REWIND_NOHALT);
+
+ switch (tok) {
+ /* One-liner implicit-scope. */
+ case (MDOC_Aq):
+ /* FALLTHROUGH */
+ case (MDOC_Bq):
+ /* FALLTHROUGH */
+ case (MDOC_D1):
+ /* FALLTHROUGH */
+ case (MDOC_Dl):
+ /* FALLTHROUGH */
+ case (MDOC_Dq):
+ /* FALLTHROUGH */
+ case (MDOC_Op):
+ /* FALLTHROUGH */
+ case (MDOC_Pq):
+ /* FALLTHROUGH */
+ case (MDOC_Ql):
+ /* FALLTHROUGH */
+ case (MDOC_Qq):
+ /* FALLTHROUGH */
+ case (MDOC_Sq):
+ assert(MDOC_BODY != type);
+ assert(MDOC_TAIL != type);
+ if (type == p->type && tok == p->tok)
+ return(REWIND_REWIND);
+ break;
+
+ /* Multi-line implicit-scope. */
+ case (MDOC_It):
+ assert(MDOC_TAIL != type);
+ if (type == p->type && tok == p->tok)
+ return(REWIND_REWIND);
+ if (MDOC_BODY == p->type && MDOC_Bl == p->tok)
+ return(REWIND_HALT);
+ break;
+ case (MDOC_Sh):
+ if (type == p->type && tok == p->tok)
+ return(REWIND_REWIND);
+ break;
+ case (MDOC_Ss):
+ assert(MDOC_TAIL != type);
+ if (type == p->type && tok == p->tok)
+ return(REWIND_REWIND);
+ if (MDOC_BODY == p->type && MDOC_Sh == p->tok)
+ return(REWIND_HALT);
+ break;
+
+ /* Multi-line explicit scope start. */
+ case (MDOC_Ao):
+ /* FALLTHROUGH */
+ case (MDOC_Bd):
+ /* FALLTHROUGH */
+ case (MDOC_Bf):
+ /* FALLTHROUGH */
+ case (MDOC_Bk):
+ /* FALLTHROUGH */
+ case (MDOC_Bl):
+ /* FALLTHROUGH */
+ case (MDOC_Bo):
+ /* FALLTHROUGH */
+ case (MDOC_Do):
+ /* FALLTHROUGH */
+ case (MDOC_Eo):
+ /* FALLTHROUGH */
+ case (MDOC_Fo):
+ /* FALLTHROUGH */
+ case (MDOC_Oo):
+ /* FALLTHROUGH */
+ case (MDOC_Po):
+ /* FALLTHROUGH */
+ case (MDOC_Qo):
+ /* FALLTHROUGH */
+ case (MDOC_Rs):
+ /* FALLTHROUGH */
+ case (MDOC_So):
+ /* FALLTHROUGH */
+ case (MDOC_Xo):
+ if (type == p->type && tok == p->tok)
+ return(REWIND_REWIND);
+ break;
+
+ /* Multi-line explicit scope close. */
+ case (MDOC_Ac):
+ /* FALLTHROUGH */
+ case (MDOC_Bc):
+ /* FALLTHROUGH */
+ case (MDOC_Dc):
+ /* FALLTHROUGH */
+ case (MDOC_Ec):
+ /* FALLTHROUGH */
+ case (MDOC_Ed):
+ /* FALLTHROUGH */
+ case (MDOC_Ek):
+ /* FALLTHROUGH */
+ case (MDOC_El):
+ /* FALLTHROUGH */
+ case (MDOC_Fc):
+ /* FALLTHROUGH */
+ case (MDOC_Ef):
+ /* FALLTHROUGH */
+ case (MDOC_Oc):
+ /* FALLTHROUGH */
+ case (MDOC_Pc):
+ /* FALLTHROUGH */
+ case (MDOC_Qc):
+ /* FALLTHROUGH */
+ case (MDOC_Re):
+ /* FALLTHROUGH */
+ case (MDOC_Sc):
+ /* FALLTHROUGH */
+ case (MDOC_Xc):
+ if (type == p->type && rewind_alt(tok) == p->tok)
+ return(REWIND_REWIND);
+ break;
+ default:
+ abort();
+ /* NOTREACHED */
+ }
+
+ return(REWIND_NOHALT);
+}
+
+
+static int
+rewind_dobreak(int tok, enum mdoc_type type, const struct mdoc_node *p)
+{
+
+ assert(MDOC_ROOT != p->type);
+ if (MDOC_ELEM == p->type)
+ return(1);
+ if (MDOC_TEXT == p->type)
+ return(1);
+
+ switch (tok) {
+ /* Implicit rules. */
+ case (MDOC_It):
+ return(MDOC_It == p->tok);
+ case (MDOC_Ss):
+ return(MDOC_Ss == p->tok);
+ case (MDOC_Sh):
+ if (MDOC_Ss == p->tok)
+ return(1);
+ return(MDOC_Sh == p->tok);
+
+ /* Extra scope rules. */
+ case (MDOC_El):
+ if (MDOC_It == p->tok)
+ return(1);
+ break;
+ default:
+ break;
+ }
+
+ if (MDOC_EXPLICIT & mdoc_macros[tok].flags)
+ return(p->tok == rewind_alt(tok));
+ else if (MDOC_BLOCK == p->type)
+ return(1);
+
+ return(tok == p->tok);
+}