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"
81 using std::istringstream;
91 InsetMath::mode_type asMode(InsetMath::mode_type oldmode, docstring const & str)
93 //lyxerr << "handling mode: '" << str << "'" << endl;
94 if (str == "mathmode")
95 return InsetMath::MATH_MODE;
96 if (str == "textmode" || str == "forcetext")
97 return InsetMath::TEXT_MODE;
102 bool stared(docstring const & s)
104 size_t const n = s.size();
105 return n && s[n - 1] == '*';
110 * Add the row \p cellrow to \p grid.
111 * \returns wether the row could be added. Adding a row can fail for
112 * environments like "equation" that have a fixed number of rows.
114 bool addRow(InsetMathGrid & grid, InsetMathGrid::row_type & cellrow,
115 docstring const & vskip)
118 if (cellrow == grid.nrows()) {
119 //lyxerr << "adding row " << cellrow << endl;
120 grid.addRow(cellrow - 1);
121 if (cellrow == grid.nrows()) {
122 // We can't add a row to this grid, so let's
123 // append the content of this cell to the previous
125 // This does not happen in well formed .lyx files,
126 // but LyX versions 1.3.x and older could create
127 // such files and tex2lyx can still do that.
129 lyxerr << "ignoring extra row";
131 lyxerr << " with extra space " << to_utf8(vskip);
132 lyxerr << '.' << endl;
136 grid.vcrskip(LyXLength(to_utf8(vskip)), cellrow - 1);
142 * Add the column \p cellcol to \p grid.
143 * \returns wether the column could be added. Adding a column can fail for
144 * environments like "eqnarray" that have a fixed number of columns.
146 bool addCol(InsetMathGrid & grid, InsetMathGrid::col_type & cellcol)
149 if (cellcol == grid.ncols()) {
150 //lyxerr << "adding column " << cellcol << endl;
151 grid.addCol(cellcol - 1);
152 if (cellcol == grid.ncols()) {
153 // We can't add a column to this grid, so let's
154 // append the content of this cell to the previous
156 // This does not happen in well formed .lyx files,
157 // but LyX versions 1.3.x and older could create
158 // such files and tex2lyx can still do that.
160 lyxerr << "ignoring extra column." << endl;
169 * Check wether the last row is empty and remove it if yes.
170 * Otherwise the following code
178 * will result in a grid with 3 rows (+ the dummy row that is always present),
179 * because the last '\\' opens a new row.
181 void delEmptyLastRow(InsetMathGrid & grid)
183 InsetMathGrid::row_type const row = grid.nrows() - 1;
184 for (InsetMathGrid::col_type col = 0; col < grid.ncols(); ++col) {
185 if (!grid.cell(grid.index(row, col)).empty())
188 // Copy the row information of the empty row (which would contain the
189 // last hline in the example above) to the dummy row and delete the
191 grid.rowinfo(row + 1) = grid.rowinfo(row);
196 // These are TeX's catcodes
198 catEscape, // 0 backslash
208 catSpace, // 10 space
209 catLetter, // 11 a-zA-Z
210 catOther, // 12 none of the above
213 catInvalid // 15 <delete>
216 CatCode theCatcode[256];
219 inline CatCode catcode(unsigned char c)
221 return theCatcode[c];
226 FLAG_ALIGN = 1 << 0, // next & or \\ ends the parsing process
227 FLAG_BRACE_LAST = 1 << 1, // next closing brace ends the parsing
228 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
229 FLAG_END = 1 << 3, // next \\end ends the parsing process
230 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
231 FLAG_TEXTMODE = 1 << 5, // we are in a box
232 FLAG_ITEM = 1 << 6, // read a (possibly braced token)
233 FLAG_LEAVE = 1 << 7, // leave the loop at the end
234 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
235 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
236 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
237 FLAG_OPTION = 1 << 11, // read [...] style option
238 FLAG_BRACED = 1 << 12 // read {...} style argument
243 // Helper class for parsing
249 Token() : cs_(), char_(0), cat_(catIgnore) {}
251 Token(char_type c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
253 Token(docstring const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
256 docstring const & cs() const { return cs_; }
258 CatCode cat() const { return cat_; }
260 char_type character() const { return char_; }
262 docstring asString() const { return cs_.size() ? cs_ : docstring(1, char_); }
264 docstring asInput() const { return cs_.size() ? '\\' + cs_ : docstring(1, char_); }
275 ostream & operator<<(ostream & os, Token const & t)
278 os << '\\' << t.cs();
279 else if (t.cat() == catLetter)
282 os << '[' << t.character() << ',' << t.cat() << ']';
290 typedef InsetMath::mode_type mode_type;
293 Parser(LyXLex & lex);
295 Parser(istream & is);
298 bool parse(MathAtom & at);
300 void parse(MathArray & array, unsigned flags, mode_type mode);
302 void parse1(InsetMathGrid & grid, unsigned flags, mode_type mode,
305 MathArray parse(unsigned flags, mode_type mode);
307 int lineno() const { return lineno_; }
313 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
314 /// get arg delimited by 'left' and 'right'
315 docstring getArg(char_type left, char_type right);
319 void error(string const & msg);
320 void error(docstring const & msg) { error(to_utf8(msg)); }
321 /// dump contents to screen
324 void tokenize(istream & is);
326 void tokenize(docstring const & s);
328 void skipSpaceTokens(idocstream & is, char_type c);
330 void push_back(Token const & t);
334 Token const & prevToken() const;
336 Token const & nextToken() const;
338 Token const & getToken();
339 /// skips spaces if any
342 void lex(docstring const & s);
346 docstring parse_verbatim_item();
348 docstring parse_verbatim_option();
353 vector<Token> tokens_;
356 /// Stack of active environments
357 vector<docstring> environments_;
361 Parser::Parser(LyXLex & lexer)
362 : lineno_(lexer.getLineNo()), pos_(0)
364 tokenize(lexer.getStream());
369 Parser::Parser(istream & is)
370 : lineno_(0), pos_(0)
376 void Parser::push_back(Token const & t)
378 tokens_.push_back(t);
382 void Parser::pop_back()
388 Token const & Parser::prevToken() const
390 static const Token dummy;
391 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
395 Token const & Parser::nextToken() const
397 static const Token dummy;
398 return good() ? tokens_[pos_] : dummy;
402 Token const & Parser::getToken()
404 static const Token dummy;
405 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
406 return good() ? tokens_[pos_++] : dummy;
410 void Parser::skipSpaces()
412 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
417 void Parser::putback()
423 bool Parser::good() const
425 return pos_ < tokens_.size();
429 char_type Parser::getChar()
432 error("The input stream is not well...");
433 return tokens_[pos_++].character();
437 docstring Parser::getArg(char_type left, char_type right)
442 char_type c = getChar();
447 while ((c = getChar()) != right && good())
454 void Parser::skipSpaceTokens(idocstream & is, char_type c)
456 // skip trailing spaces
457 while (catcode(c) == catSpace || catcode(c) == catNewline)
460 //lyxerr << "putting back: " << c << endl;
465 void Parser::tokenize(istream & is)
467 // eat everything up to the next \end_inset or end of stream
468 // and store it in s for further tokenization
473 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
474 s = s.substr(0, s.size() - 10);
478 // Remove the space after \end_inset
479 if (is.get(c) && c != ' ')
483 tokenize(from_utf8(s));
487 void Parser::tokenize(docstring const & buffer)
489 idocstringstream is(buffer, ios::in | ios::binary);
493 //lyxerr << "reading c: " << c << endl;
495 switch (catcode(c)) {
499 if (catcode(c) == catNewline)
500 ; //push_back(Token("par"));
502 push_back(Token('\n', catNewline));
510 while (is.get(c) && catcode(c) != catNewline)
520 error("unexpected end of input");
523 if (catcode(c) == catLetter) {
525 while (is.get(c) && catcode(c) == catLetter)
527 skipSpaceTokens(is, c);
536 push_back(Token(c, catcode(c)));
538 skipSpaceTokens(is, c);
543 lyxerr << "ignoring a char: " << int(c) << endl;
548 push_back(Token(c, catcode(c)));
558 void Parser::dump() const
560 lyxerr << "\nTokens: ";
561 for (unsigned i = 0; i < tokens_.size(); ++i) {
564 lyxerr << tokens_[i];
566 lyxerr << " pos: " << pos_ << endl;
570 void Parser::error(string const & msg)
572 lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
578 bool Parser::parse(MathAtom & at)
582 parse(ar, false, InsetMath::UNDECIDED_MODE);
583 if (ar.size() != 1 || ar.front()->getType() == hullNone) {
584 lyxerr << "unusual contents found: " << ar << endl;
585 at = MathAtom(new InsetMathPar(ar));
586 //if (at->nargs() > 0)
587 // at.nucleus()->cell(0) = ar;
589 // lyxerr << "unusual contents found: " << ar << endl;
597 docstring Parser::parse_verbatim_option()
601 if (nextToken().character() == '[') {
602 Token t = getToken();
603 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
604 if (t.cat() == catBegin) {
606 res += '{' + parse_verbatim_item() + '}';
615 docstring Parser::parse_verbatim_item()
619 if (nextToken().cat() == catBegin) {
620 Token t = getToken();
621 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
622 if (t.cat() == catBegin) {
624 res += '{' + parse_verbatim_item() + '}';
634 MathArray Parser::parse(unsigned flags, mode_type mode)
637 parse(ar, flags, mode);
642 void Parser::parse(MathArray & array, unsigned flags, mode_type mode)
644 InsetMathGrid grid(1, 1);
645 parse1(grid, flags, mode, false);
646 array = grid.cell(0);
650 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
653 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
657 void Parser::parse1(InsetMathGrid & grid, unsigned flags,
658 const mode_type mode, const bool numbered)
661 InsetMathGrid::row_type cellrow = 0;
662 InsetMathGrid::col_type cellcol = 0;
663 MathArray * cell = &grid.cell(grid.index(cellrow, cellcol));
665 if (grid.asHullInset())
666 grid.asHullInset()->numbered(cellrow, numbered);
669 //lyxerr << " flags: " << flags << endl;
670 //lyxerr << " mode: " << mode << endl;
671 //lyxerr << "grid: " << grid << endl;
674 Token const & t = getToken();
677 lyxerr << "t: " << t << " flags: " << flags << endl;
678 lyxerr << "mode: " << mode << endl;
683 if (flags & FLAG_ITEM) {
685 if (t.cat() == catBegin) {
686 // skip the brace and collect everything to the next matching
688 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
692 // handle only this single token, leave the loop if done
697 if (flags & FLAG_BRACED) {
698 if (t.cat() == catSpace)
701 if (t.cat() != catBegin) {
702 error("opening brace expected");
706 // skip the brace and collect everything to the next matching
708 flags = FLAG_BRACE_LAST;
712 if (flags & FLAG_OPTION) {
713 if (t.cat() == catOther && t.character() == '[') {
715 parse(ar, FLAG_BRACK_LAST, mode);
718 // no option found, put back token and we are done
727 if (t.cat() == catMath) {
728 if (mode != InsetMath::MATH_MODE) {
729 // we are inside some text mode thingy, so opening new math is allowed
730 Token const & n = getToken();
731 if (n.cat() == catMath) {
732 // TeX's $$...$$ syntax for displayed math
733 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
734 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
735 getToken(); // skip the second '$' token
737 // simple $...$ stuff
739 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
740 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
744 else if (flags & FLAG_SIMPLE) {
745 // this is the end of the formula
750 error("something strange in the parser");
755 else if (t.cat() == catLetter)
756 cell->push_back(MathAtom(new InsetMathChar(t.character())));
758 else if (t.cat() == catSpace && mode != InsetMath::MATH_MODE) {
759 if (cell->empty() || cell->back()->getChar() != ' ')
760 cell->push_back(MathAtom(new InsetMathChar(t.character())));
763 else if (t.cat() == catNewline && mode != InsetMath::MATH_MODE) {
764 if (cell->empty() || cell->back()->getChar() != ' ')
765 cell->push_back(MathAtom(new InsetMathChar(' ')));
768 else if (t.cat() == catParameter) {
769 Token const & n = getToken();
770 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
773 else if (t.cat() == catActive)
774 cell->push_back(MathAtom(new InsetMathChar(t.character())));
776 else if (t.cat() == catBegin) {
778 parse(ar, FLAG_BRACE_LAST, mode);
779 // do not create a BraceInset if they were written by LyX
780 // this helps to keep the annoyance of "a choose b" to a minimum
781 if (ar.size() == 1 && ar[0]->extraBraces())
784 cell->push_back(MathAtom(new InsetMathBrace(ar)));
787 else if (t.cat() == catEnd) {
788 if (flags & FLAG_BRACE_LAST)
790 error("found '}' unexpectedly");
791 //BOOST_ASSERT(false);
792 //add(cell, '}', LM_TC_TEX);
795 else if (t.cat() == catAlign) {
796 //lyxerr << " column now " << (cellcol + 1)
797 // << " max: " << grid.ncols() << endl;
798 if (flags & FLAG_ALIGN)
800 if (addCol(grid, cellcol))
801 cell = &grid.cell(grid.index(cellrow, cellcol));
804 else if (t.cat() == catSuper || t.cat() == catSub) {
805 bool up = (t.cat() == catSuper);
806 // we need no new script inset if the last thing was a scriptinset,
807 // which has that script already not the same script already
809 cell->push_back(MathAtom(new InsetMathScript(up)));
810 else if (cell->back()->asScriptInset() &&
811 !cell->back()->asScriptInset()->has(up))
812 cell->back().nucleus()->asScriptInset()->ensure(up);
813 else if (cell->back()->asScriptInset())
814 cell->push_back(MathAtom(new InsetMathScript(up)));
816 cell->back() = MathAtom(new InsetMathScript(cell->back(), up));
817 InsetMathScript * p = cell->back().nucleus()->asScriptInset();
818 // special handling of {}-bases
819 // Test for empty brace inset, otherwise \xxx{\vec{H}}_{0}
820 // where \xxx is an unknown command gets misparsed to
821 // \xxx\vec{H}_{0}, and that is invalid LaTeX.
822 // is this always correct?
823 if (p->nuc().size() == 1 &&
824 p->nuc().back()->asBraceInset() &&
825 p->nuc().back()->asBraceInset()->cell(0).empty())
826 p->nuc() = p->nuc().back()->asNestInset()->cell(0);
827 parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
834 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
835 //lyxerr << "finished reading option" << endl;
839 else if (t.cat() == catOther)
840 cell->push_back(MathAtom(new InsetMathChar(t.character())));
842 else if (t.cat() == catComment) {
845 Token const & t = getToken();
846 if (t.cat() == catNewline)
850 cell->push_back(MathAtom(new InsetMathComment(s)));
858 else if (t.cs() == "lyxlock") {
860 cell->back().nucleus()->lock(true);
863 else if (t.cs() == "def" ||
864 t.cs() == "newcommand" ||
865 t.cs() == "renewcommand")
867 docstring const type = t.cs();
870 if (t.cs() == "def") {
872 name = getToken().cs();
876 while (good() && nextToken().cat() != catBegin) {
877 pars += getToken().cs();
881 //lyxerr << "read \\def parameter list '" << pars << "'" << endl;
883 } else { // t.cs() == "newcommand" || t.cs() == "renewcommand"
885 if (getToken().cat() != catBegin) {
886 error("'{' in \\newcommand expected (1) ");
890 name = getToken().cs();
892 if (getToken().cat() != catEnd) {
893 error("'}' in \\newcommand expected");
897 docstring const arg = getArg('[', ']');
899 nargs = convert<int>(arg);
904 parse(ar1, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
906 // we cannot handle recursive stuff at all
908 //test.push_back(createInsetMath(name));
909 //if (ar1.contains(test)) {
910 // error("we cannot handle recursive macros at all.");
914 // is a version for display attached?
917 if (nextToken().cat() == catBegin)
918 parse(ar2, FLAG_ITEM, InsetMath::MATH_MODE);
920 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, type,
924 else if (t.cs() == "(") {
925 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
926 parse2(cell->back(), FLAG_SIMPLE2, InsetMath::MATH_MODE, false);
929 else if (t.cs() == "[") {
930 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
931 parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false);
934 else if (t.cs() == "protect")
935 // ignore \\protect, will hopefully be re-added during output
938 else if (t.cs() == "end") {
939 if (flags & FLAG_END) {
940 // eat environment name
941 docstring const name = getArg('{', '}');
942 if (environments_.empty())
943 error("'found \\end{" + name +
944 "}' without matching '\\begin{" +
946 else if (name != environments_.back())
947 error("'\\end{" + name +
948 "}' does not match '\\begin{" +
949 environments_.back() + "}'");
951 environments_.pop_back();
952 // Delete empty last row in matrix
954 // If you abuse InsetMathGrid for
955 // non-matrix like structures you
956 // probably need to refine this test.
957 // Right now we only have to test for
958 // single line hull insets.
959 if (grid.nrows() > 1)
960 delEmptyLastRow(grid);
964 error("found 'end' unexpectedly");
967 else if (t.cs() == ")") {
968 if (flags & FLAG_SIMPLE2)
970 error("found '\\)' unexpectedly");
973 else if (t.cs() == "]") {
974 if (flags & FLAG_EQUATION)
976 error("found '\\]' unexpectedly");
979 else if (t.cs() == "\\") {
980 if (flags & FLAG_ALIGN)
982 if (addRow(grid, cellrow, getArg('[', ']'))) {
984 if (grid.asHullInset())
985 grid.asHullInset()->numbered(
987 cell = &grid.cell(grid.index(cellrow,
993 else if (t.cs() == "multicolumn") {
994 // extract column count and insert dummy cells
996 parse(count, FLAG_ITEM, mode);
998 if (!extractNumber(count, cols)) {
999 lyxerr << " can't extract number of cells from " << count << endl;
1001 // resize the table if necessary
1002 for (int i = 0; i < cols; ++i) {
1003 if (addCol(grid, cellcol)) {
1004 cell = &grid.cell(grid.index(
1006 // mark this as dummy
1007 grid.cellinfo(grid.index(
1008 cellrow, cellcol)).dummy_ = true;
1011 // the last cell is the real thing, not a dummy
1012 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
1014 // read special alignment
1016 parse(align, FLAG_ITEM, mode);
1017 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
1019 // parse the remaining contents into the "real" cell
1020 parse(*cell, FLAG_ITEM, mode);
1024 else if (t.cs() == "limits")
1027 else if (t.cs() == "nolimits")
1030 else if (t.cs() == "nonumber") {
1031 if (grid.asHullInset())
1032 grid.asHullInset()->numbered(cellrow, false);
1035 else if (t.cs() == "number") {
1036 if (grid.asHullInset())
1037 grid.asHullInset()->numbered(cellrow, true);
1040 else if (t.cs() == "hline") {
1041 grid.rowinfo(cellrow).lines_ ++;
1044 else if (t.cs() == "sqrt") {
1046 parse(ar, FLAG_OPTION, mode);
1048 cell->push_back(MathAtom(new InsetMathRoot));
1049 cell->back().nucleus()->cell(0) = ar;
1050 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1052 cell->push_back(MathAtom(new InsetMathSqrt));
1053 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1057 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1058 cell->push_back(createInsetMath(t.cs()));
1059 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1060 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1063 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
1064 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1065 cell->push_back(MathAtom(new RefInset(t.cs())));
1066 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1067 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1070 else if (t.cs() == "left") {
1072 Token const & tl = getToken();
1073 // \| and \Vert are equivalent, and InsetMathDelim
1075 // FIXME: fix this in InsetMathDelim itself!
1076 docstring const l = tl.cs() == "|" ? from_ascii("Vert") : tl.asString();
1078 parse(ar, FLAG_RIGHT, mode);
1080 Token const & tr = getToken();
1081 docstring const r = tr.cs() == "|" ? from_ascii("Vert") : tr.asString();
1082 cell->push_back(MathAtom(new InsetMathDelim(l, r, ar)));
1085 else if (t.cs() == "right") {
1086 if (flags & FLAG_RIGHT)
1088 //lyxerr << "got so far: '" << cell << "'" << endl;
1089 error("Unmatched right delimiter");
1093 else if (t.cs() == "begin") {
1094 docstring const name = getArg('{', '}');
1095 environments_.push_back(name);
1097 if (name == "array" || name == "subarray") {
1098 docstring const valign = parse_verbatim_option() + 'c';
1099 docstring const halign = parse_verbatim_item();
1100 cell->push_back(MathAtom(new InsetMathArray(name, (char)valign[0], halign)));
1101 parse2(cell->back(), FLAG_END, mode, false);
1104 else if (name == "tabular") {
1105 docstring const valign = parse_verbatim_option() + 'c';
1106 docstring const halign = parse_verbatim_item();
1107 cell->push_back(MathAtom(new InsetMathTabular(name, (char)valign[0], halign)));
1108 parse2(cell->back(), FLAG_END, InsetMath::TEXT_MODE, false);
1111 else if (name == "split" || name == "cases") {
1112 cell->push_back(createInsetMath(name));
1113 parse2(cell->back(), FLAG_END, mode, false);
1116 else if (name == "alignedat") {
1117 docstring const valign = parse_verbatim_option() + 'c';
1118 // ignore this for a while
1120 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1121 parse2(cell->back(), FLAG_END, mode, false);
1124 else if (name == "math") {
1125 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
1126 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, true);
1129 else if (name == "equation" || name == "equation*"
1130 || name == "displaymath") {
1131 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
1132 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation"));
1135 else if (name == "eqnarray" || name == "eqnarray*") {
1136 cell->push_back(MathAtom(new InsetMathHull(hullEqnArray)));
1137 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1140 else if (name == "align" || name == "align*") {
1141 cell->push_back(MathAtom(new InsetMathHull(hullAlign)));
1142 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1145 else if (name == "flalign" || name == "flalign*") {
1146 cell->push_back(MathAtom(new InsetMathHull(hullFlAlign)));
1147 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1150 else if (name == "alignat" || name == "alignat*") {
1151 // ignore this for a while
1153 cell->push_back(MathAtom(new InsetMathHull(hullAlignAt)));
1154 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1157 else if (name == "xalignat" || name == "xalignat*") {
1158 // ignore this for a while
1160 cell->push_back(MathAtom(new InsetMathHull(hullXAlignAt)));
1161 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1164 else if (name == "xxalignat") {
1165 // ignore this for a while
1167 cell->push_back(MathAtom(new InsetMathHull(hullXXAlignAt)));
1168 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1171 else if (name == "multline" || name == "multline*") {
1172 cell->push_back(MathAtom(new InsetMathHull(hullMultline)));
1173 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1176 else if (name == "gather" || name == "gather*") {
1177 cell->push_back(MathAtom(new InsetMathHull(hullGather)));
1178 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1181 else if (latexkeys const * l = in_word_set(name)) {
1182 if (l->inset == "matrix") {
1183 cell->push_back(createInsetMath(name));
1184 parse2(cell->back(), FLAG_END, mode, false);
1185 } else if (l->inset == "split") {
1186 docstring const valign = parse_verbatim_option() + 'c';
1187 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1188 parse2(cell->back(), FLAG_END, mode, false);
1191 lyxerr << "found math environment `" << name
1192 << "' in symbols file with unsupported inset `"
1193 << l->inset << "'." << endl;
1194 // create generic environment inset
1195 cell->push_back(MathAtom(new InsetMathEnv(name)));
1196 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1202 lyxerr << "found unknown math environment '" << name << "'" << endl;
1203 // create generic environment inset
1204 cell->push_back(MathAtom(new InsetMathEnv(name)));
1205 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1209 else if (t.cs() == "kern") {
1210 #ifdef WITH_WARNINGS
1215 Token const & t = getToken();
1221 if (isValidLength(to_utf8(s)))
1224 cell->push_back(MathAtom(new InsetMathKern(s)));
1227 else if (t.cs() == "label") {
1228 // FIXME: This is swallowed in inline formulas
1229 docstring label = parse_verbatim_item();
1232 if (grid.asHullInset()) {
1233 grid.asHullInset()->label(cellrow, label);
1235 cell->push_back(createInsetMath(t.cs()));
1236 cell->push_back(MathAtom(new InsetMathBrace(ar)));
1240 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1241 MathAtom at = createInsetMath(t.cs());
1242 at.nucleus()->cell(0) = *cell;
1244 parse(at.nucleus()->cell(1), flags, mode);
1245 cell->push_back(at);
1249 else if (t.cs() == "color") {
1250 docstring const color = parse_verbatim_item();
1251 cell->push_back(MathAtom(new InsetMathColor(true, color)));
1252 parse(cell->back().nucleus()->cell(0), flags, mode);
1256 else if (t.cs() == "textcolor") {
1257 docstring const color = parse_verbatim_item();
1258 cell->push_back(MathAtom(new InsetMathColor(false, color)));
1259 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1262 else if (t.cs() == "normalcolor") {
1263 cell->push_back(createInsetMath(t.cs()));
1264 parse(cell->back().nucleus()->cell(0), flags, mode);
1268 else if (t.cs() == "substack") {
1269 cell->push_back(createInsetMath(t.cs()));
1270 parse2(cell->back(), FLAG_ITEM, mode, false);
1273 else if (t.cs() == "xymatrix") {
1274 odocstringstream os;
1275 while (good() && nextToken().cat() != catBegin)
1276 os << getToken().asInput();
1277 cell->push_back(createInsetMath(t.cs() + os.str()));
1278 parse2(cell->back(), FLAG_ITEM, mode, false);
1281 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1282 cell->push_back(createInsetMath(t.cs()));
1283 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, InsetMath::TEXT_MODE);
1284 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, InsetMath::TEXT_MODE);
1285 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, InsetMath::TEXT_MODE);
1288 else if (t.cs() == "tag") {
1289 if (nextToken().character() == '*') {
1291 cell->push_back(createInsetMath(t.cs() + '*'));
1293 cell->push_back(createInsetMath(t.cs()));
1294 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1298 else if (t.cs() == "infer") {
1300 parse(ar, FLAG_OPTION, mode);
1301 cell->push_back(createInsetMath(t.cs()));
1302 parse2(cell->back(), FLAG_ITEM, mode, false);
1306 else if (1 && t.cs() == "ar") {
1307 auto_ptr<InsetMathXYArrow> p(new InsetMathXYArrow);
1308 // try to read target
1309 parse(p->cell(0), FLAG_OTPTION, mode);
1310 // try to read label
1311 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1312 p->up_ = nextToken().cat() == catSuper;
1314 parse(p->cell(1), FLAG_ITEM, mode);
1315 //lyxerr << "read label: " << p->cell(1) << endl;
1318 cell->push_back(MathAtom(p.release()));
1319 //lyxerr << "read cell: " << cell << endl;
1323 else if (t.cs().size()) {
1324 latexkeys const * l = in_word_set(t.cs());
1326 if (l->inset == "big") {
1328 docstring const delim = getToken().asInput();
1329 if (InsetMathBig::isBigInsetDelim(delim))
1330 cell->push_back(MathAtom(
1331 new InsetMathBig(t.cs(), delim)));
1333 cell->push_back(createInsetMath(t.cs()));
1338 else if (l->inset == "font") {
1339 cell->push_back(createInsetMath(t.cs()));
1340 parse(cell->back().nucleus()->cell(0),
1341 FLAG_ITEM, asMode(mode, l->extra));
1344 else if (l->inset == "oldfont") {
1345 cell->push_back(createInsetMath(t.cs()));
1346 parse(cell->back().nucleus()->cell(0),
1347 flags | FLAG_ALIGN, asMode(mode, l->extra));
1348 if (prevToken().cat() != catAlign &&
1349 prevToken().cs() != "\\")
1354 else if (l->inset == "style") {
1355 cell->push_back(createInsetMath(t.cs()));
1356 parse(cell->back().nucleus()->cell(0),
1357 flags | FLAG_ALIGN, mode);
1358 if (prevToken().cat() != catAlign &&
1359 prevToken().cs() != "\\")
1365 MathAtom at = createInsetMath(t.cs());
1366 for (InsetMath::idx_type i = 0; i < at->nargs(); ++i)
1367 parse(at.nucleus()->cell(i),
1368 FLAG_ITEM, asMode(mode, l->extra));
1369 cell->push_back(at);
1374 MathAtom at = createInsetMath(t.cs());
1375 InsetMath::mode_type m = mode;
1376 //if (m == InsetMath::UNDECIDED_MODE)
1377 //lyxerr << "default creation: m1: " << m << endl;
1378 if (at->currentMode() != InsetMath::UNDECIDED_MODE)
1379 m = at->currentMode();
1380 //lyxerr << "default creation: m2: " << m << endl;
1381 InsetMath::idx_type start = 0;
1382 // this fails on \bigg[...\bigg]
1384 //parse(opt, FLAG_OPTION, InsetMath::VERBATIM_MODE);
1387 // at.nucleus()->cell(0) = opt;
1389 for (InsetMath::idx_type i = start; i < at->nargs(); ++i) {
1390 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1393 cell->push_back(at);
1398 if (flags & FLAG_LEAVE) {
1399 flags &= ~FLAG_LEAVE;
1407 } // anonymous namespace
1410 void mathed_parse_cell(MathArray & ar, docstring const & str)
1412 istringstream is(to_utf8(str));
1413 mathed_parse_cell(ar, is);
1417 void mathed_parse_cell(MathArray & ar, istream & is)
1419 Parser(is).parse(ar, 0, InsetMath::MATH_MODE);
1423 bool mathed_parse_normal(MathAtom & t, string const & str)
1425 istringstream is(str);
1426 return Parser(is).parse(t);
1430 bool mathed_parse_normal(MathAtom & t, istream & is)
1432 return Parser(is).parse(t);
1436 bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
1438 return Parser(lex).parse(t);
1442 void mathed_parse_normal(InsetMathGrid & grid, string const & str)
1444 istringstream is(str);
1445 Parser(is).parse1(grid, 0, InsetMath::MATH_MODE, false);
1451 fill(theCatcode, theCatcode + 256, catOther);
1452 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1453 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1455 theCatcode[int('\\')] = catEscape;
1456 theCatcode[int('{')] = catBegin;
1457 theCatcode[int('}')] = catEnd;
1458 theCatcode[int('$')] = catMath;
1459 theCatcode[int('&')] = catAlign;
1460 theCatcode[int('\n')] = catNewline;
1461 theCatcode[int('#')] = catParameter;
1462 theCatcode[int('^')] = catSuper;
1463 theCatcode[int('_')] = catSub;
1464 theCatcode[int(0x7f)] = catIgnore;
1465 theCatcode[int(' ')] = catSpace;
1466 theCatcode[int('\t')] = catSpace;
1467 theCatcode[int('\r')] = catNewline;
1468 theCatcode[int('~')] = catActive;
1469 theCatcode[int('%')] = catComment;