]> git.lyx.org Git - lyx.git/blob - src/mathed/math_parser.C
Make Helge happy: no more crash on arrow up/down in math macro
[lyx.git] / src / mathed / math_parser.C
1 /**
2  * \file math_parser.C
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
30   \[\begin{array}{ccc}
31 1
32 &
33
34   \end{array}\]
35
36 */
37
38
39 #include <config.h>
40
41 #include "math_parser.h"
42 #include "math_arrayinset.h"
43 #include "math_braceinset.h"
44 #include "math_charinset.h"
45 #include "math_commentinset.h"
46 #include "math_deliminset.h"
47 #include "math_envinset.h"
48 #include "math_factory.h"
49 #include "math_kerninset.h"
50 #include "math_macro.h"
51 #include "math_macroarg.h"
52 #include "math_macrotemplate.h"
53 #include "math_parinset.h"
54 #include "math_rootinset.h"
55 #include "math_scriptinset.h"
56 #include "math_sqrtinset.h"
57 #include "math_support.h"
58 #include "math_tabularinset.h"
59
60 //#include "insets/insetref.h"
61 #include "ref_inset.h"
62
63 #include "lyxlex.h"
64 #include "debug.h"
65
66 #include "support/convert.h"
67
68 #include <sstream>
69
70 using std::endl;
71 using std::fill;
72
73 using std::string;
74 using std::ios;
75 using std::istream;
76 using std::istringstream;
77 using std::ostream;
78 using std::vector;
79
80
81 //#define FILEDEBUG
82
83
84 namespace {
85
86 MathInset::mode_type asMode(MathInset::mode_type oldmode, string const & str)
87 {
88         //lyxerr << "handling mode: '" << str << "'" << endl;
89         if (str == "mathmode")
90                 return MathInset::MATH_MODE;
91         if (str == "textmode" || str == "forcetext")
92                 return MathInset::TEXT_MODE;
93         return oldmode;
94 }
95
96
97 bool stared(string const & s)
98 {
99         string::size_type const n = s.size();
100         return n && s[n - 1] == '*';
101 }
102
103
104 /*!
105  * Add the row \p cellrow to \p grid.
106  * \returns wether the row could be added. Adding a row can fail for
107  * environments like "equation" that have a fixed number of rows.
108  */
109 bool addRow(MathGridInset & grid, MathGridInset::row_type & cellrow,
110             string const & vskip)
111 {
112         ++cellrow;
113         if (cellrow == grid.nrows()) {
114                 //lyxerr << "adding row " << cellrow << endl;
115                 grid.addRow(cellrow - 1);
116                 if (cellrow == grid.nrows()) {
117                         // We can't add a row to this grid, so let's
118                         // append the content of this cell to the previous
119                         // one.
120                         // This does not happen in well formed .lyx files,
121                         // but LyX versions 1.3.x and older could create
122                         // such files and tex2lyx can still do that.
123                         --cellrow;
124                         lyxerr << "ignoring extra row";
125                         if (!vskip.empty())
126                                 lyxerr << " with extra space " << vskip;
127                         lyxerr << '.' << endl;
128                         return false;
129                 }
130         }
131         grid.vcrskip(LyXLength(vskip), cellrow - 1);
132         return true;
133 }
134
135
136 /*!
137  * Add the column \p cellcol to \p grid.
138  * \returns wether the column could be added. Adding a column can fail for
139  * environments like "eqnarray" that have a fixed number of columns.
140  */
141 bool addCol(MathGridInset & grid, MathGridInset::col_type & cellcol)
142 {
143         ++cellcol;
144         if (cellcol == grid.ncols()) {
145                 //lyxerr << "adding column " << cellcol << endl;
146                 grid.addCol(cellcol - 1);
147                 if (cellcol == grid.ncols()) {
148                         // We can't add a column to this grid, so let's
149                         // append the content of this cell to the previous
150                         // one.
151                         // This does not happen in well formed .lyx files,
152                         // but LyX versions 1.3.x and older could create
153                         // such files and tex2lyx can still do that.
154                         --cellcol;
155                         lyxerr << "ignoring extra column." << endl;
156                         return false;
157                 }
158         }
159         return true;
160 }
161
162
163 /*!
164  * Check wether the last row is empty and remove it if yes.
165  * Otherwise the following code
166  * \verbatim
167 \begin{array}{|c|c|}
168 \hline
169 1 & 2 \\ \hline
170 3 & 4 \\ \hline
171 \end{array}
172  * \endverbatim
173  * will result in a grid with 3 rows (+ the dummy row that is always present),
174  * because the last '\\' opens a new row.
175  */
176 void delEmptyLastRow(MathGridInset & grid)
177 {
178         MathGridInset::row_type const row = grid.nrows() - 1;
179         for (MathGridInset::col_type col = 0; col < grid.ncols(); ++col) {
180                 if (!grid.cell(grid.index(row, col)).empty())
181                         return;
182         }
183         grid.delRow(row + 1);
184 }
185
186
187 // These are TeX's catcodes
188 enum CatCode {
189         catEscape,     // 0    backslash
190         catBegin,      // 1    {
191         catEnd,        // 2    }
192         catMath,       // 3    $
193         catAlign,      // 4    &
194         catNewline,    // 5    ^^M
195         catParameter,  // 6    #
196         catSuper,      // 7    ^
197         catSub,        // 8    _
198         catIgnore,     // 9
199         catSpace,      // 10   space
200         catLetter,     // 11   a-zA-Z
201         catOther,      // 12   none of the above
202         catActive,     // 13   ~
203         catComment,    // 14   %
204         catInvalid     // 15   <delete>
205 };
206
207 CatCode theCatcode[256];
208
209
210 inline CatCode catcode(unsigned char c)
211 {
212         return theCatcode[c];
213 }
214
215
216 enum {
217         FLAG_ALIGN      = 1 << 0,  //  next & or \\ ends the parsing process
218         FLAG_BRACE_LAST = 1 << 1,  //  next closing brace ends the parsing
219         FLAG_RIGHT      = 1 << 2,  //  next \\right ends the parsing process
220         FLAG_END        = 1 << 3,  //  next \\end ends the parsing process
221         FLAG_BRACK_LAST = 1 << 4,  //  next closing bracket ends the parsing
222         FLAG_TEXTMODE   = 1 << 5,  //  we are in a box
223         FLAG_ITEM       = 1 << 6,  //  read a (possibly braced token)
224         FLAG_LEAVE      = 1 << 7,  //  leave the loop at the end
225         FLAG_SIMPLE     = 1 << 8,  //  next $ leaves the loop
226         FLAG_EQUATION   = 1 << 9,  //  next \] leaves the loop
227         FLAG_SIMPLE2    = 1 << 10, //  next \) leaves the loop
228         FLAG_OPTION     = 1 << 11, //  read [...] style option
229         FLAG_BRACED     = 1 << 12  //  read {...} style argument
230 };
231
232
233 //
234 // Helper class for parsing
235 //
236
237 class Token {
238 public:
239         ///
240         Token() : cs_(), char_(0), cat_(catIgnore) {}
241         ///
242         Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
243         ///
244         Token(string const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
245
246         ///
247         string const & cs() const { return cs_; }
248         ///
249         CatCode cat() const { return cat_; }
250         ///
251         char character() const { return char_; }
252         ///
253         string asString() const { return cs_.size() ? cs_ : string(1, char_); }
254
255 private:
256         ///
257         string cs_;
258         ///
259         char char_;
260         ///
261         CatCode cat_;
262 };
263
264 ostream & operator<<(ostream & os, Token const & t)
265 {
266         if (t.cs().size())
267                 os << '\\' << t.cs();
268         else if (t.cat() == catLetter)
269                 os << t.character();
270         else
271                 os << '[' << t.character() << ',' << t.cat() << ']';
272         return os;
273 }
274
275
276 class Parser {
277 public:
278         ///
279         typedef  MathInset::mode_type mode_type;
280
281         ///
282         Parser(LyXLex & lex);
283         ///
284         Parser(istream & is);
285
286         ///
287         bool parse(MathAtom & at);
288         ///
289         void parse(MathArray & array, unsigned flags, mode_type mode);
290         ///
291         void parse1(MathGridInset & grid, unsigned flags, mode_type mode,
292                 bool numbered);
293         ///
294         MathArray parse(unsigned flags, mode_type mode);
295         ///
296         int lineno() const { return lineno_; }
297         ///
298         void putback();
299
300 private:
301         ///
302         void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
303         /// get arg delimited by 'left' and 'right'
304         string getArg(char left, char right);
305         ///
306         char getChar();
307         ///
308         void error(string const & msg);
309         /// dump contents to screen
310         void dump() const;
311         ///
312         void tokenize(istream & is);
313         ///
314         void tokenize(string const & s);
315         ///
316         void skipSpaceTokens(istream & is, char c);
317         ///
318         void push_back(Token const & t);
319         ///
320         void pop_back();
321         ///
322         Token const & prevToken() const;
323         ///
324         Token const & nextToken() const;
325         ///
326         Token const & getToken();
327         /// skips spaces if any
328         void skipSpaces();
329         ///
330         void lex(string const & s);
331         ///
332         bool good() const;
333         ///
334         string parse_verbatim_item();
335         ///
336         string parse_verbatim_option();
337
338         ///
339         int lineno_;
340         ///
341         vector<Token> tokens_;
342         ///
343         unsigned pos_;
344         /// Stack of active environments
345         vector<string> environments_;
346 };
347
348
349 Parser::Parser(LyXLex & lexer)
350         : lineno_(lexer.getLineNo()), pos_(0)
351 {
352         tokenize(lexer.getStream());
353         lexer.eatLine();
354 }
355
356
357 Parser::Parser(istream & is)
358         : lineno_(0), pos_(0)
359 {
360         tokenize(is);
361 }
362
363
364 void Parser::push_back(Token const & t)
365 {
366         tokens_.push_back(t);
367 }
368
369
370 void Parser::pop_back()
371 {
372         tokens_.pop_back();
373 }
374
375
376 Token const & Parser::prevToken() const
377 {
378         static const Token dummy;
379         return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
380 }
381
382
383 Token const & Parser::nextToken() const
384 {
385         static const Token dummy;
386         return good() ? tokens_[pos_] : dummy;
387 }
388
389
390 Token const & Parser::getToken()
391 {
392         static const Token dummy;
393         //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
394         return good() ? tokens_[pos_++] : dummy;
395 }
396
397
398 void Parser::skipSpaces()
399 {
400         while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
401                 getToken();
402 }
403
404
405 void Parser::putback()
406 {
407         --pos_;
408 }
409
410
411 bool Parser::good() const
412 {
413         return pos_ < tokens_.size();
414 }
415
416
417 char Parser::getChar()
418 {
419         if (!good())
420                 error("The input stream is not well...");
421         return tokens_[pos_++].character();
422 }
423
424
425 string Parser::getArg(char left, char right)
426 {
427         skipSpaces();
428
429         string result;
430         char c = getChar();
431
432         if (c != left)
433                 putback();
434         else
435                 while ((c = getChar()) != right && good())
436                         result += c;
437
438         return result;
439 }
440
441
442 void Parser::skipSpaceTokens(istream & is, char c)
443 {
444         // skip trailing spaces
445         while (catcode(c) == catSpace || catcode(c) == catNewline)
446                 if (!is.get(c))
447                         break;
448         //lyxerr << "putting back: " << c << endl;
449         is.putback(c);
450 }
451
452
453 void Parser::tokenize(istream & is)
454 {
455         // eat everything up to the next \end_inset or end of stream
456         // and store it in s for further tokenization
457         string s;
458         char c;
459         while (is.get(c)) {
460                 s += c;
461                 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
462                         s = s.substr(0, s.size() - 10);
463                         break;
464                 }
465         }
466         // Remove the space after \end_inset
467         if (is.get(c) && c != ' ')
468                 is.unget();
469
470         // tokenize buffer
471         tokenize(s);
472 }
473
474
475 void Parser::tokenize(string const & buffer)
476 {
477         istringstream is(buffer, ios::in | ios::binary);
478
479         char c;
480         while (is.get(c)) {
481                 //lyxerr << "reading c: " << c << endl;
482
483                 switch (catcode(c)) {
484                         case catNewline: {
485                                 ++lineno_;
486                                 is.get(c);
487                                 if (catcode(c) == catNewline)
488                                         ; //push_back(Token("par"));
489                                 else {
490                                         push_back(Token('\n', catNewline));
491                                         is.putback(c);
492                                 }
493                                 break;
494                         }
495
496 /*
497                         case catComment: {
498                                 while (is.get(c) && catcode(c) != catNewline)
499                                         ;
500                                 ++lineno_;
501                                 break;
502                         }
503 */
504
505                         case catEscape: {
506                                 is.get(c);
507                                 if (!is) {
508                                         error("unexpected end of input");
509                                 } else {
510                                         string s(1, c);
511                                         if (catcode(c) == catLetter) {
512                                                 // collect letters
513                                                 while (is.get(c) && catcode(c) == catLetter)
514                                                         s += c;
515                                                 skipSpaceTokens(is, c);
516                                         }
517                                         push_back(Token(s));
518                                 }
519                                 break;
520                         }
521
522                         case catSuper:
523                         case catSub: {
524                                 push_back(Token(c, catcode(c)));
525                                 is.get(c);
526                                 skipSpaceTokens(is, c);
527                                 break;
528                         }
529
530                         case catIgnore: {
531                                 lyxerr << "ignoring a char: " << int(c) << endl;
532                                 break;
533                         }
534
535                         default:
536                                 push_back(Token(c, catcode(c)));
537                 }
538         }
539
540 #ifdef FILEDEBUG
541         dump();
542 #endif
543 }
544
545
546 void Parser::dump() const
547 {
548         lyxerr << "\nTokens: ";
549         for (unsigned i = 0; i < tokens_.size(); ++i) {
550                 if (i == pos_)
551                         lyxerr << " <#> ";
552                 lyxerr << tokens_[i];
553         }
554         lyxerr << " pos: " << pos_ << endl;
555 }
556
557
558 void Parser::error(string const & msg)
559 {
560         lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
561         dump();
562         //exit(1);
563 }
564
565
566 bool Parser::parse(MathAtom & at)
567 {
568         skipSpaces();
569         MathArray ar;
570         parse(ar, false, MathInset::UNDECIDED_MODE);
571         if (ar.size() != 1 || ar.front()->getType() == "none") {
572                 lyxerr << "unusual contents found: " << ar << endl;
573                 at = MathAtom(new MathParInset(ar));
574                 //if (at->nargs() > 0)
575                 //      at.nucleus()->cell(0) = ar;
576                 //else
577                 //      lyxerr << "unusual contents found: " << ar << endl;
578                 return true;
579         }
580         at = ar[0];
581         return true;
582 }
583
584
585 string Parser::parse_verbatim_option()
586 {
587         skipSpaces();
588         string res;
589         if (nextToken().character() == '[') {
590                 Token t = getToken();
591                 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
592                         if (t.cat() == catBegin) {
593                                 putback();
594                                 res += '{' + parse_verbatim_item() + '}';
595                         } else
596                                 res += t.asString();
597                 }
598         }
599         return res;
600 }
601
602
603 string Parser::parse_verbatim_item()
604 {
605         skipSpaces();
606         string res;
607         if (nextToken().cat() == catBegin) {
608                 Token t = getToken();
609                 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
610                         if (t.cat() == catBegin) {
611                                 putback();
612                                 res += '{' + parse_verbatim_item() + '}';
613                         }
614                         else
615                                 res += t.asString();
616                 }
617         }
618         return res;
619 }
620
621
622 MathArray Parser::parse(unsigned flags, mode_type mode)
623 {
624         MathArray ar;
625         parse(ar, flags, mode);
626         return ar;
627 }
628
629
630 void Parser::parse(MathArray & array, unsigned flags, mode_type mode)
631 {
632         MathGridInset grid(1, 1);
633         parse1(grid, flags, mode, false);
634         array = grid.cell(0);
635 }
636
637
638 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
639         const bool numbered)
640 {
641         parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
642 }
643
644
645 void Parser::parse1(MathGridInset & grid, unsigned flags,
646         const mode_type mode, const bool numbered)
647 {
648         int limits = 0;
649         MathGridInset::row_type cellrow = 0;
650         MathGridInset::col_type cellcol = 0;
651         MathArray * cell = &grid.cell(grid.index(cellrow, cellcol));
652
653         if (grid.asHullInset())
654                 grid.asHullInset()->numbered(cellrow, numbered);
655
656         //dump();
657         //lyxerr << " flags: " << flags << endl;
658         //lyxerr << " mode: " << mode  << endl;
659         //lyxerr << "grid: " << grid << endl;
660
661         while (good()) {
662                 Token const & t = getToken();
663
664 #ifdef FILEDEBUG
665                 lyxerr << "t: " << t << " flags: " << flags << endl;
666                 lyxerr << "mode: " << mode  << endl;
667                 cell->dump();
668                 lyxerr << endl;
669 #endif
670
671                 if (flags & FLAG_ITEM) {
672
673                         if (t.cat() == catBegin) {
674                                 // skip the brace and collect everything to the next matching
675                                 // closing brace
676                                 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
677                                 return;
678                         }
679
680                         // handle only this single token, leave the loop if done
681                         flags = FLAG_LEAVE;
682                 }
683
684
685                 if (flags & FLAG_BRACED) {
686                         if (t.cat() == catSpace)
687                                 continue;
688
689                         if (t.cat() != catBegin) {
690                                 error("opening brace expected");
691                                 return;
692                         }
693
694                         // skip the brace and collect everything to the next matching
695                         // closing brace
696                         flags = FLAG_BRACE_LAST;
697                 }
698
699
700                 if (flags & FLAG_OPTION) {
701                         if (t.cat() == catOther && t.character() == '[') {
702                                 MathArray ar;
703                                 parse(ar, FLAG_BRACK_LAST, mode);
704                                 cell->append(ar);
705                         } else {
706                                 // no option found, put back token and we are done
707                                 putback();
708                         }
709                         return;
710                 }
711
712                 //
713                 // cat codes
714                 //
715                 if (t.cat() == catMath) {
716                         if (mode != MathInset::MATH_MODE) {
717                                 // we are inside some text mode thingy, so opening new math is allowed
718                                 Token const & n = getToken();
719                                 if (n.cat() == catMath) {
720                                         // TeX's $$...$$ syntax for displayed math
721                                         cell->push_back(MathAtom(new MathHullInset("equation")));
722                                         parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
723                                         getToken(); // skip the second '$' token
724                                 } else {
725                                         // simple $...$  stuff
726                                         putback();
727                                         cell->push_back(MathAtom(new MathHullInset("simple")));
728                                         parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
729                                 }
730                         }
731
732                         else if (flags & FLAG_SIMPLE) {
733                                 // this is the end of the formula
734                                 return;
735                         }
736
737                         else {
738                                 error("something strange in the parser");
739                                 break;
740                         }
741                 }
742
743                 else if (t.cat() == catLetter)
744                         cell->push_back(MathAtom(new MathCharInset(t.character())));
745
746                 else if (t.cat() == catSpace && mode != MathInset::MATH_MODE) {
747                         if (cell->empty() || cell->back()->getChar() != ' ')
748                                 cell->push_back(MathAtom(new MathCharInset(t.character())));
749                 }
750
751                 else if (t.cat() == catNewline && mode != MathInset::MATH_MODE) {
752                         if (cell->empty() || cell->back()->getChar() != ' ')
753                                 cell->push_back(MathAtom(new MathCharInset(' ')));
754                 }
755
756                 else if (t.cat() == catParameter) {
757                         Token const & n = getToken();
758                         cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
759                 }
760
761                 else if (t.cat() == catActive)
762                         cell->push_back(MathAtom(new MathCharInset(t.character())));
763
764                 else if (t.cat() == catBegin) {
765                         MathArray ar;
766                         parse(ar, FLAG_BRACE_LAST, mode);
767                         // do not create a BraceInset if they were written by LyX
768                         // this helps to keep the annoyance of  "a choose b"  to a minimum
769                         if (ar.size() == 1 && ar[0]->extraBraces())
770                                 cell->append(ar);
771                         else
772                                 cell->push_back(MathAtom(new MathBraceInset(ar)));
773                 }
774
775                 else if (t.cat() == catEnd) {
776                         if (flags & FLAG_BRACE_LAST)
777                                 return;
778                         error("found '}' unexpectedly");
779                         //BOOST_ASSERT(false);
780                         //add(cell, '}', LM_TC_TEX);
781                 }
782
783                 else if (t.cat() == catAlign) {
784                         //lyxerr << " column now " << (cellcol + 1)
785                         //       << " max: " << grid.ncols() << endl;
786                         if (flags & FLAG_ALIGN)
787                                 return;
788                         if (addCol(grid, cellcol))
789                                 cell = &grid.cell(grid.index(cellrow, cellcol));
790                 }
791
792                 else if (t.cat() == catSuper || t.cat() == catSub) {
793                         bool up = (t.cat() == catSuper);
794                         // we need no new script inset if the last thing was a scriptinset,
795                         // which has that script already not the same script already
796                         if (!cell->size())
797                                 cell->push_back(MathAtom(new MathScriptInset(up)));
798                         else if (cell->back()->asScriptInset() &&
799                                         !cell->back()->asScriptInset()->has(up))
800                                 cell->back().nucleus()->asScriptInset()->ensure(up);
801                         else if (cell->back()->asScriptInset())
802                                 cell->push_back(MathAtom(new MathScriptInset(up)));
803                         else
804                                 cell->back() = MathAtom(new MathScriptInset(cell->back(), up));
805                         MathScriptInset * p = cell->back().nucleus()->asScriptInset();
806                         // special handling of {}-bases
807                         // is this always correct?
808                         // It appears that this is wrong (Dekel)
809                         //if (p->nuc().size() == 1 && p->nuc().back()->asNestInset() &&
810                         //    p->nuc().back()->extraBraces())
811                         //      p->nuc() = p->nuc().back()->asNestInset()->cell(0);
812                         parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
813                         if (limits) {
814                                 p->limits(limits);
815                                 limits = 0;
816                         }
817                 }
818
819                 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
820                         //lyxerr << "finished reading option" << endl;
821                         return;
822                 }
823
824                 else if (t.cat() == catOther)
825                         cell->push_back(MathAtom(new MathCharInset(t.character())));
826
827                 else if (t.cat() == catComment) {
828                         string s;
829                         while (good()) {
830                                 Token const & t = getToken();
831                                 if (t.cat() == catNewline)
832                                         break;
833                                 s += t.asString();
834                         }
835                         cell->push_back(MathAtom(new MathCommentInset(s)));
836                         skipSpaces();
837                 }
838
839                 //
840                 // control sequences
841                 //
842
843                 else if (t.cs() == "lyxlock") {
844                         if (cell->size())
845                                 cell->back().nucleus()->lock(true);
846                 }
847
848                 else if (t.cs() == "def" ||
849                         t.cs() == "newcommand" ||
850                         t.cs() == "renewcommand")
851                 {
852                         string const type = t.cs();
853                         string name;
854                         int nargs = 0;
855                         if (t.cs() == "def") {
856                                 // get name
857                                 name = getToken().cs();
858
859                                 // read parameter
860                                 string pars;
861                                 while (good() && nextToken().cat() != catBegin) {
862                                         pars += getToken().cs();
863                                         ++nargs;
864                                 }
865                                 nargs /= 2;
866                                 //lyxerr << "read \\def parameter list '" << pars << "'" << endl;
867
868                         } else { // t.cs() == "newcommand" || t.cs() == "renewcommand"
869
870                                 if (getToken().cat() != catBegin) {
871                                         error("'{' in \\newcommand expected (1) ");
872                                         return;
873                                 }
874
875                                 name = getToken().cs();
876
877                                 if (getToken().cat() != catEnd) {
878                                         error("'}' in \\newcommand expected");
879                                         return;
880                                 }
881
882                                 string const arg  = getArg('[', ']');
883                                 if (!arg.empty())
884                                         nargs = convert<int>(arg);
885
886                         }
887
888                         MathArray ar1;
889                         parse(ar1, FLAG_ITEM, MathInset::UNDECIDED_MODE);
890
891                         // we cannot handle recursive stuff at all
892                         //MathArray test;
893                         //test.push_back(createMathInset(name));
894                         //if (ar1.contains(test)) {
895                         //      error("we cannot handle recursive macros at all.");
896                         //      return;
897                         //}
898
899                         // is a version for display attached?
900                         skipSpaces();
901                         MathArray ar2;
902                         if (nextToken().cat() == catBegin)
903                                 parse(ar2, FLAG_ITEM, MathInset::MATH_MODE);
904
905                         cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, type,
906                                 ar1, ar2)));
907                 }
908
909                 else if (t.cs() == "(") {
910                         cell->push_back(MathAtom(new MathHullInset("simple")));
911                         parse2(cell->back(), FLAG_SIMPLE2, MathInset::MATH_MODE, false);
912                 }
913
914                 else if (t.cs() == "[") {
915                         cell->push_back(MathAtom(new MathHullInset("equation")));
916                         parse2(cell->back(), FLAG_EQUATION, MathInset::MATH_MODE, false);
917                 }
918
919                 else if (t.cs() == "protect")
920                         // ignore \\protect, will hopefully be re-added during output
921                         ;
922
923                 else if (t.cs() == "end") {
924                         if (flags & FLAG_END) {
925                                 // eat environment name
926                                 string const name = getArg('{', '}');
927                                 if (environments_.empty())
928                                         error("'found \\end{" + name +
929                                               "}' without matching '\\begin{" +
930                                               name + "}'");
931                                 else if (name != environments_.back())
932                                         error("'\\end{" + name +
933                                               "}' does not match '\\begin{" +
934                                               environments_.back() + "}'");
935                                 else {
936                                         environments_.pop_back();
937                                         if (name == "array" ||
938                                             name == "subarray")
939                                                 delEmptyLastRow(grid);
940                                         return;
941                                 }
942                         } else
943                                 error("found 'end' unexpectedly");
944                 }
945
946                 else if (t.cs() == ")") {
947                         if (flags & FLAG_SIMPLE2)
948                                 return;
949                         error("found '\\)' unexpectedly");
950                 }
951
952                 else if (t.cs() == "]") {
953                         if (flags & FLAG_EQUATION)
954                                 return;
955                         error("found '\\]' unexpectedly");
956                 }
957
958                 else if (t.cs() == "\\") {
959                         if (flags & FLAG_ALIGN)
960                                 return;
961                         if (addRow(grid, cellrow, getArg('[', ']'))) {
962                                 cellcol = 0;
963                                 if (grid.asHullInset())
964                                         grid.asHullInset()->numbered(
965                                                         cellrow, numbered);
966                                 cell = &grid.cell(grid.index(cellrow,
967                                                              cellcol));
968                         }
969                 }
970
971 #if 0
972                 else if (t.cs() == "multicolumn") {
973                         // extract column count and insert dummy cells
974                         MathArray count;
975                         parse(count, FLAG_ITEM, mode);
976                         int cols = 1;
977                         if (!extractNumber(count, cols)) {
978                                 lyxerr << " can't extract number of cells from " << count << endl;
979                         }
980                         // resize the table if necessary
981                         for (int i = 0; i < cols; ++i) {
982                                 if (addCol(grid, cellcol)) {
983                                         cell = &grid.cell(grid.index(
984                                                         cellrow, cellcol));
985                                         // mark this as dummy
986                                         grid.cellinfo(grid.index(
987                                                 cellrow, cellcol)).dummy_ = true;
988                                 }
989                         }
990                         // the last cell is the real thing, not a dummy
991                         grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
992
993                         // read special alignment
994                         MathArray align;
995                         parse(align, FLAG_ITEM, mode);
996                         //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
997
998                         // parse the remaining contents into the "real" cell
999                         parse(*cell, FLAG_ITEM, mode);
1000                 }
1001 #endif
1002
1003                 else if (t.cs() == "limits")
1004                         limits = 1;
1005
1006                 else if (t.cs() == "nolimits")
1007                         limits = -1;
1008
1009                 else if (t.cs() == "nonumber") {
1010                         if (grid.asHullInset())
1011                                 grid.asHullInset()->numbered(cellrow, false);
1012                 }
1013
1014                 else if (t.cs() == "number") {
1015                         if (grid.asHullInset())
1016                                 grid.asHullInset()->numbered(cellrow, true);
1017                 }
1018
1019                 else if (t.cs() == "hline") {
1020                         grid.rowinfo(cellrow).lines_ ++;
1021                 }
1022
1023                 else if (t.cs() == "sqrt") {
1024                         MathArray ar;
1025                         parse(ar, FLAG_OPTION, mode);
1026                         if (ar.size()) {
1027                                 cell->push_back(MathAtom(new MathRootInset));
1028                                 cell->back().nucleus()->cell(0) = ar;
1029                                 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1030                         } else {
1031                                 cell->push_back(MathAtom(new MathSqrtInset));
1032                                 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1033                         }
1034                 }
1035
1036                 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1037                         cell->push_back(createMathInset(t.cs()));
1038                         parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1039                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1040                 }
1041
1042                 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
1043                                 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1044                         cell->push_back(MathAtom(new RefInset(t.cs())));
1045                         parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1046                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1047                 }
1048
1049                 else if (t.cs() == "left") {
1050                         skipSpaces();
1051                         Token const & tl = getToken();
1052                         // \| and \Vert are equivalent, and MathDelimInset
1053                         // can't handle \|
1054                         // FIXME: fix this in MathDelimInset itself!
1055                         string const l = tl.cs() == "|" ? "Vert" : tl.asString();
1056                         MathArray ar;
1057                         parse(ar, FLAG_RIGHT, mode);
1058                         skipSpaces();
1059                         Token const & tr = getToken();
1060                         string const r = tr.cs() == "|" ? "Vert" : tr.asString();
1061                         cell->push_back(MathAtom(new MathDelimInset(l, r, ar)));
1062                 }
1063
1064                 else if (t.cs() == "right") {
1065                         if (flags & FLAG_RIGHT)
1066                                 return;
1067                         //lyxerr << "got so far: '" << cell << "'" << endl;
1068                         error("Unmatched right delimiter");
1069                         return;
1070                 }
1071
1072                 else if (t.cs() == "begin") {
1073                         string const name = getArg('{', '}');
1074                         environments_.push_back(name);
1075
1076                         if (name == "array" || name == "subarray") {
1077                                 string const valign = parse_verbatim_option() + 'c';
1078                                 string const halign = parse_verbatim_item();
1079                                 cell->push_back(MathAtom(new MathArrayInset(name, valign[0], halign)));
1080                                 parse2(cell->back(), FLAG_END, mode, false);
1081                         }
1082
1083                         else if (name == "tabular") {
1084                                 string const valign = parse_verbatim_option() + 'c';
1085                                 string const halign = parse_verbatim_item();
1086                                 cell->push_back(MathAtom(new MathTabularInset(name, valign[0], halign)));
1087                                 parse2(cell->back(), FLAG_END, MathInset::TEXT_MODE, false);
1088                         }
1089
1090                         else if (name == "split" || name == "cases" ||
1091                                  name == "gathered" || name == "aligned" ||
1092                                  name == "alignedat") {
1093                                 cell->push_back(createMathInset(name));
1094                                 parse2(cell->back(), FLAG_END, mode, false);
1095                         }
1096
1097                         else if (name == "math") {
1098                                 cell->push_back(MathAtom(new MathHullInset("simple")));
1099                                 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, true);
1100                         }
1101
1102                         else if (name == "equation" || name == "equation*"
1103                                         || name == "displaymath") {
1104                                 cell->push_back(MathAtom(new MathHullInset("equation")));
1105                                 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, (name == "equation"));
1106                         }
1107
1108                         else if (name == "eqnarray" || name == "eqnarray*") {
1109                                 cell->push_back(MathAtom(new MathHullInset("eqnarray")));
1110                                 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1111                         }
1112
1113                         else if (name == "align" || name == "align*") {
1114                                 cell->push_back(MathAtom(new MathHullInset("align")));
1115                                 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1116                         }
1117
1118                         else if (name == "flalign" || name == "flalign*") {
1119                                 cell->push_back(MathAtom(new MathHullInset("flalign")));
1120                                 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1121                         }
1122
1123                         else if (name == "alignat" || name == "alignat*") {
1124                                 // ignore this for a while
1125                                 getArg('{', '}');
1126                                 cell->push_back(MathAtom(new MathHullInset("alignat")));
1127                                 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1128                         }
1129
1130                         else if (name == "xalignat" || name == "xalignat*") {
1131                                 // ignore this for a while
1132                                 getArg('{', '}');
1133                                 cell->push_back(MathAtom(new MathHullInset("xalignat")));
1134                                 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1135                         }
1136
1137                         else if (name == "xxalignat") {
1138                                 // ignore this for a while
1139                                 getArg('{', '}');
1140                                 cell->push_back(MathAtom(new MathHullInset("xxalignat")));
1141                                 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1142                         }
1143
1144                         else if (name == "multline" || name == "multline*") {
1145                                 cell->push_back(MathAtom(new MathHullInset("multline")));
1146                                 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1147                         }
1148
1149                         else if (name == "gather" || name == "gather*") {
1150                                 cell->push_back(MathAtom(new MathHullInset("gather")));
1151                                 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1152                         }
1153
1154                         else if (latexkeys const * l = in_word_set(name)) {
1155                                 if (l->inset == "matrix") {
1156                                         cell->push_back(createMathInset(name));
1157                                         parse2(cell->back(), FLAG_END, mode, false);
1158                                 }
1159                         }
1160
1161                         else {
1162                                 dump();
1163                                 lyxerr << "found unknown math environment '" << name << "'" << endl;
1164                                 // create generic environment inset
1165                                 cell->push_back(MathAtom(new MathEnvInset(name)));
1166                                 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1167                         }
1168                 }
1169
1170                 else if (t.cs() == "kern") {
1171 #ifdef WITH_WARNINGS
1172 #warning A hack...
1173 #endif
1174                         string s;
1175                         while (true) {
1176                                 Token const & t = getToken();
1177                                 if (!good()) {
1178                                         putback();
1179                                         break;
1180                                 }
1181                                 s += t.character();
1182                                 if (isValidLength(s))
1183                                         break;
1184                         }
1185                         cell->push_back(MathAtom(new MathKernInset(s)));
1186                 }
1187
1188                 else if (t.cs() == "label") {
1189                         string label = parse_verbatim_item();
1190                         MathArray ar;
1191                         asArray(label, ar);
1192                         if (grid.asHullInset()) {
1193                                 grid.asHullInset()->label(cellrow, label);
1194                         } else {
1195                                 cell->push_back(createMathInset(t.cs()));
1196                                 cell->push_back(MathAtom(new MathBraceInset(ar)));
1197                         }
1198                 }
1199
1200                 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1201                         MathAtom at = createMathInset(t.cs());
1202                         at.nucleus()->cell(0) = *cell;
1203                         cell->clear();
1204                         parse(at.nucleus()->cell(1), flags, mode);
1205                         cell->push_back(at);
1206                         return;
1207                 }
1208
1209                 else if (t.cs() == "color") {
1210                         MathAtom at = createMathInset(t.cs());
1211                         parse(at.nucleus()->cell(0), FLAG_ITEM, MathInset::TEXT_MODE);
1212                         parse(at.nucleus()->cell(1), flags, mode);
1213                         cell->push_back(at);
1214                         return;
1215                 }
1216
1217                 else if (t.cs() == "substack") {
1218                         cell->push_back(createMathInset(t.cs()));
1219                         parse2(cell->back(), FLAG_ITEM, mode, false);
1220                 }
1221
1222                 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1223                         cell->push_back(createMathInset(t.cs()));
1224                         parse(cell->back().nucleus()->cell(0), FLAG_OPTION, MathInset::TEXT_MODE);
1225                         parse(cell->back().nucleus()->cell(1), FLAG_OPTION, MathInset::TEXT_MODE);
1226                         parse(cell->back().nucleus()->cell(2), FLAG_ITEM, MathInset::TEXT_MODE);
1227                 }
1228
1229 #if 0
1230                 else if (t.cs() == "infer") {
1231                         MathArray ar;
1232                         parse(ar, FLAG_OPTION, mode);
1233                         cell->push_back(createMathInset(t.cs()));
1234                         parse2(cell->back(), FLAG_ITEM, mode, false);
1235                 }
1236
1237                 // Disabled
1238                 else if (1 && t.cs() == "ar") {
1239                         auto_ptr<MathXYArrowInset> p(new MathXYArrowInset);
1240                         // try to read target
1241                         parse(p->cell(0), FLAG_OTPTION, mode);
1242                         // try to read label
1243                         if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1244                                 p->up_ = nextToken().cat() == catSuper;
1245                                 getToken();
1246                                 parse(p->cell(1), FLAG_ITEM, mode);
1247                                 //lyxerr << "read label: " << p->cell(1) << endl;
1248                         }
1249
1250                         cell->push_back(MathAtom(p.release()));
1251                         //lyxerr << "read cell: " << cell << endl;
1252                 }
1253 #endif
1254
1255                 else if (t.cs().size()) {
1256                         latexkeys const * l = in_word_set(t.cs());
1257                         if (l) {
1258                                 if (l->inset == "font") {
1259                                         cell->push_back(createMathInset(t.cs()));
1260                                         parse(cell->back().nucleus()->cell(0),
1261                                                 FLAG_ITEM, asMode(mode, l->extra));
1262                                 }
1263
1264                                 else if (l->inset == "oldfont") {
1265                                         cell->push_back(createMathInset(t.cs()));
1266                                         parse(cell->back().nucleus()->cell(0),
1267                                                 flags | FLAG_ALIGN, asMode(mode, l->extra));
1268                                         if (prevToken().cat() != catAlign &&
1269                                             prevToken().cs() != "\\")
1270                                                 return;
1271                                         putback();
1272                                 }
1273
1274                                 else if (l->inset == "style") {
1275                                         cell->push_back(createMathInset(t.cs()));
1276                                         parse(cell->back().nucleus()->cell(0),
1277                                                 flags | FLAG_ALIGN, mode);
1278                                         if (prevToken().cat() != catAlign &&
1279                                             prevToken().cs() != "\\")
1280                                                 return;
1281                                         putback();
1282                                 }
1283
1284                                 else {
1285                                         MathAtom at = createMathInset(t.cs());
1286                                         for (MathInset::idx_type i = 0; i < at->nargs(); ++i)
1287                                                 parse(at.nucleus()->cell(i),
1288                                                         FLAG_ITEM, asMode(mode, l->extra));
1289                                         cell->push_back(at);
1290                                 }
1291                         }
1292
1293                         else {
1294                                 MathAtom at = createMathInset(t.cs());
1295                                 MathInset::mode_type m = mode;
1296                                 //if (m == MathInset::UNDECIDED_MODE)
1297                                 //lyxerr << "default creation: m1: " << m << endl;
1298                                 if (at->currentMode() != MathInset::UNDECIDED_MODE)
1299                                         m = at->currentMode();
1300                                 //lyxerr << "default creation: m2: " << m << endl;
1301                                 MathInset::idx_type start = 0;
1302                                 // this fails on \bigg[...\bigg]
1303                                 //MathArray opt;
1304                                 //parse(opt, FLAG_OPTION, MathInset::VERBATIM_MODE);
1305                                 //if (opt.size()) {
1306                                 //      start = 1;
1307                                 //      at.nucleus()->cell(0) = opt;
1308                                 //}
1309                                 for (MathInset::idx_type i = start; i < at->nargs(); ++i) {
1310                                         parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1311                                         skipSpaces();
1312                                 }
1313                                 cell->push_back(at);
1314                         }
1315                 }
1316
1317
1318                 if (flags & FLAG_LEAVE) {
1319                         flags &= ~FLAG_LEAVE;
1320                         break;
1321                 }
1322         }
1323 }
1324
1325
1326
1327 } // anonymous namespace
1328
1329
1330 void mathed_parse_cell(MathArray & ar, string const & str)
1331 {
1332         istringstream is(str);
1333         mathed_parse_cell(ar, is);
1334 }
1335
1336
1337 void mathed_parse_cell(MathArray & ar, istream & is)
1338 {
1339         Parser(is).parse(ar, 0, MathInset::MATH_MODE);
1340 }
1341
1342
1343 bool mathed_parse_normal(MathAtom & t, string const & str)
1344 {
1345         istringstream is(str);
1346         return Parser(is).parse(t);
1347 }
1348
1349
1350 bool mathed_parse_normal(MathAtom & t, istream & is)
1351 {
1352         return Parser(is).parse(t);
1353 }
1354
1355
1356 bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
1357 {
1358         return Parser(lex).parse(t);
1359 }
1360
1361
1362 void mathed_parse_normal(MathGridInset & grid, string const & str)
1363 {
1364         istringstream is(str);
1365         Parser(is).parse1(grid, 0, MathInset::MATH_MODE, false);
1366 }
1367
1368
1369 void initParser()
1370 {
1371         fill(theCatcode, theCatcode + 256, catOther);
1372         fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1373         fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1374
1375         theCatcode[int('\\')] = catEscape;
1376         theCatcode[int('{')]  = catBegin;
1377         theCatcode[int('}')]  = catEnd;
1378         theCatcode[int('$')]  = catMath;
1379         theCatcode[int('&')]  = catAlign;
1380         theCatcode[int('\n')] = catNewline;
1381         theCatcode[int('#')]  = catParameter;
1382         theCatcode[int('^')]  = catSuper;
1383         theCatcode[int('_')]  = catSub;
1384         theCatcode[int(0x7f)] = catIgnore;
1385         theCatcode[int(' ')]  = catSpace;
1386         theCatcode[int('\t')] = catSpace;
1387         theCatcode[int('\r')] = catNewline;
1388         theCatcode[int('~')]  = catActive;
1389         theCatcode[int('%')]  = catComment;
1390 }