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 "MathParser.h"
43 #include "InsetMathArray.h"
44 #include "InsetMathBig.h"
45 #include "InsetMathBrace.h"
46 #include "InsetMathChar.h"
47 #include "InsetMathColor.h"
48 #include "InsetMathComment.h"
49 #include "InsetMathDelim.h"
50 #include "InsetMathEnv.h"
51 #include "InsetMathKern.h"
52 #include "InsetMathMacro.h"
53 #include "InsetMathPar.h"
54 #include "InsetMathRef.h"
55 #include "InsetMathRoot.h"
56 #include "InsetMathScript.h"
57 #include "InsetMathSplit.h"
58 #include "InsetMathSqrt.h"
59 #include "InsetMathTabular.h"
60 #include "MathMacroTemplate.h"
61 #include "MathFactory.h"
62 #include "MathMacroArgument.h"
63 #include "MathSupport.h"
68 #include "support/convert.h"
81 using std::istringstream;
91 InsetMath::mode_type asMode(InsetMath::mode_type oldmode, docstring const & str)
93 //lyxerr << "handling mode: '" << str << "'" << endl;
94 if (str == "mathmode")
95 return InsetMath::MATH_MODE;
96 if (str == "textmode" || str == "forcetext")
97 return InsetMath::TEXT_MODE;
102 bool stared(docstring const & s)
104 size_t const n = s.size();
105 return n && s[n - 1] == '*';
110 * Add the row \p cellrow to \p grid.
111 * \returns wether the row could be added. Adding a row can fail for
112 * environments like "equation" that have a fixed number of rows.
114 bool addRow(InsetMathGrid & grid, InsetMathGrid::row_type & cellrow,
115 docstring const & vskip)
118 if (cellrow == grid.nrows()) {
119 //lyxerr << "adding row " << cellrow << endl;
120 grid.addRow(cellrow - 1);
121 if (cellrow == grid.nrows()) {
122 // We can't add a row to this grid, so let's
123 // append the content of this cell to the previous
125 // This does not happen in well formed .lyx files,
126 // but LyX versions 1.3.x and older could create
127 // such files and tex2lyx can still do that.
129 lyxerr << "ignoring extra row";
131 lyxerr << " with extra space " << to_utf8(vskip);
132 lyxerr << '.' << endl;
136 grid.vcrskip(LyXLength(to_utf8(vskip)), cellrow - 1);
142 * Add the column \p cellcol to \p grid.
143 * \returns wether the column could be added. Adding a column can fail for
144 * environments like "eqnarray" that have a fixed number of columns.
146 bool addCol(InsetMathGrid & grid, InsetMathGrid::col_type & cellcol)
149 if (cellcol == grid.ncols()) {
150 //lyxerr << "adding column " << cellcol << endl;
151 grid.addCol(cellcol - 1);
152 if (cellcol == grid.ncols()) {
153 // We can't add a column to this grid, so let's
154 // append the content of this cell to the previous
156 // This does not happen in well formed .lyx files,
157 // but LyX versions 1.3.x and older could create
158 // such files and tex2lyx can still do that.
160 lyxerr << "ignoring extra column." << endl;
169 * Check wether the last row is empty and remove it if yes.
170 * Otherwise the following code
178 * will result in a grid with 3 rows (+ the dummy row that is always present),
179 * because the last '\\' opens a new row.
181 void delEmptyLastRow(InsetMathGrid & grid)
183 InsetMathGrid::row_type const row = grid.nrows() - 1;
184 for (InsetMathGrid::col_type col = 0; col < grid.ncols(); ++col) {
185 if (!grid.cell(grid.index(row, col)).empty())
188 // Copy the row information of the empty row (which would contain the
189 // last hline in the example above) to the dummy row and delete the
191 grid.rowinfo(row + 1) = grid.rowinfo(row);
196 // These are TeX's catcodes
198 catEscape, // 0 backslash
208 catSpace, // 10 space
209 catLetter, // 11 a-zA-Z
210 catOther, // 12 none of the above
213 catInvalid // 15 <delete>
216 CatCode theCatcode[128];
219 inline CatCode catcode(char_type c)
221 /* The only characters that are not catOther lie in the pure ASCII
222 * range. Therefore theCatcode has only 128 entries.
223 * TeX itself deals with 8bit characters, so if needed this table
224 * could be enlarged to 256 entries.
225 * Any larger value does not make sense, since the fact that we use
226 * unicode internally does not change Knuth's TeX engine.
227 * Apart from that a table for the full 21bit UCS4 range would waste
228 * too much memory. */
232 return theCatcode[c];
237 FLAG_ALIGN = 1 << 0, // next & or \\ ends the parsing process
238 FLAG_BRACE_LAST = 1 << 1, // next closing brace ends the parsing
239 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
240 FLAG_END = 1 << 3, // next \\end ends the parsing process
241 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
242 FLAG_TEXTMODE = 1 << 5, // we are in a box
243 FLAG_ITEM = 1 << 6, // read a (possibly braced token)
244 FLAG_LEAVE = 1 << 7, // leave the loop at the end
245 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
246 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
247 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
248 FLAG_OPTION = 1 << 11, // read [...] style option
249 FLAG_BRACED = 1 << 12 // read {...} style argument
254 // Helper class for parsing
260 Token() : cs_(), char_(0), cat_(catIgnore) {}
262 Token(char_type c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
264 Token(docstring const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
267 docstring const & cs() const { return cs_; }
269 CatCode cat() const { return cat_; }
271 char_type character() const { return char_; }
273 docstring asString() const { return cs_.size() ? cs_ : docstring(1, char_); }
275 docstring asInput() const { return cs_.size() ? '\\' + cs_ : docstring(1, char_); }
286 ostream & operator<<(ostream & os, Token const & t)
289 os << '\\' << t.cs();
290 else if (t.cat() == catLetter)
293 os << '[' << t.character() << ',' << t.cat() << ']';
301 typedef InsetMath::mode_type mode_type;
304 Parser(LyXLex & lex);
306 Parser(istream & is);
309 bool parse(MathAtom & at);
311 void parse(MathArray & array, unsigned flags, mode_type mode);
313 void parse1(InsetMathGrid & grid, unsigned flags, mode_type mode,
316 MathArray parse(unsigned flags, mode_type mode);
318 int lineno() const { return lineno_; }
324 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
325 /// get arg delimited by 'left' and 'right'
326 docstring getArg(char_type left, char_type right);
330 void error(string const & msg);
331 void error(docstring const & msg) { error(to_utf8(msg)); }
332 /// dump contents to screen
335 void tokenize(istream & is);
337 void tokenize(docstring const & s);
339 void skipSpaceTokens(idocstream & is, char_type c);
341 void push_back(Token const & t);
345 Token const & prevToken() const;
347 Token const & nextToken() const;
349 Token const & getToken();
350 /// skips spaces if any
353 void lex(docstring const & s);
357 docstring parse_verbatim_item();
359 docstring parse_verbatim_option();
364 vector<Token> tokens_;
367 /// Stack of active environments
368 vector<docstring> environments_;
372 Parser::Parser(LyXLex & lexer)
373 : lineno_(lexer.getLineNo()), pos_(0)
375 tokenize(lexer.getStream());
380 Parser::Parser(istream & is)
381 : lineno_(0), pos_(0)
387 void Parser::push_back(Token const & t)
389 tokens_.push_back(t);
393 void Parser::pop_back()
399 Token const & Parser::prevToken() const
401 static const Token dummy;
402 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
406 Token const & Parser::nextToken() const
408 static const Token dummy;
409 return good() ? tokens_[pos_] : dummy;
413 Token const & Parser::getToken()
415 static const Token dummy;
416 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
417 return good() ? tokens_[pos_++] : dummy;
421 void Parser::skipSpaces()
423 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
428 void Parser::putback()
434 bool Parser::good() const
436 return pos_ < tokens_.size();
440 char_type Parser::getChar()
443 error("The input stream is not well...");
444 return tokens_[pos_++].character();
448 docstring Parser::getArg(char_type left, char_type right)
453 char_type c = getChar();
458 while ((c = getChar()) != right && good())
465 void Parser::skipSpaceTokens(idocstream & is, char_type c)
467 // skip trailing spaces
468 while (catcode(c) == catSpace || catcode(c) == catNewline)
471 //lyxerr << "putting back: " << c << endl;
476 void Parser::tokenize(istream & is)
478 // eat everything up to the next \end_inset or end of stream
479 // and store it in s for further tokenization
484 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
485 s = s.substr(0, s.size() - 10);
489 // Remove the space after \end_inset
490 if (is.get(c) && c != ' ')
494 tokenize(from_utf8(s));
498 void Parser::tokenize(docstring const & buffer)
500 idocstringstream is(buffer, ios::in | ios::binary);
504 //lyxerr << "reading c: " << c << endl;
506 switch (catcode(c)) {
510 if (catcode(c) == catNewline)
511 ; //push_back(Token("par"));
513 push_back(Token('\n', catNewline));
521 while (is.get(c) && catcode(c) != catNewline)
531 error("unexpected end of input");
534 if (catcode(c) == catLetter) {
536 while (is.get(c) && catcode(c) == catLetter)
538 skipSpaceTokens(is, c);
547 push_back(Token(c, catcode(c)));
549 skipSpaceTokens(is, c);
554 lyxerr << "ignoring a char: " << int(c) << endl;
559 push_back(Token(c, catcode(c)));
569 void Parser::dump() const
571 lyxerr << "\nTokens: ";
572 for (unsigned i = 0; i < tokens_.size(); ++i) {
575 lyxerr << tokens_[i];
577 lyxerr << " pos: " << pos_ << endl;
581 void Parser::error(string const & msg)
583 lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
589 bool Parser::parse(MathAtom & at)
593 parse(ar, false, InsetMath::UNDECIDED_MODE);
594 if (ar.size() != 1 || ar.front()->getType() == hullNone) {
595 lyxerr << "unusual contents found: " << ar << endl;
596 at = MathAtom(new InsetMathPar(ar));
597 //if (at->nargs() > 0)
598 // at.nucleus()->cell(0) = ar;
600 // lyxerr << "unusual contents found: " << ar << endl;
608 docstring Parser::parse_verbatim_option()
612 if (nextToken().character() == '[') {
613 Token t = getToken();
614 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
615 if (t.cat() == catBegin) {
617 res += '{' + parse_verbatim_item() + '}';
626 docstring Parser::parse_verbatim_item()
630 if (nextToken().cat() == catBegin) {
631 Token t = getToken();
632 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
633 if (t.cat() == catBegin) {
635 res += '{' + parse_verbatim_item() + '}';
645 MathArray Parser::parse(unsigned flags, mode_type mode)
648 parse(ar, flags, mode);
653 void Parser::parse(MathArray & array, unsigned flags, mode_type mode)
655 InsetMathGrid grid(1, 1);
656 parse1(grid, flags, mode, false);
657 array = grid.cell(0);
661 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
664 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
668 void Parser::parse1(InsetMathGrid & grid, unsigned flags,
669 const mode_type mode, const bool numbered)
672 InsetMathGrid::row_type cellrow = 0;
673 InsetMathGrid::col_type cellcol = 0;
674 MathArray * cell = &grid.cell(grid.index(cellrow, cellcol));
676 if (grid.asHullInset())
677 grid.asHullInset()->numbered(cellrow, numbered);
680 //lyxerr << " flags: " << flags << endl;
681 //lyxerr << " mode: " << mode << endl;
682 //lyxerr << "grid: " << grid << endl;
685 Token const & t = getToken();
688 lyxerr << "t: " << t << " flags: " << flags << endl;
689 lyxerr << "mode: " << mode << endl;
694 if (flags & FLAG_ITEM) {
696 if (t.cat() == catBegin) {
697 // skip the brace and collect everything to the next matching
699 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
703 // handle only this single token, leave the loop if done
708 if (flags & FLAG_BRACED) {
709 if (t.cat() == catSpace)
712 if (t.cat() != catBegin) {
713 error("opening brace expected");
717 // skip the brace and collect everything to the next matching
719 flags = FLAG_BRACE_LAST;
723 if (flags & FLAG_OPTION) {
724 if (t.cat() == catOther && t.character() == '[') {
726 parse(ar, FLAG_BRACK_LAST, mode);
729 // no option found, put back token and we are done
738 if (t.cat() == catMath) {
739 if (mode != InsetMath::MATH_MODE) {
740 // we are inside some text mode thingy, so opening new math is allowed
741 Token const & n = getToken();
742 if (n.cat() == catMath) {
743 // TeX's $$...$$ syntax for displayed math
744 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
745 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
746 getToken(); // skip the second '$' token
748 // simple $...$ stuff
750 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
751 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
755 else if (flags & FLAG_SIMPLE) {
756 // this is the end of the formula
761 error("something strange in the parser");
766 else if (t.cat() == catLetter)
767 cell->push_back(MathAtom(new InsetMathChar(t.character())));
769 else if (t.cat() == catSpace && mode != InsetMath::MATH_MODE) {
770 if (cell->empty() || cell->back()->getChar() != ' ')
771 cell->push_back(MathAtom(new InsetMathChar(t.character())));
774 else if (t.cat() == catNewline && mode != InsetMath::MATH_MODE) {
775 if (cell->empty() || cell->back()->getChar() != ' ')
776 cell->push_back(MathAtom(new InsetMathChar(' ')));
779 else if (t.cat() == catParameter) {
780 Token const & n = getToken();
781 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
784 else if (t.cat() == catActive)
785 cell->push_back(MathAtom(new InsetMathChar(t.character())));
787 else if (t.cat() == catBegin) {
789 parse(ar, FLAG_BRACE_LAST, mode);
790 // do not create a BraceInset if they were written by LyX
791 // this helps to keep the annoyance of "a choose b" to a minimum
792 if (ar.size() == 1 && ar[0]->extraBraces())
795 cell->push_back(MathAtom(new InsetMathBrace(ar)));
798 else if (t.cat() == catEnd) {
799 if (flags & FLAG_BRACE_LAST)
801 error("found '}' unexpectedly");
802 //BOOST_ASSERT(false);
803 //add(cell, '}', LM_TC_TEX);
806 else if (t.cat() == catAlign) {
807 //lyxerr << " column now " << (cellcol + 1)
808 // << " max: " << grid.ncols() << endl;
809 if (flags & FLAG_ALIGN)
811 if (addCol(grid, cellcol))
812 cell = &grid.cell(grid.index(cellrow, cellcol));
815 else if (t.cat() == catSuper || t.cat() == catSub) {
816 bool up = (t.cat() == catSuper);
817 // we need no new script inset if the last thing was a scriptinset,
818 // which has that script already not the same script already
820 cell->push_back(MathAtom(new InsetMathScript(up)));
821 else if (cell->back()->asScriptInset() &&
822 !cell->back()->asScriptInset()->has(up))
823 cell->back().nucleus()->asScriptInset()->ensure(up);
824 else if (cell->back()->asScriptInset())
825 cell->push_back(MathAtom(new InsetMathScript(up)));
827 cell->back() = MathAtom(new InsetMathScript(cell->back(), up));
828 InsetMathScript * p = cell->back().nucleus()->asScriptInset();
829 // special handling of {}-bases
830 // Here we could remove the brace inset for things
831 // like {a'}^2 and add the braces back in
832 // InsetMathScript::write().
833 // We do not do it, since it is not possible to detect
834 // reliably whether the braces are needed because the
835 // nucleus contains more than one symbol, or whether
836 // they are needed for unknown commands like \xx{a}_0
837 // or \yy{a}{b}_0. This was done in revision 14819
838 // in an unreliable way. See this thread
839 // http://www.mail-archive.com/lyx-devel%40lists.lyx.org/msg104917.html
841 parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
848 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
849 //lyxerr << "finished reading option" << endl;
853 else if (t.cat() == catOther)
854 cell->push_back(MathAtom(new InsetMathChar(t.character())));
856 else if (t.cat() == catComment) {
859 Token const & t = getToken();
860 if (t.cat() == catNewline)
864 cell->push_back(MathAtom(new InsetMathComment(s)));
872 else if (t.cs() == "lyxlock") {
874 cell->back().nucleus()->lock(true);
877 else if (t.cs() == "def" ||
878 t.cs() == "newcommand" ||
879 t.cs() == "renewcommand")
881 docstring const type = t.cs();
884 if (t.cs() == "def") {
886 name = getToken().cs();
890 while (good() && nextToken().cat() != catBegin) {
891 pars += getToken().cs();
895 //lyxerr << "read \\def parameter list '" << pars << "'" << endl;
897 } else { // t.cs() == "newcommand" || t.cs() == "renewcommand"
899 if (getToken().cat() != catBegin) {
900 error("'{' in \\newcommand expected (1) ");
904 name = getToken().cs();
906 if (getToken().cat() != catEnd) {
907 error("'}' in \\newcommand expected");
911 docstring const arg = getArg('[', ']');
913 nargs = convert<int>(arg);
918 parse(ar1, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
920 // we cannot handle recursive stuff at all
922 //test.push_back(createInsetMath(name));
923 //if (ar1.contains(test)) {
924 // error("we cannot handle recursive macros at all.");
928 // is a version for display attached?
931 if (nextToken().cat() == catBegin)
932 parse(ar2, FLAG_ITEM, InsetMath::MATH_MODE);
934 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, type,
938 else if (t.cs() == "(") {
939 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
940 parse2(cell->back(), FLAG_SIMPLE2, InsetMath::MATH_MODE, false);
943 else if (t.cs() == "[") {
944 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
945 parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false);
948 else if (t.cs() == "protect")
949 // ignore \\protect, will hopefully be re-added during output
952 else if (t.cs() == "end") {
953 if (flags & FLAG_END) {
954 // eat environment name
955 docstring const name = getArg('{', '}');
956 if (environments_.empty())
957 error("'found \\end{" + name +
958 "}' without matching '\\begin{" +
960 else if (name != environments_.back())
961 error("'\\end{" + name +
962 "}' does not match '\\begin{" +
963 environments_.back() + "}'");
965 environments_.pop_back();
966 // Delete empty last row in matrix
968 // If you abuse InsetMathGrid for
969 // non-matrix like structures you
970 // probably need to refine this test.
971 // Right now we only have to test for
972 // single line hull insets.
973 if (grid.nrows() > 1)
974 delEmptyLastRow(grid);
978 error("found 'end' unexpectedly");
981 else if (t.cs() == ")") {
982 if (flags & FLAG_SIMPLE2)
984 error("found '\\)' unexpectedly");
987 else if (t.cs() == "]") {
988 if (flags & FLAG_EQUATION)
990 error("found '\\]' unexpectedly");
993 else if (t.cs() == "\\") {
994 if (flags & FLAG_ALIGN)
996 if (addRow(grid, cellrow, getArg('[', ']'))) {
998 if (grid.asHullInset())
999 grid.asHullInset()->numbered(
1001 cell = &grid.cell(grid.index(cellrow,
1007 else if (t.cs() == "multicolumn") {
1008 // extract column count and insert dummy cells
1010 parse(count, FLAG_ITEM, mode);
1012 if (!extractNumber(count, cols)) {
1013 lyxerr << " can't extract number of cells from " << count << endl;
1015 // resize the table if necessary
1016 for (int i = 0; i < cols; ++i) {
1017 if (addCol(grid, cellcol)) {
1018 cell = &grid.cell(grid.index(
1020 // mark this as dummy
1021 grid.cellinfo(grid.index(
1022 cellrow, cellcol)).dummy_ = true;
1025 // the last cell is the real thing, not a dummy
1026 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
1028 // read special alignment
1030 parse(align, FLAG_ITEM, mode);
1031 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
1033 // parse the remaining contents into the "real" cell
1034 parse(*cell, FLAG_ITEM, mode);
1038 else if (t.cs() == "limits")
1041 else if (t.cs() == "nolimits")
1044 else if (t.cs() == "nonumber") {
1045 if (grid.asHullInset())
1046 grid.asHullInset()->numbered(cellrow, false);
1049 else if (t.cs() == "number") {
1050 if (grid.asHullInset())
1051 grid.asHullInset()->numbered(cellrow, true);
1054 else if (t.cs() == "hline") {
1055 grid.rowinfo(cellrow).lines_ ++;
1058 else if (t.cs() == "sqrt") {
1060 parse(ar, FLAG_OPTION, mode);
1062 cell->push_back(MathAtom(new InsetMathRoot));
1063 cell->back().nucleus()->cell(0) = ar;
1064 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1066 cell->push_back(MathAtom(new InsetMathSqrt));
1067 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1071 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1072 cell->push_back(createInsetMath(t.cs()));
1073 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1074 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1077 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
1078 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1079 cell->push_back(MathAtom(new RefInset(t.cs())));
1080 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1081 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1084 else if (t.cs() == "left") {
1086 Token const & tl = getToken();
1087 // \| and \Vert are equivalent, and InsetMathDelim
1089 // FIXME: fix this in InsetMathDelim itself!
1090 docstring const l = tl.cs() == "|" ? from_ascii("Vert") : tl.asString();
1092 parse(ar, FLAG_RIGHT, mode);
1094 Token const & tr = getToken();
1095 docstring const r = tr.cs() == "|" ? from_ascii("Vert") : tr.asString();
1096 cell->push_back(MathAtom(new InsetMathDelim(l, r, ar)));
1099 else if (t.cs() == "right") {
1100 if (flags & FLAG_RIGHT)
1102 //lyxerr << "got so far: '" << cell << "'" << endl;
1103 error("Unmatched right delimiter");
1107 else if (t.cs() == "begin") {
1108 docstring const name = getArg('{', '}');
1109 environments_.push_back(name);
1111 if (name == "array" || name == "subarray") {
1112 docstring const valign = parse_verbatim_option() + 'c';
1113 docstring const halign = parse_verbatim_item();
1114 cell->push_back(MathAtom(new InsetMathArray(name, (char)valign[0], halign)));
1115 parse2(cell->back(), FLAG_END, mode, false);
1118 else if (name == "tabular") {
1119 docstring const valign = parse_verbatim_option() + 'c';
1120 docstring const halign = parse_verbatim_item();
1121 cell->push_back(MathAtom(new InsetMathTabular(name, (char)valign[0], halign)));
1122 parse2(cell->back(), FLAG_END, InsetMath::TEXT_MODE, false);
1125 else if (name == "split" || name == "cases") {
1126 cell->push_back(createInsetMath(name));
1127 parse2(cell->back(), FLAG_END, mode, false);
1130 else if (name == "alignedat") {
1131 docstring const valign = parse_verbatim_option() + 'c';
1132 // ignore this for a while
1134 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1135 parse2(cell->back(), FLAG_END, mode, false);
1138 else if (name == "math") {
1139 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
1140 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, true);
1143 else if (name == "equation" || name == "equation*"
1144 || name == "displaymath") {
1145 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
1146 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation"));
1149 else if (name == "eqnarray" || name == "eqnarray*") {
1150 cell->push_back(MathAtom(new InsetMathHull(hullEqnArray)));
1151 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1154 else if (name == "align" || name == "align*") {
1155 cell->push_back(MathAtom(new InsetMathHull(hullAlign)));
1156 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1159 else if (name == "flalign" || name == "flalign*") {
1160 cell->push_back(MathAtom(new InsetMathHull(hullFlAlign)));
1161 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1164 else if (name == "alignat" || name == "alignat*") {
1165 // ignore this for a while
1167 cell->push_back(MathAtom(new InsetMathHull(hullAlignAt)));
1168 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1171 else if (name == "xalignat" || name == "xalignat*") {
1172 // ignore this for a while
1174 cell->push_back(MathAtom(new InsetMathHull(hullXAlignAt)));
1175 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1178 else if (name == "xxalignat") {
1179 // ignore this for a while
1181 cell->push_back(MathAtom(new InsetMathHull(hullXXAlignAt)));
1182 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1185 else if (name == "multline" || name == "multline*") {
1186 cell->push_back(MathAtom(new InsetMathHull(hullMultline)));
1187 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1190 else if (name == "gather" || name == "gather*") {
1191 cell->push_back(MathAtom(new InsetMathHull(hullGather)));
1192 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1195 else if (latexkeys const * l = in_word_set(name)) {
1196 if (l->inset == "matrix") {
1197 cell->push_back(createInsetMath(name));
1198 parse2(cell->back(), FLAG_END, mode, false);
1199 } else if (l->inset == "split") {
1200 docstring const valign = parse_verbatim_option() + 'c';
1201 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1202 parse2(cell->back(), FLAG_END, mode, false);
1205 lyxerr << "found math environment `" << name
1206 << "' in symbols file with unsupported inset `"
1207 << l->inset << "'." << endl;
1208 // create generic environment inset
1209 cell->push_back(MathAtom(new InsetMathEnv(name)));
1210 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1216 lyxerr << "found unknown math environment '" << name << "'" << endl;
1217 // create generic environment inset
1218 cell->push_back(MathAtom(new InsetMathEnv(name)));
1219 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1223 else if (t.cs() == "kern") {
1224 #ifdef WITH_WARNINGS
1229 Token const & t = getToken();
1235 if (isValidLength(to_utf8(s)))
1238 cell->push_back(MathAtom(new InsetMathKern(s)));
1241 else if (t.cs() == "label") {
1242 // FIXME: This is swallowed in inline formulas
1243 docstring label = parse_verbatim_item();
1246 if (grid.asHullInset()) {
1247 grid.asHullInset()->label(cellrow, label);
1249 cell->push_back(createInsetMath(t.cs()));
1250 cell->push_back(MathAtom(new InsetMathBrace(ar)));
1254 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1255 MathAtom at = createInsetMath(t.cs());
1256 at.nucleus()->cell(0) = *cell;
1258 parse(at.nucleus()->cell(1), flags, mode);
1259 cell->push_back(at);
1263 else if (t.cs() == "color") {
1264 docstring const color = parse_verbatim_item();
1265 cell->push_back(MathAtom(new InsetMathColor(true, color)));
1266 parse(cell->back().nucleus()->cell(0), flags, mode);
1270 else if (t.cs() == "textcolor") {
1271 docstring const color = parse_verbatim_item();
1272 cell->push_back(MathAtom(new InsetMathColor(false, color)));
1273 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1276 else if (t.cs() == "normalcolor") {
1277 cell->push_back(createInsetMath(t.cs()));
1278 parse(cell->back().nucleus()->cell(0), flags, mode);
1282 else if (t.cs() == "substack") {
1283 cell->push_back(createInsetMath(t.cs()));
1284 parse2(cell->back(), FLAG_ITEM, mode, false);
1287 else if (t.cs() == "xymatrix") {
1288 odocstringstream os;
1289 while (good() && nextToken().cat() != catBegin)
1290 os << getToken().asInput();
1291 cell->push_back(createInsetMath(t.cs() + os.str()));
1292 parse2(cell->back(), FLAG_ITEM, mode, false);
1295 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1296 cell->push_back(createInsetMath(t.cs()));
1297 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, InsetMath::TEXT_MODE);
1298 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, InsetMath::TEXT_MODE);
1299 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, InsetMath::TEXT_MODE);
1302 else if (t.cs() == "tag") {
1303 if (nextToken().character() == '*') {
1305 cell->push_back(createInsetMath(t.cs() + '*'));
1307 cell->push_back(createInsetMath(t.cs()));
1308 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1312 else if (t.cs() == "infer") {
1314 parse(ar, FLAG_OPTION, mode);
1315 cell->push_back(createInsetMath(t.cs()));
1316 parse2(cell->back(), FLAG_ITEM, mode, false);
1320 else if (1 && t.cs() == "ar") {
1321 auto_ptr<InsetMathXYArrow> p(new InsetMathXYArrow);
1322 // try to read target
1323 parse(p->cell(0), FLAG_OTPTION, mode);
1324 // try to read label
1325 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1326 p->up_ = nextToken().cat() == catSuper;
1328 parse(p->cell(1), FLAG_ITEM, mode);
1329 //lyxerr << "read label: " << p->cell(1) << endl;
1332 cell->push_back(MathAtom(p.release()));
1333 //lyxerr << "read cell: " << cell << endl;
1337 else if (t.cs().size()) {
1338 latexkeys const * l = in_word_set(t.cs());
1340 if (l->inset == "big") {
1342 docstring const delim = getToken().asInput();
1343 if (InsetMathBig::isBigInsetDelim(delim))
1344 cell->push_back(MathAtom(
1345 new InsetMathBig(t.cs(), delim)));
1347 cell->push_back(createInsetMath(t.cs()));
1352 else if (l->inset == "font") {
1353 cell->push_back(createInsetMath(t.cs()));
1354 parse(cell->back().nucleus()->cell(0),
1355 FLAG_ITEM, asMode(mode, l->extra));
1358 else if (l->inset == "oldfont") {
1359 cell->push_back(createInsetMath(t.cs()));
1360 parse(cell->back().nucleus()->cell(0),
1361 flags | FLAG_ALIGN, asMode(mode, l->extra));
1362 if (prevToken().cat() != catAlign &&
1363 prevToken().cs() != "\\")
1368 else if (l->inset == "style") {
1369 cell->push_back(createInsetMath(t.cs()));
1370 parse(cell->back().nucleus()->cell(0),
1371 flags | FLAG_ALIGN, mode);
1372 if (prevToken().cat() != catAlign &&
1373 prevToken().cs() != "\\")
1379 MathAtom at = createInsetMath(t.cs());
1380 for (InsetMath::idx_type i = 0; i < at->nargs(); ++i)
1381 parse(at.nucleus()->cell(i),
1382 FLAG_ITEM, asMode(mode, l->extra));
1383 cell->push_back(at);
1388 MathAtom at = createInsetMath(t.cs());
1389 InsetMath::mode_type m = mode;
1390 //if (m == InsetMath::UNDECIDED_MODE)
1391 //lyxerr << "default creation: m1: " << m << endl;
1392 if (at->currentMode() != InsetMath::UNDECIDED_MODE)
1393 m = at->currentMode();
1394 //lyxerr << "default creation: m2: " << m << endl;
1395 InsetMath::idx_type start = 0;
1396 // this fails on \bigg[...\bigg]
1398 //parse(opt, FLAG_OPTION, InsetMath::VERBATIM_MODE);
1401 // at.nucleus()->cell(0) = opt;
1403 for (InsetMath::idx_type i = start; i < at->nargs(); ++i) {
1404 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1407 cell->push_back(at);
1412 if (flags & FLAG_LEAVE) {
1413 flags &= ~FLAG_LEAVE;
1421 } // anonymous namespace
1424 void mathed_parse_cell(MathArray & ar, docstring const & str)
1426 istringstream is(to_utf8(str));
1427 mathed_parse_cell(ar, is);
1431 void mathed_parse_cell(MathArray & ar, istream & is)
1433 Parser(is).parse(ar, 0, InsetMath::MATH_MODE);
1437 bool mathed_parse_normal(MathAtom & t, string const & str)
1439 istringstream is(str);
1440 return Parser(is).parse(t);
1444 bool mathed_parse_normal(MathAtom & t, istream & is)
1446 return Parser(is).parse(t);
1450 bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
1452 return Parser(lex).parse(t);
1456 void mathed_parse_normal(InsetMathGrid & grid, string const & str)
1458 istringstream is(str);
1459 Parser(is).parse1(grid, 0, InsetMath::MATH_MODE, false);
1465 fill(theCatcode, theCatcode + 128, catOther);
1466 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1467 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1469 theCatcode[int('\\')] = catEscape;
1470 theCatcode[int('{')] = catBegin;
1471 theCatcode[int('}')] = catEnd;
1472 theCatcode[int('$')] = catMath;
1473 theCatcode[int('&')] = catAlign;
1474 theCatcode[int('\n')] = catNewline;
1475 theCatcode[int('#')] = catParameter;
1476 theCatcode[int('^')] = catSuper;
1477 theCatcode[int('_')] = catSub;
1478 theCatcode[int(0x7f)] = catIgnore;
1479 theCatcode[int(' ')] = catSpace;
1480 theCatcode[int('\t')] = catSpace;
1481 theCatcode[int('\r')] = catNewline;
1482 theCatcode[int('~')] = catActive;
1483 theCatcode[int('%')] = catComment;