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.

71 lines
1.8 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. struct Configuration
  15. {
  16. expressions expressions_;
  17. std::string derivation_;
  18. Configuration() = default;
  19. Configuration(expressions expressions, std::string derivation) : expressions_(expressions), derivation_(derivation) {}
  20. ~Configuration() = default;
  21. Configuration(const Configuration&) = default;
  22. friend std::ostream& operator << (std::ostream& os, const Configuration& config) {
  23. os << "Configuration" << std::endl;
  24. for (auto& expression : config.expressions_) {
  25. os << "Expression=" << expression << std::endl;
  26. }
  27. return os << "Derviation=" << config.derivation_;
  28. }
  29. };
  30. BOOST_FUSION_ADAPT_STRUCT(
  31. Configuration,
  32. (expressions, expressions_)
  33. (std::string, derivation_)
  34. )
  35. template <typename It>
  36. struct ConfigParser : qi::grammar<It, std::vector<Configuration>>
  37. {
  38. ConfigParser(It first) : ConfigParser::base_type(config_)
  39. {
  40. using namespace qi;
  41. expression_ = +char_("a-zA-Z_0-9");
  42. expressions_ = (expression_ % ',');
  43. row_ = (expressions_ > ';' > expression_);
  44. config_ = (row_ % "\n");
  45. BOOST_SPIRIT_DEBUG_NODE(expression_);
  46. BOOST_SPIRIT_DEBUG_NODE(expressions_);
  47. BOOST_SPIRIT_DEBUG_NODE(config_);
  48. }
  49. private:
  50. qi::rule<It, expressions()> expressions_;
  51. qi::rule<It, std::string()> expression_;
  52. qi::rule<It, Configuration()> row_;
  53. qi::rule<It, std::vector<Configuration>> config_;
  54. };