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
This commit is contained in:
Jean-Marc Lasgouttes 2013-02-22 15:35:38 +01:00
parent 683050d4f9
commit e5a9199927
3 changed files with 40 additions and 16 deletions

View 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()) if (!good())
return string(); return Arg(false, string());
pushPosition();
ostringstream oss; ostringstream oss;
size_t match_index = 0; size_t match_index = 0;
setCatcodes(VERBATIM_CATCODES); setCatcodes(VERBATIM_CATCODES);
@ -646,22 +647,38 @@ string const Parser::verbatimStuff(string const & end_string)
match_index += t.asInput().length(); match_index += t.asInput().length();
if (match_index >= end_string.length()) if (match_index >= end_string.length())
break; break;
} else if (match_index) { } else {
oss << end_string.substr(0, match_index) << t.asInput(); if (!allow_linebreak && t.asInput() == "\n") {
match_index = 0; cerr << "unexpected end of input" << endl;
} else popPosition();
oss << t.asInput(); 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();
}
}
if (!good()) {
cerr << "unexpected end of input" << endl;
popPosition();
setCatcodes(NORMAL_CATCODES);
return Arg(false, string());
} }
setCatcodes(NORMAL_CATCODES); setCatcodes(NORMAL_CATCODES);
if (!good()) dropPosition();
cerr << "unexpected end of input" << endl; return Arg(true, oss.str());
return oss.str();
} }
string const Parser::verbatimEnvironment(string const & name) 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 // ignore one newline at beginning or end of string
if (prefixIs(s, "\n")) if (prefixIs(s, "\n"))
s.erase(0,1); s.erase(0,1);

View File

@ -280,9 +280,13 @@ public:
* stopped at string \p end_string. Contrary to the other * stopped at string \p end_string. Contrary to the other
* methods, this uses proper catcode setting. This function is * methods, this uses proper catcode setting. This function is
* designed to parse verbatim environments and command. The * 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. * \returns the contents of the environment \p name.
* <tt>\begin{name}</tt> must be parsed already, * <tt>\begin{name}</tt> must be parsed already,

View 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. // set catcodes to verbatim early, just in case.
p.setCatcodes(VERBATIM_CATCODES); p.setCatcodes(VERBATIM_CATCODES);
string delim = p.get_token().asInput(); string delim = p.get_token().asInput();
s = p.verbatimStuff(delim); //FIXME: handler error condition
s = p.verbatimStuff(delim).second;
// context.new_paragraph(os); // context.new_paragraph(os);
} else } else
s = p.verbatimEnvironment("lstlisting"); 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. // set catcodes to verbatim early, just in case.
p.setCatcodes(VERBATIM_CATCODES); p.setCatcodes(VERBATIM_CATCODES);
string delim = p.get_token().asInput(); 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); 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(); string delim = p.get_token().asInput();
if (delim != "{") if (delim != "{")
cerr << "Warning: bad delimiter for command " << t.asInput() << endl; 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); Context newcontext(true, context.textclass);
if (newinsetlayout->forcePlainLayout()) if (newinsetlayout->forcePlainLayout())
newcontext.layout = &context.textclass.plainLayout(); newcontext.layout = &context.textclass.plainLayout();