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 "math_parser.h"
26 #include "math_parser.h"
27 #include "math_iter.h"
28 #include "math_inset.h"
29 #include "math_macro.h"
30 #include "math_root.h"
32 #include "support/lyxlib.h"
43 extern MathMatrixInset * create_multiline(short int type, int cols);
47 FLAG_BRACE = 1, // A { needed
48 FLAG_BRACE_ARG = 2, // Next { is argument
49 FLAG_BRACE_OPT = 4, // Optional {
50 FLAG_BRACE_LAST = 8, // Last } ends the parsing process
51 FLAG_BRACK_ARG = 16, // Optional [
52 FLAG_RIGHT = 32, // Next right ends the parsing process
53 FLAG_END = 64, // Next end ends the parsing process
54 FLAG_BRACE_FONT = 128, // Next } closes a font
55 FLAG_BRACK_END = 256 // Next ] ends the parsing process
63 MathedInsetTypes mathed_env = LM_OT_MIN;
68 int const latex_mathenv_num = 10;
69 char const * latex_mathenv[latex_mathenv_num] = {
83 char const * latex_mathspace[] = {
84 "!", ",", ":", ";", "quad", "qquad"
88 char const * latex_special_chars = "#$%&_{}";
90 // These are lexical codes, not semantic
96 LexBOP, // Binary operators or relations
108 static lexcode_enum lexcode[256];
109 #warning Replace with string
110 static char yytext[256];
112 static istream * yyis;
113 static bool yy_mtextmode= false;
116 void mathPrintError(string const & msg)
118 lyxerr << "Line ~" << yylineno << ": Math parse error: "
126 for (int i = 0; i <= 255; ++i) {
127 if (isalpha(i)) lexcode[i] = LexAlpha;
128 else if (isdigit(i)) lexcode[i] = LexDigit;
129 else if (isspace(i)) lexcode[i] = LexSpace;
130 else lexcode[i] = LexNone;
133 lexcode['\t'] = lexcode['\f'] = lexcode[' '] = LexSpace;
134 lexcode['\n'] = LexNewLine;
135 lexcode['%'] = LexComment;
136 lexcode['#'] = LexArgument;
137 lexcode['+'] = lexcode['-'] = lexcode['*'] = lexcode['/'] =
138 lexcode['<'] = lexcode['>'] = lexcode['='] = LexBOP;
140 lexcode['!'] = lexcode[','] = lexcode[':'] = lexcode[';'] = LexMathSpace;
142 lexcode['('] = lexcode[')'] = lexcode['|'] = lexcode['.'] =
143 lexcode['?'] = LexOther;
145 lexcode['\''] = lexcode['@'] = LexAlpha;
147 lexcode['['] = lexcode[']'] = lexcode['^'] = lexcode['_'] =
148 lexcode['&'] = LexSelf;
150 lexcode['\\'] = LexESC;
151 lexcode['{'] = LexOpen;
152 lexcode['}'] = LexClose;
157 char LexGetArg(char lf, bool accept_spaces= false)
161 while (yyis->good()) {
168 lyxerr << "Math parse error: unexpected '"
176 (lf == '{') ? '}' : ((lf == '[') ? ']' : ((lf == '(') ? ')' : 0));
178 lyxerr << "Math parse error: unknown bracket '"
179 << lf << "'" << endl;
182 char * p = &yytext[0];
189 if ((c > ' ' || (c == ' ' && accept_spaces)) && bcnt > 0) *(p++) = c;
190 } while (bcnt > 0 && yyis->good() && p-yytext < 255);
199 static int init_done = 0;
201 if (!init_done) LexInitCodes();
205 while (yyis->good()) {
209 if (yy_mtextmode && c == ' ') {
214 if (lexcode[c] == LexNewLine) {
219 if (lexcode[c] == LexComment) {
223 } while (c != '\n' % yyis->good()); // eat comments
226 if (lexcode[c] == LexDigit
227 || lexcode[c] == LexOther
228 || lexcode[c] == LexMathSpace) {
232 if (lexcode[c] == LexAlpha) {
236 if (lexcode[c] == LexBOP) {
240 if (lexcode[c] == LexSelf) {
243 if (lexcode[c] == LexArgument) {
247 return LM_TK_ARGUMENT;
249 if (lexcode[c] == LexOpen) {
252 if (lexcode[c] == LexClose) {
255 if (lexcode[c] == LexESC) {
259 return LM_TK_NEWLINE;
262 yylval.i = LM_OT_MIN;
266 yylval.i = LM_OT_MIN;
270 yylval.i = LM_OT_PAR;
274 yylval.i = LM_OT_PAR;
277 if (strchr(latex_special_chars, c)) {
279 return LM_TK_SPECIAL;
281 if (lexcode[c] == LexMathSpace) {
283 for (i = 0; i < 4 && static_cast<int>(c) != latex_mathspace[i][0]; ++i);
284 yylval.i = (i < 4) ? i : 0;
287 if (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
288 char * p = &yytext[0];
289 while ((lexcode[c] == LexAlpha || lexcode[c] == LexDigit)
297 if (yyis->good()) yyis->putback(c);
298 latexkeys * l = in_word_set (yytext, strlen(yytext));
300 if (l->token == LM_TK_BEGIN || l->token == LM_TK_END) {
303 // for (i = 0; i < 5 && strncmp(yytext, latex_mathenv[i],
304 // strlen(latex_mathenv[i])); ++i);
306 for (i = 0; i < latex_mathenv_num
307 && strcmp(yytext, latex_mathenv[i]); ++i);
310 if (l->token == LM_TK_SPACE)
327 int parse_align(char * hor, char *)
330 for (char * c = hor; c && *c > ' '; ++c) ++nc;
335 // Accent hacks only for 0.12. Stolen from Cursor.
342 void setAccent(int ac)
344 if (ac > 0 && accent < 8) {
345 nestaccent[accent++] = ac;
347 accent = 0; // consumed!
352 MathedInset * doAccent(byte c, MathedTextCodes t)
354 MathedInset * ac = 0;
356 for (int i = accent - 1; i >= 0; --i) {
358 ac = new MathAccentInset(c, t, nestaccent[i]);
360 ac = new MathAccentInset(ac, nestaccent[i]);
362 accent = 0; // consumed!
369 MathedInset * doAccent(MathedInset * p)
371 MathedInset * ac = 0;
373 for (int i = accent - 1; i >= 0; --i) {
375 ac = new MathAccentInset(p, nestaccent[i]);
377 ac = new MathAccentInset(ac, nestaccent[i]);
379 accent = 0; // consumed!
385 LyxArrayBase * mathed_parse(unsigned flags, LyxArrayBase * array,
391 static int plevel = -1;
392 static int size = LM_ST_TEXT;
393 MathedTextCodes varcode = LM_TC_VAR;
394 MathedInset * binset = 0;
395 static MathMacroTemplate * macro = 0;
400 MathParInset * mt = (mtx) ? *mtx : 0;//(MathParInset*)0;
401 MathedRowSt * crow = (mt) ? mt->getRowSt() : 0;
404 if (!array) array = new LyxArrayBase;
405 MathedIter data(array);
407 if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
408 if ((flags & FLAG_BRACK_ARG) && t == '[') {
411 mathPrintError("Expected {. Maybe you forgot to enclose an argument in {}");
416 MathedInsetTypes fractype = LM_OT_FRAC;
421 data.Insert(doAccent(yylval.i, varcode));
423 data.Insert (yylval.i, varcode); //LM_TC_VAR);
429 data.Insert(macro->getMacroPar(yylval.i-1), LM_TC_INSET);
433 case LM_TK_NEWCOMMAND:
438 string const name(&yytext[1]);
440 // ugly trick to be removed soon (lyx3)
441 char const c = yyis->peek();
444 na = lyx::atoi(yytext);
446 macro = new MathMacroTemplate(name, na);
447 flags = FLAG_BRACE|FLAG_BRACE_LAST;
449 macro->SetData(array);
454 data.Insert (yylval.i, LM_TC_SPECIAL);
460 data.Insert(doAccent(yylval.i, LM_TC_CONST));
462 data.Insert (yylval.i, LM_TC_CONST);
468 if (accent && tprev == LM_TK_ACCENT) {
469 acc_braces[acc_brace++] = brace;
472 if (flags & FLAG_BRACE_OPT) {
473 flags &= ~FLAG_BRACE_OPT;
477 if (flags & FLAG_BRACE)
478 flags &= ~FLAG_BRACE;
480 data.Insert ('{', LM_TC_TEX);
488 mathPrintError("Unmatching braces");
492 if (acc_brace && brace == acc_braces[acc_brace-1]-1) {
496 if (flags & FLAG_BRACE_FONT) {
498 yy_mtextmode = false;
499 flags &= ~FLAG_BRACE_FONT;
502 if (brace == 0 && (flags & FLAG_BRACE_LAST)) {
506 data.Insert ('}', LM_TC_TEX);
513 if (flags & FLAG_BRACK_ARG) {
514 flags &= ~FLAG_BRACK_ARG;
515 char const rg = LexGetArg('[');
517 mathPrintError("Expected ']'");
521 // if (arg) strcpy(arg, yytext);
528 if (flags & FLAG_BRACK_END) {
538 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
539 LyxArrayBase * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
541 // lyxerr << "UP[" << p->GetStyle() << "]" << endl;
542 data.Insert (p, LM_TC_UP);
547 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
548 LyxArrayBase * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
550 data.Insert (p, LM_TC_DOWN);
557 binset->SetLimits(bool(yylval.l->id));
565 if ((flags & FLAG_END) && mt && data.getCol()<mt->GetColumns() - 1) {
566 data.setNumCols(mt->GetColumns());
567 data.Insert('T', LM_TC_TAB);
569 mathPrintError("Unexpected tab");
570 // debug info. [made that conditional -JMarc]
571 if (lyxerr.debugging(Debug::MATHED))
572 lyxerr << data.getCol() << " " << mt->GetColumns() << endl;
577 if (mt && (flags & FLAG_END)) {
578 if (mt->Permit(LMPF_ALLOW_CR)) {
580 crow->setNext(new MathedRowSt(mt->GetColumns() + 1)); // this leaks
581 crow = crow->getNext();
583 data.Insert('K', LM_TC_CR);
585 mathPrintError("Unexpected newline");
591 binset = new MathBigopInset(yylval.l->name, yylval.l->id);
597 if (yylval.l->id < 256) {
598 MathedTextCodes tc = MathIsBOPS(yylval.l->id) ? LM_TC_BOPS: LM_TC_SYMB;
600 data.Insert(doAccent(yylval.l->id, tc));
602 data.Insert(yylval.l->id, tc);
604 MathFuncInset * bg = new MathFuncInset(yylval.l->name);
606 data.Insert(doAccent(bg));
608 data.Insert(bg, true);
615 data.Insert(doAccent(yylval.i, LM_TC_BOP));
617 data.Insert(yylval.i, LM_TC_BOP);
623 mt->UserSetSize(yylval.l->id);
630 MathSpaceInset * sp = new MathSpaceInset(yylval.i);
637 MathDotsInset * p = new MathDotsInset(yylval.l->name, yylval.l->id);
642 fractype = LM_OT_STACKREL;
645 MathFracInset * fc = new MathFracInset(fractype);
646 LyxArrayBase * num = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
647 LyxArrayBase * den = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
648 fc->SetData(num, den);
649 data.Insert(fc, LM_TC_ACTIVE_INSET);
656 char c; yyis->get(c);
659 rt = new MathRootInset(size);
660 rt->setArgumentIdx(0);
661 rt->SetData(mathed_parse(FLAG_BRACK_END, 0, &rt));
662 rt->setArgumentIdx(1);
665 rt = new MathSqrtInset(size);
667 rt->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST, 0, &rt));
668 data.Insert(rt, LM_TC_ACTIVE_INSET);
675 if (lfd == LM_TK_SYM || lfd == LM_TK_STR || lfd == LM_TK_BOP|| lfd == LM_TK_SPECIAL)
676 lfd = (lfd == LM_TK_SYM) ? yylval.l->id: yylval.i;
677 // lyxerr << "L[" << lfd << " " << lfd << "]";
678 LyxArrayBase * a = mathed_parse(FLAG_RIGHT);
680 // lyxerr << "R[" << rgd << "]";
681 if (rgd == LM_TK_SYM || rgd == LM_TK_STR || rgd == LM_TK_BOP || rgd == LM_TK_SPECIAL)
682 rgd = (rgd == LM_TK_SYM) ? yylval.l->id: yylval.i;
683 MathDelimInset * dl = new MathDelimInset(lfd, rgd);
685 data.Insert(dl, LM_TC_ACTIVE_INSET);
686 // lyxerr << "RL[" << lfd << " " << rgd << "]";
691 if (flags & FLAG_RIGHT) {
695 mathPrintError("Unmatched right delimiter");
703 varcode = static_cast<MathedTextCodes>(yylval.l->id);
704 yy_mtextmode = bool(varcode == LM_TC_TEXTRM);
705 flags |= (FLAG_BRACE|FLAG_BRACE_FONT);
710 MathDecorationInset * sq = new MathDecorationInset(yylval.l->id,
712 sq->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
713 data.Insert(sq, LM_TC_ACTIVE_INSET);
718 setAccent(yylval.l->id);
724 crow->setNumbered(false);
731 MathedInset * bg = new MathFuncInset(yylval.l->name);
740 data.Insert(new MathFuncInset(yylval.l->name, LM_OT_FUNCLIM));
747 MathMacroTable::mathMTable.getMacro(yylval.s);
750 data.Insert(doAccent(p), p->getTCode());
752 data.Insert(p, p->getTCode());
753 for (int i = 0; p->setArgumentIdx(i); ++i)
754 p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
757 MathedInset * q = new MathFuncInset(yylval.s, LM_OT_UNDEF);
759 data.Insert(doAccent(q));
768 if (mathed_env != yylval.i && yylval.i != LM_OT_MATRIX)
769 mathPrintError("Unmatched environment");
770 // debug info [made that conditional -JMarc]
771 if (lyxerr.debugging(Debug::MATHED))
772 lyxerr << "[" << yylval.i << "]" << endl;
774 if (mt) { // && (flags & FLAG_END)) {
782 if (yylval.i == LM_OT_MATRIX) {
783 char ar[120], ar2[8];
784 ar[0] = ar2[0] = '\0';
785 char rg = LexGetArg(0);
791 int const nc = parse_align(ar, ar2);
792 MathParInset * mm = new MathMatrixInset(nc, 0);
793 mm->SetAlign(ar2[0], ar);
794 data.Insert(mm, LM_TC_ACTIVE_INSET);
795 mathed_parse(FLAG_END, mm->GetData(), &mm);
796 } else if (is_eqn_type(yylval.i)) {
798 mathPrintError("Misplaced environment");
802 mathPrintError("0 paragraph.");
806 mathed_env = yylval.i;
807 if (mathed_env != LM_OT_MIN) {
808 size = LM_ST_DISPLAY;
809 if (is_multiline(mathed_env)) {
811 if (is_multicolumn(mathed_env)) {
813 cols = strToInt(string(yytext));
815 mt = create_multiline(mathed_env, cols);
818 // data.Insert(' ', LM_TC_TAB);
819 // data.Insert(' ', LM_TC_TAB);
823 mt->SetType(mathed_env);
824 crow = mt->getRowSt();
828 lyxerr << "MATH BEGIN[" << mathed_env << "]" << endl;
831 // lyxerr << "MATHCRO[" << yytext << "]";
833 MathMacroTable::mathMTable.getMacro(yytext);
835 data.Insert(p, p->getTCode());
836 p->setArgumentIdx(0);
837 mathed_parse(FLAG_END, p->GetData(), reinterpret_cast<MathParInset**>(&p));
838 // for (int i = 0; p->setArgumentIdx(i); ++i)
839 // p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
841 mathPrintError("Unrecognized environment");
849 MathMacroTable::mathMTable.getMacro(yylval.l->name);
853 data.Insert(doAccent(p));
855 data.Insert(p, static_cast<MathMacro*>(p)->getTCode());
862 char const rg = LexGetArg('\0', true);
864 mathPrintError("Expected '{'");
866 lyxerr << "[" << yytext << "]" << endl;
871 crow->setLabel(yytext);
873 mathed_label = yytext;
876 lyxerr << "Label[" << mathed_label << "]" << endl;
881 mathPrintError("Unrecognized token");
883 lyxerr << "[" << t << " " << yytext << "]" << endl;
888 lyxerr << " Math Panic, expect problems!" << endl;
889 // Search for the end command.
892 } while (t != LM_TK_END && t);
896 if ((flags & FLAG_BRACE_OPT)/* && t!= '^' && t!= '_'*/) {
897 flags &= ~FLAG_BRACE_OPT;
898 //data.Insert (LM_TC_CLOSE);
907 void mathed_parser_file(istream & is, int lineno)
911 if (!MathMacroTable::built)
912 MathMacroTable::mathMTable.builtinMacros();
916 int mathed_parser_lineno()