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