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