#pragma once #include "cell.h" #include #include #include #include #include #include #include #include namespace qi = boost::spirit::qi; namespace phoenix = boost::phoenix; typedef boost::spirit::line_pos_iterator pos_iterator_t; typedef std::vector row; typedef std::vector cells; BOOST_FUSION_ADAPT_STRUCT( cell, (Type, type) (Color, color) ) template struct annotation_f { typedef void result_type; annotation_f(It first) : first(first) {} It const first; template void operator()(Val& v, First f, Last l) const { do_annotate(v, f, l, first); } private: void static do_annotate(cell& c, It f, It l, It first) { c.row = get_line(f) - 1; c.column = get_column(first, f) / 2; } static void do_annotate(...) { std::cerr << "(not having LocationInfo)\n"; } }; template struct MinigridParser : qi::grammar { MinigridParser(It first) : MinigridParser::base_type(row_), annotate(first) { using namespace qi; type_.add ("W", Type::Wall) (" ", Type::Floor) ("D", Type::Door) ("L", Type::LockedDoor) ("K", Type::Key) ("A", Type::Ball) ("B", Type::Box) ("G", Type::Goal) ("V", Type::Lava) ("n", Type::SlipperyNorth) ("e", Type::SlipperyEast) ("s", Type::SlipperySouth) ("w", Type::SlipperyWest) ("X", Type::Agent) ("Z", Type::Adversary); color_.add ("R", Color::Red) ("G", Color::Green) ("B", Color::Blue) ("P", Color::Purple) ("Y", Color::Yellow) (" ", Color::None); cell_ = type_ > color_; row_ = (cell_ % -qi::char_("\n")); auto set_location_info = annotate(_val, _1, _3); on_success(cell_, set_location_info); BOOST_SPIRIT_DEBUG_NODE(type_); BOOST_SPIRIT_DEBUG_NODE(color_); BOOST_SPIRIT_DEBUG_NODE(cell_); } private: phoenix::function> annotate; qi::symbols type_; qi::symbols color_; qi::rule cell_; qi::rule row_; }; typedef boost::tokenizer< boost::escaped_list_separator , std::string::const_iterator, std::string> Tokenizer; //std::ostream& operator<<(std::ostream& os, const row& r);