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