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