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