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_rowst.h"
29 #include "math_iter.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_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_dotsinset.h"
41 #include "math_fracinset.h"
42 #include "math_deliminset.h"
43 #include "math_decorationinset.h"
45 #include "support/lyxlib.h"
46 #include "mathed/support.h"
47 #include "boost/array.hpp"
53 extern MathMatrixInset * create_multiline(short int type, int cols);
57 FLAG_BRACE = 1, // A { needed
58 FLAG_BRACE_ARG = 2, // Next { is argument
59 FLAG_BRACE_OPT = 4, // Optional {
60 FLAG_BRACE_LAST = 8, // Last } ends the parsing process
61 FLAG_BRACK_ARG = 16, // Optional [
62 FLAG_RIGHT = 32, // Next right ends the parsing process
63 FLAG_END = 64, // Next end ends the parsing process
64 FLAG_BRACE_FONT = 128, // Next } closes a font
65 FLAG_BRACK_END = 256 // Next ] ends the parsing process
86 MathedInsetTypes mathed_env = LM_OT_MIN;
91 int const latex_mathenv_num = 12;
92 char const * latex_mathenv[latex_mathenv_num] = {
110 char const * latex_special_chars = "#$%&_{}";
112 // These are lexical codes, not semantic
118 LexBOP, // Binary operators or relations
130 static lexcode_enum lexcode[256];
131 #warning Replace with string
132 //static char yytext[256];
133 static array<char, 256> yytext;
135 static istream * yyis;
136 static bool yy_mtextmode= false;
139 void mathPrintError(string const & msg)
141 lyxerr << "Line ~" << yylineno << ": Math parse error: "
149 for (int i = 0; i <= 255; ++i) {
150 if (isalpha(i)) lexcode[i] = LexAlpha;
151 else if (isdigit(i)) lexcode[i] = LexDigit;
152 else if (isspace(i)) lexcode[i] = LexSpace;
153 else lexcode[i] = LexNone;
156 lexcode['\t'] = lexcode['\f'] = lexcode[' '] = LexSpace;
157 lexcode['\n'] = LexNewLine;
158 lexcode['%'] = LexComment;
159 lexcode['#'] = LexArgument;
160 lexcode['+'] = lexcode['-'] = lexcode['*'] = lexcode['/'] =
161 lexcode['<'] = lexcode['>'] = lexcode['='] = LexBOP;
163 lexcode['!'] = lexcode[','] = lexcode[':'] = lexcode[';'] = LexMathSpace;
165 lexcode['('] = lexcode[')'] = lexcode['|'] = lexcode['.'] =
166 lexcode['?'] = LexOther;
168 lexcode['\''] = lexcode['@'] = LexAlpha;
170 lexcode['['] = lexcode[']'] = lexcode['^'] = lexcode['_'] =
171 lexcode['&'] = LexSelf;
173 lexcode['\\'] = LexESC;
174 lexcode['{'] = LexOpen;
175 lexcode['}'] = LexClose;
180 char LexGetArg(char lf, bool accept_spaces= false)
184 while (yyis->good()) {
191 lyxerr << "Math parse error: unexpected '"
199 (lf == '{') ? '}' : ((lf == '[') ? ']' : ((lf == '(') ? ')' : 0));
201 lyxerr << "Math parse error: unknown bracket '"
202 << lf << "'" << endl;
205 char * p = &yytext[0];
212 if ((c > ' ' || (c == ' ' && accept_spaces)) && bcnt > 0) *(p++) = c;
213 } while (bcnt > 0 && yyis->good() && p - yytext.data() < 255);
222 static int init_done = 0;
224 if (!init_done) LexInitCodes();
228 while (yyis->good()) {
232 if (yy_mtextmode && c == ' ') {
237 if (lexcode[c] == LexNewLine) {
242 if (lexcode[c] == LexComment) {
246 } while (c != '\n' % yyis->good()); // eat comments
249 if (lexcode[c] == LexDigit
250 || lexcode[c] == LexOther
251 || lexcode[c] == LexMathSpace) {
255 if (lexcode[c] == LexAlpha) {
259 if (lexcode[c] == LexBOP) {
263 if (lexcode[c] == LexSelf) {
266 if (lexcode[c] == LexArgument) {
270 return LM_TK_ARGUMENT;
272 if (lexcode[c] == LexOpen) {
275 if (lexcode[c] == LexClose) {
278 if (lexcode[c] == LexESC) {
282 return LM_TK_NEWLINE;
285 yylval.i = LM_OT_MIN;
289 yylval.i = LM_OT_MIN;
293 yylval.i = LM_OT_PAR;
297 yylval.i = LM_OT_PAR;
300 if (strchr(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);
307 yylval.i = (i < 4) ? i : 0;
310 if (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
311 char * p = &yytext[0];
312 while ((lexcode[c] == LexAlpha || lexcode[c] == LexDigit)
313 && p - yytext.data() < 255) {
320 if (yyis->good()) yyis->putback(c);
321 latexkeys const * l = in_word_set (yytext.data(), strlen(yytext.data()));
323 if (l->token == LM_TK_BEGIN || l->token == LM_TK_END) {
326 // for (i = 0; i < 5 && compare(yytext, latex_mathenv[i],
327 // strlen(latex_mathenv[i])); ++i);
329 for (i = 0; i < latex_mathenv_num
330 && compare(yytext.data(), latex_mathenv[i]); ++i);
333 if (l->token == LM_TK_SPACE)
339 yylval.s = yytext.data();
350 int parse_align(char * hor, char *)
353 for (char * c = hor; c && *c > ' '; ++c) ++nc;
358 // Accent hacks only for 0.12. Stolen from Cursor.
365 void setAccent(int ac)
367 if (ac > 0 && accent < 8) {
368 nestaccent[accent++] = ac;
370 accent = 0; // consumed!
375 MathedInset * doAccent(byte c, MathedTextCodes t)
377 MathedInset * ac = 0;
379 for (int i = accent - 1; i >= 0; --i) {
381 ac = new MathAccentInset(c, t, nestaccent[i]);
383 ac = new MathAccentInset(ac, nestaccent[i]);
385 accent = 0; // consumed!
392 MathedInset * doAccent(MathedInset * p)
394 MathedInset * ac = 0;
396 for (int i = accent - 1; i >= 0; --i) {
398 ac = new MathAccentInset(p, nestaccent[i]);
400 ac = new MathAccentInset(ac, nestaccent[i]);
402 accent = 0; // consumed!
408 MathedArray * mathed_parse(unsigned flags = 0, MathedArray * array = 0,
409 MathParInset ** mtx = 0)
414 static int plevel = -1;
415 static int size = LM_ST_TEXT;
416 MathedTextCodes varcode = LM_TC_VAR;
417 MathedInset * binset = 0;
418 static MathMacroTemplate * macro = 0;
423 MathParInset * mt = (mtx) ? *mtx : 0;//(MathParInset*)0;
424 MathedRowSt * crow = (mt) ? mt->getRowSt() : 0;
427 if (!array) array = new MathedArray;
428 MathedIter data(array);
430 if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
431 if ((flags & FLAG_BRACK_ARG) && t == '[') {
434 mathPrintError("Expected {. Maybe you forgot to enclose an argument in {}");
439 MathedInsetTypes fractype = LM_OT_FRAC;
444 data.insertInset(doAccent(yylval.i, varcode), LM_TC_INSET);
446 data.insert(yylval.i, varcode); //LM_TC_VAR);
452 data.insertInset(macro->getMacroPar(yylval.i-1), LM_TC_INSET);
456 case LM_TK_NEWCOMMAND:
461 string const name(&yytext[1]);
463 // ugly trick to be removed soon (lyx3)
464 char const c = yyis->peek();
467 na = lyx::atoi(yytext.data());
469 macro = new MathMacroTemplate(name, na);
470 flags = FLAG_BRACE|FLAG_BRACE_LAST;
472 macro->setData(*array);
477 data.insert(yylval.i, LM_TC_SPECIAL);
483 data.insertInset(doAccent(yylval.i, LM_TC_CONST), LM_TC_INSET);
485 data.insert(yylval.i, LM_TC_CONST);
491 if (accent && tprev == LM_TK_ACCENT) {
492 acc_braces[acc_brace++] = brace;
495 if (flags & FLAG_BRACE_OPT) {
496 flags &= ~FLAG_BRACE_OPT;
500 if (flags & FLAG_BRACE)
501 flags &= ~FLAG_BRACE;
503 data.insert('{', LM_TC_TEX);
511 mathPrintError("Unmatching braces");
515 if (acc_brace && brace == acc_braces[acc_brace-1]-1) {
519 if (flags & FLAG_BRACE_FONT) {
521 yy_mtextmode = false;
522 flags &= ~FLAG_BRACE_FONT;
525 if (brace == 0 && (flags & FLAG_BRACE_LAST)) {
529 data.insert('}', LM_TC_TEX);
536 if (flags & FLAG_BRACK_ARG) {
537 flags &= ~FLAG_BRACK_ARG;
538 char const rg = LexGetArg('[');
540 mathPrintError("Expected ']'");
544 // if (arg) strcpy(arg, yytext);
546 data.insert('[', LM_TC_CONST);
551 if (flags & FLAG_BRACK_END) {
555 data.insert(']', LM_TC_CONST);
561 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
562 MathedArray * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
564 // lyxerr << "UP[" << p->GetStyle() << "]" << endl;
565 data.insertInset(p, LM_TC_UP);
570 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
571 MathedArray * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
573 data.insertInset(p, LM_TC_DOWN);
580 binset->SetLimits(bool(yylval.l->id));
588 if ((flags & FLAG_END) && mt && data.getCol()<mt->GetColumns() - 1) {
589 data.setNumCols(mt->GetColumns());
590 data.insert('T', LM_TC_TAB);
592 mathPrintError("Unexpected tab");
593 // debug info. [made that conditional -JMarc]
594 if (lyxerr.debugging(Debug::MATHED))
595 lyxerr << data.getCol() << " " << mt->GetColumns() << endl;
600 if (mt && (flags & FLAG_END)) {
601 if (mt->Permit(LMPF_ALLOW_CR)) {
603 crow->setNext(new MathedRowSt(mt->GetColumns() + 1)); // this leaks
604 crow = crow->getNext();
606 data.insert('K', LM_TC_CR);
608 mathPrintError("Unexpected newline");
614 binset = new MathBigopInset(yylval.l->name, yylval.l->id);
615 data.insertInset(binset, LM_TC_INSET);
620 if (yylval.l->id < 256) {
621 MathedTextCodes tc = MathIsBOPS(yylval.l->id) ? LM_TC_BOPS: LM_TC_SYMB;
623 data.insertInset(doAccent(yylval.l->id, tc), LM_TC_INSET);
625 data.insert(yylval.l->id, tc);
627 MathFuncInset * bg = new MathFuncInset(yylval.l->name);
629 data.insertInset(doAccent(bg), LM_TC_INSET);
631 #warning This is suspisious! (Lgb)
632 // it should not take a bool as second arg (Lgb)
633 data.insertInset(bg, true);
642 data.insertInset(doAccent(yylval.i, LM_TC_BOP), LM_TC_INSET);
644 data.insert(yylval.i, LM_TC_BOP);
650 mt->UserSetSize(yylval.l->id);
657 MathSpaceInset * sp = new MathSpaceInset(yylval.i);
658 data.insertInset(sp, LM_TC_INSET);
664 MathDotsInset * p = new MathDotsInset(yylval.l->name, yylval.l->id);
665 data.insertInset(p, LM_TC_INSET);
669 fractype = LM_OT_STACKREL;
672 MathFracInset * fc = new MathFracInset(fractype);
673 MathedArray * num = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
674 MathedArray * den = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
675 fc->SetData(*num, *den);
676 data.insertInset(fc, LM_TC_ACTIVE_INSET);
683 char c; yyis->get(c);
686 rt = new MathRootInset(size);
687 rt->setArgumentIdx(0);
688 rt->setData(*mathed_parse(FLAG_BRACK_END, 0, &rt));
689 rt->setArgumentIdx(1);
692 rt = new MathSqrtInset(size);
694 rt->setData(*mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST, 0, &rt));
695 data.insertInset(rt, LM_TC_ACTIVE_INSET);
702 if (lfd == LM_TK_SYM || lfd == LM_TK_STR || lfd == LM_TK_BOP|| lfd == LM_TK_SPECIAL)
703 lfd = (lfd == LM_TK_SYM) ? yylval.l->id: yylval.i;
704 // lyxerr << "L[" << lfd << " " << lfd << "]";
705 MathedArray * a = mathed_parse(FLAG_RIGHT);
707 // lyxerr << "R[" << rgd << "]";
708 if (rgd == LM_TK_SYM || rgd == LM_TK_STR || rgd == LM_TK_BOP || rgd == LM_TK_SPECIAL)
709 rgd = (rgd == LM_TK_SYM) ? yylval.l->id: yylval.i;
710 MathDelimInset * dl = new MathDelimInset(lfd, rgd);
712 data.insertInset(dl, LM_TC_ACTIVE_INSET);
713 // lyxerr << "RL[" << lfd << " " << rgd << "]";
718 if (flags & FLAG_RIGHT) {
722 mathPrintError("Unmatched right delimiter");
730 varcode = static_cast<MathedTextCodes>(yylval.l->id);
731 yy_mtextmode = bool(varcode == LM_TC_TEXTRM);
732 flags |= (FLAG_BRACE|FLAG_BRACE_FONT);
737 MathDecorationInset * sq = new MathDecorationInset(yylval.l->id,
739 sq->setData(*mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
740 data.insertInset(sq, LM_TC_ACTIVE_INSET);
745 setAccent(yylval.l->id);
751 crow->setNumbered(false);
758 #warning This must leak. (Lgb)
759 // if (accent) this must leak... (Lgb)
760 MathedInset * bg = new MathFuncInset(yylval.l->name);
762 data.insert(t, LM_TC_CONST);
764 data.insertInset(bg, LM_TC_INSET);
769 data.insertInset(new MathFuncInset(yylval.l->name, LM_OT_FUNCLIM),
777 MathMacroTable::mathMTable.getMacro(yylval.s);
780 data.insertInset(doAccent(p), p->getTCode());
782 data.insertInset(p, p->getTCode());
783 for (int i = 0; p->setArgumentIdx(i); ++i)
784 p->setData(*mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
787 MathedInset * q = new MathFuncInset(yylval.s, LM_OT_UNDEF);
789 data.insertInset(doAccent(q), LM_TC_INSET);
791 data.insertInset(q, LM_TC_INSET);
798 if (mathed_env != yylval.i && yylval.i != LM_OT_MATRIX)
799 mathPrintError("Unmatched environment");
800 // debug info [made that conditional -JMarc]
801 if (lyxerr.debugging(Debug::MATHED))
802 lyxerr << "[" << yylval.i << "]" << endl;
804 if (mt) { // && (flags & FLAG_END)) {
812 if (yylval.i == LM_OT_MATRIX) {
813 char ar[120], ar2[8];
814 ar[0] = ar2[0] = '\0';
815 char rg = LexGetArg(0);
817 strcpy(ar2, yytext.data());
820 strcpy(ar, yytext.data());
821 int const nc = parse_align(ar, ar2);
822 MathParInset * mm = new MathMatrixInset(nc, 0);
823 mm->SetAlign(ar2[0], ar);
824 data.insertInset(mm, LM_TC_ACTIVE_INSET);
825 mathed_parse(FLAG_END, &mm->GetData(), &mm);
826 } else if (is_eqn_type(yylval.i)) {
828 mathPrintError("Misplaced environment");
832 mathPrintError("0 paragraph.");
836 mathed_env = static_cast<MathedInsetTypes>(yylval.i);
837 if (mathed_env != LM_OT_MIN) {
838 size = LM_ST_DISPLAY;
839 if (is_multiline(mathed_env)) {
841 if (is_multicolumn(mathed_env)) {
842 if (mathed_env != LM_OT_ALIGNAT &&
843 mathed_env != LM_OT_ALIGNATN &&
848 lyxerr << "Math parse error: unexpected '"
852 cols = strToInt(string(yytext.data()));
854 mt = create_multiline(mathed_env, cols);
857 // data.Insert(' ', LM_TC_TAB);
858 // data.Insert(' ', LM_TC_TAB);
862 mt->SetType(mathed_env);
863 crow = mt->getRowSt();
867 lyxerr << "MATH BEGIN[" << mathed_env << "]" << endl;
870 // lyxerr << "MATHCRO[" << yytext << "]";
872 MathMacroTable::mathMTable.getMacro(yytext.data());
874 data.insertInset(p, p->getTCode());
875 p->setArgumentIdx(0);
876 mathed_parse(FLAG_END, &p->GetData(), reinterpret_cast<MathParInset**>(&p));
877 // for (int i = 0; p->setArgumentIdx(i); ++i)
878 // p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
880 mathPrintError("Unrecognized environment");
888 MathMacroTable::mathMTable.getMacro(yylval.l->name);
892 data.insertInset(doAccent(p), LM_TC_INSET);
894 data.insertInset(p, static_cast<MathMacro*>(p)->getTCode());
901 char const rg = LexGetArg('\0', true);
903 mathPrintError("Expected '{'");
905 lyxerr << "[" << yytext.data() << "]" << endl;
910 crow->setLabel(yytext.data());
912 mathed_label = yytext.data();
915 lyxerr << "Label[" << mathed_label << "]" << endl;
920 mathPrintError("Unrecognized token");
922 lyxerr << "[" << t << " " << yytext.data() << "]" << endl;
927 lyxerr << " Math Panic, expect problems!" << endl;
928 // Search for the end command.
931 } while (t != LM_TK_END && t);
935 if ((flags & FLAG_BRACE_OPT)/* && t!= '^' && t!= '_'*/) {
936 flags &= ~FLAG_BRACE_OPT;
937 //data.Insert (LM_TC_CLOSE);
946 void mathed_parser_file(istream & is, int lineno)
950 if (!MathMacroTable::built)
951 MathMacroTable::mathMTable.builtinMacros();
955 int mathed_parser_lineno()