adapt_template_struct.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright (c) 2001-2010 Hartmut Kaiser
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // This example demonstrates a trick allowing to adapt a template data
  6. // structure as a Fusion sequence in order to use is for direct attribute
  7. // propagation. For more information see
  8. // http://boost-spirit.com/home/2010/02/08/how-to-adapt-templates-as-a-fusion-sequence
  9. #include <boost/spirit/include/qi.hpp>
  10. namespace qi = boost::spirit::qi;
  11. namespace fusion = boost::fusion;
  12. namespace client
  13. {
  14. template <typename A, typename B>
  15. struct data
  16. {
  17. A a;
  18. B b;
  19. };
  20. template <typename Iterator, typename A, typename B>
  21. struct data_grammar : qi::grammar<Iterator, data<A, B>()>
  22. {
  23. data_grammar() : data_grammar::base_type(start)
  24. {
  25. start = real_start;
  26. real_start = qi::auto_ >> ',' >> qi::auto_;
  27. }
  28. qi::rule<Iterator, data<A, B>()> start;
  29. qi::rule<Iterator, fusion::vector<A&, B&>()> real_start;
  30. };
  31. }
  32. namespace boost { namespace spirit { namespace traits
  33. {
  34. template <typename A, typename B>
  35. struct transform_attribute<client::data<A, B>, fusion::vector<A&, B&>, qi::domain>
  36. {
  37. typedef fusion::vector<A&, B&> type;
  38. static type pre(client::data<A, B>& val) { return type(val.a, val.b); }
  39. static void post(client::data<A, B>&, fusion::vector<A&, B&> const&) {}
  40. static void fail(client::data<A, B>&) {}
  41. };
  42. }}}
  43. ///////////////////////////////////////////////////////////////////////////////
  44. int main()
  45. {
  46. std::cout << "/////////////////////////////////////////////////////////\n\n";
  47. std::cout << "\t\tA parser for Spirit utilizing an adapted template ...\n\n";
  48. std::cout << "/////////////////////////////////////////////////////////\n\n";
  49. std::cout << "Give me two comma separated integers:\n";
  50. std::cout << "Type [q or Q] to quit\n\n";
  51. std::string str;
  52. client::data_grammar<std::string::const_iterator, long, int> g; // Our grammar
  53. while (getline(std::cin, str))
  54. {
  55. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  56. break;
  57. client::data<long, int> d;
  58. std::string::const_iterator iter = str.begin();
  59. std::string::const_iterator end = str.end();
  60. bool r = phrase_parse(iter, end, g, qi::space, d);
  61. if (r && iter == end)
  62. {
  63. std::cout << "-------------------------\n";
  64. std::cout << "Parsing succeeded\n";
  65. std::cout << "got: " << d.a << "," << d.b << std::endl;
  66. std::cout << "\n-------------------------\n";
  67. }
  68. else
  69. {
  70. std::cout << "-------------------------\n";
  71. std::cout << "Parsing failed\n";
  72. std::cout << "-------------------------\n";
  73. }
  74. }
  75. return 0;
  76. }