Console: Replace Executor cwd handling with explicit path handling
[senf.git] / Console / Parse.cc
index a631f0f..57aba80 100644 (file)
@@ -30,7 +30,6 @@
 #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"
@@ -41,6 +40,8 @@ namespace senf {
 namespace console {
 namespace detail {
 
+#ifndef DOXYGEN
+
     struct ParserAccess
     {
         static void init(ParseCommandInfo & info)
@@ -49,23 +50,11 @@ 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
@@ -81,107 +70,117 @@ namespace detail {
             ParseDispatcher & dispatcher;
         };
 
-        void beginCommand(std::vector<std::string> & command)
+        void beginCommand(std::vector<Token> & 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_); }
+            { cb_(info_); }
 
-        void openGroup()
-            { ParserAccess::startArgument(info_); }
+        void pushToken(Token const & token)
+            { ParserAccess::addToken(info_, token); }
 
-        void closeGroup()
-            { ParserAccess::endArgument(info_); }
-
-        void pushPunctuation(std::string const & token)
-            { ParserAccess::addToken(info_, ParserAccess::makeToken(token)); }
-
-        void pushWord(std::string const & token)
-            { ParserAccess::addToken(info_, ParserAccess::makeToken(token)); }
-
-        void builtin_cd(std::vector<std::string> & path)
+        void builtin_cd(std::vector<Token> & path)
             { ParserAccess::init(info_);
               ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinCD);
               setBuiltinPathArg(path);
-              ParserAccess::finalize(info_); cb_(info_); }
+              cb_(info_); }
 
-        void builtin_ls(std::vector<std::string> & path)
+        void builtin_ls(std::vector<Token> & path)
             { ParserAccess::init(info_);
               ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinLS);
               setBuiltinPathArg(path);
-              ParserAccess::finalize(info_); cb_(info_); }
+              cb_(info_); }
 
-        void pushDirectory(std::vector<std::string> & path)
+        void pushDirectory(std::vector<Token> & path)
             { ParserAccess::init(info_);
               ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinPUSHD);
               setBuiltinPathArg(path);
-              ParserAccess::finalize(info_); cb_(info_); }
+              cb_(info_); }
 
         void popDirectory()
             { ParserAccess::init(info_);
               ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinPOPD);
-              ParserAccess::finalize(info_); cb_(info_); }
+              cb_(info_); }
         
         void builtin_exit()
             { ParserAccess::init(info_);
               ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinEXIT);
-              ParserAccess::finalize(info_); cb_(info_); }
+              cb_(info_); }
+
+        void builtin_help(std::vector<Token> & path)
+            { ParserAccess::init(info_);
+              ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinHELP);
+              setBuiltinPathArg(path);
+              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)
 {
-    typedef ParseCommandInfo::argument_value_type result_type;
-    
-    MakeRange() {}
-    MakeRange(ParseCommandInfo::token_iterator b) : b_ (b) {}
-    
-    senf::console::ParseCommandInfo::token_iterator b_;
-    
-    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", "pushd", "popd", "exit" };
+        char const * builtins[] = { "", "cd", "ls", "pushd", "popd", "exit", "help" };
         stream << "builtin-" << builtins[info.builtin()];
     }
         
@@ -203,8 +202,52 @@ prefix_ std::ostream & senf::console::operator<<(std::ostream & stream,
 }
 
 ///////////////////////////////////////////////////////////////////////////
+// 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_;
+}
+
+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;
@@ -216,6 +259,8 @@ struct senf::console::CommandParser::Impl
     Impl() : dispatcher(), context(), grammar(dispatcher, context) {}
 };
 
+#endif
+
 prefix_ senf::console::CommandParser::CommandParser()
     : impl_ (new Impl())
 {}