]> git.lyx.org Git - lyx.git/blob - src/mathed/math_parser.C
some support for \atop
[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_charinset.h"
31 #include "math_deliminset.h"
32 #include "math_factory.h"
33 #include "math_funcinset.h"
34 #include "math_macro.h"
35 #include "math_macrotable.h"
36 #include "math_macrotemplate.h"
37 #include "math_matrixinset.h"
38 #include "math_rootinset.h"
39 #include "math_scopeinset.h"
40 #include "math_sqrtinset.h"
41 #include "math_scriptinset.h"
42 #include "math_sqrtinset.h"
43 #include "debug.h"
44 #include "support.h"
45 #include "lyxlex.h"
46 #include "support/lstrings.h"
47
48 using std::istream;
49 using std::ostream;
50 using std::ios;
51 using std::endl;
52
53
54 namespace {
55
56 bool stared(string const & s)
57 {
58         unsigned n = s.size();
59         return n && s[n - 1] == '*';
60 }
61
62 MathScriptInset * prevScriptInset(MathArray const & array)
63 {
64         MathInset * p = array.back();
65         return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
66 }
67
68
69 MathInset * lastScriptInset(MathArray & array, bool up, int limits)
70 {
71         MathScriptInset * p = prevScriptInset(array);
72         if (!p) {
73                 MathInset * b = array.back();
74                 if (b && b->isScriptable()) {
75                         p = new MathScriptInset(up, !up, b->clone());
76                         array.pop_back();       
77                 } else {
78                         p = new MathScriptInset(up, !up);
79                 }
80                 array.push_back(p);
81         }
82         if (up)
83                 p->up(true);
84         else
85                 p->down(true);
86         if (limits)
87                 p->limits(limits);
88         return p;
89 }
90
91
92 // These are TeX's catcodes
93 enum CatCode {
94         catEscape,     // 0    backslash 
95         catBegin,      // 1    {
96         catEnd,        // 2    }
97         catMath,       // 3    $
98         catAlign,      // 4    &
99         catNewline,    // 5    ^^M
100         catParameter,  // 6    #
101         catSuper,      // 7    ^
102         catSub,        // 8    _
103         catIgnore,     // 9       
104         catSpace,      // 10   space
105         catLetter,     // 11   a-zA-Z
106         catOther,      // 12   none of the above
107         catActive,     // 13   ~
108         catComment,    // 14   %
109         catInvalid     // 15   <delete>
110 };
111
112 CatCode theCatcode[256];  
113
114
115 inline CatCode catcode(unsigned char c)
116 {
117         return theCatcode[c];
118 }
119
120
121 enum {
122         FLAG_BRACE      = 1 << 0,  //  an opening brace needed
123         FLAG_BRACE_LAST = 1 << 1,  //  last closing brace ends the parsing process
124         FLAG_RIGHT      = 1 << 2,  //  next \\right ends the parsing process
125         FLAG_END        = 1 << 3,  //  next \\end ends the parsing process
126         FLAG_BRACK_END  = 1 << 4,  //  next closing bracket ends the parsing process
127         FLAG_NEWLINE    = 1 << 6,  //  next \\\\ ends the parsing process
128         FLAG_ITEM       = 1 << 7,  //  read a (possibly braced token)
129         FLAG_BLOCK      = 1 << 8,  //  next block ends the parsing process
130         FLAG_LEAVE      = 1 << 9   //  leave the loop at the end
131 };
132
133
134 void catInit()
135 {
136         for (int i = 0; i <= 255; ++i) 
137                 theCatcode[i] = catOther;
138         for (int i = 'a'; i <= 'z'; ++i) 
139                 theCatcode[i] = catLetter;
140         for (int i = 'A'; i <= 'Z'; ++i) 
141                 theCatcode[i] = catLetter;
142
143         theCatcode['\\'] = catEscape;   
144         theCatcode['{']  = catBegin;    
145         theCatcode['}']  = catEnd;      
146         theCatcode['$']  = catMath;     
147         theCatcode['&']  = catAlign;    
148         theCatcode['\n'] = catNewline;  
149         theCatcode['#']  = catParameter;        
150         theCatcode['^']  = catSuper;    
151         theCatcode['_']  = catSub;      
152         theCatcode['\7f'] = catIgnore;    
153         theCatcode[' ']  = catSpace;    
154         theCatcode['\t'] = catSpace;    
155         theCatcode['\r'] = catSpace;    
156         theCatcode['~']  = catActive;   
157         theCatcode['%']  = catComment;  
158 }
159
160
161
162 //
163 // Helper class for parsing
164 //
165
166 class Token {
167 public:
168         ///
169         Token() : cs_(), char_(0), cat_(catIgnore) {}
170         ///
171         Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
172         ///
173         Token(const string & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
174
175         ///
176         string const & cs() const { return cs_; }
177         ///
178         CatCode cat() const { return cat_; }
179         ///
180         char character() const { return char_; }
181         ///
182         string asString() const;
183
184 private:        
185         ///
186         string cs_;
187         ///
188         char char_;
189         ///
190         CatCode cat_;
191 };
192
193 string Token::asString() const
194 {
195         return cs_.size() ? cs_ : string(1, char_);
196 }
197
198 bool operator==(Token const & s, Token const & t)
199 {
200         return s.character() == t.character()
201                 && s.cat() == t.cat() && s.cs() == t.cs(); 
202 }
203
204 bool operator!=(Token const & s, Token const & t)
205 {
206         return !(s == t);
207 }
208
209 ostream & operator<<(ostream & os, Token const & t)
210 {
211         if (t.cs().size())
212                 os << "\\" << t.cs();
213         else
214                 os << "[" << t.character() << "," << t.cat() << "]";
215         return os;
216 }
217
218
219 class Parser {
220
221 public:
222         ///
223         Parser(LyXLex & lex);
224         ///
225         Parser(istream & is);
226
227         ///
228         MathMacroTemplate * parse_macro();
229         ///
230         MathMatrixInset * parse_normal();
231         ///
232         void parse_into(MathArray & array, unsigned flags);
233         ///
234         int lineno() const { return lineno_; }
235         ///
236         void putback();
237
238 private:
239         ///
240         string getArg(char lf, char rf);
241         ///
242         char getChar();
243         ///
244         void error(string const & msg);
245         ///
246         void parse_lines(MathGridInset * p, bool numbered, bool outmost);
247         ///
248         latexkeys const * read_delim();
249
250 private:
251         ///
252         void tokenize(istream & is);
253         ///
254         void tokenize(string const & s);
255         ///
256         void push_back(Token const & t);
257         ///
258         void pop_back();
259         ///
260         Token const & prevToken() const;
261         ///
262         Token const & nextToken() const;
263         ///
264         Token const & getToken();
265         ///
266         void lex(string const & s);
267         ///
268         bool good() const;
269
270         ///
271         int lineno_;
272         ///
273         std::vector<Token> tokens_;
274         ///
275         unsigned pos_;
276         ///
277         bool   curr_num_;
278         ///
279         string curr_label_;
280         ///
281         string curr_skip_;
282 };
283
284
285 Parser::Parser(LyXLex & lexer)
286         : lineno_(lexer.getLineNo()), pos_(0), curr_num_(false)
287 {
288         tokenize(lexer.getStream());
289 }
290
291
292 Parser::Parser(istream & is)
293         : lineno_(0), pos_(0), curr_num_(false)
294 {
295         tokenize(is);
296 }
297
298
299 void Parser::push_back(Token const & t)
300 {
301         tokens_.push_back(t);
302 }
303
304
305 void Parser::pop_back()
306 {
307         tokens_.pop_back();
308 }
309
310
311 Token const & Parser::prevToken() const
312 {
313         static const Token dummy;
314         return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
315 }
316
317
318 Token const & Parser::nextToken() const
319 {
320         static const Token dummy;
321         return good() ? tokens_[pos_] : dummy;
322 }
323
324
325 Token const & Parser::getToken()
326 {
327         static const Token dummy;
328         return good() ? tokens_[pos_++] : dummy;
329 }
330
331
332 void Parser::putback()
333 {
334         --pos_;
335 }
336
337
338 bool Parser::good() const
339 {
340         return pos_ < tokens_.size();
341 }
342
343
344 char Parser::getChar()
345 {
346         if (!good())
347                 lyxerr << "The input stream is not well..." << endl;
348         return tokens_[pos_++].character();
349 }
350
351
352 string Parser::getArg(char lf, char rg)
353 {
354         string result;
355         char c = getChar();
356
357         if (c != lf)  
358                 putback();
359         else 
360                 while ((c = getChar()) != rg && good())
361                         result += c;
362
363         return result;
364 }
365
366
367 void Parser::tokenize(istream & is)
368 {
369         // eat everything up to the next \end_inset or end of stream
370         // and store it in s for further tokenization
371         string s;
372         char c;
373         while (is.get(c)) {
374                 s += c;
375                 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
376                         s = s.substr(0, s.size() - 10);
377                         break;
378                 }
379         }
380
381         // tokenize buffer
382         tokenize(s);
383 }
384
385
386 void Parser::tokenize(string const & buffer)
387 {
388         static bool init_done = false;
389         
390         if (!init_done) {
391                 catInit();
392                 init_done = true;
393         }
394
395         istringstream is(buffer, ios::in | ios::binary);
396
397         char c;
398         while (is.get(c)) {
399
400                 switch (catcode(c)) {
401                         case catNewline: {
402                                 ++lineno_; 
403                                 is.get(c);
404                                 if (catcode(c) == catNewline)
405                                         ; //push_back(Token("par"));
406                                 else {
407                                         push_back(Token(' ', catSpace));
408                                         is.putback(c);  
409                                 }
410                                 break;
411                         }
412
413                         case catComment: {
414                                 while (is.get(c) && catcode(c) != catNewline)
415                                         ;
416                                 ++lineno_; 
417                                 break;
418                         }
419
420                         case catEscape: {
421                                 is.get(c);
422                                 string s(1, c);
423                                 if (catcode(c) == catLetter) {
424                                         while (is.get(c) && catcode(c) == catLetter)
425                                                 s += c;
426                                         if (catcode(c) == catSpace)
427                                                 while (is.get(c) && catcode(c) == catSpace)
428                                                         ;
429                                         is.putback(c);
430                                 }       
431                                 push_back(Token(s));
432                                 break;
433                         }
434
435                         default:
436                                 push_back(Token(c, catcode(c)));
437                 }
438         }
439
440 #if 0
441         lyxerr << "\nTokens: ";
442         for (unsigned i = 0; i < tokens_.size(); ++i)
443                 lyxerr << tokens_[i];
444         lyxerr << "\n";
445 #endif
446 }
447
448
449 void Parser::error(string const & msg) 
450 {
451         lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
452 }
453
454
455 void Parser::parse_lines(MathGridInset * p, bool numbered, bool outmost)
456 {
457         const int cols = p->ncols();
458
459         // save global variables
460         bool   const saved_num   = curr_num_;
461         string const saved_label = curr_label_;
462
463         for (int row = 0; true; ++row) {
464                 // reset global variables
465                 curr_num_   = numbered;
466                 curr_label_.erase();
467
468                 // reading a row
469                 for (int col = 0; col < cols; ++col) {
470                         //lyxerr << "reading cell " << row << " " << col << "\n";
471                         parse_into(p->cell(col + row * cols), FLAG_BLOCK);
472
473                         // no ampersand
474                         if (prevToken().cat() != catAlign) {
475                                 //lyxerr << "less cells read than normal in row/col: "
476                                 //      << row << " " << col << "\n";
477                                 break;
478                         }
479                 }
480
481                 if (outmost) {
482                         MathMatrixInset * m = static_cast<MathMatrixInset *>(p);
483                         m->numbered(row, curr_num_);
484                         m->label(row, curr_label_);
485                         if (curr_skip_.size()) {
486                                 m->vskip(LyXLength(curr_skip_), row);
487                                 curr_skip_.erase();
488                         }
489                 }
490
491                 // no newline?
492                 if (prevToken() != Token("\\")) {
493                         //lyxerr << "no newline here\n";
494                         break;
495                 }
496
497                 p->appendRow();
498         }
499
500         // restore "global" variables
501         curr_num_   = saved_num;
502         curr_label_ = saved_label;
503 }
504
505
506 MathMacroTemplate * Parser::parse_macro()
507 {
508         while (nextToken().cat() == catSpace)
509                 getToken();
510
511         if (getToken().cs() != "newcommand") {
512                 lyxerr << "\\newcommand expected\n";
513                 return 0;
514         }
515
516         if (getToken().cat() != catBegin) {
517                 lyxerr << "'{' expected\n";
518                 return 0;
519         }
520
521         string name = getToken().cs();
522
523         if (getToken().cat() != catEnd) {
524                 lyxerr << "'}' expected\n";
525                 return 0;
526         }
527
528         string arg  = getArg('[', ']');
529         int    narg = arg.empty() ? 0 : atoi(arg.c_str()); 
530         //lyxerr << "creating macro " << name << " with " << narg <<  "args\n";
531         MathMacroTemplate * p = new MathMacroTemplate(name, narg);
532         parse_into(p->cell(0), FLAG_BRACE | FLAG_BRACE_LAST);
533         return p;
534 }
535
536
537 MathMatrixInset * Parser::parse_normal()
538 {
539         Token const & t = getToken();
540
541         if (t.cat() == catMath || t.cs() == "(") {
542                 MathMatrixInset * p = new MathMatrixInset(LM_OT_SIMPLE);
543                 parse_into(p->cell(0), 0);
544                 return p;
545         }
546
547         if (!t.cs().size()) {
548                 lyxerr << "start of math expected, got '" << t << "'\n";
549                 return 0;
550         }
551
552         string const & cs = t.cs();
553
554         if (cs == "[") {
555                 curr_num_ = 0;
556                 curr_label_.erase();
557                 MathMatrixInset * p = new MathMatrixInset(LM_OT_EQUATION);
558                 parse_into(p->cell(0), 0);
559                 p->numbered(0, curr_num_);
560                 p->label(0, curr_label_);
561                 return p;
562         }
563
564         if (cs != "begin") {
565                 lyxerr << "'begin' of un-simple math expected, got '" << cs << "'\n";
566                 return 0;
567         }
568
569         string const name = getArg('{', '}');
570
571         if (name == "equation" || name == "equation*") {
572                 curr_num_ = !stared(name);
573                 curr_label_.erase();
574                 MathMatrixInset * p = new MathMatrixInset(LM_OT_EQUATION);
575                 parse_into(p->cell(0), FLAG_END);
576                 p->numbered(0, curr_num_);
577                 p->label(0, curr_label_);
578                 return p;
579         }
580
581         if (name == "eqnarray" || name == "eqnarray*") {
582                 MathMatrixInset * p = new MathMatrixInset(LM_OT_EQNARRAY);
583                 parse_lines(p, !stared(name), true);
584                 return p;
585         }
586
587         if (name == "align" || name == "align*") {
588                 MathMatrixInset * p = new MathMatrixInset(LM_OT_ALIGN);
589                 p->halign(getArg('{', '}'));
590                 parse_lines(p, !stared(name), true);
591                 return p;
592         }
593
594         if (name == "alignat" || name == "alignat*") {
595                 MathMatrixInset * p = new MathMatrixInset(LM_OT_ALIGNAT);
596                 p->halign(getArg('{', '}'));
597                 parse_lines(p, !stared(name), true);
598                 return p;
599         }
600
601         lyxerr[Debug::MATHED] << "1: unknown math environment: " << name << "\n";
602         return 0;
603 }
604
605
606 latexkeys const * Parser::read_delim()
607 {
608         Token const & t = getToken();
609         latexkeys const * l = in_word_set(t.asString());
610         return l ? l : in_word_set(".");
611 }
612
613
614 void Parser::parse_into(MathArray & array, unsigned flags)
615 {
616         MathTextCodes yyvarcode = LM_TC_MIN;
617
618         bool panic  = false;
619         int  limits = 0;
620
621         while (good()) {
622                 Token const & t = getToken();
623         
624                 //lyxerr << "t: " << t << " flags: " << flags << "'\n";
625                 //array.dump(lyxerr);
626                 //lyxerr << "\n";
627
628                 if (flags & FLAG_ITEM) {
629                         flags &= ~FLAG_ITEM;
630                         if (t.cat() == catBegin) { 
631                                 // skip the brace and collect everything to the next matching
632                                 // closing brace
633                                 flags |= FLAG_BRACE_LAST;
634                                 continue;
635                         } else {
636                                 // handle only this single token, leave the loop if done
637                                 flags |= FLAG_LEAVE;
638                         }
639                 }
640
641                 if (flags & FLAG_BRACE) {
642                         if (t.cat() != catBegin) {
643                                 error("Expected {. Maybe you forgot to enclose an argument in {}");
644                                 panic = true;
645                                 break;
646                         } else {
647                                 flags &= ~FLAG_BRACE;
648                                 continue;
649                         }
650                 }
651
652                 if (flags & FLAG_BLOCK) {
653                         if (t.cat() == catEnd || t.cat() == catAlign || t.cs() == "\\")
654                                 return;
655                         if (t.cs() == "end") {
656                                 getArg('{', '}');
657                                 return;
658                         }
659                 }
660
661                 //
662                 // cat codes
663                 //
664                 if (t.cat() == catMath)
665                         break;
666
667                 else if (t.cat() == catLetter)
668                         array.push_back(new MathCharInset(t.character(), yyvarcode));
669
670                 else if (t.cat() == catSpace && yyvarcode == LM_TC_TEXTRM)
671                         array.push_back(new MathCharInset(' ', yyvarcode));
672
673                 else if (t.cat() == catParameter) {
674                         Token const & n = getToken();
675                         MathMacroArgument * p = new MathMacroArgument(n.character() - '0');
676                         array.push_back(p);
677                 }
678
679                 else if (t.cat() == catBegin) {
680                         //lyxerr << " creating ScopeInset\n";
681                         array.push_back(new MathScopeInset);
682                         parse_into(array.back()->cell(0), FLAG_BRACE_LAST);
683                 }
684
685                 else if (t.cat() == catEnd) {
686                         if (!(flags & FLAG_BRACE_LAST))
687                                 lyxerr << " ##### unexpected end of block\n";
688                         return;
689                 }
690                 
691                 else if (t.cat() == catAlign) {
692                         lyxerr << "found tab unexpectedly, array: '" << array << "'\n";
693                         return;
694                 }
695                 
696                 else if (t.cat() == catSuper)
697                         parse_into(lastScriptInset(array, true, limits)->cell(0), FLAG_ITEM);
698                 
699                 else if (t.cat() == catSub)
700                         parse_into(lastScriptInset(array, false, limits)->cell(1), FLAG_ITEM);
701                 
702                 else if (t.character() == ']' && (flags & FLAG_BRACK_END))
703                         return;
704
705                 else if (t.cat() == catOther)
706                         array.push_back(new MathCharInset(t.character(), yyvarcode));
707                 
708                 //
709                 // codesequences
710                 //      
711                 else if (t.cs() == "protect") 
712                         ;
713
714                 else if (t.cs() == "end")
715                         break;
716
717                 else if (t.cs() == ")")
718                         break;
719
720                 else if (t.cs() == "]")
721                         break;
722
723                 else if (t.cs() == "\\") {
724                         curr_skip_ = getArg('[', ']');
725                         if (!(flags & FLAG_NEWLINE))
726                                 lyxerr[Debug::MATHED]
727                                         << "found newline unexpectedly, array: '" << array << "'\n";
728                         return;
729                 }
730         
731                 else if (t.cs() == "limits") 
732                         limits = 1;
733                 
734                 else if (t.cs() == "nolimits") 
735                         limits = -1;
736                 
737                 else if (t.cs() == "nonumber")
738                         curr_num_ = false;
739
740                 else if (t.cs() == "number")
741                         curr_num_ = true;
742
743                 else if (t.cs() == "sqrt") {
744                         char c = getChar();
745                         if (c == '[') {
746                                 array.push_back(new MathRootInset);
747                                 parse_into(array.back()->cell(0), FLAG_BRACK_END);
748                                 parse_into(array.back()->cell(1), FLAG_ITEM);
749                         } else {
750                                 putback();
751                                 array.push_back(new MathSqrtInset);
752                                 parse_into(array.back()->cell(0), FLAG_ITEM);
753                         }
754                 }
755                 
756                 else if (t.cs() == "left") {
757                         latexkeys const * l = read_delim();
758                         MathArray ar;
759                         parse_into(ar, FLAG_RIGHT);
760                         latexkeys const * r = read_delim();
761                         MathDelimInset * dl = new MathDelimInset(l, r);
762                         dl->cell(0) = ar;
763                         array.push_back(dl);
764                 }
765                 
766                 else if (t.cs() == "right") {
767                         if (!(flags & FLAG_RIGHT))
768                                 error("Unmatched right delimiter");
769                         return;
770                 }
771
772 /*              
773                 case LM_TK_STY:
774                 {
775                         lyxerr[Debug::MATHED] << "LM_TK_STY not implemented\n";
776                         //MathArray tmp = array;
777                         //MathSizeInset * p = new MathSizeInset(MathStyles(lval_->id));
778                         //array.push_back(p);
779                         //parse_into(p->cell(0), FLAG_BRACE_FONT);
780                         break; 
781                 }
782
783                 case LM_TK_UNDEF: 
784                         if (MathMacroTable::hasTemplate(sval_)) {
785                                 MathMacro * m = MathMacroTable::cloneTemplate(sval_);
786                                 for (int i = 0; i < m->nargs(); ++i) 
787                                         parse_into(m->cell(i), FLAG_ITEM);
788                                 array.push_back(m);
789                                 m->metrics(LM_ST_TEXT);
790                         } else
791                                 array.push_back(new MathFuncInset(sval_));
792                         break;
793
794                 else  LM_TK_SPECIAL:
795                         array.push_back(new MathCharInset(ival_, LM_TC_SPECIAL));
796                         break;
797 */
798                 
799                 else if (t.cs() == "begin") {
800                         string const name = getArg('{', '}');   
801                         if (name == "array") {
802                                 string const valign = getArg('[', ']') + 'c';
803                                 string const halign = getArg('{', '}');
804                                 MathArrayInset * m = new MathArrayInset(halign.size(), 1);
805                                 m->valign(valign[0]);
806                                 m->halign(halign);
807                                 parse_lines(m, false, false);
808                                 array.push_back(m);
809                         } else 
810                                 lyxerr[Debug::MATHED] << "unknow math inset begin '" << name << "'\n";  
811                 }
812         
813                 else if (t.cs() == "label") {
814                         MathArray ar;
815                         parse_into(ar, FLAG_ITEM);
816                         ostringstream os;
817                         ar.write(os, true);
818                         curr_label_ = os.str();
819                         // was: 
820                         //curr_label_ = getArg('{', '}');
821                 }
822
823                 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
824                         limits = 0;
825                         MathInset * p = createMathInset(t.cs());
826                         p->cell(0).swap(array);
827                         array.push_back(p);
828                         parse_into(p->cell(1), FLAG_BLOCK);
829                 }
830         
831                 else if (t.cs().size()) {
832                         limits = 0;
833                         latexkeys const * l = in_word_set(t.cs());
834                         if (l) {
835                                 if (l->token == LM_TK_FONT) {
836                                         //lyxerr << "starting font\n";
837                                         MathArray ar;
838                                         parse_into(ar, FLAG_ITEM);
839                                         for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it)
840                                                 (*it)->handleFont(static_cast<MathTextCodes>(l->id));
841                                         array.push_back(ar);
842                                         //lyxerr << "ending font\n";
843                                 }
844
845                                 else if (l->token == LM_TK_OLDFONT)
846                                         yyvarcode = static_cast<MathTextCodes>(l->id);
847
848                                 else {
849                                         MathInset * p = createMathInset(t.cs());
850                                         for (int i = 0; i < p->nargs(); ++i) 
851                                                 parse_into(p->cell(i), FLAG_ITEM);
852                                         array.push_back(p);
853                                 }
854                         }
855
856                         else {
857                                 MathInset * p = createMathInset(t.cs());
858                                 if (p) {
859                                         for (int i = 0; i < p->nargs(); ++i)
860                                                 parse_into(p->cell(i), FLAG_ITEM);
861                                         array.push_back(p);
862                                 } else {
863                                         error("Unrecognized token");
864                                         //lyxerr[Debug::MATHED] << "[" << t << "]\n";
865                                         lyxerr << t << "\n";
866                                 }       
867                         }
868                 }
869
870
871                 if (flags & FLAG_LEAVE) {
872                         flags &= ~FLAG_LEAVE;
873                         break;
874                 }
875         }
876
877         if (panic) {
878                 lyxerr << " Math Panic, expect problems!\n";
879                 //   Search for the end command. 
880                 Token t;
881                 do {
882                         t = getToken();
883                 } while (good() && t.cs() != "end");
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         return parser.parse_macro();
919 }
920
921
922
923 MathMatrixInset * mathed_parse_normal(string const & str)
924 {
925         istringstream is(str.c_str());
926         Parser parser(is);
927         return parser.parse_normal();
928 }
929
930 MathMatrixInset * mathed_parse_normal(istream & is)
931 {
932         Parser parser(is);
933         return parser.parse_normal();
934 }
935
936 MathMatrixInset * mathed_parse_normal(LyXLex & lex)
937 {
938         Parser parser(lex);
939         return parser.parse_normal();
940 }