• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2002-2003 Joel de Guzman
3     http://spirit.sourceforge.net/
4 
5     Use, modification and distribution is subject to the Boost Software
6     License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7     http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 
10 // *** See the section "Look Ma' No Rules" in
11 // *** chapter "Techniques" of the Spirit documentation
12 // *** for information regarding this snippet
13 
14 #include <iostream>
15 #include <boost/spirit/include/classic_core.hpp>
16 #include <boost/assert.hpp>
17 
18 using namespace BOOST_SPIRIT_CLASSIC_NS;
19 
20 namespace boost { namespace spirit
21 {
22     template <typename DerivedT>
23     struct sub_grammar : parser<DerivedT>
24     {
25         typedef sub_grammar         self_t;
26         typedef DerivedT const&     embed_t;
27 
28         template <typename ScannerT>
29         struct result
30         {
31             typedef typename parser_result<
32                 typename DerivedT::start_t, ScannerT>::type
33             type;
34         };
35 
derivedboost::spirit::sub_grammar36         DerivedT const& derived() const
37         { return *static_cast<DerivedT const*>(this); }
38 
39         template <typename ScannerT>
40         typename parser_result<self_t, ScannerT>::type
parseboost::spirit::sub_grammar41         parse(ScannerT const& scan) const
42         {
43             return derived().start.parse(scan);
44         }
45     };
46 }}
47 
48 ///////////////////////////////////////////////////////////////////////////////
49 //
50 //  Client code
51 //
52 ///////////////////////////////////////////////////////////////////////////////
53 struct skip_grammar : boost::spirit::sub_grammar<skip_grammar>
54 {
55     typedef
56        alternative<alternative<space_parser, sequence<sequence<
57        strlit<const char*>, kleene_star<difference<anychar_parser,
58        chlit<char> > > >, chlit<char> > >, sequence<sequence<
59        strlit<const char*>, kleene_star<difference<anychar_parser,
60        strlit<const char*> > > >, strlit<const char*> > >
61     start_t;
62 
skip_grammarskip_grammar63     skip_grammar()
64     : start
65         (
66             space_p
67         |   "//" >> *(anychar_p - '\n') >> '\n'
68         |   "/*" >> *(anychar_p - "*/") >> "*/"
69         )
70     {}
71 
72     start_t start;
73 };
74 
75 int
main()76 main()
77 {
78     skip_grammar g;
79 
80     bool success = parse(
81         "/*this is a comment*/\n//this is a c++ comment\n\n",
82         *g).full;
83     BOOST_ASSERT(success);
84     std::cout << "SUCCESS!!!\n";
85     return 0;
86 }
87