3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 If someone desperately needs partial "structures" (such as a few
14 cells of an array inset or similar) (s)he could uses the
15 following hack as starting point to write some macros:
20 \def\makeamptab{\catcode`\&=4\relax}
21 \def\makeampletter{\catcode`\&=11\relax}
22 \def\b{\makeampletter\expandafter\makeamptab\bi}
41 #include "math_parser.h"
42 #include "math_arrayinset.h"
43 #include "math_braceinset.h"
44 #include "math_charinset.h"
45 #include "math_commentinset.h"
46 #include "math_deliminset.h"
47 #include "math_envinset.h"
48 #include "math_factory.h"
49 #include "math_kerninset.h"
50 #include "math_macro.h"
51 #include "math_macroarg.h"
52 #include "math_macrotemplate.h"
53 #include "math_parinset.h"
54 #include "math_rootinset.h"
55 #include "math_scriptinset.h"
56 #include "math_sqrtinset.h"
57 #include "math_support.h"
58 #include "math_tabularinset.h"
60 //#include "insets/insetref.h"
61 #include "ref_inset.h"
65 #include "support/lyxlib.h"
69 using lyx::support::atoi;
77 using std::istringstream;
87 MathInset::mode_type asMode(MathInset::mode_type oldmode, string const & str)
89 //lyxerr << "handling mode: '" << str << "'" << endl;
90 if (str == "mathmode")
91 return MathInset::MATH_MODE;
92 if (str == "textmode" || str == "forcetext")
93 return MathInset::TEXT_MODE;
98 bool stared(string const & s)
100 string::size_type const n = s.size();
101 return n && s[n - 1] == '*';
106 * Add the row \p cellrow to \p grid.
107 * \returns wether the row could be added. Adding a row can fail for
108 * environments like "equation" that have a fixed number of rows.
110 bool addRow(MathGridInset & grid, MathGridInset::row_type & cellrow,
111 string const & vskip)
114 if (cellrow == grid.nrows()) {
115 //lyxerr << "adding row " << cellrow << endl;
116 grid.addRow(cellrow - 1);
117 if (cellrow == grid.nrows()) {
118 // We can't add a row to this grid, so let's
119 // append the content of this cell to the previous
121 // This does not happen in well formed .lyx files,
122 // but LyX versions 1.3.x and older could create
123 // such files and tex2lyx can still do that.
125 lyxerr << "ignoring extra row";
127 lyxerr << " with extra space " << vskip;
128 lyxerr << '.' << endl;
132 grid.vcrskip(LyXLength(vskip), cellrow - 1);
138 * Add the column \p cellcol to \p grid.
139 * \returns wether the column could be added. Adding a column can fail for
140 * environments like "eqnarray" that have a fixed number of columns.
142 bool addCol(MathGridInset & grid, MathGridInset::col_type & cellcol)
145 if (cellcol == grid.ncols()) {
146 //lyxerr << "adding column " << cellcol << endl;
147 grid.addCol(cellcol - 1);
148 if (cellcol == grid.ncols()) {
149 // We can't add a column to this grid, so let's
150 // append the content of this cell to the previous
152 // This does not happen in well formed .lyx files,
153 // but LyX versions 1.3.x and older could create
154 // such files and tex2lyx can still do that.
156 lyxerr << "ignoring extra column." << endl;
164 // These are TeX's catcodes
166 catEscape, // 0 backslash
176 catSpace, // 10 space
177 catLetter, // 11 a-zA-Z
178 catOther, // 12 none of the above
181 catInvalid // 15 <delete>
184 CatCode theCatcode[256];
187 inline CatCode catcode(unsigned char c)
189 return theCatcode[c];
194 FLAG_BRACE_LAST = 1 << 1, // last closing brace ends the parsing
195 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
196 FLAG_END = 1 << 3, // next \\end ends the parsing process
197 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
198 FLAG_TEXTMODE = 1 << 5, // we are in a box
199 FLAG_ITEM = 1 << 6, // read a (possibly braced token)
200 FLAG_LEAVE = 1 << 7, // leave the loop at the end
201 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
202 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
203 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
204 FLAG_OPTION = 1 << 11, // read [...] style option
205 FLAG_BRACED = 1 << 12 // read {...} style argument
210 // Helper class for parsing
216 Token() : cs_(), char_(0), cat_(catIgnore) {}
218 Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
220 Token(string const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
223 string const & cs() const { return cs_; }
225 CatCode cat() const { return cat_; }
227 char character() const { return char_; }
229 string asString() const { return cs_.size() ? cs_ : string(1, char_); }
240 ostream & operator<<(ostream & os, Token const & t)
243 os << '\\' << t.cs();
244 else if (t.cat() == catLetter)
247 os << '[' << t.character() << ',' << t.cat() << ']';
255 typedef MathInset::mode_type mode_type;
258 Parser(LyXLex & lex);
260 Parser(istream & is);
263 bool parse(MathAtom & at);
265 void parse(MathArray & array, unsigned flags, mode_type mode);
267 void parse1(MathGridInset & grid, unsigned flags, mode_type mode,
270 MathArray parse(unsigned flags, mode_type mode);
272 int lineno() const { return lineno_; }
278 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
279 /// get arg delimited by 'left' and 'right'
280 string getArg(char left, char right);
284 void error(string const & msg);
285 /// dump contents to screen
288 void tokenize(istream & is);
290 void tokenize(string const & s);
292 void skipSpaceTokens(istream & is, char c);
294 void push_back(Token const & t);
298 Token const & prevToken() const;
300 Token const & nextToken() const;
302 Token const & getToken();
303 /// skips spaces if any
306 void lex(string const & s);
310 string parse_verbatim_item();
312 string parse_verbatim_option();
317 vector<Token> tokens_;
323 Parser::Parser(LyXLex & lexer)
324 : lineno_(lexer.getLineNo()), pos_(0)
326 tokenize(lexer.getStream());
331 Parser::Parser(istream & is)
332 : lineno_(0), pos_(0)
338 void Parser::push_back(Token const & t)
340 tokens_.push_back(t);
344 void Parser::pop_back()
350 Token const & Parser::prevToken() const
352 static const Token dummy;
353 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
357 Token const & Parser::nextToken() const
359 static const Token dummy;
360 return good() ? tokens_[pos_] : dummy;
364 Token const & Parser::getToken()
366 static const Token dummy;
367 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
368 return good() ? tokens_[pos_++] : dummy;
372 void Parser::skipSpaces()
374 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
379 void Parser::putback()
385 bool Parser::good() const
387 return pos_ < tokens_.size();
391 char Parser::getChar()
394 error("The input stream is not well...");
395 return tokens_[pos_++].character();
399 string Parser::getArg(char left, char right)
409 while ((c = getChar()) != right && good())
416 void Parser::skipSpaceTokens(istream & is, char c)
418 // skip trailing spaces
419 while (catcode(c) == catSpace || catcode(c) == catNewline)
422 //lyxerr << "putting back: " << c << endl;
427 void Parser::tokenize(istream & is)
429 // eat everything up to the next \end_inset or end of stream
430 // and store it in s for further tokenization
435 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
436 s = s.substr(0, s.size() - 10);
440 // Remove the space after \end_inset
441 if (is.get(c) && c != ' ')
449 void Parser::tokenize(string const & buffer)
451 istringstream is(buffer, ios::in | ios::binary);
455 //lyxerr << "reading c: " << c << endl;
457 switch (catcode(c)) {
461 if (catcode(c) == catNewline)
462 ; //push_back(Token("par"));
464 push_back(Token('\n', catNewline));
472 while (is.get(c) && catcode(c) != catNewline)
482 error("unexpected end of input");
485 if (catcode(c) == catLetter) {
487 while (is.get(c) && catcode(c) == catLetter)
489 skipSpaceTokens(is, c);
498 push_back(Token(c, catcode(c)));
500 skipSpaceTokens(is, c);
505 lyxerr << "ignoring a char: " << int(c) << endl;
510 push_back(Token(c, catcode(c)));
520 void Parser::dump() const
522 lyxerr << "\nTokens: ";
523 for (unsigned i = 0; i < tokens_.size(); ++i) {
526 lyxerr << tokens_[i];
528 lyxerr << " pos: " << pos_ << endl;
532 void Parser::error(string const & msg)
534 lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
540 bool Parser::parse(MathAtom & at)
544 parse(ar, false, MathInset::UNDECIDED_MODE);
545 if (ar.size() != 1 || ar.front()->getType() == "none") {
546 lyxerr << "unusual contents found: " << ar << endl;
547 at = MathAtom(new MathParInset(ar));
548 //if (at->nargs() > 0)
549 // at.nucleus()->cell(0) = ar;
551 // lyxerr << "unusual contents found: " << ar << endl;
559 string Parser::parse_verbatim_option()
563 if (nextToken().character() == '[') {
564 Token t = getToken();
565 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
566 if (t.cat() == catBegin) {
568 res += '{' + parse_verbatim_item() + '}';
577 string Parser::parse_verbatim_item()
581 if (nextToken().cat() == catBegin) {
582 Token t = getToken();
583 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
584 if (t.cat() == catBegin) {
586 res += '{' + parse_verbatim_item() + '}';
596 MathArray Parser::parse(unsigned flags, mode_type mode)
599 parse(ar, flags, mode);
604 void Parser::parse(MathArray & array, unsigned flags, mode_type mode)
606 MathGridInset grid(1, 1);
607 parse1(grid, flags, mode, false);
608 array = grid.cell(0);
612 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
615 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
619 void Parser::parse1(MathGridInset & grid, unsigned flags,
620 const mode_type mode, const bool numbered)
623 MathGridInset::row_type cellrow = 0;
624 MathGridInset::col_type cellcol = 0;
625 MathArray * cell = &grid.cell(grid.index(cellrow, cellcol));
627 if (grid.asHullInset())
628 grid.asHullInset()->numbered(cellrow, numbered);
631 //lyxerr << " flags: " << flags << endl;
632 //lyxerr << " mode: " << mode << endl;
633 //lyxerr << "grid: " << grid << endl;
636 Token const & t = getToken();
639 lyxerr << "t: " << t << " flags: " << flags << endl;
640 lyxerr << "mode: " << mode << endl;
645 if (flags & FLAG_ITEM) {
647 if (t.cat() == catBegin) {
648 // skip the brace and collect everything to the next matching
650 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
654 // handle only this single token, leave the loop if done
659 if (flags & FLAG_BRACED) {
660 if (t.cat() == catSpace)
663 if (t.cat() != catBegin) {
664 error("opening brace expected");
668 // skip the brace and collect everything to the next matching
670 flags = FLAG_BRACE_LAST;
674 if (flags & FLAG_OPTION) {
675 if (t.cat() == catOther && t.character() == '[') {
677 parse(ar, FLAG_BRACK_LAST, mode);
680 // no option found, put back token and we are done
689 if (t.cat() == catMath) {
690 if (mode != MathInset::MATH_MODE) {
691 // we are inside some text mode thingy, so opening new math is allowed
692 Token const & n = getToken();
693 if (n.cat() == catMath) {
694 // TeX's $$...$$ syntax for displayed math
695 cell->push_back(MathAtom(new MathHullInset("equation")));
696 parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
697 getToken(); // skip the second '$' token
699 // simple $...$ stuff
701 cell->push_back(MathAtom(new MathHullInset("simple")));
702 parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
706 else if (flags & FLAG_SIMPLE) {
707 // this is the end of the formula
712 error("something strange in the parser");
717 else if (t.cat() == catLetter)
718 cell->push_back(MathAtom(new MathCharInset(t.character())));
720 else if (t.cat() == catSpace && mode != MathInset::MATH_MODE) {
721 if (cell->empty() || cell->back()->getChar() != ' ')
722 cell->push_back(MathAtom(new MathCharInset(t.character())));
725 else if (t.cat() == catNewline && mode != MathInset::MATH_MODE) {
726 if (cell->empty() || cell->back()->getChar() != ' ')
727 cell->push_back(MathAtom(new MathCharInset(' ')));
730 else if (t.cat() == catParameter) {
731 Token const & n = getToken();
732 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
735 else if (t.cat() == catActive)
736 cell->push_back(MathAtom(new MathCharInset(t.character())));
738 else if (t.cat() == catBegin) {
740 parse(ar, FLAG_BRACE_LAST, mode);
741 // do not create a BraceInset if they were written by LyX
742 // this helps to keep the annoyance of "a choose b" to a minimum
743 if (ar.size() == 1 && ar[0]->extraBraces())
746 cell->push_back(MathAtom(new MathBraceInset(ar)));
749 else if (t.cat() == catEnd) {
750 if (flags & FLAG_BRACE_LAST)
752 error("found '}' unexpectedly");
753 //BOOST_ASSERT(false);
754 //add(cell, '}', LM_TC_TEX);
757 else if (t.cat() == catAlign) {
758 //lyxerr << " column now " << (cellcol + 1)
759 // << " max: " << grid.ncols() << endl;
760 if (addCol(grid, cellcol))
761 cell = &grid.cell(grid.index(cellrow,
765 else if (t.cat() == catSuper || t.cat() == catSub) {
766 bool up = (t.cat() == catSuper);
767 // we need no new script inset if the last thing was a scriptinset,
768 // which has that script already not the same script already
770 cell->push_back(MathAtom(new MathScriptInset(up)));
771 else if (cell->back()->asScriptInset() &&
772 !cell->back()->asScriptInset()->has(up))
773 cell->back().nucleus()->asScriptInset()->ensure(up);
774 else if (cell->back()->asScriptInset())
775 cell->push_back(MathAtom(new MathScriptInset(up)));
777 cell->back() = MathAtom(new MathScriptInset(cell->back(), up));
778 MathScriptInset * p = cell->back().nucleus()->asScriptInset();
779 // special handling of {}-bases
780 // is this always correct?
781 // It appears that this is wrong (Dekel)
782 //if (p->nuc().size() == 1 && p->nuc().back()->asNestInset() &&
783 // p->nuc().back()->extraBraces())
784 // p->nuc() = p->nuc().back()->asNestInset()->cell(0);
785 parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
792 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
793 //lyxerr << "finished reading option" << endl;
797 else if (t.cat() == catOther)
798 cell->push_back(MathAtom(new MathCharInset(t.character())));
800 else if (t.cat() == catComment) {
803 Token const & t = getToken();
804 if (t.cat() == catNewline)
808 cell->push_back(MathAtom(new MathCommentInset(s)));
816 else if (t.cs() == "lyxlock") {
818 cell->back().nucleus()->lock(true);
821 else if (t.cs() == "def" ||
822 t.cs() == "newcommand" ||
823 t.cs() == "renewcommand")
825 string const type = t.cs();
828 if (t.cs() == "def") {
830 name = getToken().cs();
834 while (good() && nextToken().cat() != catBegin) {
835 pars += getToken().cs();
839 //lyxerr << "read \\def parameter list '" << pars << "'" << endl;
841 } else { // t.cs() == "newcommand" || t.cs() == "renewcommand"
843 if (getToken().cat() != catBegin) {
844 error("'{' in \\newcommand expected (1) ");
848 name = getToken().cs();
850 if (getToken().cat() != catEnd) {
851 error("'}' in \\newcommand expected");
855 string arg = getArg('[', ']');
862 parse(ar1, FLAG_ITEM, MathInset::UNDECIDED_MODE);
864 // we cannot handle recursive stuff at all
866 //test.push_back(createMathInset(name));
867 //if (ar1.contains(test)) {
868 // error("we cannot handle recursive macros at all.");
872 // is a version for display attached?
875 if (nextToken().cat() == catBegin)
876 parse(ar2, FLAG_ITEM, MathInset::MATH_MODE);
878 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, type,
882 else if (t.cs() == "(") {
883 cell->push_back(MathAtom(new MathHullInset("simple")));
884 parse2(cell->back(), FLAG_SIMPLE2, MathInset::MATH_MODE, false);
887 else if (t.cs() == "[") {
888 cell->push_back(MathAtom(new MathHullInset("equation")));
889 parse2(cell->back(), FLAG_EQUATION, MathInset::MATH_MODE, false);
892 else if (t.cs() == "protect")
893 // ignore \\protect, will hopefully be re-added during output
896 else if (t.cs() == "end") {
897 if (flags & FLAG_END) {
898 // eat environment name
899 //string const name =
901 // FIXME: check that we ended the correct environment
904 error("found 'end' unexpectedly");
907 else if (t.cs() == ")") {
908 if (flags & FLAG_SIMPLE2)
910 error("found '\\)' unexpectedly");
913 else if (t.cs() == "]") {
914 if (flags & FLAG_EQUATION)
916 error("found '\\]' unexpectedly");
919 else if (t.cs() == "\\") {
920 if (addRow(grid, cellrow, getArg('[', ']'))) {
922 if (grid.asHullInset())
923 grid.asHullInset()->numbered(
925 cell = &grid.cell(grid.index(cellrow,
931 else if (t.cs() == "multicolumn") {
932 // extract column count and insert dummy cells
934 parse(count, FLAG_ITEM, mode);
936 if (!extractNumber(count, cols)) {
937 lyxerr << " can't extract number of cells from " << count << endl;
939 // resize the table if necessary
940 for (int i = 0; i < cols; ++i) {
941 if (addCol(grid, cellcol)) {
942 cell = &grid.cell(grid.index(
944 // mark this as dummy
945 grid.cellinfo(grid.index(
946 cellrow, cellcol)).dummy_ = true;
949 // the last cell is the real thing, not a dummy
950 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
952 // read special alignment
954 parse(align, FLAG_ITEM, mode);
955 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
957 // parse the remaining contents into the "real" cell
958 parse(*cell, FLAG_ITEM, mode);
962 else if (t.cs() == "limits")
965 else if (t.cs() == "nolimits")
968 else if (t.cs() == "nonumber") {
969 if (grid.asHullInset())
970 grid.asHullInset()->numbered(cellrow, false);
973 else if (t.cs() == "number") {
974 if (grid.asHullInset())
975 grid.asHullInset()->numbered(cellrow, true);
978 else if (t.cs() == "hline") {
979 grid.rowinfo(cellrow).lines_ ++;
982 else if (t.cs() == "sqrt") {
984 parse(ar, FLAG_OPTION, mode);
986 cell->push_back(MathAtom(new MathRootInset));
987 cell->back().nucleus()->cell(0) = ar;
988 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
990 cell->push_back(MathAtom(new MathSqrtInset));
991 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
995 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
996 cell->push_back(createMathInset(t.cs()));
997 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
998 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1001 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
1002 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1003 cell->push_back(MathAtom(new RefInset(t.cs())));
1004 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1005 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1008 else if (t.cs() == "left") {
1010 string l = getToken().asString();
1012 parse(ar, FLAG_RIGHT, mode);
1014 string r = getToken().asString();
1015 cell->push_back(MathAtom(new MathDelimInset(l, r, ar)));
1018 else if (t.cs() == "right") {
1019 if (flags & FLAG_RIGHT)
1021 //lyxerr << "got so far: '" << cell << "'" << endl;
1022 error("Unmatched right delimiter");
1026 else if (t.cs() == "begin") {
1027 string const name = getArg('{', '}');
1029 if (name == "array" || name == "subarray") {
1030 string const valign = parse_verbatim_option() + 'c';
1031 string const halign = parse_verbatim_item();
1032 cell->push_back(MathAtom(new MathArrayInset(name, valign[0], halign)));
1033 parse2(cell->back(), FLAG_END, mode, false);
1036 else if (name == "tabular") {
1037 string const valign = parse_verbatim_option() + 'c';
1038 string const halign = parse_verbatim_item();
1039 cell->push_back(MathAtom(new MathTabularInset(name, valign[0], halign)));
1040 parse2(cell->back(), FLAG_END, MathInset::TEXT_MODE, false);
1043 else if (name == "split" || name == "cases" ||
1044 name == "gathered" || name == "aligned" ||
1045 name == "alignedat") {
1046 cell->push_back(createMathInset(name));
1047 parse2(cell->back(), FLAG_END, mode, false);
1050 else if (name == "math") {
1051 cell->push_back(MathAtom(new MathHullInset("simple")));
1052 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, true);
1055 else if (name == "equation" || name == "equation*"
1056 || name == "displaymath") {
1057 cell->push_back(MathAtom(new MathHullInset("equation")));
1058 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, (name == "equation"));
1061 else if (name == "eqnarray" || name == "eqnarray*") {
1062 cell->push_back(MathAtom(new MathHullInset("eqnarray")));
1063 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1066 else if (name == "align" || name == "align*") {
1067 cell->push_back(MathAtom(new MathHullInset("align")));
1068 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1071 else if (name == "flalign" || name == "flalign*") {
1072 cell->push_back(MathAtom(new MathHullInset("flalign")));
1073 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1076 else if (name == "alignat" || name == "alignat*") {
1077 // ignore this for a while
1079 cell->push_back(MathAtom(new MathHullInset("alignat")));
1080 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1083 else if (name == "xalignat" || name == "xalignat*") {
1084 // ignore this for a while
1086 cell->push_back(MathAtom(new MathHullInset("xalignat")));
1087 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1090 else if (name == "xxalignat") {
1091 // ignore this for a while
1093 cell->push_back(MathAtom(new MathHullInset("xxalignat")));
1094 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1097 else if (name == "multline" || name == "multline*") {
1098 cell->push_back(MathAtom(new MathHullInset("multline")));
1099 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1102 else if (name == "gather" || name == "gather*") {
1103 cell->push_back(MathAtom(new MathHullInset("gather")));
1104 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1107 else if (latexkeys const * l = in_word_set(name)) {
1108 if (l->inset == "matrix") {
1109 cell->push_back(createMathInset(name));
1110 parse2(cell->back(), FLAG_END, mode, false);
1116 lyxerr << "found unknown math environment '" << name << "'" << endl;
1117 // create generic environment inset
1118 cell->push_back(MathAtom(new MathEnvInset(name)));
1119 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1123 else if (t.cs() == "kern") {
1124 #ifdef WITH_WARNINGS
1129 Token const & t = getToken();
1135 if (isValidLength(s))
1138 cell->push_back(MathAtom(new MathKernInset(s)));
1141 else if (t.cs() == "label") {
1142 string label = parse_verbatim_item();
1145 if (grid.asHullInset()) {
1146 grid.asHullInset()->label(cellrow, label);
1148 cell->push_back(createMathInset(t.cs()));
1149 cell->push_back(MathAtom(new MathBraceInset(ar)));
1153 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1154 MathAtom at = createMathInset(t.cs());
1155 at.nucleus()->cell(0) = *cell;
1157 parse(at.nucleus()->cell(1), flags, mode);
1158 cell->push_back(at);
1162 else if (t.cs() == "color") {
1163 MathAtom at = createMathInset(t.cs());
1164 parse(at.nucleus()->cell(0), FLAG_ITEM, MathInset::TEXT_MODE);
1165 parse(at.nucleus()->cell(1), flags, mode);
1166 cell->push_back(at);
1170 else if (t.cs() == "substack") {
1171 cell->push_back(createMathInset(t.cs()));
1172 parse2(cell->back(), FLAG_ITEM, mode, false);
1175 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1176 cell->push_back(createMathInset(t.cs()));
1177 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, MathInset::TEXT_MODE);
1178 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, MathInset::TEXT_MODE);
1179 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, MathInset::TEXT_MODE);
1183 else if (t.cs() == "infer") {
1185 parse(ar, FLAG_OPTION, mode);
1186 cell->push_back(createMathInset(t.cs()));
1187 parse2(cell->back(), FLAG_ITEM, mode, false);
1191 else if (1 && t.cs() == "ar") {
1192 auto_ptr<MathXYArrowInset> p(new MathXYArrowInset);
1193 // try to read target
1194 parse(p->cell(0), FLAG_OTPTION, mode);
1195 // try to read label
1196 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1197 p->up_ = nextToken().cat() == catSuper;
1199 parse(p->cell(1), FLAG_ITEM, mode);
1200 //lyxerr << "read label: " << p->cell(1) << endl;
1203 cell->push_back(MathAtom(p.release()));
1204 //lyxerr << "read cell: " << cell << endl;
1208 else if (t.cs().size()) {
1209 latexkeys const * l = in_word_set(t.cs());
1211 if (l->inset == "font") {
1212 cell->push_back(createMathInset(t.cs()));
1213 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, asMode(mode, l->extra));
1216 else if (l->inset == "oldfont") {
1217 cell->push_back(createMathInset(t.cs()));
1218 parse(cell->back().nucleus()->cell(0), flags, asMode(mode, l->extra));
1222 else if (l->inset == "style") {
1223 cell->push_back(createMathInset(t.cs()));
1224 parse(cell->back().nucleus()->cell(0), flags, mode);
1229 MathAtom at = createMathInset(t.cs());
1230 for (MathInset::idx_type i = 0; i < at->nargs(); ++i)
1231 parse(at.nucleus()->cell(i), FLAG_ITEM, asMode(mode, l->extra));
1232 cell->push_back(at);
1237 MathAtom at = createMathInset(t.cs());
1238 MathInset::mode_type m = mode;
1239 //if (m == MathInset::UNDECIDED_MODE)
1240 //lyxerr << "default creation: m1: " << m << endl;
1241 if (at->currentMode() != MathInset::UNDECIDED_MODE)
1242 m = at->currentMode();
1243 //lyxerr << "default creation: m2: " << m << endl;
1244 MathInset::idx_type start = 0;
1245 // this fails on \bigg[...\bigg]
1247 //parse(opt, FLAG_OPTION, MathInset::VERBATIM_MODE);
1250 // at.nucleus()->cell(0) = opt;
1252 for (MathInset::idx_type i = start; i < at->nargs(); ++i) {
1253 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1256 cell->push_back(at);
1261 if (flags & FLAG_LEAVE) {
1262 flags &= ~FLAG_LEAVE;
1270 } // anonymous namespace
1273 void mathed_parse_cell(MathArray & ar, string const & str)
1275 istringstream is(str);
1276 mathed_parse_cell(ar, is);
1280 void mathed_parse_cell(MathArray & ar, istream & is)
1282 Parser(is).parse(ar, 0, MathInset::MATH_MODE);
1286 bool mathed_parse_normal(MathAtom & t, string const & str)
1288 istringstream is(str);
1289 return Parser(is).parse(t);
1293 bool mathed_parse_normal(MathAtom & t, istream & is)
1295 return Parser(is).parse(t);
1299 bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
1301 return Parser(lex).parse(t);
1305 void mathed_parse_normal(MathGridInset & grid, string const & str)
1307 istringstream is(str);
1308 Parser(is).parse1(grid, 0, MathInset::MATH_MODE, false);
1314 fill(theCatcode, theCatcode + 256, catOther);
1315 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1316 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1318 theCatcode[int('\\')] = catEscape;
1319 theCatcode[int('{')] = catBegin;
1320 theCatcode[int('}')] = catEnd;
1321 theCatcode[int('$')] = catMath;
1322 theCatcode[int('&')] = catAlign;
1323 theCatcode[int('\n')] = catNewline;
1324 theCatcode[int('#')] = catParameter;
1325 theCatcode[int('^')] = catSuper;
1326 theCatcode[int('_')] = catSub;
1327 theCatcode[int(0x7f)] = catIgnore;
1328 theCatcode[int(' ')] = catSpace;
1329 theCatcode[int('\t')] = catSpace;
1330 theCatcode[int('\r')] = catNewline;
1331 theCatcode[int('~')] = catActive;
1332 theCatcode[int('%')] = catComment;