switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / Console / Parse.cci
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief Parse inline non-template implementation */
30
31 // We do NOT want to include the complete parser definition into every other compilation unit
32 // (disabled) #include "Parse.ih"
33
34 // Custom includes
35 #include <senf/Utils/senfassert.hh>
36
37 #define prefix_ inline
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41 // senf::console::Token
42
43 prefix_ std::string const & senf::console::Token::value()
44     const
45 {
46     return token_;
47 }
48
49 prefix_ senf::console::Token::TokenType senf::console::Token::type()
50     const
51 {
52     return type_;
53 }
54
55 prefix_ unsigned senf::console::Token::line()
56     const
57 {
58     return line_;
59 }
60
61 prefix_ unsigned senf::console::Token::column()
62     const
63 {
64     return column_;
65 }
66
67 prefix_ unsigned senf::console::Token::index()
68     const
69 {
70     return index_;
71 }
72
73 prefix_ bool senf::console::Token::is(unsigned tokens)
74     const
75 {
76     return tokens & type_;
77 }
78
79 prefix_ bool senf::console::Token::operator==(Token const & other)
80     const
81 {
82     return type() == other.type() && value() == other.value();
83 }
84
85 prefix_ bool senf::console::Token::operator!=(Token const & other)
86     const
87 {
88     return ! operator==(other);
89 }
90
91 prefix_ senf::console::Token::Token()
92     : type_ (None), token_ ()
93 {}
94
95 prefix_ senf::console::Token::Token(TokenType type, std::string token)
96     : type_ (type), token_ (token), line_ (0), column_ (0), index_ (0)
97 {}
98
99 prefix_ senf::console::Token senf::console::NoneToken()
100 {
101     return Token(Token::None,"");
102 }
103
104 prefix_ senf::console::Token senf::console::PathSeparatorToken()
105 {
106     return Token(Token::PathSeparator,"/");
107 }
108
109 prefix_ senf::console::Token senf::console::ArgumentGroupOpenToken()
110 {
111     return Token(Token::ArgumentGroupOpen,"(");
112 }
113
114 prefix_ senf::console::Token senf::console::ArgumentGroupCloseToken()
115 {
116     return Token(Token::ArgumentGroupClose,")");
117 }
118
119 prefix_ senf::console::Token senf::console::DirectoryGroupOpenToken()
120 {
121     return Token(Token::DirectoryGroupOpen,"{");
122 }
123
124 prefix_ senf::console::Token senf::console::DirectoryGroupCloseToken()
125 {
126     return Token(Token::DirectoryGroupClose,"}");
127 }
128
129 prefix_ senf::console::Token senf::console::CommandTerminatorToken()
130 {
131     return Token(Token::CommandTerminator,";");
132 }
133
134 prefix_ senf::console::Token senf::console::OtherPunctuationToken(std::string const & value)
135 {
136     return Token(Token::OtherPunctuation, value);
137 }
138
139 prefix_ senf::console::Token senf::console::BasicStringToken(std::string const & value)
140 {
141     return Token(Token::BasicString, value);
142 }
143
144 prefix_ senf::console::Token senf::console::HexStringToken(std::string const & value)
145 {
146     return Token(Token::HexString, value);
147 }
148
149 prefix_ senf::console::Token senf::console::WordToken(std::string const & value)
150 {
151     return Token(Token::Word, value);
152 }
153
154 //-/////////////////////////////////////////////////////////////////////////////////////////////////
155 // senf::console::ParseCommandInfo
156
157 prefix_ senf::console::ParseCommandInfo::ParseCommandInfo()
158     : builtin_ (NoBuiltin)
159 {}
160
161 prefix_ senf::console::ParseCommandInfo::BuiltinCommand
162 senf::console::ParseCommandInfo::builtin()
163     const
164 {
165     return builtin_;
166 }
167
168 prefix_ senf::console::ParseCommandInfo::TokensRange
169 senf::console::ParseCommandInfo::commandPath()
170     const
171 {
172     return boost::make_iterator_range(commandPath_.begin(), commandPath_.end());
173 }
174
175 prefix_ senf::console::ParseCommandInfo::ArgumentsRange
176 senf::console::ParseCommandInfo::arguments()
177     const
178 {
179     return boost::make_iterator_range( ArgumentIterator(tokens_.begin()),
180                                        ArgumentIterator(tokens_.end()) );
181 }
182
183 prefix_ senf::console::ParseCommandInfo::TokensRange senf::console::ParseCommandInfo::tokens()
184     const
185 {
186     return boost::make_iterator_range(tokens_.begin(), tokens_.end());
187 }
188
189 prefix_ void senf::console::ParseCommandInfo::clear()
190 {
191     builtin_ = NoBuiltin;
192     commandPath_.clear();
193     tokens_.clear();
194 }
195
196 prefix_ bool senf::console::ParseCommandInfo::empty()
197 {
198     return builtin_ == NoBuiltin && commandPath_.empty();
199 }
200
201 prefix_ void senf::console::ParseCommandInfo::builtin(BuiltinCommand builtin)
202 {
203     builtin_ = builtin;
204 }
205
206 prefix_ void
207 senf::console::ParseCommandInfo::command(std::vector<Token> & commandPath)
208 {
209     commandPath_.clear();
210     commandPath_.swap(commandPath);
211 }
212
213 prefix_ void senf::console::ParseCommandInfo::addToken(Token const & token)
214 {
215     tokens_.push_back(token);
216 }
217
218 //-/////////////////////////////////////////////////////////////////////////////////////////////////
219 // senf::console::ParseCommandInfo::ArgumentIterator
220
221 prefix_ senf::console::ParseCommandInfo::ArgumentIterator::ArgumentIterator()
222 {}
223
224 prefix_ senf::console::ParseCommandInfo::ArgumentIterator::
225 ArgumentIterator(ParseCommandInfo::TokensRange::iterator i)
226     : b_(i), e_(i)
227 {}
228
229 prefix_ senf::console::ParseCommandInfo::ArgumentIterator::reference
230 senf::console::ParseCommandInfo::ArgumentIterator::dereference()
231     const
232 {
233     if (b_ == e_) setRange();
234     return b_->is(Token::ArgumentGroupOpen)
235         ? boost::make_iterator_range(boost::next(b_), boost::prior(e_))
236         : boost::make_iterator_range(b_, e_);
237 }
238
239 prefix_ bool
240 senf::console::ParseCommandInfo::ArgumentIterator::equal(ArgumentIterator const & other)
241     const
242 {
243     return b_ == other.b_;
244 }
245
246 prefix_ void senf::console::ParseCommandInfo::ArgumentIterator::increment()
247 {
248     if (b_ == e_) setRange();
249     b_ = e_;
250 }
251
252 //-/////////////////////////////////////////////////////////////////////////////////////////////////
253
254 prefix_ senf::console::CheckedArgumentIteratorWrapper::
255 CheckedArgumentIteratorWrapper(ParseCommandInfo::ArgumentsRange const & range,
256                                std::string const & msg)
257     : i_ (range.begin()), e_ (range.end()), msg_ (msg)
258 {}
259
260 prefix_ senf::console::CheckedArgumentIteratorWrapper::
261 CheckedArgumentIteratorWrapper(ParseCommandInfo::TokensRange const & range,
262                                std::string const & msg)
263     : i_ (range.begin()), e_ (range.end()), msg_ (msg)
264 {}
265
266 prefix_ senf::console::CheckedArgumentIteratorWrapper::~CheckedArgumentIteratorWrapper()
267 {
268     if (i_ != e_ && ! std::uncaught_exception())
269         throw SyntaxErrorException(msg_);
270 }
271
272 prefix_ senf::console::CheckedArgumentIteratorWrapper::operator ParseCommandInfo::ArgumentIterator()
273 {
274     return i_;
275 }
276
277 prefix_ bool senf::console::CheckedArgumentIteratorWrapper::boolean_test()
278     const
279 {
280     return i_ != e_;
281 }
282
283 prefix_ bool senf::console::CheckedArgumentIteratorWrapper::done()
284     const
285 {
286     return i_ == e_;
287 }
288
289 prefix_ void senf::console::CheckedArgumentIteratorWrapper::clear()
290 {
291     i_ = e_;
292 }
293
294 prefix_ senf::console::CheckedArgumentIteratorWrapper::reference
295 senf::console::CheckedArgumentIteratorWrapper::dereference()
296     const
297 {
298     if (i_ == e_)
299         throw SyntaxErrorException(msg_);
300     return *i_;
301 }
302
303 prefix_ void senf::console::CheckedArgumentIteratorWrapper::increment()
304 {
305     if (i_ == e_)
306         throw SyntaxErrorException(msg_);
307     ++ i_;
308 }
309
310 prefix_ bool senf::console::CheckedArgumentIteratorWrapper::
311 operator==(ParseCommandInfo::ArgumentIterator const & other)
312     const
313 {
314     return i_ == other;
315 }
316
317 prefix_ bool senf::console::CheckedArgumentIteratorWrapper::
318 operator!=(ParseCommandInfo::ArgumentIterator const & other)
319     const
320 {
321     return i_ != other;
322 }
323
324 prefix_ senf::console::ParseCommandInfo::ArgumentIterator
325 senf::console::CheckedArgumentIteratorWrapper::operator++(int)
326 {
327     ParseCommandInfo::ArgumentIterator i (i_);
328     increment();
329     return i;
330 }
331
332 //-/////////////////////////////////////////////////////////////////////////////////////////////////
333 // senf::console::SingleCommandParser
334
335 prefix_ senf::console::CommandParser::Impl & senf::console::CommandParser::impl()
336 {
337     SENF_ASSERT(impl_, "Ineral error: PIMPL pointer NULL ??");
338     return *impl_;
339 }
340
341 //-/////////////////////////////////////////////////////////////////////////////////////////////////
342 #undef prefix_
343
344 \f
345 // Local Variables:
346 // mode: c++
347 // fill-column: 100
348 // comment-column: 40
349 // c-file-style: "senf"
350 // indent-tabs-mode: nil
351 // ispell-local-dictionary: "american"
352 // compile-command: "scons -u test"
353 // End: