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"
66 #include "support/convert.h"
76 using std::istringstream;
86 MathInset::mode_type asMode(MathInset::mode_type oldmode, string const & str)
88 //lyxerr << "handling mode: '" << str << "'" << endl;
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] == '*';
105 * Add the row \p cellrow to \p grid.
106 * \returns wether the row could be added. Adding a row can fail for
107 * environments like "equation" that have a fixed number of rows.
109 bool addRow(MathGridInset & grid, MathGridInset::row_type & cellrow,
110 string const & vskip)
113 if (cellrow == grid.nrows()) {
114 //lyxerr << "adding row " << cellrow << endl;
115 grid.addRow(cellrow - 1);
116 if (cellrow == grid.nrows()) {
117 // We can't add a row to this grid, so let's
118 // append the content of this cell to the previous
120 // This does not happen in well formed .lyx files,
121 // but LyX versions 1.3.x and older could create
122 // such files and tex2lyx can still do that.
124 lyxerr << "ignoring extra row";
126 lyxerr << " with extra space " << vskip;
127 lyxerr << '.' << endl;
131 grid.vcrskip(LyXLength(vskip), cellrow - 1);
137 * Add the column \p cellcol to \p grid.
138 * \returns wether the column could be added. Adding a column can fail for
139 * environments like "eqnarray" that have a fixed number of columns.
141 bool addCol(MathGridInset & grid, MathGridInset::col_type & cellcol)
144 if (cellcol == grid.ncols()) {
145 //lyxerr << "adding column " << cellcol << endl;
146 grid.addCol(cellcol - 1);
147 if (cellcol == grid.ncols()) {
148 // We can't add a column to this grid, so let's
149 // append the content of this cell to the previous
151 // This does not happen in well formed .lyx files,
152 // but LyX versions 1.3.x and older could create
153 // such files and tex2lyx can still do that.
155 lyxerr << "ignoring extra column." << endl;
164 * Check wether the last row is empty and remove it if yes.
165 * Otherwise the following code
173 * will result in a grid with 3 rows (+ the dummy row that is always present),
174 * because the last '\\' opens a new row.
176 void delEmptyLastRow(MathGridInset & grid)
178 MathGridInset::row_type const row = grid.nrows() - 1;
179 for (MathGridInset::col_type col = 0; col < grid.ncols(); ++col) {
180 if (!grid.cell(grid.index(row, col)).empty())
183 grid.delRow(row + 1);
187 // These are TeX's catcodes
189 catEscape, // 0 backslash
199 catSpace, // 10 space
200 catLetter, // 11 a-zA-Z
201 catOther, // 12 none of the above
204 catInvalid // 15 <delete>
207 CatCode theCatcode[256];
210 inline CatCode catcode(unsigned char c)
212 return theCatcode[c];
217 FLAG_ALIGN = 1 << 0, // next & or \\ ends the parsing process
218 FLAG_BRACE_LAST = 1 << 1, // next closing brace ends the parsing
219 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
220 FLAG_END = 1 << 3, // next \\end ends the parsing process
221 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
222 FLAG_TEXTMODE = 1 << 5, // we are in a box
223 FLAG_ITEM = 1 << 6, // read a (possibly braced token)
224 FLAG_LEAVE = 1 << 7, // leave the loop at the end
225 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
226 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
227 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
228 FLAG_OPTION = 1 << 11, // read [...] style option
229 FLAG_BRACED = 1 << 12 // read {...} style argument
234 // Helper class for parsing
240 Token() : cs_(), char_(0), cat_(catIgnore) {}
242 Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
244 Token(string const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
247 string const & cs() const { return cs_; }
249 CatCode cat() const { return cat_; }
251 char character() const { return char_; }
253 string asString() const { return cs_.size() ? cs_ : string(1, char_); }
264 ostream & operator<<(ostream & os, Token const & t)
267 os << '\\' << t.cs();
268 else if (t.cat() == catLetter)
271 os << '[' << t.character() << ',' << t.cat() << ']';
279 typedef MathInset::mode_type mode_type;
282 Parser(LyXLex & lex);
284 Parser(istream & is);
287 bool parse(MathAtom & at);
289 void parse(MathArray & array, unsigned flags, mode_type mode);
291 void parse1(MathGridInset & grid, unsigned flags, mode_type mode,
294 MathArray parse(unsigned flags, mode_type mode);
296 int lineno() const { return lineno_; }
302 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
303 /// get arg delimited by 'left' and 'right'
304 string getArg(char left, char right);
308 void error(string const & msg);
309 /// dump contents to screen
312 void tokenize(istream & is);
314 void tokenize(string const & s);
316 void skipSpaceTokens(istream & is, char c);
318 void push_back(Token const & t);
322 Token const & prevToken() const;
324 Token const & nextToken() const;
326 Token const & getToken();
327 /// skips spaces if any
330 void lex(string const & s);
334 string parse_verbatim_item();
336 string parse_verbatim_option();
341 vector<Token> tokens_;
344 /// Stack of active environments
345 vector<string> environments_;
349 Parser::Parser(LyXLex & lexer)
350 : lineno_(lexer.getLineNo()), pos_(0)
352 tokenize(lexer.getStream());
357 Parser::Parser(istream & is)
358 : lineno_(0), pos_(0)
364 void Parser::push_back(Token const & t)
366 tokens_.push_back(t);
370 void Parser::pop_back()
376 Token const & Parser::prevToken() const
378 static const Token dummy;
379 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
383 Token const & Parser::nextToken() const
385 static const Token dummy;
386 return good() ? tokens_[pos_] : dummy;
390 Token const & Parser::getToken()
392 static const Token dummy;
393 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
394 return good() ? tokens_[pos_++] : dummy;
398 void Parser::skipSpaces()
400 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
405 void Parser::putback()
411 bool Parser::good() const
413 return pos_ < tokens_.size();
417 char Parser::getChar()
420 error("The input stream is not well...");
421 return tokens_[pos_++].character();
425 string Parser::getArg(char left, char right)
435 while ((c = getChar()) != right && good())
442 void Parser::skipSpaceTokens(istream & is, char c)
444 // skip trailing spaces
445 while (catcode(c) == catSpace || catcode(c) == catNewline)
448 //lyxerr << "putting back: " << c << endl;
453 void Parser::tokenize(istream & is)
455 // eat everything up to the next \end_inset or end of stream
456 // and store it in s for further tokenization
461 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
462 s = s.substr(0, s.size() - 10);
466 // Remove the space after \end_inset
467 if (is.get(c) && c != ' ')
475 void Parser::tokenize(string const & buffer)
477 istringstream is(buffer, ios::in | ios::binary);
481 //lyxerr << "reading c: " << c << endl;
483 switch (catcode(c)) {
487 if (catcode(c) == catNewline)
488 ; //push_back(Token("par"));
490 push_back(Token('\n', catNewline));
498 while (is.get(c) && catcode(c) != catNewline)
508 error("unexpected end of input");
511 if (catcode(c) == catLetter) {
513 while (is.get(c) && catcode(c) == catLetter)
515 skipSpaceTokens(is, c);
524 push_back(Token(c, catcode(c)));
526 skipSpaceTokens(is, c);
531 lyxerr << "ignoring a char: " << int(c) << endl;
536 push_back(Token(c, catcode(c)));
546 void Parser::dump() const
548 lyxerr << "\nTokens: ";
549 for (unsigned i = 0; i < tokens_.size(); ++i) {
552 lyxerr << tokens_[i];
554 lyxerr << " pos: " << pos_ << endl;
558 void Parser::error(string const & msg)
560 lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
566 bool Parser::parse(MathAtom & at)
570 parse(ar, false, MathInset::UNDECIDED_MODE);
571 if (ar.size() != 1 || ar.front()->getType() == "none") {
572 lyxerr << "unusual contents found: " << ar << endl;
573 at = MathAtom(new MathParInset(ar));
574 //if (at->nargs() > 0)
575 // at.nucleus()->cell(0) = ar;
577 // lyxerr << "unusual contents found: " << ar << endl;
585 string Parser::parse_verbatim_option()
589 if (nextToken().character() == '[') {
590 Token t = getToken();
591 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
592 if (t.cat() == catBegin) {
594 res += '{' + parse_verbatim_item() + '}';
603 string Parser::parse_verbatim_item()
607 if (nextToken().cat() == catBegin) {
608 Token t = getToken();
609 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
610 if (t.cat() == catBegin) {
612 res += '{' + parse_verbatim_item() + '}';
622 MathArray Parser::parse(unsigned flags, mode_type mode)
625 parse(ar, flags, mode);
630 void Parser::parse(MathArray & array, unsigned flags, mode_type mode)
632 MathGridInset grid(1, 1);
633 parse1(grid, flags, mode, false);
634 array = grid.cell(0);
638 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
641 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
645 void Parser::parse1(MathGridInset & grid, unsigned flags,
646 const mode_type mode, const bool numbered)
649 MathGridInset::row_type cellrow = 0;
650 MathGridInset::col_type cellcol = 0;
651 MathArray * cell = &grid.cell(grid.index(cellrow, cellcol));
653 if (grid.asHullInset())
654 grid.asHullInset()->numbered(cellrow, numbered);
657 //lyxerr << " flags: " << flags << endl;
658 //lyxerr << " mode: " << mode << endl;
659 //lyxerr << "grid: " << grid << endl;
662 Token const & t = getToken();
665 lyxerr << "t: " << t << " flags: " << flags << endl;
666 lyxerr << "mode: " << mode << endl;
671 if (flags & FLAG_ITEM) {
673 if (t.cat() == catBegin) {
674 // skip the brace and collect everything to the next matching
676 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
680 // handle only this single token, leave the loop if done
685 if (flags & FLAG_BRACED) {
686 if (t.cat() == catSpace)
689 if (t.cat() != catBegin) {
690 error("opening brace expected");
694 // skip the brace and collect everything to the next matching
696 flags = FLAG_BRACE_LAST;
700 if (flags & FLAG_OPTION) {
701 if (t.cat() == catOther && t.character() == '[') {
703 parse(ar, FLAG_BRACK_LAST, mode);
706 // no option found, put back token and we are done
715 if (t.cat() == catMath) {
716 if (mode != MathInset::MATH_MODE) {
717 // we are inside some text mode thingy, so opening new math is allowed
718 Token const & n = getToken();
719 if (n.cat() == catMath) {
720 // TeX's $$...$$ syntax for displayed math
721 cell->push_back(MathAtom(new MathHullInset("equation")));
722 parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
723 getToken(); // skip the second '$' token
725 // simple $...$ stuff
727 cell->push_back(MathAtom(new MathHullInset("simple")));
728 parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
732 else if (flags & FLAG_SIMPLE) {
733 // this is the end of the formula
738 error("something strange in the parser");
743 else if (t.cat() == catLetter)
744 cell->push_back(MathAtom(new MathCharInset(t.character())));
746 else if (t.cat() == catSpace && mode != MathInset::MATH_MODE) {
747 if (cell->empty() || cell->back()->getChar() != ' ')
748 cell->push_back(MathAtom(new MathCharInset(t.character())));
751 else if (t.cat() == catNewline && mode != MathInset::MATH_MODE) {
752 if (cell->empty() || cell->back()->getChar() != ' ')
753 cell->push_back(MathAtom(new MathCharInset(' ')));
756 else if (t.cat() == catParameter) {
757 Token const & n = getToken();
758 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
761 else if (t.cat() == catActive)
762 cell->push_back(MathAtom(new MathCharInset(t.character())));
764 else if (t.cat() == catBegin) {
766 parse(ar, FLAG_BRACE_LAST, mode);
767 // do not create a BraceInset if they were written by LyX
768 // this helps to keep the annoyance of "a choose b" to a minimum
769 if (ar.size() == 1 && ar[0]->extraBraces())
772 cell->push_back(MathAtom(new MathBraceInset(ar)));
775 else if (t.cat() == catEnd) {
776 if (flags & FLAG_BRACE_LAST)
778 error("found '}' unexpectedly");
779 //BOOST_ASSERT(false);
780 //add(cell, '}', LM_TC_TEX);
783 else if (t.cat() == catAlign) {
784 //lyxerr << " column now " << (cellcol + 1)
785 // << " max: " << grid.ncols() << endl;
786 if (flags & FLAG_ALIGN)
788 if (addCol(grid, cellcol))
789 cell = &grid.cell(grid.index(cellrow, cellcol));
792 else if (t.cat() == catSuper || t.cat() == catSub) {
793 bool up = (t.cat() == catSuper);
794 // we need no new script inset if the last thing was a scriptinset,
795 // which has that script already not the same script already
797 cell->push_back(MathAtom(new MathScriptInset(up)));
798 else if (cell->back()->asScriptInset() &&
799 !cell->back()->asScriptInset()->has(up))
800 cell->back().nucleus()->asScriptInset()->ensure(up);
801 else if (cell->back()->asScriptInset())
802 cell->push_back(MathAtom(new MathScriptInset(up)));
804 cell->back() = MathAtom(new MathScriptInset(cell->back(), up));
805 MathScriptInset * p = cell->back().nucleus()->asScriptInset();
806 // special handling of {}-bases
807 // is this always correct?
808 // It appears that this is wrong (Dekel)
809 //if (p->nuc().size() == 1 && p->nuc().back()->asNestInset() &&
810 // p->nuc().back()->extraBraces())
811 // p->nuc() = p->nuc().back()->asNestInset()->cell(0);
812 parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
819 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
820 //lyxerr << "finished reading option" << endl;
824 else if (t.cat() == catOther)
825 cell->push_back(MathAtom(new MathCharInset(t.character())));
827 else if (t.cat() == catComment) {
830 Token const & t = getToken();
831 if (t.cat() == catNewline)
835 cell->push_back(MathAtom(new MathCommentInset(s)));
843 else if (t.cs() == "lyxlock") {
845 cell->back().nucleus()->lock(true);
848 else if (t.cs() == "def" ||
849 t.cs() == "newcommand" ||
850 t.cs() == "renewcommand")
852 string const type = t.cs();
855 if (t.cs() == "def") {
857 name = getToken().cs();
861 while (good() && nextToken().cat() != catBegin) {
862 pars += getToken().cs();
866 //lyxerr << "read \\def parameter list '" << pars << "'" << endl;
868 } else { // t.cs() == "newcommand" || t.cs() == "renewcommand"
870 if (getToken().cat() != catBegin) {
871 error("'{' in \\newcommand expected (1) ");
875 name = getToken().cs();
877 if (getToken().cat() != catEnd) {
878 error("'}' in \\newcommand expected");
882 string const arg = getArg('[', ']');
884 nargs = convert<int>(arg);
889 parse(ar1, FLAG_ITEM, MathInset::UNDECIDED_MODE);
891 // we cannot handle recursive stuff at all
893 //test.push_back(createMathInset(name));
894 //if (ar1.contains(test)) {
895 // error("we cannot handle recursive macros at all.");
899 // is a version for display attached?
902 if (nextToken().cat() == catBegin)
903 parse(ar2, FLAG_ITEM, MathInset::MATH_MODE);
905 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, type,
909 else if (t.cs() == "(") {
910 cell->push_back(MathAtom(new MathHullInset("simple")));
911 parse2(cell->back(), FLAG_SIMPLE2, MathInset::MATH_MODE, false);
914 else if (t.cs() == "[") {
915 cell->push_back(MathAtom(new MathHullInset("equation")));
916 parse2(cell->back(), FLAG_EQUATION, MathInset::MATH_MODE, false);
919 else if (t.cs() == "protect")
920 // ignore \\protect, will hopefully be re-added during output
923 else if (t.cs() == "end") {
924 if (flags & FLAG_END) {
925 // eat environment name
926 string const name = getArg('{', '}');
927 if (environments_.empty())
928 error("'found \\end{" + name +
929 "}' without matching '\\begin{" +
931 else if (name != environments_.back())
932 error("'\\end{" + name +
933 "}' does not match '\\begin{" +
934 environments_.back() + "}'");
936 environments_.pop_back();
937 if (name == "array" ||
939 delEmptyLastRow(grid);
943 error("found 'end' unexpectedly");
946 else if (t.cs() == ")") {
947 if (flags & FLAG_SIMPLE2)
949 error("found '\\)' unexpectedly");
952 else if (t.cs() == "]") {
953 if (flags & FLAG_EQUATION)
955 error("found '\\]' unexpectedly");
958 else if (t.cs() == "\\") {
959 if (flags & FLAG_ALIGN)
961 if (addRow(grid, cellrow, getArg('[', ']'))) {
963 if (grid.asHullInset())
964 grid.asHullInset()->numbered(
966 cell = &grid.cell(grid.index(cellrow,
972 else if (t.cs() == "multicolumn") {
973 // extract column count and insert dummy cells
975 parse(count, FLAG_ITEM, mode);
977 if (!extractNumber(count, cols)) {
978 lyxerr << " can't extract number of cells from " << count << endl;
980 // resize the table if necessary
981 for (int i = 0; i < cols; ++i) {
982 if (addCol(grid, cellcol)) {
983 cell = &grid.cell(grid.index(
985 // mark this as dummy
986 grid.cellinfo(grid.index(
987 cellrow, cellcol)).dummy_ = true;
990 // the last cell is the real thing, not a dummy
991 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
993 // read special alignment
995 parse(align, FLAG_ITEM, mode);
996 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
998 // parse the remaining contents into the "real" cell
999 parse(*cell, FLAG_ITEM, mode);
1003 else if (t.cs() == "limits")
1006 else if (t.cs() == "nolimits")
1009 else if (t.cs() == "nonumber") {
1010 if (grid.asHullInset())
1011 grid.asHullInset()->numbered(cellrow, false);
1014 else if (t.cs() == "number") {
1015 if (grid.asHullInset())
1016 grid.asHullInset()->numbered(cellrow, true);
1019 else if (t.cs() == "hline") {
1020 grid.rowinfo(cellrow).lines_ ++;
1023 else if (t.cs() == "sqrt") {
1025 parse(ar, FLAG_OPTION, mode);
1027 cell->push_back(MathAtom(new MathRootInset));
1028 cell->back().nucleus()->cell(0) = ar;
1029 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1031 cell->push_back(MathAtom(new MathSqrtInset));
1032 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1036 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1037 cell->push_back(createMathInset(t.cs()));
1038 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1039 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1042 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
1043 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1044 cell->push_back(MathAtom(new RefInset(t.cs())));
1045 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1046 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1049 else if (t.cs() == "left") {
1051 Token const & tl = getToken();
1052 // \| and \Vert are equivalent, and MathDelimInset
1054 // FIXME: fix this in MathDelimInset itself!
1055 string const l = tl.cs() == "|" ? "Vert" : tl.asString();
1057 parse(ar, FLAG_RIGHT, mode);
1059 Token const & tr = getToken();
1060 string const r = tr.cs() == "|" ? "Vert" : tr.asString();
1061 cell->push_back(MathAtom(new MathDelimInset(l, r, ar)));
1064 else if (t.cs() == "right") {
1065 if (flags & FLAG_RIGHT)
1067 //lyxerr << "got so far: '" << cell << "'" << endl;
1068 error("Unmatched right delimiter");
1072 else if (t.cs() == "begin") {
1073 string const name = getArg('{', '}');
1074 environments_.push_back(name);
1076 if (name == "array" || name == "subarray") {
1077 string const valign = parse_verbatim_option() + 'c';
1078 string const halign = parse_verbatim_item();
1079 cell->push_back(MathAtom(new MathArrayInset(name, valign[0], halign)));
1080 parse2(cell->back(), FLAG_END, mode, false);
1083 else if (name == "tabular") {
1084 string const valign = parse_verbatim_option() + 'c';
1085 string const halign = parse_verbatim_item();
1086 cell->push_back(MathAtom(new MathTabularInset(name, valign[0], halign)));
1087 parse2(cell->back(), FLAG_END, MathInset::TEXT_MODE, false);
1090 else if (name == "split" || name == "cases" ||
1091 name == "gathered" || name == "aligned" ||
1092 name == "alignedat") {
1093 cell->push_back(createMathInset(name));
1094 parse2(cell->back(), FLAG_END, mode, false);
1097 else if (name == "math") {
1098 cell->push_back(MathAtom(new MathHullInset("simple")));
1099 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, true);
1102 else if (name == "equation" || name == "equation*"
1103 || name == "displaymath") {
1104 cell->push_back(MathAtom(new MathHullInset("equation")));
1105 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, (name == "equation"));
1108 else if (name == "eqnarray" || name == "eqnarray*") {
1109 cell->push_back(MathAtom(new MathHullInset("eqnarray")));
1110 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1113 else if (name == "align" || name == "align*") {
1114 cell->push_back(MathAtom(new MathHullInset("align")));
1115 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1118 else if (name == "flalign" || name == "flalign*") {
1119 cell->push_back(MathAtom(new MathHullInset("flalign")));
1120 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1123 else if (name == "alignat" || name == "alignat*") {
1124 // ignore this for a while
1126 cell->push_back(MathAtom(new MathHullInset("alignat")));
1127 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1130 else if (name == "xalignat" || name == "xalignat*") {
1131 // ignore this for a while
1133 cell->push_back(MathAtom(new MathHullInset("xalignat")));
1134 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1137 else if (name == "xxalignat") {
1138 // ignore this for a while
1140 cell->push_back(MathAtom(new MathHullInset("xxalignat")));
1141 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1144 else if (name == "multline" || name == "multline*") {
1145 cell->push_back(MathAtom(new MathHullInset("multline")));
1146 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1149 else if (name == "gather" || name == "gather*") {
1150 cell->push_back(MathAtom(new MathHullInset("gather")));
1151 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1154 else if (latexkeys const * l = in_word_set(name)) {
1155 if (l->inset == "matrix") {
1156 cell->push_back(createMathInset(name));
1157 parse2(cell->back(), FLAG_END, mode, false);
1163 lyxerr << "found unknown math environment '" << name << "'" << endl;
1164 // create generic environment inset
1165 cell->push_back(MathAtom(new MathEnvInset(name)));
1166 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1170 else if (t.cs() == "kern") {
1171 #ifdef WITH_WARNINGS
1176 Token const & t = getToken();
1182 if (isValidLength(s))
1185 cell->push_back(MathAtom(new MathKernInset(s)));
1188 else if (t.cs() == "label") {
1189 string label = parse_verbatim_item();
1192 if (grid.asHullInset()) {
1193 grid.asHullInset()->label(cellrow, label);
1195 cell->push_back(createMathInset(t.cs()));
1196 cell->push_back(MathAtom(new MathBraceInset(ar)));
1200 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1201 MathAtom at = createMathInset(t.cs());
1202 at.nucleus()->cell(0) = *cell;
1204 parse(at.nucleus()->cell(1), flags, mode);
1205 cell->push_back(at);
1209 else if (t.cs() == "color") {
1210 MathAtom at = createMathInset(t.cs());
1211 parse(at.nucleus()->cell(0), FLAG_ITEM, MathInset::TEXT_MODE);
1212 parse(at.nucleus()->cell(1), flags, mode);
1213 cell->push_back(at);
1217 else if (t.cs() == "substack") {
1218 cell->push_back(createMathInset(t.cs()));
1219 parse2(cell->back(), FLAG_ITEM, mode, false);
1222 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1223 cell->push_back(createMathInset(t.cs()));
1224 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, MathInset::TEXT_MODE);
1225 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, MathInset::TEXT_MODE);
1226 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, MathInset::TEXT_MODE);
1230 else if (t.cs() == "infer") {
1232 parse(ar, FLAG_OPTION, mode);
1233 cell->push_back(createMathInset(t.cs()));
1234 parse2(cell->back(), FLAG_ITEM, mode, false);
1238 else if (1 && t.cs() == "ar") {
1239 auto_ptr<MathXYArrowInset> p(new MathXYArrowInset);
1240 // try to read target
1241 parse(p->cell(0), FLAG_OTPTION, mode);
1242 // try to read label
1243 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1244 p->up_ = nextToken().cat() == catSuper;
1246 parse(p->cell(1), FLAG_ITEM, mode);
1247 //lyxerr << "read label: " << p->cell(1) << endl;
1250 cell->push_back(MathAtom(p.release()));
1251 //lyxerr << "read cell: " << cell << endl;
1255 else if (t.cs().size()) {
1256 latexkeys const * l = in_word_set(t.cs());
1258 if (l->inset == "font") {
1259 cell->push_back(createMathInset(t.cs()));
1260 parse(cell->back().nucleus()->cell(0),
1261 FLAG_ITEM, asMode(mode, l->extra));
1264 else if (l->inset == "oldfont") {
1265 cell->push_back(createMathInset(t.cs()));
1266 parse(cell->back().nucleus()->cell(0),
1267 flags | FLAG_ALIGN, asMode(mode, l->extra));
1268 if (prevToken().cat() != catAlign &&
1269 prevToken().cs() != "\\")
1274 else if (l->inset == "style") {
1275 cell->push_back(createMathInset(t.cs()));
1276 parse(cell->back().nucleus()->cell(0),
1277 flags | FLAG_ALIGN, mode);
1278 if (prevToken().cat() != catAlign &&
1279 prevToken().cs() != "\\")
1285 MathAtom at = createMathInset(t.cs());
1286 for (MathInset::idx_type i = 0; i < at->nargs(); ++i)
1287 parse(at.nucleus()->cell(i),
1288 FLAG_ITEM, asMode(mode, l->extra));
1289 cell->push_back(at);
1294 MathAtom at = createMathInset(t.cs());
1295 MathInset::mode_type m = mode;
1296 //if (m == MathInset::UNDECIDED_MODE)
1297 //lyxerr << "default creation: m1: " << m << endl;
1298 if (at->currentMode() != MathInset::UNDECIDED_MODE)
1299 m = at->currentMode();
1300 //lyxerr << "default creation: m2: " << m << endl;
1301 MathInset::idx_type start = 0;
1302 // this fails on \bigg[...\bigg]
1304 //parse(opt, FLAG_OPTION, MathInset::VERBATIM_MODE);
1307 // at.nucleus()->cell(0) = opt;
1309 for (MathInset::idx_type i = start; i < at->nargs(); ++i) {
1310 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1313 cell->push_back(at);
1318 if (flags & FLAG_LEAVE) {
1319 flags &= ~FLAG_LEAVE;
1327 } // anonymous namespace
1330 void mathed_parse_cell(MathArray & ar, string const & str)
1332 istringstream is(str);
1333 mathed_parse_cell(ar, is);
1337 void mathed_parse_cell(MathArray & ar, istream & is)
1339 Parser(is).parse(ar, 0, MathInset::MATH_MODE);
1343 bool mathed_parse_normal(MathAtom & t, string const & str)
1345 istringstream is(str);
1346 return Parser(is).parse(t);
1350 bool mathed_parse_normal(MathAtom & t, istream & is)
1352 return Parser(is).parse(t);
1356 bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
1358 return Parser(lex).parse(t);
1362 void mathed_parse_normal(MathGridInset & grid, string const & str)
1364 istringstream is(str);
1365 Parser(is).parse1(grid, 0, MathInset::MATH_MODE, false);
1371 fill(theCatcode, theCatcode + 256, catOther);
1372 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1373 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1375 theCatcode[int('\\')] = catEscape;
1376 theCatcode[int('{')] = catBegin;
1377 theCatcode[int('}')] = catEnd;
1378 theCatcode[int('$')] = catMath;
1379 theCatcode[int('&')] = catAlign;
1380 theCatcode[int('\n')] = catNewline;
1381 theCatcode[int('#')] = catParameter;
1382 theCatcode[int('^')] = catSuper;
1383 theCatcode[int('_')] = catSub;
1384 theCatcode[int(0x7f)] = catIgnore;
1385 theCatcode[int(' ')] = catSpace;
1386 theCatcode[int('\t')] = catSpace;
1387 theCatcode[int('\r')] = catNewline;
1388 theCatcode[int('~')] = catActive;
1389 theCatcode[int('%')] = catComment;