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