]> git.lyx.org Git - lyx.git/blobdiff - src/tex2lyx/texparser.C
Fix bug 2667
[lyx.git] / src / tex2lyx / texparser.C
index ea69d460d49c311960521319a3610ea385fbb50a..bdfea89a5357358bb9bc5f4ff6e4495f985adbec 100644 (file)
@@ -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;
 
@@ -39,14 +40,14 @@ void catInit()
        theCatcode[int('}')]  = catEnd;
        theCatcode[int('$')]  = catMath;
        theCatcode[int('&')]  = catAlign;
-       theCatcode[10]   = catNewline;
+       theCatcode[int('\n')] = catNewline;
        theCatcode[int('#')]  = catParameter;
        theCatcode[int('^')]  = catSuper;
        theCatcode[int('_')]  = catSub;
-       theCatcode[0x7f] = catIgnore;
+       theCatcode[0x7f]      = catIgnore;
        theCatcode[int(' ')]  = catSpace;
        theCatcode[int('\t')] = catSpace;
-       theCatcode[13]   = catIgnore;
+       theCatcode[int('\r')] = catNewline;
        theCatcode[int('~')]  = catActive;
        theCatcode[int('%')]  = catComment;
 
@@ -54,6 +55,30 @@ void catInit()
        theCatcode[int('@')]  = catLetter;
 }
 
+
+/*!
+ * Translate a line ending to '\n'.
+ * \p c must have catcode catNewline, and it must be the last character read
+ * from \p is.
+ */
+char getNewline(istream & is, char c)
+{
+       // we have to handle 3 different line endings:
+       // - UNIX (\n)
+       // - MAC  (\r)
+       // - DOS  (\r\n)
+       if (c == '\r') {
+               // MAC or DOS
+               if (is.get(c) && c != '\n') {
+                       // MAC
+                       is.putback(c);
+               }
+               return '\n';
+       }
+       // UNIX
+       return c;
+}
+
 }
 
 
@@ -247,21 +272,22 @@ char Parser::getChar()
 }
 
 
-string Parser::getArg(char left, char right)
+Parser::Arg Parser::getFullArg(char left, char right)
 {
        skip_spaces(true);
 
        // This is needed if a partial file ends with a command without arguments,
        // e. g. \medskip
        if (! good())
-               return string();
+               return std::make_pair(false, string());
 
        string result;
        char c = getChar();
 
-       if (c != left)
+       if (c != left) {
                putback();
-       else
+               return std::make_pair(false, string());
+       } else
                while ((c = getChar()) != right && good()) {
                        // Ignore comments
                        if (curr_token().cat() == catComment) {
@@ -272,14 +298,59 @@ string Parser::getArg(char left, char right)
                                result += curr_token().asInput();
                }
 
-       return result;
+       return std::make_pair(true, result);
+}
+
+
+string Parser::getArg(char left, char right)
+{
+       return getFullArg(left, right).second;
+}
+
+
+string Parser::getFullOpt()
+{
+       Arg arg = getFullArg('[', ']');
+       if (arg.first)
+               return '[' + arg.second + ']';
+       return arg.second;
 }
 
 
 string Parser::getOpt()
 {
        string const res = getArg('[', ']');
-       return res.size() ? '[' + res + ']' : string();
+       return res.empty() ? string() : '[' + res + ']';
+}
+
+
+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();
 }
 
 
@@ -309,10 +380,10 @@ void Parser::tokenize(istream & is)
 
                        case catNewline: {
                                ++lineno_;
-                               string s(1, c);
+                               string s(1, getNewline(is, c));
                                while (is.get(c) && catcode(c) == catNewline) {
                                        ++lineno_;
-                                       s += c;
+                                       s += getNewline(is, c);
                                }
                                if (catcode(c) != catNewline)
                                        is.putback(c);
@@ -326,6 +397,9 @@ void Parser::tokenize(istream & is)
                                string s;
                                while (is.get(c) && catcode(c) != catNewline)
                                        s += c;
+                               // handle possible DOS line ending
+                               if (catcode(c) == catNewline)
+                                       c = getNewline(is, c);
                                // Note: The '%' at the beginning and the '\n' at the end
                                // of the comment are not stored.
                                ++lineno_;
@@ -352,8 +426,7 @@ void Parser::tokenize(istream & is)
                        }
 
                        case catIgnore: {
-                               if (c != 13)
-                                       cerr << "ignoring a char: " << int(c) << "\n";
+                               cerr << "ignoring a char: " << int(c) << "\n";
                                break;
                        }