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