2 \author André Pönitz (2001)
7 If someone desperately needs partial "structures" (such as a few
8 cells of an array inset or similar) (s)he could uses the
9 following hack as starting point to write some macros:
14 \def\makeamptab{\catcode`\&=4\relax}
15 \def\makeampletter{\catcode`\&=11\relax}
16 \def\b{\makeampletter\expandafter\makeamptab\bi}
35 #include "math_parser.h"
36 #include "math_inset.h"
37 #include "math_arrayinset.h"
38 #include "math_braceinset.h"
39 #include "math_boxinset.h"
40 #include "math_charinset.h"
41 #include "math_commentinset.h"
42 #include "math_deliminset.h"
43 #include "math_envinset.h"
44 #include "math_extern.h"
45 #include "math_factory.h"
46 #include "math_kerninset.h"
47 #include "math_macro.h"
48 #include "math_macrotemplate.h"
49 #include "math_hullinset.h"
50 #include "math_parboxinset.h"
51 #include "math_parinset.h"
52 #include "math_rootinset.h"
53 #include "math_scriptinset.h"
54 #include "math_sizeinset.h"
55 #include "math_sqrtinset.h"
56 #include "math_stringinset.h"
57 #include "math_support.h"
58 #include "math_tabularinset.h"
60 //#include "insets/insetref.h"
61 #include "ref_inset.h"
66 #include "support/LAssert.h"
85 MathInset::mode_type asMode(MathInset::mode_type oldmode, string const & str)
87 //lyxerr << "handling mode: '" << str << "'\n";
88 if (str == "mathmode")
89 return MathInset::MATH_MODE;
90 if (str == "textmode" || str == "forcetext")
91 return MathInset::TEXT_MODE;
96 bool stared(string const & s)
98 string::size_type const n = s.size();
99 return n && s[n - 1] == '*';
103 // These are TeX's catcodes
105 catEscape, // 0 backslash
115 catSpace, // 10 space
116 catLetter, // 11 a-zA-Z
117 catOther, // 12 none of the above
120 catInvalid // 15 <delete>
123 CatCode theCatcode[256];
126 inline CatCode catcode(unsigned char c)
128 return theCatcode[c];
133 FLAG_BRACE_LAST = 1 << 1, // last closing brace ends the parsing
134 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
135 FLAG_END = 1 << 3, // next \\end ends the parsing process
136 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
137 FLAG_TEXTMODE = 1 << 5, // we are in a box
138 FLAG_ITEM = 1 << 6, // read a (possibly braced token)
139 FLAG_LEAVE = 1 << 7, // leave the loop at the end
140 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
141 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
142 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
143 FLAG_OPTION = 1 << 11, // read [...] style option
144 FLAG_BRACED = 1 << 12 // read {...} style argument
150 fill(theCatcode, theCatcode + 256, catOther);
151 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
152 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
154 theCatcode['\\'] = catEscape;
155 theCatcode['{'] = catBegin;
156 theCatcode['}'] = catEnd;
157 theCatcode['$'] = catMath;
158 theCatcode['&'] = catAlign;
159 theCatcode['\n'] = catNewline;
160 theCatcode['#'] = catParameter;
161 theCatcode['^'] = catSuper;
162 theCatcode['_'] = catSub;
163 theCatcode['
\7f'] = catIgnore;
164 theCatcode[' '] = catSpace;
165 theCatcode['\t'] = catSpace;
166 theCatcode['\r'] = catNewline;
167 theCatcode['~'] = catActive;
168 theCatcode['%'] = catComment;
174 // Helper class for parsing
180 Token() : cs_(), char_(0), cat_(catIgnore) {}
182 Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
184 Token(string const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
187 string const & cs() const { return cs_; }
189 CatCode cat() const { return cat_; }
191 char character() const { return char_; }
193 string asString() const { return cs_.size() ? cs_ : string(1, char_); }
204 ostream & operator<<(ostream & os, Token const & t)
207 os << '\\' << t.cs();
208 else if (t.cat() == catLetter)
211 os << '[' << t.character() << ',' << t.cat() << ']';
220 typedef MathInset::mode_type mode_type;
223 Parser(LyXLex & lex);
225 Parser(istream & is);
228 bool parse(MathAtom & at);
230 void parse(MathArray & array, unsigned flags, mode_type mode);
232 void parse1(MathGridInset & grid, unsigned flags, mode_type mode,
235 MathArray parse(unsigned flags, mode_type mode);
237 int lineno() const { return lineno_; }
243 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
244 /// get arg delimited by 'left' and 'right'
245 string getArg(char left, char right);
249 void error(string const & msg);
250 /// dump contents to screen
253 void tokenize(istream & is);
255 void tokenize(string const & s);
257 void skipSpaceTokens(istream & is, char c);
259 void push_back(Token const & t);
263 Token const & prevToken() const;
265 Token const & nextToken() const;
267 Token const & getToken();
268 /// skips spaces if any
271 void lex(string const & s);
275 string parse_verbatim_item();
277 string parse_verbatim_option();
282 vector<Token> tokens_;
288 Parser::Parser(LyXLex & lexer)
289 : lineno_(lexer.getLineNo()), pos_(0)
291 tokenize(lexer.getStream());
296 Parser::Parser(istream & is)
297 : lineno_(0), pos_(0)
303 void Parser::push_back(Token const & t)
305 tokens_.push_back(t);
309 void Parser::pop_back()
315 Token const & Parser::prevToken() const
317 static const Token dummy;
318 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
322 Token const & Parser::nextToken() const
324 static const Token dummy;
325 return good() ? tokens_[pos_] : dummy;
329 Token const & Parser::getToken()
331 static const Token dummy;
332 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << '\n';
333 return good() ? tokens_[pos_++] : dummy;
337 void Parser::skipSpaces()
339 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
344 void Parser::putback()
350 bool Parser::good() const
352 return pos_ < tokens_.size();
356 char Parser::getChar()
359 error("The input stream is not well...");
360 return tokens_[pos_++].character();
364 string Parser::getArg(char left, char right)
374 while ((c = getChar()) != right && good())
381 void Parser::skipSpaceTokens(istream & is, char c)
383 // skip trailing spaces
384 while (catcode(c) == catSpace || catcode(c) == catNewline)
387 //lyxerr << "putting back: " << c << "\n";
392 void Parser::tokenize(istream & is)
394 // eat everything up to the next \end_inset or end of stream
395 // and store it in s for further tokenization
400 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
401 s = s.substr(0, s.size() - 10);
405 // Remove the space after \end_inset
406 if (is.get(c) && c != ' ')
414 void Parser::tokenize(string const & buffer)
416 static bool init_done = false;
423 istringstream is(buffer.c_str(), ios::in | ios::binary);
427 //lyxerr << "reading c: " << c << "\n";
429 switch (catcode(c)) {
433 if (catcode(c) == catNewline)
434 ; //push_back(Token("par"));
436 push_back(Token('\n', catNewline));
444 while (is.get(c) && catcode(c) != catNewline)
454 error("unexpected end of input");
457 if (catcode(c) == catLetter) {
459 while (is.get(c) && catcode(c) == catLetter)
461 skipSpaceTokens(is, c);
470 push_back(Token(c, catcode(c)));
472 skipSpaceTokens(is, c);
477 lyxerr << "ignoring a char: " << int(c) << "\n";
482 push_back(Token(c, catcode(c)));
492 void Parser::dump() const
494 lyxerr << "\nTokens: ";
495 for (unsigned i = 0; i < tokens_.size(); ++i) {
498 lyxerr << tokens_[i];
500 lyxerr << " pos: " << pos_ << "\n";
504 void Parser::error(string const & msg)
506 lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
512 bool Parser::parse(MathAtom & at)
516 parse(ar, false, MathInset::UNDECIDED_MODE);
517 if (ar.size() != 1 || ar.front()->getType() == "none") {
518 lyxerr << "unusual contents found: " << ar << endl;
519 at = MathAtom(new MathParInset(ar));
520 //if (at->nargs() > 0)
521 // at.nucleus()->cell(0) = ar;
523 // lyxerr << "unusual contents found: " << ar << endl;
531 string Parser::parse_verbatim_option()
535 if (nextToken().character() == '[') {
536 Token t = getToken();
537 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
538 if (t.cat() == catBegin) {
540 res += '{' + parse_verbatim_item() + '}';
549 string Parser::parse_verbatim_item()
553 if (nextToken().cat() == catBegin) {
554 Token t = getToken();
555 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
556 if (t.cat() == catBegin) {
558 res += '{' + parse_verbatim_item() + '}';
568 MathArray Parser::parse(unsigned flags, mode_type mode)
571 parse(ar, flags, mode);
576 void Parser::parse(MathArray & array, unsigned flags, mode_type mode)
578 MathGridInset grid(1, 1);
579 parse1(grid, flags, mode, false);
580 array = grid.cell(0);
584 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
587 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
591 void Parser::parse1(MathGridInset & grid, unsigned flags,
592 const mode_type mode, const bool numbered)
595 MathGridInset::row_type cellrow = 0;
596 MathGridInset::col_type cellcol = 0;
597 MathArray * cell = &grid.cell(grid.index(cellrow, cellcol));
599 if (grid.asHullInset())
600 grid.asHullInset()->numbered(cellrow, numbered);
603 //lyxerr << " flags: " << flags << "\n";
604 //lyxerr << " mode: " << mode << "\n";
605 //lyxerr << "grid: " << grid << endl;
608 Token const & t = getToken();
611 lyxerr << "t: " << t << " flags: " << flags << "\n";
612 lyxerr << "mode: " << mode << "\n";
617 if (flags & FLAG_ITEM) {
619 if (t.cat() == catBegin) {
620 // skip the brace and collect everything to the next matching
622 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
626 // handle only this single token, leave the loop if done
631 if (flags & FLAG_BRACED) {
632 if (t.cat() == catSpace)
635 if (t.cat() != catBegin) {
636 error("opening brace expected");
640 // skip the brace and collect everything to the next matching
642 flags = FLAG_BRACE_LAST;
646 if (flags & FLAG_OPTION) {
647 if (t.cat() == catOther && t.character() == '[') {
649 parse(ar, FLAG_BRACK_LAST, mode);
652 // no option found, put back token and we are done
661 if (t.cat() == catMath) {
662 if (mode != MathInset::MATH_MODE) {
663 // we are inside some text mode thingy, so opening new math is allowed
664 Token const & n = getToken();
665 if (n.cat() == catMath) {
666 // TeX's $$...$$ syntax for displayed math
667 cell->push_back(MathAtom(new MathHullInset("equation")));
668 parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
669 getToken(); // skip the second '$' token
671 // simple $...$ stuff
673 cell->push_back(MathAtom(new MathHullInset("simple")));
674 parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
678 else if (flags & FLAG_SIMPLE) {
679 // this is the end of the formula
684 error("something strange in the parser\n");
689 else if (t.cat() == catLetter)
690 cell->push_back(MathAtom(new MathCharInset(t.character())));
692 else if (t.cat() == catSpace && mode != MathInset::MATH_MODE) {
693 if (cell->empty() || cell->back()->getChar() != ' ')
694 cell->push_back(MathAtom(new MathCharInset(t.character())));
697 else if (t.cat() == catNewline && mode != MathInset::MATH_MODE) {
698 if (cell->empty() || cell->back()->getChar() != ' ')
699 cell->push_back(MathAtom(new MathCharInset(' ')));
702 else if (t.cat() == catParameter) {
703 Token const & n = getToken();
704 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
707 else if (t.cat() == catActive)
708 cell->push_back(MathAtom(new MathCharInset(t.character())));
710 else if (t.cat() == catBegin) {
712 parse(ar, FLAG_BRACE_LAST, mode);
713 // do not create a BraceInset if they were written by LyX
714 // this helps to keep the annoyance of "a choose b" to a minimum
715 if (ar.size() == 1 && ar[0]->extraBraces())
718 cell->push_back(MathAtom(new MathBraceInset(ar)));
721 else if (t.cat() == catEnd) {
722 if (flags & FLAG_BRACE_LAST)
724 error("found '}' unexpectedly");
726 //add(cell, '}', LM_TC_TEX);
729 else if (t.cat() == catAlign) {
731 //lyxerr << " column now " << cellcol << " max: " << grid.ncols() << "\n";
732 if (cellcol == grid.ncols()) {
733 //lyxerr << "adding column " << cellcol << "\n";
734 grid.addCol(cellcol - 1);
736 cell = &grid.cell(grid.index(cellrow, cellcol));
739 else if (t.cat() == catSuper || t.cat() == catSub) {
740 bool up = (t.cat() == catSuper);
741 // we need no new script inset if the last thing was a scriptinset,
742 // which has that script already not the same script already
744 cell->push_back(MathAtom(new MathScriptInset(up)));
745 else if (cell->back()->asScriptInset() &&
746 !cell->back()->asScriptInset()->has(up))
747 cell->back().nucleus()->asScriptInset()->ensure(up);
748 else if (cell->back()->asScriptInset())
749 cell->push_back(MathAtom(new MathScriptInset(up)));
751 cell->back() = MathAtom(new MathScriptInset(cell->back(), up));
752 MathScriptInset * p = cell->back().nucleus()->asScriptInset();
753 // special handling of {}-bases
754 // is this always correct?
755 // It appears that this is wrong (Dekel)
756 //if (p->nuc().size() == 1 && p->nuc().back()->asNestInset() &&
757 // p->nuc().back()->extraBraces())
758 // p->nuc() = p->nuc().back()->asNestInset()->cell(0);
759 parse(p->cell(up), FLAG_ITEM, mode);
766 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
767 //lyxerr << "finished reading option\n";
771 else if (t.cat() == catOther)
772 cell->push_back(MathAtom(new MathCharInset(t.character())));
774 else if (t.cat() == catComment) {
777 Token const & t = getToken();
778 if (t.cat() == catNewline)
782 cell->push_back(MathAtom(new MathCommentInset(s)));
790 else if (t.cs() == "lyxlock") {
792 cell->back().nucleus()->lock(true);
795 else if (t.cs() == "def" || t.cs() == "newcommand") {
798 if (t.cs() == "def") {
800 name = getToken().cs();
804 while (good() && nextToken().cat() != catBegin) {
805 pars += getToken().cs();
809 //lyxerr << "read \\def parameter list '" << pars << "'\n";
811 } else { // t.cs() == "newcommand"
813 if (getToken().cat() != catBegin) {
814 error("'{' in \\newcommand expected (1) \n");
818 name = getToken().cs();
820 if (getToken().cat() != catEnd) {
821 error("'}' in \\newcommand expected\n");
825 string arg = getArg('[', ']');
827 nargs = atoi(arg.c_str());
832 parse(ar1, FLAG_ITEM, MathInset::UNDECIDED_MODE);
834 // we cannot handle recursive stuff at all
836 //test.push_back(createMathInset(name));
837 //if (ar1.contains(test)) {
838 // error("we cannot handle recursive macros at all.\n");
842 // is a version for display attached?
845 if (nextToken().cat() == catBegin)
846 parse(ar2, FLAG_ITEM, MathInset::MATH_MODE);
848 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, ar1, ar2)));
851 else if (t.cs() == "(") {
852 cell->push_back(MathAtom(new MathHullInset("simple")));
853 parse2(cell->back(), FLAG_SIMPLE2, MathInset::MATH_MODE, false);
856 else if (t.cs() == "[") {
857 cell->push_back(MathAtom(new MathHullInset("equation")));
858 parse2(cell->back(), FLAG_EQUATION, MathInset::MATH_MODE, false);
861 else if (t.cs() == "protect")
862 // ignore \\protect, will hopefully be re-added during output
865 else if (t.cs() == "end") {
866 if (flags & FLAG_END) {
867 // eat environment name
868 //string const name =
870 // FIXME: check that we ended the correct environment
873 error("found 'end' unexpectedly");
876 else if (t.cs() == ")") {
877 if (flags & FLAG_SIMPLE2)
879 error("found '\\)' unexpectedly");
882 else if (t.cs() == "]") {
883 if (flags & FLAG_EQUATION)
885 error("found '\\]' unexpectedly");
888 else if (t.cs() == "\\") {
889 grid.vcrskip(LyXLength(getArg('[', ']')), cellrow);
892 if (cellrow == grid.nrows())
893 grid.addRow(cellrow - 1);
894 if (grid.asHullInset())
895 grid.asHullInset()->numbered(cellrow, numbered);
896 cell = &grid.cell(grid.index(cellrow, cellcol));
900 else if (t.cs() == "multicolumn") {
901 // extract column count and insert dummy cells
903 parse(count, FLAG_ITEM, mode);
905 if (!extractNumber(count, cols)) {
906 lyxerr << " can't extract number of cells from " << count << "\n";
908 // resize the table if necessary
909 for (int i = 0; i < cols; ++i) {
911 if (cellcol == grid.ncols()) {
912 //lyxerr << "adding column " << cellcol << "\n";
913 grid.addCol(cellcol - 1);
915 cell = &grid.cell(grid.index(cellrow, cellcol));
916 // mark this as dummy
917 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = true;
919 // the last cell is the real thng, not a dummy
920 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
922 // read special alignment
924 parse(align, FLAG_ITEM, mode);
925 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
927 // parse the remaining contents into the "real" cell
928 parse(*cell, FLAG_ITEM, mode);
932 else if (t.cs() == "limits")
935 else if (t.cs() == "nolimits")
938 else if (t.cs() == "nonumber") {
939 if (grid.asHullInset())
940 grid.asHullInset()->numbered(cellrow, false);
943 else if (t.cs() == "number") {
944 if (grid.asHullInset())
945 grid.asHullInset()->numbered(cellrow, true);
948 else if (t.cs() == "hline") {
949 grid.rowinfo(cellrow).lines_ ++;
952 else if (t.cs() == "sqrt") {
954 parse(ar, FLAG_OPTION, mode);
956 cell->push_back(MathAtom(new MathRootInset));
957 cell->back().nucleus()->cell(0) = ar;
958 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
960 cell->push_back(MathAtom(new MathSqrtInset));
961 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
965 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
966 cell->push_back(createMathInset(t.cs()));
967 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
968 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
971 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
972 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
973 cell->push_back(MathAtom(new RefInset(t.cs())));
974 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
975 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
978 else if (t.cs() == "left") {
980 string l = getToken().asString();
982 parse(ar, FLAG_RIGHT, mode);
984 string r = getToken().asString();
985 cell->push_back(MathAtom(new MathDelimInset(l, r, ar)));
988 else if (t.cs() == "right") {
989 if (flags & FLAG_RIGHT)
991 //lyxerr << "got so far: '" << cell << "'\n";
992 error("Unmatched right delimiter");
996 else if (t.cs() == "begin") {
997 string const name = getArg('{', '}');
999 if (name == "array" || name == "subarray") {
1000 string const valign = parse_verbatim_option() + 'c';
1001 string const halign = parse_verbatim_item();
1002 cell->push_back(MathAtom(new MathArrayInset(name, valign[0], halign)));
1003 parse2(cell->back(), FLAG_END, mode, false);
1006 else if (name == "tabular") {
1007 string const valign = parse_verbatim_option() + 'c';
1008 string const halign = parse_verbatim_item();
1009 cell->push_back(MathAtom(new MathTabularInset(name, valign[0], halign)));
1010 parse2(cell->back(), FLAG_END, MathInset::TEXT_MODE, false);
1013 else if (name == "split" || name == "cases" ||
1014 name == "gathered" || name == "aligned") {
1015 cell->push_back(createMathInset(name));
1016 parse2(cell->back(), FLAG_END, mode, false);
1019 else if (name == "math") {
1020 cell->push_back(MathAtom(new MathHullInset("simple")));
1021 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, true);
1024 else if (name == "equation" || name == "equation*"
1025 || name == "displaymath") {
1026 cell->push_back(MathAtom(new MathHullInset("equation")));
1027 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, (name == "equation"));
1030 else if (name == "eqnarray" || name == "eqnarray*") {
1031 cell->push_back(MathAtom(new MathHullInset("eqnarray")));
1032 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1035 else if (name == "align" || name == "align*") {
1036 cell->push_back(MathAtom(new MathHullInset("align")));
1037 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1040 else if (name == "flalign" || name == "flalign*") {
1041 cell->push_back(MathAtom(new MathHullInset("flalign")));
1042 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1045 else if (name == "alignat" || name == "alignat*") {
1046 // ignore this for a while
1048 cell->push_back(MathAtom(new MathHullInset("alignat")));
1049 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1052 else if (name == "xalignat" || name == "xalignat*") {
1053 // ignore this for a while
1055 cell->push_back(MathAtom(new MathHullInset("xalignat")));
1056 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1059 else if (name == "xxalignat") {
1060 // ignore this for a while
1062 cell->push_back(MathAtom(new MathHullInset("xxalignat")));
1063 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1066 else if (name == "multline" || name == "multline*") {
1067 cell->push_back(MathAtom(new MathHullInset("multline")));
1068 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1071 else if (name == "gather" || name == "gather*") {
1072 cell->push_back(MathAtom(new MathHullInset("gather")));
1073 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1076 else if (latexkeys const * l = in_word_set(name)) {
1077 if (l->inset == "matrix") {
1078 cell->push_back(createMathInset(name));
1079 parse2(cell->back(), FLAG_END, mode, false);
1085 lyxerr << "found unknown math environment '" << name << "'\n";
1086 // create generic environment inset
1087 cell->push_back(MathAtom(new MathEnvInset(name)));
1088 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1092 else if (t.cs() == "kern") {
1093 #ifdef WITH_WARNINGS
1098 Token const & t = getToken();
1104 if (isValidLength(s))
1107 cell->push_back(MathAtom(new MathKernInset(s)));
1110 else if (t.cs() == "label") {
1111 string label = parse_verbatim_item();
1112 if (grid.asHullInset()) {
1113 grid.asHullInset()->label(cellrow, label);
1115 cell->push_back(createMathInset(t.cs()));
1116 cell->push_back(MathAtom(new MathBraceInset(asArray(label))));
1120 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1121 MathAtom at = createMathInset(t.cs());
1122 at.nucleus()->cell(0) = *cell;
1124 parse(at.nucleus()->cell(1), flags, mode);
1125 cell->push_back(at);
1129 else if (t.cs() == "substack") {
1130 cell->push_back(createMathInset(t.cs()));
1131 parse2(cell->back(), FLAG_ITEM, mode, false);
1134 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1135 cell->push_back(createMathInset(t.cs()));
1136 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, MathInset::TEXT_MODE);
1137 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, MathInset::TEXT_MODE);
1138 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, MathInset::TEXT_MODE);
1142 else if (t.cs() == "infer") {
1144 parse(ar, FLAG_OPTION, mode);
1145 cell->push_back(createMathInset(t.cs()));
1146 parse2(cell->back(), FLAG_ITEM, mode, false);
1150 else if (1 && t.cs() == "ar") {
1151 MathXYArrowInset * p = new MathXYArrowInset;
1152 // try to read target
1153 parse(p->cell(0), FLAG_OTPTION, mode);
1154 // try to read label
1155 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1156 p->up_ = nextToken().cat() == catSuper;
1158 parse(p->cell(1), FLAG_ITEM, mode);
1159 //lyxerr << "read label: " << p->cell(1) << "\n";
1162 cell->push_back(MathAtom(p));
1163 //lyxerr << "read cell: " << cell << "\n";
1167 else if (t.cs().size()) {
1168 latexkeys const * l = in_word_set(t.cs());
1170 if (l->inset == "font") {
1171 cell->push_back(createMathInset(t.cs()));
1172 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, asMode(mode, l->extra));
1175 else if (l->inset == "oldfont") {
1176 cell->push_back(createMathInset(t.cs()));
1177 parse(cell->back().nucleus()->cell(0), flags, asMode(mode, l->extra));
1181 else if (l->inset == "style") {
1182 cell->push_back(createMathInset(t.cs()));
1183 parse(cell->back().nucleus()->cell(0), flags, mode);
1187 else if (l->inset == "parbox") {
1188 // read optional positioning and width
1189 string pos = parse_verbatim_option();
1190 string width = parse_verbatim_item();
1191 cell->push_back(createMathInset(t.cs()));
1192 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, MathInset::TEXT_MODE);
1193 cell->back().nucleus()->asParboxInset()->setPosition(pos);
1194 cell->back().nucleus()->asParboxInset()->setWidth(width);
1198 MathAtom at = createMathInset(t.cs());
1199 for (MathInset::idx_type i = 0; i < at->nargs(); ++i)
1200 parse(at.nucleus()->cell(i), FLAG_ITEM, asMode(mode, l->extra));
1201 cell->push_back(at);
1206 MathAtom at = createMathInset(t.cs());
1207 MathInset::mode_type m = mode;
1208 //if (m == MathInset::UNDECIDED_MODE)
1209 //lyxerr << "default creation: m1: " << m << "\n";
1210 if (at->currentMode() != MathInset::UNDECIDED_MODE)
1211 m = at->currentMode();
1212 //lyxerr << "default creation: m2: " << m << "\n";
1213 MathInset::idx_type start = 0;
1214 // this fails on \bigg[...\bigg]
1216 //parse(opt, FLAG_OPTION, MathInset::VERBATIM_MODE);
1219 // at.nucleus()->cell(0) = opt;
1221 for (MathInset::idx_type i = start; i < at->nargs(); ++i)
1222 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1223 cell->push_back(at);
1228 if (flags & FLAG_LEAVE) {
1229 flags &= ~FLAG_LEAVE;
1237 } // anonymous namespace
1240 void mathed_parse_cell(MathArray & ar, string const & str)
1242 istringstream is(str.c_str());
1243 mathed_parse_cell(ar, is);
1247 void mathed_parse_cell(MathArray & ar, istream & is)
1249 Parser(is).parse(ar, 0, MathInset::MATH_MODE);
1253 bool mathed_parse_normal(MathAtom & t, string const & str)
1255 istringstream is(str.c_str());
1256 return Parser(is).parse(t);
1260 bool mathed_parse_normal(MathAtom & t, istream & is)
1262 return Parser(is).parse(t);
1266 bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
1268 return Parser(lex).parse(t);
1272 void mathed_parse_normal(MathGridInset & grid, string const & str)
1274 istringstream is(str.c_str());
1275 Parser(is).parse1(grid, 0, MathInset::MATH_MODE, false);