version 1.83, 2010/06/29 19:20:38 |
version 1.86, 2010/06/30 04:05:02 |
|
|
#include "libmdoc.h" |
#include "libmdoc.h" |
#include "libmandoc.h" |
#include "libmandoc.h" |
|
|
enum rew { |
enum rew { /* see rew_dohalt() */ |
REWIND_REWIND, |
REWIND_NONE, |
REWIND_NOHALT, |
REWIND_THIS, |
REWIND_HALT |
REWIND_MORE, |
|
REWIND_LATER, |
|
REWIND_ERROR, |
}; |
}; |
|
|
static int blk_full(MACRO_PROT_ARGS); |
static int blk_full(MACRO_PROT_ARGS); |
Line 50 static int append_delims(struct mdoc *, |
|
Line 52 static int append_delims(struct mdoc *, |
|
int, int *, char *); |
int, int *, char *); |
static enum mdoct lookup(enum mdoct, const char *); |
static enum mdoct lookup(enum mdoct, const char *); |
static enum mdoct lookup_raw(const char *); |
static enum mdoct lookup_raw(const char *); |
static int make_pending(struct mdoc_node *, enum mdoc_type, |
static int make_pending(struct mdoc_node *, enum mdoct, |
struct mdoc *, int, int); |
struct mdoc *, int, int); |
static int phrase(struct mdoc *, int, int, char *); |
static int phrase(struct mdoc *, int, int, char *); |
static enum mdoct rew_alt(enum mdoct); |
static enum mdoct rew_alt(enum mdoct); |
static int rew_dobreak(enum mdoct, |
|
const struct mdoc_node *); |
|
static enum rew rew_dohalt(enum mdoct, enum mdoc_type, |
static enum rew rew_dohalt(enum mdoct, enum mdoc_type, |
const struct mdoc_node *); |
const struct mdoc_node *); |
static int rew_elem(struct mdoc *, enum mdoct); |
static int rew_elem(struct mdoc *, enum mdoct); |
Line 275 rew_last(struct mdoc *mdoc, const struct mdoc_node *to |
|
Line 275 rew_last(struct mdoc *mdoc, const struct mdoc_node *to |
|
|
|
|
|
/* |
/* |
* Return the opening macro of a closing one, e.g., `Ec' has `Eo' as its |
* For a block closing macro, return the corresponding opening one. |
* matching pair. |
* Otherwise, return the macro itself. |
*/ |
*/ |
static enum mdoct |
static enum mdoct |
rew_alt(enum mdoct tok) |
rew_alt(enum mdoct tok) |
Line 315 rew_alt(enum mdoct tok) |
|
Line 315 rew_alt(enum mdoct tok) |
|
case (MDOC_Xc): |
case (MDOC_Xc): |
return(MDOC_Xo); |
return(MDOC_Xo); |
default: |
default: |
break; |
return(tok); |
} |
} |
abort(); |
|
/* NOTREACHED */ |
/* NOTREACHED */ |
} |
} |
|
|
|
|
/* |
/* |
* Rewind rules. This indicates whether to stop rewinding |
* Rewinding to tok, how do we have to handle *p? |
* (REWIND_HALT) without touching our current scope, stop rewinding and |
* REWIND_NONE: *p would delimit tok, but no tok scope is open |
* close our current scope (REWIND_REWIND), or continue (REWIND_NOHALT). |
* inside *p, so there is no need to rewind anything at all. |
* The scope-closing and so on occurs in the various rew_* routines. |
* REWIND_THIS: *p matches tok, so rewind *p and nothing else. |
|
* REWIND_MORE: *p is implicit, rewind it and keep searching for tok. |
|
* REWIND_LATER: *p is explicit and still open, postpone rewinding. |
|
* REWIND_ERROR: No tok block is open at all. |
*/ |
*/ |
static enum rew |
static enum rew |
rew_dohalt(enum mdoct tok, enum mdoc_type type, |
rew_dohalt(enum mdoct tok, enum mdoc_type type, |
const struct mdoc_node *p) |
const struct mdoc_node *p) |
{ |
{ |
|
|
|
/* |
|
* No matching token, no delimiting block, no broken block. |
|
* This can happen when full implicit macros are called for |
|
* the first time but try to rewind their previous |
|
* instance anyway. |
|
*/ |
if (MDOC_ROOT == p->type) |
if (MDOC_ROOT == p->type) |
return(REWIND_HALT); |
return(MDOC_BLOCK == type && |
if (MDOC_VALID & p->flags) |
MDOC_EXPLICIT & mdoc_macros[tok].flags ? |
return(REWIND_NOHALT); |
REWIND_ERROR : REWIND_NONE); |
|
|
|
/* |
|
* When starting to rewind, skip plain text |
|
* and nodes that have already been rewound. |
|
*/ |
|
if (MDOC_TEXT == p->type || MDOC_VALID & p->flags) |
|
return(REWIND_MORE); |
|
|
|
/* |
|
* The easiest case: Found a matching token. |
|
* This applies to both blocks and elements. |
|
*/ |
|
tok = rew_alt(tok); |
|
if (tok == p->tok) |
|
return(p->end ? REWIND_NONE : |
|
type == p->type ? REWIND_THIS : REWIND_MORE); |
|
|
|
/* |
|
* While elements do require rewinding for themselves, |
|
* they never affect rewinding of other nodes. |
|
*/ |
|
if (MDOC_ELEM == p->type) |
|
return(REWIND_MORE); |
|
|
|
/* |
|
* Blocks delimited by our target token get REWIND_MORE. |
|
* Blocks delimiting our target token get REWIND_NONE. |
|
*/ |
switch (tok) { |
switch (tok) { |
case (MDOC_Aq): |
case (MDOC_Bl): |
/* FALLTHROUGH */ |
if (MDOC_It == p->tok) |
case (MDOC_Bq): |
return(REWIND_MORE); |
/* FALLTHROUGH */ |
|
case (MDOC_Brq): |
|
/* 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): |
|
/* FALLTHROUGH */ |
|
case (MDOC_Vt): |
|
assert(MDOC_TAIL != type); |
|
if (tok != p->tok) |
|
break; |
|
if (p->end) |
|
return(REWIND_HALT); |
|
if (type == p->type) |
|
return(REWIND_REWIND); |
|
break; |
break; |
case (MDOC_It): |
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) |
if (MDOC_BODY == p->type && MDOC_Bl == p->tok) |
return(REWIND_HALT); |
return(REWIND_NONE); |
break; |
break; |
case (MDOC_Sh): |
/* |
if (type == p->type && tok == p->tok) |
* XXX Badly nested block handling still fails badly |
return(REWIND_REWIND); |
* when one block is breaking two blocks of the same type. |
|
* This is an incomplete and extremely ugly workaround, |
|
* required to let the OpenBSD tree build. |
|
*/ |
|
case (MDOC_Oo): |
|
if (MDOC_Op == p->tok) |
|
return(REWIND_MORE); |
break; |
break; |
case (MDOC_Nd): |
case (MDOC_Nd): |
/* FALLTHROUGH */ |
/* FALLTHROUGH */ |
case (MDOC_Ss): |
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) |
if (MDOC_BODY == p->type && MDOC_Sh == p->tok) |
return(REWIND_HALT); |
return(REWIND_NONE); |
break; |
|
case (MDOC_Ao): |
|
/* FALLTHROUGH */ |
/* FALLTHROUGH */ |
case (MDOC_Bd): |
|
/* FALLTHROUGH */ |
|
case (MDOC_Bf): |
|
/* FALLTHROUGH */ |
|
case (MDOC_Bk): |
|
/* FALLTHROUGH */ |
|
case (MDOC_Bl): |
|
/* FALLTHROUGH */ |
|
case (MDOC_Bo): |
|
/* FALLTHROUGH */ |
|
case (MDOC_Bro): |
|
/* 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 (tok != p->tok) |
|
break; |
|
if (p->end) |
|
return(REWIND_HALT); |
|
if (type == p->type) |
|
return(REWIND_REWIND); |
|
break; |
|
/* Multi-line explicit scope close. */ |
|
case (MDOC_Ac): |
|
/* FALLTHROUGH */ |
|
case (MDOC_Bc): |
|
/* FALLTHROUGH */ |
|
case (MDOC_Brc): |
|
/* 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 (rew_alt(tok) != p->tok) |
|
break; |
|
if (p->end) |
|
return(REWIND_HALT); |
|
if (type == p->type) |
|
return(REWIND_REWIND); |
|
break; |
|
default: |
|
abort(); |
|
/* NOTREACHED */ |
|
} |
|
|
|
return(REWIND_NOHALT); |
|
} |
|
|
|
|
|
/* |
|
* See if we can break an encountered scope (the rew_dohalt has returned |
|
* REWIND_NOHALT). |
|
*/ |
|
static int |
|
rew_dobreak(enum mdoct tok, const struct mdoc_node *p) |
|
{ |
|
|
|
assert(MDOC_ROOT != p->type); |
|
if (MDOC_ELEM == p->type) |
|
return(1); |
|
if (MDOC_TEXT == p->type) |
|
return(1); |
|
if (MDOC_VALID & p->flags) |
|
return(1); |
|
if (MDOC_BODY == p->type && p->end) |
|
return(1); |
|
|
|
switch (tok) { |
|
case (MDOC_It): |
|
return(MDOC_It == p->tok); |
|
case (MDOC_Nd): |
|
return(MDOC_Nd == p->tok); |
|
case (MDOC_Ss): |
|
return(MDOC_Ss == p->tok); |
|
case (MDOC_Sh): |
case (MDOC_Sh): |
if (MDOC_Nd == p->tok) |
if (MDOC_Nd == p->tok || MDOC_Ss == p->tok || |
return(1); |
MDOC_Sh == p->tok) |
if (MDOC_Ss == p->tok) |
return(REWIND_MORE); |
return(1); |
|
return(MDOC_Sh == p->tok); |
|
case (MDOC_El): |
|
if (MDOC_It == p->tok) |
|
return(1); |
|
break; |
break; |
case (MDOC_Oc): |
|
if (MDOC_Op == p->tok) |
|
return(1); |
|
break; |
|
default: |
default: |
break; |
break; |
} |
} |
|
|
if (MDOC_EXPLICIT & mdoc_macros[tok].flags) |
/* |
return(p->tok == rew_alt(tok)); |
* Default block rewinding rules. |
else if (MDOC_BLOCK == p->type) |
* In particular, always skip block end markers. |
return(1); |
*/ |
|
if (p->end || (MDOC_BLOCK == p->type && |
|
! (MDOC_EXPLICIT & mdoc_macros[tok].flags))) |
|
return(REWIND_MORE); |
|
|
return(tok == p->tok); |
/* |
|
* Partial blocks allow delayed rewinding by default. |
|
*/ |
|
if (&blk_full != mdoc_macros[tok].fp) |
|
return (REWIND_LATER); |
|
|
|
/* |
|
* Full blocks can only be rewound when matching |
|
* or when there is an explicit rule. |
|
*/ |
|
return(REWIND_ERROR); |
} |
} |
|
|
|
|
Line 574 make_pending(struct mdoc_node *broken, enum mdoct tok, |
|
Line 475 make_pending(struct mdoc_node *broken, enum mdoct tok, |
|
continue; |
continue; |
} |
} |
|
|
if (REWIND_REWIND != rew_dohalt(tok, MDOC_BLOCK, breaker)) |
if (REWIND_THIS != rew_dohalt(tok, MDOC_BLOCK, breaker)) |
continue; |
continue; |
if (MDOC_BODY == broken->type) |
if (MDOC_BODY == broken->type) |
broken = broken->parent; |
broken = broken->parent; |
Line 614 make_pending(struct mdoc_node *broken, enum mdoct tok, |
|
Line 515 make_pending(struct mdoc_node *broken, enum mdoct tok, |
|
/* |
/* |
* Found no matching block for tok. |
* Found no matching block for tok. |
* Are you trying to close a block that is not open? |
* Are you trying to close a block that is not open? |
* Report failure and abort the parser. |
* XXX Make this non-fatal. |
*/ |
*/ |
mdoc_pmsg(m, line, ppos, MANDOCERR_SYNTNOSCOPE); |
mdoc_pmsg(m, line, ppos, MANDOCERR_SYNTNOSCOPE); |
return(0); |
return(0); |
} |
} |
|
|
|
|
static int |
static int |
rew_sub(enum mdoc_type t, struct mdoc *m, |
rew_sub(enum mdoc_type t, struct mdoc *m, |
enum mdoct tok, int line, int ppos) |
enum mdoct tok, int line, int ppos) |
{ |
{ |
struct mdoc_node *n; |
struct mdoc_node *n; |
enum rew c; |
|
|
|
/* LINTED */ |
n = m->last; |
for (n = m->last; n; n = n->parent) { |
while (n) { |
c = rew_dohalt(tok, t, n); |
switch (rew_dohalt(tok, t, n)) { |
if (REWIND_HALT == c) { |
case (REWIND_NONE): |
if (n->end || MDOC_BLOCK != t) |
return(1); |
return(1); |
case (REWIND_THIS): |
if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) |
|
return(1); |
|
/* FIXME: shouldn't raise an error */ |
|
mdoc_pmsg(m, line, ppos, MANDOCERR_SYNTNOSCOPE); |
|
return(0); |
|
} |
|
if (REWIND_REWIND == c) |
|
break; |
break; |
else if (rew_dobreak(tok, n)) |
case (REWIND_MORE): |
|
n = n->parent; |
continue; |
continue; |
return(make_pending(n, tok, m, line, ppos)); |
case (REWIND_LATER): |
|
return(make_pending(n, tok, m, line, ppos)); |
|
case (REWIND_ERROR): |
|
/* XXX Make this non-fatal. */ |
|
mdoc_pmsg(m, line, ppos, MANDOCERR_SYNTNOSCOPE); |
|
return 0; |
|
} |
|
break; |
} |
} |
|
|
assert(n); |
assert(n); |