]> git.lyx.org Git - lyx.git/blob - src/mathed/math_parser.C
bug fix (spaces were ignored in input even within \mathrm)
[lyx.git] / src / mathed / math_parser.C
1 /*
2  *  File:        math_parser.C
3  *  Purpose:     Parser for mathed
4  *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx> 
5  *  Created:     January 1996
6  *  Description: Parse LaTeX2e math mode code.
7  *
8  *  Dependencies: Xlib, XForms
9  *
10  *  Copyright: 1996, Alejandro Aguilar Sierra
11  *
12  *   Version: 0.8beta.
13  *
14  *   You are free to use and modify this code under the terms of
15  *   the GNU General Public Licence version 2 or later.
16  */
17
18 #include <config.h>
19
20 #include <cctype>
21
22 #ifdef __GNUG__
23 #pragma implementation
24 #endif
25
26 #include "math_parser.h"
27 #include "array.h"
28 #include "math_inset.h"
29 #include "math_arrayinset.h"
30 #include "math_charinset.h"
31 #include "math_deliminset.h"
32 #include "math_factory.h"
33 #include "math_funcinset.h"
34 #include "math_macro.h"
35 #include "math_macrotable.h"
36 #include "math_macrotemplate.h"
37 #include "math_matrixinset.h"
38 #include "math_rootinset.h"
39 #include "math_scopeinset.h"
40 #include "math_sqrtinset.h"
41 #include "math_scriptinset.h"
42 #include "math_sqrtinset.h"
43 #include "debug.h"
44 #include "support.h"
45 #include "lyxlex.h"
46 #include "support/lstrings.h"
47
48 using std::istream;
49 using std::ostream;
50 using std::ios;
51 using std::endl;
52
53
54 namespace {
55
56 bool stared(string const & s)
57 {
58         unsigned n = s.size();
59         return n && s[n - 1] == '*';
60 }
61
62 MathScriptInset * prevScriptInset(MathArray const & array)
63 {
64         MathInset * p = array.back();
65         return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
66 }
67
68
69 MathInset * lastScriptInset(MathArray & array, bool up, int limits)
70 {
71         MathScriptInset * p = prevScriptInset(array);
72         if (!p) {
73                 MathInset * b = array.back();
74                 if (b && b->isScriptable()) {
75                         p = new MathScriptInset(up, !up, b->clone());
76                         array.pop_back();       
77                 } else {
78                         p = new MathScriptInset(up, !up);
79                 }
80                 array.push_back(p);
81         }
82         if (up)
83                 p->up(true);
84         else
85                 p->down(true);
86         if (limits)
87                 p->limits(limits);
88         return p;
89 }
90
91
92 // These are TeX's catcodes
93 enum CatCode {
94         catEscape,     // 0    backslash 
95         catBegin,      // 1    {
96         catEnd,        // 2    }
97         catMath,       // 3    $
98         catAlign,      // 4    &
99         catNewline,    // 5    ^^M
100         catParameter,  // 6    #
101         catSuper,      // 7    ^
102         catSub,        // 8    _
103         catIgnore,     // 9       
104         catSpace,      // 10   space
105         catLetter,     // 11   a-zA-Z
106         catOther,      // 12   none of the above
107         catActive,     // 13   ~
108         catComment,    // 14   %
109         catInvalid     // 15   <delete>
110 };
111
112 CatCode theCatcode[256];  
113
114
115 inline CatCode catcode(unsigned char c)
116 {
117         return theCatcode[c];
118 }
119
120
121 enum {
122         FLAG_BRACE      = 1 << 0,  //  an opening brace needed
123         FLAG_BRACE_LAST = 1 << 1,  //  last closing brace ends the parsing process
124         FLAG_RIGHT      = 1 << 2,  //  next \\right ends the parsing process
125         FLAG_END        = 1 << 3,  //  next \\end ends the parsing process
126         FLAG_BRACK_END  = 1 << 4,  //  next closing bracket ends the parsing process
127         FLAG_NEWLINE    = 1 << 6,  //  next \\\\ ends the parsing process
128         FLAG_ITEM       = 1 << 7,  //  read a (possibly braced token)
129         FLAG_BLOCK      = 1 << 8,  //  next block ends the parsing process
130         FLAG_LEAVE      = 1 << 9   //  leave the loop at the end
131 };
132
133
134 void catInit()
135 {
136         for (int i = 0; i <= 255; ++i) 
137                 theCatcode[i] = catOther;
138         for (int i = 'a'; i <= 'z'; ++i) 
139                 theCatcode[i] = catLetter;
140         for (int i = 'A'; i <= 'Z'; ++i) 
141                 theCatcode[i] = catLetter;
142
143         theCatcode['\\'] = catEscape;   
144         theCatcode['{']  = catBegin;    
145         theCatcode['}']  = catEnd;      
146         theCatcode['$']  = catMath;     
147         theCatcode['&']  = catAlign;    
148         theCatcode['\n'] = catNewline;  
149         theCatcode['#']  = catParameter;        
150         theCatcode['^']  = catSuper;    
151         theCatcode['_']  = catSub;      
152         theCatcode['\7f'] = catIgnore;    
153         theCatcode[' ']  = catSpace;    
154         theCatcode['\t'] = catSpace;    
155         theCatcode['\r'] = catSpace;    
156         theCatcode['~']  = catActive;   
157         theCatcode['%']  = catComment;  
158 }
159
160
161
162 //
163 // Helper class for parsing
164 //
165
166 class Token {
167 public:
168         ///
169         Token() : cs_(), char_(0), cat_(catIgnore) {}
170         ///
171         Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
172         ///
173         Token(const string & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
174
175         ///
176         string const & cs() const { return cs_; }
177         ///
178         CatCode cat() const { return cat_; }
179         ///
180         char character() const { return char_; }
181         ///
182         string asString() const;
183
184 private:        
185         ///
186         string cs_;
187         ///
188         char char_;
189         ///
190         CatCode cat_;
191 };
192
193 string Token::asString() const
194 {
195         return cs_.size() ? cs_ : string(1, char_);
196 }
197
198 bool operator==(Token const & s, Token const & t)
199 {
200         return s.character() == t.character()
201                 && s.cat() == t.cat() && s.cs() == t.cs(); 
202 }
203
204 bool operator!=(Token const & s, Token const & t)
205 {
206         return !(s == t);
207 }
208
209 ostream & operator<<(ostream & os, Token const & t)
210 {
211         if (t.cs().size())
212                 os << "\\" << t.cs();
213         else
214                 os << "[" << t.character() << "," << t.cat() << "]";
215         return os;
216 }
217
218
219 class Parser {
220
221 public:
222         ///
223         Parser(LyXLex & lex);
224         ///
225         Parser(istream & is);
226
227         ///
228         MathMacroTemplate * parse_macro();
229         ///
230         MathMatrixInset * parse_normal();
231         ///
232         void parse_into(MathArray & array, unsigned flags, MathTextCodes = LM_TC_MIN);
233         ///
234         int lineno() const { return lineno_; }
235         ///
236         void putback();
237
238 private:
239         ///
240         string getArg(char lf, char rf);
241         ///
242         char getChar();
243         ///
244         void error(string const & msg);
245         ///
246         void parse_lines(MathGridInset * p, bool numbered, bool outmost);
247         ///
248         latexkeys const * read_delim();
249
250 private:
251         ///
252         void tokenize(istream & is);
253         ///
254         void tokenize(string const & s);
255         ///
256         void push_back(Token const & t);
257         ///
258         void pop_back();
259         ///
260         Token const & prevToken() const;
261         ///
262         Token const & nextToken() const;
263         ///
264         Token const & getToken();
265         ///
266         void lex(string const & s);
267         ///
268         bool good() const;
269
270         ///
271         int lineno_;
272         ///
273         std::vector<Token> tokens_;
274         ///
275         unsigned pos_;
276         ///
277         bool   curr_num_;
278         ///
279         string curr_label_;
280         ///
281         string curr_skip_;
282 };
283
284
285 Parser::Parser(LyXLex & lexer)
286         : lineno_(lexer.getLineNo()), pos_(0), curr_num_(false)
287 {
288         tokenize(lexer.getStream());
289         lexer.eatLine();
290 }
291
292
293 Parser::Parser(istream & is)
294         : lineno_(0), pos_(0), curr_num_(false)
295 {
296         tokenize(is);
297 }
298
299
300 void Parser::push_back(Token const & t)
301 {
302         tokens_.push_back(t);
303 }
304
305
306 void Parser::pop_back()
307 {
308         tokens_.pop_back();
309 }
310
311
312 Token const & Parser::prevToken() const
313 {
314         static const Token dummy;
315         return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
316 }
317
318
319 Token const & Parser::nextToken() const
320 {
321         static const Token dummy;
322         return good() ? tokens_[pos_] : dummy;
323 }
324
325
326 Token const & Parser::getToken()
327 {
328         static const Token dummy;
329         return good() ? tokens_[pos_++] : dummy;
330 }
331
332
333 void Parser::putback()
334 {
335         --pos_;
336 }
337
338
339 bool Parser::good() const
340 {
341         return pos_ < tokens_.size();
342 }
343
344
345 char Parser::getChar()
346 {
347         if (!good())
348                 lyxerr << "The input stream is not well..." << endl;
349         return tokens_[pos_++].character();
350 }
351
352
353 string Parser::getArg(char lf, char rg)
354 {
355         string result;
356         char c = getChar();
357
358         if (c != lf)  
359                 putback();
360         else 
361                 while ((c = getChar()) != rg && good())
362                         result += c;
363
364         return result;
365 }
366
367
368 void Parser::tokenize(istream & is)
369 {
370         // eat everything up to the next \end_inset or end of stream
371         // and store it in s for further tokenization
372         string s;
373         char c;
374         while (is.get(c)) {
375                 s += c;
376                 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
377                         s = s.substr(0, s.size() - 10);
378                         break;
379                 }
380         }
381
382         // tokenize buffer
383         tokenize(s);
384 }
385
386
387 void Parser::tokenize(string const & buffer)
388 {
389         static bool init_done = false;
390         
391         if (!init_done) {
392                 catInit();
393                 init_done = true;
394         }
395
396         istringstream is(buffer, ios::in | ios::binary);
397
398         char c;
399         while (is.get(c)) {
400
401                 switch (catcode(c)) {
402                         case catNewline: {
403                                 ++lineno_; 
404                                 is.get(c);
405                                 if (catcode(c) == catNewline)
406                                         ; //push_back(Token("par"));
407                                 else {
408                                         push_back(Token(' ', catSpace));
409                                         is.putback(c);  
410                                 }
411                                 break;
412                         }
413
414                         case catComment: {
415                                 while (is.get(c) && catcode(c) != catNewline)
416                                         ;
417                                 ++lineno_; 
418                                 break;
419                         }
420
421                         case catEscape: {
422                                 is.get(c);
423                                 string s(1, c);
424                                 if (catcode(c) == catLetter) {
425                                         while (is.get(c) && catcode(c) == catLetter)
426                                                 s += c;
427                                         if (catcode(c) == catSpace)
428                                                 while (is.get(c) && catcode(c) == catSpace)
429                                                         ;
430                                         is.putback(c);
431                                 }       
432                                 push_back(Token(s));
433                                 break;
434                         }
435
436                         default:
437                                 push_back(Token(c, catcode(c)));
438                 }
439         }
440
441 #if 0
442         lyxerr << "\nTokens: ";
443         for (unsigned i = 0; i < tokens_.size(); ++i)
444                 lyxerr << tokens_[i];
445         lyxerr << "\n";
446 #endif
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, MathTextCodes code)
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 &&
672                                 (yyvarcode == LM_TC_TEXTRM || code == 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" || t.cs() == "atop") {
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                                         //CatCode catSpaceSave = theCatcode[' '];
840                                         //if (l->id == LM_TC_TEXTRM) {
841                                         //      // temporarily change catcode   
842                                         //      theCatcode[' '] = catLetter;    
843                                         //}
844
845                                         MathTextCodes t = static_cast<MathTextCodes>(l->id);
846                                         MathArray ar;
847                                         parse_into(ar, FLAG_ITEM, t);
848                                         for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it)
849                                                 (*it)->handleFont(t);
850                                         array.push_back(ar);
851
852                                         // undo catcode changes
853                                         ////theCatcode[' '] = catSpaceSave;
854                                         //lyxerr << "ending font\n";
855                                 }
856
857                                 else if (l->token == LM_TK_OLDFONT)
858                                         yyvarcode = static_cast<MathTextCodes>(l->id);
859
860                                 else {
861                                         MathInset * p = createMathInset(t.cs());
862                                         for (int i = 0; i < p->nargs(); ++i) 
863                                                 parse_into(p->cell(i), FLAG_ITEM);
864                                         array.push_back(p);
865                                 }
866                         }
867
868                         else {
869                                 MathInset * p = createMathInset(t.cs());
870                                 if (p) {
871                                         for (int i = 0; i < p->nargs(); ++i)
872                                                 parse_into(p->cell(i), FLAG_ITEM);
873                                         array.push_back(p);
874                                 } else {
875                                         error("Unrecognized token");
876                                         //lyxerr[Debug::MATHED] << "[" << t << "]\n";
877                                         lyxerr << t << "\n";
878                                 }       
879                         }
880                 }
881
882
883                 if (flags & FLAG_LEAVE) {
884                         flags &= ~FLAG_LEAVE;
885                         break;
886                 }
887         }
888
889         if (panic) {
890                 lyxerr << " Math Panic, expect problems!\n";
891                 //   Search for the end command. 
892                 Token t;
893                 do {
894                         t = getToken();
895                 } while (good() && t.cs() != "end");
896         }
897 }
898
899 } // anonymous namespace
900
901
902
903 MathArray mathed_parse_cell(string const & str)
904 {
905         istringstream is(str.c_str());
906         Parser parser(is);
907         MathArray ar;
908         parser.parse_into(ar, 0);
909         return ar;
910 }
911
912
913
914 MathMacroTemplate * mathed_parse_macro(string const & str)
915 {
916         istringstream is(str.c_str());
917         Parser parser(is);
918         return parser.parse_macro();
919 }
920
921 MathMacroTemplate * mathed_parse_macro(istream & is)
922 {
923         Parser parser(is);
924         return parser.parse_macro();
925 }
926
927 MathMacroTemplate * mathed_parse_macro(LyXLex & lex)
928 {
929         Parser parser(lex);
930         return parser.parse_macro();
931 }
932
933
934
935 MathMatrixInset * mathed_parse_normal(string const & str)
936 {
937         istringstream is(str.c_str());
938         Parser parser(is);
939         return parser.parse_normal();
940 }
941
942 MathMatrixInset * mathed_parse_normal(istream & is)
943 {
944         Parser parser(is);
945         return parser.parse_normal();
946 }
947
948 MathMatrixInset * mathed_parse_normal(LyXLex & lex)
949 {
950         Parser parser(lex);
951         return parser.parse_normal();
952 }