]> git.lyx.org Git - lyx.git/blob - src/mathed/math_parser.C
fix for #141
[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         string::size_type 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 // Angus' compiler says these are not needed
217 //bool operator==(Token const & s, Token const & t)
218 //{
219 //      return s.character() == t.character()
220 //              && s.cat() == t.cat() && s.cs() == t.cs(); 
221 //}
222 //
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         bool parse_macro(string & name);
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         MathInset::col_type 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 (MathInset::col_type 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 bool Parser::parse_macro(string & name)
586 {
587         name = "{error}";
588         skipSpaces();
589
590         if (getToken().cs() != "newcommand") {
591                 lyxerr << "\\newcommand expected\n";
592                 return false;
593         }
594
595         if (getToken().cat() != catBegin) {
596                 lyxerr << "'{' in \\newcommand expected (1)\n";
597                 return false;
598         }
599
600         name = getToken().cs();
601
602         if (getToken().cat() != catEnd) {
603                 lyxerr << "'}' expected\n";
604                 return false;
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 false;
613         }
614
615         MathArray ar;
616         parse_into(ar, FLAG_BRACE_LAST);
617
618         // we cannot handle recursive stuff at all
619         MathArray test;
620         test.push_back(createMathInset(name));
621         if (ar.contains(test)) {
622                 lyxerr << "we cannot handle recursive macros at all.\n";
623                 return false;
624         }
625
626         MathMacroTable::create(name, narg, ar);
627         return true;
628 }
629
630
631 bool Parser::parse_normal(MathAtom & matrix)
632 {
633         skipSpaces();
634         Token const & t = getToken();
635
636         if (t.cs() == "(") {
637                 matrix = MathAtom(new MathHullInset(LM_OT_SIMPLE));
638                 parse_into(matrix->cell(0), 0);
639                 return true;
640         }
641
642         if (t.cat() == catMath) {
643                 Token const & n = getToken();
644                 if (n.cat() == catMath) {
645                         // TeX's $$...$$ syntax for displayed math
646                         matrix = MathAtom(new MathHullInset(LM_OT_EQUATION));
647                         MathHullInset * p = matrix->asHullInset();
648                         parse_into(p->cell(0), 0);
649                         p->numbered(0, curr_num_);
650                         p->label(0, curr_label_);
651                 } else {
652                         // simple $...$  stuff
653                         putback();
654                         matrix = MathAtom(new MathHullInset(LM_OT_SIMPLE));
655                         parse_into(matrix->cell(0), 0);
656                 }
657                 return true;
658         }
659
660         if (!t.cs().size()) {
661                 lyxerr << "start of math expected, got '" << t << "'\n";
662                 return false;
663         }
664
665         string const & cs = t.cs();
666
667         if (cs == "[") {
668                 curr_num_ = 0;
669                 curr_label_.erase();
670                 matrix = MathAtom(new MathHullInset(LM_OT_EQUATION));
671                 MathHullInset * p = matrix->asHullInset();
672                 parse_into(p->cell(0), 0);
673                 p->numbered(0, curr_num_);
674                 p->label(0, curr_label_);
675                 return true;
676         }
677
678         if (cs != "begin") {
679                 lyxerr << "'begin' of un-simple math expected, got '" << cs << "'\n";
680                 return false;
681         }
682
683         string const name = getArg('{', '}');
684
685         if (name == "math") {
686                 matrix = MathAtom(new MathHullInset(LM_OT_SIMPLE));
687                 parse_into(matrix->cell(0), 0);
688                 return true;
689         }
690
691         if (name == "equation" || name == "equation*" || name == "displaymath") {
692                 curr_num_ = (name == "equation");
693                 curr_label_.erase();
694                 matrix = MathAtom(new MathHullInset(LM_OT_EQUATION));
695                 MathHullInset * p = matrix->asHullInset();
696                 parse_into(p->cell(0), FLAG_END);
697                 p->numbered(0, curr_num_);
698                 p->label(0, curr_label_);
699                 return true;
700         }
701
702         if (name == "eqnarray" || name == "eqnarray*") {
703                 matrix = MathAtom(new MathHullInset(LM_OT_EQNARRAY));
704                 return parse_lines(matrix, !stared(name), true);
705         }
706
707         if (name == "align" || name == "align*") {
708                 matrix = MathAtom(new MathHullInset(LM_OT_ALIGN));
709                 return parse_lines(matrix, !stared(name), true);
710         }
711
712         if (name == "alignat" || name == "alignat*") {
713                 int nc = 2 * atoi(getArg('{', '}').c_str());
714                 matrix = MathAtom(new MathHullInset(LM_OT_ALIGNAT, nc));
715                 return parse_lines(matrix, !stared(name), true);
716         }
717
718         if (name == "xalignat" || name == "xalignat*") {
719                 int nc = 2 * atoi(getArg('{', '}').c_str());
720                 matrix = MathAtom(new MathHullInset(LM_OT_XALIGNAT, nc));
721                 return parse_lines(matrix, !stared(name), true);
722         }
723
724         if (name == "xxalignat") {
725                 int nc = 2 * atoi(getArg('{', '}').c_str());
726                 matrix = MathAtom(new MathHullInset(LM_OT_XXALIGNAT, nc));
727                 return parse_lines(matrix, !stared(name), true);
728         }
729
730         if (name == "multline" || name == "multline*") {
731                 matrix = MathAtom(new MathHullInset(LM_OT_MULTLINE));
732                 return parse_lines(matrix, !stared(name), true);
733         }
734
735         if (name == "gather" || name == "gather*") {
736                 matrix = MathAtom(new MathHullInset(LM_OT_GATHER));
737                 return parse_lines(matrix, !stared(name), true);
738         }
739
740         lyxerr[Debug::MATHED] << "1: unknown math environment: " << name << "\n";
741         lyxerr << "1: unknown math environment: " << name << "\n";
742         return false;
743 }
744
745
746 void Parser::parse_into(MathArray & array, unsigned flags, MathTextCodes code)
747 {
748         bool panic  = false;
749         int  limits = 0;
750
751         while (good()) {
752                 Token const & t = getToken();
753         
754                 //lyxerr << "t: " << t << " flags: " << flags << "'\n";
755                 //array.dump(lyxerr);
756                 //lyxerr << "\n";
757
758                 if (flags & FLAG_ITEM) {
759                         flags &= ~FLAG_ITEM;
760                         if (t.cat() == catBegin) { 
761                                 // skip the brace and collect everything to the next matching
762                                 // closing brace
763                                 flags |= FLAG_BRACE_LAST;
764                                 continue;
765                         } else {
766                                 // handle only this single token, leave the loop if done
767                                 flags |= FLAG_LEAVE;
768                         }
769                 }
770
771                 if (flags & FLAG_BLOCK) {
772                         if (t.cat() == catAlign || t.isCR() || t.cs() == "end") {
773                                 putback();
774                                 return;
775                         }
776                 }
777
778                 //
779                 // cat codes
780                 //
781                 if (t.cat() == catMath) {
782                         if (flags & FLAG_BOX) {
783                                 // we are inside an mbox, so opening new math is allowed
784                                 array.push_back(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
785                                 parse_into(array.back()->cell(0), 0);
786                         } else {
787                                 // otherwise this is the end of the formula
788                                 break;
789                         }
790                 }
791
792                 else if (t.cat() == catLetter)
793                         add(array, t.character(), code);
794
795                 else if (t.cat() == catSpace && code == LM_TC_TEXTRM)
796                         add(array, t.character(), code);
797
798                 else if (t.cat() == catParameter) {
799                         Token const & n = getToken();
800                         array.push_back(MathAtom(new MathMacroArgument(n.character()-'0', code)));
801                 }
802
803                 else if (t.cat() == catBegin) {
804                         MathArray ar;
805                         parse_into(ar, FLAG_BRACE_LAST);
806 #ifndef WITH_WARNINGS
807 #warning this might be wrong in general!
808 #endif
809                         // ignore braces around simple items
810                         if ((ar.size() == 1 && !ar.front()->needsBraces()
811        || (ar.size() == 2 && !ar.front()->needsBraces()
812                                             && ar.back()->asScriptInset()))
813        || (ar.size() == 0 && array.size() == 0))
814                         {
815                                 array.push_back(ar);
816                         } else {
817                                 array.push_back(MathAtom(new MathBraceInset));
818                                 array.back()->cell(0).swap(ar);
819                         }
820                 }
821
822                 else if (t.cat() == catEnd) {
823                         if (flags & FLAG_BRACE_LAST)
824                                 return;
825                         lyxerr << "found '}' unexpectedly, array: '" << array << "'\n";
826                         //lyxerr << "found '}' unexpectedly\n";
827                         add(array, '}', LM_TC_TEX);
828                 }
829                 
830                 else if (t.cat() == catAlign) {
831                         lyxerr << "found tab unexpectedly, array: '" << array << "'\n";
832                         //lyxerr << "found tab unexpectedly\n";
833                         add(array, '&', LM_TC_TEX);
834                 }
835                 
836                 else if (t.cat() == catSuper || t.cat() == catSub) {
837                         bool up = (t.cat() == catSuper);
838                         MathScriptInset * p = 0; 
839                         if (array.size()) 
840                                 p = array.back()->asScriptInset();
841                         if (!p || p->has(up)) {
842                                 array.push_back(MathAtom(new MathScriptInset(up)));
843                                 p = array.back()->asScriptInset();
844                         }
845                         p->ensure(up);
846                         parse_into(p->cell(up), FLAG_ITEM);
847                         p->limits(limits);
848                         limits = 0;
849                 }
850
851                 else if (t.character() == ']' && (flags & FLAG_BRACK_END))
852                         return;
853
854                 else if (t.cat() == catOther)
855                         add(array, t.character(), code);
856                 
857                 //
858                 // control sequences
859                 //      
860                 else if (t.cs() == "protect")
861                         // ignore \\protect, will be re-added during output 
862                         ;
863
864                 else if (t.cs() == "end")
865                         break;
866
867                 else if (t.cs() == ")")
868                         break;
869
870                 else if (t.cs() == "]")
871                         break;
872
873                 else if (t.cs() == "\\") {
874                         curr_skip_ = getArg('[', ']');
875                         //lyxerr << "found newline unexpectedly, array: '" << array << "'\n";
876                         lyxerr << "found newline unexpectedly\n";
877                         array.push_back(createMathInset("\\"));
878                 }
879         
880                 else if (t.cs() == "limits")
881                         limits = 1;
882                 
883                 else if (t.cs() == "nolimits")
884                         limits = -1;
885                 
886                 else if (t.cs() == "nonumber")
887                         curr_num_ = false;
888
889                 else if (t.cs() == "number")
890                         curr_num_ = true;
891
892                 else if (t.cs() == "sqrt") {
893                         char c = getChar();
894                         if (c == '[') {
895                                 array.push_back(MathAtom(new MathRootInset));
896                                 parse_into(array.back()->cell(0), FLAG_BRACK_END);
897                                 parse_into(array.back()->cell(1), FLAG_ITEM);
898                         } else {
899                                 putback();
900                                 array.push_back(MathAtom(new MathSqrtInset));
901                                 parse_into(array.back()->cell(0), FLAG_ITEM);
902                         }
903                 }
904                 
905                 else if (t.cs() == "left") {
906                         string l = getToken().asString();
907                         MathArray ar;
908                         parse_into(ar, FLAG_RIGHT);
909                         string r = getToken().asString();
910                         MathAtom dl(new MathDelimInset(l, r));
911                         dl->cell(0) = ar;
912                         array.push_back(dl);
913                 }
914                 
915                 else if (t.cs() == "right") {
916                         if (!(flags & FLAG_RIGHT)) {
917                                 //lyxerr << "got so far: '" << array << "'\n";
918                                 error("Unmatched right delimiter");
919                         }
920                         return;
921                 }
922
923                 else if (t.cs() == "begin") {
924                         string const name = getArg('{', '}');   
925                         if (name == "array") {
926                                 string const valign = getArg('[', ']') + 'c';
927                                 string const halign = getArg('{', '}');
928                                 array.push_back(MathAtom(new MathArrayInset(valign[0], halign)));
929                                 parse_lines(array.back(), false, false);
930                         } else if (name == "split") {
931                                 array.push_back(MathAtom(new MathSplitInset(1)));
932                                 parse_lines(array.back(), false, false);
933                         } else if (name == "cases") {
934                                 array.push_back(MathAtom(new MathCasesInset));
935                                 parse_lines(array.back(), false, false);
936                         } else 
937                                 lyxerr << "unknow math inset begin '" << name << "'\n"; 
938                 }
939         
940                 else if (t.cs() == "kern") {
941 #ifdef WITH_WARNINGS
942 #warning A hack...
943 #endif
944                         string s;
945                         while (1) {
946                                 Token const & t = getToken();
947                                 if (!good()) {
948                                         putback();      
949                                         break;
950                                 }
951                                 s += t.character();
952                                 if (isValidLength(s))
953                                         break;
954                         }
955                         array.push_back(MathAtom(new MathKernInset(s)));
956                 }
957
958                 else if (t.cs() == "label") {
959                         curr_label_ = getArg('{', '}');
960                 }
961
962                 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
963                         MathAtom p = createMathInset(t.cs());
964                         array.swap(p->cell(0));
965                         parse_into(p->cell(1), flags, code);
966                         array.push_back(p);
967                         return;
968                 }
969
970                 // Disabled
971 #if 0
972                 else if (t.cs() == "mbox") {
973                         array.push_back(createMathInset(t.cs()));
974                         // slurp in the argument of mbox
975         
976                         MathBoxInset * p = array.back()->asBoxInset();
977                         //lyx::assert(p);
978                 }
979 #endif
980
981         
982                 else if (t.cs().size()) {
983                         latexkeys const * l = in_word_set(t.cs());
984                         if (l) {
985                                 if (l->token == LM_TK_FONT) {
986                                         //lyxerr << "starting font\n";
987                                         //CatCode catSpaceSave = theCatcode[' '];
988                                         //if (l->id == LM_TC_TEXTRM) {
989                                         //      // temporarily change catcode   
990                                         //      theCatcode[' '] = catLetter;    
991                                         //}
992
993                                         MathArray ar;
994                                         parse_into(ar, FLAG_ITEM, static_cast<MathTextCodes>(l->id));
995                                         array.push_back(ar);
996
997                                         // undo catcode changes
998                                         ////theCatcode[' '] = catSpaceSave;
999                                         //lyxerr << "ending font\n";
1000                                 }
1001
1002                                 else if (l->token == LM_TK_OLDFONT) {
1003                                         code = static_cast<MathTextCodes>(l->id);
1004                                 }
1005
1006                                 else if (l->token == LM_TK_BOX) {
1007                                         MathAtom p = createMathInset(t.cs());
1008                                         parse_into(p->cell(0), FLAG_ITEM | FLAG_BOX, LM_TC_BOX);
1009                                         array.push_back(p);
1010                                 }
1011
1012                                 else if (l->token == LM_TK_STY) {
1013                                         MathAtom p = createMathInset(t.cs());
1014                                         parse_into(p->cell(0), flags, code);
1015                                         array.push_back(p);
1016                                         return;
1017                                 }
1018
1019                                 else {
1020                                         MathAtom p = createMathInset(t.cs());
1021                                         for (MathInset::idx_type i = 0; i < p->nargs(); ++i) 
1022                                                 parse_into(p->cell(i), FLAG_ITEM);
1023                                         array.push_back(p);
1024                                 }
1025                         }
1026
1027                         else {
1028                                 MathAtom p = createMathInset(t.cs());
1029                                 for (MathInset::idx_type i = 0; i < p->nargs(); ++i)
1030                                         parse_into(p->cell(i), FLAG_ITEM);
1031                                 array.push_back(p);
1032                         }
1033                 }
1034
1035
1036                 if (flags & FLAG_LEAVE) {
1037                         flags &= ~FLAG_LEAVE;
1038                         break;
1039                 }
1040         }
1041
1042         if (panic) {
1043                 lyxerr << " Math Panic, expect problems!\n";
1044                 //   Search for the end command. 
1045                 Token t;
1046                 do {
1047                         t = getToken();
1048                 } while (good() && t.cs() != "end");
1049         }
1050 }
1051
1052
1053
1054 } // anonymous namespace
1055
1056
1057 void mathed_parse_cell(MathArray & ar, string const & str)
1058 {
1059         istringstream is(str.c_str());
1060         mathed_parse_cell(ar, is);
1061 }
1062
1063
1064 void mathed_parse_cell(MathArray & ar, istream & is)
1065 {
1066         Parser(is).parse_into(ar, 0);
1067 }
1068
1069
1070
1071 bool mathed_parse_macro(string & name, string const & str)
1072 {
1073         istringstream is(str.c_str());
1074         Parser parser(is);
1075         return parser.parse_macro(name);
1076 }
1077
1078 bool mathed_parse_macro(string & name, istream & is)
1079 {
1080         Parser parser(is);
1081         return parser.parse_macro(name);
1082 }
1083
1084 bool mathed_parse_macro(string & name, LyXLex & lex)
1085 {
1086         Parser parser(lex);
1087         return parser.parse_macro(name);
1088 }
1089
1090
1091
1092 bool mathed_parse_normal(MathAtom & t, string const & str)
1093 {
1094         istringstream is(str.c_str());
1095         Parser parser(is);
1096         return parser.parse_normal(t);
1097 }
1098
1099 bool mathed_parse_normal(MathAtom & t, istream & is)
1100 {
1101         Parser parser(is);
1102         return parser.parse_normal(t);
1103 }
1104
1105 bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
1106 {
1107         Parser parser(lex);
1108         return parser.parse_normal(t);
1109 }