=================================================================== RCS file: /cvs/mandoc/eqn.c,v retrieving revision 1.13 retrieving revision 1.18 diff -u -p -r1.13 -r1.18 --- mandoc/eqn.c 2011/07/21 11:34:53 1.13 +++ mandoc/eqn.c 2011/07/21 14:13:00 1.18 @@ -1,4 +1,4 @@ -/* $Id: eqn.c,v 1.13 2011/07/21 11:34:53 kristaps Exp $ */ +/* $Id: eqn.c,v 1.18 2011/07/21 14:13:00 kristaps Exp $ */ /* * Copyright (c) 2011 Kristaps Dzonsons * @@ -31,9 +31,13 @@ #define EQN_NEST_MAX 128 /* maximum nesting of defines */ #define EQN_MSG(t, x) mandoc_msg((t), (x)->parse, (x)->eqn.ln, (x)->eqn.pos, NULL) -struct eqnpart { +struct eqnstr { const char *name; size_t sz; +}; + +struct eqnpart { + struct eqnstr str; int (*fp)(struct eqn_node *); }; @@ -48,18 +52,50 @@ static void eqn_box_free(struct eqn_box *); static struct eqn_def *eqn_def_find(struct eqn_node *, const char *, size_t); static int eqn_do_define(struct eqn_node *); -static int eqn_do_ign2(struct eqn_node *); +static int eqn_do_set(struct eqn_node *); static int eqn_do_undef(struct eqn_node *); static const char *eqn_nexttok(struct eqn_node *, size_t *); -static const char *eqn_next(struct eqn_node *, char, size_t *); -static int eqn_box(struct eqn_node *, struct eqn_box *); +static const char *eqn_nextrawtok(struct eqn_node *, size_t *); +static const char *eqn_next(struct eqn_node *, + char, size_t *, int); +static int eqn_box(struct eqn_node *, + struct eqn_box *, struct eqn_box **); static const struct eqnpart eqnparts[EQN__MAX] = { - { "define", 6, eqn_do_define }, /* EQN_DEFINE */ - { "set", 3, eqn_do_ign2 }, /* EQN_SET */ - { "undef", 5, eqn_do_undef }, /* EQN_UNDEF */ + { { "define", 6 }, eqn_do_define }, /* EQN_DEFINE */ + { { "set", 3 }, eqn_do_set }, /* EQN_SET */ + { { "undef", 5 }, eqn_do_undef }, /* EQN_UNDEF */ }; +static const struct eqnstr eqnmarks[EQNMARK__MAX] = { + { "", 0 }, /* EQNMARK_NONE */ + { "dot", 3 }, /* EQNMARK_DOT */ + { "dotdot", 6 }, /* EQNMARK_DOTDOT */ + { "hat", 3 }, /* EQNMARK_HAT */ + { "tilde", 5 }, /* EQNMARK_TILDE */ + { "vec", 3 }, /* EQNMARK_VEC */ + { "dyad", 4 }, /* EQNMARK_DYAD */ + { "bar", 3 }, /* EQNMARK_BAR */ + { "under", 5 }, /* EQNMARK_UNDER */ +}; + +static const struct eqnstr eqnfonts[EQNFONT__MAX] = { + { "", 0 }, + { "roman", 5 }, + { "bold", 4 }, + { "italic", 6 }, +}; + +static const struct eqnstr eqnposs[EQNPOS__MAX] = { + { "", 0 }, + { "over", 4 }, + { "sup", 3 }, + { "sub", 3 }, + { "to", 2 }, + { "from", 4 }, + { "above", 5 }, +}; + /* ARGSUSED */ enum rofferr eqn_read(struct eqn_node **epp, int ln, @@ -117,7 +153,8 @@ eqn_alloc(int pos, int line, struct mparse *parse) enum rofferr eqn_end(struct eqn_node *ep) { - struct eqn_box *root; + struct eqn_box *root, *last; + int c; ep->eqn.root = root = mandoc_calloc(1, sizeof(struct eqn_box)); @@ -127,50 +164,120 @@ eqn_end(struct eqn_node *ep) return(ROFF_IGN); /* - * Validate the expression. - * Use the grammar found in the literature. + * Run the parser. + * If we return before reaching the end of our input, our scope + * is still open somewhere. + * If we return alright but don't have a symmetric scoping, then + * something's not right either. + * Otherwise, return the equation. */ - return(eqn_box(ep, root) < 0 ? ROFF_IGN : ROFF_EQN); + if (0 == (c = eqn_box(ep, root, &last))) { + if (last != root) { + EQN_MSG(MANDOCERR_EQNSCOPE, ep); + c = 0; + } + } else if (c > 0) + EQN_MSG(MANDOCERR_EQNNSCOPE, ep); + + return(0 == c ? ROFF_EQN : ROFF_IGN); } static int -eqn_box(struct eqn_node *ep, struct eqn_box *last) +eqn_box(struct eqn_node *ep, struct eqn_box *last, struct eqn_box **sv) { size_t sz; const char *start; - int i, nextc; + int c, i, nextc; + enum eqn_fontt font; struct eqn_box *bp; + /* + * Mark our last level of subexpression. + * Also mark whether that the next node should be a + * subexpression node. + */ + + *sv = last; nextc = 1; + font = EQNFONT_NONE; again: if (NULL == (start = eqn_nexttok(ep, &sz))) return(0); + for (i = 0; i < (int)EQNFONT__MAX; i++) { + if (eqnfonts[i].sz != sz) + continue; + if (strncmp(eqnfonts[i].name, start, sz)) + continue; + font = (enum eqn_fontt)i; + goto again; + } + + for (i = 0; i < (int)EQNFONT__MAX; i++) { + if (eqnposs[i].sz != sz) + continue; + if (strncmp(eqnposs[i].name, start, sz)) + continue; + last->pos = (enum eqn_post)i; + goto again; + } + for (i = 0; i < (int)EQN__MAX; i++) { - if (eqnparts[i].sz != sz) + if (eqnparts[i].str.sz != sz) continue; - if (strncmp(eqnparts[i].name, start, sz)) + if (strncmp(eqnparts[i].str.name, start, sz)) continue; if ( ! (*eqnparts[i].fp)(ep)) return(-1); - goto again; } + for (i = 0; i < (int)EQNMARK__MAX; i++) { + if (eqnmarks[i].sz != sz) + continue; + if (strncmp(eqnmarks[i].name, start, sz)) + continue; + last->mark = (enum eqn_markt)i; + goto again; + } + + /* Exit this [hopefully] subexpression. */ + + if (sz == 1 && 0 == strncmp("}", start, 1)) + return(1); + bp = mandoc_calloc(1, sizeof(struct eqn_box)); - bp->type = EQN_TEXT; + bp->font = font; + font = EQNFONT_NONE; if (nextc) last->child = bp; else last->next = bp; + last = bp; + + /* + * See if we're to open a new subexpression. + * If so, mark our node as such and descend. + */ + + if (sz == 1 && 0 == strncmp("{", start, 1)) { + bp->type = EQN_SUBEXPR; + c = eqn_box(ep, bp, sv); + + nextc = 0; + goto again; + } + + /* A regular text node. */ + + bp->type = EQN_TEXT; bp->text = mandoc_malloc(sz + 1); *bp->text = '\0'; strlcat(bp->text, start, sz + 1); - last = bp; nextc = 0; goto again; } @@ -206,14 +313,21 @@ eqn_box_free(struct eqn_box *bp) } static const char * +eqn_nextrawtok(struct eqn_node *ep, size_t *sz) +{ + + return(eqn_next(ep, '"', sz, 0)); +} + +static const char * eqn_nexttok(struct eqn_node *ep, size_t *sz) { - return(eqn_next(ep, '"', sz)); + return(eqn_next(ep, '"', sz, 1)); } static const char * -eqn_next(struct eqn_node *ep, char quote, size_t *sz) +eqn_next(struct eqn_node *ep, char quote, size_t *sz, int repl) { char *start, *next; int q, diff, lim; @@ -265,7 +379,7 @@ again: /* Quotes aren't expanded for values. */ - if (q) + if (q || ! repl) return(start); if (NULL != (def = eqn_def_find(ep, start, *sz))) { @@ -289,13 +403,13 @@ again: } static int -eqn_do_ign2(struct eqn_node *ep) +eqn_do_set(struct eqn_node *ep) { const char *start; - if (NULL == (start = eqn_nexttok(ep, NULL))) + if (NULL == (start = eqn_nextrawtok(ep, NULL))) EQN_MSG(MANDOCERR_EQNARGS, ep); - else if (NULL == (start = eqn_nexttok(ep, NULL))) + else if (NULL == (start = eqn_nextrawtok(ep, NULL))) EQN_MSG(MANDOCERR_EQNARGS, ep); else return(1); @@ -311,7 +425,7 @@ eqn_do_define(struct eqn_node *ep) struct eqn_def *def; int i; - if (NULL == (start = eqn_nexttok(ep, &sz))) { + if (NULL == (start = eqn_nextrawtok(ep, &sz))) { EQN_MSG(MANDOCERR_EQNARGS, ep); return(0); } @@ -344,7 +458,7 @@ eqn_do_define(struct eqn_node *ep) def = &ep->defs[i]; } - start = eqn_next(ep, ep->data[(int)ep->cur], &sz); + start = eqn_next(ep, ep->data[(int)ep->cur], &sz, 0); if (NULL == start) { EQN_MSG(MANDOCERR_EQNARGS, ep); @@ -355,9 +469,6 @@ eqn_do_define(struct eqn_node *ep) def->val = mandoc_realloc(def->val, sz + 1); memcpy(def->val, start, sz); def->val[(int)sz] = '\0'; - - /*fprintf(stderr, "Defining: [%s], [%s]\n", - def->key, def->val);*/ return(1); } @@ -368,7 +479,7 @@ eqn_do_undef(struct eqn_node *ep) struct eqn_def *def; size_t sz; - if (NULL == (start = eqn_nexttok(ep, &sz))) { + if (NULL == (start = eqn_nextrawtok(ep, &sz))) { EQN_MSG(MANDOCERR_EQNARGS, ep); return(0); } else if (NULL != (def = eqn_def_find(ep, start, sz)))