9
3

scanner.hpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*=============================================================================
  2. Copyright (c) 1998-2002 Joel de Guzman
  3. http://spirit.sourceforge.net/
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. =============================================================================*/
  7. #if !defined(BOOST_SPIRIT_SCANNER_HPP)
  8. #define BOOST_SPIRIT_SCANNER_HPP
  9. #include <iterator>
  10. #include <boost/config.hpp>
  11. #include <boost/spirit/home/classic/namespace.hpp>
  12. #include <boost/spirit/home/classic/core/match.hpp>
  13. #include <boost/spirit/home/classic/core/non_terminal/parser_id.hpp>
  14. #include <boost/spirit/home/classic/core/scanner/scanner_fwd.hpp>
  15. namespace boost { namespace spirit {
  16. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  17. ///////////////////////////////////////////////////////////////////////////
  18. //
  19. // iteration_policy class
  20. //
  21. ///////////////////////////////////////////////////////////////////////////
  22. struct iteration_policy
  23. {
  24. template <typename ScannerT>
  25. void
  26. advance(ScannerT const& scan) const
  27. {
  28. ++scan.first;
  29. }
  30. template <typename ScannerT>
  31. bool at_end(ScannerT const& scan) const
  32. {
  33. return scan.first == scan.last;
  34. }
  35. template <typename T>
  36. T filter(T ch) const
  37. {
  38. return ch;
  39. }
  40. template <typename ScannerT>
  41. typename ScannerT::ref_t
  42. get(ScannerT const& scan) const
  43. {
  44. return *scan.first;
  45. }
  46. };
  47. ///////////////////////////////////////////////////////////////////////////
  48. //
  49. // match_policy class
  50. //
  51. ///////////////////////////////////////////////////////////////////////////
  52. struct match_policy
  53. {
  54. template <typename T>
  55. struct result { typedef match<T> type; };
  56. const match<nil_t>
  57. no_match() const
  58. {
  59. return match<nil_t>();
  60. }
  61. const match<nil_t>
  62. empty_match() const
  63. {
  64. return match<nil_t>(0, nil_t());
  65. }
  66. template <typename AttrT, typename IteratorT>
  67. match<AttrT>
  68. create_match(
  69. std::size_t length,
  70. AttrT const& val,
  71. IteratorT const& /*first*/,
  72. IteratorT const& /*last*/) const
  73. {
  74. return match<AttrT>(length, val);
  75. }
  76. template <typename MatchT, typename IteratorT>
  77. void group_match(
  78. MatchT& /*m*/,
  79. parser_id const& /*id*/,
  80. IteratorT const& /*first*/,
  81. IteratorT const& /*last*/) const {}
  82. template <typename Match1T, typename Match2T>
  83. void concat_match(Match1T& l, Match2T const& r) const
  84. {
  85. l.concat(r);
  86. }
  87. };
  88. ///////////////////////////////////////////////////////////////////////////
  89. //
  90. // match_result class
  91. //
  92. ///////////////////////////////////////////////////////////////////////////
  93. template <typename MatchPolicyT, typename T>
  94. struct match_result
  95. {
  96. typedef typename MatchPolicyT::template result<T>::type type;
  97. };
  98. ///////////////////////////////////////////////////////////////////////////
  99. //
  100. // action_policy class
  101. //
  102. ///////////////////////////////////////////////////////////////////////////
  103. template <typename AttrT>
  104. struct attributed_action_policy
  105. {
  106. template <typename ActorT, typename IteratorT>
  107. static void
  108. call(
  109. ActorT const& actor,
  110. AttrT& val,
  111. IteratorT const&,
  112. IteratorT const&)
  113. {
  114. actor(val);
  115. }
  116. };
  117. //////////////////////////////////
  118. template <>
  119. struct attributed_action_policy<nil_t>
  120. {
  121. template <typename ActorT, typename IteratorT>
  122. static void
  123. call(
  124. ActorT const& actor,
  125. nil_t,
  126. IteratorT const& first,
  127. IteratorT const& last)
  128. {
  129. actor(first, last);
  130. }
  131. };
  132. //////////////////////////////////
  133. struct action_policy
  134. {
  135. template <typename ActorT, typename AttrT, typename IteratorT>
  136. void
  137. do_action(
  138. ActorT const& actor,
  139. AttrT& val,
  140. IteratorT const& first,
  141. IteratorT const& last) const
  142. {
  143. attributed_action_policy<AttrT>::call(actor, val, first, last);
  144. }
  145. };
  146. ///////////////////////////////////////////////////////////////////////////
  147. //
  148. // scanner_policies class
  149. //
  150. ///////////////////////////////////////////////////////////////////////////
  151. template <
  152. typename IterationPolicyT,
  153. typename MatchPolicyT,
  154. typename ActionPolicyT>
  155. struct scanner_policies :
  156. public IterationPolicyT,
  157. public MatchPolicyT,
  158. public ActionPolicyT
  159. {
  160. typedef IterationPolicyT iteration_policy_t;
  161. typedef MatchPolicyT match_policy_t;
  162. typedef ActionPolicyT action_policy_t;
  163. scanner_policies(
  164. IterationPolicyT const& i_policy = IterationPolicyT(),
  165. MatchPolicyT const& m_policy = MatchPolicyT(),
  166. ActionPolicyT const& a_policy = ActionPolicyT())
  167. : IterationPolicyT(i_policy)
  168. , MatchPolicyT(m_policy)
  169. , ActionPolicyT(a_policy) {}
  170. template <typename ScannerPoliciesT>
  171. scanner_policies(ScannerPoliciesT const& policies)
  172. : IterationPolicyT(policies)
  173. , MatchPolicyT(policies)
  174. , ActionPolicyT(policies) {}
  175. };
  176. ///////////////////////////////////////////////////////////////////////////
  177. //
  178. // scanner_policies_base class: the base class of all scanners
  179. //
  180. ///////////////////////////////////////////////////////////////////////////
  181. struct scanner_base {};
  182. ///////////////////////////////////////////////////////////////////////////
  183. //
  184. // scanner class
  185. //
  186. ///////////////////////////////////////////////////////////////////////////
  187. template <
  188. typename IteratorT,
  189. typename PoliciesT>
  190. class scanner : public PoliciesT, public scanner_base
  191. {
  192. public:
  193. typedef IteratorT iterator_t;
  194. typedef PoliciesT policies_t;
  195. typedef typename std::
  196. iterator_traits<IteratorT>::value_type value_t;
  197. typedef typename std::
  198. iterator_traits<IteratorT>::reference ref_t;
  199. typedef typename boost::
  200. call_traits<IteratorT>::param_type iter_param_t;
  201. scanner(
  202. IteratorT& first_,
  203. iter_param_t last_,
  204. PoliciesT const& policies = PoliciesT())
  205. : PoliciesT(policies), first(first_), last(last_)
  206. {
  207. at_end();
  208. }
  209. scanner(scanner const& other)
  210. : PoliciesT(other), first(other.first), last(other.last) {}
  211. scanner(scanner const& other, IteratorT& first_)
  212. : PoliciesT(other), first(first_), last(other.last) {}
  213. template <typename PoliciesT1>
  214. scanner(scanner<IteratorT, PoliciesT1> const& other)
  215. : PoliciesT(other), first(other.first), last(other.last) {}
  216. bool
  217. at_end() const
  218. {
  219. typedef typename PoliciesT::iteration_policy_t iteration_policy_type;
  220. return iteration_policy_type::at_end(*this);
  221. }
  222. value_t
  223. operator*() const
  224. {
  225. typedef typename PoliciesT::iteration_policy_t iteration_policy_type;
  226. return iteration_policy_type::filter(iteration_policy_type::get(*this));
  227. }
  228. scanner const&
  229. operator++() const
  230. {
  231. typedef typename PoliciesT::iteration_policy_t iteration_policy_type;
  232. iteration_policy_type::advance(*this);
  233. return *this;
  234. }
  235. template <typename PoliciesT2>
  236. struct rebind_policies
  237. {
  238. typedef scanner<IteratorT, PoliciesT2> type;
  239. };
  240. template <typename PoliciesT2>
  241. scanner<IteratorT, PoliciesT2>
  242. change_policies(PoliciesT2 const& policies) const
  243. {
  244. return scanner<IteratorT, PoliciesT2>(first, last, policies);
  245. }
  246. template <typename IteratorT2>
  247. struct rebind_iterator
  248. {
  249. typedef scanner<IteratorT2, PoliciesT> type;
  250. };
  251. template <typename IteratorT2>
  252. scanner<IteratorT2, PoliciesT>
  253. change_iterator(IteratorT2 const& first_, IteratorT2 const &last_) const
  254. {
  255. return scanner<IteratorT2, PoliciesT>(first_, last_, *this);
  256. }
  257. IteratorT& first;
  258. IteratorT const last;
  259. private:
  260. scanner&
  261. operator=(scanner const& other);
  262. };
  263. ///////////////////////////////////////////////////////////////////////////
  264. //
  265. // rebind_scanner_policies class
  266. //
  267. ///////////////////////////////////////////////////////////////////////////
  268. template <typename ScannerT, typename PoliciesT>
  269. struct rebind_scanner_policies
  270. {
  271. typedef typename ScannerT::template
  272. rebind_policies<PoliciesT>::type type;
  273. };
  274. //////////////////////////////////
  275. template <typename ScannerT, typename IteratorT>
  276. struct rebind_scanner_iterator
  277. {
  278. typedef typename ScannerT::template
  279. rebind_iterator<IteratorT>::type type;
  280. };
  281. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  282. }}
  283. #endif