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