From 9dd355694618bcd06487072ef69080b1afa3dead Mon Sep 17 00:00:00 2001 From: Kristaps Dzonsons Date: Wed, 7 Jan 2009 15:53:00 +0000 Subject: *** empty log message *** --- action.c | 72 ++++++--- macro.c | 77 +++++---- mdoc.c | 42 ++++- mdoc.h | 10 +- mdocml.c | 30 ++-- private.h | 8 +- validate.c | 531 +++++++++++++++++++------------------------------------------ 7 files changed, 329 insertions(+), 441 deletions(-) diff --git a/action.c b/action.c index c7f27266..81d38ba1 100644 --- a/action.c +++ b/action.c @@ -1,4 +1,4 @@ -/* $Id: action.c,v 1.2 2009/01/05 16:11:14 kristaps Exp $ */ +/* $Id: action.c,v 1.3 2009/01/07 15:53:00 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -21,9 +21,7 @@ #include "private.h" -typedef int (*a_act)(struct mdoc *, int, int, - int, const char *[], - int, const struct mdoc_arg *); +typedef int (*a_act)(struct mdoc *, int, int); struct actions { @@ -31,12 +29,15 @@ struct actions { }; +static int action_sh(struct mdoc *, int, int); + + const struct actions mdoc_actions[MDOC_MAX] = { { NULL }, /* \" */ { NULL }, /* Dd */ { NULL }, /* Dt */ { NULL }, /* Os */ - { NULL }, /* Sh */ + { action_sh }, /* Sh */ { NULL }, /* Ss */ { NULL }, /* Pp */ { NULL }, /* D1 */ @@ -141,26 +142,61 @@ const struct actions mdoc_actions[MDOC_MAX] = { }; -int -mdoc_action(struct mdoc *mdoc, int tok, int pos) +static int +action_sh(struct mdoc *mdoc, int tok, int pos) { + enum mdoc_sec sec; + int i; + struct mdoc_node *n; + char *args[MDOC_LINEARG_MAX]; + + if (MDOC_HEAD != mdoc->last->type) + return(1); + + n = mdoc->last->child; + assert(n); + + for (i = 0; n && i < MDOC_LINEARG_MAX; n = n->next, i++) { + assert(MDOC_TEXT == n->type); + assert(NULL == n->child); + assert(n->data.text.string); + args[i] = n->data.text.string; + } + + sec = mdoc_atosec((size_t)i, (const char **)args); + if (SEC_CUSTOM != sec) + mdoc->sec_lastn = sec; + mdoc->sec_last = sec; return(1); } -#if 0 - /* Post-processing. */ - switch (tok) { - case (MDOC_Sh): - sec = mdoc_atosec((size_t)sz, _CC(args)); - if (SEC_CUSTOM != sec) - mdoc->sec_lastn = sec; - mdoc->sec_last = sec; + +int +mdoc_action(struct mdoc *mdoc, int pos) +{ + int t; + + switch (mdoc->last->type) { + case (MDOC_BODY): + t = mdoc->last->data.body.tok; break; - default: + case (MDOC_ELEM): + t = mdoc->last->data.elem.tok; break; + case (MDOC_BLOCK): + t = mdoc->last->data.block.tok; + break; + case (MDOC_HEAD): + t = mdoc->last->data.head.tok; + break; + default: + return(1); } - MDOC_Nm... ? -#endif + if (NULL == mdoc_actions[t].action) + return(1); + /* TODO: MDOC_Nm... ? */ + return((*mdoc_actions[t].action)(mdoc, t, pos)); +} diff --git a/macro.c b/macro.c index d15ad3df..6cc28953 100644 --- a/macro.c +++ b/macro.c @@ -1,4 +1,4 @@ -/* $Id: macro.c,v 1.24 2009/01/06 15:49:44 kristaps Exp $ */ +/* $Id: macro.c,v 1.25 2009/01/07 15:53:00 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -34,6 +34,7 @@ static int rewind_impblock(struct mdoc *, int, int); static int rewind_expblock(struct mdoc *, int, int, int); static int rewind_head(struct mdoc *, int, int); static int rewind_body(struct mdoc *, int, int, int); +static int rewind_last(struct mdoc *, int, struct mdoc_node *); static int append_delims(struct mdoc *, int, int *, char *); static int lookup(struct mdoc *, int, const char *); @@ -48,6 +49,26 @@ lookup(struct mdoc *mdoc, int from, const char *p) } +static int +rewind_last(struct mdoc *mdoc, int ppos, struct mdoc_node *to) +{ + + assert(to); + while (mdoc->last != to) { + if ( ! mdoc_valid_post(mdoc, ppos)) + return(0); + if ( ! mdoc_action(mdoc, ppos)) + return(0); + mdoc->last = mdoc->last->parent; + assert(mdoc->last); + } + mdoc->next = MDOC_NEXT_SIBLING; + if ( ! mdoc_valid_post(mdoc, ppos)) + return(0); + return(mdoc_action(mdoc, ppos)); +} + + static int rewind_elem(struct mdoc *mdoc, int ppos, int tok) { @@ -59,11 +80,7 @@ rewind_elem(struct mdoc *mdoc, int ppos, int tok) assert(MDOC_ELEM == n->type); assert(tok == n->data.elem.tok); - mdoc->last = n; - mdoc->next = MDOC_NEXT_SIBLING; - if ( ! mdoc_valid_post(mdoc, tok, ppos)) - return(0); - return(mdoc_action(mdoc, tok, ppos)); + return(rewind_last(mdoc, ppos, n)); } @@ -71,22 +88,21 @@ static int rewind_body(struct mdoc *mdoc, int ppos, int tok, int tt) { struct mdoc_node *n; + int t; /* LINTED */ for (n = mdoc->last; n; n = n->parent) { if (MDOC_BODY != n->type) continue; - if (tt == n->data.head.tok) + if (tt == (t = n->data.head.tok)) break; - if ( ! (MDOC_EXPLICIT & mdoc_macros[tt].flags)) + if ( ! (MDOC_EXPLICIT & mdoc_macros[t].flags)) continue; return(mdoc_err(mdoc, tok, ppos, ERR_SCOPE_BREAK)); } - mdoc->last = n ? n : mdoc->last; - mdoc->next = MDOC_NEXT_SIBLING; - /* XXX - no validation, we do this only for blocks/elements. */ - return(1); + assert(n); + return(rewind_last(mdoc, ppos, n)); } @@ -107,10 +123,8 @@ rewind_head(struct mdoc *mdoc, int ppos, int tok) return(mdoc_err(mdoc, tok, ppos, ERR_SCOPE_BREAK)); } - mdoc->last = n ? n : mdoc->last; - mdoc->next = MDOC_NEXT_SIBLING; - /* XXX - no validation, we do this only for blocks/elements. */ - return(1); + assert(n); + return(rewind_last(mdoc, ppos, n)); } @@ -133,13 +147,9 @@ rewind_expblock(struct mdoc *mdoc, int ppos, int tok, int tt) return(mdoc_err(mdoc, tok, ppos, ERR_SCOPE_BREAK)); } - if (NULL == (mdoc->last = n)) + if (NULL == n) return(mdoc_err(mdoc, tok, ppos, ERR_SCOPE_NOCTX)); - - mdoc->next = MDOC_NEXT_SIBLING; - if ( ! mdoc_valid_post(mdoc, tok, ppos)) - return(0); - return(mdoc_action(mdoc, tok, ppos)); + return(rewind_last(mdoc, ppos, n)); } @@ -166,12 +176,7 @@ rewind_impblock(struct mdoc *mdoc, int ppos, int tok) if (NULL == n) return(1); - - mdoc->next = MDOC_NEXT_SIBLING; - mdoc->last = n; - if ( ! mdoc_valid_post(mdoc, tok, ppos)) - return(0); - return(mdoc_action(mdoc, tok, ppos)); + return(rewind_last(mdoc, ppos, n)); } @@ -471,6 +476,9 @@ macro_scoped(MACRO_PROT_ARGS) mdoc_argv_free(argc, argv); if (0 == buf[*pos]) { + mdoc_head_alloc(mdoc, ppos, tok); + if ( ! rewind_head(mdoc, ppos, tok)) + return(0); mdoc_body_alloc(mdoc, ppos, tok); mdoc->next = MDOC_NEXT_CHILD; return(1); @@ -600,6 +608,9 @@ macro_constant_scoped(MACRO_PROT_ARGS) mdoc->next = MDOC_NEXT_CHILD; if (0 == maxargs) { + mdoc_head_alloc(mdoc, ppos, tok); + if ( ! rewind_head(mdoc, ppos, tok)) + return(0); mdoc_body_alloc(mdoc, ppos, tok); flushed = 1; } else @@ -835,3 +846,13 @@ macro_obsolete(MACRO_PROT_ARGS) return(mdoc_warn(mdoc, tok, ppos, WARN_IGN_OBSOLETE)); } + + +int +macro_end(struct mdoc *mdoc) +{ + + assert(mdoc->first); + assert(mdoc->last); + return(rewind_last(mdoc, -1, mdoc->first)); +} diff --git a/mdoc.c b/mdoc.c index 657638d7..b97908ac 100644 --- a/mdoc.c +++ b/mdoc.c @@ -1,4 +1,4 @@ -/* $Id: mdoc.c,v 1.19 2009/01/06 15:49:44 kristaps Exp $ */ +/* $Id: mdoc.c,v 1.20 2009/01/07 15:53:00 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -249,11 +249,32 @@ mdoc_alloc(void *data, const struct mdoc_cb *cb) int -mdoc_parseln(struct mdoc *mdoc, char *buf) +mdoc_endparse(struct mdoc *mdoc) +{ + + if (MDOC_HALT & mdoc->flags) + return(0); + if (NULL == mdoc->first) + return(1); + + assert(mdoc->last); + if ( ! macro_end(mdoc)) { + mdoc->flags |= MDOC_HALT; + return(0); + } + return(1); +} + + +int +mdoc_parseln(struct mdoc *mdoc, int line, char *buf) { int c, i; char tmp[5]; + if (MDOC_HALT & mdoc->flags) + return(0); + if ('.' != *buf) { if (SEC_PROLOGUE == mdoc->sec_lastn) return(mdoc_err(mdoc, -1, 0, ERR_SYNTAX_NOTEXT)); @@ -270,23 +291,32 @@ mdoc_parseln(struct mdoc *mdoc, char *buf) while (buf[i] && ! isspace(buf[i]) && i < (int)sizeof(tmp)) i++; - if (i == (int)sizeof(tmp)) + if (i == (int)sizeof(tmp)) { + mdoc->flags |= MDOC_HALT; return(mdoc_err(mdoc, -1, 1, ERR_MACRO_NOTSUP)); - else if (i <= 2) + } else if (i <= 2) { + mdoc->flags |= MDOC_HALT; return(mdoc_err(mdoc, -1, 1, ERR_MACRO_NOTSUP)); + } i--; (void)memcpy(tmp, buf + 1, (size_t)i); tmp[i++] = 0; - if (MDOC_MAX == (c = mdoc_find(mdoc, tmp))) + if (MDOC_MAX == (c = mdoc_find(mdoc, tmp))) { + mdoc->flags |= MDOC_HALT; return(mdoc_err(mdoc, c, 1, ERR_MACRO_NOTSUP)); + } while (buf[i] && isspace(buf[i])) i++; - return(mdoc_macro(mdoc, c, 1, &i, buf)); + if ( ! mdoc_macro(mdoc, c, 1, &i, buf)) { + mdoc->flags |= MDOC_HALT; + return(0); + } + return(1); } diff --git a/mdoc.h b/mdoc.h index f2184b3d..fdb6b036 100644 --- a/mdoc.h +++ b/mdoc.h @@ -1,4 +1,4 @@ -/* $Id: mdoc.h,v 1.15 2009/01/05 17:57:08 kristaps Exp $ */ +/* $Id: mdoc.h,v 1.16 2009/01/07 15:53:00 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -19,7 +19,7 @@ #ifndef MDOC_H #define MDOC_H -#define MDOC_LINEARG_MAX 8 +#define MDOC_LINEARG_MAX 12 #define MDOC___ 0 #define MDOC_Dd 1 @@ -217,6 +217,7 @@ enum mdoc_err { ERR_ARGS_EQ1, ERR_ARGS_GE1, ERR_ARGS_LE2, + ERR_ARGS_LE8, ERR_ARGS_MANY, ERR_SYNTAX_CHILDHEAD, ERR_SYNTAX_CHILDBODY, @@ -411,6 +412,8 @@ struct mdoc_node { struct mdoc_node *child; struct mdoc_node *next; struct mdoc_node *prev; + int line; + int pos; enum mdoc_type type; union mdoc_data data; }; @@ -430,9 +433,10 @@ struct mdoc; void mdoc_free(struct mdoc *); struct mdoc *mdoc_alloc(void *data, const struct mdoc_cb *); -int mdoc_parseln(struct mdoc *, char *buf); +int mdoc_parseln(struct mdoc *, int, char *buf); const struct mdoc_node *mdoc_result(struct mdoc *); +int mdoc_endparse(struct mdoc *); __END_DECLS diff --git a/mdocml.c b/mdocml.c index 5fd51990..8c8e713e 100644 --- a/mdocml.c +++ b/mdocml.c @@ -1,4 +1,4 @@ -/* $Id: mdocml.c,v 1.35 2009/01/05 17:57:08 kristaps Exp $ */ +/* $Id: mdocml.c,v 1.36 2009/01/07 15:53:00 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -215,8 +215,6 @@ print_node(const struct mdoc_node *n, int indent) t = "element"; argv = n->data.elem.argv; argc = n->data.elem.argc; - params = n->data.elem.args; - sz = n->data.elem.sz; break; case (MDOC_BLOCK): p = mdoc_macronames[n->data.block.tok]; @@ -256,11 +254,14 @@ parse_leave(struct md_parse *p, int code) { const struct mdoc_node *n; - if (p->mdoc) { - if ((n = mdoc_result(p->mdoc))) - print_node(n, 0); - mdoc_free(p->mdoc); - } + if (NULL == p->mdoc) + return(code); + + if ( ! mdoc_endparse(p->mdoc)) + code = 0; + if ((n = mdoc_result(p->mdoc))) + print_node(n, 0); + return(code); } @@ -304,7 +305,7 @@ parse_begin(struct md_parse *p) } line[(int)pos] = sv[(int)pos] = 0; - if ( ! mdoc_parseln(p->mdoc, line)) + if ( ! mdoc_parseln(p->mdoc, p->lnn, line)) return(parse_leave(p, 0)); p->lnn++; @@ -383,6 +384,9 @@ msg_err(void *arg, int tok, int col, enum mdoc_err type) case (ERR_ARGS_LE2): fmt = "macro `%s' expects two or fewer arguments"; break; + case (ERR_ARGS_LE8): + fmt = "macro `%s' expects eight or fewer arguments"; + break; case (ERR_ARGS_MANY): fmt = "macro `%s' has too many arguments"; break; @@ -434,7 +438,7 @@ msg_err(void *arg, int tok, int col, enum mdoc_err type) (void)fprintf(stderr, " (column %d)\n", col); return(0); } else if (-1 == col) { - (void)fprintf(stderr, "\nFrom: %s", p->line); + (void)fprintf(stderr, "\nFrom: %s\n", p->line); return(0); } @@ -541,13 +545,15 @@ msg_warn(void *arg, int tok, int col, enum mdoc_warn type) (void)fprintf(stderr, "%s:%d: warning: %s", p->name, p->lnn, lit); - if (p->dbg >= 1) { + if (col >= 0 && p->dbg >= 1) { (void)fprintf(stderr, "\nFrom: %s\n ", p->line); for (i = 0; i < col; i++) (void)fprintf(stderr, " "); (void)fprintf(stderr, "^\n"); - } else + } else if (col >= 0) (void)fprintf(stderr, " (column %d)\n", col); + else + (void)fprintf(stderr, "\n"); if (p->warn & MD_WARN_ERR) { (void)fprintf(stderr, "%s: considering warnings as " diff --git a/private.h b/private.h index 89a73e2c..910e6f1c 100644 --- a/private.h +++ b/private.h @@ -1,4 +1,4 @@ -/* $Id: private.h,v 1.56 2009/01/06 15:49:44 kristaps Exp $ */ +/* $Id: private.h,v 1.57 2009/01/07 15:53:00 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -31,6 +31,7 @@ struct mdoc { struct mdoc_cb cb; void *htab; int flags; +#define MDOC_HALT (1 << 0) enum mdoc_next next; struct mdoc_node *last; struct mdoc_node *first; @@ -87,8 +88,8 @@ time_t mdoc_atotime(const char *); int mdoc_valid_pre(struct mdoc *, int, int, int, const struct mdoc_arg *); -int mdoc_valid_post(struct mdoc *, int, int); -int mdoc_action(struct mdoc *, int, int); +int mdoc_valid_post(struct mdoc *, int); +int mdoc_action(struct mdoc *, int); int mdoc_argv(struct mdoc *, int, struct mdoc_arg *, int *, char *); @@ -122,6 +123,7 @@ int macro_scoped(MACRO_PROT_ARGS); int macro_close_explicit(MACRO_PROT_ARGS); int macro_scoped_line(MACRO_PROT_ARGS); int macro_prologue(MACRO_PROT_ARGS); +int macro_end(struct mdoc *); __END_DECLS diff --git a/validate.c b/validate.c index 728672e0..ddf3c70d 100644 --- a/validate.c +++ b/validate.c @@ -1,4 +1,4 @@ -/* $Id: validate.c,v 1.16 2009/01/05 14:14:04 kristaps Exp $ */ +/* $Id: validate.c,v 1.17 2009/01/07 15:53:00 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -21,440 +21,229 @@ #include "private.h" -/* FIXME: `.St' can only have one argument set. */ -typedef int (*v_args_sz)(struct mdoc *, int, int, int); -typedef int (*v_args)(struct mdoc *, int, int, - int, const char *[], +typedef int (*v_pre)(struct mdoc *, int, int, int, const struct mdoc_arg *); -typedef int (*v_tree)(struct mdoc *, int, int); +typedef int (*v_post)(struct mdoc *, int, int); struct valids { - v_args_sz sz; - v_args args; - v_tree tree_pre; - v_tree tree_post; + v_pre pre; + v_post *post; }; -static int assert_eq0(struct mdoc *, int, int, int); -static int assert_le1(struct mdoc *, int, int, int); -static int need_eq0(struct mdoc *, int, int, int); -static int need_eq1(struct mdoc *, int, int, int); -static int need_ge1(struct mdoc *, int, int, int); -static int need_le2(struct mdoc *, int, int, int); -static int want_eq0(struct mdoc *, int, int, int); -static int want_ge1(struct mdoc *, int, int, int); - -static int tree_pre_ref(struct mdoc *, int, int); -static int tree_pre_display(struct mdoc *, int, int); - -static int tree_post_onlyhead(struct mdoc *, int, int); -static int tree_post_onlybody(struct mdoc *, int, int); -static int tree_post_warnemptybody(struct mdoc *, int, int); - -static int args_bool(struct mdoc *, int, int, - int, const char *[], - int, const struct mdoc_arg *); -static int args_sh(struct mdoc *, int, int, - int, const char *[], - int, const struct mdoc_arg *); -static int args_an(struct mdoc *, int, int, - int, const char *[], - int, const struct mdoc_arg *); -static int args_nopunct(struct mdoc *, int, int, - int, const char *[], - int, const struct mdoc_arg *); -static int args_xr(struct mdoc *, int, int, - int, const char *[], +static int pre_sh(struct mdoc *, int, int, int, const struct mdoc_arg *); +static int post_headchild_err_ge1(struct mdoc *, int, int); +static int post_headchild_err_le8(struct mdoc *, int, int); +static int post_bodychild_warn_ge1(struct mdoc *, int, int); + +static v_post posts_sh[] = { post_headchild_err_ge1, + post_bodychild_warn_ge1, + post_headchild_err_le8, NULL }; const struct valids mdoc_valids[MDOC_MAX] = { - { NULL, NULL, NULL, NULL }, /* \" */ - { NULL, NULL, NULL, NULL }, /* Dd */ /* TODO */ - { NULL, NULL, NULL, NULL }, /* Dt */ /* TODO */ - { NULL, NULL, NULL, NULL }, /* Os */ /* TODO */ - { want_ge1, args_sh, NULL, NULL }, /* Sh */ /* FIXME: preceding Pp. */ - { want_ge1, NULL, NULL, NULL }, /* Ss */ /* FIXME: preceding Pp. */ - { want_eq0, NULL, NULL, NULL }, /* Pp */ - { assert_eq0, NULL, tree_pre_display, tree_post_onlyhead }, /* D1 */ - { assert_eq0, NULL, tree_pre_display, tree_post_onlyhead }, /* Dl */ - { want_eq0, NULL, tree_pre_display, tree_post_warnemptybody }, /* Bd */ /* FIXME: preceding Pp. */ - { assert_eq0, NULL, NULL, tree_post_onlybody }, /* Ed */ - { want_eq0, NULL, NULL, NULL }, /* Bl */ /* FIXME: preceding Pp. */ - { assert_eq0, NULL, NULL, tree_post_onlybody }, /* El */ - { NULL, NULL, NULL, NULL }, /* It */ - { need_ge1, NULL, NULL, NULL }, /* Ad */ - { NULL, args_an, NULL, NULL }, /* An */ - { NULL, NULL, NULL, NULL }, /* Ar */ - { need_ge1, NULL, NULL, NULL }, /* Cd */ - { NULL, NULL, NULL, NULL }, /* Cm */ - { need_ge1, NULL, NULL, NULL }, /* Dv */ - { need_ge1, NULL, NULL, NULL }, /* Er */ - { need_ge1, NULL, NULL, NULL }, /* Ev */ - { NULL, NULL, NULL, NULL }, /* Ex */ - { need_ge1, NULL, NULL, NULL }, /* Fa */ - { NULL, NULL, NULL, NULL }, /* Fd */ - { NULL, NULL, NULL, NULL }, /* Fl */ - { need_ge1, NULL, NULL, NULL }, /* Fn */ - { want_ge1, NULL, NULL, NULL }, /* Ft */ - { need_ge1, NULL, NULL, NULL }, /* Ic */ - { need_eq1, NULL, NULL, NULL }, /* In */ - { want_ge1, NULL, NULL, NULL }, /* Li */ - { want_ge1, NULL, NULL, NULL }, /* Nd */ - { NULL, NULL, NULL, NULL }, /* Nm */ - { NULL, NULL, NULL, NULL }, /* Op */ - { NULL, NULL, NULL, NULL }, /* Ot */ - { want_ge1, NULL, NULL, NULL }, /* Pa */ - { NULL, NULL, NULL, NULL }, /* Rv */ - { NULL, NULL, NULL, NULL }, /* St */ - { need_ge1, NULL, NULL, NULL }, /* Va */ - { need_ge1, NULL, NULL, NULL }, /* Vt */ - { need_le2, args_xr, NULL, NULL }, /* Xr */ - { need_ge1, NULL, tree_pre_ref, NULL }, /* %A */ - { need_ge1, NULL, tree_pre_ref, NULL }, /* %B */ - { need_ge1, NULL, tree_pre_ref, NULL }, /* %D */ - { need_ge1, NULL, tree_pre_ref, NULL }, /* %I */ - { need_ge1, NULL, tree_pre_ref, NULL }, /* %J */ - { need_ge1, NULL, tree_pre_ref, NULL }, /* %N */ - { need_ge1, NULL, tree_pre_ref, NULL }, /* %O */ - { need_ge1, NULL, tree_pre_ref, NULL }, /* %P */ - { need_ge1, NULL, tree_pre_ref, NULL }, /* %R */ - { need_ge1, NULL, tree_pre_ref, NULL }, /* %T */ - { need_ge1, NULL, tree_pre_ref, NULL }, /* %V */ - { NULL, NULL, NULL, NULL }, /* Ac */ - { NULL, NULL, NULL, NULL }, /* Ao */ - { NULL, NULL, NULL, NULL }, /* Aq */ - { need_le2, args_nopunct, NULL, NULL }, /* At */ /* FIXME */ - { NULL, NULL, NULL, NULL }, /* Bc */ - { NULL, NULL, NULL, NULL }, /* Bf */ - { NULL, NULL, NULL, NULL }, /* Bo */ - { NULL, NULL, NULL, NULL }, /* Bq */ - { assert_le1, NULL, NULL, NULL }, /* Bsx */ - { assert_le1, NULL, NULL, NULL }, /* Bx */ - { need_eq1, args_bool, NULL, NULL }, /* Db */ - { NULL, NULL, NULL, NULL }, /* Dc */ - { NULL, NULL, NULL, NULL }, /* Do */ - { NULL, NULL, NULL, NULL }, /* Dq */ - { NULL, NULL, NULL, NULL }, /* Ec */ - { NULL, NULL, NULL, NULL }, /* Ef */ /* -symbolic, etc. */ - { need_ge1, NULL, NULL, NULL }, /* Em */ - { NULL, NULL, NULL, NULL }, /* Eo */ - { assert_le1, NULL, NULL, NULL }, /* Fx */ - { want_ge1, NULL, NULL, NULL }, /* Ms */ - { NULL, NULL, NULL, NULL }, /* No */ - { NULL, NULL, NULL, NULL }, /* Ns */ - { assert_le1, NULL, NULL, NULL }, /* Nx */ - { assert_le1, NULL, NULL, NULL }, /* Ox */ - { NULL, NULL, NULL, NULL }, /* Pc */ - { NULL, NULL, NULL, NULL }, /* Pf */ /* 2 or more arguments */ - { NULL, NULL, NULL, NULL }, /* Po */ - { NULL, NULL, NULL, NULL }, /* Pq */ /* FIXME: ignore following Sh/Ss */ - { NULL, NULL, NULL, NULL }, /* Qc */ - { NULL, NULL, NULL, NULL }, /* Ql */ - { NULL, NULL, NULL, NULL }, /* Qo */ - { NULL, NULL, NULL, NULL }, /* Qq */ - { NULL, NULL, NULL, NULL }, /* Re */ - { NULL, NULL, NULL, NULL }, /* Rs */ - { NULL, NULL, NULL, NULL }, /* Sc */ - { NULL, NULL, NULL, NULL }, /* So */ - { NULL, NULL, NULL, NULL }, /* Sq */ - { need_eq1, args_bool, NULL, NULL }, /* Sm */ - { need_ge1, NULL, NULL, NULL }, /* Sx */ - { need_ge1, NULL, NULL, NULL }, /* Sy */ - { want_ge1, NULL, NULL, NULL }, /* Tn */ - { assert_eq0, NULL, NULL, NULL }, /* Ux */ - { NULL, NULL, NULL, NULL }, /* Xc */ - { NULL, NULL, NULL, NULL }, /* Xo */ - { NULL, NULL, NULL, NULL }, /* Fo */ - { NULL, NULL, NULL, NULL }, /* Fc */ - { NULL, NULL, NULL, NULL }, /* Oo */ - { NULL, NULL, NULL, NULL }, /* Oc */ - { NULL, NULL, NULL, NULL }, /* Bk */ - { NULL, NULL, NULL, NULL }, /* Ek */ - { need_eq0, NULL, NULL, NULL }, /* Bt */ - { need_eq1, NULL, NULL, NULL }, /* Hf */ - { NULL, NULL, NULL, NULL }, /* Fr */ - { need_eq0, NULL, NULL, NULL }, /* Ud */ + { NULL, NULL }, /* \" */ + { NULL, NULL }, /* Dd */ /* TODO */ + { NULL, NULL }, /* Dt */ /* TODO */ + { NULL, NULL }, /* Os */ /* TODO */ + { pre_sh, posts_sh }, /* Sh */ /* FIXME: preceding Pp. */ + { NULL, NULL }, /* Ss */ /* FIXME: preceding Pp. */ + { NULL, NULL }, /* Pp */ + { NULL, NULL }, /* D1 */ + { NULL, NULL }, /* Dl */ + { NULL, NULL }, /* Bd */ /* FIXME: preceding Pp. */ + { NULL, NULL }, /* Ed */ + { NULL, NULL }, /* Bl */ /* FIXME: preceding Pp. */ + { NULL, NULL }, /* El */ + { NULL, NULL }, /* It */ + { NULL, NULL }, /* Ad */ + { NULL, NULL }, /* An */ + { NULL, NULL }, /* Ar */ + { NULL, NULL }, /* Cd */ + { NULL, NULL }, /* Cm */ + { NULL, NULL }, /* Dv */ + { NULL, NULL }, /* Er */ + { NULL, NULL }, /* Ev */ + { NULL, NULL }, /* Ex */ + { NULL, NULL }, /* Fa */ + { NULL, NULL }, /* Fd */ + { NULL, NULL }, /* Fl */ + { NULL, NULL }, /* Fn */ + { NULL, NULL }, /* Ft */ + { NULL, NULL }, /* Ic */ + { NULL, NULL }, /* In */ + { NULL, NULL }, /* Li */ + { NULL, NULL }, /* Nd */ + { NULL, NULL }, /* Nm */ + { NULL, NULL }, /* Op */ + { NULL, NULL }, /* Ot */ + { NULL, NULL }, /* Pa */ + { NULL, NULL }, /* Rv */ + { NULL, NULL }, /* St */ + { NULL, NULL }, /* Va */ + { NULL, NULL }, /* Vt */ + { NULL, NULL }, /* Xr */ + { NULL, NULL }, /* %A */ + { NULL, NULL }, /* %B */ + { NULL, NULL }, /* %D */ + { NULL, NULL }, /* %I */ + { NULL, NULL }, /* %J */ + { NULL, NULL }, /* %N */ + { NULL, NULL }, /* %O */ + { NULL, NULL }, /* %P */ + { NULL, NULL }, /* %R */ + { NULL, NULL }, /* %T */ + { NULL, NULL }, /* %V */ + { NULL, NULL }, /* Ac */ + { NULL, NULL }, /* Ao */ + { NULL, NULL }, /* Aq */ + { NULL, NULL }, /* At */ /* FIXME */ + { NULL, NULL }, /* Bc */ + { NULL, NULL }, /* Bf */ + { NULL, NULL }, /* Bo */ + { NULL, NULL }, /* Bq */ + { NULL, NULL }, /* Bsx */ + { NULL, NULL }, /* Bx */ + { NULL, NULL }, /* Db */ + { NULL, NULL }, /* Dc */ + { NULL, NULL }, /* Do */ + { NULL, NULL }, /* Dq */ + { NULL, NULL }, /* Ec */ + { NULL, NULL }, /* Ef */ /* -symbolic, etc. */ + { NULL, NULL }, /* Em */ + { NULL, NULL }, /* Eo */ + { NULL, NULL }, /* Fx */ + { NULL, NULL }, /* Ms */ + { NULL, NULL }, /* No */ + { NULL, NULL }, /* Ns */ + { NULL, NULL }, /* Nx */ + { NULL, NULL }, /* Ox */ + { NULL, NULL }, /* Pc */ + { NULL, NULL }, /* Pf */ /* 2 or more arguments */ + { NULL, NULL }, /* Po */ + { NULL, NULL }, /* Pq */ /* FIXME: ignore following Sh/Ss */ + { NULL, NULL }, /* Qc */ + { NULL, NULL }, /* Ql */ + { NULL, NULL }, /* Qo */ + { NULL, NULL }, /* Qq */ + { NULL, NULL }, /* Re */ + { NULL, NULL }, /* Rs */ + { NULL, NULL }, /* Sc */ + { NULL, NULL }, /* So */ + { NULL, NULL }, /* Sq */ + { NULL, NULL }, /* Sm */ + { NULL, NULL }, /* Sx */ + { NULL, NULL }, /* Sy */ + { NULL, NULL }, /* Tn */ + { NULL, NULL }, /* Ux */ + { NULL, NULL }, /* Xc */ + { NULL, NULL }, /* Xo */ + { NULL, NULL }, /* Fo */ + { NULL, NULL }, /* Fc */ + { NULL, NULL }, /* Oo */ + { NULL, NULL }, /* Oc */ + { NULL, NULL }, /* Bk */ + { NULL, NULL }, /* Ek */ + { NULL, NULL }, /* Bt */ + { NULL, NULL }, /* Hf */ + { NULL, NULL }, /* Fr */ + { NULL, NULL }, /* Ud */ }; static int -need_le2(struct mdoc *mdoc, int tok, int pos, int sz) +post_bodychild_warn_ge1(struct mdoc *mdoc, int tok, int pos) { - if (sz <= 2) - return(1); - return(mdoc_err(mdoc, tok, pos, ERR_ARGS_LE2)); -} - -static int -want_ge1(struct mdoc *mdoc, int tok, int pos, int sz) -{ - if (sz > 0) + if (MDOC_BODY != mdoc->last->type) return(1); - return(mdoc_warn(mdoc, tok, pos, WARN_ARGS_GE1)); -} - - -static int -want_eq0(struct mdoc *mdoc, int tok, int pos, int sz) -{ - if (sz == 0) - return(1); - return(mdoc_warn(mdoc, tok, pos, WARN_ARGS_EQ0)); -} - - -static int -need_eq0(struct mdoc *mdoc, int tok, int pos, int sz) -{ - if (sz == 0) + if (mdoc->last->child) return(1); - return(mdoc_err(mdoc, tok, pos, ERR_ARGS_EQ0)); -} - - -static int -assert_le1(struct mdoc *mdoc, int tok, int pos, int sz) -{ - assert(sz <= 1); - return(1); + return(mdoc_warn(mdoc, tok, pos, WARN_ARGS_GE1)); } static int -assert_eq0(struct mdoc *mdoc, int tok, int pos, int sz) +post_headchild_err_ge1(struct mdoc *mdoc, int tok, int pos) { - assert(sz == 0); - return(1); -} - - -static int -need_eq1(struct mdoc *mdoc, int tok, int pos, int sz) -{ - if (sz == 1) + if (MDOC_HEAD != mdoc->last->type) return(1); - return(mdoc_err(mdoc, tok, pos, ERR_ARGS_EQ1)); -} - - -static int -need_ge1(struct mdoc *mdoc, int tok, int pos, int sz) -{ - if (sz > 0) + if (mdoc->last->child) return(1); return(mdoc_err(mdoc, tok, pos, ERR_ARGS_GE1)); } static int -tree_post_onlybody(struct mdoc *mdoc, int tok, int pos) +post_headchild_err_le8(struct mdoc *mdoc, int tok, int pos) { + int i; struct mdoc_node *n; - assert(mdoc->last); - n = mdoc->last; - - assert(MDOC_BLOCK == n->type); - assert(n->child); - - if (MDOC_BODY == n->child->type) { - if (n->child->child) - return(1); - return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_EMPTYBODY)); - } - - return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_CHILDBODY)); -} - - -static int -tree_post_warnemptybody(struct mdoc *mdoc, int tok, int pos) -{ - struct mdoc_node *n; - - assert(mdoc->last); - n = mdoc->last; - - assert(MDOC_BLOCK == n->type); - assert(n->child); - - for (n = n->child; n; n = n->next) - if (MDOC_BODY == n->type) - break; - - if (n && n->child) + if (MDOC_HEAD != mdoc->last->type) return(1); - return(mdoc_warn(mdoc, tok, pos, WARN_SYNTAX_EMPTYBODY)); -} - - -static int -tree_post_onlyhead(struct mdoc *mdoc, int tok, int pos) -{ - struct mdoc_node *n; - - assert(mdoc->last); - n = mdoc->last; - - assert(MDOC_BLOCK == n->type); - assert(n->child); - - n = n->child; - - if (MDOC_HEAD != n->type) - return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_CHILDHEAD)); - if (n->child) + for (i = 0, n = mdoc->last->child; n; n = n->next, i++) + /* Do nothing. */ ; + if (i <= 8) return(1); - return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_EMPTYHEAD)); + return(mdoc_err(mdoc, tok, pos, ERR_ARGS_LE8)); } static int -args_an(struct mdoc *mdoc, int tok, int pos, - int sz, const char *args[], +pre_sh(struct mdoc *mdoc, int tok, int pos, int argc, const struct mdoc_arg *argv) { - if (0 != argc && 0 != sz) - return(mdoc_warn(mdoc, tok, pos, WARN_ARGS_EQ0)); return(1); } -static int -args_sh(struct mdoc *mdoc, int tok, int pos, - int sz, const char *args[], - int argc, const struct mdoc_arg *argv) -{ - enum mdoc_sec sec; - - sec = mdoc_atosec((size_t)sz, args); - if (SEC_CUSTOM != sec && sec < mdoc->sec_lastn) - if ( ! mdoc_warn(mdoc, tok, pos, WARN_SEC_OO)) - return(0); - if (SEC_BODY == mdoc->sec_last && SEC_NAME != sec) - return(mdoc_err(mdoc, tok, pos, ERR_SEC_NAME)); - - return(1); -} - - -static int -args_bool(struct mdoc *mdoc, int tok, int pos, - int sz, const char *args[], - int argc, const struct mdoc_arg *argv) -{ - int i; - - for (i = 0; i < sz; i++) { - if (xstrcmp(args[i], "on")) - continue; - if (xstrcmp(args[i], "off")) - continue; - return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_ARGBAD)); - } - return(1); -} - - -static int -tree_pre_ref(struct mdoc *mdoc, int tok, int pos) -{ - struct mdoc_node *n; - - assert(mdoc->last); - for (n = mdoc->last ; n; n = n->parent) { - if (MDOC_BLOCK != n->type) - continue; - if (MDOC_Rs != n->data.block.tok) - break; - return(1); - } - - return(mdoc_err(mdoc, tok, pos, ERR_SCOPE_NOCTX)); -} - - -static int -args_xr(struct mdoc *mdoc, int tok, int pos, - int sz, const char *args[], +int +mdoc_valid_pre(struct mdoc *mdoc, int tok, int pos, int argc, const struct mdoc_arg *argv) { - if (1 == sz) + if (NULL == mdoc_valids[tok].pre) return(1); - if (0 == sz) - return(mdoc_err(mdoc, tok, pos, ERR_ARGS_GE1)); - if (MSEC_DEFAULT == mdoc_atomsec(args[1])) - return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_ARGFORM)); - return(1); + return((*mdoc_valids[tok].pre)(mdoc, tok, pos, argc, argv)); } -static int -args_nopunct(struct mdoc *mdoc, int tok, int pos, - int sz, const char *args[], - int argc, const struct mdoc_arg *argv) +int +mdoc_valid_post(struct mdoc *mdoc, int pos) { - int i; + v_post *p; + int t; - if (0 == sz) + switch (mdoc->last->type) { + case (MDOC_BODY): + t = mdoc->last->data.body.tok; + break; + case (MDOC_ELEM): + t = mdoc->last->data.elem.tok; + break; + case (MDOC_BLOCK): + t = mdoc->last->data.block.tok; + break; + case (MDOC_HEAD): + t = mdoc->last->data.head.tok; + break; + default: return(1); - - i = 0; - if (ATT_DEFAULT == mdoc_atoatt(args[i])) - i++; - for ( ; i < sz; i++) { - if ( ! mdoc_isdelim(args[i])) - continue; - return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_NOPUNCT)); } - return(1); -} - - -static int -tree_pre_display(struct mdoc *mdoc, int tok, int pos) -{ - struct mdoc_node *node; - - assert(mdoc->last); - - /* Displays may not be nested in other displays. */ - /* LINTED */ - for (node = mdoc->last; node; node = node->parent) { - if (node->type != MDOC_BLOCK) - continue; - if (node->data.block.tok != MDOC_Bd) - continue; - break; - } - if (NULL == node) + if (NULL == mdoc_valids[t].post) return(1); - return(mdoc_err(mdoc, tok, pos, ERR_SCOPE_NONEST)); -} - - -int -mdoc_valid_pre(struct mdoc *mdoc, int tok, int pos, - int argc, const struct mdoc_arg *argv) -{ - - return(1); -} - -int -mdoc_valid_post(struct mdoc *mdoc, int tok, int pos) -{ + for (p = mdoc_valids[t].post; *p; p++) + if ( ! (*p)(mdoc, t, pos)) + return(0); return(1); } -- cgit v1.2.3-56-ge451