literal_char.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 get_case_compare<encoding>(context)(ch, char_type(ch_)) == 0;
  28. }
  29. char_type ch;
  30. };
  31. template <typename Encoding, typename Attribute>
  32. struct get_info<literal_char<Encoding, Attribute>>
  33. {
  34. typedef std::string result_type;
  35. std::string operator()(literal_char<Encoding, Attribute> const& p) const
  36. {
  37. return '\'' + to_utf8(Encoding::toucs4(p.ch)) + '\'';
  38. }
  39. };
  40. }}}
  41. #endif