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 */
30 #include "../Utils/String.hh"
31 #include <boost/iterator/transform_iterator.hpp>
33 //#include "Parse.mpp"
35 ///////////////////////////////cc.p////////////////////////////////////////
43 static void init(ParseCommandInfo & info)
46 static void setBuiltin(ParseCommandInfo & info, ParseCommandInfo::BuiltinCommand builtin)
47 { info.setBuiltin(builtin); }
49 static void setCommand(ParseCommandInfo & info, std::vector<std::string> & commandPath)
50 { info.setCommand(commandPath); }
52 static void startArgument(ParseCommandInfo & info)
53 { info.startArgument(); }
55 static void endArgument(ParseCommandInfo & info)
56 { info.endArgument(); }
58 static void addToken(ParseCommandInfo & info, ArgumentToken const & token)
59 { info.addToken(token); }
61 static void finalize(ParseCommandInfo & info)
64 static ArgumentToken makeToken(std::string const & token)
65 { return ArgumentToken(token); }
68 struct ParseDispatcher
73 ParseCommandInfo * info_;
75 ParseCommandInfo & info() {
81 BindInfo( ParseDispatcher & d, ParseCommandInfo & i)
82 : dispatcher (d) { dispatcher.info_ = &i; }
84 ~BindInfo() { dispatcher.info_ = 0; }
86 ParseDispatcher & dispatcher;
89 void beginCommand(std::vector<std::string> & command)
90 { ParserAccess::init(info());
91 ParserAccess::setCommand(info(), command); }
94 { ParserAccess::finalize(info()); }
96 void pushArgument(std::string const & argument)
97 { ParserAccess::startArgument(info());
98 ParserAccess::addToken(info(), ParserAccess::makeToken(argument));
99 ParserAccess::endArgument(info()); }
102 { ParserAccess::startArgument(info()); }
105 { ParserAccess::endArgument(info()); }
107 void pushPunctuation(std::string const & token)
108 { ParserAccess::addToken(info(), ParserAccess::makeToken(token)); }
110 void pushWord(std::string const & token)
111 { ParserAccess::addToken(info(), ParserAccess::makeToken(token)); }
113 void builtin_cd(std::vector<std::string> & path)
114 { ParserAccess::init(info());
115 ParserAccess::setBuiltin(info(), ParseCommandInfo::BuiltinCD);
116 setBuiltinPathArg(path);
117 ParserAccess::finalize(info()); }
119 void builtin_ls(std::vector<std::string> & path)
120 { ParserAccess::init(info());
121 ParserAccess::setBuiltin(info(), ParseCommandInfo::BuiltinLS);
122 setBuiltinPathArg(path);
123 ParserAccess::finalize(info()); }
125 void setBuiltinPathArg(std::vector<std::string> & path)
127 ParserAccess::startArgument(info());
128 for (std::vector<std::string>::const_iterator i (path.begin());
129 i != path.end(); ++i)
130 ParserAccess::addToken(info(), ParserAccess::makeToken(*i));
131 ParserAccess::endArgument(info());
137 ///////////////////////////////////////////////////////////////////////////
138 // senf::console::ParseCommandInfo
140 struct senf::console::ParseCommandInfo::MakeRange
143 MakeRange(ParseCommandInfo::token_iterator b) : b_ (b) {}
145 senf::console::ParseCommandInfo::token_iterator b_;
147 typedef ParseCommandInfo::argument_value_type result_type;
149 result_type operator()(TempArguments::iterator::value_type const & v) const {
150 return result_type( b_ + v.first, b_ + v.second );
154 prefix_ void senf::console::ParseCommandInfo::finalize()
156 arguments_.resize( tempArguments_.size() );
158 std::copy( boost::make_transform_iterator( tempArguments_.begin(),
159 MakeRange(tokens_.begin()) ),
160 boost::make_transform_iterator( tempArguments_.end(),
162 arguments_.begin() );
164 tempArguments_.clear();
167 prefix_ std::ostream & senf::console::operator<<(std::ostream & stream,
168 ParseCommandInfo const & info)
170 if (info.builtin() == ParseCommandInfo::NoBuiltin)
171 stream << senf::stringJoin(info.commandPath(), "/");
173 char const * builtins[] = { "", "cd", "ls" };
174 stream << "builtin-" << builtins[info.builtin()];
177 ParseCommandInfo::ArgumentsRange args (info.arguments());
178 for (ParseCommandInfo::argument_iterator i (args.begin()); i != args.end(); ++i) {
179 ParseCommandInfo::token_iterator j (i->begin());
181 if ( j != i->end() ) {
183 stream << "'" << j->value() << "'";
184 if ( ++j != i->end() ) stream << ' ';
194 ///////////////////////////////////////////////////////////////////////////
195 // senf::console::SingleCommandParser
197 struct senf::console::SingleCommandParser::Impl
199 typedef detail::CommandGrammar<detail::ParseDispatcher> Grammar;
201 detail::ParseDispatcher dispatcher;
202 Grammar::Context context;
205 Impl() : dispatcher(), context(), grammar(dispatcher, context) {}
208 prefix_ senf::console::SingleCommandParser::SingleCommandParser()
212 prefix_ senf::console::SingleCommandParser::~SingleCommandParser()
215 prefix_ bool senf::console::SingleCommandParser::parseCommand(std::string command,
216 ParseCommandInfo & info)
218 detail::ParseDispatcher::BindInfo bind (impl().dispatcher, info);
219 return boost::spirit::parse( command.c_str(),
220 impl().grammar.use_parser<Impl::Grammar::CommandParser>(),
221 impl().grammar.use_parser<Impl::Grammar::SkipParser>()
225 ///////////////////////////////cc.e////////////////////////////////////////
227 //#include "Parse.mpp"
233 // comment-column: 40
234 // c-file-style: "senf"
235 // indent-tabs-mode: nil
236 // ispell-local-dictionary: "american"
237 // compile-command: "scons -u test"