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