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"
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)
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 lyxerr << '.' << endl;
135 grid.vcrskip(LyXLength(to_utf8(vskip)), cellrow - 1);
141 * Add the column \p cellcol to \p grid.
142 * \returns wether the column could be added. Adding a column can fail for
143 * environments like "eqnarray" that have a fixed number of columns.
145 bool addCol(InsetMathGrid & grid, InsetMathGrid::col_type & cellcol)
148 if (cellcol == grid.ncols()) {
149 //lyxerr << "adding column " << cellcol << endl;
150 grid.addCol(cellcol - 1);
151 if (cellcol == grid.ncols()) {
152 // We can't add a column to this grid, so let's
153 // append the content of this cell to the previous
155 // This does not happen in well formed .lyx files,
156 // but LyX versions 1.3.x and older could create
157 // such files and tex2lyx can still do that.
159 lyxerr << "ignoring extra column." << endl;
168 * Check wether the last row is empty and remove it if yes.
169 * Otherwise the following code
177 * will result in a grid with 3 rows (+ the dummy row that is always present),
178 * because the last '\\' opens a new row.
180 void delEmptyLastRow(InsetMathGrid & grid)
182 InsetMathGrid::row_type const row = grid.nrows() - 1;
183 for (InsetMathGrid::col_type col = 0; col < grid.ncols(); ++col) {
184 if (!grid.cell(grid.index(row, col)).empty())
187 // Copy the row information of the empty row (which would contain the
188 // last hline in the example above) to the dummy row and delete the
190 grid.rowinfo(row + 1) = grid.rowinfo(row);
195 // These are TeX's catcodes
197 catEscape, // 0 backslash
207 catSpace, // 10 space
208 catLetter, // 11 a-zA-Z
209 catOther, // 12 none of the above
212 catInvalid // 15 <delete>
215 CatCode theCatcode[128];
218 inline CatCode catcode(char_type c)
220 /* The only characters that are not catOther lie in the pure ASCII
221 * range. Therefore theCatcode has only 128 entries.
222 * TeX itself deals with 8bit characters, so if needed this table
223 * could be enlarged to 256 entries.
224 * Any larger value does not make sense, since the fact that we use
225 * unicode internally does not change Knuth's TeX engine.
226 * Apart from that a table for the full 21bit UCS4 range would waste
227 * too much memory. */
231 return theCatcode[c];
236 FLAG_ALIGN = 1 << 0, // next & or \\ ends the parsing process
237 FLAG_BRACE_LAST = 1 << 1, // next closing brace ends the parsing
238 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
239 FLAG_END = 1 << 3, // next \\end ends the parsing process
240 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
241 FLAG_TEXTMODE = 1 << 5, // we are in a box
242 FLAG_ITEM = 1 << 6, // read a (possibly braced token)
243 FLAG_LEAVE = 1 << 7, // leave the loop at the end
244 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
245 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
246 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
247 FLAG_OPTION = 1 << 11, // read [...] style option
248 FLAG_BRACED = 1 << 12 // read {...} style argument
253 // Helper class for parsing
259 Token() : cs_(), char_(0), cat_(catIgnore) {}
261 Token(char_type c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
263 Token(docstring const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
266 docstring const & cs() const { return cs_; }
268 CatCode cat() const { return cat_; }
270 char_type character() const { return char_; }
272 docstring asString() const { return cs_.size() ? cs_ : docstring(1, char_); }
274 docstring asInput() const { return cs_.size() ? '\\' + cs_ : docstring(1, char_); }
285 ostream & operator<<(ostream & os, Token const & t)
288 os << '\\' << t.cs();
289 else if (t.cat() == catLetter)
292 os << '[' << t.character() << ',' << t.cat() << ']';
300 typedef InsetMath::mode_type mode_type;
303 Parser(LyXLex & lex);
304 /// Only use this for reading from .lyx file format, for the reason
305 /// see Parser::tokenize(std::istream &).
306 Parser(istream & is);
308 Parser(docstring const & str);
311 bool parse(MathAtom & at);
313 void parse(MathArray & array, unsigned flags, mode_type mode);
315 void parse1(InsetMathGrid & grid, unsigned flags, mode_type mode,
318 MathArray parse(unsigned flags, mode_type mode);
320 int lineno() const { return lineno_; }
326 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
327 /// get arg delimited by 'left' and 'right'
328 docstring getArg(char_type left, char_type right);
332 void error(string const & msg);
333 void error(docstring const & msg) { error(to_utf8(msg)); }
334 /// dump contents to screen
336 /// Only use this for reading from .lyx file format (see
337 /// implementation for reason)
338 void tokenize(istream & is);
340 void tokenize(docstring const & s);
342 void skipSpaceTokens(idocstream & is, char_type c);
344 void push_back(Token const & t);
348 Token const & prevToken() const;
350 Token const & nextToken() const;
352 Token const & getToken();
353 /// skips spaces if any
356 void lex(docstring const & s);
360 docstring parse_verbatim_item();
362 docstring parse_verbatim_option();
367 vector<Token> tokens_;
370 /// Stack of active environments
371 vector<docstring> environments_;
375 Parser::Parser(LyXLex & lexer)
376 : lineno_(lexer.getLineNo()), pos_(0)
378 tokenize(lexer.getStream());
383 Parser::Parser(istream & is)
384 : lineno_(0), pos_(0)
390 Parser::Parser(docstring const & str)
391 : lineno_(0), pos_(0)
397 void Parser::push_back(Token const & t)
399 tokens_.push_back(t);
403 void Parser::pop_back()
409 Token const & Parser::prevToken() const
411 static const Token dummy;
412 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
416 Token const & Parser::nextToken() const
418 static const Token dummy;
419 return good() ? tokens_[pos_] : dummy;
423 Token const & Parser::getToken()
425 static const Token dummy;
426 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
427 return good() ? tokens_[pos_++] : dummy;
431 void Parser::skipSpaces()
433 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
438 void Parser::putback()
444 bool Parser::good() const
446 return pos_ < tokens_.size();
450 char_type Parser::getChar()
453 error("The input stream is not well...");
454 return tokens_[pos_++].character();
458 docstring Parser::getArg(char_type left, char_type right)
463 char_type c = getChar();
468 while ((c = getChar()) != right && good())
475 void Parser::skipSpaceTokens(idocstream & is, char_type c)
477 // skip trailing spaces
478 while (catcode(c) == catSpace || catcode(c) == catNewline)
481 //lyxerr << "putting back: " << c << endl;
486 void Parser::tokenize(istream & is)
488 // eat everything up to the next \end_inset or end of stream
489 // and store it in s for further tokenization
494 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
495 s = s.substr(0, s.size() - 10);
499 // Remove the space after \end_inset
500 if (is.get(c) && c != ' ')
504 tokenize(from_utf8(s));
508 void Parser::tokenize(docstring const & buffer)
510 idocstringstream is(buffer, ios::in | ios::binary);
514 //lyxerr << "reading c: " << c << endl;
516 switch (catcode(c)) {
520 if (catcode(c) == catNewline)
521 ; //push_back(Token("par"));
523 push_back(Token('\n', catNewline));
531 while (is.get(c) && catcode(c) != catNewline)
541 error("unexpected end of input");
544 if (catcode(c) == catLetter) {
546 while (is.get(c) && catcode(c) == catLetter)
548 skipSpaceTokens(is, c);
557 push_back(Token(c, catcode(c)));
559 skipSpaceTokens(is, c);
564 lyxerr << "ignoring a char: " << int(c) << endl;
569 push_back(Token(c, catcode(c)));
579 void Parser::dump() const
581 lyxerr << "\nTokens: ";
582 for (unsigned i = 0; i < tokens_.size(); ++i) {
585 lyxerr << tokens_[i];
587 lyxerr << " pos: " << pos_ << endl;
591 void Parser::error(string const & msg)
593 lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
599 bool Parser::parse(MathAtom & at)
603 parse(ar, false, InsetMath::UNDECIDED_MODE);
604 if (ar.size() != 1 || ar.front()->getType() == hullNone) {
605 lyxerr << "unusual contents found: " << ar << endl;
606 at = MathAtom(new InsetMathPar(ar));
607 //if (at->nargs() > 0)
608 // at.nucleus()->cell(0) = ar;
610 // lyxerr << "unusual contents found: " << ar << endl;
618 docstring Parser::parse_verbatim_option()
622 if (nextToken().character() == '[') {
623 Token t = getToken();
624 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
625 if (t.cat() == catBegin) {
627 res += '{' + parse_verbatim_item() + '}';
636 docstring Parser::parse_verbatim_item()
640 if (nextToken().cat() == catBegin) {
641 Token t = getToken();
642 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
643 if (t.cat() == catBegin) {
645 res += '{' + parse_verbatim_item() + '}';
655 MathArray Parser::parse(unsigned flags, mode_type mode)
658 parse(ar, flags, mode);
663 void Parser::parse(MathArray & array, unsigned flags, mode_type mode)
665 InsetMathGrid grid(1, 1);
666 parse1(grid, flags, mode, false);
667 array = grid.cell(0);
671 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
674 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
678 void Parser::parse1(InsetMathGrid & grid, unsigned flags,
679 const mode_type mode, const bool numbered)
682 InsetMathGrid::row_type cellrow = 0;
683 InsetMathGrid::col_type cellcol = 0;
684 MathArray * cell = &grid.cell(grid.index(cellrow, cellcol));
686 if (grid.asHullInset())
687 grid.asHullInset()->numbered(cellrow, numbered);
690 //lyxerr << " flags: " << flags << endl;
691 //lyxerr << " mode: " << mode << endl;
692 //lyxerr << "grid: " << grid << endl;
695 Token const & t = getToken();
698 lyxerr << "t: " << t << " flags: " << flags << endl;
699 lyxerr << "mode: " << mode << endl;
704 if (flags & FLAG_ITEM) {
706 if (t.cat() == catBegin) {
707 // skip the brace and collect everything to the next matching
709 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
713 // handle only this single token, leave the loop if done
718 if (flags & FLAG_BRACED) {
719 if (t.cat() == catSpace)
722 if (t.cat() != catBegin) {
723 error("opening brace expected");
727 // skip the brace and collect everything to the next matching
729 flags = FLAG_BRACE_LAST;
733 if (flags & FLAG_OPTION) {
734 if (t.cat() == catOther && t.character() == '[') {
736 parse(ar, FLAG_BRACK_LAST, mode);
739 // no option found, put back token and we are done
748 if (t.cat() == catMath) {
749 if (mode != InsetMath::MATH_MODE) {
750 // we are inside some text mode thingy, so opening new math is allowed
751 Token const & n = getToken();
752 if (n.cat() == catMath) {
753 // TeX's $$...$$ syntax for displayed math
754 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
755 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
756 getToken(); // skip the second '$' token
758 // simple $...$ stuff
760 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
761 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
765 else if (flags & FLAG_SIMPLE) {
766 // this is the end of the formula
771 error("something strange in the parser");
776 else if (t.cat() == catLetter)
777 cell->push_back(MathAtom(new InsetMathChar(t.character())));
779 else if (t.cat() == catSpace && mode != InsetMath::MATH_MODE) {
780 if (cell->empty() || cell->back()->getChar() != ' ')
781 cell->push_back(MathAtom(new InsetMathChar(t.character())));
784 else if (t.cat() == catNewline && mode != InsetMath::MATH_MODE) {
785 if (cell->empty() || cell->back()->getChar() != ' ')
786 cell->push_back(MathAtom(new InsetMathChar(' ')));
789 else if (t.cat() == catParameter) {
790 Token const & n = getToken();
791 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
794 else if (t.cat() == catActive)
795 cell->push_back(MathAtom(new InsetMathChar(t.character())));
797 else if (t.cat() == catBegin) {
799 parse(ar, FLAG_BRACE_LAST, mode);
800 // do not create a BraceInset if they were written by LyX
801 // this helps to keep the annoyance of "a choose b" to a minimum
802 if (ar.size() == 1 && ar[0]->extraBraces())
805 cell->push_back(MathAtom(new InsetMathBrace(ar)));
808 else if (t.cat() == catEnd) {
809 if (flags & FLAG_BRACE_LAST)
811 error("found '}' unexpectedly");
812 //BOOST_ASSERT(false);
813 //add(cell, '}', LM_TC_TEX);
816 else if (t.cat() == catAlign) {
817 //lyxerr << " column now " << (cellcol + 1)
818 // << " max: " << grid.ncols() << endl;
819 if (flags & FLAG_ALIGN)
821 if (addCol(grid, cellcol))
822 cell = &grid.cell(grid.index(cellrow, cellcol));
825 else if (t.cat() == catSuper || t.cat() == catSub) {
826 bool up = (t.cat() == catSuper);
827 // we need no new script inset if the last thing was a scriptinset,
828 // which has that script already not the same script already
830 cell->push_back(MathAtom(new InsetMathScript(up)));
831 else if (cell->back()->asScriptInset() &&
832 !cell->back()->asScriptInset()->has(up))
833 cell->back().nucleus()->asScriptInset()->ensure(up);
834 else if (cell->back()->asScriptInset())
835 cell->push_back(MathAtom(new InsetMathScript(up)));
837 cell->back() = MathAtom(new InsetMathScript(cell->back(), up));
838 InsetMathScript * p = cell->back().nucleus()->asScriptInset();
839 // special handling of {}-bases
840 // Here we could remove the brace inset for things
841 // like {a'}^2 and add the braces back in
842 // InsetMathScript::write().
843 // We do not do it, since it is not possible to detect
844 // reliably whether the braces are needed because the
845 // nucleus contains more than one symbol, or whether
846 // they are needed for unknown commands like \xx{a}_0
847 // or \yy{a}{b}_0. This was done in revision 14819
848 // in an unreliable way. See this thread
849 // http://www.mail-archive.com/lyx-devel%40lists.lyx.org/msg104917.html
851 parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
858 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
859 //lyxerr << "finished reading option" << endl;
863 else if (t.cat() == catOther)
864 cell->push_back(MathAtom(new InsetMathChar(t.character())));
866 else if (t.cat() == catComment) {
869 Token const & t = getToken();
870 if (t.cat() == catNewline)
874 cell->push_back(MathAtom(new InsetMathComment(s)));
882 else if (t.cs() == "lyxlock") {
884 cell->back().nucleus()->lock(true);
887 else if (t.cs() == "def" ||
888 t.cs() == "newcommand" ||
889 t.cs() == "renewcommand")
891 docstring const type = t.cs();
894 if (t.cs() == "def") {
896 name = getToken().cs();
900 while (good() && nextToken().cat() != catBegin) {
901 pars += getToken().cs();
905 //lyxerr << "read \\def parameter list '" << pars << "'" << endl;
907 } else { // t.cs() == "newcommand" || t.cs() == "renewcommand"
909 if (getToken().cat() != catBegin) {
910 error("'{' in \\newcommand expected (1) ");
914 name = getToken().cs();
916 if (getToken().cat() != catEnd) {
917 error("'}' in \\newcommand expected");
921 docstring const arg = getArg('[', ']');
923 nargs = convert<int>(arg);
928 parse(ar1, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
930 // we cannot handle recursive stuff at all
932 //test.push_back(createInsetMath(name));
933 //if (ar1.contains(test)) {
934 // error("we cannot handle recursive macros at all.");
938 // is a version for display attached?
941 if (nextToken().cat() == catBegin)
942 parse(ar2, FLAG_ITEM, InsetMath::MATH_MODE);
944 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, type,
948 else if (t.cs() == "(") {
949 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
950 parse2(cell->back(), FLAG_SIMPLE2, InsetMath::MATH_MODE, false);
953 else if (t.cs() == "[") {
954 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
955 parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false);
958 else if (t.cs() == "protect")
959 // ignore \\protect, will hopefully be re-added during output
962 else if (t.cs() == "end") {
963 if (flags & FLAG_END) {
964 // eat environment name
965 docstring const name = getArg('{', '}');
966 if (environments_.empty())
967 error("'found \\end{" + name +
968 "}' without matching '\\begin{" +
970 else if (name != environments_.back())
971 error("'\\end{" + name +
972 "}' does not match '\\begin{" +
973 environments_.back() + "}'");
975 environments_.pop_back();
976 // Delete empty last row in matrix
978 // If you abuse InsetMathGrid for
979 // non-matrix like structures you
980 // probably need to refine this test.
981 // Right now we only have to test for
982 // single line hull insets.
983 if (grid.nrows() > 1)
984 delEmptyLastRow(grid);
988 error("found 'end' unexpectedly");
991 else if (t.cs() == ")") {
992 if (flags & FLAG_SIMPLE2)
994 error("found '\\)' unexpectedly");
997 else if (t.cs() == "]") {
998 if (flags & FLAG_EQUATION)
1000 error("found '\\]' unexpectedly");
1003 else if (t.cs() == "\\") {
1004 if (flags & FLAG_ALIGN)
1006 if (addRow(grid, cellrow, getArg('[', ']'))) {
1008 if (grid.asHullInset())
1009 grid.asHullInset()->numbered(
1011 cell = &grid.cell(grid.index(cellrow,
1017 else if (t.cs() == "multicolumn") {
1018 // extract column count and insert dummy cells
1020 parse(count, FLAG_ITEM, mode);
1022 if (!extractNumber(count, cols)) {
1023 lyxerr << " can't extract number of cells from " << count << endl;
1025 // resize the table if necessary
1026 for (int i = 0; i < cols; ++i) {
1027 if (addCol(grid, cellcol)) {
1028 cell = &grid.cell(grid.index(
1030 // mark this as dummy
1031 grid.cellinfo(grid.index(
1032 cellrow, cellcol)).dummy_ = true;
1035 // the last cell is the real thing, not a dummy
1036 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
1038 // read special alignment
1040 parse(align, FLAG_ITEM, mode);
1041 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
1043 // parse the remaining contents into the "real" cell
1044 parse(*cell, FLAG_ITEM, mode);
1048 else if (t.cs() == "limits")
1051 else if (t.cs() == "nolimits")
1054 else if (t.cs() == "nonumber") {
1055 if (grid.asHullInset())
1056 grid.asHullInset()->numbered(cellrow, false);
1059 else if (t.cs() == "number") {
1060 if (grid.asHullInset())
1061 grid.asHullInset()->numbered(cellrow, true);
1064 else if (t.cs() == "hline") {
1065 grid.rowinfo(cellrow).lines_ ++;
1068 else if (t.cs() == "sqrt") {
1070 parse(ar, FLAG_OPTION, mode);
1072 cell->push_back(MathAtom(new InsetMathRoot));
1073 cell->back().nucleus()->cell(0) = ar;
1074 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1076 cell->push_back(MathAtom(new InsetMathSqrt));
1077 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1081 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1082 cell->push_back(createInsetMath(t.cs()));
1083 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1084 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1087 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
1088 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1089 cell->push_back(MathAtom(new RefInset(t.cs())));
1090 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1091 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1094 else if (t.cs() == "left") {
1096 Token const & tl = getToken();
1097 // \| and \Vert are equivalent, and InsetMathDelim
1099 // FIXME: fix this in InsetMathDelim itself!
1100 docstring const l = tl.cs() == "|" ? from_ascii("Vert") : tl.asString();
1102 parse(ar, FLAG_RIGHT, mode);
1104 Token const & tr = getToken();
1105 docstring const r = tr.cs() == "|" ? from_ascii("Vert") : tr.asString();
1106 cell->push_back(MathAtom(new InsetMathDelim(l, r, ar)));
1109 else if (t.cs() == "right") {
1110 if (flags & FLAG_RIGHT)
1112 //lyxerr << "got so far: '" << cell << "'" << endl;
1113 error("Unmatched right delimiter");
1117 else if (t.cs() == "begin") {
1118 docstring const name = getArg('{', '}');
1119 environments_.push_back(name);
1121 if (name == "array" || name == "subarray") {
1122 docstring const valign = parse_verbatim_option() + 'c';
1123 docstring const halign = parse_verbatim_item();
1124 cell->push_back(MathAtom(new InsetMathArray(name, (char)valign[0], halign)));
1125 parse2(cell->back(), FLAG_END, mode, false);
1128 else if (name == "tabular") {
1129 docstring const valign = parse_verbatim_option() + 'c';
1130 docstring const halign = parse_verbatim_item();
1131 cell->push_back(MathAtom(new InsetMathTabular(name, (char)valign[0], halign)));
1132 parse2(cell->back(), FLAG_END, InsetMath::TEXT_MODE, false);
1135 else if (name == "split" || name == "cases") {
1136 cell->push_back(createInsetMath(name));
1137 parse2(cell->back(), FLAG_END, mode, false);
1140 else if (name == "alignedat") {
1141 docstring const valign = parse_verbatim_option() + 'c';
1142 // ignore this for a while
1144 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1145 parse2(cell->back(), FLAG_END, mode, false);
1148 else if (name == "math") {
1149 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
1150 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, true);
1153 else if (name == "equation" || name == "equation*"
1154 || name == "displaymath") {
1155 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
1156 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation"));
1159 else if (name == "eqnarray" || name == "eqnarray*") {
1160 cell->push_back(MathAtom(new InsetMathHull(hullEqnArray)));
1161 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1164 else if (name == "align" || name == "align*") {
1165 cell->push_back(MathAtom(new InsetMathHull(hullAlign)));
1166 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1169 else if (name == "flalign" || name == "flalign*") {
1170 cell->push_back(MathAtom(new InsetMathHull(hullFlAlign)));
1171 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1174 else if (name == "alignat" || name == "alignat*") {
1175 // ignore this for a while
1177 cell->push_back(MathAtom(new InsetMathHull(hullAlignAt)));
1178 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1181 else if (name == "xalignat" || name == "xalignat*") {
1182 // ignore this for a while
1184 cell->push_back(MathAtom(new InsetMathHull(hullXAlignAt)));
1185 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1188 else if (name == "xxalignat") {
1189 // ignore this for a while
1191 cell->push_back(MathAtom(new InsetMathHull(hullXXAlignAt)));
1192 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1195 else if (name == "multline" || name == "multline*") {
1196 cell->push_back(MathAtom(new InsetMathHull(hullMultline)));
1197 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1200 else if (name == "gather" || name == "gather*") {
1201 cell->push_back(MathAtom(new InsetMathHull(hullGather)));
1202 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1205 else if (latexkeys const * l = in_word_set(name)) {
1206 if (l->inset == "matrix") {
1207 cell->push_back(createInsetMath(name));
1208 parse2(cell->back(), FLAG_END, mode, false);
1209 } else if (l->inset == "split") {
1210 docstring const valign = parse_verbatim_option() + 'c';
1211 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1212 parse2(cell->back(), FLAG_END, mode, false);
1215 lyxerr << "found math environment `" << name
1216 << "' in symbols file with unsupported inset `"
1217 << l->inset << "'." << endl;
1218 // create generic environment inset
1219 cell->push_back(MathAtom(new InsetMathEnv(name)));
1220 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1226 lyxerr << "found unknown math environment '" << name << "'" << endl;
1227 // create generic environment inset
1228 cell->push_back(MathAtom(new InsetMathEnv(name)));
1229 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1233 else if (t.cs() == "kern") {
1234 #ifdef WITH_WARNINGS
1239 Token const & t = getToken();
1245 if (isValidLength(to_utf8(s)))
1248 cell->push_back(MathAtom(new InsetMathKern(s)));
1251 else if (t.cs() == "label") {
1252 // FIXME: This is swallowed in inline formulas
1253 docstring label = parse_verbatim_item();
1256 if (grid.asHullInset()) {
1257 grid.asHullInset()->label(cellrow, label);
1259 cell->push_back(createInsetMath(t.cs()));
1260 cell->push_back(MathAtom(new InsetMathBrace(ar)));
1264 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1265 MathAtom at = createInsetMath(t.cs());
1266 at.nucleus()->cell(0) = *cell;
1268 parse(at.nucleus()->cell(1), flags, mode);
1269 cell->push_back(at);
1273 else if (t.cs() == "color") {
1274 docstring const color = parse_verbatim_item();
1275 cell->push_back(MathAtom(new InsetMathColor(true, color)));
1276 parse(cell->back().nucleus()->cell(0), flags, mode);
1280 else if (t.cs() == "textcolor") {
1281 docstring const color = parse_verbatim_item();
1282 cell->push_back(MathAtom(new InsetMathColor(false, color)));
1283 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1286 else if (t.cs() == "normalcolor") {
1287 cell->push_back(createInsetMath(t.cs()));
1288 parse(cell->back().nucleus()->cell(0), flags, mode);
1292 else if (t.cs() == "substack") {
1293 cell->push_back(createInsetMath(t.cs()));
1294 parse2(cell->back(), FLAG_ITEM, mode, false);
1297 else if (t.cs() == "xymatrix") {
1298 odocstringstream os;
1299 while (good() && nextToken().cat() != catBegin)
1300 os << getToken().asInput();
1301 cell->push_back(createInsetMath(t.cs() + os.str()));
1302 parse2(cell->back(), FLAG_ITEM, mode, false);
1305 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1306 cell->push_back(createInsetMath(t.cs()));
1307 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, InsetMath::TEXT_MODE);
1308 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, InsetMath::TEXT_MODE);
1309 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, InsetMath::TEXT_MODE);
1312 else if (t.cs() == "tag") {
1313 if (nextToken().character() == '*') {
1315 cell->push_back(createInsetMath(t.cs() + '*'));
1317 cell->push_back(createInsetMath(t.cs()));
1318 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1322 else if (t.cs() == "infer") {
1324 parse(ar, FLAG_OPTION, mode);
1325 cell->push_back(createInsetMath(t.cs()));
1326 parse2(cell->back(), FLAG_ITEM, mode, false);
1330 else if (1 && t.cs() == "ar") {
1331 auto_ptr<InsetMathXYArrow> p(new InsetMathXYArrow);
1332 // try to read target
1333 parse(p->cell(0), FLAG_OTPTION, mode);
1334 // try to read label
1335 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1336 p->up_ = nextToken().cat() == catSuper;
1338 parse(p->cell(1), FLAG_ITEM, mode);
1339 //lyxerr << "read label: " << p->cell(1) << endl;
1342 cell->push_back(MathAtom(p.release()));
1343 //lyxerr << "read cell: " << cell << endl;
1347 else if (t.cs().size()) {
1348 latexkeys const * l = in_word_set(t.cs());
1350 if (l->inset == "big") {
1352 docstring const delim = getToken().asInput();
1353 if (InsetMathBig::isBigInsetDelim(delim))
1354 cell->push_back(MathAtom(
1355 new InsetMathBig(t.cs(), delim)));
1357 cell->push_back(createInsetMath(t.cs()));
1362 else if (l->inset == "font") {
1363 cell->push_back(createInsetMath(t.cs()));
1364 parse(cell->back().nucleus()->cell(0),
1365 FLAG_ITEM, asMode(mode, l->extra));
1368 else if (l->inset == "oldfont") {
1369 cell->push_back(createInsetMath(t.cs()));
1370 parse(cell->back().nucleus()->cell(0),
1371 flags | FLAG_ALIGN, asMode(mode, l->extra));
1372 if (prevToken().cat() != catAlign &&
1373 prevToken().cs() != "\\")
1378 else if (l->inset == "style") {
1379 cell->push_back(createInsetMath(t.cs()));
1380 parse(cell->back().nucleus()->cell(0),
1381 flags | FLAG_ALIGN, mode);
1382 if (prevToken().cat() != catAlign &&
1383 prevToken().cs() != "\\")
1389 MathAtom at = createInsetMath(t.cs());
1390 for (InsetMath::idx_type i = 0; i < at->nargs(); ++i)
1391 parse(at.nucleus()->cell(i),
1392 FLAG_ITEM, asMode(mode, l->extra));
1393 cell->push_back(at);
1398 MathAtom at = createInsetMath(t.cs());
1399 InsetMath::mode_type m = mode;
1400 //if (m == InsetMath::UNDECIDED_MODE)
1401 //lyxerr << "default creation: m1: " << m << endl;
1402 if (at->currentMode() != InsetMath::UNDECIDED_MODE)
1403 m = at->currentMode();
1404 //lyxerr << "default creation: m2: " << m << endl;
1405 InsetMath::idx_type start = 0;
1406 // this fails on \bigg[...\bigg]
1408 //parse(opt, FLAG_OPTION, InsetMath::VERBATIM_MODE);
1411 // at.nucleus()->cell(0) = opt;
1413 for (InsetMath::idx_type i = start; i < at->nargs(); ++i) {
1414 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1417 cell->push_back(at);
1422 if (flags & FLAG_LEAVE) {
1423 flags &= ~FLAG_LEAVE;
1431 } // anonymous namespace
1434 void mathed_parse_cell(MathArray & ar, docstring const & str)
1436 Parser(str).parse(ar, 0, InsetMath::MATH_MODE);
1440 void mathed_parse_cell(MathArray & ar, istream & is)
1442 Parser(is).parse(ar, 0, InsetMath::MATH_MODE);
1446 bool mathed_parse_normal(MathAtom & t, docstring const & str)
1448 return Parser(str).parse(t);
1452 bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
1454 return Parser(lex).parse(t);
1458 void mathed_parse_normal(InsetMathGrid & grid, docstring const & str)
1460 Parser(str).parse1(grid, 0, InsetMath::MATH_MODE, false);
1466 fill(theCatcode, theCatcode + 128, catOther);
1467 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1468 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1470 theCatcode[int('\\')] = catEscape;
1471 theCatcode[int('{')] = catBegin;
1472 theCatcode[int('}')] = catEnd;
1473 theCatcode[int('$')] = catMath;
1474 theCatcode[int('&')] = catAlign;
1475 theCatcode[int('\n')] = catNewline;
1476 theCatcode[int('#')] = catParameter;
1477 theCatcode[int('^')] = catSuper;
1478 theCatcode[int('_')] = catSub;
1479 theCatcode[int(0x7f)] = catIgnore;
1480 theCatcode[int(' ')] = catSpace;
1481 theCatcode[int('\t')] = catSpace;
1482 theCatcode[int('\r')] = catNewline;
1483 theCatcode[int('~')] = catActive;
1484 theCatcode[int('%')] = catComment;