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
86 MathedInsetTypes mathed_env = LM_OT_MIN;
91 int const latex_mathenv_num = 12;
92 char const * latex_mathenv[latex_mathenv_num] = {
110 char const * latex_special_chars = "#$%&_{}";
112 // These are lexical codes, not semantic
118 LexBOP, // Binary operators or relations
130 static lexcode_enum lexcode[256];
131 #warning Replace with string
132 //static char yytext[256];
133 static array<char, 256> yytext;
135 static istream * yyis;
136 static bool yy_mtextmode= false;
139 void mathPrintError(string const & msg)
141 lyxerr << "Line ~" << yylineno << ": Math parse error: "
149 for (int i = 0; i <= 255; ++i) {
150 if (isalpha(i)) lexcode[i] = LexAlpha;
151 else if (isdigit(i)) lexcode[i] = LexDigit;
152 else if (isspace(i)) lexcode[i] = LexSpace;
153 else lexcode[i] = LexNone;
156 lexcode['\t'] = lexcode['\f'] = lexcode[' '] = LexSpace;
157 lexcode['\n'] = LexNewLine;
158 lexcode['%'] = LexComment;
159 lexcode['#'] = LexArgument;
160 lexcode['+'] = lexcode['-'] = lexcode['*'] = lexcode['/'] =
161 lexcode['<'] = lexcode['>'] = lexcode['='] = LexBOP;
163 lexcode['!'] = lexcode[','] = lexcode[':'] = lexcode[';'] = LexMathSpace;
165 lexcode['('] = lexcode[')'] = lexcode['|'] = lexcode['.'] =
166 lexcode['?'] = LexOther;
168 lexcode['\''] = lexcode['@'] = LexAlpha;
170 lexcode['['] = lexcode[']'] = lexcode['^'] = lexcode['_'] =
171 lexcode['&'] = LexSelf;
173 lexcode['\\'] = LexESC;
174 lexcode['{'] = LexOpen;
175 lexcode['}'] = LexClose;
180 char LexGetArg(char lf, bool accept_spaces= false)
184 while (yyis->good()) {
191 lyxerr << "Math parse error: unexpected '"
199 (lf == '{') ? '}' : ((lf == '[') ? ']' : ((lf == '(') ? ')' : 0));
201 lyxerr << "Math parse error: unknown bracket '"
202 << lf << "'" << endl;
205 char * p = &yytext[0];
212 if ((c > ' ' || (c == ' ' && accept_spaces)) && bcnt > 0) *(p++) = c;
213 } while (bcnt > 0 && yyis->good() && p - yytext.data() < 255);
222 static int init_done = 0;
224 if (!init_done) LexInitCodes();
228 while (yyis->good()) {
232 if (yy_mtextmode && c == ' ') {
237 if (lexcode[c] == LexNewLine) {
242 if (lexcode[c] == LexComment) {
246 } while (c != '\n' % yyis->good()); // eat comments
249 if (lexcode[c] == LexDigit
250 || lexcode[c] == LexOther
251 || lexcode[c] == LexMathSpace) {
255 if (lexcode[c] == LexAlpha) {
259 if (lexcode[c] == LexBOP) {
263 if (lexcode[c] == LexSelf) {
266 if (lexcode[c] == LexArgument) {
270 return LM_TK_ARGUMENT;
272 if (lexcode[c] == LexOpen) {
275 if (lexcode[c] == LexClose) {
278 if (lexcode[c] == LexESC) {
282 return LM_TK_NEWLINE;
285 yylval.i = LM_OT_MIN;
289 yylval.i = LM_OT_MIN;
293 yylval.i = LM_OT_PAR;
297 yylval.i = LM_OT_PAR;
300 if (strchr(latex_special_chars, c)) {
302 return LM_TK_SPECIAL;
304 if (lexcode[c] == LexMathSpace) {
306 for (i = 0; i < 4 && static_cast<int>(c) != latex_mathspace[i][0]; ++i);
307 yylval.i = (i < 4) ? i : 0;
310 if (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
311 char * p = &yytext[0];
312 while ((lexcode[c] == LexAlpha || lexcode[c] == LexDigit)
313 && p - yytext.data() < 255) {
320 if (yyis->good()) yyis->putback(c);
321 latexkeys const * l = in_word_set (yytext.data(), strlen(yytext.data()));
323 if (l->token == LM_TK_BEGIN || l->token == LM_TK_END) {
326 // for (i = 0; i < 5 && compare(yytext, latex_mathenv[i],
327 // strlen(latex_mathenv[i])); ++i);
329 for (i = 0; i < latex_mathenv_num
330 && compare(yytext.data(), latex_mathenv[i]); ++i);
333 if (l->token == LM_TK_SPACE)
339 yylval.s = yytext.data();
350 int parse_align(char * hor, char *)
353 for (char * c = hor; c && *c > ' '; ++c) ++nc;
358 // 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!
375 MathedInset * doAccent(byte c, MathedTextCodes t)
377 MathedInset * ac = 0;
379 for (int i = accent - 1; i >= 0; --i) {
381 ac = new MathAccentInset(c, t, nestaccent[i]);
383 ac = new MathAccentInset(ac, nestaccent[i]);
385 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!
408 void mathed_parse(MathedArray & array, unsigned flags = 0,
409 MathParInset ** mtx = 0)
414 static int plevel = -1;
415 static int size = LM_ST_TEXT;
416 MathedTextCodes varcode = LM_TC_VAR;
417 MathedInset * binset = 0;
418 static MathMacroTemplate * macro = 0;
423 MathParInset * mt = (mtx) ? *mtx : 0;//(MathParInset*)0;
424 MathedRowSt * crow = (mt) ? mt->getRowSt() : 0;
427 MathedIter data(&array);
429 if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
430 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), LM_TC_INSET);
445 data.insert(yylval.i, varcode); //LM_TC_VAR);
451 data.insertInset(macro->getMacroPar(yylval.i-1), LM_TC_INSET);
455 case LM_TK_NEWCOMMAND:
460 string const name(&yytext[1]);
462 // ugly trick to be removed soon (lyx3)
463 char const c = yyis->peek();
466 na = lyx::atoi(yytext.data());
468 macro = new MathMacroTemplate(name, na);
469 flags = FLAG_BRACE|FLAG_BRACE_LAST;
471 macro->setData(array);
476 data.insert(yylval.i, LM_TC_SPECIAL);
482 data.insertInset(doAccent(yylval.i, LM_TC_CONST), LM_TC_INSET);
484 data.insert(yylval.i, LM_TC_CONST);
490 if (accent && tprev == LM_TK_ACCENT) {
491 acc_braces[acc_brace++] = brace;
494 if (flags & FLAG_BRACE_OPT) {
495 flags &= ~FLAG_BRACE_OPT;
499 if (flags & FLAG_BRACE)
500 flags &= ~FLAG_BRACE;
502 data.insert('{', LM_TC_TEX);
510 mathPrintError("Unmatching braces");
514 if (acc_brace && brace == acc_braces[acc_brace-1]-1) {
518 if (flags & FLAG_BRACE_FONT) {
520 yy_mtextmode = false;
521 flags &= ~FLAG_BRACE_FONT;
524 if (brace == 0 && (flags & FLAG_BRACE_LAST)) {
528 data.insert('}', LM_TC_TEX);
535 if (flags & FLAG_BRACK_ARG) {
536 flags &= ~FLAG_BRACK_ARG;
537 char const rg = LexGetArg('[');
539 mathPrintError("Expected ']'");
543 // if (arg) strcpy(arg, yytext);
545 data.insert('[', LM_TC_CONST);
550 if (flags & FLAG_BRACK_END) {
554 data.insert(']', LM_TC_CONST);
560 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
562 mathed_parse(ar, FLAG_BRACE_OPT|FLAG_BRACE_LAST);
564 // lyxerr << "UP[" << p->GetStyle() << "]" << endl;
565 data.insertInset(p, LM_TC_UP);
570 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
572 mathed_parse(ar, FLAG_BRACE_OPT|FLAG_BRACE_LAST);
574 data.insertInset(p, LM_TC_DOWN);
581 binset->SetLimits(bool(yylval.l->id));
589 if ((flags & FLAG_END) && mt && 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() << " " << mt->GetColumns() << endl;
601 if (mt && (flags & FLAG_END)) {
602 if (mt->Permit(LMPF_ALLOW_CR)) {
604 crow->setNext(new MathedRowSt(mt->GetColumns() + 1)); // this leaks
605 crow = crow->getNext();
607 data.insert('K', LM_TC_CR);
609 mathPrintError("Unexpected newline");
615 binset = new MathBigopInset(yylval.l->name, yylval.l->id);
616 data.insertInset(binset, LM_TC_INSET);
621 if (yylval.l->id < 256) {
622 MathedTextCodes tc = MathIsBOPS(yylval.l->id) ? LM_TC_BOPS: LM_TC_SYMB;
624 data.insertInset(doAccent(yylval.l->id, tc), LM_TC_INSET);
626 data.insert(yylval.l->id, tc);
628 MathFuncInset * bg = new MathFuncInset(yylval.l->name);
630 data.insertInset(doAccent(bg), LM_TC_INSET);
632 #warning This is suspisious! (Lgb)
633 // it should not take a bool as second arg (Lgb)
634 data.insertInset(bg, true);
643 data.insertInset(doAccent(yylval.i, LM_TC_BOP), LM_TC_INSET);
645 data.insert(yylval.i, LM_TC_BOP);
651 mt->UserSetSize(yylval.l->id);
658 MathSpaceInset * sp = new MathSpaceInset(yylval.i);
659 data.insertInset(sp, LM_TC_INSET);
665 MathDotsInset * p = new MathDotsInset(yylval.l->name, yylval.l->id);
666 data.insertInset(p, LM_TC_INSET);
670 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);
686 char c; yyis->get(c);
689 rt = new MathRootInset(size);
690 rt->setArgumentIdx(0);
692 mathed_parse(ar, FLAG_BRACK_END, &rt);
694 rt->setArgumentIdx(1);
697 rt = new MathSqrtInset(size);
700 mathed_parse(ar, FLAG_BRACE|FLAG_BRACE_LAST, &rt);
702 data.insertInset(rt, LM_TC_ACTIVE_INSET);
709 if (lfd == LM_TK_SYM || lfd == LM_TK_STR || lfd == LM_TK_BOP|| lfd == LM_TK_SPECIAL)
710 lfd = (lfd == LM_TK_SYM) ? yylval.l->id: yylval.i;
711 // lyxerr << "L[" << lfd << " " << lfd << "]";
713 mathed_parse(ar, FLAG_RIGHT);
715 // lyxerr << "R[" << rgd << "]";
716 if (rgd == LM_TK_SYM || rgd == LM_TK_STR || rgd == LM_TK_BOP || rgd == LM_TK_SPECIAL)
717 rgd = (rgd == LM_TK_SYM) ? yylval.l->id: yylval.i;
718 MathDelimInset * dl = new MathDelimInset(lfd, rgd);
720 data.insertInset(dl, LM_TC_ACTIVE_INSET);
721 // lyxerr << "RL[" << lfd << " " << rgd << "]";
726 if (flags & FLAG_RIGHT) {
730 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);
761 crow->setNumbered(false);
768 #warning This must leak. (Lgb)
769 // if (accent) this must leak... (Lgb)
770 MathedInset * bg = new MathFuncInset(yylval.l->name);
772 data.insert(t, LM_TC_CONST);
774 data.insertInset(bg, LM_TC_INSET);
779 data.insertInset(new MathFuncInset(yylval.l->name, LM_OT_FUNCLIM),
787 MathMacroTable::mathMTable.getMacro(yylval.s);
790 data.insertInset(doAccent(p), p->getTCode());
792 data.insertInset(p, p->getTCode());
793 for (int i = 0; p->setArgumentIdx(i); ++i) {
795 mathed_parse(ar, FLAG_BRACE|FLAG_BRACE_LAST);
800 MathedInset * q = new MathFuncInset(yylval.s, LM_OT_UNDEF);
802 data.insertInset(doAccent(q), LM_TC_INSET);
804 data.insertInset(q, LM_TC_INSET);
811 if (mathed_env != yylval.i && yylval.i != LM_OT_MATRIX)
812 mathPrintError("Unmatched environment");
813 // debug info [made that conditional -JMarc]
814 if (lyxerr.debugging(Debug::MATHED))
815 lyxerr << "[" << yylval.i << "]" << endl;
817 if (mt) { // && (flags & FLAG_END)) {
825 if (yylval.i == LM_OT_MATRIX) {
826 char ar[120], ar2[8];
827 ar[0] = ar2[0] = '\0';
828 char rg = LexGetArg(0);
830 strcpy(ar2, yytext.data());
833 strcpy(ar, yytext.data());
834 int const nc = parse_align(ar, ar2);
835 MathParInset * mm = new MathMatrixInset(nc, 0);
836 mm->SetAlign(ar2[0], ar);
837 data.insertInset(mm, LM_TC_ACTIVE_INSET);
838 mathed_parse(mm->GetData(), FLAG_END, &mm);
839 } else if (is_eqn_type(yylval.i)) {
841 mathPrintError("Misplaced environment");
845 mathPrintError("0 paragraph.");
849 mathed_env = static_cast<MathedInsetTypes>(yylval.i);
850 if (mathed_env != LM_OT_MIN) {
851 size = LM_ST_DISPLAY;
852 if (is_multiline(mathed_env)) {
854 if (is_multicolumn(mathed_env)) {
855 if (mathed_env != LM_OT_ALIGNAT &&
856 mathed_env != LM_OT_ALIGNATN &&
861 lyxerr << "Math parse error: unexpected '"
865 cols = strToInt(string(yytext.data()));
867 mt = create_multiline(mathed_env, cols);
870 // data.Insert(' ', LM_TC_TAB);
871 // data.Insert(' ', LM_TC_TAB);
875 mt->SetType(mathed_env);
876 crow = mt->getRowSt();
880 lyxerr << "MATH BEGIN[" << mathed_env << "]" << endl;
883 // lyxerr << "MATHCRO[" << yytext << "]";
885 MathMacroTable::mathMTable.getMacro(yytext.data());
887 data.insertInset(p, p->getTCode());
888 p->setArgumentIdx(0);
889 mathed_parse(p->GetData(), FLAG_END, reinterpret_cast<MathParInset**>(&p));
890 // for (int i = 0; p->setArgumentIdx(i); ++i)
891 // p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
893 mathPrintError("Unrecognized environment");
901 MathMacroTable::mathMTable.getMacro(yylval.l->name);
905 data.insertInset(doAccent(p), LM_TC_INSET);
907 data.insertInset(p, static_cast<MathMacro*>(p)->getTCode());
914 char const rg = LexGetArg('\0', true);
916 mathPrintError("Expected '{'");
918 lyxerr << "[" << yytext.data() << "]" << endl;
923 crow->setLabel(yytext.data());
925 mathed_label = yytext.data();
928 lyxerr << "Label[" << mathed_label << "]" << endl;
933 mathPrintError("Unrecognized token");
935 lyxerr << "[" << t << " " << yytext.data() << "]" << endl;
940 lyxerr << " Math Panic, expect problems!" << endl;
941 // Search for the end command.
944 } while (t != LM_TK_END && t);
948 if ((flags & FLAG_BRACE_OPT)/* && t!= '^' && t!= '_'*/) {
949 flags &= ~FLAG_BRACE_OPT;
950 //data.Insert (LM_TC_CLOSE);
958 void mathed_parser_file(istream & is, int lineno)
962 if (!MathMacroTable::built)
963 MathMacroTable::mathMTable.builtinMacros();
967 int mathed_parser_lineno()