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_macrotemplate.h"
52 #include "math_parboxinset.h"
53 #include "math_parinset.h"
54 #include "math_rootinset.h"
55 #include "math_scriptinset.h"
56 #include "math_sqrtinset.h"
57 #include "math_support.h"
58 #include "math_tabularinset.h"
60 //#include "insets/insetref.h"
61 #include "ref_inset.h"
64 #include "support/std_sstream.h"
69 using std::istringstream;
83 MathInset::mode_type asMode(MathInset::mode_type oldmode, string const & str)
85 //lyxerr << "handling mode: '" << str << "'" << endl;
86 if (str == "mathmode")
87 return MathInset::MATH_MODE;
88 if (str == "textmode" || str == "forcetext")
89 return MathInset::TEXT_MODE;
94 bool stared(string const & s)
96 string::size_type const n = s.size();
97 return n && s[n - 1] == '*';
101 // These are TeX's catcodes
103 catEscape, // 0 backslash
113 catSpace, // 10 space
114 catLetter, // 11 a-zA-Z
115 catOther, // 12 none of the above
118 catInvalid // 15 <delete>
121 CatCode theCatcode[256];
124 inline CatCode catcode(unsigned char c)
126 return theCatcode[c];
131 FLAG_BRACE_LAST = 1 << 1, // last closing brace ends the parsing
132 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
133 FLAG_END = 1 << 3, // next \\end ends the parsing process
134 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
135 FLAG_TEXTMODE = 1 << 5, // we are in a box
136 FLAG_ITEM = 1 << 6, // read a (possibly braced token)
137 FLAG_LEAVE = 1 << 7, // leave the loop at the end
138 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
139 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
140 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
141 FLAG_OPTION = 1 << 11, // read [...] style option
142 FLAG_BRACED = 1 << 12 // read {...} style argument
148 fill(theCatcode, theCatcode + 256, catOther);
149 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
150 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
152 theCatcode[int('\\')] = catEscape;
153 theCatcode[int('{')] = catBegin;
154 theCatcode[int('}')] = catEnd;
155 theCatcode[int('$')] = catMath;
156 theCatcode[int('&')] = catAlign;
157 theCatcode[int('\n')] = catNewline;
158 theCatcode[int('#')] = catParameter;
159 theCatcode[int('^')] = catSuper;
160 theCatcode[int('_')] = catSub;
161 theCatcode[int(0x7f)] = catIgnore;
162 theCatcode[int(' ')] = catSpace;
163 theCatcode[int('\t')] = catSpace;
164 theCatcode[int('\r')] = catNewline;
165 theCatcode[int('~')] = catActive;
166 theCatcode[int('%')] = catComment;
172 // Helper class for parsing
178 Token() : cs_(), char_(0), cat_(catIgnore) {}
180 Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
182 Token(string const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
185 string const & cs() const { return cs_; }
187 CatCode cat() const { return cat_; }
189 char character() const { return char_; }
191 string asString() const { return cs_.size() ? cs_ : string(1, char_); }
202 ostream & operator<<(ostream & os, Token const & t)
205 os << '\\' << t.cs();
206 else if (t.cat() == catLetter)
209 os << '[' << t.character() << ',' << t.cat() << ']';
218 typedef MathInset::mode_type mode_type;
221 Parser(LyXLex & lex);
223 Parser(istream & is);
226 bool parse(MathAtom & at);
228 void parse(MathArray & array, unsigned flags, mode_type mode);
230 void parse1(MathGridInset & grid, unsigned flags, mode_type mode,
233 MathArray parse(unsigned flags, mode_type mode);
235 int lineno() const { return lineno_; }
241 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
242 /// get arg delimited by 'left' and 'right'
243 string getArg(char left, char right);
247 void error(string const & msg);
248 /// dump contents to screen
251 void tokenize(istream & is);
253 void tokenize(string const & s);
255 void skipSpaceTokens(istream & is, char c);
257 void push_back(Token const & t);
261 Token const & prevToken() const;
263 Token const & nextToken() const;
265 Token const & getToken();
266 /// skips spaces if any
269 void lex(string const & s);
273 string parse_verbatim_item();
275 string parse_verbatim_option();
280 vector<Token> tokens_;
286 Parser::Parser(LyXLex & lexer)
287 : lineno_(lexer.getLineNo()), pos_(0)
289 tokenize(lexer.getStream());
294 Parser::Parser(istream & is)
295 : lineno_(0), pos_(0)
301 void Parser::push_back(Token const & t)
303 tokens_.push_back(t);
307 void Parser::pop_back()
313 Token const & Parser::prevToken() const
315 static const Token dummy;
316 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
320 Token const & Parser::nextToken() const
322 static const Token dummy;
323 return good() ? tokens_[pos_] : dummy;
327 Token const & Parser::getToken()
329 static const Token dummy;
330 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
331 return good() ? tokens_[pos_++] : dummy;
335 void Parser::skipSpaces()
337 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
342 void Parser::putback()
348 bool Parser::good() const
350 return pos_ < tokens_.size();
354 char Parser::getChar()
357 error("The input stream is not well...");
358 return tokens_[pos_++].character();
362 string Parser::getArg(char left, char right)
372 while ((c = getChar()) != right && good())
379 void Parser::skipSpaceTokens(istream & is, char c)
381 // skip trailing spaces
382 while (catcode(c) == catSpace || catcode(c) == catNewline)
385 //lyxerr << "putting back: " << c << endl;
390 void Parser::tokenize(istream & is)
392 // eat everything up to the next \end_inset or end of stream
393 // and store it in s for further tokenization
398 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
399 s = s.substr(0, s.size() - 10);
403 // Remove the space after \end_inset
404 if (is.get(c) && c != ' ')
412 void Parser::tokenize(string const & buffer)
414 static bool init_done = false;
421 istringstream is(buffer.c_str(), ios::in | ios::binary);
425 //lyxerr << "reading c: " << c << endl;
427 switch (catcode(c)) {
431 if (catcode(c) == catNewline)
432 ; //push_back(Token("par"));
434 push_back(Token('\n', catNewline));
442 while (is.get(c) && catcode(c) != catNewline)
452 error("unexpected end of input");
455 if (catcode(c) == catLetter) {
457 while (is.get(c) && catcode(c) == catLetter)
459 skipSpaceTokens(is, c);
468 push_back(Token(c, catcode(c)));
470 skipSpaceTokens(is, c);
475 lyxerr << "ignoring a char: " << int(c) << endl;
480 push_back(Token(c, catcode(c)));
490 void Parser::dump() const
492 lyxerr << "\nTokens: ";
493 for (unsigned i = 0; i < tokens_.size(); ++i) {
496 lyxerr << tokens_[i];
498 lyxerr << " pos: " << pos_ << endl;
502 void Parser::error(string const & msg)
504 lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
510 bool Parser::parse(MathAtom & at)
514 parse(ar, false, MathInset::UNDECIDED_MODE);
515 if (ar.size() != 1 || ar.front()->getType() == "none") {
516 lyxerr << "unusual contents found: " << ar << endl;
517 at = MathAtom(new MathParInset(ar));
518 //if (at->nargs() > 0)
519 // at.nucleus()->cell(0) = ar;
521 // lyxerr << "unusual contents found: " << ar << endl;
529 string Parser::parse_verbatim_option()
533 if (nextToken().character() == '[') {
534 Token t = getToken();
535 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
536 if (t.cat() == catBegin) {
538 res += '{' + parse_verbatim_item() + '}';
547 string Parser::parse_verbatim_item()
551 if (nextToken().cat() == catBegin) {
552 Token t = getToken();
553 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
554 if (t.cat() == catBegin) {
556 res += '{' + parse_verbatim_item() + '}';
566 MathArray Parser::parse(unsigned flags, mode_type mode)
569 parse(ar, flags, mode);
574 void Parser::parse(MathArray & array, unsigned flags, mode_type mode)
576 MathGridInset grid(1, 1);
577 parse1(grid, flags, mode, false);
578 array = grid.cell(0);
582 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
585 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
589 void Parser::parse1(MathGridInset & grid, unsigned flags,
590 const mode_type mode, const bool numbered)
593 MathGridInset::row_type cellrow = 0;
594 MathGridInset::col_type cellcol = 0;
595 MathArray * cell = &grid.cell(grid.index(cellrow, cellcol));
597 if (grid.asHullInset())
598 grid.asHullInset()->numbered(cellrow, numbered);
601 //lyxerr << " flags: " << flags << endl;
602 //lyxerr << " mode: " << mode << endl;
603 //lyxerr << "grid: " << grid << endl;
606 Token const & t = getToken();
609 lyxerr << "t: " << t << " flags: " << flags << endl;
610 lyxerr << "mode: " << mode << endl;
615 if (flags & FLAG_ITEM) {
617 if (t.cat() == catBegin) {
618 // skip the brace and collect everything to the next matching
620 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
624 // handle only this single token, leave the loop if done
629 if (flags & FLAG_BRACED) {
630 if (t.cat() == catSpace)
633 if (t.cat() != catBegin) {
634 error("opening brace expected");
638 // skip the brace and collect everything to the next matching
640 flags = FLAG_BRACE_LAST;
644 if (flags & FLAG_OPTION) {
645 if (t.cat() == catOther && t.character() == '[') {
647 parse(ar, FLAG_BRACK_LAST, mode);
650 // no option found, put back token and we are done
659 if (t.cat() == catMath) {
660 if (mode != MathInset::MATH_MODE) {
661 // we are inside some text mode thingy, so opening new math is allowed
662 Token const & n = getToken();
663 if (n.cat() == catMath) {
664 // TeX's $$...$$ syntax for displayed math
665 cell->push_back(MathAtom(new MathHullInset("equation")));
666 parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
667 getToken(); // skip the second '$' token
669 // simple $...$ stuff
671 cell->push_back(MathAtom(new MathHullInset("simple")));
672 parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
676 else if (flags & FLAG_SIMPLE) {
677 // this is the end of the formula
682 error("something strange in the parser");
687 else if (t.cat() == catLetter)
688 cell->push_back(MathAtom(new MathCharInset(t.character())));
690 else if (t.cat() == catSpace && mode != MathInset::MATH_MODE) {
691 if (cell->empty() || cell->back()->getChar() != ' ')
692 cell->push_back(MathAtom(new MathCharInset(t.character())));
695 else if (t.cat() == catNewline && mode != MathInset::MATH_MODE) {
696 if (cell->empty() || cell->back()->getChar() != ' ')
697 cell->push_back(MathAtom(new MathCharInset(' ')));
700 else if (t.cat() == catParameter) {
701 Token const & n = getToken();
702 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
705 else if (t.cat() == catActive)
706 cell->push_back(MathAtom(new MathCharInset(t.character())));
708 else if (t.cat() == catBegin) {
710 parse(ar, FLAG_BRACE_LAST, mode);
711 // do not create a BraceInset if they were written by LyX
712 // this helps to keep the annoyance of "a choose b" to a minimum
713 if (ar.size() == 1 && ar[0]->extraBraces())
716 cell->push_back(MathAtom(new MathBraceInset(ar)));
719 else if (t.cat() == catEnd) {
720 if (flags & FLAG_BRACE_LAST)
722 error("found '}' unexpectedly");
724 //add(cell, '}', LM_TC_TEX);
727 else if (t.cat() == catAlign) {
729 //lyxerr << " column now " << cellcol << " max: " << grid.ncols() << endl;
730 if (cellcol == grid.ncols()) {
731 //lyxerr << "adding column " << cellcol << endl;
732 grid.addCol(cellcol - 1);
734 cell = &grid.cell(grid.index(cellrow, cellcol));
737 else if (t.cat() == catSuper || t.cat() == catSub) {
738 bool up = (t.cat() == catSuper);
739 // we need no new script inset if the last thing was a scriptinset,
740 // which has that script already not the same script already
742 cell->push_back(MathAtom(new MathScriptInset(up)));
743 else if (cell->back()->asScriptInset() &&
744 !cell->back()->asScriptInset()->has(up))
745 cell->back().nucleus()->asScriptInset()->ensure(up);
746 else if (cell->back()->asScriptInset())
747 cell->push_back(MathAtom(new MathScriptInset(up)));
749 cell->back() = MathAtom(new MathScriptInset(cell->back(), up));
750 MathScriptInset * p = cell->back().nucleus()->asScriptInset();
751 // special handling of {}-bases
752 // is this always correct?
753 // It appears that this is wrong (Dekel)
754 //if (p->nuc().size() == 1 && p->nuc().back()->asNestInset() &&
755 // p->nuc().back()->extraBraces())
756 // p->nuc() = p->nuc().back()->asNestInset()->cell(0);
757 parse(p->cell(up), FLAG_ITEM, mode);
764 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
765 //lyxerr << "finished reading option" << endl;
769 else if (t.cat() == catOther)
770 cell->push_back(MathAtom(new MathCharInset(t.character())));
772 else if (t.cat() == catComment) {
775 Token const & t = getToken();
776 if (t.cat() == catNewline)
780 cell->push_back(MathAtom(new MathCommentInset(s)));
788 else if (t.cs() == "lyxlock") {
790 cell->back().nucleus()->lock(true);
793 else if (t.cs() == "def" ||
794 t.cs() == "newcommand" ||
795 t.cs() == "renewcommand")
797 string const type = t.cs();
800 if (t.cs() == "def") {
802 name = getToken().cs();
806 while (good() && nextToken().cat() != catBegin) {
807 pars += getToken().cs();
811 //lyxerr << "read \\def parameter list '" << pars << "'" << endl;
813 } else { // t.cs() == "newcommand" || t.cs() == "renewcommand"
815 if (getToken().cat() != catBegin) {
816 error("'{' in \\newcommand expected (1) ");
820 name = getToken().cs();
822 if (getToken().cat() != catEnd) {
823 error("'}' in \\newcommand expected");
827 string arg = getArg('[', ']');
829 nargs = atoi(arg.c_str());
834 parse(ar1, FLAG_ITEM, MathInset::UNDECIDED_MODE);
836 // we cannot handle recursive stuff at all
838 //test.push_back(createMathInset(name));
839 //if (ar1.contains(test)) {
840 // error("we cannot handle recursive macros at all.");
844 // is a version for display attached?
847 if (nextToken().cat() == catBegin)
848 parse(ar2, FLAG_ITEM, MathInset::MATH_MODE);
850 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, type,
854 else if (t.cs() == "(") {
855 cell->push_back(MathAtom(new MathHullInset("simple")));
856 parse2(cell->back(), FLAG_SIMPLE2, MathInset::MATH_MODE, false);
859 else if (t.cs() == "[") {
860 cell->push_back(MathAtom(new MathHullInset("equation")));
861 parse2(cell->back(), FLAG_EQUATION, MathInset::MATH_MODE, false);
864 else if (t.cs() == "protect")
865 // ignore \\protect, will hopefully be re-added during output
868 else if (t.cs() == "end") {
869 if (flags & FLAG_END) {
870 // eat environment name
871 //string const name =
873 // FIXME: check that we ended the correct environment
876 error("found 'end' unexpectedly");
879 else if (t.cs() == ")") {
880 if (flags & FLAG_SIMPLE2)
882 error("found '\\)' unexpectedly");
885 else if (t.cs() == "]") {
886 if (flags & FLAG_EQUATION)
888 error("found '\\]' unexpectedly");
891 else if (t.cs() == "\\") {
892 grid.vcrskip(LyXLength(getArg('[', ']')), cellrow);
895 if (cellrow == grid.nrows())
896 grid.addRow(cellrow - 1);
897 if (grid.asHullInset())
898 grid.asHullInset()->numbered(cellrow, numbered);
899 cell = &grid.cell(grid.index(cellrow, cellcol));
903 else if (t.cs() == "multicolumn") {
904 // extract column count and insert dummy cells
906 parse(count, FLAG_ITEM, mode);
908 if (!extractNumber(count, cols)) {
909 lyxerr << " can't extract number of cells from " << count << endl;
911 // resize the table if necessary
912 for (int i = 0; i < cols; ++i) {
914 if (cellcol == grid.ncols()) {
915 //lyxerr << "adding column " << cellcol << endl;
916 grid.addCol(cellcol - 1);
918 cell = &grid.cell(grid.index(cellrow, cellcol));
919 // mark this as dummy
920 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = true;
922 // the last cell is the real thng, not a dummy
923 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
925 // read special alignment
927 parse(align, FLAG_ITEM, mode);
928 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
930 // parse the remaining contents into the "real" cell
931 parse(*cell, FLAG_ITEM, mode);
935 else if (t.cs() == "limits")
938 else if (t.cs() == "nolimits")
941 else if (t.cs() == "nonumber") {
942 if (grid.asHullInset())
943 grid.asHullInset()->numbered(cellrow, false);
946 else if (t.cs() == "number") {
947 if (grid.asHullInset())
948 grid.asHullInset()->numbered(cellrow, true);
951 else if (t.cs() == "hline") {
952 grid.rowinfo(cellrow).lines_ ++;
955 else if (t.cs() == "sqrt") {
957 parse(ar, FLAG_OPTION, mode);
959 cell->push_back(MathAtom(new MathRootInset));
960 cell->back().nucleus()->cell(0) = ar;
961 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
963 cell->push_back(MathAtom(new MathSqrtInset));
964 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
968 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
969 cell->push_back(createMathInset(t.cs()));
970 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
971 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
974 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
975 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
976 cell->push_back(MathAtom(new RefInset(t.cs())));
977 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
978 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
981 else if (t.cs() == "left") {
983 string l = getToken().asString();
985 parse(ar, FLAG_RIGHT, mode);
987 string r = getToken().asString();
988 cell->push_back(MathAtom(new MathDelimInset(l, r, ar)));
991 else if (t.cs() == "right") {
992 if (flags & FLAG_RIGHT)
994 //lyxerr << "got so far: '" << cell << "'" << endl;
995 error("Unmatched right delimiter");
999 else if (t.cs() == "begin") {
1000 string const name = getArg('{', '}');
1002 if (name == "array" || name == "subarray") {
1003 string const valign = parse_verbatim_option() + 'c';
1004 string const halign = parse_verbatim_item();
1005 cell->push_back(MathAtom(new MathArrayInset(name, valign[0], halign)));
1006 parse2(cell->back(), FLAG_END, mode, false);
1009 else if (name == "tabular") {
1010 string const valign = parse_verbatim_option() + 'c';
1011 string const halign = parse_verbatim_item();
1012 cell->push_back(MathAtom(new MathTabularInset(name, valign[0], halign)));
1013 parse2(cell->back(), FLAG_END, MathInset::TEXT_MODE, false);
1016 else if (name == "split" || name == "cases" ||
1017 name == "gathered" || name == "aligned") {
1018 cell->push_back(createMathInset(name));
1019 parse2(cell->back(), FLAG_END, mode, false);
1022 else if (name == "math") {
1023 cell->push_back(MathAtom(new MathHullInset("simple")));
1024 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, true);
1027 else if (name == "equation" || name == "equation*"
1028 || name == "displaymath") {
1029 cell->push_back(MathAtom(new MathHullInset("equation")));
1030 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, (name == "equation"));
1033 else if (name == "eqnarray" || name == "eqnarray*") {
1034 cell->push_back(MathAtom(new MathHullInset("eqnarray")));
1035 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1038 else if (name == "align" || name == "align*") {
1039 cell->push_back(MathAtom(new MathHullInset("align")));
1040 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1043 else if (name == "flalign" || name == "flalign*") {
1044 cell->push_back(MathAtom(new MathHullInset("flalign")));
1045 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1048 else if (name == "alignat" || name == "alignat*") {
1049 // ignore this for a while
1051 cell->push_back(MathAtom(new MathHullInset("alignat")));
1052 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1055 else if (name == "xalignat" || name == "xalignat*") {
1056 // ignore this for a while
1058 cell->push_back(MathAtom(new MathHullInset("xalignat")));
1059 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1062 else if (name == "xxalignat") {
1063 // ignore this for a while
1065 cell->push_back(MathAtom(new MathHullInset("xxalignat")));
1066 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1069 else if (name == "multline" || name == "multline*") {
1070 cell->push_back(MathAtom(new MathHullInset("multline")));
1071 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1074 else if (name == "gather" || name == "gather*") {
1075 cell->push_back(MathAtom(new MathHullInset("gather")));
1076 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1079 else if (latexkeys const * l = in_word_set(name)) {
1080 if (l->inset == "matrix") {
1081 cell->push_back(createMathInset(name));
1082 parse2(cell->back(), FLAG_END, mode, false);
1088 lyxerr << "found unknown math environment '" << name << "'" << endl;
1089 // create generic environment inset
1090 cell->push_back(MathAtom(new MathEnvInset(name)));
1091 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1095 else if (t.cs() == "kern") {
1096 #ifdef WITH_WARNINGS
1101 Token const & t = getToken();
1107 if (isValidLength(s))
1110 cell->push_back(MathAtom(new MathKernInset(s)));
1113 else if (t.cs() == "label") {
1114 string label = parse_verbatim_item();
1117 if (grid.asHullInset()) {
1118 grid.asHullInset()->label(cellrow, label);
1120 cell->push_back(createMathInset(t.cs()));
1121 cell->push_back(MathAtom(new MathBraceInset(ar)));
1125 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1126 MathAtom at = createMathInset(t.cs());
1127 at.nucleus()->cell(0) = *cell;
1129 parse(at.nucleus()->cell(1), flags, mode);
1130 cell->push_back(at);
1134 else if (t.cs() == "substack") {
1135 cell->push_back(createMathInset(t.cs()));
1136 parse2(cell->back(), FLAG_ITEM, mode, false);
1139 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1140 cell->push_back(createMathInset(t.cs()));
1141 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, MathInset::TEXT_MODE);
1142 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, MathInset::TEXT_MODE);
1143 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, MathInset::TEXT_MODE);
1147 else if (t.cs() == "infer") {
1149 parse(ar, FLAG_OPTION, mode);
1150 cell->push_back(createMathInset(t.cs()));
1151 parse2(cell->back(), FLAG_ITEM, mode, false);
1155 else if (1 && t.cs() == "ar") {
1156 MathXYArrowInset * p = new MathXYArrowInset;
1157 // try to read target
1158 parse(p->cell(0), FLAG_OTPTION, mode);
1159 // try to read label
1160 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1161 p->up_ = nextToken().cat() == catSuper;
1163 parse(p->cell(1), FLAG_ITEM, mode);
1164 //lyxerr << "read label: " << p->cell(1) << endl;
1167 cell->push_back(MathAtom(p));
1168 //lyxerr << "read cell: " << cell << endl;
1172 else if (t.cs().size()) {
1173 latexkeys const * l = in_word_set(t.cs());
1175 if (l->inset == "font") {
1176 cell->push_back(createMathInset(t.cs()));
1177 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, asMode(mode, l->extra));
1180 else if (l->inset == "oldfont") {
1181 cell->push_back(createMathInset(t.cs()));
1182 parse(cell->back().nucleus()->cell(0), flags, asMode(mode, l->extra));
1186 else if (l->inset == "style") {
1187 cell->push_back(createMathInset(t.cs()));
1188 parse(cell->back().nucleus()->cell(0), flags, mode);
1192 else if (l->inset == "parbox") {
1193 // read optional positioning and width
1194 string pos = parse_verbatim_option();
1195 string width = parse_verbatim_item();
1196 cell->push_back(createMathInset(t.cs()));
1197 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, MathInset::TEXT_MODE);
1198 cell->back().nucleus()->asParboxInset()->setPosition(pos);
1199 cell->back().nucleus()->asParboxInset()->setWidth(width);
1203 MathAtom at = createMathInset(t.cs());
1204 for (MathInset::idx_type i = 0; i < at->nargs(); ++i)
1205 parse(at.nucleus()->cell(i), FLAG_ITEM, asMode(mode, l->extra));
1206 cell->push_back(at);
1211 MathAtom at = createMathInset(t.cs());
1212 MathInset::mode_type m = mode;
1213 //if (m == MathInset::UNDECIDED_MODE)
1214 //lyxerr << "default creation: m1: " << m << endl;
1215 if (at->currentMode() != MathInset::UNDECIDED_MODE)
1216 m = at->currentMode();
1217 //lyxerr << "default creation: m2: " << m << endl;
1218 MathInset::idx_type start = 0;
1219 // this fails on \bigg[...\bigg]
1221 //parse(opt, FLAG_OPTION, MathInset::VERBATIM_MODE);
1224 // at.nucleus()->cell(0) = opt;
1226 for (MathInset::idx_type i = start; i < at->nargs(); ++i)
1227 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1228 cell->push_back(at);
1233 if (flags & FLAG_LEAVE) {
1234 flags &= ~FLAG_LEAVE;
1242 } // anonymous namespace
1245 void mathed_parse_cell(MathArray & ar, string const & str)
1247 istringstream is(str.c_str());
1248 mathed_parse_cell(ar, is);
1252 void mathed_parse_cell(MathArray & ar, istream & is)
1254 Parser(is).parse(ar, 0, MathInset::MATH_MODE);
1258 bool mathed_parse_normal(MathAtom & t, string const & str)
1260 istringstream is(str.c_str());
1261 return Parser(is).parse(t);
1265 bool mathed_parse_normal(MathAtom & t, istream & is)
1267 return Parser(is).parse(t);
1271 bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
1273 return Parser(lex).parse(t);
1277 void mathed_parse_normal(MathGridInset & grid, string const & str)
1279 istringstream is(str.c_str());
1280 Parser(is).parse1(grid, 0, MathInset::MATH_MODE, false);