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}
41 #include "MathParser.h"
43 #include "InsetMathArray.h"
44 #include "InsetMathBig.h"
45 #include "InsetMathBrace.h"
46 #include "InsetMathChar.h"
47 #include "InsetMathColor.h"
48 #include "InsetMathComment.h"
49 #include "InsetMathDelim.h"
50 #include "InsetMathEnv.h"
51 #include "InsetMathKern.h"
52 #include "InsetMathMacro.h"
53 #include "InsetMathPar.h"
54 #include "InsetMathRef.h"
55 #include "InsetMathRoot.h"
56 #include "InsetMathScript.h"
57 #include "InsetMathSplit.h"
58 #include "InsetMathSqrt.h"
59 #include "InsetMathTabular.h"
60 #include "MathMacroTemplate.h"
61 #include "MathFactory.h"
62 #include "MathMacroArgument.h"
63 #include "MathSupport.h"
68 #include "support/convert.h"
81 using std::istringstream;
91 InsetMath::mode_type asMode(InsetMath::mode_type oldmode, string const & str)
93 //lyxerr << "handling mode: '" << str << "'" << endl;
94 if (str == "mathmode")
95 return InsetMath::MATH_MODE;
96 if (str == "textmode" || str == "forcetext")
97 return InsetMath::TEXT_MODE;
102 bool stared(string const & s)
104 string::size_type const n = s.size();
105 return n && s[n - 1] == '*';
110 * Add the row \p cellrow to \p grid.
111 * \returns wether the row could be added. Adding a row can fail for
112 * environments like "equation" that have a fixed number of rows.
114 bool addRow(InsetMathGrid & grid, InsetMathGrid::row_type & cellrow,
115 string const & vskip)
118 if (cellrow == grid.nrows()) {
119 //lyxerr << "adding row " << cellrow << endl;
120 grid.addRow(cellrow - 1);
121 if (cellrow == grid.nrows()) {
122 // We can't add a row to this grid, so let's
123 // append the content of this cell to the previous
125 // This does not happen in well formed .lyx files,
126 // but LyX versions 1.3.x and older could create
127 // such files and tex2lyx can still do that.
129 lyxerr << "ignoring extra row";
131 lyxerr << " with extra space " << vskip;
132 lyxerr << '.' << endl;
136 grid.vcrskip(LyXLength(vskip), cellrow - 1);
142 * Add the column \p cellcol to \p grid.
143 * \returns wether the column could be added. Adding a column can fail for
144 * environments like "eqnarray" that have a fixed number of columns.
146 bool addCol(InsetMathGrid & grid, InsetMathGrid::col_type & cellcol)
149 if (cellcol == grid.ncols()) {
150 //lyxerr << "adding column " << cellcol << endl;
151 grid.addCol(cellcol - 1);
152 if (cellcol == grid.ncols()) {
153 // We can't add a column to this grid, so let's
154 // append the content of this cell to the previous
156 // This does not happen in well formed .lyx files,
157 // but LyX versions 1.3.x and older could create
158 // such files and tex2lyx can still do that.
160 lyxerr << "ignoring extra column." << endl;
169 * Check wether the last row is empty and remove it if yes.
170 * Otherwise the following code
178 * will result in a grid with 3 rows (+ the dummy row that is always present),
179 * because the last '\\' opens a new row.
181 void delEmptyLastRow(InsetMathGrid & grid)
183 InsetMathGrid::row_type const row = grid.nrows() - 1;
184 for (InsetMathGrid::col_type col = 0; col < grid.ncols(); ++col) {
185 if (!grid.cell(grid.index(row, col)).empty())
188 // Copy the row information of the empty row (which would contain the
189 // last hline in the example above) to the dummy row and delete the
191 grid.rowinfo(row + 1) = grid.rowinfo(row);
196 // These are TeX's catcodes
198 catEscape, // 0 backslash
208 catSpace, // 10 space
209 catLetter, // 11 a-zA-Z
210 catOther, // 12 none of the above
213 catInvalid // 15 <delete>
216 CatCode theCatcode[256];
219 inline CatCode catcode(unsigned char c)
221 return theCatcode[c];
226 FLAG_ALIGN = 1 << 0, // next & or \\ ends the parsing process
227 FLAG_BRACE_LAST = 1 << 1, // next closing brace ends the parsing
228 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
229 FLAG_END = 1 << 3, // next \\end ends the parsing process
230 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
231 FLAG_TEXTMODE = 1 << 5, // we are in a box
232 FLAG_ITEM = 1 << 6, // read a (possibly braced token)
233 FLAG_LEAVE = 1 << 7, // leave the loop at the end
234 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
235 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
236 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
237 FLAG_OPTION = 1 << 11, // read [...] style option
238 FLAG_BRACED = 1 << 12 // read {...} style argument
243 // Helper class for parsing
249 Token() : cs_(), char_(0), cat_(catIgnore) {}
251 Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
253 Token(string const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
256 string const & cs() const { return cs_; }
258 CatCode cat() const { return cat_; }
260 char character() const { return char_; }
262 string asString() const { return cs_.size() ? cs_ : string(1, char_); }
264 string asInput() const { return cs_.size() ? '\\' + cs_ : string(1, char_); }
275 ostream & operator<<(ostream & os, Token const & t)
278 os << '\\' << t.cs();
279 else if (t.cat() == catLetter)
282 os << '[' << t.character() << ',' << t.cat() << ']';
290 typedef InsetMath::mode_type mode_type;
293 Parser(LyXLex & lex);
295 Parser(istream & is);
298 bool parse(MathAtom & at);
300 void parse(MathArray & array, unsigned flags, mode_type mode);
302 void parse1(InsetMathGrid & grid, unsigned flags, mode_type mode,
305 MathArray parse(unsigned flags, mode_type mode);
307 int lineno() const { return lineno_; }
313 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
314 /// get arg delimited by 'left' and 'right'
315 string getArg(char left, char right);
319 void error(string const & msg);
320 /// dump contents to screen
323 void tokenize(istream & is);
325 void tokenize(string const & s);
327 void skipSpaceTokens(istream & is, char c);
329 void push_back(Token const & t);
333 Token const & prevToken() const;
335 Token const & nextToken() const;
337 Token const & getToken();
338 /// skips spaces if any
341 void lex(string const & s);
345 string parse_verbatim_item();
347 string parse_verbatim_option();
352 vector<Token> tokens_;
355 /// Stack of active environments
356 vector<string> environments_;
360 Parser::Parser(LyXLex & lexer)
361 : lineno_(lexer.getLineNo()), pos_(0)
363 tokenize(lexer.getStream());
368 Parser::Parser(istream & is)
369 : lineno_(0), pos_(0)
375 void Parser::push_back(Token const & t)
377 tokens_.push_back(t);
381 void Parser::pop_back()
387 Token const & Parser::prevToken() const
389 static const Token dummy;
390 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
394 Token const & Parser::nextToken() const
396 static const Token dummy;
397 return good() ? tokens_[pos_] : dummy;
401 Token const & Parser::getToken()
403 static const Token dummy;
404 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
405 return good() ? tokens_[pos_++] : dummy;
409 void Parser::skipSpaces()
411 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
416 void Parser::putback()
422 bool Parser::good() const
424 return pos_ < tokens_.size();
428 char Parser::getChar()
431 error("The input stream is not well...");
432 return tokens_[pos_++].character();
436 string Parser::getArg(char left, char right)
446 while ((c = getChar()) != right && good())
453 void Parser::skipSpaceTokens(istream & is, char c)
455 // skip trailing spaces
456 while (catcode(c) == catSpace || catcode(c) == catNewline)
459 //lyxerr << "putting back: " << c << endl;
464 void Parser::tokenize(istream & is)
466 // eat everything up to the next \end_inset or end of stream
467 // and store it in s for further tokenization
472 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
473 s = s.substr(0, s.size() - 10);
477 // Remove the space after \end_inset
478 if (is.get(c) && c != ' ')
486 void Parser::tokenize(string const & buffer)
488 istringstream is(buffer, ios::in | ios::binary);
492 //lyxerr << "reading c: " << c << endl;
494 switch (catcode(c)) {
498 if (catcode(c) == catNewline)
499 ; //push_back(Token("par"));
501 push_back(Token('\n', catNewline));
509 while (is.get(c) && catcode(c) != catNewline)
519 error("unexpected end of input");
522 if (catcode(c) == catLetter) {
524 while (is.get(c) && catcode(c) == catLetter)
526 skipSpaceTokens(is, c);
535 push_back(Token(c, catcode(c)));
537 skipSpaceTokens(is, c);
542 lyxerr << "ignoring a char: " << int(c) << endl;
547 push_back(Token(c, catcode(c)));
557 void Parser::dump() const
559 lyxerr << "\nTokens: ";
560 for (unsigned i = 0; i < tokens_.size(); ++i) {
563 lyxerr << tokens_[i];
565 lyxerr << " pos: " << pos_ << endl;
569 void Parser::error(string const & msg)
571 lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
577 bool Parser::parse(MathAtom & at)
581 parse(ar, false, InsetMath::UNDECIDED_MODE);
582 if (ar.size() != 1 || ar.front()->getType() == hullNone) {
583 lyxerr << "unusual contents found: " << ar << endl;
584 at = MathAtom(new InsetMathPar(ar));
585 //if (at->nargs() > 0)
586 // at.nucleus()->cell(0) = ar;
588 // lyxerr << "unusual contents found: " << ar << endl;
596 string Parser::parse_verbatim_option()
600 if (nextToken().character() == '[') {
601 Token t = getToken();
602 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
603 if (t.cat() == catBegin) {
605 res += '{' + parse_verbatim_item() + '}';
614 string Parser::parse_verbatim_item()
618 if (nextToken().cat() == catBegin) {
619 Token t = getToken();
620 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
621 if (t.cat() == catBegin) {
623 res += '{' + parse_verbatim_item() + '}';
633 MathArray Parser::parse(unsigned flags, mode_type mode)
636 parse(ar, flags, mode);
641 void Parser::parse(MathArray & array, unsigned flags, mode_type mode)
643 InsetMathGrid grid(1, 1);
644 parse1(grid, flags, mode, false);
645 array = grid.cell(0);
649 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
652 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
656 void Parser::parse1(InsetMathGrid & grid, unsigned flags,
657 const mode_type mode, const bool numbered)
660 InsetMathGrid::row_type cellrow = 0;
661 InsetMathGrid::col_type cellcol = 0;
662 MathArray * cell = &grid.cell(grid.index(cellrow, cellcol));
664 if (grid.asHullInset())
665 grid.asHullInset()->numbered(cellrow, numbered);
668 //lyxerr << " flags: " << flags << endl;
669 //lyxerr << " mode: " << mode << endl;
670 //lyxerr << "grid: " << grid << endl;
673 Token const & t = getToken();
676 lyxerr << "t: " << t << " flags: " << flags << endl;
677 lyxerr << "mode: " << mode << endl;
682 if (flags & FLAG_ITEM) {
684 if (t.cat() == catBegin) {
685 // skip the brace and collect everything to the next matching
687 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
691 // handle only this single token, leave the loop if done
696 if (flags & FLAG_BRACED) {
697 if (t.cat() == catSpace)
700 if (t.cat() != catBegin) {
701 error("opening brace expected");
705 // skip the brace and collect everything to the next matching
707 flags = FLAG_BRACE_LAST;
711 if (flags & FLAG_OPTION) {
712 if (t.cat() == catOther && t.character() == '[') {
714 parse(ar, FLAG_BRACK_LAST, mode);
717 // no option found, put back token and we are done
726 if (t.cat() == catMath) {
727 if (mode != InsetMath::MATH_MODE) {
728 // we are inside some text mode thingy, so opening new math is allowed
729 Token const & n = getToken();
730 if (n.cat() == catMath) {
731 // TeX's $$...$$ syntax for displayed math
732 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
733 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
734 getToken(); // skip the second '$' token
736 // simple $...$ stuff
738 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
739 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
743 else if (flags & FLAG_SIMPLE) {
744 // this is the end of the formula
749 error("something strange in the parser");
754 else if (t.cat() == catLetter)
755 cell->push_back(MathAtom(new InsetMathChar(t.character())));
757 else if (t.cat() == catSpace && mode != InsetMath::MATH_MODE) {
758 if (cell->empty() || cell->back()->getChar() != ' ')
759 cell->push_back(MathAtom(new InsetMathChar(t.character())));
762 else if (t.cat() == catNewline && mode != InsetMath::MATH_MODE) {
763 if (cell->empty() || cell->back()->getChar() != ' ')
764 cell->push_back(MathAtom(new InsetMathChar(' ')));
767 else if (t.cat() == catParameter) {
768 Token const & n = getToken();
769 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
772 else if (t.cat() == catActive)
773 cell->push_back(MathAtom(new InsetMathChar(t.character())));
775 else if (t.cat() == catBegin) {
777 parse(ar, FLAG_BRACE_LAST, mode);
778 // do not create a BraceInset if they were written by LyX
779 // this helps to keep the annoyance of "a choose b" to a minimum
780 if (ar.size() == 1 && ar[0]->extraBraces())
783 cell->push_back(MathAtom(new InsetMathBrace(ar)));
786 else if (t.cat() == catEnd) {
787 if (flags & FLAG_BRACE_LAST)
789 error("found '}' unexpectedly");
790 //BOOST_ASSERT(false);
791 //add(cell, '}', LM_TC_TEX);
794 else if (t.cat() == catAlign) {
795 //lyxerr << " column now " << (cellcol + 1)
796 // << " max: " << grid.ncols() << endl;
797 if (flags & FLAG_ALIGN)
799 if (addCol(grid, cellcol))
800 cell = &grid.cell(grid.index(cellrow, cellcol));
803 else if (t.cat() == catSuper || t.cat() == catSub) {
804 bool up = (t.cat() == catSuper);
805 // we need no new script inset if the last thing was a scriptinset,
806 // which has that script already not the same script already
808 cell->push_back(MathAtom(new InsetMathScript(up)));
809 else if (cell->back()->asScriptInset() &&
810 !cell->back()->asScriptInset()->has(up))
811 cell->back().nucleus()->asScriptInset()->ensure(up);
812 else if (cell->back()->asScriptInset())
813 cell->push_back(MathAtom(new InsetMathScript(up)));
815 cell->back() = MathAtom(new InsetMathScript(cell->back(), up));
816 InsetMathScript * p = cell->back().nucleus()->asScriptInset();
817 // special handling of {}-bases
818 // is this always correct?
819 if (p->nuc().size() == 1
820 && p->nuc().back()->asBraceInset())
821 p->nuc() = p->nuc().back()->asNestInset()->cell(0);
822 parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
829 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
830 //lyxerr << "finished reading option" << endl;
834 else if (t.cat() == catOther)
835 cell->push_back(MathAtom(new InsetMathChar(t.character())));
837 else if (t.cat() == catComment) {
840 Token const & t = getToken();
841 if (t.cat() == catNewline)
845 cell->push_back(MathAtom(new InsetMathComment(s)));
853 else if (t.cs() == "lyxlock") {
855 cell->back().nucleus()->lock(true);
858 else if (t.cs() == "def" ||
859 t.cs() == "newcommand" ||
860 t.cs() == "renewcommand")
862 string const type = t.cs();
865 if (t.cs() == "def") {
867 name = getToken().cs();
871 while (good() && nextToken().cat() != catBegin) {
872 pars += getToken().cs();
876 //lyxerr << "read \\def parameter list '" << pars << "'" << endl;
878 } else { // t.cs() == "newcommand" || t.cs() == "renewcommand"
880 if (getToken().cat() != catBegin) {
881 error("'{' in \\newcommand expected (1) ");
885 name = getToken().cs();
887 if (getToken().cat() != catEnd) {
888 error("'}' in \\newcommand expected");
892 string const arg = getArg('[', ']');
894 nargs = convert<int>(arg);
899 parse(ar1, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
901 // we cannot handle recursive stuff at all
903 //test.push_back(createInsetMath(name));
904 //if (ar1.contains(test)) {
905 // error("we cannot handle recursive macros at all.");
909 // is a version for display attached?
912 if (nextToken().cat() == catBegin)
913 parse(ar2, FLAG_ITEM, InsetMath::MATH_MODE);
915 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, type,
919 else if (t.cs() == "(") {
920 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
921 parse2(cell->back(), FLAG_SIMPLE2, InsetMath::MATH_MODE, false);
924 else if (t.cs() == "[") {
925 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
926 parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false);
929 else if (t.cs() == "protect")
930 // ignore \\protect, will hopefully be re-added during output
933 else if (t.cs() == "end") {
934 if (flags & FLAG_END) {
935 // eat environment name
936 string const name = getArg('{', '}');
937 if (environments_.empty())
938 error("'found \\end{" + name +
939 "}' without matching '\\begin{" +
941 else if (name != environments_.back())
942 error("'\\end{" + name +
943 "}' does not match '\\begin{" +
944 environments_.back() + "}'");
946 environments_.pop_back();
947 // Delete empty last row in matrix
949 // If you abuse InsetMathGrid for
950 // non-matrix like structures you
951 // probably need to refine this test.
952 // Right now we only have to test for
953 // single line hull insets.
954 if (grid.nrows() > 1)
955 delEmptyLastRow(grid);
959 error("found 'end' unexpectedly");
962 else if (t.cs() == ")") {
963 if (flags & FLAG_SIMPLE2)
965 error("found '\\)' unexpectedly");
968 else if (t.cs() == "]") {
969 if (flags & FLAG_EQUATION)
971 error("found '\\]' unexpectedly");
974 else if (t.cs() == "\\") {
975 if (flags & FLAG_ALIGN)
977 if (addRow(grid, cellrow, getArg('[', ']'))) {
979 if (grid.asHullInset())
980 grid.asHullInset()->numbered(
982 cell = &grid.cell(grid.index(cellrow,
988 else if (t.cs() == "multicolumn") {
989 // extract column count and insert dummy cells
991 parse(count, FLAG_ITEM, mode);
993 if (!extractNumber(count, cols)) {
994 lyxerr << " can't extract number of cells from " << count << endl;
996 // resize the table if necessary
997 for (int i = 0; i < cols; ++i) {
998 if (addCol(grid, cellcol)) {
999 cell = &grid.cell(grid.index(
1001 // mark this as dummy
1002 grid.cellinfo(grid.index(
1003 cellrow, cellcol)).dummy_ = true;
1006 // the last cell is the real thing, not a dummy
1007 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
1009 // read special alignment
1011 parse(align, FLAG_ITEM, mode);
1012 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
1014 // parse the remaining contents into the "real" cell
1015 parse(*cell, FLAG_ITEM, mode);
1019 else if (t.cs() == "limits")
1022 else if (t.cs() == "nolimits")
1025 else if (t.cs() == "nonumber") {
1026 if (grid.asHullInset())
1027 grid.asHullInset()->numbered(cellrow, false);
1030 else if (t.cs() == "number") {
1031 if (grid.asHullInset())
1032 grid.asHullInset()->numbered(cellrow, true);
1035 else if (t.cs() == "hline") {
1036 grid.rowinfo(cellrow).lines_ ++;
1039 else if (t.cs() == "sqrt") {
1041 parse(ar, FLAG_OPTION, mode);
1043 cell->push_back(MathAtom(new InsetMathRoot));
1044 cell->back().nucleus()->cell(0) = ar;
1045 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1047 cell->push_back(MathAtom(new InsetMathSqrt));
1048 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1052 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1053 cell->push_back(createInsetMath(t.cs()));
1054 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1055 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1058 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
1059 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1060 cell->push_back(MathAtom(new RefInset(t.cs())));
1061 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1062 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1065 else if (t.cs() == "left") {
1067 Token const & tl = getToken();
1068 // \| and \Vert are equivalent, and InsetMathDelim
1070 // FIXME: fix this in InsetMathDelim itself!
1071 string const l = tl.cs() == "|" ? "Vert" : tl.asString();
1073 parse(ar, FLAG_RIGHT, mode);
1075 Token const & tr = getToken();
1076 string const r = tr.cs() == "|" ? "Vert" : tr.asString();
1077 cell->push_back(MathAtom(new InsetMathDelim(l, r, ar)));
1080 else if (t.cs() == "right") {
1081 if (flags & FLAG_RIGHT)
1083 //lyxerr << "got so far: '" << cell << "'" << endl;
1084 error("Unmatched right delimiter");
1088 else if (t.cs() == "begin") {
1089 string const name = getArg('{', '}');
1090 environments_.push_back(name);
1092 if (name == "array" || name == "subarray") {
1093 string const valign = parse_verbatim_option() + 'c';
1094 string const halign = parse_verbatim_item();
1095 cell->push_back(MathAtom(new InsetMathArray(name, valign[0], halign)));
1096 parse2(cell->back(), FLAG_END, mode, false);
1099 else if (name == "tabular") {
1100 string const valign = parse_verbatim_option() + 'c';
1101 string const halign = parse_verbatim_item();
1102 cell->push_back(MathAtom(new InsetMathTabular(name, valign[0], halign)));
1103 parse2(cell->back(), FLAG_END, InsetMath::TEXT_MODE, false);
1106 else if (name == "split" || name == "cases") {
1107 cell->push_back(createInsetMath(name));
1108 parse2(cell->back(), FLAG_END, mode, false);
1111 else if (name == "alignedat") {
1112 string const valign = parse_verbatim_option() + 'c';
1113 // ignore this for a while
1115 cell->push_back(MathAtom(new InsetMathSplit(name, valign[0])));
1116 parse2(cell->back(), FLAG_END, mode, false);
1119 else if (name == "math") {
1120 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
1121 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, true);
1124 else if (name == "equation" || name == "equation*"
1125 || name == "displaymath") {
1126 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
1127 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation"));
1130 else if (name == "eqnarray" || name == "eqnarray*") {
1131 cell->push_back(MathAtom(new InsetMathHull(hullEqnArray)));
1132 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1135 else if (name == "align" || name == "align*") {
1136 cell->push_back(MathAtom(new InsetMathHull(hullAlign)));
1137 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1140 else if (name == "flalign" || name == "flalign*") {
1141 cell->push_back(MathAtom(new InsetMathHull(hullFlAlign)));
1142 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1145 else if (name == "alignat" || name == "alignat*") {
1146 // ignore this for a while
1148 cell->push_back(MathAtom(new InsetMathHull(hullAlignAt)));
1149 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1152 else if (name == "xalignat" || name == "xalignat*") {
1153 // ignore this for a while
1155 cell->push_back(MathAtom(new InsetMathHull(hullXAlignAt)));
1156 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1159 else if (name == "xxalignat") {
1160 // ignore this for a while
1162 cell->push_back(MathAtom(new InsetMathHull(hullXXAlignAt)));
1163 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1166 else if (name == "multline" || name == "multline*") {
1167 cell->push_back(MathAtom(new InsetMathHull(hullMultline)));
1168 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1171 else if (name == "gather" || name == "gather*") {
1172 cell->push_back(MathAtom(new InsetMathHull(hullGather)));
1173 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1176 else if (latexkeys const * l = in_word_set(name)) {
1177 if (l->inset == "matrix") {
1178 cell->push_back(createInsetMath(name));
1179 parse2(cell->back(), FLAG_END, mode, false);
1180 } else if (l->inset == "split") {
1181 string const valign = parse_verbatim_option() + 'c';
1182 cell->push_back(MathAtom(new InsetMathSplit(name, valign[0])));
1183 parse2(cell->back(), FLAG_END, mode, false);
1186 lyxerr << "found math environment `" << name
1187 << "' in symbols file with unsupported inset `"
1188 << l->inset << "'." << endl;
1189 // create generic environment inset
1190 cell->push_back(MathAtom(new InsetMathEnv(name)));
1191 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1197 lyxerr << "found unknown math environment '" << name << "'" << endl;
1198 // create generic environment inset
1199 cell->push_back(MathAtom(new InsetMathEnv(name)));
1200 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1204 else if (t.cs() == "kern") {
1205 #ifdef WITH_WARNINGS
1210 Token const & t = getToken();
1216 if (isValidLength(s))
1219 cell->push_back(MathAtom(new InsetMathKern(s)));
1222 else if (t.cs() == "label") {
1223 // FIXME: This is swallowed in inline formulas
1224 string label = parse_verbatim_item();
1227 asArray(from_utf8(label), ar);
1228 if (grid.asHullInset()) {
1229 grid.asHullInset()->label(cellrow, label);
1231 cell->push_back(createInsetMath(t.cs()));
1232 cell->push_back(MathAtom(new InsetMathBrace(ar)));
1236 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1237 MathAtom at = createInsetMath(t.cs());
1238 at.nucleus()->cell(0) = *cell;
1240 parse(at.nucleus()->cell(1), flags, mode);
1241 cell->push_back(at);
1245 else if (t.cs() == "color") {
1246 string const color = parse_verbatim_item();
1247 cell->push_back(MathAtom(new InsetMathColor(true, color)));
1248 parse(cell->back().nucleus()->cell(0), flags, mode);
1252 else if (t.cs() == "textcolor") {
1253 string const color = parse_verbatim_item();
1254 cell->push_back(MathAtom(new InsetMathColor(false, color)));
1255 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1258 else if (t.cs() == "normalcolor") {
1259 cell->push_back(createInsetMath(t.cs()));
1260 parse(cell->back().nucleus()->cell(0), flags, mode);
1264 else if (t.cs() == "substack") {
1265 cell->push_back(createInsetMath(t.cs()));
1266 parse2(cell->back(), FLAG_ITEM, mode, false);
1269 else if (t.cs() == "xymatrix") {
1270 cell->push_back(createInsetMath(t.cs()));
1271 parse2(cell->back(), FLAG_ITEM, mode, false);
1274 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1275 cell->push_back(createInsetMath(t.cs()));
1276 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, InsetMath::TEXT_MODE);
1277 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, InsetMath::TEXT_MODE);
1278 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, InsetMath::TEXT_MODE);
1281 else if (t.cs() == "tag") {
1282 if (nextToken().character() == '*') {
1284 cell->push_back(createInsetMath(t.cs() + '*'));
1286 cell->push_back(createInsetMath(t.cs()));
1287 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1291 else if (t.cs() == "infer") {
1293 parse(ar, FLAG_OPTION, mode);
1294 cell->push_back(createInsetMath(t.cs()));
1295 parse2(cell->back(), FLAG_ITEM, mode, false);
1299 else if (1 && t.cs() == "ar") {
1300 auto_ptr<InsetMathXYArrow> p(new InsetMathXYArrow);
1301 // try to read target
1302 parse(p->cell(0), FLAG_OTPTION, mode);
1303 // try to read label
1304 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1305 p->up_ = nextToken().cat() == catSuper;
1307 parse(p->cell(1), FLAG_ITEM, mode);
1308 //lyxerr << "read label: " << p->cell(1) << endl;
1311 cell->push_back(MathAtom(p.release()));
1312 //lyxerr << "read cell: " << cell << endl;
1316 else if (t.cs().size()) {
1317 latexkeys const * l = in_word_set(t.cs());
1319 if (l->inset == "big") {
1321 string const delim = getToken().asInput();
1322 if (InsetMathBig::isBigInsetDelim(delim))
1323 cell->push_back(MathAtom(
1324 new InsetMathBig(t.cs(), delim)));
1326 cell->push_back(createInsetMath(t.cs()));
1331 else if (l->inset == "font") {
1332 cell->push_back(createInsetMath(t.cs()));
1333 parse(cell->back().nucleus()->cell(0),
1334 FLAG_ITEM, asMode(mode, l->extra));
1337 else if (l->inset == "oldfont") {
1338 cell->push_back(createInsetMath(t.cs()));
1339 parse(cell->back().nucleus()->cell(0),
1340 flags | FLAG_ALIGN, asMode(mode, l->extra));
1341 if (prevToken().cat() != catAlign &&
1342 prevToken().cs() != "\\")
1347 else if (l->inset == "style") {
1348 cell->push_back(createInsetMath(t.cs()));
1349 parse(cell->back().nucleus()->cell(0),
1350 flags | FLAG_ALIGN, mode);
1351 if (prevToken().cat() != catAlign &&
1352 prevToken().cs() != "\\")
1358 MathAtom at = createInsetMath(t.cs());
1359 for (InsetMath::idx_type i = 0; i < at->nargs(); ++i)
1360 parse(at.nucleus()->cell(i),
1361 FLAG_ITEM, asMode(mode, l->extra));
1362 cell->push_back(at);
1367 MathAtom at = createInsetMath(t.cs());
1368 InsetMath::mode_type m = mode;
1369 //if (m == InsetMath::UNDECIDED_MODE)
1370 //lyxerr << "default creation: m1: " << m << endl;
1371 if (at->currentMode() != InsetMath::UNDECIDED_MODE)
1372 m = at->currentMode();
1373 //lyxerr << "default creation: m2: " << m << endl;
1374 InsetMath::idx_type start = 0;
1375 // this fails on \bigg[...\bigg]
1377 //parse(opt, FLAG_OPTION, InsetMath::VERBATIM_MODE);
1380 // at.nucleus()->cell(0) = opt;
1382 for (InsetMath::idx_type i = start; i < at->nargs(); ++i) {
1383 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1386 cell->push_back(at);
1391 if (flags & FLAG_LEAVE) {
1392 flags &= ~FLAG_LEAVE;
1400 } // anonymous namespace
1403 void mathed_parse_cell(MathArray & ar, string const & str)
1405 istringstream is(str);
1406 mathed_parse_cell(ar, is);
1410 void mathed_parse_cell(MathArray & ar, istream & is)
1412 Parser(is).parse(ar, 0, InsetMath::MATH_MODE);
1416 bool mathed_parse_normal(MathAtom & t, string const & str)
1418 istringstream is(str);
1419 return Parser(is).parse(t);
1423 bool mathed_parse_normal(MathAtom & t, istream & is)
1425 return Parser(is).parse(t);
1429 bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
1431 return Parser(lex).parse(t);
1435 void mathed_parse_normal(InsetMathGrid & grid, string const & str)
1437 istringstream is(str);
1438 Parser(is).parse1(grid, 0, InsetMath::MATH_MODE, false);
1444 fill(theCatcode, theCatcode + 256, catOther);
1445 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1446 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1448 theCatcode[int('\\')] = catEscape;
1449 theCatcode[int('{')] = catBegin;
1450 theCatcode[int('}')] = catEnd;
1451 theCatcode[int('$')] = catMath;
1452 theCatcode[int('&')] = catAlign;
1453 theCatcode[int('\n')] = catNewline;
1454 theCatcode[int('#')] = catParameter;
1455 theCatcode[int('^')] = catSuper;
1456 theCatcode[int('_')] = catSub;
1457 theCatcode[int(0x7f)] = catIgnore;
1458 theCatcode[int(' ')] = catSpace;
1459 theCatcode[int('\t')] = catSpace;
1460 theCatcode[int('\r')] = catNewline;
1461 theCatcode[int('~')] = catActive;
1462 theCatcode[int('%')] = catComment;