]> git.lyx.org Git - lyx.git/blob - src/mathed/math_parser.C
The "I want this in now" patch.
[lyx.git] / src / mathed / math_parser.C
1 /** The math parser
2     \author André Pönitz (2001)
3  */
4
5 /*
6
7 If someone desperately needs partial "structures" (such as a few
8 cells of an array inset or similar) (s)he could uses the
9 following hack as starting point to write some macros:
10
11   \newif\ifcomment
12   \commentfalse
13   \ifcomment
14           \def\makeamptab{\catcode`\&=4\relax}
15           \def\makeampletter{\catcode`\&=11\relax}
16     \def\b{\makeampletter\expandafter\makeamptab\bi}
17     \long\def\bi#1\e{}
18   \else
19     \def\b{}\def\e{}
20   \fi
21
22   ...
23
24   \[\begin{array}{ccc}
25 1
26 &
27
28   \end{array}\]
29
30 */
31
32
33 #include <config.h>
34
35 #include "math_parser.h"
36 #include "math_inset.h"
37 #include "math_arrayinset.h"
38 #include "math_braceinset.h"
39 #include "math_boxinset.h"
40 #include "math_charinset.h"
41 #include "math_commentinset.h"
42 #include "math_deliminset.h"
43 #include "math_envinset.h"
44 #include "math_extern.h"
45 #include "math_factory.h"
46 #include "math_kerninset.h"
47 #include "math_macro.h"
48 #include "math_macrotemplate.h"
49 #include "math_hullinset.h"
50 #include "math_parboxinset.h"
51 #include "math_parinset.h"
52 #include "math_rootinset.h"
53 #include "math_scriptinset.h"
54 #include "math_sizeinset.h"
55 #include "math_sqrtinset.h"
56 #include "math_stringinset.h"
57 #include "math_support.h"
58 #include "math_tabularinset.h"
59 #include "math_xyarrowinset.h"
60
61 //#include "insets/insetref.h"
62 #include "ref_inset.h"
63
64 #include "lyxlex.h"
65 #include "Lsstream.h"
66 #include "debug.h"
67 #include "support/LAssert.h"
68
69 #include <cctype>
70 #include <algorithm>
71
72 using std::istream;
73 using std::ostream;
74 using std::ios;
75 using std::endl;
76 using std::fill;
77 using std::vector;
78 using std::atoi;
79
80
81 //#define FILEDEBUG
82
83
84 namespace {
85
86 MathInset::mode_type asMode(MathInset::mode_type oldmode, string const & str)
87 {
88         //lyxerr << "handling mode: '" << str << "'\n";
89         if (str == "mathmode")
90                 return MathInset::MATH_MODE;
91         if (str == "textmode" || str == "forcetext")
92                 return MathInset::TEXT_MODE;
93         return oldmode;
94 }
95
96
97 bool stared(string const & s)
98 {
99         string::size_type const n = s.size();
100         return n && s[n - 1] == '*';
101 }
102
103
104 // These are TeX's catcodes
105 enum CatCode {
106         catEscape,     // 0    backslash
107         catBegin,      // 1    {
108         catEnd,        // 2    }
109         catMath,       // 3    $
110         catAlign,      // 4    &
111         catNewline,    // 5    ^^M
112         catParameter,  // 6    #
113         catSuper,      // 7    ^
114         catSub,        // 8    _
115         catIgnore,     // 9
116         catSpace,      // 10   space
117         catLetter,     // 11   a-zA-Z
118         catOther,      // 12   none of the above
119         catActive,     // 13   ~
120         catComment,    // 14   %
121         catInvalid     // 15   <delete>
122 };
123
124 CatCode theCatcode[256];
125
126
127 inline CatCode catcode(unsigned char c)
128 {
129         return theCatcode[c];
130 }
131
132
133 enum {
134         FLAG_BRACE_LAST = 1 << 1,  //  last closing brace ends the parsing
135         FLAG_RIGHT      = 1 << 2,  //  next \\right ends the parsing process
136         FLAG_END        = 1 << 3,  //  next \\end ends the parsing process
137         FLAG_BRACK_LAST = 1 << 4,  //  next closing bracket ends the parsing
138         FLAG_TEXTMODE   = 1 << 5,  //  we are in a box
139         FLAG_ITEM       = 1 << 6,  //  read a (possibly braced token)
140         FLAG_LEAVE      = 1 << 7,  //  leave the loop at the end
141         FLAG_SIMPLE     = 1 << 8,  //  next $ leaves the loop
142         FLAG_EQUATION   = 1 << 9,  //  next \] leaves the loop
143         FLAG_SIMPLE2    = 1 << 10, //  next \) leaves the loop
144         FLAG_OPTION     = 1 << 11, //  read [...] style option
145         FLAG_BRACED     = 1 << 12  //  read {...} style argument
146 };
147
148
149 void catInit()
150 {
151         fill(theCatcode, theCatcode + 256, catOther);
152         fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
153         fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
154
155         theCatcode['\\'] = catEscape;
156         theCatcode['{']  = catBegin;
157         theCatcode['}']  = catEnd;
158         theCatcode['$']  = catMath;
159         theCatcode['&']  = catAlign;
160         theCatcode['\n'] = catNewline;
161         theCatcode['#']  = catParameter;
162         theCatcode['^']  = catSuper;
163         theCatcode['_']  = catSub;
164         theCatcode['\7f'] = catIgnore;
165         theCatcode[' ']  = catSpace;
166         theCatcode['\t'] = catSpace;
167         theCatcode['\r'] = catNewline;
168         theCatcode['~']  = catActive;
169         theCatcode['%']  = catComment;
170 }
171
172
173
174 //
175 // Helper class for parsing
176 //
177
178 class Token {
179 public:
180         ///
181         Token() : cs_(), char_(0), cat_(catIgnore) {}
182         ///
183         Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
184         ///
185         Token(string const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
186
187         ///
188         string const & cs() const { return cs_; }
189         ///
190         CatCode cat() const { return cat_; }
191         ///
192         char character() const { return char_; }
193         ///
194         string asString() const { return cs_.size() ? cs_ : string(1, char_); }
195
196 private:
197         ///
198         string cs_;
199         ///
200         char char_;
201         ///
202         CatCode cat_;
203 };
204
205 ostream & operator<<(ostream & os, Token const & t)
206 {
207         if (t.cs().size())
208                 os << '\\' << t.cs();
209         else if (t.cat() == catLetter)
210                 os << t.character();
211         else
212                 os << '[' << t.character() << ',' << t.cat() << ']';
213         return os;
214 }
215
216
217 class Parser {
218
219 public:
220         ///
221         typedef  MathInset::mode_type mode_type;
222
223         ///
224         Parser(LyXLex & lex);
225         ///
226         Parser(istream & is);
227
228         ///
229         bool parse(MathAtom & at);
230         ///
231         void parse(MathArray & array, unsigned flags, mode_type mode);
232         ///
233         void parse1(MathGridInset & grid, unsigned flags, mode_type mode,
234                 bool numbered);
235         ///
236         MathArray parse(unsigned flags, mode_type mode);
237         ///
238         int lineno() const { return lineno_; }
239         ///
240         void putback();
241
242 private:
243         ///
244         void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
245         /// get arg delimited by 'left' and 'right'
246         string getArg(char left, char right);
247         ///
248         char getChar();
249         ///
250         void error(string const & msg);
251         /// dump contents to screen
252         void dump() const;
253         ///
254         void tokenize(istream & is);
255         ///
256         void tokenize(string const & s);
257         ///
258         void skipSpaceTokens(istream & is, char c);
259         ///
260         void push_back(Token const & t);
261         ///
262         void pop_back();
263         ///
264         Token const & prevToken() const;
265         ///
266         Token const & nextToken() const;
267         ///
268         Token const & getToken();
269         /// skips spaces if any
270         void skipSpaces();
271         ///
272         void lex(string const & s);
273         ///
274         bool good() const;
275         ///
276         string parse_verbatim_item();
277         ///
278         string parse_verbatim_option();
279
280         ///
281         int lineno_;
282         ///
283         vector<Token> tokens_;
284         ///
285         unsigned pos_;
286 };
287
288
289 Parser::Parser(LyXLex & lexer)
290         : lineno_(lexer.getLineNo()), pos_(0)
291 {
292         tokenize(lexer.getStream());
293         lexer.eatLine();
294 }
295
296
297 Parser::Parser(istream & is)
298         : lineno_(0), pos_(0)
299 {
300         tokenize(is);
301 }
302
303
304 void Parser::push_back(Token const & t)
305 {
306         tokens_.push_back(t);
307 }
308
309
310 void Parser::pop_back()
311 {
312         tokens_.pop_back();
313 }
314
315
316 Token const & Parser::prevToken() const
317 {
318         static const Token dummy;
319         return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
320 }
321
322
323 Token const & Parser::nextToken() const
324 {
325         static const Token dummy;
326         return good() ? tokens_[pos_] : dummy;
327 }
328
329
330 Token const & Parser::getToken()
331 {
332         static const Token dummy;
333         //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << '\n';
334         return good() ? tokens_[pos_++] : dummy;
335 }
336
337
338 void Parser::skipSpaces()
339 {
340         while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
341                 getToken();
342 }
343
344
345 void Parser::putback()
346 {
347         --pos_;
348 }
349
350
351 bool Parser::good() const
352 {
353         return pos_ < tokens_.size();
354 }
355
356
357 char Parser::getChar()
358 {
359         if (!good())
360                 error("The input stream is not well...");
361         return tokens_[pos_++].character();
362 }
363
364
365 string Parser::getArg(char left, char right)
366 {
367         skipSpaces();
368
369         string result;
370         char c = getChar();
371
372         if (c != left)
373                 putback();
374         else
375                 while ((c = getChar()) != right && good())
376                         result += c;
377
378         return result;
379 }
380
381
382 void Parser::skipSpaceTokens(istream & is, char c)
383 {
384         // skip trailing spaces
385         while (catcode(c) == catSpace || catcode(c) == catNewline)
386                 if (!is.get(c))
387                         break;
388         //lyxerr << "putting back: " << c << "\n";
389         is.putback(c);
390 }
391
392
393 void Parser::tokenize(istream & is)
394 {
395         // eat everything up to the next \end_inset or end of stream
396         // and store it in s for further tokenization
397         string s;
398         char c;
399         while (is.get(c)) {
400                 s += c;
401                 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
402                         s = s.substr(0, s.size() - 10);
403                         break;
404                 }
405         }
406         // Remove the space after \end_inset
407         if (is.get(c) && c != ' ')
408                 is.unget();
409
410         // tokenize buffer
411         tokenize(s);
412 }
413
414
415 void Parser::tokenize(string const & buffer)
416 {
417         static bool init_done = false;
418
419         if (!init_done) {
420                 catInit();
421                 init_done = true;
422         }
423
424         istringstream is(buffer.c_str(), ios::in | ios::binary);
425
426         char c;
427         while (is.get(c)) {
428                 //lyxerr << "reading c: " << c << "\n";
429
430                 switch (catcode(c)) {
431                         case catNewline: {
432                                 ++lineno_;
433                                 is.get(c);
434                                 if (catcode(c) == catNewline)
435                                         ; //push_back(Token("par"));
436                                 else {
437                                         push_back(Token('\n', catNewline));
438                                         is.putback(c);
439                                 }
440                                 break;
441                         }
442
443 /*
444                         case catComment: {
445                                 while (is.get(c) && catcode(c) != catNewline)
446                                         ;
447                                 ++lineno_;
448                                 break;
449                         }
450 */
451
452                         case catEscape: {
453                                 is.get(c);
454                                 if (!is) {
455                                         error("unexpected end of input");
456                                 } else {
457                                         string s(1, c);
458                                         if (catcode(c) == catLetter) {
459                                                 // collect letters
460                                                 while (is.get(c) && catcode(c) == catLetter)
461                                                         s += c;
462                                                 skipSpaceTokens(is, c);
463                                         }
464                                         push_back(Token(s));
465                                 }
466                                 break;
467                         }
468
469                         case catSuper:
470                         case catSub: {
471                                 push_back(Token(c, catcode(c)));
472                                 is.get(c);
473                                 skipSpaceTokens(is, c);
474                                 break;
475                         }
476
477                         case catIgnore: {
478                                 lyxerr << "ignoring a char: " << int(c) << "\n";
479                                 break;
480                         }
481
482                         default:
483                                 push_back(Token(c, catcode(c)));
484                 }
485         }
486
487 #ifdef FILEDEBUG
488         dump();
489 #endif
490 }
491
492
493 void Parser::dump() const
494 {
495         lyxerr << "\nTokens: ";
496         for (unsigned i = 0; i < tokens_.size(); ++i) {
497                 if (i == pos_)
498                         lyxerr << " <#> ";
499                 lyxerr << tokens_[i];
500         }
501         lyxerr << " pos: " << pos_ << "\n";
502 }
503
504
505 void Parser::error(string const & msg)
506 {
507         lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
508         dump();
509         //exit(1);
510 }
511
512
513 bool Parser::parse(MathAtom & at)
514 {
515         skipSpaces();
516         MathArray ar;
517         parse(ar, false, MathInset::UNDECIDED_MODE);
518         if (ar.size() != 1 || ar.front()->getType() == "none") {
519                 lyxerr << "unusual contents found: " << ar << endl;
520                 at = MathAtom(new MathParInset(ar));
521                 //if (at->nargs() > 0)
522                 //      at.nucleus()->cell(0) = ar;
523                 //else
524                 //      lyxerr << "unusual contents found: " << ar << endl;
525                 return true;
526         }
527         at = ar[0];
528         return true;
529 }
530
531
532 string Parser::parse_verbatim_option()
533 {
534         skipSpaces();
535         string res;
536         if (nextToken().character() == '[') {
537                 Token t = getToken();
538                 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
539                         if (t.cat() == catBegin) {
540                                 putback();
541                                 res += '{' + parse_verbatim_item() + '}';
542                         } else
543                                 res += t.asString();
544                 }
545         }
546         return res;
547 }
548
549
550 string Parser::parse_verbatim_item()
551 {
552         skipSpaces();
553         string res;
554         if (nextToken().cat() == catBegin) {
555                 Token t = getToken();
556                 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
557                         if (t.cat() == catBegin) {
558                                 putback();
559                                 res += '{' + parse_verbatim_item() + '}';
560                         }
561                         else
562                                 res += t.asString();
563                 }
564         }
565         return res;
566 }
567
568
569 MathArray Parser::parse(unsigned flags, mode_type mode)
570 {
571         MathArray ar;
572         parse(ar, flags, mode);
573         return ar;
574 }
575
576
577 void Parser::parse(MathArray & array, unsigned flags, mode_type mode)
578 {
579         MathGridInset grid(1, 1);
580         parse1(grid, flags, mode, false);
581         array = grid.cell(0);
582 }
583
584
585 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
586         const bool numbered)
587 {
588         parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
589 }
590
591
592 void Parser::parse1(MathGridInset & grid, unsigned flags,
593         const mode_type mode, const bool numbered)
594 {
595         int limits = 0;
596         MathGridInset::row_type cellrow = 0;
597         MathGridInset::col_type cellcol = 0;
598         MathArray * cell = &grid.cell(grid.index(cellrow, cellcol));
599
600         if (grid.asHullInset())
601                 grid.asHullInset()->numbered(cellrow, numbered);
602
603         //dump();
604         //lyxerr << " flags: " << flags << "\n";
605         //lyxerr << " mode: " << mode  << "\n";
606         //lyxerr << "grid: " << grid << endl;
607
608         while (good()) {
609                 Token const & t = getToken();
610
611 #ifdef FILEDEBUG
612                 lyxerr << "t: " << t << " flags: " << flags << "\n";
613                 lyxerr << "mode: " << mode  << "\n";
614                 cell->dump();
615                 lyxerr << "\n";
616 #endif
617
618                 if (flags & FLAG_ITEM) {
619
620                 if (t.cat() == catBegin) {
621                                 // skip the brace and collect everything to the next matching
622                                 // closing brace
623                                 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
624                                 return;
625                         }
626
627                         // handle only this single token, leave the loop if done
628                         flags = FLAG_LEAVE;
629                 }
630
631
632                 if (flags & FLAG_BRACED) {
633                         if (t.cat() == catSpace)
634                                 continue;
635
636                         if (t.cat() != catBegin) {
637                                 error("opening brace expected");
638                                 return;
639                         }
640
641                         // skip the brace and collect everything to the next matching
642                         // closing brace
643                         flags = FLAG_BRACE_LAST;
644                 }
645
646
647                 if (flags & FLAG_OPTION) {
648                         if (t.cat() == catOther && t.character() == '[') {
649                                 MathArray ar;
650                                 parse(ar, FLAG_BRACK_LAST, mode);
651                                 cell->append(ar);
652                         } else {
653                                 // no option found, put back token and we are done
654                                 putback();
655                         }
656                         return;
657                 }
658
659                 //
660                 // cat codes
661                 //
662                 if (t.cat() == catMath) {
663                         if (mode != MathInset::MATH_MODE) {
664                                 // we are inside some text mode thingy, so opening new math is allowed
665                                 Token const & n = getToken();
666                                 if (n.cat() == catMath) {
667                                         // TeX's $$...$$ syntax for displayed math
668                                         cell->push_back(MathAtom(new MathHullInset("equation")));
669                                         parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
670                                         getToken(); // skip the second '$' token
671                                 } else {
672                                         // simple $...$  stuff
673                                         putback();
674                                         cell->push_back(MathAtom(new MathHullInset("simple")));
675                                         parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
676                                 }
677                         }
678
679                         else if (flags & FLAG_SIMPLE) {
680                                 // this is the end of the formula
681                                 return;
682                         }
683
684                         else {
685                                 error("something strange in the parser\n");
686                                 break;
687                         }
688                 }
689
690                 else if (t.cat() == catLetter)
691                         cell->push_back(MathAtom(new MathCharInset(t.character())));
692
693                 else if (t.cat() == catSpace && mode != MathInset::MATH_MODE) {
694                         if (cell->empty() || cell->back()->getChar() != ' ')
695                                 cell->push_back(MathAtom(new MathCharInset(t.character())));
696                 }
697
698                 else if (t.cat() == catNewline && mode != MathInset::MATH_MODE) {
699                         if (cell->empty() || cell->back()->getChar() != ' ')
700                                 cell->push_back(MathAtom(new MathCharInset(' ')));
701                 }
702
703                 else if (t.cat() == catParameter) {
704                         Token const & n = getToken();
705                         cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
706                 }
707
708                 else if (t.cat() == catActive)
709                         cell->push_back(MathAtom(new MathCharInset(t.character())));
710
711                 else if (t.cat() == catBegin) {
712                         MathArray ar;
713                         parse(ar, FLAG_BRACE_LAST, mode);
714                         // do not create a BraceInset if they were written by LyX
715                         // this helps to keep the annoyance of  "a choose b"  to a minimum
716                         if (ar.size() == 1 && ar[0]->extraBraces())
717                                 cell->append(ar);
718                         else
719                                 cell->push_back(MathAtom(new MathBraceInset(ar)));
720                 }
721
722                 else if (t.cat() == catEnd) {
723                         if (flags & FLAG_BRACE_LAST)
724                                 return;
725                         error("found '}' unexpectedly");
726                         //lyx::Assert(0);
727                         //add(cell, '}', LM_TC_TEX);
728                 }
729
730                 else if (t.cat() == catAlign) {
731                         ++cellcol;
732                         //lyxerr << " column now " << cellcol << " max: " << grid.ncols() << "\n";
733                         if (cellcol == grid.ncols()) {
734                                 //lyxerr << "adding column " << cellcol << "\n";
735                                 grid.addCol(cellcol - 1);
736                         }
737                         cell = &grid.cell(grid.index(cellrow, cellcol));
738                 }
739
740                 else if (t.cat() == catSuper || t.cat() == catSub) {
741                         bool up = (t.cat() == catSuper);
742                         // we need no new script inset if the last thing was a scriptinset,
743                         // which has that script already not the same script already
744                         if (!cell->size())
745                                 cell->push_back(MathAtom(new MathScriptInset(up)));
746                         else if (cell->back()->asScriptInset() &&
747                                         !cell->back()->asScriptInset()->has(up))
748                                 cell->back().nucleus()->asScriptInset()->ensure(up);
749                         else if (cell->back()->asScriptInset())
750                                 cell->push_back(MathAtom(new MathScriptInset(up)));
751                         else
752                                 cell->back() = MathAtom(new MathScriptInset(cell->back(), up));
753                         MathScriptInset * p = cell->back().nucleus()->asScriptInset();
754                         // special handling of {}-bases
755                         // is this always correct?
756                         // It appears that this is wrong (Dekel)
757                         //if (p->nuc().size() == 1 && p->nuc().back()->asNestInset() &&
758                         //    p->nuc().back()->extraBraces())
759                         //      p->nuc() = p->nuc().back()->asNestInset()->cell(0);
760                         parse(p->cell(up), FLAG_ITEM, mode);
761                         if (limits) {
762                                 p->limits(limits);
763                                 limits = 0;
764                         }
765                 }
766
767                 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
768                         //lyxerr << "finished reading option\n";
769                         return;
770                 }
771
772                 else if (t.cat() == catOther)
773                         cell->push_back(MathAtom(new MathCharInset(t.character())));
774
775                 else if (t.cat() == catComment) {
776                         string s;
777                         while (good()) {
778                                 Token const & t = getToken();
779                                 if (t.cat() == catNewline)
780                                         break;
781                                 s += t.asString();
782                         }
783                         cell->push_back(MathAtom(new MathCommentInset(s)));
784                         skipSpaces();
785                 }
786
787                 //
788                 // control sequences
789                 //
790
791                 else if (t.cs() == "lyxlock") {
792                         if (cell->size())
793                                 cell->back().nucleus()->lock(true);
794                 }
795
796                 else if (t.cs() == "def" || t.cs() == "newcommand") {
797                         string name;
798                         int nargs = 0;
799                         if (t.cs() == "def") {
800                                 // get name
801                                 name = getToken().cs();
802
803                                 // read parameter
804                                 string pars;
805                                 while (good() && nextToken().cat() != catBegin) {
806                                         pars += getToken().cs();
807                                         ++nargs;
808                                 }
809                                 nargs /= 2;
810                                 //lyxerr << "read \\def parameter list '" << pars << "'\n";
811
812                         } else { // t.cs() == "newcommand"
813
814                                 if (getToken().cat() != catBegin) {
815                                         error("'{' in \\newcommand expected (1) \n");
816                                         return;
817                                 }
818
819                                 name = getToken().cs();
820
821                                 if (getToken().cat() != catEnd) {
822                                         error("'}' in \\newcommand expected\n");
823                                         return;
824                                 }
825
826                                 string arg  = getArg('[', ']');
827                                 if (!arg.empty())
828                                         nargs = atoi(arg.c_str());
829
830                         }
831
832                         MathArray ar1;
833                         parse(ar1, FLAG_ITEM, MathInset::UNDECIDED_MODE);
834
835                         // we cannot handle recursive stuff at all
836                         //MathArray test;
837                         //test.push_back(createMathInset(name));
838                         //if (ar1.contains(test)) {
839                         //      error("we cannot handle recursive macros at all.\n");
840                         //      return;
841                         //}
842
843                         // is a version for display attached?
844                         skipSpaces();
845                         MathArray ar2;
846                         if (nextToken().cat() == catBegin)
847                                 parse(ar2, FLAG_ITEM, MathInset::MATH_MODE);
848
849                         cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, ar1, ar2)));
850                 }
851
852                 else if (t.cs() == "(") {
853                         cell->push_back(MathAtom(new MathHullInset("simple")));
854                         parse2(cell->back(), FLAG_SIMPLE2, MathInset::MATH_MODE, false);
855                 }
856
857                 else if (t.cs() == "[") {
858                         cell->push_back(MathAtom(new MathHullInset("equation")));
859                         parse2(cell->back(), FLAG_EQUATION, MathInset::MATH_MODE, false);
860                 }
861
862                 else if (t.cs() == "protect")
863                         // ignore \\protect, will hopefully be re-added during output
864                         ;
865
866                 else if (t.cs() == "end") {
867                         if (flags & FLAG_END) {
868                                 // eat environment name
869                                 //string const name =
870                                 getArg('{', '}');
871                                 // FIXME: check that we ended the correct environment
872                                 return;
873                         }
874                         error("found 'end' unexpectedly");
875                 }
876
877                 else if (t.cs() == ")") {
878                         if (flags & FLAG_SIMPLE2)
879                                 return;
880                         error("found '\\)' unexpectedly");
881                 }
882
883                 else if (t.cs() == "]") {
884                         if (flags & FLAG_EQUATION)
885                                 return;
886                         error("found '\\]' unexpectedly");
887                 }
888
889                 else if (t.cs() == "\\") {
890                         grid.vcrskip(LyXLength(getArg('[', ']')), cellrow);
891                         ++cellrow;
892                         cellcol = 0;
893                         if (cellrow == grid.nrows())
894                                 grid.addRow(cellrow - 1);
895                         if (grid.asHullInset())
896                                 grid.asHullInset()->numbered(cellrow, numbered);
897                         cell = &grid.cell(grid.index(cellrow, cellcol));
898                 }
899
900 #if 0
901                 else if (t.cs() == "multicolumn") {
902                         // extract column count and insert dummy cells
903                         MathArray count;
904                         parse(count, FLAG_ITEM, mode);
905                         int cols = 1;
906                         if (!extractNumber(count, cols)) {
907                                 lyxerr << " can't extract number of cells from " << count << "\n";
908                         }
909                         // resize the table if necessary
910                         for (int i = 0; i < cols; ++i) {
911                                 ++cellcol;
912                                 if (cellcol == grid.ncols()) {
913                                         //lyxerr << "adding column " << cellcol << "\n";
914                                         grid.addCol(cellcol - 1);
915                                 }
916                                 cell = &grid.cell(grid.index(cellrow, cellcol));
917                                 // mark this as dummy
918                                 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = true;
919                         }
920                         // the last cell is the real thng, not a dummy
921                         grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
922
923                         // read special alignment
924                         MathArray align;
925                         parse(align, FLAG_ITEM, mode);
926                         //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
927
928                         // parse the remaining contents into the "real" cell
929                         parse(*cell, FLAG_ITEM, mode);
930                 }
931 #endif
932
933                 else if (t.cs() == "limits")
934                         limits = 1;
935
936                 else if (t.cs() == "nolimits")
937                         limits = -1;
938
939                 else if (t.cs() == "nonumber") {
940                         if (grid.asHullInset())
941                                 grid.asHullInset()->numbered(cellrow, false);
942                 }
943
944                 else if (t.cs() == "number") {
945                         if (grid.asHullInset())
946                                 grid.asHullInset()->numbered(cellrow, true);
947                 }
948
949                 else if (t.cs() == "hline") {
950                         grid.rowinfo(cellrow).lines_ ++;
951                 }
952
953                 else if (t.cs() == "sqrt") {
954                         MathArray ar;
955                         parse(ar, FLAG_OPTION, mode);
956                         if (ar.size()) {
957                                 cell->push_back(MathAtom(new MathRootInset));
958                                 cell->back().nucleus()->cell(0) = ar;
959                                 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
960                         } else {
961                                 cell->push_back(MathAtom(new MathSqrtInset));
962                                 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
963                         }
964                 }
965
966                 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
967                         cell->push_back(createMathInset(t.cs()));
968                         parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
969                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
970                 }
971
972                 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
973                                 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
974                         cell->push_back(MathAtom(new RefInset(t.cs())));
975                         parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
976                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
977                 }
978
979                 else if (t.cs() == "left") {
980                         skipSpaces();
981                         string l = getToken().asString();
982                         MathArray ar;
983                         parse(ar, FLAG_RIGHT, mode);
984                         skipSpaces();
985                         string r = getToken().asString();
986                         cell->push_back(MathAtom(new MathDelimInset(l, r, ar)));
987                 }
988
989                 else if (t.cs() == "right") {
990                         if (flags & FLAG_RIGHT)
991                                 return;
992                         //lyxerr << "got so far: '" << cell << "'\n";
993                         error("Unmatched right delimiter");
994                         return;
995                 }
996
997                 else if (t.cs() == "begin") {
998                         string const name = getArg('{', '}');
999
1000                         if (name == "array" || name == "subarray") {
1001                                 string const valign = parse_verbatim_option() + 'c';
1002                                 string const halign = parse_verbatim_item();
1003                                 cell->push_back(MathAtom(new MathArrayInset(name, valign[0], halign)));
1004                                 parse2(cell->back(), FLAG_END, mode, false);
1005                         }
1006
1007                         else if (name == "tabular") {
1008                                 string const valign = parse_verbatim_option() + 'c';
1009                                 string const halign = parse_verbatim_item();
1010                                 cell->push_back(MathAtom(new MathTabularInset(name, valign[0], halign)));
1011                                 parse2(cell->back(), FLAG_END, MathInset::TEXT_MODE, false);
1012                         }
1013
1014                         else if (name == "split" || name == "cases" ||
1015                                          name == "gathered" || name == "aligned") {
1016                                 cell->push_back(createMathInset(name));
1017                                 parse2(cell->back(), FLAG_END, mode, false);
1018                         }
1019
1020                         else if (name == "math") {
1021                                 cell->push_back(MathAtom(new MathHullInset("simple")));
1022                                 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, true);
1023                         }
1024
1025                         else if (name == "equation" || name == "equation*"
1026                                         || name == "displaymath") {
1027                                 cell->push_back(MathAtom(new MathHullInset("equation")));
1028                                 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, (name == "equation"));
1029                         }
1030
1031                         else if (name == "eqnarray" || name == "eqnarray*") {
1032                                 cell->push_back(MathAtom(new MathHullInset("eqnarray")));
1033                                 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1034                         }
1035
1036                         else if (name == "align" || name == "align*") {
1037                                 cell->push_back(MathAtom(new MathHullInset("align")));
1038                                 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1039                         }
1040
1041                         else if (name == "flalign" || name == "flalign*") {
1042                                 cell->push_back(MathAtom(new MathHullInset("flalign")));
1043                                 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1044                         }
1045
1046                         else if (name == "alignat" || name == "alignat*") {
1047                                 // ignore this for a while
1048                                 getArg('{', '}');
1049                                 cell->push_back(MathAtom(new MathHullInset("alignat")));
1050                                 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1051                         }
1052
1053                         else if (name == "xalignat" || name == "xalignat*") {
1054                                 // ignore this for a while
1055                                 getArg('{', '}');
1056                                 cell->push_back(MathAtom(new MathHullInset("xalignat")));
1057                                 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1058                         }
1059
1060                         else if (name == "xxalignat") {
1061                                 // ignore this for a while
1062                                 getArg('{', '}');
1063                                 cell->push_back(MathAtom(new MathHullInset("xxalignat")));
1064                                 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1065                         }
1066
1067                         else if (name == "multline" || name == "multline*") {
1068                                 cell->push_back(MathAtom(new MathHullInset("multline")));
1069                                 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1070                         }
1071
1072                         else if (name == "gather" || name == "gather*") {
1073                                 cell->push_back(MathAtom(new MathHullInset("gather")));
1074                                 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1075                         }
1076
1077                         else if (latexkeys const * l = in_word_set(name)) {
1078                                 if (l->inset == "matrix") {
1079                                         cell->push_back(createMathInset(name));
1080                                         parse2(cell->back(), FLAG_END, mode, false);
1081                                 }
1082                         }
1083
1084                         else {
1085                                 dump();
1086                                 lyxerr << "found unknown math environment '" << name << "'\n";
1087                                 // create generic environment inset
1088                                 cell->push_back(MathAtom(new MathEnvInset(name)));
1089                                 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1090                         }
1091                 }
1092
1093                 else if (t.cs() == "kern") {
1094 #ifdef WITH_WARNINGS
1095 #warning A hack...
1096 #endif
1097                         string s;
1098                         while (true) {
1099                                 Token const & t = getToken();
1100                                 if (!good()) {
1101                                         putback();
1102                                         break;
1103                                 }
1104                                 s += t.character();
1105                                 if (isValidLength(s))
1106                                         break;
1107                         }
1108                         cell->push_back(MathAtom(new MathKernInset(s)));
1109                 }
1110
1111                 else if (t.cs() == "label") {
1112                         string label = parse_verbatim_item();
1113                         if (grid.asHullInset()) {
1114                                 grid.asHullInset()->label(cellrow, label);
1115                         } else {
1116                                 cell->push_back(createMathInset(t.cs()));
1117                                 cell->push_back(MathAtom(new MathBraceInset(asArray(label))));
1118                         }
1119                 }
1120
1121                 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1122                         MathAtom at = createMathInset(t.cs());
1123                         at.nucleus()->cell(0) = *cell;
1124                         cell->clear();
1125                         parse(at.nucleus()->cell(1), flags, mode);
1126                         cell->push_back(at);
1127                         return;
1128                 }
1129
1130                 else if (t.cs() == "substack") {
1131                         cell->push_back(createMathInset(t.cs()));
1132                         parse2(cell->back(), FLAG_ITEM, mode, false);
1133                 }
1134
1135                 else if (t.cs() == "xymatrix") {
1136                         cell->push_back(createMathInset(t.cs()));
1137                         parse2(cell->back(), FLAG_ITEM, mode, false);
1138                 }
1139
1140                 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1141                         cell->push_back(createMathInset(t.cs()));
1142                         parse(cell->back().nucleus()->cell(0), FLAG_OPTION, MathInset::TEXT_MODE);
1143                         parse(cell->back().nucleus()->cell(1), FLAG_OPTION, MathInset::TEXT_MODE);
1144                         parse(cell->back().nucleus()->cell(2), FLAG_ITEM, MathInset::TEXT_MODE);
1145                 }
1146
1147 #if 0
1148                 else if (t.cs() == "infer") {
1149                         MathArray ar;
1150                         parse(ar, FLAG_OPTION, mode);
1151                         cell->push_back(createMathInset(t.cs()));
1152                         parse2(cell->back(), FLAG_ITEM, mode, false);
1153                 }
1154
1155                 // Disabled
1156                 else if (1 && t.cs() == "ar") {
1157                         MathXYArrowInset * p = new MathXYArrowInset;
1158                         // try to read target
1159                         parse(p->cell(0), FLAG_OTPTION, mode);
1160                         // try to read label
1161                         if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1162                                 p->up_ = nextToken().cat() == catSuper;
1163                                 getToken();
1164                                 parse(p->cell(1), FLAG_ITEM, mode);
1165                                 //lyxerr << "read label: " << p->cell(1) << "\n";
1166                         }
1167
1168                         cell->push_back(MathAtom(p));
1169                         //lyxerr << "read cell: " << cell << "\n";
1170                 }
1171 #endif
1172
1173                 else if (t.cs().size()) {
1174                         latexkeys const * l = in_word_set(t.cs());
1175                         if (l) {
1176                                 if (l->inset == "font") {
1177                                         cell->push_back(createMathInset(t.cs()));
1178                                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, asMode(mode, l->extra));
1179                                 }
1180
1181                                 else if (l->inset == "oldfont") {
1182                                         cell->push_back(createMathInset(t.cs()));
1183                                         parse(cell->back().nucleus()->cell(0), flags, asMode(mode, l->extra));
1184                                         return;
1185                                 }
1186
1187                                 else if (l->inset == "style") {
1188                                         cell->push_back(createMathInset(t.cs()));
1189                                         parse(cell->back().nucleus()->cell(0), flags, mode);
1190                                         return;
1191                                 }
1192
1193                                 else if (l->inset == "parbox") {
1194                                         // read optional positioning and width
1195                                         string pos   = parse_verbatim_option();
1196                                         string width = parse_verbatim_item();
1197                                         cell->push_back(createMathInset(t.cs()));
1198                                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, MathInset::TEXT_MODE);
1199                                         cell->back().nucleus()->asParboxInset()->setPosition(pos);
1200                                         cell->back().nucleus()->asParboxInset()->setWidth(width);
1201                                 }
1202
1203                                 else {
1204                                         MathAtom at = createMathInset(t.cs());
1205                                         for (MathInset::idx_type i = 0; i < at->nargs(); ++i)
1206                                                 parse(at.nucleus()->cell(i), FLAG_ITEM, asMode(mode, l->extra));
1207                                         cell->push_back(at);
1208                                 }
1209                         }
1210
1211                         else {
1212                                 MathAtom at = createMathInset(t.cs());
1213                                 MathInset::mode_type m = mode;
1214                                 //if (m == MathInset::UNDECIDED_MODE)
1215                                 //lyxerr << "default creation: m1: " << m << "\n";
1216                                 if (at->currentMode() != MathInset::UNDECIDED_MODE)
1217                                         m = at->currentMode();
1218                                 //lyxerr << "default creation: m2: " << m << "\n";
1219                                 MathInset::idx_type start = 0;
1220                                 // this fails on \bigg[...\bigg]
1221                                 //MathArray opt;
1222                                 //parse(opt, FLAG_OPTION, MathInset::VERBATIM_MODE);
1223                                 //if (opt.size()) {
1224                                 //      start = 1;
1225                                 //      at.nucleus()->cell(0) = opt;
1226                                 //}
1227                                 for (MathInset::idx_type i = start; i < at->nargs(); ++i)
1228                                         parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1229                                 cell->push_back(at);
1230                         }
1231                 }
1232
1233
1234                 if (flags & FLAG_LEAVE) {
1235                         flags &= ~FLAG_LEAVE;
1236                         break;
1237                 }
1238         }
1239 }
1240
1241
1242
1243 } // anonymous namespace
1244
1245
1246 void mathed_parse_cell(MathArray & ar, string const & str)
1247 {
1248         istringstream is(str.c_str());
1249         mathed_parse_cell(ar, is);
1250 }
1251
1252
1253 void mathed_parse_cell(MathArray & ar, istream & is)
1254 {
1255         Parser(is).parse(ar, 0, MathInset::MATH_MODE);
1256 }
1257
1258
1259 bool mathed_parse_normal(MathAtom & t, string const & str)
1260 {
1261         istringstream is(str.c_str());
1262         return Parser(is).parse(t);
1263 }
1264
1265
1266 bool mathed_parse_normal(MathAtom & t, istream & is)
1267 {
1268         return Parser(is).parse(t);
1269 }
1270
1271
1272 bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
1273 {
1274         return Parser(lex).parse(t);
1275 }
1276
1277
1278 void mathed_parse_normal(MathGridInset & grid, string const & str)
1279 {
1280         istringstream is(str.c_str());
1281         Parser(is).parse1(grid, 0, MathInset::MATH_MODE, false);
1282 }