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 bool warn_unusual_contents = true;
85 InsetMath::mode_type asMode(InsetMath::mode_type oldmode, docstring const & str)
87 //lyxerr << "handling mode: '" << str << "'" << endl;
88 if (str == "mathmode")
89 return InsetMath::MATH_MODE;
90 if (str == "textmode" || str == "forcetext")
91 return InsetMath::TEXT_MODE;
96 bool stared(docstring const & s)
98 size_t const n = s.size();
99 return n && s[n - 1] == '*';
104 * Add the row \p cellrow to \p grid.
105 * \returns wether the row could be added. Adding a row can fail for
106 * environments like "equation" that have a fixed number of rows.
108 bool addRow(InsetMathGrid & grid, InsetMathGrid::row_type & cellrow,
109 docstring const & vskip, bool allow_newpage_ = true)
112 if (cellrow == grid.nrows()) {
113 //lyxerr << "adding row " << cellrow << endl;
114 grid.addRow(cellrow - 1);
115 if (cellrow == grid.nrows()) {
116 // We can't add a row to this grid, so let's
117 // append the content of this cell to the previous
119 // This does not happen in well formed .lyx files,
120 // but LyX versions 1.3.x and older could create
121 // such files and tex2lyx can still do that.
123 lyxerr << "ignoring extra row";
125 lyxerr << " with extra space " << to_utf8(vskip);
127 lyxerr << " with no page break allowed";
128 lyxerr << '.' << endl;
132 grid.vcrskip(Length(to_utf8(vskip)), cellrow - 1);
133 grid.rowinfo(cellrow - 1).allow_newpage_ = allow_newpage_;
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.
143 bool addCol(InsetMathGrid & grid, InsetMathGrid::col_type & cellcol)
146 if (cellcol == grid.ncols()) {
147 //lyxerr << "adding column " << cellcol << endl;
148 grid.addCol(cellcol);
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
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.
157 lyxerr << "ignoring extra column." << endl;
166 * Check wether the last row is empty and remove it if yes.
167 * Otherwise the following code
175 * will result in a grid with 3 rows (+ the dummy row that is always present),
176 * because the last '\\' opens a new row.
178 void delEmptyLastRow(InsetMathGrid & grid)
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())
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
188 grid.rowinfo(row + 1) = grid.rowinfo(row);
193 // These are TeX's catcodes
195 catEscape, // 0 backslash
205 catSpace, // 10 space
206 catLetter, // 11 a-zA-Z
207 catOther, // 12 none of the above
210 catInvalid // 15 <delete>
213 CatCode theCatcode[128];
216 inline CatCode catcode(char_type c)
218 /* The only characters that are not catOther lie in the pure ASCII
219 * range. Therefore theCatcode has only 128 entries.
220 * TeX itself deals with 8bit characters, so if needed this table
221 * could be enlarged to 256 entries.
222 * Any larger value does not make sense, since the fact that we use
223 * unicode internally does not change Knuth's TeX engine.
224 * Apart from that a table for the full 21bit UCS4 range would waste
225 * too much memory. */
229 return theCatcode[c];
234 FLAG_ALIGN = 1 << 0, // next & or \\ ends the parsing process
235 FLAG_BRACE_LAST = 1 << 1, // next closing brace ends the parsing
236 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
237 FLAG_END = 1 << 3, // next \\end ends the parsing process
238 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
239 FLAG_TEXTMODE = 1 << 5, // we are in a box
240 FLAG_ITEM = 1 << 6, // read a (possibly braced) token
241 FLAG_LEAVE = 1 << 7, // leave the loop at the end
242 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
243 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
244 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
245 FLAG_OPTION = 1 << 11, // read [...] style option
246 FLAG_BRACED = 1 << 12 // read {...} style argument
251 // Helper class for parsing
257 Token() : cs_(), char_(0), cat_(catIgnore) {}
259 Token(char_type c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
261 explicit Token(docstring const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
264 docstring const & cs() const { return cs_; }
266 CatCode cat() const { return cat_; }
268 char_type character() const { return char_; }
270 docstring asString() const { return cs_.size() ? cs_ : docstring(1, char_); }
272 docstring asInput() const { return cs_.size() ? '\\' + cs_ : docstring(1, char_); }
284 ostream & operator<<(ostream & os, Token const & t)
287 docstring const & cs = t.cs();
288 // FIXME: For some strange reason, the stream operator instanciate
289 // a new Token before outputting the contents of t.cs().
290 // Because of this the line
292 // below becomes recursive.
293 // In order to avoid that we return early:
296 os << '\\' << to_utf8(cs);
298 else if (t.cat() == catLetter)
301 os << '[' << t.character() << ',' << t.cat() << ']';
309 typedef InsetMath::mode_type mode_type;
313 /// Only use this for reading from .lyx file format, for the reason
314 /// see Parser::tokenize(istream &).
315 Parser(istream & is);
317 Parser(docstring const & str);
320 bool parse(MathAtom & at);
322 void parse(MathData & array, unsigned flags, mode_type mode);
324 void parse1(InsetMathGrid & grid, unsigned flags, mode_type mode,
327 MathData parse(unsigned flags, mode_type mode);
329 int lineno() const { return lineno_; }
335 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
336 /// get arg delimited by 'left' and 'right'
337 docstring getArg(char_type left, char_type right);
341 void error(string const & msg);
342 void error(docstring const & msg) { error(to_utf8(msg)); }
343 /// dump contents to screen
345 /// Only use this for reading from .lyx file format (see
346 /// implementation for reason)
347 void tokenize(istream & is);
349 void tokenize(docstring const & s);
351 void skipSpaceTokens(idocstream & is, char_type c);
353 void push_back(Token const & t);
357 Token const & prevToken() const;
359 Token const & nextToken() const;
361 Token const & getToken();
362 /// skips spaces if any
365 void lex(docstring const & s);
369 docstring parse_verbatim_item();
371 docstring parse_verbatim_option();
376 vector<Token> tokens_;
379 /// Stack of active environments
380 vector<docstring> environments_;
384 Parser::Parser(Lexer & lexer)
385 : lineno_(lexer.lineNumber()), pos_(0)
387 tokenize(lexer.getStream());
392 Parser::Parser(istream & is)
393 : lineno_(0), pos_(0)
399 Parser::Parser(docstring const & str)
400 : lineno_(0), pos_(0)
406 void Parser::push_back(Token const & t)
408 tokens_.push_back(t);
412 void Parser::pop_back()
418 Token const & Parser::prevToken() const
420 static const Token dummy;
421 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
425 Token const & Parser::nextToken() const
427 static const Token dummy;
428 return good() ? tokens_[pos_] : dummy;
432 Token const & Parser::getToken()
434 static const Token dummy;
435 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
436 return good() ? tokens_[pos_++] : dummy;
440 void Parser::skipSpaces()
442 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
447 void Parser::putback()
453 bool Parser::good() const
455 return pos_ < tokens_.size();
459 char_type Parser::getChar()
462 error("The input stream is not well...");
466 return tokens_[pos_++].character();
470 docstring Parser::getArg(char_type left, char_type right)
475 char_type c = getChar();
480 while ((c = getChar()) != right && good())
487 void Parser::skipSpaceTokens(idocstream & is, char_type c)
489 // skip trailing spaces
490 while (catcode(c) == catSpace || catcode(c) == catNewline)
493 //lyxerr << "putting back: " << c << endl;
498 void Parser::tokenize(istream & is)
500 // eat everything up to the next \end_inset or end of stream
501 // and store it in s for further tokenization
506 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
507 s = s.substr(0, s.size() - 10);
511 // Remove the space after \end_inset
512 if (is.get(c) && c != ' ')
516 tokenize(from_utf8(s));
520 void Parser::tokenize(docstring const & buffer)
522 idocstringstream is(buffer, ios::in | ios::binary);
526 //lyxerr << "reading c: " << c << endl;
528 switch (catcode(c)) {
532 if (catcode(c) == catNewline)
533 ; //push_back(Token("par"));
535 push_back(Token('\n', catNewline));
543 while (is.get(c) && catcode(c) != catNewline)
553 error("unexpected end of input");
556 if (catcode(c) == catLetter) {
558 while (is.get(c) && catcode(c) == catLetter)
560 skipSpaceTokens(is, c);
569 push_back(Token(c, catcode(c)));
571 skipSpaceTokens(is, c);
576 lyxerr << "ignoring a char: " << int(c) << endl;
581 push_back(Token(c, catcode(c)));
591 void Parser::dump() const
593 lyxerr << "\nTokens: ";
594 for (unsigned i = 0; i < tokens_.size(); ++i) {
597 lyxerr << tokens_[i];
599 lyxerr << " pos: " << pos_ << endl;
603 void Parser::error(string const & msg)
605 lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
611 bool Parser::parse(MathAtom & at)
615 parse(ar, false, InsetMath::UNDECIDED_MODE);
616 if (ar.size() != 1 || ar.front()->getType() == hullNone) {
617 if (warn_unusual_contents)
618 lyxerr << "unusual contents found: " << ar << endl;
619 at = MathAtom(new InsetMathPar(ar));
620 //if (at->nargs() > 0)
621 // at.nucleus()->cell(0) = ar;
623 // lyxerr << "unusual contents found: " << ar << endl;
631 docstring Parser::parse_verbatim_option()
635 if (nextToken().character() == '[') {
636 Token t = getToken();
637 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
638 if (t.cat() == catBegin) {
640 res += '{' + parse_verbatim_item() + '}';
649 docstring Parser::parse_verbatim_item()
653 if (nextToken().cat() == catBegin) {
654 Token t = getToken();
655 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
656 if (t.cat() == catBegin) {
658 res += '{' + parse_verbatim_item() + '}';
668 MathData Parser::parse(unsigned flags, mode_type mode)
671 parse(ar, flags, mode);
676 void Parser::parse(MathData & array, unsigned flags, mode_type mode)
678 InsetMathGrid grid(1, 1);
679 parse1(grid, flags, mode, false);
680 array = grid.cell(0);
684 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
687 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
691 void Parser::parse1(InsetMathGrid & grid, unsigned flags,
692 const mode_type mode, const bool numbered)
695 InsetMathGrid::row_type cellrow = 0;
696 InsetMathGrid::col_type cellcol = 0;
697 MathData * cell = &grid.cell(grid.index(cellrow, cellcol));
699 if (grid.asHullInset())
700 grid.asHullInset()->numbered(cellrow, numbered);
703 //lyxerr << " flags: " << flags << endl;
704 //lyxerr << " mode: " << mode << endl;
705 //lyxerr << "grid: " << grid << endl;
708 Token const & t = getToken();
711 lyxerr << "t: " << t << " flags: " << flags << endl;
712 lyxerr << "mode: " << mode << endl;
717 if (flags & FLAG_ITEM) {
719 if (t.cat() == catBegin) {
720 // skip the brace and collect everything to the next matching
722 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
726 // handle only this single token, leave the loop if done
731 if (flags & FLAG_BRACED) {
732 if (t.cat() == catSpace)
735 if (t.cat() != catBegin) {
736 error("opening brace expected");
740 // skip the brace and collect everything to the next matching
742 flags = FLAG_BRACE_LAST;
746 if (flags & FLAG_OPTION) {
747 if (t.cat() == catOther && t.character() == '[') {
749 parse(ar, FLAG_BRACK_LAST, mode);
752 // no option found, put back token and we are done
761 if (t.cat() == catMath) {
762 if (mode != InsetMath::MATH_MODE) {
763 // we are inside some text mode thingy, so opening new math is allowed
764 Token const & n = getToken();
765 if (n.cat() == catMath) {
766 // TeX's $$...$$ syntax for displayed math
767 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
768 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
769 getToken(); // skip the second '$' token
771 // simple $...$ stuff
773 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
774 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
778 else if (flags & FLAG_SIMPLE) {
779 // this is the end of the formula
784 error("something strange in the parser");
789 else if (t.cat() == catLetter)
790 cell->push_back(MathAtom(new InsetMathChar(t.character())));
792 else if (t.cat() == catSpace && mode != InsetMath::MATH_MODE) {
793 if (cell->empty() || cell->back()->getChar() != ' ')
794 cell->push_back(MathAtom(new InsetMathChar(t.character())));
797 else if (t.cat() == catNewline && mode != InsetMath::MATH_MODE) {
798 if (cell->empty() || cell->back()->getChar() != ' ')
799 cell->push_back(MathAtom(new InsetMathChar(' ')));
802 else if (t.cat() == catParameter) {
803 Token const & n = getToken();
804 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
807 else if (t.cat() == catActive)
808 cell->push_back(MathAtom(new InsetMathChar(t.character())));
810 else if (t.cat() == catBegin) {
812 parse(ar, FLAG_BRACE_LAST, mode);
813 // do not create a BraceInset if they were written by LyX
814 // this helps to keep the annoyance of "a choose b" to a minimum
815 if (ar.size() == 1 && ar[0]->extraBraces())
818 cell->push_back(MathAtom(new InsetMathBrace(ar)));
821 else if (t.cat() == catEnd) {
822 if (flags & FLAG_BRACE_LAST)
824 error("found '}' unexpectedly");
825 //LASSERT(false, /**/);
826 //add(cell, '}', LM_TC_TEX);
829 else if (t.cat() == catAlign) {
830 //lyxerr << " column now " << (cellcol + 1)
831 // << " max: " << grid.ncols() << endl;
832 if (flags & FLAG_ALIGN)
834 if (addCol(grid, cellcol))
835 cell = &grid.cell(grid.index(cellrow, cellcol));
838 else if (t.cat() == catSuper || t.cat() == catSub) {
839 bool up = (t.cat() == catSuper);
840 // we need no new script inset if the last thing was a scriptinset,
841 // which has that script already not the same script already
843 cell->push_back(MathAtom(new InsetMathScript(up)));
844 else if (cell->back()->asScriptInset() &&
845 !cell->back()->asScriptInset()->has(up))
846 cell->back().nucleus()->asScriptInset()->ensure(up);
847 else if (cell->back()->asScriptInset())
848 cell->push_back(MathAtom(new InsetMathScript(up)));
850 cell->back() = MathAtom(new InsetMathScript(cell->back(), up));
851 InsetMathScript * p = cell->back().nucleus()->asScriptInset();
852 // special handling of {}-bases
853 // Here we could remove the brace inset for things
854 // like {a'}^2 and add the braces back in
855 // InsetMathScript::write().
856 // We do not do it, since it is not possible to detect
857 // reliably whether the braces are needed because the
858 // nucleus contains more than one symbol, or whether
859 // they are needed for unknown commands like \xx{a}_0
860 // or \yy{a}{b}_0. This was done in revision 14819
861 // in an unreliable way. See this thread
862 // http://www.mail-archive.com/lyx-devel%40lists.lyx.org/msg104917.html
864 parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
871 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
872 //lyxerr << "finished reading option" << endl;
876 else if (t.cat() == catOther)
877 cell->push_back(MathAtom(new InsetMathChar(t.character())));
879 else if (t.cat() == catComment) {
882 Token const & t = getToken();
883 if (t.cat() == catNewline)
887 cell->push_back(MathAtom(new InsetMathComment(s)));
895 else if (t.cs() == "lyxlock") {
897 cell->back().nucleus()->lock(true);
900 else if ((t.cs() == "global" && nextToken().cs() == "def") ||
902 if (t.cs() == "global")
906 docstring name = getToken().cs();
911 while (good() && nextToken().cat() != catBegin) {
912 pars += getToken().cs();
919 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
921 // is a version for display attached?
924 if (nextToken().cat() == catBegin)
925 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
927 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs,
928 0, MacroTypeDef, vector<MathData>(), def, display)));
931 else if (t.cs() == "newcommand" ||
932 t.cs() == "renewcommand" ||
933 t.cs() == "newlyxcommand") {
935 if (getToken().cat() != catBegin) {
936 error("'{' in \\newcommand expected (1) ");
939 docstring name = getToken().cs();
940 if (getToken().cat() != catEnd) {
941 error("'}' in \\newcommand expected");
946 docstring const arg = getArg('[', ']');
949 nargs = convert<int>(arg);
951 // optional argument given?
954 vector<MathData> optionalValues;
955 while (nextToken().character() == '[') {
957 optionalValues.push_back(MathData());
958 parse(optionalValues[optionals], FLAG_BRACK_LAST, mode);
963 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
965 // is a version for display attached?
968 if (nextToken().cat() == catBegin)
969 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
971 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs,
972 optionals, MacroTypeNewcommand, optionalValues, def, display)));
976 else if (t.cs() == "newcommandx" ||
977 t.cs() == "renewcommandx") {
978 // \newcommandx{\foo}[2][usedefault, addprefix=\global,1=default]{#1,#2}
981 if (nextToken().cat() == catBegin) {
983 name = getToken().cs();
984 if (getToken().cat() != catEnd) {
985 error("'}' in \\newcommandx expected");
989 name = getToken().cs();
992 docstring const arg = getArg('[', ']');
994 error("[num] in \\newcommandx expected");
997 int nargs = convert<int>(arg);
1001 vector<MathData> optionalValues;
1002 if (nextToken().character() == '[') {
1006 // handle 'opt=value' options, separated by ','.
1008 while (nextToken().character() != ']' && good()) {
1009 if (nextToken().character() >= '1'
1010 && nextToken().character() <= '9') {
1011 // optional value -> get parameter number
1012 int n = getChar() - '0';
1014 error("Arity of \\newcommandx too low "
1015 "for given optional parameter.");
1020 if (getToken().character() != '=') {
1021 error("'=' and optional parameter value "
1022 "expected for \\newcommandx");
1027 int optNum = max(size_t(n), optionalValues.size());
1028 optionalValues.resize(optNum);
1029 optionalValues[n - 1].clear();
1030 while (nextToken().character() != ']'
1031 && nextToken().character() != ',') {
1033 parse(data, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1034 optionalValues[n - 1].append(data);
1036 optionals = max(n, optionals);
1037 } else if (nextToken().cat() == catLetter) {
1038 // we in fact ignore every non-optional
1043 while (nextToken().cat() == catLetter)
1049 if (nextToken().character() == '=') {
1051 while (nextToken().character() != ']'
1052 && nextToken().character() != ',')
1053 parse(value, FLAG_ITEM,
1054 InsetMath::UNDECIDED_MODE);
1057 error("option for \\newcommandx expected");
1063 if (nextToken().character() == ',') {
1066 } else if (nextToken().character() != ']') {
1067 error("Expecting ',' or ']' in options "
1068 "of \\newcommandx");
1081 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1083 // is a version for display attached?
1086 if (nextToken().cat() == catBegin)
1087 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
1089 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs,
1090 optionals, MacroTypeNewcommandx, optionalValues, def,
1094 else if (t.cs() == "(") {
1095 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
1096 parse2(cell->back(), FLAG_SIMPLE2, InsetMath::MATH_MODE, false);
1099 else if (t.cs() == "[") {
1100 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
1101 parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false);
1104 else if (t.cs() == "protect")
1105 // ignore \\protect, will hopefully be re-added during output
1108 else if (t.cs() == "end") {
1109 if (flags & FLAG_END) {
1110 // eat environment name
1111 docstring const name = getArg('{', '}');
1112 if (environments_.empty())
1113 error("'found \\end{" + name +
1114 "}' without matching '\\begin{" +
1116 else if (name != environments_.back())
1117 error("'\\end{" + name +
1118 "}' does not match '\\begin{" +
1119 environments_.back() + "}'");
1121 environments_.pop_back();
1122 // Delete empty last row in matrix
1124 // If you abuse InsetMathGrid for
1125 // non-matrix like structures you
1126 // probably need to refine this test.
1127 // Right now we only have to test for
1128 // single line hull insets.
1129 if (grid.nrows() > 1)
1130 delEmptyLastRow(grid);
1134 error("found 'end' unexpectedly");
1137 else if (t.cs() == ")") {
1138 if (flags & FLAG_SIMPLE2)
1140 error("found '\\)' unexpectedly");
1143 else if (t.cs() == "]") {
1144 if (flags & FLAG_EQUATION)
1146 error("found '\\]' unexpectedly");
1149 else if (t.cs() == "\\") {
1150 if (flags & FLAG_ALIGN)
1153 if (nextToken().asInput() == "*") {
1155 added = addRow(grid, cellrow, docstring(), false);
1157 added = addRow(grid, cellrow, getArg('[', ']'));
1159 error("missing token after \\\\");
1162 if (grid.asHullInset())
1163 grid.asHullInset()->numbered(
1165 cell = &grid.cell(grid.index(cellrow,
1171 else if (t.cs() == "multicolumn") {
1172 // extract column count and insert dummy cells
1174 parse(count, FLAG_ITEM, mode);
1176 if (!extractNumber(count, cols)) {
1177 lyxerr << " can't extract number of cells from " << count << endl;
1179 // resize the table if necessary
1180 for (int i = 0; i < cols; ++i) {
1181 if (addCol(grid, cellcol)) {
1182 cell = &grid.cell(grid.index(
1184 // mark this as dummy
1185 grid.cellinfo(grid.index(
1186 cellrow, cellcol)).dummy_ = true;
1189 // the last cell is the real thing, not a dummy
1190 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
1192 // read special alignment
1194 parse(align, FLAG_ITEM, mode);
1195 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
1197 // parse the remaining contents into the "real" cell
1198 parse(*cell, FLAG_ITEM, mode);
1202 else if (t.cs() == "limits")
1205 else if (t.cs() == "nolimits")
1208 else if (t.cs() == "nonumber") {
1209 if (grid.asHullInset())
1210 grid.asHullInset()->numbered(cellrow, false);
1213 else if (t.cs() == "number") {
1214 if (grid.asHullInset())
1215 grid.asHullInset()->numbered(cellrow, true);
1218 else if (t.cs() == "hline") {
1219 grid.rowinfo(cellrow).lines_ ++;
1222 else if (t.cs() == "sqrt") {
1224 parse(ar, FLAG_OPTION, mode);
1226 cell->push_back(MathAtom(new InsetMathRoot));
1227 cell->back().nucleus()->cell(0) = ar;
1228 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1230 cell->push_back(MathAtom(new InsetMathSqrt));
1231 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1235 else if (t.cs() == "unit") {
1236 // Allowed formats \unit[val]{unit}
1238 parse(ar, FLAG_OPTION, mode);
1240 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNIT)));
1241 cell->back().nucleus()->cell(0) = ar;
1242 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1244 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNIT, 1)));
1245 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1248 else if (t.cs() == "unitfrac") {
1249 // Here allowed formats are \unitfrac[val]{num}{denom}
1251 parse(ar, FLAG_OPTION, mode);
1253 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC, 3)));
1254 cell->back().nucleus()->cell(2) = ar;
1256 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC)));
1258 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1259 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1262 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1263 cell->push_back(createInsetMath(t.cs()));
1264 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1265 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1268 else if (t.cs() == "ref" || t.cs() == "eqref" || t.cs() == "prettyref"
1269 || t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1270 cell->push_back(MathAtom(new InsetMathRef(t.cs())));
1271 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1272 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1275 else if (t.cs() == "left") {
1277 Token const & tl = getToken();
1278 // \| and \Vert are equivalent, and InsetMathDelim
1280 // FIXME: fix this in InsetMathDelim itself!
1281 docstring const l = tl.cs() == "|" ? from_ascii("Vert") : tl.asString();
1283 parse(ar, FLAG_RIGHT, mode);
1287 Token const & tr = getToken();
1288 docstring const r = tr.cs() == "|" ? from_ascii("Vert") : tr.asString();
1289 cell->push_back(MathAtom(new InsetMathDelim(l, r, ar)));
1292 else if (t.cs() == "right") {
1293 if (flags & FLAG_RIGHT)
1295 //lyxerr << "got so far: '" << cell << "'" << endl;
1296 error("Unmatched right delimiter");
1300 else if (t.cs() == "begin") {
1301 docstring const name = getArg('{', '}');
1302 environments_.push_back(name);
1304 if (name == "array" || name == "subarray") {
1305 docstring const valign = parse_verbatim_option() + 'c';
1306 docstring const halign = parse_verbatim_item();
1307 cell->push_back(MathAtom(new InsetMathArray(name,
1308 InsetMathGrid::guessColumns(halign), 1, (char)valign[0], halign)));
1309 parse2(cell->back(), FLAG_END, mode, false);
1312 else if (name == "tabular") {
1313 docstring const valign = parse_verbatim_option() + 'c';
1314 docstring const halign = parse_verbatim_item();
1315 cell->push_back(MathAtom(new InsetMathTabular(name,
1316 InsetMathGrid::guessColumns(halign), 1, (char)valign[0], halign)));
1317 parse2(cell->back(), FLAG_END, InsetMath::TEXT_MODE, false);
1320 else if (name == "split" || name == "cases") {
1321 cell->push_back(createInsetMath(name));
1322 parse2(cell->back(), FLAG_END, mode, false);
1325 else if (name == "alignedat") {
1326 docstring const valign = parse_verbatim_option() + 'c';
1327 // ignore this for a while
1329 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1330 parse2(cell->back(), FLAG_END, mode, false);
1333 else if (name == "math") {
1334 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
1335 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, true);
1338 else if (name == "equation" || name == "equation*"
1339 || name == "displaymath") {
1340 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
1341 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation"));
1344 else if (name == "eqnarray" || name == "eqnarray*") {
1345 cell->push_back(MathAtom(new InsetMathHull(hullEqnArray)));
1346 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1349 else if (name == "align" || name == "align*") {
1350 cell->push_back(MathAtom(new InsetMathHull(hullAlign)));
1351 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1354 else if (name == "flalign" || name == "flalign*") {
1355 cell->push_back(MathAtom(new InsetMathHull(hullFlAlign)));
1356 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1359 else if (name == "alignat" || name == "alignat*") {
1360 // ignore this for a while
1362 cell->push_back(MathAtom(new InsetMathHull(hullAlignAt)));
1363 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1366 else if (name == "xalignat" || name == "xalignat*") {
1367 // ignore this for a while
1369 cell->push_back(MathAtom(new InsetMathHull(hullXAlignAt)));
1370 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1373 else if (name == "xxalignat") {
1374 // ignore this for a while
1376 cell->push_back(MathAtom(new InsetMathHull(hullXXAlignAt)));
1377 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1380 else if (name == "multline" || name == "multline*") {
1381 cell->push_back(MathAtom(new InsetMathHull(hullMultline)));
1382 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1385 else if (name == "gather" || name == "gather*") {
1386 cell->push_back(MathAtom(new InsetMathHull(hullGather)));
1387 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1390 else if (latexkeys const * l = in_word_set(name)) {
1391 if (l->inset == "matrix") {
1392 cell->push_back(createInsetMath(name));
1393 parse2(cell->back(), FLAG_END, mode, false);
1394 } else if (l->inset == "split") {
1395 docstring const valign = parse_verbatim_option() + 'c';
1396 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1397 parse2(cell->back(), FLAG_END, mode, false);
1400 lyxerr << "found math environment `" << to_utf8(name)
1401 << "' in symbols file with unsupported inset `"
1402 << to_utf8(l->inset) << "'." << endl;
1403 // create generic environment inset
1404 cell->push_back(MathAtom(new InsetMathEnv(name)));
1405 parse(cell->back().nucleus()->cell(0), FLAG_END, mode);
1411 lyxerr << "found unknown math environment '" << to_utf8(name)
1413 // create generic environment inset
1414 cell->push_back(MathAtom(new InsetMathEnv(name)));
1415 parse(cell->back().nucleus()->cell(0), FLAG_END, mode);
1419 else if (t.cs() == "kern") {
1423 Token const & t = getToken();
1429 if (isValidLength(to_utf8(s)))
1432 cell->push_back(MathAtom(new InsetMathKern(s)));
1435 else if (t.cs() == "label") {
1436 // FIXME: This is swallowed in inline formulas
1437 docstring label = parse_verbatim_item();
1440 if (grid.asHullInset()) {
1441 grid.asHullInset()->label(cellrow, label);
1443 cell->push_back(createInsetMath(t.cs()));
1444 cell->push_back(MathAtom(new InsetMathBrace(ar)));
1448 else if (t.cs() == "choose" || t.cs() == "over"
1449 || t.cs() == "atop" || t.cs() == "brace"
1450 || t.cs() == "brack") {
1451 MathAtom at = createInsetMath(t.cs());
1452 at.nucleus()->cell(0) = *cell;
1454 parse(at.nucleus()->cell(1), flags, mode);
1455 cell->push_back(at);
1459 else if (t.cs() == "color") {
1460 docstring const color = parse_verbatim_item();
1461 cell->push_back(MathAtom(new InsetMathColor(true, color)));
1462 parse(cell->back().nucleus()->cell(0), flags, mode);
1466 else if (t.cs() == "textcolor") {
1467 docstring const color = parse_verbatim_item();
1468 cell->push_back(MathAtom(new InsetMathColor(false, color)));
1469 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1472 else if (t.cs() == "normalcolor") {
1473 cell->push_back(createInsetMath(t.cs()));
1474 parse(cell->back().nucleus()->cell(0), flags, mode);
1478 else if (t.cs() == "substack") {
1479 cell->push_back(createInsetMath(t.cs()));
1480 parse2(cell->back(), FLAG_ITEM, mode, false);
1483 else if (t.cs() == "xymatrix") {
1484 odocstringstream os;
1485 while (good() && nextToken().cat() != catBegin)
1486 os << getToken().asInput();
1487 cell->push_back(createInsetMath(t.cs() + os.str()));
1488 parse2(cell->back(), FLAG_ITEM, mode, false);
1491 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1492 cell->push_back(createInsetMath(t.cs()));
1493 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, InsetMath::TEXT_MODE);
1494 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, InsetMath::TEXT_MODE);
1495 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, InsetMath::TEXT_MODE);
1498 else if (t.cs() == "tag") {
1499 if (nextToken().character() == '*') {
1501 cell->push_back(createInsetMath(t.cs() + '*'));
1503 cell->push_back(createInsetMath(t.cs()));
1504 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1508 else if (t.cs() == "infer") {
1510 parse(ar, FLAG_OPTION, mode);
1511 cell->push_back(createInsetMath(t.cs()));
1512 parse2(cell->back(), FLAG_ITEM, mode, false);
1516 else if (1 && t.cs() == "ar") {
1517 auto_ptr<InsetMathXYArrow> p(new InsetMathXYArrow);
1518 // try to read target
1519 parse(p->cell(0), FLAG_OTPTION, mode);
1520 // try to read label
1521 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1522 p->up_ = nextToken().cat() == catSuper;
1524 parse(p->cell(1), FLAG_ITEM, mode);
1525 //lyxerr << "read label: " << p->cell(1) << endl;
1528 cell->push_back(MathAtom(p.release()));
1529 //lyxerr << "read cell: " << cell << endl;
1533 else if (t.cs() == "lyxmathsym") {
1535 if (getToken().cat() != catBegin) {
1536 error("'{' expected in \\" + t.cs());
1541 CatCode cat = nextToken().cat();
1542 while (good() && (count || cat != catEnd)) {
1543 if (cat == catBegin)
1545 else if (cat == catEnd)
1547 cmd += getToken().asInput();
1548 cat = nextToken().cat();
1550 if (getToken().cat() != catEnd) {
1551 error("'}' expected in \\" + t.cs());
1555 cmd = Encodings::fromLaTeXCommand(cmd, rem);
1556 for (size_t i = 0; i < cmd.size(); ++i)
1557 cell->push_back(MathAtom(new InsetMathChar(cmd[i])));
1559 MathAtom at = createInsetMath(t.cs());
1560 cell->push_back(at);
1562 mathed_parse_cell(ar, '{' + rem + '}');
1567 else if (t.cs().size()) {
1568 latexkeys const * l = in_word_set(t.cs());
1570 if (l->inset == "big") {
1572 docstring const delim = getToken().asInput();
1573 if (InsetMathBig::isBigInsetDelim(delim))
1574 cell->push_back(MathAtom(
1575 new InsetMathBig(t.cs(), delim)));
1577 cell->push_back(createInsetMath(t.cs()));
1582 else if (l->inset == "font") {
1583 cell->push_back(createInsetMath(t.cs()));
1584 parse(cell->back().nucleus()->cell(0),
1585 FLAG_ITEM, asMode(mode, l->extra));
1588 else if (l->inset == "oldfont") {
1589 cell->push_back(createInsetMath(t.cs()));
1590 parse(cell->back().nucleus()->cell(0),
1591 flags | FLAG_ALIGN, asMode(mode, l->extra));
1592 if (prevToken().cat() != catAlign &&
1593 prevToken().cs() != "\\")
1598 else if (l->inset == "style") {
1599 cell->push_back(createInsetMath(t.cs()));
1600 parse(cell->back().nucleus()->cell(0),
1601 flags | FLAG_ALIGN, mode);
1602 if (prevToken().cat() != catAlign &&
1603 prevToken().cs() != "\\")
1609 MathAtom at = createInsetMath(t.cs());
1610 for (InsetMath::idx_type i = 0; i < at->nargs(); ++i)
1611 parse(at.nucleus()->cell(i),
1612 FLAG_ITEM, asMode(mode, l->extra));
1613 cell->push_back(at);
1618 bool is_unicode_symbol = false;
1619 if (mode == InsetMath::TEXT_MODE) {
1621 docstring cmd = prevToken().asInput();
1623 CatCode cat = nextToken().cat();
1624 if (cat == catBegin) {
1626 while (good() && (count || cat != catEnd)) {
1627 cat = nextToken().cat();
1628 cmd += getToken().asInput();
1630 if (cat == catBegin)
1632 else if (cat == catEnd)
1638 Encodings::fromLaTeXCommand(cmd, is_combining);
1640 if (cat == catLetter)
1642 cmd += getToken().asInput();
1644 if (cat == catLetter)
1646 c = Encodings::fromLaTeXCommand(cmd, is_combining);
1649 is_unicode_symbol = true;
1650 cell->push_back(MathAtom(new InsetMathChar(c)));
1652 while (num_tokens--)
1656 if (!is_unicode_symbol) {
1657 MathAtom at = createInsetMath(t.cs());
1658 InsetMath::mode_type m = mode;
1659 //if (m == InsetMath::UNDECIDED_MODE)
1660 //lyxerr << "default creation: m1: " << m << endl;
1661 if (at->currentMode() != InsetMath::UNDECIDED_MODE)
1662 m = at->currentMode();
1663 //lyxerr << "default creation: m2: " << m << endl;
1664 InsetMath::idx_type start = 0;
1665 // this fails on \bigg[...\bigg]
1667 //parse(opt, FLAG_OPTION, InsetMath::VERBATIM_MODE);
1670 // at.nucleus()->cell(0) = opt;
1672 for (InsetMath::idx_type i = start; i < at->nargs(); ++i) {
1673 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1676 cell->push_back(at);
1682 if (flags & FLAG_LEAVE) {
1683 flags &= ~FLAG_LEAVE;
1691 } // anonymous namespace
1694 void mathed_parser_warn_contents(bool warn)
1696 warn_unusual_contents = warn;
1700 void mathed_parse_cell(MathData & ar, docstring const & str)
1702 Parser(str).parse(ar, 0, InsetMath::MATH_MODE);
1706 void mathed_parse_cell(MathData & ar, istream & is)
1708 Parser(is).parse(ar, 0, InsetMath::MATH_MODE);
1712 bool mathed_parse_normal(MathAtom & t, docstring const & str)
1714 return Parser(str).parse(t);
1718 bool mathed_parse_normal(MathAtom & t, Lexer & lex)
1720 return Parser(lex).parse(t);
1724 void mathed_parse_normal(InsetMathGrid & grid, docstring const & str)
1726 Parser(str).parse1(grid, 0, InsetMath::MATH_MODE, false);
1732 fill(theCatcode, theCatcode + 128, catOther);
1733 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1734 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1736 theCatcode[int('\\')] = catEscape;
1737 theCatcode[int('{')] = catBegin;
1738 theCatcode[int('}')] = catEnd;
1739 theCatcode[int('$')] = catMath;
1740 theCatcode[int('&')] = catAlign;
1741 theCatcode[int('\n')] = catNewline;
1742 theCatcode[int('#')] = catParameter;
1743 theCatcode[int('^')] = catSuper;
1744 theCatcode[int('_')] = catSub;
1745 theCatcode[int(0x7f)] = catIgnore;
1746 theCatcode[int(' ')] = catSpace;
1747 theCatcode[int('\t')] = catSpace;
1748 theCatcode[int('\r')] = catNewline;
1749 theCatcode[int('~')] = catActive;
1750 theCatcode[int('%')] = catComment;