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"
78 using std::istringstream;
88 InsetMath::mode_type asMode(InsetMath::mode_type oldmode, string const & str)
90 //lyxerr << "handling mode: '" << str << "'" << endl;
91 if (str == "mathmode")
92 return InsetMath::MATH_MODE;
93 if (str == "textmode" || str == "forcetext")
94 return InsetMath::TEXT_MODE;
99 bool stared(string const & s)
101 string::size_type const n = s.size();
102 return n && s[n - 1] == '*';
107 * Add the row \p cellrow to \p grid.
108 * \returns wether the row could be added. Adding a row can fail for
109 * environments like "equation" that have a fixed number of rows.
111 bool addRow(InsetMathGrid & grid, InsetMathGrid::row_type & cellrow,
112 string const & vskip)
115 if (cellrow == grid.nrows()) {
116 //lyxerr << "adding row " << cellrow << endl;
117 grid.addRow(cellrow - 1);
118 if (cellrow == grid.nrows()) {
119 // We can't add a row to this grid, so let's
120 // append the content of this cell to the previous
122 // This does not happen in well formed .lyx files,
123 // but LyX versions 1.3.x and older could create
124 // such files and tex2lyx can still do that.
126 lyxerr << "ignoring extra row";
128 lyxerr << " with extra space " << vskip;
129 lyxerr << '.' << endl;
133 grid.vcrskip(LyXLength(vskip), cellrow - 1);
139 * Add the column \p cellcol to \p grid.
140 * \returns wether the column could be added. Adding a column can fail for
141 * environments like "eqnarray" that have a fixed number of columns.
143 bool addCol(InsetMathGrid & grid, InsetMathGrid::col_type & cellcol)
146 if (cellcol == grid.ncols()) {
147 //lyxerr << "adding column " << cellcol << endl;
148 grid.addCol(cellcol - 1);
149 if (cellcol == grid.ncols()) {
150 // We can't add a column to this grid, so let's
151 // append the content of this cell to the previous
153 // This does not happen in well formed .lyx files,
154 // but LyX versions 1.3.x and older could create
155 // such files and tex2lyx can still do that.
157 lyxerr << "ignoring extra column." << endl;
166 * Check wether the last row is empty and remove it if yes.
167 * Otherwise the following code
175 * will result in a grid with 3 rows (+ the dummy row that is always present),
176 * because the last '\\' opens a new row.
178 void delEmptyLastRow(InsetMathGrid & grid)
180 InsetMathGrid::row_type const row = grid.nrows() - 1;
181 for (InsetMathGrid::col_type col = 0; col < grid.ncols(); ++col) {
182 if (!grid.cell(grid.index(row, col)).empty())
185 // Copy the row information of the empty row (which would contain the
186 // last hline in the example above) to the dummy row and delete the
188 grid.rowinfo(row + 1) = grid.rowinfo(row);
193 // These are TeX's catcodes
195 catEscape, // 0 backslash
205 catSpace, // 10 space
206 catLetter, // 11 a-zA-Z
207 catOther, // 12 none of the above
210 catInvalid // 15 <delete>
213 CatCode theCatcode[256];
216 inline CatCode catcode(unsigned char c)
218 return theCatcode[c];
223 FLAG_ALIGN = 1 << 0, // next & or \\ ends the parsing process
224 FLAG_BRACE_LAST = 1 << 1, // next closing brace ends the parsing
225 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
226 FLAG_END = 1 << 3, // next \\end ends the parsing process
227 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
228 FLAG_TEXTMODE = 1 << 5, // we are in a box
229 FLAG_ITEM = 1 << 6, // read a (possibly braced token)
230 FLAG_LEAVE = 1 << 7, // leave the loop at the end
231 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
232 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
233 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
234 FLAG_OPTION = 1 << 11, // read [...] style option
235 FLAG_BRACED = 1 << 12 // read {...} style argument
240 // Helper class for parsing
246 Token() : cs_(), char_(0), cat_(catIgnore) {}
248 Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
250 Token(string const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
253 string const & cs() const { return cs_; }
255 CatCode cat() const { return cat_; }
257 char character() const { return char_; }
259 string asString() const { return cs_.size() ? cs_ : string(1, char_); }
261 string asInput() const { return cs_.size() ? '\\' + cs_ : string(1, char_); }
272 ostream & operator<<(ostream & os, Token const & t)
275 os << '\\' << t.cs();
276 else if (t.cat() == catLetter)
279 os << '[' << t.character() << ',' << t.cat() << ']';
287 typedef InsetMath::mode_type mode_type;
290 Parser(LyXLex & lex);
292 Parser(istream & is);
295 bool parse(MathAtom & at);
297 void parse(MathArray & array, unsigned flags, mode_type mode);
299 void parse1(InsetMathGrid & grid, unsigned flags, mode_type mode,
302 MathArray parse(unsigned flags, mode_type mode);
304 int lineno() const { return lineno_; }
310 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
311 /// get arg delimited by 'left' and 'right'
312 string getArg(char left, char right);
316 void error(string const & msg);
317 /// dump contents to screen
320 void tokenize(istream & is);
322 void tokenize(string const & s);
324 void skipSpaceTokens(istream & is, char c);
326 void push_back(Token const & t);
330 Token const & prevToken() const;
332 Token const & nextToken() const;
334 Token const & getToken();
335 /// skips spaces if any
338 void lex(string const & s);
342 string parse_verbatim_item();
344 string parse_verbatim_option();
349 vector<Token> tokens_;
352 /// Stack of active environments
353 vector<string> environments_;
357 Parser::Parser(LyXLex & lexer)
358 : lineno_(lexer.getLineNo()), pos_(0)
360 tokenize(lexer.getStream());
365 Parser::Parser(istream & is)
366 : lineno_(0), pos_(0)
372 void Parser::push_back(Token const & t)
374 tokens_.push_back(t);
378 void Parser::pop_back()
384 Token const & Parser::prevToken() const
386 static const Token dummy;
387 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
391 Token const & Parser::nextToken() const
393 static const Token dummy;
394 return good() ? tokens_[pos_] : dummy;
398 Token const & Parser::getToken()
400 static const Token dummy;
401 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
402 return good() ? tokens_[pos_++] : dummy;
406 void Parser::skipSpaces()
408 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
413 void Parser::putback()
419 bool Parser::good() const
421 return pos_ < tokens_.size();
425 char Parser::getChar()
428 error("The input stream is not well...");
429 return tokens_[pos_++].character();
433 string Parser::getArg(char left, char right)
443 while ((c = getChar()) != right && good())
450 void Parser::skipSpaceTokens(istream & is, char c)
452 // skip trailing spaces
453 while (catcode(c) == catSpace || catcode(c) == catNewline)
456 //lyxerr << "putting back: " << c << endl;
461 void Parser::tokenize(istream & is)
463 // eat everything up to the next \end_inset or end of stream
464 // and store it in s for further tokenization
469 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
470 s = s.substr(0, s.size() - 10);
474 // Remove the space after \end_inset
475 if (is.get(c) && c != ' ')
483 void Parser::tokenize(string const & buffer)
485 istringstream is(buffer, ios::in | ios::binary);
489 //lyxerr << "reading c: " << c << endl;
491 switch (catcode(c)) {
495 if (catcode(c) == catNewline)
496 ; //push_back(Token("par"));
498 push_back(Token('\n', catNewline));
506 while (is.get(c) && catcode(c) != catNewline)
516 error("unexpected end of input");
519 if (catcode(c) == catLetter) {
521 while (is.get(c) && catcode(c) == catLetter)
523 skipSpaceTokens(is, c);
532 push_back(Token(c, catcode(c)));
534 skipSpaceTokens(is, c);
539 lyxerr << "ignoring a char: " << int(c) << endl;
544 push_back(Token(c, catcode(c)));
554 void Parser::dump() const
556 lyxerr << "\nTokens: ";
557 for (unsigned i = 0; i < tokens_.size(); ++i) {
560 lyxerr << tokens_[i];
562 lyxerr << " pos: " << pos_ << endl;
566 void Parser::error(string const & msg)
568 lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
574 bool Parser::parse(MathAtom & at)
578 parse(ar, false, InsetMath::UNDECIDED_MODE);
579 if (ar.size() != 1 || ar.front()->getType() == hullNone) {
580 lyxerr << "unusual contents found: " << ar << endl;
581 at = MathAtom(new InsetMathPar(ar));
582 //if (at->nargs() > 0)
583 // at.nucleus()->cell(0) = ar;
585 // lyxerr << "unusual contents found: " << ar << endl;
593 string Parser::parse_verbatim_option()
597 if (nextToken().character() == '[') {
598 Token t = getToken();
599 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
600 if (t.cat() == catBegin) {
602 res += '{' + parse_verbatim_item() + '}';
611 string Parser::parse_verbatim_item()
615 if (nextToken().cat() == catBegin) {
616 Token t = getToken();
617 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
618 if (t.cat() == catBegin) {
620 res += '{' + parse_verbatim_item() + '}';
630 MathArray Parser::parse(unsigned flags, mode_type mode)
633 parse(ar, flags, mode);
638 void Parser::parse(MathArray & array, unsigned flags, mode_type mode)
640 InsetMathGrid grid(1, 1);
641 parse1(grid, flags, mode, false);
642 array = grid.cell(0);
646 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
649 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
653 void Parser::parse1(InsetMathGrid & grid, unsigned flags,
654 const mode_type mode, const bool numbered)
657 InsetMathGrid::row_type cellrow = 0;
658 InsetMathGrid::col_type cellcol = 0;
659 MathArray * cell = &grid.cell(grid.index(cellrow, cellcol));
661 if (grid.asHullInset())
662 grid.asHullInset()->numbered(cellrow, numbered);
665 //lyxerr << " flags: " << flags << endl;
666 //lyxerr << " mode: " << mode << endl;
667 //lyxerr << "grid: " << grid << endl;
670 Token const & t = getToken();
673 lyxerr << "t: " << t << " flags: " << flags << endl;
674 lyxerr << "mode: " << mode << endl;
679 if (flags & FLAG_ITEM) {
681 if (t.cat() == catBegin) {
682 // skip the brace and collect everything to the next matching
684 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
688 // handle only this single token, leave the loop if done
693 if (flags & FLAG_BRACED) {
694 if (t.cat() == catSpace)
697 if (t.cat() != catBegin) {
698 error("opening brace expected");
702 // skip the brace and collect everything to the next matching
704 flags = FLAG_BRACE_LAST;
708 if (flags & FLAG_OPTION) {
709 if (t.cat() == catOther && t.character() == '[') {
711 parse(ar, FLAG_BRACK_LAST, mode);
714 // no option found, put back token and we are done
723 if (t.cat() == catMath) {
724 if (mode != InsetMath::MATH_MODE) {
725 // we are inside some text mode thingy, so opening new math is allowed
726 Token const & n = getToken();
727 if (n.cat() == catMath) {
728 // TeX's $$...$$ syntax for displayed math
729 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
730 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
731 getToken(); // skip the second '$' token
733 // simple $...$ stuff
735 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
736 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
740 else if (flags & FLAG_SIMPLE) {
741 // this is the end of the formula
746 error("something strange in the parser");
751 else if (t.cat() == catLetter)
752 cell->push_back(MathAtom(new InsetMathChar(t.character())));
754 else if (t.cat() == catSpace && mode != InsetMath::MATH_MODE) {
755 if (cell->empty() || cell->back()->getChar() != ' ')
756 cell->push_back(MathAtom(new InsetMathChar(t.character())));
759 else if (t.cat() == catNewline && mode != InsetMath::MATH_MODE) {
760 if (cell->empty() || cell->back()->getChar() != ' ')
761 cell->push_back(MathAtom(new InsetMathChar(' ')));
764 else if (t.cat() == catParameter) {
765 Token const & n = getToken();
766 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
769 else if (t.cat() == catActive)
770 cell->push_back(MathAtom(new InsetMathChar(t.character())));
772 else if (t.cat() == catBegin) {
774 parse(ar, FLAG_BRACE_LAST, mode);
775 // do not create a BraceInset if they were written by LyX
776 // this helps to keep the annoyance of "a choose b" to a minimum
777 if (ar.size() == 1 && ar[0]->extraBraces())
780 cell->push_back(MathAtom(new InsetMathBrace(ar)));
783 else if (t.cat() == catEnd) {
784 if (flags & FLAG_BRACE_LAST)
786 error("found '}' unexpectedly");
787 //BOOST_ASSERT(false);
788 //add(cell, '}', LM_TC_TEX);
791 else if (t.cat() == catAlign) {
792 //lyxerr << " column now " << (cellcol + 1)
793 // << " max: " << grid.ncols() << endl;
794 if (flags & FLAG_ALIGN)
796 if (addCol(grid, cellcol))
797 cell = &grid.cell(grid.index(cellrow, cellcol));
800 else if (t.cat() == catSuper || t.cat() == catSub) {
801 bool up = (t.cat() == catSuper);
802 // we need no new script inset if the last thing was a scriptinset,
803 // which has that script already not the same script already
805 cell->push_back(MathAtom(new InsetMathScript(up)));
806 else if (cell->back()->asScriptInset() &&
807 !cell->back()->asScriptInset()->has(up))
808 cell->back().nucleus()->asScriptInset()->ensure(up);
809 else if (cell->back()->asScriptInset())
810 cell->push_back(MathAtom(new InsetMathScript(up)));
812 cell->back() = MathAtom(new InsetMathScript(cell->back(), up));
813 InsetMathScript * p = cell->back().nucleus()->asScriptInset();
814 // special handling of {}-bases
815 // is this always correct?
816 if (p->nuc().size() == 1
817 && p->nuc().back()->asBraceInset())
818 p->nuc() = p->nuc().back()->asNestInset()->cell(0);
819 parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
826 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
827 //lyxerr << "finished reading option" << endl;
831 else if (t.cat() == catOther)
832 cell->push_back(MathAtom(new InsetMathChar(t.character())));
834 else if (t.cat() == catComment) {
837 Token const & t = getToken();
838 if (t.cat() == catNewline)
842 cell->push_back(MathAtom(new InsetMathComment(s)));
850 else if (t.cs() == "lyxlock") {
852 cell->back().nucleus()->lock(true);
855 else if (t.cs() == "def" ||
856 t.cs() == "newcommand" ||
857 t.cs() == "renewcommand")
859 string const type = t.cs();
862 if (t.cs() == "def") {
864 name = getToken().cs();
868 while (good() && nextToken().cat() != catBegin) {
869 pars += getToken().cs();
873 //lyxerr << "read \\def parameter list '" << pars << "'" << endl;
875 } else { // t.cs() == "newcommand" || t.cs() == "renewcommand"
877 if (getToken().cat() != catBegin) {
878 error("'{' in \\newcommand expected (1) ");
882 name = getToken().cs();
884 if (getToken().cat() != catEnd) {
885 error("'}' in \\newcommand expected");
889 string const arg = getArg('[', ']');
891 nargs = convert<int>(arg);
896 parse(ar1, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
898 // we cannot handle recursive stuff at all
900 //test.push_back(createInsetMath(name));
901 //if (ar1.contains(test)) {
902 // error("we cannot handle recursive macros at all.");
906 // is a version for display attached?
909 if (nextToken().cat() == catBegin)
910 parse(ar2, FLAG_ITEM, InsetMath::MATH_MODE);
912 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, type,
916 else if (t.cs() == "(") {
917 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
918 parse2(cell->back(), FLAG_SIMPLE2, InsetMath::MATH_MODE, false);
921 else if (t.cs() == "[") {
922 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
923 parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false);
926 else if (t.cs() == "protect")
927 // ignore \\protect, will hopefully be re-added during output
930 else if (t.cs() == "end") {
931 if (flags & FLAG_END) {
932 // eat environment name
933 string const name = getArg('{', '}');
934 if (environments_.empty())
935 error("'found \\end{" + name +
936 "}' without matching '\\begin{" +
938 else if (name != environments_.back())
939 error("'\\end{" + name +
940 "}' does not match '\\begin{" +
941 environments_.back() + "}'");
943 environments_.pop_back();
944 // Delete empty last row in matrix
946 // If you abuse InsetMathGrid for
947 // non-matrix like structures you
948 // probably need to refine this test.
949 // Right now we only have to test for
950 // single line hull insets.
951 if (grid.nrows() > 1)
952 delEmptyLastRow(grid);
956 error("found 'end' unexpectedly");
959 else if (t.cs() == ")") {
960 if (flags & FLAG_SIMPLE2)
962 error("found '\\)' unexpectedly");
965 else if (t.cs() == "]") {
966 if (flags & FLAG_EQUATION)
968 error("found '\\]' unexpectedly");
971 else if (t.cs() == "\\") {
972 if (flags & FLAG_ALIGN)
974 if (addRow(grid, cellrow, getArg('[', ']'))) {
976 if (grid.asHullInset())
977 grid.asHullInset()->numbered(
979 cell = &grid.cell(grid.index(cellrow,
985 else if (t.cs() == "multicolumn") {
986 // extract column count and insert dummy cells
988 parse(count, FLAG_ITEM, mode);
990 if (!extractNumber(count, cols)) {
991 lyxerr << " can't extract number of cells from " << count << endl;
993 // resize the table if necessary
994 for (int i = 0; i < cols; ++i) {
995 if (addCol(grid, cellcol)) {
996 cell = &grid.cell(grid.index(
998 // mark this as dummy
999 grid.cellinfo(grid.index(
1000 cellrow, cellcol)).dummy_ = true;
1003 // the last cell is the real thing, not a dummy
1004 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
1006 // read special alignment
1008 parse(align, FLAG_ITEM, mode);
1009 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
1011 // parse the remaining contents into the "real" cell
1012 parse(*cell, FLAG_ITEM, mode);
1016 else if (t.cs() == "limits")
1019 else if (t.cs() == "nolimits")
1022 else if (t.cs() == "nonumber") {
1023 if (grid.asHullInset())
1024 grid.asHullInset()->numbered(cellrow, false);
1027 else if (t.cs() == "number") {
1028 if (grid.asHullInset())
1029 grid.asHullInset()->numbered(cellrow, true);
1032 else if (t.cs() == "hline") {
1033 grid.rowinfo(cellrow).lines_ ++;
1036 else if (t.cs() == "sqrt") {
1038 parse(ar, FLAG_OPTION, mode);
1040 cell->push_back(MathAtom(new InsetMathRoot));
1041 cell->back().nucleus()->cell(0) = ar;
1042 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1044 cell->push_back(MathAtom(new InsetMathSqrt));
1045 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1049 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1050 cell->push_back(createInsetMath(t.cs()));
1051 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1052 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1055 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
1056 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1057 cell->push_back(MathAtom(new RefInset(t.cs())));
1058 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1059 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1062 else if (t.cs() == "left") {
1064 Token const & tl = getToken();
1065 // \| and \Vert are equivalent, and InsetMathDelim
1067 // FIXME: fix this in InsetMathDelim itself!
1068 string const l = tl.cs() == "|" ? "Vert" : tl.asString();
1070 parse(ar, FLAG_RIGHT, mode);
1072 Token const & tr = getToken();
1073 string const r = tr.cs() == "|" ? "Vert" : tr.asString();
1074 cell->push_back(MathAtom(new InsetMathDelim(l, r, ar)));
1077 else if (t.cs() == "right") {
1078 if (flags & FLAG_RIGHT)
1080 //lyxerr << "got so far: '" << cell << "'" << endl;
1081 error("Unmatched right delimiter");
1085 else if (t.cs() == "begin") {
1086 string const name = getArg('{', '}');
1087 environments_.push_back(name);
1089 if (name == "array" || name == "subarray") {
1090 string const valign = parse_verbatim_option() + 'c';
1091 string const halign = parse_verbatim_item();
1092 cell->push_back(MathAtom(new InsetMathArray(name, valign[0], halign)));
1093 parse2(cell->back(), FLAG_END, mode, false);
1096 else if (name == "tabular") {
1097 string const valign = parse_verbatim_option() + 'c';
1098 string const halign = parse_verbatim_item();
1099 cell->push_back(MathAtom(new InsetMathTabular(name, valign[0], halign)));
1100 parse2(cell->back(), FLAG_END, InsetMath::TEXT_MODE, false);
1103 else if (name == "split" || name == "cases") {
1104 cell->push_back(createInsetMath(name));
1105 parse2(cell->back(), FLAG_END, mode, false);
1108 else if (name == "alignedat") {
1109 string const valign = parse_verbatim_option() + 'c';
1110 // ignore this for a while
1112 cell->push_back(MathAtom(new InsetMathSplit(name, valign[0])));
1113 parse2(cell->back(), FLAG_END, mode, false);
1116 else if (name == "math") {
1117 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
1118 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, true);
1121 else if (name == "equation" || name == "equation*"
1122 || name == "displaymath") {
1123 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
1124 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation"));
1127 else if (name == "eqnarray" || name == "eqnarray*") {
1128 cell->push_back(MathAtom(new InsetMathHull(hullEqnArray)));
1129 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1132 else if (name == "align" || name == "align*") {
1133 cell->push_back(MathAtom(new InsetMathHull(hullAlign)));
1134 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1137 else if (name == "flalign" || name == "flalign*") {
1138 cell->push_back(MathAtom(new InsetMathHull(hullFlAlign)));
1139 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1142 else if (name == "alignat" || name == "alignat*") {
1143 // ignore this for a while
1145 cell->push_back(MathAtom(new InsetMathHull(hullAlignAt)));
1146 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1149 else if (name == "xalignat" || name == "xalignat*") {
1150 // ignore this for a while
1152 cell->push_back(MathAtom(new InsetMathHull(hullXAlignAt)));
1153 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1156 else if (name == "xxalignat") {
1157 // ignore this for a while
1159 cell->push_back(MathAtom(new InsetMathHull(hullXXAlignAt)));
1160 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1163 else if (name == "multline" || name == "multline*") {
1164 cell->push_back(MathAtom(new InsetMathHull(hullMultline)));
1165 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1168 else if (name == "gather" || name == "gather*") {
1169 cell->push_back(MathAtom(new InsetMathHull(hullGather)));
1170 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1173 else if (latexkeys const * l = in_word_set(name)) {
1174 if (l->inset == "matrix") {
1175 cell->push_back(createInsetMath(name));
1176 parse2(cell->back(), FLAG_END, mode, false);
1177 } else if (l->inset == "split") {
1178 string const valign = parse_verbatim_option() + 'c';
1179 cell->push_back(MathAtom(new InsetMathSplit(name, valign[0])));
1180 parse2(cell->back(), FLAG_END, mode, false);
1183 lyxerr << "found math environment `" << name
1184 << "' in symbols file with unsupported inset `"
1185 << l->inset << "'." << endl;
1186 // create generic environment inset
1187 cell->push_back(MathAtom(new InsetMathEnv(name)));
1188 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1194 lyxerr << "found unknown math environment '" << name << "'" << endl;
1195 // create generic environment inset
1196 cell->push_back(MathAtom(new InsetMathEnv(name)));
1197 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1201 else if (t.cs() == "kern") {
1202 #ifdef WITH_WARNINGS
1207 Token const & t = getToken();
1213 if (isValidLength(s))
1216 cell->push_back(MathAtom(new InsetMathKern(s)));
1219 else if (t.cs() == "label") {
1220 // FIXME: This is swallowed in inline formulas
1221 string label = parse_verbatim_item();
1224 if (grid.asHullInset()) {
1225 grid.asHullInset()->label(cellrow, label);
1227 cell->push_back(createInsetMath(t.cs()));
1228 cell->push_back(MathAtom(new InsetMathBrace(ar)));
1232 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1233 MathAtom at = createInsetMath(t.cs());
1234 at.nucleus()->cell(0) = *cell;
1236 parse(at.nucleus()->cell(1), flags, mode);
1237 cell->push_back(at);
1241 else if (t.cs() == "color") {
1242 string const color = parse_verbatim_item();
1243 cell->push_back(MathAtom(new InsetMathColor(true, color)));
1244 parse(cell->back().nucleus()->cell(0), flags, mode);
1248 else if (t.cs() == "textcolor") {
1249 string const color = parse_verbatim_item();
1250 cell->push_back(MathAtom(new InsetMathColor(false, color)));
1251 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1254 else if (t.cs() == "normalcolor") {
1255 cell->push_back(createInsetMath(t.cs()));
1256 parse(cell->back().nucleus()->cell(0), flags, mode);
1260 else if (t.cs() == "substack") {
1261 cell->push_back(createInsetMath(t.cs()));
1262 parse2(cell->back(), FLAG_ITEM, mode, false);
1265 else if (t.cs() == "xymatrix") {
1266 cell->push_back(createInsetMath(t.cs()));
1267 parse2(cell->back(), FLAG_ITEM, mode, false);
1270 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1271 cell->push_back(createInsetMath(t.cs()));
1272 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, InsetMath::TEXT_MODE);
1273 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, InsetMath::TEXT_MODE);
1274 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, InsetMath::TEXT_MODE);
1277 else if (t.cs() == "tag") {
1278 if (nextToken().character() == '*') {
1280 cell->push_back(createInsetMath(t.cs() + '*'));
1282 cell->push_back(createInsetMath(t.cs()));
1283 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1287 else if (t.cs() == "infer") {
1289 parse(ar, FLAG_OPTION, mode);
1290 cell->push_back(createInsetMath(t.cs()));
1291 parse2(cell->back(), FLAG_ITEM, mode, false);
1295 else if (1 && t.cs() == "ar") {
1296 auto_ptr<InsetMathXYArrow> p(new InsetMathXYArrow);
1297 // try to read target
1298 parse(p->cell(0), FLAG_OTPTION, mode);
1299 // try to read label
1300 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1301 p->up_ = nextToken().cat() == catSuper;
1303 parse(p->cell(1), FLAG_ITEM, mode);
1304 //lyxerr << "read label: " << p->cell(1) << endl;
1307 cell->push_back(MathAtom(p.release()));
1308 //lyxerr << "read cell: " << cell << endl;
1312 else if (t.cs().size()) {
1313 latexkeys const * l = in_word_set(t.cs());
1315 if (l->inset == "big") {
1317 string const delim = getToken().asInput();
1318 if (InsetMathBig::isBigInsetDelim(delim))
1319 cell->push_back(MathAtom(
1320 new InsetMathBig(t.cs(), delim)));
1322 cell->push_back(createInsetMath(t.cs()));
1323 cell->push_back(createInsetMath(
1328 else if (l->inset == "font") {
1329 cell->push_back(createInsetMath(t.cs()));
1330 parse(cell->back().nucleus()->cell(0),
1331 FLAG_ITEM, asMode(mode, l->extra));
1334 else if (l->inset == "oldfont") {
1335 cell->push_back(createInsetMath(t.cs()));
1336 parse(cell->back().nucleus()->cell(0),
1337 flags | FLAG_ALIGN, asMode(mode, l->extra));
1338 if (prevToken().cat() != catAlign &&
1339 prevToken().cs() != "\\")
1344 else if (l->inset == "style") {
1345 cell->push_back(createInsetMath(t.cs()));
1346 parse(cell->back().nucleus()->cell(0),
1347 flags | FLAG_ALIGN, mode);
1348 if (prevToken().cat() != catAlign &&
1349 prevToken().cs() != "\\")
1355 MathAtom at = createInsetMath(t.cs());
1356 for (InsetMath::idx_type i = 0; i < at->nargs(); ++i)
1357 parse(at.nucleus()->cell(i),
1358 FLAG_ITEM, asMode(mode, l->extra));
1359 cell->push_back(at);
1364 MathAtom at = createInsetMath(t.cs());
1365 InsetMath::mode_type m = mode;
1366 //if (m == InsetMath::UNDECIDED_MODE)
1367 //lyxerr << "default creation: m1: " << m << endl;
1368 if (at->currentMode() != InsetMath::UNDECIDED_MODE)
1369 m = at->currentMode();
1370 //lyxerr << "default creation: m2: " << m << endl;
1371 InsetMath::idx_type start = 0;
1372 // this fails on \bigg[...\bigg]
1374 //parse(opt, FLAG_OPTION, InsetMath::VERBATIM_MODE);
1377 // at.nucleus()->cell(0) = opt;
1379 for (InsetMath::idx_type i = start; i < at->nargs(); ++i) {
1380 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1383 cell->push_back(at);
1388 if (flags & FLAG_LEAVE) {
1389 flags &= ~FLAG_LEAVE;
1397 } // anonymous namespace
1400 void mathed_parse_cell(MathArray & ar, string const & str)
1402 istringstream is(str);
1403 mathed_parse_cell(ar, is);
1407 void mathed_parse_cell(MathArray & ar, istream & is)
1409 Parser(is).parse(ar, 0, InsetMath::MATH_MODE);
1413 bool mathed_parse_normal(MathAtom & t, string const & str)
1415 istringstream is(str);
1416 return Parser(is).parse(t);
1420 bool mathed_parse_normal(MathAtom & t, istream & is)
1422 return Parser(is).parse(t);
1426 bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
1428 return Parser(lex).parse(t);
1432 void mathed_parse_normal(InsetMathGrid & grid, string const & str)
1434 istringstream is(str);
1435 Parser(is).parse1(grid, 0, InsetMath::MATH_MODE, false);
1441 fill(theCatcode, theCatcode + 256, catOther);
1442 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1443 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1445 theCatcode[int('\\')] = catEscape;
1446 theCatcode[int('{')] = catBegin;
1447 theCatcode[int('}')] = catEnd;
1448 theCatcode[int('$')] = catMath;
1449 theCatcode[int('&')] = catAlign;
1450 theCatcode[int('\n')] = catNewline;
1451 theCatcode[int('#')] = catParameter;
1452 theCatcode[int('^')] = catSuper;
1453 theCatcode[int('_')] = catSub;
1454 theCatcode[int(0x7f)] = catIgnore;
1455 theCatcode[int(' ')] = catSpace;
1456 theCatcode[int('\t')] = catSpace;
1457 theCatcode[int('\r')] = catNewline;
1458 theCatcode[int('~')] = catActive;
1459 theCatcode[int('%')] = catComment;