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 "math_parser.h"
42 #include "math_arrayinset.h"
43 #include "math_braceinset.h"
44 #include "math_charinset.h"
45 #include "math_commentinset.h"
46 #include "math_deliminset.h"
47 #include "math_envinset.h"
48 #include "math_factory.h"
49 #include "math_kerninset.h"
50 #include "math_macro.h"
51 #include "math_macroarg.h"
52 #include "math_macrotemplate.h"
53 #include "math_parboxinset.h"
54 #include "math_parinset.h"
55 #include "math_rootinset.h"
56 #include "math_scriptinset.h"
57 #include "math_sqrtinset.h"
58 #include "math_support.h"
59 #include "math_tabularinset.h"
61 //#include "insets/insetref.h"
62 #include "ref_inset.h"
65 #include "support/std_sstream.h"
74 using std::istringstream;
84 MathInset::mode_type asMode(MathInset::mode_type oldmode, string const & str)
86 //lyxerr << "handling mode: '" << str << "'" << endl;
87 if (str == "mathmode")
88 return MathInset::MATH_MODE;
89 if (str == "textmode" || str == "forcetext")
90 return MathInset::TEXT_MODE;
95 bool stared(string const & s)
97 string::size_type const n = s.size();
98 return n && s[n - 1] == '*';
102 // These are TeX's catcodes
104 catEscape, // 0 backslash
114 catSpace, // 10 space
115 catLetter, // 11 a-zA-Z
116 catOther, // 12 none of the above
119 catInvalid // 15 <delete>
122 CatCode theCatcode[256];
125 inline CatCode catcode(unsigned char c)
127 return theCatcode[c];
132 FLAG_BRACE_LAST = 1 << 1, // last closing brace ends the parsing
133 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
134 FLAG_END = 1 << 3, // next \\end ends the parsing process
135 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
136 FLAG_TEXTMODE = 1 << 5, // we are in a box
137 FLAG_ITEM = 1 << 6, // read a (possibly braced token)
138 FLAG_LEAVE = 1 << 7, // leave the loop at the end
139 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
140 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
141 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
142 FLAG_OPTION = 1 << 11, // read [...] style option
143 FLAG_BRACED = 1 << 12 // read {...} style argument
149 fill(theCatcode, theCatcode + 256, catOther);
150 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
151 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
153 theCatcode[int('\\')] = catEscape;
154 theCatcode[int('{')] = catBegin;
155 theCatcode[int('}')] = catEnd;
156 theCatcode[int('$')] = catMath;
157 theCatcode[int('&')] = catAlign;
158 theCatcode[int('\n')] = catNewline;
159 theCatcode[int('#')] = catParameter;
160 theCatcode[int('^')] = catSuper;
161 theCatcode[int('_')] = catSub;
162 theCatcode[int(0x7f)] = catIgnore;
163 theCatcode[int(' ')] = catSpace;
164 theCatcode[int('\t')] = catSpace;
165 theCatcode[int('\r')] = catNewline;
166 theCatcode[int('~')] = catActive;
167 theCatcode[int('%')] = catComment;
173 // Helper class for parsing
179 Token() : cs_(), char_(0), cat_(catIgnore) {}
181 Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
183 Token(string const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
186 string const & cs() const { return cs_; }
188 CatCode cat() const { return cat_; }
190 char character() const { return char_; }
192 string asString() const { return cs_.size() ? cs_ : string(1, char_); }
203 ostream & operator<<(ostream & os, Token const & t)
206 os << '\\' << t.cs();
207 else if (t.cat() == catLetter)
210 os << '[' << t.character() << ',' << t.cat() << ']';
219 typedef MathInset::mode_type mode_type;
222 Parser(LyXLex & lex);
224 Parser(istream & is);
227 bool parse(MathAtom & at);
229 void parse(MathArray & array, unsigned flags, mode_type mode);
231 void parse1(MathGridInset & grid, unsigned flags, mode_type mode,
234 MathArray parse(unsigned flags, mode_type mode);
236 int lineno() const { return lineno_; }
242 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
243 /// get arg delimited by 'left' and 'right'
244 string getArg(char left, char right);
248 void error(string const & msg);
249 /// dump contents to screen
252 void tokenize(istream & is);
254 void tokenize(string const & s);
256 void skipSpaceTokens(istream & is, char c);
258 void push_back(Token const & t);
262 Token const & prevToken() const;
264 Token const & nextToken() const;
266 Token const & getToken();
267 /// skips spaces if any
270 void lex(string const & s);
274 string parse_verbatim_item();
276 string parse_verbatim_option();
281 vector<Token> tokens_;
287 Parser::Parser(LyXLex & lexer)
288 : lineno_(lexer.getLineNo()), pos_(0)
290 tokenize(lexer.getStream());
295 Parser::Parser(istream & is)
296 : lineno_(0), pos_(0)
302 void Parser::push_back(Token const & t)
304 tokens_.push_back(t);
308 void Parser::pop_back()
314 Token const & Parser::prevToken() const
316 static const Token dummy;
317 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
321 Token const & Parser::nextToken() const
323 static const Token dummy;
324 return good() ? tokens_[pos_] : dummy;
328 Token const & Parser::getToken()
330 static const Token dummy;
331 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
332 return good() ? tokens_[pos_++] : dummy;
336 void Parser::skipSpaces()
338 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
343 void Parser::putback()
349 bool Parser::good() const
351 return pos_ < tokens_.size();
355 char Parser::getChar()
358 error("The input stream is not well...");
359 return tokens_[pos_++].character();
363 string Parser::getArg(char left, char right)
373 while ((c = getChar()) != right && good())
380 void Parser::skipSpaceTokens(istream & is, char c)
382 // skip trailing spaces
383 while (catcode(c) == catSpace || catcode(c) == catNewline)
386 //lyxerr << "putting back: " << c << endl;
391 void Parser::tokenize(istream & is)
393 // eat everything up to the next \end_inset or end of stream
394 // and store it in s for further tokenization
399 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
400 s = s.substr(0, s.size() - 10);
404 // Remove the space after \end_inset
405 if (is.get(c) && c != ' ')
413 void Parser::tokenize(string const & buffer)
415 static bool init_done = false;
422 istringstream is(buffer.c_str(), ios::in | ios::binary);
426 //lyxerr << "reading c: " << c << endl;
428 switch (catcode(c)) {
432 if (catcode(c) == catNewline)
433 ; //push_back(Token("par"));
435 push_back(Token('\n', catNewline));
443 while (is.get(c) && catcode(c) != catNewline)
453 error("unexpected end of input");
456 if (catcode(c) == catLetter) {
458 while (is.get(c) && catcode(c) == catLetter)
460 skipSpaceTokens(is, c);
469 push_back(Token(c, catcode(c)));
471 skipSpaceTokens(is, c);
476 lyxerr << "ignoring a char: " << int(c) << endl;
481 push_back(Token(c, catcode(c)));
491 void Parser::dump() const
493 lyxerr << "\nTokens: ";
494 for (unsigned i = 0; i < tokens_.size(); ++i) {
497 lyxerr << tokens_[i];
499 lyxerr << " pos: " << pos_ << endl;
503 void Parser::error(string const & msg)
505 lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
511 bool Parser::parse(MathAtom & at)
515 parse(ar, false, MathInset::UNDECIDED_MODE);
516 if (ar.size() != 1 || ar.front()->getType() == "none") {
517 lyxerr << "unusual contents found: " << ar << endl;
518 at = MathAtom(new MathParInset(ar));
519 //if (at->nargs() > 0)
520 // at.nucleus()->cell(0) = ar;
522 // lyxerr << "unusual contents found: " << ar << endl;
530 string Parser::parse_verbatim_option()
534 if (nextToken().character() == '[') {
535 Token t = getToken();
536 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
537 if (t.cat() == catBegin) {
539 res += '{' + parse_verbatim_item() + '}';
548 string Parser::parse_verbatim_item()
552 if (nextToken().cat() == catBegin) {
553 Token t = getToken();
554 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
555 if (t.cat() == catBegin) {
557 res += '{' + parse_verbatim_item() + '}';
567 MathArray Parser::parse(unsigned flags, mode_type mode)
570 parse(ar, flags, mode);
575 void Parser::parse(MathArray & array, unsigned flags, mode_type mode)
577 MathGridInset grid(1, 1);
578 parse1(grid, flags, mode, false);
579 array = grid.cell(0);
583 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
586 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
590 void Parser::parse1(MathGridInset & grid, unsigned flags,
591 const mode_type mode, const bool numbered)
594 MathGridInset::row_type cellrow = 0;
595 MathGridInset::col_type cellcol = 0;
596 MathArray * cell = &grid.cell(grid.index(cellrow, cellcol));
598 if (grid.asHullInset())
599 grid.asHullInset()->numbered(cellrow, numbered);
602 //lyxerr << " flags: " << flags << endl;
603 //lyxerr << " mode: " << mode << endl;
604 //lyxerr << "grid: " << grid << endl;
607 Token const & t = getToken();
610 lyxerr << "t: " << t << " flags: " << flags << endl;
611 lyxerr << "mode: " << mode << endl;
616 if (flags & FLAG_ITEM) {
618 if (t.cat() == catBegin) {
619 // skip the brace and collect everything to the next matching
621 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
625 // handle only this single token, leave the loop if done
630 if (flags & FLAG_BRACED) {
631 if (t.cat() == catSpace)
634 if (t.cat() != catBegin) {
635 error("opening brace expected");
639 // skip the brace and collect everything to the next matching
641 flags = FLAG_BRACE_LAST;
645 if (flags & FLAG_OPTION) {
646 if (t.cat() == catOther && t.character() == '[') {
648 parse(ar, FLAG_BRACK_LAST, mode);
651 // no option found, put back token and we are done
660 if (t.cat() == catMath) {
661 if (mode != MathInset::MATH_MODE) {
662 // we are inside some text mode thingy, so opening new math is allowed
663 Token const & n = getToken();
664 if (n.cat() == catMath) {
665 // TeX's $$...$$ syntax for displayed math
666 cell->push_back(MathAtom(new MathHullInset("equation")));
667 parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
668 getToken(); // skip the second '$' token
670 // simple $...$ stuff
672 cell->push_back(MathAtom(new MathHullInset("simple")));
673 parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
677 else if (flags & FLAG_SIMPLE) {
678 // this is the end of the formula
683 error("something strange in the parser");
688 else if (t.cat() == catLetter)
689 cell->push_back(MathAtom(new MathCharInset(t.character())));
691 else if (t.cat() == catSpace && mode != MathInset::MATH_MODE) {
692 if (cell->empty() || cell->back()->getChar() != ' ')
693 cell->push_back(MathAtom(new MathCharInset(t.character())));
696 else if (t.cat() == catNewline && mode != MathInset::MATH_MODE) {
697 if (cell->empty() || cell->back()->getChar() != ' ')
698 cell->push_back(MathAtom(new MathCharInset(' ')));
701 else if (t.cat() == catParameter) {
702 Token const & n = getToken();
703 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
706 else if (t.cat() == catActive)
707 cell->push_back(MathAtom(new MathCharInset(t.character())));
709 else if (t.cat() == catBegin) {
711 parse(ar, FLAG_BRACE_LAST, mode);
712 // do not create a BraceInset if they were written by LyX
713 // this helps to keep the annoyance of "a choose b" to a minimum
714 if (ar.size() == 1 && ar[0]->extraBraces())
717 cell->push_back(MathAtom(new MathBraceInset(ar)));
720 else if (t.cat() == catEnd) {
721 if (flags & FLAG_BRACE_LAST)
723 error("found '}' unexpectedly");
724 //BOOST_ASSERT(false);
725 //add(cell, '}', LM_TC_TEX);
728 else if (t.cat() == catAlign) {
730 //lyxerr << " column now " << cellcol << " max: " << grid.ncols() << endl;
731 if (cellcol == grid.ncols()) {
732 //lyxerr << "adding column " << cellcol << endl;
733 grid.addCol(cellcol - 1);
735 cell = &grid.cell(grid.index(cellrow, cellcol));
738 else if (t.cat() == catSuper || t.cat() == catSub) {
739 bool up = (t.cat() == catSuper);
740 // we need no new script inset if the last thing was a scriptinset,
741 // which has that script already not the same script already
743 cell->push_back(MathAtom(new MathScriptInset(up)));
744 else if (cell->back()->asScriptInset() &&
745 !cell->back()->asScriptInset()->has(up))
746 cell->back().nucleus()->asScriptInset()->ensure(up);
747 else if (cell->back()->asScriptInset())
748 cell->push_back(MathAtom(new MathScriptInset(up)));
750 cell->back() = MathAtom(new MathScriptInset(cell->back(), up));
751 MathScriptInset * p = cell->back().nucleus()->asScriptInset();
752 // special handling of {}-bases
753 // is this always correct?
754 // It appears that this is wrong (Dekel)
755 //if (p->nuc().size() == 1 && p->nuc().back()->asNestInset() &&
756 // p->nuc().back()->extraBraces())
757 // p->nuc() = p->nuc().back()->asNestInset()->cell(0);
758 parse(p->cell(up), FLAG_ITEM, mode);
765 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
766 //lyxerr << "finished reading option" << endl;
770 else if (t.cat() == catOther)
771 cell->push_back(MathAtom(new MathCharInset(t.character())));
773 else if (t.cat() == catComment) {
776 Token const & t = getToken();
777 if (t.cat() == catNewline)
781 cell->push_back(MathAtom(new MathCommentInset(s)));
789 else if (t.cs() == "lyxlock") {
791 cell->back().nucleus()->lock(true);
794 else if (t.cs() == "def" ||
795 t.cs() == "newcommand" ||
796 t.cs() == "renewcommand")
798 string const type = t.cs();
801 if (t.cs() == "def") {
803 name = getToken().cs();
807 while (good() && nextToken().cat() != catBegin) {
808 pars += getToken().cs();
812 //lyxerr << "read \\def parameter list '" << pars << "'" << endl;
814 } else { // t.cs() == "newcommand" || t.cs() == "renewcommand"
816 if (getToken().cat() != catBegin) {
817 error("'{' in \\newcommand expected (1) ");
821 name = getToken().cs();
823 if (getToken().cat() != catEnd) {
824 error("'}' in \\newcommand expected");
828 string arg = getArg('[', ']');
830 nargs = atoi(arg.c_str());
835 parse(ar1, FLAG_ITEM, MathInset::UNDECIDED_MODE);
837 // we cannot handle recursive stuff at all
839 //test.push_back(createMathInset(name));
840 //if (ar1.contains(test)) {
841 // error("we cannot handle recursive macros at all.");
845 // is a version for display attached?
848 if (nextToken().cat() == catBegin)
849 parse(ar2, FLAG_ITEM, MathInset::MATH_MODE);
851 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, type,
855 else if (t.cs() == "(") {
856 cell->push_back(MathAtom(new MathHullInset("simple")));
857 parse2(cell->back(), FLAG_SIMPLE2, MathInset::MATH_MODE, false);
860 else if (t.cs() == "[") {
861 cell->push_back(MathAtom(new MathHullInset("equation")));
862 parse2(cell->back(), FLAG_EQUATION, MathInset::MATH_MODE, false);
865 else if (t.cs() == "protect")
866 // ignore \\protect, will hopefully be re-added during output
869 else if (t.cs() == "end") {
870 if (flags & FLAG_END) {
871 // eat environment name
872 //string const name =
874 // FIXME: check that we ended the correct environment
877 error("found 'end' unexpectedly");
880 else if (t.cs() == ")") {
881 if (flags & FLAG_SIMPLE2)
883 error("found '\\)' unexpectedly");
886 else if (t.cs() == "]") {
887 if (flags & FLAG_EQUATION)
889 error("found '\\]' unexpectedly");
892 else if (t.cs() == "\\") {
893 grid.vcrskip(LyXLength(getArg('[', ']')), cellrow);
896 if (cellrow == grid.nrows())
897 grid.addRow(cellrow - 1);
898 if (grid.asHullInset())
899 grid.asHullInset()->numbered(cellrow, numbered);
900 cell = &grid.cell(grid.index(cellrow, cellcol));
904 else if (t.cs() == "multicolumn") {
905 // extract column count and insert dummy cells
907 parse(count, FLAG_ITEM, mode);
909 if (!extractNumber(count, cols)) {
910 lyxerr << " can't extract number of cells from " << count << endl;
912 // resize the table if necessary
913 for (int i = 0; i < cols; ++i) {
915 if (cellcol == grid.ncols()) {
916 //lyxerr << "adding column " << cellcol << endl;
917 grid.addCol(cellcol - 1);
919 cell = &grid.cell(grid.index(cellrow, cellcol));
920 // mark this as dummy
921 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = true;
923 // the last cell is the real thng, not a dummy
924 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
926 // read special alignment
928 parse(align, FLAG_ITEM, mode);
929 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
931 // parse the remaining contents into the "real" cell
932 parse(*cell, FLAG_ITEM, mode);
936 else if (t.cs() == "limits")
939 else if (t.cs() == "nolimits")
942 else if (t.cs() == "nonumber") {
943 if (grid.asHullInset())
944 grid.asHullInset()->numbered(cellrow, false);
947 else if (t.cs() == "number") {
948 if (grid.asHullInset())
949 grid.asHullInset()->numbered(cellrow, true);
952 else if (t.cs() == "hline") {
953 grid.rowinfo(cellrow).lines_ ++;
956 else if (t.cs() == "sqrt") {
958 parse(ar, FLAG_OPTION, mode);
960 cell->push_back(MathAtom(new MathRootInset));
961 cell->back().nucleus()->cell(0) = ar;
962 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
964 cell->push_back(MathAtom(new MathSqrtInset));
965 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
969 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
970 cell->push_back(createMathInset(t.cs()));
971 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
972 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
975 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
976 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
977 cell->push_back(MathAtom(new RefInset(t.cs())));
978 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
979 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
982 else if (t.cs() == "left") {
984 string l = getToken().asString();
986 parse(ar, FLAG_RIGHT, mode);
988 string r = getToken().asString();
989 cell->push_back(MathAtom(new MathDelimInset(l, r, ar)));
992 else if (t.cs() == "right") {
993 if (flags & FLAG_RIGHT)
995 //lyxerr << "got so far: '" << cell << "'" << endl;
996 error("Unmatched right delimiter");
1000 else if (t.cs() == "begin") {
1001 string const name = getArg('{', '}');
1003 if (name == "array" || name == "subarray") {
1004 string const valign = parse_verbatim_option() + 'c';
1005 string const halign = parse_verbatim_item();
1006 cell->push_back(MathAtom(new MathArrayInset(name, valign[0], halign)));
1007 parse2(cell->back(), FLAG_END, mode, false);
1010 else if (name == "tabular") {
1011 string const valign = parse_verbatim_option() + 'c';
1012 string const halign = parse_verbatim_item();
1013 cell->push_back(MathAtom(new MathTabularInset(name, valign[0], halign)));
1014 parse2(cell->back(), FLAG_END, MathInset::TEXT_MODE, false);
1017 else if (name == "split" || name == "cases" ||
1018 name == "gathered" || name == "aligned") {
1019 cell->push_back(createMathInset(name));
1020 parse2(cell->back(), FLAG_END, mode, false);
1023 else if (name == "math") {
1024 cell->push_back(MathAtom(new MathHullInset("simple")));
1025 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, true);
1028 else if (name == "equation" || name == "equation*"
1029 || name == "displaymath") {
1030 cell->push_back(MathAtom(new MathHullInset("equation")));
1031 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, (name == "equation"));
1034 else if (name == "eqnarray" || name == "eqnarray*") {
1035 cell->push_back(MathAtom(new MathHullInset("eqnarray")));
1036 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1039 else if (name == "align" || name == "align*") {
1040 cell->push_back(MathAtom(new MathHullInset("align")));
1041 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1044 else if (name == "flalign" || name == "flalign*") {
1045 cell->push_back(MathAtom(new MathHullInset("flalign")));
1046 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1049 else if (name == "alignat" || name == "alignat*") {
1050 // ignore this for a while
1052 cell->push_back(MathAtom(new MathHullInset("alignat")));
1053 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1056 else if (name == "xalignat" || name == "xalignat*") {
1057 // ignore this for a while
1059 cell->push_back(MathAtom(new MathHullInset("xalignat")));
1060 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1063 else if (name == "xxalignat") {
1064 // ignore this for a while
1066 cell->push_back(MathAtom(new MathHullInset("xxalignat")));
1067 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1070 else if (name == "multline" || name == "multline*") {
1071 cell->push_back(MathAtom(new MathHullInset("multline")));
1072 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1075 else if (name == "gather" || name == "gather*") {
1076 cell->push_back(MathAtom(new MathHullInset("gather")));
1077 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1080 else if (latexkeys const * l = in_word_set(name)) {
1081 if (l->inset == "matrix") {
1082 cell->push_back(createMathInset(name));
1083 parse2(cell->back(), FLAG_END, mode, false);
1089 lyxerr << "found unknown math environment '" << name << "'" << endl;
1090 // create generic environment inset
1091 cell->push_back(MathAtom(new MathEnvInset(name)));
1092 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1096 else if (t.cs() == "kern") {
1097 #ifdef WITH_WARNINGS
1102 Token const & t = getToken();
1108 if (isValidLength(s))
1111 cell->push_back(MathAtom(new MathKernInset(s)));
1114 else if (t.cs() == "label") {
1115 string label = parse_verbatim_item();
1118 if (grid.asHullInset()) {
1119 grid.asHullInset()->label(cellrow, label);
1121 cell->push_back(createMathInset(t.cs()));
1122 cell->push_back(MathAtom(new MathBraceInset(ar)));
1126 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1127 MathAtom at = createMathInset(t.cs());
1128 at.nucleus()->cell(0) = *cell;
1130 parse(at.nucleus()->cell(1), flags, mode);
1131 cell->push_back(at);
1135 else if (t.cs() == "substack") {
1136 cell->push_back(createMathInset(t.cs()));
1137 parse2(cell->back(), FLAG_ITEM, mode, false);
1140 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1141 cell->push_back(createMathInset(t.cs()));
1142 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, MathInset::TEXT_MODE);
1143 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, MathInset::TEXT_MODE);
1144 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, MathInset::TEXT_MODE);
1148 else if (t.cs() == "infer") {
1150 parse(ar, FLAG_OPTION, mode);
1151 cell->push_back(createMathInset(t.cs()));
1152 parse2(cell->back(), FLAG_ITEM, mode, false);
1156 else if (1 && t.cs() == "ar") {
1157 MathXYArrowInset * p = new MathXYArrowInset;
1158 // try to read target
1159 parse(p->cell(0), FLAG_OTPTION, mode);
1160 // try to read label
1161 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1162 p->up_ = nextToken().cat() == catSuper;
1164 parse(p->cell(1), FLAG_ITEM, mode);
1165 //lyxerr << "read label: " << p->cell(1) << endl;
1168 cell->push_back(MathAtom(p));
1169 //lyxerr << "read cell: " << cell << endl;
1173 else if (t.cs().size()) {
1174 latexkeys const * l = in_word_set(t.cs());
1176 if (l->inset == "font") {
1177 cell->push_back(createMathInset(t.cs()));
1178 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, asMode(mode, l->extra));
1181 else if (l->inset == "oldfont") {
1182 cell->push_back(createMathInset(t.cs()));
1183 parse(cell->back().nucleus()->cell(0), flags, asMode(mode, l->extra));
1187 else if (l->inset == "style") {
1188 cell->push_back(createMathInset(t.cs()));
1189 parse(cell->back().nucleus()->cell(0), flags, mode);
1193 else if (l->inset == "parbox") {
1194 // read optional positioning and width
1195 string pos = parse_verbatim_option();
1196 string width = parse_verbatim_item();
1197 cell->push_back(createMathInset(t.cs()));
1198 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, MathInset::TEXT_MODE);
1199 cell->back().nucleus()->asParboxInset()->setPosition(pos);
1200 cell->back().nucleus()->asParboxInset()->setWidth(width);
1204 MathAtom at = createMathInset(t.cs());
1205 for (MathInset::idx_type i = 0; i < at->nargs(); ++i)
1206 parse(at.nucleus()->cell(i), FLAG_ITEM, asMode(mode, l->extra));
1207 cell->push_back(at);
1212 MathAtom at = createMathInset(t.cs());
1213 MathInset::mode_type m = mode;
1214 //if (m == MathInset::UNDECIDED_MODE)
1215 //lyxerr << "default creation: m1: " << m << endl;
1216 if (at->currentMode() != MathInset::UNDECIDED_MODE)
1217 m = at->currentMode();
1218 //lyxerr << "default creation: m2: " << m << endl;
1219 MathInset::idx_type start = 0;
1220 // this fails on \bigg[...\bigg]
1222 //parse(opt, FLAG_OPTION, MathInset::VERBATIM_MODE);
1225 // at.nucleus()->cell(0) = opt;
1227 for (MathInset::idx_type i = start; i < at->nargs(); ++i)
1228 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1229 cell->push_back(at);
1234 if (flags & FLAG_LEAVE) {
1235 flags &= ~FLAG_LEAVE;
1243 } // anonymous namespace
1246 void mathed_parse_cell(MathArray & ar, string const & str)
1248 istringstream is(str.c_str());
1249 mathed_parse_cell(ar, is);
1253 void mathed_parse_cell(MathArray & ar, istream & is)
1255 Parser(is).parse(ar, 0, MathInset::MATH_MODE);
1259 bool mathed_parse_normal(MathAtom & t, string const & str)
1261 istringstream is(str.c_str());
1262 return Parser(is).parse(t);
1266 bool mathed_parse_normal(MathAtom & t, istream & is)
1268 return Parser(is).parse(t);
1272 bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
1274 return Parser(lex).parse(t);
1278 void mathed_parse_normal(MathGridInset & grid, string const & str)
1280 istringstream is(str.c_str());
1281 Parser(is).parse1(grid, 0, MathInset::MATH_MODE, false);