common.hpp 44 KB

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