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 internal header */
31 #include <boost/regex.hpp>
32 #include <boost/spirit.hpp>
33 #include <boost/spirit/utility/grammar_def.hpp>
34 #include <boost/spirit/actor.hpp>
35 #include <boost/bind.hpp>
36 #include <boost/function.hpp>
37 #include <boost/ref.hpp>
39 ///////////////////////////////ih.p////////////////////////////////////////
47 ///////////////////////////////////////////////////////////////////////////
52 template <class T, class Value>
53 void act(T & ref, Value const & value) const
54 { ref += T(1, value); }
56 template <class T, class Iterator>
57 void act(T & ref, Iterator const & f, Iterator const & l) const
62 inline boost::spirit::ref_value_actor<T, append_action>
64 { return boost::spirit::ref_value_actor<T, append_action>(ref); }
66 template <class T, class Value>
67 inline boost::spirit::ref_const_ref_actor<T, Value, append_action>
68 append_a(T & ref, Value const & value)
69 { return boost::spirit::ref_const_ref_actor<T, Value, append_action>(ref, value); }
71 ///////////////////////////////////////////////////////////////////////////
74 template <class ParseDispatcher>
75 struct CommandGrammar : boost::spirit::grammar<CommandGrammar<ParseDispatcher> >
77 ///////////////////////////////////////////////////////////////////////////
80 enum { CommandParser, SkipParser };
82 ///////////////////////////////////////////////////////////////////////////
83 // The parse context (variables needed while parsing)
85 typedef Token::TokenType TokenType;
89 std::vector<Token> path;
93 // OUCH ... This is sooooo stupid .. push_back_a and assign_a take their
94 // arguments by const-reference and STORE the REFERENCE ... they do NOT accept
95 // literal values !!!!!!
96 static const Token EmptyToken;
101 ///////////////////////////////////////////////////////////////////////////
102 // Dispatching semantic actions
104 ParseDispatcher & dispatcher;
106 struct Dispatch_actor
108 Dispatch_actor(boost::function<void ()> fn_) : fn (fn_) {}
110 template <class Value>
111 void operator()(Value const & value) const
114 template <class Iterator>
115 void operator()(Iterator const & f, Iterator const & l) const
118 boost::function<void ()> fn;
121 template <class Callback>
122 Dispatch_actor dispatch(Callback cb) const
123 { return Dispatch_actor(boost::bind(cb, boost::ref(dispatcher))); }
125 template <class Callback, class Arg>
126 Dispatch_actor dispatch(Callback cb, Arg const & arg) const
127 { return Dispatch_actor(boost::bind(cb, boost::ref(dispatcher), arg)); }
129 template <class Callback, class Arg1, class Arg2>
130 Dispatch_actor dispatch(Callback cb, Arg1 const & arg1, Arg2 const & arg2) const
131 { return Dispatch_actor(boost::bind(cb, boost::ref(dispatcher), arg1, arg2)); }
133 struct TokenSetter_actor
135 TokenSetter_actor(Context & c, TokenType t) : c_ (c), t_ (t) {}
137 void operator()(std::string const & value) const
138 { c_.token = Token(t_, value); }
139 void operator()(char value) const
140 { c_.token = Token(t_, std::string(1,value)); }
141 template <class Iterator> void operator()(Iterator const & f, Iterator const & l) const
142 { c_.token = Token(t_, std::string(f,l)); }
148 struct TokenSetter_value_actor
149 : public TokenSetter_actor
151 TokenSetter_value_actor(Context & c, TokenType t, std::string & v)
152 : TokenSetter_actor(c,t), v_ (v) {}
154 template <class Value> void operator()(Value const &) const
155 { TokenSetter_actor::operator()(v_); }
157 template <class Iterator> void operator()(Iterator const &, Iterator const &) const
158 { TokenSetter_actor::operator()(v_); }
163 TokenSetter_actor set_token_a(TokenType t) const
164 { return TokenSetter_actor(context, t); }
166 TokenSetter_value_actor set_token_a(TokenType t, std::string & arg) const
167 { return TokenSetter_value_actor(context, t, arg); }
169 ///////////////////////////////////////////////////////////////////////////
171 CommandGrammar(ParseDispatcher & d, Context & c)
172 : context(c), dispatcher(d) {}
174 template <class Scanner>
176 : public boost::spirit::grammar_def< boost::spirit::rule<Scanner>,
177 boost::spirit::rule<Scanner> >
179 boost::spirit::rule<Scanner> command, path, argument, word, string, hexstring, token,
180 punctuation, hexbyte, balanced_tokens, simple_argument, complex_argument, builtin,
181 skip, commands, block, statement, relpath, abspath;
182 boost::spirit::chset<> special_p, punctuation_p, space_p, invalid_p, word_p;
183 boost::spirit::distinct_parser<> keyword_p;
185 definition(CommandGrammar const & self) :
187 // Characters with a special meaning within the parser
188 special_p ("/(){};"),
190 // Additional characters which are returned as punctuation tokens
191 // (only allowed within '()').
192 punctuation_p (",="),
194 // Whitespace characters
197 // Invalid characters: All chars below \x20 (space) which are not space_p
198 // (don't put a \0 in the chset<> argument *string* ...)
200 boost::spirit::chset<>('\0') | boost::spirit::chset<>("\x01-\x20") - space_p ),
202 // Valid word characters
204 boost::spirit::anychar_p - special_p - punctuation_p - space_p - invalid_p),
206 // Keywords must not be followed by a word char or '/'
207 keyword_p ( word_p | boost::spirit::ch_p('/') )
210 using namespace boost::spirit;
211 typedef ParseDispatcher PD;
214 ///////////////////////////////////////////////////////////////////
218 // This is EBNF with some minor tweaks to accommodate C++ syntax
220 // * and + like EBNF but they precede their argument
223 // a % b match any number of a's separated by b
224 // a - b match a but not b
226 // Beside this, we use some special parsers (ch_p, eps_p, confix_p, lex_escape_ch_p,
227 // keyword_p, comment_p) and directives (lexeme_d), however, the parser should be
230 // ch_p match character
231 // eps_p always matches nothing (to attach unconditional actions)
232 // confix_p(a,b,c) match b, preceded by a and terminated by c. Used to parse
233 // string literals and comments
234 // lex_escape_ch_p match a lex style escape char. This is like a C++ style
235 // literal string escape char, however \x will be replaced by 'x'
236 // for any char 'x' if it has no special meaning.
237 // keyword_p match a delimited keyword
238 // comment_p(a,b) match comment starting with a and terminated with b. b
239 // defaults to end-of-line
241 // lexeme_d don't skip whitespace (as defined by the skip parser)
243 // Aligned to the right at column 50 are semantic actions.
245 // For clarity, I have used 'ch_p' explicitly throughout even though it is optional
248 // More info is in the Boost.Spirit documentation
255 = builtin >> (ch_p(';') | end_p)
256 | path >> ( block | statement )
257 | ch_p(';') // Ignore empty commands
263 >> eps_p [ self.dispatch(&PD::builtin_cd,
264 boost::ref(self.context.path)) ]
267 >> eps_p [ self.dispatch(&PD::builtin_ls,
268 boost::ref(self.context.path)) ]
269 | keyword_p("exit") [ self.dispatch(&PD::builtin_exit) ]
273 >> eps_p [ self.dispatch(&PD::builtin_help,
274 boost::ref(self.context.path)) ]
278 = ch_p('{') [ self.dispatch(&PD::pushDirectory,
279 boost::ref(self.context.path)) ]
281 >> ch_p('}') [ self.dispatch(&PD::popDirectory) ]
285 = eps_p [ self.dispatch(&PD::beginCommand,
286 boost::ref(self.context.path)) ]
288 >> (ch_p(';') | end_p)
289 >> eps_p [ self.dispatch(&PD::endCommand) ]
293 = simple_argument [ self.dispatch(&PD::pushToken,
294 boost::ref(self.context.token)) ]
298 simple_argument // All these return their value in context.token
304 string // Returns value in context.token
305 = eps_p [ clear_a(self.context.str) ]
309 >> * ( ( lex_escape_ch_p[ assign_a(self.context.ch) ]
311 ) [ append_a(self.context.str,
314 >> ch_p('"') [ self.set_token_a(AT::BasicString,
319 hexstring // Returns value in context.token
320 = eps_p [ clear_a(self.context.str) ]
321 >> confix_p( "x\"", * hexbyte, '"' )
322 [ self.set_token_a(AT::HexString,
326 path // Returns value in context.path
327 = eps_p [ clear_a(self.context.path) ]
332 = ( word [ push_back_a(self.context.path,
333 self.context.token) ]
335 >> ( ! ch_p('/') [ push_back_a(self.context.path,
336 self.context.EmptyToken) ] )
340 = ch_p('/') [ push_back_a(self.context.path,
341 self.context.EmptyToken) ]
343 | eps_p [ push_back_a(self.context.path,
344 self.context.EmptyToken) ] )
348 = ch_p('(') [ self.set_token_a(AT::ArgumentGroupOpen) ]
349 [ self.dispatch(&PD::pushToken,
350 boost::ref(self.context.token)) ]
352 >> ch_p(')') [ self.set_token_a(AT::ArgumentGroupClose) ]
353 [ self.dispatch(&PD::pushToken,
354 boost::ref(self.context.token)) ]
358 = simple_argument [ self.dispatch(&PD::pushToken,
359 boost::ref(self.context.token)) ]
360 | punctuation [ self.dispatch(&PD::pushToken,
361 boost::ref(self.context.token)) ]
365 punctuation // Returns value in context.str
366 = ch_p('/') [ self.set_token_a(AT::PathSeparator) ]
367 | ch_p('{') [ self.set_token_a(AT::DirectoryGroupOpen) ]
368 | ch_p('}') [ self.set_token_a(AT::DirectoryGroupClose) ]
369 | ch_p(';') [ self.set_token_a(AT::CommandTerminator) ]
370 | punctuation_p [ self.set_token_a(AT::OtherPunctuation) ]
373 word // Returns value in context.token
377 >> (+ word_p) [ assign_a(self.context.str) ]
379 >> eps_p [ self.set_token_a(AT::Word, self.context.str) ]
383 = uint_parser<char, 16, 2, 2>()
384 [ append_a(self.context.str) ]
388 = space_p | comment_p('#')
391 ///////////////////////////////////////////////////////////////////
394 commands, // CommandParser
398 BOOST_SPIRIT_DEBUG_TRACE_RULE(command,1);
399 BOOST_SPIRIT_DEBUG_TRACE_RULE(path,1);
400 BOOST_SPIRIT_DEBUG_TRACE_RULE(argument,1);
401 BOOST_SPIRIT_DEBUG_TRACE_RULE(word,1);
402 BOOST_SPIRIT_DEBUG_TRACE_RULE(string,1);
403 BOOST_SPIRIT_DEBUG_TRACE_RULE(hexstring,1);
404 BOOST_SPIRIT_DEBUG_TRACE_RULE(token,1);
405 BOOST_SPIRIT_DEBUG_TRACE_RULE(punctuation,1);
406 BOOST_SPIRIT_DEBUG_TRACE_RULE(hexbyte,1);
407 BOOST_SPIRIT_DEBUG_TRACE_RULE(balanced_tokens,1);
408 BOOST_SPIRIT_DEBUG_TRACE_RULE(simple_argument,1);
409 BOOST_SPIRIT_DEBUG_TRACE_RULE(complex_argument,1);
410 BOOST_SPIRIT_DEBUG_TRACE_RULE(builtin,1);
411 BOOST_SPIRIT_DEBUG_TRACE_RULE(commands,1);
412 BOOST_SPIRIT_DEBUG_TRACE_RULE(block,1);
413 BOOST_SPIRIT_DEBUG_TRACE_RULE(statement,1);
414 BOOST_SPIRIT_DEBUG_TRACE_RULE(relpath,1);
415 BOOST_SPIRIT_DEBUG_TRACE_RULE(abspath,1);
420 template <class ParseDispatcher>
421 Token const CommandGrammar<ParseDispatcher>::Context::EmptyToken;
427 ///////////////////////////////ih.e////////////////////////////////////////
434 // comment-column: 40
435 // c-file-style: "senf"
436 // indent-tabs-mode: nil
437 // ispell-local-dictionary: "american"
438 // compile-command: "scons -u test"