Author: baum
Date: Wed Jan  5 21:32:45 2011
New Revision: 37117
URL: http://www.lyx.org/trac/changeset/37117

Log:
Fix several false errors "stray '}' in text" that were caused by
misinterpreting "\}" as "}" when it occured inside a pair of unescaped
braces, like in "\co...@\{*\}r||p\{1i...@\{*\}}".
The reason for this bug is that Token::character() behaves differently in
tex2lyx than in mathed. See the comment in Parser.h for a more general fix.
For now I played on the safe side and only changed those places where I
definitely know that the old code was wrong.

Modified:
   lyx-devel/trunk/src/tex2lyx/Parser.cpp
   lyx-devel/trunk/src/tex2lyx/Parser.h
   lyx-devel/trunk/src/tex2lyx/table.cpp
   lyx-devel/trunk/src/tex2lyx/test/test-insets.tex
   lyx-devel/trunk/src/tex2lyx/text.cpp

Modified: lyx-devel/trunk/src/tex2lyx/Parser.cpp
==============================================================================
--- lyx-devel/trunk/src/tex2lyx/Parser.cpp      Wed Jan  5 20:32:53 2011        
(r37116)
+++ lyx-devel/trunk/src/tex2lyx/Parser.cpp      Wed Jan  5 21:32:45 2011        
(r37117)
@@ -128,6 +128,30 @@
 }
 
 
+#ifdef FILEDEBUG
+void debugToken(std::ostream & os, Token const & t, unsigned int flags)
+{
+       char sep = ' ';
+       os << "t: " << t << " flags: " << flags;
+       if (flags & FLAG_BRACE_LAST) { os << sep << "BRACE_LAST"; sep = '|'; }
+       if (flags & FLAG_RIGHT     ) { os << sep << "RIGHT"     ; sep = '|'; }
+       if (flags & FLAG_END       ) { os << sep << "END"       ; sep = '|'; }
+       if (flags & FLAG_BRACK_LAST) { os << sep << "BRACK_LAST"; sep = '|'; }
+       if (flags & FLAG_TEXTMODE  ) { os << sep << "TEXTMODE"  ; sep = '|'; }
+       if (flags & FLAG_ITEM      ) { os << sep << "ITEM"      ; sep = '|'; }
+       if (flags & FLAG_LEAVE     ) { os << sep << "LEAVE"     ; sep = '|'; }
+       if (flags & FLAG_SIMPLE    ) { os << sep << "SIMPLE"    ; sep = '|'; }
+       if (flags & FLAG_EQUATION  ) { os << sep << "EQUATION"  ; sep = '|'; }
+       if (flags & FLAG_SIMPLE2   ) { os << sep << "SIMPLE2"   ; sep = '|'; }
+       if (flags & FLAG_OPTION    ) { os << sep << "OPTION"    ; sep = '|'; }
+       if (flags & FLAG_BRACED    ) { os << sep << "BRACED"    ; sep = '|'; }
+       if (flags & FLAG_CELL      ) { os << sep << "CELL"      ; sep = '|'; }
+       if (flags & FLAG_TABBING   ) { os << sep << "TABBING"   ; sep = '|'; }
+       os << "\n";
+}
+#endif
+
+
 //
 // Parser
 //

Modified: lyx-devel/trunk/src/tex2lyx/Parser.h
==============================================================================
--- lyx-devel/trunk/src/tex2lyx/Parser.h        Wed Jan  5 20:32:53 2011        
(r37116)
+++ lyx-devel/trunk/src/tex2lyx/Parser.h        Wed Jan  5 21:32:45 2011        
(r37117)
@@ -81,7 +81,17 @@
        std::string const & cs() const { return cs_; }
        /// Returns the catcode of the token
        CatCode cat() const { return cat_; }
-       ///
+       /** Get the character of tokens that were constructed from a single
+        * character input or a two character input and cat_ == catEscape.
+        * FIXME: The intended usage is not clear. The Token class in
+        *        ../mathed/MathParser.cpp (which is the anchestor of this
+        *        class) uses a separate char member for this method. I
+        *        believe that the intended usage is to not cover tokens with
+        *        catEscape, e.g. \code
+        *        return (cs_.empty() || cat_ == catEscape) ? 0 : cs_[0];
+        *        \endcode
+        *        All usages of this method should be checked. gb 2011-01-05
+        */
        char character() const { return cs_.empty() ? 0 : cs_[0]; }
        /// Returns the token verbatim
        std::string asInput() const;

Modified: lyx-devel/trunk/src/tex2lyx/table.cpp
==============================================================================
--- lyx-devel/trunk/src/tex2lyx/table.cpp       Wed Jan  5 20:32:53 2011        
(r37116)
+++ lyx-devel/trunk/src/tex2lyx/table.cpp       Wed Jan  5 21:32:45 2011        
(r37117)
@@ -520,7 +520,7 @@
                Token const & t = p.get_token();
 
 #ifdef FILEDEBUG
-               cerr << "t: " << t << " flags: " << flags << "\n";
+               debugToken(cerr, t, flags);
 #endif
 
                // comments and whitespace in hlines

Modified: lyx-devel/trunk/src/tex2lyx/test/test-insets.tex
==============================================================================
--- lyx-devel/trunk/src/tex2lyx/test/test-insets.tex    Wed Jan  5 20:32:53 
2011        (r37116)
+++ lyx-devel/trunk/src/tex2lyx/test/test-insets.tex    Wed Jan  5 21:32:45 
2011        (r37117)
@@ -16,9 +16,11 @@
 % the following is useful when we have the old nomencl.sty package
 \providecommand{\printnomenclature}{\printglossary}
 \providecommand{\makenomenclature}{\makeglossary}
+\makenomenclature
 \usepackage{varioref}
 \usepackage{prettyref}
 \usepackage{makeidx}
+\makeindex
 
 \usepackage{graphicx}
 \usepackage{longtable}
@@ -167,7 +169,7 @@
 \endlastfoot
 \env{longtable}  columns  are specified& in the \\
 same way as  in the \env{tabular}& environment.\\
-%\co...@\{*\}r||p\{1i...@\{*\}}& in this case.\\
+\co...@\{*\}r||p\{1i...@\{*\}}& in this case.\\
 Each row ends with a& \code{\textbackslash\textbackslash} command.\\
 The \code{\textbackslash\textbackslash} command  has an& optional\\
 argument, just as in& the\\

Modified: lyx-devel/trunk/src/tex2lyx/text.cpp
==============================================================================
--- lyx-devel/trunk/src/tex2lyx/text.cpp        Wed Jan  5 20:32:53 2011        
(r37116)
+++ lyx-devel/trunk/src/tex2lyx/text.cpp        Wed Jan  5 21:32:45 2011        
(r37117)
@@ -495,7 +495,8 @@
        unsigned int optargs = 0;
        while (optargs < context.layout->optargs) {
                eat_whitespace(p, os, context, false);
-               if (p.next_token().character() != '[') 
+               if (p.next_token().cat() == catEscape ||
+                   p.next_token().character() != '[') 
                        break;
                p.get_token(); // eat '['
                begin_inset(os, "OptArg\n");
@@ -505,14 +506,10 @@
                eat_whitespace(p, os, context, false);
                ++optargs;
        }
-#if 0
-       // This is the code needed to parse required arguments, but 
-       // required arguments come into being only much later than the
-       // file format tex2lyx is presently outputting.
        unsigned int reqargs = 0;
-       while (reqargs < context.layout->reqargs) {
+       while (LYX_FORMAT >= 392 && reqargs < context.layout->reqargs) {
                eat_whitespace(p, os, context, false);
-               if (p.next_token().character() != '{') 
+               if (p.next_token().cat() != catBegin)
                        break;
                p.get_token(); // eat '{'
                begin_inset(os, "OptArg\n");
@@ -522,7 +519,6 @@
                eat_whitespace(p, os, context, false);
                ++reqargs;
        }
-#endif
        parse_text(p, os, FLAG_ITEM, outer, context);
        context.check_end_layout(os);
        if (parent_context.deeper_paragraph) {
@@ -1337,7 +1333,7 @@
                Token const & t = p.get_token();
 
 #ifdef FILEDEBUG
-               cerr << "t: " << t << " flags: " << flags << "\n";
+               debugToken(cerr, t, flags);
 #endif
 
                if (flags & FLAG_ITEM) {
@@ -1356,9 +1352,10 @@
                        flags |= FLAG_LEAVE;
                }
 
-               if (t.character() == ']' && (flags & FLAG_BRACK_LAST))
+               if (t.cat() != catEscape && t.character() == ']' &&
+                   (flags & FLAG_BRACK_LAST))
                        return;
-               if (t.character() == '}' && (flags & FLAG_BRACE_LAST))
+               if (t.cat() == catEnd && (flags & FLAG_BRACE_LAST))
                        return;
 
                // If there is anything between \end{env} and \begin{env} we
@@ -1656,7 +1653,8 @@
                        p.skip_spaces();
                        string s;
                        bool optarg = false;
-                       if (p.next_token().character() == '[') {
+                       if (p.next_token().cat() != catEscape &&
+                           p.next_token().character() == '[') {
                                p.get_token(); // eat '['
                                s = parse_text_snippet(p, FLAG_BRACK_LAST,
                                                       outer, context);
@@ -1782,7 +1780,8 @@
                        os << "\\begin_layout " 
                           << to_utf8(context.textclass.defaultLayout().name()) 
                           << '\n';
-                       if (p.next_token().character() == '[') {
+                       if (p.next_token().cat() != catEscape &&
+                           p.next_token().character() == '[') {
                                p.get_token(); // eat '['
                                begin_inset(os, "OptArg\n");
                                os << "status collapsed\n";

Reply via email to