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"
43 extern MathMatrixInset * create_multiline(short int type, int cols);
47 FLAG_BRACE = 1, // A { needed
48 FLAG_BRACE_ARG = 2, // Next { is argument
49 FLAG_BRACE_OPT = 4, // Optional {
50 FLAG_BRACE_LAST = 8, // Last } ends the parsing process
51 FLAG_BRACK_ARG = 16, // Optional [
52 FLAG_RIGHT = 32, // Next right ends the parsing process
53 FLAG_END = 64, // Next end ends the parsing process
54 FLAG_BRACE_FONT = 128, // Next } closes a font
55 FLAG_BRACK_END = 256 // Next ] ends the parsing process
63 MathedInsetTypes mathed_env = LM_OT_MIN;
68 int const latex_mathenv_num = 12;
69 char const * latex_mathenv[latex_mathenv_num] = {
85 char const * latex_mathspace[] = {
86 "!", ",", ":", ";", "quad", "qquad"
90 char const * latex_special_chars = "#$%&_{}";
92 // These are lexical codes, not semantic
98 LexBOP, // Binary operators or relations
110 static lexcode_enum lexcode[256];
111 #warning Replace with string
112 static char yytext[256];
114 static istream * yyis;
115 static bool yy_mtextmode= false;
118 void mathPrintError(string const & msg)
120 lyxerr << "Line ~" << yylineno << ": Math parse error: "
128 for (int i = 0; i <= 255; ++i) {
129 if (isalpha(i)) lexcode[i] = LexAlpha;
130 else if (isdigit(i)) lexcode[i] = LexDigit;
131 else if (isspace(i)) lexcode[i] = LexSpace;
132 else lexcode[i] = LexNone;
135 lexcode['\t'] = lexcode['\f'] = lexcode[' '] = LexSpace;
136 lexcode['\n'] = LexNewLine;
137 lexcode['%'] = LexComment;
138 lexcode['#'] = LexArgument;
139 lexcode['+'] = lexcode['-'] = lexcode['*'] = lexcode['/'] =
140 lexcode['<'] = lexcode['>'] = lexcode['='] = LexBOP;
142 lexcode['!'] = lexcode[','] = lexcode[':'] = lexcode[';'] = LexMathSpace;
144 lexcode['('] = lexcode[')'] = lexcode['|'] = lexcode['.'] =
145 lexcode['?'] = LexOther;
147 lexcode['\''] = lexcode['@'] = LexAlpha;
149 lexcode['['] = lexcode[']'] = lexcode['^'] = lexcode['_'] =
150 lexcode['&'] = LexSelf;
152 lexcode['\\'] = LexESC;
153 lexcode['{'] = LexOpen;
154 lexcode['}'] = LexClose;
159 char LexGetArg(char lf, bool accept_spaces= false)
163 while (yyis->good()) {
170 lyxerr << "Math parse error: unexpected '"
178 (lf == '{') ? '}' : ((lf == '[') ? ']' : ((lf == '(') ? ')' : 0));
180 lyxerr << "Math parse error: unknown bracket '"
181 << lf << "'" << endl;
184 char * p = &yytext[0];
191 if ((c > ' ' || (c == ' ' && accept_spaces)) && bcnt > 0) *(p++) = c;
192 } while (bcnt > 0 && yyis->good() && p-yytext < 255);
201 static int init_done = 0;
203 if (!init_done) LexInitCodes();
207 while (yyis->good()) {
211 if (yy_mtextmode && c == ' ') {
216 if (lexcode[c] == LexNewLine) {
221 if (lexcode[c] == LexComment) {
225 } while (c != '\n' % yyis->good()); // eat comments
228 if (lexcode[c] == LexDigit
229 || lexcode[c] == LexOther
230 || lexcode[c] == LexMathSpace) {
234 if (lexcode[c] == LexAlpha) {
238 if (lexcode[c] == LexBOP) {
242 if (lexcode[c] == LexSelf) {
245 if (lexcode[c] == LexArgument) {
249 return LM_TK_ARGUMENT;
251 if (lexcode[c] == LexOpen) {
254 if (lexcode[c] == LexClose) {
257 if (lexcode[c] == LexESC) {
261 return LM_TK_NEWLINE;
264 yylval.i = LM_OT_MIN;
268 yylval.i = LM_OT_MIN;
272 yylval.i = LM_OT_PAR;
276 yylval.i = LM_OT_PAR;
279 if (strchr(latex_special_chars, c)) {
281 return LM_TK_SPECIAL;
283 if (lexcode[c] == LexMathSpace) {
285 for (i = 0; i < 4 && static_cast<int>(c) != latex_mathspace[i][0]; ++i);
286 yylval.i = (i < 4) ? i : 0;
289 if (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
290 char * p = &yytext[0];
291 while ((lexcode[c] == LexAlpha || lexcode[c] == LexDigit)
299 if (yyis->good()) yyis->putback(c);
300 latexkeys * l = in_word_set (yytext, strlen(yytext));
302 if (l->token == LM_TK_BEGIN || l->token == LM_TK_END) {
305 // for (i = 0; i < 5 && strncmp(yytext, latex_mathenv[i],
306 // strlen(latex_mathenv[i])); ++i);
308 for (i = 0; i < latex_mathenv_num
309 && strcmp(yytext, latex_mathenv[i]); ++i);
312 if (l->token == LM_TK_SPACE)
329 int parse_align(char * hor, char *)
332 for (char * c = hor; c && *c > ' '; ++c) ++nc;
337 // Accent hacks only for 0.12. Stolen from Cursor.
344 void setAccent(int ac)
346 if (ac > 0 && accent < 8) {
347 nestaccent[accent++] = ac;
349 accent = 0; // consumed!
354 MathedInset * doAccent(byte c, MathedTextCodes t)
356 MathedInset * ac = 0;
358 for (int i = accent - 1; i >= 0; --i) {
360 ac = new MathAccentInset(c, t, nestaccent[i]);
362 ac = new MathAccentInset(ac, nestaccent[i]);
364 accent = 0; // consumed!
371 MathedInset * doAccent(MathedInset * p)
373 MathedInset * ac = 0;
375 for (int i = accent - 1; i >= 0; --i) {
377 ac = new MathAccentInset(p, nestaccent[i]);
379 ac = new MathAccentInset(ac, nestaccent[i]);
381 accent = 0; // consumed!
387 MathedArray * mathed_parse(unsigned flags, MathedArray * array,
393 static int plevel = -1;
394 static int size = LM_ST_TEXT;
395 MathedTextCodes varcode = LM_TC_VAR;
396 MathedInset * binset = 0;
397 static MathMacroTemplate * macro = 0;
402 MathParInset * mt = (mtx) ? *mtx : 0;//(MathParInset*)0;
403 MathedRowSt * crow = (mt) ? mt->getRowSt() : 0;
406 if (!array) array = new MathedArray;
407 MathedIter data(array);
409 if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
410 if ((flags & FLAG_BRACK_ARG) && t == '[') {
413 mathPrintError("Expected {. Maybe you forgot to enclose an argument in {}");
418 MathedInsetTypes fractype = LM_OT_FRAC;
423 data.Insert(doAccent(yylval.i, varcode));
425 data.Insert (yylval.i, varcode); //LM_TC_VAR);
431 data.Insert(macro->getMacroPar(yylval.i-1), LM_TC_INSET);
435 case LM_TK_NEWCOMMAND:
440 string const name(&yytext[1]);
442 // ugly trick to be removed soon (lyx3)
443 char const c = yyis->peek();
446 na = lyx::atoi(yytext);
448 macro = new MathMacroTemplate(name, na);
449 flags = FLAG_BRACE|FLAG_BRACE_LAST;
451 macro->SetData(array);
456 data.Insert (yylval.i, LM_TC_SPECIAL);
462 data.Insert(doAccent(yylval.i, LM_TC_CONST));
464 data.Insert (yylval.i, LM_TC_CONST);
470 if (accent && tprev == LM_TK_ACCENT) {
471 acc_braces[acc_brace++] = brace;
474 if (flags & FLAG_BRACE_OPT) {
475 flags &= ~FLAG_BRACE_OPT;
479 if (flags & FLAG_BRACE)
480 flags &= ~FLAG_BRACE;
482 data.Insert ('{', LM_TC_TEX);
490 mathPrintError("Unmatching braces");
494 if (acc_brace && brace == acc_braces[acc_brace-1]-1) {
498 if (flags & FLAG_BRACE_FONT) {
500 yy_mtextmode = false;
501 flags &= ~FLAG_BRACE_FONT;
504 if (brace == 0 && (flags & FLAG_BRACE_LAST)) {
508 data.Insert ('}', LM_TC_TEX);
515 if (flags & FLAG_BRACK_ARG) {
516 flags &= ~FLAG_BRACK_ARG;
517 char const rg = LexGetArg('[');
519 mathPrintError("Expected ']'");
523 // if (arg) strcpy(arg, yytext);
530 if (flags & FLAG_BRACK_END) {
540 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
541 MathedArray * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
543 // lyxerr << "UP[" << p->GetStyle() << "]" << endl;
544 data.Insert (p, LM_TC_UP);
549 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
550 MathedArray * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
552 data.Insert (p, LM_TC_DOWN);
559 binset->SetLimits(bool(yylval.l->id));
567 if ((flags & FLAG_END) && mt && data.getCol()<mt->GetColumns() - 1) {
568 data.setNumCols(mt->GetColumns());
569 data.Insert('T', LM_TC_TAB);
571 mathPrintError("Unexpected tab");
572 // debug info. [made that conditional -JMarc]
573 if (lyxerr.debugging(Debug::MATHED))
574 lyxerr << data.getCol() << " " << mt->GetColumns() << endl;
579 if (mt && (flags & FLAG_END)) {
580 if (mt->Permit(LMPF_ALLOW_CR)) {
582 crow->setNext(new MathedRowSt(mt->GetColumns() + 1)); // this leaks
583 crow = crow->getNext();
585 data.Insert('K', LM_TC_CR);
587 mathPrintError("Unexpected newline");
593 binset = new MathBigopInset(yylval.l->name, yylval.l->id);
599 if (yylval.l->id < 256) {
600 MathedTextCodes tc = MathIsBOPS(yylval.l->id) ? LM_TC_BOPS: LM_TC_SYMB;
602 data.Insert(doAccent(yylval.l->id, tc));
604 data.Insert(yylval.l->id, tc);
606 MathFuncInset * bg = new MathFuncInset(yylval.l->name);
608 data.Insert(doAccent(bg));
610 data.Insert(bg, true);
617 data.Insert(doAccent(yylval.i, LM_TC_BOP));
619 data.Insert(yylval.i, LM_TC_BOP);
625 mt->UserSetSize(yylval.l->id);
632 MathSpaceInset * sp = new MathSpaceInset(yylval.i);
639 MathDotsInset * p = new MathDotsInset(yylval.l->name, yylval.l->id);
644 fractype = LM_OT_STACKREL;
647 MathFracInset * fc = new MathFracInset(fractype);
648 MathedArray * num = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
649 MathedArray * den = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
650 fc->SetData(num, den);
651 data.Insert(fc, LM_TC_ACTIVE_INSET);
658 char c; yyis->get(c);
661 rt = new MathRootInset(size);
662 rt->setArgumentIdx(0);
663 rt->SetData(mathed_parse(FLAG_BRACK_END, 0, &rt));
664 rt->setArgumentIdx(1);
667 rt = new MathSqrtInset(size);
669 rt->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST, 0, &rt));
670 data.Insert(rt, LM_TC_ACTIVE_INSET);
677 if (lfd == LM_TK_SYM || lfd == LM_TK_STR || lfd == LM_TK_BOP|| lfd == LM_TK_SPECIAL)
678 lfd = (lfd == LM_TK_SYM) ? yylval.l->id: yylval.i;
679 // lyxerr << "L[" << lfd << " " << lfd << "]";
680 MathedArray * a = mathed_parse(FLAG_RIGHT);
682 // lyxerr << "R[" << rgd << "]";
683 if (rgd == LM_TK_SYM || rgd == LM_TK_STR || rgd == LM_TK_BOP || rgd == LM_TK_SPECIAL)
684 rgd = (rgd == LM_TK_SYM) ? yylval.l->id: yylval.i;
685 MathDelimInset * dl = new MathDelimInset(lfd, rgd);
687 data.Insert(dl, LM_TC_ACTIVE_INSET);
688 // lyxerr << "RL[" << lfd << " " << rgd << "]";
693 if (flags & FLAG_RIGHT) {
697 mathPrintError("Unmatched right delimiter");
705 varcode = static_cast<MathedTextCodes>(yylval.l->id);
706 yy_mtextmode = bool(varcode == LM_TC_TEXTRM);
707 flags |= (FLAG_BRACE|FLAG_BRACE_FONT);
712 MathDecorationInset * sq = new MathDecorationInset(yylval.l->id,
714 sq->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
715 data.Insert(sq, LM_TC_ACTIVE_INSET);
720 setAccent(yylval.l->id);
726 crow->setNumbered(false);
733 MathedInset * bg = new MathFuncInset(yylval.l->name);
742 data.Insert(new MathFuncInset(yylval.l->name, LM_OT_FUNCLIM));
749 MathMacroTable::mathMTable.getMacro(yylval.s);
752 data.Insert(doAccent(p), p->getTCode());
754 data.Insert(p, p->getTCode());
755 for (int i = 0; p->setArgumentIdx(i); ++i)
756 p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
759 MathedInset * q = new MathFuncInset(yylval.s, LM_OT_UNDEF);
761 data.Insert(doAccent(q));
770 if (mathed_env != yylval.i && yylval.i != LM_OT_MATRIX)
771 mathPrintError("Unmatched environment");
772 // debug info [made that conditional -JMarc]
773 if (lyxerr.debugging(Debug::MATHED))
774 lyxerr << "[" << yylval.i << "]" << endl;
776 if (mt) { // && (flags & FLAG_END)) {
784 if (yylval.i == LM_OT_MATRIX) {
785 char ar[120], ar2[8];
786 ar[0] = ar2[0] = '\0';
787 char rg = LexGetArg(0);
793 int const nc = parse_align(ar, ar2);
794 MathParInset * mm = new MathMatrixInset(nc, 0);
795 mm->SetAlign(ar2[0], ar);
796 data.Insert(mm, LM_TC_ACTIVE_INSET);
797 mathed_parse(FLAG_END, mm->GetData(), &mm);
798 } else if (is_eqn_type(yylval.i)) {
800 mathPrintError("Misplaced environment");
804 mathPrintError("0 paragraph.");
808 mathed_env = static_cast<MathedInsetTypes>(yylval.i);
809 if (mathed_env != LM_OT_MIN) {
810 size = LM_ST_DISPLAY;
811 if (is_multiline(mathed_env)) {
813 if (is_multicolumn(mathed_env)) {
814 if (mathed_env != LM_OT_ALIGNAT &&
815 mathed_env != LM_OT_ALIGNATN &&
820 lyxerr << "Math parse error: unexpected '"
824 cols = strToInt(string(yytext));
826 mt = create_multiline(mathed_env, cols);
829 // data.Insert(' ', LM_TC_TAB);
830 // data.Insert(' ', LM_TC_TAB);
834 mt->SetType(mathed_env);
835 crow = mt->getRowSt();
839 lyxerr << "MATH BEGIN[" << mathed_env << "]" << endl;
842 // lyxerr << "MATHCRO[" << yytext << "]";
844 MathMacroTable::mathMTable.getMacro(yytext);
846 data.Insert(p, p->getTCode());
847 p->setArgumentIdx(0);
848 mathed_parse(FLAG_END, p->GetData(), reinterpret_cast<MathParInset**>(&p));
849 // for (int i = 0; p->setArgumentIdx(i); ++i)
850 // p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
852 mathPrintError("Unrecognized environment");
860 MathMacroTable::mathMTable.getMacro(yylval.l->name);
864 data.Insert(doAccent(p));
866 data.Insert(p, static_cast<MathMacro*>(p)->getTCode());
873 char const rg = LexGetArg('\0', true);
875 mathPrintError("Expected '{'");
877 lyxerr << "[" << yytext << "]" << endl;
882 crow->setLabel(yytext);
884 mathed_label = yytext;
887 lyxerr << "Label[" << mathed_label << "]" << endl;
892 mathPrintError("Unrecognized token");
894 lyxerr << "[" << t << " " << yytext << "]" << endl;
899 lyxerr << " Math Panic, expect problems!" << endl;
900 // Search for the end command.
903 } while (t != LM_TK_END && t);
907 if ((flags & FLAG_BRACE_OPT)/* && t!= '^' && t!= '_'*/) {
908 flags &= ~FLAG_BRACE_OPT;
909 //data.Insert (LM_TC_CLOSE);
918 void mathed_parser_file(istream & is, int lineno)
922 if (!MathMacroTable::built)
923 MathMacroTable::mathMTable.builtinMacros();
927 int mathed_parser_lineno()