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())
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();
}
}
if (!good()) {
cerr << "unexpected end of input" << endl;
popPosition();
setCatcodes(NORMAL_CATCODES);
return Arg(false, string());
}
setCatcodes(NORMAL_CATCODES);
if (!good())
cerr << "unexpected end of input" << endl;
return oss.str();
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);

View 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,

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.
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();