replaced some tabs with spaces
[senf.git] / Scheduler / Console / Parse.ih
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 internal header */
25
26 #ifndef IH_SENF_Scheduler_Console_Parse_
27 #define IH_SENF_Scheduler_Console_Parse_ 1
28
29 // Custom includes
30 #include <vector>
31 #include <boost/spirit.hpp>
32 #include <boost/spirit/utility/grammar_def.hpp>
33 #include <boost/spirit/dynamic.hpp>
34 #include <boost/spirit/phoenix.hpp>
35 #include "../../Utils/Phoenix.hh"
36
37 ///////////////////////////////ih.p////////////////////////////////////////
38
39 namespace senf {
40 namespace console {
41 namespace detail {
42
43 #ifndef DOXYGEN
44
45     ///////////////////////////////////////////////////////////////////////////
46     // Grammar
47
48     template <class ParseDispatcher>
49     struct CommandGrammar : boost::spirit::grammar<CommandGrammar<ParseDispatcher> >
50     {
51         ///////////////////////////////////////////////////////////////////////////
52         // Start rules
53
54         enum { CommandParser, SkipParser, ArgumentsParser };
55
56         ///////////////////////////////////////////////////////////////////////////
57         // The parse context (variables needed while parsing)
58
59         typedef Token::TokenType TokenType;
60
61         struct Context {
62             std::string str;
63             std::vector<Token> path;
64             char ch;
65             Token token;
66         };
67
68         Context & context;
69
70         ///////////////////////////////////////////////////////////////////////////
71         // Configuration
72
73         bool incremental;
74
75         ///////////////////////////////////////////////////////////////////////////
76         // Dispatching semantic actions
77
78         ParseDispatcher & dispatcher;
79
80         ///////////////////////////////////////////////////////////////////////////
81         // Errors
82
83         enum Errors {
84             EndOfStatementExpected,
85             GroupOrArgumentsExpected,
86             PathExpected,
87             ClosingParenExpected,
88             QuoteExpected
89         };
90
91         ///////////////////////////////////////////////////////////////////////////
92
93         CommandGrammar(ParseDispatcher & d, Context & c) 
94             : context(c), incremental(false), dispatcher(d) {}
95
96         template <class Scanner>
97         struct definition 
98             : public boost::spirit::grammar_def< boost::spirit::rule<Scanner>, 
99                                                  boost::spirit::rule<Scanner>,
100                                                  boost::spirit::rule<Scanner> >
101         {
102             boost::spirit::rule<Scanner> command, path, argument, word, string, hexstring, token,
103                 punctuation, hexbyte, balanced_tokens, simple_argument, complex_argument, builtin, 
104                 skip, statement, relpath, abspath, arguments, group_start, group_close, 
105                 statement_end;
106             boost::spirit::chset<> special_p, punctuation_p, space_p, invalid_p, word_p;
107             boost::spirit::distinct_parser<> keyword_p;
108
109             definition(CommandGrammar const & self) : 
110
111                 // Characters with a special meaning within the parser
112                 special_p ("/(){};\""),
113
114                 // Additional characters which are returned as punctuation tokens
115                 // (only allowed within '()').
116                 punctuation_p (",="),
117
118                 // Whitespace characters
119                 space_p (" \t\n\r"),
120
121                 // Invalid characters: All chars below \x20 (space) which are not space_p
122                 // (don't put a \0 in the chset<> argument *string* ...)
123                 invalid_p ( (boost::spirit::chset<>('\0') 
124                              | boost::spirit::chset<>("\x01-\x20")) - space_p ),
125
126                 // Valid word characters
127                 word_p (
128                     boost::spirit::anychar_p - special_p - punctuation_p - space_p - invalid_p),
129
130                 // Keywords must not be followed by a word char or '/'
131                 keyword_p ( word_p | boost::spirit::ch_p('/') )
132
133             {
134                 using namespace boost::spirit;
135                 using namespace ::phoenix;
136                 using namespace senf::phoenix;
137                 typedef ParseDispatcher PD;
138
139                 actor< variable< char > >               ch_    (self.context.ch);
140                 actor< variable< std::string > >        str_   (self.context.str);
141                 actor< variable< std::vector<Token> > > path_  (self.context.path);
142                 actor< variable< Token > >              token_ (self.context.token);
143                 actor< variable< ParseDispatcher > >    d_     (self.dispatcher);
144
145                 assertion<Errors> end_of_statement_expected   (EndOfStatementExpected);
146                 assertion<Errors> group_or_arguments_expected (GroupOrArgumentsExpected);
147                 assertion<Errors> path_expected               (PathExpected);
148                 assertion<Errors> closing_paren_expected      (ClosingParenExpected);
149                 assertion<Errors> quote_expected              (QuoteExpected);
150
151                 ///////////////////////////////////////////////////////////////////
152                 // Spirit grammar
153                 //
154                 // Syntax summary:
155                 // This is EBNF with some minor tweaks to accommodate C++ syntax
156                 //
157                 //   * a        any number of a's
158                 //   + a        at least one a
159                 //   ! a        an optional a
160                 //   a >> b     a followed by b
161                 //   a | b      a or b
162                 //   a % b      any number of a's separated by b's
163                 //   a - b      a but not b
164                 //
165                 // Beside this, we use some special parsers (ch_p, eps_p, confix_p, lex_escape_ch_p,
166                 // keyword_p, comment_p) and directives (lexeme_d), however, the parser should be
167                 // quite readable.
168                 //   
169                 //   ch_p             match character
170                 //   eps_p            always matches nothing (to attach unconditional actions)
171                 //   confix_p(a,b,c)  match b, preceded by a and terminated by c. Used to parse
172                 //                    string literals and comments
173                 //   lex_escape_ch_p  match a lex style escape char. This is like a C++ style
174                 //                    literal string escape char, however \x will be replaced by 'x'
175                 //                    for any char 'x' if it has no special meaning.
176                 //   keyword_p        match a delimited keyword
177                 //   comment_p(a,b)   match comment starting with a and terminated with b. b
178                 //                    defaults to end-of-line
179                 //
180                 //   lexeme_d         don't skip whitespace (as defined by the skip parser)
181                 //
182                 // Aligned to the right at column 50 are semantic actions.
183                 //
184                 // For clarity, I have used 'ch_p' explicitly throughout even though it is optional
185                 // in most cases.
186                 //
187                 // More info is in the Boost.Spirit documentation
188
189                 command 
190                     =    builtin >> end_of_statement_expected(statement_end)
191                     |    group_close
192                     |    ch_p(';') // Ignore empty commands
193                     |    path_expected(path) 
194                       >> group_or_arguments_expected( group_start | statement )
195                     ;
196
197                 builtin
198                     =    keyword_p("cd") 
199                       >> path_expected(path)
200                       >> eps_p                    [ bind(&PD::builtin_cd)(d_, path_) ]
201                     |    keyword_p("ls")
202                       >> ! path
203                       >> eps_p                    [ bind(&PD::builtin_ls)(d_, path_) ]
204                     |    keyword_p("exit")        [ bind(&PD::builtin_exit)(d_) ]
205                     |    keyword_p("help")
206                       >> ! path
207                       >> eps_p                    [ bind(&PD::builtin_help)(d_, path_) ]
208                     ;
209
210                 group_start
211                     =    ch_p('{')                [ bind(&PD::pushDirectory)(d_, path_) ]
212                     ;
213
214                 group_close
215                     =    ch_p('}')                [ bind(&PD::popDirectory)(d_) ]
216                     ;
217
218                 statement
219                     =    eps_p                    [ bind(&PD::beginCommand)(d_, path_) ]
220                       >> arguments
221                       >> end_of_statement_expected(statement_end)
222                                                   [ bind(&PD::endCommand)(d_) ]
223                     ;
224
225                 arguments
226                     =    * argument
227                     ;
228
229                 argument
230                     =    simple_argument          [ bind(&PD::pushToken)(d_, token_) ]
231                     |    balanced_tokens
232                     ;
233                 
234                 simple_argument         // All these return their value in context.token
235                     =    string
236                     |    hexstring
237                     |    word
238                     ;
239                 
240                 string                  // Returns value in context.token
241                     =    eps_p                    [ clear(str_) ]
242                       >> lexeme_d
243                          [
244                              ch_p('"')
245                           >> * ( ( lex_escape_ch_p[ ch_ = arg1 ] 
246                                    - '"' 
247                                  )                [ str_ += ch_ ]
248                                )
249                           >> quote_expected(ch_p('"'))
250                                                   [ token_ = construct_<Token>(Token::BasicString, 
251                                                                                str_) ]
252                          ]
253                     ;
254
255                 hexstring               // Returns value in context.token
256                     =    eps_p                    [ clear(str_) ]
257                       >>  "x\""
258                       >> * ( hexbyte - ch_p('"') )
259                       >> quote_expected(ch_p('"'))
260                                                   [ token_ = construct_<Token>(Token::HexString,
261                                                                                str_) ]
262                     ;
263
264                 path                    // Returns value in context.path
265                     =    eps_p                    [ clear(path_) ]
266                       >> relpath | abspath
267                     ;
268
269                 relpath
270                     =    (   word                 [ push_back(path_, token_) ]
271                            % ch_p('/') )
272                       >> ( ! ch_p('/')            [ push_back(path_, construct_<Token>()) ] )
273                     ;
274
275                 abspath
276                     =    ch_p('/')                [ push_back(path_, construct_<Token>()) ]
277                       >> ( relpath
278                          | eps_p                  [ push_back(path_, construct_<Token>()) ] )
279                     ;
280
281                 balanced_tokens 
282                     =    ch_p('(')                [ token_ = construct_<Token>(
283                                                         Token::ArgumentGroupOpen,
284                                                         "(") ]
285                                                   [ bind(&PD::pushToken)(d_, token_) ]
286                       >> * token
287                       >> closing_paren_expected(ch_p(')'))
288                                                   [ token_ = construct_<Token>(
289                                                         Token::ArgumentGroupClose,
290                                                         ")") ]
291                                                   [ bind(&PD::pushToken)(d_, token_) ]
292                     ;
293
294                 token
295                     =    simple_argument          [ bind(&PD::pushToken)(d_, token_) ]
296                     |    punctuation              [ bind(&PD::pushToken)(d_, token_) ]
297                     |    balanced_tokens
298                     ;
299
300                 punctuation             // Returns value in context.str
301                     =    ch_p('/')                [ token_ = construct_<Token>(
302                                                         Token::PathSeparator,
303                                                         "/") ]
304                     |    ch_p('{')                [ token_ = construct_<Token>(
305                                                         Token::DirectoryGroupOpen,
306                                                         "{") ]
307                     |    ch_p('}')                [ token_ = construct_<Token>(
308                                                         Token::DirectoryGroupClose,
309                                                         "}") ]
310                     |    ch_p(';')                [ token_ = construct_<Token>(
311                                                         Token::CommandTerminator,
312                                                         ";") ]
313                     |    punctuation_p            [ token_ = construct_<Token>(
314                                                         Token::OtherPunctuation,
315                                                         construct_<std::string>(1u, arg1)) ]
316                     ;
317
318                 word                    // Returns value in context.token
319                     =    lexeme_d
320                          [
321                              (+ word_p)           [ str_ = construct_<std::string>(arg1, arg2) ]
322                          ]
323                       >> eps_p                    [ token_ = construct_<Token>(
324                                                         Token::Word, 
325                                                         str_) ]
326                     ;
327
328                 hexbyte
329                     =    uint_parser<char, 16, 2, 2>()
330                                                   [ push_back(str_, arg1) ]
331                     ;
332
333                 statement_end
334                     =    if_p(var(self.incremental)) [
335                                ch_p(';')
336                          ]
337                          .else_p [
338                                ch_p(';') 
339                              | end_p
340                          ]
341                     ;
342
343                 skip
344                     =    space_p | comment_p('#')
345                     ;
346
347                 ///////////////////////////////////////////////////////////////////
348
349                 start_parsers(
350                     command,            // CommandParser
351                     skip,               // SkipParser
352                     arguments           // ArgumentsParser
353                 );
354
355                 BOOST_SPIRIT_DEBUG_TRACE_RULE(command,1);
356                 BOOST_SPIRIT_DEBUG_TRACE_RULE(path,1);
357                 BOOST_SPIRIT_DEBUG_TRACE_RULE(argument,1);
358                 BOOST_SPIRIT_DEBUG_TRACE_RULE(word,1);
359                 BOOST_SPIRIT_DEBUG_TRACE_RULE(string,1);
360                 BOOST_SPIRIT_DEBUG_TRACE_RULE(hexstring,1);
361                 BOOST_SPIRIT_DEBUG_TRACE_RULE(token,1);
362                 BOOST_SPIRIT_DEBUG_TRACE_RULE(punctuation,1);
363                 BOOST_SPIRIT_DEBUG_TRACE_RULE(hexbyte,1);
364                 BOOST_SPIRIT_DEBUG_TRACE_RULE(balanced_tokens,1);
365                 BOOST_SPIRIT_DEBUG_TRACE_RULE(simple_argument,1);
366                 BOOST_SPIRIT_DEBUG_TRACE_RULE(complex_argument,1);
367                 BOOST_SPIRIT_DEBUG_TRACE_RULE(builtin,1);
368                 BOOST_SPIRIT_DEBUG_TRACE_RULE(commands,1);
369                 BOOST_SPIRIT_DEBUG_TRACE_RULE(block,1);
370                 BOOST_SPIRIT_DEBUG_TRACE_RULE(statement,1);
371                 BOOST_SPIRIT_DEBUG_TRACE_RULE(relpath,1);
372                 BOOST_SPIRIT_DEBUG_TRACE_RULE(abspath,1);
373             }
374         };
375     };
376
377 #endif
378
379 }}}
380
381 ///////////////////////////////ih.e////////////////////////////////////////
382 #endif
383
384 \f
385 // Local Variables:
386 // mode: c++
387 // fill-column: 100
388 // comment-column: 40
389 // c-file-style: "senf"
390 // indent-tabs-mode: nil
391 // ispell-local-dictionary: "american"
392 // compile-command: "scons -u test"
393 // End: