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
102 void mathPrintError(char const * msg)
104 lyxerr << "Line ~" << yylineno << ": Math parse error: "
112 for (int i = 0; i <= 255; ++i) {
113 if (isalpha(i)) lexcode[i] = LexAlpha;
114 else if (isdigit(i)) lexcode[i] = LexDigit;
115 else if (isspace(i)) lexcode[i] = LexSpace;
116 else lexcode[i] = LexNone;
119 lexcode['\t'] = lexcode['\f'] = lexcode[' '] = LexSpace;
120 lexcode['\n'] = LexNewLine;
121 lexcode['%'] = LexComment;
122 lexcode['#'] = LexArgument;
123 lexcode['+'] = lexcode['-'] = lexcode['*'] = lexcode['/'] =
124 lexcode['<'] = lexcode['>'] = lexcode['='] = LexBOP;
126 lexcode['!'] = lexcode[','] = lexcode[':'] = lexcode[';'] = LexMathSpace;
127 lexcode['('] = lexcode[')'] = lexcode['|'] = lexcode['.'] = lexcode['?'] = LexOther;
128 lexcode['\'']= LexAlpha;
130 lexcode['['] = lexcode[']'] = lexcode['^'] = lexcode['_'] =
131 lexcode['&'] = LexSelf;
133 lexcode['\\'] = LexESC;
134 lexcode['{'] = LexOpen;
135 lexcode['}'] = LexClose;
140 char LexGetArg(char lf, bool accept_spaces= false)
143 char * p = &yytext[0];
147 while (yyis->good()) {
151 if (!lf) lf = c; else
153 lyxerr << "Math parse error: unexpected '"
158 rg = (lf == '{') ? '}': ((lf == '[') ? ']': ((lf == '(') ? ')': 0));
160 lyxerr << "Math parse error: unknown bracket '"
161 << lf << "'" << endl;
169 if ((c > ' ' || (c == ' ' && accept_spaces)) && bcnt>0) *(p++) = c;
170 } while (bcnt > 0 && yyis->good());
179 static int init_done = 0;
183 if (!init_done) LexInitCodes();
185 while (yyis->good()) {
189 if (yy_mtextmode && c == ' ') {
194 if (lexcode[c] == LexNewLine) {
199 if (lexcode[c] == LexComment)
200 do { yyis->get(cc); c = cc; } while (c != '\n' % yyis->good()); // eat comments
202 if (lexcode[c] == LexDigit || lexcode[c] == LexOther || lexcode[c] == LexMathSpace) { yylval.i = c; return LM_TK_STR; }
203 if (lexcode[c] == LexAlpha) { yylval.i= c; return LM_TK_ALPHA; }
204 if (lexcode[c] == LexBOP) { yylval.i= c; return LM_TK_BOP; }
205 if (lexcode[c] == LexSelf) { return c; }
206 if (lexcode[c] == LexArgument) {
210 return LM_TK_ARGUMENT;
212 if (lexcode[c] == LexOpen) { return LM_TK_OPEN; }
213 if (lexcode[c] == LexClose) { return LM_TK_CLOSE; }
215 if (lexcode[c] == LexESC) {
218 if (c == '\\') { return LM_TK_NEWLINE; }
219 if (c == '(') { yylval.i = LM_EN_INTEXT; return LM_TK_BEGIN; }
220 if (c == ')') { yylval.i = LM_EN_INTEXT; return LM_TK_END; }
221 if (c == '[') { yylval.i = LM_EN_DISPLAY; return LM_TK_BEGIN; }
222 if (c == ']') { yylval.i = LM_EN_DISPLAY; return LM_TK_END; }
223 if (strchr(latex_special_chars, c)) {
225 return LM_TK_SPECIAL;
227 if (lexcode[c] == LexMathSpace) {
229 for (i = 0; i < 4 && static_cast<int>(c) != latex_mathspace[i][0]; ++i);
230 yylval.i = (i < 4) ? i: 0;
233 if (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
234 char * p = &yytext[0];
235 while (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
242 if (yyis->good()) yyis->putback(c);
243 latexkeys * l = in_word_set (yytext, strlen(yytext));
245 if (l->token == LM_TK_BEGIN || l->token == LM_TK_END) {
248 // for (i = 0; i < 5 && strncmp(yytext, latex_mathenv[i],
249 // strlen(latex_mathenv[i])); ++i);
251 for (i = 0; i < 6 && strcmp(yytext, latex_mathenv[i]); ++i);
254 if (l->token == LM_TK_SPACE)
270 int parse_align(char * hor, char *)
273 for (char * c = hor; c && *c > ' '; ++c) ++nc;
278 // Accent hacks only for 0.12. Stolen from Cursor.
282 void setAccent(int ac)
284 if (ac > 0 && accent < 8) {
285 nestaccent[accent++] = ac;
287 accent = 0; // consumed!
291 MathedInset * doAccent(byte c, MathedTextCodes t)
293 MathedInset * ac = 0;
295 for (int i = accent - 1; i >= 0; --i) {
297 ac = new MathAccentInset(c, t, nestaccent[i]);
299 ac = new MathAccentInset(ac, nestaccent[i]);
301 accent = 0; // consumed!
307 MathedInset * doAccent(MathedInset * p)
309 MathedInset * ac = 0;
311 for (int i = accent - 1; i >= 0; --i) {
313 ac = new MathAccentInset(p, nestaccent[i]);
315 ac = new MathAccentInset(ac, nestaccent[i]);
317 accent = 0; // consumed!
323 LyxArrayBase * mathed_parse(unsigned flags, LyxArrayBase * array,
326 int t = yylex(), tprev = 0;
328 static int plevel = -1;
329 static int size = LM_ST_TEXT;
330 MathedTextCodes varcode = LM_TC_VAR;
331 MathedInset * binset = 0;
332 static MathMacroTemplate * macro= 0;
337 MathParInset * mt = (mtx) ? *mtx : 0;//(MathParInset*)0;
338 MathedRowSt * crow = (mt) ? mt->getRowSt() : 0;
341 if (!array) array = new LyxArrayBase;
342 MathedIter data(array);
344 if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
345 if ((flags & FLAG_BRACK_ARG) && t == '[') {
348 mathPrintError("Expected {. Maybe you forgot to enclose an argument in {}");
353 MathedInsetTypes fractype = LM_OT_FRAC;
358 data.Insert(doAccent(yylval.i, varcode));
360 data.Insert (yylval.i, varcode); //LM_TC_VAR);
366 data.Insert(macro->getMacroPar(yylval.i-1), LM_TC_INSET);
370 case LM_TK_NEWCOMMAND:
375 // This name lives until quitting, for that reason
376 // I didn't care on deleting explicitly. Later I will.
377 char const * name = strnew(&yytext[1]);
378 // ugly trick to be removed soon (lyx3)
379 char c; yyis->get(c);
385 macro = new MathMacroTemplate(name, na);
386 flags = FLAG_BRACE|FLAG_BRACE_LAST;
388 macro->SetData(array);
393 data.Insert (yylval.i, LM_TC_SPECIAL);
399 data.Insert(doAccent(yylval.i, LM_TC_CONST));
401 data.Insert (yylval.i, LM_TC_CONST);
407 if (accent && tprev == LM_TK_ACCENT) {
408 acc_braces[acc_brace++] = brace;
411 if (flags & FLAG_BRACE_OPT) {
412 flags &= ~FLAG_BRACE_OPT;
416 if (flags & FLAG_BRACE)
417 flags &= ~FLAG_BRACE;
419 data.Insert ('{', LM_TC_TEX);
427 mathPrintError("Unmatching braces");
431 if (acc_brace && brace == acc_braces[acc_brace-1]-1) {
435 if (flags & FLAG_BRACE_FONT) {
437 yy_mtextmode = false;
438 flags &= ~FLAG_BRACE_FONT;
441 if (brace == 0 && (flags & FLAG_BRACE_LAST)) {
445 data.Insert ('}', LM_TC_TEX);
452 if (flags & FLAG_BRACK_ARG) {
453 flags &= ~FLAG_BRACK_ARG;
454 char rg = LexGetArg('[');
456 mathPrintError("Expected ']'");
460 // if (arg) strcpy(arg, yytext);
467 if (flags & FLAG_BRACK_END) {
477 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
478 LyxArrayBase * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
480 // lyxerr << "UP[" << p->GetStyle() << "]" << endl;
481 data.Insert (p, LM_TC_UP);
486 MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
487 LyxArrayBase * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, 0);
489 data.Insert (p, LM_TC_DOWN);
496 binset->SetLimits(bool(yylval.l->id));
504 if ((flags & FLAG_END) && mt && data.getCol()<mt->GetColumns()-1) {
505 data.setNumCols(mt->GetColumns());
506 data.Insert('T', LM_TC_TAB);
508 mathPrintError("Unexpected tab");
509 // debug info. [made that conditional -JMarc]
510 if (lyxerr.debugging(Debug::MATHED))
511 lyxerr << data.getCol() << " " << mt->GetColumns() << endl;
516 if (mt && (flags & FLAG_END)) {
517 if (mt->Permit(LMPF_ALLOW_CR)) {
519 crow->setNext(new MathedRowSt(mt->GetColumns()+1)); // this leaks
520 crow = crow->getNext();
522 data.Insert('K', LM_TC_CR);
524 mathPrintError("Unexpected newline");
530 binset = new MathBigopInset(yylval.l->name, yylval.l->id);
536 if (yylval.l->id < 256) {
537 MathedTextCodes tc = MathIsBOPS(yylval.l->id) ? LM_TC_BOPS: LM_TC_SYMB;
539 data.Insert(doAccent(yylval.l->id, tc));
541 data.Insert (yylval.l->id, tc);
543 MathFuncInset * bg = new MathFuncInset(yylval.l->name);
545 data.Insert(doAccent(bg));
547 data.Insert(bg, true);
554 data.Insert(doAccent(yylval.i, LM_TC_BOP));
556 data.Insert (yylval.i, LM_TC_BOP);
562 mt->UserSetSize(yylval.l->id);
569 MathSpaceInset * sp = new MathSpaceInset(yylval.i);
576 MathDotsInset * p = new MathDotsInset(yylval.l->name, yylval.l->id);
581 fractype = LM_OT_STACKREL;
584 MathFracInset * fc = new MathFracInset(fractype);
585 LyxArrayBase * num = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
586 LyxArrayBase * den = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
587 fc->SetData(num, den);
588 data.Insert(fc, LM_TC_ACTIVE_INSET);
595 char c; yyis->get(c);
598 rt = new MathRootInset(size);
599 rt->setArgumentIdx(0);
600 rt->SetData(mathed_parse(FLAG_BRACK_END, 0, &rt));
601 rt->setArgumentIdx(1);
604 rt = new MathSqrtInset(size);
606 rt->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST, 0, &rt));
607 data.Insert(rt, LM_TC_ACTIVE_INSET);
615 if (lfd == LM_TK_SYM || lfd == LM_TK_STR || lfd == LM_TK_BOP|| lfd == LM_TK_SPECIAL)
616 lfd = (lfd == LM_TK_SYM) ? yylval.l->id: yylval.i;
617 // lyxerr << "L[" << lfd << " " << lfd << "]";
618 LyxArrayBase * a = mathed_parse(FLAG_RIGHT);
620 // lyxerr << "R[" << rgd << "]";
621 if (rgd == LM_TK_SYM || rgd == LM_TK_STR || rgd == LM_TK_BOP || rgd == LM_TK_SPECIAL)
622 rgd = (rgd == LM_TK_SYM) ? yylval.l->id: yylval.i;
623 MathDelimInset * dl = new MathDelimInset(lfd, rgd);
625 data.Insert(dl, LM_TC_ACTIVE_INSET);
626 // lyxerr << "RL[" << lfd << " " << rgd << "]";
631 if (flags & FLAG_RIGHT) {
635 mathPrintError("Unmatched right delimiter");
643 varcode = static_cast<MathedTextCodes>(yylval.l->id);
644 yy_mtextmode = bool(varcode == LM_TC_TEXTRM);
645 flags |= (FLAG_BRACE|FLAG_BRACE_FONT);
650 MathDecorationInset * sq = new MathDecorationInset(yylval.l->id,
652 sq->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
653 data.Insert(sq, LM_TC_ACTIVE_INSET);
657 case LM_TK_ACCENT: setAccent(yylval.l->id); break;
662 crow->setNumbered(false);
669 MathedInset * bg = new MathFuncInset(yylval.l->name);
678 data.Insert(new MathFuncInset(yylval.l->name, LM_OT_FUNCLIM));
685 MathMacroTable::mathMTable.getMacro(yylval.s);
688 data.Insert(doAccent(p), p->getTCode());
690 data.Insert(p, p->getTCode());
691 for (int i = 0; p->setArgumentIdx(i); ++i)
692 p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
695 MathedInset * q = new MathFuncInset(yylval.s, LM_OT_UNDEF);
697 data.Insert(doAccent(q));
706 if (mathed_env != yylval.i && yylval.i!= LM_EN_ARRAY)
707 mathPrintError("Unmatched environment");
708 // debug info [made that conditional -JMarc]
709 if (lyxerr.debugging(Debug::MATHED))
710 lyxerr << "[" << yylval.i << "]" << endl;
712 if (mt) { // && (flags & FLAG_END)) {
720 if (yylval.i == LM_EN_ARRAY) {
721 char ar[120], ar2[8];
722 ar[0] = ar2[0] = '\0';
723 char rg = LexGetArg(0);
729 int nc = parse_align(ar, ar2);
730 MathParInset * mm = new MathMatrixInset(nc, 0);
731 mm->SetAlign(ar2[0], ar);
732 data.Insert(mm, LM_TC_ACTIVE_INSET);
733 mathed_parse(FLAG_END, mm->GetData(), &mm);
735 if (yylval.i >= LM_EN_INTEXT && yylval.i<= LM_EN_EQNARRAY) {
737 mathPrintError("Misplaced environment");
741 mathPrintError("0 paragraph.");
745 mathed_env = yylval.i;
746 if (mathed_env>= LM_EN_DISPLAY) {
747 size = LM_ST_DISPLAY;
748 if (mathed_env>LM_EN_EQUATION) {
749 mt = new MathMatrixInset(3, -1);
750 mt->SetAlign(' ', "rcl");
753 // data.Insert(' ', LM_TC_TAB);
754 // data.Insert(' ', LM_TC_TAB);
758 mt->SetType(mathed_env);
759 crow = mt->getRowSt();
763 lyxerr << "MATH BEGIN[" << mathed_env << "]" << endl;
766 // lyxerr << "MATHCRO[" << yytext << "]";
768 MathMacroTable::mathMTable.getMacro(yytext);
770 data.Insert(p, p->getTCode());
771 p->setArgumentIdx(0);
772 mathed_parse(FLAG_END, p->GetData(), reinterpret_cast<MathParInset**>(&p));
773 // for (int i = 0; p->setArgumentIdx(i); ++i)
774 // p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
776 mathPrintError("Unrecognized environment");
784 MathMacroTable::mathMTable.getMacro(yylval.l->name);
788 data.Insert(doAccent(p));
790 data.Insert(p, static_cast<MathMacro*>(p)->getTCode());
797 char rg = LexGetArg('\0', true);
799 mathPrintError("Expected '{'");
801 lyxerr << "[" << yytext << "]" << endl;
806 // This is removed by crow's destructor. Bad design? yes, this
807 // will be changed after 0.12
808 crow->setLabel(strnew(yytext));
811 // where is this math_label free'ed?
812 // Supposedly in ~formula, another bad hack,
813 // give me some time please.
814 mathed_label = strnew(yytext);
817 lyxerr << "Label[" << mathed_label << "]" << endl;
822 mathPrintError("Unrecognized token");
824 lyxerr << "[" << t << " " << yytext << "]" << endl;
829 lyxerr << " Math Panic, expect problems!" << endl;
830 // Search for the end command.
831 do t = yylex (); while (t != LM_TK_END && t);
835 if ((flags & FLAG_BRACE_OPT)/* && t!= '^' && t!= '_'*/) {
836 flags &= ~FLAG_BRACE_OPT;
837 //data.Insert (LM_TC_CLOSE);
846 void mathed_parser_file(istream & is, int lineno)
850 if (!MathMacroTable::built)
851 MathMacroTable::mathMTable.builtinMacros();
855 int mathed_parser_lineno()