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