4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief Parse non-inline non-template implementation */
31 #include <boost/iterator/transform_iterator.hpp>
32 #include <boost/spirit/iterator/file_iterator.hpp>
33 #include "../Utils/String.hh"
34 #include "../Utils/Exception.hh"
36 //#include "Parse.mpp"
38 ///////////////////////////////cc.p////////////////////////////////////////
48 static void init(ParseCommandInfo & info)
51 static void setBuiltin(ParseCommandInfo & info, ParseCommandInfo::BuiltinCommand builtin)
52 { info.setBuiltin(builtin); }
54 static void setCommand(ParseCommandInfo & info, std::vector<std::string> & commandPath)
55 { info.setCommand(commandPath); }
57 static void startArgument(ParseCommandInfo & info)
58 { info.startArgument(); }
60 static void endArgument(ParseCommandInfo & info)
61 { info.endArgument(); }
63 static void addToken(ParseCommandInfo & info, ArgumentToken const & token)
64 { info.addToken(token); }
66 static void finalize(ParseCommandInfo & info)
69 static ArgumentToken makeToken(std::string const & token)
70 { return ArgumentToken(token); }
73 struct ParseDispatcher
75 ParseCommandInfo info_;
76 CommandParser::Callback cb_;
79 BindInfo( ParseDispatcher & d, CommandParser::Callback cb)
80 : dispatcher (d) { dispatcher.cb_ = cb; }
81 ~BindInfo() { dispatcher.cb_ = 0; }
83 ParseDispatcher & dispatcher;
86 void beginCommand(std::vector<std::string> & command)
87 { ParserAccess::init(info_);
88 ParserAccess::setCommand(info_, command); }
91 { ParserAccess::finalize(info_); cb_(info_); }
93 void pushArgument(std::string const & argument)
94 { ParserAccess::startArgument(info_);
95 ParserAccess::addToken(info_, ParserAccess::makeToken(argument));
96 ParserAccess::endArgument(info_); }
99 { ParserAccess::startArgument(info_); }
102 { ParserAccess::endArgument(info_); }
104 void pushPunctuation(std::string const & token)
105 { ParserAccess::addToken(info_, ParserAccess::makeToken(token)); }
107 void pushWord(std::string const & token)
108 { ParserAccess::addToken(info_, ParserAccess::makeToken(token)); }
110 void builtin_cd(std::vector<std::string> & path)
111 { ParserAccess::init(info_);
112 ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinCD);
113 setBuiltinPathArg(path);
114 ParserAccess::finalize(info_); cb_(info_); }
116 void builtin_ls(std::vector<std::string> & path)
117 { ParserAccess::init(info_);
118 ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinLS);
119 setBuiltinPathArg(path);
120 ParserAccess::finalize(info_); cb_(info_); }
122 void pushDirectory(std::vector<std::string> & path)
123 { ParserAccess::init(info_);
124 ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinPUSHD);
125 setBuiltinPathArg(path);
126 ParserAccess::finalize(info_); cb_(info_); }
129 { ParserAccess::init(info_);
130 ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinPOPD);
131 ParserAccess::finalize(info_); cb_(info_); }
134 { ParserAccess::init(info_);
135 ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinEXIT);
136 ParserAccess::finalize(info_); cb_(info_); }
138 void builtin_help(std::vector<std::string> & path)
139 { ParserAccess::init(info_);
140 ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinHELP);
141 setBuiltinPathArg(path);
142 ParserAccess::finalize(info_); cb_(info_); }
144 void setBuiltinPathArg(std::vector<std::string> & path)
146 ParserAccess::startArgument(info_);
147 for (std::vector<std::string>::const_iterator i (path.begin());
148 i != path.end(); ++i)
149 ParserAccess::addToken(info_, ParserAccess::makeToken(*i));
150 ParserAccess::endArgument(info_);
158 ///////////////////////////////////////////////////////////////////////////
159 // senf::console::ParseCommandInfo
163 struct senf::console::ParseCommandInfo::MakeRange
165 typedef ParseCommandInfo::argument_value_type result_type;
168 MakeRange(ParseCommandInfo::token_iterator b) : b_ (b) {}
170 senf::console::ParseCommandInfo::token_iterator b_;
172 result_type operator()(TempArguments::iterator::value_type const & v) const {
173 return result_type( b_ + v.first, b_ + v.second );
179 prefix_ void senf::console::ParseCommandInfo::finalize()
181 arguments_.resize( tempArguments_.size() );
183 std::copy( boost::make_transform_iterator( tempArguments_.begin(),
184 MakeRange(tokens_.begin()) ),
185 boost::make_transform_iterator( tempArguments_.end(),
187 arguments_.begin() );
189 tempArguments_.clear();
192 prefix_ std::ostream & senf::console::operator<<(std::ostream & stream,
193 ParseCommandInfo const & info)
195 if (info.builtin() == ParseCommandInfo::NoBuiltin)
196 stream << senf::stringJoin(info.commandPath(), "/");
198 char const * builtins[] = { "", "cd", "ls", "pushd", "popd", "exit", "help" };
199 stream << "builtin-" << builtins[info.builtin()];
202 ParseCommandInfo::ArgumentsRange args (info.arguments());
203 for (ParseCommandInfo::argument_iterator i (args.begin()); i != args.end(); ++i) {
204 ParseCommandInfo::token_iterator j (i->begin());
206 if ( j != i->end() ) {
208 stream << "'" << j->value() << "'";
209 if ( ++j != i->end() ) stream << ' ';
219 ///////////////////////////////////////////////////////////////////////////
220 // senf::console::CommandParser
224 struct senf::console::CommandParser::Impl
226 typedef detail::CommandGrammar<detail::ParseDispatcher> Grammar;
228 detail::ParseDispatcher dispatcher;
229 Grammar::Context context;
232 Impl() : dispatcher(), context(), grammar(dispatcher, context) {}
237 prefix_ senf::console::CommandParser::CommandParser()
241 prefix_ senf::console::CommandParser::~CommandParser()
244 prefix_ bool senf::console::CommandParser::parse(std::string command, Callback cb)
246 detail::ParseDispatcher::BindInfo bind (impl().dispatcher, cb);
247 return boost::spirit::parse( command.begin(), command.end(),
248 impl().grammar.use_parser<Impl::Grammar::CommandParser>(),
249 impl().grammar.use_parser<Impl::Grammar::SkipParser>()
253 prefix_ bool senf::console::CommandParser::parseFile(std::string filename, Callback cb)
255 detail::ParseDispatcher::BindInfo bind (impl().dispatcher, cb);
256 boost::spirit::file_iterator<> i (filename);
257 if (!i) throw SystemException(ENOENT SENF_EXC_DEBUGINFO);
258 boost::spirit::file_iterator<> const i_end (i.make_end());
260 return boost::spirit::parse( i, i_end,
261 impl().grammar.use_parser<Impl::Grammar::CommandParser>(),
262 impl().grammar.use_parser<Impl::Grammar::SkipParser>()
266 ///////////////////////////////cc.e////////////////////////////////////////
268 //#include "Parse.mpp"
274 // comment-column: 40
275 // c-file-style: "senf"
276 // indent-tabs-mode: nil
277 // ispell-local-dictionary: "american"
278 // compile-command: "scons -u test"