Console: Replace Executor cwd handling with explicit path handling
[senf.git] / Console / Parse.cc
index 1f8a6b2..57aba80 100644 (file)
 #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/Exception.hh"
 
 //#include "Parse.mpp"
 #define prefix_
@@ -38,6 +40,8 @@ namespace senf {
 namespace console {
 namespace detail {
 
+#ifndef DOXYGEN
+
     struct ParserAccess
     {
         static void init(ParseCommandInfo & info)
@@ -46,131 +50,137 @@ namespace detail {
         static void setBuiltin(ParseCommandInfo & info, ParseCommandInfo::BuiltinCommand builtin)
             { info.setBuiltin(builtin); }
 
-        static void setCommand(ParseCommandInfo & info, std::vector<std::string> & commandPath)
+        static void setCommand(ParseCommandInfo & info, std::vector<Token> & 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)
+        static void addToken(ParseCommandInfo & info, Token const & token)
             { info.addToken(token); }
-
-        static void finalize(ParseCommandInfo & info)
-            { info.finalize(); }
-
-        static ArgumentToken makeToken(std::string const & token)
-            { return ArgumentToken(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); }
+        void beginCommand(std::vector<Token> & command)
+            { ParserAccess::init(info_);
+              ParserAccess::setCommand(info_, command); }
 
         void endCommand()
-            { ParserAccess::finalize(info()); }
-
-        void pushArgument(std::string const & argument)
-            { ParserAccess::startArgument(info()); 
-              ParserAccess::addToken(info(), ParserAccess::makeToken(argument)); 
-              ParserAccess::endArgument(info()); }
-
-        void openGroup()
-            { ParserAccess::startArgument(info()); }
+            { cb_(info_); }
 
-        void closeGroup()
-            { ParserAccess::endArgument(info()); }
+        void pushToken(Token const & token)
+            { ParserAccess::addToken(info_, token); }
 
-        void pushPunctuation(std::string const & token)
-            { ParserAccess::addToken(info(), ParserAccess::makeToken(token)); }
+        void builtin_cd(std::vector<Token> & path)
+            { ParserAccess::init(info_);
+              ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinCD);
+              setBuiltinPathArg(path);
+              cb_(info_); }
 
-        void pushWord(std::string const & token)
-            { ParserAccess::addToken(info(), ParserAccess::makeToken(token)); }
+        void builtin_ls(std::vector<Token> & path)
+            { ParserAccess::init(info_);
+              ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinLS);
+              setBuiltinPathArg(path);
+              cb_(info_); }
 
-        void builtin_cd(std::vector<std::string> & path)
-            { ParserAccess::init(info());
-              ParserAccess::setBuiltin(info(), ParseCommandInfo::BuiltinCD);
+        void pushDirectory(std::vector<Token> & path)
+            { ParserAccess::init(info_);
+              ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinPUSHD);
               setBuiltinPathArg(path);
-              ParserAccess::finalize(info()); }
+              cb_(info_); }
 
-        void builtin_ls(std::vector<std::string> & path)
-            { ParserAccess::init(info());
-              ParserAccess::setBuiltin(info(), ParseCommandInfo::BuiltinLS);
+        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<Token> & path)
+            { ParserAccess::init(info_);
+              ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinHELP);
               setBuiltinPathArg(path);
-              ParserAccess::finalize(info()); }
+              cb_(info_); }
 
-        void setBuiltinPathArg(std::vector<std::string> & path)
+        void setBuiltinPathArg(std::vector<Token> & path)
             {
-                ParserAccess::startArgument(info());
-                for (std::vector<std::string>::const_iterator i (path.begin());
+                pushToken(Token(Token::ArgumentGroupOpen, "("));
+                for (std::vector<Token>::const_iterator i (path.begin());
                      i != path.end(); ++i)
-                    ParserAccess::addToken(info(), ParserAccess::makeToken(*i));
-                ParserAccess::endArgument(info());
+                    pushToken(*i);
+                pushToken(Token(Token::ArgumentGroupClose, ")"));
             }
     };
 
+#endif
+
 }}}
 
 ///////////////////////////////////////////////////////////////////////////
-// senf::console::ParseCommandInfo
+// senf::console::Token
 
-struct senf::console::ParseCommandInfo::MakeRange
+prefix_ std::ostream & senf::console::operator<<(std::ostream & os, Token const & token)
 {
-    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();
+    static char const * tokenTypeName[] = {
+        "None",
+        "PathSeparator",
+        "ArgumentGroupOpen",
+        "ArgumentGroupClose",
+        "DirectoryGroupOpen",
+        "DirectoryGroupClose",
+        "CommandTerminator",
+        "OtherPunctuation",
+        "BasicString",
+        "HexString",
+        "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
+    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 };
+    os << tokenTypeName[ token.type() 
+                         ? bitPosition[((token.type() & -token.type()) * 0x077CB531UL) >> 27]+1
+                         : 0 ]
+       << "('"
+       << token.value()
+       << "')";
+    return os;
 }
 
+///////////////////////////////////////////////////////////////////////////
+// senf::console::ParseCommandInfo
+
 prefix_ std::ostream & senf::console::operator<<(std::ostream & stream,
                                                  ParseCommandInfo const & info)
 {
-    if (info.builtin() == ParseCommandInfo::NoBuiltin) 
-        stream << senf::stringJoin(info.commandPath(), "/");
+    if (info.builtin() == ParseCommandInfo::NoBuiltin) {
+        ParseCommandInfo::TokensRange::const_iterator i (info.commandPath().begin());
+        ParseCommandInfo::TokensRange::const_iterator const i_end (info.commandPath().end());
+        if (i != i_end) {
+            for (;;) {
+                stream << i->value();
+                if ( ++i != i_end ) stream << "/";
+                else                break;
+            }
+        }
+    }
     else {
-        char const * builtins[] = { "", "cd", "ls" };
+        char const * builtins[] = { "", "cd", "ls", "pushd", "popd", "exit", "help" };
         stream << "builtin-" << builtins[info.builtin()];
     }
         
@@ -192,9 +202,53 @@ prefix_ std::ostream & senf::console::operator<<(std::ostream & stream,
 }
 
 ///////////////////////////////////////////////////////////////////////////
-// senf::console::SingleCommandParser
+// senf::console::ParseCommandInfo::ArgumentIterator
+
+prefix_ void senf::console::ParseCommandInfo::ArgumentIterator::setRange()
+    const
+{
+    if (b_->is(Token::ArgumentGroupOpen)) {
+        unsigned level (0);
+        e_ = b_;
+        for (;;) {
+            if (e_->is(Token::ArgumentGroupOpen))
+                ++ level;
+            else if (e_->is(Token::ArgumentGroupClose)) {
+                -- level;
+                if (level == 0)
+                    break;
+            }
+            ++e_;
+        }
+    }
+    ++ e_;
+}
 
-struct senf::console::SingleCommandParser::Impl
+prefix_ void senf::console::ParseCommandInfo::ArgumentIterator::decrement()
+{
+    e_ = b_;
+    --b_;
+    if (b_->is(Token::ArgumentGroupClose)) {
+        unsigned level (0);
+        for (;;) {
+            if (b_->is(Token::ArgumentGroupClose))
+                ++ level;
+            else if (b_->is(Token::ArgumentGroupOpen)) {
+                -- level;
+                if (level == 0)
+                    break;
+            }
+            --b_;
+        }
+    }
+}
+
+///////////////////////////////////////////////////////////////////////////
+// senf::console::CommandParser
+
+#ifndef DOXYGEN
+
+struct senf::console::CommandParser::Impl
 {
     typedef detail::CommandGrammar<detail::ParseDispatcher> Grammar;
 
@@ -205,18 +259,32 @@ struct senf::console::SingleCommandParser::Impl
     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;