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