Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. // spiritvariant.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. struct TRIV
  7. {
  8.     int v;
  9. };
  10.  
  11. struct TRIVT
  12. {
  13.     int v;
  14.     int t;
  15. };
  16.  
  17. struct TRIVN
  18. {
  19.     int v;
  20.     int n;
  21. };
  22.  
  23. BOOST_FUSION_ADAPT_STRUCT(
  24.     TRIV,
  25.     (int, v)
  26. )
  27.  
  28. BOOST_FUSION_ADAPT_STRUCT(
  29.     TRIVT,
  30.     (int, v),
  31.     (int, t)
  32. )
  33.  
  34. BOOST_FUSION_ADAPT_STRUCT(
  35.     TRIVN,
  36.     (int, v),
  37.     (int, n)
  38. )
  39.  
  40. struct TRI
  41. {
  42.     int v = 0;
  43.     int t = 0;
  44.     int n = 0;
  45.  
  46.     TRI() { };
  47.     TRI(int V, int T, int N) : v(V), t(T), n(N) { };
  48.     TRI(TRIV t) : v(t.v) { };
  49.     TRI(TRIVT t) : v(t.v), t(t.t) { };
  50.     TRI(TRIVN t) : v(t.v), n(t.n) { };
  51. };
  52.  
  53. BOOST_FUSION_ADAPT_STRUCT(
  54.     TRI,
  55.     (int, v),
  56.     (int, t),
  57.     (int, n)
  58. )
  59.  
  60. std::ostream& operator<<(std::ostream &out, const TRI &type)
  61. {
  62.     return out << "{ " << type.v << ", " << type.t << ", " << type.n << " }";
  63. }
  64.  
  65. using namespace std;
  66. namespace x3 = boost::spirit::x3;
  67.  
  68. namespace
  69. {
  70.     x3::rule<class tvrule, TRIV>
  71.         tvrule = "tvrule";
  72.     auto const tvrule_def
  73.         = x3::lexeme[x3::int_];
  74.         ;
  75.     BOOST_SPIRIT_DEFINE(tvrule);
  76.  
  77.     x3::rule<class tvtrule, TRIVT>
  78.         tvtrule = "tvtrule";
  79.     auto const tvtrule_def
  80.         = x3::lexeme[x3::int_ >> x3::lit("/") >> x3::int_];
  81.         ;
  82.     BOOST_SPIRIT_DEFINE(tvtrule);
  83.  
  84.     x3::rule<class tvnrule, TRIVN>
  85.         tvnrule = "tvnrule";
  86.     auto const tvnrule_def
  87.         = x3::lexeme[x3::int_ >> x3::lit("//") >> x3::int_];
  88.     BOOST_SPIRIT_DEFINE(tvnrule);
  89.  
  90.     x3::rule<class trrule, TRI>
  91.         trrule = "trrule";
  92.     auto const trrule_def
  93.         = x3::lexeme[x3::int_ >> x3::lit('/') >> x3::int_ >> x3::lit('/') >> x3::int_];
  94.     BOOST_SPIRIT_DEFINE(trrule);
  95.  
  96.     x3::rule<class trule, TRI>
  97.         trule = "trule";
  98.     auto const trule_def
  99.         = tvrule;
  100.     BOOST_SPIRIT_DEFINE(trule);
  101. }
  102.  
  103. int main()
  104. {
  105.     auto in = "1/2/2"s;
  106.  
  107.     auto i = in.begin();
  108.     x3::phrase_parse(i, in.end(), trule
  109.     [
  110.         ([](auto& ctx)
  111.         {
  112.             cout << x3::_attr(ctx) << endl;
  113.         })
  114.     ], x3::blank);
  115.  
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement