-/* $Id: action.c,v 1.6 2009/01/09 14:45:44 kristaps Exp $ */
+/* $Id: action.c,v 1.7 2009/01/12 12:52:21 kristaps Exp $ */
/*
* Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
*
if (MDOC_HEAD != mdoc->last->type)
return(1);
- assert(MDOC_Sh == mdoc->last->data.head.tok);
+ assert(MDOC_Sh == mdoc->last->tok);
n = mdoc->last->child;
assert(n);
struct mdoc_node *n;
assert(MDOC_ELEM == mdoc->last->type);
- assert(MDOC_Dt == mdoc->last->data.elem.tok);
+ assert(MDOC_Dt == mdoc->last->tok);
assert(0 == mdoc->meta.title[0]);
sz = META_TITLE_SZ;
struct mdoc_node *n;
assert(MDOC_ELEM == mdoc->last->type);
- assert(MDOC_Os == mdoc->last->data.elem.tok);
+ assert(MDOC_Os == mdoc->last->tok);
assert(0 == mdoc->meta.os[0]);
sz = META_OS_SZ;
struct mdoc_node *n;
assert(MDOC_ELEM == mdoc->last->type);
- assert(MDOC_Dd == mdoc->last->data.elem.tok);
+ assert(MDOC_Dd == mdoc->last->tok);
n = mdoc->last->child;
assert(0 == mdoc->meta.date);
assert(MDOC_TEXT == n->type);
p = n->data.text.string;
- if (xstrcmp(p, "$Mdocdate: January 9 2009 $")) {
+ if (xstrcmp(p, "$Mdocdate: January 12 2009 $")) {
mdoc->meta.date = time(NULL);
continue;
} else if (xstrcmp(p, "$")) {
int
mdoc_action_post(struct mdoc *mdoc)
{
- int t;
-
- 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:
+
+ if (MDOC_TEXT == mdoc->last->type)
+ return(1);
+ if (MDOC_ROOT == mdoc->last->type)
return(1);
- }
- if (NULL == mdoc_actions[t].post)
+ if (NULL == mdoc_actions[mdoc->last->tok].post)
return(1);
/* TODO: MDOC_Nm... ? */
- return((*mdoc_actions[t].post)(mdoc));
+ return((*mdoc_actions[mdoc->last->tok].post)(mdoc));
}
-/* $Id: mdoc.c,v 1.26 2009/01/12 10:31:53 kristaps Exp $ */
+/* $Id: mdoc.c,v 1.27 2009/01/12 12:52:21 kristaps Exp $ */
/*
* Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
*
assert(mdoc->first);
assert(MDOC_ROOT != p->type);
+ if (MDOC_TEXT == mdoc->last->type)
+ on = "<text>";
+ else if (MDOC_ROOT == mdoc->last->type)
+ on = "<root>";
+ else
+ on = mdoc_macronames[mdoc->last->tok];
+
+ /* FIXME: put this into util.c. */
switch (mdoc->last->type) {
case (MDOC_TEXT):
- on = "<text>";
ot = "text";
break;
case (MDOC_BODY):
- on = mdoc_macronames[mdoc->last->data.body.tok];
ot = "body";
break;
case (MDOC_ELEM):
- on = mdoc_macronames[mdoc->last->data.elem.tok];
ot = "elem";
break;
case (MDOC_HEAD):
- on = mdoc_macronames[mdoc->last->data.head.tok];
ot = "head";
break;
case (MDOC_TAIL):
- on = mdoc_macronames[mdoc->last->data.tail.tok];
ot = "tail";
break;
case (MDOC_BLOCK):
- on = mdoc_macronames[mdoc->last->data.block.tok];
ot = "block";
break;
case (MDOC_ROOT):
- on = "root";
ot = "root";
break;
default:
return(0);
if ( ! mdoc_action_pre(mdoc, p))
return(0);
+
+ switch (p->type) {
+ case (MDOC_HEAD):
+ assert(MDOC_BLOCK == p->parent->type);
+ p->parent->data.block.head = p;
+ break;
+ case (MDOC_TAIL):
+ assert(MDOC_BLOCK == p->parent->type);
+ p->parent->data.block.tail = p;
+ break;
+ case (MDOC_BODY):
+ assert(MDOC_BLOCK == p->parent->type);
+ p->parent->data.block.body = p;
+ break;
+ default:
+ break;
+ }
+
mdoc->last = p;
mdoc_msg(mdoc, "parse: %s of %s `%s'", act, ot, on);
return(1);
p->line = line;
p->pos = pos;
p->type = MDOC_TAIL;
- p->data.tail.tok = tok;
+ p->tok = tok;
return(mdoc_node_append(mdoc, p));
}
p->line = line;
p->pos = pos;
p->type = MDOC_HEAD;
- p->data.head.tok = tok;
+ p->tok = tok;
return(mdoc_node_append(mdoc, p));
}
p->line = line;
p->pos = pos;
p->type = MDOC_BODY;
- p->data.body.tok = tok;
+ p->tok = tok;
return(mdoc_node_append(mdoc, p));
}
p->pos = pos;
p->line = line;
p->type = MDOC_BLOCK;
- p->data.block.tok = tok;
+ p->tok = tok;
p->data.block.argc = argsz;
p->data.block.argv = argdup(argsz, args);
p->line = line;
p->pos = pos;
p->type = MDOC_ELEM;
- p->data.elem.tok = tok;
+ p->tok = tok;
p->data.elem.argc = argsz;
p->data.elem.argv = argdup(argsz, args);
-/* $Id: mdocml.c,v 1.39 2009/01/12 10:31:53 kristaps Exp $ */
+/* $Id: mdocml.c,v 1.40 2009/01/12 12:52:21 kristaps Exp $ */
/*
* Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
*
params = NULL;
sz = 0;
+ /* FIXME: put parts of this in util.c. */
switch (n->type) {
case (MDOC_TEXT):
assert(NULL == n->child);
t = "text";
break;
case (MDOC_BODY):
- p = mdoc_macronames[n->data.body.tok];
+ p = mdoc_macronames[n->tok];
t = "block-body";
break;
case (MDOC_HEAD):
- p = mdoc_macronames[n->data.head.tok];
+ p = mdoc_macronames[n->tok];
t = "block-head";
break;
case (MDOC_TAIL):
- p = mdoc_macronames[n->data.tail.tok];
+ p = mdoc_macronames[n->tok];
t = "block-tail";
break;
case (MDOC_ELEM):
- p = mdoc_macronames[n->data.elem.tok];
+ p = mdoc_macronames[n->tok];
t = "element";
argv = n->data.elem.argv;
argc = n->data.elem.argc;
break;
case (MDOC_BLOCK):
- p = mdoc_macronames[n->data.block.tok];
+ p = mdoc_macronames[n->tok];
t = "block";
argv = n->data.block.argv;
argc = n->data.block.argc;
case (ERR_SYNTAX_CHILDBAD):
lit = "syntax: invalid child for parent macro";
break;
+ case (ERR_SYNTAX_PARENTBAD):
+ lit = "syntax: invalid parent for macro";
+ break;
case (ERR_SYNTAX_CHILDHEAD):
lit = "syntax: expected only block-header section";
break;
lit = "syntax: argument-like value";
break;
case (WARN_SYNTAX_EMPTYBODY):
- lit = "syntax: empty block-body section";
+ lit = "syntax: macro suggests non-empty block-body section";
+ break;
+ case (WARN_SYNTAX_EMPTYHEAD):
+ lit = "syntax: macro suggests non-empty block-head section";
+ break;
+ case (WARN_SYNTAX_NOBODY):
+ lit = "syntax: macro suggests empty block-body section";
break;
case (WARN_SEC_OO):
lit = "section is out of conventional order";
-/* $Id: validate.c,v 1.24 2009/01/12 10:31:53 kristaps Exp $ */
+/* $Id: validate.c,v 1.25 2009/01/12 12:52:21 kristaps Exp $ */
/*
* Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
*
static int pre_display(struct mdoc *, struct mdoc_node *);
static int pre_bd(struct mdoc *, struct mdoc_node *);
static int pre_bl(struct mdoc *, struct mdoc_node *);
+static int pre_it(struct mdoc *, struct mdoc_node *);
static int pre_prologue(struct mdoc *, struct mdoc_node *);
static int pre_prologue(struct mdoc *, struct mdoc_node *);
static int pre_prologue(struct mdoc *, struct mdoc_node *);
static int bodychild_warn_ge1(struct mdoc *);
static int post_sh(struct mdoc *);
static int post_bl(struct mdoc *);
+static int post_it(struct mdoc *);
static v_pre pres_prologue[] = { pre_prologue, NULL };
static v_pre pres_d1[] = { pre_display, NULL };
static v_pre pres_bd[] = { pre_display, pre_bd, NULL };
static v_pre pres_bl[] = { pre_bl, NULL };
+static v_pre pres_it[] = { pre_it, NULL };
static v_post posts_bd[] = { headchild_err_eq0,
bodychild_warn_ge1, NULL };
bodychild_warn_ge1, post_sh, NULL };
static v_post posts_bl[] = { headchild_err_eq0,
bodychild_warn_ge1, post_bl, NULL };
+static v_post posts_it[] = { post_it, NULL };
static v_post posts_ss[] = { headchild_err_ge1, NULL };
static v_post posts_pp[] = { elemchild_warn_eq0, NULL };
static v_post posts_dd[] = { elemchild_err_ge1, NULL };
{ NULL, NULL }, /* Ed */
{ pres_bl, posts_bl }, /* Bl */ /* FIXME: preceding Pp. */
{ NULL, NULL }, /* El */
- { NULL, NULL }, /* It */
+ { pres_it, posts_it }, /* It */
{ NULL, NULL }, /* Ad */
{ NULL, NULL }, /* An */
{ NULL, NULL }, /* Ar */
for (n = mdoc->last; n; n = n->parent)
if (MDOC_BLOCK == n->type)
- if (MDOC_Bd == n->data.block.tok)
+ if (MDOC_Bd == n->tok)
break;
if (NULL == n)
return(1);
if (MDOC_BLOCK != node->type)
return(1);
- assert(MDOC_Bl == node->data.block.tok);
+ assert(MDOC_Bl == node->tok);
argv = NULL;
argc = node->data.block.argc;
if (MDOC_BLOCK != node->type)
return(1);
- assert(MDOC_Bd == node->data.block.tok);
+ assert(MDOC_Bd == node->tok);
argv = NULL;
argc = node->data.block.argc;
}
+static int
+pre_it(struct mdoc *mdoc, struct mdoc_node *node)
+{
+
+ if (MDOC_BLOCK != mdoc->last->type)
+ return(1);
+ assert(MDOC_It == mdoc->last->tok);
+
+ if (MDOC_BODY != mdoc->last->parent->type)
+ return(mdoc_verr(mdoc, node, ERR_SYNTAX_PARENTBAD));
+ if (MDOC_Bl != mdoc->last->parent->tok)
+ return(mdoc_verr(mdoc, node, ERR_SYNTAX_PARENTBAD));
+
+ return(1);
+}
+
+
static int
pre_prologue(struct mdoc *mdoc, struct mdoc_node *node)
{
/* Check for ordering. */
- switch (node->data.elem.tok) {
+ switch (node->tok) {
case (MDOC_Os):
if (mdoc->meta.title[0] && mdoc->meta.date)
break;
/* Check for repetition. */
- switch (node->data.elem.tok) {
+ switch (node->tok) {
case (MDOC_Os):
if (0 == mdoc->meta.os[0])
return(1);
}
+/* Warn if `Bl' type-specific syntax isn't reflected in items. */
+static int
+post_it(struct mdoc *mdoc)
+{
+ int type, sv;
+#define TYPE_NONE (0)
+#define TYPE_BODY (1)
+#define TYPE_HEAD (2)
+ size_t i, argc;
+ struct mdoc_node *n;
+
+ if (MDOC_BLOCK != mdoc->last->type)
+ return(1);
+
+ assert(MDOC_It == mdoc->last->tok);
+
+ n = mdoc->last->parent;
+ assert(n);
+ assert(MDOC_Bl == n->tok);
+
+ n = n->parent;
+ assert(MDOC_BLOCK == n->type);
+ assert(MDOC_Bl == n->tok);
+
+ argc = n->data.block.argc;
+ type = TYPE_NONE;
+
+ for (i = 0; TYPE_NONE == type && i < argc; i++)
+ switch (n->data.block.argv[(int)i].arg) {
+ case (MDOC_Tag):
+ /* FALLTHROUGH */
+ case (MDOC_Diag):
+ /* FALLTHROUGH */
+ case (MDOC_Hang):
+ /* FALLTHROUGH */
+ case (MDOC_Ohang):
+ /* FALLTHROUGH */
+ case (MDOC_Inset):
+ type = TYPE_HEAD;
+ sv = n->data.block.argv[(int)i].arg;
+ break;
+ case (MDOC_Bullet):
+ /* FALLTHROUGH */
+ case (MDOC_Dash):
+ /* FALLTHROUGH */
+ case (MDOC_Enum):
+ /* FALLTHROUGH */
+ case (MDOC_Hyphen):
+ /* FALLTHROUGH */
+ case (MDOC_Item):
+ /* FALLTHROUGH */
+ case (MDOC_Column):
+ type = TYPE_BODY;
+ sv = n->data.block.argv[(int)i].arg;
+ break;
+ default:
+ break;
+ }
+
+ assert(TYPE_NONE != type);
+
+ if (TYPE_HEAD == type) {
+ if (NULL == (n = mdoc->last->data.block.head)) {
+ if ( ! mdoc_warn(mdoc, WARN_SYNTAX_EMPTYHEAD))
+ return(0);
+ } else if (NULL == n->child)
+ if ( ! mdoc_warn(mdoc, WARN_SYNTAX_EMPTYHEAD))
+ return(0);
+
+ if (NULL == (n = mdoc->last->data.block.body)) {
+ if ( ! mdoc_warn(mdoc, WARN_SYNTAX_EMPTYBODY))
+ return(0);
+ } else if (NULL == n->child)
+ if ( ! mdoc_warn(mdoc, WARN_SYNTAX_EMPTYBODY))
+ return(0);
+
+ return(1);
+ }
+
+ if (NULL == (n = mdoc->last->data.block.head)) {
+ if ( ! mdoc_warn(mdoc, WARN_SYNTAX_EMPTYHEAD))
+ return(0);
+ } else if (NULL == n->child)
+ if ( ! mdoc_warn(mdoc, WARN_SYNTAX_EMPTYHEAD))
+ return(0);
+
+ if ((n = mdoc->last->data.block.body) && n->child)
+ if ( ! mdoc_warn(mdoc, WARN_SYNTAX_NOBODY))
+ return(0);
+
+ /* TODO: make sure columns are aligned. */
+ assert(MDOC_Column != sv);
+
+ return(1);
+
+#undef TYPE_NONE
+#undef TYPE_BODY
+#undef TYPE_HEAD
+}
+
+
+/* Make sure that only `It' macros are our body-children. */
static int
post_bl(struct mdoc *mdoc)
{
if (MDOC_BODY != mdoc->last->type)
return(1);
- assert(MDOC_Bl == mdoc->last->data.body.tok);
+ assert(MDOC_Bl == mdoc->last->tok);
for (n = mdoc->last->child; n; n = n->next) {
if (MDOC_BLOCK == n->type)
- if (MDOC_It == n->data.block.tok)
+ if (MDOC_It == n->tok)
continue;
break;
}
}
-/*
- * Warn if sections (those that are with a known title, such as NAME,
- * DESCRIPTION, and so forth) are out of the conventional order.
- */
+/* Warn if conventional sections are out of order. */
static int
post_sh(struct mdoc *mdoc)
{
if (MDOC_HEAD != mdoc->last->type)
return(1);
- assert(MDOC_Sh == mdoc->last->data.head.tok);
+ assert(MDOC_Sh == mdoc->last->tok);
n = mdoc->last->child;
assert(n);
mdoc_valid_pre(struct mdoc *mdoc, struct mdoc_node *node)
{
v_pre *p;
- int t;
- switch (node->type) {
- case (MDOC_BODY):
- t = node->data.body.tok;
- break;
- case (MDOC_ELEM):
- t = node->data.elem.tok;
- break;
- case (MDOC_BLOCK):
- t = node->data.block.tok;
- break;
- case (MDOC_HEAD):
- t = node->data.head.tok;
- break;
- default:
+ /* TODO: character-escape checks. */
+
+ if (MDOC_TEXT == node->type)
return(1);
- }
+ assert(MDOC_ROOT != node->type);
- if (NULL == mdoc_valids[t].pre)
+ if (NULL == mdoc_valids[node->tok].pre)
return(1);
- for (p = mdoc_valids[t].pre; *p; p++)
+ for (p = mdoc_valids[node->tok].pre; *p; p++)
if ( ! (*p)(mdoc, node))
return(0);
return(1);
mdoc_valid_post(struct mdoc *mdoc)
{
v_post *p;
- int t;
- 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:
+ if (MDOC_TEXT == mdoc->last->type)
+ return(1);
+ if (MDOC_ROOT == mdoc->last->type)
return(1);
- }
- if (NULL == mdoc_valids[t].post)
+ if (NULL == mdoc_valids[mdoc->last->tok].post)
return(1);
- for (p = mdoc_valids[t].post; *p; p++)
+ for (p = mdoc_valids[mdoc->last->tok].post; *p; p++)
if ( ! (*p)(mdoc))
return(0);