#include <iostream>
#include <sstream>
+
+namespace lyx {
+
using std::cerr;
using std::endl;
using std::fill;
using std::istream;
using std::istringstream;
+using std::ostringstream;
using std::ostream;
using std::string;
}
-string Parser::getArg(char left, char right)
+Parser::Arg Parser::getFullArg(char left, char right)
{
skip_spaces(true);
// This is needed if a partial file ends with a command without arguments,
// e. g. \medskip
if (! good())
- return string();
+ return std::make_pair(false, string());
string result;
char c = getChar();
- if (c != left)
+ if (c != left) {
putback();
- else
+ return std::make_pair(false, string());
+ } else
while ((c = getChar()) != right && good()) {
// Ignore comments
if (curr_token().cat() == catComment) {
result += curr_token().asInput();
}
- return result;
+ return std::make_pair(true, result);
+}
+
+
+string Parser::getArg(char left, char right)
+{
+ return getFullArg(left, right).second;
+}
+
+
+string Parser::getFullOpt()
+{
+ Arg arg = getFullArg('[', ']');
+ if (arg.first)
+ return '[' + arg.second + ']';
+ return arg.second;
}
string Parser::getOpt()
{
string const res = getArg('[', ']');
- return res.size() ? '[' + res + ']' : string();
+ return res.empty() ? string() : '[' + res + ']';
+}
+
+
+string const Parser::verbatimEnvironment(string const & name)
+{
+ if (!good())
+ return string();
+
+ ostringstream os;
+ for (Token t = get_token(); good(); t = get_token()) {
+ if (t.cat() == catBegin) {
+ putback();
+ os << '{' << verbatim_item() << '}';
+ } else if (t.asInput() == "\\begin") {
+ string const env = getArg('{', '}');
+ os << "\\begin{" << env << '}'
+ << verbatimEnvironment(env)
+ << "\\end{" << env << '}';
+ } else if (t.asInput() == "\\end") {
+ string const end = getArg('{', '}');
+ if (end != name)
+ cerr << "\\end{" << end
+ << "} does not match \\begin{" << name
+ << "}." << endl;
+ return os.str();
+ } else
+ os << t.asInput();
+ }
+ cerr << "unexpected end of input" << endl;
+ return os.str();
}
{
return theCatcode[(unsigned char)c];
}
+
+
+} // namespace lyx