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