common.hpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586
  1. /*=============================================================================
  2. Copyright (c) 2001-2003 Daniel Nuffer
  3. Copyright (c) 2001-2007 Hartmut Kaiser
  4. Revised 2007, Copyright (c) Tobias Schwinger
  5. http://spirit.sourceforge.net/
  6. Distributed under the Boost Software License, Version 1.0. (See accompanying
  7. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. =============================================================================*/
  9. #ifndef BOOST_SPIRIT_TREE_COMMON_HPP
  10. #define BOOST_SPIRIT_TREE_COMMON_HPP
  11. #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
  12. #include <vector>
  13. #else
  14. #include <list>
  15. #endif
  16. #if defined(BOOST_SPIRIT_USE_BOOST_ALLOCATOR_FOR_TREES)
  17. #include <boost/pool/pool_alloc.hpp>
  18. #endif
  19. #include <algorithm>
  20. #include <boost/ref.hpp>
  21. #include <boost/call_traits.hpp>
  22. #include <boost/spirit/home/classic/namespace.hpp>
  23. #include <boost/spirit/home/classic/core.hpp>
  24. #include <boost/assert.hpp>
  25. #if defined(BOOST_SPIRIT_DEBUG) && \
  26. (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
  27. #include <iostream>
  28. #include <boost/spirit/home/classic/debug/debug_node.hpp>
  29. #endif
  30. #include <boost/spirit/home/classic/tree/common_fwd.hpp>
  31. #include <iterator> // for std::iterator_traits, std::distance
  32. namespace boost { namespace spirit {
  33. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  34. template <typename T>
  35. void swap(tree_node<T>& a, tree_node<T>& b);
  36. template <typename T, typename V>
  37. void swap(node_iter_data<T, V>& a, node_iter_data<T, V>& b);
  38. namespace impl {
  39. template <typename T>
  40. inline void cp_swap(T& t1, T& t2);
  41. }
  42. template <typename T>
  43. struct tree_node
  44. {
  45. typedef T parse_node_t;
  46. #if !defined(BOOST_SPIRIT_USE_BOOST_ALLOCATOR_FOR_TREES)
  47. typedef std::allocator<tree_node<T> > allocator_type;
  48. #elif !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
  49. typedef boost::pool_allocator<tree_node<T> > allocator_type;
  50. #else
  51. typedef boost::fast_pool_allocator<tree_node<T> > allocator_type;
  52. #endif
  53. #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
  54. typedef std::vector<tree_node<T>, allocator_type> children_t;
  55. #else
  56. typedef std::list<tree_node<T>, allocator_type> children_t;
  57. #endif // BOOST_SPIRIT_USE_LIST_FOR_TREES
  58. typedef typename children_t::iterator tree_iterator;
  59. typedef typename children_t::const_iterator const_tree_iterator;
  60. T value;
  61. children_t children;
  62. tree_node()
  63. : value()
  64. , children()
  65. {}
  66. explicit tree_node(T const& v)
  67. : value(v)
  68. , children()
  69. {}
  70. tree_node(T const& v, children_t const& c)
  71. : value(v)
  72. , children(c)
  73. {}
  74. void swap(tree_node<T>& x)
  75. {
  76. impl::cp_swap(value, x.value);
  77. impl::cp_swap(children, x.children);
  78. }
  79. // Intel V5.0.1 has a problem without this explicit operator=
  80. tree_node &operator= (tree_node const &rhs)
  81. {
  82. tree_node(rhs).swap(*this);
  83. return *this;
  84. }
  85. };
  86. #if defined(BOOST_SPIRIT_DEBUG) && \
  87. (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
  88. template <typename T>
  89. inline std::ostream&
  90. operator<<(std::ostream& o, tree_node<T> const& n)
  91. {
  92. static int depth = 0;
  93. o << "\n";
  94. for (int i = 0; i <= depth; ++i)
  95. {
  96. o << "\t";
  97. }
  98. o << "(depth = " << depth++ << " value = " << n.value;
  99. int c = 0;
  100. for (typename tree_node<T>::children_t::const_iterator it = n.children.begin();
  101. it != n.children.end(); ++it)
  102. {
  103. o << " children[" << c++ << "] = " << *it;
  104. }
  105. o << ")";
  106. --depth;
  107. return o;
  108. }
  109. #endif
  110. //////////////////////////////////
  111. template <typename IteratorT, typename ValueT>
  112. struct node_iter_data
  113. {
  114. typedef IteratorT iterator_t;
  115. typedef IteratorT /*const*/ const_iterator_t;
  116. node_iter_data()
  117. : first(), last(), is_root_(false), parser_id_(), value_()
  118. {}
  119. node_iter_data(IteratorT const& _first, IteratorT const& _last)
  120. : first(_first), last(_last), is_root_(false), parser_id_(), value_()
  121. {}
  122. void swap(node_iter_data& x)
  123. {
  124. impl::cp_swap(first, x.first);
  125. impl::cp_swap(last, x.last);
  126. impl::cp_swap(parser_id_, x.parser_id_);
  127. impl::cp_swap(is_root_, x.is_root_);
  128. impl::cp_swap(value_, x.value_);
  129. }
  130. IteratorT begin()
  131. {
  132. return first;
  133. }
  134. IteratorT const& begin() const
  135. {
  136. return first;
  137. }
  138. IteratorT end()
  139. {
  140. return last;
  141. }
  142. IteratorT const& end() const
  143. {
  144. return last;
  145. }
  146. bool is_root() const
  147. {
  148. return is_root_;
  149. }
  150. void is_root(bool b)
  151. {
  152. is_root_ = b;
  153. }
  154. parser_id id() const
  155. {
  156. return parser_id_;
  157. }
  158. void id(parser_id r)
  159. {
  160. parser_id_ = r;
  161. }
  162. ValueT const& value() const
  163. {
  164. return value_;
  165. }
  166. void value(ValueT const& v)
  167. {
  168. value_ = v;
  169. }
  170. private:
  171. IteratorT first, last;
  172. bool is_root_;
  173. parser_id parser_id_;
  174. ValueT value_;
  175. public:
  176. };
  177. #if defined(BOOST_SPIRIT_DEBUG) && \
  178. (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
  179. // value is default nil_t, so provide an operator<< for nil_t
  180. inline std::ostream&
  181. operator<<(std::ostream& o, nil_t const&)
  182. {
  183. return o;
  184. }
  185. template <typename IteratorT, typename ValueT>
  186. inline std::ostream&
  187. operator<<(std::ostream& o, node_iter_data<IteratorT, ValueT> const& n)
  188. {
  189. o << "(id = " << n.id() << " text = \"";
  190. typedef typename node_iter_data<IteratorT, ValueT>::const_iterator_t
  191. iterator_t;
  192. for (iterator_t it = n.begin(); it != n.end(); ++it)
  193. impl::token_printer(o, *it);
  194. o << "\" is_root = " << n.is_root()
  195. << /*" value = " << n.value() << */")";
  196. return o;
  197. }
  198. #endif
  199. //////////////////////////////////
  200. template <typename IteratorT = char const*, typename ValueT = nil_t>
  201. struct node_val_data
  202. {
  203. typedef
  204. typename std::iterator_traits<IteratorT>::value_type
  205. value_type;
  206. #if !defined(BOOST_SPIRIT_USE_BOOST_ALLOCATOR_FOR_TREES)
  207. typedef std::allocator<value_type> allocator_type;
  208. #elif !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
  209. typedef boost::pool_allocator<value_type> allocator_type;
  210. #else
  211. typedef boost::fast_pool_allocator<value_type> allocator_type;
  212. #endif
  213. #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
  214. typedef std::vector<value_type, allocator_type> container_t;
  215. #else
  216. typedef std::list<value_type, allocator_type> container_t;
  217. #endif
  218. typedef typename container_t::iterator iterator_t;
  219. typedef typename container_t::const_iterator const_iterator_t;
  220. node_val_data()
  221. : text(), is_root_(false), parser_id_(), value_()
  222. {}
  223. #if defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)
  224. node_val_data(IteratorT const& _first, IteratorT const& _last)
  225. : text(), is_root_(false), parser_id_(), value_()
  226. {
  227. std::copy(_first, _last, std::inserter(text, text.end()));
  228. }
  229. // This constructor is for building text out of iterators
  230. template <typename IteratorT2>
  231. node_val_data(IteratorT2 const& _first, IteratorT2 const& _last)
  232. : text(), is_root_(false), parser_id_(), value_()
  233. {
  234. std::copy(_first, _last, std::inserter(text, text.end()));
  235. }
  236. #else
  237. node_val_data(IteratorT const& _first, IteratorT const& _last)
  238. : text(_first, _last), is_root_(false), parser_id_(), value_()
  239. {}
  240. // This constructor is for building text out of iterators
  241. template <typename IteratorT2>
  242. node_val_data(IteratorT2 const& _first, IteratorT2 const& _last)
  243. : text(_first, _last), is_root_(false), parser_id_(), value_()
  244. {}
  245. #endif
  246. void swap(node_val_data& x)
  247. {
  248. impl::cp_swap(text, x.text);
  249. impl::cp_swap(is_root_, x.is_root_);
  250. impl::cp_swap(parser_id_, x.parser_id_);
  251. impl::cp_swap(value_, x.value_);
  252. }
  253. typename container_t::iterator begin()
  254. {
  255. return text.begin();
  256. }
  257. typename container_t::const_iterator begin() const
  258. {
  259. return text.begin();
  260. }
  261. typename container_t::iterator end()
  262. {
  263. return text.end();
  264. }
  265. typename container_t::const_iterator end() const
  266. {
  267. return text.end();
  268. }
  269. bool is_root() const
  270. {
  271. return is_root_;
  272. }
  273. void is_root(bool b)
  274. {
  275. is_root_ = b;
  276. }
  277. parser_id id() const
  278. {
  279. return parser_id_;
  280. }
  281. void id(parser_id r)
  282. {
  283. parser_id_ = r;
  284. }
  285. ValueT const& value() const
  286. {
  287. return value_;
  288. }
  289. void value(ValueT const& v)
  290. {
  291. value_ = v;
  292. }
  293. private:
  294. container_t text;
  295. bool is_root_;
  296. parser_id parser_id_;
  297. ValueT value_;
  298. };
  299. #if defined(BOOST_SPIRIT_DEBUG) && \
  300. (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
  301. template <typename IteratorT, typename ValueT>
  302. inline std::ostream&
  303. operator<<(std::ostream& o, node_val_data<IteratorT, ValueT> const& n)
  304. {
  305. o << "(id = " << n.id() << " text = \"";
  306. typedef typename node_val_data<IteratorT, ValueT>::const_iterator_t
  307. iterator_t;
  308. for (iterator_t it = n.begin(); it != n.end(); ++it)
  309. impl::token_printer(o, *it);
  310. o << "\" is_root = " << n.is_root()
  311. << " value = " << n.value() << ")";
  312. return o;
  313. }
  314. #endif
  315. template <typename T>
  316. inline void
  317. swap(tree_node<T>& a, tree_node<T>& b)
  318. {
  319. a.swap(b);
  320. }
  321. template <typename T, typename V>
  322. inline void
  323. swap(node_iter_data<T, V>& a, node_iter_data<T, V>& b)
  324. {
  325. a.swap(b);
  326. }
  327. //////////////////////////////////
  328. template <typename ValueT>
  329. class node_iter_data_factory
  330. {
  331. public:
  332. // This inner class is so that node_iter_data_factory can simulate
  333. // a template template parameter
  334. template <typename IteratorT>
  335. class factory
  336. {
  337. public:
  338. typedef IteratorT iterator_t;
  339. typedef node_iter_data<iterator_t, ValueT> node_t;
  340. static node_t create_node(iterator_t const& first, iterator_t const& last,
  341. bool /*is_leaf_node*/)
  342. {
  343. return node_t(first, last);
  344. }
  345. static node_t empty_node()
  346. {
  347. return node_t();
  348. }
  349. // precondition: ContainerT contains a tree_node<node_t>. And all
  350. // iterators in the container point to the same sequence.
  351. template <typename ContainerT>
  352. static node_t group_nodes(ContainerT const& nodes)
  353. {
  354. return node_t(nodes.begin()->value.begin(),
  355. nodes.back().value.end());
  356. }
  357. };
  358. };
  359. //////////////////////////////////
  360. template <typename ValueT>
  361. class node_val_data_factory
  362. {
  363. public:
  364. // This inner class is so that node_val_data_factory can simulate
  365. // a template template parameter
  366. template <typename IteratorT>
  367. class factory
  368. {
  369. public:
  370. typedef IteratorT iterator_t;
  371. typedef node_val_data<iterator_t, ValueT> node_t;
  372. static node_t create_node(iterator_t const& first, iterator_t const& last,
  373. bool is_leaf_node)
  374. {
  375. if (is_leaf_node)
  376. return node_t(first, last);
  377. else
  378. return node_t();
  379. }
  380. static node_t empty_node()
  381. {
  382. return node_t();
  383. }
  384. template <typename ContainerT>
  385. static node_t group_nodes(ContainerT const& nodes)
  386. {
  387. typename node_t::container_t c;
  388. typename ContainerT::const_iterator i_end = nodes.end();
  389. // copy all the nodes text into a new one
  390. for (typename ContainerT::const_iterator i = nodes.begin();
  391. i != i_end; ++i)
  392. {
  393. // See docs: reduced_node_d cannot be used with a
  394. // rule inside the [].
  395. BOOST_ASSERT(i->children.size() == 0);
  396. c.insert(c.end(), i->value.begin(), i->value.end());
  397. }
  398. return node_t(c.begin(), c.end());
  399. }
  400. };
  401. };
  402. //////////////////////////////////
  403. template <typename ValueT>
  404. class node_all_val_data_factory
  405. {
  406. public:
  407. // This inner class is so that node_all_val_data_factory can simulate
  408. // a template template parameter
  409. template <typename IteratorT>
  410. class factory
  411. {
  412. public:
  413. typedef IteratorT iterator_t;
  414. typedef node_val_data<iterator_t, ValueT> node_t;
  415. static node_t create_node(iterator_t const& first, iterator_t const& last,
  416. bool /*is_leaf_node*/)
  417. {
  418. return node_t(first, last);
  419. }
  420. static node_t empty_node()
  421. {
  422. return node_t();
  423. }
  424. template <typename ContainerT>
  425. static node_t group_nodes(ContainerT const& nodes)
  426. {
  427. typename node_t::container_t c;
  428. typename ContainerT::const_iterator i_end = nodes.end();
  429. // copy all the nodes text into a new one
  430. for (typename ContainerT::const_iterator i = nodes.begin();
  431. i != i_end; ++i)
  432. {
  433. BOOST_ASSERT(i->children.size() == 0);
  434. c.insert(c.end(), i->value.begin(), i->value.end());
  435. }
  436. return node_t(c.begin(), c.end());
  437. }
  438. };
  439. };
  440. namespace impl {
  441. ///////////////////////////////////////////////////////////////////////////
  442. // can't call unqualified swap from within classname::swap
  443. // as Koenig lookup rules will find only the classname::swap
  444. // member function not the global declaration, so use cp_swap
  445. // as a forwarding function (JM):
  446. template <typename T>
  447. inline void cp_swap(T& t1, T& t2)
  448. {
  449. using std::swap;
  450. using BOOST_SPIRIT_CLASSIC_NS::swap;
  451. using boost::swap;
  452. swap(t1, t2);
  453. }
  454. }
  455. //////////////////////////////////
  456. template <typename IteratorT, typename NodeFactoryT, typename T>
  457. class tree_match : public match<T>
  458. {
  459. public:
  460. typedef typename NodeFactoryT::template factory<IteratorT> node_factory_t;
  461. typedef typename node_factory_t::node_t parse_node_t;
  462. typedef tree_node<parse_node_t> node_t;
  463. typedef typename node_t::children_t container_t;
  464. typedef typename container_t::iterator tree_iterator;
  465. typedef typename container_t::const_iterator const_tree_iterator;
  466. typedef T attr_t;
  467. typedef typename boost::call_traits<T>::param_type param_type;
  468. typedef typename boost::call_traits<T>::reference reference;
  469. typedef typename boost::call_traits<T>::const_reference const_reference;
  470. tree_match()
  471. : match<T>(), trees()
  472. {}
  473. explicit
  474. tree_match(std::size_t length_)
  475. : match<T>(length_), trees()
  476. {}
  477. tree_match(std::size_t length_, parse_node_t const& n)
  478. : match<T>(length_), trees()
  479. {
  480. trees.push_back(node_t(n));
  481. }
  482. tree_match(std::size_t length_, param_type val, parse_node_t const& n)
  483. : match<T>(length_, val), trees()
  484. {
  485. #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
  486. trees.reserve(10); // this is more or less an arbitrary number...
  487. #endif
  488. trees.push_back(node_t(n));
  489. }
  490. // attention, these constructors will change the second parameter!
  491. tree_match(std::size_t length_, container_t& c)
  492. : match<T>(length_), trees()
  493. {
  494. impl::cp_swap(trees, c);
  495. }
  496. tree_match(std::size_t length_, param_type val, container_t& c)
  497. : match<T>(length_, val), trees()
  498. {
  499. impl::cp_swap(trees, c);
  500. }
  501. template <typename T2>
  502. tree_match(match<T2> const& other)
  503. : match<T>(other), trees()
  504. {}
  505. template <typename T2, typename T3, typename T4>
  506. tree_match(tree_match<T2, T3, T4> const& other)
  507. : match<T>(other), trees()
  508. { impl::cp_swap(trees, other.trees); }
  509. template <typename T2>
  510. tree_match&
  511. operator=(match<T2> const& other)
  512. {
  513. match<T>::operator=(other);
  514. return *this;
  515. }
  516. template <typename T2, typename T3, typename T4>
  517. tree_match&
  518. operator=(tree_match<T2, T3, T4> const& other)
  519. {
  520. match<T>::operator=(other);
  521. impl::cp_swap(trees, other.trees);
  522. return *this;
  523. }
  524. tree_match(tree_match const& x)
  525. : match<T>(x), trees()
  526. {
  527. // use auto_ptr like ownership for the trees data member
  528. impl::cp_swap(trees, x.trees);
  529. }
  530. tree_match& operator=(tree_match const& x)
  531. {
  532. tree_match tmp(x);
  533. this->swap(tmp);
  534. return *this;
  535. }
  536. void swap(tree_match& x)
  537. {
  538. match<T>::swap(x);
  539. impl::cp_swap(trees, x.trees);
  540. }
  541. mutable container_t trees;
  542. };
  543. #if defined(BOOST_SPIRIT_DEBUG) && \
  544. (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
  545. template <typename IteratorT, typename NodeFactoryT, typename T>
  546. inline std::ostream&
  547. operator<<(std::ostream& o, tree_match<IteratorT, NodeFactoryT, T> const& m)
  548. {
  549. typedef
  550. typename tree_match<IteratorT, NodeFactoryT, T>::container_t::iterator
  551. iterator;
  552. o << "(length = " << (int)m.length();
  553. int c = 0;
  554. for (iterator i = m.trees.begin(); i != m.trees.end(); ++i)
  555. {
  556. o << " trees[" << c++ << "] = " << *i;
  557. }
  558. o << "\n)";
  559. return o;
  560. }
  561. #endif
  562. //////////////////////////////////
  563. struct tree_policy
  564. {
  565. template <typename FunctorT, typename MatchT>
  566. static void apply_op_to_match(FunctorT const& /*op*/, MatchT& /*m*/)
  567. {}
  568. template <typename MatchT, typename Iterator1T, typename Iterator2T>
  569. static void group_match(MatchT& /*m*/, parser_id const& /*id*/,
  570. Iterator1T const& /*first*/, Iterator2T const& /*last*/)
  571. {}
  572. template <typename MatchT>
  573. static void concat(MatchT& /*a*/, MatchT const& /*b*/)
  574. {}
  575. };
  576. //////////////////////////////////
  577. template <
  578. typename MatchPolicyT,
  579. typename IteratorT,
  580. typename NodeFactoryT,
  581. typename TreePolicyT,
  582. typename T
  583. >
  584. struct common_tree_match_policy : public match_policy
  585. {
  586. common_tree_match_policy()
  587. {
  588. }
  589. template <typename PolicyT>
  590. common_tree_match_policy(PolicyT const & policies)
  591. : match_policy((match_policy const &)policies)
  592. {
  593. }
  594. template <typename U>
  595. struct result { typedef tree_match<IteratorT, NodeFactoryT, U> type; };
  596. typedef tree_match<IteratorT, NodeFactoryT, T> match_t;
  597. typedef IteratorT iterator_t;
  598. typedef TreePolicyT tree_policy_t;
  599. typedef NodeFactoryT factory_t;
  600. static const match_t no_match() { return match_t(); }
  601. static const match_t empty_match()
  602. { return match_t(0, tree_policy_t::empty_node()); }
  603. template <typename AttrT, typename Iterator1T, typename Iterator2T>
  604. static tree_match<IteratorT, NodeFactoryT, AttrT> create_match(
  605. std::size_t length,
  606. AttrT const& val,
  607. Iterator1T const& first,
  608. Iterator2T const& last)
  609. {
  610. #if defined(BOOST_SPIRIT_DEBUG) && \
  611. (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
  612. BOOST_SPIRIT_DEBUG_OUT << "\n>>> create_node(begin) <<<\n"
  613. "creating node text: \"";
  614. for (Iterator1T it = first; it != last; ++it)
  615. impl::token_printer(BOOST_SPIRIT_DEBUG_OUT, *it);
  616. BOOST_SPIRIT_DEBUG_OUT << "\"\n";
  617. BOOST_SPIRIT_DEBUG_OUT << ">>> create_node(end) <<<\n\n";
  618. #endif
  619. return tree_match<IteratorT, NodeFactoryT, AttrT>(length, val,
  620. tree_policy_t::create_node(length, first, last, true));
  621. }
  622. template <typename Match1T, typename Match2T>
  623. static void concat_match(Match1T& a, Match2T const& b)
  624. {
  625. #if defined(BOOST_SPIRIT_DEBUG) && \
  626. (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
  627. BOOST_SPIRIT_DEBUG_OUT << "\n>>> concat_match(begin) <<<\n";
  628. BOOST_SPIRIT_DEBUG_OUT << "tree a:\n" << a << "\n";
  629. BOOST_SPIRIT_DEBUG_OUT << "tree b:\n" << b << "\n";
  630. BOOST_SPIRIT_DEBUG_OUT << ">>> concat_match(end) <<<\n\n";
  631. #endif
  632. BOOST_SPIRIT_ASSERT(a && b);
  633. if (a.length() == 0)
  634. {
  635. a = b;
  636. return;
  637. }
  638. else if (b.length() == 0
  639. #ifdef BOOST_SPIRIT_NO_TREE_NODE_COLLAPSING
  640. && !b.trees.begin()->value.id().to_long()
  641. #endif
  642. )
  643. {
  644. return;
  645. }
  646. a.concat(b);
  647. tree_policy_t::concat(a, b);
  648. }
  649. template <typename MatchT, typename IteratorT2>
  650. void
  651. group_match(
  652. MatchT& m,
  653. parser_id const& id,
  654. IteratorT2 const& first,
  655. IteratorT2 const& last) const
  656. {
  657. if (!m) return;
  658. #if defined(BOOST_SPIRIT_DEBUG) && \
  659. (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_TREES)
  660. BOOST_SPIRIT_DEBUG_OUT << "\n>>> group_match(begin) <<<\n"
  661. "new node(" << id << ") \"";
  662. for (IteratorT2 it = first; it != last; ++it)
  663. impl::token_printer(BOOST_SPIRIT_DEBUG_OUT, *it);
  664. BOOST_SPIRIT_DEBUG_OUT << "\"\n";
  665. BOOST_SPIRIT_DEBUG_OUT << "new child tree (before grouping):\n" << m << "\n";
  666. tree_policy_t::group_match(m, id, first, last);
  667. BOOST_SPIRIT_DEBUG_OUT << "new child tree (after grouping):\n" << m << "\n";
  668. BOOST_SPIRIT_DEBUG_OUT << ">>> group_match(end) <<<\n\n";
  669. #else
  670. tree_policy_t::group_match(m, id, first, last);
  671. #endif
  672. }
  673. };
  674. //////////////////////////////////
  675. template <typename MatchPolicyT, typename NodeFactoryT>
  676. struct common_tree_tree_policy
  677. {
  678. typedef typename MatchPolicyT::iterator_t iterator_t;
  679. typedef typename MatchPolicyT::match_t match_t;
  680. typedef typename NodeFactoryT::template factory<iterator_t> factory_t;
  681. typedef typename factory_t::node_t node_t;
  682. template <typename Iterator1T, typename Iterator2T>
  683. static node_t
  684. create_node(std::size_t /*length*/, Iterator1T const& first,
  685. Iterator2T const& last, bool leaf_node)
  686. {
  687. return factory_t::create_node(first, last, leaf_node);
  688. }
  689. static node_t
  690. empty_node()
  691. {
  692. return factory_t::empty_node();
  693. }
  694. template <typename FunctorT>
  695. static void apply_op_to_match(FunctorT const& op, match_t& m)
  696. {
  697. op(m);
  698. }
  699. };
  700. //////////////////////////////////
  701. // directives to modify how the parse tree is generated
  702. struct no_tree_gen_node_parser_gen;
  703. template <typename T>
  704. struct no_tree_gen_node_parser
  705. : public unary<T, parser<no_tree_gen_node_parser<T> > >
  706. {
  707. typedef no_tree_gen_node_parser<T> self_t;
  708. typedef no_tree_gen_node_parser_gen parser_generator_t;
  709. typedef unary_parser_category parser_category_t;
  710. no_tree_gen_node_parser(T const& a)
  711. : unary<T, parser<no_tree_gen_node_parser<T> > >(a) {}
  712. template <typename ScannerT>
  713. typename parser_result<self_t, ScannerT>::type
  714. parse(ScannerT const& scanner) const
  715. {
  716. typedef typename ScannerT::iteration_policy_t iteration_policy_t;
  717. typedef match_policy match_policy_t;
  718. typedef typename ScannerT::action_policy_t action_policy_t;
  719. typedef scanner_policies<
  720. iteration_policy_t,
  721. match_policy_t,
  722. action_policy_t
  723. > policies_t;
  724. return this->subject().parse(scanner.change_policies(policies_t(scanner)));
  725. }
  726. };
  727. struct no_tree_gen_node_parser_gen
  728. {
  729. template <typename T>
  730. struct result {
  731. typedef no_tree_gen_node_parser<T> type;
  732. };
  733. template <typename T>
  734. static no_tree_gen_node_parser<T>
  735. generate(parser<T> const& s)
  736. {
  737. return no_tree_gen_node_parser<T>(s.derived());
  738. }
  739. template <typename T>
  740. no_tree_gen_node_parser<T>
  741. operator[](parser<T> const& s) const
  742. {
  743. return no_tree_gen_node_parser<T>(s.derived());
  744. }
  745. };
  746. const no_tree_gen_node_parser_gen no_node_d = no_tree_gen_node_parser_gen();
  747. //////////////////////////////////
  748. struct leaf_node_parser_gen;
  749. template<typename T>
  750. struct leaf_node_parser
  751. : public unary<T, parser<leaf_node_parser<T> > >
  752. {
  753. typedef leaf_node_parser<T> self_t;
  754. typedef leaf_node_parser_gen parser_generator_t;
  755. typedef unary_parser_category parser_category_t;
  756. leaf_node_parser(T const& a)
  757. : unary<T, parser<leaf_node_parser<T> > >(a) {}
  758. template <typename ScannerT>
  759. typename parser_result<self_t, ScannerT>::type
  760. parse(ScannerT const& scanner) const
  761. {
  762. typedef scanner_policies< typename ScannerT::iteration_policy_t,
  763. match_policy, typename ScannerT::action_policy_t > policies_t;
  764. typedef typename ScannerT::iterator_t iterator_t;
  765. typedef typename parser_result<self_t, ScannerT>::type result_t;
  766. typedef typename result_t::node_factory_t factory_t;
  767. iterator_t from = scanner.first;
  768. result_t hit = impl::contiguous_parser_parse<result_t>(this->subject(),
  769. scanner.change_policies(policies_t(scanner,match_policy(),scanner)),
  770. scanner);
  771. if (hit)
  772. return result_t(hit.length(),
  773. factory_t::create_node(from, scanner.first, true));
  774. else
  775. return result_t(hit.length());
  776. }
  777. };
  778. struct leaf_node_parser_gen
  779. {
  780. template <typename T>
  781. struct result {
  782. typedef leaf_node_parser<T> type;
  783. };
  784. template <typename T>
  785. static leaf_node_parser<T>
  786. generate(parser<T> const& s)
  787. {
  788. return leaf_node_parser<T>(s.derived());
  789. }
  790. template <typename T>
  791. leaf_node_parser<T>
  792. operator[](parser<T> const& s) const
  793. {
  794. return leaf_node_parser<T>(s.derived());
  795. }
  796. };
  797. const leaf_node_parser_gen leaf_node_d = leaf_node_parser_gen();
  798. const leaf_node_parser_gen token_node_d = leaf_node_parser_gen();
  799. //////////////////////////////////
  800. namespace impl {
  801. template <typename MatchPolicyT>
  802. struct tree_policy_selector
  803. {
  804. typedef tree_policy type;
  805. };
  806. } // namespace impl
  807. //////////////////////////////////
  808. template <typename NodeParserT>
  809. struct node_parser_gen;
  810. template <typename T, typename NodeParserT>
  811. struct node_parser
  812. : public unary<T, parser<node_parser<T, NodeParserT> > >
  813. {
  814. typedef node_parser<T, NodeParserT> self_t;
  815. typedef node_parser_gen<NodeParserT> parser_generator_t;
  816. typedef unary_parser_category parser_category_t;
  817. node_parser(T const& a)
  818. : unary<T, parser<node_parser<T, NodeParserT> > >(a) {}
  819. template <typename ScannerT>
  820. struct result
  821. {
  822. typedef typename parser_result<T, ScannerT>::type type;
  823. };
  824. template <typename ScannerT>
  825. typename parser_result<self_t, ScannerT>::type
  826. parse(ScannerT const& scanner) const
  827. {
  828. typename parser_result<self_t, ScannerT>::type hit = this->subject().parse(scanner);
  829. if (hit)
  830. {
  831. impl::tree_policy_selector<typename ScannerT::match_policy_t>::type::apply_op_to_match(NodeParserT(), hit);
  832. }
  833. return hit;
  834. }
  835. };
  836. template <typename NodeParserT>
  837. struct node_parser_gen
  838. {
  839. template <typename T>
  840. struct result {
  841. typedef node_parser<T, NodeParserT> type;
  842. };
  843. template <typename T>
  844. static node_parser<T, NodeParserT>
  845. generate(parser<T> const& s)
  846. {
  847. return node_parser<T, NodeParserT>(s.derived());
  848. }
  849. template <typename T>
  850. node_parser<T, NodeParserT>
  851. operator[](parser<T> const& s) const
  852. {
  853. return node_parser<T, NodeParserT>(s.derived());
  854. }
  855. };
  856. //////////////////////////////////
  857. struct reduced_node_op
  858. {
  859. template <typename MatchT>
  860. void operator()(MatchT& m) const
  861. {
  862. if (m.trees.size() == 1)
  863. {
  864. m.trees.begin()->children.clear();
  865. }
  866. else if (m.trees.size() > 1)
  867. {
  868. typedef typename MatchT::node_factory_t node_factory_t;
  869. m = MatchT(m.length(), node_factory_t::group_nodes(m.trees));
  870. }
  871. }
  872. };
  873. const node_parser_gen<reduced_node_op> reduced_node_d =
  874. node_parser_gen<reduced_node_op>();
  875. struct discard_node_op
  876. {
  877. template <typename MatchT>
  878. void operator()(MatchT& m) const
  879. {
  880. m.trees.clear();
  881. }
  882. };
  883. const node_parser_gen<discard_node_op> discard_node_d =
  884. node_parser_gen<discard_node_op>();
  885. struct infix_node_op
  886. {
  887. template <typename MatchT>
  888. void operator()(MatchT& m) const
  889. {
  890. typedef typename MatchT::container_t container_t;
  891. typedef typename MatchT::container_t::iterator iter_t;
  892. typedef typename MatchT::container_t::value_type value_t;
  893. using std::swap;
  894. using boost::swap;
  895. using BOOST_SPIRIT_CLASSIC_NS::swap;
  896. // copying the tree nodes is expensive, since it may copy a whole
  897. // tree. swapping them is cheap, so swap the nodes we want into
  898. // a new container of children.
  899. container_t new_children;
  900. std::size_t length = 0;
  901. std::size_t tree_size = m.trees.size();
  902. // the infix_node_d[] make no sense for nodes with no subnodes
  903. BOOST_SPIRIT_ASSERT(tree_size >= 1);
  904. bool keep = true;
  905. #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
  906. new_children.reserve((tree_size+1)/2);
  907. #endif
  908. iter_t i_end = m.trees.end();
  909. for (iter_t i = m.trees.begin(); i != i_end; ++i)
  910. {
  911. if (keep) {
  912. // adjust the length
  913. length += std::distance((*i).value.begin(), (*i).value.end());
  914. // move the child node
  915. new_children.push_back(value_t());
  916. swap(new_children.back(), *i);
  917. keep = false;
  918. }
  919. else {
  920. // ignore this child node
  921. keep = true;
  922. }
  923. }
  924. m = MatchT(length, new_children);
  925. }
  926. };
  927. const node_parser_gen<infix_node_op> infix_node_d =
  928. node_parser_gen<infix_node_op>();
  929. struct discard_first_node_op
  930. {
  931. template <typename MatchT>
  932. void operator()(MatchT& m) const
  933. {
  934. typedef typename MatchT::container_t container_t;
  935. typedef typename MatchT::container_t::iterator iter_t;
  936. typedef typename MatchT::container_t::value_type value_t;
  937. using std::swap;
  938. using boost::swap;
  939. using BOOST_SPIRIT_CLASSIC_NS::swap;
  940. // copying the tree nodes is expensive, since it may copy a whole
  941. // tree. swapping them is cheap, so swap the nodes we want into
  942. // a new container of children, instead of saying
  943. // m.trees.erase(m.trees.begin()) because, on a container_t that will
  944. // cause all the nodes afterwards to be copied into the previous
  945. // position.
  946. container_t new_children;
  947. std::size_t length = 0;
  948. std::size_t tree_size = m.trees.size();
  949. // the discard_first_node_d[] make no sense for nodes with no subnodes
  950. BOOST_SPIRIT_ASSERT(tree_size >= 1);
  951. if (tree_size > 1) {
  952. #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
  953. new_children.reserve(tree_size - 1);
  954. #endif
  955. iter_t i = m.trees.begin(), i_end = m.trees.end();
  956. for (++i; i != i_end; ++i)
  957. {
  958. // adjust the length
  959. length += std::distance((*i).value.begin(), (*i).value.end());
  960. // move the child node
  961. new_children.push_back(value_t());
  962. swap(new_children.back(), *i);
  963. }
  964. }
  965. else {
  966. // if there was a tree and now there isn't any, insert an empty node
  967. iter_t i = m.trees.begin();
  968. // This isn't entirely correct, since the empty node will reference
  969. // the end of the discarded node, but I currently don't see any way to
  970. // get at the begin of the node following this subnode.
  971. // This should be safe anyway because the it shouldn't get dereferenced
  972. // under any circumstances.
  973. typedef typename value_t::parse_node_t::iterator_t iterator_type;
  974. iterator_type it = (*i).value.end();
  975. new_children.push_back(
  976. value_t(typename value_t::parse_node_t(it, it)));
  977. }
  978. m = MatchT(length, new_children);
  979. }
  980. };
  981. const node_parser_gen<discard_first_node_op> discard_first_node_d =
  982. node_parser_gen<discard_first_node_op>();
  983. struct discard_last_node_op
  984. {
  985. template <typename MatchT>
  986. void operator()(MatchT& m) const
  987. {
  988. typedef typename MatchT::container_t container_t;
  989. typedef typename MatchT::container_t::iterator iter_t;
  990. typedef typename MatchT::container_t::value_type value_t;
  991. using std::swap;
  992. using boost::swap;
  993. using BOOST_SPIRIT_CLASSIC_NS::swap;
  994. // copying the tree nodes is expensive, since it may copy a whole
  995. // tree. swapping them is cheap, so swap the nodes we want into
  996. // a new container of children, instead of saying
  997. // m.trees.erase(m.trees.begin()) because, on a container_t that will
  998. // cause all the nodes afterwards to be copied into the previous
  999. // position.
  1000. container_t new_children;
  1001. std::size_t length = 0;
  1002. std::size_t tree_size = m.trees.size();
  1003. // the discard_last_node_d[] make no sense for nodes with no subnodes
  1004. BOOST_SPIRIT_ASSERT(tree_size >= 1);
  1005. if (tree_size > 1) {
  1006. m.trees.pop_back();
  1007. #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
  1008. new_children.reserve(tree_size - 1);
  1009. #endif
  1010. iter_t i_end = m.trees.end();
  1011. for (iter_t i = m.trees.begin(); i != i_end; ++i)
  1012. {
  1013. // adjust the length
  1014. length += std::distance((*i).value.begin(), (*i).value.end());
  1015. // move the child node
  1016. new_children.push_back(value_t());
  1017. swap(new_children.back(), *i);
  1018. }
  1019. }
  1020. else {
  1021. // if there was a tree and now there isn't any, insert an empty node
  1022. iter_t i = m.trees.begin();
  1023. typedef typename value_t::parse_node_t::iterator_t iterator_type;
  1024. iterator_type it = (*i).value.begin();
  1025. new_children.push_back(
  1026. value_t(typename value_t::parse_node_t(it, it)));
  1027. }
  1028. m = MatchT(length, new_children);
  1029. }
  1030. };
  1031. const node_parser_gen<discard_last_node_op> discard_last_node_d =
  1032. node_parser_gen<discard_last_node_op>();
  1033. struct inner_node_op
  1034. {
  1035. template <typename MatchT>
  1036. void operator()(MatchT& m) const
  1037. {
  1038. typedef typename MatchT::container_t container_t;
  1039. typedef typename MatchT::container_t::iterator iter_t;
  1040. typedef typename MatchT::container_t::value_type value_t;
  1041. using std::swap;
  1042. using boost::swap;
  1043. using BOOST_SPIRIT_CLASSIC_NS::swap;
  1044. // copying the tree nodes is expensive, since it may copy a whole
  1045. // tree. swapping them is cheap, so swap the nodes we want into
  1046. // a new container of children, instead of saying
  1047. // m.trees.erase(m.trees.begin()) because, on a container_t that will
  1048. // cause all the nodes afterwards to be copied into the previous
  1049. // position.
  1050. container_t new_children;
  1051. std::size_t length = 0;
  1052. std::size_t tree_size = m.trees.size();
  1053. // the inner_node_d[] make no sense for nodes with less then 2 subnodes
  1054. BOOST_SPIRIT_ASSERT(tree_size >= 2);
  1055. if (tree_size > 2) {
  1056. m.trees.pop_back(); // erase the last element
  1057. #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
  1058. new_children.reserve(tree_size - 1);
  1059. #endif
  1060. iter_t i = m.trees.begin(); // skip over the first element
  1061. iter_t i_end = m.trees.end();
  1062. for (++i; i != i_end; ++i)
  1063. {
  1064. // adjust the length
  1065. length += std::distance((*i).value.begin(), (*i).value.end());
  1066. // move the child node
  1067. new_children.push_back(value_t());
  1068. swap(new_children.back(), *i);
  1069. }
  1070. }
  1071. else {
  1072. // if there was a tree and now there isn't any, insert an empty node
  1073. iter_t i = m.trees.begin(); // skip over the first element
  1074. typedef typename value_t::parse_node_t::iterator_t iterator_type;
  1075. iterator_type it = (*++i).value.begin();
  1076. new_children.push_back(
  1077. value_t(typename value_t::parse_node_t(it, it)));
  1078. }
  1079. m = MatchT(length, new_children);
  1080. }
  1081. };
  1082. const node_parser_gen<inner_node_op> inner_node_d =
  1083. node_parser_gen<inner_node_op>();
  1084. //////////////////////////////////
  1085. // action_directive_parser and action_directive_parser_gen
  1086. // are meant to be used as a template to create directives that
  1087. // generate action classes. For example access_match and
  1088. // access_node. The ActionParserT template parameter must be
  1089. // a class that has an innter class called action that is templated
  1090. // on the parser type and the action type.
  1091. template <typename ActionParserT>
  1092. struct action_directive_parser_gen;
  1093. template <typename T, typename ActionParserT>
  1094. struct action_directive_parser
  1095. : public unary<T, parser<action_directive_parser<T, ActionParserT> > >
  1096. {
  1097. typedef action_directive_parser<T, ActionParserT> self_t;
  1098. typedef action_directive_parser_gen<ActionParserT> parser_generator_t;
  1099. typedef unary_parser_category parser_category_t;
  1100. action_directive_parser(T const& a)
  1101. : unary<T, parser<action_directive_parser<T, ActionParserT> > >(a) {}
  1102. template <typename ScannerT>
  1103. struct result
  1104. {
  1105. typedef typename parser_result<T, ScannerT>::type type;
  1106. };
  1107. template <typename ScannerT>
  1108. typename parser_result<self_t, ScannerT>::type
  1109. parse(ScannerT const& scanner) const
  1110. {
  1111. return this->subject().parse(scanner);
  1112. }
  1113. template <typename ActionT>
  1114. typename ActionParserT::template action<action_directive_parser<T, ActionParserT>, ActionT>
  1115. operator[](ActionT const& actor) const
  1116. {
  1117. typedef typename
  1118. ActionParserT::template action<action_directive_parser, ActionT>
  1119. action_t;
  1120. return action_t(*this, actor);
  1121. }
  1122. };
  1123. //////////////////////////////////
  1124. template <typename ActionParserT>
  1125. struct action_directive_parser_gen
  1126. {
  1127. template <typename T>
  1128. struct result {
  1129. typedef action_directive_parser<T, ActionParserT> type;
  1130. };
  1131. template <typename T>
  1132. static action_directive_parser<T, ActionParserT>
  1133. generate(parser<T> const& s)
  1134. {
  1135. return action_directive_parser<T, ActionParserT>(s.derived());
  1136. }
  1137. template <typename T>
  1138. action_directive_parser<T, ActionParserT>
  1139. operator[](parser<T> const& s) const
  1140. {
  1141. return action_directive_parser<T, ActionParserT>(s.derived());
  1142. }
  1143. };
  1144. //////////////////////////////////
  1145. // Calls the attached action passing it the match from the parser
  1146. // and the first and last iterators.
  1147. // The inner template class is used to simulate template-template parameters
  1148. // (declared in common_fwd.hpp).
  1149. template <typename ParserT, typename ActionT>
  1150. struct access_match_action::action
  1151. : public unary<ParserT, parser<access_match_action::action<ParserT, ActionT> > >
  1152. {
  1153. typedef action_parser_category parser_category;
  1154. typedef action<ParserT, ActionT> self_t;
  1155. template <typename ScannerT>
  1156. struct result
  1157. {
  1158. typedef typename parser_result<ParserT, ScannerT>::type type;
  1159. };
  1160. action( ParserT const& subject,
  1161. ActionT const& actor_);
  1162. template <typename ScannerT>
  1163. typename parser_result<self_t, ScannerT>::type
  1164. parse(ScannerT const& scanner) const;
  1165. ActionT const &predicate() const;
  1166. private:
  1167. ActionT actor;
  1168. };
  1169. //////////////////////////////////
  1170. template <typename ParserT, typename ActionT>
  1171. access_match_action::action<ParserT, ActionT>::action(
  1172. ParserT const& subject,
  1173. ActionT const& actor_)
  1174. : unary<ParserT, parser<access_match_action::action<ParserT, ActionT> > >(subject)
  1175. , actor(actor_)
  1176. {}
  1177. //////////////////////////////////
  1178. template <typename ParserT, typename ActionT>
  1179. template <typename ScannerT>
  1180. typename parser_result<access_match_action::action<ParserT, ActionT>, ScannerT>::type
  1181. access_match_action::action<ParserT, ActionT>::
  1182. parse(ScannerT const& scan) const
  1183. {
  1184. typedef typename ScannerT::iterator_t iterator_t;
  1185. typedef typename parser_result<self_t, ScannerT>::type result_t;
  1186. if (!scan.at_end())
  1187. {
  1188. iterator_t save = scan.first;
  1189. result_t hit = this->subject().parse(scan);
  1190. actor(hit, save, scan.first);
  1191. return hit;
  1192. }
  1193. return scan.no_match();
  1194. }
  1195. //////////////////////////////////
  1196. template <typename ParserT, typename ActionT>
  1197. ActionT const &access_match_action::action<ParserT, ActionT>::predicate() const
  1198. {
  1199. return actor;
  1200. }
  1201. //////////////////////////////////
  1202. const action_directive_parser_gen<access_match_action> access_match_d
  1203. = action_directive_parser_gen<access_match_action>();
  1204. //////////////////////////////////
  1205. // Calls the attached action passing it the node from the parser
  1206. // and the first and last iterators
  1207. // The inner template class is used to simulate template-template parameters
  1208. // (declared in common_fwd.hpp).
  1209. template <typename ParserT, typename ActionT>
  1210. struct access_node_action::action
  1211. : public unary<ParserT, parser<access_node_action::action<ParserT, ActionT> > >
  1212. {
  1213. typedef action_parser_category parser_category;
  1214. typedef action<ParserT, ActionT> self_t;
  1215. template <typename ScannerT>
  1216. struct result
  1217. {
  1218. typedef typename parser_result<ParserT, ScannerT>::type type;
  1219. };
  1220. action( ParserT const& subject,
  1221. ActionT const& actor_);
  1222. template <typename ScannerT>
  1223. typename parser_result<self_t, ScannerT>::type
  1224. parse(ScannerT const& scanner) const;
  1225. ActionT const &predicate() const;
  1226. private:
  1227. ActionT actor;
  1228. };
  1229. //////////////////////////////////
  1230. template <typename ParserT, typename ActionT>
  1231. access_node_action::action<ParserT, ActionT>::action(
  1232. ParserT const& subject,
  1233. ActionT const& actor_)
  1234. : unary<ParserT, parser<access_node_action::action<ParserT, ActionT> > >(subject)
  1235. , actor(actor_)
  1236. {}
  1237. //////////////////////////////////
  1238. template <typename ParserT, typename ActionT>
  1239. template <typename ScannerT>
  1240. typename parser_result<access_node_action::action<ParserT, ActionT>, ScannerT>::type
  1241. access_node_action::action<ParserT, ActionT>::
  1242. parse(ScannerT const& scan) const
  1243. {
  1244. typedef typename ScannerT::iterator_t iterator_t;
  1245. typedef typename parser_result<self_t, ScannerT>::type result_t;
  1246. if (!scan.at_end())
  1247. {
  1248. iterator_t save = scan.first;
  1249. result_t hit = this->subject().parse(scan);
  1250. if (hit && hit.trees.size() > 0)
  1251. actor(*hit.trees.begin(), save, scan.first);
  1252. return hit;
  1253. }
  1254. return scan.no_match();
  1255. }
  1256. //////////////////////////////////
  1257. template <typename ParserT, typename ActionT>
  1258. ActionT const &access_node_action::action<ParserT, ActionT>::predicate() const
  1259. {
  1260. return actor;
  1261. }
  1262. //////////////////////////////////
  1263. const action_directive_parser_gen<access_node_action> access_node_d
  1264. = action_directive_parser_gen<access_node_action>();
  1265. //////////////////////////////////
  1266. ///////////////////////////////////////////////////////////////////////////////
  1267. //
  1268. // tree_parse_info
  1269. //
  1270. // Results returned by the tree parse functions:
  1271. //
  1272. // stop: points to the final parse position (i.e parsing
  1273. // processed the input up to this point).
  1274. //
  1275. // match: true if parsing is successful. This may be full:
  1276. // the parser consumed all the input, or partial:
  1277. // the parser consumed only a portion of the input.
  1278. //
  1279. // full: true when we have a full match (i.e the parser
  1280. // consumed all the input.
  1281. //
  1282. // length: The number of characters consumed by the parser.
  1283. // This is valid only if we have a successful match
  1284. // (either partial or full). A negative value means
  1285. // that the match is unsucessful.
  1286. //
  1287. // trees: Contains the root node(s) of the tree.
  1288. //
  1289. ///////////////////////////////////////////////////////////////////////////////
  1290. template <
  1291. typename IteratorT,
  1292. typename NodeFactoryT,
  1293. typename T
  1294. >
  1295. struct tree_parse_info
  1296. {
  1297. IteratorT stop;
  1298. bool match;
  1299. bool full;
  1300. std::size_t length;
  1301. typename tree_match<IteratorT, NodeFactoryT, T>::container_t trees;
  1302. tree_parse_info()
  1303. : stop()
  1304. , match(false)
  1305. , full(false)
  1306. , length(0)
  1307. , trees()
  1308. {}
  1309. template <typename IteratorT2>
  1310. tree_parse_info(tree_parse_info<IteratorT2> const& pi)
  1311. : stop(pi.stop)
  1312. , match(pi.match)
  1313. , full(pi.full)
  1314. , length(pi.length)
  1315. , trees()
  1316. {
  1317. using std::swap;
  1318. using boost::swap;
  1319. using BOOST_SPIRIT_CLASSIC_NS::swap;
  1320. // use auto_ptr like ownership for the trees data member
  1321. swap(trees, pi.trees);
  1322. }
  1323. tree_parse_info(
  1324. IteratorT stop_,
  1325. bool match_,
  1326. bool full_,
  1327. std::size_t length_,
  1328. typename tree_match<IteratorT, NodeFactoryT, T>::container_t trees_)
  1329. : stop(stop_)
  1330. , match(match_)
  1331. , full(full_)
  1332. , length(length_)
  1333. , trees()
  1334. {
  1335. using std::swap;
  1336. using boost::swap;
  1337. using BOOST_SPIRIT_CLASSIC_NS::swap;
  1338. // use auto_ptr like ownership for the trees data member
  1339. swap(trees, trees_);
  1340. }
  1341. };
  1342. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  1343. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  1344. #endif