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