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