Console: Add console logging documentation
[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 startArgument(ParseCommandInfo & info)
58             { info.startArgument(); }
59
60         static void endArgument(ParseCommandInfo & info)
61             { info.endArgument(); }
62
63         static void addToken(ParseCommandInfo & info, ArgumentToken const & token)
64             { info.addToken(token); }
65
66         static void finalize(ParseCommandInfo & info)
67             { info.finalize(); }
68
69         static ArgumentToken makeToken(std::string const & token)
70             { return ArgumentToken(token); }
71     };
72
73     struct ParseDispatcher
74     {
75         ParseCommandInfo info_;
76         CommandParser::Callback cb_;
77
78         struct BindInfo {
79             BindInfo( ParseDispatcher & d, CommandParser::Callback cb)
80                 : dispatcher (d) { dispatcher.cb_ = cb; }
81             ~BindInfo() { dispatcher.cb_  = 0; }
82
83             ParseDispatcher & dispatcher;
84         };
85
86         void beginCommand(std::vector<std::string> & command)
87             { ParserAccess::init(info_);
88               ParserAccess::setCommand(info_, command); }
89
90         void endCommand()
91             { ParserAccess::finalize(info_); cb_(info_); }
92
93         void pushArgument(std::string const & argument)
94             { ParserAccess::startArgument(info_); 
95               ParserAccess::addToken(info_, ParserAccess::makeToken(argument)); 
96               ParserAccess::endArgument(info_); }
97
98         void openGroup()
99             { ParserAccess::startArgument(info_); }
100
101         void closeGroup()
102             { ParserAccess::endArgument(info_); }
103
104         void pushPunctuation(std::string const & token)
105             { ParserAccess::addToken(info_, ParserAccess::makeToken(token)); }
106
107         void pushWord(std::string const & token)
108             { ParserAccess::addToken(info_, ParserAccess::makeToken(token)); }
109
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_); }
115
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_); }
121
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_); }
127
128         void popDirectory()
129             { ParserAccess::init(info_);
130               ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinPOPD);
131               ParserAccess::finalize(info_); cb_(info_); }
132         
133         void builtin_exit()
134             { ParserAccess::init(info_);
135               ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinEXIT);
136               ParserAccess::finalize(info_); cb_(info_); }
137
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_); }
143
144         void setBuiltinPathArg(std::vector<std::string> & path)
145             {
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_);
151             }
152     };
153
154 #endif
155
156 }}}
157
158 ///////////////////////////////////////////////////////////////////////////
159 // senf::console::ParseCommandInfo
160
161 #ifndef DOXYGEN
162
163 struct senf::console::ParseCommandInfo::MakeRange
164 {
165     typedef ParseCommandInfo::argument_value_type result_type;
166     
167     MakeRange() {}
168     MakeRange(ParseCommandInfo::token_iterator b) : b_ (b) {}
169     
170     senf::console::ParseCommandInfo::token_iterator b_;
171     
172     result_type operator()(TempArguments::iterator::value_type const & v) const {
173         return result_type( b_ + v.first, b_ + v.second );
174     }
175 };
176
177 #endif
178
179 prefix_ void senf::console::ParseCommandInfo::finalize()
180 {
181     arguments_.resize( tempArguments_.size() );
182
183     std::copy( boost::make_transform_iterator( tempArguments_.begin(), 
184                                                MakeRange(tokens_.begin()) ),
185                boost::make_transform_iterator( tempArguments_.end(), 
186                                                MakeRange() ),
187                arguments_.begin() );
188
189     tempArguments_.clear();
190 }
191
192 prefix_ std::ostream & senf::console::operator<<(std::ostream & stream,
193                                                  ParseCommandInfo const & info)
194 {
195     if (info.builtin() == ParseCommandInfo::NoBuiltin) 
196         stream << senf::stringJoin(info.commandPath(), "/");
197     else {
198         char const * builtins[] = { "", "cd", "ls", "pushd", "popd", "exit", "help" };
199         stream << "builtin-" << builtins[info.builtin()];
200     }
201         
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());
205         stream << " [";
206         if ( j != i->end() ) {
207             for (;;) {
208                 stream << "'" << j->value() << "'";
209                 if ( ++j != i->end() ) stream << ' ';
210                 else                   break;
211             }
212         }
213         stream << "]";
214     }
215
216     return stream;
217 }
218
219 ///////////////////////////////////////////////////////////////////////////
220 // senf::console::CommandParser
221
222 #ifndef DOXYGEN
223
224 struct senf::console::CommandParser::Impl
225 {
226     typedef detail::CommandGrammar<detail::ParseDispatcher> Grammar;
227
228     detail::ParseDispatcher dispatcher;
229     Grammar::Context context;
230     Grammar grammar;
231
232     Impl() : dispatcher(), context(), grammar(dispatcher, context) {}
233 };
234
235 #endif
236
237 prefix_ senf::console::CommandParser::CommandParser()
238     : impl_ (new Impl())
239 {}
240
241 prefix_ senf::console::CommandParser::~CommandParser()
242 {}
243
244 prefix_ bool senf::console::CommandParser::parse(std::string command, Callback cb)
245 {
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>()
250         ).full;
251 }
252
253 prefix_ bool senf::console::CommandParser::parseFile(std::string filename, Callback cb)
254 {
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());
259     
260     return boost::spirit::parse( i, i_end, 
261                                  impl().grammar.use_parser<Impl::Grammar::CommandParser>(),
262                                  impl().grammar.use_parser<Impl::Grammar::SkipParser>()
263         ).full;
264 }
265
266 ///////////////////////////////cc.e////////////////////////////////////////
267 #undef prefix_
268 //#include "Parse.mpp"
269
270 \f
271 // Local Variables:
272 // mode: c++
273 // fill-column: 100
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"
279 // End: