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