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 addToken(ParseCommandInfo & info, ArgumentToken const & token)
58 { info.addToken(token); }
60 static ArgumentToken makeToken(ArgumentToken::TokenType type, std::string const & token)
61 { return ArgumentToken(type, token); }
64 struct ParseDispatcher
66 ParseCommandInfo info_;
67 CommandParser::Callback cb_;
70 BindInfo( ParseDispatcher & d, CommandParser::Callback cb)
71 : dispatcher (d) { dispatcher.cb_ = cb; }
72 ~BindInfo() { dispatcher.cb_ = 0; }
74 ParseDispatcher & dispatcher;
77 void beginCommand(std::vector<std::string> & command)
78 { ParserAccess::init(info_);
79 ParserAccess::setCommand(info_, command); }
84 void pushArgument(ArgumentToken::TokenType type, std::string const & argument)
85 { ParserAccess::addToken(info_, ParserAccess::makeToken(type, argument)); }
88 { pushPunctuation("("); }
91 { pushPunctuation(")"); }
93 void pushPunctuation(std::string const & token)
95 ArgumentToken::TokenType type;
97 case '/' : type = ArgumentToken::PathSeparator; break;
98 case '(' : type = ArgumentToken::ArgumentGroupOpen; break;
99 case ')' : type = ArgumentToken::ArgumentGroupClose; break;
100 case '{' : type = ArgumentToken::DirectoryGroupOpen; break;
101 case '}' : type = ArgumentToken::DirectoryGroupClose; break;
102 case ';' : type = ArgumentToken::CommandTerminator; break;
103 default : type = ArgumentToken::OtherPunctuation; break;
105 ParserAccess::addToken(info_, ParserAccess::makeToken(type, token));
108 void pushWord(ArgumentToken::TokenType type, std::string const & token)
109 { ParserAccess::addToken(info_, ParserAccess::makeToken(type, token)); }
111 void builtin_cd(std::vector<std::string> & path)
112 { ParserAccess::init(info_);
113 ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinCD);
114 setBuiltinPathArg(path);
117 void builtin_ls(std::vector<std::string> & path)
118 { ParserAccess::init(info_);
119 ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinLS);
120 setBuiltinPathArg(path);
123 void pushDirectory(std::vector<std::string> & path)
124 { ParserAccess::init(info_);
125 ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinPUSHD);
126 setBuiltinPathArg(path);
130 { ParserAccess::init(info_);
131 ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinPOPD);
135 { ParserAccess::init(info_);
136 ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinEXIT);
139 void builtin_help(std::vector<std::string> & path)
140 { ParserAccess::init(info_);
141 ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinHELP);
142 setBuiltinPathArg(path);
145 void setBuiltinPathArg(std::vector<std::string> & path)
147 pushPunctuation("(");
148 for (std::vector<std::string>::const_iterator i (path.begin());
149 i != path.end(); ++i)
150 ParserAccess::addToken(info_,
151 ParserAccess::makeToken(ArgumentToken::Word, *i));
152 pushPunctuation(")");
160 ///////////////////////////////////////////////////////////////////////////
161 // senf::console::ParseCommandInfo
163 prefix_ std::ostream & senf::console::operator<<(std::ostream & stream,
164 ParseCommandInfo const & info)
166 if (info.builtin() == ParseCommandInfo::NoBuiltin)
167 stream << senf::stringJoin(info.commandPath(), "/");
169 char const * builtins[] = { "", "cd", "ls", "pushd", "popd", "exit", "help" };
170 stream << "builtin-" << builtins[info.builtin()];
173 ParseCommandInfo::ArgumentsRange args (info.arguments());
174 for (ParseCommandInfo::argument_iterator i (args.begin()); i != args.end(); ++i) {
175 ParseCommandInfo::token_iterator j (i->begin());
177 if ( j != i->end() ) {
179 stream << "'" << j->value() << "'";
180 if ( ++j != i->end() ) stream << ' ';
190 ///////////////////////////////////////////////////////////////////////////
191 // senf::console::ParseCommandInfo::ArgumentIterator
193 prefix_ void senf::console::ParseCommandInfo::ArgumentIterator::setRange()
196 if (b_->is(ArgumentToken::ArgumentGroupOpen)) {
200 if (e_->is(ArgumentToken::ArgumentGroupOpen))
202 else if (e_->is(ArgumentToken::ArgumentGroupClose)) {
213 prefix_ void senf::console::ParseCommandInfo::ArgumentIterator::decrement()
217 if (b_->is(ArgumentToken::ArgumentGroupClose)) {
220 if (b_->is(ArgumentToken::ArgumentGroupClose))
222 else if (b_->is(ArgumentToken::ArgumentGroupOpen)) {
232 ///////////////////////////////////////////////////////////////////////////
233 // senf::console::CommandParser
237 struct senf::console::CommandParser::Impl
239 typedef detail::CommandGrammar<detail::ParseDispatcher> Grammar;
241 detail::ParseDispatcher dispatcher;
242 Grammar::Context context;
245 Impl() : dispatcher(), context(), grammar(dispatcher, context) {}
250 prefix_ senf::console::CommandParser::CommandParser()
254 prefix_ senf::console::CommandParser::~CommandParser()
257 prefix_ bool senf::console::CommandParser::parse(std::string command, Callback cb)
259 detail::ParseDispatcher::BindInfo bind (impl().dispatcher, cb);
260 return boost::spirit::parse( command.begin(), command.end(),
261 impl().grammar.use_parser<Impl::Grammar::CommandParser>(),
262 impl().grammar.use_parser<Impl::Grammar::SkipParser>()
266 prefix_ bool senf::console::CommandParser::parseFile(std::string filename, Callback cb)
268 detail::ParseDispatcher::BindInfo bind (impl().dispatcher, cb);
269 boost::spirit::file_iterator<> i (filename);
270 if (!i) throw SystemException(ENOENT SENF_EXC_DEBUGINFO);
271 boost::spirit::file_iterator<> const i_end (i.make_end());
273 return boost::spirit::parse( i, i_end,
274 impl().grammar.use_parser<Impl::Grammar::CommandParser>(),
275 impl().grammar.use_parser<Impl::Grammar::SkipParser>()
279 ///////////////////////////////cc.e////////////////////////////////////////
281 //#include "Parse.mpp"
287 // comment-column: 40
288 // c-file-style: "senf"
289 // indent-tabs-mode: nil
290 // ispell-local-dictionary: "american"
291 // compile-command: "scons -u test"