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 */
29 /** \defgroup console_parser The parser
31 The console/config library defines a simple language used to interact with the console or to
32 configure the application. The parser is not concerned about interpreting commands or
33 arguments, checking that a command exists or managing directories. The parser just takes the
38 \section console_language The Language
40 The config/console language is used in configuration files and interactively at the
41 console. Some features of the language are more useful in config files, others at the
42 interactive console but the language is the same in both cases.
44 Let's start with a sample of the config/console language. The following is written as a
47 # My someserver configuration file
53 accept senf::log::Debug IMPORTANT;
54 accept server::ServerLog CRITICAL;
57 provide serverlog senf::log::FileTarget "/var/log/server.log";
59 reject senf::log::Debug senf::Console::Server NOTICE;
60 accept senf::log::Debug NOTICE;
61 accept server::ServerLog;
65 /server/stuffing (UDPPacket x"01 02 03 04");
66 /server/allow_hosts 10.1.2.3 # our internal server
67 10.2.3.4 10.4.3.5 # client workstations
70 /help/infoUrl "http://senf.j32.de/src/doc";
73 The interactive syntax is the same with some notes:
74 \li All commands must be complete on a single line. This includes grouping constructs which must
75 be closed on the same line they are opened.
76 \li The last ';' is optional. However, multiple commands may be entered on a single line when
77 they are separated by ';'.
78 \li An empty line on the interactive console will repeat the last command.
80 The language consists of a small number of syntactic entities:
82 \subsection console_special_chars Special characters
84 These are characters, which have a special meaning. Some are used internally, others are just
85 returned as punctuation tokens
88 <tr><td>/</td><td>path component separator</td></tr>
89 <tr><td>( )</td><td>argument grouping</td></tr>
90 <tr><td>{ }</td><td>directory grouping</td></tr>
91 <tr><td>;</td><td>command terminator</td></tr>
92 <tr><td>, =</td><td>punctuation tokens</td></tr>
95 \subsection console_basic Basic elements
97 A <b>word</b> is \e any sequence of consecutive characters which does not include any special
98 character. Examples for words are thus
101 jens@fokus.fraunhofer.de
106 The following are \e not valid words:
112 A <b>string literal</b> is just that: A double-quoted string (C/C++ style) possibly with
113 embedded escape chars:
119 A <b>hex-string literal</b> is used to represent binary data. It looks like a string which has
120 only hexadecimal bytes or whitespace as contents (comments and newlines are Ok when not read
121 from the interactive console)
129 A <b>token</b> is a \e word, \e string or \e hex-string, or a single special character (that's
130 true, any special character is allowed as a token). '(' and ')' must be properly nested.
132 A <b>path</b> is a sequence of \e words separated by '/' (and optional whitespace). A path may
133 have an optional initial and/or a terminating '/'.
140 \subsection console_statements Statements
142 There are several types of statements:
143 \li The bulk of all statements are \e path statements
144 \li There are some \e built-in statements which are mostly useful at the interactive console
145 \li A special form of statement is the <em>directory group</em>
147 A <b>path</b> statement consists of a (possibly relative) path followed by any number of
148 arguments and terminated with a ';' (or end-of-input)
150 /path/to/command arg1 "arg2" (complex=(1 2) another) ;
152 Every argument is either
153 \li A single word, string or hex-string
154 \li or a parenthesized list of tokens.
156 So above command has three arguments: 'arg1', 'arg2' (a single token each) and one argument with
157 the 7 tokens 'complex', '=', '(', '1', '2', ')', 'another'. The interpretation of the arguments
158 is completely up to the command.
160 A <b>built-in</b> statement is one of
163 <tr><td>\c cd \e path</td><td>Change current directory</td></tr>
164 <tr><td>\c ls [ \e path ]</td><td>List contents of \e path or current directory</td></tr>
165 <tr><td>\c exit</td><td>Exit interactive console</td></tr>
166 <tr><td>\c help [ \e path ]</td><td>Show help for \e path or current directory</td></tr>
169 A <b>directory group</b> statement is a block of statements all executed relatively to a fixed
177 At the beginning of the block, the current directory is saved and the directory is changed to
178 the given directory. All commands are executed and at the end of the block, the saved directory
181 \section console_parse_api The parser API
183 The senf::console::CommandParser is responsible for taking text input and turning it into a
184 sequence of senf::console::ParseCommandInfo structures. The structures are returned by passing
185 them successively to a callback function.
187 Every statement is returned as a senf::console::ParseCommandInfo instance. Directory groups are
188 handled specially: They are divided into two special built-in commands called PUSHD and POPD.
194 #include <boost/utility.hpp>
195 #include <boost/scoped_ptr.hpp>
196 #include <boost/range/iterator_range.hpp>
197 #include <boost/function.hpp>
199 //#include "Parse.mpp"
200 ///////////////////////////////hh.p////////////////////////////////////////
205 namespace detail { struct ParserAccess; }
207 /** \brief Single argument token
209 All command arguments are split into tokens by the parser. Each token is returned as an
210 ArgumentToken instance.
212 \ingroup console_parser
217 std::string const & value() const; ///< String value of token
218 /**< This value is properly unquoted */
223 explicit ArgumentToken(std::string token);
227 friend class detail::ParserAccess;
230 /** \brief Single parsed console command
232 Every command parsed is returned in a ParseCommandInfo instance. This information is purely
233 taken from the parser, no semantic information is attached at this point, the config/console
234 node tree is not involved in any why. ParseCommandInfo consist of
236 \li the type of command: built-in or normal command represented by a possibly relative path
237 into the command tree.
239 \li the arguments. Every argument consists of a range of ArgumentToken instances.
241 \ingroup console_parser
243 \todo Completely change the 'arguments()' member implementation: let the parser just
244 generate a flat list of tokens and implement an 'argument iterator' with the following
245 features: 1. return argument ranges, automatically detecting paranthesis 2. trying to
246 increment the iterator beyond it's range just throws an argument syntax error. For this
247 to work, the parser needs to not drop the outermost '()' pair 3. detect bad paranthesis
248 (should not be necessary since the parser already does this). This allows to use this
249 same iterator to parse nested complex arguments.
251 class ParseCommandInfo
253 typedef std::vector<ArgumentToken> Tokens;
254 typedef std::vector<std::string> CommandPath;
257 typedef CommandPath::const_iterator path_iterator;
258 typedef Tokens::const_iterator token_iterator;
259 typedef boost::iterator_range<token_iterator> argument_value_type;
263 typedef std::vector<argument_value_type> Arguments;
266 typedef Arguments::const_iterator argument_iterator;
267 typedef Arguments::size_type size_type;
269 typedef boost::iterator_range<path_iterator> CommandPathRange;
270 typedef boost::iterator_range<argument_iterator> ArgumentsRange;
271 typedef boost::iterator_range<token_iterator> TokensRange;
273 enum BuiltinCommand { NoBuiltin,
281 BuiltinCommand builtin() const; ///< Command type
282 /**< \returns \c NoBuiltin, if the command is an ordinary
283 command, otherwise the id of the built-in command */
284 CommandPathRange commandPath() const; ///< Command path
285 /**< This is the path to the command if it is not a built-in
286 command. Every element of the returned range
287 constitutes one path element. If the first element is
288 empty, the path is an absolute path, otherwise it is
289 relative. If the last element is an empty string, the
290 path ends with a '/' char. */
291 ArgumentsRange arguments() const; ///< Command arguments
292 /**< The returned range contains one TokensRange for each
294 TokensRange tokens() const; ///< All argument tokens
295 /**< The returned range contains \e all argument tokens in a
296 single range not divided into separate arguments. */
301 void setBuiltin(BuiltinCommand builtin);
302 void setCommand(std::vector<std::string> & commandPath);
303 void startArgument();
305 void addToken(ArgumentToken const & token);
310 std::vector<std::string> commandPath_;
312 typedef std::pair<Tokens::size_type, Tokens::size_type> TempArgumentRange;
313 typedef std::vector<TempArgumentRange> TempArguments;
315 BuiltinCommand builtin_;
317 Arguments arguments_;
318 TempArguments tempArguments_;
320 friend class detail::ParserAccess;
323 /**< \brief Output ParseCommandInfo instance
324 \related ParseCommandInfo
326 std::ostream & operator<<(std::ostream & stream, ParseCommandInfo const & info);
328 /** \brief Parse commands
330 This class implements a parser for the console/config language. It supports parsing strings
331 as well as files. For every parsed command, a callback function is called.
333 \implementation The implementation is based on Boost.Spirit. See the file \ref Parse.ih for
334 the formal language grammar.
336 \implementation Parsing an arbitrary iostream is not supported since arbitrary streams are
337 not seekable. If this is needed, it can however be provided using stream iterators and
338 some special iterator adaptors from Boost.Spirit. However, the amount of backtracking
339 needs to be analyzed before this is viable.
341 \todo Implement more detailed error reporting and error recovery.
343 \ingroup console_parser
349 ///////////////////////////////////////////////////////////////////////////
352 typedef boost::function<void (ParseCommandInfo const &)> Callback;
354 ///////////////////////////////////////////////////////////////////////////
355 ///\name Structors and default members
362 ///////////////////////////////////////////////////////////////////////////
364 bool parse(std::string command, Callback cb); ///< Parse string
365 bool parseFile(std::string filename, Callback cb); ///< Parse file
366 /**< \throws SystemException if the file cannot be
374 boost::scoped_ptr<Impl> impl_;
379 ///////////////////////////////hh.e////////////////////////////////////////
381 //#include "Parse.ct"
382 //#include "Parse.cti"
389 // comment-column: 40
390 // c-file-style: "senf"
391 // indent-tabs-mode: nil
392 // ispell-local-dictionary: "american"
393 // compile-command: "scons -u test"