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