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 "math_parser.h"
26 #include "math_parser.h"
27 #include "math_iter.h"
28 #include "math_inset.h"
29 #include "math_macro.h"
30 #include "math_root.h"
32 #include "support/lyxlib.h"
44 FLAG_BRACE = 1, // A { needed
45 FLAG_BRACE_ARG = 2, // Next { is argument
46 FLAG_BRACE_OPT = 4, // Optional {
47 FLAG_BRACE_LAST = 8, // Last } ends the parsing process
48 FLAG_BRACK_ARG = 16, // Optional [
49 FLAG_RIGHT = 32, // Next right ends the parsing process
50 FLAG_END = 64, // Next end ends the parsing process
51 FLAG_BRACE_FONT = 128, // Next } closes a font
52 FLAG_BRACK_END = 256 // Next ] ends the parsing process
60 short mathed_env = LM_EN_INTEXT;
65 char const * latex_mathenv[] = {
76 char const * latex_mathspace[] = {
77 "!", ",", ":", ";", "quad", "qquad"
81 char const * latex_special_chars = "#$%&_{}";
83 // These are lexical codes, not semantic
89 LexBOP, // Binary operators or relations
101 static lexcode_enum lexcode[256];
102 static char yytext[256];
104 static istream * yyis;
105 static bool yy_mtextmode= false;
108 void mathPrintError(string const & msg)
110 lyxerr << "Line ~" << yylineno << ": Math parse error: "
118 for (int i = 0; i <= 255; ++i) {
119 if (isalpha(i)) lexcode[i] = LexAlpha;
120 else if (isdigit(i)) lexcode[i] = LexDigit;
121 else if (isspace(i)) lexcode[i] = LexSpace;
122 else lexcode[i] = LexNone;
125 lexcode['\t'] = lexcode['\f'] = lexcode[' '] = LexSpace;
126 lexcode['\n'] = LexNewLine;
127 lexcode['%'] = LexComment;
128 lexcode['#'] = LexArgument;
129 lexcode['+'] = lexcode['-'] = lexcode['*'] = lexcode['/'] =
130 lexcode['<'] = lexcode['>'] = lexcode['='] = LexBOP;
132 lexcode['!'] = lexcode[','] = lexcode[':'] = lexcode[';'] = LexMathSpace;
134 lexcode['('] = lexcode[')'] = lexcode['|'] = lexcode['.'] =
135 lexcode['?'] = LexOther;
137 lexcode['\''] = lexcode['@'] = LexAlpha;
139 lexcode['['] = lexcode[']'] = lexcode['^'] = lexcode['_'] =
140 lexcode['&'] = LexSelf;
142 lexcode['\\'] = LexESC;
143 lexcode['{'] = LexOpen;
144 lexcode['}'] = LexClose;
149 char LexGetArg(char lf, bool accept_spaces= false)
153 while (yyis->good()) {
157 if (!lf) lf = c; else
159 lyxerr << "Math parse error: unexpected '"
165 (lf == '{') ? '}' : ((lf == '[') ? ']' : ((lf == '(') ? ')' : 0));
167 lyxerr << "Math parse error: unknown bracket '"
168 << lf << "'" << endl;
171 char * p = &yytext[0];
178 if ((c > ' ' || (c == ' ' && accept_spaces)) && bcnt > 0) *(p++) = c;
179 } while (bcnt > 0 && yyis->good());
188 static int init_done = 0;
190 if (!init_done) LexInitCodes();
194 while (yyis->good()) {
198 if (yy_mtextmode && c == ' ') {
203 if (lexcode[c] == LexNewLine) {
208 if (lexcode[c] == LexComment) {
212 } while (c != '\n' % yyis->good()); // eat comments
215 if (lexcode[c] == LexDigit
216 || lexcode[c] == LexOther
217 || lexcode[c] == LexMathSpace) {
221 if (lexcode[c] == LexAlpha) {
225 if (lexcode[c] == LexBOP) {
229 if (lexcode[c] == LexSelf) {
232 if (lexcode[c] == LexArgument) {
236 return LM_TK_ARGUMENT;
238 if (lexcode[c] == LexOpen) {
241 if (lexcode[c] == LexClose) {
244 if (lexcode[c] == LexESC) {
248 return LM_TK_NEWLINE;
251 yylval.i = LM_EN_INTEXT;
255 yylval.i = LM_EN_INTEXT;
259 yylval.i = LM_EN_DISPLAY;
263 yylval.i = LM_EN_DISPLAY;
266 if (strchr(latex_special_chars, c)) {
268 return LM_TK_SPECIAL;
270 if (lexcode[c] == LexMathSpace) {
272 for (i = 0; i < 4 && static_cast<int>(c) != latex_mathspace[i][0]; ++i);
273 yylval.i = (i < 4) ? i : 0;
276 if (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
277 char * p = &yytext[0];
278 while (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
285 if (yyis->good()) yyis->putback(c);
286 latexkeys * l = in_word_set (yytext, strlen(yytext));
288 if (l->token == LM_TK_BEGIN || l->token == LM_TK_END) {
291 // for (i = 0; i < 5 && strncmp(yytext, latex_mathenv[i],
292 // strlen(latex_mathenv[i])); ++i);
294 for (i = 0; i < 6 && strcmp(yytext, latex_mathenv[i]); ++i);
297 if (l->token == LM_TK_SPACE)
314 int parse_align(char * hor, char *)
317 for (char * c = hor; c && *c > ' '; ++c) ++nc;
322 // Accent hacks only for 0.12. Stolen from Cursor.
329 void setAccent(int ac)
331 if (ac > 0 && accent < 8) {
332 nestaccent[accent++] = ac;
334 accent = 0; // consumed!
339 MathedInset * doAccent(byte c, MathedTextCodes t)
341 MathedInset * ac = 0;
343 for (int i = accent - 1; i >= 0; --i) {
345 ac = new MathAccentInset(c, t, nestaccent[i]);
347 ac = new MathAccentInset(ac, nestaccent[i]);
349 accent = 0; // consumed!
356 MathedInset * doAccent(MathedInset * p)
358 MathedInset * ac = 0;
360 for (int i = accent - 1; i >= 0; --i) {
362 ac = new MathAccentInset(p, nestaccent[i]);
364 ac = new MathAccentInset(ac, nestaccent[i]);
366 accent = 0; // consumed!
372 LyxArrayBase * mathed_parse(unsigned flags, LyxArrayBase * array,
378 static int plevel = -1;
379 static int size = LM_ST_TEXT;
380 MathedTextCodes varcode = LM_TC_VAR;
381 MathedInset * binset = 0;
382 static MathMacroTemplate * macro = 0;
387 MathParInset * mt = (mtx) ? *mtx : 0;//(MathParInset*)0;
388 MathedRowSt * crow = (mt) ? mt->getRowSt() : 0;
391 if (!array) array = new LyxArrayBase;
392 MathedIter data(array);
394 if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
395 if ((flags & FLAG_BRACK_ARG) && t == '[') {
398 mathPrintError("Expected {. Maybe you forgot to enclose an argument in {}");
403 MathedInsetTypes fractype = LM_OT_FRAC;
408 data.Insert(doAccent(yylval.i, varcode));
410 data.Insert (yylval.i, varcode); //LM_TC_VAR);
416 data.Insert(macro->getMacroPar(yylval.i-1), LM_TC_INSET);
420 case LM_TK_NEWCOMMAND:
425 string const name(&yytext[1]);
427 // ugly trick to be removed soon (lyx3)
428 char const c = yyis->peek();
431 na = lyx::atoi(yytext);
433 macro = new MathMacroTemplate(name, na);
434 flags = FLAG_BRACE|FLAG_BRACE_LAST;
436 macro->SetData(array);
441 data.Insert (yylval.i, LM_TC_SPECIAL);
447 data.Insert(doAccent(yylval.i, LM_TC_CONST));
449 data.Insert (yylval.i, LM_TC_CONST);
455 if (accent && tprev == LM_TK_ACCENT) {
456 acc_braces[acc_brace++] = brace;
459 if (flags & FLAG_BRACE_OPT) {
460 flags &= ~FLAG_BRACE_OPT;
464 if (flags & FLAG_BRACE)
465 flags &= ~FLAG_BRACE;
467 data.Insert ('{', LM_TC_TEX);
475 mathPrintError("Unmatching braces");
479 if (acc_brace && brace == acc_braces[acc_brace-1]-1) {
483 if (flags & FLAG_BRACE_FONT) {
485 yy_mtextmode = false;
486 flags &= ~FLAG_BRACE_FONT;
489 if (brace == 0 && (flags & FLAG_BRACE_LAST)) {
493 data.Insert ('}', LM_TC_TEX);
500 if (flags & FLAG_BRACK_ARG) {
501 flags &= ~FLAG_BRACK_ARG;
502 char const rg = LexGetArg('[');
504 mathPrintError("Expected ']'");
508 // if (arg) strcpy(arg, yytext);
515 if (flags & FLAG_BRACK_END) {
525 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
526 LyxArrayBase * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
528 // lyxerr << "UP[" << p->GetStyle() << "]" << endl;
529 data.Insert (p, LM_TC_UP);
534 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
535 LyxArrayBase * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
537 data.Insert (p, LM_TC_DOWN);
544 binset->SetLimits(bool(yylval.l->id));
552 if ((flags & FLAG_END) && mt && data.getCol()<mt->GetColumns() - 1) {
553 data.setNumCols(mt->GetColumns());
554 data.Insert('T', LM_TC_TAB);
556 mathPrintError("Unexpected tab");
557 // debug info. [made that conditional -JMarc]
558 if (lyxerr.debugging(Debug::MATHED))
559 lyxerr << data.getCol() << " " << mt->GetColumns() << endl;
564 if (mt && (flags & FLAG_END)) {
565 if (mt->Permit(LMPF_ALLOW_CR)) {
567 crow->setNext(new MathedRowSt(mt->GetColumns() + 1)); // this leaks
568 crow = crow->getNext();
570 data.Insert('K', LM_TC_CR);
572 mathPrintError("Unexpected newline");
578 binset = new MathBigopInset(yylval.l->name, yylval.l->id);
584 if (yylval.l->id < 256) {
585 MathedTextCodes tc = MathIsBOPS(yylval.l->id) ? LM_TC_BOPS: LM_TC_SYMB;
587 data.Insert(doAccent(yylval.l->id, tc));
589 data.Insert(yylval.l->id, tc);
591 MathFuncInset * bg = new MathFuncInset(yylval.l->name);
593 data.Insert(doAccent(bg));
595 data.Insert(bg, true);
602 data.Insert(doAccent(yylval.i, LM_TC_BOP));
604 data.Insert(yylval.i, LM_TC_BOP);
610 mt->UserSetSize(yylval.l->id);
617 MathSpaceInset * sp = new MathSpaceInset(yylval.i);
624 MathDotsInset * p = new MathDotsInset(yylval.l->name, yylval.l->id);
629 fractype = LM_OT_STACKREL;
632 MathFracInset * fc = new MathFracInset(fractype);
633 LyxArrayBase * num = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
634 LyxArrayBase * den = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
635 fc->SetData(num, den);
636 data.Insert(fc, LM_TC_ACTIVE_INSET);
643 char c; yyis->get(c);
646 rt = new MathRootInset(size);
647 rt->setArgumentIdx(0);
648 rt->SetData(mathed_parse(FLAG_BRACK_END, 0, &rt));
649 rt->setArgumentIdx(1);
652 rt = new MathSqrtInset(size);
654 rt->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST, 0, &rt));
655 data.Insert(rt, LM_TC_ACTIVE_INSET);
662 if (lfd == LM_TK_SYM || lfd == LM_TK_STR || lfd == LM_TK_BOP|| lfd == LM_TK_SPECIAL)
663 lfd = (lfd == LM_TK_SYM) ? yylval.l->id: yylval.i;
664 // lyxerr << "L[" << lfd << " " << lfd << "]";
665 LyxArrayBase * a = mathed_parse(FLAG_RIGHT);
667 // lyxerr << "R[" << rgd << "]";
668 if (rgd == LM_TK_SYM || rgd == LM_TK_STR || rgd == LM_TK_BOP || rgd == LM_TK_SPECIAL)
669 rgd = (rgd == LM_TK_SYM) ? yylval.l->id: yylval.i;
670 MathDelimInset * dl = new MathDelimInset(lfd, rgd);
672 data.Insert(dl, LM_TC_ACTIVE_INSET);
673 // lyxerr << "RL[" << lfd << " " << rgd << "]";
678 if (flags & FLAG_RIGHT) {
682 mathPrintError("Unmatched right delimiter");
690 varcode = static_cast<MathedTextCodes>(yylval.l->id);
691 yy_mtextmode = bool(varcode == LM_TC_TEXTRM);
692 flags |= (FLAG_BRACE|FLAG_BRACE_FONT);
697 MathDecorationInset * sq = new MathDecorationInset(yylval.l->id,
699 sq->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
700 data.Insert(sq, LM_TC_ACTIVE_INSET);
705 setAccent(yylval.l->id);
711 crow->setNumbered(false);
718 MathedInset * bg = new MathFuncInset(yylval.l->name);
727 data.Insert(new MathFuncInset(yylval.l->name, LM_OT_FUNCLIM));
734 MathMacroTable::mathMTable.getMacro(yylval.s);
737 data.Insert(doAccent(p), p->getTCode());
739 data.Insert(p, p->getTCode());
740 for (int i = 0; p->setArgumentIdx(i); ++i)
741 p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
744 MathedInset * q = new MathFuncInset(yylval.s, LM_OT_UNDEF);
746 data.Insert(doAccent(q));
755 if (mathed_env != yylval.i && yylval.i!= LM_EN_ARRAY)
756 mathPrintError("Unmatched environment");
757 // debug info [made that conditional -JMarc]
758 if (lyxerr.debugging(Debug::MATHED))
759 lyxerr << "[" << yylval.i << "]" << endl;
761 if (mt) { // && (flags & FLAG_END)) {
769 if (yylval.i == LM_EN_ARRAY) {
770 char ar[120], ar2[8];
771 ar[0] = ar2[0] = '\0';
772 char rg = LexGetArg(0);
778 int const nc = parse_align(ar, ar2);
779 MathParInset * mm = new MathMatrixInset(nc, 0);
780 mm->SetAlign(ar2[0], ar);
781 data.Insert(mm, LM_TC_ACTIVE_INSET);
782 mathed_parse(FLAG_END, mm->GetData(), &mm);
783 } else if (yylval.i >= LM_EN_INTEXT && yylval.i<= LM_EN_EQNARRAY) {
785 mathPrintError("Misplaced environment");
789 mathPrintError("0 paragraph.");
793 mathed_env = yylval.i;
794 if (mathed_env>= LM_EN_DISPLAY) {
795 size = LM_ST_DISPLAY;
796 if (mathed_env>LM_EN_EQUATION) {
797 mt = new MathMatrixInset(3, -1);
798 mt->SetAlign(' ', "rcl");
801 // data.Insert(' ', LM_TC_TAB);
802 // data.Insert(' ', LM_TC_TAB);
806 mt->SetType(mathed_env);
807 crow = mt->getRowSt();
811 lyxerr << "MATH BEGIN[" << mathed_env << "]" << endl;
814 // lyxerr << "MATHCRO[" << yytext << "]";
816 MathMacroTable::mathMTable.getMacro(yytext);
818 data.Insert(p, p->getTCode());
819 p->setArgumentIdx(0);
820 mathed_parse(FLAG_END, p->GetData(), reinterpret_cast<MathParInset**>(&p));
821 // for (int i = 0; p->setArgumentIdx(i); ++i)
822 // p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
824 mathPrintError("Unrecognized environment");
832 MathMacroTable::mathMTable.getMacro(yylval.l->name);
836 data.Insert(doAccent(p));
838 data.Insert(p, static_cast<MathMacro*>(p)->getTCode());
845 char const rg = LexGetArg('\0', true);
847 mathPrintError("Expected '{'");
849 lyxerr << "[" << yytext << "]" << endl;
854 crow->setLabel(yytext);
856 mathed_label = yytext;
859 lyxerr << "Label[" << mathed_label << "]" << endl;
864 mathPrintError("Unrecognized token");
866 lyxerr << "[" << t << " " << yytext << "]" << endl;
871 lyxerr << " Math Panic, expect problems!" << endl;
872 // Search for the end command.
875 } while (t != LM_TK_END && t);
879 if ((flags & FLAG_BRACE_OPT)/* && t!= '^' && t!= '_'*/) {
880 flags &= ~FLAG_BRACE_OPT;
881 //data.Insert (LM_TC_CLOSE);
890 void mathed_parser_file(istream & is, int lineno)
894 if (!MathMacroTable::built)
895 MathMacroTable::mathMTable.builtinMacros();
899 int mathed_parser_lineno()