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