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.

88 lines
2.4 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 std::vector<std::string> expressions;
  14. enum class ConfigType : char {
  15. Label = 'L',
  16. Formula = 'F',
  17. };
  18. struct Configuration
  19. {
  20. expressions expressions_;
  21. std::string derivation_;
  22. ConfigType type_ {ConfigType::Label};
  23. Configuration() = default;
  24. Configuration(expressions expressions, std::string derivation, ConfigType type) : expressions_(expressions), derivation_(derivation), type_(type) {}
  25. ~Configuration() = default;
  26. Configuration(const Configuration&) = default;
  27. friend std::ostream& operator << (std::ostream& os, const Configuration& config) {
  28. os << "Configuration with Type: " << static_cast<char>(config.type_) << std::endl;
  29. for (auto& expression : config.expressions_) {
  30. os << "\tExpression=" << expression << std::endl;
  31. }
  32. return os << "\tDerviation=" << config.derivation_;
  33. }
  34. };
  35. BOOST_FUSION_ADAPT_STRUCT(
  36. Configuration,
  37. (ConfigType, type_)
  38. (expressions, expressions_)
  39. (std::string, derivation_)
  40. )
  41. template <typename It>
  42. struct ConfigParser : qi::grammar<It, std::vector<Configuration>>
  43. {
  44. ConfigParser(It first) : ConfigParser::base_type(config_)
  45. {
  46. using namespace qi;
  47. //F:(AgentCannotMoveSouth & AgentCannotMoveNorth) | (AgentCannotMoveEast & AgentCannotMoveWest) ;AgentCannotTurn
  48. configType_.add
  49. ("L", ConfigType::Label)
  50. ("F", ConfigType::Formula);
  51. expression_ = -qi::char_('!') > + char_("a-zA-Z_0-9");
  52. expressions_ = (expression_ % ',');
  53. row_ = (configType_ > ':' > expressions_ > ';' > expression_);
  54. // row_ = (expressions_ > ';' > expression_);
  55. config_ = (row_ % "\n");
  56. BOOST_SPIRIT_DEBUG_NODE(configType_);
  57. BOOST_SPIRIT_DEBUG_NODE(expression_);
  58. BOOST_SPIRIT_DEBUG_NODE(expressions_);
  59. BOOST_SPIRIT_DEBUG_NODE(config_);
  60. }
  61. private:
  62. qi::symbols<char, ConfigType> configType_;
  63. qi::rule<It, expressions()> expressions_;
  64. qi::rule<It, std::string()> expression_;
  65. qi::rule<It, Configuration()> row_;
  66. qi::rule<It, std::vector<Configuration>> config_;
  67. };