]> git.lyx.org Git - lyx.git/blob - src/mathed/MathParser.cpp
'using namespace std' instead of 'using std::xxx'
[lyx.git] / src / mathed / MathParser.cpp
1 /**
2  * \file MathParser.cpp
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   \[\begin{array}{ccc}
30 1
31 &
32
33   \end{array}\]
34
35 */
36
37
38 #include <config.h>
39
40 #include "MathParser.h"
41
42 #include "InsetMathArray.h"
43 #include "InsetMathBig.h"
44 #include "InsetMathBrace.h"
45 #include "InsetMathChar.h"
46 #include "InsetMathColor.h"
47 #include "InsetMathComment.h"
48 #include "InsetMathDelim.h"
49 #include "InsetMathEnv.h"
50 #include "InsetMathFrac.h"
51 #include "InsetMathKern.h"
52 #include "MathMacro.h"
53 #include "InsetMathPar.h"
54 #include "InsetMathRef.h"
55 #include "InsetMathRoot.h"
56 #include "InsetMathScript.h"
57 #include "InsetMathSplit.h"
58 #include "InsetMathSqrt.h"
59 #include "InsetMathTabular.h"
60 #include "MathMacroTemplate.h"
61 #include "MathFactory.h"
62 #include "MathMacroArgument.h"
63 #include "MathSupport.h"
64
65 #include "Lexer.h"
66 #include "support/debug.h"
67
68 #include "support/convert.h"
69 #include "support/docstream.h"
70
71 #include <sstream>
72
73 //#define FILEDEBUG
74
75 using namespace std;
76
77 namespace lyx {
78
79 namespace {
80
81 InsetMath::mode_type asMode(InsetMath::mode_type oldmode, docstring const & str)
82 {
83         //lyxerr << "handling mode: '" << str << "'" << endl;
84         if (str == "mathmode")
85                 return InsetMath::MATH_MODE;
86         if (str == "textmode" || str == "forcetext")
87                 return InsetMath::TEXT_MODE;
88         return oldmode;
89 }
90
91
92 bool stared(docstring const & s)
93 {
94         size_t const n = s.size();
95         return n && s[n - 1] == '*';
96 }
97
98
99 /*!
100  * Add the row \p cellrow to \p grid.
101  * \returns wether the row could be added. Adding a row can fail for
102  * environments like "equation" that have a fixed number of rows.
103  */
104 bool addRow(InsetMathGrid & grid, InsetMathGrid::row_type & cellrow,
105             docstring const & vskip, bool allow_newpage_ = true)
106 {
107         ++cellrow;
108         if (cellrow == grid.nrows()) {
109                 //lyxerr << "adding row " << cellrow << endl;
110                 grid.addRow(cellrow - 1);
111                 if (cellrow == grid.nrows()) {
112                         // We can't add a row to this grid, so let's
113                         // append the content of this cell to the previous
114                         // one.
115                         // This does not happen in well formed .lyx files,
116                         // but LyX versions 1.3.x and older could create
117                         // such files and tex2lyx can still do that.
118                         --cellrow;
119                         lyxerr << "ignoring extra row";
120                         if (!vskip.empty())
121                                 lyxerr << " with extra space " << to_utf8(vskip);
122                         if (!allow_newpage_)
123                                 lyxerr << " with no page break allowed";
124                         lyxerr << '.' << endl;
125                         return false;
126                 }
127         }
128         grid.vcrskip(Length(to_utf8(vskip)), cellrow - 1);
129         grid.rowinfo(cellrow - 1).allow_newpage_ = allow_newpage_;
130         return true;
131 }
132
133
134 /*!
135  * Add the column \p cellcol to \p grid.
136  * \returns wether the column could be added. Adding a column can fail for
137  * environments like "eqnarray" that have a fixed number of columns.
138  */
139 bool addCol(InsetMathGrid & grid, InsetMathGrid::col_type & cellcol)
140 {
141         ++cellcol;
142         if (cellcol == grid.ncols()) {
143                 //lyxerr << "adding column " << cellcol << endl;
144                 grid.addCol(cellcol);
145                 if (cellcol == grid.ncols()) {
146                         // We can't add a column to this grid, so let's
147                         // append the content of this cell to the previous
148                         // one.
149                         // This does not happen in well formed .lyx files,
150                         // but LyX versions 1.3.x and older could create
151                         // such files and tex2lyx can still do that.
152                         --cellcol;
153                         lyxerr << "ignoring extra column." << endl;
154                         return false;
155                 }
156         }
157         return true;
158 }
159
160
161 /*!
162  * Check wether the last row is empty and remove it if yes.
163  * Otherwise the following code
164  * \verbatim
165 \begin{array}{|c|c|}
166 \hline
167 1 & 2 \\ \hline
168 3 & 4 \\ \hline
169 \end{array}
170  * \endverbatim
171  * will result in a grid with 3 rows (+ the dummy row that is always present),
172  * because the last '\\' opens a new row.
173  */
174 void delEmptyLastRow(InsetMathGrid & grid)
175 {
176         InsetMathGrid::row_type const row = grid.nrows() - 1;
177         for (InsetMathGrid::col_type col = 0; col < grid.ncols(); ++col) {
178                 if (!grid.cell(grid.index(row, col)).empty())
179                         return;
180         }
181         // Copy the row information of the empty row (which would contain the
182         // last hline in the example above) to the dummy row and delete the
183         // empty row.
184         grid.rowinfo(row + 1) = grid.rowinfo(row);
185         grid.delRow(row);
186 }
187
188
189 // These are TeX's catcodes
190 enum CatCode {
191         catEscape,     // 0    backslash
192         catBegin,      // 1    {
193         catEnd,        // 2    }
194         catMath,       // 3    $
195         catAlign,      // 4    &
196         catNewline,    // 5    ^^M
197         catParameter,  // 6    #
198         catSuper,      // 7    ^
199         catSub,        // 8    _
200         catIgnore,     // 9
201         catSpace,      // 10   space
202         catLetter,     // 11   a-zA-Z
203         catOther,      // 12   none of the above
204         catActive,     // 13   ~
205         catComment,    // 14   %
206         catInvalid     // 15   <delete>
207 };
208
209 CatCode theCatcode[128];
210
211
212 inline CatCode catcode(char_type c)
213 {
214         /* The only characters that are not catOther lie in the pure ASCII
215          * range. Therefore theCatcode has only 128 entries.
216          * TeX itself deals with 8bit characters, so if needed this table
217          * could be enlarged to 256 entries.
218          * Any larger value does not make sense, since the fact that we use
219          * unicode internally does not change Knuth's TeX engine.
220          * Apart from that a table for the full 21bit UCS4 range would waste
221          * too much memory. */
222         if (c >= 128)
223                 return catOther;
224
225         return theCatcode[c];
226 }
227
228
229 enum {
230         FLAG_ALIGN      = 1 << 0,  //  next & or \\ ends the parsing process
231         FLAG_BRACE_LAST = 1 << 1,  //  next closing brace ends the parsing
232         FLAG_RIGHT      = 1 << 2,  //  next \\right ends the parsing process
233         FLAG_END        = 1 << 3,  //  next \\end ends the parsing process
234         FLAG_BRACK_LAST = 1 << 4,  //  next closing bracket ends the parsing
235         FLAG_TEXTMODE   = 1 << 5,  //  we are in a box
236         FLAG_ITEM       = 1 << 6,  //  read a (possibly braced) token
237         FLAG_LEAVE      = 1 << 7,  //  leave the loop at the end
238         FLAG_SIMPLE     = 1 << 8,  //  next $ leaves the loop
239         FLAG_EQUATION   = 1 << 9,  //  next \] leaves the loop
240         FLAG_SIMPLE2    = 1 << 10, //  next \) leaves the loop
241         FLAG_OPTION     = 1 << 11, //  read [...] style option
242         FLAG_BRACED     = 1 << 12  //  read {...} style argument
243 };
244
245
246 //
247 // Helper class for parsing
248 //
249
250 class Token {
251 public:
252         ///
253         Token() : cs_(), char_(0), cat_(catIgnore) {}
254         ///
255         Token(char_type c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
256         ///
257         explicit Token(docstring const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
258
259         ///
260         docstring const & cs() const { return cs_; }
261         ///
262         CatCode cat() const { return cat_; }
263         ///
264         char_type character() const { return char_; }
265         ///
266         docstring asString() const { return cs_.size() ? cs_ : docstring(1, char_); }
267         ///
268         docstring asInput() const { return cs_.size() ? '\\' + cs_ : docstring(1, char_); }
269
270 private:
271         ///
272         docstring cs_;
273         ///
274         char_type char_;
275         ///
276         CatCode cat_;
277 };
278
279
280 ostream & operator<<(ostream & os, Token const & t)
281 {
282         if (t.cs().size()) {
283                 docstring const & cs = t.cs();
284                 // FIXME: For some strange reason, the stream operator instanciate
285                 // a new Token before outputting the contents of t.cs().
286                 // Because of this the line
287                 //     os << '\\' << cs;
288                 // below becomes recursive.
289                 // In order to avoid that we return early:
290                 if (cs == "\\")
291                         return os;
292                 os << '\\' << to_utf8(cs);
293         }
294         else if (t.cat() == catLetter)
295                 os << t.character();
296         else
297                 os << '[' << t.character() << ',' << t.cat() << ']';
298         return os;
299 }
300
301
302 class Parser {
303 public:
304         ///
305         typedef  InsetMath::mode_type mode_type;
306
307         ///
308         Parser(Lexer & lex);
309         /// Only use this for reading from .lyx file format, for the reason
310         /// see Parser::tokenize(std::istream &).
311         Parser(istream & is);
312         ///
313         Parser(docstring const & str);
314
315         ///
316         bool parse(MathAtom & at);
317         ///
318         void parse(MathData & array, unsigned flags, mode_type mode);
319         ///
320         void parse1(InsetMathGrid & grid, unsigned flags, mode_type mode,
321                 bool numbered);
322         ///
323         MathData parse(unsigned flags, mode_type mode);
324         ///
325         int lineno() const { return lineno_; }
326         ///
327         void putback();
328
329 private:
330         ///
331         void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
332         /// get arg delimited by 'left' and 'right'
333         docstring getArg(char_type left, char_type right);
334         ///
335         char_type getChar();
336         ///
337         void error(string const & msg);
338         void error(docstring const & msg) { error(to_utf8(msg)); }
339         /// dump contents to screen
340         void dump() const;
341         /// Only use this for reading from .lyx file format (see
342         /// implementation for reason)
343         void tokenize(istream & is);
344         ///
345         void tokenize(docstring const & s);
346         ///
347         void skipSpaceTokens(idocstream & is, char_type c);
348         ///
349         void push_back(Token const & t);
350         ///
351         void pop_back();
352         ///
353         Token const & prevToken() const;
354         ///
355         Token const & nextToken() const;
356         ///
357         Token const & getToken();
358         /// skips spaces if any
359         void skipSpaces();
360         ///
361         void lex(docstring const & s);
362         ///
363         bool good() const;
364         ///
365         docstring parse_verbatim_item();
366         ///
367         docstring parse_verbatim_option();
368
369         ///
370         int lineno_;
371         ///
372         vector<Token> tokens_;
373         ///
374         unsigned pos_;
375         /// Stack of active environments
376         vector<docstring> environments_;
377 };
378
379
380 Parser::Parser(Lexer & lexer)
381         : lineno_(lexer.getLineNo()), pos_(0)
382 {
383         tokenize(lexer.getStream());
384         lexer.eatLine();
385 }
386
387
388 Parser::Parser(istream & is)
389         : lineno_(0), pos_(0)
390 {
391         tokenize(is);
392 }
393
394
395 Parser::Parser(docstring const & str)
396         : lineno_(0), pos_(0)
397 {
398         tokenize(str);
399 }
400
401
402 void Parser::push_back(Token const & t)
403 {
404         tokens_.push_back(t);
405 }
406
407
408 void Parser::pop_back()
409 {
410         tokens_.pop_back();
411 }
412
413
414 Token const & Parser::prevToken() const
415 {
416         static const Token dummy;
417         return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
418 }
419
420
421 Token const & Parser::nextToken() const
422 {
423         static const Token dummy;
424         return good() ? tokens_[pos_] : dummy;
425 }
426
427
428 Token const & Parser::getToken()
429 {
430         static const Token dummy;
431         //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
432         return good() ? tokens_[pos_++] : dummy;
433 }
434
435
436 void Parser::skipSpaces()
437 {
438         while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
439                 getToken();
440 }
441
442
443 void Parser::putback()
444 {
445         --pos_;
446 }
447
448
449 bool Parser::good() const
450 {
451         return pos_ < tokens_.size();
452 }
453
454
455 char_type Parser::getChar()
456 {
457         if (!good()) {
458                 error("The input stream is not well...");
459                 putback();
460                 return 0;
461         }
462         return tokens_[pos_++].character();
463 }
464
465
466 docstring Parser::getArg(char_type left, char_type right)
467 {
468         skipSpaces();
469
470         docstring result;
471         char_type c = getChar();
472
473         if (c != left)
474                 putback();
475         else
476                 while ((c = getChar()) != right && good())
477                         result += c;
478
479         return result;
480 }
481
482
483 void Parser::skipSpaceTokens(idocstream & is, char_type c)
484 {
485         // skip trailing spaces
486         while (catcode(c) == catSpace || catcode(c) == catNewline)
487                 if (!is.get(c))
488                         break;
489         //lyxerr << "putting back: " << c << endl;
490         is.putback(c);
491 }
492
493
494 void Parser::tokenize(istream & is)
495 {
496         // eat everything up to the next \end_inset or end of stream
497         // and store it in s for further tokenization
498         string s;
499         char c;
500         while (is.get(c)) {
501                 s += c;
502                 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
503                         s = s.substr(0, s.size() - 10);
504                         break;
505                 }
506         }
507         // Remove the space after \end_inset
508         if (is.get(c) && c != ' ')
509                 is.unget();
510
511         // tokenize buffer
512         tokenize(from_utf8(s));
513 }
514
515
516 void Parser::tokenize(docstring const & buffer)
517 {
518         idocstringstream is(buffer, ios::in | ios::binary);
519
520         char_type c;
521         while (is.get(c)) {
522                 //lyxerr << "reading c: " << c << endl;
523
524                 switch (catcode(c)) {
525                         case catNewline: {
526                                 ++lineno_;
527                                 is.get(c);
528                                 if (catcode(c) == catNewline)
529                                         ; //push_back(Token("par"));
530                                 else {
531                                         push_back(Token('\n', catNewline));
532                                         is.putback(c);
533                                 }
534                                 break;
535                         }
536
537 /*
538                         case catComment: {
539                                 while (is.get(c) && catcode(c) != catNewline)
540                                         ;
541                                 ++lineno_;
542                                 break;
543                         }
544 */
545
546                         case catEscape: {
547                                 is.get(c);
548                                 if (!is) {
549                                         error("unexpected end of input");
550                                 } else {
551                                         docstring s(1, c);
552                                         if (catcode(c) == catLetter) {
553                                                 // collect letters
554                                                 while (is.get(c) && catcode(c) == catLetter)
555                                                         s += c;
556                                                 skipSpaceTokens(is, c);
557                                         }
558                                         push_back(Token(s));
559                                 }
560                                 break;
561                         }
562
563                         case catSuper:
564                         case catSub: {
565                                 push_back(Token(c, catcode(c)));
566                                 is.get(c);
567                                 skipSpaceTokens(is, c);
568                                 break;
569                         }
570
571                         case catIgnore: {
572                                 lyxerr << "ignoring a char: " << int(c) << endl;
573                                 break;
574                         }
575
576                         default:
577                                 push_back(Token(c, catcode(c)));
578                 }
579         }
580
581 #ifdef FILEDEBUG
582         dump();
583 #endif
584 }
585
586
587 void Parser::dump() const
588 {
589         lyxerr << "\nTokens: ";
590         for (unsigned i = 0; i < tokens_.size(); ++i) {
591                 if (i == pos_)
592                         lyxerr << " <#> ";
593                 lyxerr << tokens_[i];
594         }
595         lyxerr << " pos: " << pos_ << endl;
596 }
597
598
599 void Parser::error(string const & msg)
600 {
601         lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
602         dump();
603         //exit(1);
604 }
605
606
607 bool Parser::parse(MathAtom & at)
608 {
609         skipSpaces();
610         MathData ar;
611         parse(ar, false, InsetMath::UNDECIDED_MODE);
612         if (ar.size() != 1 || ar.front()->getType() == hullNone) {
613                 lyxerr << "unusual contents found: " << ar << endl;
614                 at = MathAtom(new InsetMathPar(ar));
615                 //if (at->nargs() > 0)
616                 //      at.nucleus()->cell(0) = ar;
617                 //else
618                 //      lyxerr << "unusual contents found: " << ar << endl;
619                 return true;
620         }
621         at = ar[0];
622         return true;
623 }
624
625
626 docstring Parser::parse_verbatim_option()
627 {
628         skipSpaces();
629         docstring res;
630         if (nextToken().character() == '[') {
631                 Token t = getToken();
632                 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
633                         if (t.cat() == catBegin) {
634                                 putback();
635                                 res += '{' + parse_verbatim_item() + '}';
636                         } else
637                                 res += t.asString();
638                 }
639         }
640         return res;
641 }
642
643
644 docstring Parser::parse_verbatim_item()
645 {
646         skipSpaces();
647         docstring res;
648         if (nextToken().cat() == catBegin) {
649                 Token t = getToken();
650                 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
651                         if (t.cat() == catBegin) {
652                                 putback();
653                                 res += '{' + parse_verbatim_item() + '}';
654                         }
655                         else
656                                 res += t.asString();
657                 }
658         }
659         return res;
660 }
661
662
663 MathData Parser::parse(unsigned flags, mode_type mode)
664 {
665         MathData ar;
666         parse(ar, flags, mode);
667         return ar;
668 }
669
670
671 void Parser::parse(MathData & array, unsigned flags, mode_type mode)
672 {
673         InsetMathGrid grid(1, 1);
674         parse1(grid, flags, mode, false);
675         array = grid.cell(0);
676 }
677
678
679 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
680         const bool numbered)
681 {
682         parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
683 }
684
685
686 void Parser::parse1(InsetMathGrid & grid, unsigned flags,
687         const mode_type mode, const bool numbered)
688 {
689         int limits = 0;
690         InsetMathGrid::row_type cellrow = 0;
691         InsetMathGrid::col_type cellcol = 0;
692         MathData * cell = &grid.cell(grid.index(cellrow, cellcol));
693
694         if (grid.asHullInset())
695                 grid.asHullInset()->numbered(cellrow, numbered);
696
697         //dump();
698         //lyxerr << " flags: " << flags << endl;
699         //lyxerr << " mode: " << mode  << endl;
700         //lyxerr << "grid: " << grid << endl;
701
702         while (good()) {
703                 Token const & t = getToken();
704
705 #ifdef FILEDEBUG
706                 lyxerr << "t: " << t << " flags: " << flags << endl;
707                 lyxerr << "mode: " << mode  << endl;
708                 cell->dump();
709                 lyxerr << endl;
710 #endif
711
712                 if (flags & FLAG_ITEM) {
713
714                         if (t.cat() == catBegin) {
715                                 // skip the brace and collect everything to the next matching
716                                 // closing brace
717                                 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
718                                 return;
719                         }
720
721                         // handle only this single token, leave the loop if done
722                         flags = FLAG_LEAVE;
723                 }
724
725
726                 if (flags & FLAG_BRACED) {
727                         if (t.cat() == catSpace)
728                                 continue;
729
730                         if (t.cat() != catBegin) {
731                                 error("opening brace expected");
732                                 return;
733                         }
734
735                         // skip the brace and collect everything to the next matching
736                         // closing brace
737                         flags = FLAG_BRACE_LAST;
738                 }
739
740
741                 if (flags & FLAG_OPTION) {
742                         if (t.cat() == catOther && t.character() == '[') {
743                                 MathData ar;
744                                 parse(ar, FLAG_BRACK_LAST, mode);
745                                 cell->append(ar);
746                         } else {
747                                 // no option found, put back token and we are done
748                                 putback();
749                         }
750                         return;
751                 }
752
753                 //
754                 // cat codes
755                 //
756                 if (t.cat() == catMath) {
757                         if (mode != InsetMath::MATH_MODE) {
758                                 // we are inside some text mode thingy, so opening new math is allowed
759                                 Token const & n = getToken();
760                                 if (n.cat() == catMath) {
761                                         // TeX's $$...$$ syntax for displayed math
762                                         cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
763                                         parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
764                                         getToken(); // skip the second '$' token
765                                 } else {
766                                         // simple $...$  stuff
767                                         putback();
768                                         cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
769                                         parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
770                                 }
771                         }
772
773                         else if (flags & FLAG_SIMPLE) {
774                                 // this is the end of the formula
775                                 return;
776                         }
777
778                         else {
779                                 error("something strange in the parser");
780                                 break;
781                         }
782                 }
783
784                 else if (t.cat() == catLetter)
785                         cell->push_back(MathAtom(new InsetMathChar(t.character())));
786
787                 else if (t.cat() == catSpace && mode != InsetMath::MATH_MODE) {
788                         if (cell->empty() || cell->back()->getChar() != ' ')
789                                 cell->push_back(MathAtom(new InsetMathChar(t.character())));
790                 }
791
792                 else if (t.cat() == catNewline && mode != InsetMath::MATH_MODE) {
793                         if (cell->empty() || cell->back()->getChar() != ' ')
794                                 cell->push_back(MathAtom(new InsetMathChar(' ')));
795                 }
796
797                 else if (t.cat() == catParameter) {
798                         Token const & n = getToken();
799                         cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
800                 }
801
802                 else if (t.cat() == catActive)
803                         cell->push_back(MathAtom(new InsetMathChar(t.character())));
804
805                 else if (t.cat() == catBegin) {
806                         MathData ar;
807                         parse(ar, FLAG_BRACE_LAST, mode);
808                         // do not create a BraceInset if they were written by LyX
809                         // this helps to keep the annoyance of  "a choose b"  to a minimum
810                         if (ar.size() == 1 && ar[0]->extraBraces())
811                                 cell->append(ar);
812                         else
813                                 cell->push_back(MathAtom(new InsetMathBrace(ar)));
814                 }
815
816                 else if (t.cat() == catEnd) {
817                         if (flags & FLAG_BRACE_LAST)
818                                 return;
819                         error("found '}' unexpectedly");
820                         //BOOST_ASSERT(false);
821                         //add(cell, '}', LM_TC_TEX);
822                 }
823
824                 else if (t.cat() == catAlign) {
825                         //lyxerr << " column now " << (cellcol + 1)
826                         //       << " max: " << grid.ncols() << endl;
827                         if (flags & FLAG_ALIGN)
828                                 return;
829                         if (addCol(grid, cellcol))
830                                 cell = &grid.cell(grid.index(cellrow, cellcol));
831                 }
832
833                 else if (t.cat() == catSuper || t.cat() == catSub) {
834                         bool up = (t.cat() == catSuper);
835                         // we need no new script inset if the last thing was a scriptinset,
836                         // which has that script already not the same script already
837                         if (!cell->size())
838                                 cell->push_back(MathAtom(new InsetMathScript(up)));
839                         else if (cell->back()->asScriptInset() &&
840                                         !cell->back()->asScriptInset()->has(up))
841                                 cell->back().nucleus()->asScriptInset()->ensure(up);
842                         else if (cell->back()->asScriptInset())
843                                 cell->push_back(MathAtom(new InsetMathScript(up)));
844                         else
845                                 cell->back() = MathAtom(new InsetMathScript(cell->back(), up));
846                         InsetMathScript * p = cell->back().nucleus()->asScriptInset();
847                         // special handling of {}-bases
848                         // Here we could remove the brace inset for things
849                         // like {a'}^2 and add the braces back in
850                         // InsetMathScript::write().
851                         // We do not do it, since it is not possible to detect
852                         // reliably whether the braces are needed because the
853                         // nucleus contains more than one symbol, or whether
854                         // they are needed for unknown commands like \xx{a}_0
855                         // or \yy{a}{b}_0. This was done in revision 14819
856                         // in an unreliable way. See this thread
857                         // http://www.mail-archive.com/lyx-devel%40lists.lyx.org/msg104917.html
858                         // for more details.
859                         parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
860                         if (limits) {
861                                 p->limits(limits);
862                                 limits = 0;
863                         }
864                 }
865
866                 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
867                         //lyxerr << "finished reading option" << endl;
868                         return;
869                 }
870
871                 else if (t.cat() == catOther)
872                         cell->push_back(MathAtom(new InsetMathChar(t.character())));
873
874                 else if (t.cat() == catComment) {
875                         docstring s;
876                         while (good()) {
877                                 Token const & t = getToken();
878                                 if (t.cat() == catNewline)
879                                         break;
880                                 s += t.asString();
881                         }
882                         cell->push_back(MathAtom(new InsetMathComment(s)));
883                         skipSpaces();
884                 }
885
886                 //
887                 // control sequences
888                 //
889
890                 else if (t.cs() == "lyxlock") {
891                         if (cell->size())
892                                 cell->back().nucleus()->lock(true);
893                 }
894
895                 else if (t.cs() == "def" ||
896                         t.cs() == "newcommand" ||
897                         t.cs() == "renewcommand")
898                 {
899                         docstring const type = t.cs();
900                         docstring name;
901                         int nargs = 0;
902                         int optionals = 0;
903                         std::vector<MathData> optionalValues;
904                         if (t.cs() == "def") {
905                                 // get name
906                                 name = getToken().cs();
907
908                                 // read parameter
909                                 docstring pars;
910                                 while (good() && nextToken().cat() != catBegin) {
911                                         pars += getToken().cs();
912                                         ++nargs;
913                                 }
914                                 nargs /= 2;
915                                 //lyxerr << "read \\def parameter list '" << pars << "'" << endl;
916
917                         } else { // t.cs() == "newcommand" || t.cs() == "renewcommand"
918                                 if (getToken().cat() != catBegin) {
919                                         error("'{' in \\newcommand expected (1) ");
920                                         return;
921                                 }
922
923                                 name = getToken().cs();
924
925                                 if (getToken().cat() != catEnd) {
926                                         error("'}' in \\newcommand expected");
927                                         return;
928                                 }
929
930                                 docstring const arg = getArg('[', ']');
931                                 if (!arg.empty())
932                                         nargs = convert<int>(arg);
933
934                                 // optional argument given?
935                                 skipSpaces();
936                                 while (nextToken().character() == '[') {
937                                         getToken();
938                                         optionalValues.push_back(MathData());
939                                         parse(optionalValues[optionals], FLAG_BRACK_LAST, mode);
940                                         ++optionals;
941                                 }
942                         }
943
944                         MathData def;
945                         parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
946
947                         // is a version for display attached?
948                         skipSpaces();
949                         MathData display;
950                         if (nextToken().cat() == catBegin)
951                                 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
952
953                         cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, optionals, type, 
954                                                                                                                                                                                                                  optionalValues, def, display)));
955                 }
956
957                 else if (t.cs() == "(") {
958                         cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
959                         parse2(cell->back(), FLAG_SIMPLE2, InsetMath::MATH_MODE, false);
960                 }
961
962                 else if (t.cs() == "[") {
963                         cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
964                         parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false);
965                 }
966
967                 else if (t.cs() == "protect")
968                         // ignore \\protect, will hopefully be re-added during output
969                         ;
970
971                 else if (t.cs() == "end") {
972                         if (flags & FLAG_END) {
973                                 // eat environment name
974                                 docstring const name = getArg('{', '}');
975                                 if (environments_.empty())
976                                         error("'found \\end{" + name +
977                                               "}' without matching '\\begin{" +
978                                               name + "}'");
979                                 else if (name != environments_.back())
980                                         error("'\\end{" + name +
981                                               "}' does not match '\\begin{" +
982                                               environments_.back() + "}'");
983                                 else {
984                                         environments_.pop_back();
985                                         // Delete empty last row in matrix
986                                         // like insets.
987                                         // If you abuse InsetMathGrid for
988                                         // non-matrix like structures you
989                                         // probably need to refine this test.
990                                         // Right now we only have to test for
991                                         // single line hull insets.
992                                         if (grid.nrows() > 1)
993                                                 delEmptyLastRow(grid);
994                                         return;
995                                 }
996                         } else
997                                 error("found 'end' unexpectedly");
998                 }
999
1000                 else if (t.cs() == ")") {
1001                         if (flags & FLAG_SIMPLE2)
1002                                 return;
1003                         error("found '\\)' unexpectedly");
1004                 }
1005
1006                 else if (t.cs() == "]") {
1007                         if (flags & FLAG_EQUATION)
1008                                 return;
1009                         error("found '\\]' unexpectedly");
1010                 }
1011
1012                 else if (t.cs() == "\\") {
1013                         if (flags & FLAG_ALIGN)
1014                                 return;
1015                         bool added = false;
1016                         if (nextToken().asInput() == "*") {
1017                                 getToken();
1018                                 added = addRow(grid, cellrow, docstring(), false);
1019                         } else if (good())
1020                                 added = addRow(grid, cellrow, getArg('[', ']'));
1021                         else
1022                                 error("missing token after \\\\");
1023                         if (added) {
1024                                 cellcol = 0;
1025                                 if (grid.asHullInset())
1026                                         grid.asHullInset()->numbered(
1027                                                         cellrow, numbered);
1028                                 cell = &grid.cell(grid.index(cellrow,
1029                                                              cellcol));
1030                         }
1031                 }
1032
1033 #if 0
1034                 else if (t.cs() == "multicolumn") {
1035                         // extract column count and insert dummy cells
1036                         MathData count;
1037                         parse(count, FLAG_ITEM, mode);
1038                         int cols = 1;
1039                         if (!extractNumber(count, cols)) {
1040                                 lyxerr << " can't extract number of cells from " << count << endl;
1041                         }
1042                         // resize the table if necessary
1043                         for (int i = 0; i < cols; ++i) {
1044                                 if (addCol(grid, cellcol)) {
1045                                         cell = &grid.cell(grid.index(
1046                                                         cellrow, cellcol));
1047                                         // mark this as dummy
1048                                         grid.cellinfo(grid.index(
1049                                                 cellrow, cellcol)).dummy_ = true;
1050                                 }
1051                         }
1052                         // the last cell is the real thing, not a dummy
1053                         grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
1054
1055                         // read special alignment
1056                         MathData align;
1057                         parse(align, FLAG_ITEM, mode);
1058                         //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
1059
1060                         // parse the remaining contents into the "real" cell
1061                         parse(*cell, FLAG_ITEM, mode);
1062                 }
1063 #endif
1064
1065                 else if (t.cs() == "limits")
1066                         limits = 1;
1067
1068                 else if (t.cs() == "nolimits")
1069                         limits = -1;
1070
1071                 else if (t.cs() == "nonumber") {
1072                         if (grid.asHullInset())
1073                                 grid.asHullInset()->numbered(cellrow, false);
1074                 }
1075
1076                 else if (t.cs() == "number") {
1077                         if (grid.asHullInset())
1078                                 grid.asHullInset()->numbered(cellrow, true);
1079                 }
1080
1081                 else if (t.cs() == "hline") {
1082                         grid.rowinfo(cellrow).lines_ ++;
1083                 }
1084
1085                 else if (t.cs() == "sqrt") {
1086                         MathData ar;
1087                         parse(ar, FLAG_OPTION, mode);
1088                         if (ar.size()) {
1089                                 cell->push_back(MathAtom(new InsetMathRoot));
1090                                 cell->back().nucleus()->cell(0) = ar;
1091                                 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1092                         } else {
1093                                 cell->push_back(MathAtom(new InsetMathSqrt));
1094                                 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1095                         }
1096                 }
1097
1098                 else if (t.cs() == "unit") {
1099                         // Allowed formats \unit[val]{unit}
1100                         MathData ar;
1101                         parse(ar, FLAG_OPTION, mode);
1102                         if (ar.size()) {
1103                                 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNIT)));
1104                                 cell->back().nucleus()->cell(0) = ar;
1105                                 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1106                         } else {
1107                                 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNIT, 1)));
1108                                 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1109                         }
1110                 }
1111                 else if (t.cs() == "unitfrac") {
1112                         // Here allowed formats are \unitfrac[val]{num}{denom}
1113                         MathData ar;
1114                         parse(ar, FLAG_OPTION, mode);
1115                         if (ar.size()) {
1116                                 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC, 3)));
1117                                 cell->back().nucleus()->cell(2) = ar;
1118                         } else {
1119                                 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC)));
1120                         }
1121                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1122                         parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1123                 }
1124
1125                 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1126                         cell->push_back(createInsetMath(t.cs()));
1127                         parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1128                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1129                 }
1130
1131                 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
1132                                 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1133                         cell->push_back(MathAtom(new InsetMathRef(t.cs())));
1134                         parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1135                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1136                 }
1137
1138                 else if (t.cs() == "left") {
1139                         skipSpaces();
1140                         Token const & tl = getToken();
1141                         // \| and \Vert are equivalent, and InsetMathDelim
1142                         // can't handle \|
1143                         // FIXME: fix this in InsetMathDelim itself!
1144                         docstring const l = tl.cs() == "|" ? from_ascii("Vert") : tl.asString();
1145                         MathData ar;
1146                         parse(ar, FLAG_RIGHT, mode);
1147                         if (!good())
1148                                 break;
1149                         skipSpaces();
1150                         Token const & tr = getToken();
1151                         docstring const r = tr.cs() == "|" ? from_ascii("Vert") : tr.asString();
1152                         cell->push_back(MathAtom(new InsetMathDelim(l, r, ar)));
1153                 }
1154
1155                 else if (t.cs() == "right") {
1156                         if (flags & FLAG_RIGHT)
1157                                 return;
1158                         //lyxerr << "got so far: '" << cell << "'" << endl;
1159                         error("Unmatched right delimiter");
1160                         return;
1161                 }
1162
1163                 else if (t.cs() == "begin") {
1164                         docstring const name = getArg('{', '}');
1165                         environments_.push_back(name);
1166
1167                         if (name == "array" || name == "subarray") {
1168                                 docstring const valign = parse_verbatim_option() + 'c';
1169                                 docstring const halign = parse_verbatim_item();
1170                                 cell->push_back(MathAtom(new InsetMathArray(name, (char)valign[0], halign)));
1171                                 parse2(cell->back(), FLAG_END, mode, false);
1172                         }
1173
1174                         else if (name == "tabular") {
1175                                 docstring const valign = parse_verbatim_option() + 'c';
1176                                 docstring const halign = parse_verbatim_item();
1177                                 cell->push_back(MathAtom(new InsetMathTabular(name, (char)valign[0], halign)));
1178                                 parse2(cell->back(), FLAG_END, InsetMath::TEXT_MODE, false);
1179                         }
1180
1181                         else if (name == "split" || name == "cases") {
1182                                 cell->push_back(createInsetMath(name));
1183                                 parse2(cell->back(), FLAG_END, mode, false);
1184                         }
1185
1186                         else if (name == "alignedat") {
1187                                 docstring const valign = parse_verbatim_option() + 'c';
1188                                 // ignore this for a while
1189                                 getArg('{', '}');
1190                                 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1191                                 parse2(cell->back(), FLAG_END, mode, false);
1192                         }
1193
1194                         else if (name == "math") {
1195                                 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
1196                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, true);
1197                         }
1198
1199                         else if (name == "equation" || name == "equation*"
1200                                         || name == "displaymath") {
1201                                 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
1202                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation"));
1203                         }
1204
1205                         else if (name == "eqnarray" || name == "eqnarray*") {
1206                                 cell->push_back(MathAtom(new InsetMathHull(hullEqnArray)));
1207                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1208                         }
1209
1210                         else if (name == "align" || name == "align*") {
1211                                 cell->push_back(MathAtom(new InsetMathHull(hullAlign)));
1212                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1213                         }
1214
1215                         else if (name == "flalign" || name == "flalign*") {
1216                                 cell->push_back(MathAtom(new InsetMathHull(hullFlAlign)));
1217                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1218                         }
1219
1220                         else if (name == "alignat" || name == "alignat*") {
1221                                 // ignore this for a while
1222                                 getArg('{', '}');
1223                                 cell->push_back(MathAtom(new InsetMathHull(hullAlignAt)));
1224                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1225                         }
1226
1227                         else if (name == "xalignat" || name == "xalignat*") {
1228                                 // ignore this for a while
1229                                 getArg('{', '}');
1230                                 cell->push_back(MathAtom(new InsetMathHull(hullXAlignAt)));
1231                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1232                         }
1233
1234                         else if (name == "xxalignat") {
1235                                 // ignore this for a while
1236                                 getArg('{', '}');
1237                                 cell->push_back(MathAtom(new InsetMathHull(hullXXAlignAt)));
1238                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1239                         }
1240
1241                         else if (name == "multline" || name == "multline*") {
1242                                 cell->push_back(MathAtom(new InsetMathHull(hullMultline)));
1243                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1244                         }
1245
1246                         else if (name == "gather" || name == "gather*") {
1247                                 cell->push_back(MathAtom(new InsetMathHull(hullGather)));
1248                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1249                         }
1250
1251                         else if (latexkeys const * l = in_word_set(name)) {
1252                                 if (l->inset == "matrix") {
1253                                         cell->push_back(createInsetMath(name));
1254                                         parse2(cell->back(), FLAG_END, mode, false);
1255                                 } else if (l->inset == "split") {
1256                                         docstring const valign = parse_verbatim_option() + 'c';
1257                                         cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1258                                         parse2(cell->back(), FLAG_END, mode, false);
1259                                 } else {
1260                                         dump();
1261                                         lyxerr << "found math environment `" << to_utf8(name)
1262                                                << "' in symbols file with unsupported inset `"
1263                                                << to_utf8(l->inset) << "'." << endl;
1264                                         // create generic environment inset
1265                                         cell->push_back(MathAtom(new InsetMathEnv(name)));
1266                                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1267                                 }
1268                         }
1269
1270                         else {
1271                                 dump();
1272                                 lyxerr << "found unknown math environment '" << to_utf8(name)
1273                                         << "'" << endl;
1274                                 // create generic environment inset
1275                                 cell->push_back(MathAtom(new InsetMathEnv(name)));
1276                                 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1277                         }
1278                 }
1279
1280                 else if (t.cs() == "kern") {
1281                         // FIXME: A hack...
1282                         docstring s;
1283                         while (true) {
1284                                 Token const & t = getToken();
1285                                 if (!good()) {
1286                                         putback();
1287                                         break;
1288                                 }
1289                                 s += t.character();
1290                                 if (isValidLength(to_utf8(s)))
1291                                         break;
1292                         }
1293                         cell->push_back(MathAtom(new InsetMathKern(s)));
1294                 }
1295
1296                 else if (t.cs() == "label") {
1297                         // FIXME: This is swallowed in inline formulas
1298                         docstring label = parse_verbatim_item();
1299                         MathData ar;
1300                         asArray(label, ar);
1301                         if (grid.asHullInset()) {
1302                                 grid.asHullInset()->label(cellrow, label);
1303                         } else {
1304                                 cell->push_back(createInsetMath(t.cs()));
1305                                 cell->push_back(MathAtom(new InsetMathBrace(ar)));
1306                         }
1307                 }
1308
1309                 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1310                         MathAtom at = createInsetMath(t.cs());
1311                         at.nucleus()->cell(0) = *cell;
1312                         cell->clear();
1313                         parse(at.nucleus()->cell(1), flags, mode);
1314                         cell->push_back(at);
1315                         return;
1316                 }
1317
1318                 else if (t.cs() == "color") {
1319                         docstring const color = parse_verbatim_item();
1320                         cell->push_back(MathAtom(new InsetMathColor(true, color)));
1321                         parse(cell->back().nucleus()->cell(0), flags, mode);
1322                         return;
1323                 }
1324
1325                 else if (t.cs() == "textcolor") {
1326                         docstring const color = parse_verbatim_item();
1327                         cell->push_back(MathAtom(new InsetMathColor(false, color)));
1328                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1329                 }
1330
1331                 else if (t.cs() == "normalcolor") {
1332                         cell->push_back(createInsetMath(t.cs()));
1333                         parse(cell->back().nucleus()->cell(0), flags, mode);
1334                         return;
1335                 }
1336
1337                 else if (t.cs() == "substack") {
1338                         cell->push_back(createInsetMath(t.cs()));
1339                         parse2(cell->back(), FLAG_ITEM, mode, false);
1340                 }
1341
1342                 else if (t.cs() == "xymatrix") {
1343                         odocstringstream os;
1344                         while (good() && nextToken().cat() != catBegin)
1345                                 os << getToken().asInput();
1346                         cell->push_back(createInsetMath(t.cs() + os.str()));
1347                         parse2(cell->back(), FLAG_ITEM, mode, false);
1348                 }
1349
1350                 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1351                         cell->push_back(createInsetMath(t.cs()));
1352                         parse(cell->back().nucleus()->cell(0), FLAG_OPTION, InsetMath::TEXT_MODE);
1353                         parse(cell->back().nucleus()->cell(1), FLAG_OPTION, InsetMath::TEXT_MODE);
1354                         parse(cell->back().nucleus()->cell(2), FLAG_ITEM, InsetMath::TEXT_MODE);
1355                 }
1356
1357                 else if (t.cs() == "tag") {
1358                         if (nextToken().character() == '*') {
1359                                 getToken();
1360                                 cell->push_back(createInsetMath(t.cs() + '*'));
1361                         } else
1362                                 cell->push_back(createInsetMath(t.cs()));
1363                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1364                 }
1365
1366 #if 0
1367                 else if (t.cs() == "infer") {
1368                         MathData ar;
1369                         parse(ar, FLAG_OPTION, mode);
1370                         cell->push_back(createInsetMath(t.cs()));
1371                         parse2(cell->back(), FLAG_ITEM, mode, false);
1372                 }
1373
1374                 // Disabled
1375                 else if (1 && t.cs() == "ar") {
1376                         auto_ptr<InsetMathXYArrow> p(new InsetMathXYArrow);
1377                         // try to read target
1378                         parse(p->cell(0), FLAG_OTPTION, mode);
1379                         // try to read label
1380                         if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1381                                 p->up_ = nextToken().cat() == catSuper;
1382                                 getToken();
1383                                 parse(p->cell(1), FLAG_ITEM, mode);
1384                                 //lyxerr << "read label: " << p->cell(1) << endl;
1385                         }
1386
1387                         cell->push_back(MathAtom(p.release()));
1388                         //lyxerr << "read cell: " << cell << endl;
1389                 }
1390 #endif
1391
1392                 else if (t.cs().size()) {
1393                         latexkeys const * l = in_word_set(t.cs());
1394                         if (l) {
1395                                 if (l->inset == "big") {
1396                                         skipSpaces();
1397                                         docstring const delim = getToken().asInput();
1398                                         if (InsetMathBig::isBigInsetDelim(delim))
1399                                                 cell->push_back(MathAtom(
1400                                                         new InsetMathBig(t.cs(), delim)));
1401                                         else {
1402                                                 cell->push_back(createInsetMath(t.cs()));
1403                                                 putback();
1404                                         }
1405                                 }
1406
1407                                 else if (l->inset == "font") {
1408                                         cell->push_back(createInsetMath(t.cs()));
1409                                         parse(cell->back().nucleus()->cell(0),
1410                                                 FLAG_ITEM, asMode(mode, l->extra));
1411                                 }
1412
1413                                 else if (l->inset == "oldfont") {
1414                                         cell->push_back(createInsetMath(t.cs()));
1415                                         parse(cell->back().nucleus()->cell(0),
1416                                                 flags | FLAG_ALIGN, asMode(mode, l->extra));
1417                                         if (prevToken().cat() != catAlign &&
1418                                             prevToken().cs() != "\\")
1419                                                 return;
1420                                         putback();
1421                                 }
1422
1423                                 else if (l->inset == "style") {
1424                                         cell->push_back(createInsetMath(t.cs()));
1425                                         parse(cell->back().nucleus()->cell(0),
1426                                                 flags | FLAG_ALIGN, mode);
1427                                         if (prevToken().cat() != catAlign &&
1428                                             prevToken().cs() != "\\")
1429                                                 return;
1430                                         putback();
1431                                 }
1432
1433                                 else {
1434                                         MathAtom at = createInsetMath(t.cs());
1435                                         for (InsetMath::idx_type i = 0; i < at->nargs(); ++i)
1436                                                 parse(at.nucleus()->cell(i),
1437                                                         FLAG_ITEM, asMode(mode, l->extra));
1438                                         cell->push_back(at);
1439                                 }
1440                         }
1441
1442                         else {
1443                                 MathAtom at = createInsetMath(t.cs());
1444                                 InsetMath::mode_type m = mode;
1445                                 //if (m == InsetMath::UNDECIDED_MODE)
1446                                 //lyxerr << "default creation: m1: " << m << endl;
1447                                 if (at->currentMode() != InsetMath::UNDECIDED_MODE)
1448                                         m = at->currentMode();
1449                                 //lyxerr << "default creation: m2: " << m << endl;
1450                                 InsetMath::idx_type start = 0;
1451                                 // this fails on \bigg[...\bigg]
1452                                 //MathData opt;
1453                                 //parse(opt, FLAG_OPTION, InsetMath::VERBATIM_MODE);
1454                                 //if (opt.size()) {
1455                                 //      start = 1;
1456                                 //      at.nucleus()->cell(0) = opt;
1457                                 //}
1458                                 for (InsetMath::idx_type i = start; i < at->nargs(); ++i) {
1459                                         parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1460                                         skipSpaces();
1461                                 }
1462                                 cell->push_back(at);
1463                         }
1464                 }
1465
1466
1467                 if (flags & FLAG_LEAVE) {
1468                         flags &= ~FLAG_LEAVE;
1469                         break;
1470                 }
1471         }
1472 }
1473
1474
1475
1476 } // anonymous namespace
1477
1478
1479 void mathed_parse_cell(MathData & ar, docstring const & str)
1480 {
1481         Parser(str).parse(ar, 0, InsetMath::MATH_MODE);
1482 }
1483
1484
1485 void mathed_parse_cell(MathData & ar, istream & is)
1486 {
1487         Parser(is).parse(ar, 0, InsetMath::MATH_MODE);
1488 }
1489
1490
1491 bool mathed_parse_normal(MathAtom & t, docstring const & str)
1492 {
1493         return Parser(str).parse(t);
1494 }
1495
1496
1497 bool mathed_parse_normal(MathAtom & t, Lexer & lex)
1498 {
1499         return Parser(lex).parse(t);
1500 }
1501
1502
1503 void mathed_parse_normal(InsetMathGrid & grid, docstring const & str)
1504 {
1505         Parser(str).parse1(grid, 0, InsetMath::MATH_MODE, false);
1506 }
1507
1508
1509 void initParser()
1510 {
1511         fill(theCatcode, theCatcode + 128, catOther);
1512         fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1513         fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1514
1515         theCatcode[int('\\')] = catEscape;
1516         theCatcode[int('{')]  = catBegin;
1517         theCatcode[int('}')]  = catEnd;
1518         theCatcode[int('$')]  = catMath;
1519         theCatcode[int('&')]  = catAlign;
1520         theCatcode[int('\n')] = catNewline;
1521         theCatcode[int('#')]  = catParameter;
1522         theCatcode[int('^')]  = catSuper;
1523         theCatcode[int('_')]  = catSub;
1524         theCatcode[int(0x7f)] = catIgnore;
1525         theCatcode[int(' ')]  = catSpace;
1526         theCatcode[int('\t')] = catSpace;
1527         theCatcode[int('\r')] = catNewline;
1528         theCatcode[int('~')]  = catActive;
1529         theCatcode[int('%')]  = catComment;
1530 }
1531
1532
1533 } // namespace lyx