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