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;
427 MathedRowSt * crow = (mt) ? mt->getRowSt() : 0;
430 MathedIter data(&array);
432 if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
433 if ((flags & FLAG_BRACK_ARG) && t == '[') {
435 mathPrintError("Expected {. Maybe you forgot to enclose an argument in {}");
440 MathedInsetTypes fractype = LM_OT_FRAC;
445 data.insertInset(doAccent(yylval.i, varcode),
448 data.insert(yylval.i, varcode); //LM_TC_VAR);
453 data.insertInset(macro
454 ->getMacroPar(yylval.i - 1),
457 lyxerr[Debug::MATHED] << "mathed_parse: macro arg outside macro def." << endl;
462 case LM_TK_NEWCOMMAND:
467 string const name(&yytext[1]);
469 // ugly trick to be removed soon (lyx3)
470 char const c = yyis->peek();
473 na = lyx::atoi(yytext.data());
475 macro = new MathMacroTemplate(name, na);
476 flags = FLAG_BRACE|FLAG_BRACE_LAST;
479 macro->setData(array);
484 data.insert(yylval.i, LM_TC_SPECIAL);
489 data.insertInset(doAccent(yylval.i, LM_TC_CONST), LM_TC_INSET);
491 data.insert(yylval.i, LM_TC_CONST);
496 if (accent && tprev == LM_TK_ACCENT) {
497 acc_braces[acc_brace++] = brace;
500 if (flags & FLAG_BRACE_OPT) {
501 flags &= ~FLAG_BRACE_OPT;
505 if (flags & FLAG_BRACE)
506 flags &= ~FLAG_BRACE;
508 data.insert('{', LM_TC_TEX);
515 mathPrintError("Unmatching braces");
519 if (acc_brace && brace == acc_braces[acc_brace - 1] - 1) {
523 if (flags & FLAG_BRACE_FONT) {
525 yy_mtextmode = false;
526 flags &= ~FLAG_BRACE_FONT;
529 if (brace == 0 && (flags & FLAG_BRACE_LAST)) {
533 data.insert('}', LM_TC_TEX);
538 if (flags & FLAG_BRACK_ARG) {
539 flags &= ~FLAG_BRACK_ARG;
540 char const rg = LexGetArg('[');
542 mathPrintError("Expected ']'");
546 // if (arg) strcpy(arg, yytext);
548 data.insert('[', LM_TC_CONST);
552 if (flags & FLAG_BRACK_END) {
556 data.insert(']', LM_TC_CONST);
561 MathParInset * p = new MathParInset(size, "",
564 mathed_parse(ar, FLAG_BRACE_OPT|FLAG_BRACE_LAST);
566 // lyxerr << "UP[" << p->GetStyle() << "]" << endl;
567 data.insertInset(p, LM_TC_UP);
573 MathParInset * p = new MathParInset(size, "",
576 mathed_parse(ar, FLAG_BRACE_OPT|FLAG_BRACE_LAST);
578 data.insertInset(p, LM_TC_DOWN);
584 binset->SetLimits(bool(yylval.l->id));
590 if ((flags & FLAG_END) && mt
591 && data.getCol()<mt->GetColumns() - 1) {
592 data.setNumCols(mt->GetColumns());
593 data.insert('T', LM_TC_TAB);
595 mathPrintError("Unexpected tab");
596 // debug info. [made that conditional -JMarc]
597 if (lyxerr.debugging(Debug::MATHED))
598 lyxerr << data.getCol() << " "
599 << mt->GetColumns() << endl;
603 if (mt && (flags & FLAG_END)) {
604 if (mt->Permit(LMPF_ALLOW_CR)) {
606 crow->setNext(new MathedRowSt(mt->GetColumns() + 1)); // this leaks
607 crow = crow->getNext();
609 data.insert('K', LM_TC_CR);
611 mathPrintError("Unexpected newline");
617 binset = new MathBigopInset(yylval.l->name, yylval.l->id);
618 data.insertInset(binset, LM_TC_INSET);
623 if (yylval.l->id < 256) {
624 MathedTextCodes tc = MathIsBOPS(yylval.l->id) ? LM_TC_BOPS: LM_TC_SYMB;
626 data.insertInset(doAccent(yylval.l->id, tc), LM_TC_INSET);
628 data.insert(yylval.l->id, tc);
630 MathFuncInset * bg = new MathFuncInset(yylval.l->name);
632 data.insertInset(doAccent(bg), LM_TC_INSET);
634 #warning This is suspisious! (Lgb)
635 // it should not take a bool as second arg (Lgb)
636 data.insertInset(bg, true);
644 data.insertInset(doAccent(yylval.i, LM_TC_BOP), LM_TC_INSET);
646 data.insert(yylval.i, LM_TC_BOP);
651 mt->UserSetSize(yylval.l->id);
657 MathSpaceInset * sp = new MathSpaceInset(yylval.i);
658 data.insertInset(sp, LM_TC_INSET);
664 MathDotsInset * p = new MathDotsInset(yylval.l->name, yylval.l->id);
665 data.insertInset(p, LM_TC_INSET);
670 fractype = LM_OT_STACKREL;
674 MathFracInset * fc = new MathFracInset(fractype);
676 mathed_parse(num, FLAG_BRACE|FLAG_BRACE_LAST);
678 mathed_parse(den, FLAG_BRACE|FLAG_BRACE_LAST);
679 fc->SetData(num, den);
680 data.insertInset(fc, LM_TC_ACTIVE_INSET);
692 rt = new MathRootInset(size);
693 rt->setArgumentIdx(0);
695 mathed_parse(ar, FLAG_BRACK_END, &rt);
697 rt->setArgumentIdx(1);
700 rt = new MathSqrtInset(size);
703 mathed_parse(ar, FLAG_BRACE|FLAG_BRACE_LAST, &rt);
705 data.insertInset(rt, LM_TC_ACTIVE_INSET);
712 if (lfd == LM_TK_SYM || lfd == LM_TK_STR || lfd == LM_TK_BOP|| lfd == LM_TK_SPECIAL)
713 lfd = (lfd == LM_TK_SYM) ? yylval.l->id: yylval.i;
714 // lyxerr << "L[" << lfd << " " << lfd << "]";
716 mathed_parse(ar, FLAG_RIGHT);
718 // lyxerr << "R[" << rgd << "]";
719 if (rgd == LM_TK_SYM || rgd == LM_TK_STR || rgd == LM_TK_BOP || rgd == LM_TK_SPECIAL)
720 rgd = (rgd == LM_TK_SYM) ? yylval.l->id: yylval.i;
721 MathDelimInset * dl = new MathDelimInset(lfd, rgd);
723 data.insertInset(dl, LM_TC_ACTIVE_INSET);
724 // lyxerr << "RL[" << lfd << " " << rgd << "]";
729 if (flags & FLAG_RIGHT) {
733 mathPrintError("Unmatched right delimiter");
739 varcode = static_cast<MathedTextCodes>(yylval.l->id);
740 yy_mtextmode = bool(varcode == LM_TC_TEXTRM);
741 flags |= (FLAG_BRACE|FLAG_BRACE_FONT);
746 MathDecorationInset * sq = new MathDecorationInset(yylval.l->id,
749 mathed_parse(ar, FLAG_BRACE|FLAG_BRACE_LAST);
751 data.insertInset(sq, LM_TC_ACTIVE_INSET);
756 setAccent(yylval.l->id);
761 crow->setNumbered(false);
767 #warning This must leak. (Lgb)
768 // if (accent) this must leak... (Lgb)
769 MathedInset * bg = new MathFuncInset(yylval.l->name);
771 data.insert(t, LM_TC_CONST);
773 data.insertInset(bg, LM_TC_INSET);
778 data.insertInset(new MathFuncInset(yylval.l->name, LM_OT_FUNCLIM),
786 MathMacroTable::mathMTable.getMacro(yylval.s);
789 data.insertInset(doAccent(p), p->getTCode());
791 data.insertInset(p, p->getTCode());
792 for (int i = 0; p->setArgumentIdx(i); ++i) {
794 mathed_parse(ar, FLAG_BRACE|FLAG_BRACE_LAST);
798 MathedInset * q = new MathFuncInset(yylval.s, LM_OT_UNDEF);
800 data.insertInset(doAccent(q), LM_TC_INSET);
802 data.insertInset(q, LM_TC_INSET);
809 if (mathed_env != yylval.i && yylval.i != LM_OT_MATRIX)
810 mathPrintError("Unmatched environment");
811 // debug info [made that conditional -JMarc]
812 if (lyxerr.debugging(Debug::MATHED))
813 lyxerr << "[" << yylval.i << "]" << endl;
815 if (mt) { // && (flags & FLAG_END)) {
822 if (yylval.i == LM_OT_MATRIX) {
825 ar[0] = ar2[0] = '\0';
826 char rg = LexGetArg(0);
828 strcpy(ar2, yytext.data());
831 strcpy(ar, yytext.data());
832 int const nc = parse_align(ar, ar2);
833 MathParInset * mm = new MathMatrixInset(nc, 0);
834 mm->SetAlign(ar2[0], ar);
835 data.insertInset(mm, LM_TC_ACTIVE_INSET);
836 mathed_parse(mm->GetData(), FLAG_END, &mm);
837 } else if (is_eqn_type(yylval.i)) {
839 mathPrintError("Misplaced environment");
843 mathPrintError("0 paragraph.");
847 mathed_env = static_cast<MathedInsetTypes>(yylval.i);
848 if (mathed_env != LM_OT_MIN) {
849 size = LM_ST_DISPLAY;
850 if (is_multiline(mathed_env)) {
852 if (is_multicolumn(mathed_env)) {
853 if (mathed_env != LM_OT_ALIGNAT &&
854 mathed_env != LM_OT_ALIGNATN &&
859 lyxerr << "Math parse error: unexpected '"
863 cols = strToInt(string(yytext.data()));
865 mt = create_multiline(mathed_env, cols);
868 // data.Insert(' ', LM_TC_TAB);
869 // data.Insert(' ', LM_TC_TAB);
873 mt->SetType(mathed_env);
874 crow = mt->getRowSt();
878 lyxerr << "MATH BEGIN[" << mathed_env << "]" << endl;
881 // lyxerr << "MATHCRO[" << yytext << "]";
883 MathMacroTable::mathMTable.getMacro(yytext.data());
885 data.insertInset(p, p->getTCode());
886 p->setArgumentIdx(0);
887 mathed_parse(p->GetData(), FLAG_END, reinterpret_cast<MathParInset**>(&p));
888 // for (int i = 0; p->setArgumentIdx(i); ++i)
889 // p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
891 mathPrintError("Unrecognized environment");
898 MathMacroTable::mathMTable.getMacro(yylval.l->name);
902 data.insertInset(doAccent(p), LM_TC_INSET);
904 data.insertInset(p, static_cast<MathMacro*>(p)->getTCode());
911 char const rg = LexGetArg('\0', true);
913 mathPrintError("Expected '{'");
915 lyxerr << "[" << yytext.data() << "]" << endl;
920 crow->setLabel(yytext.data());
922 mathed_label = yytext.data();
925 lyxerr << "Label[" << mathed_label << "]" << endl;
931 mathPrintError("Unrecognized token");
933 lyxerr << "[" << t << " " << yytext.data() << "]" << endl;
939 lyxerr << " Math Panic, expect problems!" << endl;
940 // Search for the end command.
943 } while (t != LM_TK_END && t);
947 if ((flags & FLAG_BRACE_OPT)/* && t!= '^' && t!= '_'*/) {
948 flags &= ~FLAG_BRACE_OPT;
949 //data.Insert (LM_TC_CLOSE);
957 void mathed_parser_file(istream & is, int lineno)
961 if (!MathMacroTable::built)
962 MathMacroTable::mathMTable.builtinMacros();
966 int mathed_parser_lineno()