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