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