/* $Id: eqn.c,v 1.61 2016/01/08 00:50:45 schwarze Exp $ */
/*
* Copyright (c) 2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "mandoc.h"
#include "mandoc_aux.h"
#include "libmandoc.h"
#include "libroff.h"
enum eqn_tok {
EQN_TOK_DYAD = 0,
};
"dyad", /* EQN_TOK_DYAD */
"vec", /* EQN_TOK_VEC */
"under", /* EQN_TOK_UNDER */
"bar", /* EQN_TOK_BAR */
"tilde", /* EQN_TOK_TILDE */
"hat", /* EQN_TOK_HAT */
"dot", /* EQN_TOK_DOT */
"dotdot", /* EQN_TOK_DOTDOT */
"fwd", /* EQN_TOK_FWD * */
"back", /* EQN_TOK_BACK */
"down", /* EQN_TOK_DOWN */
"up", /* EQN_TOK_UP */
"fat", /* EQN_TOK_FAT */
"roman", /* EQN_TOK_ROMAN */
"italic", /* EQN_TOK_ITALIC */
"bold", /* EQN_TOK_BOLD */
"size", /* EQN_TOK_SIZE */
"sub", /* EQN_TOK_SUB */
"sup", /* EQN_TOK_SUP */
"sqrt", /* EQN_TOK_SQRT */
"over", /* EQN_TOK_OVER */
"from", /* EQN_TOK_FROM */
"to", /* EQN_TOK_TO */
"{", /* EQN_TOK_BRACE_OPEN */
"}", /* EQN_TOK_BRACE_CLOSE */
"gsize", /* EQN_TOK_GSIZE */
"gfont", /* EQN_TOK_GFONT */
"mark", /* EQN_TOK_MARK */
"lineup", /* EQN_TOK_LINEUP */
"left", /* EQN_TOK_LEFT */
"right", /* EQN_TOK_RIGHT */
"pile", /* EQN_TOK_PILE */
"lpile", /* EQN_TOK_LPILE */
"rpile", /* EQN_TOK_RPILE */
"cpile", /* EQN_TOK_CPILE */
"matrix", /* EQN_TOK_MATRIX */
"ccol", /* EQN_TOK_CCOL */
"lcol", /* EQN_TOK_LCOL */
"rcol", /* EQN_TOK_RCOL */
"delim", /* EQN_TOK_DELIM */
"define", /* EQN_TOK_DEFINE */
"tdefine", /* EQN_TOK_TDEFINE */
"ndefine", /* EQN_TOK_NDEFINE */
"undef", /* EQN_TOK_UNDEF */
NULL, /* EQN_TOK_EOF */
"above", /* EQN_TOK_ABOVE */
};
enum eqn_symt {
};
struct eqnsym {
const char *str;
const char *sym;
};
{ "alpha", "*a" }, /* EQNSYM_alpha */
{ "beta", "*b" }, /* EQNSYM_beta */
{ "chi", "*x" }, /* EQNSYM_chi */
{ "delta", "*d" }, /* EQNSYM_delta */
{ "epsilon", "*e" }, /* EQNSYM_epsilon */
{ "eta", "*y" }, /* EQNSYM_eta */
{ "gamma", "*g" }, /* EQNSYM_gamma */
{ "iota", "*i" }, /* EQNSYM_iota */
{ "kappa", "*k" }, /* EQNSYM_kappa */
{ "lambda", "*l" }, /* EQNSYM_lambda */
{ "mu", "*m" }, /* EQNSYM_mu */
{ "nu", "*n" }, /* EQNSYM_nu */
{ "omega", "*w" }, /* EQNSYM_omega */
{ "omicron", "*o" }, /* EQNSYM_omicron */
{ "phi", "*f" }, /* EQNSYM_phi */
{ "pi", "*p" }, /* EQNSYM_pi */
{ "psi", "*q" }, /* EQNSYM_psi */
{ "rho", "*r" }, /* EQNSYM_rho */
{ "sigma", "*s" }, /* EQNSYM_sigma */
{ "tau", "*t" }, /* EQNSYM_tau */
{ "theta", "*h" }, /* EQNSYM_theta */
{ "upsilon", "*u" }, /* EQNSYM_upsilon */
{ "xi", "*c" }, /* EQNSYM_xi */
{ "zeta", "*z" }, /* EQNSYM_zeta */
{ "DELTA", "*D" }, /* EQNSYM_DELTA */
{ "GAMMA", "*G" }, /* EQNSYM_GAMMA */
{ "LAMBDA", "*L" }, /* EQNSYM_LAMBDA */
{ "OMEGA", "*W" }, /* EQNSYM_OMEGA */
{ "PHI", "*F" }, /* EQNSYM_PHI */
{ "PI", "*P" }, /* EQNSYM_PI */
{ "PSI", "*Q" }, /* EQNSYM_PSI */
{ "SIGMA", "*S" }, /* EQNSYM_SIGMA */
{ "THETA", "*H" }, /* EQNSYM_THETA */
{ "UPSILON", "*U" }, /* EQNSYM_UPSILON */
{ "XI", "*C" }, /* EQNSYM_XI */
{ "inter", "ca" }, /* EQNSYM_inter */
{ "union", "cu" }, /* EQNSYM_union */
{ "prod", "product" }, /* EQNSYM_prod */
{ "int", "integral" }, /* EQNSYM_int */
{ "sum", "sum" }, /* EQNSYM_sum */
{ "grad", "gr" }, /* EQNSYM_grad */
{ "del", "gr" }, /* EQNSYM_del */
{ "times", "mu" }, /* EQNSYM_times */
{ "cdot", "pc" }, /* EQNSYM_cdot */
{ "nothing", "&" }, /* EQNSYM_nothing */
{ "approx", "~~" }, /* EQNSYM_approx */
{ "prime", "fm" }, /* EQNSYM_prime */
{ "half", "12" }, /* EQNSYM_half */
{ "partial", "pd" }, /* EQNSYM_partial */
{ "inf", "if" }, /* EQNSYM_inf */
{ ">>", ">>" }, /* EQNSYM_muchgreat */
{ "<<", "<<" }, /* EQNSYM_muchless */
{ "<-", "<-" }, /* EQNSYM_larrow */
{ "->", "->" }, /* EQNSYM_rarrow */
{ "+-", "+-" }, /* EQNSYM_pm */
{ "!=", "!=" }, /* EQNSYM_nequal */
{ "==", "==" }, /* EQNSYM_equiv */
{ "<=", "<=" }, /* EQNSYM_lessequal */
{ ">=", ">=" }, /* EQNSYM_moreequal */
{ "-", "mi" }, /* EQNSYM_minus */
};
static void eqn_box_free(struct eqn_box *);
enum rofferr
{
/*
* If we're the terminating mark, unset our equation status and
* validate the full equation.
*/
p += 3;
while (' ' == *p || '\t' == *p)
p++;
if ('\0' == *p)
return er;
return er;
}
/*
* Build up the full string, replacing all newlines with regular
* whitespace.
*/
/* First invocation: nil terminate the string. */
return ROFF_IGN;
}
struct eqn_node *
{
struct eqn_node *p;
p->gsize = EQN_DEFSIZE;
return p;
}
/*
* Find the key "key" of the give size within our eqn-defined values.
*/
static struct eqn_def *
{
int i;
return NULL;
}
/*
* Get the next token from the input stream using the given quote
* character.
* Optionally make any replacements.
*/
static const char *
{
lim = 0;
/* Prevent self-definitions. */
if (lim >= EQN_NEST_MAX) {
return NULL;
}
q = 0;
if ('\0' == *start)
return NULL;
q = 1;
}
if ( ! q) {
ssz = 1;
else
if ('\0' == *next)
} else
if (q)
} else {
if (q)
}
/* Quotes aren't expanded for values. */
if (q || ! repl)
return start;
}
lim++;
goto again;
}
return start;
}
/*
* Get the next delimited token using the default current quote
* character.
*/
static const char *
{
}
/*
* Get next token without replacement.
*/
static const char *
{
}
/*
* Parse a token from the stream of text.
* A token consists of one of the recognised eqn(7) strings.
* Strings are separated by delimiting marks.
* This returns EQN_TOK_EOF when there are no more tokens.
* If the token is an unrecognised string literal, then it returns
* EQN_TOK__MAX and sets the "p" pointer to an allocated, nil-terminated
* string.
* This must be later freed with free(3).
*/
static enum eqn_tok
{
const char *start;
int quoted;
if (NULL != p)
*p = NULL;
return EQN_TOK_EOF;
if (quoted) {
if (p != NULL)
return EQN_TOK__MAX;
}
for (i = 0; i < EQN_TOK__MAX; i++) {
continue;
break;
}
if (i == EQN_TOK__MAX && NULL != p)
return i;
}
static void
{
}
/*
* Allocate a box as the last child of the parent node.
*/
static struct eqn_box *
{
} else
return bp;
}
/*
* Reparent the current last node (of the current parent) under a new
* EQN_SUBEXPR as the first element.
* Then return the new parent.
* The new EQN_SUBEXPR will have a two-child limit.
*/
static struct eqn_box *
{
return newb;
}
/*
* Parse the "delim" control statement.
*/
static void
{
const char *start;
}
}
/*
* Undefine a previously-defined string.
*/
static void
{
const char *start;
return;
}
return;
}
static void
{
const char *start;
int i;
return;
}
/*
* Search for a key that already exists.
* Create a new key if none is found.
*/
/* Find holes in string array. */
break;
}
}
return;
}
}
/*
* Recursively parse an eqn(7) expression.
*/
static enum rofferr
{
const char *start;
char *p;
int size;
/*
* Empty equation.
* Do not add it to the high-level syntax tree.
*/
return ROFF_IGN;
switch (tok) {
case (EQN_TOK_UNDEF):
break;
case (EQN_TOK_NDEFINE):
case (EQN_TOK_DEFINE):
break;
case (EQN_TOK_TDEFINE):
break;
case (EQN_TOK_DELIM):
break;
case (EQN_TOK_GFONT):
break;
case (EQN_TOK_MARK):
case (EQN_TOK_LINEUP):
/* Ignore these. */
break;
case (EQN_TOK_DYAD):
case (EQN_TOK_VEC):
case (EQN_TOK_UNDER):
case (EQN_TOK_BAR):
case (EQN_TOK_TILDE):
case (EQN_TOK_HAT):
case (EQN_TOK_DOT):
case (EQN_TOK_DOTDOT):
}
switch (tok) {
case (EQN_TOK_DOTDOT):
break;
case (EQN_TOK_VEC):
break;
case (EQN_TOK_DYAD):
break;
case (EQN_TOK_TILDE):
break;
case (EQN_TOK_UNDER):
break;
case (EQN_TOK_BAR):
break;
case (EQN_TOK_DOT):
break;
case (EQN_TOK_HAT):
break;
default:
abort();
}
switch (tok) {
case (EQN_TOK_DOTDOT):
case (EQN_TOK_VEC):
case (EQN_TOK_DYAD):
case (EQN_TOK_TILDE):
case (EQN_TOK_BAR):
case (EQN_TOK_DOT):
case (EQN_TOK_HAT):
break;
case (EQN_TOK_UNDER):
break;
default:
abort();
}
break;
case (EQN_TOK_FWD):
case (EQN_TOK_BACK):
case (EQN_TOK_DOWN):
case (EQN_TOK_UP):
if (subtok != EQN_TOK__MAX) {
goto this_tok;
}
break;
case (EQN_TOK_FAT):
case (EQN_TOK_ROMAN):
case (EQN_TOK_ITALIC):
case (EQN_TOK_BOLD):
/*
* These values apply to the next word or sequence of
* words; thus, we mark that we'll have a child with
* exactly one of those.
*/
switch (tok) {
case (EQN_TOK_FAT):
break;
case (EQN_TOK_ROMAN):
break;
case (EQN_TOK_ITALIC):
break;
case (EQN_TOK_BOLD):
break;
default:
abort();
}
break;
case (EQN_TOK_SIZE):
case (EQN_TOK_GSIZE):
/* Accept two values: integral size and a single. */
break;
}
if (-1 == size) {
break;
}
if (EQN_TOK_GSIZE == tok) {
break;
}
break;
case (EQN_TOK_FROM):
case (EQN_TOK_TO):
case (EQN_TOK_SUB):
case (EQN_TOK_SUP):
/*
* We have a left-right-associative expression.
* Repivot under a positional node, open a child scope
* and keep on reading.
*/
}
/* Handle the "subsup" and "fromto" positions. */
break;
}
break;
}
switch (tok) {
case (EQN_TOK_FROM):
pos = EQNPOS_FROM;
break;
case (EQN_TOK_TO):
break;
case (EQN_TOK_SUP):
pos = EQNPOS_SUP;
break;
case (EQN_TOK_SUB):
pos = EQNPOS_SUB;
break;
default:
abort();
}
break;
case (EQN_TOK_SQRT):
/*
* Accept a left-right-associative set of arguments just
* like sub and sup and friends but without rebalancing
* under a pivot.
*/
break;
case (EQN_TOK_OVER):
/*
* We have a right-left-associative fraction.
* Close out anything that's currently open, then
* rebalance and continue reading.
*/
}
break;
case (EQN_TOK_RIGHT):
case (EQN_TOK_BRACE_CLOSE):
/*
* Close out the existing brace.
* FIXME: this is a shitty sentinel: we should really
* have a native EQN_BRACE type or whatnot.
*/
(tok == EQN_TOK_BRACE_CLOSE ||
break;
break;
}
if (EQN_TOK_RIGHT == tok) {
break;
}
} else
}
if (tok == EQN_TOK_BRACE_CLOSE &&
/* Close out any "singleton" lists. */
break;
case (EQN_TOK_BRACE_OPEN):
case (EQN_TOK_LEFT):
/*
* If we already have something in the stack and we're
* in an expression, then rewind til we're not any more
* (just like with the text node).
*/
if (EQN_TOK_LEFT == tok &&
break;
}
if (EQN_TOK_LEFT == tok) {
} else
}
break;
case (EQN_TOK_PILE):
case (EQN_TOK_LPILE):
case (EQN_TOK_RPILE):
case (EQN_TOK_CPILE):
case (EQN_TOK_CCOL):
case (EQN_TOK_LCOL):
case (EQN_TOK_RCOL):
break;
case (EQN_TOK_ABOVE):
break;
break;
}
break;
case (EQN_TOK_MATRIX):
break;
case (EQN_TOK_EOF):
/*
* End of file!
* TODO: make sure we're not in an open subexpression.
*/
return ROFF_EQN;
default:
/*
* If we already have something in the stack and we're
* in an expression, then rewind til we're not any more.
*/
for (i = 0; i < EQNSYM__MAX; i++)
free(p);
break;
}
if (i == EQNSYM__MAX)
/*
* Post-process list status.
*/
break;
}
goto next_tok;
}
enum rofferr
{
}
void
{
int i;
for (i = 0; i < (int)p->defsz; i++) {
}
free(p);
}