]> git.lyx.org Git - lyx.git/blobdiff - src/tex2lyx/Parser.cpp
More requires --> required, for C++2a.
[lyx.git] / src / tex2lyx / Parser.cpp
index 69e3460f459de17c1ccc6f15b564eeac8589af69..a538c760efe2ac8f83002cd2a581544bf5c8cfe4 100644 (file)
@@ -12,7 +12,6 @@
 
 #include "Encoding.h"
 #include "Parser.h"
-#include "support/foreach.h"
 #include "support/lstrings.h"
 #include "support/textutils.h"
 
@@ -49,7 +48,7 @@ char_type getNewline(iparserdocstream & is, char_type c)
        return c;
 }
 
-}
+} // namespace
 
 //
 // Token
@@ -162,6 +161,7 @@ Parser::Parser(idocstream & is, std::string const & fixedenc)
 {
        if (fixed_enc_)
                is_.setEncoding(fixedenc);
+       catInit();
 }
 
 
@@ -173,6 +173,7 @@ Parser::Parser(string const & s)
          // An idocstringstream can not change the encoding
          fixed_enc_(true)
 {
+       catInit();
 }
 
 
@@ -338,7 +339,7 @@ Token const Parser::get_token()
                if (pos_ >= tokens_.size())
                        return dummy;
        }
-       // cerr << "looking at token " << tokens_[pos_] 
+       // cerr << "looking at token " << tokens_[pos_]
        //      << " pos: " << pos_ << '\n';
        return tokens_[pos_++];
 }
@@ -452,7 +453,7 @@ bool Parser::good()
 }
 
 
-bool Parser::hasOpt()
+bool Parser::hasOpt(string const l)
 {
        // An optional argument can occur in any of the following forms:
        // - \foo[bar]
@@ -478,7 +479,7 @@ bool Parser::hasOpt()
                putback();
                break;
        }
-       bool const retval = (next_token().asInput() == "[");
+       bool const retval = (next_token().asInput() == l);
        pos_ = oldpos;
        return retval;
 }
@@ -493,6 +494,7 @@ Parser::Arg Parser::getFullArg(char left, char right, bool allow_escaping)
        if (! good())
                return make_pair(false, string());
 
+       int group_level = 0;
        string result;
        Token t = get_token();
 
@@ -503,6 +505,15 @@ Parser::Arg Parser::getFullArg(char left, char right, bool allow_escaping)
        } else {
                while (good()) {
                        t = get_token();
+                       // honor grouping
+                       if (left != '{' && t.cat() == catBegin) {
+                               ++group_level;
+                               continue;
+                       }
+                       if (left != '{' && t.cat() == catEnd) {
+                               --group_level;
+                               continue;
+                       }
                        // Ignore comments
                        if (t.cat() == catComment) {
                                if (!t.cs().empty())
@@ -510,13 +521,15 @@ Parser::Arg Parser::getFullArg(char left, char right, bool allow_escaping)
                                continue;
                        }
                        if (allow_escaping) {
-                               if (t.cat() != catEscape && t.character() == right)
+                               if (t.cat() != catEscape && t.character() == right
+                                   && group_level == 0)
                                        break;
                        } else {
                                if (t.character() == right) {
                                        if (t.cat() == catEscape)
                                                result += '\\';
-                                       break;
+                                       if (group_level == 0)
+                                               break;
                                }
                        }
                        result += t.asInput();
@@ -532,11 +545,11 @@ string Parser::getArg(char left, char right, bool allow_escaping)
 }
 
 
-string Parser::getFullOpt(bool keepws)
+string Parser::getFullOpt(bool keepws, char left, char right)
 {
-       Arg arg = getFullArg('[', ']');
+       Arg arg = getFullArg(left, right);
        if (arg.first)
-               return '[' + arg.second + ']';
+               return left + arg.second + right;
        if (keepws)
                unskip_spaces(true);
        return string();
@@ -564,6 +577,26 @@ string Parser::getFullParentheseArg()
 }
 
 
+bool Parser::hasListPreamble(string const itemcmd)
+{
+       // remember current position
+       unsigned int oldpos = pos_;
+       // jump over arguments
+       if (hasOpt())
+               getOpt();
+       if (hasOpt("{"))
+               getArg('{', '}');
+       // and swallow spaces and comments
+       skip_spaces(true);
+       // we have a list preamble if the next thing
+       // that follows is not the \item command
+       bool res =  next_token().cs() != itemcmd;
+       // back to orig position
+       pos_ = oldpos;
+       return res;
+}
+
+
 string const Parser::ertEnvironment(string const & name)
 {
        if (!good())
@@ -637,6 +670,27 @@ string const Parser::plainCommand(char left, char right, string const & name)
 }
 
 
+string const Parser::getCommandLatexParam()
+{
+       if (!good())
+               return string();
+       string res;
+       size_t offset = 0;
+       while (true) {
+               if (pos_ + offset >= tokens_.size())
+                       tokenize_one();
+               if (pos_ + offset >= tokens_.size())
+                       break;
+               Token t = tokens_[pos_ + offset];
+               if (t.cat() == catBegin)
+                       break;
+               res += t.asInput();
+               ++offset;
+       }
+       return res;
+}
+
+
 Parser::Arg Parser::verbatimStuff(string const & end_string, bool const allow_linebreak)
 {
        if (!good())
@@ -661,7 +715,7 @@ Parser::Arg Parser::verbatimStuff(string const & end_string, bool const allow_li
                                return Arg(false, string());
                        }
                        if (match_index) {
-                               oss << end_string.substr(0, match_index) 
+                               oss << end_string.substr(0, match_index)
                                    << t.asInput();
                                match_index = 0;
                        } else
@@ -719,7 +773,7 @@ string Parser::verbatim_item()
        if (next_token().cat() == catBegin) {
                Token t = get_token(); // skip brace
                string res;
-               for (Token t = get_token(); t.cat() != catEnd && good(); t = get_token()) {
+               for (t = get_token(); t.cat() != catEnd && good(); t = get_token()) {
                        if (t.cat() == catBegin) {
                                putback();
                                res += '{' + verbatim_item() + '}';