]> git.lyx.org Git - features.git/commitdiff
Clean up parsing of optional arguments:
authorGeorg Baum <Georg.Baum@post.rwth-aachen.de>
Fri, 28 Jan 2011 20:29:06 +0000 (20:29 +0000)
committerGeorg Baum <Georg.Baum@post.rwth-aachen.de>
Fri, 28 Jan 2011 20:29:06 +0000 (20:29 +0000)
If an optional argument can be followed by another one, we need to use
getFullOpt(). Otherwise it would not be possible to parse \foo[][bar].
Remove getOptContent to avoid confusion (it does exactly the same as
getArg('[', ']') which is used in most places.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@37354 a592a061-630c-0410-9148-cb99ea01b6c8

src/tex2lyx/Parser.cpp
src/tex2lyx/Parser.h
src/tex2lyx/preamble.cpp
src/tex2lyx/text.cpp

index d2998bf9e2a74b24bfa9368d729e19a4fb5ebbfa..5fafef9944c6f7ebde6ba693bfdf8bd50f16483b 100644 (file)
@@ -406,11 +406,13 @@ string Parser::getArg(char left, char right)
 }
 
 
-string Parser::getFullOpt()
+string Parser::getFullOpt(bool keepws)
 {
        Arg arg = getFullArg('[', ']');
        if (arg.first)
                return '[' + arg.second + ']';
+       if (keepws)
+               unskip_spaces(true);
        return string();
 }
 
@@ -427,14 +429,6 @@ string Parser::getOpt(bool keepws)
 }
 
 
-string Parser::getOptContent()
-// the same as getOpt but without the brackets
-{
-       string const res = getArg('[', ']');
-       return res.empty() ? string() : res;
-}
-
-
 string Parser::getFullParentheseArg()
 {
        Arg arg = getFullArg('(', ')');
index 34354154678fed9045285a6c692397cee0600994..dbb202ddf008c17c7a1cef10af2fa6779e9c4c89 100644 (file)
@@ -170,10 +170,10 @@ public:
         */
        std::string getArg(char left, char right);
        /*!
-        * \returns getFullArg('[', ']') including the brackets or the
-        * empty string if there is no such argument.
+        * Like getOpt(), but distinguishes between a missing argument ""
+        * and an empty argument "[]".
         */
-       std::string getFullOpt();
+       std::string getFullOpt(bool keepws = false);
        /*!
         * \returns getArg('[', ']') including the brackets or the
         * empty string if there is no such argument.
@@ -185,10 +185,6 @@ public:
         * during .tex export, thus creating an invalid command.
         */
        std::string getOpt(bool keepws = false);
-       /*!
-        * the same as getOpt but without the brackets
-        */
-       std::string getOptContent();
        /*!
         * \returns getFullArg('(', ')') including the parentheses or the
         * empty string if there is no such argument.
index e7aaf18f18c94b764963bcd815b8a67c43fca5dd..8de4f3b7150869634278e1fac1172333626bc564 100644 (file)
@@ -848,7 +848,7 @@ void parse_preamble(Parser & p, ostream & os,
                                star = true;
                        }
                        string const name = p.verbatim_item();
-                       string const opt1 = p.getOpt();
+                       string const opt1 = p.getFullOpt();
                        string const opt2 = p.getFullOpt();
                        string const body = p.verbatim_item();
                        // font settings
@@ -961,8 +961,8 @@ void parse_preamble(Parser & p, ostream & os,
 
                else if (t.cs() == "newenvironment") {
                        string const name = p.getArg('{', '}');
-                       string const opt1 = p.getOpt();
-                       string const opt2 = p.getOpt();
+                       string const opt1 = p.getFullOpt();
+                       string const opt2 = p.getFullOpt();
                        string const beg = p.verbatim_item();
                        string const end = p.verbatim_item();
                        if (!in_lyx_preamble) {
index adda18c38656105ba90f9ac98a369797a6a5a41a..23435d93374c7ba491b9fc5b81163d39b3924a00 100644 (file)
@@ -641,7 +641,13 @@ void parse_arguments(string const & command,
                        break;
                case optional:
                        // true because we must not eat whitespace
-                       ert += p.getOpt(true);
+                       // if an optional arg follows me must not strip the
+                       // brackets from this one
+                       if (i < no_arguments - 1 &&
+                           template_arguments[i+1] == optional)
+                               ert += p.getFullOpt(true);
+                       else
+                               ert += p.getOpt(true);
                        break;
                }
        }
@@ -712,8 +718,8 @@ void parse_box(Parser & p, ostream & os, unsigned outer_flags,
                        latex_width = "\\columnwidth";
                else {
                        Parser p2(special);
-                       latex_width = p2.getOptContent();
-                       string const opt = p2.getOptContent();
+                       latex_width = p2.getArg('[', ']');
+                       string const opt = p2.getArg('[', ']');
                        if (!opt.empty()) {
                                hor_pos = opt;
                                if (hor_pos != "l" && hor_pos != "c" &&
@@ -1874,7 +1880,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        context.set_item();
                        context.check_layout(os);
                        begin_command_inset(os, "bibitem", "bibitem");
-                       os << "label \"" << p.getOptContent() << "\"\n";
+                       os << "label \"" << p.getArg('[', ']') << "\"\n";
                        os << "key \"" << p.verbatim_item() << "\"\n";
                        end_inset(os);
                }
@@ -2425,7 +2431,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        || t.cs() == "nocite") {
                        context.check_layout(os);
                        // LyX cannot handle newlines in a latex command
-                       string after = subst(p.getOptContent(), "\n", " ");
+                       string after = subst(p.getArg('[', ']'), "\n", " ");
                        string key = subst(p.verbatim_item(), "\n", " ");
                        // store the case that it is "\nocite{*}" to use it later for
                        // the BibTeX inset
@@ -2450,7 +2456,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        context.check_layout(os);
                        begin_command_inset(os, "nomenclature", "nomenclature");
                        // LyX cannot handle newlines in a latex command
-                       string prefix = subst(p.getOptContent(), "\n", " ");
+                       string prefix = subst(p.getArg('[', ']'), "\n", " ");
                        if (!prefix.empty())
                                os << "prefix " << '"' << prefix << '"' << "\n";
                        os << "symbol " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
@@ -2920,7 +2926,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        parse_outer_box(p, os, FLAG_ITEM, outer, context, t.cs(), "");
 
                else if (t.cs() == "framebox") {
-                       string special = p.getOpt();
+                       string special = p.getFullOpt();
                        special += p.getOpt();
                        parse_outer_box(p, os, FLAG_ITEM, outer, context, t.cs(), special);
                }
@@ -2999,7 +3005,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                name += '*';
                        }
                        string const command = p.verbatim_item();
-                       string const opt1 = p.getOpt();
+                       string const opt1 = p.getFullOpt();
                        string const opt2 = p.getFullOpt();
                        add_known_command(command, opt1, !opt2.empty());
                        string const ert = name + '{' + command + '}' +