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