]> git.lyx.org Git - lyx.git/blob - src/mathed/math_parser.C
some support for the [x][x]alignat environments
[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_kerninset.h"
35 #include "math_macro.h"
36 #include "math_macrotable.h"
37 #include "math_macrotemplate.h"
38 #include "math_matrixinset.h"
39 #include "math_rootinset.h"
40 #include "math_sqrtinset.h"
41 #include "math_scriptinset.h"
42 #include "math_specialcharinset.h"
43 #include "math_sqrtinset.h"
44 #include "debug.h"
45 #include "support.h"
46 #include "lyxlex.h"
47 #include "support/lstrings.h"
48
49 using std::istream;
50 using std::ostream;
51 using std::ios;
52 using std::endl;
53
54
55 namespace {
56
57 bool stared(string const & s)
58 {
59         unsigned n = s.size();
60         return n && s[n - 1] == '*';
61 }
62
63 MathScriptInset * prevScriptInset(MathArray const & array)
64 {
65         MathInset * p = array.back();
66         return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
67 }
68
69
70 MathInset * lastScriptInset(MathArray & array, bool up, int limits)
71 {
72         MathScriptInset * p = prevScriptInset(array);
73         if (!p) {
74                 MathInset * b = array.back();
75                 if (b && b->isScriptable()) {
76                         p = new MathScriptInset(up, !up, b->clone());
77                         array.pop_back();       
78                 } else {
79                         p = new MathScriptInset(up, !up);
80                 }
81                 array.push_back(p);
82         }
83         if (up)
84                 p->up(true);
85         else
86                 p->down(true);
87         if (limits)
88                 p->limits(limits);
89         return p;
90 }
91
92
93 // These are TeX's catcodes
94 enum CatCode {
95         catEscape,     // 0    backslash 
96         catBegin,      // 1    {
97         catEnd,        // 2    }
98         catMath,       // 3    $
99         catAlign,      // 4    &
100         catNewline,    // 5    ^^M
101         catParameter,  // 6    #
102         catSuper,      // 7    ^
103         catSub,        // 8    _
104         catIgnore,     // 9       
105         catSpace,      // 10   space
106         catLetter,     // 11   a-zA-Z
107         catOther,      // 12   none of the above
108         catActive,     // 13   ~
109         catComment,    // 14   %
110         catInvalid     // 15   <delete>
111 };
112
113 CatCode theCatcode[256];  
114
115
116 inline CatCode catcode(unsigned char c)
117 {
118         return theCatcode[c];
119 }
120
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         string const & cs() const { return cs_; }
178         ///
179         CatCode cat() const { return cat_; }
180         ///
181         char character() const { return char_; }
182         ///
183         string asString() const;
184
185 private:        
186         ///
187         string cs_;
188         ///
189         char char_;
190         ///
191         CatCode cat_;
192 };
193
194 string Token::asString() const
195 {
196         return cs_.size() ? cs_ : string(1, char_);
197 }
198
199 bool operator==(Token const & s, Token const & t)
200 {
201         return s.character() == t.character()
202                 && s.cat() == t.cat() && s.cs() == t.cs(); 
203 }
204
205 bool operator!=(Token const & s, Token const & t)
206 {
207         return !(s == t);
208 }
209
210 ostream & operator<<(ostream & os, Token const & t)
211 {
212         if (t.cs().size())
213                 os << "\\" << t.cs();
214         else
215                 os << "[" << t.character() << "," << t.cat() << "]";
216         return os;
217 }
218
219
220 class Parser {
221
222 public:
223         ///
224         Parser(LyXLex & lex);
225         ///
226         Parser(istream & is);
227
228         ///
229         MathMacroTemplate * parse_macro();
230         ///
231         MathMatrixInset * parse_normal();
232         ///
233         void parse_into(MathArray & array, unsigned flags, MathTextCodes = LM_TC_MIN);
234         ///
235         int lineno() const { return lineno_; }
236         ///
237         void putback();
238
239 private:
240         ///
241         string getArg(char lf, char rf);
242         ///
243         char getChar();
244         ///
245         void error(string const & msg);
246         ///
247         void parse_lines(MathGridInset * p, bool numbered, bool outmost);
248         ///
249         latexkeys const * read_delim();
250
251 private:
252         ///
253         void tokenize(istream & is);
254         ///
255         void tokenize(string const & s);
256         ///
257         void push_back(Token const & t);
258         ///
259         void pop_back();
260         ///
261         Token const & prevToken() const;
262         ///
263         Token const & nextToken() const;
264         ///
265         Token const & getToken();
266         ///
267         void lex(string const & s);
268         ///
269         bool good() const;
270
271         ///
272         int lineno_;
273         ///
274         std::vector<Token> tokens_;
275         ///
276         unsigned pos_;
277         ///
278         bool   curr_num_;
279         ///
280         string curr_label_;
281         ///
282         string curr_skip_;
283 };
284
285
286 Parser::Parser(LyXLex & lexer)
287         : lineno_(lexer.getLineNo()), pos_(0), curr_num_(false)
288 {
289         tokenize(lexer.getStream());
290         lexer.eatLine();
291 }
292
293
294 Parser::Parser(istream & is)
295         : lineno_(0), pos_(0), curr_num_(false)
296 {
297         tokenize(is);
298 }
299
300
301 void Parser::push_back(Token const & t)
302 {
303         tokens_.push_back(t);
304 }
305
306
307 void Parser::pop_back()
308 {
309         tokens_.pop_back();
310 }
311
312
313 Token const & Parser::prevToken() const
314 {
315         static const Token dummy;
316         return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
317 }
318
319
320 Token const & Parser::nextToken() const
321 {
322         static const Token dummy;
323         return good() ? tokens_[pos_] : dummy;
324 }
325
326
327 Token const & Parser::getToken()
328 {
329         static const Token dummy;
330         return good() ? tokens_[pos_++] : dummy;
331 }
332
333
334 void Parser::putback()
335 {
336         --pos_;
337 }
338
339
340 bool Parser::good() const
341 {
342         return pos_ < tokens_.size();
343 }
344
345
346 char Parser::getChar()
347 {
348         if (!good())
349                 lyxerr << "The input stream is not well..." << endl;
350         return tokens_[pos_++].character();
351 }
352
353
354 string Parser::getArg(char lf, char rg)
355 {
356         string result;
357         char c = getChar();
358
359         if (c != lf)  
360                 putback();
361         else 
362                 while ((c = getChar()) != rg && good())
363                         result += c;
364
365         return result;
366 }
367
368
369 void Parser::tokenize(istream & is)
370 {
371         // eat everything up to the next \end_inset or end of stream
372         // and store it in s for further tokenization
373         string s;
374         char c;
375         while (is.get(c)) {
376                 s += c;
377                 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
378                         s = s.substr(0, s.size() - 10);
379                         break;
380                 }
381         }
382
383         // tokenize buffer
384         tokenize(s);
385 }
386
387
388 void Parser::tokenize(string const & buffer)
389 {
390         static bool init_done = false;
391         
392         if (!init_done) {
393                 catInit();
394                 init_done = true;
395         }
396
397         istringstream is(buffer, ios::in | ios::binary);
398
399         char c;
400         while (is.get(c)) {
401
402                 switch (catcode(c)) {
403                         case catNewline: {
404                                 ++lineno_; 
405                                 is.get(c);
406                                 if (catcode(c) == catNewline)
407                                         ; //push_back(Token("par"));
408                                 else {
409                                         push_back(Token(' ', catSpace));
410                                         is.putback(c);  
411                                 }
412                                 break;
413                         }
414
415                         case catComment: {
416                                 while (is.get(c) && catcode(c) != catNewline)
417                                         ;
418                                 ++lineno_; 
419                                 break;
420                         }
421
422                         case catEscape: {
423                                 is.get(c);
424                                 string s(1, c);
425                                 if (catcode(c) == catLetter) {
426                                         while (is.get(c) && catcode(c) == catLetter)
427                                                 s += c;
428                                         if (catcode(c) == catSpace)
429                                                 while (is.get(c) && catcode(c) == catSpace)
430                                                         ;
431                                         is.putback(c);
432                                 }       
433                                 push_back(Token(s));
434                                 break;
435                         }
436
437                         default:
438                                 push_back(Token(c, catcode(c)));
439                 }
440         }
441
442 #if 0
443         lyxerr << "\nTokens: ";
444         for (unsigned i = 0; i < tokens_.size(); ++i)
445                 lyxerr << tokens_[i];
446         lyxerr << "\n";
447 #endif
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         while (nextToken().cat() == catSpace)
542                 getToken();
543
544         Token const & t = getToken();
545
546         if (t.cat() == catMath || t.cs() == "(") {
547                 MathMatrixInset * p = new MathMatrixInset(LM_OT_SIMPLE);
548                 parse_into(p->cell(0), 0);
549                 return p;
550         }
551
552         if (!t.cs().size()) {
553                 lyxerr << "start of math expected, got '" << t << "'\n";
554                 return 0;
555         }
556
557         string const & cs = t.cs();
558
559         if (cs == "[") {
560                 curr_num_ = 0;
561                 curr_label_.erase();
562                 MathMatrixInset * p = new MathMatrixInset(LM_OT_EQUATION);
563                 parse_into(p->cell(0), 0);
564                 p->numbered(0, curr_num_);
565                 p->label(0, curr_label_);
566                 return p;
567         }
568
569         if (cs != "begin") {
570                 lyxerr << "'begin' of un-simple math expected, got '" << cs << "'\n";
571                 return 0;
572         }
573
574         string const name = getArg('{', '}');
575
576         if (name == "equation" || name == "equation*") {
577                 curr_num_ = !stared(name);
578                 curr_label_.erase();
579                 MathMatrixInset * p = new MathMatrixInset(LM_OT_EQUATION);
580                 parse_into(p->cell(0), FLAG_END);
581                 p->numbered(0, curr_num_);
582                 p->label(0, curr_label_);
583                 return p;
584         }
585
586         if (name == "eqnarray" || name == "eqnarray*") {
587                 MathMatrixInset * p = new MathMatrixInset(LM_OT_EQNARRAY);
588                 parse_lines(p, !stared(name), true);
589                 return p;
590         }
591
592         if (name == "align" || name == "align*") {
593                 MathMatrixInset * p = new MathMatrixInset(LM_OT_ALIGN);
594                 parse_lines(p, !stared(name), true);
595                 return p;
596         }
597
598         if (name == "alignat" || name == "alignat*") {
599                 MathMatrixInset * p =
600                         new MathMatrixInset(LM_OT_ALIGNAT, 2 * atoi(getArg('{', '}').c_str()));
601                 parse_lines(p, !stared(name), true);
602                 return p;
603         }
604
605         if (name == "xalignat" || name == "xalignat*") {
606                 MathMatrixInset * p =
607                         new MathMatrixInset(LM_OT_XALIGNAT, 2 * atoi(getArg('{', '}').c_str()));
608                 parse_lines(p, !stared(name), true);
609                 return p;
610         }
611
612         if (name == "xxalignat") {
613                 MathMatrixInset * p =
614                         new MathMatrixInset(LM_OT_XXALIGNAT, 2 * atoi(getArg('{', '}').c_str()));
615                 parse_lines(p, !stared(name), true);
616                 return p;
617         }
618
619         lyxerr[Debug::MATHED] << "1: unknown math environment: " << name << "\n";
620         return 0;
621 }
622
623
624 latexkeys const * Parser::read_delim()
625 {
626         Token const & t = getToken();
627         latexkeys const * l = in_word_set(t.asString());
628         return l ? l : in_word_set(".");
629 }
630
631
632 void Parser::parse_into(MathArray & array, unsigned flags, MathTextCodes code)
633 {
634         MathTextCodes yyvarcode = LM_TC_MIN;
635
636         bool panic  = false;
637         int  limits = 0;
638
639         while (good()) {
640                 Token const & t = getToken();
641         
642                 //lyxerr << "t: " << t << " flags: " << flags << "'\n";
643                 //array.dump(lyxerr);
644                 //lyxerr << "\n";
645
646                 if (flags & FLAG_ITEM) {
647                         flags &= ~FLAG_ITEM;
648                         if (t.cat() == catBegin) { 
649                                 // skip the brace and collect everything to the next matching
650                                 // closing brace
651                                 flags |= FLAG_BRACE_LAST;
652                                 continue;
653                         } else {
654                                 // handle only this single token, leave the loop if done
655                                 flags |= FLAG_LEAVE;
656                         }
657                 }
658
659                 if (flags & FLAG_BRACE) {
660                         if (t.cat() != catBegin) {
661                                 error("Expected {. Maybe you forgot to enclose an argument in {}");
662                                 panic = true;
663                                 break;
664                         } else {
665                                 flags &= ~FLAG_BRACE;
666                                 continue;
667                         }
668                 }
669
670                 if (flags & FLAG_BLOCK) {
671                         if (t.cat() == catAlign || t.cs() == "\\")
672                                 return;
673                         if (t.cs() == "end") {
674                                 getArg('{', '}');
675                                 return;
676                         }
677                 }
678
679                 //
680                 // cat codes
681                 //
682                 if (t.cat() == catMath)
683                         break;
684
685                 else if (t.cat() == catLetter)
686                         array.push_back(new MathCharInset(t.character(), yyvarcode));
687
688                 else if (t.cat() == catSpace &&
689                                 (yyvarcode == LM_TC_TEXTRM || code == LM_TC_TEXTRM))
690                         array.push_back(new MathCharInset(' ', yyvarcode));
691
692                 else if (t.cat() == catParameter) {
693                         Token const & n = getToken();
694                         MathMacroArgument * p = new MathMacroArgument(n.character() - '0');
695                         array.push_back(p);
696                 }
697
698                 else if (t.cat() == catBegin) {
699                         array.push_back(new MathCharInset('{', LM_TC_TEX));
700                 }
701
702                 else if (t.cat() == catEnd) {
703                         if (flags & FLAG_BRACE_LAST)
704                                 return;
705                         array.push_back(new MathCharInset('}', LM_TC_TEX));
706                 }
707                 
708                 else if (t.cat() == catAlign) {
709                         lyxerr << "found tab unexpectedly, array: '" << array << "'\n";
710                         array.push_back(new MathCharInset('&', LM_TC_TEX));
711                 }
712                 
713                 else if (t.cat() == catSuper)
714                         parse_into(lastScriptInset(array, true, limits)->cell(0), FLAG_ITEM);
715                 
716                 else if (t.cat() == catSub)
717                         parse_into(lastScriptInset(array, false, limits)->cell(1), FLAG_ITEM);
718                 
719                 else if (t.character() == ']' && (flags & FLAG_BRACK_END))
720                         return;
721
722                 else if (t.cat() == catOther)
723                         array.push_back(new MathCharInset(t.character(), yyvarcode));
724                 
725                 //
726                 // codesequences
727                 //      
728                 else if (t.cs() == "protect") 
729                         ;
730
731                 else if (t.cs() == "end")
732                         break;
733
734                 else if (t.cs() == ")")
735                         break;
736
737                 else if (t.cs() == "]")
738                         break;
739
740                 else if (t.cs() == "\\") {
741                         curr_skip_ = getArg('[', ']');
742                         if (flags & FLAG_NEWLINE)
743                                 return;
744                         lyxerr[Debug::MATHED]
745                                         << "found newline unexpectedly, array: '" << array << "'\n";
746                         array.push_back(createMathInset("\\"));
747                 }
748         
749                 else if (t.cs() == "limits") 
750                         limits = 1;
751                 
752                 else if (t.cs() == "nolimits") 
753                         limits = -1;
754                 
755                 else if (t.cs() == "nonumber")
756                         curr_num_ = false;
757
758                 else if (t.cs() == "number")
759                         curr_num_ = true;
760
761                 else if (t.cs() == "sqrt") {
762                         char c = getChar();
763                         if (c == '[') {
764                                 array.push_back(new MathRootInset);
765                                 parse_into(array.back()->cell(0), FLAG_BRACK_END);
766                                 parse_into(array.back()->cell(1), FLAG_ITEM);
767                         } else {
768                                 putback();
769                                 array.push_back(new MathSqrtInset);
770                                 parse_into(array.back()->cell(0), FLAG_ITEM);
771                         }
772                 }
773                 
774                 else if (t.cs() == "left") {
775                         latexkeys const * l = read_delim();
776                         MathArray ar;
777                         parse_into(ar, FLAG_RIGHT);
778                         latexkeys const * r = read_delim();
779                         MathDelimInset * dl = new MathDelimInset(l, r);
780                         dl->cell(0) = ar;
781                         array.push_back(dl);
782                 }
783                 
784                 else if (t.cs() == "right") {
785                         if (!(flags & FLAG_RIGHT))
786                                 error("Unmatched right delimiter");
787                         return;
788                 }
789
790 /*              
791                 case LM_TK_STY:
792                 {
793                         lyxerr[Debug::MATHED] << "LM_TK_STY not implemented\n";
794                         //MathArray tmp = array;
795                         //MathSizeInset * p = new MathSizeInset(MathStyles(lval_->id));
796                         //array.push_back(p);
797                         //parse_into(p->cell(0), FLAG_BRACE_FONT);
798                         break; 
799                 }
800
801                 else  LM_TK_SPECIAL:
802                         array.push_back(new MathCharInset(ival_, LM_TC_TEX));
803                         break;
804 */
805                 
806                 else if (t.cs() == "begin") {
807                         string const name = getArg('{', '}');   
808                         if (name == "array") {
809                                 string const valign = getArg('[', ']') + 'c';
810                                 string const halign = getArg('{', '}');
811                                 MathArrayInset * m = new MathArrayInset(halign.size(), 1);
812                                 m->valign(valign[0]);
813                                 m->halign(halign);
814                                 parse_lines(m, false, false);
815                                 array.push_back(m);
816                         } else 
817                                 lyxerr[Debug::MATHED] << "unknow math inset begin '" << name << "'\n";  
818                 }
819         
820                 else if (t.cs() == "kern") {
821 #ifdef WITH_WARNINGS
822 #warning A hack...
823 #endif
824                         string s;
825                         while (1) {
826                                 Token const & t = getToken();
827                                 if (!good()) {
828                                         putback();      
829                                         break;
830                                 }
831                                 s += t.character();
832                                 if (isValidLength(s))
833                                         break;
834                         }
835                         array.push_back(new MathKernInset(s));
836                 }
837
838                 else if (t.cs() == "label") {
839                         //MathArray ar;
840                         //parse_into(ar, FLAG_ITEM);
841                         //ostringstream os;
842                         //ar.write(os, true);
843                         //curr_label_ = os.str();
844                         // was: 
845                         curr_label_ = getArg('{', '}');
846                 }
847
848                 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
849                         limits = 0;
850                         MathInset * p = createMathInset(t.cs());
851                         // search backward for position of last '{' if any
852                         int pos;
853                         for (pos = array.size() - 1; pos >= 0; --pos) {
854                                 MathInset * q = array.nextInset(pos);
855                                 if (q->getChar() == '{')
856                                         break;
857                         }
858                         if (pos >= 0) {
859                                 // found it -> use the part after '{' as "numerator", erase the '{'
860                                 p->cell(0) = MathArray(array, pos + 1, array.size());
861                                 array.erase(pos, array.size());
862                         } else {
863                                 // not found -> use everything as "numerator"
864                                 p->cell(0).swap(array);
865                         }
866                         array.push_back(p);
867                         parse_into(p->cell(1), FLAG_BLOCK);
868                 }
869         
870                 else if (t.cs().size()) {
871                         limits = 0;
872                         latexkeys const * l = in_word_set(t.cs());
873                         if (l) {
874                                 if (l->token == LM_TK_FONT) {
875                                         //lyxerr << "starting font\n";
876                                         //CatCode catSpaceSave = theCatcode[' '];
877                                         //if (l->id == LM_TC_TEXTRM) {
878                                         //      // temporarily change catcode   
879                                         //      theCatcode[' '] = catLetter;    
880                                         //}
881
882                                         MathTextCodes t = static_cast<MathTextCodes>(l->id);
883                                         MathArray ar;
884                                         parse_into(ar, FLAG_ITEM, t);
885                                         for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it)
886                                                 (*it)->handleFont(t);
887                                         array.push_back(ar);
888
889                                         // undo catcode changes
890                                         ////theCatcode[' '] = catSpaceSave;
891                                         //lyxerr << "ending font\n";
892                                 }
893
894                                 else if (l->token == LM_TK_OLDFONT)
895                                         yyvarcode = static_cast<MathTextCodes>(l->id);
896
897                                 else {
898                                         MathInset * p = createMathInset(t.cs());
899                                         for (int i = 0; i < p->nargs(); ++i) 
900                                                 parse_into(p->cell(i), FLAG_ITEM);
901                                         array.push_back(p);
902                                 }
903                         }
904
905                         else {
906                                 MathInset * p = createMathInset(t.cs());
907                                 if (p) {
908                                         for (int i = 0; i < p->nargs(); ++i)
909                                                 parse_into(p->cell(i), FLAG_ITEM);
910                                         array.push_back(p);
911                                 } else {
912                                         error("Unrecognized token");
913                                         //lyxerr[Debug::MATHED] << "[" << t << "]\n";
914                                         lyxerr << t << "\n";
915                                 }       
916                         }
917                 }
918
919
920                 if (flags & FLAG_LEAVE) {
921                         flags &= ~FLAG_LEAVE;
922                         break;
923                 }
924         }
925
926         if (panic) {
927                 lyxerr << " Math Panic, expect problems!\n";
928                 //   Search for the end command. 
929                 Token t;
930                 do {
931                         t = getToken();
932                 } while (good() && t.cs() != "end");
933         }
934 }
935
936 } // anonymous namespace
937
938
939
940 MathArray mathed_parse_cell(string const & str)
941 {
942         istringstream is(str.c_str());
943         Parser parser(is);
944         MathArray ar;
945         parser.parse_into(ar, 0);
946         return ar;
947 }
948
949
950
951 MathMacroTemplate * mathed_parse_macro(string const & str)
952 {
953         istringstream is(str.c_str());
954         Parser parser(is);
955         return parser.parse_macro();
956 }
957
958 MathMacroTemplate * mathed_parse_macro(istream & is)
959 {
960         Parser parser(is);
961         return parser.parse_macro();
962 }
963
964 MathMacroTemplate * mathed_parse_macro(LyXLex & lex)
965 {
966         Parser parser(lex);
967         return parser.parse_macro();
968 }
969
970
971
972 MathMatrixInset * mathed_parse_normal(string const & str)
973 {
974         istringstream is(str.c_str());
975         Parser parser(is);
976         return parser.parse_normal();
977 }
978
979 MathMatrixInset * mathed_parse_normal(istream & is)
980 {
981         Parser parser(is);
982         return parser.parse_normal();
983 }
984
985 MathMatrixInset * mathed_parse_normal(LyXLex & lex)
986 {
987         Parser parser(lex);
988         return parser.parse_normal();
989 }