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