fork download
  1. // #define BOOST_SPIRIT_DEBUG
  2. #include <boost/fusion/adapted.hpp>
  3. #include <boost/spirit/include/qi.hpp>
  4. #include <boost/spirit/include/phoenix.hpp>
  5.  
  6. namespace qi = boost::spirit::qi;
  7.  
  8. template <typename T> struct ChainLink;
  9.  
  10. template <typename T>
  11. class SimpleLinkList {
  12. public:
  13. void push_back(T const& v) { /* todo */ _for_debug.push_back(v); }
  14.  
  15. std::list<int> _for_debug;
  16. friend std::ostream& operator<<(std::ostream& os, SimpleLinkList const& list) {
  17. os << "["; std::copy(list._for_debug.begin(), list._for_debug.end(), std::ostream_iterator<T>(os, " ")); return os << "]";
  18. }
  19. private:
  20. ChainLink<T>* head;
  21. };
  22.  
  23. namespace boost { namespace spirit { namespace traits {
  24. template <typename T>
  25. struct container_value<SimpleLinkList<T>, void> {
  26. typedef T type;
  27. };
  28.  
  29. template <typename T>
  30. struct push_back_container<SimpleLinkList<T>, T, void> {
  31. static bool call(SimpleLinkList<T>& c, T const& val) {
  32. c.push_back(val);
  33. return true;
  34. }
  35. };
  36. }}}
  37.  
  38. struct AbstractDataType
  39. {
  40. int number;
  41. SimpleLinkList<int> list1, list2;
  42. };
  43.  
  44. BOOST_FUSION_ADAPT_STRUCT(AbstractDataType, (int, number)(SimpleLinkList<int>, list1)(SimpleLinkList<int>, list2))
  45.  
  46. template<typename Iterator>
  47. struct parser_expression : qi::grammar<Iterator, AbstractDataType(), qi::space_type>
  48. {
  49. parser_expression() : parser_expression::base_type(start)
  50. {
  51. list = '[' >> -(qi::int_ % ',') >> ']';
  52. start = qi::int_ >> list >> -list >> '=';
  53.  
  54. BOOST_SPIRIT_DEBUG_NODES((list)(start))
  55. }
  56.  
  57. qi::rule<Iterator, AbstractDataType(), qi::space_type> start;
  58. qi::rule<Iterator, SimpleLinkList<int>(), qi::space_type> list;
  59. };
  60.  
  61. void test(const std::string input)
  62. {
  63. static const parser_expression<std::string::const_iterator> p;
  64.  
  65. AbstractDataType parsed;
  66. auto f(input.begin()), l(input.end());
  67. bool ok = qi::phrase_parse(f, l, p, qi::space, parsed);
  68.  
  69. if (ok)
  70. std::cout << "Result: " << parsed.number << " " << parsed.list1 << parsed.list2 << "\n";
  71. else
  72. std::cout << "Parse failed\n";
  73.  
  74. if (f!=l)
  75. std::cout << "Unparsed: '" << std::string(f,l) << "'\n";
  76. }
  77.  
  78. int main()
  79. {
  80. test("1 [2, 3, 4] [5, 6] =");
  81. test("2 [] [6, 7] =");
  82. test("3 [4, 5, 6] [ ] =");
  83. test("4 [5, 6, 7] =");
  84. }
  85.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:2:36: fatal error: boost/fusion/adapted.hpp: No such file or directory
compilation terminated.
stdout
Standard output is empty