negated_char_parser.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*=============================================================================
  2. Copyright (c) 2001-2014 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #if !defined(BOOST_SPIRIT_X3_NEGATED_CHAR_PARSER_APR_16_2006_0906AM)
  7. #define BOOST_SPIRIT_X3_NEGATED_CHAR_PARSER_APR_16_2006_0906AM
  8. #include <boost/spirit/home/x3/support/traits/attribute_of.hpp>
  9. #include <boost/spirit/home/x3/support/traits/has_attribute.hpp>
  10. #include <boost/spirit/home/x3/char/char_parser.hpp>
  11. namespace boost { namespace spirit { namespace x3
  12. {
  13. ///////////////////////////////////////////////////////////////////////////
  14. // negated_char_parser handles ~cp expressions (cp is a char_parser)
  15. ///////////////////////////////////////////////////////////////////////////
  16. template <typename Positive>
  17. struct negated_char_parser :
  18. char_parser<negated_char_parser<Positive>>
  19. {
  20. negated_char_parser(Positive const& positive)
  21. : positive(positive) {}
  22. template <typename CharParam, typename Context>
  23. bool test(CharParam ch, Context const& context) const
  24. {
  25. return !positive.test(ch, context);
  26. }
  27. Positive positive;
  28. };
  29. template <typename Positive>
  30. inline negated_char_parser<Positive>
  31. operator~(char_parser<Positive> const& cp)
  32. {
  33. return { cp.derived() };
  34. }
  35. template <typename Positive>
  36. inline Positive const&
  37. operator~(negated_char_parser<Positive> const& cp)
  38. {
  39. return cp.positive;
  40. }
  41. }}}
  42. namespace boost { namespace spirit { namespace x3 { namespace traits
  43. {
  44. template <typename Positive, typename Context>
  45. struct attribute_of<x3::negated_char_parser<Positive>, Context>
  46. : attribute_of<Positive, Context> {};
  47. template <typename Positive, typename Context>
  48. struct has_attribute<x3::negated_char_parser<Positive>, Context>
  49. : has_attribute<Positive, Context> {};
  50. }}}}
  51. #endif