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.
23 #pragma implementation
26 #include "math_parser.h"
28 #include "math_inset.h"
29 #include "math_macro.h"
30 #include "math_macrotable.h"
31 #include "math_macrotemplate.h"
32 #include "math_root.h"
33 #include "math_arrayinset.h"
34 #include "math_sqrtinset.h"
35 #include "math_matrixinset.h"
36 #include "math_accentinset.h"
37 #include "math_bigopinset.h"
38 #include "math_funcinset.h"
39 #include "math_spaceinset.h"
40 #include "math_sizeinset.h"
41 #include "math_dotsinset.h"
42 #include "math_fracinset.h"
43 #include "math_deliminset.h"
44 #include "math_decorationinset.h"
46 #include "mathed/support.h"
54 // These are lexical codes, not semantic
60 LexBOP, // Binary operators or relations
72 lexcode_enum lexcode[256];
76 char const * latex_special_chars = "#$%&_{}";
79 /// Read TeX into data, flags give stop conditions
80 void mathed_parse(MathArray & data, unsigned flags);
85 const char LM_TK_OPEN = '{';
86 const char LM_TK_CLOSE = '}';
89 FLAG_BRACE = 1 << 0, // A { needed //}
90 FLAG_BRACE_OPT = 1 << 2, // Optional {
91 FLAG_BRACE_LAST = 1 << 3, // Last } ends the parsing process
92 FLAG_BRACK_ARG = 1 << 4, // Optional [
93 FLAG_RIGHT = 1 << 5, // Next right ends the parsing process
94 FLAG_END = 1 << 6, // Next end ends the parsing process
95 FLAG_BRACE_FONT = 1 << 7, // Next } closes a font
96 FLAG_BRACK_END = 1 << 9, // Next ] ends the parsing process
97 FLAG_AMPERSAND = 1 << 10, // Next & ends the parsing process
98 FLAG_NEWLINE = 1 << 11 // Next \\ ends the parsing process
115 bool yy_mtextmode = false;
119 struct latex_mathenv_type {
121 char const * basename;
127 latex_mathenv_type latex_mathenv[] = {
128 {"math", "math", LM_OT_SIMPLE, 0, 0},
129 {"equation*", "equation", LM_OT_EQUATION, 0, 0},
130 {"equation", "equation", LM_OT_EQUATION, 1, 0},
131 {"eqnarray*", "eqnarray", LM_OT_EQNARRAY, 0, 0},
132 {"eqnarray", "eqnarray", LM_OT_EQNARRAY, 1, 0},
133 {"align*", "align", LM_OT_ALIGN, 0, 1},
134 {"align", "align", LM_OT_ALIGN, 1, 1},
135 {"alignat*", "alignat", LM_OT_ALIGNAT, 0, 1},
136 {"alignat", "alignat", LM_OT_ALIGNAT, 1, 1},
137 {"multline*", "multline", LM_OT_MULTLINE, 0, 1},
138 {"multline", "multline", LM_OT_MULTLINE, 1, 1},
139 {"array", "array", LM_OT_MATRIX, 0, 1}
142 int const latex_mathenv_num = sizeof(latex_mathenv)/sizeof(latex_mathenv[0]);
146 void mathPrintError(string const & msg)
148 lyxerr << "Line ~" << yylineno << ": Math parse error: " << msg << endl;
154 for (int i = 0; i <= 255; ++i) {
156 lexcode[i] = LexAlpha;
158 lexcode[i] = LexDigit;
160 lexcode[i] = LexSpace;
162 lexcode[i] = LexNone;
165 lexcode['\t'] = lexcode['\f'] = lexcode[' '] = LexSpace;
166 lexcode['\n'] = LexNewLine;
167 lexcode['%'] = LexComment;
168 lexcode['#'] = LexArgument;
169 lexcode['+'] = lexcode['-'] = lexcode['*'] = lexcode['/']
170 = lexcode['<'] = lexcode['>'] = lexcode['='] = LexBOP;
172 lexcode['!'] = lexcode[','] = lexcode[':']
173 = lexcode[';'] = LexMathSpace;
175 lexcode['('] = lexcode[')'] = lexcode['|'] = lexcode['.'] =
176 lexcode['?'] = LexOther;
178 lexcode['\''] = lexcode['@'] = LexAlpha;
180 lexcode['['] = lexcode[']'] = lexcode['^'] = lexcode['_'] =
181 lexcode['&'] = LexSelf;
183 lexcode['\\'] = LexESC;
184 lexcode['{'] = LexOpen;
185 lexcode['}'] = LexClose;
189 char LexGetArg(char lf, bool accept_spaces = false)
191 while (yyis->good()) {
198 lyxerr << "Math parse error: unexpected '" << c << "'" << endl;
205 if (lf == '{') rg = '}';
206 if (lf == '[') rg = ']';
207 if (lf == '(') rg = ')';
209 lyxerr << "Math parse error: unknown bracket '" << lf << "'" << endl;
219 if ((c > ' ' || (c == ' ' && accept_spaces)) && bcnt > 0)
221 } while (bcnt > 0 && yyis->good());
229 static int init_done;
231 if (!init_done) LexInitCodes();
233 while (yyis->good()) {
237 if (yy_mtextmode && c == ' ') {
240 } else if (lexcode[c] == LexNewLine) {
243 } else if (lexcode[c] == LexComment) {
246 } while (c != '\n' && yyis->good()); // eat comments
247 } else if (lexcode[c] == LexDigit
248 || lexcode[c] == LexOther
249 || lexcode[c] == LexMathSpace) {
252 } else if (lexcode[c] == LexAlpha) {
255 } else if (lexcode[c] == LexBOP) {
258 } else if (lexcode[c] == LexSelf) {
260 } else if (lexcode[c] == LexArgument) {
263 return LM_TK_ARGUMENT;
264 } else if (lexcode[c] == LexOpen) {
266 } else if (lexcode[c] == LexClose) {
268 } else if (lexcode[c] == LexESC) {
272 return LM_TK_NEWLINE;
275 yylval.i = LM_OT_SIMPLE;
279 yylval.i = LM_OT_SIMPLE;
283 yylval.i = LM_OT_EQUATION;
287 yylval.i = LM_OT_EQUATION;
290 if (contains(latex_special_chars, c)) {
292 return LM_TK_SPECIAL;
294 if (lexcode[c] == LexMathSpace) {
296 for (i = 0; i < 4 && static_cast<int>(c) != latex_mathspace[i][0]; ++i)
298 yylval.i = (i < 4) ? i : 0;
301 if (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
303 while (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
309 lyxerr << "reading: text '" << yytext << "'\n";
310 latexkeys const * l = in_word_set(yytext);
314 if (l->token == LM_TK_BEGIN || l->token == LM_TK_END) {
317 while (i < latex_mathenv_num && yytext != latex_mathenv[i].name)
320 } else if (l->token == LM_TK_SPACE)
332 // Accent hacks only for 0.12. Stolen from Cursor.
336 void setAccent(int ac)
338 if (ac > 0 && accent < 8)
339 nestaccent[accent++] = ac;
341 accent = 0; // consumed!
345 MathInset * doAccent(byte c, MathTextCodes t)
349 for (int i = accent - 1; i >= 0; --i) {
351 ac = new MathAccentInset(c, t, nestaccent[i]);
353 ac = new MathAccentInset(ac, nestaccent[i]);
355 accent = 0; // consumed!
361 MathInset * doAccent(MathInset * p)
365 for (int i = accent - 1; i >= 0; --i) {
367 ac = new MathAccentInset(p, nestaccent[i]);
369 ac = new MathAccentInset(ac, nestaccent[i]);
371 accent = 0; // consumed!
377 void do_insert(MathArray & dat, MathInset * m)
380 dat.push_back(doAccent(m));
385 void do_insert(MathArray & dat, byte ch, MathTextCodes fcode)
388 dat.push_back(doAccent(ch, fcode));
390 dat.push_back(ch, fcode);
394 void handle_frac(MathArray & dat, MathInsetTypes t)
396 MathFracInset * p = new MathFracInset(t);
397 mathed_parse(p->cell(0), FLAG_BRACE | FLAG_BRACE_LAST);
398 mathed_parse(p->cell(1), FLAG_BRACE | FLAG_BRACE_LAST);
403 MathScriptInset * lastScriptInset(MathArray & array)
405 MathInset * p = array.back_inset();
406 if (!p || p->GetType() != LM_OT_SCRIPT) {
407 p = new MathScriptInset;
410 return static_cast<MathScriptInset *>(p);
417 static bool curr_num;
418 static string curr_label;
420 void mathed_parse_lines(MathInset * inset, int col)
422 MathGridInset * p = static_cast<MathGridInset *>(inset);
424 // reset global variables
426 curr_label = string();
429 int idx = p->nargs() - p->ncols();
430 for (int i = 0; i < col - 1; ++i, ++idx)
431 mathed_parse(p->cell(idx), FLAG_AMPERSAND);
432 mathed_parse(p->cell(idx), FLAG_NEWLINE | FLAG_END);
444 MathInset * mathed_parse()
450 case LM_TK_NEWCOMMAND: {
452 string name = yytext.substr(1);
455 char const c = yyis->peek();
458 na = atoi(yytext.c_str());
461 p = new MathMacroTemplate(name, na);
462 mathed_parse(p->cell(0), FLAG_BRACE | FLAG_BRACE_LAST);
463 lyxerr << "LM_TK_NEWCOMMAND: name: " << name << " na: " << na << "\n";
469 lyxerr << "reading math environment " << i << " "
470 << latex_mathenv[i].name << "\n";
472 MathInsetTypes typ = latex_mathenv[i].typ;
473 p = new MathMatrixInset(typ);
477 mathed_parse(p->cell(0), 0);
481 case LM_OT_EQUATION: {
482 mathed_parse(p->cell(0), FLAG_END);
486 case LM_OT_EQNARRAY: {
487 mathed_parse_lines(p, 3);
491 case LM_OT_ALIGNAT: {
493 //int c = atoi(yytext.c_str());
494 lyxerr << "LM_OT_ALIGNAT: not implemented\n";
495 mathed_parse_lines(p, 2);
496 lyxerr << "LM_OT_ALIGNAT: par: " << *p << "\n";
501 lyxerr << "1: unknown math environment: " << typ << "\n";
504 p->SetName(latex_mathenv[i].basename);
507 curr_num = latex_mathenv[i].numbered;
508 p->numbered(p->nrows()-1, curr_num);
514 lyxerr << "2 unknown math environment: " << t << "\n";
521 void mathed_parse(MathArray & array, unsigned flags)
526 static int plevel = -1;
527 MathTextCodes varcode = LM_TC_VAR;
535 //lyxerr << "t: " << t << " flags: " << flags;
536 //array.dump(lyxerr);
539 if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
540 if (!(flags & FLAG_BRACK_ARG) || t != '[') {
542 "Expected {. Maybe you forgot to enclose an argument in {}");
551 do_insert(array, yylval.i, varcode);
555 array.push_back(new MathMacroArgument(yylval.i));
559 array.push_back(yylval.i, LM_TC_SPECIAL);
563 do_insert(array, yylval.i, LM_TC_CONST);
568 if (accent && tprev == LM_TK_ACCENT) {
569 acc_braces[acc_brace++] = brace;
572 if (flags & FLAG_BRACE_OPT) {
573 flags &= ~FLAG_BRACE_OPT;
577 if (flags & FLAG_BRACE)
578 flags &= ~FLAG_BRACE;
580 array.push_back('{', LM_TC_TEX);
586 mathPrintError("Unmatching braces");
590 if (acc_brace && brace == acc_braces[acc_brace - 1] - 1) {
594 if (flags & FLAG_BRACE_FONT) {
596 yy_mtextmode = false;
597 flags &= ~FLAG_BRACE_FONT;
600 if (brace == 0 && (flags & FLAG_BRACE_LAST)) {
604 array.push_back('}', LM_TC_TEX);
608 if (flags & FLAG_BRACK_ARG) {
609 flags &= ~FLAG_BRACK_ARG;
610 char const rg = LexGetArg('[');
612 mathPrintError("Expected ']'");
617 array.push_back('[', LM_TC_CONST);
621 if (flags & FLAG_BRACK_END) {
625 array.push_back(']', LM_TC_CONST);
631 mathed_parse(ar, FLAG_BRACE_OPT | FLAG_BRACE_LAST);
632 MathScriptInset * p = lastScriptInset(array);
641 mathed_parse(ar, FLAG_BRACE_OPT | FLAG_BRACE_LAST);
642 MathScriptInset * p = lastScriptInset(array);
650 MathScriptInset * p = lastScriptInset(array);
652 p->SetLimits(bool(yylval.l->id));
658 if (flags & FLAG_AMPERSAND) {
659 flags &= ~FLAG_AMPERSAND;
663 lyxerr << "found tab unexpectedly, array: '" << array << "'\n";
669 if (flags & FLAG_NEWLINE) {
670 flags &= ~FLAG_NEWLINE;
674 lyxerr << "found newline unexpectedly, array: '" << array << "'\n";
680 array.push_back(new MathBigopInset(yylval.l->name, yylval.l->id));
685 if (yylval.l->id < 256) {
686 MathTextCodes tc = MathIsBOPS(yylval.l->id) ? LM_TC_BOPS: LM_TC_SYMB;
687 do_insert(array, yylval.l->id, tc);
689 do_insert(array, new MathFuncInset(yylval.l->name));
693 do_insert(array, yylval.i, LM_TC_BOP);
698 array.push_back(new MathSpaceInset(yylval.i));
702 array.push_back(new MathDotsInset(yylval.l->name, yylval.l->id));
706 handle_frac(array, LM_OT_ATOP);
710 handle_frac(array, LM_OT_STACKREL);
714 handle_frac(array, LM_OT_FRAC);
722 MathRootInset * rt = new MathRootInset;
723 mathed_parse(rt->cell(0), FLAG_BRACK_END);
724 mathed_parse(rt->cell(1), FLAG_BRACE | FLAG_BRACE_LAST);
728 MathSqrtInset * sq = new MathSqrtInset;
729 mathed_parse(sq->cell(0), FLAG_BRACE | FLAG_BRACE_LAST);
740 else if (ld == LM_TK_STR || ld == LM_TK_BOP || ld == LM_TK_SPECIAL)
744 mathed_parse(ar, FLAG_RIGHT);
749 else if (rd == LM_TK_STR || rd == LM_TK_BOP || rd == LM_TK_SPECIAL)
752 MathDelimInset * dl = new MathDelimInset(ld, rd);
759 if (flags & FLAG_RIGHT) {
763 mathPrintError("Unmatched right delimiter");
768 yy_mtextmode = (yylval.l->id == LM_TC_TEXTRM);
769 flags |= (FLAG_BRACE | FLAG_BRACE_FONT);
774 lyxerr << "LM_TK_STY not implemented\n";
775 //MathArray tmp = array;
776 //MathSizeInset * p = new MathSizeInset(MathStyles(yylval.l->id));
777 //array.push_back(p);
778 //mathed_parse(p->cell(0), FLAG_BRACE_FONT);
785 MathDecorationInset * sq = new MathDecorationInset(yylval.l->id);
786 mathed_parse(sq->cell(0), FLAG_BRACE | FLAG_BRACE_LAST);
792 setAccent(yylval.l->id);
802 array.push_back(t, LM_TC_CONST);
804 array.push_back(new MathFuncInset(yylval.l->name));
808 array.push_back(new MathFuncInset(yylval.l->name, LM_OT_FUNCLIM));
812 if (MathMacroTable::hasTemplate(yytext)) {
813 MathMacro * m = MathMacroTable::cloneTemplate(yytext);
814 for (int i = 0; i < m->nargs(); ++i) {
815 mathed_parse(m->cell(i), FLAG_BRACE_OPT | FLAG_BRACE_LAST);
816 lyxerr << "reading cell " << i << " '" << m->cell(i) << "'\n";
820 do_insert(array, new MathFuncInset(yytext, LM_OT_UNDEF));
830 MathInsetTypes typ = latex_mathenv[i].typ;
832 if (typ == LM_OT_MATRIX) {
833 string valign = "\0";
834 char rg = LexGetArg(0);
840 string halign = yytext;
841 MathArrayInset * mm = new MathArrayInset(halign.size(), 1);
843 mm->valign(valign[0]);
846 mathed_parse_lines(mm, halign.size());
847 do_insert(array, mm);
848 //lyxerr << "read matrix " << *mm << "\n";
851 lyxerr << "unknow math inset " << typ << "\n";
856 do_insert(array, MathMacroTable::cloneTemplate(yylval.l->name));
861 char const rg = LexGetArg('\0', true);
863 mathPrintError("Expected '{'");
865 lyxerr << "[" << yytext << "]" << endl;
869 //lyxerr << " setting label to " << yytext << "\n";
875 mathPrintError("Unrecognized token");
876 lyxerr << "[" << t << " " << yytext << "]" << endl;
879 } // end of big switch
883 lyxerr << " Math Panic, expect problems!" << endl;
884 // Search for the end command.
887 } while (t != LM_TK_END && t);
891 if (flags & FLAG_BRACE_OPT) {
892 flags &= ~FLAG_BRACE_OPT;
900 MathInset * mathed_parse(istream & is)
904 return mathed_parse();
908 MathInset * mathed_parse(LyXLex & lex)
910 yyis = &lex.getStream();
911 yylineno = lex.GetLineNo();
913 MathInset * p = mathed_parse();
915 // Update line number
916 lex.setLineNo(yylineno);
918 // reading of end_inset
921 if (lex.GetString() == "\\end_inset")
923 lyxerr << "InsetFormula::Read: Garbage before \\end_inset,"
924 " or missing \\end_inset!" << endl;