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_root.h"
33 #include "math_matrixinset.h"
34 #include "math_accentinset.h"
35 #include "math_bigopinset.h"
36 #include "math_funcinset.h"
37 #include "math_spaceinset.h"
38 #include "math_dotsinset.h"
39 #include "math_fracinset.h"
40 #include "math_deliminset.h"
41 #include "math_decorationinset.h"
43 #include "support/lyxlib.h"
44 #include "mathed/support.h"
55 extern MathMatrixInset * create_multiline(short int type, int cols);
59 FLAG_BRACE = 1, // A { needed
60 FLAG_BRACE_ARG = 2, // Next { is argument
61 FLAG_BRACE_OPT = 4, // Optional {
62 FLAG_BRACE_LAST = 8, // Last } ends the parsing process
63 FLAG_BRACK_ARG = 16, // Optional [
64 FLAG_RIGHT = 32, // Next right ends the parsing process
65 FLAG_END = 64, // Next end ends the parsing process
66 FLAG_BRACE_FONT = 128, // Next } closes a font
67 FLAG_BRACK_END = 256 // Next ] ends the parsing process
75 MathedInsetTypes mathed_env = LM_OT_MIN;
80 int const latex_mathenv_num = 12;
81 char const * latex_mathenv[latex_mathenv_num] = {
99 char const * latex_special_chars = "#$%&_{}";
101 // These are lexical codes, not semantic
107 LexBOP, // Binary operators or relations
119 static lexcode_enum lexcode[256];
120 #warning Replace with string
121 static char yytext[256];
123 static istream * yyis;
124 static bool yy_mtextmode= false;
127 void mathPrintError(string const & msg)
129 lyxerr << "Line ~" << yylineno << ": Math parse error: "
137 for (int i = 0; i <= 255; ++i) {
138 if (isalpha(i)) lexcode[i] = LexAlpha;
139 else if (isdigit(i)) lexcode[i] = LexDigit;
140 else if (isspace(i)) lexcode[i] = LexSpace;
141 else lexcode[i] = LexNone;
144 lexcode['\t'] = lexcode['\f'] = lexcode[' '] = LexSpace;
145 lexcode['\n'] = LexNewLine;
146 lexcode['%'] = LexComment;
147 lexcode['#'] = LexArgument;
148 lexcode['+'] = lexcode['-'] = lexcode['*'] = lexcode['/'] =
149 lexcode['<'] = lexcode['>'] = lexcode['='] = LexBOP;
151 lexcode['!'] = lexcode[','] = lexcode[':'] = lexcode[';'] = LexMathSpace;
153 lexcode['('] = lexcode[')'] = lexcode['|'] = lexcode['.'] =
154 lexcode['?'] = LexOther;
156 lexcode['\''] = lexcode['@'] = LexAlpha;
158 lexcode['['] = lexcode[']'] = lexcode['^'] = lexcode['_'] =
159 lexcode['&'] = LexSelf;
161 lexcode['\\'] = LexESC;
162 lexcode['{'] = LexOpen;
163 lexcode['}'] = LexClose;
168 char LexGetArg(char lf, bool accept_spaces= false)
172 while (yyis->good()) {
179 lyxerr << "Math parse error: unexpected '"
187 (lf == '{') ? '}' : ((lf == '[') ? ']' : ((lf == '(') ? ')' : 0));
189 lyxerr << "Math parse error: unknown bracket '"
190 << lf << "'" << endl;
193 char * p = &yytext[0];
200 if ((c > ' ' || (c == ' ' && accept_spaces)) && bcnt > 0) *(p++) = c;
201 } while (bcnt > 0 && yyis->good() && p-yytext < 255);
210 static int init_done = 0;
212 if (!init_done) LexInitCodes();
216 while (yyis->good()) {
220 if (yy_mtextmode && c == ' ') {
225 if (lexcode[c] == LexNewLine) {
230 if (lexcode[c] == LexComment) {
234 } while (c != '\n' % yyis->good()); // eat comments
237 if (lexcode[c] == LexDigit
238 || lexcode[c] == LexOther
239 || lexcode[c] == LexMathSpace) {
243 if (lexcode[c] == LexAlpha) {
247 if (lexcode[c] == LexBOP) {
251 if (lexcode[c] == LexSelf) {
254 if (lexcode[c] == LexArgument) {
258 return LM_TK_ARGUMENT;
260 if (lexcode[c] == LexOpen) {
263 if (lexcode[c] == LexClose) {
266 if (lexcode[c] == LexESC) {
270 return LM_TK_NEWLINE;
273 yylval.i = LM_OT_MIN;
277 yylval.i = LM_OT_MIN;
281 yylval.i = LM_OT_PAR;
285 yylval.i = LM_OT_PAR;
288 if (strchr(latex_special_chars, c)) {
290 return LM_TK_SPECIAL;
292 if (lexcode[c] == LexMathSpace) {
294 for (i = 0; i < 4 && static_cast<int>(c) != latex_mathspace[i][0]; ++i);
295 yylval.i = (i < 4) ? i : 0;
298 if (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
299 char * p = &yytext[0];
300 while ((lexcode[c] == LexAlpha || lexcode[c] == LexDigit)
308 if (yyis->good()) yyis->putback(c);
309 latexkeys * l = in_word_set (yytext, strlen(yytext));
311 if (l->token == LM_TK_BEGIN || l->token == LM_TK_END) {
314 // for (i = 0; i < 5 && strncmp(yytext, latex_mathenv[i],
315 // strlen(latex_mathenv[i])); ++i);
317 for (i = 0; i < latex_mathenv_num
318 && strcmp(yytext, latex_mathenv[i]); ++i);
321 if (l->token == LM_TK_SPACE)
338 int parse_align(char * hor, char *)
341 for (char * c = hor; c && *c > ' '; ++c) ++nc;
346 // Accent hacks only for 0.12. Stolen from Cursor.
353 void setAccent(int ac)
355 if (ac > 0 && accent < 8) {
356 nestaccent[accent++] = ac;
358 accent = 0; // consumed!
363 MathedInset * doAccent(byte c, MathedTextCodes t)
365 MathedInset * ac = 0;
367 for (int i = accent - 1; i >= 0; --i) {
369 ac = new MathAccentInset(c, t, nestaccent[i]);
371 ac = new MathAccentInset(ac, nestaccent[i]);
373 accent = 0; // consumed!
380 MathedInset * doAccent(MathedInset * p)
382 MathedInset * ac = 0;
384 for (int i = accent - 1; i >= 0; --i) {
386 ac = new MathAccentInset(p, nestaccent[i]);
388 ac = new MathAccentInset(ac, nestaccent[i]);
390 accent = 0; // consumed!
396 MathedArray * mathed_parse(unsigned flags, MathedArray * array,
402 static int plevel = -1;
403 static int size = LM_ST_TEXT;
404 MathedTextCodes varcode = LM_TC_VAR;
405 MathedInset * binset = 0;
406 static MathMacroTemplate * macro = 0;
411 MathParInset * mt = (mtx) ? *mtx : 0;//(MathParInset*)0;
412 MathedRowSt * crow = (mt) ? mt->getRowSt() : 0;
415 if (!array) array = new MathedArray;
416 MathedIter data(array);
418 if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
419 if ((flags & FLAG_BRACK_ARG) && t == '[') {
422 mathPrintError("Expected {. Maybe you forgot to enclose an argument in {}");
427 MathedInsetTypes fractype = LM_OT_FRAC;
432 data.Insert(doAccent(yylval.i, varcode));
434 data.Insert (yylval.i, varcode); //LM_TC_VAR);
440 data.Insert(macro->getMacroPar(yylval.i-1), LM_TC_INSET);
444 case LM_TK_NEWCOMMAND:
449 string const name(&yytext[1]);
451 // ugly trick to be removed soon (lyx3)
452 char const c = yyis->peek();
455 na = lyx::atoi(yytext);
457 macro = new MathMacroTemplate(name, na);
458 flags = FLAG_BRACE|FLAG_BRACE_LAST;
460 macro->SetData(array);
465 data.Insert (yylval.i, LM_TC_SPECIAL);
471 data.Insert(doAccent(yylval.i, LM_TC_CONST));
473 data.Insert (yylval.i, LM_TC_CONST);
479 if (accent && tprev == LM_TK_ACCENT) {
480 acc_braces[acc_brace++] = brace;
483 if (flags & FLAG_BRACE_OPT) {
484 flags &= ~FLAG_BRACE_OPT;
488 if (flags & FLAG_BRACE)
489 flags &= ~FLAG_BRACE;
491 data.Insert ('{', LM_TC_TEX);
499 mathPrintError("Unmatching braces");
503 if (acc_brace && brace == acc_braces[acc_brace-1]-1) {
507 if (flags & FLAG_BRACE_FONT) {
509 yy_mtextmode = false;
510 flags &= ~FLAG_BRACE_FONT;
513 if (brace == 0 && (flags & FLAG_BRACE_LAST)) {
517 data.Insert ('}', LM_TC_TEX);
524 if (flags & FLAG_BRACK_ARG) {
525 flags &= ~FLAG_BRACK_ARG;
526 char const rg = LexGetArg('[');
528 mathPrintError("Expected ']'");
532 // if (arg) strcpy(arg, yytext);
539 if (flags & FLAG_BRACK_END) {
549 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
550 MathedArray * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
552 // lyxerr << "UP[" << p->GetStyle() << "]" << endl;
553 data.Insert (p, LM_TC_UP);
558 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
559 MathedArray * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
561 data.Insert (p, LM_TC_DOWN);
568 binset->SetLimits(bool(yylval.l->id));
576 if ((flags & FLAG_END) && mt && data.getCol()<mt->GetColumns() - 1) {
577 data.setNumCols(mt->GetColumns());
578 data.Insert('T', LM_TC_TAB);
580 mathPrintError("Unexpected tab");
581 // debug info. [made that conditional -JMarc]
582 if (lyxerr.debugging(Debug::MATHED))
583 lyxerr << data.getCol() << " " << mt->GetColumns() << endl;
588 if (mt && (flags & FLAG_END)) {
589 if (mt->Permit(LMPF_ALLOW_CR)) {
591 crow->setNext(new MathedRowSt(mt->GetColumns() + 1)); // this leaks
592 crow = crow->getNext();
594 data.Insert('K', LM_TC_CR);
596 mathPrintError("Unexpected newline");
602 binset = new MathBigopInset(yylval.l->name, yylval.l->id);
608 if (yylval.l->id < 256) {
609 MathedTextCodes tc = MathIsBOPS(yylval.l->id) ? LM_TC_BOPS: LM_TC_SYMB;
611 data.Insert(doAccent(yylval.l->id, tc));
613 data.Insert(yylval.l->id, tc);
615 MathFuncInset * bg = new MathFuncInset(yylval.l->name);
617 data.Insert(doAccent(bg));
619 data.Insert(bg, true);
626 data.Insert(doAccent(yylval.i, LM_TC_BOP));
628 data.Insert(yylval.i, LM_TC_BOP);
634 mt->UserSetSize(yylval.l->id);
641 MathSpaceInset * sp = new MathSpaceInset(yylval.i);
648 MathDotsInset * p = new MathDotsInset(yylval.l->name, yylval.l->id);
653 fractype = LM_OT_STACKREL;
656 MathFracInset * fc = new MathFracInset(fractype);
657 MathedArray * num = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
658 MathedArray * den = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
659 fc->SetData(num, den);
660 data.Insert(fc, LM_TC_ACTIVE_INSET);
667 char c; yyis->get(c);
670 rt = new MathRootInset(size);
671 rt->setArgumentIdx(0);
672 rt->SetData(mathed_parse(FLAG_BRACK_END, 0, &rt));
673 rt->setArgumentIdx(1);
676 rt = new MathSqrtInset(size);
678 rt->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST, 0, &rt));
679 data.Insert(rt, LM_TC_ACTIVE_INSET);
686 if (lfd == LM_TK_SYM || lfd == LM_TK_STR || lfd == LM_TK_BOP|| lfd == LM_TK_SPECIAL)
687 lfd = (lfd == LM_TK_SYM) ? yylval.l->id: yylval.i;
688 // lyxerr << "L[" << lfd << " " << lfd << "]";
689 MathedArray * a = mathed_parse(FLAG_RIGHT);
691 // lyxerr << "R[" << rgd << "]";
692 if (rgd == LM_TK_SYM || rgd == LM_TK_STR || rgd == LM_TK_BOP || rgd == LM_TK_SPECIAL)
693 rgd = (rgd == LM_TK_SYM) ? yylval.l->id: yylval.i;
694 MathDelimInset * dl = new MathDelimInset(lfd, rgd);
696 data.Insert(dl, LM_TC_ACTIVE_INSET);
697 // lyxerr << "RL[" << lfd << " " << rgd << "]";
702 if (flags & FLAG_RIGHT) {
706 mathPrintError("Unmatched right delimiter");
714 varcode = static_cast<MathedTextCodes>(yylval.l->id);
715 yy_mtextmode = bool(varcode == LM_TC_TEXTRM);
716 flags |= (FLAG_BRACE|FLAG_BRACE_FONT);
721 MathDecorationInset * sq = new MathDecorationInset(yylval.l->id,
723 sq->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
724 data.Insert(sq, LM_TC_ACTIVE_INSET);
729 setAccent(yylval.l->id);
735 crow->setNumbered(false);
742 MathedInset * bg = new MathFuncInset(yylval.l->name);
751 data.Insert(new MathFuncInset(yylval.l->name, LM_OT_FUNCLIM));
758 MathMacroTable::mathMTable.getMacro(yylval.s);
761 data.Insert(doAccent(p), p->getTCode());
763 data.Insert(p, p->getTCode());
764 for (int i = 0; p->setArgumentIdx(i); ++i)
765 p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
768 MathedInset * q = new MathFuncInset(yylval.s, LM_OT_UNDEF);
770 data.Insert(doAccent(q));
779 if (mathed_env != yylval.i && yylval.i != LM_OT_MATRIX)
780 mathPrintError("Unmatched environment");
781 // debug info [made that conditional -JMarc]
782 if (lyxerr.debugging(Debug::MATHED))
783 lyxerr << "[" << yylval.i << "]" << endl;
785 if (mt) { // && (flags & FLAG_END)) {
793 if (yylval.i == LM_OT_MATRIX) {
794 char ar[120], ar2[8];
795 ar[0] = ar2[0] = '\0';
796 char rg = LexGetArg(0);
802 int const nc = parse_align(ar, ar2);
803 MathParInset * mm = new MathMatrixInset(nc, 0);
804 mm->SetAlign(ar2[0], ar);
805 data.Insert(mm, LM_TC_ACTIVE_INSET);
806 mathed_parse(FLAG_END, mm->GetData(), &mm);
807 } else if (is_eqn_type(yylval.i)) {
809 mathPrintError("Misplaced environment");
813 mathPrintError("0 paragraph.");
817 mathed_env = static_cast<MathedInsetTypes>(yylval.i);
818 if (mathed_env != LM_OT_MIN) {
819 size = LM_ST_DISPLAY;
820 if (is_multiline(mathed_env)) {
822 if (is_multicolumn(mathed_env)) {
823 if (mathed_env != LM_OT_ALIGNAT &&
824 mathed_env != LM_OT_ALIGNATN &&
829 lyxerr << "Math parse error: unexpected '"
833 cols = strToInt(string(yytext));
835 mt = create_multiline(mathed_env, cols);
838 // data.Insert(' ', LM_TC_TAB);
839 // data.Insert(' ', LM_TC_TAB);
843 mt->SetType(mathed_env);
844 crow = mt->getRowSt();
848 lyxerr << "MATH BEGIN[" << mathed_env << "]" << endl;
851 // lyxerr << "MATHCRO[" << yytext << "]";
853 MathMacroTable::mathMTable.getMacro(yytext);
855 data.Insert(p, p->getTCode());
856 p->setArgumentIdx(0);
857 mathed_parse(FLAG_END, p->GetData(), reinterpret_cast<MathParInset**>(&p));
858 // for (int i = 0; p->setArgumentIdx(i); ++i)
859 // p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
861 mathPrintError("Unrecognized environment");
869 MathMacroTable::mathMTable.getMacro(yylval.l->name);
873 data.Insert(doAccent(p));
875 data.Insert(p, static_cast<MathMacro*>(p)->getTCode());
882 char const rg = LexGetArg('\0', true);
884 mathPrintError("Expected '{'");
886 lyxerr << "[" << yytext << "]" << endl;
891 crow->setLabel(yytext);
893 mathed_label = yytext;
896 lyxerr << "Label[" << mathed_label << "]" << endl;
901 mathPrintError("Unrecognized token");
903 lyxerr << "[" << t << " " << yytext << "]" << endl;
908 lyxerr << " Math Panic, expect problems!" << endl;
909 // Search for the end command.
912 } while (t != LM_TK_END && t);
916 if ((flags & FLAG_BRACE_OPT)/* && t!= '^' && t!= '_'*/) {
917 flags &= ~FLAG_BRACE_OPT;
918 //data.Insert (LM_TC_CLOSE);
927 void mathed_parser_file(istream & is, int lineno)
931 if (!MathMacroTable::built)
932 MathMacroTable::mathMTable.builtinMacros();
936 int mathed_parser_lineno()