Utils/Logger: Remove dependency on libboost_datetime
[senf.git] / Console / Parse.cc
index 502dd1c..52858e9 100644 (file)
 #include "Parse.ih"
 
 // Custom includes
+#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_
@@ -37,133 +41,239 @@ namespace senf {
 namespace console {
 namespace detail {
 
+#ifndef DOXYGEN
+
     struct ParserAccess
     {
         static void init(ParseCommandInfo & info)
             { info.init(); }
 
-        static void setCommand(ParseCommandInfo & info, std::string const & commandPath)
-            { info.setCommand(commandPath); }
-
-        static void startArgument(ParseCommandInfo & info)
-            { info.startArgument(); }
+        static void setBuiltin(ParseCommandInfo & info, ParseCommandInfo::BuiltinCommand builtin)
+            { info.setBuiltin(builtin); }
 
-        static void endArgument(ParseCommandInfo & info)
-            { info.endArgument(); }
+        static void setCommand(ParseCommandInfo & info, std::vector<std::string> & commandPath)
+            { info.setCommand(commandPath); }
 
         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::string const & command)
-            { ParserAccess::init(info());
-              ParserAccess::setCommand(info(), command); }
+        void beginCommand(std::vector<std::string> & 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)); }
-
-        void pushWord(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(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);
+              setBuiltinPathArg(path);
+              cb_(info_); }
+
+        void builtin_ls(std::vector<std::string> & path)
+            { 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);
+              cb_(info_); }
+
+        void setBuiltinPathArg(std::vector<std::string> & path)
+            {
+                pushPunctuation("(");
+                for (std::vector<std::string>::const_iterator i (path.begin());
+                     i != path.end(); ++i)
+                    ParserAccess::addToken(info_, 
+                                           ParserAccess::makeToken(ArgumentToken::Word, *i));
+                pushPunctuation(")");
+            }
     };
 
+#endif
+
 }}}
 
 ///////////////////////////////////////////////////////////////////////////
 // senf::console::ParseCommandInfo
 
-struct senf::console::ParseCommandInfo::MakeRange
+prefix_ std::ostream & senf::console::operator<<(std::ostream & stream,
+                                                 ParseCommandInfo const & info)
 {
-    MakeRange() {}
-    MakeRange(ParseCommandInfo::token_iterator b) : b_ (b) {}
-
-    senf::console::ParseCommandInfo::token_iterator b_;
-
-    typedef ParseCommandInfo::argument_value_type result_type;
+    if (info.builtin() == ParseCommandInfo::NoBuiltin) 
+        stream << senf::stringJoin(info.commandPath(), "/");
+    else {
+        char const * builtins[] = { "", "cd", "ls", "pushd", "popd", "exit", "help" };
+        stream << "builtin-" << builtins[info.builtin()];
+    }
         
-    result_type operator()(TempArguments::iterator::value_type const & v) const {
-        return result_type( b_ + v.first, b_ + v.second );
+    ParseCommandInfo::ArgumentsRange args (info.arguments());
+    for (ParseCommandInfo::argument_iterator i (args.begin()); i != args.end(); ++i) {
+        ParseCommandInfo::token_iterator j (i->begin());
+        stream << " [";
+        if ( j != i->end() ) {
+            for (;;) {
+                stream << "'" << j->value() << "'";
+                if ( ++j != i->end() ) stream << ' ';
+                else                   break;
+            }
+        }
+        stream << "]";
     }
-};
 
-prefix_ void senf::console::ParseCommandInfo::finalize()
-{
-    arguments_.resize( tempArguments_.size() );
+    return stream;
+}
 
-    std::copy( boost::make_transform_iterator( tempArguments_.begin(), 
-                                               MakeRange(tokens_.begin()) ),
-               boost::make_transform_iterator( tempArguments_.end(), 
-                                               MakeRange() ),
-               arguments_.begin() );
+///////////////////////////////////////////////////////////////////////////
+// senf::console::ParseCommandInfo::ArgumentIterator
 
-    tempArguments_.clear();
+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::SingleCommandParser
+// senf::console::CommandParser
 
-struct senf::console::SingleCommandParser::Impl
+#ifndef DOXYGEN
+
+struct senf::console::CommandParser::Impl
 {
+    typedef detail::CommandGrammar<detail::ParseDispatcher> Grammar;
+
     detail::ParseDispatcher dispatcher;
-    detail::CommandGrammar<detail::ParseDispatcher>::Context context;
-    detail::CommandGrammar<detail::ParseDispatcher> grammar;
-    detail::SkipGrammar skipGrammar;
+    Grammar::Context context;
+    Grammar 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(), impl().grammar, impl().skipGrammar ).full;
+    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;
 }
 
 ///////////////////////////////cc.e////////////////////////////////////////