]> git.lyx.org Git - lyx.git/blob - src/mathed/math_parser.C
Initial revision
[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: (c) 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 #include <stdlib.h>
20 #include <ctype.h>
21
22 #ifdef __GNUG__
23 #pragma implementation "math_parser.h"
24 #endif
25
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"
31 #include "error.h"
32
33 enum {
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
43 };
44
45 YYSTYPE yylval;
46
47
48 static short mathed_env = LM_EN_INTEXT;
49
50 char *mathed_label = NULL;
51
52 char const *latex_mathenv[] = { 
53    "math", 
54    "displaymath", 
55    "equation", 
56    "eqnarray*",
57    "eqnarray",
58    "array"
59 };
60
61
62 char const *latex_mathspace[] = {
63    "!", ",", ":", ";", "quad", "qquad"
64 };
65    
66 char const *latex_special_chars = "#$%&_{}";
67             
68 // These are lexical codes, not semantic
69 enum lexcode_enum {
70    LexNone,
71    LexESC,
72    LexAlpha,
73    LexDigit,
74    LexBOP,         // Binary operators or relations
75    LexMathSpace,
76    LexOpen,
77    LexClose,
78    LexComment,
79    LexArgument,
80    LexSpace,
81    LexNewLine,
82    LexOther,
83    LexSelf
84 };
85
86 static lexcode_enum lexcode[256];  
87 static char yytext[256];
88 static int yylineno;
89 static FILE *yyin;
90 static bool yy_mtextmode=false;
91             
92 inline
93 char *strnew(char const* s)
94 {
95         char *s1 = new char[strlen(s)+1]; // this leaks when not delete[]'ed
96         strcpy(s1, s);
97         return s1;
98 }
99
100
101 static void mathPrintError(char const *msg) 
102 {
103     fprintf(stderr, "Line ~%d: Math parse error: %s\n", yylineno, msg); 
104 }
105
106
107 static void LexInitCodes()
108 {
109    int i;
110    
111    for (i=0;  i<=255; i++)     {
112      if (isalpha(i)) lexcode[i] = LexAlpha;
113      else if (isdigit(i)) lexcode[i] = LexDigit;
114      else if (isspace(i)) lexcode[i] = LexSpace;
115      else lexcode[i] = LexNone;
116    }
117     
118    lexcode['\t'] = lexcode['\f'] = lexcode[' '] = LexSpace;
119    lexcode['\n'] = LexNewLine;
120    lexcode['%'] = LexComment;
121    lexcode['#'] = LexArgument;
122    lexcode['+'] = lexcode['-'] = lexcode['*'] = lexcode['/'] = 
123    lexcode['<'] = lexcode['>'] = lexcode['='] = LexBOP;
124    
125    lexcode['!'] = lexcode[','] = lexcode[':'] = lexcode[';'] = LexMathSpace;
126    lexcode['('] = lexcode[')'] = lexcode['|'] = lexcode['.'] = lexcode['?'] = LexOther; 
127    lexcode['\'']= LexAlpha;
128    
129    lexcode['['] = lexcode[']'] = lexcode['^'] = lexcode['_'] = 
130    lexcode['&'] = LexSelf;  
131    
132    lexcode['\\'] = LexESC;
133    lexcode['{'] = LexOpen;
134    lexcode['}'] = LexClose;
135 }
136
137 static char LexGetArg(char lf, bool accept_spaces=false)
138 {
139    char c, rg, *p = &yytext[0];
140    int bcnt =1;
141    
142    while (!feof(yyin)) {
143       c = getc(yyin); 
144       if (c>' ') {
145          if (!lf) lf = c; else
146          if (c!=lf)
147            fprintf(stderr, "Math parse error: unexpected '%c'\n", c);
148          break;
149       }
150    }
151    rg = (lf=='{') ? '}': ((lf=='[') ? ']': ((lf=='(') ? ')': 0));
152    if (!rg) {
153       fprintf(stderr, "Math parse error: unknown bracket '%c'\n", lf);
154       return '\0';
155    } 
156    do {
157       c = getc(yyin); 
158       if (c==lf) bcnt++;
159       if (c==rg) bcnt--;
160       if ((c>' ' || (c==' ' && accept_spaces)) && bcnt>0) *(p++) = c;
161    } while (bcnt>0 && !feof(yyin));
162    *p = '\0';
163    return rg;
164 }
165
166 static int yylex(void)
167 {
168    static int init_done = 0;
169    unsigned char c;
170    
171    if (!init_done) LexInitCodes();
172    
173    while (!feof(yyin)) { 
174       c = getc(yyin);
175        
176       if (yy_mtextmode && c==' ') {
177           yylval.i=' ';
178           return LM_TK_ALPHA;
179       }
180        
181        if (lexcode[c]==LexNewLine) {
182            yylineno++; 
183            continue;
184        }
185          
186       if (lexcode[c]==LexComment) 
187         do c = getc(yyin); while (c!='\n' % !feof(yyin));  // eat comments
188     
189       if (lexcode[c]==LexDigit || lexcode[c]==LexOther || lexcode[c]==LexMathSpace) 
190         { yylval.i= c; return LM_TK_STR; }
191       if (lexcode[c]==LexAlpha) { yylval.i=c; return LM_TK_ALPHA; }
192       if (lexcode[c]==LexBOP)   { yylval.i=c; return LM_TK_BOP; }
193       if (lexcode[c]==LexSelf)  { return c; }   
194       if (lexcode[c]==LexArgument)   { 
195           c = getc(yyin);
196           yylval.i = c - '0';
197           return LM_TK_ARGUMENT; 
198       }
199       if (lexcode[c]==LexOpen)   { return LM_TK_OPEN; }
200       if (lexcode[c]==LexClose)   { return LM_TK_CLOSE; }
201       
202       if (lexcode[c]==LexESC)   {
203          c = getc(yyin);
204          if (c=='\\')   { return LM_TK_NEWLINE; }
205          if (c=='(')    { yylval.i = LM_EN_INTEXT; return LM_TK_BEGIN; }
206          if (c==')')    { yylval.i = LM_EN_INTEXT; return LM_TK_END; }
207          if (c=='[')    { yylval.i = LM_EN_DISPLAY; return LM_TK_BEGIN; }
208          if (c==']')    { yylval.i = LM_EN_DISPLAY; return LM_TK_END; }
209          if (strchr(latex_special_chars, c)) {
210              yylval.i = c;
211              return LM_TK_SPECIAL;
212          }  
213          if (lexcode[c]==LexMathSpace) {
214             int i;
215             for (i=0; i<4 && c!=latex_mathspace[i][0]; i++);
216             yylval.i = (i<4) ? i: 0; 
217             return LM_TK_SPACE; 
218          }
219          if (lexcode[c]==LexAlpha || lexcode[c]==LexDigit) {
220             char* p = &yytext[0];
221             while (lexcode[c]==LexAlpha || lexcode[c]==LexDigit) {
222                *p = c;
223                c = getc(yyin);
224                p++;
225             }
226             *p = '\0';
227             if (!feof(yyin)) ungetc(c, yyin);
228             latexkeys *l = in_word_set (yytext, strlen(yytext));
229             if (l) {
230                if (l->token==LM_TK_BEGIN || l->token==LM_TK_END) { 
231                   int i;
232                   LexGetArg('{');
233 //                for (i=0; i<5 && strncmp(yytext, latex_mathenv[i],
234 //                              strlen(latex_mathenv[i])); i++);
235                   
236                   for (i=0; i<6 && strcmp(yytext, latex_mathenv[i]); i++);
237                   yylval.i = i;
238                } else
239                if (l->token==LM_TK_SPACE) 
240                  yylval.i = l->id;
241                else
242                  yylval.l = l;
243                return l->token;
244             } else { 
245                yylval.s = yytext;
246                return LM_TK_UNDEF;
247             }
248          }
249       }
250    }
251    return 0;
252 }
253
254 int parse_align(char *hor, char *)
255 {    
256    char *c;
257
258    int nc = 0;
259    for (c=hor; c && *c>' '; c++) nc++;
260    return nc;
261 }
262
263 // Accent hacks only for 0.12. Stolen from Cursor.
264 int accent = 0;
265 int nestaccent[8];
266
267 void setAccent(int ac)
268 {
269         if (ac > 0 && accent < 8) {
270                 nestaccent[accent++] = ac;
271         } else
272           accent = 0;  // consumed!
273 }
274
275
276 MathedInset *doAccent(byte c, MathedTextCodes t)
277 {
278         MathedInset *ac = 0;
279         
280         for (int i=accent-1; i>=0; i--) {
281                 if (i==accent-1)
282                   ac = new MathAccentInset(c, t, nestaccent[i]);
283                 else 
284                   ac = new MathAccentInset(ac, nestaccent[i]);
285         }
286         accent = 0;  // consumed!
287         
288         return ac;
289 }
290
291
292 MathedInset *doAccent(MathedInset *p)
293 {
294         MathedInset *ac = 0;
295         
296         for (int i=accent-1; i>=0; i--) {
297                 if (i==accent-1)
298                   ac = new MathAccentInset(p, nestaccent[i]);
299                 else 
300                   ac = new MathAccentInset(ac, nestaccent[i]);
301         }
302         accent = 0;  // consumed!
303         
304         return ac;
305 }
306
307
308
309
310 LyxArrayBase *mathed_parse(unsigned flags, LyxArrayBase *array, MathParInset **mtx)
311 {
312    int t = yylex(), tprev = 0;
313    bool panic = false;
314    static int plevel = -1;
315    static int size = LM_ST_TEXT;
316    MathedTextCodes varcode = LM_TC_VAR;
317    MathedInset* binset = 0;
318    static MathMacroTemplate *macro=0;
319    
320    int brace = 0;
321    int acc_brace = 0;
322    int acc_braces[8];
323    MathParInset *mt = (mtx) ? *mtx: 0;//(MathParInset*)NULL;
324     MathedRowSt *crow = (mt) ? mt->getRowSt(): 0;
325
326    plevel++;
327    if (!array) array = new LyxArrayBase;
328    MathedIter data(array);
329    while (t) {
330       if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
331          if ((flags & FLAG_BRACK_ARG) && t=='[') {
332          }
333          else {
334              mathPrintError("Expected {. Maybe you forgot to enclose an argument in {}");
335             panic = true;
336             break;
337          }
338       }
339     MathedInsetTypes fractype = LM_OT_FRAC;
340     switch (t) {
341     case LM_TK_ALPHA:
342       {
343          if (accent) {
344              data.Insert(doAccent(yylval.i, varcode));
345          } else
346             data.Insert (yylval.i, varcode);  //LM_TC_VAR);
347          break;
348       }
349     case LM_TK_ARGUMENT:
350       {
351           if (macro) {
352               data.Insert(macro->getMacroPar(yylval.i-1), LM_TC_INSET);
353           } 
354           break;
355       } 
356     case LM_TK_NEWCOMMAND:
357       {
358           int na = 0; 
359
360           LexGetArg('{');
361           // This name lives until quitting, for that reason
362           // I didn't care on deleting explicitly. Later I will.
363           char const *name = strnew(&yytext[1]);
364           // ugly trick to be removed soon (lyx3)
365           char c = getc(yyin);
366           ungetc(c, yyin);
367           if (c=='[') {
368               LexGetArg('[');
369               na = atoi(yytext);
370           }  
371           macro = new MathMacroTemplate(name, na);
372           flags = FLAG_BRACE|FLAG_BRACE_LAST;
373           *mtx = macro;
374           macro->SetData(array);
375           break;
376       }
377     case LM_TK_SPECIAL:
378       {   
379           data.Insert (yylval.i, LM_TC_SPECIAL);
380           break;
381       }
382     case LM_TK_STR:
383       {   
384           if (accent) {
385                   data.Insert(doAccent(yylval.i, LM_TC_CONST));
386           } else
387             data.Insert (yylval.i, LM_TC_CONST);
388           break;
389       }
390     case LM_TK_OPEN:
391       {
392         brace++;
393         if  (accent && tprev==LM_TK_ACCENT) {
394             acc_braces[acc_brace++] = brace;
395             break;
396         }
397         if (flags & FLAG_BRACE_OPT) {
398            flags &= ~FLAG_BRACE_OPT;
399            flags |= FLAG_BRACE;
400         }
401                  
402         if (flags & FLAG_BRACE)
403           flags &= ~FLAG_BRACE;
404          else {
405             data.Insert ('{', LM_TC_TEX);
406          }
407         break;
408       }
409     case LM_TK_CLOSE:
410       {
411          brace--;        
412          if (brace < 0) {
413             mathPrintError("Unmatching braces");
414             panic = true;
415             break;
416          }
417          if (acc_brace && brace==acc_braces[acc_brace-1]-1) {
418              acc_brace--;
419              break;
420          }
421          if (flags & FLAG_BRACE_FONT) {
422             varcode = LM_TC_VAR;
423             yy_mtextmode = false;
424             flags &= ~FLAG_BRACE_FONT;
425             break;
426          }
427          if (brace == 0 && (flags & FLAG_BRACE_LAST)) {
428             plevel--;
429             return array;
430          } else {
431             data.Insert ('}', LM_TC_TEX);
432          }
433          break;
434       }
435
436     case '[':
437       {
438          if (flags & FLAG_BRACK_ARG) {
439            flags &= ~FLAG_BRACK_ARG;
440            char rg=LexGetArg('[');
441            if (rg!=']') {
442               mathPrintError("Expected ']'");
443               panic = true;
444               break;
445            }       
446 //         if (arg) strcpy(arg, yytext);
447         } else
448           data.Insert ('[');
449         break;
450       }
451     case ']':
452       {
453           if (flags & FLAG_BRACK_END) {
454               plevel--;
455               return array;
456           } else
457             data.Insert (']');
458         break;
459       }
460
461     case '^':
462       {  
463          MathParInset *p = new MathParInset(size, "", LM_OT_SCRIPT);
464          LyxArrayBase * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, NULL);
465          p->SetData(ar);
466 //       fprintf(stderr, "UP[%d]", p->GetStyle());
467          data.Insert (p, LM_TC_UP);
468          break;
469       }
470     case '_':
471       {
472          MathParInset *p = new MathParInset(size, "", LM_OT_SCRIPT);
473          LyxArrayBase * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, NULL);
474          p->SetData(ar);
475          data.Insert (p, LM_TC_DOWN);
476          break;
477       }
478
479     case LM_TK_LIMIT:
480       {
481          if (binset) {
482             binset->SetLimits((bool)(yylval.l->id));
483             binset = NULL;
484          }
485          break;
486       }
487       
488     case '&':    // Tab
489       {
490          if ((flags & FLAG_END) && mt && data.getCol()<mt->GetColumns()-1) {
491              data.setNumCols(mt->GetColumns());
492              data.Insert('T', LM_TC_TAB);
493          } else 
494             mathPrintError("Unexpected tab");
495          // debug info. [made that conditional -JMarc]
496          if (lyxerr.debugging(Error::MATHED))
497                  fprintf(stderr, "%d %d\n", data.getCol(), mt->GetColumns());
498         break;
499       }
500     case LM_TK_NEWLINE:
501       {
502           if (mt && (flags & FLAG_END)) {
503               if (mt->Permit(LMPF_ALLOW_CR)) {
504                   if (crow) {
505                           crow->setNext(new MathedRowSt(mt->GetColumns()+1)); // this leaks
506                       crow = crow->getNext();
507                   }
508                   data.Insert('K', LM_TC_CR);
509               } else 
510                 mathPrintError("Unexpected newline");
511           }
512           break;
513       }
514     case LM_TK_BIGSYM:  
515       {
516          binset = new MathBigopInset(yylval.l->name,yylval.l->id);
517          data.Insert(binset);   
518          break;
519       }
520     case LM_TK_SYM:
521       {
522          if (yylval.l->id < 256) {
523             MathedTextCodes tc = MathIsBOPS(yylval.l->id) ? LM_TC_BOPS: LM_TC_SYMB;
524             if (accent) {
525                 data.Insert(doAccent(yylval.l->id, tc));
526             } else
527             data.Insert (yylval.l->id, tc);
528          } else {
529             MathFuncInset *bg = new MathFuncInset(yylval.l->name);
530              if (accent) {
531                      data.Insert(doAccent(bg));
532              } else
533              data.Insert(bg, true);     
534          }
535          break;
536       }
537     case LM_TK_BOP:
538       {
539          if (accent) {
540                  data.Insert(doAccent(yylval.i, LM_TC_BOP));
541           } else
542             data.Insert (yylval.i, LM_TC_BOP);
543          break;
544       }
545     case LM_TK_STY:
546       {
547           if (mt) {
548               mt->UserSetSize(yylval.l->id);
549           }
550           break; 
551       }
552     case LM_TK_SPACE:
553       {
554          if (yylval.i>=0) {
555             MathSpaceInset *sp = new MathSpaceInset(yylval.i);
556             data.Insert(sp);
557          }
558          break;
559       }    
560     case LM_TK_DOTS:
561       {
562          MathDotsInset *p = new MathDotsInset(yylval.l->name, yylval.l->id);
563          data.Insert(p);
564          break;
565       }     
566     case LM_TK_STACK:
567        fractype = LM_OT_STACKREL;
568     case LM_TK_FRAC:
569       {
570          MathFracInset *fc = new MathFracInset(fractype);
571          LyxArrayBase* num = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
572          LyxArrayBase* den = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
573          fc->SetData(num, den);
574          data.Insert(fc, LM_TC_ACTIVE_INSET);
575          break;
576       }
577     case LM_TK_SQRT:
578       {     
579          MathParInset *rt;
580           
581          char c = getc(yyin);
582           
583          if (c=='[') {
584              rt = new MathRootInset(size);
585              rt->setArgumentIdx(0);
586              rt->SetData(mathed_parse(FLAG_BRACK_END, 0, &rt));
587              rt->setArgumentIdx(1);
588          } else {
589              ungetc(c, yyin);
590              rt = new MathSqrtInset(size);
591          }
592          rt->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST, 0, &rt));
593          data.Insert(rt, LM_TC_ACTIVE_INSET);
594          break;
595       }
596        
597     case LM_TK_LEFT:
598       {
599          int lfd, rgd;
600          lfd=yylex();
601          if (lfd==LM_TK_SYM || lfd==LM_TK_STR || lfd==LM_TK_BOP|| lfd==LM_TK_SPECIAL)
602            lfd = (lfd==LM_TK_SYM) ? yylval.l->id: yylval.i;
603 //       fprintf(stderr, "L[%d %c]", lfd, lfd);
604          LyxArrayBase* a = mathed_parse(FLAG_RIGHT);
605          rgd=yylex();
606 //       fprintf(stderr, "R[%d]", rgd);
607          if (rgd==LM_TK_SYM || rgd==LM_TK_STR || rgd==LM_TK_BOP || rgd==LM_TK_SPECIAL)
608            rgd = (rgd==LM_TK_SYM) ? yylval.l->id: yylval.i;      
609          MathDelimInset *dl = new MathDelimInset(lfd, rgd);
610          dl->SetData(a);
611          data.Insert(dl, LM_TC_ACTIVE_INSET);
612 //       fprintf(stderr, "RL[%d %d]", lfd, rgd);
613          break;
614       }
615     case LM_TK_RIGHT:
616       {
617          if (flags & FLAG_RIGHT) { 
618             plevel--;
619             return array;
620          } else {
621             mathPrintError("Unmatched right delimiter");
622 //          panic = true;
623          }
624          break;
625       }
626
627     case LM_TK_FONT:
628       {
629          varcode = (MathedTextCodes)yylval.l->id;
630           yy_mtextmode = (bool)(varcode==LM_TC_TEXTRM);
631          flags |= (FLAG_BRACE|FLAG_BRACE_FONT);
632         break;
633       }
634     case LM_TK_WIDE:
635       {  
636          MathDecorationInset *sq = new MathDecorationInset(yylval.l->id, size);
637          sq->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
638          data.Insert(sq, LM_TC_ACTIVE_INSET);
639          break;
640       }
641       
642     case LM_TK_ACCENT: setAccent(yylval.l->id); break;
643           
644     case LM_TK_NONUM:
645       {
646           if (crow)
647             crow->setNumbered(false);
648           break;
649       }
650
651     case LM_TK_PMOD:
652     case LM_TK_FUNC:
653       {
654           MathedInset *bg = new MathFuncInset(yylval.l->name); 
655           if (accent) {
656               data.Insert(t);
657           } else
658             data.Insert(bg);
659           break;
660       }
661     case LM_TK_FUNCLIM:
662       {
663          data.Insert(new MathFuncInset(yylval.l->name, LM_OT_FUNCLIM));
664          break;
665       }
666     case LM_TK_UNDEF:
667       {
668           
669        MathMacro* p = 
670          MathMacroTable::mathMTable.getMacro(yylval.s);
671        if (p) {
672            if (accent) 
673              data.Insert(doAccent(p), p->getTCode());
674            else
675              data.Insert(p, p->getTCode());
676            for (int i=0; p->setArgumentIdx(i); i++)
677              p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
678        }
679        else {
680            MathedInset *q = new MathFuncInset(yylval.s, LM_OT_UNDEF);
681            if (accent) {
682                    data.Insert(doAccent(q));
683            } else {
684                data.Insert(q);
685            }
686        }
687          break;
688       }
689     case LM_TK_END:
690       {
691          if (mathed_env != yylval.i && yylval.i!=LM_EN_ARRAY)
692            mathPrintError("Unmatched environment");
693          // debug info [made that conditional -JMarc]
694          if (lyxerr.debugging(Error::MATHED))
695                  fprintf(stderr, "[%d]\n", yylval.i);
696          plevel--;
697          if (mt) { // && (flags & FLAG_END)) {
698             mt->SetData(array);
699             array = NULL;
700          }
701          return array;
702       }
703     case LM_TK_BEGIN:
704       {
705          if (yylval.i==LM_EN_ARRAY) {
706             char ar[120], ar2[8];
707             ar[0] = ar2[0] = '\0'; 
708             char rg=LexGetArg(0);
709             if (rg==']') {
710                strcpy(ar2, yytext);
711                rg = LexGetArg('{');
712             }
713             strcpy(ar, yytext);
714             int nc = parse_align(ar, ar2);
715             MathParInset* mm = new MathMatrixInset(nc, 0);
716             mm->SetAlign(ar2[0], ar);
717             data.Insert(mm, LM_TC_ACTIVE_INSET);
718             mathed_parse(FLAG_END, mm->GetData(), &mm);
719          } else
720          if (yylval.i>=LM_EN_INTEXT && yylval.i<=LM_EN_EQNARRAY) {
721              if (plevel!=0) {
722                  mathPrintError("Misplaced environment");
723                  break;
724              }
725              if (!mt) {
726                  mathPrintError("NULL paragraph.");
727                  panic = true;
728              }
729              
730              mathed_env = yylval.i;
731              if (mathed_env>=LM_EN_DISPLAY) {
732                  size = LM_ST_DISPLAY;
733                  if (mathed_env>LM_EN_EQUATION) {
734                      mt = new MathMatrixInset(3, -1);
735                      mt->SetAlign(' ', "rcl");
736                      if (mtx) *mtx = mt;
737                      flags |= FLAG_END;
738 //                   data.Insert(' ', LM_TC_TAB);
739 //                   data.Insert(' ', LM_TC_TAB);
740 //                   data.Reset();
741                  }
742                  mt->SetStyle(size);
743                  mt->SetType(mathed_env);
744                  crow = mt->getRowSt();
745              }
746                                
747 #ifdef DEBUG
748                fprintf(stderr, "MATH BEGIN[%d]\n", mathed_env);
749 #endif
750          } else {
751 //           fprintf(stderr, "MATHCRO[%s]",yytext);
752              MathMacro* p = 
753                MathMacroTable::mathMTable.getMacro(yytext);
754              if (p) {
755                  data.Insert(p, p->getTCode());
756                  p->setArgumentIdx(0);
757                  mathed_parse(FLAG_END, p->GetData(), (MathParInset**)&p);
758 //               for (int i=0; p->setArgumentIdx(i); i++)
759 //                 p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
760              } else 
761                mathPrintError("Unrecognized environment");
762          }
763          break;
764       }
765        
766     case LM_TK_MACRO:
767      { 
768           MathedInset* p = 
769             MathMacroTable::mathMTable.getMacro(yylval.l->name);
770          
771           if (p) {
772               if (accent) {
773                 data.Insert(doAccent(p));
774               } else
775                 data.Insert(p, ((MathMacro*)p)->getTCode());
776           }
777           break;
778       }
779        
780      case LM_TK_LABEL:
781        {           
782           char rg = LexGetArg('\0', true);
783           if (rg != '}') {
784              mathPrintError("Expected '{'");
785               // debug info
786               fprintf(stderr, "[%s]\n", yytext); fflush(stderr);
787               panic = true;
788              break;
789           } 
790           if (crow) {
791               // This is removed by crow's destructor. Bad design? yes, this 
792               // will be changed after 0.12
793               crow->setLabel(strnew(yytext));
794           }
795           else {
796                   // where is this math_label free'ed?
797                   // Supposedly in ~formula, another bad hack,
798                   // give me some time please.
799                   mathed_label = strnew(yytext);
800           }
801 #ifdef DEBUG
802           fprintf(stderr, "Label[%d]\n", mathed_label);
803 #endif
804           break;
805         } 
806      default:
807        mathPrintError("Unrecognized token");
808        // debug info
809        fprintf(stderr, "[%d %s]\n", t, yytext);
810        break;
811     }
812     tprev = t;
813     if (panic) {
814        fprintf(stderr, " Math Panic, expect problems!\n"); 
815        //   Search for the end command. 
816        do t = yylex (); while (t != LM_TK_END && t);
817     } else
818      t = yylex ();
819    
820    if ((flags & FLAG_BRACE_OPT)/* && t!='^' && t!='_'*/) {
821         flags &= ~FLAG_BRACE_OPT;
822        //data.Insert (LM_TC_CLOSE);
823        break;
824     }
825    }
826    plevel--;
827    return array;
828 }
829
830
831 void mathed_parser_file(FILE* file, int lineno)
832 {
833     yyin = file;
834     yylineno = lineno;
835     if (!MathMacroTable::built)
836         MathMacroTable::mathMTable.builtinMacros();
837 }
838
839
840 int mathed_parser_lineno()
841 {
842     return yylineno;
843 }
844