]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/classic/example/fundamental/distinct/distinct_parser.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / classic / example / fundamental / distinct / distinct_parser.cpp
1 /*=============================================================================
2 Copyright (c) 2003 Vaclav Vesely
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 #include <boost/assert.hpp>
10 #include <iostream>
11 #include <boost/cstdlib.hpp>
12 #include <boost/spirit/include/classic_core.hpp>
13 #include <boost/spirit/include/classic_distinct.hpp>
14
15 using namespace std;
16 using namespace boost;
17 using namespace BOOST_SPIRIT_CLASSIC_NS;
18
19 // keyword_p for C++
20 // (for basic usage instead of std_p)
21 const distinct_parser<> keyword_p("0-9a-zA-Z_");
22
23 // keyword_d for C++
24 // (for mor intricate usage, for example together with symbol tables)
25 const distinct_directive<> keyword_d("0-9a-zA-Z_");
26
27 struct my_grammar: public grammar<my_grammar>
28 {
29 template <typename ScannerT>
30 struct definition
31 {
32 typedef rule<ScannerT> rule_t;
33
34 definition(my_grammar const& self)
35 {
36 top
37 =
38 keyword_p("declare") // use keyword_p instead of std_p
39 >> !ch_p(':')
40 >> keyword_d[str_p("ident")] // use keyword_d
41 ;
42 }
43
44 rule_t top;
45
46 rule_t const& start() const
47 {
48 return top;
49 }
50 };
51 };
52
53 int main()
54 {
55 my_grammar gram;
56 parse_info<> info;
57
58 info = parse("declare ident", gram, space_p);
59 BOOST_ASSERT(info.full); // valid input
60
61 info = parse("declare: ident", gram, space_p);
62 BOOST_ASSERT(info.full); // valid input
63
64 info = parse("declareident", gram, space_p);
65 BOOST_ASSERT(!info.hit); // invalid input
66
67 return exit_success;
68 }