You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.6 KiB

  1. #pragma once
  2. #include "cell.h"
  3. #include <vector>
  4. #include <boost/tokenizer.hpp>
  5. #include <boost/fusion/adapted/struct.hpp>
  6. #include <boost/spirit/include/qi.hpp>
  7. #include <boost/spirit/include/phoenix.hpp>
  8. #include <boost/spirit/include/phoenix_operator.hpp>
  9. #include <boost/variant/recursive_wrapper.hpp>
  10. #include <boost/spirit/include/support_line_pos_iterator.hpp>
  11. namespace qi = boost::spirit::qi;
  12. namespace phoenix = boost::phoenix;
  13. typedef boost::spirit::line_pos_iterator<std::string::const_iterator> pos_iterator_t;
  14. typedef std::vector<cell> row;
  15. typedef std::vector<cell> cells;
  16. BOOST_FUSION_ADAPT_STRUCT(
  17. cell,
  18. (Type, type)
  19. (Color, color)
  20. )
  21. template<typename It>
  22. struct annotation_f {
  23. typedef void result_type;
  24. annotation_f(It first) : first(first) {}
  25. It const first;
  26. template<typename Val, typename First, typename Last>
  27. void operator()(Val& v, First f, Last l) const {
  28. do_annotate(v, f, l, first);
  29. }
  30. private:
  31. void static do_annotate(cell& c, It f, It l, It first) {
  32. c.row = get_line(f) - 1;
  33. c.column = get_column(first, f) / 2;
  34. }
  35. static void do_annotate(...) { std::cerr << "(not having LocationInfo)\n"; }
  36. };
  37. template <typename It>
  38. struct MinigridParser : qi::grammar<It, row()>
  39. {
  40. MinigridParser(It first) : MinigridParser::base_type(row_), annotate(first)
  41. {
  42. using namespace qi;
  43. type_.add
  44. ("W", Type::Wall)
  45. (" ", Type::Floor)
  46. ("D", Type::Door)
  47. ("L", Type::LockedDoor)
  48. ("K", Type::Key)
  49. ("A", Type::Ball)
  50. ("B", Type::Box)
  51. ("G", Type::Goal)
  52. ("V", Type::Lava)
  53. ("n", Type::SlipperyNorth)
  54. ("e", Type::SlipperyEast)
  55. ("s", Type::SlipperySouth)
  56. ("w", Type::SlipperyWest)
  57. ("X", Type::Agent)
  58. ("Z", Type::Adversary);
  59. color_.add
  60. ("R", Color::Red)
  61. ("G", Color::Green)
  62. ("B", Color::Blue)
  63. ("P", Color::Purple)
  64. ("Y", Color::Yellow)
  65. (" ", Color::None);
  66. cell_ = type_ > color_;
  67. row_ = (cell_ % -qi::char_("\n"));
  68. auto set_location_info = annotate(_val, _1, _3);
  69. on_success(cell_, set_location_info);
  70. BOOST_SPIRIT_DEBUG_NODE(type_);
  71. BOOST_SPIRIT_DEBUG_NODE(color_);
  72. BOOST_SPIRIT_DEBUG_NODE(cell_);
  73. }
  74. private:
  75. phoenix::function<annotation_f<It>> annotate;
  76. qi::symbols<char, Type> type_;
  77. qi::symbols<char, Color> color_;
  78. qi::rule<It, cell()> cell_;
  79. qi::rule<It, row()> row_;
  80. };
  81. typedef boost::tokenizer< boost::escaped_list_separator<char> , std::string::const_iterator, std::string> Tokenizer;
  82. //std::ostream& operator<<(std::ostream& os, const row& r);