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);
58 FLAG_BRACE = 1, // A { needed
59 FLAG_BRACE_ARG = 2, // Next { is argument
60 FLAG_BRACE_OPT = 4, // Optional {
61 FLAG_BRACE_LAST = 8, // Last } ends the parsing process
62 FLAG_BRACK_ARG = 16, // Optional [
63 FLAG_RIGHT = 32, // Next right ends the parsing process
64 FLAG_END = 64, // Next end ends the parsing process
65 FLAG_BRACE_FONT = 128, // Next } closes a font
66 FLAG_BRACK_END = 256 // Next ] ends the parsing process
86 MathedInsetTypes mathed_env = LM_OT_MIN;
95 int const latex_mathenv_num = 12;
96 char const * latex_mathenv[latex_mathenv_num] = {
112 char const * latex_special_chars = "#$%&_{}";
117 // These are lexical codes, not semantic
123 LexBOP, // Binary operators or relations
136 lexcode_enum lexcode[256];
137 #warning Replace with string
139 array<char, 256> yytext;
142 bool yy_mtextmode= false;
146 void mathPrintError(string const & msg)
148 lyxerr << "Line ~" << yylineno << ": Math parse error: "
155 for (int i = 0; i <= 255; ++i) {
157 lexcode[i] = LexAlpha;
159 lexcode[i] = LexDigit;
161 lexcode[i] = LexSpace;
163 lexcode[i] = LexNone;
166 lexcode['\t'] = lexcode['\f'] = lexcode[' '] = LexSpace;
167 lexcode['\n'] = LexNewLine;
168 lexcode['%'] = LexComment;
169 lexcode['#'] = LexArgument;
170 lexcode['+'] = lexcode['-'] = lexcode['*'] = lexcode['/']
171 = lexcode['<'] = lexcode['>'] = lexcode['='] = LexBOP;
173 lexcode['!'] = lexcode[','] = lexcode[':']
174 = lexcode[';'] = LexMathSpace;
176 lexcode['('] = lexcode[')'] = lexcode['|'] = lexcode['.'] =
177 lexcode['?'] = LexOther;
179 lexcode['\''] = lexcode['@'] = LexAlpha;
181 lexcode['['] = lexcode[']'] = lexcode['^'] = lexcode['_'] =
182 lexcode['&'] = LexSelf;
184 lexcode['\\'] = LexESC;
185 lexcode['{'] = LexOpen;
186 lexcode['}'] = LexClose;
190 char LexGetArg(char lf, bool accept_spaces = false)
194 while (yyis->good()) {
197 unsigned char c = cc;
202 lyxerr << "Math parse error: unexpected '"
212 : ((lf == '(') ? ')' : 0));
214 lyxerr << "Math parse error: unknown bracket '"
215 << lf << "'" << endl;
218 char * p = &yytext[0];
223 unsigned char c = cc;
226 if ((c > ' ' || (c == ' ' && accept_spaces)) && bcnt > 0)
228 } while (bcnt > 0 && yyis->good() && p - yytext.data() < 255);
237 static int init_done = 0;
239 if (!init_done) LexInitCodes();
243 while (yyis->good()) {
247 if (yy_mtextmode && c == ' ') {
250 } else if (lexcode[c] == LexNewLine) {
253 } else if (lexcode[c] == LexComment) {
257 } while (c != '\n' % yyis->good()); // eat comments
258 } else if (lexcode[c] == LexDigit
259 || lexcode[c] == LexOther
260 || lexcode[c] == LexMathSpace) {
263 } else if (lexcode[c] == LexAlpha) {
266 } else if (lexcode[c] == LexBOP) {
269 } else if (lexcode[c] == LexSelf) {
271 } else if (lexcode[c] == LexArgument) {
275 return LM_TK_ARGUMENT;
276 } else if (lexcode[c] == LexOpen) {
278 } else if (lexcode[c] == LexClose) {
280 } else if (lexcode[c] == LexESC) {
284 return LM_TK_NEWLINE;
287 yylval.i = LM_OT_MIN;
291 yylval.i = LM_OT_MIN;
295 yylval.i = LM_OT_PAR;
299 yylval.i = LM_OT_PAR;
302 if (strchr(latex_special_chars, c)) {
304 return LM_TK_SPECIAL;
306 if (lexcode[c] == LexMathSpace) {
308 for (i = 0; i < 4 && static_cast<int>(c) != latex_mathspace[i][0]; ++i);
309 yylval.i = (i < 4) ? i : 0;
312 if (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
313 char * p = &yytext[0];
314 while ((lexcode[c] == LexAlpha || lexcode[c] == LexDigit)
315 && p - yytext.data() < 255) {
322 if (yyis->good()) yyis->putback(c);
323 latexkeys const * l = in_word_set (yytext.data(), strlen(yytext.data()));
325 if (l->token == LM_TK_BEGIN || l->token == LM_TK_END) {
328 // for (i = 0; i < 5 && compare(yytext, latex_mathenv[i],
329 // strlen(latex_mathenv[i])); ++i);
332 i < latex_mathenv_num
333 && compare(yytext.data(), latex_mathenv[i]); ++i);
335 } else if (l->token == LM_TK_SPACE)
341 yylval.s = yytext.data();
352 int parse_align(char * hor, char *)
355 for (char * c = hor; c && *c > ' '; ++c) ++nc;
360 // Accent hacks only for 0.12. Stolen from Cursor.
365 void setAccent(int ac)
367 if (ac > 0 && accent < 8) {
368 nestaccent[accent++] = ac;
370 accent = 0; // consumed!
374 MathedInset * doAccent(byte c, MathedTextCodes t)
376 MathedInset * ac = 0;
378 for (int i = accent - 1; i >= 0; --i) {
380 ac = new MathAccentInset(c, t, nestaccent[i]);
382 ac = new MathAccentInset(ac, nestaccent[i]);
384 accent = 0; // consumed!
390 MathedInset * doAccent(MathedInset * p)
392 MathedInset * ac = 0;
394 for (int i = accent - 1; i >= 0; --i) {
396 ac = new MathAccentInset(p, nestaccent[i]);
398 ac = new MathAccentInset(ac, nestaccent[i]);
400 accent = 0; // consumed!
410 void mathed_parse(MathedArray & array, unsigned flags = 0,
411 MathParInset ** mtx = 0)
416 static int plevel = -1;
417 static int size = LM_ST_TEXT;
418 MathedTextCodes varcode = LM_TC_VAR;
419 MathedInset * binset = 0;
420 static MathMacroTemplate * macro = 0;
425 MathParInset * mt = (mtx) ? *mtx : 0;
428 MathedIter data(&array);
430 if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
431 if ((flags & FLAG_BRACK_ARG) && t == '[') {
433 mathPrintError("Expected {. Maybe you forgot to enclose an argument in {}");
438 MathedInsetTypes fractype = LM_OT_FRAC;
443 data.insertInset(doAccent(yylval.i, varcode),
446 data.insert(yylval.i, varcode); //LM_TC_VAR);
451 data.insertInset(macro
452 ->getMacroPar(yylval.i - 1),
455 lyxerr[Debug::MATHED] << "mathed_parse: macro arg outside macro def." << endl;
460 case LM_TK_NEWCOMMAND:
465 string const name(&yytext[1]);
467 // ugly trick to be removed soon (lyx3)
468 char const c = yyis->peek();
471 na = lyx::atoi(yytext.data());
473 macro = new MathMacroTemplate(name, na);
474 flags = FLAG_BRACE|FLAG_BRACE_LAST;
477 macro->setData(array);
482 data.insert(yylval.i, LM_TC_SPECIAL);
487 data.insertInset(doAccent(yylval.i, LM_TC_CONST), LM_TC_INSET);
489 data.insert(yylval.i, LM_TC_CONST);
494 if (accent && tprev == LM_TK_ACCENT) {
495 acc_braces[acc_brace++] = brace;
498 if (flags & FLAG_BRACE_OPT) {
499 flags &= ~FLAG_BRACE_OPT;
503 if (flags & FLAG_BRACE)
504 flags &= ~FLAG_BRACE;
506 data.insert('{', LM_TC_TEX);
513 mathPrintError("Unmatching braces");
517 if (acc_brace && brace == acc_braces[acc_brace - 1] - 1) {
521 if (flags & FLAG_BRACE_FONT) {
523 yy_mtextmode = false;
524 flags &= ~FLAG_BRACE_FONT;
527 if (brace == 0 && (flags & FLAG_BRACE_LAST)) {
531 data.insert('}', LM_TC_TEX);
536 if (flags & FLAG_BRACK_ARG) {
537 flags &= ~FLAG_BRACK_ARG;
538 char const rg = LexGetArg('[');
540 mathPrintError("Expected ']'");
544 // if (arg) strcpy(arg, yytext);
546 data.insert('[', LM_TC_CONST);
550 if (flags & FLAG_BRACK_END) {
554 data.insert(']', LM_TC_CONST);
559 MathParInset * p = new MathParInset(size, "",
562 mathed_parse(ar, FLAG_BRACE_OPT|FLAG_BRACE_LAST);
564 // lyxerr << "UP[" << p->GetStyle() << "]" << endl;
565 data.insertInset(p, LM_TC_UP);
571 MathParInset * p = new MathParInset(size, "",
574 mathed_parse(ar, FLAG_BRACE_OPT|FLAG_BRACE_LAST);
576 data.insertInset(p, LM_TC_DOWN);
582 binset->SetLimits(bool(yylval.l->id));
588 if ((flags & FLAG_END) && mt
589 && data.getCol()<mt->GetColumns() - 1) {
590 data.setNumCols(mt->GetColumns());
591 data.insert('T', LM_TC_TAB);
593 mathPrintError("Unexpected tab");
594 // debug info. [made that conditional -JMarc]
595 if (lyxerr.debugging(Debug::MATHED))
596 lyxerr << data.getCol() << " "
597 << mt->GetColumns() << endl;
601 if (mt && (flags & FLAG_END)) {
602 if (mt->Permit(LMPF_ALLOW_CR)) {
603 mt->getRowSt().push_back();
604 data.insert('K', LM_TC_CR);
606 mathPrintError("Unexpected newline");
612 binset = new MathBigopInset(yylval.l->name, yylval.l->id);
613 data.insertInset(binset, LM_TC_INSET);
618 if (yylval.l->id < 256) {
619 MathedTextCodes tc = MathIsBOPS(yylval.l->id) ? LM_TC_BOPS: LM_TC_SYMB;
621 data.insertInset(doAccent(yylval.l->id, tc), LM_TC_INSET);
623 data.insert(yylval.l->id, tc);
625 MathFuncInset * bg = new MathFuncInset(yylval.l->name);
627 data.insertInset(doAccent(bg), LM_TC_INSET);
629 #warning This is suspisious! (Lgb)
630 // it should not take a bool as second arg (Lgb)
631 data.insertInset(bg, true);
639 data.insertInset(doAccent(yylval.i, LM_TC_BOP), LM_TC_INSET);
641 data.insert(yylval.i, LM_TC_BOP);
646 mt->UserSetSize(yylval.l->id);
652 MathSpaceInset * sp = new MathSpaceInset(yylval.i);
653 data.insertInset(sp, LM_TC_INSET);
659 MathDotsInset * p = new MathDotsInset(yylval.l->name, yylval.l->id);
660 data.insertInset(p, LM_TC_INSET);
665 fractype = LM_OT_STACKREL;
669 MathFracInset * fc = new MathFracInset(fractype);
671 mathed_parse(num, FLAG_BRACE|FLAG_BRACE_LAST);
673 mathed_parse(den, FLAG_BRACE|FLAG_BRACE_LAST);
674 fc->SetData(num, den);
675 data.insertInset(fc, LM_TC_ACTIVE_INSET);
687 rt = new MathRootInset(size);
688 rt->setArgumentIdx(0);
690 mathed_parse(ar, FLAG_BRACK_END, &rt);
691 rt->setData(ar); // I belive that line is not needed (Lgb)
692 rt->setArgumentIdx(1);
695 rt = new MathSqrtInset(size);
698 mathed_parse(ar, FLAG_BRACE|FLAG_BRACE_LAST, &rt);
699 rt->setData(ar); // I belive that this line is not needed (Lgb)
700 data.insertInset(rt, LM_TC_ACTIVE_INSET);
707 if (lfd == LM_TK_SYM || lfd == LM_TK_STR || lfd == LM_TK_BOP|| lfd == LM_TK_SPECIAL)
708 lfd = (lfd == LM_TK_SYM) ? yylval.l->id: yylval.i;
709 // lyxerr << "L[" << lfd << " " << lfd << "]";
711 mathed_parse(ar, FLAG_RIGHT);
713 // lyxerr << "R[" << rgd << "]";
714 if (rgd == LM_TK_SYM || rgd == LM_TK_STR || rgd == LM_TK_BOP || rgd == LM_TK_SPECIAL)
715 rgd = (rgd == LM_TK_SYM) ? yylval.l->id: yylval.i;
716 MathDelimInset * dl = new MathDelimInset(lfd, rgd);
718 data.insertInset(dl, LM_TC_ACTIVE_INSET);
719 // lyxerr << "RL[" << lfd << " " << rgd << "]";
724 if (flags & FLAG_RIGHT) {
728 mathPrintError("Unmatched right delimiter");
734 varcode = static_cast<MathedTextCodes>(yylval.l->id);
735 yy_mtextmode = bool(varcode == LM_TC_TEXTRM);
736 flags |= (FLAG_BRACE|FLAG_BRACE_FONT);
741 MathDecorationInset * sq = new MathDecorationInset(yylval.l->id,
744 mathed_parse(ar, FLAG_BRACE|FLAG_BRACE_LAST);
746 data.insertInset(sq, LM_TC_ACTIVE_INSET);
751 setAccent(yylval.l->id);
756 if (!mt->getRowSt().size())
757 mt->getRowSt().push_back();
758 mt->getRowSt().back().setNumbered(false);
765 data.insert(t, LM_TC_CONST);
767 MathedInset * bg = new MathFuncInset(yylval.l->name);
768 data.insertInset(bg, LM_TC_INSET);
773 data.insertInset(new MathFuncInset(yylval.l->name, LM_OT_FUNCLIM),
781 MathMacroTable::mathMTable.createMacro(yylval.s);
784 data.insertInset(doAccent(p), p->getTCode());
786 data.insertInset(p, p->getTCode());
787 for (int i = 0; p->setArgumentIdx(i); ++i) {
789 mathed_parse(ar, FLAG_BRACE|FLAG_BRACE_LAST);
793 MathedInset * q = new MathFuncInset(yylval.s, LM_OT_UNDEF);
795 data.insertInset(doAccent(q), LM_TC_INSET);
797 data.insertInset(q, LM_TC_INSET);
804 if (mathed_env != yylval.i && yylval.i != LM_OT_MATRIX)
805 mathPrintError("Unmatched environment");
806 // debug info [made that conditional -JMarc]
807 if (lyxerr.debugging(Debug::MATHED))
808 lyxerr << "[" << yylval.i << "]" << endl;
810 if (mt) { // && (flags & FLAG_END)) {
817 if (yylval.i == LM_OT_MATRIX) {
820 ar[0] = ar2[0] = '\0';
821 char rg = LexGetArg(0);
823 strcpy(ar2, yytext.data());
826 strcpy(ar, yytext.data());
827 int const nc = parse_align(ar, ar2);
828 MathParInset * mm = new MathMatrixInset(nc, 0);
829 mm->SetAlign(ar2[0], ar);
830 data.insertInset(mm, LM_TC_ACTIVE_INSET);
832 mathed_parse(dat, FLAG_END, &mm);
833 } else if (is_eqn_type(yylval.i)) {
835 mathPrintError("Misplaced environment");
839 mathPrintError("0 paragraph.");
843 mathed_env = static_cast<MathedInsetTypes>(yylval.i);
844 if (mathed_env != LM_OT_MIN) {
845 size = LM_ST_DISPLAY;
846 if (is_multiline(mathed_env)) {
848 if (is_multicolumn(mathed_env)) {
849 if (mathed_env != LM_OT_ALIGNAT &&
850 mathed_env != LM_OT_ALIGNATN &&
855 lyxerr << "Math parse error: unexpected '"
859 cols = strToInt(string(yytext.data()));
861 mt = create_multiline(mathed_env, cols);
864 // data.Insert(' ', LM_TC_TAB);
865 // data.Insert(' ', LM_TC_TAB);
869 mt->SetType(mathed_env);
872 lyxerr[Debug::MATHED] << "MATH BEGIN[" << mathed_env << "]" << endl;
874 // lyxerr << "MATHCRO[" << yytext << "]";
876 MathMacroTable::mathMTable.createMacro(yytext.data());
878 data.insertInset(p, p->getTCode());
879 p->setArgumentIdx(0);
880 //mathed_parse(p->GetData(), FLAG_END, reinterpret_cast<MathParInset**>(&p));
882 mathed_parse(dat, FLAG_END, reinterpret_cast<MathParInset**>(&p));
883 // for (int i = 0; p->setArgumentIdx(i); ++i)
884 // p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
886 mathPrintError("Unrecognized environment");
893 MathMacroTable::mathMTable.createMacro(yylval.l->name);
897 data.insertInset(doAccent(p), LM_TC_INSET);
899 data.insertInset(p, static_cast<MathMacro*>(p)->getTCode());
906 char const rg = LexGetArg('\0', true);
908 mathPrintError("Expected '{'");
910 lyxerr << "[" << yytext.data() << "]" << endl;
915 if (!mt->getRowSt().size())
916 mt->getRowSt().push_back();
917 mt->getRowSt().back().setLabel(yytext.data());
919 mathed_label = yytext.data();
921 lyxerr[Debug::MATHED] << "Label[" << mathed_label << "]" << endl;
926 mathPrintError("Unrecognized token");
928 lyxerr << "[" << t << " " << yytext.data() << "]" << endl;
934 lyxerr << " Math Panic, expect problems!" << endl;
935 // Search for the end command.
938 } while (t != LM_TK_END && t);
942 if ((flags & FLAG_BRACE_OPT)/* && t!= '^' && t!= '_'*/) {
943 flags &= ~FLAG_BRACE_OPT;
944 //data.Insert (LM_TC_CLOSE);
952 void mathed_parser_file(istream & is, int lineno)
956 if (!MathMacroTable::built)
957 MathMacroTable::mathMTable.builtinMacros();
961 int mathed_parser_lineno()