1 /* $Id: eqn.c,v 1.58 2015/03/04 12:19:49 schwarze Exp $ */
3 * Copyright (c) 2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/types.h>
30 #include "mandoc_aux.h"
31 #include "libmandoc.h"
34 #define EQN_NEST_MAX 128 /* maximum nesting of defines */
35 #define STRNEQ(p1, sz1, p2, sz2) \
36 ((sz1) == (sz2) && 0 == strncmp((p1), (p2), (sz1)))
88 static const char *eqn_toks[EQN_TOK__MAX] = {
89 "dyad", /* EQN_TOK_DYAD */
90 "vec", /* EQN_TOK_VEC */
91 "under", /* EQN_TOK_UNDER */
92 "bar", /* EQN_TOK_BAR */
93 "tilde", /* EQN_TOK_TILDE */
94 "hat", /* EQN_TOK_HAT */
95 "dot", /* EQN_TOK_DOT */
96 "dotdot", /* EQN_TOK_DOTDOT */
97 "fwd", /* EQN_TOK_FWD * */
98 "back", /* EQN_TOK_BACK */
99 "down", /* EQN_TOK_DOWN */
100 "up", /* EQN_TOK_UP */
101 "fat", /* EQN_TOK_FAT */
102 "roman", /* EQN_TOK_ROMAN */
103 "italic", /* EQN_TOK_ITALIC */
104 "bold", /* EQN_TOK_BOLD */
105 "size", /* EQN_TOK_SIZE */
106 "sub", /* EQN_TOK_SUB */
107 "sup", /* EQN_TOK_SUP */
108 "sqrt", /* EQN_TOK_SQRT */
109 "over", /* EQN_TOK_OVER */
110 "from", /* EQN_TOK_FROM */
111 "to", /* EQN_TOK_TO */
112 "{", /* EQN_TOK_BRACE_OPEN */
113 "}", /* EQN_TOK_BRACE_CLOSE */
114 "gsize", /* EQN_TOK_GSIZE */
115 "gfont", /* EQN_TOK_GFONT */
116 "mark", /* EQN_TOK_MARK */
117 "lineup", /* EQN_TOK_LINEUP */
118 "left", /* EQN_TOK_LEFT */
119 "right", /* EQN_TOK_RIGHT */
120 "pile", /* EQN_TOK_PILE */
121 "lpile", /* EQN_TOK_LPILE */
122 "rpile", /* EQN_TOK_RPILE */
123 "cpile", /* EQN_TOK_CPILE */
124 "matrix", /* EQN_TOK_MATRIX */
125 "ccol", /* EQN_TOK_CCOL */
126 "lcol", /* EQN_TOK_LCOL */
127 "rcol", /* EQN_TOK_RCOL */
128 "delim", /* EQN_TOK_DELIM */
129 "define", /* EQN_TOK_DEFINE */
130 "tdefine", /* EQN_TOK_TDEFINE */
131 "ndefine", /* EQN_TOK_NDEFINE */
132 "undef", /* EQN_TOK_UNDEF */
133 NULL, /* EQN_TOK_EOF */
134 "above", /* EQN_TOK_ABOVE */
206 static const struct eqnsym eqnsyms[EQNSYM__MAX] = {
207 { "alpha", "*a" }, /* EQNSYM_alpha */
208 { "beta", "*b" }, /* EQNSYM_beta */
209 { "chi", "*x" }, /* EQNSYM_chi */
210 { "delta", "*d" }, /* EQNSYM_delta */
211 { "epsilon", "*e" }, /* EQNSYM_epsilon */
212 { "eta", "*y" }, /* EQNSYM_eta */
213 { "gamma", "*g" }, /* EQNSYM_gamma */
214 { "iota", "*i" }, /* EQNSYM_iota */
215 { "kappa", "*k" }, /* EQNSYM_kappa */
216 { "lambda", "*l" }, /* EQNSYM_lambda */
217 { "mu", "*m" }, /* EQNSYM_mu */
218 { "nu", "*n" }, /* EQNSYM_nu */
219 { "omega", "*w" }, /* EQNSYM_omega */
220 { "omicron", "*o" }, /* EQNSYM_omicron */
221 { "phi", "*f" }, /* EQNSYM_phi */
222 { "pi", "*p" }, /* EQNSYM_pi */
223 { "psi", "*q" }, /* EQNSYM_psi */
224 { "rho", "*r" }, /* EQNSYM_rho */
225 { "sigma", "*s" }, /* EQNSYM_sigma */
226 { "tau", "*t" }, /* EQNSYM_tau */
227 { "theta", "*h" }, /* EQNSYM_theta */
228 { "upsilon", "*u" }, /* EQNSYM_upsilon */
229 { "xi", "*c" }, /* EQNSYM_xi */
230 { "zeta", "*z" }, /* EQNSYM_zeta */
231 { "DELTA", "*D" }, /* EQNSYM_DELTA */
232 { "GAMMA", "*G" }, /* EQNSYM_GAMMA */
233 { "LAMBDA", "*L" }, /* EQNSYM_LAMBDA */
234 { "OMEGA", "*W" }, /* EQNSYM_OMEGA */
235 { "PHI", "*F" }, /* EQNSYM_PHI */
236 { "PI", "*P" }, /* EQNSYM_PI */
237 { "PSI", "*Q" }, /* EQNSYM_PSI */
238 { "SIGMA", "*S" }, /* EQNSYM_SIGMA */
239 { "THETA", "*H" }, /* EQNSYM_THETA */
240 { "UPSILON", "*U" }, /* EQNSYM_UPSILON */
241 { "XI", "*C" }, /* EQNSYM_XI */
242 { "inter", "ca" }, /* EQNSYM_inter */
243 { "union", "cu" }, /* EQNSYM_union */
244 { "prod", "product" }, /* EQNSYM_prod */
245 { "int", "integral" }, /* EQNSYM_int */
246 { "sum", "sum" }, /* EQNSYM_sum */
247 { "grad", "gr" }, /* EQNSYM_grad */
248 { "del", "gr" }, /* EQNSYM_del */
249 { "times", "mu" }, /* EQNSYM_times */
250 { "cdot", "pc" }, /* EQNSYM_cdot */
251 { "nothing", "&" }, /* EQNSYM_nothing */
252 { "approx", "~~" }, /* EQNSYM_approx */
253 { "prime", "fm" }, /* EQNSYM_prime */
254 { "half", "12" }, /* EQNSYM_half */
255 { "partial", "pd" }, /* EQNSYM_partial */
256 { "inf", "if" }, /* EQNSYM_inf */
257 { ">>", ">>" }, /* EQNSYM_muchgreat */
258 { "<<", "<<" }, /* EQNSYM_muchless */
259 { "<-", "<-" }, /* EQNSYM_larrow */
260 { "->", "->" }, /* EQNSYM_rarrow */
261 { "+-", "+-" }, /* EQNSYM_pm */
262 { "!=", "!=" }, /* EQNSYM_nequal */
263 { "==", "==" }, /* EQNSYM_equiv */
264 { "<=", "<=" }, /* EQNSYM_lessequal */
265 { ">=", ">=" }, /* EQNSYM_moreequal */
266 { "-", "mi" }, /* EQNSYM_minus */
269 static struct eqn_box *eqn_box_alloc(struct eqn_node *, struct eqn_box *);
270 static void eqn_box_free(struct eqn_box *);
271 static struct eqn_box *eqn_box_makebinary(struct eqn_node *,
272 enum eqn_post, struct eqn_box *);
273 static void eqn_def(struct eqn_node *);
274 static struct eqn_def *eqn_def_find(struct eqn_node *, const char *, size_t);
275 static void eqn_delim(struct eqn_node *);
276 static const char *eqn_next(struct eqn_node *, char, size_t *, int);
277 static const char *eqn_nextrawtok(struct eqn_node *, size_t *);
278 static const char *eqn_nexttok(struct eqn_node *, size_t *);
279 static enum rofferr eqn_parse(struct eqn_node *, struct eqn_box *);
280 static enum eqn_tok eqn_tok_parse(struct eqn_node *, char **);
281 static void eqn_undef(struct eqn_node *);
285 eqn_read(struct eqn_node **epp, int ln,
286 const char *p, int pos, int *offs)
295 * If we're the terminating mark, unset our equation status and
296 * validate the full equation.
299 if (0 == strncmp(p, ".EN", 3)) {
302 while (' ' == *p || '\t' == *p)
306 mandoc_vmsg(MANDOCERR_ARG_SKIP, ep->parse,
307 ln, pos, "EN %s", p);
312 * Build up the full string, replacing all newlines with regular
316 sz = strlen(p + pos) + 1;
317 ep->data = mandoc_realloc(ep->data, ep->sz + sz + 1);
319 /* First invocation: nil terminate the string. */
325 strlcat(ep->data, p + pos, ep->sz + 1);
326 strlcat(ep->data, " ", ep->sz + 1);
331 eqn_alloc(int pos, int line, struct mparse *parse)
335 p = mandoc_calloc(1, sizeof(struct eqn_node));
340 p->gsize = EQN_DEFSIZE;
346 * Find the key "key" of the give size within our eqn-defined values.
348 static struct eqn_def *
349 eqn_def_find(struct eqn_node *ep, const char *key, size_t sz)
353 for (i = 0; i < (int)ep->defsz; i++)
354 if (ep->defs[i].keysz && STRNEQ(ep->defs[i].key,
355 ep->defs[i].keysz, key, sz))
356 return(&ep->defs[i]);
362 * Get the next token from the input stream using the given quote
364 * Optionally make any replacements.
367 eqn_next(struct eqn_node *ep, char quote, size_t *sz, int repl)
380 /* Prevent self-definitions. */
382 if (lim >= EQN_NEST_MAX) {
383 mandoc_msg(MANDOCERR_ROFFLOOP, ep->parse,
384 ep->eqn.ln, ep->eqn.pos, NULL);
389 start = &ep->data[(int)ep->cur];
395 if (quote == *start) {
400 start = &ep->data[(int)ep->cur];
403 if ('{' == *start || '}' == *start)
406 ssz = strcspn(start + 1, " ^~\"{}\t") + 1;
407 next = start + (int)ssz;
411 next = strchr(start, quote);
414 *sz = (size_t)(next - start);
418 while (' ' == ep->data[(int)ep->cur] ||
419 '\t' == ep->data[(int)ep->cur] ||
420 '^' == ep->data[(int)ep->cur] ||
421 '~' == ep->data[(int)ep->cur])
425 mandoc_msg(MANDOCERR_ARG_QUOTE, ep->parse,
426 ep->eqn.ln, ep->eqn.pos, NULL);
427 next = strchr(start, '\0');
428 *sz = (size_t)(next - start);
432 /* Quotes aren't expanded for values. */
437 if (NULL != (def = eqn_def_find(ep, start, *sz))) {
438 diff = def->valsz - *sz;
440 if (def->valsz > *sz) {
442 ep->data = mandoc_realloc(ep->data, ep->sz + 1);
443 ep->data[ep->sz] = '\0';
444 start = &ep->data[(int)ep->rew];
447 diff = def->valsz - *sz;
448 memmove(start + *sz + diff, start + *sz,
449 (strlen(start) - *sz) + 1);
450 memcpy(start, def->val, def->valsz);
458 * Get the next delimited token using the default current quote
462 eqn_nexttok(struct eqn_node *ep, size_t *sz)
465 return(eqn_next(ep, '"', sz, 1));
469 * Get next token without replacement.
472 eqn_nextrawtok(struct eqn_node *ep, size_t *sz)
475 return(eqn_next(ep, '"', sz, 0));
479 * Parse a token from the stream of text.
480 * A token consists of one of the recognised eqn(7) strings.
481 * Strings are separated by delimiting marks.
482 * This returns EQN_TOK_EOF when there are no more tokens.
483 * If the token is an unrecognised string literal, then it returns
484 * EQN_TOK__MAX and sets the "p" pointer to an allocated, nil-terminated
486 * This must be later freed with free(3).
489 eqn_tok_parse(struct eqn_node *ep, char **p)
498 quoted = ep->data[ep->cur] == '"';
500 if (NULL == (start = eqn_nexttok(ep, &sz)))
505 *p = mandoc_strndup(start, sz);
506 return(EQN_TOK__MAX);
509 for (i = 0; i < EQN_TOK__MAX; i++) {
510 if (NULL == eqn_toks[i])
512 if (STRNEQ(start, sz, eqn_toks[i], strlen(eqn_toks[i])))
516 if (i == EQN_TOK__MAX && NULL != p)
517 *p = mandoc_strndup(start, sz);
523 eqn_box_free(struct eqn_box *bp)
527 eqn_box_free(bp->first);
529 eqn_box_free(bp->next);
540 * Allocate a box as the last child of the parent node.
542 static struct eqn_box *
543 eqn_box_alloc(struct eqn_node *ep, struct eqn_box *parent)
547 bp = mandoc_calloc(1, sizeof(struct eqn_box));
550 bp->expectargs = UINT_MAX;
551 bp->size = ep->gsize;
553 if (NULL != parent->first) {
554 parent->last->next = bp;
555 bp->prev = parent->last;
564 * Reparent the current last node (of the current parent) under a new
565 * EQN_SUBEXPR as the first element.
566 * Then return the new parent.
567 * The new EQN_SUBEXPR will have a two-child limit.
569 static struct eqn_box *
570 eqn_box_makebinary(struct eqn_node *ep,
571 enum eqn_post pos, struct eqn_box *parent)
573 struct eqn_box *b, *newb;
575 assert(NULL != parent->last);
577 if (parent->last == parent->first)
578 parent->first = NULL;
580 parent->last = b->prev;
582 newb = eqn_box_alloc(ep, parent);
584 newb->type = EQN_SUBEXPR;
585 newb->expectargs = 2;
587 newb->first = newb->last = b;
588 newb->first->next = NULL;
594 * Parse the "delim" control statement.
597 eqn_delim(struct eqn_node *ep)
602 if ((start = eqn_nextrawtok(ep, &sz)) == NULL)
603 mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
604 ep->eqn.ln, ep->eqn.pos, "delim");
605 else if (strncmp(start, "off", 3) == 0)
607 else if (strncmp(start, "on", 2) == 0) {
608 if (ep->odelim && ep->cdelim)
610 } else if (start[1] != '\0') {
611 ep->odelim = start[0];
612 ep->cdelim = start[1];
618 * Undefine a previously-defined string.
621 eqn_undef(struct eqn_node *ep)
627 if ((start = eqn_nextrawtok(ep, &sz)) == NULL) {
628 mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
629 ep->eqn.ln, ep->eqn.pos, "undef");
632 if ((def = eqn_def_find(ep, start, sz)) == NULL)
636 def->key = def->val = NULL;
637 def->keysz = def->valsz = 0;
641 eqn_def(struct eqn_node *ep)
648 if ((start = eqn_nextrawtok(ep, &sz)) == NULL) {
649 mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
650 ep->eqn.ln, ep->eqn.pos, "define");
655 * Search for a key that already exists.
656 * Create a new key if none is found.
658 if (NULL == (def = eqn_def_find(ep, start, sz))) {
659 /* Find holes in string array. */
660 for (i = 0; i < (int)ep->defsz; i++)
661 if (0 == ep->defs[i].keysz)
664 if (i == (int)ep->defsz) {
666 ep->defs = mandoc_reallocarray(ep->defs,
667 ep->defsz, sizeof(struct eqn_def));
668 ep->defs[i].key = ep->defs[i].val = NULL;
673 def->key = mandoc_strndup(start, sz);
677 start = eqn_next(ep, ep->data[(int)ep->cur], &sz, 0);
679 mandoc_vmsg(MANDOCERR_REQ_EMPTY, ep->parse,
680 ep->eqn.ln, ep->eqn.pos, "define %s", def->key);
683 def->key = def->val = NULL;
684 def->keysz = def->valsz = 0;
688 def->val = mandoc_strndup(start, sz);
693 * Recursively parse an eqn(7) expression.
696 eqn_parse(struct eqn_node *ep, struct eqn_box *parent)
703 enum eqn_tok tok, subtok;
707 assert(parent != NULL);
711 * Do not add it to the high-level syntax tree.
714 if (ep->data == NULL)
718 tok = eqn_tok_parse(ep, &p);
722 case (EQN_TOK_UNDEF):
725 case (EQN_TOK_NDEFINE):
726 case (EQN_TOK_DEFINE):
729 case (EQN_TOK_TDEFINE):
730 if (eqn_nextrawtok(ep, NULL) == NULL ||
731 eqn_next(ep, ep->data[(int)ep->cur], NULL, 0) == NULL)
732 mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
733 ep->eqn.ln, ep->eqn.pos, "tdefine");
735 case (EQN_TOK_DELIM):
738 case (EQN_TOK_GFONT):
739 if (eqn_nextrawtok(ep, NULL) == NULL)
740 mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
741 ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
744 case (EQN_TOK_LINEUP):
749 case (EQN_TOK_UNDER):
751 case (EQN_TOK_TILDE):
754 case (EQN_TOK_DOTDOT):
755 if (parent->last == NULL) {
756 mandoc_msg(MANDOCERR_EQN_NOBOX, ep->parse,
757 ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
758 cur = eqn_box_alloc(ep, parent);
759 cur->type = EQN_TEXT;
760 cur->text = mandoc_strdup("");
762 parent = eqn_box_makebinary(ep, EQNPOS_NONE, parent);
763 parent->type = EQN_LISTONE;
764 parent->expectargs = 1;
766 case (EQN_TOK_DOTDOT):
767 strlcpy(sym, "\\[ad]", sizeof(sym));
770 strlcpy(sym, "\\[->]", sizeof(sym));
773 strlcpy(sym, "\\[<>]", sizeof(sym));
775 case (EQN_TOK_TILDE):
776 strlcpy(sym, "\\[a~]", sizeof(sym));
778 case (EQN_TOK_UNDER):
779 strlcpy(sym, "\\[ul]", sizeof(sym));
782 strlcpy(sym, "\\[rl]", sizeof(sym));
785 strlcpy(sym, "\\[a.]", sizeof(sym));
788 strlcpy(sym, "\\[ha]", sizeof(sym));
795 case (EQN_TOK_DOTDOT):
798 case (EQN_TOK_TILDE):
802 parent->top = mandoc_strdup(sym);
804 case (EQN_TOK_UNDER):
805 parent->bottom = mandoc_strdup(sym);
810 parent = parent->parent;
816 subtok = eqn_tok_parse(ep, NULL);
817 if (subtok != EQN_TOK__MAX) {
818 mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
819 ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
825 case (EQN_TOK_ROMAN):
826 case (EQN_TOK_ITALIC):
828 while (parent->args == parent->expectargs)
829 parent = parent->parent;
831 * These values apply to the next word or sequence of
832 * words; thus, we mark that we'll have a child with
833 * exactly one of those.
835 parent = eqn_box_alloc(ep, parent);
836 parent->type = EQN_LISTONE;
837 parent->expectargs = 1;
840 parent->font = EQNFONT_FAT;
842 case (EQN_TOK_ROMAN):
843 parent->font = EQNFONT_ROMAN;
845 case (EQN_TOK_ITALIC):
846 parent->font = EQNFONT_ITALIC;
849 parent->font = EQNFONT_BOLD;
856 case (EQN_TOK_GSIZE):
857 /* Accept two values: integral size and a single. */
858 if (NULL == (start = eqn_nexttok(ep, &sz))) {
859 mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
860 ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
863 size = mandoc_strntoi(start, sz, 10);
865 mandoc_msg(MANDOCERR_IT_NONUM, ep->parse,
866 ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
869 if (EQN_TOK_GSIZE == tok) {
873 parent = eqn_box_alloc(ep, parent);
874 parent->type = EQN_LISTONE;
875 parent->expectargs = 1;
883 * We have a left-right-associative expression.
884 * Repivot under a positional node, open a child scope
885 * and keep on reading.
887 if (parent->last == NULL) {
888 mandoc_msg(MANDOCERR_EQN_NOBOX, ep->parse,
889 ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
890 cur = eqn_box_alloc(ep, parent);
891 cur->type = EQN_TEXT;
892 cur->text = mandoc_strdup("");
894 /* Handle the "subsup" and "fromto" positions. */
895 if (EQN_TOK_SUP == tok && parent->pos == EQNPOS_SUB) {
896 parent->expectargs = 3;
897 parent->pos = EQNPOS_SUBSUP;
900 if (EQN_TOK_TO == tok && parent->pos == EQNPOS_FROM) {
901 parent->expectargs = 3;
902 parent->pos = EQNPOS_FROMTO;
921 parent = eqn_box_makebinary(ep, pos, parent);
924 while (parent->args == parent->expectargs)
925 parent = parent->parent;
927 * Accept a left-right-associative set of arguments just
928 * like sub and sup and friends but without rebalancing
931 parent = eqn_box_alloc(ep, parent);
932 parent->type = EQN_SUBEXPR;
933 parent->pos = EQNPOS_SQRT;
934 parent->expectargs = 1;
938 * We have a right-left-associative fraction.
939 * Close out anything that's currently open, then
940 * rebalance and continue reading.
942 if (parent->last == NULL) {
943 mandoc_msg(MANDOCERR_EQN_NOBOX, ep->parse,
944 ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
945 cur = eqn_box_alloc(ep, parent);
946 cur->type = EQN_TEXT;
947 cur->text = mandoc_strdup("");
949 while (EQN_SUBEXPR == parent->type)
950 parent = parent->parent;
951 parent = eqn_box_makebinary(ep, EQNPOS_OVER, parent);
953 case (EQN_TOK_RIGHT):
954 case (EQN_TOK_BRACE_CLOSE):
956 * Close out the existing brace.
957 * FIXME: this is a shitty sentinel: we should really
958 * have a native EQN_BRACE type or whatnot.
960 for (cur = parent; cur != NULL; cur = cur->parent)
961 if (cur->type == EQN_LIST &&
962 (tok == EQN_TOK_BRACE_CLOSE ||
966 mandoc_msg(MANDOCERR_BLK_NOTOPEN, ep->parse,
967 ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
971 if (EQN_TOK_RIGHT == tok) {
972 if (NULL == (start = eqn_nexttok(ep, &sz))) {
973 mandoc_msg(MANDOCERR_REQ_EMPTY,
974 ep->parse, ep->eqn.ln,
975 ep->eqn.pos, eqn_toks[tok]);
978 /* Handling depends on right/left. */
979 if (STRNEQ(start, sz, "ceiling", 7)) {
980 strlcpy(sym, "\\[rc]", sizeof(sym));
981 parent->right = mandoc_strdup(sym);
982 } else if (STRNEQ(start, sz, "floor", 5)) {
983 strlcpy(sym, "\\[rf]", sizeof(sym));
984 parent->right = mandoc_strdup(sym);
986 parent->right = mandoc_strndup(start, sz);
988 parent = parent->parent;
989 if (EQN_TOK_BRACE_CLOSE == tok && parent &&
990 (parent->type == EQN_PILE ||
991 parent->type == EQN_MATRIX))
992 parent = parent->parent;
993 /* Close out any "singleton" lists. */
994 while (parent->type == EQN_LISTONE &&
995 parent->args == parent->expectargs)
996 parent = parent->parent;
998 case (EQN_TOK_BRACE_OPEN):
1001 * If we already have something in the stack and we're
1002 * in an expression, then rewind til we're not any more
1003 * (just like with the text node).
1005 while (parent->args == parent->expectargs)
1006 parent = parent->parent;
1007 if (EQN_TOK_LEFT == tok &&
1008 (start = eqn_nexttok(ep, &sz)) == NULL) {
1009 mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
1010 ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
1013 parent = eqn_box_alloc(ep, parent);
1014 parent->type = EQN_LIST;
1015 if (EQN_TOK_LEFT == tok) {
1016 if (STRNEQ(start, sz, "ceiling", 7)) {
1017 strlcpy(sym, "\\[lc]", sizeof(sym));
1018 parent->left = mandoc_strdup(sym);
1019 } else if (STRNEQ(start, sz, "floor", 5)) {
1020 strlcpy(sym, "\\[lf]", sizeof(sym));
1021 parent->left = mandoc_strdup(sym);
1023 parent->left = mandoc_strndup(start, sz);
1026 case (EQN_TOK_PILE):
1027 case (EQN_TOK_LPILE):
1028 case (EQN_TOK_RPILE):
1029 case (EQN_TOK_CPILE):
1030 case (EQN_TOK_CCOL):
1031 case (EQN_TOK_LCOL):
1032 case (EQN_TOK_RCOL):
1033 while (parent->args == parent->expectargs)
1034 parent = parent->parent;
1035 parent = eqn_box_alloc(ep, parent);
1036 parent->type = EQN_PILE;
1037 parent->expectargs = 1;
1039 case (EQN_TOK_ABOVE):
1040 for (cur = parent; cur != NULL; cur = cur->parent)
1041 if (cur->type == EQN_PILE)
1044 mandoc_msg(MANDOCERR_IT_STRAY, ep->parse,
1045 ep->eqn.ln, ep->eqn.pos, eqn_toks[tok]);
1048 parent = eqn_box_alloc(ep, cur);
1049 parent->type = EQN_LIST;
1051 case (EQN_TOK_MATRIX):
1052 while (parent->args == parent->expectargs)
1053 parent = parent->parent;
1054 parent = eqn_box_alloc(ep, parent);
1055 parent->type = EQN_MATRIX;
1056 parent->expectargs = 1;
1061 * TODO: make sure we're not in an open subexpression.
1065 assert(tok == EQN_TOK__MAX);
1068 * If we already have something in the stack and we're
1069 * in an expression, then rewind til we're not any more.
1071 while (parent->args == parent->expectargs)
1072 parent = parent->parent;
1073 cur = eqn_box_alloc(ep, parent);
1074 cur->type = EQN_TEXT;
1075 for (i = 0; i < EQNSYM__MAX; i++)
1076 if (0 == strcmp(eqnsyms[i].str, p)) {
1077 (void)snprintf(sym, sizeof(sym),
1078 "\\[%s]", eqnsyms[i].sym);
1079 cur->text = mandoc_strdup(sym);
1084 if (i == EQNSYM__MAX)
1087 * Post-process list status.
1089 while (parent->type == EQN_LISTONE &&
1090 parent->args == parent->expectargs)
1091 parent = parent->parent;
1098 eqn_end(struct eqn_node **epp)
1100 struct eqn_node *ep;
1105 ep->eqn.root = mandoc_calloc(1, sizeof(struct eqn_box));
1106 ep->eqn.root->expectargs = UINT_MAX;
1107 return(eqn_parse(ep, ep->eqn.root));
1111 eqn_free(struct eqn_node *p)
1115 eqn_box_free(p->eqn.root);
1117 for (i = 0; i < (int)p->defsz; i++) {
1118 free(p->defs[i].key);
1119 free(p->defs[i].val);