regex_actions.hpp 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  1. ///////////////////////////////////////////////////////////////////////////////
  2. /// \file regex_actions.hpp
  3. /// Defines the syntax elements of xpressive's action expressions.
  4. //
  5. // Copyright 2008 Eric Niebler. Distributed under the Boost
  6. // Software License, Version 1.0. (See accompanying file
  7. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. #ifndef BOOST_XPRESSIVE_ACTIONS_HPP_EAN_03_22_2007
  9. #define BOOST_XPRESSIVE_ACTIONS_HPP_EAN_03_22_2007
  10. // MS compatible compilers support #pragma once
  11. #if defined(_MSC_VER)
  12. # pragma once
  13. #endif
  14. #include <boost/config.hpp>
  15. #include <boost/preprocessor/punctuation/comma_if.hpp>
  16. #include <boost/ref.hpp>
  17. #include <boost/mpl/if.hpp>
  18. #include <boost/mpl/or.hpp>
  19. #include <boost/mpl/int.hpp>
  20. #include <boost/mpl/assert.hpp>
  21. #include <boost/noncopyable.hpp>
  22. #include <boost/lexical_cast.hpp>
  23. #include <boost/throw_exception.hpp>
  24. #include <boost/utility/enable_if.hpp>
  25. #include <boost/type_traits/is_same.hpp>
  26. #include <boost/type_traits/is_const.hpp>
  27. #include <boost/type_traits/is_integral.hpp>
  28. #include <boost/type_traits/decay.hpp>
  29. #include <boost/type_traits/remove_cv.hpp>
  30. #include <boost/type_traits/remove_reference.hpp>
  31. #include <boost/range/iterator_range.hpp>
  32. #include <boost/xpressive/detail/detail_fwd.hpp>
  33. #include <boost/xpressive/detail/core/state.hpp>
  34. #include <boost/xpressive/detail/core/matcher/attr_matcher.hpp>
  35. #include <boost/xpressive/detail/core/matcher/attr_end_matcher.hpp>
  36. #include <boost/xpressive/detail/core/matcher/attr_begin_matcher.hpp>
  37. #include <boost/xpressive/detail/core/matcher/predicate_matcher.hpp>
  38. #include <boost/xpressive/detail/utility/ignore_unused.hpp>
  39. #include <boost/xpressive/detail/static/type_traits.hpp>
  40. // These are very often needed by client code.
  41. #include <boost/typeof/std/map.hpp>
  42. #include <boost/typeof/std/string.hpp>
  43. // Doxygen can't handle proto :-(
  44. #ifndef BOOST_XPRESSIVE_DOXYGEN_INVOKED
  45. # include <boost/proto/core.hpp>
  46. # include <boost/proto/transform.hpp>
  47. # include <boost/xpressive/detail/core/matcher/action_matcher.hpp>
  48. #endif
  49. #if BOOST_MSVC
  50. #pragma warning(push)
  51. #pragma warning(disable : 4510) // default constructor could not be generated
  52. #pragma warning(disable : 4512) // assignment operator could not be generated
  53. #pragma warning(disable : 4610) // can never be instantiated - user defined constructor required
  54. #endif
  55. namespace boost { namespace xpressive
  56. {
  57. namespace detail
  58. {
  59. template<typename T, typename U>
  60. struct action_arg
  61. {
  62. typedef T type;
  63. typedef typename add_reference<T>::type reference;
  64. reference cast(void *pv) const
  65. {
  66. return *static_cast<typename remove_reference<T>::type *>(pv);
  67. }
  68. };
  69. template<typename T>
  70. struct value_wrapper
  71. : private noncopyable
  72. {
  73. value_wrapper()
  74. : value()
  75. {}
  76. value_wrapper(T const &t)
  77. : value(t)
  78. {}
  79. T value;
  80. };
  81. struct check_tag
  82. {};
  83. struct BindArg
  84. {
  85. BOOST_PROTO_CALLABLE()
  86. template<typename Sig>
  87. struct result {};
  88. template<typename This, typename MatchResults, typename Expr>
  89. struct result<This(MatchResults, Expr)>
  90. {
  91. typedef Expr type;
  92. };
  93. template<typename MatchResults, typename Expr>
  94. Expr const & operator ()(MatchResults &what, Expr const &expr) const
  95. {
  96. what.let(expr);
  97. return expr;
  98. }
  99. };
  100. struct let_tag
  101. {};
  102. // let(_a = b, _c = d)
  103. struct BindArgs
  104. : proto::function<
  105. proto::terminal<let_tag>
  106. , proto::vararg<
  107. proto::when<
  108. proto::assign<proto::_, proto::_>
  109. , proto::call<BindArg(proto::_data, proto::_)>
  110. >
  111. >
  112. >
  113. {};
  114. struct let_domain
  115. : boost::proto::domain<boost::proto::pod_generator<let_> >
  116. {};
  117. template<typename Expr>
  118. struct let_
  119. {
  120. BOOST_PROTO_BASIC_EXTENDS(Expr, let_<Expr>, let_domain)
  121. BOOST_PROTO_EXTENDS_FUNCTION()
  122. };
  123. template<typename Args, typename BidiIter>
  124. void bind_args(let_<Args> const &args, match_results<BidiIter> &what)
  125. {
  126. BindArgs()(args, 0, what);
  127. }
  128. typedef boost::proto::functional::make_expr<proto::tag::function, proto::default_domain> make_function;
  129. }
  130. namespace op
  131. {
  132. /// \brief \c at is a PolymorphicFunctionObject for indexing into a sequence
  133. struct at
  134. {
  135. BOOST_PROTO_CALLABLE()
  136. template<typename Sig>
  137. struct result {};
  138. template<typename This, typename Cont, typename Idx>
  139. struct result<This(Cont &, Idx)>
  140. {
  141. typedef typename Cont::reference type;
  142. };
  143. template<typename This, typename Cont, typename Idx>
  144. struct result<This(Cont const &, Idx)>
  145. {
  146. typedef typename Cont::const_reference type;
  147. };
  148. template<typename This, typename Cont, typename Idx>
  149. struct result<This(Cont, Idx)>
  150. {
  151. typedef typename Cont::const_reference type;
  152. };
  153. /// \pre \c Cont is a model of RandomAccessSequence
  154. /// \param c The RandomAccessSequence to index into
  155. /// \param idx The index
  156. /// \return <tt>c[idx]</tt>
  157. template<typename Cont, typename Idx>
  158. typename Cont::reference operator()(Cont &c, Idx idx BOOST_PROTO_DISABLE_IF_IS_CONST(Cont)) const
  159. {
  160. return c[idx];
  161. }
  162. /// \overload
  163. ///
  164. template<typename Cont, typename Idx>
  165. typename Cont::const_reference operator()(Cont const &c, Idx idx) const
  166. {
  167. return c[idx];
  168. }
  169. };
  170. /// \brief \c push is a PolymorphicFunctionObject for pushing an element into a container.
  171. struct push
  172. {
  173. BOOST_PROTO_CALLABLE()
  174. typedef void result_type;
  175. /// \param seq The sequence into which the value should be pushed.
  176. /// \param val The value to push into the sequence.
  177. /// \brief Equivalent to <tt>seq.push(val)</tt>.
  178. /// \return \c void
  179. template<typename Sequence, typename Value>
  180. void operator()(Sequence &seq, Value const &val) const
  181. {
  182. seq.push(val);
  183. }
  184. };
  185. /// \brief \c push_back is a PolymorphicFunctionObject for pushing an element into the back of a container.
  186. struct push_back
  187. {
  188. BOOST_PROTO_CALLABLE()
  189. typedef void result_type;
  190. /// \param seq The sequence into which the value should be pushed.
  191. /// \param val The value to push into the sequence.
  192. /// \brief Equivalent to <tt>seq.push_back(val)</tt>.
  193. /// \return \c void
  194. template<typename Sequence, typename Value>
  195. void operator()(Sequence &seq, Value const &val) const
  196. {
  197. seq.push_back(val);
  198. }
  199. };
  200. /// \brief \c push_front is a PolymorphicFunctionObject for pushing an element into the front of a container.
  201. struct push_front
  202. {
  203. BOOST_PROTO_CALLABLE()
  204. typedef void result_type;
  205. /// \param seq The sequence into which the value should be pushed.
  206. /// \param val The value to push into the sequence.
  207. /// \brief Equivalent to <tt>seq.push_front(val)</tt>.
  208. /// \return \c void
  209. template<typename Sequence, typename Value>
  210. void operator()(Sequence &seq, Value const &val) const
  211. {
  212. seq.push_front(val);
  213. }
  214. };
  215. /// \brief \c pop is a PolymorphicFunctionObject for popping an element from a container.
  216. struct pop
  217. {
  218. BOOST_PROTO_CALLABLE()
  219. typedef void result_type;
  220. /// \param seq The sequence from which to pop.
  221. /// \brief Equivalent to <tt>seq.pop()</tt>.
  222. /// \return \c void
  223. template<typename Sequence>
  224. void operator()(Sequence &seq) const
  225. {
  226. seq.pop();
  227. }
  228. };
  229. /// \brief \c pop_back is a PolymorphicFunctionObject for popping an element from the back of a container.
  230. struct pop_back
  231. {
  232. BOOST_PROTO_CALLABLE()
  233. typedef void result_type;
  234. /// \param seq The sequence from which to pop.
  235. /// \brief Equivalent to <tt>seq.pop_back()</tt>.
  236. /// \return \c void
  237. template<typename Sequence>
  238. void operator()(Sequence &seq) const
  239. {
  240. seq.pop_back();
  241. }
  242. };
  243. /// \brief \c pop_front is a PolymorphicFunctionObject for popping an element from the front of a container.
  244. struct pop_front
  245. {
  246. BOOST_PROTO_CALLABLE()
  247. typedef void result_type;
  248. /// \param seq The sequence from which to pop.
  249. /// \brief Equivalent to <tt>seq.pop_front()</tt>.
  250. /// \return \c void
  251. template<typename Sequence>
  252. void operator()(Sequence &seq) const
  253. {
  254. seq.pop_front();
  255. }
  256. };
  257. /// \brief \c front is a PolymorphicFunctionObject for fetching the front element of a container.
  258. struct front
  259. {
  260. BOOST_PROTO_CALLABLE()
  261. template<typename Sig>
  262. struct result {};
  263. template<typename This, typename Sequence>
  264. struct result<This(Sequence)>
  265. {
  266. typedef typename remove_reference<Sequence>::type sequence_type;
  267. typedef
  268. typename mpl::if_c<
  269. is_const<sequence_type>::value
  270. , typename sequence_type::const_reference
  271. , typename sequence_type::reference
  272. >::type
  273. type;
  274. };
  275. /// \param seq The sequence from which to fetch the front.
  276. /// \return <tt>seq.front()</tt>
  277. template<typename Sequence>
  278. typename result<front(Sequence &)>::type operator()(Sequence &seq) const
  279. {
  280. return seq.front();
  281. }
  282. };
  283. /// \brief \c back is a PolymorphicFunctionObject for fetching the back element of a container.
  284. struct back
  285. {
  286. BOOST_PROTO_CALLABLE()
  287. template<typename Sig>
  288. struct result {};
  289. template<typename This, typename Sequence>
  290. struct result<This(Sequence)>
  291. {
  292. typedef typename remove_reference<Sequence>::type sequence_type;
  293. typedef
  294. typename mpl::if_c<
  295. is_const<sequence_type>::value
  296. , typename sequence_type::const_reference
  297. , typename sequence_type::reference
  298. >::type
  299. type;
  300. };
  301. /// \param seq The sequence from which to fetch the back.
  302. /// \return <tt>seq.back()</tt>
  303. template<typename Sequence>
  304. typename result<back(Sequence &)>::type operator()(Sequence &seq) const
  305. {
  306. return seq.back();
  307. }
  308. };
  309. /// \brief \c top is a PolymorphicFunctionObject for fetching the top element of a stack.
  310. struct top
  311. {
  312. BOOST_PROTO_CALLABLE()
  313. template<typename Sig>
  314. struct result {};
  315. template<typename This, typename Sequence>
  316. struct result<This(Sequence)>
  317. {
  318. typedef typename remove_reference<Sequence>::type sequence_type;
  319. typedef
  320. typename mpl::if_c<
  321. is_const<sequence_type>::value
  322. , typename sequence_type::value_type const &
  323. , typename sequence_type::value_type &
  324. >::type
  325. type;
  326. };
  327. /// \param seq The sequence from which to fetch the top.
  328. /// \return <tt>seq.top()</tt>
  329. template<typename Sequence>
  330. typename result<top(Sequence &)>::type operator()(Sequence &seq) const
  331. {
  332. return seq.top();
  333. }
  334. };
  335. /// \brief \c first is a PolymorphicFunctionObject for fetching the first element of a pair.
  336. struct first
  337. {
  338. BOOST_PROTO_CALLABLE()
  339. template<typename Sig>
  340. struct result {};
  341. template<typename This, typename Pair>
  342. struct result<This(Pair)>
  343. {
  344. typedef typename remove_reference<Pair>::type::first_type type;
  345. };
  346. /// \param p The pair from which to fetch the first element.
  347. /// \return <tt>p.first</tt>
  348. template<typename Pair>
  349. typename Pair::first_type operator()(Pair const &p) const
  350. {
  351. return p.first;
  352. }
  353. };
  354. /// \brief \c second is a PolymorphicFunctionObject for fetching the second element of a pair.
  355. struct second
  356. {
  357. BOOST_PROTO_CALLABLE()
  358. template<typename Sig>
  359. struct result {};
  360. template<typename This, typename Pair>
  361. struct result<This(Pair)>
  362. {
  363. typedef typename remove_reference<Pair>::type::second_type type;
  364. };
  365. /// \param p The pair from which to fetch the second element.
  366. /// \return <tt>p.second</tt>
  367. template<typename Pair>
  368. typename Pair::second_type operator()(Pair const &p) const
  369. {
  370. return p.second;
  371. }
  372. };
  373. /// \brief \c matched is a PolymorphicFunctionObject for assessing whether a \c sub_match object
  374. /// matched or not.
  375. struct matched
  376. {
  377. BOOST_PROTO_CALLABLE()
  378. typedef bool result_type;
  379. /// \param sub The \c sub_match object.
  380. /// \return <tt>sub.matched</tt>
  381. template<typename Sub>
  382. bool operator()(Sub const &sub) const
  383. {
  384. return sub.matched;
  385. }
  386. };
  387. /// \brief \c length is a PolymorphicFunctionObject for fetching the length of \c sub_match.
  388. struct length
  389. {
  390. BOOST_PROTO_CALLABLE()
  391. template<typename Sig>
  392. struct result {};
  393. template<typename This, typename Sub>
  394. struct result<This(Sub)>
  395. {
  396. typedef typename remove_reference<Sub>::type::difference_type type;
  397. };
  398. /// \param sub The \c sub_match object.
  399. /// \return <tt>sub.length()</tt>
  400. template<typename Sub>
  401. typename Sub::difference_type operator()(Sub const &sub) const
  402. {
  403. return sub.length();
  404. }
  405. };
  406. /// \brief \c str is a PolymorphicFunctionObject for turning a \c sub_match into an
  407. /// equivalent \c std::string.
  408. struct str
  409. {
  410. BOOST_PROTO_CALLABLE()
  411. template<typename Sig>
  412. struct result {};
  413. template<typename This, typename Sub>
  414. struct result<This(Sub)>
  415. {
  416. typedef typename remove_reference<Sub>::type::string_type type;
  417. };
  418. /// \param sub The \c sub_match object.
  419. /// \return <tt>sub.str()</tt>
  420. template<typename Sub>
  421. typename Sub::string_type operator()(Sub const &sub) const
  422. {
  423. return sub.str();
  424. }
  425. };
  426. // This codifies the return types of the various insert member
  427. // functions found in sequence containers, the 2 flavors of
  428. // associative containers, and strings.
  429. //
  430. /// \brief \c insert is a PolymorphicFunctionObject for inserting a value or a
  431. /// sequence of values into a sequence container, an associative
  432. /// container, or a string.
  433. struct insert
  434. {
  435. BOOST_PROTO_CALLABLE()
  436. /// INTERNAL ONLY
  437. ///
  438. struct detail
  439. {
  440. template<typename Sig, typename EnableIf = void>
  441. struct result_detail
  442. {};
  443. // assoc containers
  444. template<typename This, typename Cont, typename Value>
  445. struct result_detail<This(Cont, Value), void>
  446. {
  447. typedef typename remove_reference<Cont>::type cont_type;
  448. typedef typename remove_reference<Value>::type value_type;
  449. static cont_type &scont_;
  450. static value_type &svalue_;
  451. typedef char yes_type;
  452. typedef char (&no_type)[2];
  453. static yes_type check_insert_return(typename cont_type::iterator);
  454. static no_type check_insert_return(std::pair<typename cont_type::iterator, bool>);
  455. BOOST_STATIC_CONSTANT(bool, is_iterator = (sizeof(yes_type) == sizeof(check_insert_return(scont_.insert(svalue_)))));
  456. typedef
  457. typename mpl::if_c<
  458. is_iterator
  459. , typename cont_type::iterator
  460. , std::pair<typename cont_type::iterator, bool>
  461. >::type
  462. type;
  463. };
  464. // sequence containers, assoc containers, strings
  465. template<typename This, typename Cont, typename It, typename Value>
  466. struct result_detail<This(Cont, It, Value),
  467. typename disable_if<
  468. mpl::or_<
  469. is_integral<typename remove_cv<typename remove_reference<It>::type>::type>
  470. , is_same<
  471. typename remove_cv<typename remove_reference<It>::type>::type
  472. , typename remove_cv<typename remove_reference<Value>::type>::type
  473. >
  474. >
  475. >::type
  476. >
  477. {
  478. typedef typename remove_reference<Cont>::type::iterator type;
  479. };
  480. // strings
  481. template<typename This, typename Cont, typename Size, typename T>
  482. struct result_detail<This(Cont, Size, T),
  483. typename enable_if<
  484. is_integral<typename remove_cv<typename remove_reference<Size>::type>::type>
  485. >::type
  486. >
  487. {
  488. typedef typename remove_reference<Cont>::type &type;
  489. };
  490. // assoc containers
  491. template<typename This, typename Cont, typename It>
  492. struct result_detail<This(Cont, It, It), void>
  493. {
  494. typedef void type;
  495. };
  496. // sequence containers, strings
  497. template<typename This, typename Cont, typename It, typename Size, typename Value>
  498. struct result_detail<This(Cont, It, Size, Value),
  499. typename disable_if<
  500. is_integral<typename remove_cv<typename remove_reference<It>::type>::type>
  501. >::type
  502. >
  503. {
  504. typedef void type;
  505. };
  506. // strings
  507. template<typename This, typename Cont, typename Size, typename A0, typename A1>
  508. struct result_detail<This(Cont, Size, A0, A1),
  509. typename enable_if<
  510. is_integral<typename remove_cv<typename remove_reference<Size>::type>::type>
  511. >::type
  512. >
  513. {
  514. typedef typename remove_reference<Cont>::type &type;
  515. };
  516. // strings
  517. template<typename This, typename Cont, typename Pos0, typename String, typename Pos1, typename Length>
  518. struct result_detail<This(Cont, Pos0, String, Pos1, Length)>
  519. {
  520. typedef typename remove_reference<Cont>::type &type;
  521. };
  522. };
  523. template<typename Sig>
  524. struct result
  525. {
  526. typedef typename detail::result_detail<Sig>::type type;
  527. };
  528. /// \overload
  529. ///
  530. template<typename Cont, typename A0>
  531. typename result<insert(Cont &, A0 const &)>::type
  532. operator()(Cont &cont, A0 const &a0) const
  533. {
  534. return cont.insert(a0);
  535. }
  536. /// \overload
  537. ///
  538. template<typename Cont, typename A0, typename A1>
  539. typename result<insert(Cont &, A0 const &, A1 const &)>::type
  540. operator()(Cont &cont, A0 const &a0, A1 const &a1) const
  541. {
  542. return cont.insert(a0, a1);
  543. }
  544. /// \overload
  545. ///
  546. template<typename Cont, typename A0, typename A1, typename A2>
  547. typename result<insert(Cont &, A0 const &, A1 const &, A2 const &)>::type
  548. operator()(Cont &cont, A0 const &a0, A1 const &a1, A2 const &a2) const
  549. {
  550. return cont.insert(a0, a1, a2);
  551. }
  552. /// \param cont The container into which to insert the element(s)
  553. /// \param a0 A value, iterator, or count
  554. /// \param a1 A value, iterator, string, count, or character
  555. /// \param a2 A value, iterator, or count
  556. /// \param a3 A count
  557. /// \return \li For the form <tt>insert()(cont, a0)</tt>, return <tt>cont.insert(a0)</tt>.
  558. /// \li For the form <tt>insert()(cont, a0, a1)</tt>, return <tt>cont.insert(a0, a1)</tt>.
  559. /// \li For the form <tt>insert()(cont, a0, a1, a2)</tt>, return <tt>cont.insert(a0, a1, a2)</tt>.
  560. /// \li For the form <tt>insert()(cont, a0, a1, a2, a3)</tt>, return <tt>cont.insert(a0, a1, a2, a3)</tt>.
  561. template<typename Cont, typename A0, typename A1, typename A2, typename A3>
  562. typename result<insert(Cont &, A0 const &, A1 const &, A2 const &, A3 const &)>::type
  563. operator()(Cont &cont, A0 const &a0, A1 const &a1, A2 const &a2, A3 const &a3) const
  564. {
  565. return cont.insert(a0, a1, a2, a3);
  566. }
  567. };
  568. /// \brief \c make_pair is a PolymorphicFunctionObject for building a \c std::pair out of two parameters
  569. struct make_pair
  570. {
  571. BOOST_PROTO_CALLABLE()
  572. template<typename Sig>
  573. struct result {};
  574. template<typename This, typename First, typename Second>
  575. struct result<This(First, Second)>
  576. {
  577. /// \brief For exposition only
  578. typedef typename decay<First>::type first_type;
  579. /// \brief For exposition only
  580. typedef typename decay<Second>::type second_type;
  581. typedef std::pair<first_type, second_type> type;
  582. };
  583. /// \param first The first element of the pair
  584. /// \param second The second element of the pair
  585. /// \return <tt>std::make_pair(first, second)</tt>
  586. template<typename First, typename Second>
  587. std::pair<First, Second> operator()(First const &first, Second const &second) const
  588. {
  589. return std::make_pair(first, second);
  590. }
  591. };
  592. /// \brief \c as\<\> is a PolymorphicFunctionObject for lexically casting a parameter to a different type.
  593. /// \tparam T The type to which to lexically cast the parameter.
  594. template<typename T>
  595. struct as
  596. {
  597. BOOST_PROTO_CALLABLE()
  598. typedef T result_type;
  599. /// \param val The value to lexically cast.
  600. /// \return <tt>boost::lexical_cast\<T\>(val)</tt>
  601. template<typename Value>
  602. T operator()(Value const &val) const
  603. {
  604. return boost::lexical_cast<T>(val);
  605. }
  606. // Hack around some limitations in boost::lexical_cast
  607. /// INTERNAL ONLY
  608. T operator()(csub_match const &val) const
  609. {
  610. return val.matched
  611. ? boost::lexical_cast<T>(boost::make_iterator_range(val.first, val.second))
  612. : boost::lexical_cast<T>("");
  613. }
  614. #ifndef BOOST_XPRESSIVE_NO_WREGEX
  615. /// INTERNAL ONLY
  616. T operator()(wcsub_match const &val) const
  617. {
  618. return val.matched
  619. ? boost::lexical_cast<T>(boost::make_iterator_range(val.first, val.second))
  620. : boost::lexical_cast<T>("");
  621. }
  622. #endif
  623. /// INTERNAL ONLY
  624. template<typename BidiIter>
  625. T operator()(sub_match<BidiIter> const &val) const
  626. {
  627. // If this assert fires, you're trying to coerce a sequences of non-characters
  628. // to some other type. Xpressive doesn't know how to do that.
  629. typedef typename iterator_value<BidiIter>::type char_type;
  630. BOOST_MPL_ASSERT_MSG(
  631. (xpressive::detail::is_char<char_type>::value)
  632. , CAN_ONLY_CONVERT_FROM_CHARACTER_SEQUENCES
  633. , (char_type)
  634. );
  635. return this->impl(val, xpressive::detail::is_string_iterator<BidiIter>());
  636. }
  637. private:
  638. /// INTERNAL ONLY
  639. template<typename RandIter>
  640. T impl(sub_match<RandIter> const &val, mpl::true_) const
  641. {
  642. return val.matched
  643. ? boost::lexical_cast<T>(boost::make_iterator_range(&*val.first, &*val.first + (val.second - val.first)))
  644. : boost::lexical_cast<T>("");
  645. }
  646. /// INTERNAL ONLY
  647. template<typename BidiIter>
  648. T impl(sub_match<BidiIter> const &val, mpl::false_) const
  649. {
  650. return boost::lexical_cast<T>(val.str());
  651. }
  652. };
  653. /// \brief \c static_cast_\<\> is a PolymorphicFunctionObject for statically casting a parameter to a different type.
  654. /// \tparam T The type to which to statically cast the parameter.
  655. template<typename T>
  656. struct static_cast_
  657. {
  658. BOOST_PROTO_CALLABLE()
  659. typedef T result_type;
  660. /// \param val The value to statically cast.
  661. /// \return <tt>static_cast\<T\>(val)</tt>
  662. template<typename Value>
  663. T operator()(Value const &val) const
  664. {
  665. return static_cast<T>(val);
  666. }
  667. };
  668. /// \brief \c dynamic_cast_\<\> is a PolymorphicFunctionObject for dynamically casting a parameter to a different type.
  669. /// \tparam T The type to which to dynamically cast the parameter.
  670. template<typename T>
  671. struct dynamic_cast_
  672. {
  673. BOOST_PROTO_CALLABLE()
  674. typedef T result_type;
  675. /// \param val The value to dynamically cast.
  676. /// \return <tt>dynamic_cast\<T\>(val)</tt>
  677. template<typename Value>
  678. T operator()(Value const &val) const
  679. {
  680. return dynamic_cast<T>(val);
  681. }
  682. };
  683. /// \brief \c const_cast_\<\> is a PolymorphicFunctionObject for const-casting a parameter to a cv qualification.
  684. /// \tparam T The type to which to const-cast the parameter.
  685. template<typename T>
  686. struct const_cast_
  687. {
  688. BOOST_PROTO_CALLABLE()
  689. typedef T result_type;
  690. /// \param val The value to const-cast.
  691. /// \pre Types \c T and \c Value differ only in cv-qualification.
  692. /// \return <tt>const_cast\<T\>(val)</tt>
  693. template<typename Value>
  694. T operator()(Value const &val) const
  695. {
  696. return const_cast<T>(val);
  697. }
  698. };
  699. /// \brief \c construct\<\> is a PolymorphicFunctionObject for constructing a new object.
  700. /// \tparam T The type of the object to construct.
  701. template<typename T>
  702. struct construct
  703. {
  704. BOOST_PROTO_CALLABLE()
  705. typedef T result_type;
  706. /// \overload
  707. T operator()() const
  708. {
  709. return T();
  710. }
  711. /// \overload
  712. template<typename A0>
  713. T operator()(A0 const &a0) const
  714. {
  715. return T(a0);
  716. }
  717. /// \overload
  718. template<typename A0, typename A1>
  719. T operator()(A0 const &a0, A1 const &a1) const
  720. {
  721. return T(a0, a1);
  722. }
  723. /// \param a0 The first argument to the constructor
  724. /// \param a1 The second argument to the constructor
  725. /// \param a2 The third argument to the constructor
  726. /// \return <tt>T(a0,a1,...)</tt>
  727. template<typename A0, typename A1, typename A2>
  728. T operator()(A0 const &a0, A1 const &a1, A2 const &a2) const
  729. {
  730. return T(a0, a1, a2);
  731. }
  732. };
  733. /// \brief \c throw_\<\> is a PolymorphicFunctionObject for throwing an exception.
  734. /// \tparam Except The type of the object to throw.
  735. template<typename Except>
  736. struct throw_
  737. {
  738. BOOST_PROTO_CALLABLE()
  739. typedef void result_type;
  740. /// \overload
  741. void operator()() const
  742. {
  743. BOOST_THROW_EXCEPTION(Except());
  744. }
  745. /// \overload
  746. template<typename A0>
  747. void operator()(A0 const &a0) const
  748. {
  749. BOOST_THROW_EXCEPTION(Except(a0));
  750. }
  751. /// \overload
  752. template<typename A0, typename A1>
  753. void operator()(A0 const &a0, A1 const &a1) const
  754. {
  755. BOOST_THROW_EXCEPTION(Except(a0, a1));
  756. }
  757. /// \param a0 The first argument to the constructor
  758. /// \param a1 The second argument to the constructor
  759. /// \param a2 The third argument to the constructor
  760. /// \throw <tt>Except(a0,a1,...)</tt>
  761. /// \note This function makes use of the \c BOOST_THROW_EXCEPTION macro
  762. /// to actually throw the exception. See the documentation for the
  763. /// Boost.Exception library.
  764. template<typename A0, typename A1, typename A2>
  765. void operator()(A0 const &a0, A1 const &a1, A2 const &a2) const
  766. {
  767. BOOST_THROW_EXCEPTION(Except(a0, a1, a2));
  768. }
  769. };
  770. /// \brief \c unwrap_reference is a PolymorphicFunctionObject for unwrapping a <tt>boost::reference_wrapper\<\></tt>.
  771. struct unwrap_reference
  772. {
  773. BOOST_PROTO_CALLABLE()
  774. template<typename Sig>
  775. struct result {};
  776. template<typename This, typename Ref>
  777. struct result<This(Ref)>
  778. {
  779. typedef typename boost::unwrap_reference<Ref>::type &type;
  780. };
  781. template<typename This, typename Ref>
  782. struct result<This(Ref &)>
  783. {
  784. typedef typename boost::unwrap_reference<Ref>::type &type;
  785. };
  786. /// \param r The <tt>boost::reference_wrapper\<T\></tt> to unwrap.
  787. /// \return <tt>static_cast\<T &\>(r)</tt>
  788. template<typename T>
  789. T &operator()(boost::reference_wrapper<T> r) const
  790. {
  791. return static_cast<T &>(r);
  792. }
  793. };
  794. }
  795. /// \brief A unary metafunction that turns an ordinary function object type into the type of
  796. /// a deferred function object for use in xpressive semantic actions.
  797. ///
  798. /// Use \c xpressive::function\<\> to turn an ordinary polymorphic function object type
  799. /// into a type that can be used to declare an object for use in xpressive semantic actions.
  800. ///
  801. /// For example, the global object \c xpressive::push_back can be used to create deferred actions
  802. /// that have the effect of pushing a value into a container. It is defined with
  803. /// \c xpressive::function\<\> as follows:
  804. ///
  805. /** \code
  806. xpressive::function<xpressive::op::push_back>::type const push_back = {};
  807. \endcode
  808. */
  809. ///
  810. /// where \c op::push_back is an ordinary function object that pushes its second argument into
  811. /// its first. Thus defined, \c xpressive::push_back can be used in semantic actions as follows:
  812. ///
  813. /** \code
  814. namespace xp = boost::xpressive;
  815. using xp::_;
  816. std::list<int> result;
  817. std::string str("1 23 456 7890");
  818. xp::sregex rx = (+_d)[ xp::push_back(xp::ref(result), xp::as<int>(_) ]
  819. >> *(' ' >> (+_d)[ xp::push_back(xp::ref(result), xp::as<int>(_) ) ]);
  820. \endcode
  821. */
  822. template<typename PolymorphicFunctionObject>
  823. struct function
  824. {
  825. typedef typename proto::terminal<PolymorphicFunctionObject>::type type;
  826. };
  827. /// \brief \c at is a lazy PolymorphicFunctionObject for indexing into a sequence in an
  828. /// xpressive semantic action.
  829. function<op::at>::type const at = {{}};
  830. /// \brief \c push is a lazy PolymorphicFunctionObject for pushing a value into a container in an
  831. /// xpressive semantic action.
  832. function<op::push>::type const push = {{}};
  833. /// \brief \c push_back is a lazy PolymorphicFunctionObject for pushing a value into a container in an
  834. /// xpressive semantic action.
  835. function<op::push_back>::type const push_back = {{}};
  836. /// \brief \c push_front is a lazy PolymorphicFunctionObject for pushing a value into a container in an
  837. /// xpressive semantic action.
  838. function<op::push_front>::type const push_front = {{}};
  839. /// \brief \c pop is a lazy PolymorphicFunctionObject for popping the top element from a sequence in an
  840. /// xpressive semantic action.
  841. function<op::pop>::type const pop = {{}};
  842. /// \brief \c pop_back is a lazy PolymorphicFunctionObject for popping the back element from a sequence in an
  843. /// xpressive semantic action.
  844. function<op::pop_back>::type const pop_back = {{}};
  845. /// \brief \c pop_front is a lazy PolymorphicFunctionObject for popping the front element from a sequence in an
  846. /// xpressive semantic action.
  847. function<op::pop_front>::type const pop_front = {{}};
  848. /// \brief \c top is a lazy PolymorphicFunctionObject for accessing the top element from a stack in an
  849. /// xpressive semantic action.
  850. function<op::top>::type const top = {{}};
  851. /// \brief \c back is a lazy PolymorphicFunctionObject for fetching the back element of a sequence in an
  852. /// xpressive semantic action.
  853. function<op::back>::type const back = {{}};
  854. /// \brief \c front is a lazy PolymorphicFunctionObject for fetching the front element of a sequence in an
  855. /// xpressive semantic action.
  856. function<op::front>::type const front = {{}};
  857. /// \brief \c first is a lazy PolymorphicFunctionObject for accessing the first element of a \c std::pair\<\> in an
  858. /// xpressive semantic action.
  859. function<op::first>::type const first = {{}};
  860. /// \brief \c second is a lazy PolymorphicFunctionObject for accessing the second element of a \c std::pair\<\> in an
  861. /// xpressive semantic action.
  862. function<op::second>::type const second = {{}};
  863. /// \brief \c matched is a lazy PolymorphicFunctionObject for accessing the \c matched member of a \c xpressive::sub_match\<\> in an
  864. /// xpressive semantic action.
  865. function<op::matched>::type const matched = {{}};
  866. /// \brief \c length is a lazy PolymorphicFunctionObject for computing the length of a \c xpressive::sub_match\<\> in an
  867. /// xpressive semantic action.
  868. function<op::length>::type const length = {{}};
  869. /// \brief \c str is a lazy PolymorphicFunctionObject for converting a \c xpressive::sub_match\<\> to a \c std::basic_string\<\> in an
  870. /// xpressive semantic action.
  871. function<op::str>::type const str = {{}};
  872. /// \brief \c insert is a lazy PolymorphicFunctionObject for inserting a value or a range of values into a sequence in an
  873. /// xpressive semantic action.
  874. function<op::insert>::type const insert = {{}};
  875. /// \brief \c make_pair is a lazy PolymorphicFunctionObject for making a \c std::pair\<\> in an
  876. /// xpressive semantic action.
  877. function<op::make_pair>::type const make_pair = {{}};
  878. /// \brief \c unwrap_reference is a lazy PolymorphicFunctionObject for unwrapping a \c boost::reference_wrapper\<\> in an
  879. /// xpressive semantic action.
  880. function<op::unwrap_reference>::type const unwrap_reference = {{}};
  881. /// \brief \c value\<\> is a lazy wrapper for a value that can be used in xpressive semantic actions.
  882. /// \tparam T The type of the value to store.
  883. ///
  884. /// Below is an example that shows where \c <tt>value\<\></tt> is useful.
  885. ///
  886. /** \code
  887. sregex good_voodoo(boost::shared_ptr<int> pi)
  888. {
  889. using namespace boost::xpressive;
  890. // Use val() to hold the shared_ptr by value:
  891. sregex rex = +( _d [ ++*val(pi) ] >> '!' );
  892. // OK, rex holds a reference count to the integer.
  893. return rex;
  894. }
  895. \endcode
  896. */
  897. ///
  898. /// In the above code, \c xpressive::val() is a function that returns a \c value\<\> object. Had
  899. /// \c val() not been used here, the operation <tt>++*pi</tt> would have been evaluated eagerly
  900. /// once, instead of lazily when the regex match happens.
  901. template<typename T>
  902. struct value
  903. : proto::extends<typename proto::terminal<T>::type, value<T> >
  904. {
  905. /// INTERNAL ONLY
  906. typedef proto::extends<typename proto::terminal<T>::type, value<T> > base_type;
  907. /// \brief Store a default-constructed \c T
  908. value()
  909. : base_type()
  910. {}
  911. /// \param t The initial value.
  912. /// \brief Store a copy of \c t.
  913. explicit value(T const &t)
  914. : base_type(base_type::proto_base_expr::make(t))
  915. {}
  916. using base_type::operator=;
  917. /// \overload
  918. T &get()
  919. {
  920. return proto::value(*this);
  921. }
  922. /// \brief Fetch the stored value
  923. T const &get() const
  924. {
  925. return proto::value(*this);
  926. }
  927. };
  928. /// \brief \c reference\<\> is a lazy wrapper for a reference that can be used in
  929. /// xpressive semantic actions.
  930. ///
  931. /// \tparam T The type of the referent.
  932. ///
  933. /// Here is an example of how to use \c reference\<\> to create a lazy reference to
  934. /// an existing object so it can be read and written in an xpressive semantic action.
  935. ///
  936. /** \code
  937. using namespace boost::xpressive;
  938. std::map<std::string, int> result;
  939. reference<std::map<std::string, int> > result_ref(result);
  940. // Match a word and an integer, separated by =>,
  941. // and then stuff the result into a std::map<>
  942. sregex pair = ( (s1= +_w) >> "=>" >> (s2= +_d) )
  943. [ result_ref[s1] = as<int>(s2) ];
  944. \endcode
  945. */
  946. template<typename T>
  947. struct reference
  948. : proto::extends<typename proto::terminal<reference_wrapper<T> >::type, reference<T> >
  949. {
  950. /// INTERNAL ONLY
  951. typedef proto::extends<typename proto::terminal<reference_wrapper<T> >::type, reference<T> > base_type;
  952. /// \param t Reference to object
  953. /// \brief Store a reference to \c t
  954. explicit reference(T &t)
  955. : base_type(base_type::proto_base_expr::make(boost::ref(t)))
  956. {}
  957. using base_type::operator=;
  958. /// \brief Fetch the stored value
  959. T &get() const
  960. {
  961. return proto::value(*this).get();
  962. }
  963. };
  964. /// \brief \c local\<\> is a lazy wrapper for a reference to a value that is stored within the local itself.
  965. /// It is for use within xpressive semantic actions.
  966. ///
  967. /// \tparam T The type of the local variable.
  968. ///
  969. /// Below is an example of how to use \c local\<\> in semantic actions.
  970. ///
  971. /** \code
  972. using namespace boost::xpressive;
  973. local<int> i(0);
  974. std::string str("1!2!3?");
  975. // count the exciting digits, but not the
  976. // questionable ones.
  977. sregex rex = +( _d [ ++i ] >> '!' );
  978. regex_search(str, rex);
  979. assert( i.get() == 2 );
  980. \endcode
  981. */
  982. ///
  983. /// \note As the name "local" suggests, \c local\<\> objects and the regexes
  984. /// that refer to them should never leave the local scope. The value stored
  985. /// within the local object will be destroyed at the end of the \c local\<\>'s
  986. /// lifetime, and any regex objects still holding the \c local\<\> will be
  987. /// left with a dangling reference.
  988. template<typename T>
  989. struct local
  990. : detail::value_wrapper<T>
  991. , proto::terminal<reference_wrapper<T> >::type
  992. {
  993. /// INTERNAL ONLY
  994. typedef typename proto::terminal<reference_wrapper<T> >::type base_type;
  995. /// \brief Store a default-constructed value of type \c T
  996. local()
  997. : detail::value_wrapper<T>()
  998. , base_type(base_type::make(boost::ref(detail::value_wrapper<T>::value)))
  999. {}
  1000. /// \param t The initial value.
  1001. /// \brief Store a default-constructed value of type \c T
  1002. explicit local(T const &t)
  1003. : detail::value_wrapper<T>(t)
  1004. , base_type(base_type::make(boost::ref(detail::value_wrapper<T>::value)))
  1005. {}
  1006. using base_type::operator=;
  1007. /// Fetch the wrapped value.
  1008. T &get()
  1009. {
  1010. return proto::value(*this);
  1011. }
  1012. /// \overload
  1013. T const &get() const
  1014. {
  1015. return proto::value(*this);
  1016. }
  1017. };
  1018. /// \brief \c as() is a lazy funtion for lexically casting a parameter to a different type.
  1019. /// \tparam T The type to which to lexically cast the parameter.
  1020. /// \param a The lazy value to lexically cast.
  1021. /// \return A lazy object that, when evaluated, lexically casts its argument to the desired type.
  1022. template<typename T, typename A>
  1023. typename detail::make_function::impl<op::as<T> const, A const &>::result_type const
  1024. as(A const &a)
  1025. {
  1026. return detail::make_function::impl<op::as<T> const, A const &>()((op::as<T>()), a);
  1027. }
  1028. /// \brief \c static_cast_ is a lazy funtion for statically casting a parameter to a different type.
  1029. /// \tparam T The type to which to statically cast the parameter.
  1030. /// \param a The lazy value to statically cast.
  1031. /// \return A lazy object that, when evaluated, statically casts its argument to the desired type.
  1032. template<typename T, typename A>
  1033. typename detail::make_function::impl<op::static_cast_<T> const, A const &>::result_type const
  1034. static_cast_(A const &a)
  1035. {
  1036. return detail::make_function::impl<op::static_cast_<T> const, A const &>()((op::static_cast_<T>()), a);
  1037. }
  1038. /// \brief \c dynamic_cast_ is a lazy funtion for dynamically casting a parameter to a different type.
  1039. /// \tparam T The type to which to dynamically cast the parameter.
  1040. /// \param a The lazy value to dynamically cast.
  1041. /// \return A lazy object that, when evaluated, dynamically casts its argument to the desired type.
  1042. template<typename T, typename A>
  1043. typename detail::make_function::impl<op::dynamic_cast_<T> const, A const &>::result_type const
  1044. dynamic_cast_(A const &a)
  1045. {
  1046. return detail::make_function::impl<op::dynamic_cast_<T> const, A const &>()((op::dynamic_cast_<T>()), a);
  1047. }
  1048. /// \brief \c dynamic_cast_ is a lazy funtion for const-casting a parameter to a different type.
  1049. /// \tparam T The type to which to const-cast the parameter.
  1050. /// \param a The lazy value to const-cast.
  1051. /// \return A lazy object that, when evaluated, const-casts its argument to the desired type.
  1052. template<typename T, typename A>
  1053. typename detail::make_function::impl<op::const_cast_<T> const, A const &>::result_type const
  1054. const_cast_(A const &a)
  1055. {
  1056. return detail::make_function::impl<op::const_cast_<T> const, A const &>()((op::const_cast_<T>()), a);
  1057. }
  1058. /// \brief Helper for constructing \c value\<\> objects.
  1059. /// \return <tt>value\<T\>(t)</tt>
  1060. template<typename T>
  1061. value<T> const val(T const &t)
  1062. {
  1063. return value<T>(t);
  1064. }
  1065. /// \brief Helper for constructing \c reference\<\> objects.
  1066. /// \return <tt>reference\<T\>(t)</tt>
  1067. template<typename T>
  1068. reference<T> const ref(T &t)
  1069. {
  1070. return reference<T>(t);
  1071. }
  1072. /// \brief Helper for constructing \c reference\<\> objects that
  1073. /// store a reference to const.
  1074. /// \return <tt>reference\<T const\>(t)</tt>
  1075. template<typename T>
  1076. reference<T const> const cref(T const &t)
  1077. {
  1078. return reference<T const>(t);
  1079. }
  1080. /// \brief For adding user-defined assertions to your regular expressions.
  1081. ///
  1082. /// \param t The UnaryPredicate object or Boolean semantic action.
  1083. ///
  1084. /// A \RefSect{user_s_guide.semantic_actions_and_user_defined_assertions.user_defined_assertions,user-defined assertion}
  1085. /// is a kind of semantic action that evaluates
  1086. /// a Boolean lambda and, if it evaluates to false, causes the match to
  1087. /// fail at that location in the string. This will cause backtracking,
  1088. /// so the match may ultimately succeed.
  1089. ///
  1090. /// To use \c check() to specify a user-defined assertion in a regex, use the
  1091. /// following syntax:
  1092. ///
  1093. /** \code
  1094. sregex s = (_d >> _d)[check( XXX )]; // XXX is a custom assertion
  1095. \endcode
  1096. */
  1097. ///
  1098. /// The assertion is evaluated with a \c sub_match\<\> object that delineates
  1099. /// what part of the string matched the sub-expression to which the assertion
  1100. /// was attached.
  1101. ///
  1102. /// \c check() can be used with an ordinary predicate that takes a
  1103. /// \c sub_match\<\> object as follows:
  1104. ///
  1105. /** \code
  1106. // A predicate that is true IFF a sub-match is
  1107. // either 3 or 6 characters long.
  1108. struct three_or_six
  1109. {
  1110. bool operator()(ssub_match const &sub) const
  1111. {
  1112. return sub.length() == 3 || sub.length() == 6;
  1113. }
  1114. };
  1115. // match words of 3 characters or 6 characters.
  1116. sregex rx = (bow >> +_w >> eow)[ check(three_or_six()) ] ;
  1117. \endcode
  1118. */
  1119. ///
  1120. /// Alternately, \c check() can be used to define inline custom
  1121. /// assertions with the same syntax as is used to define semantic
  1122. /// actions. The following code is equivalent to above:
  1123. ///
  1124. /** \code
  1125. // match words of 3 characters or 6 characters.
  1126. sregex rx = (bow >> +_w >> eow)[ check(length(_)==3 || length(_)==6) ] ;
  1127. \endcode
  1128. */
  1129. ///
  1130. /// Within a custom assertion, \c _ is a placeholder for the \c sub_match\<\>
  1131. /// That delineates the part of the string matched by the sub-expression to
  1132. /// which the custom assertion was attached.
  1133. #ifdef BOOST_XPRESSIVE_DOXYGEN_INVOKED // A hack so Doxygen emits something more meaningful.
  1134. template<typename T>
  1135. detail::unspecified check(T const &t);
  1136. #else
  1137. proto::terminal<detail::check_tag>::type const check = {{}};
  1138. #endif
  1139. /// \brief For binding local variables to placeholders in semantic actions when
  1140. /// constructing a \c regex_iterator or a \c regex_token_iterator.
  1141. ///
  1142. /// \param args A set of argument bindings, where each argument binding is an assignment
  1143. /// expression, the left hand side of which must be an instance of \c placeholder\<X\>
  1144. /// for some \c X, and the right hand side is an lvalue of type \c X.
  1145. ///
  1146. /// \c xpressive::let() serves the same purpose as <tt>match_results::let()</tt>;
  1147. /// that is, it binds a placeholder to a local value. The purpose is to allow a
  1148. /// regex with semantic actions to be defined that refers to objects that do not yet exist.
  1149. /// Rather than referring directly to an object, a semantic action can refer to a placeholder,
  1150. /// and the value of the placeholder can be specified later with a <em>let expression</em>.
  1151. /// The <em>let expression</em> created with \c let() is passed to the constructor of either
  1152. /// \c regex_iterator or \c regex_token_iterator.
  1153. ///
  1154. /// See the section \RefSect{user_s_guide.semantic_actions_and_user_defined_assertions.referring_to_non_local_variables, "Referring to Non-Local Variables"}
  1155. /// in the Users' Guide for more discussion.
  1156. ///
  1157. /// \em Example:
  1158. ///
  1159. /**
  1160. \code
  1161. // Define a placeholder for a map object:
  1162. placeholder<std::map<std::string, int> > _map;
  1163. // Match a word and an integer, separated by =>,
  1164. // and then stuff the result into a std::map<>
  1165. sregex pair = ( (s1= +_w) >> "=>" >> (s2= +_d) )
  1166. [ _map[s1] = as<int>(s2) ];
  1167. // The string to parse
  1168. std::string str("aaa=>1 bbb=>23 ccc=>456");
  1169. // Here is the actual map to fill in:
  1170. std::map<std::string, int> result;
  1171. // Create a regex_iterator to find all the matches
  1172. sregex_iterator it(str.begin(), str.end(), pair, let(_map=result));
  1173. sregex_iterator end;
  1174. // step through all the matches, and fill in
  1175. // the result map
  1176. while(it != end)
  1177. ++it;
  1178. std::cout << result["aaa"] << '\n';
  1179. std::cout << result["bbb"] << '\n';
  1180. std::cout << result["ccc"] << '\n';
  1181. \endcode
  1182. */
  1183. ///
  1184. /// The above code displays:
  1185. ///
  1186. /** \code{.txt}
  1187. 1
  1188. 23
  1189. 456
  1190. \endcode
  1191. */
  1192. #ifdef BOOST_XPRESSIVE_DOXYGEN_INVOKED // A hack so Doxygen emits something more meaningful.
  1193. template<typename...ArgBindings>
  1194. detail::unspecified let(ArgBindings const &...args);
  1195. #else
  1196. detail::let_<proto::terminal<detail::let_tag>::type> const let = {{{}}};
  1197. #endif
  1198. /// \brief For defining a placeholder to stand in for a variable a semantic action.
  1199. ///
  1200. /// Use \c placeholder\<\> to define a placeholder for use in semantic actions to stand
  1201. /// in for real objects. The use of placeholders allows regular expressions with actions
  1202. /// to be defined once and reused in many contexts to read and write from objects which
  1203. /// were not available when the regex was defined.
  1204. ///
  1205. /// \tparam T The type of the object for which this placeholder stands in.
  1206. /// \tparam I An optional identifier that can be used to distinguish this placeholder
  1207. /// from others that may be used in the same semantic action that happen
  1208. /// to have the same type.
  1209. ///
  1210. /// You can use \c placeholder\<\> by creating an object of type \c placeholder\<T\>
  1211. /// and using that object in a semantic action exactly as you intend an object of
  1212. /// type \c T to be used.
  1213. ///
  1214. /**
  1215. \code
  1216. placeholder<int> _i;
  1217. placeholder<double> _d;
  1218. sregex rex = ( some >> regex >> here )
  1219. [ ++_i, _d *= _d ];
  1220. \endcode
  1221. */
  1222. ///
  1223. /// Then, when doing a pattern match with either \c regex_search(),
  1224. /// \c regex_match() or \c regex_replace(), pass a \c match_results\<\> object that
  1225. /// contains bindings for the placeholders used in the regex object's semantic actions.
  1226. /// You can create the bindings by calling \c match_results::let as follows:
  1227. ///
  1228. /**
  1229. \code
  1230. int i = 0;
  1231. double d = 3.14;
  1232. smatch what;
  1233. what.let(_i = i)
  1234. .let(_d = d);
  1235. if(regex_match("some string", rex, what))
  1236. // i and d mutated here
  1237. \endcode
  1238. */
  1239. ///
  1240. /// If a semantic action executes that contains an unbound placeholder, a exception of
  1241. /// type \c regex_error is thrown.
  1242. ///
  1243. /// See the discussion for \c xpressive::let() and the
  1244. /// \RefSect{user_s_guide.semantic_actions_and_user_defined_assertions.referring_to_non_local_variables, "Referring to Non-Local Variables"}
  1245. /// section in the Users' Guide for more information.
  1246. ///
  1247. /// <em>Example:</em>
  1248. ///
  1249. /**
  1250. \code
  1251. // Define a placeholder for a map object:
  1252. placeholder<std::map<std::string, int> > _map;
  1253. // Match a word and an integer, separated by =>,
  1254. // and then stuff the result into a std::map<>
  1255. sregex pair = ( (s1= +_w) >> "=>" >> (s2= +_d) )
  1256. [ _map[s1] = as<int>(s2) ];
  1257. // Match one or more word/integer pairs, separated
  1258. // by whitespace.
  1259. sregex rx = pair >> *(+_s >> pair);
  1260. // The string to parse
  1261. std::string str("aaa=>1 bbb=>23 ccc=>456");
  1262. // Here is the actual map to fill in:
  1263. std::map<std::string, int> result;
  1264. // Bind the _map placeholder to the actual map
  1265. smatch what;
  1266. what.let( _map = result );
  1267. // Execute the match and fill in result map
  1268. if(regex_match(str, what, rx))
  1269. {
  1270. std::cout << result["aaa"] << '\n';
  1271. std::cout << result["bbb"] << '\n';
  1272. std::cout << result["ccc"] << '\n';
  1273. }
  1274. \endcode
  1275. */
  1276. #ifdef BOOST_XPRESSIVE_DOXYGEN_INVOKED // A hack so Doxygen emits something more meaningful.
  1277. template<typename T, int I = 0>
  1278. struct placeholder
  1279. {
  1280. /// \param t The object to associate with this placeholder
  1281. /// \return An object of unspecified type that records the association of \c t
  1282. /// with \c *this.
  1283. detail::unspecified operator=(T &t) const;
  1284. /// \overload
  1285. detail::unspecified operator=(T const &t) const;
  1286. };
  1287. #else
  1288. template<typename T, int I, typename Dummy>
  1289. struct placeholder
  1290. {
  1291. typedef placeholder<T, I, Dummy> this_type;
  1292. typedef
  1293. typename proto::terminal<detail::action_arg<T, mpl::int_<I> > >::type
  1294. action_arg_type;
  1295. BOOST_PROTO_EXTENDS(action_arg_type, this_type, proto::default_domain)
  1296. };
  1297. #endif
  1298. /// \brief A lazy funtion for constructing objects objects of the specified type.
  1299. /// \tparam T The type of object to construct.
  1300. /// \param args The arguments to the constructor.
  1301. /// \return A lazy object that, when evaluated, returns <tt>T(xs...)</tt>, where
  1302. /// <tt>xs...</tt> is the result of evaluating the lazy arguments
  1303. /// <tt>args...</tt>.
  1304. #ifdef BOOST_XPRESSIVE_DOXYGEN_INVOKED // A hack so Doxygen emits something more meaningful.
  1305. template<typename T, typename ...Args>
  1306. detail::unspecified construct(Args const &...args);
  1307. #else
  1308. /// INTERNAL ONLY
  1309. #define BOOST_PROTO_LOCAL_MACRO(N, typename_A, A_const_ref, A_const_ref_a, a) \
  1310. template<typename X2_0 BOOST_PP_COMMA_IF(N) typename_A(N)> \
  1311. typename detail::make_function::impl< \
  1312. op::construct<X2_0> const \
  1313. BOOST_PP_COMMA_IF(N) A_const_ref(N) \
  1314. >::result_type const \
  1315. construct(A_const_ref_a(N)) \
  1316. { \
  1317. return detail::make_function::impl< \
  1318. op::construct<X2_0> const \
  1319. BOOST_PP_COMMA_IF(N) A_const_ref(N) \
  1320. >()((op::construct<X2_0>()) BOOST_PP_COMMA_IF(N) a(N)); \
  1321. } \
  1322. \
  1323. template<typename X2_0 BOOST_PP_COMMA_IF(N) typename_A(N)> \
  1324. typename detail::make_function::impl< \
  1325. op::throw_<X2_0> const \
  1326. BOOST_PP_COMMA_IF(N) A_const_ref(N) \
  1327. >::result_type const \
  1328. throw_(A_const_ref_a(N)) \
  1329. { \
  1330. return detail::make_function::impl< \
  1331. op::throw_<X2_0> const \
  1332. BOOST_PP_COMMA_IF(N) A_const_ref(N) \
  1333. >()((op::throw_<X2_0>()) BOOST_PP_COMMA_IF(N) a(N)); \
  1334. } \
  1335. /**/
  1336. #define BOOST_PROTO_LOCAL_a BOOST_PROTO_a ///< INTERNAL ONLY
  1337. #define BOOST_PROTO_LOCAL_LIMITS (0, BOOST_PP_DEC(BOOST_PROTO_MAX_ARITY)) ///< INTERNAL ONLY
  1338. #include BOOST_PROTO_LOCAL_ITERATE()
  1339. #endif
  1340. namespace detail
  1341. {
  1342. inline void ignore_unused_regex_actions()
  1343. {
  1344. detail::ignore_unused(xpressive::at);
  1345. detail::ignore_unused(xpressive::push);
  1346. detail::ignore_unused(xpressive::push_back);
  1347. detail::ignore_unused(xpressive::push_front);
  1348. detail::ignore_unused(xpressive::pop);
  1349. detail::ignore_unused(xpressive::pop_back);
  1350. detail::ignore_unused(xpressive::pop_front);
  1351. detail::ignore_unused(xpressive::top);
  1352. detail::ignore_unused(xpressive::back);
  1353. detail::ignore_unused(xpressive::front);
  1354. detail::ignore_unused(xpressive::first);
  1355. detail::ignore_unused(xpressive::second);
  1356. detail::ignore_unused(xpressive::matched);
  1357. detail::ignore_unused(xpressive::length);
  1358. detail::ignore_unused(xpressive::str);
  1359. detail::ignore_unused(xpressive::insert);
  1360. detail::ignore_unused(xpressive::make_pair);
  1361. detail::ignore_unused(xpressive::unwrap_reference);
  1362. detail::ignore_unused(xpressive::check);
  1363. detail::ignore_unused(xpressive::let);
  1364. }
  1365. struct mark_nbr
  1366. {
  1367. BOOST_PROTO_CALLABLE()
  1368. typedef int result_type;
  1369. int operator()(mark_placeholder m) const
  1370. {
  1371. return m.mark_number_;
  1372. }
  1373. };
  1374. struct ReplaceAlgo
  1375. : proto::or_<
  1376. proto::when<
  1377. proto::terminal<mark_placeholder>
  1378. , op::at(proto::_data, proto::call<mark_nbr(proto::_value)>)
  1379. >
  1380. , proto::when<
  1381. proto::terminal<any_matcher>
  1382. , op::at(proto::_data, proto::size_t<0>)
  1383. >
  1384. , proto::when<
  1385. proto::terminal<reference_wrapper<proto::_> >
  1386. , op::unwrap_reference(proto::_value)
  1387. >
  1388. , proto::_default<ReplaceAlgo>
  1389. >
  1390. {};
  1391. }
  1392. }}
  1393. #if BOOST_MSVC
  1394. #pragma warning(pop)
  1395. #endif
  1396. #endif // BOOST_XPRESSIVE_ACTIONS_HPP_EAN_03_22_2007