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
27 bool is_math_env(string const & name)
29 static char const * known_math_envs[] = { "equation", "equation*",
30 "eqnarray", "eqnarray*", "align", "align*", "gather", "gather*",
31 "multline", "multline*", 0};
33 for (char const ** what = known_math_envs; *what; ++what)
40 void parse_math(Parser & p, ostream & os, unsigned flags, const mode_type mode)
43 Token const & t = p.get_token();
46 cerr << "t: " << t << " flags: " << flags << "\n";
49 if (flags & FLAG_ITEM) {
50 if (t.cat() == catSpace)
54 if (t.cat() == catBegin) {
55 // skip the brace and collect everything to the next matching
57 flags |= FLAG_BRACE_LAST;
61 // handle only this single token, leave the loop if done
69 if (t.cat() == catMath) {
70 if (mode == MATHTEXT_MODE) {
71 // we are inside some text mode thingy, so opening new math is allowed
72 Token const & n = p.get_token();
73 if (n.cat() == catMath) {
74 // TeX's $$...$$ syntax for displayed math
76 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
78 p.get_token(); // skip the second '$' token
83 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
88 else if (flags & FLAG_SIMPLE) {
89 // this is the end of the formula
94 cerr << "\nmode: " << mode << endl;
95 p.error("something strange in the parser\n");
100 else if (t.cat() == catLetter ||
101 t.cat() == catSpace ||
102 t.cat() == catSuper ||
104 t.cat() == catOther ||
105 t.cat() == catAlign ||
106 t.cat() == catActive ||
107 t.cat() == catParameter)
110 else if (t.cat() == catNewline) {
111 //if (p.next_token().cat() == catNewline) {
115 os << "\n "; // note the space
119 else if (t.cat() == catBegin) {
121 parse_math(p, os, FLAG_BRACE_LAST, mode);
125 else if (t.cat() == catEnd) {
126 if (flags & FLAG_BRACE_LAST)
128 os << "unexpected '}' in math\n";
131 else if (t.cat() == catComment)
138 else if (t.cs() == "(") {
140 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
144 else if (t.cs() == "[") {
145 // special handling of a few common SW user quirks
147 //if (p.next_token().cs() ==
149 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
153 else if (t.cs() == "protect")
154 // ignore \\protect, will hopefully be re-added during output
157 else if (t.cs() == "begin") {
158 string const name = p.getArg('{', '}');
159 active_environments.push_back(name);
160 os << "\\begin{" << name << "}";
161 if (name == "tabular")
162 parse_math(p, os, FLAG_END, MATHTEXT_MODE);
164 parse_math(p, os, FLAG_END, mode);
165 os << "\\end{" << name << "}";
168 else if (t.cs() == "end") {
169 if (flags & FLAG_END) {
170 // eat environment name
171 string const name = p.getArg('{', '}');
172 if (name != active_environment())
173 p.error("\\end{" + name + "} does not match \\begin{"
174 + active_environment() + "}");
175 active_environments.pop_back();
178 p.error("found 'end' unexpectedly");
181 else if (t.cs() == ")") {
182 if (flags & FLAG_SIMPLE2)
184 p.error("found '\\)' unexpectedly");
187 else if (t.cs() == "]") {
188 if (flags & FLAG_EQUATION)
190 p.error("found '\\]' unexpectedly");
193 else if (t.cs() == "textrm" || t.cs() == "textsf" || t.cs() == "textbf"
194 || t.cs() == "texttt" || t.cs() == "textsc") {
195 os << '\\' << t.cs() << '{';
196 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
200 else if (t.cs() == "mbox") {
202 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
206 else if (t.cs() == "\"") {
207 string const name = p.verbatim_item();
208 if (name == "a") os << 'ä';
209 else if (name == "o") os << 'ö';
210 else if (name == "u") os << 'ü';
211 else if (name == "A") os << 'Ä';
212 else if (name == "O") os << 'Ö';
213 else if (name == "U") os << 'Ü';
214 else os << "\"{" << name << "}";
217 else if (t.cs() == "ss")
223 if (flags & FLAG_LEAVE) {
224 flags &= ~FLAG_LEAVE;