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 docstring const & cs = t.cs();
289 // FIXME: For some strange reason, the stream operator instanciate
290 // a new Token before outputting the contents of t.cs().
291 // Because of this the line
293 // below becomes recursive.
294 // In order to avoid that we return early:
299 else if (t.cat() == catLetter)
302 os << '[' << t.character() << ',' << t.cat() << ']';
310 typedef InsetMath::mode_type mode_type;
313 Parser(LyXLex & lex);
314 /// Only use this for reading from .lyx file format, for the reason
315 /// see Parser::tokenize(std::istream &).
316 Parser(istream & is);
318 Parser(docstring const & str);
321 bool parse(MathAtom & at);
323 void parse(MathArray & array, unsigned flags, mode_type mode);
325 void parse1(InsetMathGrid & grid, unsigned flags, mode_type mode,
328 MathArray parse(unsigned flags, mode_type mode);
330 int lineno() const { return lineno_; }
336 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
337 /// get arg delimited by 'left' and 'right'
338 docstring getArg(char_type left, char_type right);
342 void error(string const & msg);
343 void error(docstring const & msg) { error(to_utf8(msg)); }
344 /// dump contents to screen
346 /// Only use this for reading from .lyx file format (see
347 /// implementation for reason)
348 void tokenize(istream & is);
350 void tokenize(docstring const & s);
352 void skipSpaceTokens(idocstream & is, char_type c);
354 void push_back(Token const & t);
358 Token const & prevToken() const;
360 Token const & nextToken() const;
362 Token const & getToken();
363 /// skips spaces if any
366 void lex(docstring const & s);
370 docstring parse_verbatim_item();
372 docstring parse_verbatim_option();
377 vector<Token> tokens_;
380 /// Stack of active environments
381 vector<docstring> environments_;
385 Parser::Parser(LyXLex & lexer)
386 : lineno_(lexer.getLineNo()), pos_(0)
388 tokenize(lexer.getStream());
393 Parser::Parser(istream & is)
394 : lineno_(0), pos_(0)
400 Parser::Parser(docstring const & str)
401 : lineno_(0), pos_(0)
407 void Parser::push_back(Token const & t)
409 tokens_.push_back(t);
413 void Parser::pop_back()
419 Token const & Parser::prevToken() const
421 static const Token dummy;
422 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
426 Token const & Parser::nextToken() const
428 static const Token dummy;
429 return good() ? tokens_[pos_] : dummy;
433 Token const & Parser::getToken()
435 static const Token dummy;
436 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
437 return good() ? tokens_[pos_++] : dummy;
441 void Parser::skipSpaces()
443 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
448 void Parser::putback()
454 bool Parser::good() const
456 return pos_ < tokens_.size();
460 char_type Parser::getChar()
463 error("The input stream is not well...");
464 return tokens_[pos_++].character();
468 docstring Parser::getArg(char_type left, char_type right)
473 char_type c = getChar();
478 while ((c = getChar()) != right && good())
485 void Parser::skipSpaceTokens(idocstream & is, char_type c)
487 // skip trailing spaces
488 while (catcode(c) == catSpace || catcode(c) == catNewline)
491 //lyxerr << "putting back: " << c << endl;
496 void Parser::tokenize(istream & is)
498 // eat everything up to the next \end_inset or end of stream
499 // and store it in s for further tokenization
504 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
505 s = s.substr(0, s.size() - 10);
509 // Remove the space after \end_inset
510 if (is.get(c) && c != ' ')
514 tokenize(from_utf8(s));
518 void Parser::tokenize(docstring const & buffer)
520 idocstringstream is(buffer, ios::in | ios::binary);
524 //lyxerr << "reading c: " << c << endl;
526 switch (catcode(c)) {
530 if (catcode(c) == catNewline)
531 ; //push_back(Token("par"));
533 push_back(Token('\n', catNewline));
541 while (is.get(c) && catcode(c) != catNewline)
551 error("unexpected end of input");
554 if (catcode(c) == catLetter) {
556 while (is.get(c) && catcode(c) == catLetter)
558 skipSpaceTokens(is, c);
567 push_back(Token(c, catcode(c)));
569 skipSpaceTokens(is, c);
574 lyxerr << "ignoring a char: " << int(c) << endl;
579 push_back(Token(c, catcode(c)));
589 void Parser::dump() const
591 lyxerr << "\nTokens: ";
592 for (unsigned i = 0; i < tokens_.size(); ++i) {
595 lyxerr << tokens_[i];
597 lyxerr << " pos: " << pos_ << endl;
601 void Parser::error(string const & msg)
603 lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
609 bool Parser::parse(MathAtom & at)
613 parse(ar, false, InsetMath::UNDECIDED_MODE);
614 if (ar.size() != 1 || ar.front()->getType() == hullNone) {
615 lyxerr << "unusual contents found: " << ar << endl;
616 at = MathAtom(new InsetMathPar(ar));
617 //if (at->nargs() > 0)
618 // at.nucleus()->cell(0) = ar;
620 // lyxerr << "unusual contents found: " << ar << endl;
628 docstring Parser::parse_verbatim_option()
632 if (nextToken().character() == '[') {
633 Token t = getToken();
634 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
635 if (t.cat() == catBegin) {
637 res += '{' + parse_verbatim_item() + '}';
646 docstring Parser::parse_verbatim_item()
650 if (nextToken().cat() == catBegin) {
651 Token t = getToken();
652 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
653 if (t.cat() == catBegin) {
655 res += '{' + parse_verbatim_item() + '}';
665 MathArray Parser::parse(unsigned flags, mode_type mode)
668 parse(ar, flags, mode);
673 void Parser::parse(MathArray & array, unsigned flags, mode_type mode)
675 InsetMathGrid grid(1, 1);
676 parse1(grid, flags, mode, false);
677 array = grid.cell(0);
681 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
684 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
688 void Parser::parse1(InsetMathGrid & grid, unsigned flags,
689 const mode_type mode, const bool numbered)
692 InsetMathGrid::row_type cellrow = 0;
693 InsetMathGrid::col_type cellcol = 0;
694 MathArray * cell = &grid.cell(grid.index(cellrow, cellcol));
696 if (grid.asHullInset())
697 grid.asHullInset()->numbered(cellrow, numbered);
700 //lyxerr << " flags: " << flags << endl;
701 //lyxerr << " mode: " << mode << endl;
702 //lyxerr << "grid: " << grid << endl;
705 Token const & t = getToken();
708 lyxerr << "t: " << t << " flags: " << flags << endl;
709 lyxerr << "mode: " << mode << endl;
714 if (flags & FLAG_ITEM) {
716 if (t.cat() == catBegin) {
717 // skip the brace and collect everything to the next matching
719 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
723 // handle only this single token, leave the loop if done
728 if (flags & FLAG_BRACED) {
729 if (t.cat() == catSpace)
732 if (t.cat() != catBegin) {
733 error("opening brace expected");
737 // skip the brace and collect everything to the next matching
739 flags = FLAG_BRACE_LAST;
743 if (flags & FLAG_OPTION) {
744 if (t.cat() == catOther && t.character() == '[') {
746 parse(ar, FLAG_BRACK_LAST, mode);
749 // no option found, put back token and we are done
758 if (t.cat() == catMath) {
759 if (mode != InsetMath::MATH_MODE) {
760 // we are inside some text mode thingy, so opening new math is allowed
761 Token const & n = getToken();
762 if (n.cat() == catMath) {
763 // TeX's $$...$$ syntax for displayed math
764 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
765 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
766 getToken(); // skip the second '$' token
768 // simple $...$ stuff
770 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
771 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
775 else if (flags & FLAG_SIMPLE) {
776 // this is the end of the formula
781 error("something strange in the parser");
786 else if (t.cat() == catLetter)
787 cell->push_back(MathAtom(new InsetMathChar(t.character())));
789 else if (t.cat() == catSpace && mode != InsetMath::MATH_MODE) {
790 if (cell->empty() || cell->back()->getChar() != ' ')
791 cell->push_back(MathAtom(new InsetMathChar(t.character())));
794 else if (t.cat() == catNewline && mode != InsetMath::MATH_MODE) {
795 if (cell->empty() || cell->back()->getChar() != ' ')
796 cell->push_back(MathAtom(new InsetMathChar(' ')));
799 else if (t.cat() == catParameter) {
800 Token const & n = getToken();
801 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
804 else if (t.cat() == catActive)
805 cell->push_back(MathAtom(new InsetMathChar(t.character())));
807 else if (t.cat() == catBegin) {
809 parse(ar, FLAG_BRACE_LAST, mode);
810 // do not create a BraceInset if they were written by LyX
811 // this helps to keep the annoyance of "a choose b" to a minimum
812 if (ar.size() == 1 && ar[0]->extraBraces())
815 cell->push_back(MathAtom(new InsetMathBrace(ar)));
818 else if (t.cat() == catEnd) {
819 if (flags & FLAG_BRACE_LAST)
821 error("found '}' unexpectedly");
822 //BOOST_ASSERT(false);
823 //add(cell, '}', LM_TC_TEX);
826 else if (t.cat() == catAlign) {
827 //lyxerr << " column now " << (cellcol + 1)
828 // << " max: " << grid.ncols() << endl;
829 if (flags & FLAG_ALIGN)
831 if (addCol(grid, cellcol))
832 cell = &grid.cell(grid.index(cellrow, cellcol));
835 else if (t.cat() == catSuper || t.cat() == catSub) {
836 bool up = (t.cat() == catSuper);
837 // we need no new script inset if the last thing was a scriptinset,
838 // which has that script already not the same script already
840 cell->push_back(MathAtom(new InsetMathScript(up)));
841 else if (cell->back()->asScriptInset() &&
842 !cell->back()->asScriptInset()->has(up))
843 cell->back().nucleus()->asScriptInset()->ensure(up);
844 else if (cell->back()->asScriptInset())
845 cell->push_back(MathAtom(new InsetMathScript(up)));
847 cell->back() = MathAtom(new InsetMathScript(cell->back(), up));
848 InsetMathScript * p = cell->back().nucleus()->asScriptInset();
849 // special handling of {}-bases
850 // Here we could remove the brace inset for things
851 // like {a'}^2 and add the braces back in
852 // InsetMathScript::write().
853 // We do not do it, since it is not possible to detect
854 // reliably whether the braces are needed because the
855 // nucleus contains more than one symbol, or whether
856 // they are needed for unknown commands like \xx{a}_0
857 // or \yy{a}{b}_0. This was done in revision 14819
858 // in an unreliable way. See this thread
859 // http://www.mail-archive.com/lyx-devel%40lists.lyx.org/msg104917.html
861 parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
868 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
869 //lyxerr << "finished reading option" << endl;
873 else if (t.cat() == catOther)
874 cell->push_back(MathAtom(new InsetMathChar(t.character())));
876 else if (t.cat() == catComment) {
879 Token const & t = getToken();
880 if (t.cat() == catNewline)
884 cell->push_back(MathAtom(new InsetMathComment(s)));
892 else if (t.cs() == "lyxlock") {
894 cell->back().nucleus()->lock(true);
897 else if (t.cs() == "def" ||
898 t.cs() == "newcommand" ||
899 t.cs() == "renewcommand")
901 docstring const type = t.cs();
904 if (t.cs() == "def") {
906 name = getToken().cs();
910 while (good() && nextToken().cat() != catBegin) {
911 pars += getToken().cs();
915 //lyxerr << "read \\def parameter list '" << pars << "'" << endl;
917 } else { // t.cs() == "newcommand" || t.cs() == "renewcommand"
919 if (getToken().cat() != catBegin) {
920 error("'{' in \\newcommand expected (1) ");
924 name = getToken().cs();
926 if (getToken().cat() != catEnd) {
927 error("'}' in \\newcommand expected");
931 docstring const arg = getArg('[', ']');
933 nargs = convert<int>(arg);
938 parse(ar1, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
940 // we cannot handle recursive stuff at all
942 //test.push_back(createInsetMath(name));
943 //if (ar1.contains(test)) {
944 // error("we cannot handle recursive macros at all.");
948 // is a version for display attached?
951 if (nextToken().cat() == catBegin)
952 parse(ar2, FLAG_ITEM, InsetMath::MATH_MODE);
954 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, type,
958 else if (t.cs() == "(") {
959 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
960 parse2(cell->back(), FLAG_SIMPLE2, InsetMath::MATH_MODE, false);
963 else if (t.cs() == "[") {
964 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
965 parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false);
968 else if (t.cs() == "protect")
969 // ignore \\protect, will hopefully be re-added during output
972 else if (t.cs() == "end") {
973 if (flags & FLAG_END) {
974 // eat environment name
975 docstring const name = getArg('{', '}');
976 if (environments_.empty())
977 error("'found \\end{" + name +
978 "}' without matching '\\begin{" +
980 else if (name != environments_.back())
981 error("'\\end{" + name +
982 "}' does not match '\\begin{" +
983 environments_.back() + "}'");
985 environments_.pop_back();
986 // Delete empty last row in matrix
988 // If you abuse InsetMathGrid for
989 // non-matrix like structures you
990 // probably need to refine this test.
991 // Right now we only have to test for
992 // single line hull insets.
993 if (grid.nrows() > 1)
994 delEmptyLastRow(grid);
998 error("found 'end' unexpectedly");
1001 else if (t.cs() == ")") {
1002 if (flags & FLAG_SIMPLE2)
1004 error("found '\\)' unexpectedly");
1007 else if (t.cs() == "]") {
1008 if (flags & FLAG_EQUATION)
1010 error("found '\\]' unexpectedly");
1013 else if (t.cs() == "\\") {
1014 if (flags & FLAG_ALIGN)
1016 if (addRow(grid, cellrow, getArg('[', ']'))) {
1018 if (grid.asHullInset())
1019 grid.asHullInset()->numbered(
1021 cell = &grid.cell(grid.index(cellrow,
1027 else if (t.cs() == "multicolumn") {
1028 // extract column count and insert dummy cells
1030 parse(count, FLAG_ITEM, mode);
1032 if (!extractNumber(count, cols)) {
1033 lyxerr << " can't extract number of cells from " << count << endl;
1035 // resize the table if necessary
1036 for (int i = 0; i < cols; ++i) {
1037 if (addCol(grid, cellcol)) {
1038 cell = &grid.cell(grid.index(
1040 // mark this as dummy
1041 grid.cellinfo(grid.index(
1042 cellrow, cellcol)).dummy_ = true;
1045 // the last cell is the real thing, not a dummy
1046 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
1048 // read special alignment
1050 parse(align, FLAG_ITEM, mode);
1051 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
1053 // parse the remaining contents into the "real" cell
1054 parse(*cell, FLAG_ITEM, mode);
1058 else if (t.cs() == "limits")
1061 else if (t.cs() == "nolimits")
1064 else if (t.cs() == "nonumber") {
1065 if (grid.asHullInset())
1066 grid.asHullInset()->numbered(cellrow, false);
1069 else if (t.cs() == "number") {
1070 if (grid.asHullInset())
1071 grid.asHullInset()->numbered(cellrow, true);
1074 else if (t.cs() == "hline") {
1075 grid.rowinfo(cellrow).lines_ ++;
1078 else if (t.cs() == "sqrt") {
1080 parse(ar, FLAG_OPTION, mode);
1082 cell->push_back(MathAtom(new InsetMathRoot));
1083 cell->back().nucleus()->cell(0) = ar;
1084 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1086 cell->push_back(MathAtom(new InsetMathSqrt));
1087 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1091 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1092 cell->push_back(createInsetMath(t.cs()));
1093 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1094 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1097 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
1098 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1099 cell->push_back(MathAtom(new RefInset(t.cs())));
1100 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1101 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1104 else if (t.cs() == "left") {
1106 Token const & tl = getToken();
1107 // \| and \Vert are equivalent, and InsetMathDelim
1109 // FIXME: fix this in InsetMathDelim itself!
1110 docstring const l = tl.cs() == "|" ? from_ascii("Vert") : tl.asString();
1112 parse(ar, FLAG_RIGHT, mode);
1114 Token const & tr = getToken();
1115 docstring const r = tr.cs() == "|" ? from_ascii("Vert") : tr.asString();
1116 cell->push_back(MathAtom(new InsetMathDelim(l, r, ar)));
1119 else if (t.cs() == "right") {
1120 if (flags & FLAG_RIGHT)
1122 //lyxerr << "got so far: '" << cell << "'" << endl;
1123 error("Unmatched right delimiter");
1127 else if (t.cs() == "begin") {
1128 docstring const name = getArg('{', '}');
1129 environments_.push_back(name);
1131 if (name == "array" || name == "subarray") {
1132 docstring const valign = parse_verbatim_option() + 'c';
1133 docstring const halign = parse_verbatim_item();
1134 cell->push_back(MathAtom(new InsetMathArray(name, (char)valign[0], halign)));
1135 parse2(cell->back(), FLAG_END, mode, false);
1138 else if (name == "tabular") {
1139 docstring const valign = parse_verbatim_option() + 'c';
1140 docstring const halign = parse_verbatim_item();
1141 cell->push_back(MathAtom(new InsetMathTabular(name, (char)valign[0], halign)));
1142 parse2(cell->back(), FLAG_END, InsetMath::TEXT_MODE, false);
1145 else if (name == "split" || name == "cases") {
1146 cell->push_back(createInsetMath(name));
1147 parse2(cell->back(), FLAG_END, mode, false);
1150 else if (name == "alignedat") {
1151 docstring const valign = parse_verbatim_option() + 'c';
1152 // ignore this for a while
1154 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1155 parse2(cell->back(), FLAG_END, mode, false);
1158 else if (name == "math") {
1159 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
1160 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, true);
1163 else if (name == "equation" || name == "equation*"
1164 || name == "displaymath") {
1165 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
1166 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation"));
1169 else if (name == "eqnarray" || name == "eqnarray*") {
1170 cell->push_back(MathAtom(new InsetMathHull(hullEqnArray)));
1171 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1174 else if (name == "align" || name == "align*") {
1175 cell->push_back(MathAtom(new InsetMathHull(hullAlign)));
1176 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1179 else if (name == "flalign" || name == "flalign*") {
1180 cell->push_back(MathAtom(new InsetMathHull(hullFlAlign)));
1181 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1184 else if (name == "alignat" || name == "alignat*") {
1185 // ignore this for a while
1187 cell->push_back(MathAtom(new InsetMathHull(hullAlignAt)));
1188 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1191 else if (name == "xalignat" || name == "xalignat*") {
1192 // ignore this for a while
1194 cell->push_back(MathAtom(new InsetMathHull(hullXAlignAt)));
1195 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1198 else if (name == "xxalignat") {
1199 // ignore this for a while
1201 cell->push_back(MathAtom(new InsetMathHull(hullXXAlignAt)));
1202 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1205 else if (name == "multline" || name == "multline*") {
1206 cell->push_back(MathAtom(new InsetMathHull(hullMultline)));
1207 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1210 else if (name == "gather" || name == "gather*") {
1211 cell->push_back(MathAtom(new InsetMathHull(hullGather)));
1212 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1215 else if (latexkeys const * l = in_word_set(name)) {
1216 if (l->inset == "matrix") {
1217 cell->push_back(createInsetMath(name));
1218 parse2(cell->back(), FLAG_END, mode, false);
1219 } else if (l->inset == "split") {
1220 docstring const valign = parse_verbatim_option() + 'c';
1221 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1222 parse2(cell->back(), FLAG_END, mode, false);
1225 lyxerr << "found math environment `" << name
1226 << "' in symbols file with unsupported inset `"
1227 << l->inset << "'." << endl;
1228 // create generic environment inset
1229 cell->push_back(MathAtom(new InsetMathEnv(name)));
1230 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1236 lyxerr << "found unknown math environment '" << name << "'" << endl;
1237 // create generic environment inset
1238 cell->push_back(MathAtom(new InsetMathEnv(name)));
1239 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1243 else if (t.cs() == "kern") {
1244 #ifdef WITH_WARNINGS
1249 Token const & t = getToken();
1255 if (isValidLength(to_utf8(s)))
1258 cell->push_back(MathAtom(new InsetMathKern(s)));
1261 else if (t.cs() == "label") {
1262 // FIXME: This is swallowed in inline formulas
1263 docstring label = parse_verbatim_item();
1266 if (grid.asHullInset()) {
1267 grid.asHullInset()->label(cellrow, label);
1269 cell->push_back(createInsetMath(t.cs()));
1270 cell->push_back(MathAtom(new InsetMathBrace(ar)));
1274 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1275 MathAtom at = createInsetMath(t.cs());
1276 at.nucleus()->cell(0) = *cell;
1278 parse(at.nucleus()->cell(1), flags, mode);
1279 cell->push_back(at);
1283 else if (t.cs() == "color") {
1284 docstring const color = parse_verbatim_item();
1285 cell->push_back(MathAtom(new InsetMathColor(true, color)));
1286 parse(cell->back().nucleus()->cell(0), flags, mode);
1290 else if (t.cs() == "textcolor") {
1291 docstring const color = parse_verbatim_item();
1292 cell->push_back(MathAtom(new InsetMathColor(false, color)));
1293 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1296 else if (t.cs() == "normalcolor") {
1297 cell->push_back(createInsetMath(t.cs()));
1298 parse(cell->back().nucleus()->cell(0), flags, mode);
1302 else if (t.cs() == "substack") {
1303 cell->push_back(createInsetMath(t.cs()));
1304 parse2(cell->back(), FLAG_ITEM, mode, false);
1307 else if (t.cs() == "xymatrix") {
1308 odocstringstream os;
1309 while (good() && nextToken().cat() != catBegin)
1310 os << getToken().asInput();
1311 cell->push_back(createInsetMath(t.cs() + os.str()));
1312 parse2(cell->back(), FLAG_ITEM, mode, false);
1315 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1316 cell->push_back(createInsetMath(t.cs()));
1317 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, InsetMath::TEXT_MODE);
1318 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, InsetMath::TEXT_MODE);
1319 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, InsetMath::TEXT_MODE);
1322 else if (t.cs() == "tag") {
1323 if (nextToken().character() == '*') {
1325 cell->push_back(createInsetMath(t.cs() + '*'));
1327 cell->push_back(createInsetMath(t.cs()));
1328 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1332 else if (t.cs() == "infer") {
1334 parse(ar, FLAG_OPTION, mode);
1335 cell->push_back(createInsetMath(t.cs()));
1336 parse2(cell->back(), FLAG_ITEM, mode, false);
1340 else if (1 && t.cs() == "ar") {
1341 auto_ptr<InsetMathXYArrow> p(new InsetMathXYArrow);
1342 // try to read target
1343 parse(p->cell(0), FLAG_OTPTION, mode);
1344 // try to read label
1345 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1346 p->up_ = nextToken().cat() == catSuper;
1348 parse(p->cell(1), FLAG_ITEM, mode);
1349 //lyxerr << "read label: " << p->cell(1) << endl;
1352 cell->push_back(MathAtom(p.release()));
1353 //lyxerr << "read cell: " << cell << endl;
1357 else if (t.cs().size()) {
1358 latexkeys const * l = in_word_set(t.cs());
1360 if (l->inset == "big") {
1362 docstring const delim = getToken().asInput();
1363 if (InsetMathBig::isBigInsetDelim(delim))
1364 cell->push_back(MathAtom(
1365 new InsetMathBig(t.cs(), delim)));
1367 cell->push_back(createInsetMath(t.cs()));
1372 else if (l->inset == "font") {
1373 cell->push_back(createInsetMath(t.cs()));
1374 parse(cell->back().nucleus()->cell(0),
1375 FLAG_ITEM, asMode(mode, l->extra));
1378 else if (l->inset == "oldfont") {
1379 cell->push_back(createInsetMath(t.cs()));
1380 parse(cell->back().nucleus()->cell(0),
1381 flags | FLAG_ALIGN, asMode(mode, l->extra));
1382 if (prevToken().cat() != catAlign &&
1383 prevToken().cs() != "\\")
1388 else if (l->inset == "style") {
1389 cell->push_back(createInsetMath(t.cs()));
1390 parse(cell->back().nucleus()->cell(0),
1391 flags | FLAG_ALIGN, mode);
1392 if (prevToken().cat() != catAlign &&
1393 prevToken().cs() != "\\")
1399 MathAtom at = createInsetMath(t.cs());
1400 for (InsetMath::idx_type i = 0; i < at->nargs(); ++i)
1401 parse(at.nucleus()->cell(i),
1402 FLAG_ITEM, asMode(mode, l->extra));
1403 cell->push_back(at);
1408 MathAtom at = createInsetMath(t.cs());
1409 InsetMath::mode_type m = mode;
1410 //if (m == InsetMath::UNDECIDED_MODE)
1411 //lyxerr << "default creation: m1: " << m << endl;
1412 if (at->currentMode() != InsetMath::UNDECIDED_MODE)
1413 m = at->currentMode();
1414 //lyxerr << "default creation: m2: " << m << endl;
1415 InsetMath::idx_type start = 0;
1416 // this fails on \bigg[...\bigg]
1418 //parse(opt, FLAG_OPTION, InsetMath::VERBATIM_MODE);
1421 // at.nucleus()->cell(0) = opt;
1423 for (InsetMath::idx_type i = start; i < at->nargs(); ++i) {
1424 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1427 cell->push_back(at);
1432 if (flags & FLAG_LEAVE) {
1433 flags &= ~FLAG_LEAVE;
1441 } // anonymous namespace
1444 void mathed_parse_cell(MathArray & ar, docstring const & str)
1446 Parser(str).parse(ar, 0, InsetMath::MATH_MODE);
1450 void mathed_parse_cell(MathArray & ar, istream & is)
1452 Parser(is).parse(ar, 0, InsetMath::MATH_MODE);
1456 bool mathed_parse_normal(MathAtom & t, docstring const & str)
1458 return Parser(str).parse(t);
1462 bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
1464 return Parser(lex).parse(t);
1468 void mathed_parse_normal(InsetMathGrid & grid, docstring const & str)
1470 Parser(str).parse1(grid, 0, InsetMath::MATH_MODE, false);
1476 fill(theCatcode, theCatcode + 128, catOther);
1477 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1478 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1480 theCatcode[int('\\')] = catEscape;
1481 theCatcode[int('{')] = catBegin;
1482 theCatcode[int('}')] = catEnd;
1483 theCatcode[int('$')] = catMath;
1484 theCatcode[int('&')] = catAlign;
1485 theCatcode[int('\n')] = catNewline;
1486 theCatcode[int('#')] = catParameter;
1487 theCatcode[int('^')] = catSuper;
1488 theCatcode[int('_')] = catSub;
1489 theCatcode[int(0x7f)] = catIgnore;
1490 theCatcode[int(' ')] = catSpace;
1491 theCatcode[int('\t')] = catSpace;
1492 theCatcode[int('\r')] = catNewline;
1493 theCatcode[int('~')] = catActive;
1494 theCatcode[int('%')] = catComment;