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