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;
128 lexcode['('] = lexcode[')'] = lexcode['|'] = lexcode['.'] =
129 lexcode['?'] = LexOther;
131 lexcode['\''] = lexcode['@'] = LexAlpha;
133 lexcode['['] = lexcode[']'] = lexcode['^'] = lexcode['_'] =
134 lexcode['&'] = LexSelf;
136 lexcode['\\'] = LexESC;
137 lexcode['{'] = LexOpen;
138 lexcode['}'] = LexClose;
143 char LexGetArg(char lf, bool accept_spaces= false)
147 while (yyis->good()) {
151 if (!lf) lf = c; else
153 lyxerr << "Math parse error: unexpected '"
159 (lf == '{') ? '}' : ((lf == '[') ? ']' : ((lf == '(') ? ')' : 0));
161 lyxerr << "Math parse error: unknown bracket '"
162 << lf << "'" << endl;
165 char * p = &yytext[0];
172 if ((c > ' ' || (c == ' ' && accept_spaces)) && bcnt > 0) *(p++) = c;
173 } while (bcnt > 0 && yyis->good());
182 static int init_done = 0;
184 if (!init_done) LexInitCodes();
188 while (yyis->good()) {
192 if (yy_mtextmode && c == ' ') {
197 if (lexcode[c] == LexNewLine) {
202 if (lexcode[c] == LexComment) {
206 } while (c != '\n' % yyis->good()); // eat comments
209 if (lexcode[c] == LexDigit
210 || lexcode[c] == LexOther
211 || lexcode[c] == LexMathSpace) {
215 if (lexcode[c] == LexAlpha) {
219 if (lexcode[c] == LexBOP) {
223 if (lexcode[c] == LexSelf) {
226 if (lexcode[c] == LexArgument) {
230 return LM_TK_ARGUMENT;
232 if (lexcode[c] == LexOpen) {
235 if (lexcode[c] == LexClose) {
238 if (lexcode[c] == LexESC) {
242 return LM_TK_NEWLINE;
245 yylval.i = LM_EN_INTEXT;
249 yylval.i = LM_EN_INTEXT;
253 yylval.i = LM_EN_DISPLAY;
257 yylval.i = LM_EN_DISPLAY;
260 if (strchr(latex_special_chars, c)) {
262 return LM_TK_SPECIAL;
264 if (lexcode[c] == LexMathSpace) {
266 for (i = 0; i < 4 && static_cast<int>(c) != latex_mathspace[i][0]; ++i);
267 yylval.i = (i < 4) ? i : 0;
270 if (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
271 char * p = &yytext[0];
272 while (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
279 if (yyis->good()) yyis->putback(c);
280 latexkeys * l = in_word_set (yytext, strlen(yytext));
282 if (l->token == LM_TK_BEGIN || l->token == LM_TK_END) {
285 // for (i = 0; i < 5 && strncmp(yytext, latex_mathenv[i],
286 // strlen(latex_mathenv[i])); ++i);
288 for (i = 0; i < 6 && strcmp(yytext, latex_mathenv[i]); ++i);
291 if (l->token == LM_TK_SPACE)
308 int parse_align(char * hor, char *)
311 for (char * c = hor; c && *c > ' '; ++c) ++nc;
316 // Accent hacks only for 0.12. Stolen from Cursor.
323 void setAccent(int ac)
325 if (ac > 0 && accent < 8) {
326 nestaccent[accent++] = ac;
328 accent = 0; // consumed!
333 MathedInset * doAccent(byte c, MathedTextCodes t)
335 MathedInset * ac = 0;
337 for (int i = accent - 1; i >= 0; --i) {
339 ac = new MathAccentInset(c, t, nestaccent[i]);
341 ac = new MathAccentInset(ac, nestaccent[i]);
343 accent = 0; // consumed!
350 MathedInset * doAccent(MathedInset * p)
352 MathedInset * ac = 0;
354 for (int i = accent - 1; i >= 0; --i) {
356 ac = new MathAccentInset(p, nestaccent[i]);
358 ac = new MathAccentInset(ac, nestaccent[i]);
360 accent = 0; // consumed!
366 LyxArrayBase * mathed_parse(unsigned flags, LyxArrayBase * array,
372 static int plevel = -1;
373 static int size = LM_ST_TEXT;
374 MathedTextCodes varcode = LM_TC_VAR;
375 MathedInset * binset = 0;
376 static MathMacroTemplate * macro = 0;
381 MathParInset * mt = (mtx) ? *mtx : 0;//(MathParInset*)0;
382 MathedRowSt * crow = (mt) ? mt->getRowSt() : 0;
385 if (!array) array = new LyxArrayBase;
386 MathedIter data(array);
388 if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
389 if ((flags & FLAG_BRACK_ARG) && t == '[') {
392 mathPrintError("Expected {. Maybe you forgot to enclose an argument in {}");
397 MathedInsetTypes fractype = LM_OT_FRAC;
402 data.Insert(doAccent(yylval.i, varcode));
404 data.Insert (yylval.i, varcode); //LM_TC_VAR);
410 data.Insert(macro->getMacroPar(yylval.i-1), LM_TC_INSET);
414 case LM_TK_NEWCOMMAND:
419 string const name(&yytext[1]);
421 // ugly trick to be removed soon (lyx3)
422 char const c = yyis->peek();
425 na = lyx::atoi(yytext);
427 macro = new MathMacroTemplate(name, na);
428 flags = FLAG_BRACE|FLAG_BRACE_LAST;
430 macro->SetData(array);
435 data.Insert (yylval.i, LM_TC_SPECIAL);
441 data.Insert(doAccent(yylval.i, LM_TC_CONST));
443 data.Insert (yylval.i, LM_TC_CONST);
449 if (accent && tprev == LM_TK_ACCENT) {
450 acc_braces[acc_brace++] = brace;
453 if (flags & FLAG_BRACE_OPT) {
454 flags &= ~FLAG_BRACE_OPT;
458 if (flags & FLAG_BRACE)
459 flags &= ~FLAG_BRACE;
461 data.Insert ('{', LM_TC_TEX);
469 mathPrintError("Unmatching braces");
473 if (acc_brace && brace == acc_braces[acc_brace-1]-1) {
477 if (flags & FLAG_BRACE_FONT) {
479 yy_mtextmode = false;
480 flags &= ~FLAG_BRACE_FONT;
483 if (brace == 0 && (flags & FLAG_BRACE_LAST)) {
487 data.Insert ('}', LM_TC_TEX);
494 if (flags & FLAG_BRACK_ARG) {
495 flags &= ~FLAG_BRACK_ARG;
496 char const rg = LexGetArg('[');
498 mathPrintError("Expected ']'");
502 // if (arg) strcpy(arg, yytext);
509 if (flags & FLAG_BRACK_END) {
519 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
520 LyxArrayBase * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
522 // lyxerr << "UP[" << p->GetStyle() << "]" << endl;
523 data.Insert (p, LM_TC_UP);
528 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
529 LyxArrayBase * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
531 data.Insert (p, LM_TC_DOWN);
538 binset->SetLimits(bool(yylval.l->id));
546 if ((flags & FLAG_END) && mt && data.getCol()<mt->GetColumns() - 1) {
547 data.setNumCols(mt->GetColumns());
548 data.Insert('T', LM_TC_TAB);
550 mathPrintError("Unexpected tab");
551 // debug info. [made that conditional -JMarc]
552 if (lyxerr.debugging(Debug::MATHED))
553 lyxerr << data.getCol() << " " << mt->GetColumns() << endl;
558 if (mt && (flags & FLAG_END)) {
559 if (mt->Permit(LMPF_ALLOW_CR)) {
561 crow->setNext(new MathedRowSt(mt->GetColumns() + 1)); // this leaks
562 crow = crow->getNext();
564 data.Insert('K', LM_TC_CR);
566 mathPrintError("Unexpected newline");
572 binset = new MathBigopInset(yylval.l->name, yylval.l->id);
578 if (yylval.l->id < 256) {
579 MathedTextCodes tc = MathIsBOPS(yylval.l->id) ? LM_TC_BOPS: LM_TC_SYMB;
581 data.Insert(doAccent(yylval.l->id, tc));
583 data.Insert(yylval.l->id, tc);
585 MathFuncInset * bg = new MathFuncInset(yylval.l->name);
587 data.Insert(doAccent(bg));
589 data.Insert(bg, true);
596 data.Insert(doAccent(yylval.i, LM_TC_BOP));
598 data.Insert(yylval.i, LM_TC_BOP);
604 mt->UserSetSize(yylval.l->id);
611 MathSpaceInset * sp = new MathSpaceInset(yylval.i);
618 MathDotsInset * p = new MathDotsInset(yylval.l->name, yylval.l->id);
623 fractype = LM_OT_STACKREL;
626 MathFracInset * fc = new MathFracInset(fractype);
627 LyxArrayBase * num = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
628 LyxArrayBase * den = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
629 fc->SetData(num, den);
630 data.Insert(fc, LM_TC_ACTIVE_INSET);
637 char c; yyis->get(c);
640 rt = new MathRootInset(size);
641 rt->setArgumentIdx(0);
642 rt->SetData(mathed_parse(FLAG_BRACK_END, 0, &rt));
643 rt->setArgumentIdx(1);
646 rt = new MathSqrtInset(size);
648 rt->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST, 0, &rt));
649 data.Insert(rt, LM_TC_ACTIVE_INSET);
656 if (lfd == LM_TK_SYM || lfd == LM_TK_STR || lfd == LM_TK_BOP|| lfd == LM_TK_SPECIAL)
657 lfd = (lfd == LM_TK_SYM) ? yylval.l->id: yylval.i;
658 // lyxerr << "L[" << lfd << " " << lfd << "]";
659 LyxArrayBase * a = mathed_parse(FLAG_RIGHT);
661 // lyxerr << "R[" << rgd << "]";
662 if (rgd == LM_TK_SYM || rgd == LM_TK_STR || rgd == LM_TK_BOP || rgd == LM_TK_SPECIAL)
663 rgd = (rgd == LM_TK_SYM) ? yylval.l->id: yylval.i;
664 MathDelimInset * dl = new MathDelimInset(lfd, rgd);
666 data.Insert(dl, LM_TC_ACTIVE_INSET);
667 // lyxerr << "RL[" << lfd << " " << rgd << "]";
672 if (flags & FLAG_RIGHT) {
676 mathPrintError("Unmatched right delimiter");
684 varcode = static_cast<MathedTextCodes>(yylval.l->id);
685 yy_mtextmode = bool(varcode == LM_TC_TEXTRM);
686 flags |= (FLAG_BRACE|FLAG_BRACE_FONT);
691 MathDecorationInset * sq = new MathDecorationInset(yylval.l->id,
693 sq->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
694 data.Insert(sq, LM_TC_ACTIVE_INSET);
699 setAccent(yylval.l->id);
705 crow->setNumbered(false);
712 MathedInset * bg = new MathFuncInset(yylval.l->name);
721 data.Insert(new MathFuncInset(yylval.l->name, LM_OT_FUNCLIM));
728 MathMacroTable::mathMTable.getMacro(yylval.s);
731 data.Insert(doAccent(p), p->getTCode());
733 data.Insert(p, p->getTCode());
734 for (int i = 0; p->setArgumentIdx(i); ++i)
735 p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
738 MathedInset * q = new MathFuncInset(yylval.s, LM_OT_UNDEF);
740 data.Insert(doAccent(q));
749 if (mathed_env != yylval.i && yylval.i!= LM_EN_ARRAY)
750 mathPrintError("Unmatched environment");
751 // debug info [made that conditional -JMarc]
752 if (lyxerr.debugging(Debug::MATHED))
753 lyxerr << "[" << yylval.i << "]" << endl;
755 if (mt) { // && (flags & FLAG_END)) {
763 if (yylval.i == LM_EN_ARRAY) {
764 char ar[120], ar2[8];
765 ar[0] = ar2[0] = '\0';
766 char rg = LexGetArg(0);
772 int const nc = parse_align(ar, ar2);
773 MathParInset * mm = new MathMatrixInset(nc, 0);
774 mm->SetAlign(ar2[0], ar);
775 data.Insert(mm, LM_TC_ACTIVE_INSET);
776 mathed_parse(FLAG_END, mm->GetData(), &mm);
777 } else if (yylval.i >= LM_EN_INTEXT && yylval.i<= LM_EN_EQNARRAY) {
779 mathPrintError("Misplaced environment");
783 mathPrintError("0 paragraph.");
787 mathed_env = yylval.i;
788 if (mathed_env>= LM_EN_DISPLAY) {
789 size = LM_ST_DISPLAY;
790 if (mathed_env>LM_EN_EQUATION) {
791 mt = new MathMatrixInset(3, -1);
792 mt->SetAlign(' ', "rcl");
795 // data.Insert(' ', LM_TC_TAB);
796 // data.Insert(' ', LM_TC_TAB);
800 mt->SetType(mathed_env);
801 crow = mt->getRowSt();
805 lyxerr << "MATH BEGIN[" << mathed_env << "]" << endl;
808 // lyxerr << "MATHCRO[" << yytext << "]";
810 MathMacroTable::mathMTable.getMacro(yytext);
812 data.Insert(p, p->getTCode());
813 p->setArgumentIdx(0);
814 mathed_parse(FLAG_END, p->GetData(), reinterpret_cast<MathParInset**>(&p));
815 // for (int i = 0; p->setArgumentIdx(i); ++i)
816 // p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
818 mathPrintError("Unrecognized environment");
826 MathMacroTable::mathMTable.getMacro(yylval.l->name);
830 data.Insert(doAccent(p));
832 data.Insert(p, static_cast<MathMacro*>(p)->getTCode());
839 char const rg = LexGetArg('\0', true);
841 mathPrintError("Expected '{'");
843 lyxerr << "[" << yytext << "]" << endl;
848 crow->setLabel(yytext);
850 mathed_label = yytext;
853 lyxerr << "Label[" << mathed_label << "]" << endl;
858 mathPrintError("Unrecognized token");
860 lyxerr << "[" << t << " " << yytext << "]" << endl;
865 lyxerr << " Math Panic, expect problems!" << endl;
866 // Search for the end command.
869 } while (t != LM_TK_END && t);
873 if ((flags & FLAG_BRACE_OPT)/* && t!= '^' && t!= '_'*/) {
874 flags &= ~FLAG_BRACE_OPT;
875 //data.Insert (LM_TC_CLOSE);
884 void mathed_parser_file(istream & is, int lineno)
888 if (!MathMacroTable::built)
889 MathMacroTable::mathMTable.builtinMacros();
893 int mathed_parser_lineno()