]> git.lyx.org Git - lyx.git/blobdiff - src/tex2lyx/texparser.C
Fix bug 2667
[lyx.git] / src / tex2lyx / texparser.C
index 014c9a798f06edad9d82f01af3b8ae0881f79e2e..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;
 
@@ -271,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) {
@@ -296,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();
 }