]> git.lyx.org Git - lyx.git/blob - src/mathed/MathParser.C
Fix comment according to Enricos explanation
[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                         // Test for empty brace inset, otherwise \xxx{\vec{H}}_{0}
831                         // where \xxx is an unknown command gets misparsed to
832                         // \xxx\vec{H}_{0}, and that is invalid LaTeX.
833                         // is this always correct?
834                         if (p->nuc().size() == 1 &&
835                             p->nuc().back()->asBraceInset() &&
836                             p->nuc().back()->asBraceInset()->cell(0).empty())
837                                 p->nuc() = p->nuc().back()->asNestInset()->cell(0);
838                         parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
839                         if (limits) {
840                                 p->limits(limits);
841                                 limits = 0;
842                         }
843                 }
844
845                 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
846                         //lyxerr << "finished reading option" << endl;
847                         return;
848                 }
849
850                 else if (t.cat() == catOther)
851                         cell->push_back(MathAtom(new InsetMathChar(t.character())));
852
853                 else if (t.cat() == catComment) {
854                         docstring s;
855                         while (good()) {
856                                 Token const & t = getToken();
857                                 if (t.cat() == catNewline)
858                                         break;
859                                 s += t.asString();
860                         }
861                         cell->push_back(MathAtom(new InsetMathComment(s)));
862                         skipSpaces();
863                 }
864
865                 //
866                 // control sequences
867                 //
868
869                 else if (t.cs() == "lyxlock") {
870                         if (cell->size())
871                                 cell->back().nucleus()->lock(true);
872                 }
873
874                 else if (t.cs() == "def" ||
875                         t.cs() == "newcommand" ||
876                         t.cs() == "renewcommand")
877                 {
878                         docstring const type = t.cs();
879                         docstring name;
880                         int nargs = 0;
881                         if (t.cs() == "def") {
882                                 // get name
883                                 name = getToken().cs();
884
885                                 // read parameter
886                                 docstring pars;
887                                 while (good() && nextToken().cat() != catBegin) {
888                                         pars += getToken().cs();
889                                         ++nargs;
890                                 }
891                                 nargs /= 2;
892                                 //lyxerr << "read \\def parameter list '" << pars << "'" << endl;
893
894                         } else { // t.cs() == "newcommand" || t.cs() == "renewcommand"
895
896                                 if (getToken().cat() != catBegin) {
897                                         error("'{' in \\newcommand expected (1) ");
898                                         return;
899                                 }
900
901                                 name = getToken().cs();
902
903                                 if (getToken().cat() != catEnd) {
904                                         error("'}' in \\newcommand expected");
905                                         return;
906                                 }
907
908                                 docstring const arg  = getArg('[', ']');
909                                 if (!arg.empty())
910                                         nargs = convert<int>(arg);
911
912                         }
913
914                         MathArray ar1;
915                         parse(ar1, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
916
917                         // we cannot handle recursive stuff at all
918                         //MathArray test;
919                         //test.push_back(createInsetMath(name));
920                         //if (ar1.contains(test)) {
921                         //      error("we cannot handle recursive macros at all.");
922                         //      return;
923                         //}
924
925                         // is a version for display attached?
926                         skipSpaces();
927                         MathArray ar2;
928                         if (nextToken().cat() == catBegin)
929                                 parse(ar2, FLAG_ITEM, InsetMath::MATH_MODE);
930
931                         cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, type,
932                                 ar1, ar2)));
933                 }
934
935                 else if (t.cs() == "(") {
936                         cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
937                         parse2(cell->back(), FLAG_SIMPLE2, InsetMath::MATH_MODE, false);
938                 }
939
940                 else if (t.cs() == "[") {
941                         cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
942                         parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false);
943                 }
944
945                 else if (t.cs() == "protect")
946                         // ignore \\protect, will hopefully be re-added during output
947                         ;
948
949                 else if (t.cs() == "end") {
950                         if (flags & FLAG_END) {
951                                 // eat environment name
952                                 docstring const name = getArg('{', '}');
953                                 if (environments_.empty())
954                                         error("'found \\end{" + name +
955                                               "}' without matching '\\begin{" +
956                                               name + "}'");
957                                 else if (name != environments_.back())
958                                         error("'\\end{" + name +
959                                               "}' does not match '\\begin{" +
960                                               environments_.back() + "}'");
961                                 else {
962                                         environments_.pop_back();
963                                         // Delete empty last row in matrix
964                                         // like insets.
965                                         // If you abuse InsetMathGrid for
966                                         // non-matrix like structures you
967                                         // probably need to refine this test.
968                                         // Right now we only have to test for
969                                         // single line hull insets.
970                                         if (grid.nrows() > 1)
971                                                 delEmptyLastRow(grid);
972                                         return;
973                                 }
974                         } else
975                                 error("found 'end' unexpectedly");
976                 }
977
978                 else if (t.cs() == ")") {
979                         if (flags & FLAG_SIMPLE2)
980                                 return;
981                         error("found '\\)' unexpectedly");
982                 }
983
984                 else if (t.cs() == "]") {
985                         if (flags & FLAG_EQUATION)
986                                 return;
987                         error("found '\\]' unexpectedly");
988                 }
989
990                 else if (t.cs() == "\\") {
991                         if (flags & FLAG_ALIGN)
992                                 return;
993                         if (addRow(grid, cellrow, getArg('[', ']'))) {
994                                 cellcol = 0;
995                                 if (grid.asHullInset())
996                                         grid.asHullInset()->numbered(
997                                                         cellrow, numbered);
998                                 cell = &grid.cell(grid.index(cellrow,
999                                                              cellcol));
1000                         }
1001                 }
1002
1003 #if 0
1004                 else if (t.cs() == "multicolumn") {
1005                         // extract column count and insert dummy cells
1006                         MathArray count;
1007                         parse(count, FLAG_ITEM, mode);
1008                         int cols = 1;
1009                         if (!extractNumber(count, cols)) {
1010                                 lyxerr << " can't extract number of cells from " << count << endl;
1011                         }
1012                         // resize the table if necessary
1013                         for (int i = 0; i < cols; ++i) {
1014                                 if (addCol(grid, cellcol)) {
1015                                         cell = &grid.cell(grid.index(
1016                                                         cellrow, cellcol));
1017                                         // mark this as dummy
1018                                         grid.cellinfo(grid.index(
1019                                                 cellrow, cellcol)).dummy_ = true;
1020                                 }
1021                         }
1022                         // the last cell is the real thing, not a dummy
1023                         grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
1024
1025                         // read special alignment
1026                         MathArray align;
1027                         parse(align, FLAG_ITEM, mode);
1028                         //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
1029
1030                         // parse the remaining contents into the "real" cell
1031                         parse(*cell, FLAG_ITEM, mode);
1032                 }
1033 #endif
1034
1035                 else if (t.cs() == "limits")
1036                         limits = 1;
1037
1038                 else if (t.cs() == "nolimits")
1039                         limits = -1;
1040
1041                 else if (t.cs() == "nonumber") {
1042                         if (grid.asHullInset())
1043                                 grid.asHullInset()->numbered(cellrow, false);
1044                 }
1045
1046                 else if (t.cs() == "number") {
1047                         if (grid.asHullInset())
1048                                 grid.asHullInset()->numbered(cellrow, true);
1049                 }
1050
1051                 else if (t.cs() == "hline") {
1052                         grid.rowinfo(cellrow).lines_ ++;
1053                 }
1054
1055                 else if (t.cs() == "sqrt") {
1056                         MathArray ar;
1057                         parse(ar, FLAG_OPTION, mode);
1058                         if (ar.size()) {
1059                                 cell->push_back(MathAtom(new InsetMathRoot));
1060                                 cell->back().nucleus()->cell(0) = ar;
1061                                 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1062                         } else {
1063                                 cell->push_back(MathAtom(new InsetMathSqrt));
1064                                 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1065                         }
1066                 }
1067
1068                 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1069                         cell->push_back(createInsetMath(t.cs()));
1070                         parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1071                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1072                 }
1073
1074                 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
1075                                 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1076                         cell->push_back(MathAtom(new RefInset(t.cs())));
1077                         parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1078                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1079                 }
1080
1081                 else if (t.cs() == "left") {
1082                         skipSpaces();
1083                         Token const & tl = getToken();
1084                         // \| and \Vert are equivalent, and InsetMathDelim
1085                         // can't handle \|
1086                         // FIXME: fix this in InsetMathDelim itself!
1087                         docstring const l = tl.cs() == "|" ? from_ascii("Vert") : tl.asString();
1088                         MathArray ar;
1089                         parse(ar, FLAG_RIGHT, mode);
1090                         skipSpaces();
1091                         Token const & tr = getToken();
1092                         docstring const r = tr.cs() == "|" ? from_ascii("Vert") : tr.asString();
1093                         cell->push_back(MathAtom(new InsetMathDelim(l, r, ar)));
1094                 }
1095
1096                 else if (t.cs() == "right") {
1097                         if (flags & FLAG_RIGHT)
1098                                 return;
1099                         //lyxerr << "got so far: '" << cell << "'" << endl;
1100                         error("Unmatched right delimiter");
1101                         return;
1102                 }
1103
1104                 else if (t.cs() == "begin") {
1105                         docstring const name = getArg('{', '}');
1106                         environments_.push_back(name);
1107
1108                         if (name == "array" || name == "subarray") {
1109                                 docstring const valign = parse_verbatim_option() + 'c';
1110                                 docstring const halign = parse_verbatim_item();
1111                                 cell->push_back(MathAtom(new InsetMathArray(name, (char)valign[0], halign)));
1112                                 parse2(cell->back(), FLAG_END, mode, false);
1113                         }
1114
1115                         else if (name == "tabular") {
1116                                 docstring const valign = parse_verbatim_option() + 'c';
1117                                 docstring const halign = parse_verbatim_item();
1118                                 cell->push_back(MathAtom(new InsetMathTabular(name, (char)valign[0], halign)));
1119                                 parse2(cell->back(), FLAG_END, InsetMath::TEXT_MODE, false);
1120                         }
1121
1122                         else if (name == "split" || name == "cases") {
1123                                 cell->push_back(createInsetMath(name));
1124                                 parse2(cell->back(), FLAG_END, mode, false);
1125                         }
1126
1127                         else if (name == "alignedat") {
1128                                 docstring const valign = parse_verbatim_option() + 'c';
1129                                 // ignore this for a while
1130                                 getArg('{', '}');
1131                                 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1132                                 parse2(cell->back(), FLAG_END, mode, false);
1133                         }
1134
1135                         else if (name == "math") {
1136                                 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
1137                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, true);
1138                         }
1139
1140                         else if (name == "equation" || name == "equation*"
1141                                         || name == "displaymath") {
1142                                 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
1143                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation"));
1144                         }
1145
1146                         else if (name == "eqnarray" || name == "eqnarray*") {
1147                                 cell->push_back(MathAtom(new InsetMathHull(hullEqnArray)));
1148                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1149                         }
1150
1151                         else if (name == "align" || name == "align*") {
1152                                 cell->push_back(MathAtom(new InsetMathHull(hullAlign)));
1153                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1154                         }
1155
1156                         else if (name == "flalign" || name == "flalign*") {
1157                                 cell->push_back(MathAtom(new InsetMathHull(hullFlAlign)));
1158                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1159                         }
1160
1161                         else if (name == "alignat" || name == "alignat*") {
1162                                 // ignore this for a while
1163                                 getArg('{', '}');
1164                                 cell->push_back(MathAtom(new InsetMathHull(hullAlignAt)));
1165                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1166                         }
1167
1168                         else if (name == "xalignat" || name == "xalignat*") {
1169                                 // ignore this for a while
1170                                 getArg('{', '}');
1171                                 cell->push_back(MathAtom(new InsetMathHull(hullXAlignAt)));
1172                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1173                         }
1174
1175                         else if (name == "xxalignat") {
1176                                 // ignore this for a while
1177                                 getArg('{', '}');
1178                                 cell->push_back(MathAtom(new InsetMathHull(hullXXAlignAt)));
1179                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1180                         }
1181
1182                         else if (name == "multline" || name == "multline*") {
1183                                 cell->push_back(MathAtom(new InsetMathHull(hullMultline)));
1184                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1185                         }
1186
1187                         else if (name == "gather" || name == "gather*") {
1188                                 cell->push_back(MathAtom(new InsetMathHull(hullGather)));
1189                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1190                         }
1191
1192                         else if (latexkeys const * l = in_word_set(name)) {
1193                                 if (l->inset == "matrix") {
1194                                         cell->push_back(createInsetMath(name));
1195                                         parse2(cell->back(), FLAG_END, mode, false);
1196                                 } else if (l->inset == "split") {
1197                                         docstring const valign = parse_verbatim_option() + 'c';
1198                                         cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1199                                         parse2(cell->back(), FLAG_END, mode, false);
1200                                 } else {
1201                                         dump();
1202                                         lyxerr << "found math environment `" << name
1203                                                << "' in symbols file with unsupported inset `"
1204                                                << l->inset << "'." << endl;
1205                                         // create generic environment inset
1206                                         cell->push_back(MathAtom(new InsetMathEnv(name)));
1207                                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1208                                 }
1209                         }
1210
1211                         else {
1212                                 dump();
1213                                 lyxerr << "found unknown math environment '" << name << "'" << endl;
1214                                 // create generic environment inset
1215                                 cell->push_back(MathAtom(new InsetMathEnv(name)));
1216                                 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1217                         }
1218                 }
1219
1220                 else if (t.cs() == "kern") {
1221 #ifdef WITH_WARNINGS
1222 #warning A hack...
1223 #endif
1224                         docstring s;
1225                         while (true) {
1226                                 Token const & t = getToken();
1227                                 if (!good()) {
1228                                         putback();
1229                                         break;
1230                                 }
1231                                 s += t.character();
1232                                 if (isValidLength(to_utf8(s)))
1233                                         break;
1234                         }
1235                         cell->push_back(MathAtom(new InsetMathKern(s)));
1236                 }
1237
1238                 else if (t.cs() == "label") {
1239                         // FIXME: This is swallowed in inline formulas
1240                         docstring label = parse_verbatim_item();
1241                         MathArray ar;
1242                         asArray(label, ar);
1243                         if (grid.asHullInset()) {
1244                                 grid.asHullInset()->label(cellrow, label);
1245                         } else {
1246                                 cell->push_back(createInsetMath(t.cs()));
1247                                 cell->push_back(MathAtom(new InsetMathBrace(ar)));
1248                         }
1249                 }
1250
1251                 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1252                         MathAtom at = createInsetMath(t.cs());
1253                         at.nucleus()->cell(0) = *cell;
1254                         cell->clear();
1255                         parse(at.nucleus()->cell(1), flags, mode);
1256                         cell->push_back(at);
1257                         return;
1258                 }
1259
1260                 else if (t.cs() == "color") {
1261                         docstring const color = parse_verbatim_item();
1262                         cell->push_back(MathAtom(new InsetMathColor(true, color)));
1263                         parse(cell->back().nucleus()->cell(0), flags, mode);
1264                         return;
1265                 }
1266
1267                 else if (t.cs() == "textcolor") {
1268                         docstring const color = parse_verbatim_item();
1269                         cell->push_back(MathAtom(new InsetMathColor(false, color)));
1270                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1271                 }
1272
1273                 else if (t.cs() == "normalcolor") {
1274                         cell->push_back(createInsetMath(t.cs()));
1275                         parse(cell->back().nucleus()->cell(0), flags, mode);
1276                         return;
1277                 }
1278
1279                 else if (t.cs() == "substack") {
1280                         cell->push_back(createInsetMath(t.cs()));
1281                         parse2(cell->back(), FLAG_ITEM, mode, false);
1282                 }
1283
1284                 else if (t.cs() == "xymatrix") {
1285                         odocstringstream os;
1286                         while (good() && nextToken().cat() != catBegin)
1287                                 os << getToken().asInput();
1288                         cell->push_back(createInsetMath(t.cs() + os.str()));
1289                         parse2(cell->back(), FLAG_ITEM, mode, false);
1290                 }
1291
1292                 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1293                         cell->push_back(createInsetMath(t.cs()));
1294                         parse(cell->back().nucleus()->cell(0), FLAG_OPTION, InsetMath::TEXT_MODE);
1295                         parse(cell->back().nucleus()->cell(1), FLAG_OPTION, InsetMath::TEXT_MODE);
1296                         parse(cell->back().nucleus()->cell(2), FLAG_ITEM, InsetMath::TEXT_MODE);
1297                 }
1298
1299                 else if (t.cs() == "tag") {
1300                         if (nextToken().character() == '*') {
1301                                 getToken();
1302                                 cell->push_back(createInsetMath(t.cs() + '*'));
1303                         } else
1304                                 cell->push_back(createInsetMath(t.cs()));
1305                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1306                 }
1307
1308 #if 0
1309                 else if (t.cs() == "infer") {
1310                         MathArray ar;
1311                         parse(ar, FLAG_OPTION, mode);
1312                         cell->push_back(createInsetMath(t.cs()));
1313                         parse2(cell->back(), FLAG_ITEM, mode, false);
1314                 }
1315
1316                 // Disabled
1317                 else if (1 && t.cs() == "ar") {
1318                         auto_ptr<InsetMathXYArrow> p(new InsetMathXYArrow);
1319                         // try to read target
1320                         parse(p->cell(0), FLAG_OTPTION, mode);
1321                         // try to read label
1322                         if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1323                                 p->up_ = nextToken().cat() == catSuper;
1324                                 getToken();
1325                                 parse(p->cell(1), FLAG_ITEM, mode);
1326                                 //lyxerr << "read label: " << p->cell(1) << endl;
1327                         }
1328
1329                         cell->push_back(MathAtom(p.release()));
1330                         //lyxerr << "read cell: " << cell << endl;
1331                 }
1332 #endif
1333
1334                 else if (t.cs().size()) {
1335                         latexkeys const * l = in_word_set(t.cs());
1336                         if (l) {
1337                                 if (l->inset == "big") {
1338                                         skipSpaces();
1339                                         docstring const delim = getToken().asInput();
1340                                         if (InsetMathBig::isBigInsetDelim(delim))
1341                                                 cell->push_back(MathAtom(
1342                                                         new InsetMathBig(t.cs(), delim)));
1343                                         else {
1344                                                 cell->push_back(createInsetMath(t.cs()));
1345                                                 putback();
1346                                         }
1347                                 }
1348
1349                                 else if (l->inset == "font") {
1350                                         cell->push_back(createInsetMath(t.cs()));
1351                                         parse(cell->back().nucleus()->cell(0),
1352                                                 FLAG_ITEM, asMode(mode, l->extra));
1353                                 }
1354
1355                                 else if (l->inset == "oldfont") {
1356                                         cell->push_back(createInsetMath(t.cs()));
1357                                         parse(cell->back().nucleus()->cell(0),
1358                                                 flags | FLAG_ALIGN, asMode(mode, l->extra));
1359                                         if (prevToken().cat() != catAlign &&
1360                                             prevToken().cs() != "\\")
1361                                                 return;
1362                                         putback();
1363                                 }
1364
1365                                 else if (l->inset == "style") {
1366                                         cell->push_back(createInsetMath(t.cs()));
1367                                         parse(cell->back().nucleus()->cell(0),
1368                                                 flags | FLAG_ALIGN, mode);
1369                                         if (prevToken().cat() != catAlign &&
1370                                             prevToken().cs() != "\\")
1371                                                 return;
1372                                         putback();
1373                                 }
1374
1375                                 else {
1376                                         MathAtom at = createInsetMath(t.cs());
1377                                         for (InsetMath::idx_type i = 0; i < at->nargs(); ++i)
1378                                                 parse(at.nucleus()->cell(i),
1379                                                         FLAG_ITEM, asMode(mode, l->extra));
1380                                         cell->push_back(at);
1381                                 }
1382                         }
1383
1384                         else {
1385                                 MathAtom at = createInsetMath(t.cs());
1386                                 InsetMath::mode_type m = mode;
1387                                 //if (m == InsetMath::UNDECIDED_MODE)
1388                                 //lyxerr << "default creation: m1: " << m << endl;
1389                                 if (at->currentMode() != InsetMath::UNDECIDED_MODE)
1390                                         m = at->currentMode();
1391                                 //lyxerr << "default creation: m2: " << m << endl;
1392                                 InsetMath::idx_type start = 0;
1393                                 // this fails on \bigg[...\bigg]
1394                                 //MathArray opt;
1395                                 //parse(opt, FLAG_OPTION, InsetMath::VERBATIM_MODE);
1396                                 //if (opt.size()) {
1397                                 //      start = 1;
1398                                 //      at.nucleus()->cell(0) = opt;
1399                                 //}
1400                                 for (InsetMath::idx_type i = start; i < at->nargs(); ++i) {
1401                                         parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1402                                         skipSpaces();
1403                                 }
1404                                 cell->push_back(at);
1405                         }
1406                 }
1407
1408
1409                 if (flags & FLAG_LEAVE) {
1410                         flags &= ~FLAG_LEAVE;
1411                         break;
1412                 }
1413         }
1414 }
1415
1416
1417
1418 } // anonymous namespace
1419
1420
1421 void mathed_parse_cell(MathArray & ar, docstring const & str)
1422 {
1423         istringstream is(to_utf8(str));
1424         mathed_parse_cell(ar, is);
1425 }
1426
1427
1428 void mathed_parse_cell(MathArray & ar, istream & is)
1429 {
1430         Parser(is).parse(ar, 0, InsetMath::MATH_MODE);
1431 }
1432
1433
1434 bool mathed_parse_normal(MathAtom & t, string const & str)
1435 {
1436         istringstream is(str);
1437         return Parser(is).parse(t);
1438 }
1439
1440
1441 bool mathed_parse_normal(MathAtom & t, istream & is)
1442 {
1443         return Parser(is).parse(t);
1444 }
1445
1446
1447 bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
1448 {
1449         return Parser(lex).parse(t);
1450 }
1451
1452
1453 void mathed_parse_normal(InsetMathGrid & grid, string const & str)
1454 {
1455         istringstream is(str);
1456         Parser(is).parse1(grid, 0, InsetMath::MATH_MODE, false);
1457 }
1458
1459
1460 void initParser()
1461 {
1462         fill(theCatcode, theCatcode + 128, catOther);
1463         fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1464         fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1465
1466         theCatcode[int('\\')] = catEscape;
1467         theCatcode[int('{')]  = catBegin;
1468         theCatcode[int('}')]  = catEnd;
1469         theCatcode[int('$')]  = catMath;
1470         theCatcode[int('&')]  = catAlign;
1471         theCatcode[int('\n')] = catNewline;
1472         theCatcode[int('#')]  = catParameter;
1473         theCatcode[int('^')]  = catSuper;
1474         theCatcode[int('_')]  = catSub;
1475         theCatcode[int(0x7f)] = catIgnore;
1476         theCatcode[int(' ')]  = catSpace;
1477         theCatcode[int('\t')] = catSpace;
1478         theCatcode[int('\r')] = catNewline;
1479         theCatcode[int('~')]  = catActive;
1480         theCatcode[int('%')]  = catComment;
1481 }
1482
1483
1484 } // namespace lyx