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 "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/convert.h"
90 InsetMath::mode_type asMode(InsetMath::mode_type oldmode, docstring const & str)
92 //lyxerr << "handling mode: '" << str << "'" << endl;
93 if (str == "mathmode")
94 return InsetMath::MATH_MODE;
95 if (str == "textmode" || str == "forcetext")
96 return InsetMath::TEXT_MODE;
101 bool stared(docstring const & s)
103 size_t const n = s.size();
104 return n && s[n - 1] == '*';
109 * Add the row \p cellrow to \p grid.
110 * \returns wether the row could be added. Adding a row can fail for
111 * environments like "equation" that have a fixed number of rows.
113 bool addRow(InsetMathGrid & grid, InsetMathGrid::row_type & cellrow,
114 docstring const & vskip, bool allow_pagebreak = true)
117 if (cellrow == grid.nrows()) {
118 //lyxerr << "adding row " << cellrow << endl;
119 grid.addRow(cellrow - 1);
120 if (cellrow == grid.nrows()) {
121 // We can't add a row to this grid, so let's
122 // append the content of this cell to the previous
124 // This does not happen in well formed .lyx files,
125 // but LyX versions 1.3.x and older could create
126 // such files and tex2lyx can still do that.
128 lyxerr << "ignoring extra row";
130 lyxerr << " with extra space " << to_utf8(vskip);
131 if (!allow_pagebreak)
132 lyxerr << " with no page break allowed";
133 lyxerr << '.' << endl;
137 grid.vcrskip(Length(to_utf8(vskip)), cellrow - 1);
138 grid.rowinfo(cellrow - 1).allow_pagebreak_ = allow_pagebreak;
144 * Add the column \p cellcol to \p grid.
145 * \returns wether the column could be added. Adding a column can fail for
146 * environments like "eqnarray" that have a fixed number of columns.
148 bool addCol(InsetMathGrid & grid, InsetMathGrid::col_type & cellcol)
151 if (cellcol == grid.ncols()) {
152 //lyxerr << "adding column " << cellcol << endl;
153 grid.addCol(cellcol - 1);
154 if (cellcol == grid.ncols()) {
155 // We can't add a column to this grid, so let's
156 // append the content of this cell to the previous
158 // This does not happen in well formed .lyx files,
159 // but LyX versions 1.3.x and older could create
160 // such files and tex2lyx can still do that.
162 lyxerr << "ignoring extra column." << endl;
171 * Check wether the last row is empty and remove it if yes.
172 * Otherwise the following code
180 * will result in a grid with 3 rows (+ the dummy row that is always present),
181 * because the last '\\' opens a new row.
183 void delEmptyLastRow(InsetMathGrid & grid)
185 InsetMathGrid::row_type const row = grid.nrows() - 1;
186 for (InsetMathGrid::col_type col = 0; col < grid.ncols(); ++col) {
187 if (!grid.cell(grid.index(row, col)).empty())
190 // Copy the row information of the empty row (which would contain the
191 // last hline in the example above) to the dummy row and delete the
193 grid.rowinfo(row + 1) = grid.rowinfo(row);
198 // These are TeX's catcodes
200 catEscape, // 0 backslash
210 catSpace, // 10 space
211 catLetter, // 11 a-zA-Z
212 catOther, // 12 none of the above
215 catInvalid // 15 <delete>
218 CatCode theCatcode[128];
221 inline CatCode catcode(char_type c)
223 /* The only characters that are not catOther lie in the pure ASCII
224 * range. Therefore theCatcode has only 128 entries.
225 * TeX itself deals with 8bit characters, so if needed this table
226 * could be enlarged to 256 entries.
227 * Any larger value does not make sense, since the fact that we use
228 * unicode internally does not change Knuth's TeX engine.
229 * Apart from that a table for the full 21bit UCS4 range would waste
230 * too much memory. */
234 return theCatcode[c];
239 FLAG_ALIGN = 1 << 0, // next & or \\ ends the parsing process
240 FLAG_BRACE_LAST = 1 << 1, // next closing brace ends the parsing
241 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
242 FLAG_END = 1 << 3, // next \\end ends the parsing process
243 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
244 FLAG_TEXTMODE = 1 << 5, // we are in a box
245 FLAG_ITEM = 1 << 6, // read a (possibly braced token)
246 FLAG_LEAVE = 1 << 7, // leave the loop at the end
247 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
248 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
249 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
250 FLAG_OPTION = 1 << 11, // read [...] style option
251 FLAG_BRACED = 1 << 12 // read {...} style argument
256 // Helper class for parsing
262 Token() : cs_(), char_(0), cat_(catIgnore) {}
264 Token(char_type c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
266 explicit Token(docstring const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
269 docstring const & cs() const { return cs_; }
271 CatCode cat() const { return cat_; }
273 char_type character() const { return char_; }
275 docstring asString() const { return cs_.size() ? cs_ : docstring(1, char_); }
277 docstring asInput() const { return cs_.size() ? '\\' + cs_ : docstring(1, char_); }
289 ostream & operator<<(ostream & os, Token const & t)
292 docstring const & cs = t.cs();
293 // FIXME: For some strange reason, the stream operator instanciate
294 // a new Token before outputting the contents of t.cs().
295 // Because of this the line
297 // below becomes recursive.
298 // In order to avoid that we return early:
301 os << '\\' << to_utf8(cs);
303 else if (t.cat() == catLetter)
306 os << '[' << t.character() << ',' << t.cat() << ']';
314 typedef InsetMath::mode_type mode_type;
318 /// Only use this for reading from .lyx file format, for the reason
319 /// see Parser::tokenize(std::istream &).
320 Parser(istream & is);
322 Parser(docstring const & str);
325 bool parse(MathAtom & at);
327 void parse(MathData & array, unsigned flags, mode_type mode);
329 void parse1(InsetMathGrid & grid, unsigned flags, mode_type mode,
332 MathData parse(unsigned flags, mode_type mode);
334 int lineno() const { return lineno_; }
340 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
341 /// get arg delimited by 'left' and 'right'
342 docstring getArg(char_type left, char_type right);
346 void error(string const & msg);
347 void error(docstring const & msg) { error(to_utf8(msg)); }
348 /// dump contents to screen
350 /// Only use this for reading from .lyx file format (see
351 /// implementation for reason)
352 void tokenize(istream & is);
354 void tokenize(docstring const & s);
356 void skipSpaceTokens(idocstream & is, char_type c);
358 void push_back(Token const & t);
362 Token const & prevToken() const;
364 Token const & nextToken() const;
366 Token const & getToken();
367 /// skips spaces if any
370 void lex(docstring const & s);
374 docstring parse_verbatim_item();
376 docstring parse_verbatim_option();
381 vector<Token> tokens_;
384 /// Stack of active environments
385 vector<docstring> environments_;
389 Parser::Parser(Lexer & lexer)
390 : lineno_(lexer.getLineNo()), pos_(0)
392 tokenize(lexer.getStream());
397 Parser::Parser(istream & is)
398 : lineno_(0), pos_(0)
404 Parser::Parser(docstring const & str)
405 : lineno_(0), pos_(0)
411 void Parser::push_back(Token const & t)
413 tokens_.push_back(t);
417 void Parser::pop_back()
423 Token const & Parser::prevToken() const
425 static const Token dummy;
426 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
430 Token const & Parser::nextToken() const
432 static const Token dummy;
433 return good() ? tokens_[pos_] : dummy;
437 Token const & Parser::getToken()
439 static const Token dummy;
440 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
441 return good() ? tokens_[pos_++] : dummy;
445 void Parser::skipSpaces()
447 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
452 void Parser::putback()
458 bool Parser::good() const
460 return pos_ < tokens_.size();
464 char_type Parser::getChar()
467 error("The input stream is not well...");
468 return tokens_[pos_++].character();
472 docstring Parser::getArg(char_type left, char_type right)
477 char_type c = getChar();
482 while ((c = getChar()) != right && good())
489 void Parser::skipSpaceTokens(idocstream & is, char_type c)
491 // skip trailing spaces
492 while (catcode(c) == catSpace || catcode(c) == catNewline)
495 //lyxerr << "putting back: " << c << endl;
500 void Parser::tokenize(istream & is)
502 // eat everything up to the next \end_inset or end of stream
503 // and store it in s for further tokenization
508 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
509 s = s.substr(0, s.size() - 10);
513 // Remove the space after \end_inset
514 if (is.get(c) && c != ' ')
518 tokenize(from_utf8(s));
522 void Parser::tokenize(docstring const & buffer)
524 idocstringstream is(buffer, ios::in | ios::binary);
528 //lyxerr << "reading c: " << c << endl;
530 switch (catcode(c)) {
534 if (catcode(c) == catNewline)
535 ; //push_back(Token("par"));
537 push_back(Token('\n', catNewline));
545 while (is.get(c) && catcode(c) != catNewline)
555 error("unexpected end of input");
558 if (catcode(c) == catLetter) {
560 while (is.get(c) && catcode(c) == catLetter)
562 skipSpaceTokens(is, c);
571 push_back(Token(c, catcode(c)));
573 skipSpaceTokens(is, c);
578 lyxerr << "ignoring a char: " << int(c) << endl;
583 push_back(Token(c, catcode(c)));
593 void Parser::dump() const
595 lyxerr << "\nTokens: ";
596 for (unsigned i = 0; i < tokens_.size(); ++i) {
599 lyxerr << tokens_[i];
601 lyxerr << " pos: " << pos_ << endl;
605 void Parser::error(string const & msg)
607 lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
613 bool Parser::parse(MathAtom & at)
617 parse(ar, false, InsetMath::UNDECIDED_MODE);
618 if (ar.size() != 1 || ar.front()->getType() == hullNone) {
619 lyxerr << "unusual contents found: " << ar << endl;
620 at = MathAtom(new InsetMathPar(ar));
621 //if (at->nargs() > 0)
622 // at.nucleus()->cell(0) = ar;
624 // lyxerr << "unusual contents found: " << ar << endl;
632 docstring Parser::parse_verbatim_option()
636 if (nextToken().character() == '[') {
637 Token t = getToken();
638 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
639 if (t.cat() == catBegin) {
641 res += '{' + parse_verbatim_item() + '}';
650 docstring Parser::parse_verbatim_item()
654 if (nextToken().cat() == catBegin) {
655 Token t = getToken();
656 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
657 if (t.cat() == catBegin) {
659 res += '{' + parse_verbatim_item() + '}';
669 MathData Parser::parse(unsigned flags, mode_type mode)
672 parse(ar, flags, mode);
677 void Parser::parse(MathData & array, unsigned flags, mode_type mode)
679 InsetMathGrid grid(1, 1);
680 parse1(grid, flags, mode, false);
681 array = grid.cell(0);
685 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
688 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
692 void Parser::parse1(InsetMathGrid & grid, unsigned flags,
693 const mode_type mode, const bool numbered)
696 InsetMathGrid::row_type cellrow = 0;
697 InsetMathGrid::col_type cellcol = 0;
698 MathData * cell = &grid.cell(grid.index(cellrow, cellcol));
700 if (grid.asHullInset())
701 grid.asHullInset()->numbered(cellrow, numbered);
704 //lyxerr << " flags: " << flags << endl;
705 //lyxerr << " mode: " << mode << endl;
706 //lyxerr << "grid: " << grid << endl;
709 Token const & t = getToken();
712 lyxerr << "t: " << t << " flags: " << flags << endl;
713 lyxerr << "mode: " << mode << endl;
718 if (flags & FLAG_ITEM) {
720 if (t.cat() == catBegin) {
721 // skip the brace and collect everything to the next matching
723 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
727 // handle only this single token, leave the loop if done
732 if (flags & FLAG_BRACED) {
733 if (t.cat() == catSpace)
736 if (t.cat() != catBegin) {
737 error("opening brace expected");
741 // skip the brace and collect everything to the next matching
743 flags = FLAG_BRACE_LAST;
747 if (flags & FLAG_OPTION) {
748 if (t.cat() == catOther && t.character() == '[') {
750 parse(ar, FLAG_BRACK_LAST, mode);
753 // no option found, put back token and we are done
762 if (t.cat() == catMath) {
763 if (mode != InsetMath::MATH_MODE) {
764 // we are inside some text mode thingy, so opening new math is allowed
765 Token const & n = getToken();
766 if (n.cat() == catMath) {
767 // TeX's $$...$$ syntax for displayed math
768 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
769 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
770 getToken(); // skip the second '$' token
772 // simple $...$ stuff
774 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
775 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
779 else if (flags & FLAG_SIMPLE) {
780 // this is the end of the formula
785 error("something strange in the parser");
790 else if (t.cat() == catLetter)
791 cell->push_back(MathAtom(new InsetMathChar(t.character())));
793 else if (t.cat() == catSpace && mode != InsetMath::MATH_MODE) {
794 if (cell->empty() || cell->back()->getChar() != ' ')
795 cell->push_back(MathAtom(new InsetMathChar(t.character())));
798 else if (t.cat() == catNewline && mode != InsetMath::MATH_MODE) {
799 if (cell->empty() || cell->back()->getChar() != ' ')
800 cell->push_back(MathAtom(new InsetMathChar(' ')));
803 else if (t.cat() == catParameter) {
804 Token const & n = getToken();
805 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
808 else if (t.cat() == catActive)
809 cell->push_back(MathAtom(new InsetMathChar(t.character())));
811 else if (t.cat() == catBegin) {
813 parse(ar, FLAG_BRACE_LAST, mode);
814 // do not create a BraceInset if they were written by LyX
815 // this helps to keep the annoyance of "a choose b" to a minimum
816 if (ar.size() == 1 && ar[0]->extraBraces())
819 cell->push_back(MathAtom(new InsetMathBrace(ar)));
822 else if (t.cat() == catEnd) {
823 if (flags & FLAG_BRACE_LAST)
825 error("found '}' unexpectedly");
826 //BOOST_ASSERT(false);
827 //add(cell, '}', LM_TC_TEX);
830 else if (t.cat() == catAlign) {
831 //lyxerr << " column now " << (cellcol + 1)
832 // << " max: " << grid.ncols() << endl;
833 if (flags & FLAG_ALIGN)
835 if (addCol(grid, cellcol))
836 cell = &grid.cell(grid.index(cellrow, cellcol));
839 else if (t.cat() == catSuper || t.cat() == catSub) {
840 bool up = (t.cat() == catSuper);
841 // we need no new script inset if the last thing was a scriptinset,
842 // which has that script already not the same script already
844 cell->push_back(MathAtom(new InsetMathScript(up)));
845 else if (cell->back()->asScriptInset() &&
846 !cell->back()->asScriptInset()->has(up))
847 cell->back().nucleus()->asScriptInset()->ensure(up);
848 else if (cell->back()->asScriptInset())
849 cell->push_back(MathAtom(new InsetMathScript(up)));
851 cell->back() = MathAtom(new InsetMathScript(cell->back(), up));
852 InsetMathScript * p = cell->back().nucleus()->asScriptInset();
853 // special handling of {}-bases
854 // Here we could remove the brace inset for things
855 // like {a'}^2 and add the braces back in
856 // InsetMathScript::write().
857 // We do not do it, since it is not possible to detect
858 // reliably whether the braces are needed because the
859 // nucleus contains more than one symbol, or whether
860 // they are needed for unknown commands like \xx{a}_0
861 // or \yy{a}{b}_0. This was done in revision 14819
862 // in an unreliable way. See this thread
863 // http://www.mail-archive.com/lyx-devel%40lists.lyx.org/msg104917.html
865 parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
872 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
873 //lyxerr << "finished reading option" << endl;
877 else if (t.cat() == catOther)
878 cell->push_back(MathAtom(new InsetMathChar(t.character())));
880 else if (t.cat() == catComment) {
883 Token const & t = getToken();
884 if (t.cat() == catNewline)
888 cell->push_back(MathAtom(new InsetMathComment(s)));
896 else if (t.cs() == "lyxlock") {
898 cell->back().nucleus()->lock(true);
901 else if (t.cs() == "def" ||
902 t.cs() == "newcommand" ||
903 t.cs() == "renewcommand")
905 docstring const type = t.cs();
908 if (t.cs() == "def") {
910 name = getToken().cs();
914 while (good() && nextToken().cat() != catBegin) {
915 pars += getToken().cs();
919 //lyxerr << "read \\def parameter list '" << pars << "'" << endl;
921 } else { // t.cs() == "newcommand" || t.cs() == "renewcommand"
923 if (getToken().cat() != catBegin) {
924 error("'{' in \\newcommand expected (1) ");
928 name = getToken().cs();
930 if (getToken().cat() != catEnd) {
931 error("'}' in \\newcommand expected");
935 docstring const arg = getArg('[', ']');
937 nargs = convert<int>(arg);
942 parse(ar1, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
944 // we cannot handle recursive stuff at all
946 //test.push_back(createInsetMath(name));
947 //if (ar1.contains(test)) {
948 // error("we cannot handle recursive macros at all.");
952 // is a version for display attached?
955 if (nextToken().cat() == catBegin)
956 parse(ar2, FLAG_ITEM, InsetMath::MATH_MODE);
958 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, type,
962 else if (t.cs() == "(") {
963 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
964 parse2(cell->back(), FLAG_SIMPLE2, InsetMath::MATH_MODE, false);
967 else if (t.cs() == "[") {
968 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
969 parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false);
972 else if (t.cs() == "protect")
973 // ignore \\protect, will hopefully be re-added during output
976 else if (t.cs() == "end") {
977 if (flags & FLAG_END) {
978 // eat environment name
979 docstring const name = getArg('{', '}');
980 if (environments_.empty())
981 error("'found \\end{" + name +
982 "}' without matching '\\begin{" +
984 else if (name != environments_.back())
985 error("'\\end{" + name +
986 "}' does not match '\\begin{" +
987 environments_.back() + "}'");
989 environments_.pop_back();
990 // Delete empty last row in matrix
992 // If you abuse InsetMathGrid for
993 // non-matrix like structures you
994 // probably need to refine this test.
995 // Right now we only have to test for
996 // single line hull insets.
997 if (grid.nrows() > 1)
998 delEmptyLastRow(grid);
1002 error("found 'end' unexpectedly");
1005 else if (t.cs() == ")") {
1006 if (flags & FLAG_SIMPLE2)
1008 error("found '\\)' unexpectedly");
1011 else if (t.cs() == "]") {
1012 if (flags & FLAG_EQUATION)
1014 error("found '\\]' unexpectedly");
1017 else if (t.cs() == "\\") {
1018 if (flags & FLAG_ALIGN)
1021 if (nextToken().asInput() == "*") {
1023 added = addRow(grid, cellrow, docstring(), false);
1025 added = addRow(grid, cellrow, getArg('[', ']'));
1028 if (grid.asHullInset())
1029 grid.asHullInset()->numbered(
1031 cell = &grid.cell(grid.index(cellrow,
1037 else if (t.cs() == "multicolumn") {
1038 // extract column count and insert dummy cells
1040 parse(count, FLAG_ITEM, mode);
1042 if (!extractNumber(count, cols)) {
1043 lyxerr << " can't extract number of cells from " << count << endl;
1045 // resize the table if necessary
1046 for (int i = 0; i < cols; ++i) {
1047 if (addCol(grid, cellcol)) {
1048 cell = &grid.cell(grid.index(
1050 // mark this as dummy
1051 grid.cellinfo(grid.index(
1052 cellrow, cellcol)).dummy_ = true;
1055 // the last cell is the real thing, not a dummy
1056 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
1058 // read special alignment
1060 parse(align, FLAG_ITEM, mode);
1061 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
1063 // parse the remaining contents into the "real" cell
1064 parse(*cell, FLAG_ITEM, mode);
1068 else if (t.cs() == "limits")
1071 else if (t.cs() == "nolimits")
1074 else if (t.cs() == "nonumber") {
1075 if (grid.asHullInset())
1076 grid.asHullInset()->numbered(cellrow, false);
1079 else if (t.cs() == "number") {
1080 if (grid.asHullInset())
1081 grid.asHullInset()->numbered(cellrow, true);
1084 else if (t.cs() == "hline") {
1085 grid.rowinfo(cellrow).lines_ ++;
1088 else if (t.cs() == "sqrt") {
1090 parse(ar, FLAG_OPTION, mode);
1092 cell->push_back(MathAtom(new InsetMathRoot));
1093 cell->back().nucleus()->cell(0) = ar;
1094 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1096 cell->push_back(MathAtom(new InsetMathSqrt));
1097 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1101 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1102 cell->push_back(createInsetMath(t.cs()));
1103 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1104 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1107 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
1108 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1109 cell->push_back(MathAtom(new InsetMathRef(t.cs())));
1110 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1111 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1114 else if (t.cs() == "left") {
1116 Token const & tl = getToken();
1117 // \| and \Vert are equivalent, and InsetMathDelim
1119 // FIXME: fix this in InsetMathDelim itself!
1120 docstring const l = tl.cs() == "|" ? from_ascii("Vert") : tl.asString();
1122 parse(ar, FLAG_RIGHT, mode);
1126 Token const & tr = getToken();
1127 docstring const r = tr.cs() == "|" ? from_ascii("Vert") : tr.asString();
1128 cell->push_back(MathAtom(new InsetMathDelim(l, r, ar)));
1131 else if (t.cs() == "right") {
1132 if (flags & FLAG_RIGHT)
1134 //lyxerr << "got so far: '" << cell << "'" << endl;
1135 error("Unmatched right delimiter");
1139 else if (t.cs() == "begin") {
1140 docstring const name = getArg('{', '}');
1141 environments_.push_back(name);
1143 if (name == "array" || name == "subarray") {
1144 docstring const valign = parse_verbatim_option() + 'c';
1145 docstring const halign = parse_verbatim_item();
1146 cell->push_back(MathAtom(new InsetMathArray(name, (char)valign[0], halign)));
1147 parse2(cell->back(), FLAG_END, mode, false);
1150 else if (name == "tabular") {
1151 docstring const valign = parse_verbatim_option() + 'c';
1152 docstring const halign = parse_verbatim_item();
1153 cell->push_back(MathAtom(new InsetMathTabular(name, (char)valign[0], halign)));
1154 parse2(cell->back(), FLAG_END, InsetMath::TEXT_MODE, false);
1157 else if (name == "split" || name == "cases") {
1158 cell->push_back(createInsetMath(name));
1159 parse2(cell->back(), FLAG_END, mode, false);
1162 else if (name == "alignedat") {
1163 docstring const valign = parse_verbatim_option() + 'c';
1164 // ignore this for a while
1166 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1167 parse2(cell->back(), FLAG_END, mode, false);
1170 else if (name == "math") {
1171 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
1172 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, true);
1175 else if (name == "equation" || name == "equation*"
1176 || name == "displaymath") {
1177 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
1178 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation"));
1181 else if (name == "eqnarray" || name == "eqnarray*") {
1182 cell->push_back(MathAtom(new InsetMathHull(hullEqnArray)));
1183 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1186 else if (name == "align" || name == "align*") {
1187 cell->push_back(MathAtom(new InsetMathHull(hullAlign)));
1188 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1191 else if (name == "flalign" || name == "flalign*") {
1192 cell->push_back(MathAtom(new InsetMathHull(hullFlAlign)));
1193 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1196 else if (name == "alignat" || name == "alignat*") {
1197 // ignore this for a while
1199 cell->push_back(MathAtom(new InsetMathHull(hullAlignAt)));
1200 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1203 else if (name == "xalignat" || name == "xalignat*") {
1204 // ignore this for a while
1206 cell->push_back(MathAtom(new InsetMathHull(hullXAlignAt)));
1207 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1210 else if (name == "xxalignat") {
1211 // ignore this for a while
1213 cell->push_back(MathAtom(new InsetMathHull(hullXXAlignAt)));
1214 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1217 else if (name == "multline" || name == "multline*") {
1218 cell->push_back(MathAtom(new InsetMathHull(hullMultline)));
1219 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1222 else if (name == "gather" || name == "gather*") {
1223 cell->push_back(MathAtom(new InsetMathHull(hullGather)));
1224 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1227 else if (latexkeys const * l = in_word_set(name)) {
1228 if (l->inset == "matrix") {
1229 cell->push_back(createInsetMath(name));
1230 parse2(cell->back(), FLAG_END, mode, false);
1231 } else if (l->inset == "split") {
1232 docstring const valign = parse_verbatim_option() + 'c';
1233 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1234 parse2(cell->back(), FLAG_END, mode, false);
1237 lyxerr << "found math environment `" << to_utf8(name)
1238 << "' in symbols file with unsupported inset `"
1239 << to_utf8(l->inset) << "'." << endl;
1240 // create generic environment inset
1241 cell->push_back(MathAtom(new InsetMathEnv(name)));
1242 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1248 lyxerr << "found unknown math environment '" << to_utf8(name)
1250 // create generic environment inset
1251 cell->push_back(MathAtom(new InsetMathEnv(name)));
1252 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1256 else if (t.cs() == "kern") {
1260 Token const & t = getToken();
1266 if (isValidLength(to_utf8(s)))
1269 cell->push_back(MathAtom(new InsetMathKern(s)));
1272 else if (t.cs() == "label") {
1273 // FIXME: This is swallowed in inline formulas
1274 docstring label = parse_verbatim_item();
1277 if (grid.asHullInset()) {
1278 grid.asHullInset()->label(cellrow, label);
1280 cell->push_back(createInsetMath(t.cs()));
1281 cell->push_back(MathAtom(new InsetMathBrace(ar)));
1285 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1286 MathAtom at = createInsetMath(t.cs());
1287 at.nucleus()->cell(0) = *cell;
1289 parse(at.nucleus()->cell(1), flags, mode);
1290 cell->push_back(at);
1294 else if (t.cs() == "color") {
1295 docstring const color = parse_verbatim_item();
1296 cell->push_back(MathAtom(new InsetMathColor(true, color)));
1297 parse(cell->back().nucleus()->cell(0), flags, mode);
1301 else if (t.cs() == "textcolor") {
1302 docstring const color = parse_verbatim_item();
1303 cell->push_back(MathAtom(new InsetMathColor(false, color)));
1304 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1307 else if (t.cs() == "normalcolor") {
1308 cell->push_back(createInsetMath(t.cs()));
1309 parse(cell->back().nucleus()->cell(0), flags, mode);
1313 else if (t.cs() == "substack") {
1314 cell->push_back(createInsetMath(t.cs()));
1315 parse2(cell->back(), FLAG_ITEM, mode, false);
1318 else if (t.cs() == "xymatrix") {
1319 odocstringstream os;
1320 while (good() && nextToken().cat() != catBegin)
1321 os << getToken().asInput();
1322 cell->push_back(createInsetMath(t.cs() + os.str()));
1323 parse2(cell->back(), FLAG_ITEM, mode, false);
1326 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1327 cell->push_back(createInsetMath(t.cs()));
1328 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, InsetMath::TEXT_MODE);
1329 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, InsetMath::TEXT_MODE);
1330 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, InsetMath::TEXT_MODE);
1333 else if (t.cs() == "tag") {
1334 if (nextToken().character() == '*') {
1336 cell->push_back(createInsetMath(t.cs() + '*'));
1338 cell->push_back(createInsetMath(t.cs()));
1339 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1343 else if (t.cs() == "infer") {
1345 parse(ar, FLAG_OPTION, mode);
1346 cell->push_back(createInsetMath(t.cs()));
1347 parse2(cell->back(), FLAG_ITEM, mode, false);
1351 else if (1 && t.cs() == "ar") {
1352 auto_ptr<InsetMathXYArrow> p(new InsetMathXYArrow);
1353 // try to read target
1354 parse(p->cell(0), FLAG_OTPTION, mode);
1355 // try to read label
1356 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1357 p->up_ = nextToken().cat() == catSuper;
1359 parse(p->cell(1), FLAG_ITEM, mode);
1360 //lyxerr << "read label: " << p->cell(1) << endl;
1363 cell->push_back(MathAtom(p.release()));
1364 //lyxerr << "read cell: " << cell << endl;
1368 else if (t.cs().size()) {
1369 latexkeys const * l = in_word_set(t.cs());
1371 if (l->inset == "big") {
1373 docstring const delim = getToken().asInput();
1374 if (InsetMathBig::isBigInsetDelim(delim))
1375 cell->push_back(MathAtom(
1376 new InsetMathBig(t.cs(), delim)));
1378 cell->push_back(createInsetMath(t.cs()));
1383 else if (l->inset == "font") {
1384 cell->push_back(createInsetMath(t.cs()));
1385 parse(cell->back().nucleus()->cell(0),
1386 FLAG_ITEM, asMode(mode, l->extra));
1389 else if (l->inset == "oldfont") {
1390 cell->push_back(createInsetMath(t.cs()));
1391 parse(cell->back().nucleus()->cell(0),
1392 flags | FLAG_ALIGN, asMode(mode, l->extra));
1393 if (prevToken().cat() != catAlign &&
1394 prevToken().cs() != "\\")
1399 else if (l->inset == "style") {
1400 cell->push_back(createInsetMath(t.cs()));
1401 parse(cell->back().nucleus()->cell(0),
1402 flags | FLAG_ALIGN, mode);
1403 if (prevToken().cat() != catAlign &&
1404 prevToken().cs() != "\\")
1410 MathAtom at = createInsetMath(t.cs());
1411 for (InsetMath::idx_type i = 0; i < at->nargs(); ++i)
1412 parse(at.nucleus()->cell(i),
1413 FLAG_ITEM, asMode(mode, l->extra));
1414 cell->push_back(at);
1419 MathAtom at = createInsetMath(t.cs());
1420 InsetMath::mode_type m = mode;
1421 //if (m == InsetMath::UNDECIDED_MODE)
1422 //lyxerr << "default creation: m1: " << m << endl;
1423 if (at->currentMode() != InsetMath::UNDECIDED_MODE)
1424 m = at->currentMode();
1425 //lyxerr << "default creation: m2: " << m << endl;
1426 InsetMath::idx_type start = 0;
1427 // this fails on \bigg[...\bigg]
1429 //parse(opt, FLAG_OPTION, InsetMath::VERBATIM_MODE);
1432 // at.nucleus()->cell(0) = opt;
1434 for (InsetMath::idx_type i = start; i < at->nargs(); ++i) {
1435 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1438 cell->push_back(at);
1443 if (flags & FLAG_LEAVE) {
1444 flags &= ~FLAG_LEAVE;
1452 } // anonymous namespace
1455 void mathed_parse_cell(MathData & ar, docstring const & str)
1457 Parser(str).parse(ar, 0, InsetMath::MATH_MODE);
1461 void mathed_parse_cell(MathData & ar, istream & is)
1463 Parser(is).parse(ar, 0, InsetMath::MATH_MODE);
1467 bool mathed_parse_normal(MathAtom & t, docstring const & str)
1469 return Parser(str).parse(t);
1473 bool mathed_parse_normal(MathAtom & t, Lexer & lex)
1475 return Parser(lex).parse(t);
1479 void mathed_parse_normal(InsetMathGrid & grid, docstring const & str)
1481 Parser(str).parse1(grid, 0, InsetMath::MATH_MODE, false);
1487 fill(theCatcode, theCatcode + 128, catOther);
1488 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1489 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1491 theCatcode[int('\\')] = catEscape;
1492 theCatcode[int('{')] = catBegin;
1493 theCatcode[int('}')] = catEnd;
1494 theCatcode[int('$')] = catMath;
1495 theCatcode[int('&')] = catAlign;
1496 theCatcode[int('\n')] = catNewline;
1497 theCatcode[int('#')] = catParameter;
1498 theCatcode[int('^')] = catSuper;
1499 theCatcode[int('_')] = catSub;
1500 theCatcode[int(0x7f)] = catIgnore;
1501 theCatcode[int(' ')] = catSpace;
1502 theCatcode[int('\t')] = catSpace;
1503 theCatcode[int('\r')] = catNewline;
1504 theCatcode[int('~')] = catActive;
1505 theCatcode[int('%')] = catComment;