+ 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);
+}
+
+
+static int
+rewind_elem(struct mdoc *mdoc, int tok)
+{
+ struct mdoc_node *n;
+
+ n = mdoc->last;
+ if (MDOC_ELEM != n->type)
+ n = n->parent;
+ assert(MDOC_ELEM == n->type);
+ assert(tok == n->tok);
+
+ return(rewind_last(tok, MDOC_ELEM, mdoc, n));
+}
+
+
+static int
+rewind_subblock(enum mdoc_type type, struct mdoc *mdoc, int tok)
+{
+ struct mdoc_node *n;
+ int c;
+
+ c = rewind_dohalt(tok, type, mdoc->last);
+ if (REWIND_HALT == c)
+ return(1);
+ if (REWIND_REWIND == c)
+ return(rewind_last(tok, type, mdoc, mdoc->last));
+
+ /* LINTED */
+ for (n = mdoc->last->parent; n; n = n->parent) {
+ c = rewind_dohalt(tok, type, n);
+ if (REWIND_HALT == c)
+ return(1);
+ if (REWIND_REWIND == c)
+ break;
+ else if (rewind_dobreak(tok, type, n))
+ continue;
+ return(mdoc_nerr(mdoc, n, "body scope broken"));
+ }
+
+ assert(n);
+ return(rewind_last(tok, type, mdoc, n));
+}
+
+
+static int
+rewind_expblock(struct mdoc *mdoc, int tok, int line, int ppos)
+{
+ struct mdoc_node *n;
+ int c;
+
+ c = rewind_dohalt(tok, MDOC_BLOCK, mdoc->last);
+ if (REWIND_HALT == c)
+ return(mdoc_perr(mdoc, line, ppos, "closing macro has no context"));
+ if (REWIND_REWIND == c)
+ return(rewind_last(tok, MDOC_BLOCK, mdoc, mdoc->last));
+
+ /* LINTED */
+ for (n = mdoc->last->parent; n; n = n->parent) {
+ c = rewind_dohalt(tok, MDOC_BLOCK, n);
+ if (REWIND_HALT == c)
+ return(mdoc_perr(mdoc, line, ppos, "closing macro has no context"));
+ if (REWIND_REWIND == c)
+ break;
+ else if (rewind_dobreak(tok, MDOC_BLOCK, n))
+ continue;
+ return(mdoc_nerr(mdoc, n, "block scope broken"));
+ }
+
+ assert(n);
+ return(rewind_last(tok, MDOC_BLOCK, mdoc, n));
+}
+
+
+static int
+rewind_impblock(struct mdoc *mdoc, int tok, int line, int ppos)
+{
+ struct mdoc_node *n;
+ int c;
+
+ c = rewind_dohalt(tok, MDOC_BLOCK, mdoc->last);
+ if (REWIND_HALT == c)
+ return(1);
+ if (REWIND_REWIND == c)
+ return(rewind_last(tok, MDOC_BLOCK, mdoc, mdoc->last));
+
+ /* LINTED */
+ for (n = mdoc->last->parent; n; n = n->parent) {
+ c = rewind_dohalt(tok, MDOC_BLOCK, n);
+ if (REWIND_HALT == c)
+ return(1);
+ else if (REWIND_REWIND == c)
+ break;
+ else if (rewind_dobreak(tok, MDOC_BLOCK, n))
+ continue;
+ return(mdoc_nerr(mdoc, n, "block scope broken"));
+ }
+
+ assert(n);
+ return(rewind_last(tok, MDOC_BLOCK, mdoc, n));
+}
+
+
+static int
+append_delims(struct mdoc *mdoc, int tok,
+ int line, int *pos, char *buf)
+{
+ int c, lastarg;
+ char *p;
+
+ if (0 == buf[*pos])
+ return(1);
+
+ for (;;) {
+ lastarg = *pos;
+ c = mdoc_args(mdoc, line, pos, buf, 0, &p);
+ if (ARGS_ERROR == c)
+ return(0);
+ else if (ARGS_EOLN == c)
+ break;
+ assert(mdoc_isdelim(p));
+ if ( ! mdoc_word_alloc(mdoc, line, lastarg, p))
+ return(0);
+ mdoc->next = MDOC_NEXT_SIBLING;
+ }
+