Utils/Logger: Remove dependency on libboost_datetime
[senf.git] / Console / Parse.cc
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief Parse non-inline non-template implementation */
25
26 #include "Parse.hh"
27 #include "Parse.ih"
28
29 // Custom includes
30 #include <cerrno>
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"
35
36 //#include "Parse.mpp"
37 #define prefix_
38 ///////////////////////////////cc.p////////////////////////////////////////
39
40 namespace senf {
41 namespace console {
42 namespace detail {
43
44 #ifndef DOXYGEN
45
46     struct ParserAccess
47     {
48         static void init(ParseCommandInfo & info)
49             { info.init(); }
50
51         static void setBuiltin(ParseCommandInfo & info, ParseCommandInfo::BuiltinCommand builtin)
52             { info.setBuiltin(builtin); }
53
54         static void setCommand(ParseCommandInfo & info, std::vector<std::string> & commandPath)
55             { info.setCommand(commandPath); }
56
57         static void addToken(ParseCommandInfo & info, ArgumentToken const & token)
58             { info.addToken(token); }
59
60         static ArgumentToken makeToken(ArgumentToken::TokenType type, std::string const & token)
61             { return ArgumentToken(type, token); }
62     };
63
64     struct ParseDispatcher
65     {
66         ParseCommandInfo info_;
67         CommandParser::Callback cb_;
68
69         struct BindInfo {
70             BindInfo( ParseDispatcher & d, CommandParser::Callback cb)
71                 : dispatcher (d) { dispatcher.cb_ = cb; }
72             ~BindInfo() { dispatcher.cb_  = 0; }
73
74             ParseDispatcher & dispatcher;
75         };
76
77         void beginCommand(std::vector<std::string> & command)
78             { ParserAccess::init(info_);
79               ParserAccess::setCommand(info_, command); }
80
81         void endCommand()
82             { cb_(info_); }
83
84         void pushArgument(ArgumentToken::TokenType type, std::string const & argument)
85             { ParserAccess::addToken(info_, ParserAccess::makeToken(type, argument)); }
86
87         void openGroup()
88             { pushPunctuation("("); }
89
90         void closeGroup()
91             { pushPunctuation(")"); }
92
93         void pushPunctuation(std::string const & token)
94             {
95                 ArgumentToken::TokenType type;
96                 switch (token[0]) {
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;
104                 }
105                 ParserAccess::addToken(info_, ParserAccess::makeToken(type, token));
106             }
107
108         void pushWord(ArgumentToken::TokenType type, std::string const & token)
109             { ParserAccess::addToken(info_, ParserAccess::makeToken(type, token)); }
110
111         void builtin_cd(std::vector<std::string> & path)
112             { ParserAccess::init(info_);
113               ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinCD);
114               setBuiltinPathArg(path);
115               cb_(info_); }
116
117         void builtin_ls(std::vector<std::string> & path)
118             { ParserAccess::init(info_);
119               ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinLS);
120               setBuiltinPathArg(path);
121               cb_(info_); }
122
123         void pushDirectory(std::vector<std::string> & path)
124             { ParserAccess::init(info_);
125               ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinPUSHD);
126               setBuiltinPathArg(path);
127               cb_(info_); }
128
129         void popDirectory()
130             { ParserAccess::init(info_);
131               ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinPOPD);
132               cb_(info_); }
133         
134         void builtin_exit()
135             { ParserAccess::init(info_);
136               ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinEXIT);
137               cb_(info_); }
138
139         void builtin_help(std::vector<std::string> & path)
140             { ParserAccess::init(info_);
141               ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinHELP);
142               setBuiltinPathArg(path);
143               cb_(info_); }
144
145         void setBuiltinPathArg(std::vector<std::string> & path)
146             {
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(")");
153             }
154     };
155
156 #endif
157
158 }}}
159
160 ///////////////////////////////////////////////////////////////////////////
161 // senf::console::ParseCommandInfo
162
163 prefix_ std::ostream & senf::console::operator<<(std::ostream & stream,
164                                                  ParseCommandInfo const & info)
165 {
166     if (info.builtin() == ParseCommandInfo::NoBuiltin) 
167         stream << senf::stringJoin(info.commandPath(), "/");
168     else {
169         char const * builtins[] = { "", "cd", "ls", "pushd", "popd", "exit", "help" };
170         stream << "builtin-" << builtins[info.builtin()];
171     }
172         
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());
176         stream << " [";
177         if ( j != i->end() ) {
178             for (;;) {
179                 stream << "'" << j->value() << "'";
180                 if ( ++j != i->end() ) stream << ' ';
181                 else                   break;
182             }
183         }
184         stream << "]";
185     }
186
187     return stream;
188 }
189
190 ///////////////////////////////////////////////////////////////////////////
191 // senf::console::ParseCommandInfo::ArgumentIterator
192
193 prefix_ void senf::console::ParseCommandInfo::ArgumentIterator::setRange()
194     const
195 {
196     if (b_->is(ArgumentToken::ArgumentGroupOpen)) {
197         unsigned level (0);
198         e_ = b_;
199         for (;;) {
200             if (e_->is(ArgumentToken::ArgumentGroupOpen))
201                 ++ level;
202             else if (e_->is(ArgumentToken::ArgumentGroupClose)) {
203                 -- level;
204                 if (level == 0)
205                     break;
206             }
207             ++e_;
208         }
209     }
210     ++ e_;
211 }
212
213 prefix_ void senf::console::ParseCommandInfo::ArgumentIterator::decrement()
214 {
215     e_ = b_;
216     --b_;
217     if (b_->is(ArgumentToken::ArgumentGroupClose)) {
218         unsigned level (0);
219         for (;;) {
220             if (b_->is(ArgumentToken::ArgumentGroupClose))
221                 ++ level;
222             else if (b_->is(ArgumentToken::ArgumentGroupOpen)) {
223                 -- level;
224                 if (level == 0)
225                     break;
226             }
227             --b_;
228         }
229     }
230 }
231
232 ///////////////////////////////////////////////////////////////////////////
233 // senf::console::CommandParser
234
235 #ifndef DOXYGEN
236
237 struct senf::console::CommandParser::Impl
238 {
239     typedef detail::CommandGrammar<detail::ParseDispatcher> Grammar;
240
241     detail::ParseDispatcher dispatcher;
242     Grammar::Context context;
243     Grammar grammar;
244
245     Impl() : dispatcher(), context(), grammar(dispatcher, context) {}
246 };
247
248 #endif
249
250 prefix_ senf::console::CommandParser::CommandParser()
251     : impl_ (new Impl())
252 {}
253
254 prefix_ senf::console::CommandParser::~CommandParser()
255 {}
256
257 prefix_ bool senf::console::CommandParser::parse(std::string command, Callback cb)
258 {
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>()
263         ).full;
264 }
265
266 prefix_ bool senf::console::CommandParser::parseFile(std::string filename, Callback cb)
267 {
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());
272     
273     return boost::spirit::parse( i, i_end, 
274                                  impl().grammar.use_parser<Impl::Grammar::CommandParser>(),
275                                  impl().grammar.use_parser<Impl::Grammar::SkipParser>()
276         ).full;
277 }
278
279 ///////////////////////////////cc.e////////////////////////////////////////
280 #undef prefix_
281 //#include "Parse.mpp"
282
283 \f
284 // Local Variables:
285 // mode: c++
286 // fill-column: 100
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"
292 // End: