From: Jean-Marc Lasgouttes Date: Fri, 22 Feb 2013 14:35:38 +0000 (+0100) Subject: Improvements to Parser::verbatimStuff X-Git-Tag: 2.1.0beta1~616 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=e5a91999278253d597ad1e18f322884df4a4c94e;p=features.git Improvements to Parser::verbatimStuff * return a Parser:Arg pair to indicate whether parsing was successful * add new parameter to restrict parsing to the current line --- diff --git a/src/tex2lyx/Parser.cpp b/src/tex2lyx/Parser.cpp index d87ff29546..d0954cf5c1 100644 --- a/src/tex2lyx/Parser.cpp +++ b/src/tex2lyx/Parser.cpp @@ -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); diff --git a/src/tex2lyx/Parser.h b/src/tex2lyx/Parser.h index 42243c6465..28d5017bdf 100644 --- a/src/tex2lyx/Parser.h +++ b/src/tex2lyx/Parser.h @@ -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. * \begin{name} must be parsed already, diff --git a/src/tex2lyx/text.cpp b/src/tex2lyx/text.cpp index a4785d4aed..fae7eb16af 100644 --- a/src/tex2lyx/text.cpp +++ b/src/tex2lyx/text.cpp @@ -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();