4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief Parse non-inline non-template implementation */
30 #include <boost/iterator/transform_iterator.hpp>
32 //#include "Parse.mpp"
34 ///////////////////////////////cc.p////////////////////////////////////////
42 static void init(ParseCommandInfo & info)
45 static void setCommand(ParseCommandInfo & info, std::string const & commandPath)
46 { info.setCommand(commandPath); }
48 static void startArgument(ParseCommandInfo & info)
49 { info.startArgument(); }
51 static void endArgument(ParseCommandInfo & info)
52 { info.endArgument(); }
54 static void addToken(ParseCommandInfo & info, ArgumentToken const & token)
55 { info.addToken(token); }
57 static void finalize(ParseCommandInfo & info)
60 static ArgumentToken makeToken(std::string const & token)
61 { return ArgumentToken(token); }
64 struct ParseDispatcher
69 ParseCommandInfo * info_;
71 ParseCommandInfo & info() {
77 BindInfo( ParseDispatcher & d, ParseCommandInfo & i)
78 : dispatcher (d) { dispatcher.info_ = &i; }
80 ~BindInfo() { dispatcher.info_ = 0; }
82 ParseDispatcher & dispatcher;
85 void beginCommand(std::string const & command)
86 { ParserAccess::init(info());
87 ParserAccess::setCommand(info(), command); }
90 { ParserAccess::finalize(info()); }
92 void pushArgument(std::string const & argument)
93 { ParserAccess::startArgument(info());
94 ParserAccess::addToken(info(), ParserAccess::makeToken(argument));
95 ParserAccess::endArgument(info()); }
98 { ParserAccess::startArgument(info()); }
101 { ParserAccess::endArgument(info()); }
103 void pushPunctuation(std::string const & token)
104 { ParserAccess::addToken(info(), ParserAccess::makeToken(token)); }
106 void pushWord(std::string const & token)
107 { ParserAccess::addToken(info(), ParserAccess::makeToken(token)); }
112 ///////////////////////////////////////////////////////////////////////////
113 // senf::console::ParseCommandInfo
115 struct senf::console::ParseCommandInfo::MakeRange
118 MakeRange(ParseCommandInfo::token_iterator b) : b_ (b) {}
120 senf::console::ParseCommandInfo::token_iterator b_;
122 typedef ParseCommandInfo::argument_value_type result_type;
124 result_type operator()(TempArguments::iterator::value_type const & v) const {
125 return result_type( b_ + v.first, b_ + v.second );
129 prefix_ void senf::console::ParseCommandInfo::finalize()
131 arguments_.resize( tempArguments_.size() );
133 std::copy( boost::make_transform_iterator( tempArguments_.begin(),
134 MakeRange(tokens_.begin()) ),
135 boost::make_transform_iterator( tempArguments_.end(),
137 arguments_.begin() );
139 tempArguments_.clear();
142 ///////////////////////////////////////////////////////////////////////////
143 // senf::console::SingleCommandParser
145 struct senf::console::SingleCommandParser::Impl
147 detail::ParseDispatcher dispatcher;
148 detail::CommandGrammar<detail::ParseDispatcher>::Context context;
149 detail::CommandGrammar<detail::ParseDispatcher> grammar;
150 detail::SkipGrammar skipGrammar;
152 Impl() : dispatcher(), context(), grammar(dispatcher, context) {}
155 prefix_ senf::console::SingleCommandParser::SingleCommandParser()
159 prefix_ senf::console::SingleCommandParser::~SingleCommandParser()
162 prefix_ bool senf::console::SingleCommandParser::parseCommand(std::string command,
163 ParseCommandInfo & info)
165 detail::ParseDispatcher::BindInfo bind (impl().dispatcher, info);
166 return boost::spirit::parse( command.c_str(), impl().grammar, impl().skipGrammar ).full;
169 ///////////////////////////////cc.e////////////////////////////////////////
171 //#include "Parse.mpp"
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"