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