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_arrayinset.h"
30 #include "math_bigopinset.h"
31 #include "math_charinset.h"
32 #include "math_dotsinset.h"
33 #include "math_decorationinset.h"
34 #include "math_deliminset.h"
35 #include "math_fracinset.h"
36 #include "math_funcinset.h"
37 #include "math_funcliminset.h"
38 #include "math_macro.h"
39 #include "math_macrotable.h"
40 #include "math_macrotemplate.h"
41 #include "math_matrixinset.h"
42 #include "math_noglyphinset.h"
43 #include "math_rootinset.h"
44 #include "math_scriptinset.h"
45 #include "math_sizeinset.h"
46 #include "math_spaceinset.h"
47 #include "math_sqrtinset.h"
48 #include "math_stackrelinset.h"
49 #include "math_symbolinset.h"
51 #include "mathed/support.h"
53 #include "support/lstrings.h"
61 // These are lexical codes, not semantic
66 LexBOP, // Binary operators or relations
78 lexcode_enum lexcode[256];
81 void mathed_parse_into(MathArray & array, unsigned flags);
83 unsigned char getuchar(std::istream * is)
88 lyxerr << "The input stream is not well..." << endl;
90 return static_cast<unsigned char>(c);
93 const unsigned char LM_TK_OPEN = '{';
94 const unsigned char LM_TK_CLOSE = '}';
97 FLAG_BRACE = 1 << 0, // A { needed //}
98 FLAG_BRACE_LAST = 1 << 1, // // { Last } ends the parsing process
99 FLAG_RIGHT = 1 << 2, // Next right ends the parsing process
100 FLAG_END = 1 << 3, // Next end ends the parsing process
101 FLAG_BRACE_FONT = 1 << 4, // // { Next } closes a font
102 FLAG_BRACK_END = 1 << 5, // // [ Next ] ends the parsing process
103 FLAG_AMPERSAND = 1 << 6, // Next & ends the parsing process
104 FLAG_NEWLINE = 1 << 7, // Next \\ ends the parsing process
105 FLAG_ITEM = 1 << 8, // read a (possibly braced token)
106 FLAG_LEAVE = 1 << 9, // marker for leaving the
107 FLAG_OPTARG = 1 << 10 // reads an argument in []
126 struct latex_mathenv_type {
128 char const * basename;
134 latex_mathenv_type latex_mathenv[] = {
135 {"math", "math", LM_OT_SIMPLE, 0, 0},
136 {"equation*", "equation", LM_OT_EQUATION, 0, 0},
137 {"equation", "equation", LM_OT_EQUATION, 1, 0},
138 {"eqnarray*", "eqnarray", LM_OT_EQNARRAY, 0, 0},
139 {"eqnarray", "eqnarray", LM_OT_EQNARRAY, 1, 0},
140 {"align*", "align", LM_OT_ALIGN, 0, 1},
141 {"align", "align", LM_OT_ALIGN, 1, 1},
142 {"alignat*", "alignat", LM_OT_ALIGNAT, 0, 1},
143 {"alignat", "alignat", LM_OT_ALIGNAT, 1, 1},
144 {"multline*", "multline", LM_OT_MULTLINE, 0, 1},
145 {"multline", "multline", LM_OT_MULTLINE, 1, 1},
146 {"array", "array", LM_OT_MATRIX, 0, 1}
149 int const latex_mathenv_num = sizeof(latex_mathenv)/sizeof(latex_mathenv[0]);
153 void mathPrintError(string const & msg)
155 //lyxerr[Debug::MATHED] << "Line ~" << yylineno << ": Math parse error: " << msg << endl;
156 lyxerr << "Line ~" << yylineno << ": Math parse error: " << msg << endl;
162 for (int i = 0; i <= 255; ++i) {
164 lexcode[i] = LexOther;
166 lexcode[i] = LexSpace;
168 lexcode[i] = LexAlpha;
171 lexcode['\t'] = lexcode['\f'] = lexcode[' '] = LexSpace;
172 lexcode['\n'] = LexNewLine;
173 lexcode['%'] = LexComment;
174 lexcode['#'] = LexArgument;
175 lexcode['$'] = LexMath;
176 lexcode['+'] = lexcode['-'] = lexcode['*'] = lexcode['/']
177 = lexcode['<'] = lexcode['>'] = lexcode['='] = LexBOP;
179 lexcode['('] = lexcode[')'] = lexcode['|'] = lexcode['.'] =
180 lexcode['?'] = LexOther;
182 lexcode['\''] = lexcode['@'] = LexAlpha;
184 lexcode['['] = lexcode[']'] = lexcode['^'] = lexcode['_'] =
185 lexcode['&'] = LexSelf;
187 lexcode['\\'] = LexESC;
188 lexcode['{'] = LexOpen;
189 lexcode['}'] = LexClose;
193 string lexArg(unsigned char lf, bool accept_spaces = false)
197 while (yyis->good()) {
208 unsigned char rg = 0;
209 if (lf == '{') rg = '}';
210 if (lf == '[') rg = ']';
211 if (lf == '(') rg = ')';
213 lyxerr[Debug::MATHED] << "Math parse error: unknown bracket '"
214 << lf << "'" << endl;
220 unsigned char c = getuchar(yyis);
225 if ((!isspace(c) || (c == ' ' && accept_spaces)) && depth > 0)
227 } while (depth > 0 && yyis->good());
235 static bool init_done = false;
242 while (yyis->good()) {
243 unsigned char c = getuchar(yyis);
244 //lyxerr << "reading byte: '" << c << "' code: " << lexcode[c] << endl;
246 if (lexcode[c] == LexNewLine) {
249 } else if (lexcode[c] == LexComment) {
252 } while (c != '\n' && yyis->good()); // eat comments
253 } else if (lexcode[c] == LexOther) {
256 } else if (lexcode[c] == LexAlpha || lexcode[c] == LexSpace) {
259 } else if (lexcode[c] == LexBOP) {
262 } else if (lexcode[c] == LexMath) {
265 } else if (lexcode[c] == LexSelf) {
267 } else if (lexcode[c] == LexArgument) {
270 return LM_TK_ARGUMENT;
271 } else if (lexcode[c] == LexOpen) {
273 } else if (lexcode[c] == LexClose) {
275 } else if (lexcode[c] == LexESC) {
277 //lyxerr << "reading second byte: '" << c << "' code: " << lexcode[c] << endl;
280 latexkeys const * l = in_word_set(s);
282 //lyxerr << "found key: " << l << endl;
283 //lyxerr << "found key name: " << l->name << endl;
284 //lyxerr << "found key token: " << l->token << endl;
289 if (lexcode[c] == LexAlpha) {
291 while (lexcode[c] == LexAlpha && yyis->good()) {
295 while (lexcode[c] == LexSpace && yyis->good())
297 if (lexcode[c] != LexSpace)
300 //lyxerr[Debug::MATHED] << "reading: text '" << yytext << "'\n";
301 //lyxerr << "reading: text '" << yytext << "'\n";
302 latexkeys const * l = in_word_set(yytext);
306 if (l->token == LM_TK_BEGIN || l->token == LM_TK_END) {
307 string name = lexArg('{');
309 while (i < latex_mathenv_num && name != latex_mathenv[i].name)
312 } else if (l->token == LM_TK_SPACE)
324 MathScriptInset * prevScriptInset(MathArray const & array)
326 MathInset * p = array.back();
327 return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
331 MathInset * lastScriptInset(MathArray & array, bool up, bool down, int limits)
333 MathScriptInset * p = prevScriptInset(array);
335 MathInset * b = array.back();
336 if (b && b->isScriptable()) {
337 p = new MathScriptInset(up, down, b->clone());
340 p = new MathScriptInset(up, down);
354 static bool curr_num;
355 static string curr_label;
357 void mathed_parse_lines(MathGridInset * p, int col, bool numbered, bool outmost)
359 // save global variables
360 bool const saved_num = curr_num;
361 string const saved_label = curr_label;
363 for (int row = 0; true; ++row) {
364 // reset global variables
369 int idx = p->nargs() - p->ncols();
370 for (int i = 0; i < col - 1; ++i, ++idx)
371 mathed_parse_into(p->cell(idx), FLAG_AMPERSAND);
372 mathed_parse_into(p->cell(idx), FLAG_NEWLINE | FLAG_END);
375 MathMatrixInset * m = static_cast<MathMatrixInset *>(p);
376 m->numbered(row, curr_num);
377 m->label(row, curr_label);
390 // restore global variables
391 curr_num = saved_num;
392 curr_label = saved_label;
396 MathMacroTemplate * mathed_parse_macro()
398 if (yylex() != LM_TK_NEWCOMMAND) {
399 lyxerr << "\\newcommand expected\n";
403 string name = lexArg('{').substr(1);
404 string arg = lexArg('[');
405 int narg = arg.empty() ? 0 : atoi(arg.c_str());
406 MathMacroTemplate * p = new MathMacroTemplate(name, narg);
407 mathed_parse_into(p->cell(0), FLAG_BRACE | FLAG_BRACE_LAST);
412 MathMatrixInset * mathed_parse_normal()
414 MathMatrixInset * p = 0;
421 lyxerr[Debug::MATHED]
422 << "reading math environment " << i << " "
423 << latex_mathenv[i].name << "\n";
425 MathInsetTypes typ = latex_mathenv[i].typ;
426 p = new MathMatrixInset(typ);
431 curr_num = latex_mathenv[i].numbered;
433 mathed_parse_into(p->cell(0), 0);
434 p->numbered(0, curr_num);
435 p->label(0, curr_label);
439 case LM_OT_EQUATION: {
440 curr_num = latex_mathenv[i].numbered;
442 mathed_parse_into(p->cell(0), FLAG_END);
443 p->numbered(0, curr_num);
444 p->label(0, curr_label);
448 case LM_OT_EQNARRAY: {
449 mathed_parse_lines(p, 3, latex_mathenv[i].numbered, true);
454 p->halign(lexArg('{'));
455 mathed_parse_lines(p, 2, latex_mathenv[i].numbered, true);
459 case LM_OT_ALIGNAT: {
460 p->halign(lexArg('{'));
461 mathed_parse_lines(p, 2, latex_mathenv[i].numbered, true);
466 lyxerr[Debug::MATHED]
467 << "1: unknown math environment: " << typ << "\n";
474 lyxerr[Debug::MATHED]
475 << "2 unknown math environment: " << t << "\n";
482 latexkeys const * read_delim()
485 //lyxerr << "found symbol: " << ld << "\n";
486 latexkeys const * l = in_word_set(".");
493 //lyxerr << "found key 1: '" << l << "'\n";
494 //lyxerr << "found key 1: '" << l->name << "'\n";
500 //lyxerr << "found key 2: '" << l->name << "'\n";
506 void mathed_parse_into(MathArray & array, unsigned flags)
508 static int plevel = -1;
511 MathTextCodes yyvarcode = LM_TC_VAR;
519 //lyxerr << "t: " << t << " flags: " << flags << " i: " << yylval.i
520 // << " '" << yytext << "'\n";
521 //array.dump(lyxerr);
524 if (flags & FLAG_ITEM) {
526 if (t == LM_TK_OPEN) {
527 // skip the brace and regard everything to the next matching
531 flags |= FLAG_BRACE_LAST;
533 // regard only this single token
538 if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
540 "Expected {. Maybe you forgot to enclose an argument in {}");
548 if (!isspace(yylval.i) || yyvarcode == LM_TC_TEXTRM)
549 array.push_back(new MathCharInset(yylval.i, yyvarcode));
552 case LM_TK_ARGUMENT: {
553 MathMacroArgument * p = new MathMacroArgument(yylval.i);
560 array.push_back(new MathCharInset(yylval.i, LM_TC_SPECIAL));
564 array.push_back(new MathCharInset(yylval.i, LM_TC_CONST));
569 if (flags & FLAG_BRACE)
570 flags &= ~FLAG_BRACE;
572 array.push_back(new MathCharInset('{', LM_TC_TEX));
578 mathPrintError("Unmatching braces");
582 if (flags & FLAG_BRACE_FONT) {
583 yyvarcode = LM_TC_VAR;
584 flags &= ~FLAG_BRACE_FONT;
587 if (brace == 0 && (flags & FLAG_BRACE_LAST))
590 array.push_back(new MathCharInset('}', LM_TC_TEX));
594 array.push_back(new MathCharInset('[', LM_TC_CONST));
598 if (flags & FLAG_BRACK_END)
601 array.push_back(new MathCharInset(']', LM_TC_CONST));
606 lastScriptInset(array, true, false, limits)->cell(0), FLAG_ITEM);
611 lastScriptInset(array, false, true, limits)->cell(1), FLAG_ITEM);
615 limits = yylval.l->id;
616 //lyxerr << "setting limit to " << limits << "\n";
620 if (flags & FLAG_AMPERSAND) {
621 flags &= ~FLAG_AMPERSAND;
625 lyxerr[Debug::MATHED]
626 << "found tab unexpectedly, array: '" << array << "'\n";
630 if (flags & FLAG_NEWLINE) {
631 flags &= ~FLAG_NEWLINE;
635 lyxerr[Debug::MATHED]
636 << "found newline unexpectedly, array: '" << array << "'\n";
645 array.push_back(new MathNoglyphInset(yylval.l));
650 array.push_back(new MathBigopInset(yylval.l));
655 array.push_back(new MathFuncLimInset(yylval.l));
660 array.push_back(new MathSymbolInset(yylval.l));
664 array.push_back(new MathCharInset(yylval.i, LM_TC_BOP));
669 array.push_back(new MathSpaceInset(yylval.i));
673 array.push_back(new MathDotsInset(yylval.l));
678 MathStackrelInset * p = new MathStackrelInset;
679 mathed_parse_into(p->cell(0), FLAG_ITEM);
680 mathed_parse_into(p->cell(1), FLAG_ITEM);
687 MathFracInset * p = new MathFracInset;
688 mathed_parse_into(p->cell(0), FLAG_ITEM);
689 mathed_parse_into(p->cell(1), FLAG_ITEM);
696 unsigned char c = getuchar(yyis);
698 array.push_back(new MathRootInset);
699 mathed_parse_into(array.back()->cell(0), FLAG_BRACK_END);
700 mathed_parse_into(array.back()->cell(1), FLAG_ITEM);
703 array.push_back(new MathSqrtInset);
704 mathed_parse_into(array.back()->cell(0), FLAG_ITEM);
711 latexkeys const * l = read_delim();
713 mathed_parse_into(ar, FLAG_RIGHT);
714 latexkeys const * r = read_delim();
715 MathDelimInset * dl = new MathDelimInset(l, r);
722 if (flags & FLAG_RIGHT) {
726 mathPrintError("Unmatched right delimiter");
731 yyvarcode = static_cast<MathTextCodes>(yylval.l->id);
732 flags |= (FLAG_BRACE | FLAG_BRACE_FONT);
737 lyxerr[Debug::MATHED] << "LM_TK_STY not implemented\n";
738 //MathArray tmp = array;
739 //MathSizeInset * p = new MathSizeInset(MathStyles(yylval.l->id));
740 //array.push_back(p);
741 //mathed_parse_into(p->cell(0), FLAG_BRACE_FONT);
746 case LM_TK_DECORATION:
748 MathDecorationInset * p = new MathDecorationInset(yylval.l);
749 mathed_parse_into(p->cell(0), FLAG_ITEM);
759 array.push_back(new MathSymbolInset(yylval.l));
763 if (MathMacroTable::hasTemplate(yytext)) {
764 MathMacro * m = MathMacroTable::cloneTemplate(yytext);
765 for (int i = 0; i < m->nargs(); ++i)
766 mathed_parse_into(m->cell(i), FLAG_ITEM);
768 m->metrics(LM_ST_TEXT);
770 array.push_back(new MathFuncInset(yytext));
781 MathInsetTypes typ = latex_mathenv[i].typ;
783 if (typ == LM_OT_MATRIX) {
784 string const valign = lexArg('[') + 'c';
785 string const halign = lexArg('{');
786 //lyxerr << "valign: '" << valign << "'\n";
787 //lyxerr << "halign: '" << halign << "'\n";
788 MathArrayInset * m = new MathArrayInset(halign.size(), 1);
789 m->valign(valign[0]);
792 mathed_parse_lines(m, halign.size(), latex_mathenv[i].numbered, false);
794 //lyxerr << "read matrix " << *m << "\n";
797 lyxerr[Debug::MATHED] << "unknow math inset " << typ << "\n";
802 array.push_back(MathMacroTable::cloneTemplate(yylval.l->name));
806 curr_label = lexArg('{', true);
810 mathPrintError("Unrecognized token");
811 lyxerr[Debug::MATHED] << "[" << t << " " << yytext << "]" << endl;
814 } // end of big switch
816 if (flags & FLAG_LEAVE) {
817 flags &= ~FLAG_LEAVE;
822 lyxerr << " Math Panic, expect problems!" << endl;
823 // Search for the end command.
826 } while (yyis->good() && t != LM_TK_END && t);
834 void mathed_parse_end(LyXLex & lex)
836 // Update line number
837 lex.setLineNo(yylineno);
839 // reading of end_inset
842 if (lex.getString() == "\\end_inset")
844 lyxerr[Debug::MATHED] << "InsetFormula::Read: Garbage before \\end_inset,"
845 " or missing \\end_inset!" << endl;
852 MathArray mathed_parse_cell(string const & str)
854 istringstream is(str.c_str());
858 mathed_parse_into(ar, 0);
864 MathMacroTemplate * mathed_parse_macro(string const & str)
866 istringstream is(str.c_str());
867 return mathed_parse_macro(is);
870 MathMacroTemplate * mathed_parse_macro(istream & is)
874 return mathed_parse_macro();
877 MathMacroTemplate * mathed_parse_macro(LyXLex & lex)
879 yyis = &lex.getStream();
880 yylineno = lex.getLineNo();
881 MathMacroTemplate * p = mathed_parse_macro();
882 mathed_parse_end(lex);
888 MathMatrixInset * mathed_parse_normal(string const & str)
890 istringstream is(str.c_str());
891 return mathed_parse_normal(is);
894 MathMatrixInset * mathed_parse_normal(istream & is)
898 return mathed_parse_normal();
901 MathMatrixInset * mathed_parse_normal(LyXLex & lex)
903 yyis = &lex.getStream();
904 yylineno = lex.getLineNo();
905 MathMatrixInset * p = mathed_parse_normal();
906 mathed_parse_end(lex);