fcce862998de188b0e7d2c085544ee75eaa6732b
[senf.git] / 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_ void senf::console::ParseCommandInfo::builtin(BuiltinCommand builtin)
174 {
175     builtin_ = builtin;
176     commandPath_.clear();
177 }
178
179 prefix_ void
180 senf::console::ParseCommandInfo::command(std::vector<Token> & commandPath)
181 {
182     commandPath_.clear();
183     commandPath_.swap(commandPath);
184     builtin_ = NoBuiltin;
185 }
186
187 prefix_ void senf::console::ParseCommandInfo::addToken(Token const & token)
188 {
189     tokens_.push_back(token);
190 }
191
192 ///////////////////////////////////////////////////////////////////////////
193 // senf::console::ParseCommandInfo::ArgumentIterator
194
195 prefix_ senf::console::ParseCommandInfo::ArgumentIterator::ArgumentIterator()
196 {}
197
198 prefix_ senf::console::ParseCommandInfo::ArgumentIterator::
199 ArgumentIterator(ParseCommandInfo::TokensRange::iterator i)
200     : b_(i), e_(i)
201 {}
202
203 prefix_ senf::console::ParseCommandInfo::ArgumentIterator::reference
204 senf::console::ParseCommandInfo::ArgumentIterator::dereference()
205     const
206 {
207     if (b_ == e_) setRange();
208     return b_->is(Token::ArgumentGroupOpen) 
209         ? boost::make_iterator_range(boost::next(b_), boost::prior(e_))
210         : boost::make_iterator_range(b_, e_);
211 }
212
213 prefix_ bool
214 senf::console::ParseCommandInfo::ArgumentIterator::equal(ArgumentIterator const & other)
215     const
216 {
217     return b_ == other.b_;
218 }
219
220 prefix_ void senf::console::ParseCommandInfo::ArgumentIterator::increment()
221 {
222     if (b_ == e_) setRange();
223     b_ = e_;
224 }
225
226 ///////////////////////////////////////////////////////////////////////////
227 // senf::console::SyntaxErrorException
228
229 prefix_ senf::console::SyntaxErrorException::SyntaxErrorException(std::string const & msg)
230     : message_(msg)
231 {}
232
233 prefix_ senf::console::SyntaxErrorException::~SyntaxErrorException()
234     throw()
235 {}
236
237 prefix_ std::string const & senf::console::SyntaxErrorException::message()
238     const
239 {
240     return message_;
241 }
242
243 ///////////////////////////////////////////////////////////////////////////
244
245 prefix_ senf::console::CheckedArgumentIteratorWrapper::
246 CheckedArgumentIteratorWrapper(ParseCommandInfo::ArgumentsRange const & range,
247                                std::string const & msg)
248     : i_ (range.begin()), e_ (range.end()), msg_ (msg)
249 {}
250
251 prefix_ senf::console::CheckedArgumentIteratorWrapper::
252 CheckedArgumentIteratorWrapper(ParseCommandInfo::TokensRange const & range,
253                                std::string const & msg)
254     : i_ (range.begin()), e_ (range.end()), msg_ (msg)
255 {}
256
257 prefix_ senf::console::CheckedArgumentIteratorWrapper::~CheckedArgumentIteratorWrapper()
258 {
259     if (i_ != e_ && ! std::uncaught_exception())
260         throw SyntaxErrorException(msg_);
261 }
262
263 prefix_ senf::console::CheckedArgumentIteratorWrapper::operator ParseCommandInfo::ArgumentIterator()
264 {
265     return i_;
266 }
267
268 prefix_ bool senf::console::CheckedArgumentIteratorWrapper::boolean_test()
269     const
270 {
271     return i_ != e_;
272 }
273
274 prefix_ bool senf::console::CheckedArgumentIteratorWrapper::done()
275     const
276 {
277     return i_ == e_;
278 }
279
280 prefix_ void senf::console::CheckedArgumentIteratorWrapper::clear()
281 {
282     i_ = e_;
283 }
284
285 prefix_ senf::console::CheckedArgumentIteratorWrapper::reference
286 senf::console::CheckedArgumentIteratorWrapper::dereference()
287     const
288 {
289     if (i_ == e_)
290         throw SyntaxErrorException(msg_);
291     return *i_;
292 }
293
294 prefix_ void senf::console::CheckedArgumentIteratorWrapper::increment()
295 {
296     if (i_ == e_)
297         throw SyntaxErrorException(msg_);
298     ++ i_;
299 }
300
301 prefix_ bool senf::console::CheckedArgumentIteratorWrapper::
302 operator==(ParseCommandInfo::ArgumentIterator const & other)
303     const
304 {
305     return i_ == other;
306 }
307
308 prefix_ bool senf::console::CheckedArgumentIteratorWrapper::
309 operator!=(ParseCommandInfo::ArgumentIterator const & other)
310     const
311 {
312     return i_ != other;
313 }
314
315 prefix_ senf::console::ParseCommandInfo::ArgumentIterator
316 senf::console::CheckedArgumentIteratorWrapper::operator++(int)
317 {
318     ParseCommandInfo::ArgumentIterator i (i_);
319     increment();
320     return i;
321 }
322
323 ///////////////////////////////////////////////////////////////////////////
324 // senf::console::SingleCommandParser
325
326 prefix_ senf::console::CommandParser::Impl & senf::console::CommandParser::impl()
327 {
328     SENF_ASSERT(impl_);
329     return *impl_;
330 }
331
332 ///////////////////////////////cci.e///////////////////////////////////////
333 #undef prefix_
334
335 \f
336 // Local Variables:
337 // mode: c++
338 // fill-column: 100
339 // comment-column: 40
340 // c-file-style: "senf"
341 // indent-tabs-mode: nil
342 // ispell-local-dictionary: "american"
343 // compile-command: "scons -u test"
344 // End: