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