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: (c) 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"
34 FLAG_BRACE = 1, // A { needed
35 FLAG_BRACE_ARG = 2, // Next { is argument
36 FLAG_BRACE_OPT = 4, // Optional {
37 FLAG_BRACE_LAST = 8, // Last } ends the parsing process
38 FLAG_BRACK_ARG = 16, // Optional [
39 FLAG_RIGHT = 32, // Next right ends the parsing process
40 FLAG_END = 64, // Next end ends the parsing process
41 FLAG_BRACE_FONT = 128, // Next } closes a font
42 FLAG_BRACK_END = 256 // Next ] ends the parsing process
48 static short mathed_env = LM_EN_INTEXT;
50 char * mathed_label = 0;
52 char const * latex_mathenv[] = {
62 char const * latex_mathspace[] = {
63 "!", ",", ":", ";", "quad", "qquad"
66 char const * latex_special_chars = "#$%&_{}";
68 // These are lexical codes, not semantic
74 LexBOP, // Binary operators or relations
86 static lexcode_enum lexcode[256];
87 static char yytext[256];
89 static istream * yyis;
90 static bool yy_mtextmode= false;
93 char * strnew(char const * s)
95 char * s1 = new char[strlen(s) + 1]; // this leaks when not delete[]'ed
101 static void mathPrintError(char const * msg)
103 lyxerr << "Line ~" << yylineno << ": Math parse error: "
108 static void LexInitCodes()
110 for (int i = 0; i <= 255; ++i) {
111 if (isalpha(i)) lexcode[i] = LexAlpha;
112 else if (isdigit(i)) lexcode[i] = LexDigit;
113 else if (isspace(i)) lexcode[i] = LexSpace;
114 else lexcode[i] = LexNone;
117 lexcode['\t'] = lexcode['\f'] = lexcode[' '] = LexSpace;
118 lexcode['\n'] = LexNewLine;
119 lexcode['%'] = LexComment;
120 lexcode['#'] = LexArgument;
121 lexcode['+'] = lexcode['-'] = lexcode['*'] = lexcode['/'] =
122 lexcode['<'] = lexcode['>'] = lexcode['='] = LexBOP;
124 lexcode['!'] = lexcode[','] = lexcode[':'] = lexcode[';'] = LexMathSpace;
125 lexcode['('] = lexcode[')'] = lexcode['|'] = lexcode['.'] = lexcode['?'] = LexOther;
126 lexcode['\'']= LexAlpha;
128 lexcode['['] = lexcode[']'] = lexcode['^'] = lexcode['_'] =
129 lexcode['&'] = LexSelf;
131 lexcode['\\'] = LexESC;
132 lexcode['{'] = LexOpen;
133 lexcode['}'] = LexClose;
137 static char LexGetArg(char lf, bool accept_spaces= false)
140 char * p = &yytext[0];
144 while (yyis->good()) {
148 if (!lf) lf = c; else
150 lyxerr << "Math parse error: unexpected '"
155 rg = (lf == '{') ? '}': ((lf == '[') ? ']': ((lf == '(') ? ')': 0));
157 lyxerr << "Math parse error: unknown bracket '"
158 << lf << "'" << endl;
166 if ((c > ' ' || (c == ' ' && accept_spaces)) && bcnt>0) *(p++) = c;
167 } while (bcnt > 0 && yyis->good());
173 static int yylex(void)
175 static int init_done = 0;
179 if (!init_done) LexInitCodes();
181 while (yyis->good()) {
185 if (yy_mtextmode && c == ' ') {
190 if (lexcode[c] == LexNewLine) {
195 if (lexcode[c] == LexComment)
196 do { yyis->get(cc); c = cc; } while (c != '\n' % yyis->good()); // eat comments
198 if (lexcode[c] == LexDigit || lexcode[c] == LexOther || lexcode[c] == LexMathSpace) { yylval.i = c; return LM_TK_STR; }
199 if (lexcode[c] == LexAlpha) { yylval.i= c; return LM_TK_ALPHA; }
200 if (lexcode[c] == LexBOP) { yylval.i= c; return LM_TK_BOP; }
201 if (lexcode[c] == LexSelf) { return c; }
202 if (lexcode[c] == LexArgument) {
206 return LM_TK_ARGUMENT;
208 if (lexcode[c] == LexOpen) { return LM_TK_OPEN; }
209 if (lexcode[c] == LexClose) { return LM_TK_CLOSE; }
211 if (lexcode[c] == LexESC) {
214 if (c == '\\') { return LM_TK_NEWLINE; }
215 if (c == '(') { yylval.i = LM_EN_INTEXT; return LM_TK_BEGIN; }
216 if (c == ')') { yylval.i = LM_EN_INTEXT; return LM_TK_END; }
217 if (c == '[') { yylval.i = LM_EN_DISPLAY; return LM_TK_BEGIN; }
218 if (c == ']') { yylval.i = LM_EN_DISPLAY; return LM_TK_END; }
219 if (strchr(latex_special_chars, c)) {
221 return LM_TK_SPECIAL;
223 if (lexcode[c] == LexMathSpace) {
225 for (i = 0; i < 4 && static_cast<int>(c) != latex_mathspace[i][0]; ++i);
226 yylval.i = (i < 4) ? i: 0;
229 if (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
230 char * p = &yytext[0];
231 while (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
238 if (yyis->good()) yyis->putback(c);
239 latexkeys * l = in_word_set (yytext, strlen(yytext));
241 if (l->token == LM_TK_BEGIN || l->token == LM_TK_END) {
244 // for (i = 0; i < 5 && strncmp(yytext, latex_mathenv[i],
245 // strlen(latex_mathenv[i])); ++i);
247 for (i = 0; i < 6 && strcmp(yytext, latex_mathenv[i]); ++i);
250 if (l->token == LM_TK_SPACE)
266 int parse_align(char * hor, char *)
269 for (char * c = hor; c && *c > ' '; ++c) ++nc;
274 // Accent hacks only for 0.12. Stolen from Cursor.
278 void setAccent(int ac)
280 if (ac > 0 && accent < 8) {
281 nestaccent[accent++] = ac;
283 accent = 0; // consumed!
287 MathedInset * doAccent(byte c, MathedTextCodes t)
289 MathedInset * ac = 0;
291 for (int i = accent - 1; i >= 0; --i) {
293 ac = new MathAccentInset(c, t, nestaccent[i]);
295 ac = new MathAccentInset(ac, nestaccent[i]);
297 accent = 0; // consumed!
303 MathedInset * doAccent(MathedInset * p)
305 MathedInset * ac = 0;
307 for (int i = accent - 1; i >= 0; --i) {
309 ac = new MathAccentInset(p, nestaccent[i]);
311 ac = new MathAccentInset(ac, nestaccent[i]);
313 accent = 0; // consumed!
319 LyxArrayBase * mathed_parse(unsigned flags, LyxArrayBase * array,
322 int t = yylex(), tprev = 0;
324 static int plevel = -1;
325 static int size = LM_ST_TEXT;
326 MathedTextCodes varcode = LM_TC_VAR;
327 MathedInset * binset = 0;
328 static MathMacroTemplate * macro= 0;
333 MathParInset * mt = (mtx) ? *mtx: 0;//(MathParInset*)0;
334 MathedRowSt * crow = (mt) ? mt->getRowSt() : 0;
337 if (!array) array = new LyxArrayBase;
338 MathedIter data(array);
340 if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
341 if ((flags & FLAG_BRACK_ARG) && t == '[') {
344 mathPrintError("Expected {. Maybe you forgot to enclose an argument in {}");
349 MathedInsetTypes fractype = LM_OT_FRAC;
354 data.Insert(doAccent(yylval.i, varcode));
356 data.Insert (yylval.i, varcode); //LM_TC_VAR);
362 data.Insert(macro->getMacroPar(yylval.i-1), LM_TC_INSET);
366 case LM_TK_NEWCOMMAND:
371 // This name lives until quitting, for that reason
372 // I didn't care on deleting explicitly. Later I will.
373 char const * name = strnew(&yytext[1]);
374 // ugly trick to be removed soon (lyx3)
375 char c; yyis->get(c);
381 macro = new MathMacroTemplate(name, na);
382 flags = FLAG_BRACE|FLAG_BRACE_LAST;
384 macro->SetData(array);
389 data.Insert (yylval.i, LM_TC_SPECIAL);
395 data.Insert(doAccent(yylval.i, LM_TC_CONST));
397 data.Insert (yylval.i, LM_TC_CONST);
403 if (accent && tprev == LM_TK_ACCENT) {
404 acc_braces[acc_brace++] = brace;
407 if (flags & FLAG_BRACE_OPT) {
408 flags &= ~FLAG_BRACE_OPT;
412 if (flags & FLAG_BRACE)
413 flags &= ~FLAG_BRACE;
415 data.Insert ('{', LM_TC_TEX);
423 mathPrintError("Unmatching braces");
427 if (acc_brace && brace == acc_braces[acc_brace-1]-1) {
431 if (flags & FLAG_BRACE_FONT) {
433 yy_mtextmode = false;
434 flags &= ~FLAG_BRACE_FONT;
437 if (brace == 0 && (flags & FLAG_BRACE_LAST)) {
441 data.Insert ('}', LM_TC_TEX);
448 if (flags & FLAG_BRACK_ARG) {
449 flags &= ~FLAG_BRACK_ARG;
450 char rg = LexGetArg('[');
452 mathPrintError("Expected ']'");
456 // if (arg) strcpy(arg, yytext);
463 if (flags & FLAG_BRACK_END) {
473 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
474 LyxArrayBase * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
476 // lyxerr << "UP[" << p->GetStyle() << "]" << endl;
477 data.Insert (p, LM_TC_UP);
482 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
483 LyxArrayBase * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
485 data.Insert (p, LM_TC_DOWN);
492 binset->SetLimits(bool(yylval.l->id));
500 if ((flags & FLAG_END) && mt && data.getCol()<mt->GetColumns()-1) {
501 data.setNumCols(mt->GetColumns());
502 data.Insert('T', LM_TC_TAB);
504 mathPrintError("Unexpected tab");
505 // debug info. [made that conditional -JMarc]
506 if (lyxerr.debugging(Debug::MATHED))
507 lyxerr << data.getCol() << " " << mt->GetColumns() << endl;
512 if (mt && (flags & FLAG_END)) {
513 if (mt->Permit(LMPF_ALLOW_CR)) {
515 crow->setNext(new MathedRowSt(mt->GetColumns()+1)); // this leaks
516 crow = crow->getNext();
518 data.Insert('K', LM_TC_CR);
520 mathPrintError("Unexpected newline");
526 binset = new MathBigopInset(yylval.l->name, yylval.l->id);
532 if (yylval.l->id < 256) {
533 MathedTextCodes tc = MathIsBOPS(yylval.l->id) ? LM_TC_BOPS: LM_TC_SYMB;
535 data.Insert(doAccent(yylval.l->id, tc));
537 data.Insert (yylval.l->id, tc);
539 MathFuncInset * bg = new MathFuncInset(yylval.l->name);
541 data.Insert(doAccent(bg));
543 data.Insert(bg, true);
550 data.Insert(doAccent(yylval.i, LM_TC_BOP));
552 data.Insert (yylval.i, LM_TC_BOP);
558 mt->UserSetSize(yylval.l->id);
565 MathSpaceInset * sp = new MathSpaceInset(yylval.i);
572 MathDotsInset * p = new MathDotsInset(yylval.l->name, yylval.l->id);
577 fractype = LM_OT_STACKREL;
580 MathFracInset * fc = new MathFracInset(fractype);
581 LyxArrayBase * num = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
582 LyxArrayBase * den = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
583 fc->SetData(num, den);
584 data.Insert(fc, LM_TC_ACTIVE_INSET);
591 char c; yyis->get(c);
594 rt = new MathRootInset(size);
595 rt->setArgumentIdx(0);
596 rt->SetData(mathed_parse(FLAG_BRACK_END, 0, &rt));
597 rt->setArgumentIdx(1);
600 rt = new MathSqrtInset(size);
602 rt->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST, 0, &rt));
603 data.Insert(rt, LM_TC_ACTIVE_INSET);
611 if (lfd == LM_TK_SYM || lfd == LM_TK_STR || lfd == LM_TK_BOP|| lfd == LM_TK_SPECIAL)
612 lfd = (lfd == LM_TK_SYM) ? yylval.l->id: yylval.i;
613 // lyxerr << "L[" << lfd << " " << lfd << "]";
614 LyxArrayBase * a = mathed_parse(FLAG_RIGHT);
616 // lyxerr << "R[" << rgd << "]";
617 if (rgd == LM_TK_SYM || rgd == LM_TK_STR || rgd == LM_TK_BOP || rgd == LM_TK_SPECIAL)
618 rgd = (rgd == LM_TK_SYM) ? yylval.l->id: yylval.i;
619 MathDelimInset * dl = new MathDelimInset(lfd, rgd);
621 data.Insert(dl, LM_TC_ACTIVE_INSET);
622 // lyxerr << "RL[" << lfd << " " << rgd << "]";
627 if (flags & FLAG_RIGHT) {
631 mathPrintError("Unmatched right delimiter");
639 varcode = static_cast<MathedTextCodes>(yylval.l->id);
640 yy_mtextmode = bool(varcode == LM_TC_TEXTRM);
641 flags |= (FLAG_BRACE|FLAG_BRACE_FONT);
646 MathDecorationInset * sq = new MathDecorationInset(yylval.l->id,
648 sq->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
649 data.Insert(sq, LM_TC_ACTIVE_INSET);
653 case LM_TK_ACCENT: setAccent(yylval.l->id); break;
658 crow->setNumbered(false);
665 MathedInset * bg = new MathFuncInset(yylval.l->name);
674 data.Insert(new MathFuncInset(yylval.l->name, LM_OT_FUNCLIM));
681 MathMacroTable::mathMTable.getMacro(yylval.s);
684 data.Insert(doAccent(p), p->getTCode());
686 data.Insert(p, p->getTCode());
687 for (int i = 0; p->setArgumentIdx(i); ++i)
688 p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
691 MathedInset * q = new MathFuncInset(yylval.s, LM_OT_UNDEF);
693 data.Insert(doAccent(q));
702 if (mathed_env != yylval.i && yylval.i!= LM_EN_ARRAY)
703 mathPrintError("Unmatched environment");
704 // debug info [made that conditional -JMarc]
705 if (lyxerr.debugging(Debug::MATHED))
706 lyxerr << "[" << yylval.i << "]" << endl;
708 if (mt) { // && (flags & FLAG_END)) {
716 if (yylval.i == LM_EN_ARRAY) {
717 char ar[120], ar2[8];
718 ar[0] = ar2[0] = '\0';
719 char rg = LexGetArg(0);
725 int nc = parse_align(ar, ar2);
726 MathParInset * mm = new MathMatrixInset(nc, 0);
727 mm->SetAlign(ar2[0], ar);
728 data.Insert(mm, LM_TC_ACTIVE_INSET);
729 mathed_parse(FLAG_END, mm->GetData(), &mm);
731 if (yylval.i >= LM_EN_INTEXT && yylval.i<= LM_EN_EQNARRAY) {
733 mathPrintError("Misplaced environment");
737 mathPrintError("0 paragraph.");
741 mathed_env = yylval.i;
742 if (mathed_env>= LM_EN_DISPLAY) {
743 size = LM_ST_DISPLAY;
744 if (mathed_env>LM_EN_EQUATION) {
745 mt = new MathMatrixInset(3, -1);
746 mt->SetAlign(' ', "rcl");
749 // data.Insert(' ', LM_TC_TAB);
750 // data.Insert(' ', LM_TC_TAB);
754 mt->SetType(mathed_env);
755 crow = mt->getRowSt();
759 lyxerr << "MATH BEGIN[" << mathed_env << "]" << endl;
762 // lyxerr << "MATHCRO[" << yytext << "]";
764 MathMacroTable::mathMTable.getMacro(yytext);
766 data.Insert(p, p->getTCode());
767 p->setArgumentIdx(0);
768 mathed_parse(FLAG_END, p->GetData(), reinterpret_cast<MathParInset**>(&p));
769 // for (int i = 0; p->setArgumentIdx(i); ++i)
770 // p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
772 mathPrintError("Unrecognized environment");
780 MathMacroTable::mathMTable.getMacro(yylval.l->name);
784 data.Insert(doAccent(p));
786 data.Insert(p, static_cast<MathMacro*>(p)->getTCode());
793 char rg = LexGetArg('\0', true);
795 mathPrintError("Expected '{'");
797 lyxerr << "[" << yytext << "]" << endl;
802 // This is removed by crow's destructor. Bad design? yes, this
803 // will be changed after 0.12
804 crow->setLabel(strnew(yytext));
807 // where is this math_label free'ed?
808 // Supposedly in ~formula, another bad hack,
809 // give me some time please.
810 mathed_label = strnew(yytext);
813 lyxerr << "Label[" << mathed_label << "]" << endl;
818 mathPrintError("Unrecognized token");
820 lyxerr << "[" << t << " " << yytext << "]" << endl;
825 lyxerr << " Math Panic, expect problems!" << endl;
826 // Search for the end command.
827 do t = yylex (); while (t != LM_TK_END && t);
831 if ((flags & FLAG_BRACE_OPT)/* && t!= '^' && t!= '_'*/) {
832 flags &= ~FLAG_BRACE_OPT;
833 //data.Insert (LM_TC_CLOSE);
842 void mathed_parser_file(istream & is, int lineno)
846 if (!MathMacroTable::built)
847 MathMacroTable::mathMTable.builtinMacros();
851 int mathed_parser_lineno()