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