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}
36 #include "math_parser.h"
37 #include "math_inset.h"
38 #include "math_arrayinset.h"
39 #include "math_braceinset.h"
40 #include "math_boxinset.h"
41 #include "math_charinset.h"
42 #include "math_commentinset.h"
43 #include "math_deliminset.h"
44 #include "math_envinset.h"
45 #include "math_extern.h"
46 #include "math_factory.h"
47 #include "math_kerninset.h"
48 #include "math_macro.h"
49 #include "math_macrotemplate.h"
50 #include "math_hullinset.h"
51 #include "math_parboxinset.h"
52 #include "math_parinset.h"
53 #include "math_rootinset.h"
54 #include "math_scriptinset.h"
55 #include "math_sizeinset.h"
56 #include "math_sqrtinset.h"
57 #include "math_stringinset.h"
58 #include "math_support.h"
59 #include "math_tabularinset.h"
60 #include "math_xyarrowinset.h"
62 //#include "insets/insetref.h"
63 #include "ref_inset.h"
67 #include "support/LAssert.h"
68 #include "support/lstrings.h"
87 MathInset::mode_type asMode(MathInset::mode_type oldmode, string const & str)
89 if (str == "mathmode")
90 return MathInset::MATH_MODE;
91 if (str == "textmode" || str == "forcetext")
92 return MathInset::TEXT_MODE;
97 bool stared(string const & s)
99 string::size_type const n = s.size();
100 return n && s[n - 1] == '*';
104 // These are TeX's catcodes
106 catEscape, // 0 backslash
116 catSpace, // 10 space
117 catLetter, // 11 a-zA-Z
118 catOther, // 12 none of the above
121 catInvalid // 15 <delete>
124 CatCode theCatcode[256];
127 inline CatCode catcode(unsigned char c)
129 return theCatcode[c];
134 FLAG_BRACE_LAST = 1 << 1, // last closing brace ends the parsing
135 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
136 FLAG_END = 1 << 3, // next \\end ends the parsing process
137 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
138 FLAG_TEXTMODE = 1 << 5, // we are in a box
139 FLAG_ITEM = 1 << 6, // read a (possibly braced token)
140 FLAG_LEAVE = 1 << 7, // leave the loop at the end
141 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
142 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
143 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
144 FLAG_OPTION = 1 << 11, // read [...] style option
145 FLAG_BRACED = 1 << 12 // read {...} style argument
151 fill(theCatcode, theCatcode + 256, catOther);
152 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
153 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
155 theCatcode['\\'] = catEscape;
156 theCatcode['{'] = catBegin;
157 theCatcode['}'] = catEnd;
158 theCatcode['$'] = catMath;
159 theCatcode['&'] = catAlign;
160 theCatcode['\n'] = catNewline;
161 theCatcode['#'] = catParameter;
162 theCatcode['^'] = catSuper;
163 theCatcode['_'] = catSub;
164 theCatcode['
\7f'] = catIgnore;
165 theCatcode[' '] = catSpace;
166 theCatcode['\t'] = catSpace;
167 theCatcode['\r'] = catNewline;
168 theCatcode['~'] = catActive;
169 theCatcode['%'] = catComment;
175 // Helper class for parsing
181 Token() : cs_(), char_(0), cat_(catIgnore) {}
183 Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
185 Token(string const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
188 string const & cs() const { return cs_; }
190 CatCode cat() const { return cat_; }
192 char character() const { return char_; }
194 string asString() const { return cs_.size() ? cs_ : string(1, char_); }
205 ostream & operator<<(ostream & os, Token const & t)
208 os << '\\' << t.cs();
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_ << '\n';
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 << "\n";
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 << "\n";
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) << "\n";
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_ << "\n";
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()
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()
550 if (nextToken().cat() == catBegin) {
551 Token t = getToken();
552 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
553 if (t.cat() == catBegin) {
555 res += '{' + parse_verbatim_item() + '}';
565 MathArray Parser::parse(unsigned flags, mode_type mode)
568 parse(ar, flags, mode);
573 void Parser::parse(MathArray & array, unsigned flags, mode_type mode)
575 MathGridInset grid(1, 1);
576 parse1(grid, flags, mode, false);
577 array = grid.cell(0);
581 void Parser::parse2(MathAtom & at, unsigned flags, mode_type mode,
584 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
588 void Parser::parse1(MathGridInset & grid, unsigned flags,
589 mode_type mode, bool numbered)
592 MathGridInset::row_type cellrow = 0;
593 MathGridInset::col_type cellcol = 0;
594 MathArray * cell = &grid.cell(grid.index(cellrow, cellcol));
596 if (grid.asHullInset())
597 grid.asHullInset()->numbered(cellrow, numbered);
600 //lyxerr << "grid: " << grid << endl;
603 Token const & t = getToken();
606 lyxerr << "t: " << t << " flags: " << flags << "\n";
611 if (flags & FLAG_ITEM) {
612 if (t.cat() == catSpace)
616 if (t.cat() == catBegin) {
617 // skip the brace and collect everything to the next matching
619 flags |= FLAG_BRACE_LAST;
623 // handle only this single token, leave the loop if done
628 if (flags & FLAG_BRACED) {
629 if (t.cat() == catSpace)
632 if (t.cat() != catBegin) {
633 error("opening brace expected");
637 // skip the brace and collect everything to the next matching
639 flags = FLAG_BRACE_LAST;
643 if (flags & FLAG_OPTION) {
644 if (t.cat() == catOther && t.character() == '[') {
646 parse(ar, FLAG_BRACK_LAST, mode);
649 // no option found, put back token and we are done
658 if (t.cat() == catMath) {
659 if (mode != MathInset::MATH_MODE) {
660 // we are inside some text mode thingy, so opening new math is allowed
661 Token const & n = getToken();
662 if (n.cat() == catMath) {
663 // TeX's $$...$$ syntax for displayed math
664 cell->push_back(MathAtom(new MathHullInset("equation")));
665 parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
666 getToken(); // skip the second '$' token
668 // simple $...$ stuff
670 cell->push_back(MathAtom(new MathHullInset("simple")));
671 parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
675 else if (flags & FLAG_SIMPLE) {
676 // this is the end of the formula
681 error("something strange in the parser\n");
686 else if (t.cat() == catLetter)
687 cell->push_back(MathAtom(new MathCharInset(t.character())));
689 else if (t.cat() == catSpace && mode != MathInset::MATH_MODE) {
690 if (cell->empty() || cell->back()->getChar() != ' ')
691 cell->push_back(MathAtom(new MathCharInset(t.character())));
694 else if (t.cat() == catNewline && mode != MathInset::MATH_MODE)
695 cell->push_back(MathAtom(new MathCharInset(t.character())));
697 else if (t.cat() == catParameter) {
698 Token const & n = getToken();
699 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
702 else if (t.cat() == catActive)
703 cell->push_back(MathAtom(new MathCharInset(t.character())));
705 else if (t.cat() == catBegin) {
707 parse(ar, FLAG_BRACE_LAST, mode);
708 // do not create a BraceInset if they were written by LyX
709 // this helps to keep the annoyance of "a choose b" to a minimum
710 if (ar.size() == 1 && ar[0]->extraBraces())
713 cell->push_back(MathAtom(new MathBraceInset(ar)));
716 else if (t.cat() == catEnd) {
717 if (flags & FLAG_BRACE_LAST)
719 error("found '}' unexpectedly");
721 //add(cell, '}', LM_TC_TEX);
724 else if (t.cat() == catAlign) {
726 //lyxerr << " column now " << cellcol << " max: " << grid.ncols() << "\n";
727 if (cellcol == grid.ncols()) {
728 //lyxerr << "adding column " << cellcol << "\n";
729 grid.addCol(cellcol - 1);
731 cell = &grid.cell(grid.index(cellrow, cellcol));
734 else if (t.cat() == catSuper || t.cat() == catSub) {
735 bool up = (t.cat() == catSuper);
736 // we need no new script inset if the last thing was a scriptinset,
737 // which has that script already not the same script already
739 cell->push_back(MathAtom(new MathScriptInset(up)));
740 else if (cell->back()->asScriptInset() &&
741 !cell->back()->asScriptInset()->has(up))
742 cell->back().nucleus()->asScriptInset()->ensure(up);
743 else if (cell->back()->asScriptInset())
744 cell->push_back(MathAtom(new MathScriptInset(up)));
746 cell->back() = MathAtom(new MathScriptInset(cell->back(), up));
747 MathScriptInset * p = cell->back().nucleus()->asScriptInset();
748 // special handling of {}-bases
749 // is this always correct?
750 // It appears that this is wrong (Dekel)
751 //if (p->nuc().size() == 1 && p->nuc().back()->asNestInset() &&
752 // p->nuc().back()->extraBraces())
753 // p->nuc() = p->nuc().back()->asNestInset()->cell(0);
754 parse(p->cell(up), FLAG_ITEM, mode);
761 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
762 //lyxerr << "finished reading option\n";
766 else if (t.cat() == catOther)
767 cell->push_back(MathAtom(new MathCharInset(t.character())));
769 else if (t.cat() == catComment) {
772 Token const & t = getToken();
773 if (t.cat() == catNewline)
777 cell->push_back(MathAtom(new MathCommentInset(s)));
785 else if (t.cs() == "lyxlock") {
787 cell->back().nucleus()->lock(true);
790 else if (t.cs() == "def" || t.cs() == "newcommand") {
793 if (t.cs() == "def") {
795 name = getToken().cs();
799 while (good() && nextToken().cat() != catBegin) {
800 pars += getToken().cs();
804 //lyxerr << "read \\def parameter list '" << pars << "'\n";
806 } else { // t.cs() == "newcommand"
808 if (getToken().cat() != catBegin) {
809 error("'{' in \\newcommand expected (1) \n");
813 name = getToken().cs();
815 if (getToken().cat() != catEnd) {
816 error("'}' in \\newcommand expected\n");
820 string arg = getArg('[', ']');
822 nargs = atoi(arg.c_str());
827 parse(ar1, FLAG_ITEM, MathInset::UNDECIDED_MODE);
829 // we cannot handle recursive stuff at all
831 //test.push_back(createMathInset(name));
832 //if (ar1.contains(test)) {
833 // error("we cannot handle recursive macros at all.\n");
837 // is a version for display attached?
840 if (nextToken().cat() == catBegin) {
841 parse(ar2, FLAG_ITEM, MathInset::MATH_MODE);
844 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, ar1, ar2)));
847 else if (t.cs() == "(") {
848 cell->push_back(MathAtom(new MathHullInset("simple")));
849 parse2(cell->back(), FLAG_SIMPLE2, MathInset::MATH_MODE, false);
852 else if (t.cs() == "[") {
853 cell->push_back(MathAtom(new MathHullInset("equation")));
854 parse2(cell->back(), FLAG_EQUATION, MathInset::MATH_MODE, false);
857 else if (t.cs() == "protect")
858 // ignore \\protect, will hopefully be re-added during output
861 else if (t.cs() == "end") {
862 if (flags & FLAG_END) {
863 // eat environment name
864 //string const name =
866 // FIXME: check that we ended the correct environment
869 error("found 'end' unexpectedly");
872 else if (t.cs() == ")") {
873 if (flags & FLAG_SIMPLE2)
875 error("found '\\)' unexpectedly");
878 else if (t.cs() == "]") {
879 if (flags & FLAG_EQUATION)
881 error("found '\\]' unexpectedly");
884 else if (t.cs() == "\\") {
885 grid.vcrskip(LyXLength(getArg('[', ']')), cellrow);
888 if (cellrow == grid.nrows())
889 grid.addRow(cellrow - 1);
890 if (grid.asHullInset())
891 grid.asHullInset()->numbered(cellrow, numbered);
892 cell = &grid.cell(grid.index(cellrow, cellcol));
896 else if (t.cs() == "multicolumn") {
897 // extract column count and insert dummy cells
899 parse(count, FLAG_ITEM, mode);
901 if (!extractNumber(count, cols)) {
902 lyxerr << " can't extract number of cells from " << count << "\n";
904 // resize the table if necessary
905 for (int i = 0; i < cols; ++i) {
907 if (cellcol == grid.ncols()) {
908 //lyxerr << "adding column " << cellcol << "\n";
909 grid.addCol(cellcol - 1);
911 cell = &grid.cell(grid.index(cellrow, cellcol));
912 // mark this as dummy
913 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = true;
915 // the last cell is the real thng, not a dummy
916 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
918 // read special alignment
920 parse(align, FLAG_ITEM, mode);
921 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
923 // parse the remaining contents into the "real" cell
924 parse(*cell, FLAG_ITEM, mode);
928 else if (t.cs() == "limits")
931 else if (t.cs() == "nolimits")
934 else if (t.cs() == "nonumber") {
935 if (grid.asHullInset())
936 grid.asHullInset()->numbered(cellrow, false);
939 else if (t.cs() == "number") {
940 if (grid.asHullInset())
941 grid.asHullInset()->numbered(cellrow, true);
944 else if (t.cs() == "hline") {
945 grid.rowinfo(cellrow).lines_ ++;
948 else if (t.cs() == "sqrt") {
950 parse(ar, FLAG_OPTION, mode);
952 cell->push_back(MathAtom(new MathRootInset));
953 cell->back().nucleus()->cell(0) = ar;
954 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
956 cell->push_back(MathAtom(new MathSqrtInset));
957 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
961 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
962 cell->push_back(createMathInset(t.cs()));
963 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
964 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
967 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
968 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
969 cell->push_back(MathAtom(new RefInset(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() == "left") {
975 string l = getToken().asString();
977 parse(ar, FLAG_RIGHT, mode);
978 string r = getToken().asString();
979 cell->push_back(MathAtom(new MathDelimInset(l, r, ar)));
982 else if (t.cs() == "right") {
983 if (flags & FLAG_RIGHT)
985 //lyxerr << "got so far: '" << cell << "'\n";
986 error("Unmatched right delimiter");
990 else if (t.cs() == "begin") {
991 string const name = getArg('{', '}');
993 if (name == "array" || name == "subarray") {
994 string const valign = parse_verbatim_option() + 'c';
995 string const halign = parse_verbatim_item();
996 cell->push_back(MathAtom(new MathArrayInset(name, valign[0], halign)));
997 parse2(cell->back(), FLAG_END, mode, false);
1000 if (name == "tabular") {
1001 string const valign = parse_verbatim_option() + 'c';
1002 string const halign = parse_verbatim_item();
1003 cell->push_back(MathAtom(new MathTabularInset(name, valign[0], halign)));
1004 parse2(cell->back(), FLAG_END, mode, false);
1007 else if (name == "split" || name == "cases" ||
1008 name == "gathered" || name == "aligned") {
1009 cell->push_back(createMathInset(name));
1010 parse2(cell->back(), FLAG_END, mode, false);
1013 else if (name == "math") {
1014 cell->push_back(MathAtom(new MathHullInset("simple")));
1015 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, true);
1018 else if (name == "equation" || name == "equation*"
1019 || name == "displaymath") {
1020 cell->push_back(MathAtom(new MathHullInset("equation")));
1021 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, (name == "equation"));
1024 else if (name == "eqnarray" || name == "eqnarray*") {
1025 cell->push_back(MathAtom(new MathHullInset("eqnarray")));
1026 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1029 else if (name == "align" || name == "align*") {
1030 cell->push_back(MathAtom(new MathHullInset("align")));
1031 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1034 else if (name == "flalign" || name == "flalign*") {
1035 cell->push_back(MathAtom(new MathHullInset("flalign")));
1036 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1039 else if (name == "alignat" || name == "alignat*") {
1040 // ignore this for a while
1042 cell->push_back(MathAtom(new MathHullInset("alignat")));
1043 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1046 else if (name == "xalignat" || name == "xalignat*") {
1047 // ignore this for a while
1049 cell->push_back(MathAtom(new MathHullInset("xalignat")));
1050 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1053 else if (name == "xxalignat") {
1054 // ignore this for a while
1056 cell->push_back(MathAtom(new MathHullInset("xxalignat")));
1057 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1060 else if (name == "multline" || name == "multline*") {
1061 cell->push_back(MathAtom(new MathHullInset("multline")));
1062 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1065 else if (name == "gather" || name == "gather*") {
1066 cell->push_back(MathAtom(new MathHullInset("gather")));
1067 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1070 else if (latexkeys const * l = in_word_set(name)) {
1071 if (l->inset == "matrix") {
1072 cell->push_back(createMathInset(name));
1073 parse2(cell->back(), FLAG_END, mode, false);
1078 // lyxerr << "unknow math inset begin '" << name << "'\n";
1079 // create generic environment inset
1080 cell->push_back(MathAtom(new MathEnvInset(name)));
1081 parse(cell->back().nucleus()->cell(0), FLAG_END, mode);
1085 else if (t.cs() == "kern") {
1086 #ifdef WITH_WARNINGS
1091 Token const & t = getToken();
1097 if (isValidLength(s))
1100 cell->push_back(MathAtom(new MathKernInset(s)));
1103 else if (t.cs() == "label") {
1104 string label = parse_verbatim_item();
1105 if (grid.asHullInset()) {
1106 grid.asHullInset()->label(cellrow, label);
1108 cell->push_back(createMathInset(t.cs()));
1109 cell->push_back(MathAtom(new MathBraceInset(asArray(label))));
1113 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1114 MathAtom at = createMathInset(t.cs());
1115 at.nucleus()->cell(0) = *cell;
1117 parse(at.nucleus()->cell(1), flags, mode);
1118 cell->push_back(at);
1122 else if (t.cs() == "substack") {
1123 cell->push_back(createMathInset(t.cs()));
1124 parse2(cell->back(), FLAG_ITEM, mode, false);
1127 else if (t.cs() == "xymatrix") {
1128 cell->push_back(createMathInset(t.cs()));
1129 parse2(cell->back(), FLAG_ITEM, mode, false);
1132 else if (t.cs() == "framebox") {
1133 cell->push_back(createMathInset(t.cs()));
1134 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, MathInset::TEXT_MODE);
1135 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, MathInset::TEXT_MODE);
1136 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, MathInset::TEXT_MODE);
1140 else if (t.cs() == "infer") {
1142 parse(ar, FLAG_OPTION, mode);
1143 cell->push_back(createMathInset(t.cs()));
1144 parse2(cell->back(), FLAG_ITEM, mode, false);
1148 else if (1 && t.cs() == "ar") {
1149 MathXYArrowInset * p = new MathXYArrowInset;
1150 // try to read target
1151 parse(p->cell(0), FLAG_OTPTION, mode);
1152 // try to read label
1153 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1154 p->up_ = nextToken().cat() == catSuper;
1156 parse(p->cell(1), FLAG_ITEM, mode);
1157 //lyxerr << "read label: " << p->cell(1) << "\n";
1160 cell->push_back(MathAtom(p));
1161 //lyxerr << "read cell: " << cell << "\n";
1165 else if (t.cs().size()) {
1166 latexkeys const * l = in_word_set(t.cs());
1168 if (l->inset == "font") {
1169 cell->push_back(createMathInset(t.cs()));
1170 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, asMode(mode, l->extra));
1173 else if (l->inset == "oldfont") {
1174 cell->push_back(createMathInset(t.cs()));
1175 parse(cell->back().nucleus()->cell(0), flags, asMode(mode, l->extra));
1179 else if (l->inset == "style") {
1180 cell->push_back(createMathInset(t.cs()));
1181 parse(cell->back().nucleus()->cell(0), flags, mode);
1185 else if (l->inset == "parbox") {
1186 // read optional positioning and width
1187 string pos = parse_verbatim_option();
1188 string width = parse_verbatim_item();
1189 cell->push_back(createMathInset(t.cs()));
1190 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, MathInset::TEXT_MODE);
1191 cell->back().nucleus()->asParboxInset()->setPosition(pos);
1192 cell->back().nucleus()->asParboxInset()->setWidth(width);
1196 MathAtom at = createMathInset(t.cs());
1197 for (MathInset::idx_type i = 0; i < at->nargs(); ++i)
1198 parse(at.nucleus()->cell(i), FLAG_ITEM, asMode(mode, l->extra));
1199 cell->push_back(at);
1204 MathAtom at = createMathInset(t.cs());
1205 MathInset::mode_type m = mode;
1206 if (m == MathInset::UNDECIDED_MODE)
1207 m = at->currentMode();
1208 MathInset::idx_type start = 0;
1209 // this fails on \bigg[...\bigg]
1211 //parse(opt, FLAG_OPTION, MathInset::VERBATIM_MODE);
1214 // at.nucleus()->cell(0) = opt;
1216 for (MathInset::idx_type i = start; i < at->nargs(); ++i)
1217 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1218 cell->push_back(at);
1223 if (flags & FLAG_LEAVE) {
1224 flags &= ~FLAG_LEAVE;
1232 } // anonymous namespace
1235 void mathed_parse_cell(MathArray & ar, string const & str)
1237 istringstream is(str.c_str());
1238 mathed_parse_cell(ar, is);
1242 void mathed_parse_cell(MathArray & ar, istream & is)
1244 Parser(is).parse(ar, 0, MathInset::MATH_MODE);
1248 bool mathed_parse_normal(MathAtom & t, string const & str)
1250 istringstream is(str.c_str());
1251 return Parser(is).parse(t);
1255 bool mathed_parse_normal(MathAtom & t, istream & is)
1257 return Parser(is).parse(t);
1261 bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
1263 return Parser(lex).parse(t);
1267 void mathed_parse_normal(MathGridInset & grid, string const & str)
1269 istringstream is(str.c_str());
1270 Parser(is).parse1(grid, 0, MathInset::MATH_MODE, false);