Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. 1;2
  2. =1200
  3. 3;4
  4. 5;6
  5.  
  6. #define BOOST_SPIRIT_USE_PHOENIX_V3
  7. #define DATAPAIR_PAIR
  8.  
  9. #include <iostream>
  10. #include <boost/spirit/include/qi.hpp>
  11. #include <boost/fusion/adapted/std_pair.hpp>
  12. #include <boost/mpl/bool.hpp>
  13. #include <map>
  14. #if !defined(DATAPAIR_PAIR)
  15. #include <vector>
  16. #endif
  17.  
  18.  
  19. static const char g_data[] = "1;2n=1200n3;4n5;6n";
  20. typedef std::string DataTypeFirst;
  21.  
  22. #if defined(DATAPAIR_PAIR)
  23. typedef std::string DataTypeSecond;
  24. typedef std::pair<DataTypeFirst, DataTypeSecond> DataPair;
  25. typedef std::map<DataTypeFirst, DataTypeSecond> DataMap;
  26. #else
  27. typedef std::vector<DataTypeFirst> DataPair;
  28. typedef std::map<DataTypeFirst, DataTypeFirst> DataMap;
  29. #endif
  30.  
  31. struct MyContainer {
  32. DataMap data;
  33. double number;
  34. };
  35.  
  36. namespace boost { namespace spirit { namespace traits {
  37. template<> struct is_container<MyContainer> : boost::mpl::true_ {};
  38.  
  39. template<>
  40. struct container_value<MyContainer> {
  41. typedef boost::variant<double, DataPair> type;
  42. };
  43.  
  44. template <>
  45. struct push_back_container<MyContainer, double> {
  46. static bool call ( MyContainer& parContainer, double parValue ) {
  47. parContainer.number = parValue;
  48. return true;
  49. }
  50. };
  51.  
  52. template <>
  53. struct push_back_container<MyContainer, DataPair> {
  54. static bool call ( MyContainer& parContainer, const DataPair& parValue ) {
  55. #if defined(DATAPAIR_PAIR)
  56. parContainer.data[parValue.first] = parValue.second;
  57. #else
  58. parContainer.data[parValue[0]] = parValue[1];
  59. #endif
  60. return true;
  61. }
  62. };
  63. } } }
  64.  
  65. template <typename Iterator>
  66. struct TestGrammar : boost::spirit::qi::grammar<Iterator, MyContainer()> {
  67.  
  68. TestGrammar ( void );
  69. boost::spirit::qi::rule<Iterator, MyContainer()> start;
  70. boost::spirit::qi::rule<Iterator, DataPair()> data;
  71. boost::spirit::qi::rule<Iterator, double()> num;
  72. };
  73.  
  74. template <typename Iterator>
  75. TestGrammar<Iterator>::TestGrammar() :
  76. TestGrammar::base_type(start)
  77. {
  78. using boost::spirit::qi::alnum;
  79. using boost::spirit::qi::lit;
  80. using boost::spirit::ascii::char_;;
  81. using boost::spirit::qi::double_;
  82. using boost::spirit::qi::eol;
  83. using boost::spirit::qi::eoi;
  84.  
  85. start %= *((num | data) >> (eol | eoi));
  86. data = +alnum >> lit(";") >> +alnum;
  87. num = '=' >> double_;
  88. }
  89.  
  90. int main() {
  91. std::cout << "Parsing data:n" << g_data << "n";
  92.  
  93. TestGrammar<const char*> gramm;
  94. MyContainer result;
  95. boost::spirit::qi::parse(static_cast<const char*>(g_data),
  96. g_data + sizeof(g_data) / sizeof(g_data[0]) - 1,
  97. gramm,
  98. result
  99. );
  100. std::cout << "Parsed data:n";
  101. std::cout << "Result: " << result.number << "n";
  102. for (const auto& p : result.data) {
  103. std::cout << p.first << " = " << p.second << 'n';
  104. }
  105.  
  106. return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement