3 * Purpose: Parser for mathed
4 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 * Created: January 1996
6 * Description: Parse LaTeX2e math mode code.
8 * Dependencies: Xlib, XForms
10 * Copyright: 1996, Alejandro Aguilar Sierra
14 * You are free to use and modify this code under the terms of
15 * the GNU General Public Licence version 2 or later.
20 If someone desperately needs partial "structures" (such as a few cells of
21 an array inset or similar) (s)he could uses the following hack as starting
22 point to write some macros:
27 \def\makeamptab{\catcode`\&=4\relax}
28 \def\makeampletter{\catcode`\&=11\relax}
29 \def\b{\makeampletter\expandafter\makeamptab\bi}
49 #pragma implementation
52 #include "math_parser.h"
53 #include "math_inset.h"
54 #include "math_arrayinset.h"
55 #include "math_braceinset.h"
56 #include "math_boxinset.h"
57 #include "math_charinset.h"
58 #include "math_deliminset.h"
59 #include "math_factory.h"
60 #include "math_funcinset.h"
61 #include "math_kerninset.h"
62 #include "math_macro.h"
63 #include "math_macrotable.h"
64 #include "math_macrotemplate.h"
65 #include "math_hullinset.h"
66 #include "math_rootinset.h"
67 #include "math_sizeinset.h"
68 #include "math_sqrtinset.h"
69 #include "math_scriptinset.h"
70 #include "math_specialcharinset.h"
71 #include "math_sqrtinset.h"
72 #include "math_support.h"
73 #include "math_xyarrowinset.h"
77 #include "support/LAssert.h"
78 #include "support/lstrings.h"
97 bool stared(string const & s)
99 string::size_type const n = s.size();
100 return n && s[n - 1] == '*';
104 void add(MathArray & ar, char c, MathTextCodes code)
106 ar.push_back(MathAtom(new MathCharInset(c, code)));
110 // These are TeX's catcodes
112 catEscape, // 0 backslash
122 catSpace, // 10 space
123 catLetter, // 11 a-zA-Z
124 catOther, // 12 none of the above
127 catInvalid // 15 <delete>
130 CatCode theCatcode[256];
133 inline CatCode catcode(unsigned char c)
135 return theCatcode[c];
140 FLAG_BRACE_LAST = 1 << 1, // last closing brace ends the parsing process
141 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
142 FLAG_END = 1 << 3, // next \\end ends the parsing process
143 FLAG_BRACK_END = 1 << 4, // next closing bracket ends the parsing process
144 FLAG_BOX = 1 << 5, // we are in a box
145 FLAG_ITEM = 1 << 6, // read a (possibly braced token)
146 FLAG_BLOCK = 1 << 7, // next block ends the parsing process
147 FLAG_BLOCK2 = 1 << 8, // next block2 ends the parsing process
148 FLAG_LEAVE = 1 << 9 // leave the loop at the end
154 fill(theCatcode, theCatcode + 256, catOther);
155 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
156 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
158 theCatcode['\\'] = catEscape;
159 theCatcode['{'] = catBegin;
160 theCatcode['}'] = catEnd;
161 theCatcode['$'] = catMath;
162 theCatcode['&'] = catAlign;
163 theCatcode['\n'] = catNewline;
164 theCatcode['#'] = catParameter;
165 theCatcode['^'] = catSuper;
166 theCatcode['_'] = catSub;
167 theCatcode['
\7f'] = catIgnore;
168 theCatcode[' '] = catSpace;
169 theCatcode['\t'] = catSpace;
170 theCatcode['\r'] = catSpace;
171 theCatcode['~'] = catActive;
172 theCatcode['%'] = catComment;
178 // Helper class for parsing
184 Token() : cs_(), char_(0), cat_(catIgnore) {}
186 Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
188 Token(string const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
191 string const & cs() const { return cs_; }
193 CatCode cat() const { return cat_; }
195 char character() const { return char_; }
197 string asString() const;
210 bool Token::isCR() const
212 return cs_ == "\\" || cs_ == "cr" || cs_ == "crcr";
215 string Token::asString() const
217 return cs_.size() ? cs_ : string(1, char_);
220 // Angus' compiler says these are not needed
221 //bool operator==(Token const & s, Token const & t)
223 // return s.character() == t.character()
224 // && s.cat() == t.cat() && s.cs() == t.cs();
227 //bool operator!=(Token const & s, Token const & t)
232 ostream & operator<<(ostream & os, Token const & t)
235 os << "\\" << t.cs();
237 os << "[" << t.character() << "," << t.cat() << "]";
246 Parser(LyXLex & lex);
248 Parser(istream & is);
251 bool parse_macro(string & name);
253 bool parse_normal(MathAtom &);
255 void parse_into(MathArray & array, unsigned flags, MathTextCodes = LM_TC_MIN);
257 int lineno() const { return lineno_; }
263 void parse_into1(MathArray & array, unsigned flags, MathTextCodes);
265 string getArg(char lf, char rf);
269 void error(string const & msg);
271 bool parse_lines(MathAtom & t, bool numbered, bool outmost);
272 /// parses {... & ... \\ ... & ... }
273 bool parse_lines2(MathAtom & t, bool braced);
274 /// dump contents to screen
279 void tokenize(istream & is);
281 void tokenize(string const & s);
283 void skipSpaceTokens(istream & is, char c);
285 void push_back(Token const & t);
289 Token const & prevToken() const;
291 Token const & nextToken() const;
293 Token const & getToken();
294 /// skips spaces if any
296 /// skips opening brace
298 /// skips closing brace
300 /// counts a sequence of hlines
303 void lex(string const & s);
310 vector<Token> tokens_;
322 Parser::Parser(LyXLex & lexer)
323 : lineno_(lexer.getLineNo()), pos_(0), curr_num_(false)
325 tokenize(lexer.getStream());
330 Parser::Parser(istream & is)
331 : lineno_(0), pos_(0), curr_num_(false)
337 void Parser::push_back(Token const & t)
339 tokens_.push_back(t);
343 void Parser::pop_back()
349 Token const & Parser::prevToken() const
351 static const Token dummy;
352 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
356 Token const & Parser::nextToken() const
358 static const Token dummy;
359 return good() ? tokens_[pos_] : dummy;
363 Token const & Parser::getToken()
365 static const Token dummy;
366 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << '\n';
367 return good() ? tokens_[pos_++] : dummy;
371 void Parser::skipSpaces()
373 while (nextToken().cat() == catSpace)
378 void Parser::skipBegin()
380 if (nextToken().cat() == catBegin)
383 lyxerr << "'{' expected\n";
387 void Parser::skipEnd()
389 if (nextToken().cat() == catEnd)
392 lyxerr << "'}' expected\n";
396 int Parser::readHLines()
400 while (nextToken().cs() == "hline") {
409 void Parser::putback()
415 bool Parser::good() const
417 return pos_ < tokens_.size();
421 char Parser::getChar()
424 lyxerr << "The input stream is not well..." << endl;
425 return tokens_[pos_++].character();
429 string Parser::getArg(char left, char right)
439 while ((c = getChar()) != right && good())
446 void Parser::tokenize(istream & is)
448 // eat everything up to the next \end_inset or end of stream
449 // and store it in s for further tokenization
454 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
455 s = s.substr(0, s.size() - 10);
465 void Parser::skipSpaceTokens(istream & is, char c)
467 // skip trailing spaces
468 while (catcode(c) == catSpace || catcode(c) == catNewline)
471 //lyxerr << "putting back: " << c << "\n";
476 void Parser::tokenize(string const & buffer)
478 static bool init_done = false;
485 istringstream is(buffer.c_str(), ios::in | ios::binary);
489 //lyxerr << "reading c: " << c << "\n";
491 switch (catcode(c)) {
495 if (catcode(c) == catNewline)
496 ; //push_back(Token("par"));
498 push_back(Token(' ', catSpace));
505 while (is.get(c) && catcode(c) != catNewline)
514 error("unexpected end of input");
517 if (catcode(c) == catLetter) {
519 while (is.get(c) && catcode(c) == catLetter)
521 skipSpaceTokens(is, c);
530 push_back(Token(c, catcode(c)));
532 skipSpaceTokens(is, c);
537 lyxerr << "ignoring a char: " << int(c) << "\n";
542 push_back(Token(c, catcode(c)));
552 void Parser::dump() const
554 lyxerr << "\nTokens: ";
555 for (unsigned i = 0; i < tokens_.size(); ++i) {
558 lyxerr << tokens_[i];
564 void Parser::error(string const & msg)
566 lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
573 bool Parser::parse_lines(MathAtom & t, bool numbered, bool outmost)
575 MathGridInset * p = t->asGridInset();
578 lyxerr << "error in Parser::parse_lines() 1\n";
582 // save global variables
583 bool const saved_num = curr_num_;
584 string const saved_label = curr_label_;
586 // read initial hlines
587 p->rowinfo(0).lines_ = readHLines();
589 for (int row = 0; true; ++row) {
590 // reset global variables
591 curr_num_ = numbered;
595 for (MathInset::col_type col = 0; true; ++col) {
596 //lyxerr << "reading cell " << row << " " << col << " "
597 // << p->ncols() << "\n";
598 //lyxerr << "ncols: " << p->ncols() << "\n";
600 if (col >= p->ncols()) {
601 //lyxerr << "adding col " << col << "\n";
602 p->addCol(p->ncols());
605 MathArray & ar = p->cell(col + row * p->ncols());
606 parse_into(ar, FLAG_BLOCK);
607 // remove 'unnecessary' braces:
608 if (ar.size() == 1 && ar.back()->asBraceInset())
609 ar = ar.back()->asBraceInset()->cell(0);
610 //lyxerr << "ar: " << ar << "\n";
612 // break if cell is not followed by an ampersand
613 if (nextToken().cat() != catAlign) {
614 //lyxerr << "less cells read than normal in row/col: "
615 // << row << " " << col << "\n";
619 // skip the ampersand
624 MathHullInset * m = t->asHullInset();
626 lyxerr << "error in Parser::parse_lines() 2\n";
629 m->numbered(row, curr_num_);
630 m->label(row, curr_label_);
631 if (curr_skip_.size()) {
632 m->vcrskip(LyXLength(curr_skip_), row);
638 if (nextToken().isCR()) {
642 // try to read a length
645 // read hlines for next row
646 p->rowinfo(row + 1).lines_ = readHLines();
649 // we are finished if the next token is an 'end'
650 if (nextToken().cs() == "end") {
651 // skip the end-token
655 // leave the 'read a line'-loop
659 // otherwise, we have to start a new row
663 // restore "global" variables
664 curr_num_ = saved_num;
665 curr_label_ = saved_label;
671 bool Parser::parse_lines2(MathAtom & t, bool braced)
673 MathGridInset * p = t->asGridInset();
675 lyxerr << "error in Parser::parse_lines() 1\n";
679 for (int row = 0; true; ++row) {
681 for (MathInset::col_type col = 0; true; ++col) {
682 //lyxerr << "reading cell " << row << " " << col << " " << p->ncols() << "\n";
684 if (col >= p->ncols()) {
685 //lyxerr << "adding col " << col << "\n";
686 p->addCol(p->ncols());
689 parse_into(p->cell(col + row * p->ncols()), FLAG_BLOCK2);
690 //lyxerr << "read cell: " << p->cell(col + row * p->ncols()) << "\n";
692 // break if cell is not followed by an ampersand
693 if (nextToken().cat() != catAlign) {
694 //lyxerr << "less cells read than normal in row/col: " << row << " " << col << "\n";
698 // skip the ampersand
703 if (nextToken().isCR()) {
708 // we are finished if the next token is the one we expected
709 // skip the end-token
710 // leave the 'read a line'-loop
712 if (nextToken().cat() == catEnd) {
717 if (nextToken().cs() == "end") {
724 // otherwise, we have to start a new row
734 bool Parser::parse_macro(string & name)
740 if (nextToken().cs() == "def") {
743 name = getToken().cs();
746 while (good() && nextToken().cat() != catBegin)
747 pars += getToken().cs();
750 lyxerr << "bad stream in parse_macro\n";
755 //lyxerr << "read \\def parameter list '" << pars << "'\n";
757 lyxerr << "can't handle non-empty parameter lists\n";
762 } else if (nextToken().cs() == "newcommand") {
766 if (getToken().cat() != catBegin) {
767 lyxerr << "'{' in \\newcommand expected (1) \n";
772 name = getToken().cs();
774 if (getToken().cat() != catEnd) {
775 lyxerr << "'}' expected\n";
779 string arg = getArg('[', ']');
781 nargs = atoi(arg.c_str());
784 lyxerr << "\\newcommand or \\def expected\n";
789 if (getToken().cat() != catBegin) {
790 lyxerr << "'{' in macro definition expected (2)\n";
795 parse_into(ar1, FLAG_BRACE_LAST);
797 // we cannot handle recursive stuff at all
799 test.push_back(createMathInset(name));
800 if (ar1.contains(test)) {
801 lyxerr << "we cannot handle recursive macros at all.\n";
805 // is a version for display attached?
807 parse_into(ar2, FLAG_ITEM);
809 MathMacroTable::create(name, nargs, ar1, ar2);
814 bool Parser::parse_normal(MathAtom & matrix)
817 Token const & t = getToken();
820 matrix = MathAtom(new MathHullInset(LM_OT_SIMPLE));
821 parse_into(matrix->cell(0), 0);
825 if (t.cat() == catMath) {
826 Token const & n = getToken();
827 if (n.cat() == catMath) {
828 // TeX's $$...$$ syntax for displayed math
829 matrix = MathAtom(new MathHullInset(LM_OT_EQUATION));
830 MathHullInset * p = matrix->asHullInset();
831 parse_into(p->cell(0), 0);
832 p->numbered(0, curr_num_);
833 p->label(0, curr_label_);
835 // simple $...$ stuff
837 matrix = MathAtom(new MathHullInset(LM_OT_SIMPLE));
838 parse_into(matrix->cell(0), 0);
843 if (!t.cs().size()) {
844 lyxerr << "start of math expected, got '" << t << "'\n";
848 string const & cs = t.cs();
853 matrix = MathAtom(new MathHullInset(LM_OT_EQUATION));
854 MathHullInset * p = matrix->asHullInset();
855 parse_into(p->cell(0), 0);
856 p->numbered(0, curr_num_);
857 p->label(0, curr_label_);
862 lyxerr << "'begin' of un-simple math expected, got '" << cs << "'\n";
866 string const name = getArg('{', '}');
868 if (name == "math") {
869 matrix = MathAtom(new MathHullInset(LM_OT_SIMPLE));
870 parse_into(matrix->cell(0), 0);
874 if (name == "equation" || name == "equation*" || name == "displaymath") {
875 curr_num_ = (name == "equation");
877 matrix = MathAtom(new MathHullInset(LM_OT_EQUATION));
878 MathHullInset * p = matrix->asHullInset();
879 parse_into(p->cell(0), FLAG_END);
880 p->numbered(0, curr_num_);
881 p->label(0, curr_label_);
885 if (name == "eqnarray" || name == "eqnarray*") {
886 matrix = MathAtom(new MathHullInset(LM_OT_EQNARRAY));
887 return parse_lines(matrix, !stared(name), true);
890 if (name == "align" || name == "align*") {
891 matrix = MathAtom(new MathHullInset(LM_OT_ALIGN));
892 return parse_lines(matrix, !stared(name), true);
895 if (name == "alignat" || name == "alignat*") {
896 int nc = 2 * atoi(getArg('{', '}').c_str());
897 matrix = MathAtom(new MathHullInset(LM_OT_ALIGNAT, nc));
898 return parse_lines(matrix, !stared(name), true);
901 if (name == "xalignat" || name == "xalignat*") {
902 int nc = 2 * atoi(getArg('{', '}').c_str());
903 matrix = MathAtom(new MathHullInset(LM_OT_XALIGNAT, nc));
904 return parse_lines(matrix, !stared(name), true);
907 if (name == "xxalignat") {
908 int nc = 2 * atoi(getArg('{', '}').c_str());
909 matrix = MathAtom(new MathHullInset(LM_OT_XXALIGNAT, nc));
910 return parse_lines(matrix, !stared(name), true);
913 if (name == "multline" || name == "multline*") {
914 matrix = MathAtom(new MathHullInset(LM_OT_MULTLINE));
915 return parse_lines(matrix, !stared(name), true);
918 if (name == "gather" || name == "gather*") {
919 matrix = MathAtom(new MathHullInset(LM_OT_GATHER));
920 return parse_lines(matrix, !stared(name), true);
923 lyxerr[Debug::MATHED] << "1: unknown math environment: " << name << "\n";
924 lyxerr << "1: unknown math environment: " << name << "\n";
929 void Parser::parse_into(MathArray & array, unsigned flags, MathTextCodes code)
931 parse_into1(array, flags, code);
932 // remove 'unnecessary' braces:
933 if (array.size() == 1 && array.back()->asBraceInset()) {
934 lyxerr << "extra braces removed\n";
935 array = array.back()->asBraceInset()->cell(0);
940 void Parser::parse_into1(MathArray & array, unsigned flags, MathTextCodes code)
946 Token const & t = getToken();
949 lyxerr << "t: " << t << " flags: " << flags << "\n";
954 if (flags & FLAG_ITEM) {
955 if (t.cat() == catSpace)
959 if (t.cat() == catBegin) {
960 // skip the brace and collect everything to the next matching
962 flags |= FLAG_BRACE_LAST;
966 // handle only this single token, leave the loop if done
970 if (flags & FLAG_BLOCK) {
971 if (t.cat() == catAlign || t.isCR() || t.cs() == "end") {
977 if (flags & FLAG_BLOCK2) {
978 if (t.cat() == catAlign || t.isCR() || t.cs() == "end"
979 || t.cat() == catEnd) {
988 if (t.cat() == catMath) {
989 if (flags & FLAG_BOX) {
990 // we are inside an mbox, so opening new math is allowed
991 array.push_back(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
992 parse_into(array.back()->cell(0), 0);
994 // otherwise this is the end of the formula
999 else if (t.cat() == catLetter)
1000 add(array, t.character(), code);
1002 else if (t.cat() == catSpace && code == LM_TC_TEXTRM)
1003 add(array, t.character(), code);
1005 else if (t.cat() == catParameter) {
1006 Token const & n = getToken();
1007 array.push_back(MathAtom(new MathMacroArgument(n.character()-'0', code)));
1010 else if (t.cat() == catBegin) {
1012 parse_into(ar, FLAG_BRACE_LAST);
1013 #ifndef WITH_WARNINGS
1014 #warning this might be wrong in general!
1016 // ignore braces around simple items
1017 if ((ar.size() == 1 && !ar.front()->needsBraces()
1018 || (ar.size() == 2 && !ar.front()->needsBraces()
1019 && ar.back()->asScriptInset()))
1020 || (ar.size() == 0 && array.size() == 0))
1022 array.push_back(ar);
1024 array.push_back(MathAtom(new MathBraceInset));
1025 array.back()->cell(0).swap(ar);
1029 else if (t.cat() == catEnd) {
1030 if (flags & FLAG_BRACE_LAST)
1033 lyxerr << "found '}' unexpectedly, array: '" << array << "'\n";
1034 //lyxerr << "found '}' unexpectedly\n";
1036 //add(array, '}', LM_TC_TEX);
1039 else if (t.cat() == catAlign) {
1040 lyxerr << "found tab unexpectedly, array: '" << array << "'\n";
1041 //lyxerr << "found tab unexpectedly\n";
1042 add(array, '&', LM_TC_TEX);
1045 else if (t.cat() == catSuper || t.cat() == catSub) {
1046 bool up = (t.cat() == catSuper);
1047 MathScriptInset * p = 0;
1049 p = array.back()->asScriptInset();
1050 if (!p || p->has(up)) {
1051 array.push_back(MathAtom(new MathScriptInset(up)));
1052 p = array.back()->asScriptInset();
1055 parse_into(p->cell(up), FLAG_ITEM);
1060 else if (t.character() == ']' && (flags & FLAG_BRACK_END))
1063 else if (t.cat() == catOther)
1064 add(array, t.character(), code);
1067 // control sequences
1069 else if (t.cs() == "protect")
1070 // ignore \\protect, will be re-added during output
1073 else if (t.cs() == "end")
1076 else if (t.cs() == ")")
1079 else if (t.cs() == "]")
1082 else if (t.cs() == "\\") {
1083 curr_skip_ = getArg('[', ']');
1084 //lyxerr << "found newline unexpectedly, array: '" << array << "'\n";
1085 lyxerr << "found newline unexpectedly\n";
1086 array.push_back(createMathInset("\\"));
1089 else if (t.cs() == "limits")
1092 else if (t.cs() == "nolimits")
1095 else if (t.cs() == "nonumber")
1098 else if (t.cs() == "number")
1101 else if (t.cs() == "sqrt") {
1104 array.push_back(MathAtom(new MathRootInset));
1105 parse_into(array.back()->cell(0), FLAG_BRACK_END);
1106 parse_into(array.back()->cell(1), FLAG_ITEM);
1109 array.push_back(MathAtom(new MathSqrtInset));
1110 parse_into(array.back()->cell(0), FLAG_ITEM);
1114 else if (t.cs() == "left") {
1115 string l = getToken().asString();
1117 parse_into(ar, FLAG_RIGHT);
1118 string r = getToken().asString();
1119 MathAtom dl(new MathDelimInset(l, r));
1121 array.push_back(dl);
1124 else if (t.cs() == "right") {
1125 if (!(flags & FLAG_RIGHT)) {
1126 //lyxerr << "got so far: '" << array << "'\n";
1127 error("Unmatched right delimiter");
1132 else if (t.cs() == "begin") {
1133 string const name = getArg('{', '}');
1134 if (name == "array" || name == "subarray") {
1135 string const valign = getArg('[', ']') + 'c';
1136 string const halign = getArg('{', '}');
1137 array.push_back(MathAtom(new MathArrayInset(name, valign[0], halign)));
1138 parse_lines(array.back(), false, false);
1139 } else if (name == "split" || name == "cases" ||
1140 name == "gathered" || name == "aligned") {
1141 array.push_back(createMathInset(name));
1142 parse_lines(array.back(), false, false);
1143 } else if (name == "matrix" || name == "pmatrix" || name == "bmatrix" ||
1144 name == "vmatrix" || name == "Vmatrix") {
1145 array.push_back(createMathInset(name));
1146 parse_lines2(array.back(), false);
1148 lyxerr << "unknow math inset begin '" << name << "'\n";
1151 else if (t.cs() == "kern") {
1152 #ifdef WITH_WARNINGS
1157 Token const & t = getToken();
1163 if (isValidLength(s))
1166 array.push_back(MathAtom(new MathKernInset(s)));
1170 else if (t.cs() == "lyxkern") {
1171 MathAtom p = createMathInset(t.cs());
1172 parse_into(p->cell(0), flags, code);
1177 else if (t.cs() == "label") {
1178 curr_label_ = getArg('{', '}');
1181 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1182 MathAtom p = createMathInset(t.cs());
1183 array.swap(p->cell(0));
1184 parse_into(p->cell(1), flags, code);
1189 else if (t.cs() == "substack") {
1190 array.push_back(createMathInset(t.cs()));
1192 parse_lines2(array.back(), true);
1195 else if (t.cs() == "xymatrix") {
1196 array.push_back(createMathInset(t.cs()));
1198 parse_lines2(array.back(), true);
1203 else if (1 && t.cs() == "ar") {
1204 MathXYArrowInset * p = new MathXYArrowInset;
1206 // try to read target
1209 parse_into(p->cell(0), FLAG_BRACK_END);
1210 //lyxerr << "read target: " << p->cell(0) << "\n";
1215 // try to read label
1216 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1217 p->up_ = nextToken().cat() == catSuper;
1219 parse_into(p->cell(1), FLAG_ITEM);
1220 //lyxerr << "read label: " << p->cell(1) << "\n";
1223 array.push_back(MathAtom(p));
1224 //lyxerr << "read array: " << array << "\n";
1229 else if (t.cs() == "mbox" || t.cs() == "text") {
1230 //array.push_back(createMathInset(t.cs()));
1231 array.push_back(MathAtom(new MathBoxInset(t.cs())));
1232 // slurp in the argument of mbox
1234 MathBoxInset * p = array.back()->asBoxInset();
1240 else if (t.cs().size()) {
1241 latexkeys const * l = in_word_set(t.cs());
1243 if (l->token == LM_TK_FONT) {
1244 //lyxerr << "starting font\n";
1245 //CatCode catSpaceSave = theCatcode[' '];
1246 //if (l->id == LM_TC_TEXTRM) {
1247 // // temporarily change catcode
1248 // theCatcode[' '] = catLetter;
1252 parse_into(ar, FLAG_ITEM, static_cast<MathTextCodes>(l->id));
1253 array.push_back(ar);
1255 // undo catcode changes
1256 ////theCatcode[' '] = catSpaceSave;
1257 //lyxerr << "ending font\n";
1260 else if (l->token == LM_TK_OLDFONT) {
1261 code = static_cast<MathTextCodes>(l->id);
1264 else if (l->token == LM_TK_BOX) {
1265 MathAtom p = createMathInset(t.cs());
1266 parse_into(p->cell(0), FLAG_ITEM | FLAG_BOX, LM_TC_BOX);
1270 else if (l->token == LM_TK_STY) {
1271 MathAtom p = createMathInset(t.cs());
1272 parse_into(p->cell(0), flags, code);
1278 MathAtom p = createMathInset(t.cs());
1279 for (MathInset::idx_type i = 0; i < p->nargs(); ++i)
1280 parse_into(p->cell(i), FLAG_ITEM);
1286 MathAtom p = createMathInset(t.cs());
1287 for (MathInset::idx_type i = 0; i < p->nargs(); ++i)
1288 parse_into(p->cell(i), FLAG_ITEM);
1294 if (flags & FLAG_LEAVE) {
1295 flags &= ~FLAG_LEAVE;
1301 lyxerr << " Math Panic, expect problems!\n";
1302 // Search for the end command.
1306 } while (good() && t.cs() != "end");
1312 } // anonymous namespace
1315 void mathed_parse_cell(MathArray & ar, string const & str)
1317 istringstream is(str.c_str());
1318 mathed_parse_cell(ar, is);
1322 void mathed_parse_cell(MathArray & ar, istream & is)
1324 Parser(is).parse_into(ar, 0);
1329 bool mathed_parse_macro(string & name, string const & str)
1331 istringstream is(str.c_str());
1333 return parser.parse_macro(name);
1336 bool mathed_parse_macro(string & name, istream & is)
1339 return parser.parse_macro(name);
1342 bool mathed_parse_macro(string & name, LyXLex & lex)
1345 return parser.parse_macro(name);
1350 bool mathed_parse_normal(MathAtom & t, string const & str)
1352 istringstream is(str.c_str());
1354 return parser.parse_normal(t);
1357 bool mathed_parse_normal(MathAtom & t, istream & is)
1360 return parser.parse_normal(t);
1363 bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
1366 return parser.parse_normal(t);