literal_char.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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_LITERAL_CHAR_APRIL_16_2006_1051AM)
  7. #define BOOST_SPIRIT_X3_LITERAL_CHAR_APRIL_16_2006_1051AM
  8. #include <boost/spirit/home/x3/char/char_parser.hpp>
  9. #include <boost/spirit/home/x3/support/utility/utf8.hpp>
  10. #include <boost/type_traits/is_same.hpp>
  11. namespace boost { namespace spirit { namespace x3
  12. {
  13. template <typename Encoding, typename Attribute = typename Encoding::char_type>
  14. struct literal_char : char_parser<literal_char<Encoding, Attribute>>
  15. {
  16. typedef typename Encoding::char_type char_type;
  17. typedef Encoding encoding;
  18. typedef Attribute attribute_type;
  19. static bool const has_attribute =
  20. !is_same<unused_type, attribute_type>::value;
  21. template <typename Char>
  22. literal_char(Char ch)
  23. : ch(static_cast<char_type>(ch)) {}
  24. template <typename Char, typename Context>
  25. bool test(Char ch_, Context const& context) const
  26. {
  27. return ((sizeof(Char) <= sizeof(char_type)) || encoding::ischar(ch_))
  28. && (get_case_compare<encoding>(context)(ch, char_type(ch_)) == 0);
  29. }
  30. char_type ch;
  31. };
  32. template <typename Encoding, typename Attribute>
  33. struct get_info<literal_char<Encoding, Attribute>>
  34. {
  35. typedef std::string result_type;
  36. std::string operator()(literal_char<Encoding, Attribute> const& p) const
  37. {
  38. return '\'' + to_utf8(Encoding::toucs4(p.ch)) + '\'';
  39. }
  40. };
  41. }}}
  42. #endif