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 "InsetMathSqrt.h"
58 #include "InsetMathTabular.h"
59 #include "MathMacroTemplate.h"
60 #include "MathFactory.h"
61 #include "MathMacroArgument.h"
62 #include "MathSupport.h"
67 #include "support/convert.h"
77 using std::istringstream;
87 InsetMath::mode_type asMode(InsetMath::mode_type oldmode, string const & str)
89 //lyxerr << "handling mode: '" << str << "'" << endl;
90 if (str == "mathmode")
91 return InsetMath::MATH_MODE;
92 if (str == "textmode" || str == "forcetext")
93 return InsetMath::TEXT_MODE;
98 bool stared(string const & s)
100 string::size_type const n = s.size();
101 return n && s[n - 1] == '*';
106 * Add the row \p cellrow to \p grid.
107 * \returns wether the row could be added. Adding a row can fail for
108 * environments like "equation" that have a fixed number of rows.
110 bool addRow(InsetMathGrid & grid, InsetMathGrid::row_type & cellrow,
111 string const & vskip)
114 if (cellrow == grid.nrows()) {
115 //lyxerr << "adding row " << cellrow << endl;
116 grid.addRow(cellrow - 1);
117 if (cellrow == grid.nrows()) {
118 // We can't add a row to this grid, so let's
119 // append the content of this cell to the previous
121 // This does not happen in well formed .lyx files,
122 // but LyX versions 1.3.x and older could create
123 // such files and tex2lyx can still do that.
125 lyxerr << "ignoring extra row";
127 lyxerr << " with extra space " << vskip;
128 lyxerr << '.' << endl;
132 grid.vcrskip(LyXLength(vskip), cellrow - 1);
138 * Add the column \p cellcol to \p grid.
139 * \returns wether the column could be added. Adding a column can fail for
140 * environments like "eqnarray" that have a fixed number of columns.
142 bool addCol(InsetMathGrid & grid, InsetMathGrid::col_type & cellcol)
145 if (cellcol == grid.ncols()) {
146 //lyxerr << "adding column " << cellcol << endl;
147 grid.addCol(cellcol - 1);
148 if (cellcol == grid.ncols()) {
149 // We can't add a column to this grid, so let's
150 // append the content of this cell to the previous
152 // This does not happen in well formed .lyx files,
153 // but LyX versions 1.3.x and older could create
154 // such files and tex2lyx can still do that.
156 lyxerr << "ignoring extra column." << endl;
165 * Check wether the last row is empty and remove it if yes.
166 * Otherwise the following code
174 * will result in a grid with 3 rows (+ the dummy row that is always present),
175 * because the last '\\' opens a new row.
177 void delEmptyLastRow(InsetMathGrid & grid)
179 InsetMathGrid::row_type const row = grid.nrows() - 1;
180 for (InsetMathGrid::col_type col = 0; col < grid.ncols(); ++col) {
181 if (!grid.cell(grid.index(row, col)).empty())
184 // Copy the row information of the empty row (which would contain the
185 // last hline in the example above) to the dummy row and delete the
187 grid.rowinfo(row + 1) = grid.rowinfo(row);
192 // These are TeX's catcodes
194 catEscape, // 0 backslash
204 catSpace, // 10 space
205 catLetter, // 11 a-zA-Z
206 catOther, // 12 none of the above
209 catInvalid // 15 <delete>
212 CatCode theCatcode[256];
215 inline CatCode catcode(unsigned char c)
217 return theCatcode[c];
222 FLAG_ALIGN = 1 << 0, // next & or \\ ends the parsing process
223 FLAG_BRACE_LAST = 1 << 1, // next closing brace ends the parsing
224 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
225 FLAG_END = 1 << 3, // next \\end ends the parsing process
226 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
227 FLAG_TEXTMODE = 1 << 5, // we are in a box
228 FLAG_ITEM = 1 << 6, // read a (possibly braced token)
229 FLAG_LEAVE = 1 << 7, // leave the loop at the end
230 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
231 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
232 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
233 FLAG_OPTION = 1 << 11, // read [...] style option
234 FLAG_BRACED = 1 << 12 // read {...} style argument
239 // Helper class for parsing
245 Token() : cs_(), char_(0), cat_(catIgnore) {}
247 Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
249 Token(string const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
252 string const & cs() const { return cs_; }
254 CatCode cat() const { return cat_; }
256 char character() const { return char_; }
258 string asString() const { return cs_.size() ? cs_ : string(1, char_); }
260 string asInput() const { return cs_.size() ? '\\' + cs_ : string(1, char_); }
271 ostream & operator<<(ostream & os, Token const & t)
274 os << '\\' << t.cs();
275 else if (t.cat() == catLetter)
278 os << '[' << t.character() << ',' << t.cat() << ']';
286 typedef InsetMath::mode_type mode_type;
289 Parser(LyXLex & lex);
291 Parser(istream & is);
294 bool parse(MathAtom & at);
296 void parse(MathArray & array, unsigned flags, mode_type mode);
298 void parse1(InsetMathGrid & grid, unsigned flags, mode_type mode,
301 MathArray parse(unsigned flags, mode_type mode);
303 int lineno() const { return lineno_; }
309 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
310 /// get arg delimited by 'left' and 'right'
311 string getArg(char left, char right);
315 void error(string const & msg);
316 /// dump contents to screen
319 void tokenize(istream & is);
321 void tokenize(string const & s);
323 void skipSpaceTokens(istream & is, char c);
325 void push_back(Token const & t);
329 Token const & prevToken() const;
331 Token const & nextToken() const;
333 Token const & getToken();
334 /// skips spaces if any
337 void lex(string const & s);
341 string parse_verbatim_item();
343 string parse_verbatim_option();
348 vector<Token> tokens_;
351 /// Stack of active environments
352 vector<string> environments_;
356 Parser::Parser(LyXLex & lexer)
357 : lineno_(lexer.getLineNo()), pos_(0)
359 tokenize(lexer.getStream());
364 Parser::Parser(istream & is)
365 : lineno_(0), pos_(0)
371 void Parser::push_back(Token const & t)
373 tokens_.push_back(t);
377 void Parser::pop_back()
383 Token const & Parser::prevToken() const
385 static const Token dummy;
386 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
390 Token const & Parser::nextToken() const
392 static const Token dummy;
393 return good() ? tokens_[pos_] : dummy;
397 Token const & Parser::getToken()
399 static const Token dummy;
400 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
401 return good() ? tokens_[pos_++] : dummy;
405 void Parser::skipSpaces()
407 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
412 void Parser::putback()
418 bool Parser::good() const
420 return pos_ < tokens_.size();
424 char Parser::getChar()
427 error("The input stream is not well...");
428 return tokens_[pos_++].character();
432 string Parser::getArg(char left, char right)
442 while ((c = getChar()) != right && good())
449 void Parser::skipSpaceTokens(istream & is, char c)
451 // skip trailing spaces
452 while (catcode(c) == catSpace || catcode(c) == catNewline)
455 //lyxerr << "putting back: " << c << endl;
460 void Parser::tokenize(istream & is)
462 // eat everything up to the next \end_inset or end of stream
463 // and store it in s for further tokenization
468 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
469 s = s.substr(0, s.size() - 10);
473 // Remove the space after \end_inset
474 if (is.get(c) && c != ' ')
482 void Parser::tokenize(string const & buffer)
484 istringstream is(buffer, ios::in | ios::binary);
488 //lyxerr << "reading c: " << c << endl;
490 switch (catcode(c)) {
494 if (catcode(c) == catNewline)
495 ; //push_back(Token("par"));
497 push_back(Token('\n', catNewline));
505 while (is.get(c) && catcode(c) != catNewline)
515 error("unexpected end of input");
518 if (catcode(c) == catLetter) {
520 while (is.get(c) && catcode(c) == catLetter)
522 skipSpaceTokens(is, c);
531 push_back(Token(c, catcode(c)));
533 skipSpaceTokens(is, c);
538 lyxerr << "ignoring a char: " << int(c) << endl;
543 push_back(Token(c, catcode(c)));
553 void Parser::dump() const
555 lyxerr << "\nTokens: ";
556 for (unsigned i = 0; i < tokens_.size(); ++i) {
559 lyxerr << tokens_[i];
561 lyxerr << " pos: " << pos_ << endl;
565 void Parser::error(string const & msg)
567 lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
573 bool Parser::parse(MathAtom & at)
577 parse(ar, false, InsetMath::UNDECIDED_MODE);
578 if (ar.size() != 1 || ar.front()->getType() == hullNone) {
579 lyxerr << "unusual contents found: " << ar << endl;
580 at = MathAtom(new InsetMathPar(ar));
581 //if (at->nargs() > 0)
582 // at.nucleus()->cell(0) = ar;
584 // lyxerr << "unusual contents found: " << ar << endl;
592 string Parser::parse_verbatim_option()
596 if (nextToken().character() == '[') {
597 Token t = getToken();
598 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
599 if (t.cat() == catBegin) {
601 res += '{' + parse_verbatim_item() + '}';
610 string Parser::parse_verbatim_item()
614 if (nextToken().cat() == catBegin) {
615 Token t = getToken();
616 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
617 if (t.cat() == catBegin) {
619 res += '{' + parse_verbatim_item() + '}';
629 MathArray Parser::parse(unsigned flags, mode_type mode)
632 parse(ar, flags, mode);
637 void Parser::parse(MathArray & array, unsigned flags, mode_type mode)
639 InsetMathGrid grid(1, 1);
640 parse1(grid, flags, mode, false);
641 array = grid.cell(0);
645 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
648 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
652 void Parser::parse1(InsetMathGrid & grid, unsigned flags,
653 const mode_type mode, const bool numbered)
656 InsetMathGrid::row_type cellrow = 0;
657 InsetMathGrid::col_type cellcol = 0;
658 MathArray * cell = &grid.cell(grid.index(cellrow, cellcol));
660 if (grid.asHullInset())
661 grid.asHullInset()->numbered(cellrow, numbered);
664 //lyxerr << " flags: " << flags << endl;
665 //lyxerr << " mode: " << mode << endl;
666 //lyxerr << "grid: " << grid << endl;
669 Token const & t = getToken();
672 lyxerr << "t: " << t << " flags: " << flags << endl;
673 lyxerr << "mode: " << mode << endl;
678 if (flags & FLAG_ITEM) {
680 if (t.cat() == catBegin) {
681 // skip the brace and collect everything to the next matching
683 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
687 // handle only this single token, leave the loop if done
692 if (flags & FLAG_BRACED) {
693 if (t.cat() == catSpace)
696 if (t.cat() != catBegin) {
697 error("opening brace expected");
701 // skip the brace and collect everything to the next matching
703 flags = FLAG_BRACE_LAST;
707 if (flags & FLAG_OPTION) {
708 if (t.cat() == catOther && t.character() == '[') {
710 parse(ar, FLAG_BRACK_LAST, mode);
713 // no option found, put back token and we are done
722 if (t.cat() == catMath) {
723 if (mode != InsetMath::MATH_MODE) {
724 // we are inside some text mode thingy, so opening new math is allowed
725 Token const & n = getToken();
726 if (n.cat() == catMath) {
727 // TeX's $$...$$ syntax for displayed math
728 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
729 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
730 getToken(); // skip the second '$' token
732 // simple $...$ stuff
734 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
735 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
739 else if (flags & FLAG_SIMPLE) {
740 // this is the end of the formula
745 error("something strange in the parser");
750 else if (t.cat() == catLetter)
751 cell->push_back(MathAtom(new InsetMathChar(t.character())));
753 else if (t.cat() == catSpace && mode != InsetMath::MATH_MODE) {
754 if (cell->empty() || cell->back()->getChar() != ' ')
755 cell->push_back(MathAtom(new InsetMathChar(t.character())));
758 else if (t.cat() == catNewline && mode != InsetMath::MATH_MODE) {
759 if (cell->empty() || cell->back()->getChar() != ' ')
760 cell->push_back(MathAtom(new InsetMathChar(' ')));
763 else if (t.cat() == catParameter) {
764 Token const & n = getToken();
765 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
768 else if (t.cat() == catActive)
769 cell->push_back(MathAtom(new InsetMathChar(t.character())));
771 else if (t.cat() == catBegin) {
773 parse(ar, FLAG_BRACE_LAST, mode);
774 // do not create a BraceInset if they were written by LyX
775 // this helps to keep the annoyance of "a choose b" to a minimum
776 if (ar.size() == 1 && ar[0]->extraBraces())
779 cell->push_back(MathAtom(new InsetMathBrace(ar)));
782 else if (t.cat() == catEnd) {
783 if (flags & FLAG_BRACE_LAST)
785 error("found '}' unexpectedly");
786 //BOOST_ASSERT(false);
787 //add(cell, '}', LM_TC_TEX);
790 else if (t.cat() == catAlign) {
791 //lyxerr << " column now " << (cellcol + 1)
792 // << " max: " << grid.ncols() << endl;
793 if (flags & FLAG_ALIGN)
795 if (addCol(grid, cellcol))
796 cell = &grid.cell(grid.index(cellrow, cellcol));
799 else if (t.cat() == catSuper || t.cat() == catSub) {
800 bool up = (t.cat() == catSuper);
801 // we need no new script inset if the last thing was a scriptinset,
802 // which has that script already not the same script already
804 cell->push_back(MathAtom(new InsetMathScript(up)));
805 else if (cell->back()->asScriptInset() &&
806 !cell->back()->asScriptInset()->has(up))
807 cell->back().nucleus()->asScriptInset()->ensure(up);
808 else if (cell->back()->asScriptInset())
809 cell->push_back(MathAtom(new InsetMathScript(up)));
811 cell->back() = MathAtom(new InsetMathScript(cell->back(), up));
812 InsetMathScript * p = cell->back().nucleus()->asScriptInset();
813 // special handling of {}-bases
814 // is this always correct?
815 if (p->nuc().size() == 1
816 && p->nuc().back()->asBraceInset())
817 p->nuc() = p->nuc().back()->asNestInset()->cell(0);
818 parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
825 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
826 //lyxerr << "finished reading option" << endl;
830 else if (t.cat() == catOther)
831 cell->push_back(MathAtom(new InsetMathChar(t.character())));
833 else if (t.cat() == catComment) {
836 Token const & t = getToken();
837 if (t.cat() == catNewline)
841 cell->push_back(MathAtom(new InsetMathComment(s)));
849 else if (t.cs() == "lyxlock") {
851 cell->back().nucleus()->lock(true);
854 else if (t.cs() == "def" ||
855 t.cs() == "newcommand" ||
856 t.cs() == "renewcommand")
858 string const type = t.cs();
861 if (t.cs() == "def") {
863 name = getToken().cs();
867 while (good() && nextToken().cat() != catBegin) {
868 pars += getToken().cs();
872 //lyxerr << "read \\def parameter list '" << pars << "'" << endl;
874 } else { // t.cs() == "newcommand" || t.cs() == "renewcommand"
876 if (getToken().cat() != catBegin) {
877 error("'{' in \\newcommand expected (1) ");
881 name = getToken().cs();
883 if (getToken().cat() != catEnd) {
884 error("'}' in \\newcommand expected");
888 string const arg = getArg('[', ']');
890 nargs = convert<int>(arg);
895 parse(ar1, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
897 // we cannot handle recursive stuff at all
899 //test.push_back(createInsetMath(name));
900 //if (ar1.contains(test)) {
901 // error("we cannot handle recursive macros at all.");
905 // is a version for display attached?
908 if (nextToken().cat() == catBegin)
909 parse(ar2, FLAG_ITEM, InsetMath::MATH_MODE);
911 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, type,
915 else if (t.cs() == "(") {
916 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
917 parse2(cell->back(), FLAG_SIMPLE2, InsetMath::MATH_MODE, false);
920 else if (t.cs() == "[") {
921 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
922 parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false);
925 else if (t.cs() == "protect")
926 // ignore \\protect, will hopefully be re-added during output
929 else if (t.cs() == "end") {
930 if (flags & FLAG_END) {
931 // eat environment name
932 string const name = getArg('{', '}');
933 if (environments_.empty())
934 error("'found \\end{" + name +
935 "}' without matching '\\begin{" +
937 else if (name != environments_.back())
938 error("'\\end{" + name +
939 "}' does not match '\\begin{" +
940 environments_.back() + "}'");
942 environments_.pop_back();
943 // Delete empty last row in matrix
945 // If you abuse InsetMathGrid for
946 // non-matrix like structures you
947 // probably need to refine this test.
948 // Right now we only have to test for
949 // single line hull insets.
950 if (grid.nrows() > 1)
951 delEmptyLastRow(grid);
955 error("found 'end' unexpectedly");
958 else if (t.cs() == ")") {
959 if (flags & FLAG_SIMPLE2)
961 error("found '\\)' unexpectedly");
964 else if (t.cs() == "]") {
965 if (flags & FLAG_EQUATION)
967 error("found '\\]' unexpectedly");
970 else if (t.cs() == "\\") {
971 if (flags & FLAG_ALIGN)
973 if (addRow(grid, cellrow, getArg('[', ']'))) {
975 if (grid.asHullInset())
976 grid.asHullInset()->numbered(
978 cell = &grid.cell(grid.index(cellrow,
984 else if (t.cs() == "multicolumn") {
985 // extract column count and insert dummy cells
987 parse(count, FLAG_ITEM, mode);
989 if (!extractNumber(count, cols)) {
990 lyxerr << " can't extract number of cells from " << count << endl;
992 // resize the table if necessary
993 for (int i = 0; i < cols; ++i) {
994 if (addCol(grid, cellcol)) {
995 cell = &grid.cell(grid.index(
997 // mark this as dummy
998 grid.cellinfo(grid.index(
999 cellrow, cellcol)).dummy_ = true;
1002 // the last cell is the real thing, not a dummy
1003 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
1005 // read special alignment
1007 parse(align, FLAG_ITEM, mode);
1008 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
1010 // parse the remaining contents into the "real" cell
1011 parse(*cell, FLAG_ITEM, mode);
1015 else if (t.cs() == "limits")
1018 else if (t.cs() == "nolimits")
1021 else if (t.cs() == "nonumber") {
1022 if (grid.asHullInset())
1023 grid.asHullInset()->numbered(cellrow, false);
1026 else if (t.cs() == "number") {
1027 if (grid.asHullInset())
1028 grid.asHullInset()->numbered(cellrow, true);
1031 else if (t.cs() == "hline") {
1032 grid.rowinfo(cellrow).lines_ ++;
1035 else if (t.cs() == "sqrt") {
1037 parse(ar, FLAG_OPTION, mode);
1039 cell->push_back(MathAtom(new InsetMathRoot));
1040 cell->back().nucleus()->cell(0) = ar;
1041 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1043 cell->push_back(MathAtom(new InsetMathSqrt));
1044 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1048 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1049 cell->push_back(createInsetMath(t.cs()));
1050 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1051 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1054 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
1055 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1056 cell->push_back(MathAtom(new RefInset(t.cs())));
1057 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1058 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1061 else if (t.cs() == "left") {
1063 Token const & tl = getToken();
1064 // \| and \Vert are equivalent, and InsetMathDelim
1066 // FIXME: fix this in InsetMathDelim itself!
1067 string const l = tl.cs() == "|" ? "Vert" : tl.asString();
1069 parse(ar, FLAG_RIGHT, mode);
1071 Token const & tr = getToken();
1072 string const r = tr.cs() == "|" ? "Vert" : tr.asString();
1073 cell->push_back(MathAtom(new InsetMathDelim(l, r, ar)));
1076 else if (t.cs() == "right") {
1077 if (flags & FLAG_RIGHT)
1079 //lyxerr << "got so far: '" << cell << "'" << endl;
1080 error("Unmatched right delimiter");
1084 else if (t.cs() == "begin") {
1085 string const name = getArg('{', '}');
1086 environments_.push_back(name);
1088 if (name == "array" || name == "subarray") {
1089 string const valign = parse_verbatim_option() + 'c';
1090 string const halign = parse_verbatim_item();
1091 cell->push_back(MathAtom(new InsetMathArray(name, valign[0], halign)));
1092 parse2(cell->back(), FLAG_END, mode, false);
1095 else if (name == "tabular") {
1096 string const valign = parse_verbatim_option() + 'c';
1097 string const halign = parse_verbatim_item();
1098 cell->push_back(MathAtom(new InsetMathTabular(name, valign[0], halign)));
1099 parse2(cell->back(), FLAG_END, InsetMath::TEXT_MODE, false);
1102 else if (name == "split" || name == "cases" ||
1103 name == "gathered" || name == "aligned") {
1104 cell->push_back(createInsetMath(name));
1105 parse2(cell->back(), FLAG_END, mode, false);
1108 else if (name == "alignedat") {
1109 // ignore this for a while
1111 cell->push_back(createInsetMath(name));
1112 parse2(cell->back(), FLAG_END, mode, false);
1115 else if (name == "math") {
1116 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
1117 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, true);
1120 else if (name == "equation" || name == "equation*"
1121 || name == "displaymath") {
1122 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
1123 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation"));
1126 else if (name == "eqnarray" || name == "eqnarray*") {
1127 cell->push_back(MathAtom(new InsetMathHull(hullEqnArray)));
1128 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1131 else if (name == "align" || name == "align*") {
1132 cell->push_back(MathAtom(new InsetMathHull(hullAlign)));
1133 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1136 else if (name == "flalign" || name == "flalign*") {
1137 cell->push_back(MathAtom(new InsetMathHull(hullFlAlign)));
1138 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1141 else if (name == "alignat" || name == "alignat*") {
1142 // ignore this for a while
1144 cell->push_back(MathAtom(new InsetMathHull(hullAlignAt)));
1145 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1148 else if (name == "xalignat" || name == "xalignat*") {
1149 // ignore this for a while
1151 cell->push_back(MathAtom(new InsetMathHull(hullXAlignAt)));
1152 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1155 else if (name == "xxalignat") {
1156 // ignore this for a while
1158 cell->push_back(MathAtom(new InsetMathHull(hullXXAlignAt)));
1159 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1162 else if (name == "multline" || name == "multline*") {
1163 cell->push_back(MathAtom(new InsetMathHull(hullMultline)));
1164 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1167 else if (name == "gather" || name == "gather*") {
1168 cell->push_back(MathAtom(new InsetMathHull(hullGather)));
1169 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1172 else if (latexkeys const * l = in_word_set(name)) {
1173 if (l->inset == "matrix") {
1174 cell->push_back(createInsetMath(name));
1175 parse2(cell->back(), FLAG_END, mode, false);
1181 lyxerr << "found unknown math environment '" << name << "'" << endl;
1182 // create generic environment inset
1183 cell->push_back(MathAtom(new InsetMathEnv(name)));
1184 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1188 else if (t.cs() == "kern") {
1189 #ifdef WITH_WARNINGS
1194 Token const & t = getToken();
1200 if (isValidLength(s))
1203 cell->push_back(MathAtom(new InsetMathKern(s)));
1206 else if (t.cs() == "label") {
1207 // FIXME: This is swallowed in inline formulas
1208 string label = parse_verbatim_item();
1211 if (grid.asHullInset()) {
1212 grid.asHullInset()->label(cellrow, label);
1214 cell->push_back(createInsetMath(t.cs()));
1215 cell->push_back(MathAtom(new InsetMathBrace(ar)));
1219 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1220 MathAtom at = createInsetMath(t.cs());
1221 at.nucleus()->cell(0) = *cell;
1223 parse(at.nucleus()->cell(1), flags, mode);
1224 cell->push_back(at);
1228 else if (t.cs() == "color") {
1229 string const color = parse_verbatim_item();
1230 cell->push_back(MathAtom(new InsetMathColor(true, color)));
1231 parse(cell->back().nucleus()->cell(0), flags, mode);
1235 else if (t.cs() == "textcolor") {
1236 string const color = parse_verbatim_item();
1237 cell->push_back(MathAtom(new InsetMathColor(false, color)));
1238 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1241 else if (t.cs() == "normalcolor") {
1242 cell->push_back(createInsetMath(t.cs()));
1243 parse(cell->back().nucleus()->cell(0), flags, mode);
1247 else if (t.cs() == "substack") {
1248 cell->push_back(createInsetMath(t.cs()));
1249 parse2(cell->back(), FLAG_ITEM, mode, false);
1252 else if (t.cs() == "xymatrix") {
1253 cell->push_back(createInsetMath(t.cs()));
1254 parse2(cell->back(), FLAG_ITEM, mode, false);
1257 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1258 cell->push_back(createInsetMath(t.cs()));
1259 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, InsetMath::TEXT_MODE);
1260 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, InsetMath::TEXT_MODE);
1261 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, InsetMath::TEXT_MODE);
1264 else if (t.cs() == "tag") {
1265 if (nextToken().character() == '*') {
1267 cell->push_back(createInsetMath(t.cs() + '*'));
1269 cell->push_back(createInsetMath(t.cs()));
1270 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1274 else if (t.cs() == "infer") {
1276 parse(ar, FLAG_OPTION, mode);
1277 cell->push_back(createInsetMath(t.cs()));
1278 parse2(cell->back(), FLAG_ITEM, mode, false);
1282 else if (1 && t.cs() == "ar") {
1283 auto_ptr<InsetMathXYArrow> p(new InsetMathXYArrow);
1284 // try to read target
1285 parse(p->cell(0), FLAG_OTPTION, mode);
1286 // try to read label
1287 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1288 p->up_ = nextToken().cat() == catSuper;
1290 parse(p->cell(1), FLAG_ITEM, mode);
1291 //lyxerr << "read label: " << p->cell(1) << endl;
1294 cell->push_back(MathAtom(p.release()));
1295 //lyxerr << "read cell: " << cell << endl;
1299 else if (t.cs().size()) {
1300 latexkeys const * l = in_word_set(t.cs());
1302 if (l->inset == "big") {
1304 string const delim = getToken().asInput();
1305 if (InsetMathBig::isBigInsetDelim(delim))
1306 cell->push_back(MathAtom(
1307 new InsetMathBig(t.cs(), delim)));
1309 cell->push_back(createInsetMath(t.cs()));
1310 cell->push_back(createInsetMath(
1315 else if (l->inset == "font") {
1316 cell->push_back(createInsetMath(t.cs()));
1317 parse(cell->back().nucleus()->cell(0),
1318 FLAG_ITEM, asMode(mode, l->extra));
1321 else if (l->inset == "oldfont") {
1322 cell->push_back(createInsetMath(t.cs()));
1323 parse(cell->back().nucleus()->cell(0),
1324 flags | FLAG_ALIGN, asMode(mode, l->extra));
1325 if (prevToken().cat() != catAlign &&
1326 prevToken().cs() != "\\")
1331 else if (l->inset == "style") {
1332 cell->push_back(createInsetMath(t.cs()));
1333 parse(cell->back().nucleus()->cell(0),
1334 flags | FLAG_ALIGN, mode);
1335 if (prevToken().cat() != catAlign &&
1336 prevToken().cs() != "\\")
1342 MathAtom at = createInsetMath(t.cs());
1343 for (InsetMath::idx_type i = 0; i < at->nargs(); ++i)
1344 parse(at.nucleus()->cell(i),
1345 FLAG_ITEM, asMode(mode, l->extra));
1346 cell->push_back(at);
1351 MathAtom at = createInsetMath(t.cs());
1352 InsetMath::mode_type m = mode;
1353 //if (m == InsetMath::UNDECIDED_MODE)
1354 //lyxerr << "default creation: m1: " << m << endl;
1355 if (at->currentMode() != InsetMath::UNDECIDED_MODE)
1356 m = at->currentMode();
1357 //lyxerr << "default creation: m2: " << m << endl;
1358 InsetMath::idx_type start = 0;
1359 // this fails on \bigg[...\bigg]
1361 //parse(opt, FLAG_OPTION, InsetMath::VERBATIM_MODE);
1364 // at.nucleus()->cell(0) = opt;
1366 for (InsetMath::idx_type i = start; i < at->nargs(); ++i) {
1367 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1370 cell->push_back(at);
1375 if (flags & FLAG_LEAVE) {
1376 flags &= ~FLAG_LEAVE;
1384 } // anonymous namespace
1387 void mathed_parse_cell(MathArray & ar, string const & str)
1389 istringstream is(str);
1390 mathed_parse_cell(ar, is);
1394 void mathed_parse_cell(MathArray & ar, istream & is)
1396 Parser(is).parse(ar, 0, InsetMath::MATH_MODE);
1400 bool mathed_parse_normal(MathAtom & t, string const & str)
1402 istringstream is(str);
1403 return Parser(is).parse(t);
1407 bool mathed_parse_normal(MathAtom & t, istream & is)
1409 return Parser(is).parse(t);
1413 bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
1415 return Parser(lex).parse(t);
1419 void mathed_parse_normal(InsetMathGrid & grid, string const & str)
1421 istringstream is(str);
1422 Parser(is).parse1(grid, 0, InsetMath::MATH_MODE, false);
1428 fill(theCatcode, theCatcode + 256, catOther);
1429 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1430 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1432 theCatcode[int('\\')] = catEscape;
1433 theCatcode[int('{')] = catBegin;
1434 theCatcode[int('}')] = catEnd;
1435 theCatcode[int('$')] = catMath;
1436 theCatcode[int('&')] = catAlign;
1437 theCatcode[int('\n')] = catNewline;
1438 theCatcode[int('#')] = catParameter;
1439 theCatcode[int('^')] = catSuper;
1440 theCatcode[int('_')] = catSub;
1441 theCatcode[int(0x7f)] = catIgnore;
1442 theCatcode[int(' ')] = catSpace;
1443 theCatcode[int('\t')] = catSpace;
1444 theCatcode[int('\r')] = catNewline;
1445 theCatcode[int('~')] = catActive;
1446 theCatcode[int('%')] = catComment;