Utils/Console: Implement command node return value support
[senf.git] / Utils / Console / Parse.cci
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 inline non-template implementation */
25
26 // We do NOT want to include the complete parser definition into every other compilation unit
27 // (disabled) #include "Parse.ih"
28
29 // Custom includes
30 #include "../../Utils/senfassert.hh"
31
32 #define prefix_ inline
33 ///////////////////////////////cci.p///////////////////////////////////////
34
35 ///////////////////////////////////////////////////////////////////////////
36 // senf::console::Token
37
38 prefix_ std::string const & senf::console::Token::value()
39     const
40 {
41     return token_;
42 }
43
44 prefix_ senf::console::Token::TokenType senf::console::Token::type()
45     const
46 {
47     return type_;
48 }
49
50 prefix_ bool senf::console::Token::is(unsigned tokens)
51     const
52 {
53     return tokens & type_;
54 }
55
56 prefix_ bool senf::console::Token::operator==(Token const & other)
57     const
58 {
59     return type() == other.type() && value() == other.value();
60 }
61
62 prefix_ bool senf::console::Token::operator!=(Token const & other)
63     const
64 {
65     return ! operator==(other);
66 }
67
68 prefix_ senf::console::Token::Token()
69     : type_(None), token_()
70 {}
71
72 prefix_ senf::console::Token::Token(TokenType type, std::string token)
73     : type_(type), token_ (token)
74 {}
75
76 prefix_ senf::console::Token senf::console::NoneToken()
77 {
78     return Token(Token::None,"");
79 }
80
81 prefix_ senf::console::Token senf::console::PathSeparatorToken()
82 {
83     return Token(Token::PathSeparator,"/");
84 }
85
86 prefix_ senf::console::Token senf::console::ArgumentGroupOpenToken()
87 {
88     return Token(Token::ArgumentGroupOpen,"(");
89 }
90
91 prefix_ senf::console::Token senf::console::ArgumentGroupCloseToken()
92 {
93     return Token(Token::ArgumentGroupClose,")");
94 }
95
96 prefix_ senf::console::Token senf::console::DirectoryGroupOpenToken()
97 {
98     return Token(Token::DirectoryGroupOpen,"{");
99 }
100
101 prefix_ senf::console::Token senf::console::DirectoryGroupCloseToken()
102 {
103     return Token(Token::DirectoryGroupClose,"}");
104 }
105
106 prefix_ senf::console::Token senf::console::CommandTerminatorToken()
107 {
108     return Token(Token::CommandTerminator,";");
109 }
110
111 prefix_ senf::console::Token senf::console::OtherPunctuationToken(std::string const & value)
112 {
113     return Token(Token::OtherPunctuation, value);
114 }
115
116 prefix_ senf::console::Token senf::console::BasicStringToken(std::string const & value)
117 {
118     return Token(Token::BasicString, value);
119 }
120
121 prefix_ senf::console::Token senf::console::HexStringToken(std::string const & value)
122 {
123     return Token(Token::HexString, value);
124 }
125
126 prefix_ senf::console::Token senf::console::WordToken(std::string const & value)
127 {
128     return Token(Token::Word, value);
129 }
130
131 ///////////////////////////////////////////////////////////////////////////
132 // senf::console::ParseCommandInfo
133
134 prefix_ senf::console::ParseCommandInfo::ParseCommandInfo()
135     : builtin_ (NoBuiltin)
136 {}
137
138 prefix_ senf::console::ParseCommandInfo::BuiltinCommand
139 senf::console::ParseCommandInfo::builtin()
140     const
141 {
142     return builtin_;
143 }
144
145 prefix_ senf::console::ParseCommandInfo::TokensRange
146 senf::console::ParseCommandInfo::commandPath()
147     const
148 {
149     return boost::make_iterator_range(commandPath_.begin(), commandPath_.end());
150 }
151
152 prefix_ senf::console::ParseCommandInfo::ArgumentsRange
153 senf::console::ParseCommandInfo::arguments()
154     const
155 {
156     return boost::make_iterator_range( ArgumentIterator(tokens_.begin()),
157                                        ArgumentIterator(tokens_.end()) );
158 }
159
160 prefix_ senf::console::ParseCommandInfo::TokensRange senf::console::ParseCommandInfo::tokens()
161     const
162 {
163     return boost::make_iterator_range(tokens_.begin(), tokens_.end());
164 }
165
166 prefix_ void senf::console::ParseCommandInfo::clear()
167 {
168     builtin_ = NoBuiltin;
169     commandPath_.clear();
170     tokens_.clear();
171 }
172
173 prefix_ bool senf::console::ParseCommandInfo::empty()
174 {
175     return builtin_ == NoBuiltin && commandPath_.empty();
176 }
177
178 prefix_ void senf::console::ParseCommandInfo::builtin(BuiltinCommand builtin)
179 {
180     builtin_ = builtin;
181 }
182
183 prefix_ void
184 senf::console::ParseCommandInfo::command(std::vector<Token> & commandPath)
185 {
186     commandPath_.clear();
187     commandPath_.swap(commandPath);
188 }
189
190 prefix_ void senf::console::ParseCommandInfo::addToken(Token const & token)
191 {
192     tokens_.push_back(token);
193 }
194
195 ///////////////////////////////////////////////////////////////////////////
196 // senf::console::ParseCommandInfo::ArgumentIterator
197
198 prefix_ senf::console::ParseCommandInfo::ArgumentIterator::ArgumentIterator()
199 {}
200
201 prefix_ senf::console::ParseCommandInfo::ArgumentIterator::
202 ArgumentIterator(ParseCommandInfo::TokensRange::iterator i)
203     : b_(i), e_(i)
204 {}
205
206 prefix_ senf::console::ParseCommandInfo::ArgumentIterator::reference
207 senf::console::ParseCommandInfo::ArgumentIterator::dereference()
208     const
209 {
210     if (b_ == e_) setRange();
211     return b_->is(Token::ArgumentGroupOpen) 
212         ? boost::make_iterator_range(boost::next(b_), boost::prior(e_))
213         : boost::make_iterator_range(b_, e_);
214 }
215
216 prefix_ bool
217 senf::console::ParseCommandInfo::ArgumentIterator::equal(ArgumentIterator const & other)
218     const
219 {
220     return b_ == other.b_;
221 }
222
223 prefix_ void senf::console::ParseCommandInfo::ArgumentIterator::increment()
224 {
225     if (b_ == e_) setRange();
226     b_ = e_;
227 }
228
229 ///////////////////////////////////////////////////////////////////////////
230
231 prefix_ senf::console::CheckedArgumentIteratorWrapper::
232 CheckedArgumentIteratorWrapper(ParseCommandInfo::ArgumentsRange const & range,
233                                std::string const & msg)
234     : i_ (range.begin()), e_ (range.end()), msg_ (msg)
235 {}
236
237 prefix_ senf::console::CheckedArgumentIteratorWrapper::
238 CheckedArgumentIteratorWrapper(ParseCommandInfo::TokensRange const & range,
239                                std::string const & msg)
240     : i_ (range.begin()), e_ (range.end()), msg_ (msg)
241 {}
242
243 prefix_ senf::console::CheckedArgumentIteratorWrapper::~CheckedArgumentIteratorWrapper()
244 {
245     if (i_ != e_ && ! std::uncaught_exception())
246         throw SyntaxErrorException(msg_);
247 }
248
249 prefix_ senf::console::CheckedArgumentIteratorWrapper::operator ParseCommandInfo::ArgumentIterator()
250 {
251     return i_;
252 }
253
254 prefix_ bool senf::console::CheckedArgumentIteratorWrapper::boolean_test()
255     const
256 {
257     return i_ != e_;
258 }
259
260 prefix_ bool senf::console::CheckedArgumentIteratorWrapper::done()
261     const
262 {
263     return i_ == e_;
264 }
265
266 prefix_ void senf::console::CheckedArgumentIteratorWrapper::clear()
267 {
268     i_ = e_;
269 }
270
271 prefix_ senf::console::CheckedArgumentIteratorWrapper::reference
272 senf::console::CheckedArgumentIteratorWrapper::dereference()
273     const
274 {
275     if (i_ == e_)
276         throw SyntaxErrorException(msg_);
277     return *i_;
278 }
279
280 prefix_ void senf::console::CheckedArgumentIteratorWrapper::increment()
281 {
282     if (i_ == e_)
283         throw SyntaxErrorException(msg_);
284     ++ i_;
285 }
286
287 prefix_ bool senf::console::CheckedArgumentIteratorWrapper::
288 operator==(ParseCommandInfo::ArgumentIterator const & other)
289     const
290 {
291     return i_ == other;
292 }
293
294 prefix_ bool senf::console::CheckedArgumentIteratorWrapper::
295 operator!=(ParseCommandInfo::ArgumentIterator const & other)
296     const
297 {
298     return i_ != other;
299 }
300
301 prefix_ senf::console::ParseCommandInfo::ArgumentIterator
302 senf::console::CheckedArgumentIteratorWrapper::operator++(int)
303 {
304     ParseCommandInfo::ArgumentIterator i (i_);
305     increment();
306     return i;
307 }
308
309 ///////////////////////////////////////////////////////////////////////////
310 // senf::console::SingleCommandParser
311
312 prefix_ senf::console::CommandParser::Impl & senf::console::CommandParser::impl()
313 {
314     SENF_ASSERT(impl_);
315     return *impl_;
316 }
317
318 ///////////////////////////////cci.e///////////////////////////////////////
319 #undef prefix_
320
321 \f
322 // Local Variables:
323 // mode: c++
324 // fill-column: 100
325 // comment-column: 40
326 // c-file-style: "senf"
327 // indent-tabs-mode: nil
328 // ispell-local-dictionary: "american"
329 // compile-command: "scons -u test"
330 // End: