1 /*=============================================================================
2 Copyright (c) 2001-2010 Hartmut Kaiser
3 Copyright (c) 2001-2010 Joel de Guzman
4
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
8
9 // This example is not meant to be a sophisticated date parser. It's sole
10 // purpose is to demonstrate the intrinsic attribute transformation
11 // capabilities of a rule.
12 //
13 // Note how the rule exposes a fusion sequence, but gets passed an instance of
14 // a boost::gregorian::date as the attribute. In order to make these types
15 // compatible for the rule we define a specialization of the customization
16 // point called 'transform_attribute'.
17
18 #include <boost/config/warning_disable.hpp>
19 #include <boost/spirit/include/qi.hpp>
20 #include <boost/fusion/include/vector.hpp>
21 #include <boost/date_time.hpp>
22
23 // define custom transformation
24 namespace boost { namespace spirit { namespace traits
25 {
26 // This specialization of the customization point transform_attribute
27 // allows to pass a boost::gregorian::date to a rule which is expecting
28 // a fusion sequence consisting out of three integers as its attribute.
29 template<>
30 struct transform_attribute<
31 boost::gregorian::date, fusion::vector<int, int, int>, qi::domain>
32 {
33 typedef fusion::vector<int, int, int> date_parts;
34
35 // The embedded typedef 'type' exposes the attribute as it will be
36 // passed to the right hand side of the rule.
37 typedef date_parts type;
38
39 // The function pre() is called for down-stream conversion of the
40 // attribute supplied to the rule to the attribute expected by the
41 // right hand side.
42 // The supplied attribute might have been pre-initialized by parsers
43 // (i.e. semantic actions) higher up the parser hierarchy (in the
44 // grammar), in which case we would need to properly initialize the
45 // returned value from the argument. In this example this is not
46 // required, so we just create a new instance of a date_parts.
preboost::spirit::traits::transform_attribute47 static date_parts pre(boost::gregorian::date)
48 {
49 return date_parts();
50 }
51
52 // The function post() is called for up-stream conversion of the
53 // results returned from parsing the right hand side of the rule.
54 // We need to initialize the attribute supplied to the rule (referenced
55 // by the first argument) with the values taken from the parsing
56 // results (referenced by the second argument).
postboost::spirit::traits::transform_attribute57 static void post(boost::gregorian::date& d, date_parts const& v)
58 {
59 d = boost::gregorian::date(fusion::at_c<0>(v), fusion::at_c<1>(v)
60 , fusion::at_c<2>(v));
61 }
62
63 // The function fail() is called whenever the parsing of the right hand
64 // side of the rule fails. We don't need to do anything here.
failboost::spirit::traits::transform_attribute65 static void fail(boost::gregorian::date&) {}
66 };
67 }}}
68
69 ///////////////////////////////////////////////////////////////////////////////
70 namespace client
71 {
72 namespace qi = boost::spirit::qi;
73
74 template <typename Iterator>
parse_date(Iterator & first,Iterator last,boost::gregorian::date & d)75 bool parse_date(Iterator& first, Iterator last, boost::gregorian::date& d)
76 {
77 typedef boost::fusion::vector<int, int, int> date_parts;
78 qi::rule<Iterator, date_parts(), qi::space_type> date =
79 qi::int_ >> '-' >> qi::int_ >> '-' >> qi::int_;
80
81 return phrase_parse(first, last, date, qi::space, d);
82 }
83 }
84
85 ///////////////////////////////////////////////////////////////////////////////
main()86 int main()
87 {
88 std::cout << "/////////////////////////////////////////////////////////\n\n";
89 std::cout << "\t\tA date parser for Spirit...\n\n";
90 std::cout << "/////////////////////////////////////////////////////////\n\n";
91
92 std::cout << "Give me a date of the form : year-month-day\n";
93 std::cout << "Type [q or Q] to quit\n\n";
94
95 std::string str;
96 while (getline(std::cin, str))
97 {
98 if (str.empty() || str[0] == 'q' || str[0] == 'Q')
99 break;
100
101 boost::gregorian::date d;
102 std::string::const_iterator iter = str.begin();
103 std::string::const_iterator end = str.end();
104 bool r = client::parse_date(iter, end, d);
105
106 if (r && iter == end)
107 {
108 std::cout << "-------------------------\n";
109 std::cout << "Parsing succeeded\n";
110 std::cout << "got: " << d << std::endl;
111 std::cout << "\n-------------------------\n";
112 }
113 else
114 {
115 std::cout << "-------------------------\n";
116 std::cout << "Parsing failed\n";
117 std::cout << "-------------------------\n";
118 }
119 }
120
121 std::cout << "Bye... :-) \n\n";
122 return 0;
123 }
124
125
126