3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 If someone desperately needs partial "structures" (such as a few
14 cells of an array inset or similar) (s)he could uses the
15 following hack as starting point to write some macros:
20 \def\makeamptab{\catcode`\&=4\relax}
21 \def\makeampletter{\catcode`\&=11\relax}
22 \def\b{\makeampletter\expandafter\makeamptab\bi}
40 #include "MathParser.h"
42 #include "InsetMathArray.h"
43 #include "InsetMathBig.h"
44 #include "InsetMathBrace.h"
45 #include "InsetMathChar.h"
46 #include "InsetMathColor.h"
47 #include "InsetMathComment.h"
48 #include "InsetMathDelim.h"
49 #include "InsetMathEnv.h"
50 #include "InsetMathFrac.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() == "unit") {
1102 // Allowed formats \unit[val]{unit}
1104 parse(ar, FLAG_OPTION, mode);
1106 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNIT)));
1107 cell->back().nucleus()->cell(0) = ar;
1108 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1110 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNIT, 1)));
1111 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1114 else if (t.cs() == "unitfrac") {
1115 // Here allowed formats are \unitfrac[val]{num}{denom}
1117 parse(ar, FLAG_OPTION, mode);
1119 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC, 3)));
1120 cell->back().nucleus()->cell(2) = ar;
1122 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC)));
1124 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1125 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1128 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1129 cell->push_back(createInsetMath(t.cs()));
1130 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1131 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1134 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
1135 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1136 cell->push_back(MathAtom(new InsetMathRef(t.cs())));
1137 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1138 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1141 else if (t.cs() == "left") {
1143 Token const & tl = getToken();
1144 // \| and \Vert are equivalent, and InsetMathDelim
1146 // FIXME: fix this in InsetMathDelim itself!
1147 docstring const l = tl.cs() == "|" ? from_ascii("Vert") : tl.asString();
1149 parse(ar, FLAG_RIGHT, mode);
1153 Token const & tr = getToken();
1154 docstring const r = tr.cs() == "|" ? from_ascii("Vert") : tr.asString();
1155 cell->push_back(MathAtom(new InsetMathDelim(l, r, ar)));
1158 else if (t.cs() == "right") {
1159 if (flags & FLAG_RIGHT)
1161 //lyxerr << "got so far: '" << cell << "'" << endl;
1162 error("Unmatched right delimiter");
1166 else if (t.cs() == "begin") {
1167 docstring const name = getArg('{', '}');
1168 environments_.push_back(name);
1170 if (name == "array" || name == "subarray") {
1171 docstring const valign = parse_verbatim_option() + 'c';
1172 docstring const halign = parse_verbatim_item();
1173 cell->push_back(MathAtom(new InsetMathArray(name, (char)valign[0], halign)));
1174 parse2(cell->back(), FLAG_END, mode, false);
1177 else if (name == "tabular") {
1178 docstring const valign = parse_verbatim_option() + 'c';
1179 docstring const halign = parse_verbatim_item();
1180 cell->push_back(MathAtom(new InsetMathTabular(name, (char)valign[0], halign)));
1181 parse2(cell->back(), FLAG_END, InsetMath::TEXT_MODE, false);
1184 else if (name == "split" || name == "cases") {
1185 cell->push_back(createInsetMath(name));
1186 parse2(cell->back(), FLAG_END, mode, false);
1189 else if (name == "alignedat") {
1190 docstring const valign = parse_verbatim_option() + 'c';
1191 // ignore this for a while
1193 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1194 parse2(cell->back(), FLAG_END, mode, false);
1197 else if (name == "math") {
1198 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
1199 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, true);
1202 else if (name == "equation" || name == "equation*"
1203 || name == "displaymath") {
1204 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
1205 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation"));
1208 else if (name == "eqnarray" || name == "eqnarray*") {
1209 cell->push_back(MathAtom(new InsetMathHull(hullEqnArray)));
1210 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1213 else if (name == "align" || name == "align*") {
1214 cell->push_back(MathAtom(new InsetMathHull(hullAlign)));
1215 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1218 else if (name == "flalign" || name == "flalign*") {
1219 cell->push_back(MathAtom(new InsetMathHull(hullFlAlign)));
1220 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1223 else if (name == "alignat" || name == "alignat*") {
1224 // ignore this for a while
1226 cell->push_back(MathAtom(new InsetMathHull(hullAlignAt)));
1227 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1230 else if (name == "xalignat" || name == "xalignat*") {
1231 // ignore this for a while
1233 cell->push_back(MathAtom(new InsetMathHull(hullXAlignAt)));
1234 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1237 else if (name == "xxalignat") {
1238 // ignore this for a while
1240 cell->push_back(MathAtom(new InsetMathHull(hullXXAlignAt)));
1241 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1244 else if (name == "multline" || name == "multline*") {
1245 cell->push_back(MathAtom(new InsetMathHull(hullMultline)));
1246 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1249 else if (name == "gather" || name == "gather*") {
1250 cell->push_back(MathAtom(new InsetMathHull(hullGather)));
1251 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1254 else if (latexkeys const * l = in_word_set(name)) {
1255 if (l->inset == "matrix") {
1256 cell->push_back(createInsetMath(name));
1257 parse2(cell->back(), FLAG_END, mode, false);
1258 } else if (l->inset == "split") {
1259 docstring const valign = parse_verbatim_option() + 'c';
1260 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1261 parse2(cell->back(), FLAG_END, mode, false);
1264 lyxerr << "found math environment `" << to_utf8(name)
1265 << "' in symbols file with unsupported inset `"
1266 << to_utf8(l->inset) << "'." << endl;
1267 // create generic environment inset
1268 cell->push_back(MathAtom(new InsetMathEnv(name)));
1269 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1275 lyxerr << "found unknown math environment '" << to_utf8(name)
1277 // create generic environment inset
1278 cell->push_back(MathAtom(new InsetMathEnv(name)));
1279 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1283 else if (t.cs() == "kern") {
1287 Token const & t = getToken();
1293 if (isValidLength(to_utf8(s)))
1296 cell->push_back(MathAtom(new InsetMathKern(s)));
1299 else if (t.cs() == "label") {
1300 // FIXME: This is swallowed in inline formulas
1301 docstring label = parse_verbatim_item();
1304 if (grid.asHullInset()) {
1305 grid.asHullInset()->label(cellrow, label);
1307 cell->push_back(createInsetMath(t.cs()));
1308 cell->push_back(MathAtom(new InsetMathBrace(ar)));
1312 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1313 MathAtom at = createInsetMath(t.cs());
1314 at.nucleus()->cell(0) = *cell;
1316 parse(at.nucleus()->cell(1), flags, mode);
1317 cell->push_back(at);
1321 else if (t.cs() == "color") {
1322 docstring const color = parse_verbatim_item();
1323 cell->push_back(MathAtom(new InsetMathColor(true, color)));
1324 parse(cell->back().nucleus()->cell(0), flags, mode);
1328 else if (t.cs() == "textcolor") {
1329 docstring const color = parse_verbatim_item();
1330 cell->push_back(MathAtom(new InsetMathColor(false, color)));
1331 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1334 else if (t.cs() == "normalcolor") {
1335 cell->push_back(createInsetMath(t.cs()));
1336 parse(cell->back().nucleus()->cell(0), flags, mode);
1340 else if (t.cs() == "substack") {
1341 cell->push_back(createInsetMath(t.cs()));
1342 parse2(cell->back(), FLAG_ITEM, mode, false);
1345 else if (t.cs() == "xymatrix") {
1346 odocstringstream os;
1347 while (good() && nextToken().cat() != catBegin)
1348 os << getToken().asInput();
1349 cell->push_back(createInsetMath(t.cs() + os.str()));
1350 parse2(cell->back(), FLAG_ITEM, mode, false);
1353 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1354 cell->push_back(createInsetMath(t.cs()));
1355 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, InsetMath::TEXT_MODE);
1356 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, InsetMath::TEXT_MODE);
1357 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, InsetMath::TEXT_MODE);
1360 else if (t.cs() == "tag") {
1361 if (nextToken().character() == '*') {
1363 cell->push_back(createInsetMath(t.cs() + '*'));
1365 cell->push_back(createInsetMath(t.cs()));
1366 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1370 else if (t.cs() == "infer") {
1372 parse(ar, FLAG_OPTION, mode);
1373 cell->push_back(createInsetMath(t.cs()));
1374 parse2(cell->back(), FLAG_ITEM, mode, false);
1378 else if (1 && t.cs() == "ar") {
1379 auto_ptr<InsetMathXYArrow> p(new InsetMathXYArrow);
1380 // try to read target
1381 parse(p->cell(0), FLAG_OTPTION, mode);
1382 // try to read label
1383 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1384 p->up_ = nextToken().cat() == catSuper;
1386 parse(p->cell(1), FLAG_ITEM, mode);
1387 //lyxerr << "read label: " << p->cell(1) << endl;
1390 cell->push_back(MathAtom(p.release()));
1391 //lyxerr << "read cell: " << cell << endl;
1395 else if (t.cs().size()) {
1396 latexkeys const * l = in_word_set(t.cs());
1398 if (l->inset == "big") {
1400 docstring const delim = getToken().asInput();
1401 if (InsetMathBig::isBigInsetDelim(delim))
1402 cell->push_back(MathAtom(
1403 new InsetMathBig(t.cs(), delim)));
1405 cell->push_back(createInsetMath(t.cs()));
1410 else if (l->inset == "font") {
1411 cell->push_back(createInsetMath(t.cs()));
1412 parse(cell->back().nucleus()->cell(0),
1413 FLAG_ITEM, asMode(mode, l->extra));
1416 else if (l->inset == "oldfont") {
1417 cell->push_back(createInsetMath(t.cs()));
1418 parse(cell->back().nucleus()->cell(0),
1419 flags | FLAG_ALIGN, asMode(mode, l->extra));
1420 if (prevToken().cat() != catAlign &&
1421 prevToken().cs() != "\\")
1426 else if (l->inset == "style") {
1427 cell->push_back(createInsetMath(t.cs()));
1428 parse(cell->back().nucleus()->cell(0),
1429 flags | FLAG_ALIGN, mode);
1430 if (prevToken().cat() != catAlign &&
1431 prevToken().cs() != "\\")
1437 MathAtom at = createInsetMath(t.cs());
1438 for (InsetMath::idx_type i = 0; i < at->nargs(); ++i)
1439 parse(at.nucleus()->cell(i),
1440 FLAG_ITEM, asMode(mode, l->extra));
1441 cell->push_back(at);
1446 MathAtom at = createInsetMath(t.cs());
1447 InsetMath::mode_type m = mode;
1448 //if (m == InsetMath::UNDECIDED_MODE)
1449 //lyxerr << "default creation: m1: " << m << endl;
1450 if (at->currentMode() != InsetMath::UNDECIDED_MODE)
1451 m = at->currentMode();
1452 //lyxerr << "default creation: m2: " << m << endl;
1453 InsetMath::idx_type start = 0;
1454 // this fails on \bigg[...\bigg]
1456 //parse(opt, FLAG_OPTION, InsetMath::VERBATIM_MODE);
1459 // at.nucleus()->cell(0) = opt;
1461 for (InsetMath::idx_type i = start; i < at->nargs(); ++i) {
1462 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1465 cell->push_back(at);
1470 if (flags & FLAG_LEAVE) {
1471 flags &= ~FLAG_LEAVE;
1479 } // anonymous namespace
1482 void mathed_parse_cell(MathData & ar, docstring const & str)
1484 Parser(str).parse(ar, 0, InsetMath::MATH_MODE);
1488 void mathed_parse_cell(MathData & ar, istream & is)
1490 Parser(is).parse(ar, 0, InsetMath::MATH_MODE);
1494 bool mathed_parse_normal(MathAtom & t, docstring const & str)
1496 return Parser(str).parse(t);
1500 bool mathed_parse_normal(MathAtom & t, Lexer & lex)
1502 return Parser(lex).parse(t);
1506 void mathed_parse_normal(InsetMathGrid & grid, docstring const & str)
1508 Parser(str).parse1(grid, 0, InsetMath::MATH_MODE, false);
1514 fill(theCatcode, theCatcode + 128, catOther);
1515 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1516 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1518 theCatcode[int('\\')] = catEscape;
1519 theCatcode[int('{')] = catBegin;
1520 theCatcode[int('}')] = catEnd;
1521 theCatcode[int('$')] = catMath;
1522 theCatcode[int('&')] = catAlign;
1523 theCatcode[int('\n')] = catNewline;
1524 theCatcode[int('#')] = catParameter;
1525 theCatcode[int('^')] = catSuper;
1526 theCatcode[int('_')] = catSub;
1527 theCatcode[int(0x7f)] = catIgnore;
1528 theCatcode[int(' ')] = catSpace;
1529 theCatcode[int('\t')] = catSpace;
1530 theCatcode[int('\r')] = catNewline;
1531 theCatcode[int('~')] = catActive;
1532 theCatcode[int('%')] = catComment;