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