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