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