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"
37 FLAG_BRACE = 1, // A { needed
38 FLAG_BRACE_ARG = 2, // Next { is argument
39 FLAG_BRACE_OPT = 4, // Optional {
40 FLAG_BRACE_LAST = 8, // Last } ends the parsing process
41 FLAG_BRACK_ARG = 16, // Optional [
42 FLAG_RIGHT = 32, // Next right ends the parsing process
43 FLAG_END = 64, // Next end ends the parsing process
44 FLAG_BRACE_FONT = 128, // Next } closes a font
45 FLAG_BRACK_END = 256 // Next ] ends the parsing process
51 static short mathed_env = LM_EN_INTEXT;
53 char * mathed_label = 0;
55 char const * latex_mathenv[] = {
65 char const * latex_mathspace[] = {
66 "!", ",", ":", ";", "quad", "qquad"
69 char const * latex_special_chars = "#$%&_{}";
71 // These are lexical codes, not semantic
77 LexBOP, // Binary operators or relations
89 static lexcode_enum lexcode[256];
90 static char yytext[256];
92 static istream * yyis;
93 static bool yy_mtextmode= false;
96 char * strnew(char const * s)
98 char * s1 = new char[strlen(s) + 1]; // this leaks when not delete[]'ed
105 void mathPrintError(char const * msg)
107 lyxerr << "Line ~" << yylineno << ": Math parse error: "
115 for (int i = 0; i <= 255; ++i) {
116 if (isalpha(i)) lexcode[i] = LexAlpha;
117 else if (isdigit(i)) lexcode[i] = LexDigit;
118 else if (isspace(i)) lexcode[i] = LexSpace;
119 else lexcode[i] = LexNone;
122 lexcode['\t'] = lexcode['\f'] = lexcode[' '] = LexSpace;
123 lexcode['\n'] = LexNewLine;
124 lexcode['%'] = LexComment;
125 lexcode['#'] = LexArgument;
126 lexcode['+'] = lexcode['-'] = lexcode['*'] = lexcode['/'] =
127 lexcode['<'] = lexcode['>'] = lexcode['='] = LexBOP;
129 lexcode['!'] = lexcode[','] = lexcode[':'] = lexcode[';'] = LexMathSpace;
130 lexcode['('] = lexcode[')'] = lexcode['|'] = lexcode['.'] = lexcode['?'] = LexOther;
131 lexcode['\'']= LexAlpha;
133 lexcode['['] = lexcode[']'] = lexcode['^'] = lexcode['_'] =
134 lexcode['&'] = LexSelf;
136 lexcode['\\'] = LexESC;
137 lexcode['{'] = LexOpen;
138 lexcode['}'] = LexClose;
143 char LexGetArg(char lf, bool accept_spaces= false)
146 char * p = &yytext[0];
150 while (yyis->good()) {
154 if (!lf) lf = c; else
156 lyxerr << "Math parse error: unexpected '"
161 rg = (lf == '{') ? '}': ((lf == '[') ? ']': ((lf == '(') ? ')': 0));
163 lyxerr << "Math parse error: unknown bracket '"
164 << lf << "'" << endl;
172 if ((c > ' ' || (c == ' ' && accept_spaces)) && bcnt>0) *(p++) = c;
173 } while (bcnt > 0 && yyis->good());
182 static int init_done = 0;
186 if (!init_done) LexInitCodes();
188 while (yyis->good()) {
192 if (yy_mtextmode && c == ' ') {
197 if (lexcode[c] == LexNewLine) {
202 if (lexcode[c] == LexComment)
203 do { yyis->get(cc); c = cc; } while (c != '\n' % yyis->good()); // eat comments
205 if (lexcode[c] == LexDigit || lexcode[c] == LexOther || lexcode[c] == LexMathSpace) { yylval.i = c; return LM_TK_STR; }
206 if (lexcode[c] == LexAlpha) { yylval.i= c; return LM_TK_ALPHA; }
207 if (lexcode[c] == LexBOP) { yylval.i= c; return LM_TK_BOP; }
208 if (lexcode[c] == LexSelf) { return c; }
209 if (lexcode[c] == LexArgument) {
213 return LM_TK_ARGUMENT;
215 if (lexcode[c] == LexOpen) { return LM_TK_OPEN; }
216 if (lexcode[c] == LexClose) { return LM_TK_CLOSE; }
218 if (lexcode[c] == LexESC) {
221 if (c == '\\') { return LM_TK_NEWLINE; }
222 if (c == '(') { yylval.i = LM_EN_INTEXT; return LM_TK_BEGIN; }
223 if (c == ')') { yylval.i = LM_EN_INTEXT; return LM_TK_END; }
224 if (c == '[') { yylval.i = LM_EN_DISPLAY; return LM_TK_BEGIN; }
225 if (c == ']') { yylval.i = LM_EN_DISPLAY; return LM_TK_END; }
226 if (strchr(latex_special_chars, c)) {
228 return LM_TK_SPECIAL;
230 if (lexcode[c] == LexMathSpace) {
232 for (i = 0; i < 4 && static_cast<int>(c) != latex_mathspace[i][0]; ++i);
233 yylval.i = (i < 4) ? i: 0;
236 if (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
237 char * p = &yytext[0];
238 while (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
245 if (yyis->good()) yyis->putback(c);
246 latexkeys * l = in_word_set (yytext, strlen(yytext));
248 if (l->token == LM_TK_BEGIN || l->token == LM_TK_END) {
251 // for (i = 0; i < 5 && strncmp(yytext, latex_mathenv[i],
252 // strlen(latex_mathenv[i])); ++i);
254 for (i = 0; i < 6 && strcmp(yytext, latex_mathenv[i]); ++i);
257 if (l->token == LM_TK_SPACE)
273 int parse_align(char * hor, char *)
276 for (char * c = hor; c && *c > ' '; ++c) ++nc;
281 // Accent hacks only for 0.12. Stolen from Cursor.
285 void setAccent(int ac)
287 if (ac > 0 && accent < 8) {
288 nestaccent[accent++] = ac;
290 accent = 0; // consumed!
294 MathedInset * doAccent(byte c, MathedTextCodes t)
296 MathedInset * ac = 0;
298 for (int i = accent - 1; i >= 0; --i) {
300 ac = new MathAccentInset(c, t, nestaccent[i]);
302 ac = new MathAccentInset(ac, nestaccent[i]);
304 accent = 0; // consumed!
310 MathedInset * doAccent(MathedInset * p)
312 MathedInset * ac = 0;
314 for (int i = accent - 1; i >= 0; --i) {
316 ac = new MathAccentInset(p, nestaccent[i]);
318 ac = new MathAccentInset(ac, nestaccent[i]);
320 accent = 0; // consumed!
326 LyxArrayBase * mathed_parse(unsigned flags, LyxArrayBase * array,
329 int t = yylex(), tprev = 0;
331 static int plevel = -1;
332 static int size = LM_ST_TEXT;
333 MathedTextCodes varcode = LM_TC_VAR;
334 MathedInset * binset = 0;
335 static MathMacroTemplate * macro= 0;
340 MathParInset * mt = (mtx) ? *mtx : 0;//(MathParInset*)0;
341 MathedRowSt * crow = (mt) ? mt->getRowSt() : 0;
344 if (!array) array = new LyxArrayBase;
345 MathedIter data(array);
347 if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
348 if ((flags & FLAG_BRACK_ARG) && t == '[') {
351 mathPrintError("Expected {. Maybe you forgot to enclose an argument in {}");
356 MathedInsetTypes fractype = LM_OT_FRAC;
361 data.Insert(doAccent(yylval.i, varcode));
363 data.Insert (yylval.i, varcode); //LM_TC_VAR);
369 data.Insert(macro->getMacroPar(yylval.i-1), LM_TC_INSET);
373 case LM_TK_NEWCOMMAND:
378 // This name lives until quitting, for that reason
379 // I didn't care on deleting explicitly. Later I will.
380 char const * name = strnew(&yytext[1]);
381 // ugly trick to be removed soon (lyx3)
382 char c; yyis->get(c);
388 macro = new MathMacroTemplate(name, na);
389 flags = FLAG_BRACE|FLAG_BRACE_LAST;
391 macro->SetData(array);
396 data.Insert (yylval.i, LM_TC_SPECIAL);
402 data.Insert(doAccent(yylval.i, LM_TC_CONST));
404 data.Insert (yylval.i, LM_TC_CONST);
410 if (accent && tprev == LM_TK_ACCENT) {
411 acc_braces[acc_brace++] = brace;
414 if (flags & FLAG_BRACE_OPT) {
415 flags &= ~FLAG_BRACE_OPT;
419 if (flags & FLAG_BRACE)
420 flags &= ~FLAG_BRACE;
422 data.Insert ('{', LM_TC_TEX);
430 mathPrintError("Unmatching braces");
434 if (acc_brace && brace == acc_braces[acc_brace-1]-1) {
438 if (flags & FLAG_BRACE_FONT) {
440 yy_mtextmode = false;
441 flags &= ~FLAG_BRACE_FONT;
444 if (brace == 0 && (flags & FLAG_BRACE_LAST)) {
448 data.Insert ('}', LM_TC_TEX);
455 if (flags & FLAG_BRACK_ARG) {
456 flags &= ~FLAG_BRACK_ARG;
457 char rg = LexGetArg('[');
459 mathPrintError("Expected ']'");
463 // if (arg) strcpy(arg, yytext);
470 if (flags & FLAG_BRACK_END) {
480 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
481 LyxArrayBase * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
483 // lyxerr << "UP[" << p->GetStyle() << "]" << endl;
484 data.Insert (p, LM_TC_UP);
489 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
490 LyxArrayBase * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
492 data.Insert (p, LM_TC_DOWN);
499 binset->SetLimits(bool(yylval.l->id));
507 if ((flags & FLAG_END) && mt && data.getCol()<mt->GetColumns()-1) {
508 data.setNumCols(mt->GetColumns());
509 data.Insert('T', LM_TC_TAB);
511 mathPrintError("Unexpected tab");
512 // debug info. [made that conditional -JMarc]
513 if (lyxerr.debugging(Debug::MATHED))
514 lyxerr << data.getCol() << " " << mt->GetColumns() << endl;
519 if (mt && (flags & FLAG_END)) {
520 if (mt->Permit(LMPF_ALLOW_CR)) {
522 crow->setNext(new MathedRowSt(mt->GetColumns()+1)); // this leaks
523 crow = crow->getNext();
525 data.Insert('K', LM_TC_CR);
527 mathPrintError("Unexpected newline");
533 binset = new MathBigopInset(yylval.l->name, yylval.l->id);
539 if (yylval.l->id < 256) {
540 MathedTextCodes tc = MathIsBOPS(yylval.l->id) ? LM_TC_BOPS: LM_TC_SYMB;
542 data.Insert(doAccent(yylval.l->id, tc));
544 data.Insert (yylval.l->id, tc);
546 MathFuncInset * bg = new MathFuncInset(yylval.l->name);
548 data.Insert(doAccent(bg));
550 data.Insert(bg, true);
557 data.Insert(doAccent(yylval.i, LM_TC_BOP));
559 data.Insert (yylval.i, LM_TC_BOP);
565 mt->UserSetSize(yylval.l->id);
572 MathSpaceInset * sp = new MathSpaceInset(yylval.i);
579 MathDotsInset * p = new MathDotsInset(yylval.l->name, yylval.l->id);
584 fractype = LM_OT_STACKREL;
587 MathFracInset * fc = new MathFracInset(fractype);
588 LyxArrayBase * num = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
589 LyxArrayBase * den = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
590 fc->SetData(num, den);
591 data.Insert(fc, LM_TC_ACTIVE_INSET);
598 char c; yyis->get(c);
601 rt = new MathRootInset(size);
602 rt->setArgumentIdx(0);
603 rt->SetData(mathed_parse(FLAG_BRACK_END, 0, &rt));
604 rt->setArgumentIdx(1);
607 rt = new MathSqrtInset(size);
609 rt->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST, 0, &rt));
610 data.Insert(rt, LM_TC_ACTIVE_INSET);
618 if (lfd == LM_TK_SYM || lfd == LM_TK_STR || lfd == LM_TK_BOP|| lfd == LM_TK_SPECIAL)
619 lfd = (lfd == LM_TK_SYM) ? yylval.l->id: yylval.i;
620 // lyxerr << "L[" << lfd << " " << lfd << "]";
621 LyxArrayBase * a = mathed_parse(FLAG_RIGHT);
623 // lyxerr << "R[" << rgd << "]";
624 if (rgd == LM_TK_SYM || rgd == LM_TK_STR || rgd == LM_TK_BOP || rgd == LM_TK_SPECIAL)
625 rgd = (rgd == LM_TK_SYM) ? yylval.l->id: yylval.i;
626 MathDelimInset * dl = new MathDelimInset(lfd, rgd);
628 data.Insert(dl, LM_TC_ACTIVE_INSET);
629 // lyxerr << "RL[" << lfd << " " << rgd << "]";
634 if (flags & FLAG_RIGHT) {
638 mathPrintError("Unmatched right delimiter");
646 varcode = static_cast<MathedTextCodes>(yylval.l->id);
647 yy_mtextmode = bool(varcode == LM_TC_TEXTRM);
648 flags |= (FLAG_BRACE|FLAG_BRACE_FONT);
653 MathDecorationInset * sq = new MathDecorationInset(yylval.l->id,
655 sq->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
656 data.Insert(sq, LM_TC_ACTIVE_INSET);
660 case LM_TK_ACCENT: setAccent(yylval.l->id); break;
665 crow->setNumbered(false);
672 MathedInset * bg = new MathFuncInset(yylval.l->name);
681 data.Insert(new MathFuncInset(yylval.l->name, LM_OT_FUNCLIM));
688 MathMacroTable::mathMTable.getMacro(yylval.s);
691 data.Insert(doAccent(p), p->getTCode());
693 data.Insert(p, p->getTCode());
694 for (int i = 0; p->setArgumentIdx(i); ++i)
695 p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
698 MathedInset * q = new MathFuncInset(yylval.s, LM_OT_UNDEF);
700 data.Insert(doAccent(q));
709 if (mathed_env != yylval.i && yylval.i!= LM_EN_ARRAY)
710 mathPrintError("Unmatched environment");
711 // debug info [made that conditional -JMarc]
712 if (lyxerr.debugging(Debug::MATHED))
713 lyxerr << "[" << yylval.i << "]" << endl;
715 if (mt) { // && (flags & FLAG_END)) {
723 if (yylval.i == LM_EN_ARRAY) {
724 char ar[120], ar2[8];
725 ar[0] = ar2[0] = '\0';
726 char rg = LexGetArg(0);
732 int nc = parse_align(ar, ar2);
733 MathParInset * mm = new MathMatrixInset(nc, 0);
734 mm->SetAlign(ar2[0], ar);
735 data.Insert(mm, LM_TC_ACTIVE_INSET);
736 mathed_parse(FLAG_END, mm->GetData(), &mm);
738 if (yylval.i >= LM_EN_INTEXT && yylval.i<= LM_EN_EQNARRAY) {
740 mathPrintError("Misplaced environment");
744 mathPrintError("0 paragraph.");
748 mathed_env = yylval.i;
749 if (mathed_env>= LM_EN_DISPLAY) {
750 size = LM_ST_DISPLAY;
751 if (mathed_env>LM_EN_EQUATION) {
752 mt = new MathMatrixInset(3, -1);
753 mt->SetAlign(' ', "rcl");
756 // data.Insert(' ', LM_TC_TAB);
757 // data.Insert(' ', LM_TC_TAB);
761 mt->SetType(mathed_env);
762 crow = mt->getRowSt();
766 lyxerr << "MATH BEGIN[" << mathed_env << "]" << endl;
769 // lyxerr << "MATHCRO[" << yytext << "]";
771 MathMacroTable::mathMTable.getMacro(yytext);
773 data.Insert(p, p->getTCode());
774 p->setArgumentIdx(0);
775 mathed_parse(FLAG_END, p->GetData(), reinterpret_cast<MathParInset**>(&p));
776 // for (int i = 0; p->setArgumentIdx(i); ++i)
777 // p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
779 mathPrintError("Unrecognized environment");
787 MathMacroTable::mathMTable.getMacro(yylval.l->name);
791 data.Insert(doAccent(p));
793 data.Insert(p, static_cast<MathMacro*>(p)->getTCode());
800 char rg = LexGetArg('\0', true);
802 mathPrintError("Expected '{'");
804 lyxerr << "[" << yytext << "]" << endl;
809 // This is removed by crow's destructor. Bad design? yes, this
810 // will be changed after 0.12
811 crow->setLabel(strnew(yytext));
814 // where is this math_label free'ed?
815 // Supposedly in ~formula, another bad hack,
816 // give me some time please.
817 mathed_label = strnew(yytext);
820 lyxerr << "Label[" << mathed_label << "]" << endl;
825 mathPrintError("Unrecognized token");
827 lyxerr << "[" << t << " " << yytext << "]" << endl;
832 lyxerr << " Math Panic, expect problems!" << endl;
833 // Search for the end command.
834 do t = yylex (); while (t != LM_TK_END && t);
838 if ((flags & FLAG_BRACE_OPT)/* && t!= '^' && t!= '_'*/) {
839 flags &= ~FLAG_BRACE_OPT;
840 //data.Insert (LM_TC_CLOSE);
849 void mathed_parser_file(istream & is, int lineno)
853 if (!MathMacroTable::built)
854 MathMacroTable::mathMTable.builtinMacros();
858 int mathed_parser_lineno()