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