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_macro.h"
32 #include "math_macrotable.h"
33 #include "math_macrotemplate.h"
34 #include "math_root.h"
35 #include "math_arrayinset.h"
36 #include "math_sqrtinset.h"
37 #include "math_matrixinset.h"
38 #include "math_accentinset.h"
39 #include "math_bigopinset.h"
40 #include "math_funcinset.h"
41 #include "math_spaceinset.h"
42 #include "math_sizeinset.h"
43 #include "math_scriptinset.h"
44 #include "math_dotsinset.h"
45 #include "math_fracinset.h"
46 #include "math_deliminset.h"
47 #include "math_decorationinset.h"
49 #include "mathed/support.h"
57 // These are lexical codes, not semantic
63 LexBOP, // Binary operators or relations
75 lexcode_enum lexcode[256];
78 char const * latex_special_chars = "#$%&_{}";
83 void mathed_parse(MathArray & array, unsigned flags);
85 unsigned char getuchar(std::istream * is)
89 return static_cast<unsigned char>(c);
92 const unsigned char LM_TK_OPEN = '{';
93 const unsigned char LM_TK_CLOSE = '}';
96 FLAG_BRACE = 1 << 0, // A { needed //}
97 FLAG_BRACE_OPT = 1 << 2, // Optional { //}
98 FLAG_BRACE_LAST = 1 << 3, // // { Last } ends the parsing process
99 FLAG_BRACK_ARG = 1 << 4, // Optional [ //]
100 FLAG_RIGHT = 1 << 5, // Next right ends the parsing process
101 FLAG_END = 1 << 6, // Next end ends the parsing process
102 FLAG_BRACE_FONT = 1 << 7, // // { Next } closes a font
103 FLAG_BRACK_END = 1 << 9, // // [ Next ] ends the parsing process
104 FLAG_AMPERSAND = 1 << 10, // Next & ends the parsing process
105 FLAG_NEWLINE = 1 << 11, // Next \\ ends the parsing process
107 // Read a (possibly braced token)
108 FLAG_ITEM = FLAG_BRACE_OPT | FLAG_BRACE_LAST
127 MathTextCodes yyvarcode;
131 struct latex_mathenv_type {
133 char const * basename;
139 latex_mathenv_type latex_mathenv[] = {
140 {"math", "math", LM_OT_SIMPLE, 0, 0},
141 {"equation*", "equation", LM_OT_EQUATION, 0, 0},
142 {"equation", "equation", LM_OT_EQUATION, 1, 0},
143 {"eqnarray*", "eqnarray", LM_OT_EQNARRAY, 0, 0},
144 {"eqnarray", "eqnarray", LM_OT_EQNARRAY, 1, 0},
145 {"align*", "align", LM_OT_ALIGN, 0, 1},
146 {"align", "align", LM_OT_ALIGN, 1, 1},
147 {"alignat*", "alignat", LM_OT_ALIGNAT, 0, 1},
148 {"alignat", "alignat", LM_OT_ALIGNAT, 1, 1},
149 {"multline*", "multline", LM_OT_MULTLINE, 0, 1},
150 {"multline", "multline", LM_OT_MULTLINE, 1, 1},
151 {"array", "array", LM_OT_MATRIX, 0, 1}
154 int const latex_mathenv_num = sizeof(latex_mathenv)/sizeof(latex_mathenv[0]);
158 void mathPrintError(string const & msg)
160 lyxerr[Debug::MATHED] << "Line ~" << yylineno << ": Math parse error: " << msg << endl;
166 for (int i = 0; i <= 255; ++i) {
168 lexcode[i] = LexDigit;
170 lexcode[i] = LexSpace;
172 lexcode[i] = LexAlpha;
175 lexcode['\t'] = lexcode['\f'] = lexcode[' '] = LexSpace;
176 lexcode['\n'] = LexNewLine;
177 lexcode['%'] = LexComment;
178 lexcode['#'] = LexArgument;
179 lexcode['+'] = lexcode['-'] = lexcode['*'] = lexcode['/']
180 = lexcode['<'] = lexcode['>'] = lexcode['='] = LexBOP;
182 lexcode['!'] = lexcode[','] = lexcode[':']
183 = lexcode[';'] = LexMathSpace;
185 lexcode['('] = lexcode[')'] = lexcode['|'] = lexcode['.'] =
186 lexcode['?'] = LexOther;
188 lexcode['\''] = lexcode['@'] = LexAlpha;
190 lexcode['['] = lexcode[']'] = lexcode['^'] = lexcode['_'] =
191 lexcode['&'] = LexSelf;
193 lexcode['\\'] = LexESC;
194 lexcode['{'] = LexOpen;
195 lexcode['}'] = LexClose;
199 unsigned char LexGetArg(unsigned char lf, bool accept_spaces = false)
201 while (yyis->good()) {
202 unsigned char c = getuchar(yyis);
207 lyxerr[Debug::MATHED] << "Math parse error: unexpected '" << c << "'" << endl;
213 unsigned char rg = 0;
214 if (lf == '{') rg = '}';
215 if (lf == '[') rg = ']';
216 if (lf == '(') rg = ')';
218 lyxerr[Debug::MATHED] << "Math parse error: unknown bracket '" << lf << "'" << endl;
224 unsigned char c = getuchar(yyis);
227 if ((c > ' ' || (c == ' ' && accept_spaces)) && bcnt > 0)
229 } while (bcnt > 0 && yyis->good());
237 static bool init_done = false;
244 while (yyis->good()) {
245 unsigned char c = getuchar(yyis);
246 //lyxerr << "reading byte: '" << c << "' code: " << lexcode[c] << endl;
248 if (yyvarcode == LM_TC_TEXTRM && c == ' ') {
251 } else if (lexcode[c] == LexNewLine) {
254 } else if (lexcode[c] == LexComment) {
257 } while (c != '\n' && yyis->good()); // eat comments
258 } else if (lexcode[c] == LexDigit
259 || lexcode[c] == LexOther
260 || lexcode[c] == LexMathSpace) {
263 } else if (lexcode[c] == LexAlpha) {
266 } else if (lexcode[c] == LexBOP) {
269 } else if (lexcode[c] == LexSelf) {
271 } else if (lexcode[c] == LexArgument) {
274 return LM_TK_ARGUMENT;
275 } else if (lexcode[c] == LexOpen) {
277 } else if (lexcode[c] == LexClose) {
279 } else if (lexcode[c] == LexESC) {
283 return LM_TK_NEWLINE;
286 yylval.i = LM_OT_SIMPLE;
290 yylval.i = LM_OT_SIMPLE;
294 yylval.i = LM_OT_EQUATION;
298 yylval.i = LM_OT_EQUATION;
301 if (contains(latex_special_chars, c)) {
303 return LM_TK_SPECIAL;
305 if (lexcode[c] == LexMathSpace) {
307 for (i = 0; i < 4 && static_cast<int>(c) != latex_mathspace[i][0]; ++i)
309 yylval.i = (i < 4) ? i : 0;
312 if (lexcode[c] == LexAlpha) {
314 while (lexcode[c] == LexAlpha) {
320 lyxerr[Debug::MATHED] << "reading: text '" << yytext << "'\n";
321 latexkeys const * l = in_word_set(yytext);
325 if (l->token == LM_TK_BEGIN || l->token == LM_TK_END) {
328 while (i < latex_mathenv_num && yytext != latex_mathenv[i].name)
331 } else if (l->token == LM_TK_SPACE)
343 MathInset * lastUpDownInset(MathArray & array, bool up, bool down)
345 MathInset * p = array.back_inset();
346 if (!p || !p->isUpDownInset()) {
347 p = new MathScriptInset(up, down);
350 MathUpDownInset * q = static_cast<MathScriptInset *>(p);
359 MathBigopInset * lastBigopInset(MathArray & array)
361 MathInset * p = array.back_inset();
362 return (p && p->isBigopInset()) ? static_cast<MathBigopInset *>(p) : 0;
367 static bool curr_num;
368 static string curr_label;
370 void mathed_parse_lines(MathInset * inset, int col, bool numbered, bool outmost)
372 // save global variables
373 bool saved_num = curr_num;
374 string saved_label = curr_label;
376 MathGridInset * p = static_cast<MathGridInset *>(inset);
377 for (int row = 0; true; ++row) {
378 // reset global variables
380 curr_label = string();
383 int idx = p->nargs() - p->ncols();
384 for (int i = 0; i < col - 1; ++i, ++idx)
385 mathed_parse(p->cell(idx), FLAG_AMPERSAND);
386 mathed_parse(p->cell(idx), FLAG_NEWLINE | FLAG_END);
389 MathMatrixInset * m = static_cast<MathMatrixInset *>(p);
390 m->numbered(row, curr_num);
391 m->label(row, curr_label);
402 // restore global variables
403 curr_num = saved_num;
404 curr_label = saved_label;
408 MathInset * mathed_parse()
414 case LM_TK_NEWCOMMAND: {
416 string name = yytext.substr(1);
419 unsigned char const c = yyis->peek();
422 na = atoi(yytext.c_str());
425 p = new MathMacroTemplate(name, na);
426 mathed_parse(p->cell(0), FLAG_BRACE | FLAG_BRACE_LAST);
427 lyxerr[Debug::MATHED] << "LM_TK_NEWCOMMAND: name: " << name << " na: " << na << "\n";
433 lyxerr[Debug::MATHED] << "reading math environment " << i << " "
434 << latex_mathenv[i].name << "\n";
436 MathInsetTypes typ = latex_mathenv[i].typ;
437 p = new MathMatrixInset(typ);
441 curr_num = latex_mathenv[i].numbered;
442 curr_label = string();
443 mathed_parse(p->cell(0), 0);
444 MathMatrixInset * m = static_cast<MathMatrixInset *>(p);
445 m->numbered(0, curr_num);
446 m->label(0, curr_label);
450 case LM_OT_EQUATION: {
451 curr_num = latex_mathenv[i].numbered;
452 curr_label = string();
453 mathed_parse(p->cell(0), FLAG_END);
454 MathMatrixInset * m = static_cast<MathMatrixInset *>(p);
455 m->numbered(0, curr_num);
456 m->label(0, curr_label);
460 case LM_OT_EQNARRAY: {
461 mathed_parse_lines(p, 3, latex_mathenv[i].numbered, true);
465 case LM_OT_ALIGNAT: {
467 //int c = atoi(yytext.c_str());
468 lyxerr[Debug::MATHED] << "LM_OT_ALIGNAT: not implemented\n";
469 mathed_parse_lines(p, 2, latex_mathenv[i].numbered, true);
470 lyxerr[Debug::MATHED] << "LM_OT_ALIGNAT: par: " << *p << "\n";
475 lyxerr[Debug::MATHED] << "1: unknown math environment: " << typ << "\n";
478 p->SetName(latex_mathenv[i].basename);
484 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;
514 //lyxerr << "t: " << t << " flags: " << flags;
515 //array.dump(lyxerr);
518 if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
519 if (!(flags & FLAG_BRACK_ARG) || t != '[') {
521 "Expected {. Maybe you forgot to enclose an argument in {}");
530 array.push_back(yylval.i, yyvarcode);
534 array.push_back(new MathMacroArgument(yylval.i));
538 array.push_back(yylval.i, LM_TC_SPECIAL);
542 array.push_back(yylval.i, LM_TC_CONST);
547 if (flags & FLAG_BRACE_OPT) {
548 flags &= ~FLAG_BRACE_OPT;
552 if (flags & FLAG_BRACE)
553 flags &= ~FLAG_BRACE;
555 array.push_back('{', LM_TC_TEX);
561 mathPrintError("Unmatching braces");
565 if (flags & FLAG_BRACE_FONT) {
566 yyvarcode = LM_TC_VAR;
567 flags &= ~FLAG_BRACE_FONT;
570 if (brace == 0 && (flags & FLAG_BRACE_LAST)) {
574 array.push_back('}', LM_TC_TEX);
578 if (flags & FLAG_BRACK_ARG) {
579 flags &= ~FLAG_BRACK_ARG;
580 unsigned char const rg = LexGetArg('[');
582 mathPrintError("Expected ']'");
587 array.push_back('[', LM_TC_CONST);
591 if (flags & FLAG_BRACK_END) {
595 array.push_back(']', LM_TC_CONST);
599 mathed_parse(lastUpDownInset(array, true, false)->cell(0), FLAG_ITEM);
603 mathed_parse(lastUpDownInset(array, false, true)->cell(1), FLAG_ITEM);
608 MathBigopInset * p = lastBigopInset(array);
610 p->limits(yylval.l->id ? 1 : -1);
616 if (flags & FLAG_AMPERSAND) {
617 flags &= ~FLAG_AMPERSAND;
621 lyxerr[Debug::MATHED] << "found tab unexpectedly, array: '" << array << "'\n";
627 if (flags & FLAG_NEWLINE) {
628 flags &= ~FLAG_NEWLINE;
632 lyxerr[Debug::MATHED] << "found newline unexpectedly, array: '" << array << "'\n";
638 array.push_back(new MathBigopInset(yylval.l->name, yylval.l->id));
643 if (yylval.l->id < 256) {
644 MathTextCodes tc = MathIsBOPS(yylval.l->id) ? LM_TC_BOPS: LM_TC_SYMB;
645 array.push_back(yylval.l->id, tc);
647 array.push_back(new MathFuncInset(yylval.l->name));
651 array.push_back(yylval.i, LM_TC_BOP);
656 array.push_back(new MathSpaceInset(yylval.i));
660 array.push_back(new MathDotsInset(yylval.l->name, yylval.l->id));
664 handle_frac(array, "atop");
668 handle_frac(array, "stackrel");
672 handle_frac(array, "frac");
677 unsigned char c = getuchar(yyis);
679 array.push_back(new MathRootInset);
680 mathed_parse(array.back_inset()->cell(0), FLAG_BRACK_END);
681 mathed_parse(array.back_inset()->cell(1), FLAG_ITEM);
684 array.push_back(new MathSqrtInset);
685 mathed_parse(array.back_inset()->cell(0), FLAG_ITEM);
695 else if (ld == LM_TK_STR || ld == LM_TK_BOP || ld == LM_TK_SPECIAL)
699 mathed_parse(ar, FLAG_RIGHT);
704 else if (rd == LM_TK_STR || rd == LM_TK_BOP || rd == LM_TK_SPECIAL)
707 MathDelimInset * dl = new MathDelimInset(ld, rd);
714 if (flags & FLAG_RIGHT) {
718 mathPrintError("Unmatched right delimiter");
723 yyvarcode = static_cast<MathTextCodes>(yylval.l->id);
724 flags |= (FLAG_BRACE | FLAG_BRACE_FONT);
729 lyxerr[Debug::MATHED] << "LM_TK_STY not implemented\n";
730 //MathArray tmp = array;
731 //MathSizeInset * p = new MathSizeInset(MathStyles(yylval.l->id));
732 //array.push_back(p);
733 //mathed_parse(p->cell(0), FLAG_BRACE_FONT);
740 MathDecorationInset * p = new MathDecorationInset(yylval.l->id);
741 mathed_parse(p->cell(0), FLAG_BRACE | FLAG_BRACE_LAST);
748 MathAccentInset * p = new MathAccentInset(yylval.l->id);
749 mathed_parse(p->cell(0), FLAG_BRACE | FLAG_BRACE_LAST);
761 // array.push_back(t, LM_TC_CONST);
763 array.push_back(new MathFuncInset(yylval.l->name));
767 array.push_back(new MathFuncInset(yylval.l->name, LM_OT_FUNCLIM));
771 if (MathMacroTable::hasTemplate(yytext)) {
772 MathMacro * m = MathMacroTable::cloneTemplate(yytext);
773 for (int i = 0; i < m->nargs(); ++i)
774 mathed_parse(m->cell(i), FLAG_ITEM);
776 m->Metrics(LM_ST_TEXT);
778 array.push_back(new MathFuncInset(yytext, LM_OT_UNDEF));
788 MathInsetTypes typ = latex_mathenv[i].typ;
790 if (typ == LM_OT_MATRIX) {
791 string valign = "\0";
792 unsigned char rg = LexGetArg(0);
798 string halign = yytext;
799 MathArrayInset * mm = new MathArrayInset(halign.size(), 1);
801 mm->valign(valign[0]);
804 mathed_parse_lines(mm, halign.size(), latex_mathenv[i].numbered, false);
806 //lyxerr << "read matrix " << *mm << "\n";
809 lyxerr[Debug::MATHED] << "unknow math inset " << typ << "\n";
814 array.push_back(MathMacroTable::cloneTemplate(yylval.l->name));
819 unsigned char const rg = LexGetArg('\0', true);
821 mathPrintError("Expected '{'");
823 lyxerr[Debug::MATHED] << "[" << yytext << "]" << endl;
827 //lyxerr << " setting label to " << yytext << "\n";
833 mathPrintError("Unrecognized token");
834 lyxerr[Debug::MATHED] << "[" << t << " " << yytext << "]" << endl;
837 } // end of big switch
840 lyxerr << " Math Panic, expect problems!" << endl;
841 // Search for the end command.
844 } while (t != LM_TK_END && t);
848 if (flags & FLAG_BRACE_OPT) {
849 flags &= ~FLAG_BRACE_OPT;
859 MathInset * mathed_parse(istream & is)
863 return mathed_parse();
867 MathInset * mathed_parse(LyXLex & lex)
869 yyis = &lex.getStream();
870 yylineno = lex.GetLineNo();
872 MathInset * p = mathed_parse();
874 // Update line number
875 lex.setLineNo(yylineno);
877 // reading of end_inset
880 if (lex.GetString() == "\\end_inset")
882 lyxerr[Debug::MATHED] << "InsetFormula::Read: Garbage before \\end_inset,"
883 " or missing \\end_inset!" << endl;