]> git.lyx.org Git - lyx.git/blob - src/mathed/math_parser.C
mathed48.diff
[lyx.git] / src / mathed / math_parser.C
1 /*
2  *  File:        math_parser.C
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.
7  *
8  *  Dependencies: Xlib, XForms
9  *
10  *  Copyright: 1996, Alejandro Aguilar Sierra
11  *
12  *   Version: 0.8beta.
13  *
14  *   You are free to use and modify this code under the terms of
15  *   the GNU General Public Licence version 2 or later.
16  */
17
18 #include <config.h>
19
20 #include <cctype>
21
22 #ifdef __GNUG__
23 #pragma implementation
24 #endif
25
26 #include "math_parser.h"
27 #include "array.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"
44 #include "debug.h"
45 #include "support/lyxlib.h"
46 #include "mathed/support.h"
47 #include "boost/array.hpp"
48
49 using std::istream;
50 using std::endl;
51
52
53 extern MathMatrixInset * create_multiline(short int type, int cols);
54
55
56 enum {
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
66 };
67
68
69 ///
70 union YYSTYPE {
71         ///
72         unsigned char c;
73         ///
74         char const * s;
75         ///
76         int i;
77         ///
78         latexkeys const * l;
79 };
80
81
82 static
83 YYSTYPE yylval;
84
85
86 static
87 MathedInsetTypes mathed_env = LM_OT_MIN;
88
89
90 string mathed_label;
91
92
93 int const latex_mathenv_num = 12;
94 char const * latex_mathenv[latex_mathenv_num] = { 
95         "math", 
96         "displaymath", 
97         "equation", 
98         "eqnarray*",
99         "eqnarray",
100         "align*",
101         "align",
102         "alignat*",
103         "alignat",
104         "multline*",
105         "multline",
106         "array"
107 };
108
109
110 char const * latex_special_chars = "#$%&_{}";
111
112
113 // These are lexical codes, not semantic
114 enum lexcode_enum {
115         LexNone,
116         LexESC,
117         LexAlpha,
118         LexDigit,
119         LexBOP,         // Binary operators or relations
120         LexMathSpace,
121         LexOpen,
122         LexClose,
123         LexComment,
124         LexArgument,
125         LexSpace,
126         LexNewLine,
127         LexOther,
128         LexSelf
129 };
130
131
132 static lexcode_enum lexcode[256];  
133 #warning Replace with string
134 //static char yytext[256];
135 static array<char, 256> yytext;
136 static int yylineno;
137 static istream * yyis;
138 static bool yy_mtextmode= false;
139
140
141 static inline
142 void mathPrintError(string const & msg) 
143 {
144         lyxerr << "Line ~" << yylineno << ": Math parse error: "
145                << msg << endl;
146 }
147
148
149 static
150 void LexInitCodes()
151 {
152         for (int i = 0;  i <= 255; ++i) {
153                 if (isalpha(i))
154                         lexcode[i] = LexAlpha;
155                 else if (isdigit(i))
156                         lexcode[i] = LexDigit;
157                 else if (isspace(i))
158                         lexcode[i] = LexSpace;
159                 else
160                         lexcode[i] = LexNone;
161         }
162         
163         lexcode['\t'] = lexcode['\f'] = lexcode[' '] = LexSpace;
164         lexcode['\n'] = LexNewLine;
165         lexcode['%'] = LexComment;
166         lexcode['#'] = LexArgument;
167         lexcode['+'] = lexcode['-'] = lexcode['*'] = lexcode['/']
168                 = lexcode['<'] = lexcode['>'] = lexcode['='] = LexBOP;
169         
170         lexcode['!'] = lexcode[','] = lexcode[':']
171                 = lexcode[';'] = LexMathSpace;
172         
173         lexcode['('] = lexcode[')'] = lexcode['|'] = lexcode['.'] =
174                 lexcode['?'] = LexOther; 
175         
176         lexcode['\''] = lexcode['@'] = LexAlpha;
177         
178         lexcode['['] = lexcode[']'] = lexcode['^'] = lexcode['_'] = 
179                 lexcode['&'] = LexSelf;  
180         
181         lexcode['\\'] = LexESC;
182         lexcode['{'] = LexOpen;
183         lexcode['}'] = LexClose;
184 }
185
186
187 static
188 char LexGetArg(char lf, bool accept_spaces = false)
189 {
190         // unsigned char c;
191         // char cc;
192         while (yyis->good()) {
193                 char cc;
194                 yyis->get(cc);
195                 unsigned char c = cc;
196                 if (c > ' ') {
197                         if (!lf) 
198                                 lf = c;
199                         else if (c != lf) {
200                                 lyxerr << "Math parse error: unexpected '"
201                                        << c << "'" << endl;
202                                 return '\0';
203                         }
204                         break;
205                 }
206         }
207         char const rg =
208                 (lf == '{') ? '}' :
209                 ((lf == '[') ? ']'
210                  : ((lf == '(') ? ')' : 0));
211         if (!rg) {
212                 lyxerr << "Math parse error: unknown bracket '"
213                        << lf << "'" << endl;
214                 return '\0';
215         }
216         char * p = &yytext[0];
217         int bcnt = 1;
218         do {
219                 char cc;
220                 yyis->get(cc);
221                 unsigned char c = cc;
222                 if (c == lf) ++bcnt;
223                 if (c == rg) --bcnt;
224                 if ((c > ' ' || (c == ' ' && accept_spaces)) && bcnt > 0)
225                         *(p++) = c;
226         } while (bcnt > 0 && yyis->good() && p - yytext.data() < 255);
227         
228         *p = '\0';
229         return rg;
230 }
231
232
233 static
234 int yylex(void)
235 {
236         static int init_done = 0;
237         
238         if (!init_done) LexInitCodes();
239         
240         unsigned char c;
241         char cc;
242         while (yyis->good()) {
243                 yyis->get(cc);
244                 c = cc;
245                 
246                 if (yy_mtextmode && c == ' ') {
247                         yylval.i= ' ';
248                         return LM_TK_ALPHA;
249                 } else if (lexcode[c] == LexNewLine) {
250                         ++yylineno; 
251                         continue;
252                 } else if (lexcode[c] == LexComment) {
253                         do {
254                                 yyis->get(cc);
255                                 c = cc;
256                         } while (c != '\n' % yyis->good());  // eat comments
257                 } else if (lexcode[c] == LexDigit
258                            || lexcode[c] == LexOther
259                            || lexcode[c] == LexMathSpace) {
260                         yylval.i = c;
261                         return LM_TK_STR;
262                 } else if (lexcode[c] == LexAlpha) {
263                         yylval.i= c;
264                         return LM_TK_ALPHA;
265                 } else if (lexcode[c] == LexBOP) {
266                         yylval.i= c;
267                         return LM_TK_BOP;
268                 } else if (lexcode[c] == LexSelf) {
269                         return c;
270                 } else if (lexcode[c] == LexArgument) {
271                         yyis->get(cc);
272                         c = cc;
273                         yylval.i = c - '0';
274                         return LM_TK_ARGUMENT; 
275                 } else if (lexcode[c] == LexOpen) {
276                         return LM_TK_OPEN;
277                 } else if (lexcode[c] == LexClose) {
278                         return LM_TK_CLOSE;
279                 } else if (lexcode[c] == LexESC)   {
280                         yyis->get(cc);
281                         c = cc;
282                         if (c == '\\')  {
283                                 return LM_TK_NEWLINE;
284                         }
285                         if (c == '(') {
286                                 yylval.i = LM_OT_MIN;
287                                 return LM_TK_BEGIN;
288                         }
289                         if (c == ')') {
290                                 yylval.i = LM_OT_MIN;
291                                 return LM_TK_END;
292                         }
293                         if (c == '[') {
294                                 yylval.i = LM_OT_PAR;
295                                 return LM_TK_BEGIN;
296                         }
297                         if (c == ']') {
298                                 yylval.i = LM_OT_PAR;
299                                 return LM_TK_END;
300                         }
301                         if (strchr(latex_special_chars, c)) {
302                                 yylval.i = c;
303                                 return LM_TK_SPECIAL;
304                         } 
305                         if (lexcode[c] == LexMathSpace) {
306                                 int i;
307                                 for (i = 0; i < 4 && static_cast<int>(c) != latex_mathspace[i][0]; ++i);
308                                 yylval.i = (i < 4) ? i : 0; 
309                                 return LM_TK_SPACE; 
310                         }
311                         if (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) {
312                                 char * p = &yytext[0];
313                                 while ((lexcode[c] == LexAlpha || lexcode[c] == LexDigit)
314                                        && p - yytext.data() < 255) {
315                                         *p = c;
316                                         yyis->get(cc);
317                                         c = cc;
318                                         ++p;
319                                 }
320                                 *p = '\0';
321                                 if (yyis->good()) yyis->putback(c);
322                                 latexkeys const * l = in_word_set (yytext.data(), strlen(yytext.data()));
323                                 if (l) {
324                                         if (l->token == LM_TK_BEGIN || l->token == LM_TK_END) { 
325                                                 int i;
326                                                 LexGetArg('{');
327 //                for (i = 0; i < 5 && compare(yytext, latex_mathenv[i],
328 //                              strlen(latex_mathenv[i])); ++i);
329                                                 
330                                                 for (i = 0;
331                                                      i < latex_mathenv_num
332                                                              && compare(yytext.data(), latex_mathenv[i]); ++i);
333                                                 yylval.i = i;
334                                         } else if (l->token == LM_TK_SPACE) 
335                                                 yylval.i = l->id;
336                                         else
337                                                 yylval.l = l;
338                                         return l->token;
339                                 } else { 
340                                         yylval.s = yytext.data();
341                                         return LM_TK_UNDEF;
342                                 }
343                         }
344                 }
345         }
346         return 0;
347 }
348
349
350 static inline
351 int parse_align(char * hor, char *)
352 {
353         int nc = 0;
354         for (char * c = hor; c && *c > ' '; ++c) ++nc;
355         return nc;
356 }
357
358
359 // Accent hacks only for 0.12. Stolen from Cursor.
360 static
361 int accent = 0;
362 static
363 int nestaccent[8];
364
365 static inline
366 void setAccent(int ac)
367 {
368         if (ac > 0 && accent < 8) {
369                 nestaccent[accent++] = ac;
370         } else
371           accent = 0;  // consumed!
372 }
373
374
375 static
376 MathedInset * doAccent(byte c, MathedTextCodes t)
377 {
378         MathedInset * ac = 0;
379         
380         for (int i = accent - 1; i >= 0; --i) {
381                 if (i == accent - 1)
382                   ac = new MathAccentInset(c, t, nestaccent[i]);
383                 else 
384                   ac = new MathAccentInset(ac, nestaccent[i]);
385         }
386         accent = 0;  // consumed!
387         
388         return ac;
389 }
390
391
392 static
393 MathedInset * doAccent(MathedInset * p)
394 {
395         MathedInset * ac = 0;
396         
397         for (int i = accent - 1; i >= 0; --i) {
398                 if (i == accent - 1)
399                   ac = new MathAccentInset(p, nestaccent[i]);
400                 else 
401                   ac = new MathAccentInset(ac, nestaccent[i]);
402         }
403         accent = 0;  // consumed!
404         
405         return ac;
406 }
407
408
409 /**
410  */
411 void mathed_parse(MathedArray & array, unsigned flags = 0,
412                             MathParInset ** mtx = 0)
413 {
414         int t = yylex();
415         int tprev = 0;
416         bool panic = false;
417         static int plevel = -1;
418         static int size = LM_ST_TEXT;
419         MathedTextCodes varcode = LM_TC_VAR;
420         MathedInset * binset = 0;
421         static MathMacroTemplate * macro = 0;
422         
423         int brace = 0;
424         int acc_brace = 0;
425         int acc_braces[8];
426         MathParInset * mt = (mtx) ? *mtx : 0;
427         MathedRowSt * crow = (mt) ? mt->getRowSt().data_ : 0;
428         
429         ++plevel;
430         MathedIter data(&array);
431         while (t) {
432                 if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
433                         if ((flags & FLAG_BRACK_ARG) && t == '[') {
434                         } else {
435                                 mathPrintError("Expected {. Maybe you forgot to enclose an argument in {}");
436                                 panic = true;
437                                 break;
438                         }
439                 }
440                 MathedInsetTypes fractype = LM_OT_FRAC;
441                 switch (t) {
442                         
443                 case LM_TK_ALPHA:
444                         if (accent) {
445                                 data.insertInset(doAccent(yylval.i, varcode),
446                                                  LM_TC_INSET);
447                         } else
448                                 data.insert(yylval.i, varcode);  //LM_TC_VAR);
449                         break;
450
451                 case LM_TK_ARGUMENT:
452                         if (macro) {
453                                 data.insertInset(macro
454                                                  ->getMacroPar(yylval.i - 1),
455                                                  LM_TC_INSET);
456                         } else {
457                                 lyxerr[Debug::MATHED] << "mathed_parse: macro arg outside macro def." << endl;
458                         }
459                         
460                         break;
461
462                 case LM_TK_NEWCOMMAND:
463                 {
464                         int na = 0; 
465                         
466                         LexGetArg('{');
467                         string const name(&yytext[1]);
468                         
469                         // ugly trick to be removed soon (lyx3)
470                         char const c = yyis->peek();
471                         if (c == '[') {
472                                 LexGetArg('[');
473                                 na = lyx::atoi(yytext.data());
474                         }  
475                         macro = new MathMacroTemplate(name, na);
476                         flags = FLAG_BRACE|FLAG_BRACE_LAST;
477
478                         *mtx = macro;
479                         macro->setData(array);
480                         break;
481                 }
482                 
483                 case LM_TK_SPECIAL:
484                         data.insert(yylval.i, LM_TC_SPECIAL);
485                         break;
486
487                 case LM_TK_STR:
488                         if (accent) {
489                                 data.insertInset(doAccent(yylval.i, LM_TC_CONST), LM_TC_INSET);
490                         } else
491                                 data.insert(yylval.i, LM_TC_CONST);
492                         break;
493
494                 case LM_TK_OPEN:
495                         ++brace;
496                         if  (accent && tprev == LM_TK_ACCENT) {
497                                 acc_braces[acc_brace++] = brace;
498                                 break;
499                         }
500                         if (flags & FLAG_BRACE_OPT) {
501                                 flags &= ~FLAG_BRACE_OPT;
502                                 flags |= FLAG_BRACE;
503                         }
504                         
505                         if (flags & FLAG_BRACE)
506                                 flags &= ~FLAG_BRACE;
507                         else {
508                                 data.insert('{', LM_TC_TEX);
509                         }
510                         break;
511
512                 case LM_TK_CLOSE:
513                         --brace;         
514                         if (brace < 0) {
515                                 mathPrintError("Unmatching braces");
516                                 panic = true;
517                                 break;
518                         }
519                         if (acc_brace && brace == acc_braces[acc_brace - 1] - 1) {
520                                 --acc_brace;
521                                 break;
522                         }
523                         if (flags & FLAG_BRACE_FONT) {
524                                 varcode = LM_TC_VAR;
525                                 yy_mtextmode = false;
526                                 flags &= ~FLAG_BRACE_FONT;
527                                 break;
528                         }
529                         if (brace == 0 && (flags & FLAG_BRACE_LAST)) {
530                                 --plevel;
531                                 return;
532                         } else {
533                                 data.insert('}', LM_TC_TEX);
534                         }
535                         break;
536                 
537                 case '[':
538                         if (flags & FLAG_BRACK_ARG) {
539                                 flags &= ~FLAG_BRACK_ARG;
540                                 char const rg = LexGetArg('[');
541                                 if (rg != ']') {
542                                         mathPrintError("Expected ']'");
543                                         panic = true;
544                                         break;
545                                 }          
546                                 // if (arg) strcpy(arg, yytext);
547                         } else
548                                 data.insert('[', LM_TC_CONST);
549                         break;
550
551                 case ']':
552                         if (flags & FLAG_BRACK_END) {
553                                 --plevel;
554                                 return;
555                         } else
556                                 data.insert(']', LM_TC_CONST);
557                         break;
558                 
559                 case '^':
560                 {  
561                         MathParInset * p = new MathParInset(size, "",
562                                                             LM_OT_SCRIPT);
563                         MathedArray ar;
564                         mathed_parse(ar, FLAG_BRACE_OPT|FLAG_BRACE_LAST);
565                         p->setData(ar);
566                         // lyxerr << "UP[" << p->GetStyle() << "]" << endl;
567                         data.insertInset(p, LM_TC_UP);
568                         break;
569                 }
570                 
571                 case '_':
572                 {
573                         MathParInset * p = new MathParInset(size, "",
574                                                             LM_OT_SCRIPT);
575                         MathedArray ar;
576                         mathed_parse(ar, FLAG_BRACE_OPT|FLAG_BRACE_LAST);
577                         p->setData(ar);
578                         data.insertInset(p, LM_TC_DOWN);
579                         break;
580                 }
581                 
582                 case LM_TK_LIMIT:
583                         if (binset) {
584                                 binset->SetLimits(bool(yylval.l->id));
585                                 binset = 0;
586                         }
587                         break;
588                 
589                 case '&':    // Tab
590                         if ((flags & FLAG_END) && mt
591                             && data.getCol()<mt->GetColumns() - 1) {
592                                 data.setNumCols(mt->GetColumns());
593                                 data.insert('T', LM_TC_TAB);
594                         } else 
595                                 mathPrintError("Unexpected tab");
596                         // debug info. [made that conditional -JMarc]
597                         if (lyxerr.debugging(Debug::MATHED))
598                                 lyxerr << data.getCol() << " "
599                                        << mt->GetColumns() << endl;
600                         break;
601                 
602                 case LM_TK_NEWLINE:
603                         if (mt && (flags & FLAG_END)) {
604                                 if (mt->Permit(LMPF_ALLOW_CR)) {
605                                         if (crow) {
606                                                 crow->next_ = new MathedRowSt(mt->GetColumns() + 1);
607                                                 crow = crow->next_;
608                                         }
609                                         data.insert('K', LM_TC_CR);
610                                 } else 
611                                         mathPrintError("Unexpected newline");
612                         }
613                         break;
614
615                 case LM_TK_BIGSYM:  
616                 {
617                         binset = new MathBigopInset(yylval.l->name, yylval.l->id);
618                         data.insertInset(binset, LM_TC_INSET);  
619                         break;
620                 }
621                 
622                 case LM_TK_SYM:
623                         if (yylval.l->id < 256) {
624                                 MathedTextCodes tc = MathIsBOPS(yylval.l->id) ? LM_TC_BOPS: LM_TC_SYMB;
625                                 if (accent) {
626                                         data.insertInset(doAccent(yylval.l->id, tc), LM_TC_INSET);
627                                 } else
628                                         data.insert(yylval.l->id, tc);
629                         } else {
630                                 MathFuncInset * bg = new MathFuncInset(yylval.l->name);
631                                 if (accent) {
632                                         data.insertInset(doAccent(bg), LM_TC_INSET);
633                                 } else {
634 #warning This is suspisious! (Lgb)
635                                         // it should not take a bool as second arg (Lgb)
636                                         data.insertInset(bg, true);
637                                 }
638                                 
639                         }
640                         break;
641
642                 case LM_TK_BOP:
643                         if (accent) {
644                                 data.insertInset(doAccent(yylval.i, LM_TC_BOP), LM_TC_INSET);
645                         } else
646                                 data.insert(yylval.i, LM_TC_BOP);
647                         break;
648
649                 case LM_TK_STY:
650                         if (mt) {
651                                 mt->UserSetSize(yylval.l->id);
652                         }
653                         break; 
654
655                 case LM_TK_SPACE:
656                         if (yylval.i >= 0) {
657                                 MathSpaceInset * sp = new MathSpaceInset(yylval.i);
658                                 data.insertInset(sp, LM_TC_INSET);
659                         }
660                         break;
661
662                 case LM_TK_DOTS:
663                 {
664                         MathDotsInset * p = new MathDotsInset(yylval.l->name, yylval.l->id);
665                         data.insertInset(p, LM_TC_INSET);
666                         break;
667                 }
668                 
669                 case LM_TK_STACK:
670                         fractype = LM_OT_STACKREL;
671                         // fallthru
672                 case LM_TK_FRAC:
673                 {
674                         MathFracInset * fc = new MathFracInset(fractype);
675                         MathedArray num;
676                         mathed_parse(num, FLAG_BRACE|FLAG_BRACE_LAST);
677                         MathedArray den;
678                         mathed_parse(den, FLAG_BRACE|FLAG_BRACE_LAST);
679                         fc->SetData(num, den);
680                         data.insertInset(fc, LM_TC_ACTIVE_INSET);
681                         break;
682                 }
683                 
684                 case LM_TK_SQRT:
685                 {           
686                         MathParInset * rt;
687                         
688                         char c;
689                         yyis->get(c);
690                         
691                         if (c == '[') {
692                                 rt = new MathRootInset(size);
693                                 rt->setArgumentIdx(0);
694                                 MathedArray ar;
695                                 mathed_parse(ar, FLAG_BRACK_END, &rt);
696                                 rt->setData(ar); // I belive that line is not needed (Lgb)
697                                 rt->setArgumentIdx(1);
698                         } else {
699                                 yyis->putback(c);
700                                 rt = new MathSqrtInset(size);
701                         }
702                         MathedArray ar;
703                         mathed_parse(ar, FLAG_BRACE|FLAG_BRACE_LAST, &rt);
704                         rt->setData(ar); // I belive that this line is not needed (Lgb)
705                         data.insertInset(rt, LM_TC_ACTIVE_INSET);
706                         break;
707                 }
708                 
709                 case LM_TK_LEFT:
710                 {
711                         int lfd = yylex();
712                         if (lfd == LM_TK_SYM || lfd == LM_TK_STR || lfd == LM_TK_BOP|| lfd == LM_TK_SPECIAL)
713                                 lfd = (lfd == LM_TK_SYM) ? yylval.l->id: yylval.i;
714 //       lyxerr << "L[" << lfd << " " << lfd << "]";
715                         MathedArray ar;
716                         mathed_parse(ar, FLAG_RIGHT);
717                         int rgd = yylex();
718 //       lyxerr << "R[" << rgd << "]";
719                         if (rgd == LM_TK_SYM || rgd == LM_TK_STR || rgd == LM_TK_BOP || rgd == LM_TK_SPECIAL)
720                                 rgd = (rgd == LM_TK_SYM) ? yylval.l->id: yylval.i;       
721                         MathDelimInset * dl = new MathDelimInset(lfd, rgd);
722                         dl->setData(ar);
723                         data.insertInset(dl, LM_TC_ACTIVE_INSET);
724 //       lyxerr << "RL[" << lfd << " " << rgd << "]";
725                         break;
726                 }
727                 
728                 case LM_TK_RIGHT:
729                         if (flags & FLAG_RIGHT) { 
730                                 --plevel;
731                                 return;
732                         } else {
733                                 mathPrintError("Unmatched right delimiter");
734 //          panic = true;
735                         }
736                         break;
737                 
738                 case LM_TK_FONT:
739                         varcode = static_cast<MathedTextCodes>(yylval.l->id);
740                         yy_mtextmode = bool(varcode == LM_TC_TEXTRM);
741                         flags |= (FLAG_BRACE|FLAG_BRACE_FONT);
742                         break;
743
744                 case LM_TK_WIDE:
745                 {  
746                         MathDecorationInset * sq = new MathDecorationInset(yylval.l->id,
747                                                                            size);
748                         MathedArray ar;
749                         mathed_parse(ar, FLAG_BRACE|FLAG_BRACE_LAST);
750                         sq->setData(ar);
751                         data.insertInset(sq, LM_TC_ACTIVE_INSET);
752                         break;
753                 }
754                 
755                 case LM_TK_ACCENT:
756                         setAccent(yylval.l->id);
757                         break;
758                         
759                 case LM_TK_NONUM:
760                         if (crow)
761                                 crow->setNumbered(false);
762                         break;
763                 
764                 case LM_TK_PMOD:
765                 case LM_TK_FUNC:
766                         if (accent) {
767                                 data.insert(t, LM_TC_CONST);
768                         } else {
769                                 MathedInset * bg = new MathFuncInset(yylval.l->name); 
770                                 data.insertInset(bg, LM_TC_INSET);
771                         }
772                         break;
773                 
774                 case LM_TK_FUNCLIM:
775                         data.insertInset(new MathFuncInset(yylval.l->name, LM_OT_FUNCLIM),
776                                          LM_TC_INSET);
777                         break;
778
779                 case LM_TK_UNDEF:
780                 {
781                         
782                         MathMacro * p = 
783                                 MathMacroTable::mathMTable.createMacro(yylval.s);
784                         if (p) {
785                                 if (accent) 
786                                         data.insertInset(doAccent(p), p->getTCode());
787                                 else
788                                         data.insertInset(p, p->getTCode());
789                                 for (int i = 0; p->setArgumentIdx(i); ++i) {
790                                         MathedArray ar;
791                                         mathed_parse(ar, FLAG_BRACE|FLAG_BRACE_LAST);
792                                         p->setData(ar);
793                                 }
794                         } else {
795                                 MathedInset * q = new MathFuncInset(yylval.s, LM_OT_UNDEF);
796                                 if (accent) {
797                                         data.insertInset(doAccent(q), LM_TC_INSET);
798                                 } else {
799                                         data.insertInset(q, LM_TC_INSET);
800                                 }
801                         }
802                         break;
803                 }
804                 
805                 case LM_TK_END:
806                         if (mathed_env != yylval.i && yylval.i != LM_OT_MATRIX)
807                                 mathPrintError("Unmatched environment");
808                         // debug info [made that conditional -JMarc]
809                         if (lyxerr.debugging(Debug::MATHED))
810                                 lyxerr << "[" << yylval.i << "]" << endl;
811                         --plevel;
812                         if (mt) { // && (flags & FLAG_END)) {
813                                 mt->setData(array);
814                                 array.clear();
815                         }
816                         return;
817
818                 case LM_TK_BEGIN:
819                         if (yylval.i == LM_OT_MATRIX) {
820                                 char ar[120];
821                                 char ar2[8];
822                                 ar[0] = ar2[0] = '\0'; 
823                                 char rg = LexGetArg(0);
824                                 if (rg == ']') {
825                                         strcpy(ar2, yytext.data());
826                                         rg = LexGetArg('{');
827                                 }
828                                 strcpy(ar, yytext.data());
829                                 int const nc = parse_align(ar, ar2);
830                                 MathParInset * mm = new MathMatrixInset(nc, 0);
831                                 mm->SetAlign(ar2[0], ar);
832                                 data.insertInset(mm, LM_TC_ACTIVE_INSET);
833                                 MathedArray dat;
834                                 mathed_parse(dat, FLAG_END, &mm);
835                         } else if (is_eqn_type(yylval.i)) {
836                                 if (plevel!= 0) {
837                                         mathPrintError("Misplaced environment");
838                                         break;
839                                 }
840                                 if (!mt) {
841                                         mathPrintError("0 paragraph.");
842                                         panic = true;
843                                 }
844                                 
845                                 mathed_env = static_cast<MathedInsetTypes>(yylval.i);
846                                 if (mathed_env != LM_OT_MIN) {
847                                         size = LM_ST_DISPLAY;
848                                         if (is_multiline(mathed_env)) {
849                                                 int cols = 1;
850                                                 if (is_multicolumn(mathed_env)) {
851                                                         if (mathed_env != LM_OT_ALIGNAT &&
852                                                             mathed_env != LM_OT_ALIGNATN &&
853                                                             yyis->good()) {
854                                                                 char c;
855                                                                 yyis->get(c);
856                                                                 if (c != '%')
857                                                                         lyxerr << "Math parse error: unexpected '"
858                                                                                << c << "'" << endl;
859                                                         }
860                                                         LexGetArg('{');
861                                                         cols = strToInt(string(yytext.data()));
862                                                 }
863                                                 mt = create_multiline(mathed_env, cols);
864                                                 if (mtx) *mtx = mt;
865                                                 flags |= FLAG_END;
866 //                   data.Insert(' ', LM_TC_TAB);
867 //                   data.Insert(' ', LM_TC_TAB);
868 //                   data.Reset();
869                                         }
870                                         mt->SetStyle(size);
871                                         mt->SetType(mathed_env);
872                                         crow = mt->getRowSt().data_;
873                                 }
874                                 
875                                 lyxerr[Debug::MATHED] << "MATH BEGIN[" << mathed_env << "]" << endl;
876                         } else {
877 //           lyxerr << "MATHCRO[" << yytext << "]";
878                                 MathMacro * p = 
879                                         MathMacroTable::mathMTable.createMacro(yytext.data());
880                                 if (p) {
881                                         data.insertInset(p, p->getTCode());
882                                         p->setArgumentIdx(0);
883                                         //mathed_parse(p->GetData(), FLAG_END, reinterpret_cast<MathParInset**>(&p));
884                                         MathedArray dat;
885                                         mathed_parse(dat, FLAG_END, reinterpret_cast<MathParInset**>(&p));
886 //               for (int i = 0; p->setArgumentIdx(i); ++i)
887 //                 p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
888                                 } else 
889                                         mathPrintError("Unrecognized environment");
890                         }
891                         break;
892                 
893                 case LM_TK_MACRO:
894                 { 
895                         MathedInset * p = 
896                                 MathMacroTable::mathMTable.createMacro(yylval.l->name);
897                         
898                         if (p) {
899                                 if (accent) {
900                                         data.insertInset(doAccent(p), LM_TC_INSET);
901                                 } else
902                                         data.insertInset(p, static_cast<MathMacro*>(p)->getTCode());
903                         }
904                         break;
905                 }
906                 
907                 case LM_TK_LABEL:
908                 {          
909                         char const rg = LexGetArg('\0', true);
910                         if (rg != '}') {
911                                 mathPrintError("Expected '{'");
912                                 // debug info
913                                 lyxerr << "[" << yytext.data() << "]" << endl;
914                                 panic = true;
915                                 break;
916                         } 
917                         if (crow) {
918                                 crow->setLabel(yytext.data());
919                         } else {
920                                 mathed_label = yytext.data();
921                         }
922                         lyxerr[Debug::MATHED] << "Label[" << mathed_label << "]" << endl;
923                         break;
924                 }
925                 
926                 default:
927                         mathPrintError("Unrecognized token");
928                         // debug info
929                         lyxerr << "[" << t << " " << yytext.data() << "]" << endl;
930                         break;
931                 } // end of switch
932                 
933                 tprev = t;
934                 if (panic) {
935                         lyxerr << " Math Panic, expect problems!" << endl;
936                         //   Search for the end command. 
937                         do {
938                                 t = yylex ();
939                         } while (t != LM_TK_END && t);
940                 } else
941                         t = yylex ();
942                 
943                 if ((flags & FLAG_BRACE_OPT)/* && t!= '^' && t!= '_'*/) {
944                         flags &= ~FLAG_BRACE_OPT;
945                         //data.Insert (LM_TC_CLOSE);
946                         break;
947                 }
948         }
949         --plevel;
950 }
951
952
953 void mathed_parser_file(istream & is, int lineno)
954 {
955         yyis = &is;
956         yylineno = lineno;
957         if (!MathMacroTable::built)
958                 MathMacroTable::mathMTable.builtinMacros();
959 }
960
961
962 int mathed_parser_lineno()
963 {
964         return yylineno;
965 }