Nested tables make tex2lyx crash, because they are not recognized in the
first parsing stage where the table structure is extracted. The attached
patch fixes this by introducing a new method Parser::verbatimEnvironment()
which parses a complete environment and returns the TeX code verbatim.
OK to apply?


Georg
Index: src/tex2lyx/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/tex2lyx/ChangeLog,v
retrieving revision 1.86
diff -u -p -r1.86 ChangeLog
--- src/tex2lyx/ChangeLog	10 Mar 2005 17:58:39 -0000	1.86
+++ src/tex2lyx/ChangeLog	11 Mar 2005 15:19:42 -0000
@@ -1,3 +1,8 @@
+2005-03-11  Georg Baum  <[EMAIL PROTECTED]>
+
+	* table.C (parse_table): handle nested tables
+	* texparser.[Ch] (verbatimEnvironment): new
+
 2005-03-07  Georg Baum  <[EMAIL PROTECTED]>
 
 	* table.C (verbose_valign): new
Index: src/tex2lyx/table.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/tex2lyx/table.C,v
retrieving revision 1.30
diff -u -p -r1.30 table.C
--- src/tex2lyx/table.C	10 Mar 2005 17:58:39 -0000	1.30
+++ src/tex2lyx/table.C	11 Mar 2005 15:19:43 -0000
@@ -653,6 +713,7 @@ void parse_table(Parser & p, ostream & o
 			if (n.cat() == catMath) {
 				// TeX's $$...$$ syntax for displayed math
 				os << "\\[";
+				// This does only work because parse_math outputs TeX
 				parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
 				os << "\\]";
 				p.get_token(); // skip the second '$' token
@@ -660,6 +721,7 @@ void parse_table(Parser & p, ostream & o
 				// simple $...$  stuff
 				p.putback();
 				os << '$';
+				// This does only work because parse_math outputs TeX
 				parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
 				os << '$';
 			}
@@ -699,26 +761,26 @@ void parse_table(Parser & p, ostream & o
 
 		else if (t.cs() == "(") {
 			os << "\\(";
+			// This does only work because parse_math outputs TeX
 			parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
 			os << "\\)";
 		}
 
 		else if (t.cs() == "[") {
 			os << "\\[";
+			// This does only work because parse_math outputs TeX
 			parse_math(p, os, FLAG_EQUATION, MATH_MODE);
 			os << "\\]";
 		}
 
 		else if (t.cs() == "begin") {
 			string const name = p.getArg('{', '}');
 			active_environments.push_back(name);
 			os << "\\begin{" << name << '}';
-			if (is_math_env(name)) {
-				parse_math(p, os, FLAG_END, MATH_MODE);
-			} else {
-				parse_table(p, os, is_long_tabular, pos,
-				            FLAG_END);
-			}
+			// treat the nested environment as a block, don't
+			// parse &, \\ etc, because they don't belong to our
+			// table if they appear.
+			os << p.verbatimEnvironment(name);
 			os << "\\end{" << name << '}';
 			active_environments.pop_back();
 		}
Index: src/tex2lyx/texparser.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/tex2lyx/texparser.C,v
retrieving revision 1.27
diff -u -p -r1.27 texparser.C
--- src/tex2lyx/texparser.C	6 Jan 2005 13:22:20 -0000	1.27
+++ src/tex2lyx/texparser.C	11 Mar 2005 15:19:43 -0000
@@ -20,6 +20,7 @@ using std::endl;
 using std::fill;
 using std::istream;
 using std::istringstream;
+using std::ostringstream;
 using std::ostream;
 using std::string;
 
@@ -323,6 +324,36 @@ string Parser::getOpt()
 }
 
 
+string const Parser::verbatimEnvironment(string const & name)
+{
+	if (!good())
+		return string();
+
+	ostringstream os;
+	for (Token t = get_token(); good(); t = get_token()) {
+		if (t.cat() == catBegin) {
+			putback();
+			os << '{' << verbatim_item() << '}';
+		} else if (t.asInput() == "\\begin") {
+			string const env = getArg('{', '}');
+			os << "\\begin{" << env << '}'
+			   << verbatimEnvironment(env)
+			   << "\\end{" << env << '}';
+		} else if (t.asInput() == "\\end") {
+			string const end = getArg('{', '}');
+			if (end != name)
+				cerr << "\\end{" << end
+				     << "} does not match \\begin{" << name
+				     << "}." << endl;
+			return os.str();
+		} else
+			os << t.asInput();
+	}
+	cerr << "unexpected end of input" << endl;
+	return os.str();
+}
+
+
 void Parser::tokenize(istream & is)
 {
 	static bool init_done = false;
Index: src/tex2lyx/texparser.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/tex2lyx/texparser.h,v
retrieving revision 1.18
diff -u -p -r1.18 texparser.h
--- src/tex2lyx/texparser.h	6 Jan 2005 13:22:20 -0000	1.18
+++ src/tex2lyx/texparser.h	11 Mar 2005 15:19:43 -0000
@@ -150,6 +154,12 @@ public:
 	std::string getFullOpt();
 	/// \returns getArg('[', ']') including the brackets
 	std::string getOpt();
+	/*!
+	 * \returns the contents of the environment \p name.
+	 * <tt>\begin{name}</tt> must be parsed already, <tt>\end{name}</tt>
+	 * is parsed but not returned.
+	 */
+	std::string const verbatimEnvironment(std::string const & name);
 	/// Returns the character of the current token and increments the token position.
 	char getChar();
 	///

Reply via email to