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