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"
38 FLAG_BRACE = 1, // A { needed
39 FLAG_BRACE_ARG = 2, // Next { is argument
40 FLAG_BRACE_OPT = 4, // Optional {
41 FLAG_BRACE_LAST = 8, // Last } ends the parsing process
42 FLAG_BRACK_ARG = 16, // Optional [
43 FLAG_RIGHT = 32, // Next right ends the parsing process
44 FLAG_END = 64, // Next end ends the parsing process
45 FLAG_BRACE_FONT = 128, // Next } closes a font
46 FLAG_BRACK_END = 256 // Next ] ends the parsing process
54 short mathed_env = LM_EN_INTEXT;
59 char const * latex_mathenv[] = {
70 char const * latex_mathspace[] = {
71 "!", ",", ":", ";", "quad", "qquad"
75 char const * latex_special_chars = "#$%&_{}";
77 // These are lexical codes, not semantic
83 LexBOP, // Binary operators or relations
95 static lexcode_enum lexcode[256];
96 static char yytext[256];
98 static istream * yyis;
99 static bool yy_mtextmode= false;
102 void mathPrintError(string const & msg)
104 lyxerr << "Line ~" << yylineno << ": Math parse error: "
112 for (int i = 0; i <= 255; ++i) {
113 if (isalpha(i)) lexcode[i] = LexAlpha;
114 else if (isdigit(i)) lexcode[i] = LexDigit;
115 else if (isspace(i)) lexcode[i] = LexSpace;
116 else lexcode[i] = LexNone;
119 lexcode['\t'] = lexcode['\f'] = lexcode[' '] = LexSpace;
120 lexcode['\n'] = LexNewLine;
121 lexcode['%'] = LexComment;
122 lexcode['#'] = LexArgument;
123 lexcode['+'] = lexcode['-'] = lexcode['*'] = lexcode['/'] =
124 lexcode['<'] = lexcode['>'] = lexcode['='] = LexBOP;
126 lexcode['!'] = lexcode[','] = lexcode[':'] = lexcode[';'] = LexMathSpace;
128 lexcode['('] = lexcode[')'] = lexcode['|'] = lexcode['.'] =
129 lexcode['?'] = LexOther;
131 lexcode['\''] = lexcode['@'] = LexAlpha;
133 lexcode['['] = lexcode[']'] = lexcode['^'] = lexcode['_'] =
134 lexcode['&'] = LexSelf;
136 lexcode['\\'] = LexESC;
137 lexcode['{'] = LexOpen;
138 lexcode['}'] = LexClose;
143 char LexGetArg(char lf, bool accept_spaces= false)
147 while (yyis->good()) {
151 if (!lf) lf = c; else
153 lyxerr << "Math parse error: unexpected '"
159 (lf == '{') ? '}' : ((lf == '[') ? ']' : ((lf == '(') ? ')' : 0));
161 lyxerr << "Math parse error: unknown bracket '"
162 << lf << "'" << endl;
165 char * p = &yytext[0];
172 if ((c > ' ' || (c == ' ' && accept_spaces)) && bcnt > 0) *(p++) = c;
173 } while (bcnt > 0 && yyis->good());
182 static int init_done = 0;
184 if (!init_done) LexInitCodes();
188 while (yyis->good()) {
192 if (yy_mtextmode && c == ' ') {
197 if (lexcode[c] == LexNewLine) {
202 if (lexcode[c] == LexComment) {
206 } while (c != '\n' % yyis->good()); // eat comments
209 if (lexcode[c] == LexDigit
210 || lexcode[c] == LexOther
211 || lexcode[c] == LexMathSpace) {
215 if (lexcode[c] == LexAlpha) {
219 if (lexcode[c] == LexBOP) {
223 if (lexcode[c] == LexSelf) {
226 if (lexcode[c] == LexArgument) {
230 return LM_TK_ARGUMENT;
232 if (lexcode[c] == LexOpen) {
235 if (lexcode[c] == LexClose) {
238 if (lexcode[c] == LexESC) {
242 return LM_TK_NEWLINE;
245 yylval.i = LM_EN_INTEXT;
249 yylval.i = LM_EN_INTEXT;
253 yylval.i = LM_EN_DISPLAY;
257 yylval.i = LM_EN_DISPLAY;
260 if (strchr(latex_special_chars, c)) {
262 return LM_TK_SPECIAL;
264 if (lexcode[c] == LexMathSpace) {
266 for (i = 0; i < 4 && static_cast<int>(c) != latex_mathspace[i][0]; ++i);
267 yylval.i = (i < 4) ? i : 0;
270 if (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
271 char * p = &yytext[0];
272 while (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
279 if (yyis->good()) yyis->putback(c);
280 latexkeys * l = in_word_set (yytext, strlen(yytext));
282 if (l->token == LM_TK_BEGIN || l->token == LM_TK_END) {
285 // for (i = 0; i < 5 && strncmp(yytext, latex_mathenv[i],
286 // strlen(latex_mathenv[i])); ++i);
288 for (i = 0; i < 6 && strcmp(yytext, latex_mathenv[i]); ++i);
291 if (l->token == LM_TK_SPACE)
308 int parse_align(char * hor, char *)
311 for (char * c = hor; c && *c > ' '; ++c) ++nc;
316 // Accent hacks only for 0.12. Stolen from Cursor.
323 void setAccent(int ac)
325 if (ac > 0 && accent < 8) {
326 nestaccent[accent++] = ac;
328 accent = 0; // consumed!
333 MathedInset * doAccent(byte c, MathedTextCodes t)
335 MathedInset * ac = 0;
337 for (int i = accent - 1; i >= 0; --i) {
339 ac = new MathAccentInset(c, t, nestaccent[i]);
341 ac = new MathAccentInset(ac, nestaccent[i]);
343 accent = 0; // consumed!
350 MathedInset * doAccent(MathedInset * p)
352 MathedInset * ac = 0;
354 for (int i = accent - 1; i >= 0; --i) {
356 ac = new MathAccentInset(p, nestaccent[i]);
358 ac = new MathAccentInset(ac, nestaccent[i]);
360 accent = 0; // consumed!
366 LyxArrayBase * mathed_parse(unsigned flags, LyxArrayBase * array,
372 static int plevel = -1;
373 static int size = LM_ST_TEXT;
374 MathedTextCodes varcode = LM_TC_VAR;
375 MathedInset * binset = 0;
376 static MathMacroTemplate * macro = 0;
381 MathParInset * mt = (mtx) ? *mtx : 0;//(MathParInset*)0;
382 MathedRowSt * crow = (mt) ? mt->getRowSt() : 0;
385 if (!array) array = new LyxArrayBase;
386 MathedIter data(array);
388 if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
389 if ((flags & FLAG_BRACK_ARG) && t == '[') {
392 mathPrintError("Expected {. Maybe you forgot to enclose an argument in {}");
397 MathedInsetTypes fractype = LM_OT_FRAC;
402 data.Insert(doAccent(yylval.i, varcode));
404 data.Insert (yylval.i, varcode); //LM_TC_VAR);
410 data.Insert(macro->getMacroPar(yylval.i-1), LM_TC_INSET);
414 case LM_TK_NEWCOMMAND:
419 string const name(&yytext[1]);
421 // ugly trick to be removed soon (lyx3)
422 //char c; yyis->get(c);
424 char const c = yyis->peek();
427 na = lyx::atoi(yytext);
429 macro = new MathMacroTemplate(name, na);
430 flags = FLAG_BRACE|FLAG_BRACE_LAST;
432 macro->SetData(array);
437 data.Insert (yylval.i, LM_TC_SPECIAL);
443 data.Insert(doAccent(yylval.i, LM_TC_CONST));
445 data.Insert (yylval.i, LM_TC_CONST);
451 if (accent && tprev == LM_TK_ACCENT) {
452 acc_braces[acc_brace++] = brace;
455 if (flags & FLAG_BRACE_OPT) {
456 flags &= ~FLAG_BRACE_OPT;
460 if (flags & FLAG_BRACE)
461 flags &= ~FLAG_BRACE;
463 data.Insert ('{', LM_TC_TEX);
471 mathPrintError("Unmatching braces");
475 if (acc_brace && brace == acc_braces[acc_brace-1]-1) {
479 if (flags & FLAG_BRACE_FONT) {
481 yy_mtextmode = false;
482 flags &= ~FLAG_BRACE_FONT;
485 if (brace == 0 && (flags & FLAG_BRACE_LAST)) {
489 data.Insert ('}', LM_TC_TEX);
496 if (flags & FLAG_BRACK_ARG) {
497 flags &= ~FLAG_BRACK_ARG;
498 char const rg = LexGetArg('[');
500 mathPrintError("Expected ']'");
504 // if (arg) strcpy(arg, yytext);
511 if (flags & FLAG_BRACK_END) {
521 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
522 LyxArrayBase * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
524 // lyxerr << "UP[" << p->GetStyle() << "]" << endl;
525 data.Insert (p, LM_TC_UP);
530 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
531 LyxArrayBase * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
533 data.Insert (p, LM_TC_DOWN);
540 binset->SetLimits(bool(yylval.l->id));
548 if ((flags & FLAG_END) && mt && data.getCol()<mt->GetColumns() - 1) {
549 data.setNumCols(mt->GetColumns());
550 data.Insert('T', LM_TC_TAB);
552 mathPrintError("Unexpected tab");
553 // debug info. [made that conditional -JMarc]
554 if (lyxerr.debugging(Debug::MATHED))
555 lyxerr << data.getCol() << " " << mt->GetColumns() << endl;
560 if (mt && (flags & FLAG_END)) {
561 if (mt->Permit(LMPF_ALLOW_CR)) {
563 crow->setNext(new MathedRowSt(mt->GetColumns() + 1)); // this leaks
564 crow = crow->getNext();
566 data.Insert('K', LM_TC_CR);
568 mathPrintError("Unexpected newline");
574 binset = new MathBigopInset(yylval.l->name, yylval.l->id);
580 if (yylval.l->id < 256) {
581 MathedTextCodes tc = MathIsBOPS(yylval.l->id) ? LM_TC_BOPS: LM_TC_SYMB;
583 data.Insert(doAccent(yylval.l->id, tc));
585 data.Insert(yylval.l->id, tc);
587 MathFuncInset * bg = new MathFuncInset(yylval.l->name);
589 data.Insert(doAccent(bg));
591 data.Insert(bg, true);
598 data.Insert(doAccent(yylval.i, LM_TC_BOP));
600 data.Insert(yylval.i, LM_TC_BOP);
606 mt->UserSetSize(yylval.l->id);
613 MathSpaceInset * sp = new MathSpaceInset(yylval.i);
620 MathDotsInset * p = new MathDotsInset(yylval.l->name, yylval.l->id);
625 fractype = LM_OT_STACKREL;
628 MathFracInset * fc = new MathFracInset(fractype);
629 LyxArrayBase * num = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
630 LyxArrayBase * den = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
631 fc->SetData(num, den);
632 data.Insert(fc, LM_TC_ACTIVE_INSET);
639 char c; yyis->get(c);
642 rt = new MathRootInset(size);
643 rt->setArgumentIdx(0);
644 rt->SetData(mathed_parse(FLAG_BRACK_END, 0, &rt));
645 rt->setArgumentIdx(1);
648 rt = new MathSqrtInset(size);
650 rt->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST, 0, &rt));
651 data.Insert(rt, LM_TC_ACTIVE_INSET);
658 if (lfd == LM_TK_SYM || lfd == LM_TK_STR || lfd == LM_TK_BOP|| lfd == LM_TK_SPECIAL)
659 lfd = (lfd == LM_TK_SYM) ? yylval.l->id: yylval.i;
660 // lyxerr << "L[" << lfd << " " << lfd << "]";
661 LyxArrayBase * a = mathed_parse(FLAG_RIGHT);
663 // lyxerr << "R[" << rgd << "]";
664 if (rgd == LM_TK_SYM || rgd == LM_TK_STR || rgd == LM_TK_BOP || rgd == LM_TK_SPECIAL)
665 rgd = (rgd == LM_TK_SYM) ? yylval.l->id: yylval.i;
666 MathDelimInset * dl = new MathDelimInset(lfd, rgd);
668 data.Insert(dl, LM_TC_ACTIVE_INSET);
669 // lyxerr << "RL[" << lfd << " " << rgd << "]";
674 if (flags & FLAG_RIGHT) {
678 mathPrintError("Unmatched right delimiter");
686 varcode = static_cast<MathedTextCodes>(yylval.l->id);
687 yy_mtextmode = bool(varcode == LM_TC_TEXTRM);
688 flags |= (FLAG_BRACE|FLAG_BRACE_FONT);
693 MathDecorationInset * sq = new MathDecorationInset(yylval.l->id,
695 sq->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
696 data.Insert(sq, LM_TC_ACTIVE_INSET);
701 setAccent(yylval.l->id);
707 crow->setNumbered(false);
714 MathedInset * bg = new MathFuncInset(yylval.l->name);
723 data.Insert(new MathFuncInset(yylval.l->name, LM_OT_FUNCLIM));
730 MathMacroTable::mathMTable.getMacro(yylval.s);
733 data.Insert(doAccent(p), p->getTCode());
735 data.Insert(p, p->getTCode());
736 for (int i = 0; p->setArgumentIdx(i); ++i)
737 p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
740 MathedInset * q = new MathFuncInset(yylval.s, LM_OT_UNDEF);
742 data.Insert(doAccent(q));
751 if (mathed_env != yylval.i && yylval.i!= LM_EN_ARRAY)
752 mathPrintError("Unmatched environment");
753 // debug info [made that conditional -JMarc]
754 if (lyxerr.debugging(Debug::MATHED))
755 lyxerr << "[" << yylval.i << "]" << endl;
757 if (mt) { // && (flags & FLAG_END)) {
765 if (yylval.i == LM_EN_ARRAY) {
766 char ar[120], ar2[8];
767 ar[0] = ar2[0] = '\0';
768 char rg = LexGetArg(0);
774 int const nc = parse_align(ar, ar2);
775 MathParInset * mm = new MathMatrixInset(nc, 0);
776 mm->SetAlign(ar2[0], ar);
777 data.Insert(mm, LM_TC_ACTIVE_INSET);
778 mathed_parse(FLAG_END, mm->GetData(), &mm);
779 } else if (yylval.i >= LM_EN_INTEXT && yylval.i<= LM_EN_EQNARRAY) {
781 mathPrintError("Misplaced environment");
785 mathPrintError("0 paragraph.");
789 mathed_env = yylval.i;
790 if (mathed_env>= LM_EN_DISPLAY) {
791 size = LM_ST_DISPLAY;
792 if (mathed_env>LM_EN_EQUATION) {
793 mt = new MathMatrixInset(3, -1);
794 mt->SetAlign(' ', "rcl");
797 // data.Insert(' ', LM_TC_TAB);
798 // data.Insert(' ', LM_TC_TAB);
802 mt->SetType(mathed_env);
803 crow = mt->getRowSt();
807 lyxerr << "MATH BEGIN[" << mathed_env << "]" << endl;
810 // lyxerr << "MATHCRO[" << yytext << "]";
812 MathMacroTable::mathMTable.getMacro(yytext);
814 data.Insert(p, p->getTCode());
815 p->setArgumentIdx(0);
816 mathed_parse(FLAG_END, p->GetData(), reinterpret_cast<MathParInset**>(&p));
817 // for (int i = 0; p->setArgumentIdx(i); ++i)
818 // p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
820 mathPrintError("Unrecognized environment");
828 MathMacroTable::mathMTable.getMacro(yylval.l->name);
832 data.Insert(doAccent(p));
834 data.Insert(p, static_cast<MathMacro*>(p)->getTCode());
841 char const rg = LexGetArg('\0', true);
843 mathPrintError("Expected '{'");
845 lyxerr << "[" << yytext << "]" << endl;
850 crow->setLabel(yytext);
852 mathed_label = yytext;
855 lyxerr << "Label[" << mathed_label << "]" << endl;
860 mathPrintError("Unrecognized token");
862 lyxerr << "[" << t << " " << yytext << "]" << endl;
867 lyxerr << " Math Panic, expect problems!" << endl;
868 // Search for the end command.
871 } while (t != LM_TK_END && t);
875 if ((flags & FLAG_BRACE_OPT)/* && t!= '^' && t!= '_'*/) {
876 flags &= ~FLAG_BRACE_OPT;
877 //data.Insert (LM_TC_CLOSE);
886 void mathed_parser_file(istream & is, int lineno)
890 if (!MathMacroTable::built)
891 MathMacroTable::mathMTable.builtinMacros();
895 int mathed_parser_lineno()