parse_date.cpp 4.8 KB

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