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"
47 #include "boost/array.hpp"
53 extern MathMatrixInset * create_multiline(short int type, int cols);
57 FLAG_BRACE = 1, // A { needed
58 FLAG_BRACE_ARG = 2, // Next { is argument
59 FLAG_BRACE_OPT = 4, // Optional {
60 FLAG_BRACE_LAST = 8, // Last } ends the parsing process
61 FLAG_BRACK_ARG = 16, // Optional [
62 FLAG_RIGHT = 32, // Next right ends the parsing process
63 FLAG_END = 64, // Next end ends the parsing process
64 FLAG_BRACE_FONT = 128, // Next } closes a font
65 FLAG_BRACK_END = 256 // Next ] ends the parsing process
87 MathedInsetTypes mathed_env = LM_OT_MIN;
93 int const latex_mathenv_num = 12;
94 char const * latex_mathenv[latex_mathenv_num] = {
110 char const * latex_special_chars = "#$%&_{}";
113 // These are lexical codes, not semantic
119 LexBOP, // Binary operators or relations
132 static lexcode_enum lexcode[256];
133 #warning Replace with string
134 //static char yytext[256];
135 static array<char, 256> yytext;
137 static istream * yyis;
138 static bool yy_mtextmode= false;
142 void mathPrintError(string const & msg)
144 lyxerr << "Line ~" << yylineno << ": Math parse error: "
152 for (int i = 0; i <= 255; ++i) {
154 lexcode[i] = LexAlpha;
156 lexcode[i] = LexDigit;
158 lexcode[i] = LexSpace;
160 lexcode[i] = LexNone;
163 lexcode['\t'] = lexcode['\f'] = lexcode[' '] = LexSpace;
164 lexcode['\n'] = LexNewLine;
165 lexcode['%'] = LexComment;
166 lexcode['#'] = LexArgument;
167 lexcode['+'] = lexcode['-'] = lexcode['*'] = lexcode['/']
168 = lexcode['<'] = lexcode['>'] = lexcode['='] = LexBOP;
170 lexcode['!'] = lexcode[','] = lexcode[':']
171 = lexcode[';'] = LexMathSpace;
173 lexcode['('] = lexcode[')'] = lexcode['|'] = lexcode['.'] =
174 lexcode['?'] = LexOther;
176 lexcode['\''] = lexcode['@'] = LexAlpha;
178 lexcode['['] = lexcode[']'] = lexcode['^'] = lexcode['_'] =
179 lexcode['&'] = LexSelf;
181 lexcode['\\'] = LexESC;
182 lexcode['{'] = LexOpen;
183 lexcode['}'] = LexClose;
188 char LexGetArg(char lf, bool accept_spaces = false)
192 while (yyis->good()) {
195 unsigned char c = cc;
200 lyxerr << "Math parse error: unexpected '"
210 : ((lf == '(') ? ')' : 0));
212 lyxerr << "Math parse error: unknown bracket '"
213 << lf << "'" << endl;
216 char * p = &yytext[0];
221 unsigned char c = cc;
224 if ((c > ' ' || (c == ' ' && accept_spaces)) && bcnt > 0)
226 } while (bcnt > 0 && yyis->good() && p - yytext.data() < 255);
236 static int init_done = 0;
238 if (!init_done) LexInitCodes();
242 while (yyis->good()) {
246 if (yy_mtextmode && c == ' ') {
249 } else if (lexcode[c] == LexNewLine) {
252 } else if (lexcode[c] == LexComment) {
256 } while (c != '\n' % yyis->good()); // eat comments
257 } else if (lexcode[c] == LexDigit
258 || lexcode[c] == LexOther
259 || lexcode[c] == LexMathSpace) {
262 } else if (lexcode[c] == LexAlpha) {
265 } else if (lexcode[c] == LexBOP) {
268 } else if (lexcode[c] == LexSelf) {
270 } else if (lexcode[c] == LexArgument) {
274 return LM_TK_ARGUMENT;
275 } else if (lexcode[c] == LexOpen) {
277 } else if (lexcode[c] == LexClose) {
279 } else if (lexcode[c] == LexESC) {
283 return LM_TK_NEWLINE;
286 yylval.i = LM_OT_MIN;
290 yylval.i = LM_OT_MIN;
294 yylval.i = LM_OT_PAR;
298 yylval.i = LM_OT_PAR;
301 if (strchr(latex_special_chars, c)) {
303 return LM_TK_SPECIAL;
305 if (lexcode[c] == LexMathSpace) {
307 for (i = 0; i < 4 && static_cast<int>(c) != latex_mathspace[i][0]; ++i);
308 yylval.i = (i < 4) ? i : 0;
311 if (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
312 char * p = &yytext[0];
313 while ((lexcode[c] == LexAlpha || lexcode[c] == LexDigit)
314 && p - yytext.data() < 255) {
321 if (yyis->good()) yyis->putback(c);
322 latexkeys const * l = in_word_set (yytext.data(), strlen(yytext.data()));
324 if (l->token == LM_TK_BEGIN || l->token == LM_TK_END) {
327 // for (i = 0; i < 5 && compare(yytext, latex_mathenv[i],
328 // strlen(latex_mathenv[i])); ++i);
331 i < latex_mathenv_num
332 && compare(yytext.data(), latex_mathenv[i]); ++i);
334 } else if (l->token == LM_TK_SPACE)
340 yylval.s = yytext.data();
351 int parse_align(char * hor, char *)
354 for (char * c = hor; c && *c > ' '; ++c) ++nc;
359 // Accent hacks only for 0.12. Stolen from Cursor.
366 void setAccent(int ac)
368 if (ac > 0 && accent < 8) {
369 nestaccent[accent++] = ac;
371 accent = 0; // consumed!
376 MathedInset * doAccent(byte c, MathedTextCodes t)
378 MathedInset * ac = 0;
380 for (int i = accent - 1; i >= 0; --i) {
382 ac = new MathAccentInset(c, t, nestaccent[i]);
384 ac = new MathAccentInset(ac, nestaccent[i]);
386 accent = 0; // consumed!
393 MathedInset * doAccent(MathedInset * p)
395 MathedInset * ac = 0;
397 for (int i = accent - 1; i >= 0; --i) {
399 ac = new MathAccentInset(p, nestaccent[i]);
401 ac = new MathAccentInset(ac, nestaccent[i]);
403 accent = 0; // consumed!
411 void mathed_parse(MathedArray & array, unsigned flags = 0,
412 MathParInset ** mtx = 0)
417 static int plevel = -1;
418 static int size = LM_ST_TEXT;
419 MathedTextCodes varcode = LM_TC_VAR;
420 MathedInset * binset = 0;
421 static MathMacroTemplate * macro = 0;
426 MathParInset * mt = (mtx) ? *mtx : 0;
429 MathedIter data(&array);
431 if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
432 if ((flags & FLAG_BRACK_ARG) && t == '[') {
434 mathPrintError("Expected {. Maybe you forgot to enclose an argument in {}");
439 MathedInsetTypes fractype = LM_OT_FRAC;
444 data.insertInset(doAccent(yylval.i, varcode),
447 data.insert(yylval.i, varcode); //LM_TC_VAR);
452 data.insertInset(macro
453 ->getMacroPar(yylval.i - 1),
456 lyxerr[Debug::MATHED] << "mathed_parse: macro arg outside macro def." << endl;
461 case LM_TK_NEWCOMMAND:
466 string const name(&yytext[1]);
468 // ugly trick to be removed soon (lyx3)
469 char const c = yyis->peek();
472 na = lyx::atoi(yytext.data());
474 macro = new MathMacroTemplate(name, na);
475 flags = FLAG_BRACE|FLAG_BRACE_LAST;
478 macro->setData(array);
483 data.insert(yylval.i, LM_TC_SPECIAL);
488 data.insertInset(doAccent(yylval.i, LM_TC_CONST), LM_TC_INSET);
490 data.insert(yylval.i, LM_TC_CONST);
495 if (accent && tprev == LM_TK_ACCENT) {
496 acc_braces[acc_brace++] = brace;
499 if (flags & FLAG_BRACE_OPT) {
500 flags &= ~FLAG_BRACE_OPT;
504 if (flags & FLAG_BRACE)
505 flags &= ~FLAG_BRACE;
507 data.insert('{', LM_TC_TEX);
514 mathPrintError("Unmatching braces");
518 if (acc_brace && brace == acc_braces[acc_brace - 1] - 1) {
522 if (flags & FLAG_BRACE_FONT) {
524 yy_mtextmode = false;
525 flags &= ~FLAG_BRACE_FONT;
528 if (brace == 0 && (flags & FLAG_BRACE_LAST)) {
532 data.insert('}', LM_TC_TEX);
537 if (flags & FLAG_BRACK_ARG) {
538 flags &= ~FLAG_BRACK_ARG;
539 char const rg = LexGetArg('[');
541 mathPrintError("Expected ']'");
545 // if (arg) strcpy(arg, yytext);
547 data.insert('[', LM_TC_CONST);
551 if (flags & FLAG_BRACK_END) {
555 data.insert(']', LM_TC_CONST);
560 MathParInset * p = new MathParInset(size, "",
563 mathed_parse(ar, FLAG_BRACE_OPT|FLAG_BRACE_LAST);
565 // lyxerr << "UP[" << p->GetStyle() << "]" << endl;
566 data.insertInset(p, LM_TC_UP);
572 MathParInset * p = new MathParInset(size, "",
575 mathed_parse(ar, FLAG_BRACE_OPT|FLAG_BRACE_LAST);
577 data.insertInset(p, LM_TC_DOWN);
583 binset->SetLimits(bool(yylval.l->id));
589 if ((flags & FLAG_END) && mt
590 && data.getCol()<mt->GetColumns() - 1) {
591 data.setNumCols(mt->GetColumns());
592 data.insert('T', LM_TC_TAB);
594 mathPrintError("Unexpected tab");
595 // debug info. [made that conditional -JMarc]
596 if (lyxerr.debugging(Debug::MATHED))
597 lyxerr << data.getCol() << " "
598 << mt->GetColumns() << endl;
602 if (mt && (flags & FLAG_END)) {
603 if (mt->Permit(LMPF_ALLOW_CR)) {
604 mt->getRowSt().push_back();
605 data.insert('K', LM_TC_CR);
607 mathPrintError("Unexpected newline");
613 binset = new MathBigopInset(yylval.l->name, yylval.l->id);
614 data.insertInset(binset, LM_TC_INSET);
619 if (yylval.l->id < 256) {
620 MathedTextCodes tc = MathIsBOPS(yylval.l->id) ? LM_TC_BOPS: LM_TC_SYMB;
622 data.insertInset(doAccent(yylval.l->id, tc), LM_TC_INSET);
624 data.insert(yylval.l->id, tc);
626 MathFuncInset * bg = new MathFuncInset(yylval.l->name);
628 data.insertInset(doAccent(bg), LM_TC_INSET);
630 #warning This is suspisious! (Lgb)
631 // it should not take a bool as second arg (Lgb)
632 data.insertInset(bg, true);
640 data.insertInset(doAccent(yylval.i, LM_TC_BOP), LM_TC_INSET);
642 data.insert(yylval.i, LM_TC_BOP);
647 mt->UserSetSize(yylval.l->id);
653 MathSpaceInset * sp = new MathSpaceInset(yylval.i);
654 data.insertInset(sp, LM_TC_INSET);
660 MathDotsInset * p = new MathDotsInset(yylval.l->name, yylval.l->id);
661 data.insertInset(p, LM_TC_INSET);
666 fractype = LM_OT_STACKREL;
670 MathFracInset * fc = new MathFracInset(fractype);
672 mathed_parse(num, FLAG_BRACE|FLAG_BRACE_LAST);
674 mathed_parse(den, FLAG_BRACE|FLAG_BRACE_LAST);
675 fc->SetData(num, den);
676 data.insertInset(fc, LM_TC_ACTIVE_INSET);
688 rt = new MathRootInset(size);
689 rt->setArgumentIdx(0);
691 mathed_parse(ar, FLAG_BRACK_END, &rt);
692 rt->setData(ar); // I belive that line is not needed (Lgb)
693 rt->setArgumentIdx(1);
696 rt = new MathSqrtInset(size);
699 mathed_parse(ar, FLAG_BRACE|FLAG_BRACE_LAST, &rt);
700 rt->setData(ar); // I belive that this line is not needed (Lgb)
701 data.insertInset(rt, LM_TC_ACTIVE_INSET);
708 if (lfd == LM_TK_SYM || lfd == LM_TK_STR || lfd == LM_TK_BOP|| lfd == LM_TK_SPECIAL)
709 lfd = (lfd == LM_TK_SYM) ? yylval.l->id: yylval.i;
710 // lyxerr << "L[" << lfd << " " << lfd << "]";
712 mathed_parse(ar, FLAG_RIGHT);
714 // lyxerr << "R[" << rgd << "]";
715 if (rgd == LM_TK_SYM || rgd == LM_TK_STR || rgd == LM_TK_BOP || rgd == LM_TK_SPECIAL)
716 rgd = (rgd == LM_TK_SYM) ? yylval.l->id: yylval.i;
717 MathDelimInset * dl = new MathDelimInset(lfd, rgd);
719 data.insertInset(dl, LM_TC_ACTIVE_INSET);
720 // lyxerr << "RL[" << lfd << " " << rgd << "]";
725 if (flags & FLAG_RIGHT) {
729 mathPrintError("Unmatched right delimiter");
735 varcode = static_cast<MathedTextCodes>(yylval.l->id);
736 yy_mtextmode = bool(varcode == LM_TC_TEXTRM);
737 flags |= (FLAG_BRACE|FLAG_BRACE_FONT);
742 MathDecorationInset * sq = new MathDecorationInset(yylval.l->id,
745 mathed_parse(ar, FLAG_BRACE|FLAG_BRACE_LAST);
747 data.insertInset(sq, LM_TC_ACTIVE_INSET);
752 setAccent(yylval.l->id);
757 if (!mt->getRowSt().size())
758 mt->getRowSt().push_back();
759 mt->getRowSt().back().setNumbered(false);
766 data.insert(t, LM_TC_CONST);
768 MathedInset * bg = new MathFuncInset(yylval.l->name);
769 data.insertInset(bg, LM_TC_INSET);
774 data.insertInset(new MathFuncInset(yylval.l->name, LM_OT_FUNCLIM),
782 MathMacroTable::mathMTable.createMacro(yylval.s);
785 data.insertInset(doAccent(p), p->getTCode());
787 data.insertInset(p, p->getTCode());
788 for (int i = 0; p->setArgumentIdx(i); ++i) {
790 mathed_parse(ar, FLAG_BRACE|FLAG_BRACE_LAST);
794 MathedInset * q = new MathFuncInset(yylval.s, LM_OT_UNDEF);
796 data.insertInset(doAccent(q), LM_TC_INSET);
798 data.insertInset(q, LM_TC_INSET);
805 if (mathed_env != yylval.i && yylval.i != LM_OT_MATRIX)
806 mathPrintError("Unmatched environment");
807 // debug info [made that conditional -JMarc]
808 if (lyxerr.debugging(Debug::MATHED))
809 lyxerr << "[" << yylval.i << "]" << endl;
811 if (mt) { // && (flags & FLAG_END)) {
818 if (yylval.i == LM_OT_MATRIX) {
821 ar[0] = ar2[0] = '\0';
822 char rg = LexGetArg(0);
824 strcpy(ar2, yytext.data());
827 strcpy(ar, yytext.data());
828 int const nc = parse_align(ar, ar2);
829 MathParInset * mm = new MathMatrixInset(nc, 0);
830 mm->SetAlign(ar2[0], ar);
831 data.insertInset(mm, LM_TC_ACTIVE_INSET);
833 mathed_parse(dat, FLAG_END, &mm);
834 } else if (is_eqn_type(yylval.i)) {
836 mathPrintError("Misplaced environment");
840 mathPrintError("0 paragraph.");
844 mathed_env = static_cast<MathedInsetTypes>(yylval.i);
845 if (mathed_env != LM_OT_MIN) {
846 size = LM_ST_DISPLAY;
847 if (is_multiline(mathed_env)) {
849 if (is_multicolumn(mathed_env)) {
850 if (mathed_env != LM_OT_ALIGNAT &&
851 mathed_env != LM_OT_ALIGNATN &&
856 lyxerr << "Math parse error: unexpected '"
860 cols = strToInt(string(yytext.data()));
862 mt = create_multiline(mathed_env, cols);
865 // data.Insert(' ', LM_TC_TAB);
866 // data.Insert(' ', LM_TC_TAB);
870 mt->SetType(mathed_env);
873 lyxerr[Debug::MATHED] << "MATH BEGIN[" << mathed_env << "]" << endl;
875 // lyxerr << "MATHCRO[" << yytext << "]";
877 MathMacroTable::mathMTable.createMacro(yytext.data());
879 data.insertInset(p, p->getTCode());
880 p->setArgumentIdx(0);
881 //mathed_parse(p->GetData(), FLAG_END, reinterpret_cast<MathParInset**>(&p));
883 mathed_parse(dat, FLAG_END, reinterpret_cast<MathParInset**>(&p));
884 // for (int i = 0; p->setArgumentIdx(i); ++i)
885 // p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
887 mathPrintError("Unrecognized environment");
894 MathMacroTable::mathMTable.createMacro(yylval.l->name);
898 data.insertInset(doAccent(p), LM_TC_INSET);
900 data.insertInset(p, static_cast<MathMacro*>(p)->getTCode());
907 char const rg = LexGetArg('\0', true);
909 mathPrintError("Expected '{'");
911 lyxerr << "[" << yytext.data() << "]" << endl;
916 if (!mt->getRowSt().size())
917 mt->getRowSt().push_back();
918 mt->getRowSt().back().setLabel(yytext.data());
920 mathed_label = yytext.data();
922 lyxerr[Debug::MATHED] << "Label[" << mathed_label << "]" << endl;
927 mathPrintError("Unrecognized token");
929 lyxerr << "[" << t << " " << yytext.data() << "]" << endl;
935 lyxerr << " Math Panic, expect problems!" << endl;
936 // Search for the end command.
939 } while (t != LM_TK_END && t);
943 if ((flags & FLAG_BRACE_OPT)/* && t!= '^' && t!= '_'*/) {
944 flags &= ~FLAG_BRACE_OPT;
945 //data.Insert (LM_TC_CLOSE);
953 void mathed_parser_file(istream & is, int lineno)
957 if (!MathMacroTable::built)
958 MathMacroTable::mathMTable.builtinMacros();
962 int mathed_parser_lineno()