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