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_inset.h"
43 #include "math_arrayinset.h"
44 #include "math_braceinset.h"
45 #include "math_boxinset.h"
46 #include "math_charinset.h"
47 #include "math_commentinset.h"
48 #include "math_deliminset.h"
49 #include "math_envinset.h"
50 #include "math_extern.h"
51 #include "math_factory.h"
52 #include "math_kerninset.h"
53 #include "math_macro.h"
54 #include "math_macrotemplate.h"
55 #include "math_hullinset.h"
56 #include "math_parboxinset.h"
57 #include "math_parinset.h"
58 #include "math_rootinset.h"
59 #include "math_scriptinset.h"
60 #include "math_sizeinset.h"
61 #include "math_sqrtinset.h"
62 #include "math_stringinset.h"
63 #include "math_support.h"
64 #include "math_tabularinset.h"
66 //#include "insets/insetref.h"
67 #include "ref_inset.h"
72 #include "support/LAssert.h"
91 MathInset::mode_type asMode(MathInset::mode_type oldmode, string const & str)
93 //lyxerr << "handling mode: '" << str << "'" << endl;
94 if (str == "mathmode")
95 return MathInset::MATH_MODE;
96 if (str == "textmode" || str == "forcetext")
97 return MathInset::TEXT_MODE;
102 bool stared(string const & s)
104 string::size_type const n = s.size();
105 return n && s[n - 1] == '*';
109 // These are TeX's catcodes
111 catEscape, // 0 backslash
121 catSpace, // 10 space
122 catLetter, // 11 a-zA-Z
123 catOther, // 12 none of the above
126 catInvalid // 15 <delete>
129 CatCode theCatcode[256];
132 inline CatCode catcode(unsigned char c)
134 return theCatcode[c];
139 FLAG_BRACE_LAST = 1 << 1, // last closing brace ends the parsing
140 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
141 FLAG_END = 1 << 3, // next \\end ends the parsing process
142 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
143 FLAG_TEXTMODE = 1 << 5, // we are in a box
144 FLAG_ITEM = 1 << 6, // read a (possibly braced token)
145 FLAG_LEAVE = 1 << 7, // leave the loop at the end
146 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
147 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
148 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
149 FLAG_OPTION = 1 << 11, // read [...] style option
150 FLAG_BRACED = 1 << 12 // read {...} style argument
156 fill(theCatcode, theCatcode + 256, catOther);
157 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
158 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
160 theCatcode[int('\\')] = catEscape;
161 theCatcode[int('{')] = catBegin;
162 theCatcode[int('}')] = catEnd;
163 theCatcode[int('$')] = catMath;
164 theCatcode[int('&')] = catAlign;
165 theCatcode[int('\n')] = catNewline;
166 theCatcode[int('#')] = catParameter;
167 theCatcode[int('^')] = catSuper;
168 theCatcode[int('_')] = catSub;
169 theCatcode[int(0x7f)] = catIgnore;
170 theCatcode[int(' ')] = catSpace;
171 theCatcode[int('\t')] = catSpace;
172 theCatcode[int('\r')] = catNewline;
173 theCatcode[int('~')] = catActive;
174 theCatcode[int('%')] = catComment;
180 // Helper class for parsing
186 Token() : cs_(), char_(0), cat_(catIgnore) {}
188 Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
190 Token(string const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
193 string const & cs() const { return cs_; }
195 CatCode cat() const { return cat_; }
197 char character() const { return char_; }
199 string asString() const { return cs_.size() ? cs_ : string(1, char_); }
210 ostream & operator<<(ostream & os, Token const & t)
213 os << '\\' << t.cs();
214 else if (t.cat() == catLetter)
217 os << '[' << t.character() << ',' << t.cat() << ']';
226 typedef MathInset::mode_type mode_type;
229 Parser(LyXLex & lex);
231 Parser(istream & is);
234 bool parse(MathAtom & at);
236 void parse(MathArray & array, unsigned flags, mode_type mode);
238 void parse1(MathGridInset & grid, unsigned flags, mode_type mode,
241 MathArray parse(unsigned flags, mode_type mode);
243 int lineno() const { return lineno_; }
249 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
250 /// get arg delimited by 'left' and 'right'
251 string getArg(char left, char right);
255 void error(string const & msg);
256 /// dump contents to screen
259 void tokenize(istream & is);
261 void tokenize(string const & s);
263 void skipSpaceTokens(istream & is, char c);
265 void push_back(Token const & t);
269 Token const & prevToken() const;
271 Token const & nextToken() const;
273 Token const & getToken();
274 /// skips spaces if any
277 void lex(string const & s);
281 string parse_verbatim_item();
283 string parse_verbatim_option();
288 vector<Token> tokens_;
294 Parser::Parser(LyXLex & lexer)
295 : lineno_(lexer.getLineNo()), pos_(0)
297 tokenize(lexer.getStream());
302 Parser::Parser(istream & is)
303 : lineno_(0), pos_(0)
309 void Parser::push_back(Token const & t)
311 tokens_.push_back(t);
315 void Parser::pop_back()
321 Token const & Parser::prevToken() const
323 static const Token dummy;
324 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
328 Token const & Parser::nextToken() const
330 static const Token dummy;
331 return good() ? tokens_[pos_] : dummy;
335 Token const & Parser::getToken()
337 static const Token dummy;
338 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
339 return good() ? tokens_[pos_++] : dummy;
343 void Parser::skipSpaces()
345 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
350 void Parser::putback()
356 bool Parser::good() const
358 return pos_ < tokens_.size();
362 char Parser::getChar()
365 error("The input stream is not well...");
366 return tokens_[pos_++].character();
370 string Parser::getArg(char left, char right)
380 while ((c = getChar()) != right && good())
387 void Parser::skipSpaceTokens(istream & is, char c)
389 // skip trailing spaces
390 while (catcode(c) == catSpace || catcode(c) == catNewline)
393 //lyxerr << "putting back: " << c << endl;
398 void Parser::tokenize(istream & is)
400 // eat everything up to the next \end_inset or end of stream
401 // and store it in s for further tokenization
406 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
407 s = s.substr(0, s.size() - 10);
411 // Remove the space after \end_inset
412 if (is.get(c) && c != ' ')
420 void Parser::tokenize(string const & buffer)
422 static bool init_done = false;
429 istringstream is(buffer.c_str(), ios::in | ios::binary);
433 //lyxerr << "reading c: " << c << endl;
435 switch (catcode(c)) {
439 if (catcode(c) == catNewline)
440 ; //push_back(Token("par"));
442 push_back(Token('\n', catNewline));
450 while (is.get(c) && catcode(c) != catNewline)
460 error("unexpected end of input");
463 if (catcode(c) == catLetter) {
465 while (is.get(c) && catcode(c) == catLetter)
467 skipSpaceTokens(is, c);
476 push_back(Token(c, catcode(c)));
478 skipSpaceTokens(is, c);
483 lyxerr << "ignoring a char: " << int(c) << endl;
488 push_back(Token(c, catcode(c)));
498 void Parser::dump() const
500 lyxerr << "\nTokens: ";
501 for (unsigned i = 0; i < tokens_.size(); ++i) {
504 lyxerr << tokens_[i];
506 lyxerr << " pos: " << pos_ << endl;
510 void Parser::error(string const & msg)
512 lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
518 bool Parser::parse(MathAtom & at)
522 parse(ar, false, MathInset::UNDECIDED_MODE);
523 if (ar.size() != 1 || ar.front()->getType() == "none") {
524 lyxerr << "unusual contents found: " << ar << endl;
525 at = MathAtom(new MathParInset(ar));
526 //if (at->nargs() > 0)
527 // at.nucleus()->cell(0) = ar;
529 // lyxerr << "unusual contents found: " << ar << endl;
537 string Parser::parse_verbatim_option()
541 if (nextToken().character() == '[') {
542 Token t = getToken();
543 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
544 if (t.cat() == catBegin) {
546 res += '{' + parse_verbatim_item() + '}';
555 string Parser::parse_verbatim_item()
559 if (nextToken().cat() == catBegin) {
560 Token t = getToken();
561 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
562 if (t.cat() == catBegin) {
564 res += '{' + parse_verbatim_item() + '}';
574 MathArray Parser::parse(unsigned flags, mode_type mode)
577 parse(ar, flags, mode);
582 void Parser::parse(MathArray & array, unsigned flags, mode_type mode)
584 MathGridInset grid(1, 1);
585 parse1(grid, flags, mode, false);
586 array = grid.cell(0);
590 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
593 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
597 void Parser::parse1(MathGridInset & grid, unsigned flags,
598 const mode_type mode, const bool numbered)
601 MathGridInset::row_type cellrow = 0;
602 MathGridInset::col_type cellcol = 0;
603 MathArray * cell = &grid.cell(grid.index(cellrow, cellcol));
605 if (grid.asHullInset())
606 grid.asHullInset()->numbered(cellrow, numbered);
609 //lyxerr << " flags: " << flags << endl;
610 //lyxerr << " mode: " << mode << endl;
611 //lyxerr << "grid: " << grid << endl;
614 Token const & t = getToken();
617 lyxerr << "t: " << t << " flags: " << flags << endl;
618 lyxerr << "mode: " << mode << endl;
623 if (flags & FLAG_ITEM) {
625 if (t.cat() == catBegin) {
626 // skip the brace and collect everything to the next matching
628 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
632 // handle only this single token, leave the loop if done
637 if (flags & FLAG_BRACED) {
638 if (t.cat() == catSpace)
641 if (t.cat() != catBegin) {
642 error("opening brace expected");
646 // skip the brace and collect everything to the next matching
648 flags = FLAG_BRACE_LAST;
652 if (flags & FLAG_OPTION) {
653 if (t.cat() == catOther && t.character() == '[') {
655 parse(ar, FLAG_BRACK_LAST, mode);
658 // no option found, put back token and we are done
667 if (t.cat() == catMath) {
668 if (mode != MathInset::MATH_MODE) {
669 // we are inside some text mode thingy, so opening new math is allowed
670 Token const & n = getToken();
671 if (n.cat() == catMath) {
672 // TeX's $$...$$ syntax for displayed math
673 cell->push_back(MathAtom(new MathHullInset("equation")));
674 parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
675 getToken(); // skip the second '$' token
677 // simple $...$ stuff
679 cell->push_back(MathAtom(new MathHullInset("simple")));
680 parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
684 else if (flags & FLAG_SIMPLE) {
685 // this is the end of the formula
690 error("something strange in the parser");
695 else if (t.cat() == catLetter)
696 cell->push_back(MathAtom(new MathCharInset(t.character())));
698 else if (t.cat() == catSpace && mode != MathInset::MATH_MODE) {
699 if (cell->empty() || cell->back()->getChar() != ' ')
700 cell->push_back(MathAtom(new MathCharInset(t.character())));
703 else if (t.cat() == catNewline && mode != MathInset::MATH_MODE) {
704 if (cell->empty() || cell->back()->getChar() != ' ')
705 cell->push_back(MathAtom(new MathCharInset(' ')));
708 else if (t.cat() == catParameter) {
709 Token const & n = getToken();
710 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
713 else if (t.cat() == catActive)
714 cell->push_back(MathAtom(new MathCharInset(t.character())));
716 else if (t.cat() == catBegin) {
718 parse(ar, FLAG_BRACE_LAST, mode);
719 // do not create a BraceInset if they were written by LyX
720 // this helps to keep the annoyance of "a choose b" to a minimum
721 if (ar.size() == 1 && ar[0]->extraBraces())
724 cell->push_back(MathAtom(new MathBraceInset(ar)));
727 else if (t.cat() == catEnd) {
728 if (flags & FLAG_BRACE_LAST)
730 error("found '}' unexpectedly");
732 //add(cell, '}', LM_TC_TEX);
735 else if (t.cat() == catAlign) {
737 //lyxerr << " column now " << cellcol << " max: " << grid.ncols() << endl;
738 if (cellcol == grid.ncols()) {
739 //lyxerr << "adding column " << cellcol << endl;
740 grid.addCol(cellcol - 1);
742 cell = &grid.cell(grid.index(cellrow, cellcol));
745 else if (t.cat() == catSuper || t.cat() == catSub) {
746 bool up = (t.cat() == catSuper);
747 // we need no new script inset if the last thing was a scriptinset,
748 // which has that script already not the same script already
750 cell->push_back(MathAtom(new MathScriptInset(up)));
751 else if (cell->back()->asScriptInset() &&
752 !cell->back()->asScriptInset()->has(up))
753 cell->back().nucleus()->asScriptInset()->ensure(up);
754 else if (cell->back()->asScriptInset())
755 cell->push_back(MathAtom(new MathScriptInset(up)));
757 cell->back() = MathAtom(new MathScriptInset(cell->back(), up));
758 MathScriptInset * p = cell->back().nucleus()->asScriptInset();
759 // special handling of {}-bases
760 // is this always correct?
761 // It appears that this is wrong (Dekel)
762 //if (p->nuc().size() == 1 && p->nuc().back()->asNestInset() &&
763 // p->nuc().back()->extraBraces())
764 // p->nuc() = p->nuc().back()->asNestInset()->cell(0);
765 parse(p->cell(up), FLAG_ITEM, mode);
772 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
773 //lyxerr << "finished reading option" << endl;
777 else if (t.cat() == catOther)
778 cell->push_back(MathAtom(new MathCharInset(t.character())));
780 else if (t.cat() == catComment) {
783 Token const & t = getToken();
784 if (t.cat() == catNewline)
788 cell->push_back(MathAtom(new MathCommentInset(s)));
796 else if (t.cs() == "lyxlock") {
798 cell->back().nucleus()->lock(true);
801 else if (t.cs() == "def" ||
802 t.cs() == "newcommand" ||
803 t.cs() == "renewcommand")
805 string const type = t.cs();
808 if (t.cs() == "def") {
810 name = getToken().cs();
814 while (good() && nextToken().cat() != catBegin) {
815 pars += getToken().cs();
819 //lyxerr << "read \\def parameter list '" << pars << "'" << endl;
821 } else { // t.cs() == "newcommand" || t.cs() == "renewcommand"
823 if (getToken().cat() != catBegin) {
824 error("'{' in \\newcommand expected (1) ");
828 name = getToken().cs();
830 if (getToken().cat() != catEnd) {
831 error("'}' in \\newcommand expected");
835 string arg = getArg('[', ']');
837 nargs = atoi(arg.c_str());
842 parse(ar1, FLAG_ITEM, MathInset::UNDECIDED_MODE);
844 // we cannot handle recursive stuff at all
846 //test.push_back(createMathInset(name));
847 //if (ar1.contains(test)) {
848 // error("we cannot handle recursive macros at all.");
852 // is a version for display attached?
855 if (nextToken().cat() == catBegin)
856 parse(ar2, FLAG_ITEM, MathInset::MATH_MODE);
858 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, type,
862 else if (t.cs() == "(") {
863 cell->push_back(MathAtom(new MathHullInset("simple")));
864 parse2(cell->back(), FLAG_SIMPLE2, MathInset::MATH_MODE, false);
867 else if (t.cs() == "[") {
868 cell->push_back(MathAtom(new MathHullInset("equation")));
869 parse2(cell->back(), FLAG_EQUATION, MathInset::MATH_MODE, false);
872 else if (t.cs() == "protect")
873 // ignore \\protect, will hopefully be re-added during output
876 else if (t.cs() == "end") {
877 if (flags & FLAG_END) {
878 // eat environment name
879 //string const name =
881 // FIXME: check that we ended the correct environment
884 error("found 'end' unexpectedly");
887 else if (t.cs() == ")") {
888 if (flags & FLAG_SIMPLE2)
890 error("found '\\)' unexpectedly");
893 else if (t.cs() == "]") {
894 if (flags & FLAG_EQUATION)
896 error("found '\\]' unexpectedly");
899 else if (t.cs() == "\\") {
900 grid.vcrskip(LyXLength(getArg('[', ']')), cellrow);
903 if (cellrow == grid.nrows())
904 grid.addRow(cellrow - 1);
905 if (grid.asHullInset())
906 grid.asHullInset()->numbered(cellrow, numbered);
907 cell = &grid.cell(grid.index(cellrow, cellcol));
911 else if (t.cs() == "multicolumn") {
912 // extract column count and insert dummy cells
914 parse(count, FLAG_ITEM, mode);
916 if (!extractNumber(count, cols)) {
917 lyxerr << " can't extract number of cells from " << count << endl;
919 // resize the table if necessary
920 for (int i = 0; i < cols; ++i) {
922 if (cellcol == grid.ncols()) {
923 //lyxerr << "adding column " << cellcol << endl;
924 grid.addCol(cellcol - 1);
926 cell = &grid.cell(grid.index(cellrow, cellcol));
927 // mark this as dummy
928 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = true;
930 // the last cell is the real thng, not a dummy
931 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
933 // read special alignment
935 parse(align, FLAG_ITEM, mode);
936 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
938 // parse the remaining contents into the "real" cell
939 parse(*cell, FLAG_ITEM, mode);
943 else if (t.cs() == "limits")
946 else if (t.cs() == "nolimits")
949 else if (t.cs() == "nonumber") {
950 if (grid.asHullInset())
951 grid.asHullInset()->numbered(cellrow, false);
954 else if (t.cs() == "number") {
955 if (grid.asHullInset())
956 grid.asHullInset()->numbered(cellrow, true);
959 else if (t.cs() == "hline") {
960 grid.rowinfo(cellrow).lines_ ++;
963 else if (t.cs() == "sqrt") {
965 parse(ar, FLAG_OPTION, mode);
967 cell->push_back(MathAtom(new MathRootInset));
968 cell->back().nucleus()->cell(0) = ar;
969 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
971 cell->push_back(MathAtom(new MathSqrtInset));
972 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
976 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
977 cell->push_back(createMathInset(t.cs()));
978 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
979 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
982 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
983 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
984 cell->push_back(MathAtom(new RefInset(t.cs())));
985 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
986 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
989 else if (t.cs() == "left") {
991 string l = getToken().asString();
993 parse(ar, FLAG_RIGHT, mode);
995 string r = getToken().asString();
996 cell->push_back(MathAtom(new MathDelimInset(l, r, ar)));
999 else if (t.cs() == "right") {
1000 if (flags & FLAG_RIGHT)
1002 //lyxerr << "got so far: '" << cell << "'" << endl;
1003 error("Unmatched right delimiter");
1007 else if (t.cs() == "begin") {
1008 string const name = getArg('{', '}');
1010 if (name == "array" || name == "subarray") {
1011 string const valign = parse_verbatim_option() + 'c';
1012 string const halign = parse_verbatim_item();
1013 cell->push_back(MathAtom(new MathArrayInset(name, valign[0], halign)));
1014 parse2(cell->back(), FLAG_END, mode, false);
1017 else if (name == "tabular") {
1018 string const valign = parse_verbatim_option() + 'c';
1019 string const halign = parse_verbatim_item();
1020 cell->push_back(MathAtom(new MathTabularInset(name, valign[0], halign)));
1021 parse2(cell->back(), FLAG_END, MathInset::TEXT_MODE, false);
1024 else if (name == "split" || name == "cases" ||
1025 name == "gathered" || name == "aligned") {
1026 cell->push_back(createMathInset(name));
1027 parse2(cell->back(), FLAG_END, mode, false);
1030 else if (name == "math") {
1031 cell->push_back(MathAtom(new MathHullInset("simple")));
1032 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, true);
1035 else if (name == "equation" || name == "equation*"
1036 || name == "displaymath") {
1037 cell->push_back(MathAtom(new MathHullInset("equation")));
1038 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, (name == "equation"));
1041 else if (name == "eqnarray" || name == "eqnarray*") {
1042 cell->push_back(MathAtom(new MathHullInset("eqnarray")));
1043 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1046 else if (name == "align" || name == "align*") {
1047 cell->push_back(MathAtom(new MathHullInset("align")));
1048 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1051 else if (name == "flalign" || name == "flalign*") {
1052 cell->push_back(MathAtom(new MathHullInset("flalign")));
1053 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1056 else if (name == "alignat" || name == "alignat*") {
1057 // ignore this for a while
1059 cell->push_back(MathAtom(new MathHullInset("alignat")));
1060 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1063 else if (name == "xalignat" || name == "xalignat*") {
1064 // ignore this for a while
1066 cell->push_back(MathAtom(new MathHullInset("xalignat")));
1067 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1070 else if (name == "xxalignat") {
1071 // ignore this for a while
1073 cell->push_back(MathAtom(new MathHullInset("xxalignat")));
1074 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1077 else if (name == "multline" || name == "multline*") {
1078 cell->push_back(MathAtom(new MathHullInset("multline")));
1079 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1082 else if (name == "gather" || name == "gather*") {
1083 cell->push_back(MathAtom(new MathHullInset("gather")));
1084 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1087 else if (latexkeys const * l = in_word_set(name)) {
1088 if (l->inset == "matrix") {
1089 cell->push_back(createMathInset(name));
1090 parse2(cell->back(), FLAG_END, mode, false);
1096 lyxerr << "found unknown math environment '" << name << "'" << endl;
1097 // create generic environment inset
1098 cell->push_back(MathAtom(new MathEnvInset(name)));
1099 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1103 else if (t.cs() == "kern") {
1104 #ifdef WITH_WARNINGS
1109 Token const & t = getToken();
1115 if (isValidLength(s))
1118 cell->push_back(MathAtom(new MathKernInset(s)));
1121 else if (t.cs() == "label") {
1122 string label = parse_verbatim_item();
1125 if (grid.asHullInset()) {
1126 grid.asHullInset()->label(cellrow, label);
1128 cell->push_back(createMathInset(t.cs()));
1129 cell->push_back(MathAtom(new MathBraceInset(ar)));
1133 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1134 MathAtom at = createMathInset(t.cs());
1135 at.nucleus()->cell(0) = *cell;
1137 parse(at.nucleus()->cell(1), flags, mode);
1138 cell->push_back(at);
1142 else if (t.cs() == "substack") {
1143 cell->push_back(createMathInset(t.cs()));
1144 parse2(cell->back(), FLAG_ITEM, mode, false);
1147 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1148 cell->push_back(createMathInset(t.cs()));
1149 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, MathInset::TEXT_MODE);
1150 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, MathInset::TEXT_MODE);
1151 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, MathInset::TEXT_MODE);
1155 else if (t.cs() == "infer") {
1157 parse(ar, FLAG_OPTION, mode);
1158 cell->push_back(createMathInset(t.cs()));
1159 parse2(cell->back(), FLAG_ITEM, mode, false);
1163 else if (1 && t.cs() == "ar") {
1164 MathXYArrowInset * p = new MathXYArrowInset;
1165 // try to read target
1166 parse(p->cell(0), FLAG_OTPTION, mode);
1167 // try to read label
1168 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1169 p->up_ = nextToken().cat() == catSuper;
1171 parse(p->cell(1), FLAG_ITEM, mode);
1172 //lyxerr << "read label: " << p->cell(1) << endl;
1175 cell->push_back(MathAtom(p));
1176 //lyxerr << "read cell: " << cell << endl;
1180 else if (t.cs().size()) {
1181 latexkeys const * l = in_word_set(t.cs());
1183 if (l->inset == "font") {
1184 cell->push_back(createMathInset(t.cs()));
1185 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, asMode(mode, l->extra));
1188 else if (l->inset == "oldfont") {
1189 cell->push_back(createMathInset(t.cs()));
1190 parse(cell->back().nucleus()->cell(0), flags, asMode(mode, l->extra));
1194 else if (l->inset == "style") {
1195 cell->push_back(createMathInset(t.cs()));
1196 parse(cell->back().nucleus()->cell(0), flags, mode);
1200 else if (l->inset == "parbox") {
1201 // read optional positioning and width
1202 string pos = parse_verbatim_option();
1203 string width = parse_verbatim_item();
1204 cell->push_back(createMathInset(t.cs()));
1205 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, MathInset::TEXT_MODE);
1206 cell->back().nucleus()->asParboxInset()->setPosition(pos);
1207 cell->back().nucleus()->asParboxInset()->setWidth(width);
1211 MathAtom at = createMathInset(t.cs());
1212 for (MathInset::idx_type i = 0; i < at->nargs(); ++i)
1213 parse(at.nucleus()->cell(i), FLAG_ITEM, asMode(mode, l->extra));
1214 cell->push_back(at);
1219 MathAtom at = createMathInset(t.cs());
1220 MathInset::mode_type m = mode;
1221 //if (m == MathInset::UNDECIDED_MODE)
1222 //lyxerr << "default creation: m1: " << m << endl;
1223 if (at->currentMode() != MathInset::UNDECIDED_MODE)
1224 m = at->currentMode();
1225 //lyxerr << "default creation: m2: " << m << endl;
1226 MathInset::idx_type start = 0;
1227 // this fails on \bigg[...\bigg]
1229 //parse(opt, FLAG_OPTION, MathInset::VERBATIM_MODE);
1232 // at.nucleus()->cell(0) = opt;
1234 for (MathInset::idx_type i = start; i < at->nargs(); ++i)
1235 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1236 cell->push_back(at);
1241 if (flags & FLAG_LEAVE) {
1242 flags &= ~FLAG_LEAVE;
1250 } // anonymous namespace
1253 void mathed_parse_cell(MathArray & ar, string const & str)
1255 istringstream is(str.c_str());
1256 mathed_parse_cell(ar, is);
1260 void mathed_parse_cell(MathArray & ar, istream & is)
1262 Parser(is).parse(ar, 0, MathInset::MATH_MODE);
1266 bool mathed_parse_normal(MathAtom & t, string const & str)
1268 istringstream is(str.c_str());
1269 return Parser(is).parse(t);
1273 bool mathed_parse_normal(MathAtom & t, istream & is)
1275 return Parser(is).parse(t);
1279 bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
1281 return Parser(lex).parse(t);
1285 void mathed_parse_normal(MathGridInset & grid, string const & str)
1287 istringstream is(str.c_str());
1288 Parser(is).parse1(grid, 0, MathInset::MATH_MODE, false);