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"
66 #include "support/debug.h"
68 #include "support/convert.h"
69 #include "support/docstream.h"
81 InsetMath::mode_type asMode(InsetMath::mode_type oldmode, docstring const & str)
83 //lyxerr << "handling mode: '" << str << "'" << endl;
84 if (str == "mathmode")
85 return InsetMath::MATH_MODE;
86 if (str == "textmode" || str == "forcetext")
87 return InsetMath::TEXT_MODE;
92 bool stared(docstring const & s)
94 size_t const n = s.size();
95 return n && s[n - 1] == '*';
100 * Add the row \p cellrow to \p grid.
101 * \returns wether the row could be added. Adding a row can fail for
102 * environments like "equation" that have a fixed number of rows.
104 bool addRow(InsetMathGrid & grid, InsetMathGrid::row_type & cellrow,
105 docstring const & vskip, bool allow_newpage_ = true)
108 if (cellrow == grid.nrows()) {
109 //lyxerr << "adding row " << cellrow << endl;
110 grid.addRow(cellrow - 1);
111 if (cellrow == grid.nrows()) {
112 // We can't add a row to this grid, so let's
113 // append the content of this cell to the previous
115 // This does not happen in well formed .lyx files,
116 // but LyX versions 1.3.x and older could create
117 // such files and tex2lyx can still do that.
119 lyxerr << "ignoring extra row";
121 lyxerr << " with extra space " << to_utf8(vskip);
123 lyxerr << " with no page break allowed";
124 lyxerr << '.' << endl;
128 grid.vcrskip(Length(to_utf8(vskip)), cellrow - 1);
129 grid.rowinfo(cellrow - 1).allow_newpage_ = allow_newpage_;
135 * Add the column \p cellcol to \p grid.
136 * \returns wether the column could be added. Adding a column can fail for
137 * environments like "eqnarray" that have a fixed number of columns.
139 bool addCol(InsetMathGrid & grid, InsetMathGrid::col_type & cellcol)
142 if (cellcol == grid.ncols()) {
143 //lyxerr << "adding column " << cellcol << endl;
144 grid.addCol(cellcol);
145 if (cellcol == grid.ncols()) {
146 // We can't add a column to this grid, so let's
147 // append the content of this cell to the previous
149 // This does not happen in well formed .lyx files,
150 // but LyX versions 1.3.x and older could create
151 // such files and tex2lyx can still do that.
153 lyxerr << "ignoring extra column." << endl;
162 * Check wether the last row is empty and remove it if yes.
163 * Otherwise the following code
171 * will result in a grid with 3 rows (+ the dummy row that is always present),
172 * because the last '\\' opens a new row.
174 void delEmptyLastRow(InsetMathGrid & grid)
176 InsetMathGrid::row_type const row = grid.nrows() - 1;
177 for (InsetMathGrid::col_type col = 0; col < grid.ncols(); ++col) {
178 if (!grid.cell(grid.index(row, col)).empty())
181 // Copy the row information of the empty row (which would contain the
182 // last hline in the example above) to the dummy row and delete the
184 grid.rowinfo(row + 1) = grid.rowinfo(row);
189 // These are TeX's catcodes
191 catEscape, // 0 backslash
201 catSpace, // 10 space
202 catLetter, // 11 a-zA-Z
203 catOther, // 12 none of the above
206 catInvalid // 15 <delete>
209 CatCode theCatcode[128];
212 inline CatCode catcode(char_type c)
214 /* The only characters that are not catOther lie in the pure ASCII
215 * range. Therefore theCatcode has only 128 entries.
216 * TeX itself deals with 8bit characters, so if needed this table
217 * could be enlarged to 256 entries.
218 * Any larger value does not make sense, since the fact that we use
219 * unicode internally does not change Knuth's TeX engine.
220 * Apart from that a table for the full 21bit UCS4 range would waste
221 * too much memory. */
225 return theCatcode[c];
230 FLAG_ALIGN = 1 << 0, // next & or \\ ends the parsing process
231 FLAG_BRACE_LAST = 1 << 1, // next closing brace ends the parsing
232 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
233 FLAG_END = 1 << 3, // next \\end ends the parsing process
234 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
235 FLAG_TEXTMODE = 1 << 5, // we are in a box
236 FLAG_ITEM = 1 << 6, // read a (possibly braced) token
237 FLAG_LEAVE = 1 << 7, // leave the loop at the end
238 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
239 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
240 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
241 FLAG_OPTION = 1 << 11, // read [...] style option
242 FLAG_BRACED = 1 << 12 // read {...} style argument
247 // Helper class for parsing
253 Token() : cs_(), char_(0), cat_(catIgnore) {}
255 Token(char_type c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
257 explicit Token(docstring const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
260 docstring const & cs() const { return cs_; }
262 CatCode cat() const { return cat_; }
264 char_type character() const { return char_; }
266 docstring asString() const { return cs_.size() ? cs_ : docstring(1, char_); }
268 docstring asInput() const { return cs_.size() ? '\\' + cs_ : docstring(1, char_); }
280 ostream & operator<<(ostream & os, Token const & t)
283 docstring const & cs = t.cs();
284 // FIXME: For some strange reason, the stream operator instanciate
285 // a new Token before outputting the contents of t.cs().
286 // Because of this the line
288 // below becomes recursive.
289 // In order to avoid that we return early:
292 os << '\\' << to_utf8(cs);
294 else if (t.cat() == catLetter)
297 os << '[' << t.character() << ',' << t.cat() << ']';
305 typedef InsetMath::mode_type mode_type;
309 /// Only use this for reading from .lyx file format, for the reason
310 /// see Parser::tokenize(istream &).
311 Parser(istream & is);
313 Parser(docstring const & str);
316 bool parse(MathAtom & at);
318 void parse(MathData & array, unsigned flags, mode_type mode);
320 void parse1(InsetMathGrid & grid, unsigned flags, mode_type mode,
323 MathData parse(unsigned flags, mode_type mode);
325 int lineno() const { return lineno_; }
331 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
332 /// get arg delimited by 'left' and 'right'
333 docstring getArg(char_type left, char_type right);
337 void error(string const & msg);
338 void error(docstring const & msg) { error(to_utf8(msg)); }
339 /// dump contents to screen
341 /// Only use this for reading from .lyx file format (see
342 /// implementation for reason)
343 void tokenize(istream & is);
345 void tokenize(docstring const & s);
347 void skipSpaceTokens(idocstream & is, char_type c);
349 void push_back(Token const & t);
353 Token const & prevToken() const;
355 Token const & nextToken() const;
357 Token const & getToken();
358 /// skips spaces if any
361 void lex(docstring const & s);
365 docstring parse_verbatim_item();
367 docstring parse_verbatim_option();
372 vector<Token> tokens_;
375 /// Stack of active environments
376 vector<docstring> environments_;
380 Parser::Parser(Lexer & lexer)
381 : lineno_(lexer.getLineNo()), pos_(0)
383 tokenize(lexer.getStream());
388 Parser::Parser(istream & is)
389 : lineno_(0), pos_(0)
395 Parser::Parser(docstring const & str)
396 : lineno_(0), pos_(0)
402 void Parser::push_back(Token const & t)
404 tokens_.push_back(t);
408 void Parser::pop_back()
414 Token const & Parser::prevToken() const
416 static const Token dummy;
417 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
421 Token const & Parser::nextToken() const
423 static const Token dummy;
424 return good() ? tokens_[pos_] : dummy;
428 Token const & Parser::getToken()
430 static const Token dummy;
431 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
432 return good() ? tokens_[pos_++] : dummy;
436 void Parser::skipSpaces()
438 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
443 void Parser::putback()
449 bool Parser::good() const
451 return pos_ < tokens_.size();
455 char_type Parser::getChar()
458 error("The input stream is not well...");
462 return tokens_[pos_++].character();
466 docstring Parser::getArg(char_type left, char_type right)
471 char_type c = getChar();
476 while ((c = getChar()) != right && good())
483 void Parser::skipSpaceTokens(idocstream & is, char_type c)
485 // skip trailing spaces
486 while (catcode(c) == catSpace || catcode(c) == catNewline)
489 //lyxerr << "putting back: " << c << endl;
494 void Parser::tokenize(istream & is)
496 // eat everything up to the next \end_inset or end of stream
497 // and store it in s for further tokenization
502 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
503 s = s.substr(0, s.size() - 10);
507 // Remove the space after \end_inset
508 if (is.get(c) && c != ' ')
512 tokenize(from_utf8(s));
516 void Parser::tokenize(docstring const & buffer)
518 idocstringstream is(buffer, ios::in | ios::binary);
522 //lyxerr << "reading c: " << c << endl;
524 switch (catcode(c)) {
528 if (catcode(c) == catNewline)
529 ; //push_back(Token("par"));
531 push_back(Token('\n', catNewline));
539 while (is.get(c) && catcode(c) != catNewline)
549 error("unexpected end of input");
552 if (catcode(c) == catLetter) {
554 while (is.get(c) && catcode(c) == catLetter)
556 skipSpaceTokens(is, c);
565 push_back(Token(c, catcode(c)));
567 skipSpaceTokens(is, c);
572 lyxerr << "ignoring a char: " << int(c) << endl;
577 push_back(Token(c, catcode(c)));
587 void Parser::dump() const
589 lyxerr << "\nTokens: ";
590 for (unsigned i = 0; i < tokens_.size(); ++i) {
593 lyxerr << tokens_[i];
595 lyxerr << " pos: " << pos_ << endl;
599 void Parser::error(string const & msg)
601 lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
607 bool Parser::parse(MathAtom & at)
611 parse(ar, false, InsetMath::UNDECIDED_MODE);
612 if (ar.size() != 1 || ar.front()->getType() == hullNone) {
613 lyxerr << "unusual contents found: " << ar << endl;
614 at = MathAtom(new InsetMathPar(ar));
615 //if (at->nargs() > 0)
616 // at.nucleus()->cell(0) = ar;
618 // lyxerr << "unusual contents found: " << ar << endl;
626 docstring Parser::parse_verbatim_option()
630 if (nextToken().character() == '[') {
631 Token t = getToken();
632 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
633 if (t.cat() == catBegin) {
635 res += '{' + parse_verbatim_item() + '}';
644 docstring Parser::parse_verbatim_item()
648 if (nextToken().cat() == catBegin) {
649 Token t = getToken();
650 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
651 if (t.cat() == catBegin) {
653 res += '{' + parse_verbatim_item() + '}';
663 MathData Parser::parse(unsigned flags, mode_type mode)
666 parse(ar, flags, mode);
671 void Parser::parse(MathData & array, unsigned flags, mode_type mode)
673 InsetMathGrid grid(1, 1);
674 parse1(grid, flags, mode, false);
675 array = grid.cell(0);
679 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
682 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
686 void Parser::parse1(InsetMathGrid & grid, unsigned flags,
687 const mode_type mode, const bool numbered)
690 InsetMathGrid::row_type cellrow = 0;
691 InsetMathGrid::col_type cellcol = 0;
692 MathData * cell = &grid.cell(grid.index(cellrow, cellcol));
694 if (grid.asHullInset())
695 grid.asHullInset()->numbered(cellrow, numbered);
698 //lyxerr << " flags: " << flags << endl;
699 //lyxerr << " mode: " << mode << endl;
700 //lyxerr << "grid: " << grid << endl;
703 Token const & t = getToken();
706 lyxerr << "t: " << t << " flags: " << flags << endl;
707 lyxerr << "mode: " << mode << endl;
712 if (flags & FLAG_ITEM) {
714 if (t.cat() == catBegin) {
715 // skip the brace and collect everything to the next matching
717 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
721 // handle only this single token, leave the loop if done
726 if (flags & FLAG_BRACED) {
727 if (t.cat() == catSpace)
730 if (t.cat() != catBegin) {
731 error("opening brace expected");
735 // skip the brace and collect everything to the next matching
737 flags = FLAG_BRACE_LAST;
741 if (flags & FLAG_OPTION) {
742 if (t.cat() == catOther && t.character() == '[') {
744 parse(ar, FLAG_BRACK_LAST, mode);
747 // no option found, put back token and we are done
756 if (t.cat() == catMath) {
757 if (mode != InsetMath::MATH_MODE) {
758 // we are inside some text mode thingy, so opening new math is allowed
759 Token const & n = getToken();
760 if (n.cat() == catMath) {
761 // TeX's $$...$$ syntax for displayed math
762 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
763 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
764 getToken(); // skip the second '$' token
766 // simple $...$ stuff
768 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
769 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
773 else if (flags & FLAG_SIMPLE) {
774 // this is the end of the formula
779 error("something strange in the parser");
784 else if (t.cat() == catLetter)
785 cell->push_back(MathAtom(new InsetMathChar(t.character())));
787 else if (t.cat() == catSpace && mode != InsetMath::MATH_MODE) {
788 if (cell->empty() || cell->back()->getChar() != ' ')
789 cell->push_back(MathAtom(new InsetMathChar(t.character())));
792 else if (t.cat() == catNewline && mode != InsetMath::MATH_MODE) {
793 if (cell->empty() || cell->back()->getChar() != ' ')
794 cell->push_back(MathAtom(new InsetMathChar(' ')));
797 else if (t.cat() == catParameter) {
798 Token const & n = getToken();
799 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
802 else if (t.cat() == catActive)
803 cell->push_back(MathAtom(new InsetMathChar(t.character())));
805 else if (t.cat() == catBegin) {
807 parse(ar, FLAG_BRACE_LAST, mode);
808 // do not create a BraceInset if they were written by LyX
809 // this helps to keep the annoyance of "a choose b" to a minimum
810 if (ar.size() == 1 && ar[0]->extraBraces())
813 cell->push_back(MathAtom(new InsetMathBrace(ar)));
816 else if (t.cat() == catEnd) {
817 if (flags & FLAG_BRACE_LAST)
819 error("found '}' unexpectedly");
820 //BOOST_ASSERT(false);
821 //add(cell, '}', LM_TC_TEX);
824 else if (t.cat() == catAlign) {
825 //lyxerr << " column now " << (cellcol + 1)
826 // << " max: " << grid.ncols() << endl;
827 if (flags & FLAG_ALIGN)
829 if (addCol(grid, cellcol))
830 cell = &grid.cell(grid.index(cellrow, cellcol));
833 else if (t.cat() == catSuper || t.cat() == catSub) {
834 bool up = (t.cat() == catSuper);
835 // we need no new script inset if the last thing was a scriptinset,
836 // which has that script already not the same script already
838 cell->push_back(MathAtom(new InsetMathScript(up)));
839 else if (cell->back()->asScriptInset() &&
840 !cell->back()->asScriptInset()->has(up))
841 cell->back().nucleus()->asScriptInset()->ensure(up);
842 else if (cell->back()->asScriptInset())
843 cell->push_back(MathAtom(new InsetMathScript(up)));
845 cell->back() = MathAtom(new InsetMathScript(cell->back(), up));
846 InsetMathScript * p = cell->back().nucleus()->asScriptInset();
847 // special handling of {}-bases
848 // Here we could remove the brace inset for things
849 // like {a'}^2 and add the braces back in
850 // InsetMathScript::write().
851 // We do not do it, since it is not possible to detect
852 // reliably whether the braces are needed because the
853 // nucleus contains more than one symbol, or whether
854 // they are needed for unknown commands like \xx{a}_0
855 // or \yy{a}{b}_0. This was done in revision 14819
856 // in an unreliable way. See this thread
857 // http://www.mail-archive.com/lyx-devel%40lists.lyx.org/msg104917.html
859 parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
866 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
867 //lyxerr << "finished reading option" << endl;
871 else if (t.cat() == catOther)
872 cell->push_back(MathAtom(new InsetMathChar(t.character())));
874 else if (t.cat() == catComment) {
877 Token const & t = getToken();
878 if (t.cat() == catNewline)
882 cell->push_back(MathAtom(new InsetMathComment(s)));
890 else if (t.cs() == "lyxlock") {
892 cell->back().nucleus()->lock(true);
895 else if (t.cs() == "def" ||
896 t.cs() == "newcommand" ||
897 t.cs() == "renewcommand" ||
898 t.cs() == "newlyxcommand")
900 MacroType type = MacroTypeNewcommand;
906 vector<MathData> optionalValues;
907 if (type == MacroTypeDef) {
909 name = getToken().cs();
913 while (good() && nextToken().cat() != catBegin) {
914 pars += getToken().cs();
918 //lyxerr << "read \\def parameter list '" << pars << "'" << endl;
921 if (getToken().cat() != catBegin) {
922 error("'{' in \\newcommand expected (1) ");
926 name = getToken().cs();
928 if (getToken().cat() != catEnd) {
929 error("'}' in \\newcommand expected");
933 docstring const arg = getArg('[', ']');
935 nargs = convert<int>(arg);
937 // optional argument given?
939 while (nextToken().character() == '[') {
941 optionalValues.push_back(MathData());
942 parse(optionalValues[optionals], FLAG_BRACK_LAST, mode);
948 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
950 // is a version for display attached?
953 if (nextToken().cat() == catBegin)
954 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
956 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, optionals, type,
957 optionalValues, def, display)));
960 else if (t.cs() == "(") {
961 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
962 parse2(cell->back(), FLAG_SIMPLE2, InsetMath::MATH_MODE, false);
965 else if (t.cs() == "[") {
966 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
967 parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false);
970 else if (t.cs() == "protect")
971 // ignore \\protect, will hopefully be re-added during output
974 else if (t.cs() == "end") {
975 if (flags & FLAG_END) {
976 // eat environment name
977 docstring const name = getArg('{', '}');
978 if (environments_.empty())
979 error("'found \\end{" + name +
980 "}' without matching '\\begin{" +
982 else if (name != environments_.back())
983 error("'\\end{" + name +
984 "}' does not match '\\begin{" +
985 environments_.back() + "}'");
987 environments_.pop_back();
988 // Delete empty last row in matrix
990 // If you abuse InsetMathGrid for
991 // non-matrix like structures you
992 // probably need to refine this test.
993 // Right now we only have to test for
994 // single line hull insets.
995 if (grid.nrows() > 1)
996 delEmptyLastRow(grid);
1000 error("found 'end' unexpectedly");
1003 else if (t.cs() == ")") {
1004 if (flags & FLAG_SIMPLE2)
1006 error("found '\\)' unexpectedly");
1009 else if (t.cs() == "]") {
1010 if (flags & FLAG_EQUATION)
1012 error("found '\\]' unexpectedly");
1015 else if (t.cs() == "\\") {
1016 if (flags & FLAG_ALIGN)
1019 if (nextToken().asInput() == "*") {
1021 added = addRow(grid, cellrow, docstring(), false);
1023 added = addRow(grid, cellrow, getArg('[', ']'));
1025 error("missing token after \\\\");
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;