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