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"
52 extern MathMatrixInset * create_multiline(short int type, int cols);
56 FLAG_BRACE = 1, // A { needed
57 FLAG_BRACE_ARG = 2, // Next { is argument
58 FLAG_BRACE_OPT = 4, // Optional {
59 FLAG_BRACE_LAST = 8, // Last } ends the parsing process
60 FLAG_BRACK_ARG = 16, // Optional [
61 FLAG_RIGHT = 32, // Next right ends the parsing process
62 FLAG_END = 64, // Next end ends the parsing process
63 FLAG_BRACE_FONT = 128, // Next } closes a font
64 FLAG_BRACK_END = 256 // Next ] ends the parsing process
72 MathedInsetTypes mathed_env = LM_OT_MIN;
77 int const latex_mathenv_num = 12;
78 char const * latex_mathenv[latex_mathenv_num] = {
96 char const * latex_special_chars = "#$%&_{}";
98 // These are lexical codes, not semantic
104 LexBOP, // Binary operators or relations
116 static lexcode_enum lexcode[256];
117 #warning Replace with string
118 static char yytext[256];
120 static istream * yyis;
121 static bool yy_mtextmode= false;
124 void mathPrintError(string const & msg)
126 lyxerr << "Line ~" << yylineno << ": Math parse error: "
134 for (int i = 0; i <= 255; ++i) {
135 if (isalpha(i)) lexcode[i] = LexAlpha;
136 else if (isdigit(i)) lexcode[i] = LexDigit;
137 else if (isspace(i)) lexcode[i] = LexSpace;
138 else lexcode[i] = LexNone;
141 lexcode['\t'] = lexcode['\f'] = lexcode[' '] = LexSpace;
142 lexcode['\n'] = LexNewLine;
143 lexcode['%'] = LexComment;
144 lexcode['#'] = LexArgument;
145 lexcode['+'] = lexcode['-'] = lexcode['*'] = lexcode['/'] =
146 lexcode['<'] = lexcode['>'] = lexcode['='] = LexBOP;
148 lexcode['!'] = lexcode[','] = lexcode[':'] = lexcode[';'] = LexMathSpace;
150 lexcode['('] = lexcode[')'] = lexcode['|'] = lexcode['.'] =
151 lexcode['?'] = LexOther;
153 lexcode['\''] = lexcode['@'] = LexAlpha;
155 lexcode['['] = lexcode[']'] = lexcode['^'] = lexcode['_'] =
156 lexcode['&'] = LexSelf;
158 lexcode['\\'] = LexESC;
159 lexcode['{'] = LexOpen;
160 lexcode['}'] = LexClose;
165 char LexGetArg(char lf, bool accept_spaces= false)
169 while (yyis->good()) {
176 lyxerr << "Math parse error: unexpected '"
184 (lf == '{') ? '}' : ((lf == '[') ? ']' : ((lf == '(') ? ')' : 0));
186 lyxerr << "Math parse error: unknown bracket '"
187 << lf << "'" << endl;
190 char * p = &yytext[0];
197 if ((c > ' ' || (c == ' ' && accept_spaces)) && bcnt > 0) *(p++) = c;
198 } while (bcnt > 0 && yyis->good() && p-yytext < 255);
207 static int init_done = 0;
209 if (!init_done) LexInitCodes();
213 while (yyis->good()) {
217 if (yy_mtextmode && c == ' ') {
222 if (lexcode[c] == LexNewLine) {
227 if (lexcode[c] == LexComment) {
231 } while (c != '\n' % yyis->good()); // eat comments
234 if (lexcode[c] == LexDigit
235 || lexcode[c] == LexOther
236 || lexcode[c] == LexMathSpace) {
240 if (lexcode[c] == LexAlpha) {
244 if (lexcode[c] == LexBOP) {
248 if (lexcode[c] == LexSelf) {
251 if (lexcode[c] == LexArgument) {
255 return LM_TK_ARGUMENT;
257 if (lexcode[c] == LexOpen) {
260 if (lexcode[c] == LexClose) {
263 if (lexcode[c] == LexESC) {
267 return LM_TK_NEWLINE;
270 yylval.i = LM_OT_MIN;
274 yylval.i = LM_OT_MIN;
278 yylval.i = LM_OT_PAR;
282 yylval.i = LM_OT_PAR;
285 if (strchr(latex_special_chars, c)) {
287 return LM_TK_SPECIAL;
289 if (lexcode[c] == LexMathSpace) {
291 for (i = 0; i < 4 && static_cast<int>(c) != latex_mathspace[i][0]; ++i);
292 yylval.i = (i < 4) ? i : 0;
295 if (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
296 char * p = &yytext[0];
297 while ((lexcode[c] == LexAlpha || lexcode[c] == LexDigit)
305 if (yyis->good()) yyis->putback(c);
306 latexkeys * l = in_word_set (yytext, strlen(yytext));
308 if (l->token == LM_TK_BEGIN || l->token == LM_TK_END) {
311 // for (i = 0; i < 5 && strncmp(yytext, latex_mathenv[i],
312 // strlen(latex_mathenv[i])); ++i);
314 for (i = 0; i < latex_mathenv_num
315 && strcmp(yytext, latex_mathenv[i]); ++i);
318 if (l->token == LM_TK_SPACE)
335 int parse_align(char * hor, char *)
338 for (char * c = hor; c && *c > ' '; ++c) ++nc;
343 // Accent hacks only for 0.12. Stolen from Cursor.
350 void setAccent(int ac)
352 if (ac > 0 && accent < 8) {
353 nestaccent[accent++] = ac;
355 accent = 0; // consumed!
360 MathedInset * doAccent(byte c, MathedTextCodes t)
362 MathedInset * ac = 0;
364 for (int i = accent - 1; i >= 0; --i) {
366 ac = new MathAccentInset(c, t, nestaccent[i]);
368 ac = new MathAccentInset(ac, nestaccent[i]);
370 accent = 0; // consumed!
377 MathedInset * doAccent(MathedInset * p)
379 MathedInset * ac = 0;
381 for (int i = accent - 1; i >= 0; --i) {
383 ac = new MathAccentInset(p, nestaccent[i]);
385 ac = new MathAccentInset(ac, nestaccent[i]);
387 accent = 0; // consumed!
393 MathedArray * mathed_parse(unsigned flags = 0, MathedArray * array = 0,
394 MathParInset ** mtx = 0)
399 static int plevel = -1;
400 static int size = LM_ST_TEXT;
401 MathedTextCodes varcode = LM_TC_VAR;
402 MathedInset * binset = 0;
403 static MathMacroTemplate * macro = 0;
408 MathParInset * mt = (mtx) ? *mtx : 0;//(MathParInset*)0;
409 MathedRowSt * crow = (mt) ? mt->getRowSt() : 0;
412 if (!array) array = new MathedArray;
413 MathedIter data(array);
415 if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
416 if ((flags & FLAG_BRACK_ARG) && t == '[') {
419 mathPrintError("Expected {. Maybe you forgot to enclose an argument in {}");
424 MathedInsetTypes fractype = LM_OT_FRAC;
429 data.Insert(doAccent(yylval.i, varcode));
431 data.Insert (yylval.i, varcode); //LM_TC_VAR);
437 data.Insert(macro->getMacroPar(yylval.i-1), LM_TC_INSET);
441 case LM_TK_NEWCOMMAND:
446 string const name(&yytext[1]);
448 // ugly trick to be removed soon (lyx3)
449 char const c = yyis->peek();
452 na = lyx::atoi(yytext);
454 macro = new MathMacroTemplate(name, na);
455 flags = FLAG_BRACE|FLAG_BRACE_LAST;
457 macro->SetData(array);
462 data.Insert (yylval.i, LM_TC_SPECIAL);
468 data.Insert(doAccent(yylval.i, LM_TC_CONST));
470 data.Insert (yylval.i, LM_TC_CONST);
476 if (accent && tprev == LM_TK_ACCENT) {
477 acc_braces[acc_brace++] = brace;
480 if (flags & FLAG_BRACE_OPT) {
481 flags &= ~FLAG_BRACE_OPT;
485 if (flags & FLAG_BRACE)
486 flags &= ~FLAG_BRACE;
488 data.Insert ('{', LM_TC_TEX);
496 mathPrintError("Unmatching braces");
500 if (acc_brace && brace == acc_braces[acc_brace-1]-1) {
504 if (flags & FLAG_BRACE_FONT) {
506 yy_mtextmode = false;
507 flags &= ~FLAG_BRACE_FONT;
510 if (brace == 0 && (flags & FLAG_BRACE_LAST)) {
514 data.Insert ('}', LM_TC_TEX);
521 if (flags & FLAG_BRACK_ARG) {
522 flags &= ~FLAG_BRACK_ARG;
523 char const rg = LexGetArg('[');
525 mathPrintError("Expected ']'");
529 // if (arg) strcpy(arg, yytext);
536 if (flags & FLAG_BRACK_END) {
546 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
547 MathedArray * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
549 // lyxerr << "UP[" << p->GetStyle() << "]" << endl;
550 data.Insert (p, LM_TC_UP);
555 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
556 MathedArray * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
558 data.Insert (p, LM_TC_DOWN);
565 binset->SetLimits(bool(yylval.l->id));
573 if ((flags & FLAG_END) && mt && data.getCol()<mt->GetColumns() - 1) {
574 data.setNumCols(mt->GetColumns());
575 data.Insert('T', LM_TC_TAB);
577 mathPrintError("Unexpected tab");
578 // debug info. [made that conditional -JMarc]
579 if (lyxerr.debugging(Debug::MATHED))
580 lyxerr << data.getCol() << " " << mt->GetColumns() << endl;
585 if (mt && (flags & FLAG_END)) {
586 if (mt->Permit(LMPF_ALLOW_CR)) {
588 crow->setNext(new MathedRowSt(mt->GetColumns() + 1)); // this leaks
589 crow = crow->getNext();
591 data.Insert('K', LM_TC_CR);
593 mathPrintError("Unexpected newline");
599 binset = new MathBigopInset(yylval.l->name, yylval.l->id);
605 if (yylval.l->id < 256) {
606 MathedTextCodes tc = MathIsBOPS(yylval.l->id) ? LM_TC_BOPS: LM_TC_SYMB;
608 data.Insert(doAccent(yylval.l->id, tc));
610 data.Insert(yylval.l->id, tc);
612 MathFuncInset * bg = new MathFuncInset(yylval.l->name);
614 data.Insert(doAccent(bg));
616 data.Insert(bg, true);
623 data.Insert(doAccent(yylval.i, LM_TC_BOP));
625 data.Insert(yylval.i, LM_TC_BOP);
631 mt->UserSetSize(yylval.l->id);
638 MathSpaceInset * sp = new MathSpaceInset(yylval.i);
645 MathDotsInset * p = new MathDotsInset(yylval.l->name, yylval.l->id);
650 fractype = LM_OT_STACKREL;
653 MathFracInset * fc = new MathFracInset(fractype);
654 MathedArray * num = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
655 MathedArray * den = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
656 fc->SetData(num, den);
657 data.Insert(fc, LM_TC_ACTIVE_INSET);
664 char c; yyis->get(c);
667 rt = new MathRootInset(size);
668 rt->setArgumentIdx(0);
669 rt->SetData(mathed_parse(FLAG_BRACK_END, 0, &rt));
670 rt->setArgumentIdx(1);
673 rt = new MathSqrtInset(size);
675 rt->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST, 0, &rt));
676 data.Insert(rt, LM_TC_ACTIVE_INSET);
683 if (lfd == LM_TK_SYM || lfd == LM_TK_STR || lfd == LM_TK_BOP|| lfd == LM_TK_SPECIAL)
684 lfd = (lfd == LM_TK_SYM) ? yylval.l->id: yylval.i;
685 // lyxerr << "L[" << lfd << " " << lfd << "]";
686 MathedArray * a = mathed_parse(FLAG_RIGHT);
688 // lyxerr << "R[" << rgd << "]";
689 if (rgd == LM_TK_SYM || rgd == LM_TK_STR || rgd == LM_TK_BOP || rgd == LM_TK_SPECIAL)
690 rgd = (rgd == LM_TK_SYM) ? yylval.l->id: yylval.i;
691 MathDelimInset * dl = new MathDelimInset(lfd, rgd);
693 data.Insert(dl, LM_TC_ACTIVE_INSET);
694 // lyxerr << "RL[" << lfd << " " << rgd << "]";
699 if (flags & FLAG_RIGHT) {
703 mathPrintError("Unmatched right delimiter");
711 varcode = static_cast<MathedTextCodes>(yylval.l->id);
712 yy_mtextmode = bool(varcode == LM_TC_TEXTRM);
713 flags |= (FLAG_BRACE|FLAG_BRACE_FONT);
718 MathDecorationInset * sq = new MathDecorationInset(yylval.l->id,
720 sq->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
721 data.Insert(sq, LM_TC_ACTIVE_INSET);
726 setAccent(yylval.l->id);
732 crow->setNumbered(false);
739 MathedInset * bg = new MathFuncInset(yylval.l->name);
748 data.Insert(new MathFuncInset(yylval.l->name, LM_OT_FUNCLIM));
755 MathMacroTable::mathMTable.getMacro(yylval.s);
758 data.Insert(doAccent(p), p->getTCode());
760 data.Insert(p, p->getTCode());
761 for (int i = 0; p->setArgumentIdx(i); ++i)
762 p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
765 MathedInset * q = new MathFuncInset(yylval.s, LM_OT_UNDEF);
767 data.Insert(doAccent(q));
776 if (mathed_env != yylval.i && yylval.i != LM_OT_MATRIX)
777 mathPrintError("Unmatched environment");
778 // debug info [made that conditional -JMarc]
779 if (lyxerr.debugging(Debug::MATHED))
780 lyxerr << "[" << yylval.i << "]" << endl;
782 if (mt) { // && (flags & FLAG_END)) {
790 if (yylval.i == LM_OT_MATRIX) {
791 char ar[120], ar2[8];
792 ar[0] = ar2[0] = '\0';
793 char rg = LexGetArg(0);
799 int const nc = parse_align(ar, ar2);
800 MathParInset * mm = new MathMatrixInset(nc, 0);
801 mm->SetAlign(ar2[0], ar);
802 data.Insert(mm, LM_TC_ACTIVE_INSET);
803 mathed_parse(FLAG_END, mm->GetData(), &mm);
804 } else if (is_eqn_type(yylval.i)) {
806 mathPrintError("Misplaced environment");
810 mathPrintError("0 paragraph.");
814 mathed_env = static_cast<MathedInsetTypes>(yylval.i);
815 if (mathed_env != LM_OT_MIN) {
816 size = LM_ST_DISPLAY;
817 if (is_multiline(mathed_env)) {
819 if (is_multicolumn(mathed_env)) {
820 if (mathed_env != LM_OT_ALIGNAT &&
821 mathed_env != LM_OT_ALIGNATN &&
826 lyxerr << "Math parse error: unexpected '"
830 cols = strToInt(string(yytext));
832 mt = create_multiline(mathed_env, cols);
835 // data.Insert(' ', LM_TC_TAB);
836 // data.Insert(' ', LM_TC_TAB);
840 mt->SetType(mathed_env);
841 crow = mt->getRowSt();
845 lyxerr << "MATH BEGIN[" << mathed_env << "]" << endl;
848 // lyxerr << "MATHCRO[" << yytext << "]";
850 MathMacroTable::mathMTable.getMacro(yytext);
852 data.Insert(p, p->getTCode());
853 p->setArgumentIdx(0);
854 mathed_parse(FLAG_END, p->GetData(), reinterpret_cast<MathParInset**>(&p));
855 // for (int i = 0; p->setArgumentIdx(i); ++i)
856 // p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
858 mathPrintError("Unrecognized environment");
866 MathMacroTable::mathMTable.getMacro(yylval.l->name);
870 data.Insert(doAccent(p));
872 data.Insert(p, static_cast<MathMacro*>(p)->getTCode());
879 char const rg = LexGetArg('\0', true);
881 mathPrintError("Expected '{'");
883 lyxerr << "[" << yytext << "]" << endl;
888 crow->setLabel(yytext);
890 mathed_label = yytext;
893 lyxerr << "Label[" << mathed_label << "]" << endl;
898 mathPrintError("Unrecognized token");
900 lyxerr << "[" << t << " " << yytext << "]" << endl;
905 lyxerr << " Math Panic, expect problems!" << endl;
906 // Search for the end command.
909 } while (t != LM_TK_END && t);
913 if ((flags & FLAG_BRACE_OPT)/* && t!= '^' && t!= '_'*/) {
914 flags &= ~FLAG_BRACE_OPT;
915 //data.Insert (LM_TC_CLOSE);
924 void mathed_parser_file(istream & is, int lineno)
928 if (!MathMacroTable::built)
929 MathMacroTable::mathMTable.builtinMacros();
933 int mathed_parser_lineno()