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