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"
57 extern MathMatrixInset * create_multiline(short int type, int cols);
61 FLAG_BRACE = 1, // A { needed
62 FLAG_BRACE_ARG = 2, // Next { is argument
63 FLAG_BRACE_OPT = 4, // Optional {
64 FLAG_BRACE_LAST = 8, // Last } ends the parsing process
65 FLAG_BRACK_ARG = 16, // Optional [
66 FLAG_RIGHT = 32, // Next right ends the parsing process
67 FLAG_END = 64, // Next end ends the parsing process
68 FLAG_BRACE_FONT = 128, // Next } closes a font
69 FLAG_BRACK_END = 256 // Next ] ends the parsing process
77 MathedInsetTypes mathed_env = LM_OT_MIN;
82 int const latex_mathenv_num = 12;
83 char const * latex_mathenv[latex_mathenv_num] = {
101 char const * latex_special_chars = "#$%&_{}";
103 // These are lexical codes, not semantic
109 LexBOP, // Binary operators or relations
121 static lexcode_enum lexcode[256];
122 #warning Replace with string
123 static char yytext[256];
125 static istream * yyis;
126 static bool yy_mtextmode= false;
129 void mathPrintError(string const & msg)
131 lyxerr << "Line ~" << yylineno << ": Math parse error: "
139 for (int i = 0; i <= 255; ++i) {
140 if (isalpha(i)) lexcode[i] = LexAlpha;
141 else if (isdigit(i)) lexcode[i] = LexDigit;
142 else if (isspace(i)) lexcode[i] = LexSpace;
143 else lexcode[i] = LexNone;
146 lexcode['\t'] = lexcode['\f'] = lexcode[' '] = LexSpace;
147 lexcode['\n'] = LexNewLine;
148 lexcode['%'] = LexComment;
149 lexcode['#'] = LexArgument;
150 lexcode['+'] = lexcode['-'] = lexcode['*'] = lexcode['/'] =
151 lexcode['<'] = lexcode['>'] = lexcode['='] = LexBOP;
153 lexcode['!'] = lexcode[','] = lexcode[':'] = lexcode[';'] = LexMathSpace;
155 lexcode['('] = lexcode[')'] = lexcode['|'] = lexcode['.'] =
156 lexcode['?'] = LexOther;
158 lexcode['\''] = lexcode['@'] = LexAlpha;
160 lexcode['['] = lexcode[']'] = lexcode['^'] = lexcode['_'] =
161 lexcode['&'] = LexSelf;
163 lexcode['\\'] = LexESC;
164 lexcode['{'] = LexOpen;
165 lexcode['}'] = LexClose;
170 char LexGetArg(char lf, bool accept_spaces= false)
174 while (yyis->good()) {
181 lyxerr << "Math parse error: unexpected '"
189 (lf == '{') ? '}' : ((lf == '[') ? ']' : ((lf == '(') ? ')' : 0));
191 lyxerr << "Math parse error: unknown bracket '"
192 << lf << "'" << endl;
195 char * p = &yytext[0];
202 if ((c > ' ' || (c == ' ' && accept_spaces)) && bcnt > 0) *(p++) = c;
203 } while (bcnt > 0 && yyis->good() && p-yytext < 255);
212 static int init_done = 0;
214 if (!init_done) LexInitCodes();
218 while (yyis->good()) {
222 if (yy_mtextmode && c == ' ') {
227 if (lexcode[c] == LexNewLine) {
232 if (lexcode[c] == LexComment) {
236 } while (c != '\n' % yyis->good()); // eat comments
239 if (lexcode[c] == LexDigit
240 || lexcode[c] == LexOther
241 || lexcode[c] == LexMathSpace) {
245 if (lexcode[c] == LexAlpha) {
249 if (lexcode[c] == LexBOP) {
253 if (lexcode[c] == LexSelf) {
256 if (lexcode[c] == LexArgument) {
260 return LM_TK_ARGUMENT;
262 if (lexcode[c] == LexOpen) {
265 if (lexcode[c] == LexClose) {
268 if (lexcode[c] == LexESC) {
272 return LM_TK_NEWLINE;
275 yylval.i = LM_OT_MIN;
279 yylval.i = LM_OT_MIN;
283 yylval.i = LM_OT_PAR;
287 yylval.i = LM_OT_PAR;
290 if (strchr(latex_special_chars, c)) {
292 return LM_TK_SPECIAL;
294 if (lexcode[c] == LexMathSpace) {
296 for (i = 0; i < 4 && static_cast<int>(c) != latex_mathspace[i][0]; ++i);
297 yylval.i = (i < 4) ? i : 0;
300 if (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
301 char * p = &yytext[0];
302 while ((lexcode[c] == LexAlpha || lexcode[c] == LexDigit)
310 if (yyis->good()) yyis->putback(c);
311 latexkeys * l = in_word_set (yytext, strlen(yytext));
313 if (l->token == LM_TK_BEGIN || l->token == LM_TK_END) {
316 // for (i = 0; i < 5 && strncmp(yytext, latex_mathenv[i],
317 // strlen(latex_mathenv[i])); ++i);
319 for (i = 0; i < latex_mathenv_num
320 && strcmp(yytext, latex_mathenv[i]); ++i);
323 if (l->token == LM_TK_SPACE)
340 int parse_align(char * hor, char *)
343 for (char * c = hor; c && *c > ' '; ++c) ++nc;
348 // Accent hacks only for 0.12. Stolen from Cursor.
355 void setAccent(int ac)
357 if (ac > 0 && accent < 8) {
358 nestaccent[accent++] = ac;
360 accent = 0; // consumed!
365 MathedInset * doAccent(byte c, MathedTextCodes t)
367 MathedInset * ac = 0;
369 for (int i = accent - 1; i >= 0; --i) {
371 ac = new MathAccentInset(c, t, nestaccent[i]);
373 ac = new MathAccentInset(ac, nestaccent[i]);
375 accent = 0; // consumed!
382 MathedInset * doAccent(MathedInset * p)
384 MathedInset * ac = 0;
386 for (int i = accent - 1; i >= 0; --i) {
388 ac = new MathAccentInset(p, nestaccent[i]);
390 ac = new MathAccentInset(ac, nestaccent[i]);
392 accent = 0; // consumed!
398 MathedArray * mathed_parse(unsigned flags, MathedArray * array,
404 static int plevel = -1;
405 static int size = LM_ST_TEXT;
406 MathedTextCodes varcode = LM_TC_VAR;
407 MathedInset * binset = 0;
408 static MathMacroTemplate * macro = 0;
413 MathParInset * mt = (mtx) ? *mtx : 0;//(MathParInset*)0;
414 MathedRowSt * crow = (mt) ? mt->getRowSt() : 0;
417 if (!array) array = new MathedArray;
418 MathedIter data(array);
420 if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
421 if ((flags & FLAG_BRACK_ARG) && t == '[') {
424 mathPrintError("Expected {. Maybe you forgot to enclose an argument in {}");
429 MathedInsetTypes fractype = LM_OT_FRAC;
434 data.Insert(doAccent(yylval.i, varcode));
436 data.Insert (yylval.i, varcode); //LM_TC_VAR);
442 data.Insert(macro->getMacroPar(yylval.i-1), LM_TC_INSET);
446 case LM_TK_NEWCOMMAND:
451 string const name(&yytext[1]);
453 // ugly trick to be removed soon (lyx3)
454 char const c = yyis->peek();
457 na = lyx::atoi(yytext);
459 macro = new MathMacroTemplate(name, na);
460 flags = FLAG_BRACE|FLAG_BRACE_LAST;
462 macro->SetData(array);
467 data.Insert (yylval.i, LM_TC_SPECIAL);
473 data.Insert(doAccent(yylval.i, LM_TC_CONST));
475 data.Insert (yylval.i, LM_TC_CONST);
481 if (accent && tprev == LM_TK_ACCENT) {
482 acc_braces[acc_brace++] = brace;
485 if (flags & FLAG_BRACE_OPT) {
486 flags &= ~FLAG_BRACE_OPT;
490 if (flags & FLAG_BRACE)
491 flags &= ~FLAG_BRACE;
493 data.Insert ('{', LM_TC_TEX);
501 mathPrintError("Unmatching braces");
505 if (acc_brace && brace == acc_braces[acc_brace-1]-1) {
509 if (flags & FLAG_BRACE_FONT) {
511 yy_mtextmode = false;
512 flags &= ~FLAG_BRACE_FONT;
515 if (brace == 0 && (flags & FLAG_BRACE_LAST)) {
519 data.Insert ('}', LM_TC_TEX);
526 if (flags & FLAG_BRACK_ARG) {
527 flags &= ~FLAG_BRACK_ARG;
528 char const rg = LexGetArg('[');
530 mathPrintError("Expected ']'");
534 // if (arg) strcpy(arg, yytext);
541 if (flags & FLAG_BRACK_END) {
551 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
552 MathedArray * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
554 // lyxerr << "UP[" << p->GetStyle() << "]" << endl;
555 data.Insert (p, LM_TC_UP);
560 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
561 MathedArray * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
563 data.Insert (p, LM_TC_DOWN);
570 binset->SetLimits(bool(yylval.l->id));
578 if ((flags & FLAG_END) && mt && data.getCol()<mt->GetColumns() - 1) {
579 data.setNumCols(mt->GetColumns());
580 data.Insert('T', LM_TC_TAB);
582 mathPrintError("Unexpected tab");
583 // debug info. [made that conditional -JMarc]
584 if (lyxerr.debugging(Debug::MATHED))
585 lyxerr << data.getCol() << " " << mt->GetColumns() << endl;
590 if (mt && (flags & FLAG_END)) {
591 if (mt->Permit(LMPF_ALLOW_CR)) {
593 crow->setNext(new MathedRowSt(mt->GetColumns() + 1)); // this leaks
594 crow = crow->getNext();
596 data.Insert('K', LM_TC_CR);
598 mathPrintError("Unexpected newline");
604 binset = new MathBigopInset(yylval.l->name, yylval.l->id);
610 if (yylval.l->id < 256) {
611 MathedTextCodes tc = MathIsBOPS(yylval.l->id) ? LM_TC_BOPS: LM_TC_SYMB;
613 data.Insert(doAccent(yylval.l->id, tc));
615 data.Insert(yylval.l->id, tc);
617 MathFuncInset * bg = new MathFuncInset(yylval.l->name);
619 data.Insert(doAccent(bg));
621 data.Insert(bg, true);
628 data.Insert(doAccent(yylval.i, LM_TC_BOP));
630 data.Insert(yylval.i, LM_TC_BOP);
636 mt->UserSetSize(yylval.l->id);
643 MathSpaceInset * sp = new MathSpaceInset(yylval.i);
650 MathDotsInset * p = new MathDotsInset(yylval.l->name, yylval.l->id);
655 fractype = LM_OT_STACKREL;
658 MathFracInset * fc = new MathFracInset(fractype);
659 MathedArray * num = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
660 MathedArray * den = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
661 fc->SetData(num, den);
662 data.Insert(fc, LM_TC_ACTIVE_INSET);
669 char c; yyis->get(c);
672 rt = new MathRootInset(size);
673 rt->setArgumentIdx(0);
674 rt->SetData(mathed_parse(FLAG_BRACK_END, 0, &rt));
675 rt->setArgumentIdx(1);
678 rt = new MathSqrtInset(size);
680 rt->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST, 0, &rt));
681 data.Insert(rt, LM_TC_ACTIVE_INSET);
688 if (lfd == LM_TK_SYM || lfd == LM_TK_STR || lfd == LM_TK_BOP|| lfd == LM_TK_SPECIAL)
689 lfd = (lfd == LM_TK_SYM) ? yylval.l->id: yylval.i;
690 // lyxerr << "L[" << lfd << " " << lfd << "]";
691 MathedArray * a = mathed_parse(FLAG_RIGHT);
693 // lyxerr << "R[" << rgd << "]";
694 if (rgd == LM_TK_SYM || rgd == LM_TK_STR || rgd == LM_TK_BOP || rgd == LM_TK_SPECIAL)
695 rgd = (rgd == LM_TK_SYM) ? yylval.l->id: yylval.i;
696 MathDelimInset * dl = new MathDelimInset(lfd, rgd);
698 data.Insert(dl, LM_TC_ACTIVE_INSET);
699 // lyxerr << "RL[" << lfd << " " << rgd << "]";
704 if (flags & FLAG_RIGHT) {
708 mathPrintError("Unmatched right delimiter");
716 varcode = static_cast<MathedTextCodes>(yylval.l->id);
717 yy_mtextmode = bool(varcode == LM_TC_TEXTRM);
718 flags |= (FLAG_BRACE|FLAG_BRACE_FONT);
723 MathDecorationInset * sq = new MathDecorationInset(yylval.l->id,
725 sq->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
726 data.Insert(sq, LM_TC_ACTIVE_INSET);
731 setAccent(yylval.l->id);
737 crow->setNumbered(false);
744 MathedInset * bg = new MathFuncInset(yylval.l->name);
753 data.Insert(new MathFuncInset(yylval.l->name, LM_OT_FUNCLIM));
760 MathMacroTable::mathMTable.getMacro(yylval.s);
763 data.Insert(doAccent(p), p->getTCode());
765 data.Insert(p, p->getTCode());
766 for (int i = 0; p->setArgumentIdx(i); ++i)
767 p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
770 MathedInset * q = new MathFuncInset(yylval.s, LM_OT_UNDEF);
772 data.Insert(doAccent(q));
781 if (mathed_env != yylval.i && yylval.i != LM_OT_MATRIX)
782 mathPrintError("Unmatched environment");
783 // debug info [made that conditional -JMarc]
784 if (lyxerr.debugging(Debug::MATHED))
785 lyxerr << "[" << yylval.i << "]" << endl;
787 if (mt) { // && (flags & FLAG_END)) {
795 if (yylval.i == LM_OT_MATRIX) {
796 char ar[120], ar2[8];
797 ar[0] = ar2[0] = '\0';
798 char rg = LexGetArg(0);
804 int const nc = parse_align(ar, ar2);
805 MathParInset * mm = new MathMatrixInset(nc, 0);
806 mm->SetAlign(ar2[0], ar);
807 data.Insert(mm, LM_TC_ACTIVE_INSET);
808 mathed_parse(FLAG_END, mm->GetData(), &mm);
809 } else if (is_eqn_type(yylval.i)) {
811 mathPrintError("Misplaced environment");
815 mathPrintError("0 paragraph.");
819 mathed_env = static_cast<MathedInsetTypes>(yylval.i);
820 if (mathed_env != LM_OT_MIN) {
821 size = LM_ST_DISPLAY;
822 if (is_multiline(mathed_env)) {
824 if (is_multicolumn(mathed_env)) {
825 if (mathed_env != LM_OT_ALIGNAT &&
826 mathed_env != LM_OT_ALIGNATN &&
831 lyxerr << "Math parse error: unexpected '"
835 cols = strToInt(string(yytext));
837 mt = create_multiline(mathed_env, cols);
840 // data.Insert(' ', LM_TC_TAB);
841 // data.Insert(' ', LM_TC_TAB);
845 mt->SetType(mathed_env);
846 crow = mt->getRowSt();
850 lyxerr << "MATH BEGIN[" << mathed_env << "]" << endl;
853 // lyxerr << "MATHCRO[" << yytext << "]";
855 MathMacroTable::mathMTable.getMacro(yytext);
857 data.Insert(p, p->getTCode());
858 p->setArgumentIdx(0);
859 mathed_parse(FLAG_END, p->GetData(), reinterpret_cast<MathParInset**>(&p));
860 // for (int i = 0; p->setArgumentIdx(i); ++i)
861 // p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
863 mathPrintError("Unrecognized environment");
871 MathMacroTable::mathMTable.getMacro(yylval.l->name);
875 data.Insert(doAccent(p));
877 data.Insert(p, static_cast<MathMacro*>(p)->getTCode());
884 char const rg = LexGetArg('\0', true);
886 mathPrintError("Expected '{'");
888 lyxerr << "[" << yytext << "]" << endl;
893 crow->setLabel(yytext);
895 mathed_label = yytext;
898 lyxerr << "Label[" << mathed_label << "]" << endl;
903 mathPrintError("Unrecognized token");
905 lyxerr << "[" << t << " " << yytext << "]" << endl;
910 lyxerr << " Math Panic, expect problems!" << endl;
911 // Search for the end command.
914 } while (t != LM_TK_END && t);
918 if ((flags & FLAG_BRACE_OPT)/* && t!= '^' && t!= '_'*/) {
919 flags &= ~FLAG_BRACE_OPT;
920 //data.Insert (LM_TC_CLOSE);
929 void mathed_parser_file(istream & is, int lineno)
933 if (!MathMacroTable::built)
934 MathMacroTable::mathMTable.builtinMacros();
938 int mathed_parser_lineno()