#include "Parse.ih"
// Custom includes
-#include "../Utils/String.hh"
+#include <cerrno>
#include <boost/iterator/transform_iterator.hpp>
+#include <boost/spirit/iterator/file_iterator.hpp>
+#include "../Utils/String.hh"
+#include "../Utils/Exception.hh"
//#include "Parse.mpp"
#define prefix_
namespace console {
namespace detail {
+#ifndef DOXYGEN
+
struct ParserAccess
{
static void init(ParseCommandInfo & info)
static void setCommand(ParseCommandInfo & info, std::vector<std::string> & commandPath)
{ info.setCommand(commandPath); }
- static void startArgument(ParseCommandInfo & info)
- { info.startArgument(); }
-
- static void endArgument(ParseCommandInfo & info)
- { info.endArgument(); }
-
static void addToken(ParseCommandInfo & info, ArgumentToken const & token)
{ info.addToken(token); }
- static void finalize(ParseCommandInfo & info)
- { info.finalize(); }
-
- static ArgumentToken makeToken(std::string const & token)
- { return ArgumentToken(token); }
+ static ArgumentToken makeToken(ArgumentToken::TokenType type, std::string const & token)
+ { return ArgumentToken(type, token); }
};
struct ParseDispatcher
{
- ParseDispatcher()
- : info_ (0) {}
-
- ParseCommandInfo * info_;
-
- ParseCommandInfo & info() {
- SENF_ASSERT( info_ );
- return *info_;
- }
+ ParseCommandInfo info_;
+ CommandParser::Callback cb_;
struct BindInfo {
- BindInfo( ParseDispatcher & d, ParseCommandInfo & i)
- : dispatcher (d) { dispatcher.info_ = &i; }
-
- ~BindInfo() { dispatcher.info_ = 0; }
+ BindInfo( ParseDispatcher & d, CommandParser::Callback cb)
+ : dispatcher (d) { dispatcher.cb_ = cb; }
+ ~BindInfo() { dispatcher.cb_ = 0; }
ParseDispatcher & dispatcher;
};
void beginCommand(std::vector<std::string> & command)
- { ParserAccess::init(info());
- ParserAccess::setCommand(info(), command); }
+ { ParserAccess::init(info_);
+ ParserAccess::setCommand(info_, command); }
void endCommand()
- { ParserAccess::finalize(info()); }
+ { cb_(info_); }
- void pushArgument(std::string const & argument)
- { ParserAccess::startArgument(info());
- ParserAccess::addToken(info(), ParserAccess::makeToken(argument));
- ParserAccess::endArgument(info()); }
+ void pushArgument(ArgumentToken::TokenType type, std::string const & argument)
+ { ParserAccess::addToken(info_, ParserAccess::makeToken(type, argument)); }
void openGroup()
- { ParserAccess::startArgument(info()); }
+ { pushPunctuation("("); }
void closeGroup()
- { ParserAccess::endArgument(info()); }
+ { pushPunctuation(")"); }
void pushPunctuation(std::string const & token)
- { ParserAccess::addToken(info(), ParserAccess::makeToken(token)); }
+ {
+ ArgumentToken::TokenType type;
+ switch (token[0]) {
+ case '/' : type = ArgumentToken::PathSeparator; break;
+ case '(' : type = ArgumentToken::ArgumentGroupOpen; break;
+ case ')' : type = ArgumentToken::ArgumentGroupClose; break;
+ case '{' : type = ArgumentToken::DirectoryGroupOpen; break;
+ case '}' : type = ArgumentToken::DirectoryGroupClose; break;
+ case ';' : type = ArgumentToken::CommandTerminator; break;
+ default : type = ArgumentToken::OtherPunctuation; break;
+ }
+ ParserAccess::addToken(info_, ParserAccess::makeToken(type, token));
+ }
- void pushWord(std::string const & token)
- { ParserAccess::addToken(info(), ParserAccess::makeToken(token)); }
+ void pushWord(ArgumentToken::TokenType type, std::string const & token)
+ { ParserAccess::addToken(info_, ParserAccess::makeToken(type, token)); }
void builtin_cd(std::vector<std::string> & path)
- { ParserAccess::init(info());
- ParserAccess::setBuiltin(info(), ParseCommandInfo::BuiltinCD);
+ { ParserAccess::init(info_);
+ ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinCD);
setBuiltinPathArg(path);
- ParserAccess::finalize(info()); }
+ cb_(info_); }
void builtin_ls(std::vector<std::string> & path)
- { ParserAccess::init(info());
- ParserAccess::setBuiltin(info(), ParseCommandInfo::BuiltinLS);
+ { ParserAccess::init(info_);
+ ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinLS);
+ setBuiltinPathArg(path);
+ cb_(info_); }
+
+ void pushDirectory(std::vector<std::string> & path)
+ { ParserAccess::init(info_);
+ ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinPUSHD);
+ setBuiltinPathArg(path);
+ cb_(info_); }
+
+ void popDirectory()
+ { ParserAccess::init(info_);
+ ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinPOPD);
+ cb_(info_); }
+
+ void builtin_exit()
+ { ParserAccess::init(info_);
+ ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinEXIT);
+ cb_(info_); }
+
+ void builtin_help(std::vector<std::string> & path)
+ { ParserAccess::init(info_);
+ ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinHELP);
setBuiltinPathArg(path);
- ParserAccess::finalize(info()); }
+ cb_(info_); }
void setBuiltinPathArg(std::vector<std::string> & path)
{
- ParserAccess::startArgument(info());
+ pushPunctuation("(");
for (std::vector<std::string>::const_iterator i (path.begin());
i != path.end(); ++i)
- ParserAccess::addToken(info(), ParserAccess::makeToken(*i));
- ParserAccess::endArgument(info());
+ ParserAccess::addToken(info_,
+ ParserAccess::makeToken(ArgumentToken::Word, *i));
+ pushPunctuation(")");
}
};
+#endif
+
}}}
///////////////////////////////////////////////////////////////////////////
// senf::console::ParseCommandInfo
-struct senf::console::ParseCommandInfo::MakeRange
-{
- MakeRange() {}
- MakeRange(ParseCommandInfo::token_iterator b) : b_ (b) {}
-
- senf::console::ParseCommandInfo::token_iterator b_;
-
- typedef ParseCommandInfo::argument_value_type result_type;
-
- result_type operator()(TempArguments::iterator::value_type const & v) const {
- return result_type( b_ + v.first, b_ + v.second );
- }
-};
-
-prefix_ void senf::console::ParseCommandInfo::finalize()
-{
- arguments_.resize( tempArguments_.size() );
-
- std::copy( boost::make_transform_iterator( tempArguments_.begin(),
- MakeRange(tokens_.begin()) ),
- boost::make_transform_iterator( tempArguments_.end(),
- MakeRange() ),
- arguments_.begin() );
-
- tempArguments_.clear();
-}
-
prefix_ std::ostream & senf::console::operator<<(std::ostream & stream,
ParseCommandInfo const & info)
{
if (info.builtin() == ParseCommandInfo::NoBuiltin)
stream << senf::stringJoin(info.commandPath(), "/");
else {
- char const * builtins[] = { "", "cd", "ls" };
+ char const * builtins[] = { "", "cd", "ls", "pushd", "popd", "exit", "help" };
stream << "builtin-" << builtins[info.builtin()];
}
}
///////////////////////////////////////////////////////////////////////////
-// senf::console::SingleCommandParser
+// senf::console::ParseCommandInfo::ArgumentIterator
+
+prefix_ void senf::console::ParseCommandInfo::ArgumentIterator::setRange()
+ const
+{
+ if (b_->is(ArgumentToken::ArgumentGroupOpen)) {
+ unsigned level (0);
+ e_ = b_;
+ for (;;) {
+ if (e_->is(ArgumentToken::ArgumentGroupOpen))
+ ++ level;
+ else if (e_->is(ArgumentToken::ArgumentGroupClose)) {
+ -- level;
+ if (level == 0)
+ break;
+ }
+ ++e_;
+ }
+ }
+ ++ e_;
+}
+
+prefix_ void senf::console::ParseCommandInfo::ArgumentIterator::decrement()
+{
+ e_ = b_;
+ --b_;
+ if (b_->is(ArgumentToken::ArgumentGroupClose)) {
+ unsigned level (0);
+ for (;;) {
+ if (b_->is(ArgumentToken::ArgumentGroupClose))
+ ++ level;
+ else if (b_->is(ArgumentToken::ArgumentGroupOpen)) {
+ -- level;
+ if (level == 0)
+ break;
+ }
+ --b_;
+ }
+ }
+}
+
+///////////////////////////////////////////////////////////////////////////
+// senf::console::CommandParser
+
+#ifndef DOXYGEN
-struct senf::console::SingleCommandParser::Impl
+struct senf::console::CommandParser::Impl
{
typedef detail::CommandGrammar<detail::ParseDispatcher> Grammar;
Impl() : dispatcher(), context(), grammar(dispatcher, context) {}
};
-prefix_ senf::console::SingleCommandParser::SingleCommandParser()
+#endif
+
+prefix_ senf::console::CommandParser::CommandParser()
: impl_ (new Impl())
{}
-prefix_ senf::console::SingleCommandParser::~SingleCommandParser()
+prefix_ senf::console::CommandParser::~CommandParser()
{}
-prefix_ bool senf::console::SingleCommandParser::parseCommand(std::string command,
- ParseCommandInfo & info)
+prefix_ bool senf::console::CommandParser::parse(std::string command, 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;
+}
+
+prefix_ bool senf::console::CommandParser::parseFile(std::string filename, Callback cb)
{
- detail::ParseDispatcher::BindInfo bind (impl().dispatcher, info);
- return boost::spirit::parse( command.c_str(),
+ 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>(),
impl().grammar.use_parser<Impl::Grammar::SkipParser>()
).full;