Console: Parser cleanup
[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 ///////////////////////////////////////////////////////////////////////////
77 // senf::console::ParseCommandInfo
78
79 prefix_ senf::console::ParseCommandInfo::BuiltinCommand
80 senf::console::ParseCommandInfo::builtin()
81     const
82 {
83     return builtin_;
84 }
85
86 prefix_ senf::console::ParseCommandInfo::TokensRange
87 senf::console::ParseCommandInfo::commandPath()
88     const
89 {
90     return boost::make_iterator_range(commandPath_.begin(), commandPath_.end());
91 }
92
93 prefix_ senf::console::ParseCommandInfo::ArgumentsRange
94 senf::console::ParseCommandInfo::arguments()
95     const
96 {
97     return boost::make_iterator_range( ArgumentIterator(tokens_.begin()),
98                                        ArgumentIterator(tokens_.end()) );
99 }
100
101 prefix_ senf::console::ParseCommandInfo::TokensRange senf::console::ParseCommandInfo::tokens()
102     const
103 {
104     return boost::make_iterator_range(tokens_.begin(), tokens_.end());
105 }
106
107 ////////////////////////////////////////
108 // private members
109
110 prefix_ void senf::console::ParseCommandInfo::init()
111 {
112     builtin_ = NoBuiltin;
113     commandPath_.clear();
114     tokens_.clear();
115 }
116
117 prefix_ void senf::console::ParseCommandInfo::setBuiltin(BuiltinCommand builtin)
118 {
119     builtin_ = builtin;
120 }
121
122 prefix_ void
123 senf::console::ParseCommandInfo::setCommand(std::vector<Token> & commandPath)
124 {
125     commandPath_.clear();
126     commandPath_.swap(commandPath);
127 }
128
129 prefix_ void senf::console::ParseCommandInfo::addToken(Token const & token)
130 {
131     tokens_.push_back(token);
132 }
133
134 ///////////////////////////////////////////////////////////////////////////
135 // senf::console::ParseCommandInfo::ArgumentIterator
136
137 prefix_ senf::console::ParseCommandInfo::ArgumentIterator::ArgumentIterator()
138 {}
139
140 prefix_ senf::console::ParseCommandInfo::ArgumentIterator::
141 ArgumentIterator(ParseCommandInfo::TokensRange::iterator i)
142     : b_(i), e_(i)
143 {}
144
145 prefix_ senf::console::ParseCommandInfo::ArgumentIterator::reference
146 senf::console::ParseCommandInfo::ArgumentIterator::dereference()
147     const
148 {
149     if (b_ == e_) setRange();
150     return b_->is(Token::ArgumentGroupOpen) 
151         ? boost::make_iterator_range(boost::next(b_), boost::prior(e_))
152         : boost::make_iterator_range(b_, e_);
153 }
154
155 prefix_ bool
156 senf::console::ParseCommandInfo::ArgumentIterator::equal(ArgumentIterator const & other)
157     const
158 {
159     return b_ == other.b_;
160 }
161
162 prefix_ void senf::console::ParseCommandInfo::ArgumentIterator::increment()
163 {
164     if (b_ == e_) setRange();
165     b_ = e_;
166 }
167
168 ///////////////////////////////////////////////////////////////////////////
169
170 prefix_ senf::console::CheckedArgumentIteratorWrapper::
171 CheckedArgumentIteratorWrapper(ParseCommandInfo::ArgumentsRange const & range,
172                                std::string const & msg)
173     : i_ (range.begin()), e_ (range.end()), msg_ (msg)
174 {}
175
176 prefix_ senf::console::CheckedArgumentIteratorWrapper::
177 CheckedArgumentIteratorWrapper(ParseCommandInfo::TokensRange const & range,
178                                std::string const & msg)
179     : i_ (range.begin()), e_ (range.end()), msg_ (msg)
180 {}
181
182 prefix_ senf::console::CheckedArgumentIteratorWrapper::~CheckedArgumentIteratorWrapper()
183 {
184     if (i_ != e_ && ! std::uncaught_exception())
185         throw SyntaxErrorException(msg_);
186 }
187
188 prefix_ senf::console::CheckedArgumentIteratorWrapper::operator ParseCommandInfo::ArgumentIterator()
189 {
190     return i_;
191 }
192
193 prefix_ bool senf::console::CheckedArgumentIteratorWrapper::boolean_test()
194     const
195 {
196     return i_ != e_;
197 }
198
199 prefix_ bool senf::console::CheckedArgumentIteratorWrapper::done()
200     const
201 {
202     return i_ == e_;
203 }
204
205 prefix_ void senf::console::CheckedArgumentIteratorWrapper::clear()
206 {
207     i_ = e_;
208 }
209
210 prefix_ senf::console::CheckedArgumentIteratorWrapper::reference
211 senf::console::CheckedArgumentIteratorWrapper::dereference()
212     const
213 {
214     if (i_ == e_)
215         throw SyntaxErrorException(msg_);
216     return *i_;
217 }
218
219 prefix_ void senf::console::CheckedArgumentIteratorWrapper::increment()
220 {
221     if (i_ == e_)
222         throw SyntaxErrorException(msg_);
223     ++ i_;
224 }
225
226 prefix_ bool senf::console::CheckedArgumentIteratorWrapper::
227 operator==(ParseCommandInfo::ArgumentIterator const & other)
228     const
229 {
230     return i_ == other;
231 }
232
233 prefix_ bool senf::console::CheckedArgumentIteratorWrapper::
234 operator!=(ParseCommandInfo::ArgumentIterator const & other)
235     const
236 {
237     return i_ != other;
238 }
239
240 prefix_ senf::console::ParseCommandInfo::ArgumentIterator
241 senf::console::CheckedArgumentIteratorWrapper::operator++(int)
242 {
243     ParseCommandInfo::ArgumentIterator i (i_);
244     increment();
245     return i;
246 }
247
248 ///////////////////////////////////////////////////////////////////////////
249 // senf::console::SingleCommandParser
250
251 prefix_ senf::console::CommandParser::Impl & senf::console::CommandParser::impl()
252 {
253     SENF_ASSERT(impl_);
254     return *impl_;
255 }
256
257 ///////////////////////////////cci.e///////////////////////////////////////
258 #undef prefix_
259
260 \f
261 // Local Variables:
262 // mode: c++
263 // fill-column: 100
264 // comment-column: 40
265 // c-file-style: "senf"
266 // indent-tabs-mode: nil
267 // ispell-local-dictionary: "american"
268 // compile-command: "scons -u test"
269 // End: