3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
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:
20 \def\makeamptab{\catcode`\&=4\relax}
21 \def\makeampletter{\catcode`\&=11\relax}
22 \def\b{\makeampletter\expandafter\makeamptab\bi}
40 #include "MathParser.h"
42 #include "InsetMathArray.h"
43 #include "InsetMathBig.h"
44 #include "InsetMathBrace.h"
45 #include "InsetMathChar.h"
46 #include "InsetMathColor.h"
47 #include "InsetMathComment.h"
48 #include "InsetMathDelim.h"
49 #include "InsetMathEnsureMath.h"
50 #include "InsetMathEnv.h"
51 #include "InsetMathFrac.h"
52 #include "InsetMathKern.h"
53 #include "MathMacro.h"
54 #include "InsetMathPar.h"
55 #include "InsetMathRef.h"
56 #include "InsetMathRoot.h"
57 #include "InsetMathScript.h"
58 #include "InsetMathSpace.h"
59 #include "InsetMathSplit.h"
60 #include "InsetMathSqrt.h"
61 #include "InsetMathTabular.h"
62 #include "MathMacroTemplate.h"
63 #include "MathFactory.h"
64 #include "MathMacroArgument.h"
65 #include "MathSupport.h"
70 #include "support/debug.h"
71 #include "support/convert.h"
72 #include "support/docstream.h"
73 #include "support/lstrings.h"
87 InsetMath::mode_type asMode(InsetMath::mode_type oldmode, docstring const & str)
89 //lyxerr << "handling mode: '" << str << "'" << endl;
90 if (str == "mathmode")
91 return InsetMath::MATH_MODE;
92 if (str == "textmode" || str == "forcetext")
93 return InsetMath::TEXT_MODE;
98 bool stared(docstring const & s)
100 size_t const n = s.size();
101 return n && s[n - 1] == '*';
105 docstring escapeSpecialChars(docstring const & str, bool textmode)
107 docstring const backslash = textmode ? from_ascii("\\textbackslash ")
108 : from_ascii("\\backslash ");
109 docstring const caret = textmode ? from_ascii("\\textasciicircum ")
110 : from_ascii("\\mathcircumflex ");
112 return subst(subst(subst(subst(subst(subst(subst(subst(subst(str,
113 from_ascii("\\"), backslash),
114 from_ascii("^"), caret),
115 from_ascii("_"), from_ascii("\\_")),
116 from_ascii("$"), from_ascii("\\$")),
117 from_ascii("#"), from_ascii("\\#")),
118 from_ascii("&"), from_ascii("\\&")),
119 from_ascii("%"), from_ascii("\\%")),
120 from_ascii("{"), from_ascii("\\{")),
121 from_ascii("}"), from_ascii("\\}"));
126 * Add the row \p cellrow to \p grid.
127 * \returns wether the row could be added. Adding a row can fail for
128 * environments like "equation" that have a fixed number of rows.
130 bool addRow(InsetMathGrid & grid, InsetMathGrid::row_type & cellrow,
131 docstring const & vskip, bool allow_newpage_ = true)
134 if (cellrow == grid.nrows()) {
135 //lyxerr << "adding row " << cellrow << endl;
136 grid.addRow(cellrow - 1);
137 if (cellrow == grid.nrows()) {
138 // We can't add a row to this grid, so let's
139 // append the content of this cell to the previous
141 // This does not happen in well formed .lyx files,
142 // but LyX versions 1.3.x and older could create
143 // such files and tex2lyx can still do that.
145 lyxerr << "ignoring extra row";
147 lyxerr << " with extra space " << to_utf8(vskip);
149 lyxerr << " with no page break allowed";
150 lyxerr << '.' << endl;
154 grid.vcrskip(Length(to_utf8(vskip)), cellrow - 1);
155 grid.rowinfo(cellrow - 1).allow_newpage_ = allow_newpage_;
161 * Add the column \p cellcol to \p grid.
162 * \returns wether the column could be added. Adding a column can fail for
163 * environments like "eqnarray" that have a fixed number of columns.
165 bool addCol(InsetMathGrid & grid, InsetMathGrid::col_type & cellcol)
168 if (cellcol == grid.ncols()) {
169 //lyxerr << "adding column " << cellcol << endl;
170 grid.addCol(cellcol);
171 if (cellcol == grid.ncols()) {
172 // We can't add a column to this grid, so let's
173 // append the content of this cell to the previous
175 // This does not happen in well formed .lyx files,
176 // but LyX versions 1.3.x and older could create
177 // such files and tex2lyx can still do that.
179 lyxerr << "ignoring extra column." << endl;
188 * Check wether the last row is empty and remove it if yes.
189 * Otherwise the following code
197 * will result in a grid with 3 rows (+ the dummy row that is always present),
198 * because the last '\\' opens a new row.
200 void delEmptyLastRow(InsetMathGrid & grid)
202 InsetMathGrid::row_type const row = grid.nrows() - 1;
203 for (InsetMathGrid::col_type col = 0; col < grid.ncols(); ++col) {
204 if (!grid.cell(grid.index(row, col)).empty())
207 // Copy the row information of the empty row (which would contain the
208 // last hline in the example above) to the dummy row and delete the
210 grid.rowinfo(row + 1) = grid.rowinfo(row);
215 // These are TeX's catcodes
217 catEscape, // 0 backslash
227 catSpace, // 10 space
228 catLetter, // 11 a-zA-Z
229 catOther, // 12 none of the above
232 catInvalid // 15 <delete>
235 CatCode theCatcode[128];
238 inline CatCode catcode(char_type c)
240 /* The only characters that are not catOther lie in the pure ASCII
241 * range. Therefore theCatcode has only 128 entries.
242 * TeX itself deals with 8bit characters, so if needed this table
243 * could be enlarged to 256 entries.
244 * Any larger value does not make sense, since the fact that we use
245 * unicode internally does not change Knuth's TeX engine.
246 * Apart from that a table for the full 21bit UCS4 range would waste
247 * too much memory. */
251 return theCatcode[c];
256 FLAG_ALIGN = 1 << 0, // next & or \\ ends the parsing process
257 FLAG_BRACE_LAST = 1 << 1, // next closing brace ends the parsing
258 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
259 FLAG_END = 1 << 3, // next \\end ends the parsing process
260 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
261 FLAG_TEXTMODE = 1 << 5, // we are in a box
262 FLAG_ITEM = 1 << 6, // read a (possibly braced) token
263 FLAG_LEAVE = 1 << 7, // leave the loop at the end
264 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
265 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
266 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
267 FLAG_OPTION = 1 << 11, // read [...] style option
268 FLAG_BRACED = 1 << 12 // read {...} style argument
273 // Helper class for parsing
279 Token() : cs_(), char_(0), cat_(catIgnore) {}
281 Token(char_type c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
283 explicit Token(docstring const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
286 docstring const & cs() const { return cs_; }
288 CatCode cat() const { return cat_; }
290 char_type character() const { return char_; }
292 docstring asString() const { return cs_.size() ? cs_ : docstring(1, char_); }
294 docstring asInput() const { return cs_.size() ? '\\' + cs_ : docstring(1, char_); }
306 ostream & operator<<(ostream & os, Token const & t)
309 docstring const & cs = t.cs();
310 // FIXME: For some strange reason, the stream operator instanciate
311 // a new Token before outputting the contents of t.cs().
312 // Because of this the line
314 // below becomes recursive.
315 // In order to avoid that we return early:
318 os << '\\' << to_utf8(cs);
320 else if (t.cat() == catLetter)
323 os << '[' << t.character() << ',' << t.cat() << ']';
331 typedef InsetMath::mode_type mode_type;
333 typedef Parse::flags parse_mode;
336 Parser(Lexer & lex, parse_mode mode);
337 /// Only use this for reading from .lyx file format, for the reason
338 /// see Parser::tokenize(istream &).
339 Parser(istream & is, parse_mode mode);
341 Parser(docstring const & str, parse_mode mode);
344 bool parse(MathAtom & at);
346 bool parse(MathData & array, unsigned flags, mode_type mode);
348 bool parse1(InsetMathGrid & grid, unsigned flags, mode_type mode,
351 MathData parse(unsigned flags, mode_type mode);
353 int lineno() const { return lineno_; }
359 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
360 /// get arg delimited by 'left' and 'right'
361 docstring getArg(char_type left, char_type right);
365 void error(string const & msg);
366 void error(docstring const & msg) { error(to_utf8(msg)); }
367 /// dump contents to screen
369 /// Only use this for reading from .lyx file format (see
370 /// implementation for reason)
371 void tokenize(istream & is);
373 void tokenize(docstring const & s);
375 void skipSpaceTokens(idocstream & is, char_type c);
377 void push_back(Token const & t);
381 Token const & prevToken() const;
383 Token const & nextToken() const;
385 Token const & getToken();
386 /// skips spaces if any
389 void lex(docstring const & s);
393 docstring parse_verbatim_item();
395 docstring parse_verbatim_option();
400 vector<Token> tokens_;
403 /// Stack of active environments
404 vector<docstring> environments_;
412 Parser::Parser(Lexer & lexer, parse_mode mode)
413 : lineno_(lexer.lineNumber()), pos_(0), mode_(mode), success_(true)
415 tokenize(lexer.getStream());
420 Parser::Parser(istream & is, parse_mode mode)
421 : lineno_(0), pos_(0), mode_(mode), success_(true)
427 Parser::Parser(docstring const & str, parse_mode mode)
428 : lineno_(0), pos_(0), mode_(mode), success_(true)
434 void Parser::push_back(Token const & t)
436 tokens_.push_back(t);
440 void Parser::pop_back()
446 Token const & Parser::prevToken() const
448 static const Token dummy;
449 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
453 Token const & Parser::nextToken() const
455 static const Token dummy;
456 return good() ? tokens_[pos_] : dummy;
460 Token const & Parser::getToken()
462 static const Token dummy;
463 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
464 return good() ? tokens_[pos_++] : dummy;
468 void Parser::skipSpaces()
470 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
475 void Parser::putback()
481 bool Parser::good() const
483 return pos_ < tokens_.size();
487 char_type Parser::getChar()
490 error("The input stream is not well...");
494 return tokens_[pos_++].character();
498 docstring Parser::getArg(char_type left, char_type right)
503 char_type c = getChar();
508 while ((c = getChar()) != right && good())
515 void Parser::skipSpaceTokens(idocstream & is, char_type c)
517 // skip trailing spaces
518 while (catcode(c) == catSpace || catcode(c) == catNewline)
521 //lyxerr << "putting back: " << c << endl;
526 void Parser::tokenize(istream & is)
528 // eat everything up to the next \end_inset or end of stream
529 // and store it in s for further tokenization
534 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
535 s = s.substr(0, s.size() - 10);
539 // Remove the space after \end_inset
540 if (is.get(c) && c != ' ')
544 tokenize(from_utf8(s));
548 void Parser::tokenize(docstring const & buffer)
550 idocstringstream is(mode_ & Parse::VERBATIM
551 ? escapeSpecialChars(buffer, mode_ & Parse::TEXTMODE)
552 : buffer, ios::in | ios::binary);
556 //lyxerr << "reading c: " << c << endl;
558 switch (catcode(c)) {
562 if (catcode(c) == catNewline)
563 ; //push_back(Token("par"));
565 push_back(Token('\n', catNewline));
573 while (is.get(c) && catcode(c) != catNewline)
583 error("unexpected end of input");
586 if (catcode(c) == catLetter) {
588 while (is.get(c) && catcode(c) == catLetter)
590 skipSpaceTokens(is, c);
599 push_back(Token(c, catcode(c)));
601 skipSpaceTokens(is, c);
606 if (!(mode_ & Parse::QUIET))
607 lyxerr << "ignoring a char: " << int(c) << endl;
612 push_back(Token(c, catcode(c)));
622 void Parser::dump() const
624 lyxerr << "\nTokens: ";
625 for (unsigned i = 0; i < tokens_.size(); ++i) {
628 lyxerr << tokens_[i];
630 lyxerr << " pos: " << pos_ << endl;
634 void Parser::error(string const & msg)
637 if (!(mode_ & Parse::QUIET)) {
638 lyxerr << "Line ~" << lineno_ << ": Math parse error: "
645 bool Parser::parse(MathAtom & at)
649 parse(ar, false, InsetMath::UNDECIDED_MODE);
650 if (ar.size() != 1 || ar.front()->getType() == hullNone) {
651 if (!(mode_ & Parse::QUIET))
652 lyxerr << "unusual contents found: " << ar << endl;
653 at = MathAtom(new InsetMathPar(ar));
654 //if (at->nargs() > 0)
655 // at.nucleus()->cell(0) = ar;
657 // lyxerr << "unusual contents found: " << ar << endl;
665 docstring Parser::parse_verbatim_option()
669 if (nextToken().character() == '[') {
670 Token t = getToken();
671 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
672 if (t.cat() == catBegin) {
674 res += '{' + parse_verbatim_item() + '}';
683 docstring Parser::parse_verbatim_item()
687 if (nextToken().cat() == catBegin) {
688 Token t = getToken();
689 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
690 if (t.cat() == catBegin) {
692 res += '{' + parse_verbatim_item() + '}';
702 MathData Parser::parse(unsigned flags, mode_type mode)
705 parse(ar, flags, mode);
710 bool Parser::parse(MathData & array, unsigned flags, mode_type mode)
712 InsetMathGrid grid(1, 1);
713 parse1(grid, flags, mode, false);
714 array = grid.cell(0);
719 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
722 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
726 bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
727 const mode_type mode, const bool numbered)
730 InsetMathGrid::row_type cellrow = 0;
731 InsetMathGrid::col_type cellcol = 0;
732 MathData * cell = &grid.cell(grid.index(cellrow, cellcol));
734 if (grid.asHullInset())
735 grid.asHullInset()->numbered(cellrow, numbered);
738 //lyxerr << " flags: " << flags << endl;
739 //lyxerr << " mode: " << mode << endl;
740 //lyxerr << "grid: " << grid << endl;
743 Token const & t = getToken();
746 lyxerr << "t: " << t << " flags: " << flags << endl;
747 lyxerr << "mode: " << mode << endl;
752 if (flags & FLAG_ITEM) {
754 if (t.cat() == catBegin) {
755 // skip the brace and collect everything to the next matching
757 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
761 // handle only this single token, leave the loop if done
766 if (flags & FLAG_BRACED) {
767 if (t.cat() == catSpace)
770 if (t.cat() != catBegin) {
771 error("opening brace expected");
775 // skip the brace and collect everything to the next matching
777 flags = FLAG_BRACE_LAST;
781 if (flags & FLAG_OPTION) {
782 if (t.cat() == catOther && t.character() == '[') {
784 parse(ar, FLAG_BRACK_LAST, mode);
787 // no option found, put back token and we are done
796 if (t.cat() == catMath) {
797 if (mode != InsetMath::MATH_MODE) {
798 // we are inside some text mode thingy, so opening new math is allowed
799 Token const & n = getToken();
800 if (n.cat() == catMath) {
801 // TeX's $$...$$ syntax for displayed math
802 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
803 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
804 getToken(); // skip the second '$' token
806 // simple $...$ stuff
808 if (mode == InsetMath::UNDECIDED_MODE) {
809 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
810 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
812 // Don't create nested math hulls (bug #5392)
813 cell->push_back(MathAtom(new InsetMathEnsureMath));
814 parse(cell->back().nucleus()->cell(0), FLAG_SIMPLE, InsetMath::MATH_MODE);
819 else if (flags & FLAG_SIMPLE) {
820 // this is the end of the formula
825 error("something strange in the parser");
830 else if (t.cat() == catLetter)
831 cell->push_back(MathAtom(new InsetMathChar(t.character())));
833 else if (t.cat() == catSpace && mode != InsetMath::MATH_MODE) {
834 if (cell->empty() || cell->back()->getChar() != ' ')
835 cell->push_back(MathAtom(new InsetMathChar(t.character())));
838 else if (t.cat() == catNewline && mode != InsetMath::MATH_MODE) {
839 if (cell->empty() || cell->back()->getChar() != ' ')
840 cell->push_back(MathAtom(new InsetMathChar(' ')));
843 else if (t.cat() == catParameter) {
844 Token const & n = getToken();
845 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
848 else if (t.cat() == catActive)
849 cell->push_back(MathAtom(new InsetMathChar(t.character())));
851 else if (t.cat() == catBegin) {
853 parse(ar, FLAG_BRACE_LAST, mode);
854 // do not create a BraceInset if they were written by LyX
855 // this helps to keep the annoyance of "a choose b" to a minimum
856 if (ar.size() == 1 && ar[0]->extraBraces())
859 cell->push_back(MathAtom(new InsetMathBrace(ar)));
862 else if (t.cat() == catEnd) {
863 if (flags & FLAG_BRACE_LAST)
865 error("found '}' unexpectedly");
866 //LASSERT(false, /**/);
867 //add(cell, '}', LM_TC_TEX);
870 else if (t.cat() == catAlign) {
871 //lyxerr << " column now " << (cellcol + 1)
872 // << " max: " << grid.ncols() << endl;
873 if (flags & FLAG_ALIGN)
875 if (addCol(grid, cellcol))
876 cell = &grid.cell(grid.index(cellrow, cellcol));
879 else if (t.cat() == catSuper || t.cat() == catSub) {
880 bool up = (t.cat() == catSuper);
881 // we need no new script inset if the last thing was a scriptinset,
882 // which has that script already not the same script already
884 cell->push_back(MathAtom(new InsetMathScript(up)));
885 else if (cell->back()->asScriptInset() &&
886 !cell->back()->asScriptInset()->has(up))
887 cell->back().nucleus()->asScriptInset()->ensure(up);
888 else if (cell->back()->asScriptInset())
889 cell->push_back(MathAtom(new InsetMathScript(up)));
891 cell->back() = MathAtom(new InsetMathScript(cell->back(), up));
892 InsetMathScript * p = cell->back().nucleus()->asScriptInset();
893 // special handling of {}-bases
894 // Here we could remove the brace inset for things
895 // like {a'}^2 and add the braces back in
896 // InsetMathScript::write().
897 // We do not do it, since it is not possible to detect
898 // reliably whether the braces are needed because the
899 // nucleus contains more than one symbol, or whether
900 // they are needed for unknown commands like \xx{a}_0
901 // or \yy{a}{b}_0. This was done in revision 14819
902 // in an unreliable way. See this thread
903 // http://www.mail-archive.com/lyx-devel%40lists.lyx.org/msg104917.html
905 parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
912 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
913 //lyxerr << "finished reading option" << endl;
917 else if (t.cat() == catOther) {
918 char_type c = t.character();
919 if (isAsciiOrMathAlpha(c)
920 || mode_ & Parse::VERBATIM
921 || !(mode_ & Parse::USETEXT)
922 || mode == InsetMath::TEXT_MODE) {
923 cell->push_back(MathAtom(new InsetMathChar(c)));
925 MathAtom at = createInsetMath("text");
926 at.nucleus()->cell(0).push_back(MathAtom(new InsetMathChar(c)));
927 while (nextToken().cat() == catOther
928 && !isAsciiOrMathAlpha(nextToken().character())) {
929 c = getToken().character();
930 at.nucleus()->cell(0).push_back(MathAtom(new InsetMathChar(c)));
936 else if (t.cat() == catComment) {
939 Token const & t = getToken();
940 if (t.cat() == catNewline)
944 cell->push_back(MathAtom(new InsetMathComment(s)));
952 else if (t.cs() == "lyxlock") {
954 cell->back().nucleus()->lock(true);
957 else if ((t.cs() == "global" && nextToken().cs() == "def") ||
959 if (t.cs() == "global")
963 docstring name = getToken().cs();
968 while (good() && nextToken().cat() != catBegin) {
969 pars += getToken().cs();
976 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
978 // is a version for display attached?
981 if (nextToken().cat() == catBegin)
982 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
984 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs,
985 0, MacroTypeDef, vector<MathData>(), def, display)));
988 else if (t.cs() == "newcommand" ||
989 t.cs() == "renewcommand" ||
990 t.cs() == "newlyxcommand") {
992 if (getToken().cat() != catBegin) {
993 error("'{' in \\newcommand expected (1) ");
996 docstring name = getToken().cs();
997 if (getToken().cat() != catEnd) {
998 error("'}' in \\newcommand expected");
1003 docstring const arg = getArg('[', ']');
1006 nargs = convert<int>(arg);
1008 // optional argument given?
1011 vector<MathData> optionalValues;
1012 while (nextToken().character() == '[') {
1014 optionalValues.push_back(MathData());
1015 parse(optionalValues[optionals], FLAG_BRACK_LAST, mode);
1020 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1022 // is a version for display attached?
1025 if (nextToken().cat() == catBegin)
1026 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
1028 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs,
1029 optionals, MacroTypeNewcommand, optionalValues, def, display)));
1033 else if (t.cs() == "newcommandx" ||
1034 t.cs() == "renewcommandx") {
1035 // \newcommandx{\foo}[2][usedefault, addprefix=\global,1=default]{#1,#2}
1038 if (nextToken().cat() == catBegin) {
1040 name = getToken().cs();
1041 if (getToken().cat() != catEnd) {
1042 error("'}' in \\newcommandx expected");
1046 name = getToken().cs();
1049 docstring const arg = getArg('[', ']');
1051 error("[num] in \\newcommandx expected");
1054 int nargs = convert<int>(arg);
1058 vector<MathData> optionalValues;
1059 if (nextToken().character() == '[') {
1063 // handle 'opt=value' options, separated by ','.
1065 while (nextToken().character() != ']' && good()) {
1066 if (nextToken().character() >= '1'
1067 && nextToken().character() <= '9') {
1068 // optional value -> get parameter number
1069 int n = getChar() - '0';
1071 error("Arity of \\newcommandx too low "
1072 "for given optional parameter.");
1077 if (getToken().character() != '=') {
1078 error("'=' and optional parameter value "
1079 "expected for \\newcommandx");
1084 int optNum = max(size_t(n), optionalValues.size());
1085 optionalValues.resize(optNum);
1086 optionalValues[n - 1].clear();
1087 while (nextToken().character() != ']'
1088 && nextToken().character() != ',') {
1090 parse(data, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1091 optionalValues[n - 1].append(data);
1093 optionals = max(n, optionals);
1094 } else if (nextToken().cat() == catLetter) {
1095 // we in fact ignore every non-optional
1100 while (nextToken().cat() == catLetter)
1106 if (nextToken().character() == '=') {
1108 while (nextToken().character() != ']'
1109 && nextToken().character() != ',')
1110 parse(value, FLAG_ITEM,
1111 InsetMath::UNDECIDED_MODE);
1114 error("option for \\newcommandx expected");
1120 if (nextToken().character() == ',') {
1123 } else if (nextToken().character() != ']') {
1124 error("Expecting ',' or ']' in options "
1125 "of \\newcommandx");
1138 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1140 // is a version for display attached?
1143 if (nextToken().cat() == catBegin)
1144 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
1146 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs,
1147 optionals, MacroTypeNewcommandx, optionalValues, def,
1151 else if (t.cs() == "(") {
1152 if (mode == InsetMath::MATH_MODE) {
1153 error("bad math environment");
1156 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
1157 parse2(cell->back(), FLAG_SIMPLE2, InsetMath::MATH_MODE, false);
1160 else if (t.cs() == "[") {
1161 if (mode != InsetMath::UNDECIDED_MODE) {
1162 error("bad math environment");
1165 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
1166 parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false);
1169 else if (t.cs() == "protect")
1170 // ignore \\protect, will hopefully be re-added during output
1173 else if (t.cs() == "end") {
1174 if (flags & FLAG_END) {
1175 // eat environment name
1176 docstring const name = getArg('{', '}');
1177 if (environments_.empty())
1178 error("'found \\end{" + name +
1179 "}' without matching '\\begin{" +
1181 else if (name != environments_.back())
1182 error("'\\end{" + name +
1183 "}' does not match '\\begin{" +
1184 environments_.back() + "}'");
1186 environments_.pop_back();
1187 // Delete empty last row in matrix
1189 // If you abuse InsetMathGrid for
1190 // non-matrix like structures you
1191 // probably need to refine this test.
1192 // Right now we only have to test for
1193 // single line hull insets.
1194 if (grid.nrows() > 1)
1195 delEmptyLastRow(grid);
1199 error("found 'end' unexpectedly");
1202 else if (t.cs() == ")") {
1203 if (flags & FLAG_SIMPLE2)
1205 error("found '\\)' unexpectedly");
1208 else if (t.cs() == "]") {
1209 if (flags & FLAG_EQUATION)
1211 error("found '\\]' unexpectedly");
1214 else if (t.cs() == "\\") {
1215 if (flags & FLAG_ALIGN)
1218 if (nextToken().asInput() == "*") {
1220 added = addRow(grid, cellrow, docstring(), false);
1222 added = addRow(grid, cellrow, getArg('[', ']'));
1224 error("missing token after \\\\");
1227 if (grid.asHullInset())
1228 grid.asHullInset()->numbered(
1230 cell = &grid.cell(grid.index(cellrow,
1236 else if (t.cs() == "multicolumn") {
1237 // extract column count and insert dummy cells
1239 parse(count, FLAG_ITEM, mode);
1241 if (!extractNumber(count, cols)) {
1243 lyxerr << " can't extract number of cells from " << count << endl;
1245 // resize the table if necessary
1246 for (int i = 0; i < cols; ++i) {
1247 if (addCol(grid, cellcol)) {
1248 cell = &grid.cell(grid.index(
1250 // mark this as dummy
1251 grid.cellinfo(grid.index(
1252 cellrow, cellcol)).dummy_ = true;
1255 // the last cell is the real thing, not a dummy
1256 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
1258 // read special alignment
1260 parse(align, FLAG_ITEM, mode);
1261 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
1263 // parse the remaining contents into the "real" cell
1264 parse(*cell, FLAG_ITEM, mode);
1268 else if (t.cs() == "limits")
1271 else if (t.cs() == "nolimits")
1274 else if (t.cs() == "nonumber") {
1275 if (grid.asHullInset())
1276 grid.asHullInset()->numbered(cellrow, false);
1279 else if (t.cs() == "number") {
1280 if (grid.asHullInset())
1281 grid.asHullInset()->numbered(cellrow, true);
1284 else if (t.cs() == "hline") {
1285 grid.rowinfo(cellrow).lines_ ++;
1288 else if (t.cs() == "sqrt") {
1290 parse(ar, FLAG_OPTION, mode);
1292 cell->push_back(MathAtom(new InsetMathRoot));
1293 cell->back().nucleus()->cell(0) = ar;
1294 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1296 cell->push_back(MathAtom(new InsetMathSqrt));
1297 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1301 else if (t.cs() == "unit") {
1302 // Allowed formats \unit[val]{unit}
1304 parse(ar, FLAG_OPTION, mode);
1306 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNIT)));
1307 cell->back().nucleus()->cell(0) = ar;
1308 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1310 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNIT, 1)));
1311 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1315 else if (t.cs() == "unitfrac") {
1316 // Here allowed formats are \unitfrac[val]{num}{denom}
1318 parse(ar, FLAG_OPTION, mode);
1320 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC, 3)));
1321 cell->back().nucleus()->cell(2) = ar;
1323 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC)));
1325 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1326 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1329 else if (t.cs() == "cfrac") {
1330 // allowed formats are \cfrac[pos]{num}{denom}
1331 docstring const arg = getArg('[', ']');
1332 //lyxerr << "got so far: '" << arg << "'" << endl;
1334 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::CFRACLEFT)));
1335 else if (arg == "r")
1336 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::CFRACRIGHT)));
1337 else if (arg.empty() || arg == "c")
1338 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::CFRAC)));
1340 error("found invalid optional argument");
1343 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1344 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1347 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1348 cell->push_back(createInsetMath(t.cs()));
1349 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1350 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1353 else if (t.cs() == "ref" || t.cs() == "eqref" || t.cs() == "prettyref"
1354 || t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1355 cell->push_back(MathAtom(new InsetMathRef(t.cs())));
1356 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1357 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1360 else if (t.cs() == "left") {
1362 Token const & tl = getToken();
1363 // \| and \Vert are equivalent, and InsetMathDelim
1365 // FIXME: fix this in InsetMathDelim itself!
1366 docstring const l = tl.cs() == "|" ? from_ascii("Vert") : tl.asString();
1368 parse(ar, FLAG_RIGHT, mode);
1372 Token const & tr = getToken();
1373 docstring const r = tr.cs() == "|" ? from_ascii("Vert") : tr.asString();
1374 cell->push_back(MathAtom(new InsetMathDelim(l, r, ar)));
1377 else if (t.cs() == "right") {
1378 if (flags & FLAG_RIGHT)
1380 //lyxerr << "got so far: '" << cell << "'" << endl;
1381 error("Unmatched right delimiter");
1385 else if (t.cs() == "begin") {
1386 docstring const name = getArg('{', '}');
1387 environments_.push_back(name);
1389 if (name == "array" || name == "subarray") {
1390 docstring const valign = parse_verbatim_option() + 'c';
1391 docstring const halign = parse_verbatim_item();
1392 cell->push_back(MathAtom(new InsetMathArray(name,
1393 InsetMathGrid::guessColumns(halign), 1, (char)valign[0], halign)));
1394 parse2(cell->back(), FLAG_END, mode, false);
1397 else if (name == "tabular") {
1398 docstring const valign = parse_verbatim_option() + 'c';
1399 docstring const halign = parse_verbatim_item();
1400 cell->push_back(MathAtom(new InsetMathTabular(name,
1401 InsetMathGrid::guessColumns(halign), 1, (char)valign[0], halign)));
1402 parse2(cell->back(), FLAG_END, InsetMath::TEXT_MODE, false);
1405 else if (name == "split" || name == "cases") {
1406 cell->push_back(createInsetMath(name));
1407 parse2(cell->back(), FLAG_END, mode, false);
1410 else if (name == "alignedat") {
1411 docstring const valign = parse_verbatim_option() + 'c';
1412 // ignore this for a while
1414 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1415 parse2(cell->back(), FLAG_END, mode, false);
1418 else if (name == "math") {
1419 if (mode == InsetMath::MATH_MODE) {
1420 error("bad math environment");
1423 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
1424 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, true);
1427 else if (name == "equation" || name == "equation*"
1428 || name == "displaymath") {
1429 if (mode != InsetMath::UNDECIDED_MODE) {
1430 error("bad math environment");
1433 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
1434 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation"));
1437 else if (name == "eqnarray" || name == "eqnarray*") {
1438 if (mode != InsetMath::UNDECIDED_MODE) {
1439 error("bad math environment");
1442 cell->push_back(MathAtom(new InsetMathHull(hullEqnArray)));
1443 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1446 else if (name == "align" || name == "align*") {
1447 if (mode != InsetMath::UNDECIDED_MODE) {
1448 error("bad math environment");
1451 cell->push_back(MathAtom(new InsetMathHull(hullAlign)));
1452 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1455 else if (name == "flalign" || name == "flalign*") {
1456 if (mode != InsetMath::UNDECIDED_MODE) {
1457 error("bad math environment");
1460 cell->push_back(MathAtom(new InsetMathHull(hullFlAlign)));
1461 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1464 else if (name == "alignat" || name == "alignat*") {
1465 if (mode != InsetMath::UNDECIDED_MODE) {
1466 error("bad math environment");
1469 // ignore this for a while
1471 cell->push_back(MathAtom(new InsetMathHull(hullAlignAt)));
1472 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1475 else if (name == "xalignat" || name == "xalignat*") {
1476 if (mode != InsetMath::UNDECIDED_MODE) {
1477 error("bad math environment");
1480 // ignore this for a while
1482 cell->push_back(MathAtom(new InsetMathHull(hullXAlignAt)));
1483 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1486 else if (name == "xxalignat") {
1487 if (mode != InsetMath::UNDECIDED_MODE) {
1488 error("bad math environment");
1491 // ignore this for a while
1493 cell->push_back(MathAtom(new InsetMathHull(hullXXAlignAt)));
1494 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1497 else if (name == "multline" || name == "multline*") {
1498 if (mode != InsetMath::UNDECIDED_MODE) {
1499 error("bad math environment");
1502 cell->push_back(MathAtom(new InsetMathHull(hullMultline)));
1503 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1506 else if (name == "gather" || name == "gather*") {
1507 if (mode != InsetMath::UNDECIDED_MODE) {
1508 error("bad math environment");
1511 cell->push_back(MathAtom(new InsetMathHull(hullGather)));
1512 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1515 else if (latexkeys const * l = in_word_set(name)) {
1516 if (l->inset == "matrix") {
1517 cell->push_back(createInsetMath(name));
1518 parse2(cell->back(), FLAG_END, mode, false);
1519 } else if (l->inset == "split") {
1520 docstring const valign = parse_verbatim_option() + 'c';
1521 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1522 parse2(cell->back(), FLAG_END, mode, false);
1525 if (!(mode_ & Parse::QUIET)) {
1527 lyxerr << "found math environment `"
1529 << "' in symbols file with unsupported inset `"
1530 << to_utf8(l->inset)
1533 // create generic environment inset
1534 cell->push_back(MathAtom(new InsetMathEnv(name)));
1535 parse(cell->back().nucleus()->cell(0), FLAG_END, mode);
1541 if (!(mode_ & Parse::QUIET)) {
1543 lyxerr << "found unknown math environment '"
1544 << to_utf8(name) << "'" << endl;
1546 // create generic environment inset
1547 cell->push_back(MathAtom(new InsetMathEnv(name)));
1548 parse(cell->back().nucleus()->cell(0), FLAG_END, mode);
1552 else if (t.cs() == "kern") {
1556 Token const & t = getToken();
1562 if (isValidLength(to_utf8(s)))
1565 cell->push_back(MathAtom(new InsetMathKern(s)));
1568 else if (t.cs() == "label") {
1569 // FIXME: This is swallowed in inline formulas
1570 docstring label = parse_verbatim_item();
1573 if (grid.asHullInset()) {
1574 grid.asHullInset()->label(cellrow, label);
1576 cell->push_back(createInsetMath(t.cs()));
1577 cell->push_back(MathAtom(new InsetMathBrace(ar)));
1581 else if (t.cs() == "choose" || t.cs() == "over"
1582 || t.cs() == "atop" || t.cs() == "brace"
1583 || t.cs() == "brack") {
1584 MathAtom at = createInsetMath(t.cs());
1585 at.nucleus()->cell(0) = *cell;
1587 parse(at.nucleus()->cell(1), flags, mode);
1588 cell->push_back(at);
1592 else if (t.cs() == "color") {
1593 docstring const color = parse_verbatim_item();
1594 cell->push_back(MathAtom(new InsetMathColor(true, color)));
1595 parse(cell->back().nucleus()->cell(0), flags, mode);
1599 else if (t.cs() == "textcolor") {
1600 docstring const color = parse_verbatim_item();
1601 cell->push_back(MathAtom(new InsetMathColor(false, color)));
1602 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1605 else if (t.cs() == "normalcolor") {
1606 cell->push_back(createInsetMath(t.cs()));
1607 parse(cell->back().nucleus()->cell(0), flags, mode);
1611 else if (t.cs() == "substack") {
1612 cell->push_back(createInsetMath(t.cs()));
1613 parse2(cell->back(), FLAG_ITEM, mode, false);
1616 else if (t.cs() == "xymatrix") {
1617 odocstringstream os;
1618 while (good() && nextToken().cat() != catBegin)
1619 os << getToken().asInput();
1620 cell->push_back(createInsetMath(t.cs() + os.str()));
1621 parse2(cell->back(), FLAG_ITEM, mode, false);
1624 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1625 cell->push_back(createInsetMath(t.cs()));
1626 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, InsetMath::TEXT_MODE);
1627 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, InsetMath::TEXT_MODE);
1628 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, InsetMath::TEXT_MODE);
1631 else if (t.cs() == "tag") {
1632 if (nextToken().character() == '*') {
1634 cell->push_back(createInsetMath(t.cs() + '*'));
1636 cell->push_back(createInsetMath(t.cs()));
1637 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1640 else if (t.cs() == "hspace" && nextToken().character() != '*') {
1641 docstring const name = t.cs();
1642 docstring const arg = parse_verbatim_item();
1644 if (isValidLength(to_utf8(arg), &length))
1645 cell->push_back(MathAtom(new InsetMathSpace(length)));
1647 // Since the Length class cannot use length variables
1648 // we must not create an InsetMathSpace.
1649 cell->push_back(MathAtom(new MathMacro(name)));
1651 mathed_parse_cell(ar, '{' + arg + '}');
1657 else if (t.cs() == "infer") {
1659 parse(ar, FLAG_OPTION, mode);
1660 cell->push_back(createInsetMath(t.cs()));
1661 parse2(cell->back(), FLAG_ITEM, mode, false);
1665 else if (1 && t.cs() == "ar") {
1666 auto_ptr<InsetMathXYArrow> p(new InsetMathXYArrow);
1667 // try to read target
1668 parse(p->cell(0), FLAG_OTPTION, mode);
1669 // try to read label
1670 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1671 p->up_ = nextToken().cat() == catSuper;
1673 parse(p->cell(1), FLAG_ITEM, mode);
1674 //lyxerr << "read label: " << p->cell(1) << endl;
1677 cell->push_back(MathAtom(p.release()));
1678 //lyxerr << "read cell: " << cell << endl;
1682 else if (t.cs() == "lyxmathsym") {
1684 if (getToken().cat() != catBegin) {
1685 error("'{' expected in \\" + t.cs());
1690 CatCode cat = nextToken().cat();
1691 while (good() && (count || cat != catEnd)) {
1692 if (cat == catBegin)
1694 else if (cat == catEnd)
1696 cmd += getToken().asInput();
1697 cat = nextToken().cat();
1699 if (getToken().cat() != catEnd) {
1700 error("'}' expected in \\" + t.cs());
1705 cmd = Encodings::fromLaTeXCommand(cmd, rem);
1706 for (size_t i = 0; i < cmd.size(); ++i)
1707 cell->push_back(MathAtom(new InsetMathChar(cmd[i])));
1709 char_type c = rem[0];
1710 cell->push_back(MathAtom(new InsetMathChar(c)));
1711 cmd = rem.substr(1);
1715 } while (cmd.size());
1718 else if (t.cs().size()) {
1719 latexkeys const * l = in_word_set(t.cs());
1721 if (l->inset == "big") {
1723 docstring const delim = getToken().asInput();
1724 if (InsetMathBig::isBigInsetDelim(delim))
1725 cell->push_back(MathAtom(
1726 new InsetMathBig(t.cs(), delim)));
1728 cell->push_back(createInsetMath(t.cs()));
1733 else if (l->inset == "font") {
1734 cell->push_back(createInsetMath(t.cs()));
1735 parse(cell->back().nucleus()->cell(0),
1736 FLAG_ITEM, asMode(mode, l->extra));
1739 else if (l->inset == "oldfont") {
1740 cell->push_back(createInsetMath(t.cs()));
1741 parse(cell->back().nucleus()->cell(0),
1742 flags | FLAG_ALIGN, asMode(mode, l->extra));
1743 if (prevToken().cat() != catAlign &&
1744 prevToken().cs() != "\\")
1749 else if (l->inset == "style") {
1750 cell->push_back(createInsetMath(t.cs()));
1751 parse(cell->back().nucleus()->cell(0),
1752 flags | FLAG_ALIGN, mode);
1753 if (prevToken().cat() != catAlign &&
1754 prevToken().cs() != "\\")
1760 MathAtom at = createInsetMath(t.cs());
1761 for (InsetMath::idx_type i = 0; i < at->nargs(); ++i)
1762 parse(at.nucleus()->cell(i),
1763 FLAG_ITEM, asMode(mode, l->extra));
1764 cell->push_back(at);
1769 bool is_unicode_symbol = false;
1770 if (mode == InsetMath::TEXT_MODE) {
1772 docstring cmd = prevToken().asInput();
1774 CatCode cat = nextToken().cat();
1775 if (cat == catBegin) {
1777 while (good() && (count || cat != catEnd)) {
1778 cat = nextToken().cat();
1779 cmd += getToken().asInput();
1781 if (cat == catBegin)
1783 else if (cat == catEnd)
1789 Encodings::fromLaTeXCommand(cmd, is_combining);
1791 if (cat == catLetter)
1793 cmd += getToken().asInput();
1795 if (cat == catLetter)
1797 c = Encodings::fromLaTeXCommand(cmd, is_combining);
1800 is_unicode_symbol = true;
1801 cell->push_back(MathAtom(new InsetMathChar(c)));
1803 while (num_tokens--)
1807 if (!is_unicode_symbol) {
1808 MathAtom at = createInsetMath(t.cs());
1809 InsetMath::mode_type m = mode;
1810 //if (m == InsetMath::UNDECIDED_MODE)
1811 //lyxerr << "default creation: m1: " << m << endl;
1812 if (at->currentMode() != InsetMath::UNDECIDED_MODE)
1813 m = at->currentMode();
1814 //lyxerr << "default creation: m2: " << m << endl;
1815 InsetMath::idx_type start = 0;
1816 // this fails on \bigg[...\bigg]
1818 //parse(opt, FLAG_OPTION, InsetMath::VERBATIM_MODE);
1821 // at.nucleus()->cell(0) = opt;
1823 for (InsetMath::idx_type i = start; i < at->nargs(); ++i) {
1824 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1827 cell->push_back(at);
1833 if (flags & FLAG_LEAVE) {
1834 flags &= ~FLAG_LEAVE;
1843 } // anonymous namespace
1846 bool mathed_parse_cell(MathData & ar, docstring const & str, Parse::flags f)
1848 return Parser(str, f).parse(ar, 0, f & Parse::TEXTMODE ?
1849 InsetMath::TEXT_MODE : InsetMath::MATH_MODE);
1853 bool mathed_parse_cell(MathData & ar, istream & is, Parse::flags f)
1855 return Parser(is, f).parse(ar, 0, f & Parse::TEXTMODE ?
1856 InsetMath::TEXT_MODE : InsetMath::MATH_MODE);
1860 bool mathed_parse_normal(MathAtom & t, docstring const & str, Parse::flags f)
1862 return Parser(str, f).parse(t);
1866 bool mathed_parse_normal(MathAtom & t, Lexer & lex, Parse::flags f)
1868 return Parser(lex, f).parse(t);
1872 bool mathed_parse_normal(InsetMathGrid & grid, docstring const & str, Parse::flags f)
1874 return Parser(str, f).parse1(grid, 0, f & Parse::TEXTMODE ?
1875 InsetMath::TEXT_MODE : InsetMath::MATH_MODE, false);
1881 fill(theCatcode, theCatcode + 128, catOther);
1882 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1883 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1885 theCatcode[int('\\')] = catEscape;
1886 theCatcode[int('{')] = catBegin;
1887 theCatcode[int('}')] = catEnd;
1888 theCatcode[int('$')] = catMath;
1889 theCatcode[int('&')] = catAlign;
1890 theCatcode[int('\n')] = catNewline;
1891 theCatcode[int('#')] = catParameter;
1892 theCatcode[int('^')] = catSuper;
1893 theCatcode[int('_')] = catSub;
1894 theCatcode[int(0x7f)] = catIgnore;
1895 theCatcode[int(' ')] = catSpace;
1896 theCatcode[int('\t')] = catSpace;
1897 theCatcode[int('\r')] = catNewline;
1898 theCatcode[int('~')] = catActive;
1899 theCatcode[int('%')] = catComment;