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];
138 #warning Replace with string
141 boost::array<char, 256> yytext;
144 bool yy_mtextmode= false;
148 void mathPrintError(string const & msg)
150 lyxerr << "Line ~" << yylineno << ": Math parse error: "
157 for (int i = 0; i <= 255; ++i) {
159 lexcode[i] = LexAlpha;
161 lexcode[i] = LexDigit;
163 lexcode[i] = LexSpace;
165 lexcode[i] = LexNone;
168 lexcode['\t'] = lexcode['\f'] = lexcode[' '] = LexSpace;
169 lexcode['\n'] = LexNewLine;
170 lexcode['%'] = LexComment;
171 lexcode['#'] = LexArgument;
172 lexcode['+'] = lexcode['-'] = lexcode['*'] = lexcode['/']
173 = lexcode['<'] = lexcode['>'] = lexcode['='] = LexBOP;
175 lexcode['!'] = lexcode[','] = lexcode[':']
176 = lexcode[';'] = LexMathSpace;
178 lexcode['('] = lexcode[')'] = lexcode['|'] = lexcode['.'] =
179 lexcode['?'] = LexOther;
181 lexcode['\''] = lexcode['@'] = LexAlpha;
183 lexcode['['] = lexcode[']'] = lexcode['^'] = lexcode['_'] =
184 lexcode['&'] = LexSelf;
186 lexcode['\\'] = LexESC;
187 lexcode['{'] = LexOpen;
188 lexcode['}'] = LexClose;
192 char LexGetArg(char lf, bool accept_spaces = false)
196 while (yyis->good()) {
199 unsigned char c = cc;
204 lyxerr << "Math parse error: unexpected '"
214 : ((lf == '(') ? ')' : 0));
216 lyxerr << "Math parse error: unknown bracket '"
217 << lf << "'" << endl;
220 char * p = &yytext[0];
225 unsigned char c = cc;
228 if ((c > ' ' || (c == ' ' && accept_spaces)) && bcnt > 0)
230 } while (bcnt > 0 && yyis->good() && p - yytext.data() < 255);
239 static int init_done = 0;
241 if (!init_done) LexInitCodes();
245 while (yyis->good()) {
249 if (yy_mtextmode && c == ' ') {
252 } else if (lexcode[c] == LexNewLine) {
255 } else if (lexcode[c] == LexComment) {
259 } while (c != '\n' % yyis->good()); // eat comments
260 } else if (lexcode[c] == LexDigit
261 || lexcode[c] == LexOther
262 || lexcode[c] == LexMathSpace) {
265 } else if (lexcode[c] == LexAlpha) {
268 } else if (lexcode[c] == LexBOP) {
271 } else if (lexcode[c] == LexSelf) {
273 } else if (lexcode[c] == LexArgument) {
277 return LM_TK_ARGUMENT;
278 } else if (lexcode[c] == LexOpen) {
280 } else if (lexcode[c] == LexClose) {
282 } else if (lexcode[c] == LexESC) {
286 return LM_TK_NEWLINE;
289 yylval.i = LM_OT_MIN;
293 yylval.i = LM_OT_MIN;
297 yylval.i = LM_OT_PAR;
301 yylval.i = LM_OT_PAR;
304 if (strchr(latex_special_chars, c)) {
306 return LM_TK_SPECIAL;
308 if (lexcode[c] == LexMathSpace) {
310 for (i = 0; i < 4 && static_cast<int>(c) != latex_mathspace[i][0]; ++i);
311 yylval.i = (i < 4) ? i : 0;
314 if (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
315 char * p = &yytext[0];
316 while ((lexcode[c] == LexAlpha || lexcode[c] == LexDigit)
317 && p - yytext.data() < 255) {
324 if (yyis->good()) yyis->putback(c);
325 latexkeys const * l = in_word_set (yytext.data(), strlen(yytext.data()));
327 if (l->token == LM_TK_BEGIN || l->token == LM_TK_END) {
330 // for (i = 0; i < 5 && compare(yytext, latex_mathenv[i],
331 // strlen(latex_mathenv[i])); ++i);
334 i < latex_mathenv_num
335 && compare(yytext.data(), latex_mathenv[i]); ++i);
337 } else if (l->token == LM_TK_SPACE)
343 yylval.s = yytext.data();
354 int parse_align(char * hor, char *)
357 for (char * c = hor; c && *c > ' '; ++c) ++nc;
362 // Accent hacks only for 0.12. Stolen from Cursor.
367 void setAccent(int ac)
369 if (ac > 0 && accent < 8) {
370 nestaccent[accent++] = ac;
372 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!
392 MathedInset * doAccent(MathedInset * p)
394 MathedInset * ac = 0;
396 for (int i = accent - 1; i >= 0; --i) {
398 ac = new MathAccentInset(p, nestaccent[i]);
400 ac = new MathAccentInset(ac, nestaccent[i]);
402 accent = 0; // consumed!
412 void mathed_parse(MathedArray & array, unsigned flags = 0,
413 MathParInset ** mtx = 0)
418 static int plevel = -1;
419 static int size = LM_ST_TEXT;
420 MathedTextCodes varcode = LM_TC_VAR;
421 MathedInset * binset = 0;
422 static MathMacroTemplate * macro = 0;
427 MathParInset * mt = (mtx) ? *mtx : 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)) {
605 mt->getRowSt().push_back();
606 data.insert('K', LM_TC_CR);
608 mathPrintError("Unexpected newline");
614 binset = new MathBigopInset(yylval.l->name, yylval.l->id);
615 data.insertInset(binset, LM_TC_INSET);
620 if (yylval.l->id < 256) {
621 MathedTextCodes tc = MathIsBOPS(yylval.l->id) ? LM_TC_BOPS: LM_TC_SYMB;
623 data.insertInset(doAccent(yylval.l->id, tc), LM_TC_INSET);
625 data.insert(yylval.l->id, tc);
627 MathFuncInset * bg = new MathFuncInset(yylval.l->name);
629 data.insertInset(doAccent(bg), LM_TC_INSET);
632 #warning This is suspisious! (Lgb)
634 // it should not take a bool as second arg (Lgb)
635 data.insertInset(bg, true);
643 data.insertInset(doAccent(yylval.i, LM_TC_BOP), LM_TC_INSET);
645 data.insert(yylval.i, LM_TC_BOP);
650 mt->UserSetSize(yylval.l->id);
656 MathSpaceInset * sp = new MathSpaceInset(yylval.i);
657 data.insertInset(sp, LM_TC_INSET);
663 MathDotsInset * p = new MathDotsInset(yylval.l->name, yylval.l->id);
664 data.insertInset(p, LM_TC_INSET);
669 fractype = LM_OT_STACKREL;
673 MathFracInset * fc = new MathFracInset(fractype);
675 mathed_parse(num, FLAG_BRACE|FLAG_BRACE_LAST);
677 mathed_parse(den, FLAG_BRACE|FLAG_BRACE_LAST);
678 fc->SetData(num, den);
679 data.insertInset(fc, LM_TC_ACTIVE_INSET);
691 rt = new MathRootInset(size);
692 rt->setArgumentIdx(0);
694 mathed_parse(ar, FLAG_BRACK_END, &rt);
695 rt->setData(ar); // I belive that line is not needed (Lgb)
696 rt->setArgumentIdx(1);
699 rt = new MathSqrtInset(size);
702 mathed_parse(ar, FLAG_BRACE|FLAG_BRACE_LAST, &rt);
703 rt->setData(ar); // I belive that this line is not needed (Lgb)
704 data.insertInset(rt, LM_TC_ACTIVE_INSET);
711 if (lfd == LM_TK_SYM || lfd == LM_TK_STR || lfd == LM_TK_BOP|| lfd == LM_TK_SPECIAL)
712 lfd = (lfd == LM_TK_SYM) ? yylval.l->id: yylval.i;
713 // lyxerr << "L[" << lfd << " " << lfd << "]";
715 mathed_parse(ar, FLAG_RIGHT);
717 // lyxerr << "R[" << rgd << "]";
718 if (rgd == LM_TK_SYM || rgd == LM_TK_STR || rgd == LM_TK_BOP || rgd == LM_TK_SPECIAL)
719 rgd = (rgd == LM_TK_SYM) ? yylval.l->id: yylval.i;
720 MathDelimInset * dl = new MathDelimInset(lfd, rgd);
722 data.insertInset(dl, LM_TC_ACTIVE_INSET);
723 // lyxerr << "RL[" << lfd << " " << rgd << "]";
728 if (flags & FLAG_RIGHT) {
732 mathPrintError("Unmatched right delimiter");
738 varcode = static_cast<MathedTextCodes>(yylval.l->id);
739 yy_mtextmode = bool(varcode == LM_TC_TEXTRM);
740 flags |= (FLAG_BRACE|FLAG_BRACE_FONT);
745 MathDecorationInset * sq = new MathDecorationInset(yylval.l->id,
748 mathed_parse(ar, FLAG_BRACE|FLAG_BRACE_LAST);
750 data.insertInset(sq, LM_TC_ACTIVE_INSET);
755 setAccent(yylval.l->id);
760 if (!mt->getRowSt().size())
761 mt->getRowSt().push_back();
762 mt->getRowSt().back().setNumbered(false);
769 data.insert(t, LM_TC_CONST);
771 MathedInset * bg = new MathFuncInset(yylval.l->name);
772 data.insertInset(bg, LM_TC_INSET);
777 data.insertInset(new MathFuncInset(yylval.l->name, LM_OT_FUNCLIM),
785 MathMacroTable::mathMTable.createMacro(yylval.s);
788 data.insertInset(doAccent(p), p->getTCode());
790 data.insertInset(p, p->getTCode());
791 for (int i = 0; p->setArgumentIdx(i); ++i) {
793 mathed_parse(ar, FLAG_BRACE|FLAG_BRACE_LAST);
797 MathedInset * q = new MathFuncInset(yylval.s, LM_OT_UNDEF);
799 data.insertInset(doAccent(q), LM_TC_INSET);
801 data.insertInset(q, LM_TC_INSET);
808 if (mathed_env != yylval.i && yylval.i != LM_OT_MATRIX)
809 mathPrintError("Unmatched environment");
810 // debug info [made that conditional -JMarc]
811 if (lyxerr.debugging(Debug::MATHED))
812 lyxerr << "[" << yylval.i << "]" << endl;
814 if (mt) { // && (flags & FLAG_END)) {
821 if (yylval.i == LM_OT_MATRIX) {
824 ar[0] = ar2[0] = '\0';
825 char rg = LexGetArg(0);
827 strcpy(ar2, yytext.data());
830 strcpy(ar, yytext.data());
831 int const nc = parse_align(ar, ar2);
832 MathParInset * mm = new MathMatrixInset(nc, 0);
833 mm->SetAlign(ar2[0], ar);
834 data.insertInset(mm, LM_TC_ACTIVE_INSET);
836 mathed_parse(dat, 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);
876 lyxerr[Debug::MATHED] << "MATH BEGIN[" << mathed_env << "]" << endl;
878 // lyxerr << "MATHCRO[" << yytext << "]";
880 MathMacroTable::mathMTable.createMacro(yytext.data());
882 data.insertInset(p, p->getTCode());
883 p->setArgumentIdx(0);
884 //mathed_parse(p->GetData(), FLAG_END, reinterpret_cast<MathParInset**>(&p));
886 mathed_parse(dat, FLAG_END, reinterpret_cast<MathParInset**>(&p));
887 // for (int i = 0; p->setArgumentIdx(i); ++i)
888 // p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
890 mathPrintError("Unrecognized environment");
897 MathMacroTable::mathMTable.createMacro(yylval.l->name);
901 data.insertInset(doAccent(p), LM_TC_INSET);
903 data.insertInset(p, static_cast<MathMacro*>(p)->getTCode());
910 char const rg = LexGetArg('\0', true);
912 mathPrintError("Expected '{'");
914 lyxerr << "[" << yytext.data() << "]" << endl;
919 if (!mt->getRowSt().size())
920 mt->getRowSt().push_back();
921 mt->getRowSt().back().setLabel(yytext.data());
923 mathed_label = yytext.data();
925 lyxerr[Debug::MATHED] << "Label[" << mathed_label << "]" << endl;
930 mathPrintError("Unrecognized token");
932 lyxerr << "[" << t << " " << yytext.data() << "]" << endl;
938 lyxerr << " Math Panic, expect problems!" << endl;
939 // Search for the end command.
942 } while (t != LM_TK_END && t);
946 if ((flags & FLAG_BRACE_OPT)/* && t!= '^' && t!= '_'*/) {
947 flags &= ~FLAG_BRACE_OPT;
948 //data.Insert (LM_TC_CLOSE);
956 void mathed_parser_file(istream & is, int lineno)
960 if (!MathMacroTable::built)
961 MathMacroTable::mathMTable.builtinMacros();
965 int mathed_parser_lineno()