Socket: Fix reuseaddr / bind ordering
[senf.git] / Console / Parse.cc
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 non-inline non-template implementation */
25
26 #include "Parse.hh"
27 #include "Parse.ih"
28
29 // Custom includes
30 #include <boost/iterator/transform_iterator.hpp>
31
32 //#include "Parse.mpp"
33 #define prefix_
34 ///////////////////////////////cc.p////////////////////////////////////////
35
36 namespace senf {
37 namespace console {
38 namespace detail {
39
40     struct ParserAccess
41     {
42         static void init(ParseCommandInfo & info)
43             { info.init(); }
44
45         static void setCommand(ParseCommandInfo & info, std::string const & commandPath)
46             { info.setCommand(commandPath); }
47
48         static void startArgument(ParseCommandInfo & info)
49             { info.startArgument(); }
50
51         static void endArgument(ParseCommandInfo & info)
52             { info.endArgument(); }
53
54         static void addToken(ParseCommandInfo & info, ArgumentToken const & token)
55             { info.addToken(token); }
56
57         static void finalize(ParseCommandInfo & info)
58             { info.finalize(); }
59
60         static ArgumentToken makeToken(std::string const & token)
61             { return ArgumentToken(token); }
62     };
63
64     struct ParseDispatcher
65     {
66         ParseDispatcher()
67             : info_ (0) {}
68         
69         ParseCommandInfo * info_;
70
71         ParseCommandInfo & info() {
72             SENF_ASSERT( info_ );
73             return *info_;
74         }
75
76         struct BindInfo {
77             BindInfo( ParseDispatcher & d, ParseCommandInfo & i)
78                 : dispatcher (d) { dispatcher.info_ = &i; }
79
80             ~BindInfo() { dispatcher.info_  = 0; }
81
82             ParseDispatcher & dispatcher;
83         };
84
85         void beginCommand(std::string const & command)
86             { ParserAccess::init(info());
87               ParserAccess::setCommand(info(), command); }
88
89         void endCommand()
90             { ParserAccess::finalize(info()); }
91
92         void pushArgument(std::string const & argument)
93             { ParserAccess::startArgument(info()); 
94               ParserAccess::addToken(info(), ParserAccess::makeToken(argument)); 
95               ParserAccess::endArgument(info()); }
96
97         void openGroup()
98             { ParserAccess::startArgument(info()); }
99
100         void closeGroup()
101             { ParserAccess::endArgument(info()); }
102
103         void pushPunctuation(std::string const & token)
104             { ParserAccess::addToken(info(), ParserAccess::makeToken(token)); }
105
106         void pushWord(std::string const & token)
107             { ParserAccess::addToken(info(), ParserAccess::makeToken(token)); }
108     };
109
110 }}}
111
112 ///////////////////////////////////////////////////////////////////////////
113 // senf::console::ParseCommandInfo
114
115 struct senf::console::ParseCommandInfo::MakeRange
116 {
117     MakeRange() {}
118     MakeRange(ParseCommandInfo::token_iterator b) : b_ (b) {}
119
120     senf::console::ParseCommandInfo::token_iterator b_;
121
122     typedef ParseCommandInfo::argument_value_type result_type;
123         
124     result_type operator()(TempArguments::iterator::value_type const & v) const {
125         return result_type( b_ + v.first, b_ + v.second );
126     }
127 };
128
129 prefix_ void senf::console::ParseCommandInfo::finalize()
130 {
131     arguments_.resize( tempArguments_.size() );
132
133     std::copy( boost::make_transform_iterator( tempArguments_.begin(), 
134                                                MakeRange(tokens_.begin()) ),
135                boost::make_transform_iterator( tempArguments_.end(), 
136                                                MakeRange() ),
137                arguments_.begin() );
138
139     tempArguments_.clear();
140 }
141
142 ///////////////////////////////////////////////////////////////////////////
143 // senf::console::SingleCommandParser
144
145 struct senf::console::SingleCommandParser::Impl
146 {
147     detail::ParseDispatcher dispatcher;
148     detail::CommandGrammar<detail::ParseDispatcher>::Context context;
149     detail::CommandGrammar<detail::ParseDispatcher> grammar;
150     detail::SkipGrammar skipGrammar;
151
152     Impl() : dispatcher(), context(), grammar(dispatcher, context) {}
153 };
154
155 prefix_ senf::console::SingleCommandParser::SingleCommandParser()
156     : impl_ (new Impl())
157 {}
158
159 prefix_ senf::console::SingleCommandParser::~SingleCommandParser()
160 {}
161
162 prefix_ bool senf::console::SingleCommandParser::parseCommand(std::string command,
163                                                               ParseCommandInfo & info)
164 {
165     detail::ParseDispatcher::BindInfo bind (impl().dispatcher, info);
166     return boost::spirit::parse( command.c_str(), impl().grammar, impl().skipGrammar ).full;
167 }
168
169 ///////////////////////////////cc.e////////////////////////////////////////
170 #undef prefix_
171 //#include "Parse.mpp"
172
173 \f
174 // Local Variables:
175 // mode: c++
176 // fill-column: 100
177 // comment-column: 40
178 // c-file-style: "senf"
179 // indent-tabs-mode: nil
180 // ispell-local-dictionary: "american"
181 // compile-command: "scons -u test"
182 // End: