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 "InsetMathEnv.h"
50 #include "InsetMathFrac.h"
51 #include "InsetMathKern.h"
52 #include "MathMacro.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"
68 #include "support/debug.h"
69 #include "support/convert.h"
70 #include "support/docstream.h"
82 InsetMath::mode_type asMode(InsetMath::mode_type oldmode, docstring const & str)
84 //lyxerr << "handling mode: '" << str << "'" << endl;
85 if (str == "mathmode")
86 return InsetMath::MATH_MODE;
87 if (str == "textmode" || str == "forcetext")
88 return InsetMath::TEXT_MODE;
93 bool stared(docstring const & s)
95 size_t const n = s.size();
96 return n && s[n - 1] == '*';
101 * Add the row \p cellrow to \p grid.
102 * \returns wether the row could be added. Adding a row can fail for
103 * environments like "equation" that have a fixed number of rows.
105 bool addRow(InsetMathGrid & grid, InsetMathGrid::row_type & cellrow,
106 docstring const & vskip, bool allow_newpage_ = true)
109 if (cellrow == grid.nrows()) {
110 //lyxerr << "adding row " << cellrow << endl;
111 grid.addRow(cellrow - 1);
112 if (cellrow == grid.nrows()) {
113 // We can't add a row to this grid, so let's
114 // append the content of this cell to the previous
116 // This does not happen in well formed .lyx files,
117 // but LyX versions 1.3.x and older could create
118 // such files and tex2lyx can still do that.
120 lyxerr << "ignoring extra row";
122 lyxerr << " with extra space " << to_utf8(vskip);
124 lyxerr << " with no page break allowed";
125 lyxerr << '.' << endl;
129 grid.vcrskip(Length(to_utf8(vskip)), cellrow - 1);
130 grid.rowinfo(cellrow - 1).allow_newpage_ = allow_newpage_;
136 * Add the column \p cellcol to \p grid.
137 * \returns wether the column could be added. Adding a column can fail for
138 * environments like "eqnarray" that have a fixed number of columns.
140 bool addCol(InsetMathGrid & grid, InsetMathGrid::col_type & cellcol)
143 if (cellcol == grid.ncols()) {
144 //lyxerr << "adding column " << cellcol << endl;
145 grid.addCol(cellcol);
146 if (cellcol == grid.ncols()) {
147 // We can't add a column to this grid, so let's
148 // append the content of this cell to the previous
150 // This does not happen in well formed .lyx files,
151 // but LyX versions 1.3.x and older could create
152 // such files and tex2lyx can still do that.
154 lyxerr << "ignoring extra column." << endl;
163 * Check wether the last row is empty and remove it if yes.
164 * Otherwise the following code
172 * will result in a grid with 3 rows (+ the dummy row that is always present),
173 * because the last '\\' opens a new row.
175 void delEmptyLastRow(InsetMathGrid & grid)
177 InsetMathGrid::row_type const row = grid.nrows() - 1;
178 for (InsetMathGrid::col_type col = 0; col < grid.ncols(); ++col) {
179 if (!grid.cell(grid.index(row, col)).empty())
182 // Copy the row information of the empty row (which would contain the
183 // last hline in the example above) to the dummy row and delete the
185 grid.rowinfo(row + 1) = grid.rowinfo(row);
190 // These are TeX's catcodes
192 catEscape, // 0 backslash
202 catSpace, // 10 space
203 catLetter, // 11 a-zA-Z
204 catOther, // 12 none of the above
207 catInvalid // 15 <delete>
210 CatCode theCatcode[128];
213 inline CatCode catcode(char_type c)
215 /* The only characters that are not catOther lie in the pure ASCII
216 * range. Therefore theCatcode has only 128 entries.
217 * TeX itself deals with 8bit characters, so if needed this table
218 * could be enlarged to 256 entries.
219 * Any larger value does not make sense, since the fact that we use
220 * unicode internally does not change Knuth's TeX engine.
221 * Apart from that a table for the full 21bit UCS4 range would waste
222 * too much memory. */
226 return theCatcode[c];
231 FLAG_ALIGN = 1 << 0, // next & or \\ ends the parsing process
232 FLAG_BRACE_LAST = 1 << 1, // next closing brace ends the parsing
233 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
234 FLAG_END = 1 << 3, // next \\end ends the parsing process
235 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
236 FLAG_TEXTMODE = 1 << 5, // we are in a box
237 FLAG_ITEM = 1 << 6, // read a (possibly braced) token
238 FLAG_LEAVE = 1 << 7, // leave the loop at the end
239 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
240 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
241 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
242 FLAG_OPTION = 1 << 11, // read [...] style option
243 FLAG_BRACED = 1 << 12 // read {...} style argument
248 // Helper class for parsing
254 Token() : cs_(), char_(0), cat_(catIgnore) {}
256 Token(char_type c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
258 explicit Token(docstring const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
261 docstring const & cs() const { return cs_; }
263 CatCode cat() const { return cat_; }
265 char_type character() const { return char_; }
267 docstring asString() const { return cs_.size() ? cs_ : docstring(1, char_); }
269 docstring asInput() const { return cs_.size() ? '\\' + cs_ : docstring(1, char_); }
281 ostream & operator<<(ostream & os, Token const & t)
284 docstring const & cs = t.cs();
285 // FIXME: For some strange reason, the stream operator instanciate
286 // a new Token before outputting the contents of t.cs().
287 // Because of this the line
289 // below becomes recursive.
290 // In order to avoid that we return early:
293 os << '\\' << to_utf8(cs);
295 else if (t.cat() == catLetter)
298 os << '[' << t.character() << ',' << t.cat() << ']';
306 typedef InsetMath::mode_type mode_type;
310 /// Only use this for reading from .lyx file format, for the reason
311 /// see Parser::tokenize(istream &).
312 Parser(istream & is);
314 Parser(docstring const & str);
317 bool parse(MathAtom & at);
319 void parse(MathData & array, unsigned flags, mode_type mode);
321 void parse1(InsetMathGrid & grid, unsigned flags, mode_type mode,
324 MathData parse(unsigned flags, mode_type mode);
326 int lineno() const { return lineno_; }
332 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
333 /// get arg delimited by 'left' and 'right'
334 docstring getArg(char_type left, char_type right);
338 void error(string const & msg);
339 void error(docstring const & msg) { error(to_utf8(msg)); }
340 /// dump contents to screen
342 /// Only use this for reading from .lyx file format (see
343 /// implementation for reason)
344 void tokenize(istream & is);
346 void tokenize(docstring const & s);
348 void skipSpaceTokens(idocstream & is, char_type c);
350 void push_back(Token const & t);
354 Token const & prevToken() const;
356 Token const & nextToken() const;
358 Token const & getToken();
359 /// skips spaces if any
362 void lex(docstring const & s);
366 docstring parse_verbatim_item();
368 docstring parse_verbatim_option();
373 vector<Token> tokens_;
376 /// Stack of active environments
377 vector<docstring> environments_;
381 Parser::Parser(Lexer & lexer)
382 : lineno_(lexer.lineNumber()), pos_(0)
384 tokenize(lexer.getStream());
389 Parser::Parser(istream & is)
390 : lineno_(0), pos_(0)
396 Parser::Parser(docstring const & str)
397 : lineno_(0), pos_(0)
403 void Parser::push_back(Token const & t)
405 tokens_.push_back(t);
409 void Parser::pop_back()
415 Token const & Parser::prevToken() const
417 static const Token dummy;
418 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
422 Token const & Parser::nextToken() const
424 static const Token dummy;
425 return good() ? tokens_[pos_] : dummy;
429 Token const & Parser::getToken()
431 static const Token dummy;
432 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
433 return good() ? tokens_[pos_++] : dummy;
437 void Parser::skipSpaces()
439 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
444 void Parser::putback()
450 bool Parser::good() const
452 return pos_ < tokens_.size();
456 char_type Parser::getChar()
459 error("The input stream is not well...");
463 return tokens_[pos_++].character();
467 docstring Parser::getArg(char_type left, char_type right)
472 char_type c = getChar();
477 while ((c = getChar()) != right && good())
484 void Parser::skipSpaceTokens(idocstream & is, char_type c)
486 // skip trailing spaces
487 while (catcode(c) == catSpace || catcode(c) == catNewline)
490 //lyxerr << "putting back: " << c << endl;
495 void Parser::tokenize(istream & is)
497 // eat everything up to the next \end_inset or end of stream
498 // and store it in s for further tokenization
503 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
504 s = s.substr(0, s.size() - 10);
508 // Remove the space after \end_inset
509 if (is.get(c) && c != ' ')
513 tokenize(from_utf8(s));
517 void Parser::tokenize(docstring const & buffer)
519 idocstringstream is(buffer, ios::in | ios::binary);
523 //lyxerr << "reading c: " << c << endl;
525 switch (catcode(c)) {
529 if (catcode(c) == catNewline)
530 ; //push_back(Token("par"));
532 push_back(Token('\n', catNewline));
540 while (is.get(c) && catcode(c) != catNewline)
550 error("unexpected end of input");
553 if (catcode(c) == catLetter) {
555 while (is.get(c) && catcode(c) == catLetter)
557 skipSpaceTokens(is, c);
566 push_back(Token(c, catcode(c)));
568 skipSpaceTokens(is, c);
573 lyxerr << "ignoring a char: " << int(c) << endl;
578 push_back(Token(c, catcode(c)));
588 void Parser::dump() const
590 lyxerr << "\nTokens: ";
591 for (unsigned i = 0; i < tokens_.size(); ++i) {
594 lyxerr << tokens_[i];
596 lyxerr << " pos: " << pos_ << endl;
600 void Parser::error(string const & msg)
602 lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
608 bool Parser::parse(MathAtom & at)
612 parse(ar, false, InsetMath::UNDECIDED_MODE);
613 if (ar.size() != 1 || ar.front()->getType() == hullNone) {
614 lyxerr << "unusual contents found: " << ar << endl;
615 at = MathAtom(new InsetMathPar(ar));
616 //if (at->nargs() > 0)
617 // at.nucleus()->cell(0) = ar;
619 // lyxerr << "unusual contents found: " << ar << endl;
627 docstring Parser::parse_verbatim_option()
631 if (nextToken().character() == '[') {
632 Token t = getToken();
633 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
634 if (t.cat() == catBegin) {
636 res += '{' + parse_verbatim_item() + '}';
645 docstring Parser::parse_verbatim_item()
649 if (nextToken().cat() == catBegin) {
650 Token t = getToken();
651 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
652 if (t.cat() == catBegin) {
654 res += '{' + parse_verbatim_item() + '}';
664 MathData Parser::parse(unsigned flags, mode_type mode)
667 parse(ar, flags, mode);
672 void Parser::parse(MathData & array, unsigned flags, mode_type mode)
674 InsetMathGrid grid(1, 1);
675 parse1(grid, flags, mode, false);
676 array = grid.cell(0);
680 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
683 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
687 void Parser::parse1(InsetMathGrid & grid, unsigned flags,
688 const mode_type mode, const bool numbered)
691 InsetMathGrid::row_type cellrow = 0;
692 InsetMathGrid::col_type cellcol = 0;
693 MathData * cell = &grid.cell(grid.index(cellrow, cellcol));
695 if (grid.asHullInset())
696 grid.asHullInset()->numbered(cellrow, numbered);
699 //lyxerr << " flags: " << flags << endl;
700 //lyxerr << " mode: " << mode << endl;
701 //lyxerr << "grid: " << grid << endl;
704 Token const & t = getToken();
707 lyxerr << "t: " << t << " flags: " << flags << endl;
708 lyxerr << "mode: " << mode << endl;
713 if (flags & FLAG_ITEM) {
715 if (t.cat() == catBegin) {
716 // skip the brace and collect everything to the next matching
718 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
722 // handle only this single token, leave the loop if done
727 if (flags & FLAG_BRACED) {
728 if (t.cat() == catSpace)
731 if (t.cat() != catBegin) {
732 error("opening brace expected");
736 // skip the brace and collect everything to the next matching
738 flags = FLAG_BRACE_LAST;
742 if (flags & FLAG_OPTION) {
743 if (t.cat() == catOther && t.character() == '[') {
745 parse(ar, FLAG_BRACK_LAST, mode);
748 // no option found, put back token and we are done
757 if (t.cat() == catMath) {
758 if (mode != InsetMath::MATH_MODE) {
759 // we are inside some text mode thingy, so opening new math is allowed
760 Token const & n = getToken();
761 if (n.cat() == catMath) {
762 // TeX's $$...$$ syntax for displayed math
763 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
764 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
765 getToken(); // skip the second '$' token
767 // simple $...$ stuff
769 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
770 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
774 else if (flags & FLAG_SIMPLE) {
775 // this is the end of the formula
780 error("something strange in the parser");
785 else if (t.cat() == catLetter)
786 cell->push_back(MathAtom(new InsetMathChar(t.character())));
788 else if (t.cat() == catSpace && mode != InsetMath::MATH_MODE) {
789 if (cell->empty() || cell->back()->getChar() != ' ')
790 cell->push_back(MathAtom(new InsetMathChar(t.character())));
793 else if (t.cat() == catNewline && mode != InsetMath::MATH_MODE) {
794 if (cell->empty() || cell->back()->getChar() != ' ')
795 cell->push_back(MathAtom(new InsetMathChar(' ')));
798 else if (t.cat() == catParameter) {
799 Token const & n = getToken();
800 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
803 else if (t.cat() == catActive)
804 cell->push_back(MathAtom(new InsetMathChar(t.character())));
806 else if (t.cat() == catBegin) {
808 parse(ar, FLAG_BRACE_LAST, mode);
809 // do not create a BraceInset if they were written by LyX
810 // this helps to keep the annoyance of "a choose b" to a minimum
811 if (ar.size() == 1 && ar[0]->extraBraces())
814 cell->push_back(MathAtom(new InsetMathBrace(ar)));
817 else if (t.cat() == catEnd) {
818 if (flags & FLAG_BRACE_LAST)
820 error("found '}' unexpectedly");
821 //LASSERT(false, /**/);
822 //add(cell, '}', LM_TC_TEX);
825 else if (t.cat() == catAlign) {
826 //lyxerr << " column now " << (cellcol + 1)
827 // << " max: " << grid.ncols() << endl;
828 if (flags & FLAG_ALIGN)
830 if (addCol(grid, cellcol))
831 cell = &grid.cell(grid.index(cellrow, cellcol));
834 else if (t.cat() == catSuper || t.cat() == catSub) {
835 bool up = (t.cat() == catSuper);
836 // we need no new script inset if the last thing was a scriptinset,
837 // which has that script already not the same script already
839 cell->push_back(MathAtom(new InsetMathScript(up)));
840 else if (cell->back()->asScriptInset() &&
841 !cell->back()->asScriptInset()->has(up))
842 cell->back().nucleus()->asScriptInset()->ensure(up);
843 else if (cell->back()->asScriptInset())
844 cell->push_back(MathAtom(new InsetMathScript(up)));
846 cell->back() = MathAtom(new InsetMathScript(cell->back(), up));
847 InsetMathScript * p = cell->back().nucleus()->asScriptInset();
848 // special handling of {}-bases
849 // Here we could remove the brace inset for things
850 // like {a'}^2 and add the braces back in
851 // InsetMathScript::write().
852 // We do not do it, since it is not possible to detect
853 // reliably whether the braces are needed because the
854 // nucleus contains more than one symbol, or whether
855 // they are needed for unknown commands like \xx{a}_0
856 // or \yy{a}{b}_0. This was done in revision 14819
857 // in an unreliable way. See this thread
858 // http://www.mail-archive.com/lyx-devel%40lists.lyx.org/msg104917.html
860 parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
867 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
868 //lyxerr << "finished reading option" << endl;
872 else if (t.cat() == catOther)
873 cell->push_back(MathAtom(new InsetMathChar(t.character())));
875 else if (t.cat() == catComment) {
878 Token const & t = getToken();
879 if (t.cat() == catNewline)
883 cell->push_back(MathAtom(new InsetMathComment(s)));
891 else if (t.cs() == "lyxlock") {
893 cell->back().nucleus()->lock(true);
896 else if ((t.cs() == "global" && nextToken().cs() == "def") ||
898 if (t.cs() == "global")
902 docstring name = getToken().cs();
907 while (good() && nextToken().cat() != catBegin) {
908 pars += getToken().cs();
915 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
917 // is a version for display attached?
920 if (nextToken().cat() == catBegin)
921 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
923 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs,
924 0, MacroTypeDef, vector<MathData>(), def, display)));
927 else if (t.cs() == "newcommand" ||
928 t.cs() == "renewcommand" ||
929 t.cs() == "newlyxcommand") {
931 if (getToken().cat() != catBegin) {
932 error("'{' in \\newcommand expected (1) ");
935 docstring name = getToken().cs();
936 if (getToken().cat() != catEnd) {
937 error("'}' in \\newcommand expected");
942 docstring const arg = getArg('[', ']');
945 nargs = convert<int>(arg);
947 // optional argument given?
950 vector<MathData> optionalValues;
951 while (nextToken().character() == '[') {
953 optionalValues.push_back(MathData());
954 parse(optionalValues[optionals], FLAG_BRACK_LAST, mode);
959 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
961 // is a version for display attached?
964 if (nextToken().cat() == catBegin)
965 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
967 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs,
968 optionals, MacroTypeNewcommand, optionalValues, def, display)));
972 else if (t.cs() == "newcommandx" ||
973 t.cs() == "renewcommandx") {
974 // \newcommandx{\foo}[2][usedefault, addprefix=\global,1=default]{#1,#2}
977 if (nextToken().cat() == catBegin) {
979 name = getToken().cs();
980 if (getToken().cat() != catEnd) {
981 error("'}' in \\newcommandx expected");
985 name = getToken().cs();
988 docstring const arg = getArg('[', ']');
990 error("[num] in \\newcommandx expected");
993 int nargs = convert<int>(arg);
997 vector<MathData> optionalValues;
998 if (nextToken().character() == '[') {
1002 // handle 'opt=value' options, separated by ','.
1004 while (nextToken().character() != ']' && good()) {
1005 if (nextToken().character() >= '1'
1006 && nextToken().character() <= '9') {
1007 // optional value -> get parameter number
1008 int n = getChar() - '0';
1010 error("Arity of \\newcommandx too low "
1011 "for given optional parameter.");
1016 if (getToken().character() != '=') {
1017 error("'=' and optional parameter value "
1018 "expected for \\newcommandx");
1023 int optNum = max(size_t(n), optionalValues.size());
1024 optionalValues.resize(optNum);
1025 optionalValues[n - 1].clear();
1026 while (nextToken().character() != ']'
1027 && nextToken().character() != ',') {
1029 parse(data, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1030 optionalValues[n - 1].append(data);
1032 optionals = max(n, optionals);
1033 } else if (nextToken().cat() == catLetter) {
1034 // we in fact ignore every non-optional
1039 while (nextToken().cat() == catLetter)
1045 if (nextToken().character() == '=') {
1047 while (nextToken().character() != ']'
1048 && nextToken().character() != ',')
1049 parse(value, FLAG_ITEM,
1050 InsetMath::UNDECIDED_MODE);
1053 error("option for \\newcommandx expected");
1059 if (nextToken().character() == ',') {
1062 } else if (nextToken().character() != ']') {
1063 error("Expecting ',' or ']' in options "
1064 "of \\newcommandx");
1077 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1079 // is a version for display attached?
1082 if (nextToken().cat() == catBegin)
1083 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
1085 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs,
1086 optionals, MacroTypeNewcommandx, optionalValues, def,
1090 else if (t.cs() == "(") {
1091 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
1092 parse2(cell->back(), FLAG_SIMPLE2, InsetMath::MATH_MODE, false);
1095 else if (t.cs() == "[") {
1096 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
1097 parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false);
1100 else if (t.cs() == "protect")
1101 // ignore \\protect, will hopefully be re-added during output
1104 else if (t.cs() == "end") {
1105 if (flags & FLAG_END) {
1106 // eat environment name
1107 docstring const name = getArg('{', '}');
1108 if (environments_.empty())
1109 error("'found \\end{" + name +
1110 "}' without matching '\\begin{" +
1112 else if (name != environments_.back())
1113 error("'\\end{" + name +
1114 "}' does not match '\\begin{" +
1115 environments_.back() + "}'");
1117 environments_.pop_back();
1118 // Delete empty last row in matrix
1120 // If you abuse InsetMathGrid for
1121 // non-matrix like structures you
1122 // probably need to refine this test.
1123 // Right now we only have to test for
1124 // single line hull insets.
1125 if (grid.nrows() > 1)
1126 delEmptyLastRow(grid);
1130 error("found 'end' unexpectedly");
1133 else if (t.cs() == ")") {
1134 if (flags & FLAG_SIMPLE2)
1136 error("found '\\)' unexpectedly");
1139 else if (t.cs() == "]") {
1140 if (flags & FLAG_EQUATION)
1142 error("found '\\]' unexpectedly");
1145 else if (t.cs() == "\\") {
1146 if (flags & FLAG_ALIGN)
1149 if (nextToken().asInput() == "*") {
1151 added = addRow(grid, cellrow, docstring(), false);
1153 added = addRow(grid, cellrow, getArg('[', ']'));
1155 error("missing token after \\\\");
1158 if (grid.asHullInset())
1159 grid.asHullInset()->numbered(
1161 cell = &grid.cell(grid.index(cellrow,
1167 else if (t.cs() == "multicolumn") {
1168 // extract column count and insert dummy cells
1170 parse(count, FLAG_ITEM, mode);
1172 if (!extractNumber(count, cols)) {
1173 lyxerr << " can't extract number of cells from " << count << endl;
1175 // resize the table if necessary
1176 for (int i = 0; i < cols; ++i) {
1177 if (addCol(grid, cellcol)) {
1178 cell = &grid.cell(grid.index(
1180 // mark this as dummy
1181 grid.cellinfo(grid.index(
1182 cellrow, cellcol)).dummy_ = true;
1185 // the last cell is the real thing, not a dummy
1186 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
1188 // read special alignment
1190 parse(align, FLAG_ITEM, mode);
1191 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
1193 // parse the remaining contents into the "real" cell
1194 parse(*cell, FLAG_ITEM, mode);
1198 else if (t.cs() == "limits")
1201 else if (t.cs() == "nolimits")
1204 else if (t.cs() == "nonumber") {
1205 if (grid.asHullInset())
1206 grid.asHullInset()->numbered(cellrow, false);
1209 else if (t.cs() == "number") {
1210 if (grid.asHullInset())
1211 grid.asHullInset()->numbered(cellrow, true);
1214 else if (t.cs() == "hline") {
1215 grid.rowinfo(cellrow).lines_ ++;
1218 else if (t.cs() == "sqrt") {
1220 parse(ar, FLAG_OPTION, mode);
1222 cell->push_back(MathAtom(new InsetMathRoot));
1223 cell->back().nucleus()->cell(0) = ar;
1224 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1226 cell->push_back(MathAtom(new InsetMathSqrt));
1227 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1231 else if (t.cs() == "unit") {
1232 // Allowed formats \unit[val]{unit}
1234 parse(ar, FLAG_OPTION, mode);
1236 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNIT)));
1237 cell->back().nucleus()->cell(0) = ar;
1238 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1240 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNIT, 1)));
1241 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1244 else if (t.cs() == "unitfrac") {
1245 // Here allowed formats are \unitfrac[val]{num}{denom}
1247 parse(ar, FLAG_OPTION, mode);
1249 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC, 3)));
1250 cell->back().nucleus()->cell(2) = ar;
1252 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC)));
1254 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1255 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1258 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1259 cell->push_back(createInsetMath(t.cs()));
1260 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1261 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1264 else if (t.cs() == "ref" || t.cs() == "eqref" || t.cs() == "prettyref"
1265 || t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1266 cell->push_back(MathAtom(new InsetMathRef(t.cs())));
1267 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1268 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1271 else if (t.cs() == "left") {
1273 Token const & tl = getToken();
1274 // \| and \Vert are equivalent, and InsetMathDelim
1276 // FIXME: fix this in InsetMathDelim itself!
1277 docstring const l = tl.cs() == "|" ? from_ascii("Vert") : tl.asString();
1279 parse(ar, FLAG_RIGHT, mode);
1283 Token const & tr = getToken();
1284 docstring const r = tr.cs() == "|" ? from_ascii("Vert") : tr.asString();
1285 cell->push_back(MathAtom(new InsetMathDelim(l, r, ar)));
1288 else if (t.cs() == "right") {
1289 if (flags & FLAG_RIGHT)
1291 //lyxerr << "got so far: '" << cell << "'" << endl;
1292 error("Unmatched right delimiter");
1296 else if (t.cs() == "begin") {
1297 docstring const name = getArg('{', '}');
1298 environments_.push_back(name);
1300 if (name == "array" || name == "subarray") {
1301 docstring const valign = parse_verbatim_option() + 'c';
1302 docstring const halign = parse_verbatim_item();
1303 cell->push_back(MathAtom(new InsetMathArray(name,
1304 InsetMathGrid::guessColumns(halign), 1, (char)valign[0], halign)));
1305 parse2(cell->back(), FLAG_END, mode, false);
1308 else if (name == "tabular") {
1309 docstring const valign = parse_verbatim_option() + 'c';
1310 docstring const halign = parse_verbatim_item();
1311 cell->push_back(MathAtom(new InsetMathTabular(name,
1312 InsetMathGrid::guessColumns(halign), 1, (char)valign[0], halign)));
1313 parse2(cell->back(), FLAG_END, InsetMath::TEXT_MODE, false);
1316 else if (name == "split" || name == "cases") {
1317 cell->push_back(createInsetMath(name));
1318 parse2(cell->back(), FLAG_END, mode, false);
1321 else if (name == "alignedat") {
1322 docstring const valign = parse_verbatim_option() + 'c';
1323 // ignore this for a while
1325 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1326 parse2(cell->back(), FLAG_END, mode, false);
1329 else if (name == "math") {
1330 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
1331 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, true);
1334 else if (name == "equation" || name == "equation*"
1335 || name == "displaymath") {
1336 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
1337 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation"));
1340 else if (name == "eqnarray" || name == "eqnarray*") {
1341 cell->push_back(MathAtom(new InsetMathHull(hullEqnArray)));
1342 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1345 else if (name == "align" || name == "align*") {
1346 cell->push_back(MathAtom(new InsetMathHull(hullAlign)));
1347 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1350 else if (name == "flalign" || name == "flalign*") {
1351 cell->push_back(MathAtom(new InsetMathHull(hullFlAlign)));
1352 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1355 else if (name == "alignat" || name == "alignat*") {
1356 // ignore this for a while
1358 cell->push_back(MathAtom(new InsetMathHull(hullAlignAt)));
1359 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1362 else if (name == "xalignat" || name == "xalignat*") {
1363 // ignore this for a while
1365 cell->push_back(MathAtom(new InsetMathHull(hullXAlignAt)));
1366 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1369 else if (name == "xxalignat") {
1370 // ignore this for a while
1372 cell->push_back(MathAtom(new InsetMathHull(hullXXAlignAt)));
1373 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1376 else if (name == "multline" || name == "multline*") {
1377 cell->push_back(MathAtom(new InsetMathHull(hullMultline)));
1378 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1381 else if (name == "gather" || name == "gather*") {
1382 cell->push_back(MathAtom(new InsetMathHull(hullGather)));
1383 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1386 else if (latexkeys const * l = in_word_set(name)) {
1387 if (l->inset == "matrix") {
1388 cell->push_back(createInsetMath(name));
1389 parse2(cell->back(), FLAG_END, mode, false);
1390 } else if (l->inset == "split") {
1391 docstring const valign = parse_verbatim_option() + 'c';
1392 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1393 parse2(cell->back(), FLAG_END, mode, false);
1396 lyxerr << "found math environment `" << to_utf8(name)
1397 << "' in symbols file with unsupported inset `"
1398 << to_utf8(l->inset) << "'." << endl;
1399 // create generic environment inset
1400 cell->push_back(MathAtom(new InsetMathEnv(name)));
1401 parse(cell->back().nucleus()->cell(0), FLAG_END, mode);
1407 lyxerr << "found unknown math environment '" << to_utf8(name)
1409 // create generic environment inset
1410 cell->push_back(MathAtom(new InsetMathEnv(name)));
1411 parse(cell->back().nucleus()->cell(0), FLAG_END, mode);
1415 else if (t.cs() == "kern") {
1419 Token const & t = getToken();
1425 if (isValidLength(to_utf8(s)))
1428 cell->push_back(MathAtom(new InsetMathKern(s)));
1431 else if (t.cs() == "label") {
1432 // FIXME: This is swallowed in inline formulas
1433 docstring label = parse_verbatim_item();
1436 if (grid.asHullInset()) {
1437 grid.asHullInset()->label(cellrow, label);
1439 cell->push_back(createInsetMath(t.cs()));
1440 cell->push_back(MathAtom(new InsetMathBrace(ar)));
1444 else if (t.cs() == "choose" || t.cs() == "over"
1445 || t.cs() == "atop" || t.cs() == "brace"
1446 || t.cs() == "brack") {
1447 MathAtom at = createInsetMath(t.cs());
1448 at.nucleus()->cell(0) = *cell;
1450 parse(at.nucleus()->cell(1), flags, mode);
1451 cell->push_back(at);
1455 else if (t.cs() == "color") {
1456 docstring const color = parse_verbatim_item();
1457 cell->push_back(MathAtom(new InsetMathColor(true, color)));
1458 parse(cell->back().nucleus()->cell(0), flags, mode);
1462 else if (t.cs() == "textcolor") {
1463 docstring const color = parse_verbatim_item();
1464 cell->push_back(MathAtom(new InsetMathColor(false, color)));
1465 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1468 else if (t.cs() == "normalcolor") {
1469 cell->push_back(createInsetMath(t.cs()));
1470 parse(cell->back().nucleus()->cell(0), flags, mode);
1474 else if (t.cs() == "substack") {
1475 cell->push_back(createInsetMath(t.cs()));
1476 parse2(cell->back(), FLAG_ITEM, mode, false);
1479 else if (t.cs() == "xymatrix") {
1480 odocstringstream os;
1481 while (good() && nextToken().cat() != catBegin)
1482 os << getToken().asInput();
1483 cell->push_back(createInsetMath(t.cs() + os.str()));
1484 parse2(cell->back(), FLAG_ITEM, mode, false);
1487 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1488 cell->push_back(createInsetMath(t.cs()));
1489 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, InsetMath::TEXT_MODE);
1490 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, InsetMath::TEXT_MODE);
1491 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, InsetMath::TEXT_MODE);
1494 else if (t.cs() == "tag") {
1495 if (nextToken().character() == '*') {
1497 cell->push_back(createInsetMath(t.cs() + '*'));
1499 cell->push_back(createInsetMath(t.cs()));
1500 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1504 else if (t.cs() == "infer") {
1506 parse(ar, FLAG_OPTION, mode);
1507 cell->push_back(createInsetMath(t.cs()));
1508 parse2(cell->back(), FLAG_ITEM, mode, false);
1512 else if (1 && t.cs() == "ar") {
1513 auto_ptr<InsetMathXYArrow> p(new InsetMathXYArrow);
1514 // try to read target
1515 parse(p->cell(0), FLAG_OTPTION, mode);
1516 // try to read label
1517 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1518 p->up_ = nextToken().cat() == catSuper;
1520 parse(p->cell(1), FLAG_ITEM, mode);
1521 //lyxerr << "read label: " << p->cell(1) << endl;
1524 cell->push_back(MathAtom(p.release()));
1525 //lyxerr << "read cell: " << cell << endl;
1529 else if (t.cs() == "lyxmathsym" || t.cs() == "ensuremath") {
1531 if (getToken().cat() != catBegin) {
1532 error("'{' expected in \\" + t.cs());
1537 CatCode cat = nextToken().cat();
1538 while (good() && (count || cat != catEnd)) {
1539 if (cat == catBegin)
1541 else if (cat == catEnd)
1543 cmd += getToken().asInput();
1544 cat = nextToken().cat();
1546 if (getToken().cat() != catEnd) {
1547 error("'}' expected in \\" + t.cs());
1550 if (t.cs() == "ensuremath") {
1552 mathed_parse_cell(ar, cmd);
1556 cmd = Encodings::fromLaTeXCommand(cmd, rem);
1557 for (size_t i = 0; i < cmd.size(); ++i)
1558 cell->push_back(MathAtom(new InsetMathChar(cmd[i])));
1560 MathAtom at = createInsetMath(t.cs());
1561 cell->push_back(at);
1563 mathed_parse_cell(ar, '{' + rem + '}');
1569 else if (t.cs().size()) {
1570 latexkeys const * l = in_word_set(t.cs());
1572 if (l->inset == "big") {
1574 docstring const delim = getToken().asInput();
1575 if (InsetMathBig::isBigInsetDelim(delim))
1576 cell->push_back(MathAtom(
1577 new InsetMathBig(t.cs(), delim)));
1579 cell->push_back(createInsetMath(t.cs()));
1584 else if (l->inset == "font") {
1585 cell->push_back(createInsetMath(t.cs()));
1586 parse(cell->back().nucleus()->cell(0),
1587 FLAG_ITEM, asMode(mode, l->extra));
1590 else if (l->inset == "oldfont") {
1591 cell->push_back(createInsetMath(t.cs()));
1592 parse(cell->back().nucleus()->cell(0),
1593 flags | FLAG_ALIGN, asMode(mode, l->extra));
1594 if (prevToken().cat() != catAlign &&
1595 prevToken().cs() != "\\")
1600 else if (l->inset == "style") {
1601 cell->push_back(createInsetMath(t.cs()));
1602 parse(cell->back().nucleus()->cell(0),
1603 flags | FLAG_ALIGN, mode);
1604 if (prevToken().cat() != catAlign &&
1605 prevToken().cs() != "\\")
1611 MathAtom at = createInsetMath(t.cs());
1612 for (InsetMath::idx_type i = 0; i < at->nargs(); ++i)
1613 parse(at.nucleus()->cell(i),
1614 FLAG_ITEM, asMode(mode, l->extra));
1615 cell->push_back(at);
1620 bool is_unicode_symbol = false;
1621 if (mode == InsetMath::TEXT_MODE) {
1623 docstring cmd = prevToken().asInput();
1625 CatCode cat = nextToken().cat();
1626 if (cat == catBegin) {
1628 while (good() && (count || cat != catEnd)) {
1629 cat = nextToken().cat();
1630 cmd += getToken().asInput();
1632 if (cat == catBegin)
1634 else if (cat == catEnd)
1640 Encodings::fromLaTeXCommand(cmd, is_combining);
1642 if (cat == catLetter)
1644 cmd += getToken().asInput();
1646 if (cat == catLetter)
1648 c = Encodings::fromLaTeXCommand(cmd, is_combining);
1651 is_unicode_symbol = true;
1652 cell->push_back(MathAtom(new InsetMathChar(c)));
1654 while (num_tokens--)
1658 if (!is_unicode_symbol) {
1659 MathAtom at = createInsetMath(t.cs());
1660 InsetMath::mode_type m = mode;
1661 //if (m == InsetMath::UNDECIDED_MODE)
1662 //lyxerr << "default creation: m1: " << m << endl;
1663 if (at->currentMode() != InsetMath::UNDECIDED_MODE)
1664 m = at->currentMode();
1665 //lyxerr << "default creation: m2: " << m << endl;
1666 InsetMath::idx_type start = 0;
1667 // this fails on \bigg[...\bigg]
1669 //parse(opt, FLAG_OPTION, InsetMath::VERBATIM_MODE);
1672 // at.nucleus()->cell(0) = opt;
1674 for (InsetMath::idx_type i = start; i < at->nargs(); ++i) {
1675 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1678 cell->push_back(at);
1684 if (flags & FLAG_LEAVE) {
1685 flags &= ~FLAG_LEAVE;
1693 } // anonymous namespace
1696 void mathed_parse_cell(MathData & ar, docstring const & str)
1698 Parser(str).parse(ar, 0, InsetMath::MATH_MODE);
1702 void mathed_parse_cell(MathData & ar, istream & is)
1704 Parser(is).parse(ar, 0, InsetMath::MATH_MODE);
1708 bool mathed_parse_normal(MathAtom & t, docstring const & str)
1710 return Parser(str).parse(t);
1714 bool mathed_parse_normal(MathAtom & t, Lexer & lex)
1716 return Parser(lex).parse(t);
1720 void mathed_parse_normal(InsetMathGrid & grid, docstring const & str)
1722 Parser(str).parse1(grid, 0, InsetMath::MATH_MODE, false);
1728 fill(theCatcode, theCatcode + 128, catOther);
1729 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1730 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1732 theCatcode[int('\\')] = catEscape;
1733 theCatcode[int('{')] = catBegin;
1734 theCatcode[int('}')] = catEnd;
1735 theCatcode[int('$')] = catMath;
1736 theCatcode[int('&')] = catAlign;
1737 theCatcode[int('\n')] = catNewline;
1738 theCatcode[int('#')] = catParameter;
1739 theCatcode[int('^')] = catSuper;
1740 theCatcode[int('_')] = catSub;
1741 theCatcode[int(0x7f)] = catIgnore;
1742 theCatcode[int(' ')] = catSpace;
1743 theCatcode[int('\t')] = catSpace;
1744 theCatcode[int('\r')] = catNewline;
1745 theCatcode[int('~')] = catActive;
1746 theCatcode[int('%')] = catComment;