3 * Purpose: Parser for mathed
4 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 * Created: January 1996
6 * Description: Parse LaTeX2e math mode code.
8 * Dependencies: Xlib, XForms
10 * Copyright: 1996, Alejandro Aguilar Sierra
14 * You are free to use and modify this code under the terms of
15 * the GNU General Public Licence version 2 or later.
25 #pragma implementation
28 #include "math_parser.h"
30 #include "math_inset.h"
31 #include "math_arrayinset.h"
32 #include "math_bigopinset.h"
33 #include "math_dotsinset.h"
34 #include "math_decorationinset.h"
35 #include "math_deliminset.h"
36 #include "math_fracinset.h"
37 #include "math_funcinset.h"
38 #include "math_macro.h"
39 #include "math_macrotable.h"
40 #include "math_macrotemplate.h"
41 #include "math_matrixinset.h"
42 #include "math_rootinset.h"
43 #include "math_scriptinset.h"
44 #include "math_sizeinset.h"
45 #include "math_spaceinset.h"
46 #include "math_sqrtinset.h"
48 #include "mathed/support.h"
56 // These are lexical codes, not semantic
62 LexBOP, // Binary operators or relations
74 lexcode_enum lexcode[256];
77 char const * latex_special_chars = "#$%&_{}";
82 void mathed_parse(MathArray & array, unsigned flags);
84 unsigned char getuchar(std::istream * is)
88 return static_cast<unsigned char>(c);
91 const unsigned char LM_TK_OPEN = '{';
92 const unsigned char LM_TK_CLOSE = '}';
95 FLAG_BRACE = 1 << 0, // A { needed //}
96 FLAG_BRACE_LAST = 1 << 1, // // { Last } ends the parsing process
97 FLAG_RIGHT = 1 << 2, // Next right ends the parsing process
98 FLAG_END = 1 << 3, // Next end ends the parsing process
99 FLAG_BRACE_FONT = 1 << 4, // // { Next } closes a font
100 FLAG_BRACK_END = 1 << 5, // // [ Next ] ends the parsing process
101 FLAG_AMPERSAND = 1 << 6, // Next & ends the parsing process
102 FLAG_NEWLINE = 1 << 7, // Next \\ ends the parsing process
103 FLAG_ITEM = 1 << 8, // read a (possibly braced token)
104 FLAG_LEAVE = 1 << 9, // marker for leaving the
105 FLAG_OPTARG = 1 << 10 // reads an argument in []
121 MathTextCodes yyvarcode;
125 struct latex_mathenv_type {
127 char const * basename;
133 latex_mathenv_type latex_mathenv[] = {
134 {"math", "math", LM_OT_SIMPLE, 0, 0},
135 {"equation*", "equation", LM_OT_EQUATION, 0, 0},
136 {"equation", "equation", LM_OT_EQUATION, 1, 0},
137 {"eqnarray*", "eqnarray", LM_OT_EQNARRAY, 0, 0},
138 {"eqnarray", "eqnarray", LM_OT_EQNARRAY, 1, 0},
139 {"align*", "align", LM_OT_ALIGN, 0, 1},
140 {"align", "align", LM_OT_ALIGN, 1, 1},
141 {"alignat*", "alignat", LM_OT_ALIGNAT, 0, 1},
142 {"alignat", "alignat", LM_OT_ALIGNAT, 1, 1},
143 {"multline*", "multline", LM_OT_MULTLINE, 0, 1},
144 {"multline", "multline", LM_OT_MULTLINE, 1, 1},
145 {"array", "array", LM_OT_MATRIX, 0, 1}
148 int const latex_mathenv_num = sizeof(latex_mathenv)/sizeof(latex_mathenv[0]);
152 void mathPrintError(string const & msg)
154 //lyxerr[Debug::MATHED] << "Line ~" << yylineno << ": Math parse error: " << msg << endl;
155 lyxerr << "Line ~" << yylineno << ": Math parse error: " << msg << endl;
161 for (int i = 0; i <= 255; ++i) {
163 lexcode[i] = LexDigit;
165 lexcode[i] = LexSpace;
167 lexcode[i] = LexAlpha;
170 lexcode['\t'] = lexcode['\f'] = lexcode[' '] = LexSpace;
171 lexcode['\n'] = LexNewLine;
172 lexcode['%'] = LexComment;
173 lexcode['#'] = LexArgument;
174 lexcode['+'] = lexcode['-'] = lexcode['*'] = lexcode['/']
175 = lexcode['<'] = lexcode['>'] = lexcode['='] = LexBOP;
177 lexcode['!'] = lexcode[','] = lexcode[':']
178 = lexcode[';'] = LexMathSpace;
180 lexcode['('] = lexcode[')'] = lexcode['|'] = lexcode['.'] =
181 lexcode['?'] = LexOther;
183 lexcode['\''] = lexcode['@'] = LexAlpha;
185 lexcode['['] = lexcode[']'] = lexcode['^'] = lexcode['_'] =
186 lexcode['&'] = LexSelf;
188 lexcode['\\'] = LexESC;
189 lexcode['{'] = LexOpen;
190 lexcode['}'] = LexClose;
194 string lexArg(unsigned char lf, bool accept_spaces = false)
198 while (yyis->good()) {
209 unsigned char rg = 0;
210 if (lf == '{') rg = '}';
211 if (lf == '[') rg = ']';
212 if (lf == '(') rg = ')';
214 lyxerr[Debug::MATHED] << "Math parse error: unknown bracket '"
215 << lf << "'" << endl;
221 unsigned char c = getuchar(yyis);
226 if ((!isspace(c) || (c == ' ' && accept_spaces)) && depth > 0)
228 } while (depth > 0 && yyis->good());
236 static bool init_done = false;
243 while (yyis->good()) {
244 unsigned char c = getuchar(yyis);
245 //lyxerr << "reading byte: '" << c << "' code: " << lexcode[c] << endl;
247 if (yyvarcode == LM_TC_TEXTRM && c == ' ') {
250 } else if (lexcode[c] == LexNewLine) {
253 } else if (lexcode[c] == LexComment) {
256 } while (c != '\n' && yyis->good()); // eat comments
257 } else if (lexcode[c] == LexDigit
258 || lexcode[c] == LexOther
259 || lexcode[c] == LexMathSpace) {
262 } else if (lexcode[c] == LexAlpha) {
265 } else if (lexcode[c] == LexBOP) {
268 } else if (lexcode[c] == LexSelf) {
270 } else if (lexcode[c] == LexArgument) {
273 return LM_TK_ARGUMENT;
274 } else if (lexcode[c] == LexOpen) {
276 } else if (lexcode[c] == LexClose) {
278 } else if (lexcode[c] == LexESC) {
282 return LM_TK_NEWLINE;
285 yylval.i = LM_OT_SIMPLE;
289 yylval.i = LM_OT_SIMPLE;
293 yylval.i = LM_OT_EQUATION;
297 yylval.i = LM_OT_EQUATION;
300 if (contains(latex_special_chars, c)) {
302 return LM_TK_SPECIAL;
304 if (lexcode[c] == LexMathSpace) {
306 for (i = 0; i < 4 && static_cast<int>(c) != latex_mathspace[i][0]; ++i)
308 yylval.i = (i < 4) ? i : 0;
311 if (lexcode[c] == LexAlpha) {
313 while (lexcode[c] == LexAlpha) {
319 //lyxerr[Debug::MATHED] << "reading: text '" << yytext << "'\n";
320 latexkeys const * l = in_word_set(yytext);
324 if (l->token == LM_TK_BEGIN || l->token == LM_TK_END) {
325 string name = lexArg('{');
327 while (i < latex_mathenv_num && name != latex_mathenv[i].name)
330 } else if (l->token == LM_TK_SPACE)
343 MathScriptInset * prevScriptInset(MathArray const & array)
345 MathInset * p = array.back_inset();
346 return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
350 MathInset * lastScriptInset(MathArray & array, bool up, bool down, int limits)
352 MathScriptInset * p = prevScriptInset(array);
354 MathInset * b = array.back_inset();
355 if (b && b->isScriptable()) {
356 p = new MathScriptInset(up, down, b->clone());
359 p = new MathScriptInset(up, down);
374 static bool curr_num;
375 static string curr_label;
377 void mathed_parse_lines(MathInset * inset, int col, bool numbered, bool outmost)
379 // save global variables
380 bool saved_num = curr_num;
381 string saved_label = curr_label;
383 MathGridInset * p = static_cast<MathGridInset *>(inset);
384 for (int row = 0; true; ++row) {
385 // reset global variables
387 curr_label = string();
390 int idx = p->nargs() - p->ncols();
391 for (int i = 0; i < col - 1; ++i, ++idx)
392 mathed_parse(p->cell(idx), FLAG_AMPERSAND);
393 mathed_parse(p->cell(idx), FLAG_NEWLINE | FLAG_END);
396 MathMatrixInset * m = static_cast<MathMatrixInset *>(p);
397 m->numbered(row, curr_num);
398 m->label(row, curr_label);
409 // restore global variables
410 curr_num = saved_num;
411 curr_label = saved_label;
415 MathInset * mathed_parse()
421 case LM_TK_NEWCOMMAND: {
422 string name = lexArg('{').substr(1);
423 string arg = lexArg('[');
424 int narg = arg.empty() ? 0 : atoi(arg.c_str());
425 p = new MathMacroTemplate(name, narg);
426 mathed_parse(p->cell(0), FLAG_BRACE | FLAG_BRACE_LAST);
427 //lyxerr[Debug::MATHED] << "LM_TK_NEWCOMMAND: name: "
428 // << name << " nargs: " << narg << "\n";
434 //lyxerr[Debug::MATHED] << "reading math environment " << i << " "
435 // << latex_mathenv[i].name << "\n";
437 MathInsetTypes typ = latex_mathenv[i].typ;
438 p = new MathMatrixInset(typ);
439 MathMatrixInset * m = static_cast<MathMatrixInset *>(p);
443 curr_num = latex_mathenv[i].numbered;
444 curr_label = string();
445 mathed_parse(m->cell(0), 0);
446 m->numbered(0, curr_num);
447 m->label(0, curr_label);
451 case LM_OT_EQUATION: {
452 curr_num = latex_mathenv[i].numbered;
453 curr_label = string();
454 mathed_parse(m->cell(0), FLAG_END);
455 m->numbered(0, curr_num);
456 m->label(0, curr_label);
460 case LM_OT_EQNARRAY: {
461 mathed_parse_lines(m, 3, latex_mathenv[i].numbered, true);
466 m->halign(lexArg('{'));
467 mathed_parse_lines(m, 2, latex_mathenv[i].numbered, true);
471 case LM_OT_ALIGNAT: {
472 m->halign(lexArg('{'));
473 mathed_parse_lines(m, 2, latex_mathenv[i].numbered, true);
478 lyxerr[Debug::MATHED] << "1: unknown math environment: " << typ << "\n";
481 p->setName(latex_mathenv[i].basename);
487 lyxerr[Debug::MATHED] << "2 unknown math environment: " << t << "\n";
494 void handle_frac(MathArray & array, string const & name)
496 MathFracInset * p = new MathFracInset(name);
497 mathed_parse(p->cell(0), FLAG_ITEM);
498 mathed_parse(p->cell(1), FLAG_ITEM);
503 void mathed_parse(MathArray & array, unsigned flags)
507 static int plevel = -1;
508 yyvarcode = LM_TC_VAR;
515 //lyxerr << "t: " << t << " flags: " << flags << " i: " << yylval.i
516 // << " TK_LIMIT " << LM_TK_LIMIT << "\n";
518 // << " plevel: " << plevel << " ";
519 //array.dump(lyxerr);
522 if (flags & FLAG_ITEM) {
524 if (t == LM_TK_OPEN) {
525 // skip the brace and regard everything to the next matching
529 flags |= FLAG_BRACE_LAST;
531 // regard only this single token
536 if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
538 "Expected {. Maybe you forgot to enclose an argument in {}");
546 array.push_back(yylval.i, yyvarcode);
550 array.push_back(new MathMacroArgument(yylval.i));
554 array.push_back(yylval.i, LM_TC_SPECIAL);
558 array.push_back(yylval.i, LM_TC_CONST);
563 if (flags & FLAG_BRACE)
564 flags &= ~FLAG_BRACE;
566 array.push_back('{', LM_TC_TEX);
572 mathPrintError("Unmatching braces");
576 if (flags & FLAG_BRACE_FONT) {
577 yyvarcode = LM_TC_VAR;
578 flags &= ~FLAG_BRACE_FONT;
581 if (brace == 0 && (flags & FLAG_BRACE_LAST))
584 array.push_back('}', LM_TC_TEX);
588 array.push_back('[', LM_TC_CONST);
592 if (flags & FLAG_BRACK_END)
595 array.push_back(']', LM_TC_CONST);
600 lastScriptInset(array, true, false, limits)->cell(0), FLAG_ITEM);
605 lastScriptInset(array, false, true, limits)->cell(1), FLAG_ITEM);
609 limits = yylval.l->id;
610 //lyxerr << "setting limit to " << limits << "\n";
614 if (flags & FLAG_AMPERSAND) {
615 flags &= ~FLAG_AMPERSAND;
619 lyxerr[Debug::MATHED]
620 << "found tab unexpectedly, array: '" << array << "'\n";
624 if (flags & FLAG_NEWLINE) {
625 flags &= ~FLAG_NEWLINE;
629 lyxerr[Debug::MATHED]
630 << "found newline unexpectedly, array: '" << array << "'\n";
637 array.push_back(new MathBigopInset(yylval.l));
641 array.push_back(yylval.i, LM_TC_BOP);
646 array.push_back(new MathSpaceInset(yylval.i));
650 array.push_back(new MathDotsInset(yylval.l->name, yylval.l->id));
654 handle_frac(array, "atop");
658 handle_frac(array, "stackrel");
662 handle_frac(array, "frac");
667 unsigned char c = getuchar(yyis);
669 array.push_back(new MathRootInset);
670 mathed_parse(array.back_inset()->cell(0), FLAG_BRACK_END);
671 mathed_parse(array.back_inset()->cell(1), FLAG_ITEM);
674 array.push_back(new MathSqrtInset);
675 mathed_parse(array.back_inset()->cell(0), FLAG_ITEM);
685 else if (ld == LM_TK_STR || ld == LM_TK_BOP || ld == LM_TK_SPECIAL)
689 mathed_parse(ar, FLAG_RIGHT);
694 else if (rd == LM_TK_STR || rd == LM_TK_BOP || rd == LM_TK_SPECIAL)
697 MathDelimInset * dl = new MathDelimInset(ld, rd);
704 if (flags & FLAG_RIGHT) {
708 mathPrintError("Unmatched right delimiter");
713 yyvarcode = static_cast<MathTextCodes>(yylval.l->id);
714 flags |= (FLAG_BRACE | FLAG_BRACE_FONT);
719 lyxerr[Debug::MATHED] << "LM_TK_STY not implemented\n";
720 //MathArray tmp = array;
721 //MathSizeInset * p = new MathSizeInset(MathStyles(yylval.l->id));
722 //array.push_back(p);
723 //mathed_parse(p->cell(0), FLAG_BRACE_FONT);
728 case LM_TK_DECORATION:
730 MathDecorationInset * p
731 = new MathDecorationInset(yylval.l->name, yylval.l->id);
732 mathed_parse(p->cell(0), FLAG_ITEM);
742 array.push_back(new MathFuncInset(yylval.l->name));
746 if (MathMacroTable::hasTemplate(yytext)) {
747 MathMacro * m = MathMacroTable::cloneTemplate(yytext);
748 for (int i = 0; i < m->nargs(); ++i)
749 mathed_parse(m->cell(i), FLAG_ITEM);
751 m->metrics(LM_ST_TEXT);
753 array.push_back(new MathFuncInset(yytext));
763 MathInsetTypes typ = latex_mathenv[i].typ;
765 if (typ == LM_OT_MATRIX) {
766 string valign = lexArg('[') + 'c';
767 string halign = lexArg('{');
768 //lyxerr << "valign: '" << valign << "'\n";
769 //lyxerr << "halign: '" << halign << "'\n";
770 MathArrayInset * m = new MathArrayInset(halign.size(), 1);
771 m->valign(valign[0]);
774 mathed_parse_lines(m, halign.size(), latex_mathenv[i].numbered, false);
776 //lyxerr << "read matrix " << *m << "\n";
779 lyxerr[Debug::MATHED] << "unknow math inset " << typ << "\n";
784 array.push_back(MathMacroTable::cloneTemplate(yylval.l->name));
788 curr_label = lexArg('{', true);
792 mathPrintError("Unrecognized token");
793 lyxerr[Debug::MATHED] << "[" << t << " " << yytext << "]" << endl;
796 } // end of big switch
798 if (flags & FLAG_LEAVE) {
799 flags &= ~FLAG_LEAVE;
804 lyxerr << " Math Panic, expect problems!" << endl;
805 // Search for the end command.
808 } while (t != LM_TK_END && t);
818 MathInset * mathed_parse(string const & str)
820 istringstream is(str.c_str());
821 return mathed_parse(is);
825 MathInset * mathed_parse(istream & is)
829 return mathed_parse();
833 MathInset * mathed_parse(LyXLex & lex)
835 yyis = &lex.getStream();
836 yylineno = lex.GetLineNo();
838 MathInset * p = mathed_parse();
840 // Update line number
841 lex.setLineNo(yylineno);
843 // reading of end_inset
846 if (lex.GetString() == "\\end_inset")
848 lyxerr[Debug::MATHED] << "InsetFormula::Read: Garbage before \\end_inset,"
849 " or missing \\end_inset!" << endl;