]> git.lyx.org Git - lyx.git/blob - src/mathed/math_parser.C
First shot at "auto correction" feature in mathed
[lyx.git] / src / mathed / math_parser.C
1 /*
2  *  File:        math_parser.C
3  *  Purpose:     Parser for mathed
4  *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5  *  Created:     January 1996
6  *  Description: Parse LaTeX2e math mode code.
7  *
8  *  Dependencies: Xlib, XForms
9  *
10  *  Copyright: 1996, Alejandro Aguilar Sierra
11  *
12  *   Version: 0.8beta.
13  *
14  *   You are free to use and modify this code under the terms of
15  *   the GNU General Public Licence version 2 or later.
16  */
17
18 /*
19
20 If someone desperately needs partial "structures" (such as a few cells of
21 an array inset or similar) (s)he could uses the following hack as starting
22 point to write some macros:
23
24   \newif\ifcomment
25   \commentfalse
26   \ifcomment
27           \def\makeamptab{\catcode`\&=4\relax}
28           \def\makeampletter{\catcode`\&=11\relax}
29     \def\b{\makeampletter\expandafter\makeamptab\bi}
30     \long\def\bi#1\e{}
31   \else
32     \def\b{}\def\e{}
33   \fi
34
35   ...
36
37   \[\begin{array}{ccc}
38    1 & 2\b & 3^2\\
39    4 & 5\e & 6\\
40    7 & 8 & 9
41   \end{array}\]
42
43 */
44
45
46 #include <config.h>
47
48 #ifdef __GNUG__
49 #pragma implementation
50 #endif
51
52 #include "math_parser.h"
53 #include "math_inset.h"
54 #include "math_arrayinset.h"
55 #include "math_braceinset.h"
56 #include "math_boxinset.h"
57 #include "math_charinset.h"
58 #include "math_deliminset.h"
59 #include "math_factory.h"
60 #include "math_funcinset.h"
61 #include "math_kerninset.h"
62 #include "math_macro.h"
63 #include "math_macrotable.h"
64 #include "math_macrotemplate.h"
65 #include "math_hullinset.h"
66 #include "math_rootinset.h"
67 #include "math_sizeinset.h"
68 #include "math_sqrtinset.h"
69 #include "math_scriptinset.h"
70 #include "math_specialcharinset.h"
71 #include "math_sqrtinset.h"
72 #include "math_support.h"
73 #include "math_xyarrowinset.h"
74
75 #include "lyxlex.h"
76 #include "debug.h"
77 #include "support/LAssert.h"
78 #include "support/lstrings.h"
79
80 #include <cctype>
81 #include <stack>
82 #include <algorithm>
83
84 using std::istream;
85 using std::ostream;
86 using std::ios;
87 using std::endl;
88 using std::stack;
89 using std::fill;
90 using std::vector;
91
92 //#define FILEDEBUG
93
94
95 namespace {
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 void add(MathArray & ar, char c, MathTextCodes code)
105 {
106         ar.push_back(MathAtom(new MathCharInset(c, code)));
107 }
108
109
110 // These are TeX's catcodes
111 enum CatCode {
112         catEscape,     // 0    backslash
113         catBegin,      // 1    {
114         catEnd,        // 2    }
115         catMath,       // 3    $
116         catAlign,      // 4    &
117         catNewline,    // 5    ^^M
118         catParameter,  // 6    #
119         catSuper,      // 7    ^
120         catSub,        // 8    _
121         catIgnore,     // 9
122         catSpace,      // 10   space
123         catLetter,     // 11   a-zA-Z
124         catOther,      // 12   none of the above
125         catActive,     // 13   ~
126         catComment,    // 14   %
127         catInvalid     // 15   <delete>
128 };
129
130 CatCode theCatcode[256];
131
132
133 inline CatCode catcode(unsigned char c)
134 {
135         return theCatcode[c];
136 }
137
138
139 enum {
140         FLAG_BRACE_LAST = 1 << 1,  //  last closing brace ends the parsing process
141         FLAG_RIGHT      = 1 << 2,  //  next \\right ends the parsing process
142         FLAG_END        = 1 << 3,  //  next \\end ends the parsing process
143         FLAG_BRACK_END  = 1 << 4,  //  next closing bracket ends the parsing process
144         FLAG_BOX        = 1 << 5,  //  we are in a box
145         FLAG_ITEM       = 1 << 6,  //  read a (possibly braced token)
146         FLAG_BLOCK      = 1 << 7,  //  next block ends the parsing process
147         FLAG_BLOCK2     = 1 << 8,  //  next block2 ends the parsing process
148         FLAG_LEAVE      = 1 << 9   //  leave the loop at the end
149 };
150
151
152 void catInit()
153 {
154         fill(theCatcode, theCatcode + 256, catOther);
155         fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
156         fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
157
158         theCatcode['\\'] = catEscape;
159         theCatcode['{']  = catBegin;
160         theCatcode['}']  = catEnd;
161         theCatcode['$']  = catMath;
162         theCatcode['&']  = catAlign;
163         theCatcode['\n'] = catNewline;
164         theCatcode['#']  = catParameter;
165         theCatcode['^']  = catSuper;
166         theCatcode['_']  = catSub;
167         theCatcode['\7f'] = catIgnore;
168         theCatcode[' ']  = catSpace;
169         theCatcode['\t'] = catSpace;
170         theCatcode['\r'] = catSpace;
171         theCatcode['~']  = catActive;
172         theCatcode['%']  = catComment;
173 }
174
175
176
177 //
178 // Helper class for parsing
179 //
180
181 class Token {
182 public:
183         ///
184         Token() : cs_(), char_(0), cat_(catIgnore) {}
185         ///
186         Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
187         ///
188         Token(string const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
189
190         ///
191         string const & cs() const { return cs_; }
192         ///
193         CatCode cat() const { return cat_; }
194         ///
195         char character() const { return char_; }
196         ///
197         string asString() const;
198         ///
199         bool isCR() const;
200
201 private:
202         ///
203         string cs_;
204         ///
205         char char_;
206         ///
207         CatCode cat_;
208 };
209
210 bool Token::isCR() const
211 {
212         return cs_ == "\\" || cs_ == "cr" || cs_ == "crcr";
213 }
214
215 string Token::asString() const
216 {
217         return cs_.size() ? cs_ : string(1, char_);
218 }
219
220 // Angus' compiler says these are not needed
221 //bool operator==(Token const & s, Token const & t)
222 //{
223 //      return s.character() == t.character()
224 //              && s.cat() == t.cat() && s.cs() == t.cs();
225 //}
226 //
227 //bool operator!=(Token const & s, Token const & t)
228 //{
229 //      return !(s == t);
230 //}
231
232 ostream & operator<<(ostream & os, Token const & t)
233 {
234         if (t.cs().size())
235                 os << "\\" << t.cs();
236         else
237                 os << "[" << t.character() << "," << t.cat() << "]";
238         return os;
239 }
240
241
242 class Parser {
243
244 public:
245         ///
246         Parser(LyXLex & lex);
247         ///
248         Parser(istream & is);
249
250         ///
251         bool parse_macro(string & name);
252         ///
253         bool parse_normal(MathAtom &);
254         ///
255         void parse_into(MathArray & array, unsigned flags, MathTextCodes = LM_TC_MIN);
256         ///
257         int lineno() const { return lineno_; }
258         ///
259         void putback();
260
261 private:
262         ///
263         void parse_into1(MathArray & array, unsigned flags, MathTextCodes);
264         ///
265         string getArg(char lf, char rf);
266         ///
267         char getChar();
268         ///
269         void error(string const & msg);
270         ///
271         bool parse_lines(MathAtom & t, bool numbered, bool outmost);
272         /// parses {... & ... \\ ... & ... }
273         bool parse_lines2(MathAtom & t, bool braced);
274         /// dump contents to screen
275         void dump() const;
276
277 private:
278         ///
279         void tokenize(istream & is);
280         ///
281         void tokenize(string const & s);
282         ///
283         void skipSpaceTokens(istream & is, char c);
284         ///
285         void push_back(Token const & t);
286         ///
287         void pop_back();
288         ///
289         Token const & prevToken() const;
290         ///
291         Token const & nextToken() const;
292         ///
293         Token const & getToken();
294         /// skips spaces if any
295         void skipSpaces();
296         /// skips opening brace
297         void skipBegin();
298         /// skips closing brace
299         void skipEnd();
300         /// counts a sequence of hlines
301         int readHLines();
302         ///
303         void lex(string const & s);
304         ///
305         bool good() const;
306
307         ///
308         int lineno_;
309         ///
310         vector<Token> tokens_;
311         ///
312         unsigned pos_;
313         ///
314         bool   curr_num_;
315         ///
316         string curr_label_;
317         ///
318         string curr_skip_;
319 };
320
321
322 Parser::Parser(LyXLex & lexer)
323         : lineno_(lexer.getLineNo()), pos_(0), curr_num_(false)
324 {
325         tokenize(lexer.getStream());
326         lexer.eatLine();
327 }
328
329
330 Parser::Parser(istream & is)
331         : lineno_(0), pos_(0), curr_num_(false)
332 {
333         tokenize(is);
334 }
335
336
337 void Parser::push_back(Token const & t)
338 {
339         tokens_.push_back(t);
340 }
341
342
343 void Parser::pop_back()
344 {
345         tokens_.pop_back();
346 }
347
348
349 Token const & Parser::prevToken() const
350 {
351         static const Token dummy;
352         return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
353 }
354
355
356 Token const & Parser::nextToken() const
357 {
358         static const Token dummy;
359         return good() ? tokens_[pos_] : dummy;
360 }
361
362
363 Token const & Parser::getToken()
364 {
365         static const Token dummy;
366         //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << '\n';
367         return good() ? tokens_[pos_++] : dummy;
368 }
369
370
371 void Parser::skipSpaces()
372 {
373         while (nextToken().cat() == catSpace)
374                 getToken();
375 }
376
377
378 void Parser::skipBegin()
379 {
380         if (nextToken().cat() == catBegin)
381                 getToken();
382         else
383                 lyxerr << "'{' expected\n";
384 }
385
386
387 void Parser::skipEnd()
388 {
389         if (nextToken().cat() == catEnd)
390                 getToken();
391         else
392                 lyxerr << "'}' expected\n";
393 }
394
395
396 int Parser::readHLines()
397 {
398         int num = 0;
399         skipSpaces();
400         while (nextToken().cs() == "hline") {
401                 getToken();
402                 ++num;
403                 skipSpaces();
404         }
405         return num;
406 }
407
408
409 void Parser::putback()
410 {
411         --pos_;
412 }
413
414
415 bool Parser::good() const
416 {
417         return pos_ < tokens_.size();
418 }
419
420
421 char Parser::getChar()
422 {
423         if (!good()) {
424                 lyxerr << "The input stream is not well..." << endl;
425                 dump();
426         }
427         return tokens_[pos_++].character();
428 }
429
430
431 string Parser::getArg(char left, char right)
432 {
433         skipSpaces();
434
435         string result;
436         char c = getChar();
437
438         if (c != left)
439                 putback();
440         else
441                 while ((c = getChar()) != right && good())
442                         result += c;
443
444         return result;
445 }
446
447
448 void Parser::tokenize(istream & is)
449 {
450         // eat everything up to the next \end_inset or end of stream
451         // and store it in s for further tokenization
452         string s;
453         char c;
454         while (is.get(c)) {
455                 s += c;
456                 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
457                         s = s.substr(0, s.size() - 10);
458                         break;
459                 }
460         }
461
462         // tokenize buffer
463         tokenize(s);
464 }
465
466
467 void Parser::skipSpaceTokens(istream & is, char c)
468 {
469         // skip trailing spaces
470         while (catcode(c) == catSpace || catcode(c) == catNewline)
471                 if (!is.get(c))
472                         break;
473         //lyxerr << "putting back: " << c << "\n";
474         is.putback(c);
475 }
476
477
478 void Parser::tokenize(string const & buffer)
479 {
480         static bool init_done = false;
481
482         if (!init_done) {
483                 catInit();
484                 init_done = true;
485         }
486
487         istringstream is(buffer.c_str(), ios::in | ios::binary);
488
489         char c;
490         while (is.get(c)) {
491                 lyxerr << "reading c: " << c << "\n";
492
493                 switch (catcode(c)) {
494                         case catNewline: {
495                                 ++lineno_;
496                                 is.get(c);
497                                 if (catcode(c) == catNewline)
498                                         ; //push_back(Token("par"));
499                                 else {
500                                         push_back(Token(' ', catSpace));
501                                         is.putback(c);
502                                 }
503                                 break;
504                         }
505
506                         case catComment: {
507                                 while (is.get(c) && catcode(c) != catNewline)
508                                         ;
509                                 ++lineno_;
510                                 break;
511                         }
512
513                         case catEscape: {
514                                 is.get(c);
515                                 if (!is) {
516                                         error("unexpected end of input");
517                                 } else {
518                                         string s(1, c);
519                                         if (catcode(c) == catLetter) {
520                                                 // collect letters
521                                                 while (is.get(c) && catcode(c) == catLetter)
522                                                         s += c;
523                                                 skipSpaceTokens(is, c);
524                                         }
525                                         push_back(Token(s));
526                                 }
527                                 break;
528                         }
529
530                         case catSuper:
531                         case catSub: {
532                                 push_back(Token(c, catcode(c)));
533                                 is.get(c);
534                                 skipSpaceTokens(is, c);
535                                 break;
536                         }
537
538                         case catIgnore: {
539                                 lyxerr << "ignoring a char: " << int(c) << "\n";
540                                 break;
541                         }
542
543                         default:
544                                 push_back(Token(c, catcode(c)));
545                 }
546         }
547
548 #ifdef FILEDEBUG
549         dump();
550 #endif
551 }
552
553
554 void Parser::dump() const
555 {
556         lyxerr << "\nTokens: ";
557         for (unsigned i = 0; i < tokens_.size(); ++i) {
558                 if (i == pos_)
559                         lyxerr << " <#> ";
560                 lyxerr << tokens_[i];
561         }
562         lyxerr << "\n";
563 }
564
565
566 void Parser::error(string const & msg)
567 {
568         lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
569         dump();
570         //exit(1);
571 }
572
573
574
575 bool Parser::parse_lines(MathAtom & t, bool numbered, bool outmost)
576 {
577         MathGridInset * p = t->asGridInset();
578         if (!p) {
579                 dump();
580                 lyxerr << "error in Parser::parse_lines() 1\n";
581                 return false;
582         }
583
584         // save global variables
585         bool   const saved_num   = curr_num_;
586         string const saved_label = curr_label_;
587
588         // read initial hlines
589         p->rowinfo(0).lines_ = readHLines();
590
591         for (int row = 0; true; ++row) {
592                 // reset global variables
593                 curr_num_   = numbered;
594                 curr_label_.erase();
595
596                 // reading a row
597                 for (MathInset::col_type col = 0; true; ++col) {
598                         //lyxerr << "reading cell " << row << " " << col << " "
599                         // << p->ncols() << "\n";
600                         //lyxerr << "ncols: " << p->ncols() << "\n";
601
602                         if (col >= p->ncols()) {
603                                 //lyxerr << "adding col " << col << "\n";
604                                 p->addCol(p->ncols());
605                         }
606
607                         MathArray & ar = p->cell(col + row * p->ncols());
608                         parse_into(ar, FLAG_BLOCK);
609                         // remove 'unnecessary' braces:
610                         if (ar.size() == 1 && ar.back()->asBraceInset())
611                                 ar = ar.back()->asBraceInset()->cell(0);
612                         //lyxerr << "ar: " << ar << "\n";
613
614                         // break if cell is not followed by an ampersand
615                         if (nextToken().cat() != catAlign) {
616                                 //lyxerr << "less cells read than normal in row/col: "
617                                 //      << row << " " << col << "\n";
618                                 break;
619                         }
620
621                         // skip the ampersand
622                         getToken();
623                 }
624
625                 if (outmost) {
626                         MathHullInset * m = t->asHullInset();
627                         if (!m) {
628                                 lyxerr << "error in Parser::parse_lines() 2\n";
629                                 return false;
630                         }
631                         m->numbered(row, curr_num_);
632                         m->label(row, curr_label_);
633                         if (curr_skip_.size()) {
634                                 m->vcrskip(LyXLength(curr_skip_), row);
635                                 curr_skip_.erase();
636                         }
637                 }
638
639                 // is a \\ coming?
640                 if (nextToken().isCR()) {
641                         // skip the cr-token
642                         getToken();
643
644                         // try to read a length
645                         //get
646
647                         // read hlines for next row
648                         p->rowinfo(row + 1).lines_ = readHLines();
649                 }
650
651                 // we are finished if the next token is an 'end'
652                 if (nextToken().cs() == "end") {
653                         // skip the end-token
654                         getToken();
655                         getArg('{','}');
656
657                         // leave the 'read a line'-loop
658                         break;
659                 }
660
661                 // otherwise, we have to start a new row
662                 p->appendRow();
663         }
664
665         // restore "global" variables
666         curr_num_   = saved_num;
667         curr_label_ = saved_label;
668
669         return true;
670 }
671
672
673 bool Parser::parse_lines2(MathAtom & t, bool braced)
674 {
675         MathGridInset * p = t->asGridInset();
676         if (!p) {
677                 lyxerr << "error in Parser::parse_lines() 1\n";
678                 return false;
679         }
680
681         for (int row = 0; true; ++row) {
682                 // reading a row
683                 for (MathInset::col_type col = 0; true; ++col) {
684                         //lyxerr << "reading cell " << row << " " << col << " " << p->ncols() << "\n";
685
686                         if (col >= p->ncols()) {
687                                 //lyxerr << "adding col " << col << "\n";
688                                 p->addCol(p->ncols());
689                         }
690
691                         parse_into(p->cell(col + row * p->ncols()), FLAG_BLOCK2);
692                         //lyxerr << "read cell: " << p->cell(col + row * p->ncols()) << "\n";
693
694                         // break if cell is not followed by an ampersand
695                         if (nextToken().cat() != catAlign) {
696                                 //lyxerr << "less cells read than normal in row/col: " << row << " " << col << "\n";
697                                 break;
698                         }
699
700                         // skip the ampersand
701                         getToken();
702                 }
703
704                 // is a \\ coming?
705                 if (nextToken().isCR()) {
706                         // skip the cr-token
707                         getToken();
708                 }
709
710                 // we are finished if the next token is the one we expected
711                 // skip the end-token
712                 // leave the 'read a line'-loop
713                 if (braced) {
714                         if (nextToken().cat() == catEnd) {
715                                 getToken();
716                                 break;
717                         }
718                 } else {
719                         if (nextToken().cs() == "end") {
720                                 getToken();
721                                 getArg('{','}');
722                                 break;
723                         }
724                 }
725
726                 // otherwise, we have to start a new row
727                 p->appendRow();
728         }
729
730         return true;
731 }
732
733
734
735
736 bool Parser::parse_macro(string & name)
737 {
738         int nargs = 0;
739         name = "{error}";
740         skipSpaces();
741
742         if (nextToken().cs() == "def") {
743
744                 getToken();
745                 name = getToken().cs();
746
747                 string pars;
748                 while (good() && nextToken().cat() != catBegin)
749                         pars += getToken().cs();
750
751                 if (!good()) {
752                         lyxerr << "bad stream in parse_macro\n";
753                         dump();
754                         return false;
755                 }
756
757                 //lyxerr << "read \\def parameter list '" << pars << "'\n";
758                 if (!pars.empty()) {
759                         lyxerr << "can't handle non-empty parameter lists\n";
760                         dump();
761                         return false;
762                 }
763
764         } else if (nextToken().cs() == "newcommand") {
765
766                 getToken();
767
768                 if (getToken().cat() != catBegin) {
769                         lyxerr << "'{' in \\newcommand expected (1) \n";
770                         dump();
771                         return false;
772                 }
773
774                 name = getToken().cs();
775
776                 if (getToken().cat() != catEnd) {
777                         lyxerr << "'}' expected\n";
778                         return false;
779                 }
780
781                 string arg  = getArg('[', ']');
782                 if (!arg.empty())
783                         nargs = atoi(arg.c_str());
784
785         } else {
786                 lyxerr << "\\newcommand or \\def  expected\n";
787                 return false;
788         }
789
790
791         if (getToken().cat() != catBegin) {
792                 lyxerr << "'{' in macro definition expected (2)\n";
793                 return false;
794         }
795
796         MathArray ar1;
797         parse_into(ar1, FLAG_BRACE_LAST);
798
799         // we cannot handle recursive stuff at all
800         MathArray test;
801         test.push_back(createMathInset(name));
802         if (ar1.contains(test)) {
803                 lyxerr << "we cannot handle recursive macros at all.\n";
804                 return false;
805         }
806
807         // is a version for display attached?
808         MathArray ar2;
809         parse_into(ar2, FLAG_ITEM);
810
811         MathMacroTable::create(name, nargs, ar1, ar2);
812         return true;
813 }
814
815
816 bool Parser::parse_normal(MathAtom & matrix)
817 {
818         skipSpaces();
819         Token const & t = getToken();
820
821         if (t.cs() == "(") {
822                 matrix = MathAtom(new MathHullInset(LM_OT_SIMPLE));
823                 parse_into(matrix->cell(0), 0);
824                 return true;
825         }
826
827         if (t.cat() == catMath) {
828                 Token const & n = getToken();
829                 if (n.cat() == catMath) {
830                         // TeX's $$...$$ syntax for displayed math
831                         matrix = MathAtom(new MathHullInset(LM_OT_EQUATION));
832                         MathHullInset * p = matrix->asHullInset();
833                         parse_into(p->cell(0), 0);
834                         p->numbered(0, curr_num_);
835                         p->label(0, curr_label_);
836                 } else {
837                         // simple $...$  stuff
838                         putback();
839                         matrix = MathAtom(new MathHullInset(LM_OT_SIMPLE));
840                         parse_into(matrix->cell(0), 0);
841                 }
842                 return true;
843         }
844
845         if (!t.cs().size()) {
846                 lyxerr << "start of math expected, got '" << t << "'\n";
847                 return false;
848         }
849
850         string const & cs = t.cs();
851
852         if (cs == "[") {
853                 curr_num_ = 0;
854                 curr_label_.erase();
855                 matrix = MathAtom(new MathHullInset(LM_OT_EQUATION));
856                 MathHullInset * p = matrix->asHullInset();
857                 parse_into(p->cell(0), 0);
858                 p->numbered(0, curr_num_);
859                 p->label(0, curr_label_);
860                 return true;
861         }
862
863         if (cs != "begin") {
864                 lyxerr[Debug::MATHED]
865                         << "'begin' of un-simple math expected, got '" << cs << "'\n";
866                 return false;
867         }
868
869         string const name = getArg('{', '}');
870
871         if (name == "math") {
872                 matrix = MathAtom(new MathHullInset(LM_OT_SIMPLE));
873                 parse_into(matrix->cell(0), 0);
874                 return true;
875         }
876
877         if (name == "equation" || name == "equation*" || name == "displaymath") {
878                 curr_num_ = (name == "equation");
879                 curr_label_.erase();
880                 matrix = MathAtom(new MathHullInset(LM_OT_EQUATION));
881                 MathHullInset * p = matrix->asHullInset();
882                 parse_into(p->cell(0), FLAG_END);
883                 p->numbered(0, curr_num_);
884                 p->label(0, curr_label_);
885                 return true;
886         }
887
888         if (name == "eqnarray" || name == "eqnarray*") {
889                 matrix = MathAtom(new MathHullInset(LM_OT_EQNARRAY));
890                 return parse_lines(matrix, !stared(name), true);
891         }
892
893         if (name == "align" || name == "align*") {
894                 matrix = MathAtom(new MathHullInset(LM_OT_ALIGN));
895                 return parse_lines(matrix, !stared(name), true);
896         }
897
898         if (name == "alignat" || name == "alignat*") {
899                 int nc = 2 * atoi(getArg('{', '}').c_str());
900                 matrix = MathAtom(new MathHullInset(LM_OT_ALIGNAT, nc));
901                 return parse_lines(matrix, !stared(name), true);
902         }
903
904         if (name == "xalignat" || name == "xalignat*") {
905                 int nc = 2 * atoi(getArg('{', '}').c_str());
906                 matrix = MathAtom(new MathHullInset(LM_OT_XALIGNAT, nc));
907                 return parse_lines(matrix, !stared(name), true);
908         }
909
910         if (name == "xxalignat") {
911                 int nc = 2 * atoi(getArg('{', '}').c_str());
912                 matrix = MathAtom(new MathHullInset(LM_OT_XXALIGNAT, nc));
913                 return parse_lines(matrix, !stared(name), true);
914         }
915
916         if (name == "multline" || name == "multline*") {
917                 matrix = MathAtom(new MathHullInset(LM_OT_MULTLINE));
918                 return parse_lines(matrix, !stared(name), true);
919         }
920
921         if (name == "gather" || name == "gather*") {
922                 matrix = MathAtom(new MathHullInset(LM_OT_GATHER));
923                 return parse_lines(matrix, !stared(name), true);
924         }
925
926         lyxerr[Debug::MATHED] << "1: unknown math environment: " << name << "\n";
927         lyxerr << "1: unknown math environment: " << name << "\n";
928         return false;
929 }
930
931
932 void Parser::parse_into(MathArray & array, unsigned flags, MathTextCodes code)
933 {
934         parse_into1(array, flags, code);
935         // remove 'unnecessary' braces:
936         if (array.size() == 1 && array.back()->asBraceInset()) {
937                 lyxerr << "extra braces removed\n";
938                 array = array.back()->asBraceInset()->cell(0);
939         }
940 }
941
942
943 void Parser::parse_into1(MathArray & array, unsigned flags, MathTextCodes code)
944 {
945         bool panic  = false;
946         int  limits = 0;
947
948         while (good()) {
949                 Token const & t = getToken();
950
951 #ifdef FILEDEBUG
952                 lyxerr << "t: " << t << " flags: " << flags << "\n";
953                 //array.dump();
954                 lyxerr << "\n";
955 #endif
956
957                 if (flags & FLAG_ITEM) {
958                         if (t.cat() == catSpace)
959                                 continue;
960
961                         flags &= ~FLAG_ITEM;
962                         if (t.cat() == catBegin) {
963                                 // skip the brace and collect everything to the next matching
964                                 // closing brace
965                                 flags |= FLAG_BRACE_LAST;
966                                 continue;
967                         }
968
969                         // handle only this single token, leave the loop if done
970                         flags |= FLAG_LEAVE;
971                 }
972
973                 if (flags & FLAG_BLOCK) {
974                         if (t.cat() == catAlign || t.isCR() || t.cs() == "end") {
975                                 putback();
976                                 return;
977                         }
978                 }
979
980                 if (flags & FLAG_BLOCK2) {
981                         if (t.cat() == catAlign || t.isCR() || t.cs() == "end"
982                                         || t.cat() == catEnd) {
983                                 putback();
984                                 return;
985                         }
986                 }
987
988                 //
989                 // cat codes
990                 //
991                 if (t.cat() == catMath) {
992                         if (flags & FLAG_BOX) {
993                                 // we are inside an mbox, so opening new math is allowed
994                                 array.push_back(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
995                                 parse_into(array.back()->cell(0), 0);
996                         } else {
997                                 // otherwise this is the end of the formula
998                                 break;
999                         }
1000                 }
1001
1002                 else if (t.cat() == catLetter)
1003                         add(array, t.character(), code);
1004
1005                 else if (t.cat() == catSpace && code == LM_TC_TEXTRM)
1006                         add(array, t.character(), code);
1007
1008                 else if (t.cat() == catParameter) {
1009                         Token const & n = getToken();
1010                         array.push_back(MathAtom(new MathMacroArgument(n.character()-'0', code)));
1011                 }
1012
1013                 else if (t.cat() == catBegin) {
1014                         MathArray ar;
1015                         parse_into(ar, FLAG_BRACE_LAST);
1016 #ifndef WITH_WARNINGS
1017 #warning this might be wrong in general!
1018 #endif
1019                         // ignore braces around simple items
1020                         if ((ar.size() == 1 && !ar.front()->needsBraces()
1021        || (ar.size() == 2 && !ar.front()->needsBraces()
1022                                             && ar.back()->asScriptInset()))
1023        || (ar.size() == 0 && array.size() == 0))
1024                         {
1025                                 array.push_back(ar);
1026                         } else {
1027                                 array.push_back(MathAtom(new MathBraceInset));
1028                                 array.back()->cell(0).swap(ar);
1029                         }
1030                 }
1031
1032                 else if (t.cat() == catEnd) {
1033                         if (flags & FLAG_BRACE_LAST)
1034                                 return;
1035                         lyxerr << "found '}' unexpectedly, array: '" << array << "'\n";
1036                         dump();
1037                         //lyxerr << "found '}' unexpectedly\n";
1038                         //lyx::Assert(0);
1039                         //add(array, '}', LM_TC_TEX);
1040                 }
1041
1042                 else if (t.cat() == catAlign) {
1043                         lyxerr << "found tab unexpectedly, array: '" << array << "'\n";
1044                         dump();
1045                         //lyxerr << "found tab unexpectedly\n";
1046                         add(array, '&', LM_TC_TEX);
1047                 }
1048
1049                 else if (t.cat() == catSuper || t.cat() == catSub) {
1050                         bool up = (t.cat() == catSuper);
1051                         MathScriptInset * p = 0;
1052                         if (array.size())
1053                                 p = array.back()->asScriptInset();
1054                         if (!p || p->has(up)) {
1055                                 array.push_back(MathAtom(new MathScriptInset(up)));
1056                                 p = array.back()->asScriptInset();
1057                         }
1058                         p->ensure(up);
1059                         parse_into(p->cell(up), FLAG_ITEM);
1060                         p->limits(limits);
1061                         limits = 0;
1062                 }
1063
1064                 else if (t.character() == ']' && (flags & FLAG_BRACK_END))
1065                         return;
1066
1067                 else if (t.cat() == catOther)
1068                         add(array, t.character(), code);
1069
1070                 //
1071                 // control sequences
1072                 //
1073                 else if (t.cs() == "protect")
1074                         // ignore \\protect, will be re-added during output
1075                         ;
1076
1077                 else if (t.cs() == "end")
1078                         break;
1079
1080                 else if (t.cs() == ")")
1081                         break;
1082
1083                 else if (t.cs() == "]")
1084                         break;
1085
1086                 else if (t.cs() == "\\") {
1087                         curr_skip_ = getArg('[', ']');
1088                         //lyxerr << "found newline unexpectedly, array: '" << array << "'\n";
1089                         lyxerr << "found newline unexpectedly\n";
1090                         array.push_back(createMathInset("\\"));
1091                 }
1092
1093                 else if (t.cs() == "limits")
1094                         limits = 1;
1095
1096                 else if (t.cs() == "nolimits")
1097                         limits = -1;
1098
1099                 else if (t.cs() == "nonumber")
1100                         curr_num_ = false;
1101
1102                 else if (t.cs() == "number")
1103                         curr_num_ = true;
1104
1105                 else if (t.cs() == "sqrt") {
1106                         char c = getChar();
1107                         if (c == '[') {
1108                                 array.push_back(MathAtom(new MathRootInset));
1109                                 parse_into(array.back()->cell(0), FLAG_BRACK_END);
1110                                 parse_into(array.back()->cell(1), FLAG_ITEM);
1111                         } else {
1112                                 putback();
1113                                 array.push_back(MathAtom(new MathSqrtInset));
1114                                 parse_into(array.back()->cell(0), FLAG_ITEM);
1115                         }
1116                 }
1117
1118                 else if (t.cs() == "left") {
1119                         string l = getToken().asString();
1120                         MathArray ar;
1121                         parse_into(ar, FLAG_RIGHT);
1122                         string r = getToken().asString();
1123                         MathAtom dl(new MathDelimInset(l, r));
1124                         dl->cell(0) = ar;
1125                         array.push_back(dl);
1126                 }
1127
1128                 else if (t.cs() == "right") {
1129                         if (!(flags & FLAG_RIGHT)) {
1130                                 //lyxerr << "got so far: '" << array << "'\n";
1131                                 error("Unmatched right delimiter");
1132                         }
1133                         return;
1134                 }
1135
1136                 else if (t.cs() == "begin") {
1137                         string const name = getArg('{', '}');
1138                         if (name == "array" || name == "subarray") {
1139                                 string const valign = getArg('[', ']') + 'c';
1140                                 string const halign = getArg('{', '}');
1141                                 array.push_back(MathAtom(new MathArrayInset(name, valign[0], halign)));
1142                                 parse_lines(array.back(), false, false);
1143                         } else if (name == "split" || name == "cases" ||
1144                                          name == "gathered" || name == "aligned") {
1145                                 array.push_back(createMathInset(name));
1146                                 parse_lines(array.back(), false, false);
1147                         } else if (name == "matrix"  || name == "pmatrix" || name == "bmatrix" ||
1148                                          name == "vmatrix" || name == "Vmatrix") {
1149                                 array.push_back(createMathInset(name));
1150                                 parse_lines2(array.back(), false);
1151                         } else
1152                                 lyxerr << "unknow math inset begin '" << name << "'\n";
1153                 }
1154
1155                 else if (t.cs() == "kern") {
1156 #ifdef WITH_WARNINGS
1157 #warning A hack...
1158 #endif
1159                         string s;
1160                         while (1) {
1161                                 Token const & t = getToken();
1162                                 if (!good()) {
1163                                         putback();
1164                                         break;
1165                                 }
1166                                 s += t.character();
1167                                 if (isValidLength(s))
1168                                         break;
1169                         }
1170                         array.push_back(MathAtom(new MathKernInset(s)));
1171                 }
1172
1173 /*
1174                 else if (t.cs() == "lyxkern") {
1175                         MathAtom p = createMathInset(t.cs());
1176                         parse_into(p->cell(0), flags, code);
1177                         array.push_back(p);
1178                 }
1179 */
1180
1181                 else if (t.cs() == "label") {
1182                         curr_label_ = getArg('{', '}');
1183                 }
1184
1185                 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1186                         MathAtom p = createMathInset(t.cs());
1187                         array.swap(p->cell(0));
1188                         parse_into(p->cell(1), flags, code);
1189                         array.push_back(p);
1190                         return;
1191                 }
1192
1193                 else if (t.cs() == "substack") {
1194                         array.push_back(createMathInset(t.cs()));
1195                         skipBegin();
1196                         parse_lines2(array.back(), true);
1197                 }
1198
1199                 else if (t.cs() == "xymatrix") {
1200                         array.push_back(createMathInset(t.cs()));
1201                         skipBegin();
1202                         parse_lines2(array.back(), true);
1203                 }
1204
1205 #if 0
1206                 // Disabled
1207                 else if (1 && t.cs() == "ar") {
1208                         MathXYArrowInset * p = new MathXYArrowInset;
1209
1210                         // try to read target
1211                         char c = getChar();
1212                         if (c == '[') {
1213                                 parse_into(p->cell(0), FLAG_BRACK_END);
1214                                 //lyxerr << "read target: " << p->cell(0) << "\n";
1215                         } else {
1216                                 putback();
1217                         }
1218
1219                         // try to read label
1220                         if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1221                                 p->up_ = nextToken().cat() == catSuper;
1222                                 getToken();
1223                                 parse_into(p->cell(1), FLAG_ITEM);
1224                                 //lyxerr << "read label: " << p->cell(1) << "\n";
1225                         }
1226
1227                         array.push_back(MathAtom(p));
1228                         //lyxerr << "read array: " << array << "\n";
1229                 }
1230 #endif
1231
1232 #if 0
1233                 else if (t.cs() == "mbox" || t.cs() == "text") {
1234                         //array.push_back(createMathInset(t.cs()));
1235                         array.push_back(MathAtom(new MathBoxInset(t.cs())));
1236                         // slurp in the argument of mbox
1237
1238                         MathBoxInset * p = array.back()->asBoxInset();
1239                         //lyx::assert(p);
1240                 }
1241 #endif
1242
1243
1244                 else if (t.cs().size()) {
1245                         latexkeys const * l = in_word_set(t.cs());
1246                         if (l) {
1247                                 if (l->token == LM_TK_FONT) {
1248                                         //lyxerr << "starting font\n";
1249                                         //CatCode catSpaceSave = theCatcode[' '];
1250                                         //if (l->id == LM_TC_TEXTRM) {
1251                                         //      // temporarily change catcode
1252                                         //      theCatcode[' '] = catLetter;
1253                                         //}
1254
1255                                         MathArray ar;
1256                                         parse_into(ar, FLAG_ITEM, static_cast<MathTextCodes>(l->id));
1257                                         array.push_back(ar);
1258
1259                                         // undo catcode changes
1260                                         ////theCatcode[' '] = catSpaceSave;
1261                                         //lyxerr << "ending font\n";
1262                                 }
1263
1264                                 else if (l->token == LM_TK_OLDFONT) {
1265                                         code = static_cast<MathTextCodes>(l->id);
1266                                 }
1267
1268                                 else if (l->token == LM_TK_BOX) {
1269                                         MathAtom p = createMathInset(t.cs());
1270                                         parse_into(p->cell(0), FLAG_ITEM | FLAG_BOX, LM_TC_BOX);
1271                                         array.push_back(p);
1272                                 }
1273
1274                                 else if (l->token == LM_TK_STY) {
1275                                         MathAtom p = createMathInset(t.cs());
1276                                         parse_into(p->cell(0), flags, code);
1277                                         array.push_back(p);
1278                                         return;
1279                                 }
1280
1281                                 else {
1282                                         MathAtom p = createMathInset(t.cs());
1283                                         for (MathInset::idx_type i = 0; i < p->nargs(); ++i)
1284                                                 parse_into(p->cell(i), FLAG_ITEM);
1285                                         array.push_back(p);
1286                                 }
1287                         }
1288
1289                         else {
1290                                 MathAtom p = createMathInset(t.cs());
1291                                 for (MathInset::idx_type i = 0; i < p->nargs(); ++i)
1292                                         parse_into(p->cell(i), FLAG_ITEM);
1293                                 array.push_back(p);
1294                         }
1295                 }
1296
1297
1298                 if (flags & FLAG_LEAVE) {
1299                         flags &= ~FLAG_LEAVE;
1300                         break;
1301                 }
1302         }
1303
1304         if (panic) {
1305                 lyxerr << " Math Panic, expect problems!\n";
1306                 //   Search for the end command.
1307                 Token t;
1308                 do {
1309                         t = getToken();
1310                 } while (good() && t.cs() != "end");
1311         }
1312 }
1313
1314
1315
1316 } // anonymous namespace
1317
1318
1319 void mathed_parse_cell(MathArray & ar, string const & str)
1320 {
1321         istringstream is(str.c_str());
1322         mathed_parse_cell(ar, is);
1323 }
1324
1325
1326 void mathed_parse_cell(MathArray & ar, istream & is)
1327 {
1328         Parser(is).parse_into(ar, 0);
1329 }
1330
1331
1332
1333 bool mathed_parse_macro(string & name, string const & str)
1334 {
1335         istringstream is(str.c_str());
1336         Parser parser(is);
1337         return parser.parse_macro(name);
1338 }
1339
1340 bool mathed_parse_macro(string & name, istream & is)
1341 {
1342         Parser parser(is);
1343         return parser.parse_macro(name);
1344 }
1345
1346 bool mathed_parse_macro(string & name, LyXLex & lex)
1347 {
1348         Parser parser(lex);
1349         return parser.parse_macro(name);
1350 }
1351
1352
1353
1354 bool mathed_parse_normal(MathAtom & t, string const & str)
1355 {
1356         istringstream is(str.c_str());
1357         Parser parser(is);
1358         return parser.parse_normal(t);
1359 }
1360
1361 bool mathed_parse_normal(MathAtom & t, istream & is)
1362 {
1363         Parser parser(is);
1364         return parser.parse_normal(t);
1365 }
1366
1367 bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
1368 {
1369         Parser parser(lex);
1370         return parser.parse_normal(t);
1371 }