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 "InsetMathKern.h"
51 #include "MathMacro.h"
52 #include "InsetMathPar.h"
53 #include "InsetMathRef.h"
54 #include "InsetMathRoot.h"
55 #include "InsetMathScript.h"
56 #include "InsetMathSplit.h"
57 #include "InsetMathSqrt.h"
58 #include "InsetMathTabular.h"
59 #include "MathMacroTemplate.h"
60 #include "MathFactory.h"
61 #include "MathMacroArgument.h"
62 #include "MathSupport.h"
67 #include "support/convert.h"
89 InsetMath::mode_type asMode(InsetMath::mode_type oldmode, docstring const & str)
91 //lyxerr << "handling mode: '" << str << "'" << endl;
92 if (str == "mathmode")
93 return InsetMath::MATH_MODE;
94 if (str == "textmode" || str == "forcetext")
95 return InsetMath::TEXT_MODE;
100 bool stared(docstring const & s)
102 size_t const n = s.size();
103 return n && s[n - 1] == '*';
108 * Add the row \p cellrow to \p grid.
109 * \returns wether the row could be added. Adding a row can fail for
110 * environments like "equation" that have a fixed number of rows.
112 bool addRow(InsetMathGrid & grid, InsetMathGrid::row_type & cellrow,
113 docstring const & vskip, bool allow_pagebreak = true)
116 if (cellrow == grid.nrows()) {
117 //lyxerr << "adding row " << cellrow << endl;
118 grid.addRow(cellrow - 1);
119 if (cellrow == grid.nrows()) {
120 // We can't add a row to this grid, so let's
121 // append the content of this cell to the previous
123 // This does not happen in well formed .lyx files,
124 // but LyX versions 1.3.x and older could create
125 // such files and tex2lyx can still do that.
127 lyxerr << "ignoring extra row";
129 lyxerr << " with extra space " << to_utf8(vskip);
130 if (!allow_pagebreak)
131 lyxerr << " with no page break allowed";
132 lyxerr << '.' << endl;
136 grid.vcrskip(Length(to_utf8(vskip)), cellrow - 1);
137 grid.rowinfo(cellrow - 1).allow_pagebreak_ = allow_pagebreak;
143 * Add the column \p cellcol to \p grid.
144 * \returns wether the column could be added. Adding a column can fail for
145 * environments like "eqnarray" that have a fixed number of columns.
147 bool addCol(InsetMathGrid & grid, InsetMathGrid::col_type & cellcol)
150 if (cellcol == grid.ncols()) {
151 //lyxerr << "adding column " << cellcol << endl;
152 grid.addCol(cellcol - 1);
153 if (cellcol == grid.ncols()) {
154 // We can't add a column to this grid, so let's
155 // append the content of this cell to the previous
157 // This does not happen in well formed .lyx files,
158 // but LyX versions 1.3.x and older could create
159 // such files and tex2lyx can still do that.
161 lyxerr << "ignoring extra column." << endl;
170 * Check wether the last row is empty and remove it if yes.
171 * Otherwise the following code
179 * will result in a grid with 3 rows (+ the dummy row that is always present),
180 * because the last '\\' opens a new row.
182 void delEmptyLastRow(InsetMathGrid & grid)
184 InsetMathGrid::row_type const row = grid.nrows() - 1;
185 for (InsetMathGrid::col_type col = 0; col < grid.ncols(); ++col) {
186 if (!grid.cell(grid.index(row, col)).empty())
189 // Copy the row information of the empty row (which would contain the
190 // last hline in the example above) to the dummy row and delete the
192 grid.rowinfo(row + 1) = grid.rowinfo(row);
197 // These are TeX's catcodes
199 catEscape, // 0 backslash
209 catSpace, // 10 space
210 catLetter, // 11 a-zA-Z
211 catOther, // 12 none of the above
214 catInvalid // 15 <delete>
217 CatCode theCatcode[128];
220 inline CatCode catcode(char_type c)
222 /* The only characters that are not catOther lie in the pure ASCII
223 * range. Therefore theCatcode has only 128 entries.
224 * TeX itself deals with 8bit characters, so if needed this table
225 * could be enlarged to 256 entries.
226 * Any larger value does not make sense, since the fact that we use
227 * unicode internally does not change Knuth's TeX engine.
228 * Apart from that a table for the full 21bit UCS4 range would waste
229 * too much memory. */
233 return theCatcode[c];
238 FLAG_ALIGN = 1 << 0, // next & or \\ ends the parsing process
239 FLAG_BRACE_LAST = 1 << 1, // next closing brace ends the parsing
240 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
241 FLAG_END = 1 << 3, // next \\end ends the parsing process
242 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
243 FLAG_TEXTMODE = 1 << 5, // we are in a box
244 FLAG_ITEM = 1 << 6, // read a (possibly braced token)
245 FLAG_LEAVE = 1 << 7, // leave the loop at the end
246 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
247 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
248 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
249 FLAG_OPTION = 1 << 11, // read [...] style option
250 FLAG_BRACED = 1 << 12 // read {...} style argument
255 // Helper class for parsing
261 Token() : cs_(), char_(0), cat_(catIgnore) {}
263 Token(char_type c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
265 explicit Token(docstring const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
268 docstring const & cs() const { return cs_; }
270 CatCode cat() const { return cat_; }
272 char_type character() const { return char_; }
274 docstring asString() const { return cs_.size() ? cs_ : docstring(1, char_); }
276 docstring asInput() const { return cs_.size() ? '\\' + cs_ : docstring(1, char_); }
288 ostream & operator<<(ostream & os, Token const & t)
291 docstring const & cs = t.cs();
292 // FIXME: For some strange reason, the stream operator instanciate
293 // a new Token before outputting the contents of t.cs().
294 // Because of this the line
296 // below becomes recursive.
297 // In order to avoid that we return early:
300 os << '\\' << to_utf8(cs);
302 else if (t.cat() == catLetter)
305 os << '[' << t.character() << ',' << t.cat() << ']';
313 typedef InsetMath::mode_type mode_type;
317 /// Only use this for reading from .lyx file format, for the reason
318 /// see Parser::tokenize(std::istream &).
319 Parser(istream & is);
321 Parser(docstring const & str);
324 bool parse(MathAtom & at);
326 void parse(MathData & array, unsigned flags, mode_type mode);
328 void parse1(InsetMathGrid & grid, unsigned flags, mode_type mode,
331 MathData parse(unsigned flags, mode_type mode);
333 int lineno() const { return lineno_; }
339 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
340 /// get arg delimited by 'left' and 'right'
341 docstring getArg(char_type left, char_type right);
345 void error(string const & msg);
346 void error(docstring const & msg) { error(to_utf8(msg)); }
347 /// dump contents to screen
349 /// Only use this for reading from .lyx file format (see
350 /// implementation for reason)
351 void tokenize(istream & is);
353 void tokenize(docstring const & s);
355 void skipSpaceTokens(idocstream & is, char_type c);
357 void push_back(Token const & t);
361 Token const & prevToken() const;
363 Token const & nextToken() const;
365 Token const & getToken();
366 /// skips spaces if any
369 void lex(docstring const & s);
373 docstring parse_verbatim_item();
375 docstring parse_verbatim_option();
380 vector<Token> tokens_;
383 /// Stack of active environments
384 vector<docstring> environments_;
388 Parser::Parser(Lexer & lexer)
389 : lineno_(lexer.getLineNo()), pos_(0)
391 tokenize(lexer.getStream());
396 Parser::Parser(istream & is)
397 : lineno_(0), pos_(0)
403 Parser::Parser(docstring const & str)
404 : lineno_(0), pos_(0)
410 void Parser::push_back(Token const & t)
412 tokens_.push_back(t);
416 void Parser::pop_back()
422 Token const & Parser::prevToken() const
424 static const Token dummy;
425 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
429 Token const & Parser::nextToken() const
431 static const Token dummy;
432 return good() ? tokens_[pos_] : dummy;
436 Token const & Parser::getToken()
438 static const Token dummy;
439 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
440 return good() ? tokens_[pos_++] : dummy;
444 void Parser::skipSpaces()
446 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
451 void Parser::putback()
457 bool Parser::good() const
459 return pos_ < tokens_.size();
463 char_type Parser::getChar()
466 error("The input stream is not well...");
467 return tokens_[pos_++].character();
471 docstring Parser::getArg(char_type left, char_type right)
476 char_type c = getChar();
481 while ((c = getChar()) != right && good())
488 void Parser::skipSpaceTokens(idocstream & is, char_type c)
490 // skip trailing spaces
491 while (catcode(c) == catSpace || catcode(c) == catNewline)
494 //lyxerr << "putting back: " << c << endl;
499 void Parser::tokenize(istream & is)
501 // eat everything up to the next \end_inset or end of stream
502 // and store it in s for further tokenization
507 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
508 s = s.substr(0, s.size() - 10);
512 // Remove the space after \end_inset
513 if (is.get(c) && c != ' ')
517 tokenize(from_utf8(s));
521 void Parser::tokenize(docstring const & buffer)
523 idocstringstream is(buffer, ios::in | ios::binary);
527 //lyxerr << "reading c: " << c << endl;
529 switch (catcode(c)) {
533 if (catcode(c) == catNewline)
534 ; //push_back(Token("par"));
536 push_back(Token('\n', catNewline));
544 while (is.get(c) && catcode(c) != catNewline)
554 error("unexpected end of input");
557 if (catcode(c) == catLetter) {
559 while (is.get(c) && catcode(c) == catLetter)
561 skipSpaceTokens(is, c);
570 push_back(Token(c, catcode(c)));
572 skipSpaceTokens(is, c);
577 lyxerr << "ignoring a char: " << int(c) << endl;
582 push_back(Token(c, catcode(c)));
592 void Parser::dump() const
594 lyxerr << "\nTokens: ";
595 for (unsigned i = 0; i < tokens_.size(); ++i) {
598 lyxerr << tokens_[i];
600 lyxerr << " pos: " << pos_ << endl;
604 void Parser::error(string const & msg)
606 lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
612 bool Parser::parse(MathAtom & at)
616 parse(ar, false, InsetMath::UNDECIDED_MODE);
617 if (ar.size() != 1 || ar.front()->getType() == hullNone) {
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 //BOOST_ASSERT(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() == "def" ||
901 t.cs() == "newcommand" ||
902 t.cs() == "renewcommand")
904 docstring const type = t.cs();
907 if (t.cs() == "def") {
909 name = getToken().cs();
913 while (good() && nextToken().cat() != catBegin) {
914 pars += getToken().cs();
918 //lyxerr << "read \\def parameter list '" << pars << "'" << endl;
920 } else { // t.cs() == "newcommand" || t.cs() == "renewcommand"
922 if (getToken().cat() != catBegin) {
923 error("'{' in \\newcommand expected (1) ");
927 name = getToken().cs();
929 if (getToken().cat() != catEnd) {
930 error("'}' in \\newcommand expected");
934 docstring const arg = getArg('[', ']');
936 nargs = convert<int>(arg);
941 parse(ar1, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
943 // we cannot handle recursive stuff at all
945 //test.push_back(createInsetMath(name));
946 //if (ar1.contains(test)) {
947 // error("we cannot handle recursive macros at all.");
951 // is a version for display attached?
954 if (nextToken().cat() == catBegin)
955 parse(ar2, FLAG_ITEM, InsetMath::MATH_MODE);
957 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, type,
961 else if (t.cs() == "(") {
962 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
963 parse2(cell->back(), FLAG_SIMPLE2, InsetMath::MATH_MODE, false);
966 else if (t.cs() == "[") {
967 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
968 parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false);
971 else if (t.cs() == "protect")
972 // ignore \\protect, will hopefully be re-added during output
975 else if (t.cs() == "end") {
976 if (flags & FLAG_END) {
977 // eat environment name
978 docstring const name = getArg('{', '}');
979 if (environments_.empty())
980 error("'found \\end{" + name +
981 "}' without matching '\\begin{" +
983 else if (name != environments_.back())
984 error("'\\end{" + name +
985 "}' does not match '\\begin{" +
986 environments_.back() + "}'");
988 environments_.pop_back();
989 // Delete empty last row in matrix
991 // If you abuse InsetMathGrid for
992 // non-matrix like structures you
993 // probably need to refine this test.
994 // Right now we only have to test for
995 // single line hull insets.
996 if (grid.nrows() > 1)
997 delEmptyLastRow(grid);
1001 error("found 'end' unexpectedly");
1004 else if (t.cs() == ")") {
1005 if (flags & FLAG_SIMPLE2)
1007 error("found '\\)' unexpectedly");
1010 else if (t.cs() == "]") {
1011 if (flags & FLAG_EQUATION)
1013 error("found '\\]' unexpectedly");
1016 else if (t.cs() == "\\") {
1017 if (flags & FLAG_ALIGN)
1020 if (nextToken().asInput() == "*") {
1022 added = addRow(grid, cellrow, docstring(), false);
1024 added = addRow(grid, cellrow, getArg('[', ']'));
1027 if (grid.asHullInset())
1028 grid.asHullInset()->numbered(
1030 cell = &grid.cell(grid.index(cellrow,
1036 else if (t.cs() == "multicolumn") {
1037 // extract column count and insert dummy cells
1039 parse(count, FLAG_ITEM, mode);
1041 if (!extractNumber(count, cols)) {
1042 lyxerr << " can't extract number of cells from " << count << endl;
1044 // resize the table if necessary
1045 for (int i = 0; i < cols; ++i) {
1046 if (addCol(grid, cellcol)) {
1047 cell = &grid.cell(grid.index(
1049 // mark this as dummy
1050 grid.cellinfo(grid.index(
1051 cellrow, cellcol)).dummy_ = true;
1054 // the last cell is the real thing, not a dummy
1055 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
1057 // read special alignment
1059 parse(align, FLAG_ITEM, mode);
1060 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
1062 // parse the remaining contents into the "real" cell
1063 parse(*cell, FLAG_ITEM, mode);
1067 else if (t.cs() == "limits")
1070 else if (t.cs() == "nolimits")
1073 else if (t.cs() == "nonumber") {
1074 if (grid.asHullInset())
1075 grid.asHullInset()->numbered(cellrow, false);
1078 else if (t.cs() == "number") {
1079 if (grid.asHullInset())
1080 grid.asHullInset()->numbered(cellrow, true);
1083 else if (t.cs() == "hline") {
1084 grid.rowinfo(cellrow).lines_ ++;
1087 else if (t.cs() == "sqrt") {
1089 parse(ar, FLAG_OPTION, mode);
1091 cell->push_back(MathAtom(new InsetMathRoot));
1092 cell->back().nucleus()->cell(0) = ar;
1093 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1095 cell->push_back(MathAtom(new InsetMathSqrt));
1096 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1100 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1101 cell->push_back(createInsetMath(t.cs()));
1102 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1103 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1106 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
1107 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1108 cell->push_back(MathAtom(new InsetMathRef(t.cs())));
1109 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1110 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1113 else if (t.cs() == "left") {
1115 Token const & tl = getToken();
1116 // \| and \Vert are equivalent, and InsetMathDelim
1118 // FIXME: fix this in InsetMathDelim itself!
1119 docstring const l = tl.cs() == "|" ? from_ascii("Vert") : tl.asString();
1121 parse(ar, FLAG_RIGHT, mode);
1125 Token const & tr = getToken();
1126 docstring const r = tr.cs() == "|" ? from_ascii("Vert") : tr.asString();
1127 cell->push_back(MathAtom(new InsetMathDelim(l, r, ar)));
1130 else if (t.cs() == "right") {
1131 if (flags & FLAG_RIGHT)
1133 //lyxerr << "got so far: '" << cell << "'" << endl;
1134 error("Unmatched right delimiter");
1138 else if (t.cs() == "begin") {
1139 docstring const name = getArg('{', '}');
1140 environments_.push_back(name);
1142 if (name == "array" || name == "subarray") {
1143 docstring const valign = parse_verbatim_option() + 'c';
1144 docstring const halign = parse_verbatim_item();
1145 cell->push_back(MathAtom(new InsetMathArray(name, (char)valign[0], halign)));
1146 parse2(cell->back(), FLAG_END, mode, false);
1149 else if (name == "tabular") {
1150 docstring const valign = parse_verbatim_option() + 'c';
1151 docstring const halign = parse_verbatim_item();
1152 cell->push_back(MathAtom(new InsetMathTabular(name, (char)valign[0], halign)));
1153 parse2(cell->back(), FLAG_END, InsetMath::TEXT_MODE, false);
1156 else if (name == "split" || name == "cases") {
1157 cell->push_back(createInsetMath(name));
1158 parse2(cell->back(), FLAG_END, mode, false);
1161 else if (name == "alignedat") {
1162 docstring const valign = parse_verbatim_option() + 'c';
1163 // ignore this for a while
1165 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1166 parse2(cell->back(), FLAG_END, mode, false);
1169 else if (name == "math") {
1170 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
1171 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, true);
1174 else if (name == "equation" || name == "equation*"
1175 || name == "displaymath") {
1176 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
1177 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation"));
1180 else if (name == "eqnarray" || name == "eqnarray*") {
1181 cell->push_back(MathAtom(new InsetMathHull(hullEqnArray)));
1182 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1185 else if (name == "align" || name == "align*") {
1186 cell->push_back(MathAtom(new InsetMathHull(hullAlign)));
1187 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1190 else if (name == "flalign" || name == "flalign*") {
1191 cell->push_back(MathAtom(new InsetMathHull(hullFlAlign)));
1192 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1195 else if (name == "alignat" || name == "alignat*") {
1196 // ignore this for a while
1198 cell->push_back(MathAtom(new InsetMathHull(hullAlignAt)));
1199 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1202 else if (name == "xalignat" || name == "xalignat*") {
1203 // ignore this for a while
1205 cell->push_back(MathAtom(new InsetMathHull(hullXAlignAt)));
1206 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1209 else if (name == "xxalignat") {
1210 // ignore this for a while
1212 cell->push_back(MathAtom(new InsetMathHull(hullXXAlignAt)));
1213 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1216 else if (name == "multline" || name == "multline*") {
1217 cell->push_back(MathAtom(new InsetMathHull(hullMultline)));
1218 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1221 else if (name == "gather" || name == "gather*") {
1222 cell->push_back(MathAtom(new InsetMathHull(hullGather)));
1223 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1226 else if (latexkeys const * l = in_word_set(name)) {
1227 if (l->inset == "matrix") {
1228 cell->push_back(createInsetMath(name));
1229 parse2(cell->back(), FLAG_END, mode, false);
1230 } else if (l->inset == "split") {
1231 docstring const valign = parse_verbatim_option() + 'c';
1232 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1233 parse2(cell->back(), FLAG_END, mode, false);
1236 lyxerr << "found math environment `" << to_utf8(name)
1237 << "' in symbols file with unsupported inset `"
1238 << to_utf8(l->inset) << "'." << endl;
1239 // create generic environment inset
1240 cell->push_back(MathAtom(new InsetMathEnv(name)));
1241 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1247 lyxerr << "found unknown math environment '" << to_utf8(name)
1249 // create generic environment inset
1250 cell->push_back(MathAtom(new InsetMathEnv(name)));
1251 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1255 else if (t.cs() == "kern") {
1259 Token const & t = getToken();
1265 if (isValidLength(to_utf8(s)))
1268 cell->push_back(MathAtom(new InsetMathKern(s)));
1271 else if (t.cs() == "label") {
1272 // FIXME: This is swallowed in inline formulas
1273 docstring label = parse_verbatim_item();
1276 if (grid.asHullInset()) {
1277 grid.asHullInset()->label(cellrow, label);
1279 cell->push_back(createInsetMath(t.cs()));
1280 cell->push_back(MathAtom(new InsetMathBrace(ar)));
1284 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1285 MathAtom at = createInsetMath(t.cs());
1286 at.nucleus()->cell(0) = *cell;
1288 parse(at.nucleus()->cell(1), flags, mode);
1289 cell->push_back(at);
1293 else if (t.cs() == "color") {
1294 docstring const color = parse_verbatim_item();
1295 cell->push_back(MathAtom(new InsetMathColor(true, color)));
1296 parse(cell->back().nucleus()->cell(0), flags, mode);
1300 else if (t.cs() == "textcolor") {
1301 docstring const color = parse_verbatim_item();
1302 cell->push_back(MathAtom(new InsetMathColor(false, color)));
1303 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1306 else if (t.cs() == "normalcolor") {
1307 cell->push_back(createInsetMath(t.cs()));
1308 parse(cell->back().nucleus()->cell(0), flags, mode);
1312 else if (t.cs() == "substack") {
1313 cell->push_back(createInsetMath(t.cs()));
1314 parse2(cell->back(), FLAG_ITEM, mode, false);
1317 else if (t.cs() == "xymatrix") {
1318 odocstringstream os;
1319 while (good() && nextToken().cat() != catBegin)
1320 os << getToken().asInput();
1321 cell->push_back(createInsetMath(t.cs() + os.str()));
1322 parse2(cell->back(), FLAG_ITEM, mode, false);
1325 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1326 cell->push_back(createInsetMath(t.cs()));
1327 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, InsetMath::TEXT_MODE);
1328 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, InsetMath::TEXT_MODE);
1329 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, InsetMath::TEXT_MODE);
1332 else if (t.cs() == "tag") {
1333 if (nextToken().character() == '*') {
1335 cell->push_back(createInsetMath(t.cs() + '*'));
1337 cell->push_back(createInsetMath(t.cs()));
1338 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1342 else if (t.cs() == "infer") {
1344 parse(ar, FLAG_OPTION, mode);
1345 cell->push_back(createInsetMath(t.cs()));
1346 parse2(cell->back(), FLAG_ITEM, mode, false);
1350 else if (1 && t.cs() == "ar") {
1351 auto_ptr<InsetMathXYArrow> p(new InsetMathXYArrow);
1352 // try to read target
1353 parse(p->cell(0), FLAG_OTPTION, mode);
1354 // try to read label
1355 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1356 p->up_ = nextToken().cat() == catSuper;
1358 parse(p->cell(1), FLAG_ITEM, mode);
1359 //lyxerr << "read label: " << p->cell(1) << endl;
1362 cell->push_back(MathAtom(p.release()));
1363 //lyxerr << "read cell: " << cell << endl;
1367 else if (t.cs().size()) {
1368 latexkeys const * l = in_word_set(t.cs());
1370 if (l->inset == "big") {
1372 docstring const delim = getToken().asInput();
1373 if (InsetMathBig::isBigInsetDelim(delim))
1374 cell->push_back(MathAtom(
1375 new InsetMathBig(t.cs(), delim)));
1377 cell->push_back(createInsetMath(t.cs()));
1382 else if (l->inset == "font") {
1383 cell->push_back(createInsetMath(t.cs()));
1384 parse(cell->back().nucleus()->cell(0),
1385 FLAG_ITEM, asMode(mode, l->extra));
1388 else if (l->inset == "oldfont") {
1389 cell->push_back(createInsetMath(t.cs()));
1390 parse(cell->back().nucleus()->cell(0),
1391 flags | FLAG_ALIGN, asMode(mode, l->extra));
1392 if (prevToken().cat() != catAlign &&
1393 prevToken().cs() != "\\")
1398 else if (l->inset == "style") {
1399 cell->push_back(createInsetMath(t.cs()));
1400 parse(cell->back().nucleus()->cell(0),
1401 flags | FLAG_ALIGN, mode);
1402 if (prevToken().cat() != catAlign &&
1403 prevToken().cs() != "\\")
1409 MathAtom at = createInsetMath(t.cs());
1410 for (InsetMath::idx_type i = 0; i < at->nargs(); ++i)
1411 parse(at.nucleus()->cell(i),
1412 FLAG_ITEM, asMode(mode, l->extra));
1413 cell->push_back(at);
1418 MathAtom at = createInsetMath(t.cs());
1419 InsetMath::mode_type m = mode;
1420 //if (m == InsetMath::UNDECIDED_MODE)
1421 //lyxerr << "default creation: m1: " << m << endl;
1422 if (at->currentMode() != InsetMath::UNDECIDED_MODE)
1423 m = at->currentMode();
1424 //lyxerr << "default creation: m2: " << m << endl;
1425 InsetMath::idx_type start = 0;
1426 // this fails on \bigg[...\bigg]
1428 //parse(opt, FLAG_OPTION, InsetMath::VERBATIM_MODE);
1431 // at.nucleus()->cell(0) = opt;
1433 for (InsetMath::idx_type i = start; i < at->nargs(); ++i) {
1434 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1437 cell->push_back(at);
1442 if (flags & FLAG_LEAVE) {
1443 flags &= ~FLAG_LEAVE;
1451 } // anonymous namespace
1454 void mathed_parse_cell(MathData & ar, docstring const & str)
1456 Parser(str).parse(ar, 0, InsetMath::MATH_MODE);
1460 void mathed_parse_cell(MathData & ar, istream & is)
1462 Parser(is).parse(ar, 0, InsetMath::MATH_MODE);
1466 bool mathed_parse_normal(MathAtom & t, docstring const & str)
1468 return Parser(str).parse(t);
1472 bool mathed_parse_normal(MathAtom & t, Lexer & lex)
1474 return Parser(lex).parse(t);
1478 void mathed_parse_normal(InsetMathGrid & grid, docstring const & str)
1480 Parser(str).parse1(grid, 0, InsetMath::MATH_MODE, false);
1486 fill(theCatcode, theCatcode + 128, catOther);
1487 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1488 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1490 theCatcode[int('\\')] = catEscape;
1491 theCatcode[int('{')] = catBegin;
1492 theCatcode[int('}')] = catEnd;
1493 theCatcode[int('$')] = catMath;
1494 theCatcode[int('&')] = catAlign;
1495 theCatcode[int('\n')] = catNewline;
1496 theCatcode[int('#')] = catParameter;
1497 theCatcode[int('^')] = catSuper;
1498 theCatcode[int('_')] = catSub;
1499 theCatcode[int(0x7f)] = catIgnore;
1500 theCatcode[int(' ')] = catSpace;
1501 theCatcode[int('\t')] = catSpace;
1502 theCatcode[int('\r')] = catNewline;
1503 theCatcode[int('~')] = catActive;
1504 theCatcode[int('%')] = catComment;