]> git.lyx.org Git - features.git/commitdiff
Improvements to Parser::verbatimStuff
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Fri, 22 Feb 2013 14:35:38 +0000 (15:35 +0100)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Fri, 22 Feb 2013 14:53:40 +0000 (15:53 +0100)
 * return a Parser:Arg pair to indicate whether parsing was successful
 * add new parameter to restrict parsing to the current line

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

index d87ff29546518c39322fe750ce84af98a0f7599f..d0954cf5c1adb611cbfd9230d76b8cdf7f59736f 100644 (file)
@@ -631,11 +631,12 @@ string const Parser::plainCommand(char left, char right, string const & name)
 }
 
 
-string const Parser::verbatimStuff(string const & end_string)
+Parser::Arg Parser::verbatimStuff(string const & end_string, bool const allow_linebreak)
 {
        if (!good())
-               return string();
+               return Arg(false, string());
 
+       pushPosition();
        ostringstream oss;
        size_t match_index = 0;
        setCatcodes(VERBATIM_CATCODES);
@@ -646,22 +647,38 @@ string const Parser::verbatimStuff(string const & end_string)
                        match_index += t.asInput().length();
                        if (match_index >= end_string.length())
                                break;
-               } else if (match_index) {
-                       oss << end_string.substr(0, match_index) << t.asInput();
-                       match_index = 0;
-               } else
-                       oss << t.asInput();
+               } else {
+                       if (!allow_linebreak && t.asInput() == "\n") {
+                               cerr << "unexpected end of input" << endl;
+                               popPosition();
+                               setCatcodes(NORMAL_CATCODES);
+                               return Arg(false, string());
+                       }
+                       if (match_index) {
+                               oss << end_string.substr(0, match_index) 
+                                   << t.asInput();
+                               match_index = 0;
+                       } else
+                               oss << t.asInput();
+               }
        }
-       setCatcodes(NORMAL_CATCODES);
-       if (!good())
+
+       if (!good()) {
                cerr << "unexpected end of input" << endl;
-       return oss.str();
+               popPosition();
+               setCatcodes(NORMAL_CATCODES);
+               return Arg(false, string());
+       }
+       setCatcodes(NORMAL_CATCODES);
+       dropPosition();
+       return Arg(true, oss.str());
 }
 
 
 string const Parser::verbatimEnvironment(string const & name)
 {
-       string s = verbatimStuff("\\end{" + name + "}");
+       //FIXME: do something if endstring is not found
+       string s = verbatimStuff("\\end{" + name + "}").second;
        // ignore one newline at beginning or end of string
        if (prefixIs(s, "\n"))
                s.erase(0,1);
index 42243c6465a581a4427bca8f183310519831aef7..28d5017bdff650531ef266ebf0dfc4ecd25cec09 100644 (file)
@@ -280,9 +280,13 @@ public:
         * stopped at string \p end_string. Contrary to the other
         * methods, this uses proper catcode setting. This function is
         * designed to parse verbatim environments and command. The
-        * intention is to eventually replace all of its siblings.
+        * intention is to eventually replace all of its siblings. the
+        * member \p first of the result tells whether the arg was
+        * found and the member \p second is the value. If \p
+        * allow_linebreak is false, then the parsing is limited to one line
         */
-       std::string const verbatimStuff(std::string const & end_string);
+       Arg verbatimStuff(std::string const & end_string, 
+                         bool allow_linebreak = true);
        /*
         * \returns the contents of the environment \p name.
         * <tt>\begin{name}</tt> must be parsed already,
index a4785d4aed167ae033fcadcab27609a7888f74bc..fae7eb16af00769e5d6385b8d5178c6c461d29ea 100644 (file)
@@ -1189,7 +1189,8 @@ void parse_listings(Parser & p, ostream & os, Context & parent_context, bool in_
                // set catcodes to verbatim early, just in case.
                p.setCatcodes(VERBATIM_CATCODES);
                string delim = p.get_token().asInput();
-               s = p.verbatimStuff(delim);
+               //FIXME: handler error condition
+               s = p.verbatimStuff(delim).second;
 //             context.new_paragraph(os);
        } else
                s = p.verbatimEnvironment("lstlisting");
@@ -3920,7 +3921,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        // set catcodes to verbatim early, just in case.
                        p.setCatcodes(VERBATIM_CATCODES);
                        string delim = p.get_token().asInput();
-                       string const arg = p.verbatimStuff(delim);
+                       //FIXME: handle error condition
+                       string const arg = p.verbatimStuff(delim).second;
                        output_ert_inset(os, "\\verb" + delim + arg + delim, context);
                }
 
@@ -4506,7 +4508,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                string delim = p.get_token().asInput();
                                if (delim != "{")
                                        cerr << "Warning: bad delimiter for command " << t.asInput() << endl;
-                               string const arg = p.verbatimStuff("}");
+                               //FIXME: handle error condition
+                               string const arg = p.verbatimStuff("}").second;
                                Context newcontext(true, context.textclass);
                                if (newinsetlayout->forcePlainLayout())
                                        newcontext.layout = &context.textclass.plainLayout();