Console: Add builtin parsing
[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 "../Utils/String.hh"
31 #include <boost/iterator/transform_iterator.hpp>
32
33 //#include "Parse.mpp"
34 #define prefix_
35 ///////////////////////////////cc.p////////////////////////////////////////
36
37 namespace senf {
38 namespace console {
39 namespace detail {
40
41     struct ParserAccess
42     {
43         static void init(ParseCommandInfo & info)
44             { info.init(); }
45
46         static void setBuiltin(ParseCommandInfo & info, ParseCommandInfo::BuiltinCommand builtin)
47             { info.setBuiltin(builtin); }
48
49         static void setCommand(ParseCommandInfo & info, std::vector<std::string> & commandPath)
50             { info.setCommand(commandPath); }
51
52         static void startArgument(ParseCommandInfo & info)
53             { info.startArgument(); }
54
55         static void endArgument(ParseCommandInfo & info)
56             { info.endArgument(); }
57
58         static void addToken(ParseCommandInfo & info, ArgumentToken const & token)
59             { info.addToken(token); }
60
61         static void finalize(ParseCommandInfo & info)
62             { info.finalize(); }
63
64         static ArgumentToken makeToken(std::string const & token)
65             { return ArgumentToken(token); }
66     };
67
68     struct ParseDispatcher
69     {
70         ParseDispatcher()
71             : info_ (0) {}
72         
73         ParseCommandInfo * info_;
74
75         ParseCommandInfo & info() {
76             SENF_ASSERT( info_ );
77             return *info_;
78         }
79
80         struct BindInfo {
81             BindInfo( ParseDispatcher & d, ParseCommandInfo & i)
82                 : dispatcher (d) { dispatcher.info_ = &i; }
83
84             ~BindInfo() { dispatcher.info_  = 0; }
85
86             ParseDispatcher & dispatcher;
87         };
88
89         void beginCommand(std::vector<std::string> & command)
90             { ParserAccess::init(info());
91               ParserAccess::setCommand(info(), command); }
92
93         void endCommand()
94             { ParserAccess::finalize(info()); }
95
96         void pushArgument(std::string const & argument)
97             { ParserAccess::startArgument(info()); 
98               ParserAccess::addToken(info(), ParserAccess::makeToken(argument)); 
99               ParserAccess::endArgument(info()); }
100
101         void openGroup()
102             { ParserAccess::startArgument(info()); }
103
104         void closeGroup()
105             { ParserAccess::endArgument(info()); }
106
107         void pushPunctuation(std::string const & token)
108             { ParserAccess::addToken(info(), ParserAccess::makeToken(token)); }
109
110         void pushWord(std::string const & token)
111             { ParserAccess::addToken(info(), ParserAccess::makeToken(token)); }
112
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()); }
118
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()); }
124
125         void setBuiltinPathArg(std::vector<std::string> & path)
126             {
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());
132             }
133     };
134
135 }}}
136
137 ///////////////////////////////////////////////////////////////////////////
138 // senf::console::ParseCommandInfo
139
140 struct senf::console::ParseCommandInfo::MakeRange
141 {
142     MakeRange() {}
143     MakeRange(ParseCommandInfo::token_iterator b) : b_ (b) {}
144
145     senf::console::ParseCommandInfo::token_iterator b_;
146
147     typedef ParseCommandInfo::argument_value_type result_type;
148         
149     result_type operator()(TempArguments::iterator::value_type const & v) const {
150         return result_type( b_ + v.first, b_ + v.second );
151     }
152 };
153
154 prefix_ void senf::console::ParseCommandInfo::finalize()
155 {
156     arguments_.resize( tempArguments_.size() );
157
158     std::copy( boost::make_transform_iterator( tempArguments_.begin(), 
159                                                MakeRange(tokens_.begin()) ),
160                boost::make_transform_iterator( tempArguments_.end(), 
161                                                MakeRange() ),
162                arguments_.begin() );
163
164     tempArguments_.clear();
165 }
166
167 prefix_ std::ostream & senf::console::operator<<(std::ostream & stream,
168                                                  ParseCommandInfo const & info)
169 {
170     if (info.builtin() == ParseCommandInfo::NoBuiltin) 
171         stream << senf::stringJoin(info.commandPath(), "/");
172     else {
173         char const * builtins[] = { "", "cd", "ls" };
174         stream << "builtin-" << builtins[info.builtin()];
175     }
176         
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());
180         stream << " [";
181         if ( j != i->end() ) {
182             for (;;) {
183                 stream << "'" << j->value() << "'";
184                 if ( ++j != i->end() ) stream << ' ';
185                 else                   break;
186             }
187         }
188         stream << "]";
189     }
190
191     return stream;
192 }
193
194 ///////////////////////////////////////////////////////////////////////////
195 // senf::console::SingleCommandParser
196
197 struct senf::console::SingleCommandParser::Impl
198 {
199     typedef detail::CommandGrammar<detail::ParseDispatcher> Grammar;
200
201     detail::ParseDispatcher dispatcher;
202     Grammar::Context context;
203     Grammar grammar;
204
205     Impl() : dispatcher(), context(), grammar(dispatcher, context) {}
206 };
207
208 prefix_ senf::console::SingleCommandParser::SingleCommandParser()
209     : impl_ (new Impl())
210 {}
211
212 prefix_ senf::console::SingleCommandParser::~SingleCommandParser()
213 {}
214
215 prefix_ bool senf::console::SingleCommandParser::parseCommand(std::string command,
216                                                               ParseCommandInfo & info)
217 {
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>()
222         ).full;
223 }
224
225 ///////////////////////////////cc.e////////////////////////////////////////
226 #undef prefix_
227 //#include "Parse.mpp"
228
229 \f
230 // Local Variables:
231 // mode: c++
232 // fill-column: 100
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"
238 // End: