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"
68 #ifndef CXX_GLOBAL_CSTD
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] == '*';
105 // These are TeX's catcodes
107 catEscape, // 0 backslash
117 catSpace, // 10 space
118 catLetter, // 11 a-zA-Z
119 catOther, // 12 none of the above
122 catInvalid // 15 <delete>
125 CatCode theCatcode[256];
128 inline CatCode catcode(unsigned char c)
130 return theCatcode[c];
135 FLAG_BRACE_LAST = 1 << 1, // last closing brace ends the parsing
136 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
137 FLAG_END = 1 << 3, // next \\end ends the parsing process
138 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
139 FLAG_TEXTMODE = 1 << 5, // we are in a box
140 FLAG_ITEM = 1 << 6, // read a (possibly braced token)
141 FLAG_LEAVE = 1 << 7, // leave the loop at the end
142 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
143 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
144 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
145 FLAG_OPTION = 1 << 11, // read [...] style option
146 FLAG_BRACED = 1 << 12 // read {...} style argument
151 // Helper class for parsing
157 Token() : cs_(), char_(0), cat_(catIgnore) {}
159 Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
161 Token(string const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
164 string const & cs() const { return cs_; }
166 CatCode cat() const { return cat_; }
168 char character() const { return char_; }
170 string asString() const { return cs_.size() ? cs_ : string(1, char_); }
181 ostream & operator<<(ostream & os, Token const & t)
184 os << '\\' << t.cs();
185 else if (t.cat() == catLetter)
188 os << '[' << t.character() << ',' << t.cat() << ']';
196 typedef MathInset::mode_type mode_type;
199 Parser(LyXLex & lex);
201 Parser(istream & is);
204 bool parse(MathAtom & at);
206 void parse(MathArray & array, unsigned flags, mode_type mode);
208 void parse1(MathGridInset & grid, unsigned flags, mode_type mode,
211 MathArray parse(unsigned flags, mode_type mode);
213 int lineno() const { return lineno_; }
219 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
220 /// get arg delimited by 'left' and 'right'
221 string getArg(char left, char right);
225 void error(string const & msg);
226 /// dump contents to screen
229 void tokenize(istream & is);
231 void tokenize(string const & s);
233 void skipSpaceTokens(istream & is, char c);
235 void push_back(Token const & t);
239 Token const & prevToken() const;
241 Token const & nextToken() const;
243 Token const & getToken();
244 /// skips spaces if any
247 void lex(string const & s);
251 string parse_verbatim_item();
253 string parse_verbatim_option();
258 vector<Token> tokens_;
264 Parser::Parser(LyXLex & lexer)
265 : lineno_(lexer.getLineNo()), pos_(0)
267 tokenize(lexer.getStream());
272 Parser::Parser(istream & is)
273 : lineno_(0), pos_(0)
279 void Parser::push_back(Token const & t)
281 tokens_.push_back(t);
285 void Parser::pop_back()
291 Token const & Parser::prevToken() const
293 static const Token dummy;
294 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
298 Token const & Parser::nextToken() const
300 static const Token dummy;
301 return good() ? tokens_[pos_] : dummy;
305 Token const & Parser::getToken()
307 static const Token dummy;
308 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
309 return good() ? tokens_[pos_++] : dummy;
313 void Parser::skipSpaces()
315 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
320 void Parser::putback()
326 bool Parser::good() const
328 return pos_ < tokens_.size();
332 char Parser::getChar()
335 error("The input stream is not well...");
336 return tokens_[pos_++].character();
340 string Parser::getArg(char left, char right)
350 while ((c = getChar()) != right && good())
357 void Parser::skipSpaceTokens(istream & is, char c)
359 // skip trailing spaces
360 while (catcode(c) == catSpace || catcode(c) == catNewline)
363 //lyxerr << "putting back: " << c << endl;
368 void Parser::tokenize(istream & is)
370 // eat everything up to the next \end_inset or end of stream
371 // and store it in s for further tokenization
376 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
377 s = s.substr(0, s.size() - 10);
381 // Remove the space after \end_inset
382 if (is.get(c) && c != ' ')
390 void Parser::tokenize(string const & buffer)
392 istringstream is(buffer.c_str(), ios::in | ios::binary);
396 //lyxerr << "reading c: " << c << endl;
398 switch (catcode(c)) {
402 if (catcode(c) == catNewline)
403 ; //push_back(Token("par"));
405 push_back(Token('\n', catNewline));
413 while (is.get(c) && catcode(c) != catNewline)
423 error("unexpected end of input");
426 if (catcode(c) == catLetter) {
428 while (is.get(c) && catcode(c) == catLetter)
430 skipSpaceTokens(is, c);
439 push_back(Token(c, catcode(c)));
441 skipSpaceTokens(is, c);
446 lyxerr << "ignoring a char: " << int(c) << endl;
451 push_back(Token(c, catcode(c)));
461 void Parser::dump() const
463 lyxerr << "\nTokens: ";
464 for (unsigned i = 0; i < tokens_.size(); ++i) {
467 lyxerr << tokens_[i];
469 lyxerr << " pos: " << pos_ << endl;
473 void Parser::error(string const & msg)
475 lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
481 bool Parser::parse(MathAtom & at)
485 parse(ar, false, MathInset::UNDECIDED_MODE);
486 if (ar.size() != 1 || ar.front()->getType() == "none") {
487 lyxerr << "unusual contents found: " << ar << endl;
488 at = MathAtom(new MathParInset(ar));
489 //if (at->nargs() > 0)
490 // at.nucleus()->cell(0) = ar;
492 // lyxerr << "unusual contents found: " << ar << endl;
500 string Parser::parse_verbatim_option()
504 if (nextToken().character() == '[') {
505 Token t = getToken();
506 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
507 if (t.cat() == catBegin) {
509 res += '{' + parse_verbatim_item() + '}';
518 string Parser::parse_verbatim_item()
522 if (nextToken().cat() == catBegin) {
523 Token t = getToken();
524 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
525 if (t.cat() == catBegin) {
527 res += '{' + parse_verbatim_item() + '}';
537 MathArray Parser::parse(unsigned flags, mode_type mode)
540 parse(ar, flags, mode);
545 void Parser::parse(MathArray & array, unsigned flags, mode_type mode)
547 MathGridInset grid(1, 1);
548 parse1(grid, flags, mode, false);
549 array = grid.cell(0);
553 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
556 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
560 void Parser::parse1(MathGridInset & grid, unsigned flags,
561 const mode_type mode, const bool numbered)
564 MathGridInset::row_type cellrow = 0;
565 MathGridInset::col_type cellcol = 0;
566 MathArray * cell = &grid.cell(grid.index(cellrow, cellcol));
568 if (grid.asHullInset())
569 grid.asHullInset()->numbered(cellrow, numbered);
572 //lyxerr << " flags: " << flags << endl;
573 //lyxerr << " mode: " << mode << endl;
574 //lyxerr << "grid: " << grid << endl;
577 Token const & t = getToken();
580 lyxerr << "t: " << t << " flags: " << flags << endl;
581 lyxerr << "mode: " << mode << endl;
586 if (flags & FLAG_ITEM) {
588 if (t.cat() == catBegin) {
589 // skip the brace and collect everything to the next matching
591 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
595 // handle only this single token, leave the loop if done
600 if (flags & FLAG_BRACED) {
601 if (t.cat() == catSpace)
604 if (t.cat() != catBegin) {
605 error("opening brace expected");
609 // skip the brace and collect everything to the next matching
611 flags = FLAG_BRACE_LAST;
615 if (flags & FLAG_OPTION) {
616 if (t.cat() == catOther && t.character() == '[') {
618 parse(ar, FLAG_BRACK_LAST, mode);
621 // no option found, put back token and we are done
630 if (t.cat() == catMath) {
631 if (mode != MathInset::MATH_MODE) {
632 // we are inside some text mode thingy, so opening new math is allowed
633 Token const & n = getToken();
634 if (n.cat() == catMath) {
635 // TeX's $$...$$ syntax for displayed math
636 cell->push_back(MathAtom(new MathHullInset("equation")));
637 parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
638 getToken(); // skip the second '$' token
640 // simple $...$ stuff
642 cell->push_back(MathAtom(new MathHullInset("simple")));
643 parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
647 else if (flags & FLAG_SIMPLE) {
648 // this is the end of the formula
653 error("something strange in the parser");
658 else if (t.cat() == catLetter)
659 cell->push_back(MathAtom(new MathCharInset(t.character())));
661 else if (t.cat() == catSpace && mode != MathInset::MATH_MODE) {
662 if (cell->empty() || cell->back()->getChar() != ' ')
663 cell->push_back(MathAtom(new MathCharInset(t.character())));
666 else if (t.cat() == catNewline && mode != MathInset::MATH_MODE) {
667 if (cell->empty() || cell->back()->getChar() != ' ')
668 cell->push_back(MathAtom(new MathCharInset(' ')));
671 else if (t.cat() == catParameter) {
672 Token const & n = getToken();
673 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
676 else if (t.cat() == catActive)
677 cell->push_back(MathAtom(new MathCharInset(t.character())));
679 else if (t.cat() == catBegin) {
681 parse(ar, FLAG_BRACE_LAST, mode);
682 // do not create a BraceInset if they were written by LyX
683 // this helps to keep the annoyance of "a choose b" to a minimum
684 if (ar.size() == 1 && ar[0]->extraBraces())
687 cell->push_back(MathAtom(new MathBraceInset(ar)));
690 else if (t.cat() == catEnd) {
691 if (flags & FLAG_BRACE_LAST)
693 error("found '}' unexpectedly");
694 //BOOST_ASSERT(false);
695 //add(cell, '}', LM_TC_TEX);
698 else if (t.cat() == catAlign) {
700 //lyxerr << " column now " << cellcol << " max: " << grid.ncols() << endl;
701 if (cellcol == grid.ncols()) {
702 //lyxerr << "adding column " << cellcol << endl;
703 grid.addCol(cellcol - 1);
705 cell = &grid.cell(grid.index(cellrow, cellcol));
708 else if (t.cat() == catSuper || t.cat() == catSub) {
709 bool up = (t.cat() == catSuper);
710 // we need no new script inset if the last thing was a scriptinset,
711 // which has that script already not the same script already
713 cell->push_back(MathAtom(new MathScriptInset(up)));
714 else if (cell->back()->asScriptInset() &&
715 !cell->back()->asScriptInset()->has(up))
716 cell->back().nucleus()->asScriptInset()->ensure(up);
717 else if (cell->back()->asScriptInset())
718 cell->push_back(MathAtom(new MathScriptInset(up)));
720 cell->back() = MathAtom(new MathScriptInset(cell->back(), up));
721 MathScriptInset * p = cell->back().nucleus()->asScriptInset();
722 // special handling of {}-bases
723 // is this always correct?
724 // It appears that this is wrong (Dekel)
725 //if (p->nuc().size() == 1 && p->nuc().back()->asNestInset() &&
726 // p->nuc().back()->extraBraces())
727 // p->nuc() = p->nuc().back()->asNestInset()->cell(0);
728 parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
735 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
736 //lyxerr << "finished reading option" << endl;
740 else if (t.cat() == catOther)
741 cell->push_back(MathAtom(new MathCharInset(t.character())));
743 else if (t.cat() == catComment) {
746 Token const & t = getToken();
747 if (t.cat() == catNewline)
751 cell->push_back(MathAtom(new MathCommentInset(s)));
759 else if (t.cs() == "lyxlock") {
761 cell->back().nucleus()->lock(true);
764 else if (t.cs() == "def" ||
765 t.cs() == "newcommand" ||
766 t.cs() == "renewcommand")
768 string const type = t.cs();
771 if (t.cs() == "def") {
773 name = getToken().cs();
777 while (good() && nextToken().cat() != catBegin) {
778 pars += getToken().cs();
782 //lyxerr << "read \\def parameter list '" << pars << "'" << endl;
784 } else { // t.cs() == "newcommand" || t.cs() == "renewcommand"
786 if (getToken().cat() != catBegin) {
787 error("'{' in \\newcommand expected (1) ");
791 name = getToken().cs();
793 if (getToken().cat() != catEnd) {
794 error("'}' in \\newcommand expected");
798 string arg = getArg('[', ']');
800 nargs = atoi(arg.c_str());
805 parse(ar1, FLAG_ITEM, MathInset::UNDECIDED_MODE);
807 // we cannot handle recursive stuff at all
809 //test.push_back(createMathInset(name));
810 //if (ar1.contains(test)) {
811 // error("we cannot handle recursive macros at all.");
815 // is a version for display attached?
818 if (nextToken().cat() == catBegin)
819 parse(ar2, FLAG_ITEM, MathInset::MATH_MODE);
821 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, type,
825 else if (t.cs() == "(") {
826 cell->push_back(MathAtom(new MathHullInset("simple")));
827 parse2(cell->back(), FLAG_SIMPLE2, MathInset::MATH_MODE, false);
830 else if (t.cs() == "[") {
831 cell->push_back(MathAtom(new MathHullInset("equation")));
832 parse2(cell->back(), FLAG_EQUATION, MathInset::MATH_MODE, false);
835 else if (t.cs() == "protect")
836 // ignore \\protect, will hopefully be re-added during output
839 else if (t.cs() == "end") {
840 if (flags & FLAG_END) {
841 // eat environment name
842 //string const name =
844 // FIXME: check that we ended the correct environment
847 error("found 'end' unexpectedly");
850 else if (t.cs() == ")") {
851 if (flags & FLAG_SIMPLE2)
853 error("found '\\)' unexpectedly");
856 else if (t.cs() == "]") {
857 if (flags & FLAG_EQUATION)
859 error("found '\\]' unexpectedly");
862 else if (t.cs() == "\\") {
863 grid.vcrskip(LyXLength(getArg('[', ']')), cellrow);
866 if (cellrow == grid.nrows())
867 grid.addRow(cellrow - 1);
868 if (grid.asHullInset())
869 grid.asHullInset()->numbered(cellrow, numbered);
870 cell = &grid.cell(grid.index(cellrow, cellcol));
874 else if (t.cs() == "multicolumn") {
875 // extract column count and insert dummy cells
877 parse(count, FLAG_ITEM, mode);
879 if (!extractNumber(count, cols)) {
880 lyxerr << " can't extract number of cells from " << count << endl;
882 // resize the table if necessary
883 for (int i = 0; i < cols; ++i) {
885 if (cellcol == grid.ncols()) {
886 //lyxerr << "adding column " << cellcol << endl;
887 grid.addCol(cellcol - 1);
889 cell = &grid.cell(grid.index(cellrow, cellcol));
890 // mark this as dummy
891 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = true;
893 // the last cell is the real thng, not a dummy
894 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
896 // read special alignment
898 parse(align, FLAG_ITEM, mode);
899 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
901 // parse the remaining contents into the "real" cell
902 parse(*cell, FLAG_ITEM, mode);
906 else if (t.cs() == "limits")
909 else if (t.cs() == "nolimits")
912 else if (t.cs() == "nonumber") {
913 if (grid.asHullInset())
914 grid.asHullInset()->numbered(cellrow, false);
917 else if (t.cs() == "number") {
918 if (grid.asHullInset())
919 grid.asHullInset()->numbered(cellrow, true);
922 else if (t.cs() == "hline") {
923 grid.rowinfo(cellrow).lines_ ++;
926 else if (t.cs() == "sqrt") {
928 parse(ar, FLAG_OPTION, mode);
930 cell->push_back(MathAtom(new MathRootInset));
931 cell->back().nucleus()->cell(0) = ar;
932 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
934 cell->push_back(MathAtom(new MathSqrtInset));
935 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
939 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
940 cell->push_back(createMathInset(t.cs()));
941 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
942 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
945 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
946 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
947 cell->push_back(MathAtom(new RefInset(t.cs())));
948 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
949 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
952 else if (t.cs() == "left") {
954 string l = getToken().asString();
956 parse(ar, FLAG_RIGHT, mode);
958 string r = getToken().asString();
959 cell->push_back(MathAtom(new MathDelimInset(l, r, ar)));
962 else if (t.cs() == "right") {
963 if (flags & FLAG_RIGHT)
965 //lyxerr << "got so far: '" << cell << "'" << endl;
966 error("Unmatched right delimiter");
970 else if (t.cs() == "begin") {
971 string const name = getArg('{', '}');
973 if (name == "array" || name == "subarray") {
974 string const valign = parse_verbatim_option() + 'c';
975 string const halign = parse_verbatim_item();
976 cell->push_back(MathAtom(new MathArrayInset(name, valign[0], halign)));
977 parse2(cell->back(), FLAG_END, mode, false);
980 else if (name == "tabular") {
981 string const valign = parse_verbatim_option() + 'c';
982 string const halign = parse_verbatim_item();
983 cell->push_back(MathAtom(new MathTabularInset(name, valign[0], halign)));
984 parse2(cell->back(), FLAG_END, MathInset::TEXT_MODE, false);
987 else if (name == "split" || name == "cases" ||
988 name == "gathered" || name == "aligned" ||
989 name == "alignedat") {
990 cell->push_back(createMathInset(name));
991 parse2(cell->back(), FLAG_END, mode, false);
994 else if (name == "math") {
995 cell->push_back(MathAtom(new MathHullInset("simple")));
996 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, true);
999 else if (name == "equation" || name == "equation*"
1000 || name == "displaymath") {
1001 cell->push_back(MathAtom(new MathHullInset("equation")));
1002 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, (name == "equation"));
1005 else if (name == "eqnarray" || name == "eqnarray*") {
1006 cell->push_back(MathAtom(new MathHullInset("eqnarray")));
1007 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1010 else if (name == "align" || name == "align*") {
1011 cell->push_back(MathAtom(new MathHullInset("align")));
1012 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1015 else if (name == "flalign" || name == "flalign*") {
1016 cell->push_back(MathAtom(new MathHullInset("flalign")));
1017 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1020 else if (name == "alignat" || name == "alignat*") {
1021 // ignore this for a while
1023 cell->push_back(MathAtom(new MathHullInset("alignat")));
1024 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1027 else if (name == "xalignat" || name == "xalignat*") {
1028 // ignore this for a while
1030 cell->push_back(MathAtom(new MathHullInset("xalignat")));
1031 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1034 else if (name == "xxalignat") {
1035 // ignore this for a while
1037 cell->push_back(MathAtom(new MathHullInset("xxalignat")));
1038 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1041 else if (name == "multline" || name == "multline*") {
1042 cell->push_back(MathAtom(new MathHullInset("multline")));
1043 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1046 else if (name == "gather" || name == "gather*") {
1047 cell->push_back(MathAtom(new MathHullInset("gather")));
1048 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1051 else if (latexkeys const * l = in_word_set(name)) {
1052 if (l->inset == "matrix") {
1053 cell->push_back(createMathInset(name));
1054 parse2(cell->back(), FLAG_END, mode, false);
1060 lyxerr << "found unknown math environment '" << name << "'" << endl;
1061 // create generic environment inset
1062 cell->push_back(MathAtom(new MathEnvInset(name)));
1063 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1067 else if (t.cs() == "kern") {
1068 #ifdef WITH_WARNINGS
1073 Token const & t = getToken();
1079 if (isValidLength(s))
1082 cell->push_back(MathAtom(new MathKernInset(s)));
1085 else if (t.cs() == "label") {
1086 string label = parse_verbatim_item();
1089 if (grid.asHullInset()) {
1090 grid.asHullInset()->label(cellrow, label);
1092 cell->push_back(createMathInset(t.cs()));
1093 cell->push_back(MathAtom(new MathBraceInset(ar)));
1097 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1098 MathAtom at = createMathInset(t.cs());
1099 at.nucleus()->cell(0) = *cell;
1101 parse(at.nucleus()->cell(1), flags, mode);
1102 cell->push_back(at);
1106 else if (t.cs() == "color") {
1107 MathAtom at = createMathInset(t.cs());
1108 parse(at.nucleus()->cell(0), FLAG_ITEM, MathInset::TEXT_MODE);
1109 parse(at.nucleus()->cell(1), flags, mode);
1110 cell->push_back(at);
1114 else if (t.cs() == "substack") {
1115 cell->push_back(createMathInset(t.cs()));
1116 parse2(cell->back(), FLAG_ITEM, mode, false);
1119 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1120 cell->push_back(createMathInset(t.cs()));
1121 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, MathInset::TEXT_MODE);
1122 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, MathInset::TEXT_MODE);
1123 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, MathInset::TEXT_MODE);
1127 else if (t.cs() == "infer") {
1129 parse(ar, FLAG_OPTION, mode);
1130 cell->push_back(createMathInset(t.cs()));
1131 parse2(cell->back(), FLAG_ITEM, mode, false);
1135 else if (1 && t.cs() == "ar") {
1136 auto_ptr<MathXYArrowInset> p(new MathXYArrowInset);
1137 // try to read target
1138 parse(p->cell(0), FLAG_OTPTION, mode);
1139 // try to read label
1140 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1141 p->up_ = nextToken().cat() == catSuper;
1143 parse(p->cell(1), FLAG_ITEM, mode);
1144 //lyxerr << "read label: " << p->cell(1) << endl;
1147 cell->push_back(MathAtom(p.release()));
1148 //lyxerr << "read cell: " << cell << endl;
1152 else if (t.cs().size()) {
1153 latexkeys const * l = in_word_set(t.cs());
1155 if (l->inset == "font") {
1156 cell->push_back(createMathInset(t.cs()));
1157 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, asMode(mode, l->extra));
1160 else if (l->inset == "oldfont") {
1161 cell->push_back(createMathInset(t.cs()));
1162 parse(cell->back().nucleus()->cell(0), flags, asMode(mode, l->extra));
1166 else if (l->inset == "style") {
1167 cell->push_back(createMathInset(t.cs()));
1168 parse(cell->back().nucleus()->cell(0), flags, mode);
1173 MathAtom at = createMathInset(t.cs());
1174 for (MathInset::idx_type i = 0; i < at->nargs(); ++i)
1175 parse(at.nucleus()->cell(i), FLAG_ITEM, asMode(mode, l->extra));
1176 cell->push_back(at);
1181 MathAtom at = createMathInset(t.cs());
1182 MathInset::mode_type m = mode;
1183 //if (m == MathInset::UNDECIDED_MODE)
1184 //lyxerr << "default creation: m1: " << m << endl;
1185 if (at->currentMode() != MathInset::UNDECIDED_MODE)
1186 m = at->currentMode();
1187 //lyxerr << "default creation: m2: " << m << endl;
1188 MathInset::idx_type start = 0;
1189 // this fails on \bigg[...\bigg]
1191 //parse(opt, FLAG_OPTION, MathInset::VERBATIM_MODE);
1194 // at.nucleus()->cell(0) = opt;
1196 for (MathInset::idx_type i = start; i < at->nargs(); ++i) {
1197 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1200 cell->push_back(at);
1205 if (flags & FLAG_LEAVE) {
1206 flags &= ~FLAG_LEAVE;
1214 } // anonymous namespace
1217 void mathed_parse_cell(MathArray & ar, string const & str)
1219 istringstream is(str.c_str());
1220 mathed_parse_cell(ar, is);
1224 void mathed_parse_cell(MathArray & ar, istream & is)
1226 Parser(is).parse(ar, 0, MathInset::MATH_MODE);
1230 bool mathed_parse_normal(MathAtom & t, string const & str)
1232 istringstream is(str.c_str());
1233 return Parser(is).parse(t);
1237 bool mathed_parse_normal(MathAtom & t, istream & is)
1239 return Parser(is).parse(t);
1243 bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
1245 return Parser(lex).parse(t);
1249 void mathed_parse_normal(MathGridInset & grid, string const & str)
1251 istringstream is(str.c_str());
1252 Parser(is).parse1(grid, 0, MathInset::MATH_MODE, false);
1258 fill(theCatcode, theCatcode + 256, catOther);
1259 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1260 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1262 theCatcode[int('\\')] = catEscape;
1263 theCatcode[int('{')] = catBegin;
1264 theCatcode[int('}')] = catEnd;
1265 theCatcode[int('$')] = catMath;
1266 theCatcode[int('&')] = catAlign;
1267 theCatcode[int('\n')] = catNewline;
1268 theCatcode[int('#')] = catParameter;
1269 theCatcode[int('^')] = catSuper;
1270 theCatcode[int('_')] = catSub;
1271 theCatcode[int(0x7f)] = catIgnore;
1272 theCatcode[int(' ')] = catSpace;
1273 theCatcode[int('\t')] = catSpace;
1274 theCatcode[int('\r')] = catNewline;
1275 theCatcode[int('~')] = catActive;
1276 theCatcode[int('%')] = catComment;