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_ALIGN = 1 << 0, // next & or \\ ends the parsing process
195 FLAG_BRACE_LAST = 1 << 1, // next closing brace ends the parsing
196 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
197 FLAG_END = 1 << 3, // next \\end ends the parsing process
198 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
199 FLAG_TEXTMODE = 1 << 5, // we are in a box
200 FLAG_ITEM = 1 << 6, // read a (possibly braced token)
201 FLAG_LEAVE = 1 << 7, // leave the loop at the end
202 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
203 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
204 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
205 FLAG_OPTION = 1 << 11, // read [...] style option
206 FLAG_BRACED = 1 << 12 // read {...} style argument
211 // Helper class for parsing
217 Token() : cs_(), char_(0), cat_(catIgnore) {}
219 Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
221 Token(string const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
224 string const & cs() const { return cs_; }
226 CatCode cat() const { return cat_; }
228 char character() const { return char_; }
230 string asString() const { return cs_.size() ? cs_ : string(1, char_); }
241 ostream & operator<<(ostream & os, Token const & t)
244 os << '\\' << t.cs();
245 else if (t.cat() == catLetter)
248 os << '[' << t.character() << ',' << t.cat() << ']';
256 typedef MathInset::mode_type mode_type;
259 Parser(LyXLex & lex);
261 Parser(istream & is);
264 bool parse(MathAtom & at);
266 void parse(MathArray & array, unsigned flags, mode_type mode);
268 void parse1(MathGridInset & grid, unsigned flags, mode_type mode,
271 MathArray parse(unsigned flags, mode_type mode);
273 int lineno() const { return lineno_; }
279 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
280 /// get arg delimited by 'left' and 'right'
281 string getArg(char left, char right);
285 void error(string const & msg);
286 /// dump contents to screen
289 void tokenize(istream & is);
291 void tokenize(string const & s);
293 void skipSpaceTokens(istream & is, char c);
295 void push_back(Token const & t);
299 Token const & prevToken() const;
301 Token const & nextToken() const;
303 Token const & getToken();
304 /// skips spaces if any
307 void lex(string const & s);
311 string parse_verbatim_item();
313 string parse_verbatim_option();
318 vector<Token> tokens_;
324 Parser::Parser(LyXLex & lexer)
325 : lineno_(lexer.getLineNo()), pos_(0)
327 tokenize(lexer.getStream());
332 Parser::Parser(istream & is)
333 : lineno_(0), pos_(0)
339 void Parser::push_back(Token const & t)
341 tokens_.push_back(t);
345 void Parser::pop_back()
351 Token const & Parser::prevToken() const
353 static const Token dummy;
354 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
358 Token const & Parser::nextToken() const
360 static const Token dummy;
361 return good() ? tokens_[pos_] : dummy;
365 Token const & Parser::getToken()
367 static const Token dummy;
368 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
369 return good() ? tokens_[pos_++] : dummy;
373 void Parser::skipSpaces()
375 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
380 void Parser::putback()
386 bool Parser::good() const
388 return pos_ < tokens_.size();
392 char Parser::getChar()
395 error("The input stream is not well...");
396 return tokens_[pos_++].character();
400 string Parser::getArg(char left, char right)
410 while ((c = getChar()) != right && good())
417 void Parser::skipSpaceTokens(istream & is, char c)
419 // skip trailing spaces
420 while (catcode(c) == catSpace || catcode(c) == catNewline)
423 //lyxerr << "putting back: " << c << endl;
428 void Parser::tokenize(istream & is)
430 // eat everything up to the next \end_inset or end of stream
431 // and store it in s for further tokenization
436 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
437 s = s.substr(0, s.size() - 10);
441 // Remove the space after \end_inset
442 if (is.get(c) && c != ' ')
450 void Parser::tokenize(string const & buffer)
452 istringstream is(buffer, ios::in | ios::binary);
456 //lyxerr << "reading c: " << c << endl;
458 switch (catcode(c)) {
462 if (catcode(c) == catNewline)
463 ; //push_back(Token("par"));
465 push_back(Token('\n', catNewline));
473 while (is.get(c) && catcode(c) != catNewline)
483 error("unexpected end of input");
486 if (catcode(c) == catLetter) {
488 while (is.get(c) && catcode(c) == catLetter)
490 skipSpaceTokens(is, c);
499 push_back(Token(c, catcode(c)));
501 skipSpaceTokens(is, c);
506 lyxerr << "ignoring a char: " << int(c) << endl;
511 push_back(Token(c, catcode(c)));
521 void Parser::dump() const
523 lyxerr << "\nTokens: ";
524 for (unsigned i = 0; i < tokens_.size(); ++i) {
527 lyxerr << tokens_[i];
529 lyxerr << " pos: " << pos_ << endl;
533 void Parser::error(string const & msg)
535 lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
541 bool Parser::parse(MathAtom & at)
545 parse(ar, false, MathInset::UNDECIDED_MODE);
546 if (ar.size() != 1 || ar.front()->getType() == "none") {
547 lyxerr << "unusual contents found: " << ar << endl;
548 at = MathAtom(new MathParInset(ar));
549 //if (at->nargs() > 0)
550 // at.nucleus()->cell(0) = ar;
552 // lyxerr << "unusual contents found: " << ar << endl;
560 string Parser::parse_verbatim_option()
564 if (nextToken().character() == '[') {
565 Token t = getToken();
566 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
567 if (t.cat() == catBegin) {
569 res += '{' + parse_verbatim_item() + '}';
578 string Parser::parse_verbatim_item()
582 if (nextToken().cat() == catBegin) {
583 Token t = getToken();
584 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
585 if (t.cat() == catBegin) {
587 res += '{' + parse_verbatim_item() + '}';
597 MathArray Parser::parse(unsigned flags, mode_type mode)
600 parse(ar, flags, mode);
605 void Parser::parse(MathArray & array, unsigned flags, mode_type mode)
607 MathGridInset grid(1, 1);
608 parse1(grid, flags, mode, false);
609 array = grid.cell(0);
613 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
616 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
620 void Parser::parse1(MathGridInset & grid, unsigned flags,
621 const mode_type mode, const bool numbered)
624 MathGridInset::row_type cellrow = 0;
625 MathGridInset::col_type cellcol = 0;
626 MathArray * cell = &grid.cell(grid.index(cellrow, cellcol));
628 if (grid.asHullInset())
629 grid.asHullInset()->numbered(cellrow, numbered);
632 //lyxerr << " flags: " << flags << endl;
633 //lyxerr << " mode: " << mode << endl;
634 //lyxerr << "grid: " << grid << endl;
637 Token const & t = getToken();
640 lyxerr << "t: " << t << " flags: " << flags << endl;
641 lyxerr << "mode: " << mode << endl;
646 if (flags & FLAG_ITEM) {
648 if (t.cat() == catBegin) {
649 // skip the brace and collect everything to the next matching
651 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
655 // handle only this single token, leave the loop if done
660 if (flags & FLAG_BRACED) {
661 if (t.cat() == catSpace)
664 if (t.cat() != catBegin) {
665 error("opening brace expected");
669 // skip the brace and collect everything to the next matching
671 flags = FLAG_BRACE_LAST;
675 if (flags & FLAG_OPTION) {
676 if (t.cat() == catOther && t.character() == '[') {
678 parse(ar, FLAG_BRACK_LAST, mode);
681 // no option found, put back token and we are done
690 if (t.cat() == catMath) {
691 if (mode != MathInset::MATH_MODE) {
692 // we are inside some text mode thingy, so opening new math is allowed
693 Token const & n = getToken();
694 if (n.cat() == catMath) {
695 // TeX's $$...$$ syntax for displayed math
696 cell->push_back(MathAtom(new MathHullInset("equation")));
697 parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
698 getToken(); // skip the second '$' token
700 // simple $...$ stuff
702 cell->push_back(MathAtom(new MathHullInset("simple")));
703 parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
707 else if (flags & FLAG_SIMPLE) {
708 // this is the end of the formula
713 error("something strange in the parser");
718 else if (t.cat() == catLetter)
719 cell->push_back(MathAtom(new MathCharInset(t.character())));
721 else if (t.cat() == catSpace && mode != MathInset::MATH_MODE) {
722 if (cell->empty() || cell->back()->getChar() != ' ')
723 cell->push_back(MathAtom(new MathCharInset(t.character())));
726 else if (t.cat() == catNewline && mode != MathInset::MATH_MODE) {
727 if (cell->empty() || cell->back()->getChar() != ' ')
728 cell->push_back(MathAtom(new MathCharInset(' ')));
731 else if (t.cat() == catParameter) {
732 Token const & n = getToken();
733 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
736 else if (t.cat() == catActive)
737 cell->push_back(MathAtom(new MathCharInset(t.character())));
739 else if (t.cat() == catBegin) {
741 parse(ar, FLAG_BRACE_LAST, mode);
742 // do not create a BraceInset if they were written by LyX
743 // this helps to keep the annoyance of "a choose b" to a minimum
744 if (ar.size() == 1 && ar[0]->extraBraces())
747 cell->push_back(MathAtom(new MathBraceInset(ar)));
750 else if (t.cat() == catEnd) {
751 if (flags & FLAG_BRACE_LAST)
753 error("found '}' unexpectedly");
754 //BOOST_ASSERT(false);
755 //add(cell, '}', LM_TC_TEX);
758 else if (t.cat() == catAlign) {
759 //lyxerr << " column now " << (cellcol + 1)
760 // << " max: " << grid.ncols() << endl;
761 if (flags & FLAG_ALIGN)
763 if (addCol(grid, cellcol))
764 cell = &grid.cell(grid.index(cellrow, cellcol));
767 else if (t.cat() == catSuper || t.cat() == catSub) {
768 bool up = (t.cat() == catSuper);
769 // we need no new script inset if the last thing was a scriptinset,
770 // which has that script already not the same script already
772 cell->push_back(MathAtom(new MathScriptInset(up)));
773 else if (cell->back()->asScriptInset() &&
774 !cell->back()->asScriptInset()->has(up))
775 cell->back().nucleus()->asScriptInset()->ensure(up);
776 else if (cell->back()->asScriptInset())
777 cell->push_back(MathAtom(new MathScriptInset(up)));
779 cell->back() = MathAtom(new MathScriptInset(cell->back(), up));
780 MathScriptInset * p = cell->back().nucleus()->asScriptInset();
781 // special handling of {}-bases
782 // is this always correct?
783 // It appears that this is wrong (Dekel)
784 //if (p->nuc().size() == 1 && p->nuc().back()->asNestInset() &&
785 // p->nuc().back()->extraBraces())
786 // p->nuc() = p->nuc().back()->asNestInset()->cell(0);
787 parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
794 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
795 //lyxerr << "finished reading option" << endl;
799 else if (t.cat() == catOther)
800 cell->push_back(MathAtom(new MathCharInset(t.character())));
802 else if (t.cat() == catComment) {
805 Token const & t = getToken();
806 if (t.cat() == catNewline)
810 cell->push_back(MathAtom(new MathCommentInset(s)));
818 else if (t.cs() == "lyxlock") {
820 cell->back().nucleus()->lock(true);
823 else if (t.cs() == "def" ||
824 t.cs() == "newcommand" ||
825 t.cs() == "renewcommand")
827 string const type = t.cs();
830 if (t.cs() == "def") {
832 name = getToken().cs();
836 while (good() && nextToken().cat() != catBegin) {
837 pars += getToken().cs();
841 //lyxerr << "read \\def parameter list '" << pars << "'" << endl;
843 } else { // t.cs() == "newcommand" || t.cs() == "renewcommand"
845 if (getToken().cat() != catBegin) {
846 error("'{' in \\newcommand expected (1) ");
850 name = getToken().cs();
852 if (getToken().cat() != catEnd) {
853 error("'}' in \\newcommand expected");
857 string arg = getArg('[', ']');
864 parse(ar1, FLAG_ITEM, MathInset::UNDECIDED_MODE);
866 // we cannot handle recursive stuff at all
868 //test.push_back(createMathInset(name));
869 //if (ar1.contains(test)) {
870 // error("we cannot handle recursive macros at all.");
874 // is a version for display attached?
877 if (nextToken().cat() == catBegin)
878 parse(ar2, FLAG_ITEM, MathInset::MATH_MODE);
880 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, type,
884 else if (t.cs() == "(") {
885 cell->push_back(MathAtom(new MathHullInset("simple")));
886 parse2(cell->back(), FLAG_SIMPLE2, MathInset::MATH_MODE, false);
889 else if (t.cs() == "[") {
890 cell->push_back(MathAtom(new MathHullInset("equation")));
891 parse2(cell->back(), FLAG_EQUATION, MathInset::MATH_MODE, false);
894 else if (t.cs() == "protect")
895 // ignore \\protect, will hopefully be re-added during output
898 else if (t.cs() == "end") {
899 if (flags & FLAG_END) {
900 // eat environment name
901 //string const name =
903 // FIXME: check that we ended the correct environment
906 error("found 'end' unexpectedly");
909 else if (t.cs() == ")") {
910 if (flags & FLAG_SIMPLE2)
912 error("found '\\)' unexpectedly");
915 else if (t.cs() == "]") {
916 if (flags & FLAG_EQUATION)
918 error("found '\\]' unexpectedly");
921 else if (t.cs() == "\\") {
922 if (flags & FLAG_ALIGN)
924 if (addRow(grid, cellrow, getArg('[', ']'))) {
926 if (grid.asHullInset())
927 grid.asHullInset()->numbered(
929 cell = &grid.cell(grid.index(cellrow,
935 else if (t.cs() == "multicolumn") {
936 // extract column count and insert dummy cells
938 parse(count, FLAG_ITEM, mode);
940 if (!extractNumber(count, cols)) {
941 lyxerr << " can't extract number of cells from " << count << endl;
943 // resize the table if necessary
944 for (int i = 0; i < cols; ++i) {
945 if (addCol(grid, cellcol)) {
946 cell = &grid.cell(grid.index(
948 // mark this as dummy
949 grid.cellinfo(grid.index(
950 cellrow, cellcol)).dummy_ = true;
953 // the last cell is the real thing, not a dummy
954 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
956 // read special alignment
958 parse(align, FLAG_ITEM, mode);
959 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
961 // parse the remaining contents into the "real" cell
962 parse(*cell, FLAG_ITEM, mode);
966 else if (t.cs() == "limits")
969 else if (t.cs() == "nolimits")
972 else if (t.cs() == "nonumber") {
973 if (grid.asHullInset())
974 grid.asHullInset()->numbered(cellrow, false);
977 else if (t.cs() == "number") {
978 if (grid.asHullInset())
979 grid.asHullInset()->numbered(cellrow, true);
982 else if (t.cs() == "hline") {
983 grid.rowinfo(cellrow).lines_ ++;
986 else if (t.cs() == "sqrt") {
988 parse(ar, FLAG_OPTION, mode);
990 cell->push_back(MathAtom(new MathRootInset));
991 cell->back().nucleus()->cell(0) = ar;
992 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
994 cell->push_back(MathAtom(new MathSqrtInset));
995 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
999 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1000 cell->push_back(createMathInset(t.cs()));
1001 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1002 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1005 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
1006 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1007 cell->push_back(MathAtom(new RefInset(t.cs())));
1008 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1009 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1012 else if (t.cs() == "left") {
1014 string l = getToken().asString();
1016 parse(ar, FLAG_RIGHT, mode);
1018 string r = getToken().asString();
1019 cell->push_back(MathAtom(new MathDelimInset(l, r, ar)));
1022 else if (t.cs() == "right") {
1023 if (flags & FLAG_RIGHT)
1025 //lyxerr << "got so far: '" << cell << "'" << endl;
1026 error("Unmatched right delimiter");
1030 else if (t.cs() == "begin") {
1031 string const name = getArg('{', '}');
1033 if (name == "array" || name == "subarray") {
1034 string const valign = parse_verbatim_option() + 'c';
1035 string const halign = parse_verbatim_item();
1036 cell->push_back(MathAtom(new MathArrayInset(name, valign[0], halign)));
1037 parse2(cell->back(), FLAG_END, mode, false);
1040 else if (name == "tabular") {
1041 string const valign = parse_verbatim_option() + 'c';
1042 string const halign = parse_verbatim_item();
1043 cell->push_back(MathAtom(new MathTabularInset(name, valign[0], halign)));
1044 parse2(cell->back(), FLAG_END, MathInset::TEXT_MODE, false);
1047 else if (name == "split" || name == "cases" ||
1048 name == "gathered" || name == "aligned" ||
1049 name == "alignedat") {
1050 cell->push_back(createMathInset(name));
1051 parse2(cell->back(), FLAG_END, mode, false);
1054 else if (name == "math") {
1055 cell->push_back(MathAtom(new MathHullInset("simple")));
1056 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, true);
1059 else if (name == "equation" || name == "equation*"
1060 || name == "displaymath") {
1061 cell->push_back(MathAtom(new MathHullInset("equation")));
1062 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, (name == "equation"));
1065 else if (name == "eqnarray" || name == "eqnarray*") {
1066 cell->push_back(MathAtom(new MathHullInset("eqnarray")));
1067 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1070 else if (name == "align" || name == "align*") {
1071 cell->push_back(MathAtom(new MathHullInset("align")));
1072 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1075 else if (name == "flalign" || name == "flalign*") {
1076 cell->push_back(MathAtom(new MathHullInset("flalign")));
1077 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1080 else if (name == "alignat" || name == "alignat*") {
1081 // ignore this for a while
1083 cell->push_back(MathAtom(new MathHullInset("alignat")));
1084 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1087 else if (name == "xalignat" || name == "xalignat*") {
1088 // ignore this for a while
1090 cell->push_back(MathAtom(new MathHullInset("xalignat")));
1091 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1094 else if (name == "xxalignat") {
1095 // ignore this for a while
1097 cell->push_back(MathAtom(new MathHullInset("xxalignat")));
1098 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1101 else if (name == "multline" || name == "multline*") {
1102 cell->push_back(MathAtom(new MathHullInset("multline")));
1103 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1106 else if (name == "gather" || name == "gather*") {
1107 cell->push_back(MathAtom(new MathHullInset("gather")));
1108 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1111 else if (latexkeys const * l = in_word_set(name)) {
1112 if (l->inset == "matrix") {
1113 cell->push_back(createMathInset(name));
1114 parse2(cell->back(), FLAG_END, mode, false);
1120 lyxerr << "found unknown math environment '" << name << "'" << endl;
1121 // create generic environment inset
1122 cell->push_back(MathAtom(new MathEnvInset(name)));
1123 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1127 else if (t.cs() == "kern") {
1128 #ifdef WITH_WARNINGS
1133 Token const & t = getToken();
1139 if (isValidLength(s))
1142 cell->push_back(MathAtom(new MathKernInset(s)));
1145 else if (t.cs() == "label") {
1146 string label = parse_verbatim_item();
1149 if (grid.asHullInset()) {
1150 grid.asHullInset()->label(cellrow, label);
1152 cell->push_back(createMathInset(t.cs()));
1153 cell->push_back(MathAtom(new MathBraceInset(ar)));
1157 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1158 MathAtom at = createMathInset(t.cs());
1159 at.nucleus()->cell(0) = *cell;
1161 parse(at.nucleus()->cell(1), flags, mode);
1162 cell->push_back(at);
1166 else if (t.cs() == "color") {
1167 MathAtom at = createMathInset(t.cs());
1168 parse(at.nucleus()->cell(0), FLAG_ITEM, MathInset::TEXT_MODE);
1169 parse(at.nucleus()->cell(1), flags, mode);
1170 cell->push_back(at);
1174 else if (t.cs() == "substack") {
1175 cell->push_back(createMathInset(t.cs()));
1176 parse2(cell->back(), FLAG_ITEM, mode, false);
1179 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1180 cell->push_back(createMathInset(t.cs()));
1181 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, MathInset::TEXT_MODE);
1182 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, MathInset::TEXT_MODE);
1183 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, MathInset::TEXT_MODE);
1187 else if (t.cs() == "infer") {
1189 parse(ar, FLAG_OPTION, mode);
1190 cell->push_back(createMathInset(t.cs()));
1191 parse2(cell->back(), FLAG_ITEM, mode, false);
1195 else if (1 && t.cs() == "ar") {
1196 auto_ptr<MathXYArrowInset> p(new MathXYArrowInset);
1197 // try to read target
1198 parse(p->cell(0), FLAG_OTPTION, mode);
1199 // try to read label
1200 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1201 p->up_ = nextToken().cat() == catSuper;
1203 parse(p->cell(1), FLAG_ITEM, mode);
1204 //lyxerr << "read label: " << p->cell(1) << endl;
1207 cell->push_back(MathAtom(p.release()));
1208 //lyxerr << "read cell: " << cell << endl;
1212 else if (t.cs().size()) {
1213 latexkeys const * l = in_word_set(t.cs());
1215 if (l->inset == "font") {
1216 cell->push_back(createMathInset(t.cs()));
1217 parse(cell->back().nucleus()->cell(0),
1218 FLAG_ITEM, asMode(mode, l->extra));
1221 else if (l->inset == "oldfont") {
1222 cell->push_back(createMathInset(t.cs()));
1223 parse(cell->back().nucleus()->cell(0),
1224 flags | FLAG_ALIGN, asMode(mode, l->extra));
1225 if (prevToken().cat() != catAlign &&
1226 prevToken().cs() != "\\")
1231 else if (l->inset == "style") {
1232 cell->push_back(createMathInset(t.cs()));
1233 parse(cell->back().nucleus()->cell(0),
1234 flags | FLAG_ALIGN, mode);
1235 if (prevToken().cat() != catAlign &&
1236 prevToken().cs() != "\\")
1242 MathAtom at = createMathInset(t.cs());
1243 for (MathInset::idx_type i = 0; i < at->nargs(); ++i)
1244 parse(at.nucleus()->cell(i),
1245 FLAG_ITEM, asMode(mode, l->extra));
1246 cell->push_back(at);
1251 MathAtom at = createMathInset(t.cs());
1252 MathInset::mode_type m = mode;
1253 //if (m == MathInset::UNDECIDED_MODE)
1254 //lyxerr << "default creation: m1: " << m << endl;
1255 if (at->currentMode() != MathInset::UNDECIDED_MODE)
1256 m = at->currentMode();
1257 //lyxerr << "default creation: m2: " << m << endl;
1258 MathInset::idx_type start = 0;
1259 // this fails on \bigg[...\bigg]
1261 //parse(opt, FLAG_OPTION, MathInset::VERBATIM_MODE);
1264 // at.nucleus()->cell(0) = opt;
1266 for (MathInset::idx_type i = start; i < at->nargs(); ++i) {
1267 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1270 cell->push_back(at);
1275 if (flags & FLAG_LEAVE) {
1276 flags &= ~FLAG_LEAVE;
1284 } // anonymous namespace
1287 void mathed_parse_cell(MathArray & ar, string const & str)
1289 istringstream is(str);
1290 mathed_parse_cell(ar, is);
1294 void mathed_parse_cell(MathArray & ar, istream & is)
1296 Parser(is).parse(ar, 0, MathInset::MATH_MODE);
1300 bool mathed_parse_normal(MathAtom & t, string const & str)
1302 istringstream is(str);
1303 return Parser(is).parse(t);
1307 bool mathed_parse_normal(MathAtom & t, istream & is)
1309 return Parser(is).parse(t);
1313 bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
1315 return Parser(lex).parse(t);
1319 void mathed_parse_normal(MathGridInset & grid, string const & str)
1321 istringstream is(str);
1322 Parser(is).parse1(grid, 0, MathInset::MATH_MODE, false);
1328 fill(theCatcode, theCatcode + 256, catOther);
1329 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1330 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1332 theCatcode[int('\\')] = catEscape;
1333 theCatcode[int('{')] = catBegin;
1334 theCatcode[int('}')] = catEnd;
1335 theCatcode[int('$')] = catMath;
1336 theCatcode[int('&')] = catAlign;
1337 theCatcode[int('\n')] = catNewline;
1338 theCatcode[int('#')] = catParameter;
1339 theCatcode[int('^')] = catSuper;
1340 theCatcode[int('_')] = catSub;
1341 theCatcode[int(0x7f)] = catIgnore;
1342 theCatcode[int(' ')] = catSpace;
1343 theCatcode[int('\t')] = catSpace;
1344 theCatcode[int('\r')] = catNewline;
1345 theCatcode[int('~')] = catActive;
1346 theCatcode[int('%')] = catComment;