]> git.lyx.org Git - lyx.git/blob - src/mathed/math_parser.C
support for \land and \vee, cosmetics
[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 // {[(
19
20 #include <config.h>
21
22 #include <cctype>
23
24 #ifdef __GNUG__
25 #pragma implementation
26 #endif
27
28 #include "math_parser.h"
29 #include "array.h"
30 #include "math_inset.h"
31 #include "math_arrayinset.h"
32 #include "math_bigopinset.h"
33 #include "math_dotsinset.h"
34 #include "math_decorationinset.h"
35 #include "math_deliminset.h"
36 #include "math_fracinset.h"
37 #include "math_funcinset.h"
38 #include "math_macro.h"
39 #include "math_macrotable.h"
40 #include "math_macrotemplate.h"
41 #include "math_matrixinset.h"
42 #include "math_rootinset.h"
43 #include "math_scriptinset.h"
44 #include "math_sizeinset.h"
45 #include "math_spaceinset.h"
46 #include "math_sqrtinset.h"
47 #include "debug.h"
48 #include "mathed/support.h"
49 #include "lyxlex.h"
50
51 using std::istream;
52 using std::endl;
53
54
55
56 // These are lexical codes, not semantic
57 enum lexcode_enum {
58         LexNone,
59         LexESC,
60         LexAlpha,
61         LexDigit,
62         LexBOP,         // Binary operators or relations
63         LexMathSpace,
64         LexOpen,
65         LexClose,
66         LexComment,
67         LexArgument,
68         LexSpace,
69         LexNewLine,
70         LexOther,
71         LexSelf
72 };
73
74 lexcode_enum lexcode[256];  
75
76
77 char const * latex_special_chars = "#$%&_{}";
78
79
80 namespace {
81
82 void mathed_parse(MathArray & array, unsigned flags);
83
84 unsigned char getuchar(std::istream * is)
85 {
86         char c;
87         is->get(c);
88         return static_cast<unsigned char>(c);
89 }
90
91 const unsigned char LM_TK_OPEN  = '{';
92 const unsigned char LM_TK_CLOSE = '}';
93
94 enum {
95         FLAG_BRACE      = 1 << 0,  //  A { needed              //}
96         FLAG_BRACE_LAST = 1 << 1,  //  // { Last } ends the parsing process
97         FLAG_RIGHT      = 1 << 2,  //  Next right ends the parsing process
98         FLAG_END        = 1 << 3,  //  Next end ends the parsing process
99         FLAG_BRACE_FONT = 1 << 4,  //  // { Next } closes a font
100         FLAG_BRACK_END  = 1 << 5,  //  // [ Next ] ends the parsing process
101         FLAG_AMPERSAND  = 1 << 6,  //  Next & ends the parsing process
102         FLAG_NEWLINE    = 1 << 7,  //  Next \\ ends the parsing process
103         FLAG_ITEM       = 1 << 8,  //  read a (possibly braced token)
104         FLAG_LEAVE      = 1 << 9,  //  marker for leaving the 
105         FLAG_OPTARG     = 1 << 10  //  reads an argument in []
106 };
107
108 ///
109 union {
110         ///
111         int i;
112         ///
113         latexkeys const * l;
114 } yylval;
115
116
117
118 string yytext;
119 int yylineno;
120 istream * yyis;
121 MathTextCodes yyvarcode;
122
123
124
125 struct latex_mathenv_type {
126         char const *      name;
127         char const *      basename;
128         MathInsetTypes    typ;
129         bool              numbered;
130         bool              ams;
131 };
132
133 latex_mathenv_type latex_mathenv[] = { 
134         {"math",         "math",         LM_OT_SIMPLE,   0, 0},
135         {"equation*",    "equation",     LM_OT_EQUATION, 0, 0},
136         {"equation",     "equation",     LM_OT_EQUATION, 1, 0},
137         {"eqnarray*",    "eqnarray",     LM_OT_EQNARRAY, 0, 0},
138         {"eqnarray",     "eqnarray",     LM_OT_EQNARRAY, 1, 0},
139         {"align*",       "align",        LM_OT_ALIGN,    0, 1},
140         {"align",        "align",        LM_OT_ALIGN,    1, 1},
141         {"alignat*",     "alignat",      LM_OT_ALIGNAT,  0, 1},
142         {"alignat",      "alignat",      LM_OT_ALIGNAT,  1, 1},
143         {"multline*",    "multline",     LM_OT_MULTLINE, 0, 1},
144         {"multline",     "multline",     LM_OT_MULTLINE, 1, 1},
145         {"array",        "array",        LM_OT_MATRIX,   0, 1}
146 };
147
148 int const latex_mathenv_num = sizeof(latex_mathenv)/sizeof(latex_mathenv[0]);
149
150
151
152 void mathPrintError(string const & msg) 
153 {
154         //lyxerr[Debug::MATHED] << "Line ~" << yylineno << ": Math parse error: " << msg << endl;
155         lyxerr << "Line ~" << yylineno << ": Math parse error: " << msg << endl;
156 }
157
158
159 void lexInit()
160 {
161         for (int i = 0; i <= 255; ++i) {
162                 if (isdigit(i))
163                         lexcode[i] = LexDigit;
164                 else if (isspace(i))
165                         lexcode[i] = LexSpace;
166                 else
167                         lexcode[i] = LexAlpha;
168         }
169         
170         lexcode['\t'] = lexcode['\f'] = lexcode[' '] = LexSpace;
171         lexcode['\n'] = LexNewLine;
172         lexcode['%'] = LexComment;
173         lexcode['#'] = LexArgument;
174         lexcode['+'] = lexcode['-'] = lexcode['*'] = lexcode['/']
175                 = lexcode['<'] = lexcode['>'] = lexcode['='] = LexBOP;
176         
177         lexcode['!'] = lexcode[','] = lexcode[':']
178                 = lexcode[';'] = LexMathSpace;
179         
180         lexcode['('] = lexcode[')'] = lexcode['|'] = lexcode['.'] =
181                 lexcode['?'] = LexOther; 
182         
183         lexcode['\''] = lexcode['@'] = LexAlpha;
184         
185         lexcode['['] = lexcode[']'] = lexcode['^'] = lexcode['_'] = 
186                 lexcode['&'] = LexSelf;  
187         
188         lexcode['\\'] = LexESC;
189         lexcode['{'] = LexOpen;
190         lexcode['}'] = LexClose;
191 }
192
193
194 string lexArg(unsigned char lf, bool accept_spaces = false)
195 {
196         string result;
197         unsigned char c = 0;
198         while (yyis->good()) {
199                 c = getuchar(yyis);
200                 if (!isspace(c))
201                         break;
202         }
203
204         if (c != lf) {
205                 yyis->putback(c);
206                 return result;
207         }
208                 
209         unsigned char rg = 0;
210         if (lf == '{') rg = '}';
211         if (lf == '[') rg = ']';
212         if (lf == '(') rg = ')';
213         if (!rg) {
214                 lyxerr[Debug::MATHED] << "Math parse error: unknown bracket '"
215                         << lf << "'" << endl;
216                 return result;
217         }
218
219         int depth = 1;
220         do {
221                 unsigned char c = getuchar(yyis);
222                 if (c == lf)
223                         ++depth;
224                 if (c == rg)
225                         --depth;
226                 if ((!isspace(c) || (c == ' ' && accept_spaces)) && depth > 0)
227                         result += c;
228         } while (depth > 0 && yyis->good());
229
230         return result;
231 }
232
233
234 int yylex()
235 {
236         static bool init_done = false;
237         
238         if (!init_done) {
239                 lexInit();
240                 init_done = true;
241         }
242         
243         while (yyis->good()) {
244                 unsigned char c = getuchar(yyis);
245                 //lyxerr << "reading byte: '" << c << "' code: " << lexcode[c] << endl;
246                 
247                 if (yyvarcode == LM_TC_TEXTRM && c == ' ') {
248                         yylval.i = ' ';
249                         return LM_TK_ALPHA;
250                 } else if (lexcode[c] == LexNewLine) {
251                         ++yylineno; 
252                         continue;
253                 } else if (lexcode[c] == LexComment) {
254                         do {
255                                 c = getuchar(yyis);
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                         c = getuchar(yyis);
272                         yylval.i = c - '0';
273                         return LM_TK_ARGUMENT; 
274                 } else if (lexcode[c] == LexOpen) {
275                         return LM_TK_OPEN;
276                 } else if (lexcode[c] == LexClose) {
277                         return LM_TK_CLOSE;
278                 } else if (lexcode[c] == LexESC)   {
279                         c = getuchar(yyis);
280                         if (c == '\\')  {
281                                 yylval.i = -1;
282                                 return LM_TK_NEWLINE;
283                         }
284                         if (c == '(') {
285                                 yylval.i = LM_OT_SIMPLE;
286                                 return LM_TK_BEGIN;
287                         }
288                         if (c == ')') {
289                                 yylval.i = LM_OT_SIMPLE;
290                                 return LM_TK_END;
291                         }
292                         if (c == '[') {
293                                 yylval.i = LM_OT_EQUATION;
294                                 return LM_TK_BEGIN;
295                         }
296                         if (c == ']') {
297                                 yylval.i = LM_OT_EQUATION;
298                                 return LM_TK_END;
299                         }
300                         if (contains(latex_special_chars, c)) {
301                                 yylval.i = c;
302                                 return LM_TK_SPECIAL;
303                         } 
304                         if (lexcode[c] == LexMathSpace) {
305                                 int i;
306                                 for (i = 0; i < 4 && static_cast<int>(c) != latex_mathspace[i][0]; ++i)
307                                         ;
308                                 yylval.i = (i < 4) ? i : 0; 
309                                 return LM_TK_SPACE; 
310                         }
311                         if (lexcode[c] == LexAlpha) {
312                                 yytext.erase();
313                                 while (lexcode[c] == LexAlpha) {
314                                         yytext += c;
315                                         c = getuchar(yyis);
316                                 }
317                                 if (yyis->good())
318                                         yyis->putback(c);
319                                 //lyxerr[Debug::MATHED] << "reading: text '" << yytext << "'\n";
320                                 latexkeys const * l = in_word_set(yytext);
321                                 if (!l)
322                                         return LM_TK_UNDEF;
323
324                                 if (l->token == LM_TK_BEGIN || l->token == LM_TK_END) { 
325                                         string name = lexArg('{');
326                                         int i = 0;
327                                         while (i < latex_mathenv_num && name != latex_mathenv[i].name)
328                                                  ++i;
329                                         yylval.i = i;
330                                 } else if (l->token == LM_TK_SPACE) 
331                                         yylval.i = l->id;
332                                 else
333                                         yylval.l = l;
334                                 return l->token;
335                         }
336                 }
337         }
338         return 0;
339 }
340
341
342 MathInset * lastUpDownInset(MathArray & array, bool up, bool down)
343 {
344         MathInset * p = array.back_inset();
345         if (!p || !p->isUpDownInset()) {
346                 p = new MathScriptInset(up, down);
347                 array.push_back(p);
348         }
349         MathUpDownInset * q = static_cast<MathScriptInset *>(p);
350         if (up)
351                 q->up(true);
352         if (down)
353                 q->down(down);
354         return p;
355 }
356
357
358 MathBigopInset * lastBigopInset(MathArray & array)
359 {
360         MathInset * p = array.back_inset();
361         return (p && p->isBigopInset()) ? static_cast<MathBigopInset *>(p) : 0;
362 }
363
364
365
366 static bool   curr_num;
367 static string curr_label;
368
369 void mathed_parse_lines(MathInset * inset, int col, bool numbered, bool outmost)
370 {
371         // save global variables
372         bool   saved_num   = curr_num;
373         string saved_label = curr_label;
374
375         MathGridInset * p = static_cast<MathGridInset *>(inset);
376         for (int row = 0; true; ++row) {
377                 // reset global variables
378                 curr_num   = numbered;
379                 curr_label = string();
380
381                 // reading a row
382                 int idx = p->nargs() - p->ncols();
383                 for (int i = 0; i < col - 1; ++i, ++idx)
384                         mathed_parse(p->cell(idx), FLAG_AMPERSAND);
385                 mathed_parse(p->cell(idx), FLAG_NEWLINE | FLAG_END);
386
387                 if (outmost) {
388                         MathMatrixInset * m = static_cast<MathMatrixInset *>(p);
389                         m->numbered(row, curr_num);
390                         m->label(row, curr_label);
391                 }
392
393                 // Hack!
394                 // no newline
395                 if (yylval.i != -1)
396                         break;
397
398                 p->appendRow();
399         }
400
401         // restore global variables
402         curr_num   = saved_num;
403         curr_label = saved_label;
404 }
405
406
407 MathInset * mathed_parse()
408 {
409         MathInset * p = 0;
410         int t = yylex();
411
412         switch (t) {
413                 case LM_TK_NEWCOMMAND: {
414                         string name = lexArg('{').substr(1);
415                         string arg  = lexArg('[');
416                         int    narg = arg.empty() ? 0 : atoi(arg.c_str()); 
417                         p = new MathMacroTemplate(name, narg);
418                         mathed_parse(p->cell(0), FLAG_BRACE | FLAG_BRACE_LAST);
419                         //lyxerr[Debug::MATHED] << "LM_TK_NEWCOMMAND: name: "
420                         //      << name << " nargs: " << narg << "\n";
421                         break;
422                 }
423
424                 case LM_TK_BEGIN: {
425                         int i = yylval.i;
426                         //lyxerr[Debug::MATHED] << "reading math environment " << i << " "
427                         //      << latex_mathenv[i].name << "\n";
428
429                         MathInsetTypes typ = latex_mathenv[i].typ;
430                         p = new MathMatrixInset(typ);
431                         MathMatrixInset * m = static_cast<MathMatrixInset *>(p);
432                         switch (typ) {
433
434                                 case LM_OT_SIMPLE: {
435                                         curr_num   = latex_mathenv[i].numbered;
436                                         curr_label = string();
437                                         mathed_parse(m->cell(0), 0);
438                                         m->numbered(0, curr_num);
439                                         m->label(0, curr_label);
440                                         break;
441                                 }
442
443                                 case LM_OT_EQUATION: {
444                                         curr_num   = latex_mathenv[i].numbered;
445                                         curr_label = string();
446                                         mathed_parse(m->cell(0), FLAG_END);
447                                         m->numbered(0, curr_num);
448                                         m->label(0, curr_label);
449                                         break;
450                                 }
451
452                                 case LM_OT_EQNARRAY: {
453                                         mathed_parse_lines(m, 3, latex_mathenv[i].numbered, true);
454                                         break;
455                                 }
456
457                                 case LM_OT_ALIGN: {
458                                         m->halign(lexArg('{'));
459                                         mathed_parse_lines(m, 2, latex_mathenv[i].numbered, true);
460                                         break;
461                                 }
462
463                                 case LM_OT_ALIGNAT: {
464                                         m->halign(lexArg('{'));
465                                         mathed_parse_lines(m, 2, latex_mathenv[i].numbered, true);
466                                         break;
467                                 }
468
469                                 default: 
470                                         lyxerr[Debug::MATHED] << "1: unknown math environment: " << typ << "\n";
471                         }
472
473                         p->SetName(latex_mathenv[i].basename);
474
475                         break;
476                 }
477                 
478                 default:
479                         lyxerr[Debug::MATHED] << "2 unknown math environment: " << t << "\n";
480         }
481
482         return p;
483 }
484
485
486 void handle_frac(MathArray & array, string const & name)
487 {
488         MathFracInset * p = new MathFracInset(name);
489         mathed_parse(p->cell(0), FLAG_ITEM);
490         mathed_parse(p->cell(1), FLAG_ITEM);
491         array.push_back(p);
492 }
493
494
495 void mathed_parse(MathArray & array, unsigned flags)
496 {
497         int t = yylex();
498         bool panic = false;
499         static int plevel = -1;
500         yyvarcode = LM_TC_VAR;
501         
502         int brace = 0;
503
504         ++plevel;
505         while (t) {
506                 //lyxerr << "t: " << t << " flags: " << flags << " i: " << yylval.i << " "
507                 //      << " plevel: " << plevel << " ";
508                 //array.dump(lyxerr);
509                 //lyxerr << "\n";
510
511                 if (flags & FLAG_ITEM) {
512                         flags &= ~FLAG_ITEM;
513                         if (t == LM_TK_OPEN) { 
514                                 // skip the brace and regard everything to the next matching
515                                 // closing brace
516                                 t = yylex();
517                                 ++brace;
518                                 flags |= FLAG_BRACE_LAST;
519                         } else {
520                                 // regard only this single token
521                                 flags |= FLAG_LEAVE;
522                         }
523                 }
524
525                 if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
526                         mathPrintError(
527                                 "Expected {. Maybe you forgot to enclose an argument in {}");
528                         panic = true;
529                         break;
530                 }
531
532                 switch (t) {
533                         
534                 case LM_TK_ALPHA:
535                         array.push_back(yylval.i, yyvarcode);
536                         break;
537
538                 case LM_TK_ARGUMENT:
539                         array.push_back(new MathMacroArgument(yylval.i));
540                         break;
541
542                 case LM_TK_SPECIAL:
543                         array.push_back(yylval.i, LM_TC_SPECIAL);
544                         break;
545
546                 case LM_TK_STR:
547                         array.push_back(yylval.i, LM_TC_CONST);
548                         break;
549
550                 case LM_TK_OPEN:
551                         ++brace;
552                         if (flags & FLAG_BRACE)
553                                 flags &= ~FLAG_BRACE;
554                         else 
555                                 array.push_back('{', LM_TC_TEX);
556                         break;
557
558                 case LM_TK_CLOSE:
559                         --brace;         
560                         if (brace < 0) {
561                                 mathPrintError("Unmatching braces");
562                                 panic = true;
563                                 break;
564                         }
565                         if (flags & FLAG_BRACE_FONT) {
566                                 yyvarcode = LM_TC_VAR;
567                                 flags &= ~FLAG_BRACE_FONT;
568                                 break;
569                         }
570                         if (brace == 0 && (flags & FLAG_BRACE_LAST))
571                                 flags |= FLAG_LEAVE;
572                         else
573                                 array.push_back('}', LM_TC_TEX);
574                         break;
575                 
576                 case '[':
577                         array.push_back('[', LM_TC_CONST);
578                         break;
579
580                 case ']':
581                         if (flags & FLAG_BRACK_END)
582                                 flags |= FLAG_LEAVE;
583                         else 
584                                 array.push_back(']', LM_TC_CONST);
585                         break;
586                 
587                 case '^':
588                         mathed_parse(lastUpDownInset(array, true, false)->cell(0), FLAG_ITEM);
589                         break;
590                 
591                 case '_':
592                         mathed_parse(lastUpDownInset(array, false, true)->cell(1), FLAG_ITEM);
593                         break;
594                 
595                 case LM_TK_LIMIT:
596                 {
597                         MathBigopInset * p = lastBigopInset(array);
598                         if (p) 
599                                 p->limits(yylval.l->id ? 1 : -1);
600                         break;
601                 }
602                 
603                 case '&':
604                 {
605                         if (flags & FLAG_AMPERSAND) {
606                                 flags &= ~FLAG_AMPERSAND;
607                                 --plevel;
608                                 return;
609                         }
610                         lyxerr[Debug::MATHED] << "found tab unexpectedly, array: '" << array << "'\n";
611                         break;
612                 }
613                 
614                 case LM_TK_NEWLINE:
615                 {
616                         if (flags & FLAG_NEWLINE) {
617                                 flags &= ~FLAG_NEWLINE;
618                                 --plevel;
619                                 return;
620                         }
621                         lyxerr[Debug::MATHED] << "found newline unexpectedly, array: '" << array << "'\n";
622                         break;
623                 }
624                 
625                 case LM_TK_BIGSYM:  
626                 {
627                         array.push_back(new MathBigopInset(yylval.l->name, yylval.l->id));
628                         break;
629                 }
630                 
631                 case LM_TK_SYM:
632                         if (yylval.l->id < 256) {
633                                 MathTextCodes tc = MathIsBOPS(yylval.l->id) ? LM_TC_BOPS : LM_TC_SYMB;
634                                 array.push_back(yylval.l->id, tc);
635                         } else 
636                                 array.push_back(new MathFuncInset(yylval.l->name));
637                         break;
638
639                 case LM_TK_BOP:
640                         array.push_back(yylval.i, LM_TC_BOP);
641                         break;
642
643                 case LM_TK_SPACE:
644                         if (yylval.i >= 0) 
645                                 array.push_back(new MathSpaceInset(yylval.i));
646                         break;
647
648                 case LM_TK_DOTS:
649                         array.push_back(new MathDotsInset(yylval.l->name, yylval.l->id));
650                         break;
651                 
652                 case LM_TK_CHOOSE:
653                         handle_frac(array, "atop");     
654                         break;
655
656                 case LM_TK_STACK:
657                         handle_frac(array, "stackrel"); 
658                         break;
659
660                 case LM_TK_FRAC:
661                         handle_frac(array, "frac");     
662                         break;
663
664                 case LM_TK_SQRT:
665                 {
666                         unsigned char c = getuchar(yyis);
667                         if (c == '[') {
668                                 array.push_back(new MathRootInset);
669                                 mathed_parse(array.back_inset()->cell(0), FLAG_BRACK_END);
670                                 mathed_parse(array.back_inset()->cell(1), FLAG_ITEM);
671                         } else {
672                                 yyis->putback(c);
673                                 array.push_back(new MathSqrtInset);
674                                 mathed_parse(array.back_inset()->cell(0), FLAG_ITEM);
675                         }
676                         break;
677                 }
678                 
679                 case LM_TK_LEFT:
680                 {
681                         int ld = yylex();
682                         if (ld == LM_TK_SYM)
683                                 ld = yylval.l->id;
684                         else if (ld == LM_TK_STR || ld == LM_TK_BOP || ld == LM_TK_SPECIAL)
685                                 ld = yylval.i;
686
687                         MathArray ar;
688                         mathed_parse(ar, FLAG_RIGHT);
689
690                         int rd = yylex();
691                         if (rd == LM_TK_SYM)
692                                 rd = yylval.l->id;
693                         else if (rd == LM_TK_STR || rd == LM_TK_BOP || rd == LM_TK_SPECIAL)
694                                 rd = yylval.i;   
695
696                         MathDelimInset * dl = new MathDelimInset(ld, rd);
697                         dl->cell(0) = ar;
698                         array.push_back(dl);
699                         break;
700                 }
701                 
702                 case LM_TK_RIGHT:
703                         if (flags & FLAG_RIGHT) { 
704                                 --plevel;
705                                 return;
706                         }
707                         mathPrintError("Unmatched right delimiter");
708 //        panic = true;
709                         break;
710                 
711                 case LM_TK_FONT:
712                         yyvarcode = static_cast<MathTextCodes>(yylval.l->id);
713                         flags |= (FLAG_BRACE | FLAG_BRACE_FONT);
714                         break;
715
716                 case LM_TK_STY:
717                 {
718                         lyxerr[Debug::MATHED] << "LM_TK_STY not implemented\n";
719                         //MathArray tmp = array;
720                         //MathSizeInset * p = new MathSizeInset(MathStyles(yylval.l->id));
721                         //array.push_back(p);
722                         //mathed_parse(p->cell(0), FLAG_BRACE_FONT);
723                         break; 
724                 }
725
726
727                 case LM_TK_DECORATION:
728                 {  
729                         MathDecorationInset * p
730                                 = new MathDecorationInset(yylval.l->name, yylval.l->id);
731                         mathed_parse(p->cell(0), FLAG_ITEM);
732                         array.push_back(p);
733                         break;
734                 }
735                         
736                 case LM_TK_NONUM:
737                         curr_num = false;
738                         break;
739                 
740                 case LM_TK_PMOD:
741                 case LM_TK_FUNC:
742                         array.push_back(new MathFuncInset(yylval.l->name));
743                         break;
744                 
745                 case LM_TK_FUNCLIM:
746                         array.push_back(new MathFuncInset(yylval.l->name, LM_OT_FUNCLIM));
747                         break;
748
749                 case LM_TK_UNDEF: 
750                         if (MathMacroTable::hasTemplate(yytext)) {
751                                 MathMacro * m = MathMacroTable::cloneTemplate(yytext);
752                                 for (int i = 0; i < m->nargs(); ++i) 
753                                         mathed_parse(m->cell(i), FLAG_ITEM);
754                                 array.push_back(m);
755                                 m->Metrics(LM_ST_TEXT);
756                         } else
757                                 array.push_back(new MathFuncInset(yytext, LM_OT_UNDEF));
758                         break;
759                 
760                 case LM_TK_END:
761                         --plevel;
762                         return;
763
764                 case LM_TK_BEGIN:
765                 {
766                         int i = yylval.i;
767                         MathInsetTypes typ = latex_mathenv[i].typ;
768
769                         if (typ == LM_OT_MATRIX) {
770                                 string valign = lexArg('[') + 'c';
771                                 string halign = lexArg('{');
772                                 //lyxerr << "valign: '" << valign << "'\n";
773                                 //lyxerr << "halign: '" << halign << "'\n";
774                                 MathArrayInset * m = new MathArrayInset(halign.size(), 1);
775                                 m->valign(valign[0]);
776                                 m->halign(halign);
777
778                                 mathed_parse_lines(m, halign.size(), latex_mathenv[i].numbered, false);
779                                 array.push_back(m);
780                                 //lyxerr << "read matrix " << *m << "\n";       
781                                 break;
782                         } else 
783                                 lyxerr[Debug::MATHED] << "unknow math inset " << typ << "\n";   
784                         break;
785                 }
786         
787                 case LM_TK_MACRO:
788                         array.push_back(MathMacroTable::cloneTemplate(yylval.l->name));
789                         break;
790                 
791                 case LM_TK_LABEL:
792                         curr_label = lexArg('{', true);
793                         break;
794                 
795                 default:
796                         mathPrintError("Unrecognized token");
797                         lyxerr[Debug::MATHED] << "[" << t << " " << yytext << "]" << endl;
798                         break;
799
800                 } // end of big switch
801
802                 if (flags & FLAG_LEAVE) {
803                         flags &= ~FLAG_LEAVE;
804                         break;
805                 }
806
807                 if (panic) {
808                         lyxerr << " Math Panic, expect problems!" << endl;
809                         //   Search for the end command. 
810                         do {
811                                 t = yylex();
812                         } while (t != LM_TK_END && t);
813                 } else
814                         t = yylex();
815
816         }
817         --plevel;
818 }
819
820 }
821
822 MathInset * mathed_parse(string const & str)
823 {
824         istringstream is(str.c_str());
825         return mathed_parse(is);
826 }
827
828
829 MathInset * mathed_parse(istream & is)
830 {
831         yyis     = &is;
832         yylineno = 0;
833         return mathed_parse();
834 }
835
836
837 MathInset * mathed_parse(LyXLex & lex)
838 {
839         yyis     = &lex.getStream();
840         yylineno = lex.GetLineNo();
841
842         MathInset * p = mathed_parse();
843
844         // Update line number
845         lex.setLineNo(yylineno);
846
847         // reading of end_inset
848         while (lex.IsOK()) {
849                 lex.nextToken();
850                 if (lex.GetString() == "\\end_inset")
851                         break;
852                 lyxerr[Debug::MATHED] << "InsetFormula::Read: Garbage before \\end_inset,"
853                         " or missing \\end_inset!" << endl;
854         }
855
856         return p;
857 }
858
859 //]})