]> git.lyx.org Git - lyx.git/blob - src/mathed/MathParser.C
Almost fix 'make check'. The only remaining problem is an undefined
[lyx.git] / src / mathed / MathParser.C
1 /**
2  * \file MathParser.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 /*
12
13 If someone desperately needs partial "structures" (such as a few
14 cells of an array inset or similar) (s)he could uses the
15 following hack as starting point to write some macros:
16
17   \newif\ifcomment
18   \commentfalse
19   \ifcomment
20           \def\makeamptab{\catcode`\&=4\relax}
21           \def\makeampletter{\catcode`\&=11\relax}
22     \def\b{\makeampletter\expandafter\makeamptab\bi}
23     \long\def\bi#1\e{}
24   \else
25     \def\b{}\def\e{}
26   \fi
27
28   ...
29
30   \[\begin{array}{ccc}
31 1
32 &
33
34   \end{array}\]
35
36 */
37
38
39 #include <config.h>
40
41 #include "MathParser.h"
42
43 #include "InsetMathArray.h"
44 #include "InsetMathBig.h"
45 #include "InsetMathBrace.h"
46 #include "InsetMathChar.h"
47 #include "InsetMathColor.h"
48 #include "InsetMathComment.h"
49 #include "InsetMathDelim.h"
50 #include "InsetMathEnv.h"
51 #include "InsetMathKern.h"
52 #include "InsetMathMacro.h"
53 #include "InsetMathPar.h"
54 #include "InsetMathRef.h"
55 #include "InsetMathRoot.h"
56 #include "InsetMathScript.h"
57 #include "InsetMathSplit.h"
58 #include "InsetMathSqrt.h"
59 #include "InsetMathTabular.h"
60 #include "MathMacroTemplate.h"
61 #include "MathFactory.h"
62 #include "MathMacroArgument.h"
63 #include "MathSupport.h"
64
65 #include "lyxlex.h"
66 #include "debug.h"
67
68 #include "support/convert.h"
69
70 #include <sstream>
71
72
73 namespace lyx {
74
75 using std::endl;
76 using std::fill;
77
78 using std::string;
79 using std::ios;
80 using std::istream;
81 using std::ostream;
82 using std::vector;
83
84
85 //#define FILEDEBUG
86
87
88 namespace {
89
90 InsetMath::mode_type asMode(InsetMath::mode_type oldmode, docstring const & str)
91 {
92         //lyxerr << "handling mode: '" << str << "'" << endl;
93         if (str == "mathmode")
94                 return InsetMath::MATH_MODE;
95         if (str == "textmode" || str == "forcetext")
96                 return InsetMath::TEXT_MODE;
97         return oldmode;
98 }
99
100
101 bool stared(docstring const & s)
102 {
103         size_t const n = s.size();
104         return n && s[n - 1] == '*';
105 }
106
107
108 /*!
109  * Add the row \p cellrow to \p grid.
110  * \returns wether the row could be added. Adding a row can fail for
111  * environments like "equation" that have a fixed number of rows.
112  */
113 bool addRow(InsetMathGrid & grid, InsetMathGrid::row_type & cellrow,
114             docstring const & vskip)
115 {
116         ++cellrow;
117         if (cellrow == grid.nrows()) {
118                 //lyxerr << "adding row " << cellrow << endl;
119                 grid.addRow(cellrow - 1);
120                 if (cellrow == grid.nrows()) {
121                         // We can't add a row to this grid, so let's
122                         // append the content of this cell to the previous
123                         // one.
124                         // This does not happen in well formed .lyx files,
125                         // but LyX versions 1.3.x and older could create
126                         // such files and tex2lyx can still do that.
127                         --cellrow;
128                         lyxerr << "ignoring extra row";
129                         if (!vskip.empty())
130                                 lyxerr << " with extra space " << to_utf8(vskip);
131                         lyxerr << '.' << endl;
132                         return false;
133                 }
134         }
135         grid.vcrskip(LyXLength(to_utf8(vskip)), cellrow - 1);
136         return true;
137 }
138
139
140 /*!
141  * Add the column \p cellcol to \p grid.
142  * \returns wether the column could be added. Adding a column can fail for
143  * environments like "eqnarray" that have a fixed number of columns.
144  */
145 bool addCol(InsetMathGrid & grid, InsetMathGrid::col_type & cellcol)
146 {
147         ++cellcol;
148         if (cellcol == grid.ncols()) {
149                 //lyxerr << "adding column " << cellcol << endl;
150                 grid.addCol(cellcol - 1);
151                 if (cellcol == grid.ncols()) {
152                         // We can't add a column to this grid, so let's
153                         // append the content of this cell to the previous
154                         // one.
155                         // This does not happen in well formed .lyx files,
156                         // but LyX versions 1.3.x and older could create
157                         // such files and tex2lyx can still do that.
158                         --cellcol;
159                         lyxerr << "ignoring extra column." << endl;
160                         return false;
161                 }
162         }
163         return true;
164 }
165
166
167 /*!
168  * Check wether the last row is empty and remove it if yes.
169  * Otherwise the following code
170  * \verbatim
171 \begin{array}{|c|c|}
172 \hline
173 1 & 2 \\ \hline
174 3 & 4 \\ \hline
175 \end{array}
176  * \endverbatim
177  * will result in a grid with 3 rows (+ the dummy row that is always present),
178  * because the last '\\' opens a new row.
179  */
180 void delEmptyLastRow(InsetMathGrid & grid)
181 {
182         InsetMathGrid::row_type const row = grid.nrows() - 1;
183         for (InsetMathGrid::col_type col = 0; col < grid.ncols(); ++col) {
184                 if (!grid.cell(grid.index(row, col)).empty())
185                         return;
186         }
187         // Copy the row information of the empty row (which would contain the
188         // last hline in the example above) to the dummy row and delete the
189         // empty row.
190         grid.rowinfo(row + 1) = grid.rowinfo(row);
191         grid.delRow(row);
192 }
193
194
195 // These are TeX's catcodes
196 enum CatCode {
197         catEscape,     // 0    backslash
198         catBegin,      // 1    {
199         catEnd,        // 2    }
200         catMath,       // 3    $
201         catAlign,      // 4    &
202         catNewline,    // 5    ^^M
203         catParameter,  // 6    #
204         catSuper,      // 7    ^
205         catSub,        // 8    _
206         catIgnore,     // 9
207         catSpace,      // 10   space
208         catLetter,     // 11   a-zA-Z
209         catOther,      // 12   none of the above
210         catActive,     // 13   ~
211         catComment,    // 14   %
212         catInvalid     // 15   <delete>
213 };
214
215 CatCode theCatcode[128];
216
217
218 inline CatCode catcode(char_type c)
219 {
220         /* The only characters that are not catOther lie in the pure ASCII
221          * range. Therefore theCatcode has only 128 entries.
222          * TeX itself deals with 8bit characters, so if needed this table
223          * could be enlarged to 256 entries.
224          * Any larger value does not make sense, since the fact that we use
225          * unicode internally does not change Knuth's TeX engine.
226          * Apart from that a table for the full 21bit UCS4 range would waste
227          * too much memory. */
228         if (c >= 128)
229                 return catOther;
230
231         return theCatcode[c];
232 }
233
234
235 enum {
236         FLAG_ALIGN      = 1 << 0,  //  next & or \\ ends the parsing process
237         FLAG_BRACE_LAST = 1 << 1,  //  next closing brace ends the parsing
238         FLAG_RIGHT      = 1 << 2,  //  next \\right ends the parsing process
239         FLAG_END        = 1 << 3,  //  next \\end ends the parsing process
240         FLAG_BRACK_LAST = 1 << 4,  //  next closing bracket ends the parsing
241         FLAG_TEXTMODE   = 1 << 5,  //  we are in a box
242         FLAG_ITEM       = 1 << 6,  //  read a (possibly braced token)
243         FLAG_LEAVE      = 1 << 7,  //  leave the loop at the end
244         FLAG_SIMPLE     = 1 << 8,  //  next $ leaves the loop
245         FLAG_EQUATION   = 1 << 9,  //  next \] leaves the loop
246         FLAG_SIMPLE2    = 1 << 10, //  next \) leaves the loop
247         FLAG_OPTION     = 1 << 11, //  read [...] style option
248         FLAG_BRACED     = 1 << 12  //  read {...} style argument
249 };
250
251
252 //
253 // Helper class for parsing
254 //
255
256 class Token {
257 public:
258         ///
259         Token() : cs_(), char_(0), cat_(catIgnore) {}
260         ///
261         Token(char_type c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
262         ///
263         Token(docstring const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
264
265         ///
266         docstring const & cs() const { return cs_; }
267         ///
268         CatCode cat() const { return cat_; }
269         ///
270         char_type character() const { return char_; }
271         ///
272         docstring asString() const { return cs_.size() ? cs_ : docstring(1, char_); }
273         ///
274         docstring asInput() const { return cs_.size() ? '\\' + cs_ : docstring(1, char_); }
275
276 private:
277         ///
278         docstring cs_;
279         ///
280         char_type char_;
281         ///
282         CatCode cat_;
283 };
284
285 ostream & operator<<(ostream & os, Token const & t)
286 {
287         if (t.cs().size()) {
288                 docstring const & cs = t.cs();
289                 // FIXME: For some strange reason, the stream operator instanciate
290                 // a new Token before outputting the contents of t.cs().
291                 // Because of this the line 
292                 //     os << '\\' << cs;
293                 // below becomes recursive.
294                 // In order to avoid that we return early:
295                 if (cs == "\\")
296                         return os;
297                 os << '\\' << cs;
298         }
299         else if (t.cat() == catLetter)
300                 os << t.character();
301         else
302                 os << '[' << t.character() << ',' << t.cat() << ']';
303         return os;
304 }
305
306
307 class Parser {
308 public:
309         ///
310         typedef  InsetMath::mode_type mode_type;
311
312         ///
313         Parser(LyXLex & lex);
314         /// Only use this for reading from .lyx file format, for the reason
315         /// see Parser::tokenize(std::istream &).
316         Parser(istream & is);
317         ///
318         Parser(docstring const & str);
319
320         ///
321         bool parse(MathAtom & at);
322         ///
323         void parse(MathArray & array, unsigned flags, mode_type mode);
324         ///
325         void parse1(InsetMathGrid & grid, unsigned flags, mode_type mode,
326                 bool numbered);
327         ///
328         MathArray parse(unsigned flags, mode_type mode);
329         ///
330         int lineno() const { return lineno_; }
331         ///
332         void putback();
333
334 private:
335         ///
336         void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
337         /// get arg delimited by 'left' and 'right'
338         docstring getArg(char_type left, char_type right);
339         ///
340         char_type getChar();
341         ///
342         void error(string const & msg);
343         void error(docstring const & msg) { error(to_utf8(msg)); }
344         /// dump contents to screen
345         void dump() const;
346         /// Only use this for reading from .lyx file format (see
347         /// implementation for reason)
348         void tokenize(istream & is);
349         ///
350         void tokenize(docstring const & s);
351         ///
352         void skipSpaceTokens(idocstream & is, char_type c);
353         ///
354         void push_back(Token const & t);
355         ///
356         void pop_back();
357         ///
358         Token const & prevToken() const;
359         ///
360         Token const & nextToken() const;
361         ///
362         Token const & getToken();
363         /// skips spaces if any
364         void skipSpaces();
365         ///
366         void lex(docstring const & s);
367         ///
368         bool good() const;
369         ///
370         docstring parse_verbatim_item();
371         ///
372         docstring parse_verbatim_option();
373
374         ///
375         int lineno_;
376         ///
377         vector<Token> tokens_;
378         ///
379         unsigned pos_;
380         /// Stack of active environments
381         vector<docstring> environments_;
382 };
383
384
385 Parser::Parser(LyXLex & lexer)
386         : lineno_(lexer.getLineNo()), pos_(0)
387 {
388         tokenize(lexer.getStream());
389         lexer.eatLine();
390 }
391
392
393 Parser::Parser(istream & is)
394         : lineno_(0), pos_(0)
395 {
396         tokenize(is);
397 }
398
399
400 Parser::Parser(docstring const & str)
401         : lineno_(0), pos_(0)
402 {
403         tokenize(str);
404 }
405
406
407 void Parser::push_back(Token const & t)
408 {
409         tokens_.push_back(t);
410 }
411
412
413 void Parser::pop_back()
414 {
415         tokens_.pop_back();
416 }
417
418
419 Token const & Parser::prevToken() const
420 {
421         static const Token dummy;
422         return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
423 }
424
425
426 Token const & Parser::nextToken() const
427 {
428         static const Token dummy;
429         return good() ? tokens_[pos_] : dummy;
430 }
431
432
433 Token const & Parser::getToken()
434 {
435         static const Token dummy;
436         //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
437         return good() ? tokens_[pos_++] : dummy;
438 }
439
440
441 void Parser::skipSpaces()
442 {
443         while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
444                 getToken();
445 }
446
447
448 void Parser::putback()
449 {
450         --pos_;
451 }
452
453
454 bool Parser::good() const
455 {
456         return pos_ < tokens_.size();
457 }
458
459
460 char_type Parser::getChar()
461 {
462         if (!good())
463                 error("The input stream is not well...");
464         return tokens_[pos_++].character();
465 }
466
467
468 docstring Parser::getArg(char_type left, char_type right)
469 {
470         skipSpaces();
471
472         docstring result;
473         char_type c = getChar();
474
475         if (c != left)
476                 putback();
477         else
478                 while ((c = getChar()) != right && good())
479                         result += c;
480
481         return result;
482 }
483
484
485 void Parser::skipSpaceTokens(idocstream & is, char_type c)
486 {
487         // skip trailing spaces
488         while (catcode(c) == catSpace || catcode(c) == catNewline)
489                 if (!is.get(c))
490                         break;
491         //lyxerr << "putting back: " << c << endl;
492         is.putback(c);
493 }
494
495
496 void Parser::tokenize(istream & is)
497 {
498         // eat everything up to the next \end_inset or end of stream
499         // and store it in s for further tokenization
500         string s;
501         char c;
502         while (is.get(c)) {
503                 s += c;
504                 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
505                         s = s.substr(0, s.size() - 10);
506                         break;
507                 }
508         }
509         // Remove the space after \end_inset
510         if (is.get(c) && c != ' ')
511                 is.unget();
512
513         // tokenize buffer
514         tokenize(from_utf8(s));
515 }
516
517
518 void Parser::tokenize(docstring const & buffer)
519 {
520         idocstringstream is(buffer, ios::in | ios::binary);
521
522         char_type c;
523         while (is.get(c)) {
524                 //lyxerr << "reading c: " << c << endl;
525
526                 switch (catcode(c)) {
527                         case catNewline: {
528                                 ++lineno_;
529                                 is.get(c);
530                                 if (catcode(c) == catNewline)
531                                         ; //push_back(Token("par"));
532                                 else {
533                                         push_back(Token('\n', catNewline));
534                                         is.putback(c);
535                                 }
536                                 break;
537                         }
538
539 /*
540                         case catComment: {
541                                 while (is.get(c) && catcode(c) != catNewline)
542                                         ;
543                                 ++lineno_;
544                                 break;
545                         }
546 */
547
548                         case catEscape: {
549                                 is.get(c);
550                                 if (!is) {
551                                         error("unexpected end of input");
552                                 } else {
553                                         docstring s(1, c);
554                                         if (catcode(c) == catLetter) {
555                                                 // collect letters
556                                                 while (is.get(c) && catcode(c) == catLetter)
557                                                         s += c;
558                                                 skipSpaceTokens(is, c);
559                                         }
560                                         push_back(Token(s));
561                                 }
562                                 break;
563                         }
564
565                         case catSuper:
566                         case catSub: {
567                                 push_back(Token(c, catcode(c)));
568                                 is.get(c);
569                                 skipSpaceTokens(is, c);
570                                 break;
571                         }
572
573                         case catIgnore: {
574                                 lyxerr << "ignoring a char: " << int(c) << endl;
575                                 break;
576                         }
577
578                         default:
579                                 push_back(Token(c, catcode(c)));
580                 }
581         }
582
583 #ifdef FILEDEBUG
584         dump();
585 #endif
586 }
587
588
589 void Parser::dump() const
590 {
591         lyxerr << "\nTokens: ";
592         for (unsigned i = 0; i < tokens_.size(); ++i) {
593                 if (i == pos_)
594                         lyxerr << " <#> ";
595                 lyxerr << tokens_[i];
596         }
597         lyxerr << " pos: " << pos_ << endl;
598 }
599
600
601 void Parser::error(string const & msg)
602 {
603         lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
604         dump();
605         //exit(1);
606 }
607
608
609 bool Parser::parse(MathAtom & at)
610 {
611         skipSpaces();
612         MathArray ar;
613         parse(ar, false, InsetMath::UNDECIDED_MODE);
614         if (ar.size() != 1 || ar.front()->getType() == hullNone) {
615                 lyxerr << "unusual contents found: " << ar << endl;
616                 at = MathAtom(new InsetMathPar(ar));
617                 //if (at->nargs() > 0)
618                 //      at.nucleus()->cell(0) = ar;
619                 //else
620                 //      lyxerr << "unusual contents found: " << ar << endl;
621                 return true;
622         }
623         at = ar[0];
624         return true;
625 }
626
627
628 docstring Parser::parse_verbatim_option()
629 {
630         skipSpaces();
631         docstring res;
632         if (nextToken().character() == '[') {
633                 Token t = getToken();
634                 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
635                         if (t.cat() == catBegin) {
636                                 putback();
637                                 res += '{' + parse_verbatim_item() + '}';
638                         } else
639                                 res += t.asString();
640                 }
641         }
642         return res;
643 }
644
645
646 docstring Parser::parse_verbatim_item()
647 {
648         skipSpaces();
649         docstring res;
650         if (nextToken().cat() == catBegin) {
651                 Token t = getToken();
652                 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
653                         if (t.cat() == catBegin) {
654                                 putback();
655                                 res += '{' + parse_verbatim_item() + '}';
656                         }
657                         else
658                                 res += t.asString();
659                 }
660         }
661         return res;
662 }
663
664
665 MathArray Parser::parse(unsigned flags, mode_type mode)
666 {
667         MathArray ar;
668         parse(ar, flags, mode);
669         return ar;
670 }
671
672
673 void Parser::parse(MathArray & array, unsigned flags, mode_type mode)
674 {
675         InsetMathGrid grid(1, 1);
676         parse1(grid, flags, mode, false);
677         array = grid.cell(0);
678 }
679
680
681 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
682         const bool numbered)
683 {
684         parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
685 }
686
687
688 void Parser::parse1(InsetMathGrid & grid, unsigned flags,
689         const mode_type mode, const bool numbered)
690 {
691         int limits = 0;
692         InsetMathGrid::row_type cellrow = 0;
693         InsetMathGrid::col_type cellcol = 0;
694         MathArray * cell = &grid.cell(grid.index(cellrow, cellcol));
695
696         if (grid.asHullInset())
697                 grid.asHullInset()->numbered(cellrow, numbered);
698
699         //dump();
700         //lyxerr << " flags: " << flags << endl;
701         //lyxerr << " mode: " << mode  << endl;
702         //lyxerr << "grid: " << grid << endl;
703
704         while (good()) {
705                 Token const & t = getToken();
706
707 #ifdef FILEDEBUG
708                 lyxerr << "t: " << t << " flags: " << flags << endl;
709                 lyxerr << "mode: " << mode  << endl;
710                 cell->dump();
711                 lyxerr << endl;
712 #endif
713
714                 if (flags & FLAG_ITEM) {
715
716                         if (t.cat() == catBegin) {
717                                 // skip the brace and collect everything to the next matching
718                                 // closing brace
719                                 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
720                                 return;
721                         }
722
723                         // handle only this single token, leave the loop if done
724                         flags = FLAG_LEAVE;
725                 }
726
727
728                 if (flags & FLAG_BRACED) {
729                         if (t.cat() == catSpace)
730                                 continue;
731
732                         if (t.cat() != catBegin) {
733                                 error("opening brace expected");
734                                 return;
735                         }
736
737                         // skip the brace and collect everything to the next matching
738                         // closing brace
739                         flags = FLAG_BRACE_LAST;
740                 }
741
742
743                 if (flags & FLAG_OPTION) {
744                         if (t.cat() == catOther && t.character() == '[') {
745                                 MathArray ar;
746                                 parse(ar, FLAG_BRACK_LAST, mode);
747                                 cell->append(ar);
748                         } else {
749                                 // no option found, put back token and we are done
750                                 putback();
751                         }
752                         return;
753                 }
754
755                 //
756                 // cat codes
757                 //
758                 if (t.cat() == catMath) {
759                         if (mode != InsetMath::MATH_MODE) {
760                                 // we are inside some text mode thingy, so opening new math is allowed
761                                 Token const & n = getToken();
762                                 if (n.cat() == catMath) {
763                                         // TeX's $$...$$ syntax for displayed math
764                                         cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
765                                         parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
766                                         getToken(); // skip the second '$' token
767                                 } else {
768                                         // simple $...$  stuff
769                                         putback();
770                                         cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
771                                         parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
772                                 }
773                         }
774
775                         else if (flags & FLAG_SIMPLE) {
776                                 // this is the end of the formula
777                                 return;
778                         }
779
780                         else {
781                                 error("something strange in the parser");
782                                 break;
783                         }
784                 }
785
786                 else if (t.cat() == catLetter)
787                         cell->push_back(MathAtom(new InsetMathChar(t.character())));
788
789                 else if (t.cat() == catSpace && mode != InsetMath::MATH_MODE) {
790                         if (cell->empty() || cell->back()->getChar() != ' ')
791                                 cell->push_back(MathAtom(new InsetMathChar(t.character())));
792                 }
793
794                 else if (t.cat() == catNewline && mode != InsetMath::MATH_MODE) {
795                         if (cell->empty() || cell->back()->getChar() != ' ')
796                                 cell->push_back(MathAtom(new InsetMathChar(' ')));
797                 }
798
799                 else if (t.cat() == catParameter) {
800                         Token const & n = getToken();
801                         cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
802                 }
803
804                 else if (t.cat() == catActive)
805                         cell->push_back(MathAtom(new InsetMathChar(t.character())));
806
807                 else if (t.cat() == catBegin) {
808                         MathArray ar;
809                         parse(ar, FLAG_BRACE_LAST, mode);
810                         // do not create a BraceInset if they were written by LyX
811                         // this helps to keep the annoyance of  "a choose b"  to a minimum
812                         if (ar.size() == 1 && ar[0]->extraBraces())
813                                 cell->append(ar);
814                         else
815                                 cell->push_back(MathAtom(new InsetMathBrace(ar)));
816                 }
817
818                 else if (t.cat() == catEnd) {
819                         if (flags & FLAG_BRACE_LAST)
820                                 return;
821                         error("found '}' unexpectedly");
822                         //BOOST_ASSERT(false);
823                         //add(cell, '}', LM_TC_TEX);
824                 }
825
826                 else if (t.cat() == catAlign) {
827                         //lyxerr << " column now " << (cellcol + 1)
828                         //       << " max: " << grid.ncols() << endl;
829                         if (flags & FLAG_ALIGN)
830                                 return;
831                         if (addCol(grid, cellcol))
832                                 cell = &grid.cell(grid.index(cellrow, cellcol));
833                 }
834
835                 else if (t.cat() == catSuper || t.cat() == catSub) {
836                         bool up = (t.cat() == catSuper);
837                         // we need no new script inset if the last thing was a scriptinset,
838                         // which has that script already not the same script already
839                         if (!cell->size())
840                                 cell->push_back(MathAtom(new InsetMathScript(up)));
841                         else if (cell->back()->asScriptInset() &&
842                                         !cell->back()->asScriptInset()->has(up))
843                                 cell->back().nucleus()->asScriptInset()->ensure(up);
844                         else if (cell->back()->asScriptInset())
845                                 cell->push_back(MathAtom(new InsetMathScript(up)));
846                         else
847                                 cell->back() = MathAtom(new InsetMathScript(cell->back(), up));
848                         InsetMathScript * p = cell->back().nucleus()->asScriptInset();
849                         // special handling of {}-bases
850                         // Here we could remove the brace inset for things
851                         // like {a'}^2 and add the braces back in
852                         // InsetMathScript::write().
853                         // We do not do it, since it is not possible to detect
854                         // reliably whether the braces are needed because the
855                         // nucleus contains more than one symbol, or whether
856                         // they are needed for unknown commands like \xx{a}_0
857                         // or \yy{a}{b}_0. This was done in revision 14819
858                         // in an unreliable way. See this thread
859                         // http://www.mail-archive.com/lyx-devel%40lists.lyx.org/msg104917.html
860                         // for more details.
861                         parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
862                         if (limits) {
863                                 p->limits(limits);
864                                 limits = 0;
865                         }
866                 }
867
868                 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
869                         //lyxerr << "finished reading option" << endl;
870                         return;
871                 }
872
873                 else if (t.cat() == catOther)
874                         cell->push_back(MathAtom(new InsetMathChar(t.character())));
875
876                 else if (t.cat() == catComment) {
877                         docstring s;
878                         while (good()) {
879                                 Token const & t = getToken();
880                                 if (t.cat() == catNewline)
881                                         break;
882                                 s += t.asString();
883                         }
884                         cell->push_back(MathAtom(new InsetMathComment(s)));
885                         skipSpaces();
886                 }
887
888                 //
889                 // control sequences
890                 //
891
892                 else if (t.cs() == "lyxlock") {
893                         if (cell->size())
894                                 cell->back().nucleus()->lock(true);
895                 }
896
897                 else if (t.cs() == "def" ||
898                         t.cs() == "newcommand" ||
899                         t.cs() == "renewcommand")
900                 {
901                         docstring const type = t.cs();
902                         docstring name;
903                         int nargs = 0;
904                         if (t.cs() == "def") {
905                                 // get name
906                                 name = getToken().cs();
907
908                                 // read parameter
909                                 docstring pars;
910                                 while (good() && nextToken().cat() != catBegin) {
911                                         pars += getToken().cs();
912                                         ++nargs;
913                                 }
914                                 nargs /= 2;
915                                 //lyxerr << "read \\def parameter list '" << pars << "'" << endl;
916
917                         } else { // t.cs() == "newcommand" || t.cs() == "renewcommand"
918
919                                 if (getToken().cat() != catBegin) {
920                                         error("'{' in \\newcommand expected (1) ");
921                                         return;
922                                 }
923
924                                 name = getToken().cs();
925
926                                 if (getToken().cat() != catEnd) {
927                                         error("'}' in \\newcommand expected");
928                                         return;
929                                 }
930
931                                 docstring const arg = getArg('[', ']');
932                                 if (!arg.empty())
933                                         nargs = convert<int>(arg);
934
935                         }
936
937                         MathArray ar1;
938                         parse(ar1, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
939
940                         // we cannot handle recursive stuff at all
941                         //MathArray test;
942                         //test.push_back(createInsetMath(name));
943                         //if (ar1.contains(test)) {
944                         //      error("we cannot handle recursive macros at all.");
945                         //      return;
946                         //}
947
948                         // is a version for display attached?
949                         skipSpaces();
950                         MathArray ar2;
951                         if (nextToken().cat() == catBegin)
952                                 parse(ar2, FLAG_ITEM, InsetMath::MATH_MODE);
953
954                         cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, type,
955                                 ar1, ar2)));
956                 }
957
958                 else if (t.cs() == "(") {
959                         cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
960                         parse2(cell->back(), FLAG_SIMPLE2, InsetMath::MATH_MODE, false);
961                 }
962
963                 else if (t.cs() == "[") {
964                         cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
965                         parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false);
966                 }
967
968                 else if (t.cs() == "protect")
969                         // ignore \\protect, will hopefully be re-added during output
970                         ;
971
972                 else if (t.cs() == "end") {
973                         if (flags & FLAG_END) {
974                                 // eat environment name
975                                 docstring const name = getArg('{', '}');
976                                 if (environments_.empty())
977                                         error("'found \\end{" + name +
978                                               "}' without matching '\\begin{" +
979                                               name + "}'");
980                                 else if (name != environments_.back())
981                                         error("'\\end{" + name +
982                                               "}' does not match '\\begin{" +
983                                               environments_.back() + "}'");
984                                 else {
985                                         environments_.pop_back();
986                                         // Delete empty last row in matrix
987                                         // like insets.
988                                         // If you abuse InsetMathGrid for
989                                         // non-matrix like structures you
990                                         // probably need to refine this test.
991                                         // Right now we only have to test for
992                                         // single line hull insets.
993                                         if (grid.nrows() > 1)
994                                                 delEmptyLastRow(grid);
995                                         return;
996                                 }
997                         } else
998                                 error("found 'end' unexpectedly");
999                 }
1000
1001                 else if (t.cs() == ")") {
1002                         if (flags & FLAG_SIMPLE2)
1003                                 return;
1004                         error("found '\\)' unexpectedly");
1005                 }
1006
1007                 else if (t.cs() == "]") {
1008                         if (flags & FLAG_EQUATION)
1009                                 return;
1010                         error("found '\\]' unexpectedly");
1011                 }
1012
1013                 else if (t.cs() == "\\") {
1014                         if (flags & FLAG_ALIGN)
1015                                 return;
1016                         if (addRow(grid, cellrow, getArg('[', ']'))) {
1017                                 cellcol = 0;
1018                                 if (grid.asHullInset())
1019                                         grid.asHullInset()->numbered(
1020                                                         cellrow, numbered);
1021                                 cell = &grid.cell(grid.index(cellrow,
1022                                                              cellcol));
1023                         }
1024                 }
1025
1026 #if 0
1027                 else if (t.cs() == "multicolumn") {
1028                         // extract column count and insert dummy cells
1029                         MathArray count;
1030                         parse(count, FLAG_ITEM, mode);
1031                         int cols = 1;
1032                         if (!extractNumber(count, cols)) {
1033                                 lyxerr << " can't extract number of cells from " << count << endl;
1034                         }
1035                         // resize the table if necessary
1036                         for (int i = 0; i < cols; ++i) {
1037                                 if (addCol(grid, cellcol)) {
1038                                         cell = &grid.cell(grid.index(
1039                                                         cellrow, cellcol));
1040                                         // mark this as dummy
1041                                         grid.cellinfo(grid.index(
1042                                                 cellrow, cellcol)).dummy_ = true;
1043                                 }
1044                         }
1045                         // the last cell is the real thing, not a dummy
1046                         grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
1047
1048                         // read special alignment
1049                         MathArray align;
1050                         parse(align, FLAG_ITEM, mode);
1051                         //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
1052
1053                         // parse the remaining contents into the "real" cell
1054                         parse(*cell, FLAG_ITEM, mode);
1055                 }
1056 #endif
1057
1058                 else if (t.cs() == "limits")
1059                         limits = 1;
1060
1061                 else if (t.cs() == "nolimits")
1062                         limits = -1;
1063
1064                 else if (t.cs() == "nonumber") {
1065                         if (grid.asHullInset())
1066                                 grid.asHullInset()->numbered(cellrow, false);
1067                 }
1068
1069                 else if (t.cs() == "number") {
1070                         if (grid.asHullInset())
1071                                 grid.asHullInset()->numbered(cellrow, true);
1072                 }
1073
1074                 else if (t.cs() == "hline") {
1075                         grid.rowinfo(cellrow).lines_ ++;
1076                 }
1077
1078                 else if (t.cs() == "sqrt") {
1079                         MathArray ar;
1080                         parse(ar, FLAG_OPTION, mode);
1081                         if (ar.size()) {
1082                                 cell->push_back(MathAtom(new InsetMathRoot));
1083                                 cell->back().nucleus()->cell(0) = ar;
1084                                 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1085                         } else {
1086                                 cell->push_back(MathAtom(new InsetMathSqrt));
1087                                 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1088                         }
1089                 }
1090
1091                 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1092                         cell->push_back(createInsetMath(t.cs()));
1093                         parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1094                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1095                 }
1096
1097                 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
1098                                 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1099                         cell->push_back(MathAtom(new RefInset(t.cs())));
1100                         parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1101                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1102                 }
1103
1104                 else if (t.cs() == "left") {
1105                         skipSpaces();
1106                         Token const & tl = getToken();
1107                         // \| and \Vert are equivalent, and InsetMathDelim
1108                         // can't handle \|
1109                         // FIXME: fix this in InsetMathDelim itself!
1110                         docstring const l = tl.cs() == "|" ? from_ascii("Vert") : tl.asString();
1111                         MathArray ar;
1112                         parse(ar, FLAG_RIGHT, mode);
1113                         skipSpaces();
1114                         Token const & tr = getToken();
1115                         docstring const r = tr.cs() == "|" ? from_ascii("Vert") : tr.asString();
1116                         cell->push_back(MathAtom(new InsetMathDelim(l, r, ar)));
1117                 }
1118
1119                 else if (t.cs() == "right") {
1120                         if (flags & FLAG_RIGHT)
1121                                 return;
1122                         //lyxerr << "got so far: '" << cell << "'" << endl;
1123                         error("Unmatched right delimiter");
1124                         return;
1125                 }
1126
1127                 else if (t.cs() == "begin") {
1128                         docstring const name = getArg('{', '}');
1129                         environments_.push_back(name);
1130
1131                         if (name == "array" || name == "subarray") {
1132                                 docstring const valign = parse_verbatim_option() + 'c';
1133                                 docstring const halign = parse_verbatim_item();
1134                                 cell->push_back(MathAtom(new InsetMathArray(name, (char)valign[0], halign)));
1135                                 parse2(cell->back(), FLAG_END, mode, false);
1136                         }
1137
1138                         else if (name == "tabular") {
1139                                 docstring const valign = parse_verbatim_option() + 'c';
1140                                 docstring const halign = parse_verbatim_item();
1141                                 cell->push_back(MathAtom(new InsetMathTabular(name, (char)valign[0], halign)));
1142                                 parse2(cell->back(), FLAG_END, InsetMath::TEXT_MODE, false);
1143                         }
1144
1145                         else if (name == "split" || name == "cases") {
1146                                 cell->push_back(createInsetMath(name));
1147                                 parse2(cell->back(), FLAG_END, mode, false);
1148                         }
1149
1150                         else if (name == "alignedat") {
1151                                 docstring const valign = parse_verbatim_option() + 'c';
1152                                 // ignore this for a while
1153                                 getArg('{', '}');
1154                                 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1155                                 parse2(cell->back(), FLAG_END, mode, false);
1156                         }
1157
1158                         else if (name == "math") {
1159                                 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
1160                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, true);
1161                         }
1162
1163                         else if (name == "equation" || name == "equation*"
1164                                         || name == "displaymath") {
1165                                 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
1166                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation"));
1167                         }
1168
1169                         else if (name == "eqnarray" || name == "eqnarray*") {
1170                                 cell->push_back(MathAtom(new InsetMathHull(hullEqnArray)));
1171                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1172                         }
1173
1174                         else if (name == "align" || name == "align*") {
1175                                 cell->push_back(MathAtom(new InsetMathHull(hullAlign)));
1176                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1177                         }
1178
1179                         else if (name == "flalign" || name == "flalign*") {
1180                                 cell->push_back(MathAtom(new InsetMathHull(hullFlAlign)));
1181                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1182                         }
1183
1184                         else if (name == "alignat" || name == "alignat*") {
1185                                 // ignore this for a while
1186                                 getArg('{', '}');
1187                                 cell->push_back(MathAtom(new InsetMathHull(hullAlignAt)));
1188                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1189                         }
1190
1191                         else if (name == "xalignat" || name == "xalignat*") {
1192                                 // ignore this for a while
1193                                 getArg('{', '}');
1194                                 cell->push_back(MathAtom(new InsetMathHull(hullXAlignAt)));
1195                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1196                         }
1197
1198                         else if (name == "xxalignat") {
1199                                 // ignore this for a while
1200                                 getArg('{', '}');
1201                                 cell->push_back(MathAtom(new InsetMathHull(hullXXAlignAt)));
1202                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1203                         }
1204
1205                         else if (name == "multline" || name == "multline*") {
1206                                 cell->push_back(MathAtom(new InsetMathHull(hullMultline)));
1207                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1208                         }
1209
1210                         else if (name == "gather" || name == "gather*") {
1211                                 cell->push_back(MathAtom(new InsetMathHull(hullGather)));
1212                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1213                         }
1214
1215                         else if (latexkeys const * l = in_word_set(name)) {
1216                                 if (l->inset == "matrix") {
1217                                         cell->push_back(createInsetMath(name));
1218                                         parse2(cell->back(), FLAG_END, mode, false);
1219                                 } else if (l->inset == "split") {
1220                                         docstring const valign = parse_verbatim_option() + 'c';
1221                                         cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1222                                         parse2(cell->back(), FLAG_END, mode, false);
1223                                 } else {
1224                                         dump();
1225                                         lyxerr << "found math environment `" << name
1226                                                << "' in symbols file with unsupported inset `"
1227                                                << l->inset << "'." << endl;
1228                                         // create generic environment inset
1229                                         cell->push_back(MathAtom(new InsetMathEnv(name)));
1230                                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1231                                 }
1232                         }
1233
1234                         else {
1235                                 dump();
1236                                 lyxerr << "found unknown math environment '" << name << "'" << endl;
1237                                 // create generic environment inset
1238                                 cell->push_back(MathAtom(new InsetMathEnv(name)));
1239                                 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1240                         }
1241                 }
1242
1243                 else if (t.cs() == "kern") {
1244 #ifdef WITH_WARNINGS
1245 #warning A hack...
1246 #endif
1247                         docstring s;
1248                         while (true) {
1249                                 Token const & t = getToken();
1250                                 if (!good()) {
1251                                         putback();
1252                                         break;
1253                                 }
1254                                 s += t.character();
1255                                 if (isValidLength(to_utf8(s)))
1256                                         break;
1257                         }
1258                         cell->push_back(MathAtom(new InsetMathKern(s)));
1259                 }
1260
1261                 else if (t.cs() == "label") {
1262                         // FIXME: This is swallowed in inline formulas
1263                         docstring label = parse_verbatim_item();
1264                         MathArray ar;
1265                         asArray(label, ar);
1266                         if (grid.asHullInset()) {
1267                                 grid.asHullInset()->label(cellrow, label);
1268                         } else {
1269                                 cell->push_back(createInsetMath(t.cs()));
1270                                 cell->push_back(MathAtom(new InsetMathBrace(ar)));
1271                         }
1272                 }
1273
1274                 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1275                         MathAtom at = createInsetMath(t.cs());
1276                         at.nucleus()->cell(0) = *cell;
1277                         cell->clear();
1278                         parse(at.nucleus()->cell(1), flags, mode);
1279                         cell->push_back(at);
1280                         return;
1281                 }
1282
1283                 else if (t.cs() == "color") {
1284                         docstring const color = parse_verbatim_item();
1285                         cell->push_back(MathAtom(new InsetMathColor(true, color)));
1286                         parse(cell->back().nucleus()->cell(0), flags, mode);
1287                         return;
1288                 }
1289
1290                 else if (t.cs() == "textcolor") {
1291                         docstring const color = parse_verbatim_item();
1292                         cell->push_back(MathAtom(new InsetMathColor(false, color)));
1293                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1294                 }
1295
1296                 else if (t.cs() == "normalcolor") {
1297                         cell->push_back(createInsetMath(t.cs()));
1298                         parse(cell->back().nucleus()->cell(0), flags, mode);
1299                         return;
1300                 }
1301
1302                 else if (t.cs() == "substack") {
1303                         cell->push_back(createInsetMath(t.cs()));
1304                         parse2(cell->back(), FLAG_ITEM, mode, false);
1305                 }
1306
1307                 else if (t.cs() == "xymatrix") {
1308                         odocstringstream os;
1309                         while (good() && nextToken().cat() != catBegin)
1310                                 os << getToken().asInput();
1311                         cell->push_back(createInsetMath(t.cs() + os.str()));
1312                         parse2(cell->back(), FLAG_ITEM, mode, false);
1313                 }
1314
1315                 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1316                         cell->push_back(createInsetMath(t.cs()));
1317                         parse(cell->back().nucleus()->cell(0), FLAG_OPTION, InsetMath::TEXT_MODE);
1318                         parse(cell->back().nucleus()->cell(1), FLAG_OPTION, InsetMath::TEXT_MODE);
1319                         parse(cell->back().nucleus()->cell(2), FLAG_ITEM, InsetMath::TEXT_MODE);
1320                 }
1321
1322                 else if (t.cs() == "tag") {
1323                         if (nextToken().character() == '*') {
1324                                 getToken();
1325                                 cell->push_back(createInsetMath(t.cs() + '*'));
1326                         } else
1327                                 cell->push_back(createInsetMath(t.cs()));
1328                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1329                 }
1330
1331 #if 0
1332                 else if (t.cs() == "infer") {
1333                         MathArray ar;
1334                         parse(ar, FLAG_OPTION, mode);
1335                         cell->push_back(createInsetMath(t.cs()));
1336                         parse2(cell->back(), FLAG_ITEM, mode, false);
1337                 }
1338
1339                 // Disabled
1340                 else if (1 && t.cs() == "ar") {
1341                         auto_ptr<InsetMathXYArrow> p(new InsetMathXYArrow);
1342                         // try to read target
1343                         parse(p->cell(0), FLAG_OTPTION, mode);
1344                         // try to read label
1345                         if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1346                                 p->up_ = nextToken().cat() == catSuper;
1347                                 getToken();
1348                                 parse(p->cell(1), FLAG_ITEM, mode);
1349                                 //lyxerr << "read label: " << p->cell(1) << endl;
1350                         }
1351
1352                         cell->push_back(MathAtom(p.release()));
1353                         //lyxerr << "read cell: " << cell << endl;
1354                 }
1355 #endif
1356
1357                 else if (t.cs().size()) {
1358                         latexkeys const * l = in_word_set(t.cs());
1359                         if (l) {
1360                                 if (l->inset == "big") {
1361                                         skipSpaces();
1362                                         docstring const delim = getToken().asInput();
1363                                         if (InsetMathBig::isBigInsetDelim(delim))
1364                                                 cell->push_back(MathAtom(
1365                                                         new InsetMathBig(t.cs(), delim)));
1366                                         else {
1367                                                 cell->push_back(createInsetMath(t.cs()));
1368                                                 putback();
1369                                         }
1370                                 }
1371
1372                                 else if (l->inset == "font") {
1373                                         cell->push_back(createInsetMath(t.cs()));
1374                                         parse(cell->back().nucleus()->cell(0),
1375                                                 FLAG_ITEM, asMode(mode, l->extra));
1376                                 }
1377
1378                                 else if (l->inset == "oldfont") {
1379                                         cell->push_back(createInsetMath(t.cs()));
1380                                         parse(cell->back().nucleus()->cell(0),
1381                                                 flags | FLAG_ALIGN, asMode(mode, l->extra));
1382                                         if (prevToken().cat() != catAlign &&
1383                                             prevToken().cs() != "\\")
1384                                                 return;
1385                                         putback();
1386                                 }
1387
1388                                 else if (l->inset == "style") {
1389                                         cell->push_back(createInsetMath(t.cs()));
1390                                         parse(cell->back().nucleus()->cell(0),
1391                                                 flags | FLAG_ALIGN, mode);
1392                                         if (prevToken().cat() != catAlign &&
1393                                             prevToken().cs() != "\\")
1394                                                 return;
1395                                         putback();
1396                                 }
1397
1398                                 else {
1399                                         MathAtom at = createInsetMath(t.cs());
1400                                         for (InsetMath::idx_type i = 0; i < at->nargs(); ++i)
1401                                                 parse(at.nucleus()->cell(i),
1402                                                         FLAG_ITEM, asMode(mode, l->extra));
1403                                         cell->push_back(at);
1404                                 }
1405                         }
1406
1407                         else {
1408                                 MathAtom at = createInsetMath(t.cs());
1409                                 InsetMath::mode_type m = mode;
1410                                 //if (m == InsetMath::UNDECIDED_MODE)
1411                                 //lyxerr << "default creation: m1: " << m << endl;
1412                                 if (at->currentMode() != InsetMath::UNDECIDED_MODE)
1413                                         m = at->currentMode();
1414                                 //lyxerr << "default creation: m2: " << m << endl;
1415                                 InsetMath::idx_type start = 0;
1416                                 // this fails on \bigg[...\bigg]
1417                                 //MathArray opt;
1418                                 //parse(opt, FLAG_OPTION, InsetMath::VERBATIM_MODE);
1419                                 //if (opt.size()) {
1420                                 //      start = 1;
1421                                 //      at.nucleus()->cell(0) = opt;
1422                                 //}
1423                                 for (InsetMath::idx_type i = start; i < at->nargs(); ++i) {
1424                                         parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1425                                         skipSpaces();
1426                                 }
1427                                 cell->push_back(at);
1428                         }
1429                 }
1430
1431
1432                 if (flags & FLAG_LEAVE) {
1433                         flags &= ~FLAG_LEAVE;
1434                         break;
1435                 }
1436         }
1437 }
1438
1439
1440
1441 } // anonymous namespace
1442
1443
1444 void mathed_parse_cell(MathArray & ar, docstring const & str)
1445 {
1446         Parser(str).parse(ar, 0, InsetMath::MATH_MODE);
1447 }
1448
1449
1450 void mathed_parse_cell(MathArray & ar, istream & is)
1451 {
1452         Parser(is).parse(ar, 0, InsetMath::MATH_MODE);
1453 }
1454
1455
1456 bool mathed_parse_normal(MathAtom & t, docstring const & str)
1457 {
1458         return Parser(str).parse(t);
1459 }
1460
1461
1462 bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
1463 {
1464         return Parser(lex).parse(t);
1465 }
1466
1467
1468 void mathed_parse_normal(InsetMathGrid & grid, docstring const & str)
1469 {
1470         Parser(str).parse1(grid, 0, InsetMath::MATH_MODE, false);
1471 }
1472
1473
1474 void initParser()
1475 {
1476         fill(theCatcode, theCatcode + 128, catOther);
1477         fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1478         fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1479
1480         theCatcode[int('\\')] = catEscape;
1481         theCatcode[int('{')]  = catBegin;
1482         theCatcode[int('}')]  = catEnd;
1483         theCatcode[int('$')]  = catMath;
1484         theCatcode[int('&')]  = catAlign;
1485         theCatcode[int('\n')] = catNewline;
1486         theCatcode[int('#')]  = catParameter;
1487         theCatcode[int('^')]  = catSuper;
1488         theCatcode[int('_')]  = catSub;
1489         theCatcode[int(0x7f)] = catIgnore;
1490         theCatcode[int(' ')]  = catSpace;
1491         theCatcode[int('\t')] = catSpace;
1492         theCatcode[int('\r')] = catNewline;
1493         theCatcode[int('~')]  = catActive;
1494         theCatcode[int('%')]  = catComment;
1495 }
1496
1497
1498 } // namespace lyx