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