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 public header */
32 #include <boost/utility.hpp>
33 #include <boost/scoped_ptr.hpp>
34 #include <boost/range/iterator_range.hpp>
35 #include <boost/function.hpp>
37 //#include "Parse.mpp"
38 ///////////////////////////////hh.p////////////////////////////////////////
43 namespace detail { struct ParserAccess; }
45 /** \brief Single argument token
47 All command arguments are split into tokens by the parser. Each token is returned as an
48 ArgumentToken instance.
53 std::string const & value() const; ///< String value of token
54 /**< This value is properly unquoted */
59 explicit ArgumentToken(std::string token);
63 friend class detail::ParserAccess;
66 /** \brief Console command
68 Every command parsed is returned in a ParseCommandInfo instance. This information is purely
69 taken from the parser, no semantic information is attached at this point, the config/console
70 is not involved in any why. ParseCommandInfo consist of
72 \li the type of command: builtin or normal command represented by a possibly relative path
73 into the command tree.
75 \li the arguments. Every argument consists of a range of ArgumentToken instances.
77 class ParseCommandInfo
79 typedef std::vector<ArgumentToken> Tokens;
80 typedef std::vector<std::string> CommandPath;
83 typedef CommandPath::const_iterator path_iterator;
84 typedef Tokens::const_iterator token_iterator;
85 typedef boost::iterator_range<token_iterator> argument_value_type;
89 typedef std::vector<argument_value_type> Arguments;
92 typedef Arguments::const_iterator argument_iterator;
93 typedef Arguments::size_type size_type;
95 typedef boost::iterator_range<path_iterator> CommandPathRange;
96 typedef boost::iterator_range<argument_iterator> ArgumentsRange;
97 typedef boost::iterator_range<token_iterator> TokensRange;
99 enum BuiltinCommand { NoBuiltin,
107 BuiltinCommand builtin() const; ///< Command type
108 /**< \returns \c NoBuiltin, if the command is an ordinary
109 command, otherwise the id of the builtin command */
110 CommandPathRange commandPath() const; ///< Command path
111 /**< This is the path to the command if it is not a builtin
112 command. Every element of the returned range
113 constitutes one path element. If the first element is
114 empty, the path is an absolute path, otherwise it is
115 relative. If the last element is an empty string, the
116 path ends in a '/' char. */
117 ArgumentsRange arguments() const; ///< Command arguments
118 /**< The returned range contains one token range for each
120 TokensRange tokens() const; ///< All argument tokens
121 /**< The returned range contains \e all argument tokens in a
122 single range not divided into separate arguments. */
127 void setBuiltin(BuiltinCommand builtin);
128 void setCommand(std::vector<std::string> & commandPath);
129 void startArgument();
131 void addToken(ArgumentToken const & token);
136 std::vector<std::string> commandPath_;
138 typedef std::pair<Tokens::size_type, Tokens::size_type> TempArgumentRange;
139 typedef std::vector<TempArgumentRange> TempArguments;
141 BuiltinCommand builtin_;
143 Arguments arguments_;
144 TempArguments tempArguments_;
146 friend class detail::ParserAccess;
149 /**< \brief Output ParseCommandInfo instance
150 \related ParseCommandInfo
152 std::ostream & operator<<(std::ostream & stream, ParseCommandInfo const & info);
154 /** \brief Parse commands
156 This class implements a parser for the console/config language. It supports parsing strings
157 as well as files. For every parsed command, a callback function is called.
159 \implementation The implementation is based on Boost.Spirit. See the file \ref Parse.ih for
160 the formal language grammar.
162 \implementation Parsing an arbitrary iostream is not supported since arbitrary streams are
163 not seekable. If this is needed, it can however be provided using stream iterators and
164 some special iterator adaptors from Boost.Spirit. However, the amount of backtracking
165 needs to be analyzed before this is viable.
167 \todo Implement more detailed error reporting and error recovery.
173 ///////////////////////////////////////////////////////////////////////////
176 typedef boost::function<void (ParseCommandInfo const &)> Callback;
178 ///////////////////////////////////////////////////////////////////////////
179 ///\name Structors and default members
186 ///////////////////////////////////////////////////////////////////////////
188 bool parse(std::string command, Callback cb); ///< Parse string
189 bool parseFile(std::string filename, Callback cb); ///< Parse file
190 /**< \throws SystemException if the file cannot be
198 boost::scoped_ptr<Impl> impl_;
203 ///////////////////////////////hh.e////////////////////////////////////////
205 //#include "Parse.ct"
206 //#include "Parse.cti"
213 // comment-column: 40
214 // c-file-style: "senf"
215 // indent-tabs-mode: nil
216 // ispell-local-dictionary: "american"
217 // compile-command: "scons -u test"