Console: Implement ObjectDirectory proxy
[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     struct ParserAccess
45     {
46         static void init(ParseCommandInfo & info)
47             { info.init(); }
48
49         static void setBuiltin(ParseCommandInfo & info, ParseCommandInfo::BuiltinCommand builtin)
50             { info.setBuiltin(builtin); }
51
52         static void setCommand(ParseCommandInfo & info, std::vector<std::string> & commandPath)
53             { info.setCommand(commandPath); }
54
55         static void startArgument(ParseCommandInfo & info)
56             { info.startArgument(); }
57
58         static void endArgument(ParseCommandInfo & info)
59             { info.endArgument(); }
60
61         static void addToken(ParseCommandInfo & info, ArgumentToken const & token)
62             { info.addToken(token); }
63
64         static void finalize(ParseCommandInfo & info)
65             { info.finalize(); }
66
67         static ArgumentToken makeToken(std::string const & token)
68             { return ArgumentToken(token); }
69     };
70
71     struct ParseDispatcher
72     {
73         ParseCommandInfo info_;
74         CommandParser::Callback cb_;
75
76         struct BindInfo {
77             BindInfo( ParseDispatcher & d, CommandParser::Callback cb)
78                 : dispatcher (d) { dispatcher.cb_ = cb; }
79             ~BindInfo() { dispatcher.cb_  = 0; }
80
81             ParseDispatcher & dispatcher;
82         };
83
84         void beginCommand(std::vector<std::string> & command)
85             { ParserAccess::init(info_);
86               ParserAccess::setCommand(info_, command); }
87
88         void endCommand()
89             { ParserAccess::finalize(info_); cb_(info_); }
90
91         void pushArgument(std::string const & argument)
92             { ParserAccess::startArgument(info_); 
93               ParserAccess::addToken(info_, ParserAccess::makeToken(argument)); 
94               ParserAccess::endArgument(info_); }
95
96         void openGroup()
97             { ParserAccess::startArgument(info_); }
98
99         void closeGroup()
100             { ParserAccess::endArgument(info_); }
101
102         void pushPunctuation(std::string const & token)
103             { ParserAccess::addToken(info_, ParserAccess::makeToken(token)); }
104
105         void pushWord(std::string const & token)
106             { ParserAccess::addToken(info_, ParserAccess::makeToken(token)); }
107
108         void builtin_cd(std::vector<std::string> & path)
109             { ParserAccess::init(info_);
110               ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinCD);
111               setBuiltinPathArg(path);
112               ParserAccess::finalize(info_); cb_(info_); }
113
114         void builtin_ls(std::vector<std::string> & path)
115             { ParserAccess::init(info_);
116               ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinLS);
117               setBuiltinPathArg(path);
118               ParserAccess::finalize(info_); cb_(info_); }
119
120         void pushDirectory(std::vector<std::string> & path)
121             { ParserAccess::init(info_);
122               ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinPUSHD);
123               setBuiltinPathArg(path);
124               ParserAccess::finalize(info_); cb_(info_); }
125
126         void popDirectory()
127             { ParserAccess::init(info_);
128               ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinPOPD);
129               ParserAccess::finalize(info_); cb_(info_); }
130         
131         void builtin_exit()
132             { ParserAccess::init(info_);
133               ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinEXIT);
134               ParserAccess::finalize(info_); cb_(info_); }
135
136         void builtin_help(std::vector<std::string> & path)
137             { ParserAccess::init(info_);
138               ParserAccess::setBuiltin(info_, ParseCommandInfo::BuiltinHELP);
139               setBuiltinPathArg(path);
140               ParserAccess::finalize(info_); cb_(info_); }
141
142         void setBuiltinPathArg(std::vector<std::string> & path)
143             {
144                 ParserAccess::startArgument(info_);
145                 for (std::vector<std::string>::const_iterator i (path.begin());
146                      i != path.end(); ++i)
147                     ParserAccess::addToken(info_, ParserAccess::makeToken(*i));
148                 ParserAccess::endArgument(info_);
149             }
150     };
151
152 }}}
153
154 ///////////////////////////////////////////////////////////////////////////
155 // senf::console::ParseCommandInfo
156
157 struct senf::console::ParseCommandInfo::MakeRange
158 {
159     typedef ParseCommandInfo::argument_value_type result_type;
160     
161     MakeRange() {}
162     MakeRange(ParseCommandInfo::token_iterator b) : b_ (b) {}
163     
164     senf::console::ParseCommandInfo::token_iterator b_;
165     
166     result_type operator()(TempArguments::iterator::value_type const & v) const {
167         return result_type( b_ + v.first, b_ + v.second );
168     }
169 };
170
171 prefix_ void senf::console::ParseCommandInfo::finalize()
172 {
173     arguments_.resize( tempArguments_.size() );
174
175     std::copy( boost::make_transform_iterator( tempArguments_.begin(), 
176                                                MakeRange(tokens_.begin()) ),
177                boost::make_transform_iterator( tempArguments_.end(), 
178                                                MakeRange() ),
179                arguments_.begin() );
180
181     tempArguments_.clear();
182 }
183
184 prefix_ std::ostream & senf::console::operator<<(std::ostream & stream,
185                                                  ParseCommandInfo const & info)
186 {
187     if (info.builtin() == ParseCommandInfo::NoBuiltin) 
188         stream << senf::stringJoin(info.commandPath(), "/");
189     else {
190         char const * builtins[] = { "", "cd", "ls", "pushd", "popd", "exit", "help" };
191         stream << "builtin-" << builtins[info.builtin()];
192     }
193         
194     ParseCommandInfo::ArgumentsRange args (info.arguments());
195     for (ParseCommandInfo::argument_iterator i (args.begin()); i != args.end(); ++i) {
196         ParseCommandInfo::token_iterator j (i->begin());
197         stream << " [";
198         if ( j != i->end() ) {
199             for (;;) {
200                 stream << "'" << j->value() << "'";
201                 if ( ++j != i->end() ) stream << ' ';
202                 else                   break;
203             }
204         }
205         stream << "]";
206     }
207
208     return stream;
209 }
210
211 ///////////////////////////////////////////////////////////////////////////
212 // senf::console::CommandParser
213
214 struct senf::console::CommandParser::Impl
215 {
216     typedef detail::CommandGrammar<detail::ParseDispatcher> Grammar;
217
218     detail::ParseDispatcher dispatcher;
219     Grammar::Context context;
220     Grammar grammar;
221
222     Impl() : dispatcher(), context(), grammar(dispatcher, context) {}
223 };
224
225 prefix_ senf::console::CommandParser::CommandParser()
226     : impl_ (new Impl())
227 {}
228
229 prefix_ senf::console::CommandParser::~CommandParser()
230 {}
231
232 prefix_ bool senf::console::CommandParser::parse(std::string command, Callback cb)
233 {
234     detail::ParseDispatcher::BindInfo bind (impl().dispatcher, cb);
235     return boost::spirit::parse( command.begin(), command.end(), 
236                                  impl().grammar.use_parser<Impl::Grammar::CommandParser>(),
237                                  impl().grammar.use_parser<Impl::Grammar::SkipParser>()
238         ).full;
239 }
240
241 prefix_ bool senf::console::CommandParser::parseFile(std::string filename, Callback cb)
242 {
243     detail::ParseDispatcher::BindInfo bind (impl().dispatcher, cb);
244     boost::spirit::file_iterator<> i (filename);
245     if (!i) throw SystemException(ENOENT SENF_EXC_DEBUGINFO);
246     boost::spirit::file_iterator<> const i_end (i.make_end());
247     
248     return boost::spirit::parse( i, i_end, 
249                                  impl().grammar.use_parser<Impl::Grammar::CommandParser>(),
250                                  impl().grammar.use_parser<Impl::Grammar::SkipParser>()
251         ).full;
252 }
253
254 ///////////////////////////////cc.e////////////////////////////////////////
255 #undef prefix_
256 //#include "Parse.mpp"
257
258 \f
259 // Local Variables:
260 // mode: c++
261 // fill-column: 100
262 // comment-column: 40
263 // c-file-style: "senf"
264 // indent-tabs-mode: nil
265 // ispell-local-dictionary: "american"
266 // compile-command: "scons -u test"
267 // End: