#ifndef DOXYGEN
- struct ParserAccess
- {
- static void init(ParseCommandInfo & info)
- { info.init(); }
-
- static void setBuiltin(ParseCommandInfo & info, ParseCommandInfo::BuiltinCommand builtin)
- { info.setBuiltin(builtin); }
-
- static void setCommand(ParseCommandInfo & info, std::vector<Token> & commandPath)
- { info.setCommand(commandPath); }
-
- static void addToken(ParseCommandInfo & info, Token const & token)
- { info.addToken(token); }
- };
-
struct ParseDispatcher
{
- ParseCommandInfo info_;
- CommandParser::Callback cb_;
+ ParseCommandInfo * info_;
struct BindInfo {
- BindInfo( ParseDispatcher & d, CommandParser::Callback cb)
- : dispatcher (d) { dispatcher.cb_ = cb; }
- ~BindInfo() { dispatcher.cb_ = 0; }
+ BindInfo( ParseDispatcher & d, ParseCommandInfo & info)
+ : dispatcher (d) { dispatcher.info_ = &info; }
+ ~BindInfo() { dispatcher.info_ = 0; }
ParseDispatcher & dispatcher;
};
void beginCommand(std::vector<Token> & command)
- { ParserAccess::init(info_);
- ParserAccess::setCommand(info_, command); }
+ { info_->clear();
+ info_->command(command); }
void endCommand()
- { cb_(info_); }
+ { }
void pushToken(Token const & token)
- { ParserAccess::addToken(info_, token); }
+ { info_->addToken(token); }
void builtin_cd(std::vector<Token> & path)
- { ParserAccess::init(info_);
- ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinCD);
- setBuiltinPathArg(path);
- cb_(info_); }
+ { info_->clear();
+ info_->builtin(ParseCommandInfo::BuiltinCD);
+ setBuiltinPathArg(path); }
void builtin_ls(std::vector<Token> & path)
- { ParserAccess::init(info_);
- ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinLS);
- setBuiltinPathArg(path);
- cb_(info_); }
+ { info_->clear();
+ info_->builtin(ParseCommandInfo::BuiltinLS);
+ setBuiltinPathArg(path); }
void pushDirectory(std::vector<Token> & path)
- { ParserAccess::init(info_);
- ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinPUSHD);
- setBuiltinPathArg(path);
- cb_(info_); }
+ { info_->clear();
+ info_->builtin(ParseCommandInfo::BuiltinPUSHD);
+ setBuiltinPathArg(path); }
void popDirectory()
- { ParserAccess::init(info_);
- ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinPOPD);
- cb_(info_); }
+ { info_->clear();
+ info_->builtin(ParseCommandInfo::BuiltinPOPD); }
void builtin_exit()
- { ParserAccess::init(info_);
- ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinEXIT);
- cb_(info_); }
+ { info_->clear();
+ info_->builtin(ParseCommandInfo::BuiltinEXIT); }
void builtin_help(std::vector<Token> & path)
- { ParserAccess::init(info_);
- ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinHELP);
- setBuiltinPathArg(path);
- cb_(info_); }
+ { info_->clear();
+ info_->builtin(ParseCommandInfo::BuiltinHELP);
+ setBuiltinPathArg(path); }
void setBuiltinPathArg(std::vector<Token> & path)
{
- pushToken(Token(Token::ArgumentGroupOpen, "("));
+ pushToken(ArgumentGroupOpenToken());
for (std::vector<Token>::const_iterator i (path.begin());
i != path.end(); ++i)
pushToken(*i);
- pushToken(Token(Token::ArgumentGroupClose, ")"));
+ pushToken(ArgumentGroupCloseToken());
}
};
"Word" };
// The real table is:
// static const int bitPosition[32] = {
- // 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
- // 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9 };
- // However, we have replaced all values > sizeof(tokenTypeName) with 0
+ // 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
+ // 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9 };
+ // However, we have replaced all values >= sizeof(tokenTypeName) with 0
+ // and have added 1 to all the remaining values
static const int bitPosition[32] = {
- 0, 1, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 4, 8,
- 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 6, 0, 5, 10, 9 };
+ 1, 2, 0, 3, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 5, 9,
+ 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 7, 0, 6, 0, 10 };
+ // We need to check token.type() against 0 explicitly since 0 and 1 will both be mapped to 0
os << tokenTypeName[ token.type()
- ? bitPosition[((token.type() & -token.type()) * 0x077CB531UL) >> 27]+1
+ ? bitPosition[((token.type() & -token.type()) * 0x077CB531UL) >> 27]
: 0 ]
<< "('"
<< token.value()
}
}
else {
- char const * builtins[] = { "", "cd", "ls", "pushd", "popd", "exit", "help" };
+ char const * builtins[] = { 0, "cd", "ls", "pushd", "popd", "exit", "help" };
stream << "builtin-" << builtins[info.builtin()];
}
prefix_ senf::console::CommandParser::~CommandParser()
{}
-prefix_ bool senf::console::CommandParser::parse(std::string command, Callback cb)
+// This template member is placed here, since it is ONLY called from the implementation. Otherwise,
+// we would need to expose the Impl member to the public, which we don't want to do.
+
+template <class Iterator>
+prefix_ Iterator senf::console::CommandParser::parseLoop(Iterator b, Iterator e, Callback cb)
{
- detail::ParseDispatcher::BindInfo bind (impl().dispatcher, cb);
- return boost::spirit::parse( command.begin(), command.end(),
- impl().grammar.use_parser<Impl::Grammar::CommandParser>(),
- impl().grammar.use_parser<Impl::Grammar::SkipParser>()
- ).full;
+ ParseCommandInfo info;
+ detail::ParseDispatcher::BindInfo bind (impl().dispatcher, info);
+ boost::spirit::parse_info<Iterator> result;
+
+ for(;;) {
+ result = boost::spirit::parse(
+ b, e, * impl().grammar.use_parser<Impl::Grammar::SkipParser>());
+ b = result.stop;
+ if (b == e)
+ return e;
+ info.clear();
+ result = boost::spirit::parse(b, e,
+ impl().grammar.use_parser<Impl::Grammar::CommandParser>(),
+ impl().grammar.use_parser<Impl::Grammar::SkipParser>());
+ if (! result.hit)
+ return b;
+ if (! info.empty())
+ cb(info);
+ b = result.stop;
+ }
+}
+
+prefix_ bool senf::console::CommandParser::parse(std::string const & command, Callback cb)
+{
+ return parseLoop(command.begin(), command.end(), cb) == command.end();
}
-prefix_ bool senf::console::CommandParser::parseFile(std::string filename, Callback cb)
+prefix_ bool senf::console::CommandParser::parseFile(std::string const & filename, Callback cb)
{
- detail::ParseDispatcher::BindInfo bind (impl().dispatcher, cb);
boost::spirit::file_iterator<> i (filename);
if (!i) throw SystemException(ENOENT SENF_EXC_DEBUGINFO);
boost::spirit::file_iterator<> const i_end (i.make_end());
- return boost::spirit::parse( i, i_end,
- impl().grammar.use_parser<Impl::Grammar::CommandParser>(),
+ return parseLoop(i, i_end, cb) == i_end;
+}
+
+prefix_ bool senf::console::CommandParser::parseArguments(std::string const & arguments,
+ ParseCommandInfo & info)
+{
+ detail::ParseDispatcher::BindInfo bind (impl().dispatcher, info);
+ return boost::spirit::parse( arguments.begin(), arguments.end(),
+ impl().grammar.use_parser<Impl::Grammar::ArgumentsParser>(),
impl().grammar.use_parser<Impl::Grammar::SkipParser>()
).full;
}
+struct senf::console::CommandParser::SetIncremental
+{
+ SetIncremental(CommandParser & parser) : parser_ (parser) {
+ parser_.impl().grammar.incremental = true;
+ }
+
+ ~SetIncremental() {
+ parser_.impl().grammar.incremental = false;
+ }
+
+ CommandParser & parser_;
+};
+
+prefix_ std::string::size_type
+senf::console::CommandParser::parseIncremental(std::string const & commands, Callback cb)
+{
+ SetIncremental si (*this);
+ return std::distance( commands.begin(),
+ parseLoop(commands.begin(), commands.end(), cb) );
+}
+
+///////////////////////////////////////////////////////////////////////////
+// senf::console::SyntaxErrorException
+
+prefix_ char const * senf::console::SyntaxErrorException::what()
+ const throw()
+{
+ return message().empty() ? "syntax error" : message().c_str();
+}
+
///////////////////////////////cc.e////////////////////////////////////////
#undef prefix_
//#include "Parse.mpp"