confix.hpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*=============================================================================
  2. Copyright (c) 2009 Chris Hoeppler
  3. Copyright (c) 2014 Lee Clagett
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #if !defined(BOOST_SPIRIT_X3_CONFIX_MAY_30_2014_1819PM)
  8. #define BOOST_SPIRIT_X3_CONFIX_MAY_30_2014_1819PM
  9. #include <boost/spirit/home/x3/core/parser.hpp>
  10. namespace boost { namespace spirit { namespace x3
  11. {
  12. template<typename Prefix, typename Subject, typename Postfix>
  13. struct confix_directive :
  14. unary_parser<Subject, confix_directive<Prefix, Subject, Postfix>>
  15. {
  16. typedef unary_parser<
  17. Subject, confix_directive<Prefix, Subject, Postfix>> base_type;
  18. static bool const is_pass_through_unary = true;
  19. static bool const handles_container = Subject::handles_container;
  20. confix_directive(Prefix const& prefix
  21. , Subject const& subject
  22. , Postfix const& postfix) :
  23. base_type(subject),
  24. prefix(prefix),
  25. postfix(postfix)
  26. {
  27. }
  28. template<typename Iterator, typename Context
  29. , typename RContext, typename Attribute>
  30. bool parse(
  31. Iterator& first, Iterator const& last
  32. , Context const& context, RContext& rcontext, Attribute& attr) const
  33. {
  34. Iterator save = first;
  35. if (!(prefix.parse(first, last, context, rcontext, unused) &&
  36. this->subject.parse(first, last, context, rcontext, attr) &&
  37. postfix.parse(first, last, context, rcontext, unused)))
  38. {
  39. first = save;
  40. return false;
  41. }
  42. return true;
  43. }
  44. Prefix prefix;
  45. Postfix postfix;
  46. };
  47. template<typename Prefix, typename Postfix>
  48. struct confix_gen
  49. {
  50. template<typename Subject>
  51. confix_directive<
  52. Prefix, typename extension::as_parser<Subject>::value_type, Postfix>
  53. operator[](Subject const& subject) const
  54. {
  55. return { prefix, as_parser(subject), postfix };
  56. }
  57. Prefix prefix;
  58. Postfix postfix;
  59. };
  60. template<typename Prefix, typename Postfix>
  61. confix_gen<typename extension::as_parser<Prefix>::value_type,
  62. typename extension::as_parser<Postfix>::value_type>
  63. confix(Prefix const& prefix, Postfix const& postfix)
  64. {
  65. return { as_parser(prefix), as_parser(postfix) };
  66. }
  67. }}}
  68. #endif