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;
127 lexcode['('] = lexcode[')'] = lexcode['|'] = lexcode['.'] = lexcode['?'] = LexOther;
128 lexcode['\'']= LexAlpha;
130 lexcode['['] = lexcode[']'] = lexcode['^'] = lexcode['_'] =
131 lexcode['&'] = LexSelf;
133 lexcode['\\'] = LexESC;
134 lexcode['{'] = LexOpen;
135 lexcode['}'] = LexClose;
140 char LexGetArg(char lf, bool accept_spaces= false)
144 while (yyis->good()) {
148 if (!lf) lf = c; else
150 lyxerr << "Math parse error: unexpected '"
156 (lf == '{') ? '}' : ((lf == '[') ? ']' : ((lf == '(') ? ')' : 0));
158 lyxerr << "Math parse error: unknown bracket '"
159 << lf << "'" << endl;
162 char * p = &yytext[0];
169 if ((c > ' ' || (c == ' ' && accept_spaces)) && bcnt > 0) *(p++) = c;
170 } while (bcnt > 0 && yyis->good());
179 static int init_done = 0;
181 if (!init_done) LexInitCodes();
185 while (yyis->good()) {
189 if (yy_mtextmode && c == ' ') {
194 if (lexcode[c] == LexNewLine) {
199 if (lexcode[c] == LexComment) {
203 } while (c != '\n' % yyis->good()); // eat comments
206 if (lexcode[c] == LexDigit
207 || lexcode[c] == LexOther
208 || lexcode[c] == LexMathSpace) {
212 if (lexcode[c] == LexAlpha) {
216 if (lexcode[c] == LexBOP) {
220 if (lexcode[c] == LexSelf) {
223 if (lexcode[c] == LexArgument) {
227 return LM_TK_ARGUMENT;
229 if (lexcode[c] == LexOpen) {
232 if (lexcode[c] == LexClose) {
235 if (lexcode[c] == LexESC) {
239 return LM_TK_NEWLINE;
242 yylval.i = LM_EN_INTEXT;
246 yylval.i = LM_EN_INTEXT;
250 yylval.i = LM_EN_DISPLAY;
254 yylval.i = LM_EN_DISPLAY;
257 if (strchr(latex_special_chars, c)) {
259 return LM_TK_SPECIAL;
261 if (lexcode[c] == LexMathSpace) {
263 for (i = 0; i < 4 && static_cast<int>(c) != latex_mathspace[i][0]; ++i);
264 yylval.i = (i < 4) ? i : 0;
267 if (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
268 char * p = &yytext[0];
269 while (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
276 if (yyis->good()) yyis->putback(c);
277 latexkeys * l = in_word_set (yytext, strlen(yytext));
279 if (l->token == LM_TK_BEGIN || l->token == LM_TK_END) {
282 // for (i = 0; i < 5 && strncmp(yytext, latex_mathenv[i],
283 // strlen(latex_mathenv[i])); ++i);
285 for (i = 0; i < 6 && strcmp(yytext, latex_mathenv[i]); ++i);
288 if (l->token == LM_TK_SPACE)
305 int parse_align(char * hor, char *)
308 for (char * c = hor; c && *c > ' '; ++c) ++nc;
313 // Accent hacks only for 0.12. Stolen from Cursor.
320 void setAccent(int ac)
322 if (ac > 0 && accent < 8) {
323 nestaccent[accent++] = ac;
325 accent = 0; // consumed!
330 MathedInset * doAccent(byte c, MathedTextCodes t)
332 MathedInset * ac = 0;
334 for (int i = accent - 1; i >= 0; --i) {
336 ac = new MathAccentInset(c, t, nestaccent[i]);
338 ac = new MathAccentInset(ac, nestaccent[i]);
340 accent = 0; // consumed!
347 MathedInset * doAccent(MathedInset * p)
349 MathedInset * ac = 0;
351 for (int i = accent - 1; i >= 0; --i) {
353 ac = new MathAccentInset(p, nestaccent[i]);
355 ac = new MathAccentInset(ac, nestaccent[i]);
357 accent = 0; // consumed!
363 LyxArrayBase * mathed_parse(unsigned flags, LyxArrayBase * array,
369 static int plevel = -1;
370 static int size = LM_ST_TEXT;
371 MathedTextCodes varcode = LM_TC_VAR;
372 MathedInset * binset = 0;
373 static MathMacroTemplate * macro = 0;
378 MathParInset * mt = (mtx) ? *mtx : 0;//(MathParInset*)0;
379 MathedRowSt * crow = (mt) ? mt->getRowSt() : 0;
382 if (!array) array = new LyxArrayBase;
383 MathedIter data(array);
385 if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
386 if ((flags & FLAG_BRACK_ARG) && t == '[') {
389 mathPrintError("Expected {. Maybe you forgot to enclose an argument in {}");
394 MathedInsetTypes fractype = LM_OT_FRAC;
399 data.Insert(doAccent(yylval.i, varcode));
401 data.Insert (yylval.i, varcode); //LM_TC_VAR);
407 data.Insert(macro->getMacroPar(yylval.i-1), LM_TC_INSET);
411 case LM_TK_NEWCOMMAND:
416 string const name(&yytext[1]);
418 // ugly trick to be removed soon (lyx3)
419 //char c; yyis->get(c);
421 char const c = yyis->peek();
424 na = lyx::atoi(yytext);
426 macro = new MathMacroTemplate(name, na);
427 flags = FLAG_BRACE|FLAG_BRACE_LAST;
429 macro->SetData(array);
434 data.Insert (yylval.i, LM_TC_SPECIAL);
440 data.Insert(doAccent(yylval.i, LM_TC_CONST));
442 data.Insert (yylval.i, LM_TC_CONST);
448 if (accent && tprev == LM_TK_ACCENT) {
449 acc_braces[acc_brace++] = brace;
452 if (flags & FLAG_BRACE_OPT) {
453 flags &= ~FLAG_BRACE_OPT;
457 if (flags & FLAG_BRACE)
458 flags &= ~FLAG_BRACE;
460 data.Insert ('{', LM_TC_TEX);
468 mathPrintError("Unmatching braces");
472 if (acc_brace && brace == acc_braces[acc_brace-1]-1) {
476 if (flags & FLAG_BRACE_FONT) {
478 yy_mtextmode = false;
479 flags &= ~FLAG_BRACE_FONT;
482 if (brace == 0 && (flags & FLAG_BRACE_LAST)) {
486 data.Insert ('}', LM_TC_TEX);
493 if (flags & FLAG_BRACK_ARG) {
494 flags &= ~FLAG_BRACK_ARG;
495 char const rg = LexGetArg('[');
497 mathPrintError("Expected ']'");
501 // if (arg) strcpy(arg, yytext);
508 if (flags & FLAG_BRACK_END) {
518 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
519 LyxArrayBase * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
521 // lyxerr << "UP[" << p->GetStyle() << "]" << endl;
522 data.Insert (p, LM_TC_UP);
527 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
528 LyxArrayBase * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
530 data.Insert (p, LM_TC_DOWN);
537 binset->SetLimits(bool(yylval.l->id));
545 if ((flags & FLAG_END) && mt && data.getCol()<mt->GetColumns() - 1) {
546 data.setNumCols(mt->GetColumns());
547 data.Insert('T', LM_TC_TAB);
549 mathPrintError("Unexpected tab");
550 // debug info. [made that conditional -JMarc]
551 if (lyxerr.debugging(Debug::MATHED))
552 lyxerr << data.getCol() << " " << mt->GetColumns() << endl;
557 if (mt && (flags & FLAG_END)) {
558 if (mt->Permit(LMPF_ALLOW_CR)) {
560 crow->setNext(new MathedRowSt(mt->GetColumns() + 1)); // this leaks
561 crow = crow->getNext();
563 data.Insert('K', LM_TC_CR);
565 mathPrintError("Unexpected newline");
571 binset = new MathBigopInset(yylval.l->name, yylval.l->id);
577 if (yylval.l->id < 256) {
578 MathedTextCodes tc = MathIsBOPS(yylval.l->id) ? LM_TC_BOPS: LM_TC_SYMB;
580 data.Insert(doAccent(yylval.l->id, tc));
582 data.Insert(yylval.l->id, tc);
584 MathFuncInset * bg = new MathFuncInset(yylval.l->name);
586 data.Insert(doAccent(bg));
588 data.Insert(bg, true);
595 data.Insert(doAccent(yylval.i, LM_TC_BOP));
597 data.Insert(yylval.i, LM_TC_BOP);
603 mt->UserSetSize(yylval.l->id);
610 MathSpaceInset * sp = new MathSpaceInset(yylval.i);
617 MathDotsInset * p = new MathDotsInset(yylval.l->name, yylval.l->id);
622 fractype = LM_OT_STACKREL;
625 MathFracInset * fc = new MathFracInset(fractype);
626 LyxArrayBase * num = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
627 LyxArrayBase * den = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
628 fc->SetData(num, den);
629 data.Insert(fc, LM_TC_ACTIVE_INSET);
636 char c; yyis->get(c);
639 rt = new MathRootInset(size);
640 rt->setArgumentIdx(0);
641 rt->SetData(mathed_parse(FLAG_BRACK_END, 0, &rt));
642 rt->setArgumentIdx(1);
645 rt = new MathSqrtInset(size);
647 rt->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST, 0, &rt));
648 data.Insert(rt, LM_TC_ACTIVE_INSET);
655 if (lfd == LM_TK_SYM || lfd == LM_TK_STR || lfd == LM_TK_BOP|| lfd == LM_TK_SPECIAL)
656 lfd = (lfd == LM_TK_SYM) ? yylval.l->id: yylval.i;
657 // lyxerr << "L[" << lfd << " " << lfd << "]";
658 LyxArrayBase * a = mathed_parse(FLAG_RIGHT);
660 // lyxerr << "R[" << rgd << "]";
661 if (rgd == LM_TK_SYM || rgd == LM_TK_STR || rgd == LM_TK_BOP || rgd == LM_TK_SPECIAL)
662 rgd = (rgd == LM_TK_SYM) ? yylval.l->id: yylval.i;
663 MathDelimInset * dl = new MathDelimInset(lfd, rgd);
665 data.Insert(dl, LM_TC_ACTIVE_INSET);
666 // lyxerr << "RL[" << lfd << " " << rgd << "]";
671 if (flags & FLAG_RIGHT) {
675 mathPrintError("Unmatched right delimiter");
683 varcode = static_cast<MathedTextCodes>(yylval.l->id);
684 yy_mtextmode = bool(varcode == LM_TC_TEXTRM);
685 flags |= (FLAG_BRACE|FLAG_BRACE_FONT);
690 MathDecorationInset * sq = new MathDecorationInset(yylval.l->id,
692 sq->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
693 data.Insert(sq, LM_TC_ACTIVE_INSET);
698 setAccent(yylval.l->id);
704 crow->setNumbered(false);
711 MathedInset * bg = new MathFuncInset(yylval.l->name);
720 data.Insert(new MathFuncInset(yylval.l->name, LM_OT_FUNCLIM));
727 MathMacroTable::mathMTable.getMacro(yylval.s);
730 data.Insert(doAccent(p), p->getTCode());
732 data.Insert(p, p->getTCode());
733 for (int i = 0; p->setArgumentIdx(i); ++i)
734 p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
737 MathedInset * q = new MathFuncInset(yylval.s, LM_OT_UNDEF);
739 data.Insert(doAccent(q));
748 if (mathed_env != yylval.i && yylval.i!= LM_EN_ARRAY)
749 mathPrintError("Unmatched environment");
750 // debug info [made that conditional -JMarc]
751 if (lyxerr.debugging(Debug::MATHED))
752 lyxerr << "[" << yylval.i << "]" << endl;
754 if (mt) { // && (flags & FLAG_END)) {
762 if (yylval.i == LM_EN_ARRAY) {
763 char ar[120], ar2[8];
764 ar[0] = ar2[0] = '\0';
765 char rg = LexGetArg(0);
771 int const nc = parse_align(ar, ar2);
772 MathParInset * mm = new MathMatrixInset(nc, 0);
773 mm->SetAlign(ar2[0], ar);
774 data.Insert(mm, LM_TC_ACTIVE_INSET);
775 mathed_parse(FLAG_END, mm->GetData(), &mm);
776 } else if (yylval.i >= LM_EN_INTEXT && yylval.i<= LM_EN_EQNARRAY) {
778 mathPrintError("Misplaced environment");
782 mathPrintError("0 paragraph.");
786 mathed_env = yylval.i;
787 if (mathed_env>= LM_EN_DISPLAY) {
788 size = LM_ST_DISPLAY;
789 if (mathed_env>LM_EN_EQUATION) {
790 mt = new MathMatrixInset(3, -1);
791 mt->SetAlign(' ', "rcl");
794 // data.Insert(' ', LM_TC_TAB);
795 // data.Insert(' ', LM_TC_TAB);
799 mt->SetType(mathed_env);
800 crow = mt->getRowSt();
804 lyxerr << "MATH BEGIN[" << mathed_env << "]" << endl;
807 // lyxerr << "MATHCRO[" << yytext << "]";
809 MathMacroTable::mathMTable.getMacro(yytext);
811 data.Insert(p, p->getTCode());
812 p->setArgumentIdx(0);
813 mathed_parse(FLAG_END, p->GetData(), reinterpret_cast<MathParInset**>(&p));
814 // for (int i = 0; p->setArgumentIdx(i); ++i)
815 // p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
817 mathPrintError("Unrecognized environment");
825 MathMacroTable::mathMTable.getMacro(yylval.l->name);
829 data.Insert(doAccent(p));
831 data.Insert(p, static_cast<MathMacro*>(p)->getTCode());
838 char const rg = LexGetArg('\0', true);
840 mathPrintError("Expected '{'");
842 lyxerr << "[" << yytext << "]" << endl;
847 crow->setLabel(yytext);
849 mathed_label = yytext;
852 lyxerr << "Label[" << mathed_label << "]" << endl;
857 mathPrintError("Unrecognized token");
859 lyxerr << "[" << t << " " << yytext << "]" << endl;
864 lyxerr << " Math Panic, expect problems!" << endl;
865 // Search for the end command.
868 } while (t != LM_TK_END && t);
872 if ((flags & FLAG_BRACE_OPT)/* && t!= '^' && t!= '_'*/) {
873 flags &= ~FLAG_BRACE_OPT;
874 //data.Insert (LM_TC_CLOSE);
883 void mathed_parser_file(istream & is, int lineno)
887 if (!MathMacroTable::built)
888 MathMacroTable::mathMTable.builtinMacros();
892 int mathed_parser_lineno()