-/* $Id: eqn.c,v 1.27 2011/07/22 13:40:15 kristaps Exp $ */
+/* $Id: eqn.c,v 1.38 2011/07/25 15:37:00 kristaps Exp $ */
/*
* Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
*
EQNSYM_THETA,
EQNSYM_UPSILON,
EQNSYM_XI,
+ EQNSYM_inter,
+ EQNSYM_union,
+ EQNSYM_prod,
+ EQNSYM_int,
+ EQNSYM_sum,
+ EQNSYM_grad,
+ EQNSYM_del,
+ EQNSYM_times,
+ EQNSYM_cdot,
+ EQNSYM_nothing,
+ EQNSYM_approx,
+ EQNSYM_prime,
+ EQNSYM_half,
+ EQNSYM_partial,
+ EQNSYM_inf,
+ EQNSYM_muchgreat,
+ EQNSYM_muchless,
+ EQNSYM_larrow,
+ EQNSYM_rarrow,
+ EQNSYM_pm,
+ EQNSYM_nequal,
+ EQNSYM_equiv,
+ EQNSYM_lessequal,
+ EQNSYM_moreequal,
EQNSYM__MAX
};
+enum eqnpartt {
+ EQN_DEFINE = 0,
+ EQN_NDEFINE,
+ EQN_TDEFINE,
+ EQN_SET,
+ EQN_UNDEF,
+ EQN_GFONT,
+ EQN_GSIZE,
+ EQN_BACK,
+ EQN_FWD,
+ EQN_UP,
+ EQN_DOWN,
+ EQN__MAX
+};
+
struct eqnstr {
const char *name;
size_t sz;
struct eqnsym {
struct eqnstr str;
- char sym;
+ const char *sym;
};
-enum eqnpartt {
- EQN_DEFINE = 0,
- EQN_SET,
- EQN_UNDEF,
- EQN__MAX
-};
static enum eqn_rest eqn_box(struct eqn_node *, struct eqn_box *);
-static struct eqn_box *eqn_box_alloc(struct eqn_box *);
+static struct eqn_box *eqn_box_alloc(struct eqn_node *,
+ struct eqn_box *);
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_gfont(struct eqn_node *);
+static int eqn_do_gsize(struct eqn_node *);
static int eqn_do_define(struct eqn_node *);
-static int eqn_do_set(struct eqn_node *);
+static int eqn_do_ign1(struct eqn_node *);
+static int eqn_do_ign2(struct eqn_node *);
+static int eqn_do_tdefine(struct eqn_node *);
static int eqn_do_undef(struct eqn_node *);
static enum eqn_rest eqn_eqn(struct eqn_node *, struct eqn_box *);
static enum eqn_rest eqn_list(struct eqn_node *, struct eqn_box *);
+static enum eqn_rest eqn_matrix(struct eqn_node *, struct eqn_box *);
static const char *eqn_nexttok(struct eqn_node *, size_t *);
static const char *eqn_nextrawtok(struct eqn_node *, size_t *);
static const char *eqn_next(struct eqn_node *,
static const struct eqnpart eqnparts[EQN__MAX] = {
{ { "define", 6 }, eqn_do_define }, /* EQN_DEFINE */
- { { "set", 3 }, eqn_do_set }, /* EQN_SET */
+ { { "ndefine", 7 }, eqn_do_define }, /* EQN_NDEFINE */
+ { { "tdefine", 7 }, eqn_do_tdefine }, /* EQN_TDEFINE */
+ { { "set", 3 }, eqn_do_ign2 }, /* EQN_SET */
{ { "undef", 5 }, eqn_do_undef }, /* EQN_UNDEF */
+ { { "gfont", 5 }, eqn_do_gfont }, /* EQN_GFONT */
+ { { "gsize", 5 }, eqn_do_gsize }, /* EQN_GSIZE */
+ { { "back", 4 }, eqn_do_ign1 }, /* EQN_BACK */
+ { { "fwd", 3 }, eqn_do_ign1 }, /* EQN_FWD */
+ { { "up", 2 }, eqn_do_ign1 }, /* EQN_UP */
+ { { "down", 4 }, eqn_do_ign1 }, /* EQN_DOWN */
};
static const struct eqnstr eqnmarks[EQNMARK__MAX] = {
{ "", 0 }, /* EQNFONT_NONE */
{ "roman", 5 }, /* EQNFONT_ROMAN */
{ "bold", 4 }, /* EQNFONT_BOLD */
+ { "fat", 3 }, /* EQNFONT_FAT */
{ "italic", 6 }, /* EQNFONT_ITALIC */
};
static const struct eqnstr eqnpiles[EQNPILE__MAX] = {
{ "", 0 }, /* EQNPILE_NONE */
+ { "pile", 4 }, /* EQNPILE_PILE */
{ "cpile", 5 }, /* EQNPILE_CPILE */
{ "rpile", 5 }, /* EQNPILE_RPILE */
{ "lpile", 5 }, /* EQNPILE_LPILE */
+ { "col", 3 }, /* EQNPILE_COL */
+ { "ccol", 4 }, /* EQNPILE_CCOL */
+ { "rcol", 4 }, /* EQNPILE_RCOL */
+ { "lcol", 4 }, /* EQNPILE_LCOL */
};
static const struct eqnsym eqnsyms[EQNSYM__MAX] = {
- { { "alpha", 5 }, 'a' }, /* EQNSYM_alpha */
- { { "beta", 4 }, 'b' }, /* EQNSYM_beta */
- { { "chi", 3 }, 'x' }, /* EQNSYM_chi */
- { { "delta", 5 }, 'd' }, /* EQNSYM_delta */
- { { "epsilon", 7 }, 'e' }, /* EQNSYM_epsilon */
- { { "eta", 3 }, 'y' }, /* EQNSYM_eta */
- { { "gamma", 5 }, 'g' }, /* EQNSYM_gamma */
- { { "iota", 4 }, 'i' }, /* EQNSYM_iota */
- { { "kappa", 5 }, 'k' }, /* EQNSYM_kappa */
- { { "lambda", 6 }, 'l' }, /* EQNSYM_lambda */
- { { "mu", 2 }, 'm' }, /* EQNSYM_mu */
- { { "nu", 2 }, 'n' }, /* EQNSYM_nu */
- { { "omega", 5 }, 'w' }, /* EQNSYM_omega */
- { { "omicron", 7 }, 'o' }, /* EQNSYM_omicron */
- { { "phi", 3 }, 'f' }, /* EQNSYM_phi */
- { { "pi", 2 }, 'p' }, /* EQNSYM_pi */
- { { "psi", 2 }, 'q' }, /* EQNSYM_psi */
- { { "rho", 3 }, 'r' }, /* EQNSYM_rho */
- { { "sigma", 5 }, 's' }, /* EQNSYM_sigma */
- { { "tau", 3 }, 't' }, /* EQNSYM_tau */
- { { "theta", 5 }, 'h' }, /* EQNSYM_theta */
- { { "upsilon", 7 }, 'u' }, /* EQNSYM_upsilon */
- { { "xi", 2 }, 'c' }, /* EQNSYM_xi */
- { { "zeta", 4 }, 'z' }, /* EQNSYM_zeta */
- { { "DELTA", 5 }, 'D' }, /* EQNSYM_DELTA */
- { { "GAMMA", 5 }, 'G' }, /* EQNSYM_GAMMA */
- { { "LAMBDA", 6 }, 'L' }, /* EQNSYM_LAMBDA */
- { { "OMEGA", 5 }, 'W' }, /* EQNSYM_OMEGA */
- { { "PHI", 3 }, 'F' }, /* EQNSYM_PHI */
- { { "PI", 2 }, 'P' }, /* EQNSYM_PI */
- { { "PSI", 3 }, 'Q' }, /* EQNSYM_PSI */
- { { "SIGMA", 5 }, 'S' }, /* EQNSYM_SIGMA */
- { { "THETA", 5 }, 'H' }, /* EQNSYM_THETA */
- { { "UPSILON", 7 }, 'U' }, /* EQNSYM_UPSILON */
- { { "XI", 2 }, 'C' }, /* EQNSYM_XI */
+ { { "alpha", 5 }, "*a" }, /* EQNSYM_alpha */
+ { { "beta", 4 }, "*b" }, /* EQNSYM_beta */
+ { { "chi", 3 }, "*x" }, /* EQNSYM_chi */
+ { { "delta", 5 }, "*d" }, /* EQNSYM_delta */
+ { { "epsilon", 7 }, "*e" }, /* EQNSYM_epsilon */
+ { { "eta", 3 }, "*y" }, /* EQNSYM_eta */
+ { { "gamma", 5 }, "*g" }, /* EQNSYM_gamma */
+ { { "iota", 4 }, "*i" }, /* EQNSYM_iota */
+ { { "kappa", 5 }, "*k" }, /* EQNSYM_kappa */
+ { { "lambda", 6 }, "*l" }, /* EQNSYM_lambda */
+ { { "mu", 2 }, "*m" }, /* EQNSYM_mu */
+ { { "nu", 2 }, "*n" }, /* EQNSYM_nu */
+ { { "omega", 5 }, "*w" }, /* EQNSYM_omega */
+ { { "omicron", 7 }, "*o" }, /* EQNSYM_omicron */
+ { { "phi", 3 }, "*f" }, /* EQNSYM_phi */
+ { { "pi", 2 }, "*p" }, /* EQNSYM_pi */
+ { { "psi", 2 }, "*q" }, /* EQNSYM_psi */
+ { { "rho", 3 }, "*r" }, /* EQNSYM_rho */
+ { { "sigma", 5 }, "*s" }, /* EQNSYM_sigma */
+ { { "tau", 3 }, "*t" }, /* EQNSYM_tau */
+ { { "theta", 5 }, "*h" }, /* EQNSYM_theta */
+ { { "upsilon", 7 }, "*u" }, /* EQNSYM_upsilon */
+ { { "xi", 2 }, "*c" }, /* EQNSYM_xi */
+ { { "zeta", 4 }, "*z" }, /* EQNSYM_zeta */
+ { { "DELTA", 5 }, "*D" }, /* EQNSYM_DELTA */
+ { { "GAMMA", 5 }, "*G" }, /* EQNSYM_GAMMA */
+ { { "LAMBDA", 6 }, "*L" }, /* EQNSYM_LAMBDA */
+ { { "OMEGA", 5 }, "*W" }, /* EQNSYM_OMEGA */
+ { { "PHI", 3 }, "*F" }, /* EQNSYM_PHI */
+ { { "PI", 2 }, "*P" }, /* EQNSYM_PI */
+ { { "PSI", 3 }, "*Q" }, /* EQNSYM_PSI */
+ { { "SIGMA", 5 }, "*S" }, /* EQNSYM_SIGMA */
+ { { "THETA", 5 }, "*H" }, /* EQNSYM_THETA */
+ { { "UPSILON", 7 }, "*U" }, /* EQNSYM_UPSILON */
+ { { "XI", 2 }, "*C" }, /* EQNSYM_XI */
+ { { "inter", 5 }, "ca" }, /* EQNSYM_inter */
+ { { "union", 5 }, "cu" }, /* EQNSYM_union */
+ { { "prod", 4 }, "product" }, /* EQNSYM_prod */
+ { { "int", 3 }, "integral" }, /* EQNSYM_int */
+ { { "sum", 3 }, "sum" }, /* EQNSYM_sum */
+ { { "grad", 4 }, "gr" }, /* EQNSYM_grad */
+ { { "del", 3 }, "gr" }, /* EQNSYM_del */
+ { { "times", 5 }, "mu" }, /* EQNSYM_times */
+ { { "cdot", 4 }, "pc" }, /* EQNSYM_cdot */
+ { { "nothing", 7 }, "&" }, /* EQNSYM_nothing */
+ { { "approx", 6 }, "~~" }, /* EQNSYM_approx */
+ { { "prime", 5 }, "aq" }, /* EQNSYM_prime */
+ { { "half", 4 }, "12" }, /* EQNSYM_half */
+ { { "partial", 7 }, "pd" }, /* EQNSYM_partial */
+ { { "inf", 3 }, "if" }, /* EQNSYM_inf */
+ { { ">>", 2 }, ">>" }, /* EQNSYM_muchgreat */
+ { { "<<", 2 }, "<<" }, /* EQNSYM_muchless */
+ { { "<-", 2 }, "<-" }, /* EQNSYM_larrow */
+ { { "->", 2 }, "->" }, /* EQNSYM_rarrow */
+ { { "+-", 2 }, "+-" }, /* EQNSYM_pm */
+ { { "!=", 2 }, "!=" }, /* EQNSYM_nequal */
+ { { "==", 2 }, "==" }, /* EQNSYM_equiv */
+ { { "<=", 2 }, "<=" }, /* EQNSYM_lessequal */
+ { { ">=", 2 }, ">=" }, /* EQNSYM_moreequal */
};
/* ARGSUSED */
* validate the full equation.
*/
- if (0 == strcmp(p, ".EN")) {
- er = eqn_end(ep);
- *epp = NULL;
+ if (0 == strncmp(p, ".EN", 3)) {
+ er = eqn_end(epp);
+ p += 3;
+ while (' ' == *p || '\t' == *p)
+ p++;
+ if ('\0' == *p)
+ return(er);
+ mandoc_msg(MANDOCERR_ARGSLOST, ep->parse, ln, pos, NULL);
return(er);
}
}
struct eqn_node *
-eqn_alloc(int pos, int line, struct mparse *parse)
+eqn_alloc(const char *name, int pos, int line, struct mparse *parse)
{
struct eqn_node *p;
+ size_t sz;
+ const char *end;
p = mandoc_calloc(1, sizeof(struct eqn_node));
+
+ if (name && '\0' != *name) {
+ sz = strlen(name);
+ assert(sz);
+ do {
+ sz--;
+ end = name + (int)sz;
+ } while (' ' == *end || '\t' == *end);
+ p->eqn.name = mandoc_strndup(name, sz + 1);
+ }
+
p->parse = parse;
p->eqn.ln = line;
p->eqn.pos = pos;
+ p->gsize = EQN_DEFSIZE;
return(p);
}
enum rofferr
-eqn_end(struct eqn_node *ep)
+eqn_end(struct eqn_node **epp)
{
+ struct eqn_node *ep;
struct eqn_box *root;
enum eqn_rest c;
+ ep = *epp;
+ *epp = NULL;
+
ep->eqn.root = mandoc_calloc(1, sizeof(struct eqn_box));
root = ep->eqn.root;
struct eqn_box *bp;
enum eqn_rest c;
- bp = eqn_box_alloc(last);
+ bp = eqn_box_alloc(ep, last);
bp->type = EQN_SUBEXPR;
while (EQN_OK == (c = eqn_box(ep, bp)))
return(c);
}
+static enum eqn_rest
+eqn_matrix(struct eqn_node *ep, struct eqn_box *last)
+{
+ struct eqn_box *bp;
+ const char *start;
+ size_t sz;
+ enum eqn_rest c;
+
+ bp = eqn_box_alloc(ep, last);
+ bp->type = EQN_MATRIX;
+
+ if (NULL == (start = eqn_nexttok(ep, &sz))) {
+ EQN_MSG(MANDOCERR_EQNEOF, ep);
+ return(EQN_ERR);
+ }
+ if ( ! STRNEQ(start, sz, "{", 1)) {
+ EQN_MSG(MANDOCERR_EQNSYNT, ep);
+ return(EQN_ERR);
+ }
+
+ while (EQN_OK == (c = eqn_box(ep, bp)))
+ switch (bp->last->pile) {
+ case (EQNPILE_LCOL):
+ /* FALLTHROUGH */
+ case (EQNPILE_CCOL):
+ /* FALLTHROUGH */
+ case (EQNPILE_RCOL):
+ continue;
+ default:
+ EQN_MSG(MANDOCERR_EQNSYNT, ep);
+ return(EQN_ERR);
+ };
+
+ if (EQN_DESCOPE != c) {
+ if (EQN_EOF == c)
+ EQN_MSG(MANDOCERR_EQNEOF, ep);
+ return(EQN_ERR);
+ }
+
+ eqn_rewind(ep);
+ start = eqn_nexttok(ep, &sz);
+ assert(start);
+ if (STRNEQ(start, sz, "}", 1))
+ return(EQN_OK);
+
+ EQN_MSG(MANDOCERR_EQNBADSCOPE, ep);
+ return(EQN_ERR);
+}
+
static enum eqn_rest
eqn_list(struct eqn_node *ep, struct eqn_box *last)
{
size_t sz;
enum eqn_rest c;
- bp = eqn_box_alloc(last);
+ bp = eqn_box_alloc(ep, last);
bp->type = EQN_LIST;
if (NULL == (start = eqn_nexttok(ep, &sz))) {
assert(start);
if ( ! STRNEQ(start, sz, "above", 5))
break;
- bp->last->above = 1;
}
if (EQN_DESCOPE != c) {
size_t sz;
const char *start;
char *left;
- char sym[5];
+ char sym[64];
enum eqn_rest c;
int i, size;
struct eqn_box *bp;
return(EQN_DESCOPE);
else if (STRNEQ(start, sz, "above", 5))
return(EQN_DESCOPE);
+ else if (STRNEQ(start, sz, "mark", 4))
+ return(EQN_OK);
+ else if (STRNEQ(start, sz, "lineup", 6))
+ return(EQN_OK);
for (i = 0; i < (int)EQN__MAX; i++) {
if ( ! EQNSTREQ(&eqnparts[i].str, start, sz))
continue;
- return((*eqnparts[i].fp)(ep) ? EQN_OK : EQN_ERR);
+ return((*eqnparts[i].fp)(ep) ?
+ EQN_OK : EQN_ERR);
}
if (STRNEQ(start, sz, "{", 1)) {
return(c);
}
+ if (STRNEQ(start, sz, "matrix", 6))
+ return(eqn_matrix(ep, last));
+
if (STRNEQ(start, sz, "left", 4)) {
if (NULL == (start = eqn_nexttok(ep, &sz))) {
EQN_MSG(MANDOCERR_EQNEOF, ep);
last->last->size = size;
}
- bp = eqn_box_alloc(last);
+ bp = eqn_box_alloc(ep, last);
bp->type = EQN_TEXT;
for (i = 0; i < (int)EQNSYM__MAX; i++)
if (EQNSTREQ(&eqnsyms[i].str, start, sz)) {
- sym[0] = '\\';
- sym[1] = '(';
- sym[2] = '*';
- sym[3] = eqnsyms[i].sym;
- sym[4] = '\0';
+ sym[63] = '\0';
+ snprintf(sym, 62, "\\[%s]", eqnsyms[i].sym);
bp->text = mandoc_strdup(sym);
return(EQN_OK);
}
free(p->defs[i].val);
}
+ free(p->eqn.name);
free(p->data);
free(p->defs);
free(p);
}
static struct eqn_box *
-eqn_box_alloc(struct eqn_box *parent)
+eqn_box_alloc(struct eqn_node *ep, struct eqn_box *parent)
{
struct eqn_box *bp;
bp = mandoc_calloc(1, sizeof(struct eqn_box));
bp->parent = parent;
- bp->size = EQN_DEFSIZE;
+ bp->size = ep->gsize;
if (NULL == parent->first)
parent->first = bp;
/* Prevent self-definitions. */
if (lim >= EQN_NEST_MAX) {
- EQN_MSG(MANDOCERR_EQNNEST, ep);
+ EQN_MSG(MANDOCERR_ROFFLOOP, ep);
return(NULL);
}
if ('{' == *start || '}' == *start)
ssz = 1;
else
- ssz = strcspn(start + 1, " ~\"{}\t") + 1;
+ ssz = strcspn(start + 1, " ^~\"{}\t") + 1;
next = start + (int)ssz;
if ('\0' == *next)
next = NULL;
ep->cur++;
while (' ' == ep->data[(int)ep->cur] ||
'\t' == ep->data[(int)ep->cur] ||
+ '^' == ep->data[(int)ep->cur] ||
'~' == ep->data[(int)ep->cur])
ep->cur++;
} else {
}
static int
-eqn_do_set(struct eqn_node *ep)
+eqn_do_ign1(struct eqn_node *ep)
+{
+
+ if (NULL == eqn_nextrawtok(ep, NULL))
+ EQN_MSG(MANDOCERR_EQNEOF, ep);
+ else
+ return(1);
+
+ return(0);
+}
+
+static int
+eqn_do_ign2(struct eqn_node *ep)
+{
+
+ if (NULL == eqn_nextrawtok(ep, NULL))
+ EQN_MSG(MANDOCERR_EQNEOF, ep);
+ else if (NULL == eqn_nextrawtok(ep, NULL))
+ EQN_MSG(MANDOCERR_EQNEOF, ep);
+ else
+ return(1);
+
+ return(0);
+}
+
+static int
+eqn_do_tdefine(struct eqn_node *ep)
{
- const char *start;
- if (NULL == (start = eqn_nextrawtok(ep, NULL)))
- EQN_MSG(MANDOCERR_EQNARGS, ep);
- else if (NULL == (start = eqn_nextrawtok(ep, NULL)))
- EQN_MSG(MANDOCERR_EQNARGS, ep);
+ if (NULL == eqn_nextrawtok(ep, NULL))
+ EQN_MSG(MANDOCERR_EQNEOF, ep);
+ else if (NULL == eqn_next(ep, ep->data[(int)ep->cur], NULL, 0))
+ EQN_MSG(MANDOCERR_EQNEOF, ep);
else
return(1);
int i;
if (NULL == (start = eqn_nextrawtok(ep, &sz))) {
- EQN_MSG(MANDOCERR_EQNARGS, ep);
+ EQN_MSG(MANDOCERR_EQNEOF, ep);
return(0);
}
start = eqn_next(ep, ep->data[(int)ep->cur], &sz, 0);
if (NULL == start) {
- EQN_MSG(MANDOCERR_EQNARGS, ep);
+ EQN_MSG(MANDOCERR_EQNEOF, ep);
return(0);
}
return(1);
}
+static int
+eqn_do_gfont(struct eqn_node *ep)
+{
+
+ if (NULL == eqn_nextrawtok(ep, NULL)) {
+ EQN_MSG(MANDOCERR_EQNEOF, ep);
+ return(0);
+ }
+ return(1);
+}
+
+static int
+eqn_do_gsize(struct eqn_node *ep)
+{
+ const char *start;
+ size_t sz;
+
+ if (NULL == (start = eqn_nextrawtok(ep, &sz))) {
+ EQN_MSG(MANDOCERR_EQNEOF, ep);
+ return(0);
+ }
+ ep->gsize = mandoc_strntoi(start, sz, 10);
+ return(1);
+}
+
static int
eqn_do_undef(struct eqn_node *ep)
{
size_t sz;
if (NULL == (start = eqn_nextrawtok(ep, &sz))) {
- EQN_MSG(MANDOCERR_EQNARGS, ep);
+ EQN_MSG(MANDOCERR_EQNEOF, ep);
return(0);
} else if (NULL != (def = eqn_def_find(ep, start, sz)))
def->keysz = 0;