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