stt_grammar.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // Copyright 2008 Christophe Henry
  2. // henry UNDERSCORE christophe AT hotmail DOT com
  3. // This is an extended version of the state machine available in the boost::mpl library
  4. // Distributed under the same license as the original.
  5. // Copyright for the original version:
  6. // Copyright 2005 David Abrahams and Aleksey Gurtovoy. Distributed
  7. // under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_MSM_FRONT_EUML_STT_GRAMMAR_H
  11. #define BOOST_MSM_FRONT_EUML_STT_GRAMMAR_H
  12. #include <boost/msm/front/euml/common.hpp>
  13. #include <boost/mpl/vector.hpp>
  14. #include <boost/mpl/eval_if.hpp>
  15. #include <boost/msm/front/euml/operator.hpp>
  16. #include <boost/msm/front/euml/guard_grammar.hpp>
  17. #include <boost/msm/front/euml/state_grammar.hpp>
  18. namespace proto = boost::proto;
  19. namespace boost { namespace msm { namespace front { namespace euml
  20. {
  21. template <class SOURCE,class EVENT,class TARGET,class ACTION=none,class GUARD=none>
  22. struct TempRow
  23. {
  24. typedef SOURCE Source;
  25. typedef EVENT Evt;
  26. typedef TARGET Target;
  27. typedef ACTION Action;
  28. typedef GUARD Guard;
  29. };
  30. template <class TEMP_ROW>
  31. struct convert_to_row
  32. {
  33. typedef Row<typename TEMP_ROW::Source,typename TEMP_ROW::Evt,typename TEMP_ROW::Target,
  34. typename TEMP_ROW::Action,typename TEMP_ROW::Guard> type;
  35. };
  36. template <class TEMP_ROW>
  37. struct convert_to_internal_row
  38. {
  39. typedef Internal<typename TEMP_ROW::Evt,
  40. typename TEMP_ROW::Action,typename TEMP_ROW::Guard> type;
  41. };
  42. // explicit + fork + entry point + exit point grammar
  43. struct BuildEntry
  44. : proto::or_<
  45. proto::when<
  46. proto::function<proto::terminal<proto::_>,proto::terminal<state_tag>,proto::terminal<state_tag> >,
  47. get_fct<proto::_child_c<0>,get_state_name<proto::_child_c<1>() >(),get_state_name<proto::_child_c<2>() >() >()
  48. >
  49. >
  50. {};
  51. // row grammar
  52. struct BuildNextStates
  53. : proto::or_<
  54. proto::when<
  55. proto::terminal<state_tag>,
  56. get_state_name<proto::_>()
  57. >,
  58. proto::when<
  59. BuildEntry,
  60. BuildEntry
  61. >,
  62. proto::when<
  63. proto::comma<BuildEntry,BuildEntry >,
  64. ::boost::mpl::push_back<
  65. make_vector_one_row<BuildEntry(proto::_left)>(),
  66. BuildEntry(proto::_right)>()
  67. >,
  68. proto::when <
  69. proto::comma<BuildNextStates,BuildEntry >,
  70. ::boost::mpl::push_back<
  71. BuildNextStates(proto::_left),
  72. BuildEntry(proto::_right) >()
  73. >
  74. >
  75. {};
  76. template <class EventGuard,class ActionClass>
  77. struct fusion_event_action_guard
  78. {
  79. typedef TempRow<none,typename EventGuard::Evt,none,typename ActionClass::Action,typename EventGuard::Guard> type;
  80. };
  81. template <class SourceGuard,class ActionClass>
  82. struct fusion_source_action_guard
  83. {
  84. typedef TempRow<typename SourceGuard::Source,none,none,typename ActionClass::Action,typename SourceGuard::Guard> type;
  85. };
  86. template <class SourceClass,class EventClass>
  87. struct fusion_source_event_action_guard
  88. {
  89. typedef TempRow<typename SourceClass::Source,typename EventClass::Evt,
  90. none,typename EventClass::Action,typename EventClass::Guard> type;
  91. };
  92. template <class Left,class Right>
  93. struct fusion_left_right
  94. {
  95. typedef TempRow<typename Right::Source,typename Right::Evt,typename Left::Target
  96. ,typename Right::Action,typename Right::Guard> type;
  97. };
  98. struct BuildEventPlusGuard
  99. : proto::or_<
  100. proto::when<
  101. proto::subscript<proto::terminal<event_tag>, GuardGrammar >,
  102. TempRow<none,proto::_left,none,none, GuardGrammar(proto::_right)>(proto::_right)
  103. >
  104. >
  105. {};
  106. struct BuildSourceState
  107. : proto::or_<
  108. proto::when<
  109. proto::terminal<state_tag>,
  110. get_state_name<proto::_>()
  111. >,
  112. proto::when<
  113. BuildEntry,
  114. BuildEntry
  115. >
  116. >
  117. {};
  118. struct BuildSourcePlusGuard
  119. : proto::when<
  120. proto::subscript<BuildSourceState,GuardGrammar >,
  121. TempRow<BuildSourceState(proto::_left),none,none,none,GuardGrammar(proto::_right)>(proto::_right)
  122. >
  123. {};
  124. struct BuildEvent
  125. : proto::or_<
  126. // just event without guard/action
  127. proto::when<
  128. proto::terminal<event_tag>,
  129. TempRow<none,proto::_,none>() >
  130. // event / action
  131. , proto::when<
  132. proto::divides<proto::terminal<event_tag>,ActionGrammar >,
  133. TempRow<none,proto::_left,none,ActionGrammar(proto::_right) >(proto::_right) >
  134. // event [ guard ]
  135. , proto::when<
  136. proto::subscript<proto::terminal<event_tag>,GuardGrammar >,
  137. TempRow<none,proto::_left,none,none,GuardGrammar(proto::_right)>(proto::_right) >
  138. // event [ guard ] / action
  139. , proto::when<
  140. proto::divides<BuildEventPlusGuard, ActionGrammar>,
  141. fusion_event_action_guard<BuildEventPlusGuard(proto::_left),
  142. TempRow<none,none,none,ActionGrammar(proto::_right)>(proto::_right)
  143. >()
  144. >
  145. >
  146. {};
  147. struct BuildSource
  148. : proto::or_<
  149. // after == if just state without event or guard/action
  150. proto::when<
  151. BuildSourceState,
  152. TempRow<BuildSourceState(proto::_),none,none>() >
  153. // == source / action
  154. , proto::when<
  155. proto::divides<BuildSourceState,ActionGrammar >,
  156. TempRow<BuildSourceState(proto::_left),none,none,ActionGrammar(proto::_right) >(proto::_right) >
  157. // == source [ guard ]
  158. , proto::when<
  159. proto::subscript<BuildSourceState,GuardGrammar >,
  160. TempRow<BuildSourceState(proto::_left),none,none,none,GuardGrammar(proto::_right)>(proto::_right) >
  161. // == source [ guard ] / action
  162. , proto::when<
  163. proto::divides<BuildSourcePlusGuard,
  164. ActionGrammar >,
  165. fusion_source_action_guard<BuildSourcePlusGuard(proto::_left),
  166. TempRow<none,none,none,ActionGrammar(proto::_right)>(proto::_right)
  167. >()
  168. >
  169. >
  170. {};
  171. struct BuildRight
  172. : proto::or_<
  173. proto::when<
  174. proto::plus<BuildSource,BuildEvent >,
  175. fusion_source_event_action_guard<BuildSource(proto::_left),BuildEvent(proto::_right)>()
  176. >,
  177. proto::when<
  178. BuildSource,
  179. BuildSource
  180. >
  181. >
  182. {};
  183. struct BuildRow
  184. : proto::or_<
  185. // grammar 1
  186. proto::when<
  187. proto::equal_to<BuildNextStates,BuildRight >,
  188. convert_to_row<
  189. fusion_left_right<TempRow<none,none,BuildNextStates(proto::_left)>,BuildRight(proto::_right)> >()
  190. >,
  191. // internal events
  192. proto::when<
  193. BuildRight,
  194. convert_to_row<
  195. fusion_left_right<TempRow<none,none,none>,BuildRight(proto::_)> >()
  196. >,
  197. // grammar 2
  198. proto::when<
  199. proto::equal_to<BuildRight,BuildNextStates>,
  200. convert_to_row<
  201. fusion_left_right<TempRow<none,none,BuildNextStates(proto::_right)>,BuildRight(proto::_left)> >()
  202. >
  203. >
  204. {};
  205. // stt grammar
  206. struct BuildStt
  207. : proto::or_<
  208. proto::when<
  209. proto::comma<BuildStt,BuildStt>,
  210. boost::mpl::push_back<BuildStt(proto::_left),BuildRow(proto::_right)>()
  211. >,
  212. proto::when <
  213. BuildRow,
  214. make_vector_one_row<BuildRow(proto::_)>()
  215. >
  216. >
  217. {};
  218. template <class Expr>
  219. typename ::boost::mpl::eval_if<
  220. typename proto::matches<Expr,BuildStt>::type,
  221. boost::result_of<BuildStt(Expr)>,
  222. make_invalid_type>::type
  223. build_stt(Expr const&)
  224. {
  225. return typename boost::result_of<BuildStt(Expr)>::type();
  226. }
  227. // internal stt grammar
  228. struct BuildInternalRow
  229. : proto::when<
  230. BuildEvent,
  231. convert_to_internal_row<
  232. fusion_left_right<TempRow<none,none,none>,BuildEvent(proto::_)> >()
  233. >
  234. {};
  235. struct BuildInternalStt
  236. : proto::or_<
  237. proto::when<
  238. proto::comma<BuildInternalStt,BuildInternalStt>,
  239. boost::mpl::push_back<BuildInternalStt(proto::_left),BuildInternalRow(proto::_right)>()
  240. >,
  241. proto::when <
  242. BuildInternalRow,
  243. make_vector_one_row<BuildInternalRow(proto::_)>()
  244. >
  245. >
  246. {};
  247. template <class Expr>
  248. typename ::boost::mpl::eval_if<
  249. typename proto::matches<Expr,BuildInternalStt>::type,
  250. boost::result_of<BuildInternalStt(Expr)>,
  251. make_invalid_type>::type
  252. build_internal_stt(Expr const&)
  253. {
  254. return typename boost::result_of<BuildInternalStt(Expr)>::type();
  255. }
  256. }}}}
  257. #endif //BOOST_MSM_FRONT_EUML_STT_GRAMMAR_H