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"
59 #include "math_xyarrowinset.h"
61 //#include "insets/insetref.h"
62 #include "ref_inset.h"
67 #include "support/LAssert.h"
86 MathInset::mode_type asMode(MathInset::mode_type oldmode, string const & str)
88 //lyxerr << "handling mode: '" << str << "'\n";
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();
209 else if (t.cat() == catLetter)
212 os << '[' << t.character() << ',' << t.cat() << ']';
221 typedef MathInset::mode_type mode_type;
224 Parser(LyXLex & lex);
226 Parser(istream & is);
229 bool parse(MathAtom & at);
231 void parse(MathArray & array, unsigned flags, mode_type mode);
233 void parse1(MathGridInset & grid, unsigned flags, mode_type mode,
236 MathArray parse(unsigned flags, mode_type mode);
238 int lineno() const { return lineno_; }
244 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
245 /// get arg delimited by 'left' and 'right'
246 string getArg(char left, char right);
250 void error(string const & msg);
251 /// dump contents to screen
254 void tokenize(istream & is);
256 void tokenize(string const & s);
258 void skipSpaceTokens(istream & is, char c);
260 void push_back(Token const & t);
264 Token const & prevToken() const;
266 Token const & nextToken() const;
268 Token const & getToken();
269 /// skips spaces if any
272 void lex(string const & s);
276 string parse_verbatim_item();
278 string parse_verbatim_option();
283 vector<Token> tokens_;
289 Parser::Parser(LyXLex & lexer)
290 : lineno_(lexer.getLineNo()), pos_(0)
292 tokenize(lexer.getStream());
297 Parser::Parser(istream & is)
298 : lineno_(0), pos_(0)
304 void Parser::push_back(Token const & t)
306 tokens_.push_back(t);
310 void Parser::pop_back()
316 Token const & Parser::prevToken() const
318 static const Token dummy;
319 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
323 Token const & Parser::nextToken() const
325 static const Token dummy;
326 return good() ? tokens_[pos_] : dummy;
330 Token const & Parser::getToken()
332 static const Token dummy;
333 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << '\n';
334 return good() ? tokens_[pos_++] : dummy;
338 void Parser::skipSpaces()
340 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
345 void Parser::putback()
351 bool Parser::good() const
353 return pos_ < tokens_.size();
357 char Parser::getChar()
360 error("The input stream is not well...");
361 return tokens_[pos_++].character();
365 string Parser::getArg(char left, char right)
375 while ((c = getChar()) != right && good())
382 void Parser::skipSpaceTokens(istream & is, char c)
384 // skip trailing spaces
385 while (catcode(c) == catSpace || catcode(c) == catNewline)
388 //lyxerr << "putting back: " << c << "\n";
393 void Parser::tokenize(istream & is)
395 // eat everything up to the next \end_inset or end of stream
396 // and store it in s for further tokenization
401 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
402 s = s.substr(0, s.size() - 10);
406 // Remove the space after \end_inset
407 if (is.get(c) && c != ' ')
415 void Parser::tokenize(string const & buffer)
417 static bool init_done = false;
424 istringstream is(buffer.c_str(), ios::in | ios::binary);
428 //lyxerr << "reading c: " << c << "\n";
430 switch (catcode(c)) {
434 if (catcode(c) == catNewline)
435 ; //push_back(Token("par"));
437 push_back(Token('\n', catNewline));
445 while (is.get(c) && catcode(c) != catNewline)
455 error("unexpected end of input");
458 if (catcode(c) == catLetter) {
460 while (is.get(c) && catcode(c) == catLetter)
462 skipSpaceTokens(is, c);
471 push_back(Token(c, catcode(c)));
473 skipSpaceTokens(is, c);
478 lyxerr << "ignoring a char: " << int(c) << "\n";
483 push_back(Token(c, catcode(c)));
493 void Parser::dump() const
495 lyxerr << "\nTokens: ";
496 for (unsigned i = 0; i < tokens_.size(); ++i) {
499 lyxerr << tokens_[i];
501 lyxerr << " pos: " << pos_ << "\n";
505 void Parser::error(string const & msg)
507 lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
513 bool Parser::parse(MathAtom & at)
517 parse(ar, false, MathInset::UNDECIDED_MODE);
518 if (ar.size() != 1 || ar.front()->getType() == "none") {
519 lyxerr << "unusual contents found: " << ar << endl;
520 at = MathAtom(new MathParInset(ar));
521 //if (at->nargs() > 0)
522 // at.nucleus()->cell(0) = ar;
524 // lyxerr << "unusual contents found: " << ar << endl;
532 string Parser::parse_verbatim_option()
536 if (nextToken().character() == '[') {
537 Token t = getToken();
538 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
539 if (t.cat() == catBegin) {
541 res += '{' + parse_verbatim_item() + '}';
550 string Parser::parse_verbatim_item()
554 if (nextToken().cat() == catBegin) {
555 Token t = getToken();
556 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
557 if (t.cat() == catBegin) {
559 res += '{' + parse_verbatim_item() + '}';
569 MathArray Parser::parse(unsigned flags, mode_type mode)
572 parse(ar, flags, mode);
577 void Parser::parse(MathArray & array, unsigned flags, mode_type mode)
579 MathGridInset grid(1, 1);
580 parse1(grid, flags, mode, false);
581 array = grid.cell(0);
585 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
588 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
592 void Parser::parse1(MathGridInset & grid, unsigned flags,
593 const mode_type mode, const bool numbered)
596 MathGridInset::row_type cellrow = 0;
597 MathGridInset::col_type cellcol = 0;
598 MathArray * cell = &grid.cell(grid.index(cellrow, cellcol));
600 if (grid.asHullInset())
601 grid.asHullInset()->numbered(cellrow, numbered);
604 //lyxerr << " flags: " << flags << "\n";
605 //lyxerr << " mode: " << mode << "\n";
606 //lyxerr << "grid: " << grid << endl;
609 Token const & t = getToken();
612 lyxerr << "t: " << t << " flags: " << flags << "\n";
613 lyxerr << "mode: " << mode << "\n";
618 if (flags & FLAG_ITEM) {
620 if (t.cat() == catBegin) {
621 // skip the brace and collect everything to the next matching
623 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
627 // handle only this single token, leave the loop if done
632 if (flags & FLAG_BRACED) {
633 if (t.cat() == catSpace)
636 if (t.cat() != catBegin) {
637 error("opening brace expected");
641 // skip the brace and collect everything to the next matching
643 flags = FLAG_BRACE_LAST;
647 if (flags & FLAG_OPTION) {
648 if (t.cat() == catOther && t.character() == '[') {
650 parse(ar, FLAG_BRACK_LAST, mode);
653 // no option found, put back token and we are done
662 if (t.cat() == catMath) {
663 if (mode != MathInset::MATH_MODE) {
664 // we are inside some text mode thingy, so opening new math is allowed
665 Token const & n = getToken();
666 if (n.cat() == catMath) {
667 // TeX's $$...$$ syntax for displayed math
668 cell->push_back(MathAtom(new MathHullInset("equation")));
669 parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
670 getToken(); // skip the second '$' token
672 // simple $...$ stuff
674 cell->push_back(MathAtom(new MathHullInset("simple")));
675 parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
679 else if (flags & FLAG_SIMPLE) {
680 // this is the end of the formula
685 error("something strange in the parser\n");
690 else if (t.cat() == catLetter)
691 cell->push_back(MathAtom(new MathCharInset(t.character())));
693 else if (t.cat() == catSpace && mode != MathInset::MATH_MODE) {
694 if (cell->empty() || cell->back()->getChar() != ' ')
695 cell->push_back(MathAtom(new MathCharInset(t.character())));
698 else if (t.cat() == catNewline && mode != MathInset::MATH_MODE) {
699 if (cell->empty() || cell->back()->getChar() != ' ')
700 cell->push_back(MathAtom(new MathCharInset(' ')));
703 else if (t.cat() == catParameter) {
704 Token const & n = getToken();
705 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
708 else if (t.cat() == catActive)
709 cell->push_back(MathAtom(new MathCharInset(t.character())));
711 else if (t.cat() == catBegin) {
713 parse(ar, FLAG_BRACE_LAST, mode);
714 // do not create a BraceInset if they were written by LyX
715 // this helps to keep the annoyance of "a choose b" to a minimum
716 if (ar.size() == 1 && ar[0]->extraBraces())
719 cell->push_back(MathAtom(new MathBraceInset(ar)));
722 else if (t.cat() == catEnd) {
723 if (flags & FLAG_BRACE_LAST)
725 error("found '}' unexpectedly");
727 //add(cell, '}', LM_TC_TEX);
730 else if (t.cat() == catAlign) {
732 //lyxerr << " column now " << cellcol << " max: " << grid.ncols() << "\n";
733 if (cellcol == grid.ncols()) {
734 //lyxerr << "adding column " << cellcol << "\n";
735 grid.addCol(cellcol - 1);
737 cell = &grid.cell(grid.index(cellrow, cellcol));
740 else if (t.cat() == catSuper || t.cat() == catSub) {
741 bool up = (t.cat() == catSuper);
742 // we need no new script inset if the last thing was a scriptinset,
743 // which has that script already not the same script already
745 cell->push_back(MathAtom(new MathScriptInset(up)));
746 else if (cell->back()->asScriptInset() &&
747 !cell->back()->asScriptInset()->has(up))
748 cell->back().nucleus()->asScriptInset()->ensure(up);
749 else if (cell->back()->asScriptInset())
750 cell->push_back(MathAtom(new MathScriptInset(up)));
752 cell->back() = MathAtom(new MathScriptInset(cell->back(), up));
753 MathScriptInset * p = cell->back().nucleus()->asScriptInset();
754 // special handling of {}-bases
755 // is this always correct?
756 // It appears that this is wrong (Dekel)
757 //if (p->nuc().size() == 1 && p->nuc().back()->asNestInset() &&
758 // p->nuc().back()->extraBraces())
759 // p->nuc() = p->nuc().back()->asNestInset()->cell(0);
760 parse(p->cell(up), FLAG_ITEM, mode);
767 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
768 //lyxerr << "finished reading option\n";
772 else if (t.cat() == catOther)
773 cell->push_back(MathAtom(new MathCharInset(t.character())));
775 else if (t.cat() == catComment) {
778 Token const & t = getToken();
779 if (t.cat() == catNewline)
783 cell->push_back(MathAtom(new MathCommentInset(s)));
791 else if (t.cs() == "lyxlock") {
793 cell->back().nucleus()->lock(true);
796 else if (t.cs() == "def" || t.cs() == "newcommand") {
799 if (t.cs() == "def") {
801 name = getToken().cs();
805 while (good() && nextToken().cat() != catBegin) {
806 pars += getToken().cs();
810 //lyxerr << "read \\def parameter list '" << pars << "'\n";
812 } else { // t.cs() == "newcommand"
814 if (getToken().cat() != catBegin) {
815 error("'{' in \\newcommand expected (1) \n");
819 name = getToken().cs();
821 if (getToken().cat() != catEnd) {
822 error("'}' in \\newcommand expected\n");
826 string arg = getArg('[', ']');
828 nargs = atoi(arg.c_str());
833 parse(ar1, FLAG_ITEM, MathInset::UNDECIDED_MODE);
835 // we cannot handle recursive stuff at all
837 //test.push_back(createMathInset(name));
838 //if (ar1.contains(test)) {
839 // error("we cannot handle recursive macros at all.\n");
843 // is a version for display attached?
846 if (nextToken().cat() == catBegin)
847 parse(ar2, FLAG_ITEM, MathInset::MATH_MODE);
849 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, ar1, ar2)));
852 else if (t.cs() == "(") {
853 cell->push_back(MathAtom(new MathHullInset("simple")));
854 parse2(cell->back(), FLAG_SIMPLE2, MathInset::MATH_MODE, false);
857 else if (t.cs() == "[") {
858 cell->push_back(MathAtom(new MathHullInset("equation")));
859 parse2(cell->back(), FLAG_EQUATION, MathInset::MATH_MODE, false);
862 else if (t.cs() == "protect")
863 // ignore \\protect, will hopefully be re-added during output
866 else if (t.cs() == "end") {
867 if (flags & FLAG_END) {
868 // eat environment name
869 //string const name =
871 // FIXME: check that we ended the correct environment
874 error("found 'end' unexpectedly");
877 else if (t.cs() == ")") {
878 if (flags & FLAG_SIMPLE2)
880 error("found '\\)' unexpectedly");
883 else if (t.cs() == "]") {
884 if (flags & FLAG_EQUATION)
886 error("found '\\]' unexpectedly");
889 else if (t.cs() == "\\") {
890 grid.vcrskip(LyXLength(getArg('[', ']')), cellrow);
893 if (cellrow == grid.nrows())
894 grid.addRow(cellrow - 1);
895 if (grid.asHullInset())
896 grid.asHullInset()->numbered(cellrow, numbered);
897 cell = &grid.cell(grid.index(cellrow, cellcol));
901 else if (t.cs() == "multicolumn") {
902 // extract column count and insert dummy cells
904 parse(count, FLAG_ITEM, mode);
906 if (!extractNumber(count, cols)) {
907 lyxerr << " can't extract number of cells from " << count << "\n";
909 // resize the table if necessary
910 for (int i = 0; i < cols; ++i) {
912 if (cellcol == grid.ncols()) {
913 //lyxerr << "adding column " << cellcol << "\n";
914 grid.addCol(cellcol - 1);
916 cell = &grid.cell(grid.index(cellrow, cellcol));
917 // mark this as dummy
918 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = true;
920 // the last cell is the real thng, not a dummy
921 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
923 // read special alignment
925 parse(align, FLAG_ITEM, mode);
926 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
928 // parse the remaining contents into the "real" cell
929 parse(*cell, FLAG_ITEM, mode);
933 else if (t.cs() == "limits")
936 else if (t.cs() == "nolimits")
939 else if (t.cs() == "nonumber") {
940 if (grid.asHullInset())
941 grid.asHullInset()->numbered(cellrow, false);
944 else if (t.cs() == "number") {
945 if (grid.asHullInset())
946 grid.asHullInset()->numbered(cellrow, true);
949 else if (t.cs() == "hline") {
950 grid.rowinfo(cellrow).lines_ ++;
953 else if (t.cs() == "sqrt") {
955 parse(ar, FLAG_OPTION, mode);
957 cell->push_back(MathAtom(new MathRootInset));
958 cell->back().nucleus()->cell(0) = ar;
959 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
961 cell->push_back(MathAtom(new MathSqrtInset));
962 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
966 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
967 cell->push_back(createMathInset(t.cs()));
968 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
969 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
972 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
973 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
974 cell->push_back(MathAtom(new RefInset(t.cs())));
975 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
976 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
979 else if (t.cs() == "left") {
981 string l = getToken().asString();
983 parse(ar, FLAG_RIGHT, mode);
985 string r = getToken().asString();
986 cell->push_back(MathAtom(new MathDelimInset(l, r, ar)));
989 else if (t.cs() == "right") {
990 if (flags & FLAG_RIGHT)
992 //lyxerr << "got so far: '" << cell << "'\n";
993 error("Unmatched right delimiter");
997 else if (t.cs() == "begin") {
998 string const name = getArg('{', '}');
1000 if (name == "array" || name == "subarray") {
1001 string const valign = parse_verbatim_option() + 'c';
1002 string const halign = parse_verbatim_item();
1003 cell->push_back(MathAtom(new MathArrayInset(name, valign[0], halign)));
1004 parse2(cell->back(), FLAG_END, mode, false);
1007 else if (name == "tabular") {
1008 string const valign = parse_verbatim_option() + 'c';
1009 string const halign = parse_verbatim_item();
1010 cell->push_back(MathAtom(new MathTabularInset(name, valign[0], halign)));
1011 parse2(cell->back(), FLAG_END, MathInset::TEXT_MODE, false);
1014 else if (name == "split" || name == "cases" ||
1015 name == "gathered" || name == "aligned") {
1016 cell->push_back(createMathInset(name));
1017 parse2(cell->back(), FLAG_END, mode, false);
1020 else if (name == "math") {
1021 cell->push_back(MathAtom(new MathHullInset("simple")));
1022 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, true);
1025 else if (name == "equation" || name == "equation*"
1026 || name == "displaymath") {
1027 cell->push_back(MathAtom(new MathHullInset("equation")));
1028 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, (name == "equation"));
1031 else if (name == "eqnarray" || name == "eqnarray*") {
1032 cell->push_back(MathAtom(new MathHullInset("eqnarray")));
1033 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1036 else if (name == "align" || name == "align*") {
1037 cell->push_back(MathAtom(new MathHullInset("align")));
1038 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1041 else if (name == "flalign" || name == "flalign*") {
1042 cell->push_back(MathAtom(new MathHullInset("flalign")));
1043 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1046 else if (name == "alignat" || name == "alignat*") {
1047 // ignore this for a while
1049 cell->push_back(MathAtom(new MathHullInset("alignat")));
1050 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1053 else if (name == "xalignat" || name == "xalignat*") {
1054 // ignore this for a while
1056 cell->push_back(MathAtom(new MathHullInset("xalignat")));
1057 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1060 else if (name == "xxalignat") {
1061 // ignore this for a while
1063 cell->push_back(MathAtom(new MathHullInset("xxalignat")));
1064 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1067 else if (name == "multline" || name == "multline*") {
1068 cell->push_back(MathAtom(new MathHullInset("multline")));
1069 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1072 else if (name == "gather" || name == "gather*") {
1073 cell->push_back(MathAtom(new MathHullInset("gather")));
1074 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1077 else if (latexkeys const * l = in_word_set(name)) {
1078 if (l->inset == "matrix") {
1079 cell->push_back(createMathInset(name));
1080 parse2(cell->back(), FLAG_END, mode, false);
1086 lyxerr << "found unknown math environment '" << name << "'\n";
1087 // create generic environment inset
1088 cell->push_back(MathAtom(new MathEnvInset(name)));
1089 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1093 else if (t.cs() == "kern") {
1094 #ifdef WITH_WARNINGS
1099 Token const & t = getToken();
1105 if (isValidLength(s))
1108 cell->push_back(MathAtom(new MathKernInset(s)));
1111 else if (t.cs() == "label") {
1112 string label = parse_verbatim_item();
1113 if (grid.asHullInset()) {
1114 grid.asHullInset()->label(cellrow, label);
1116 cell->push_back(createMathInset(t.cs()));
1117 cell->push_back(MathAtom(new MathBraceInset(asArray(label))));
1121 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1122 MathAtom at = createMathInset(t.cs());
1123 at.nucleus()->cell(0) = *cell;
1125 parse(at.nucleus()->cell(1), flags, mode);
1126 cell->push_back(at);
1130 else if (t.cs() == "substack") {
1131 cell->push_back(createMathInset(t.cs()));
1132 parse2(cell->back(), FLAG_ITEM, mode, false);
1135 else if (t.cs() == "xymatrix") {
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) << "\n";
1168 cell->push_back(MathAtom(p));
1169 //lyxerr << "read cell: " << cell << "\n";
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 << "\n";
1216 if (at->currentMode() != MathInset::UNDECIDED_MODE)
1217 m = at->currentMode();
1218 //lyxerr << "default creation: m2: " << m << "\n";
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);