char_class.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_CHAR_CLASS_APRIL_16_2006_1051AM)
  7. #define BOOST_SPIRIT_X3_CHAR_CLASS_APRIL_16_2006_1051AM
  8. #include <boost/spirit/home/x3/char/char_parser.hpp>
  9. #include <boost/spirit/home/x3/char/detail/cast_char.hpp>
  10. #include <boost/spirit/home/support/char_encoding/standard.hpp>
  11. #include <boost/spirit/home/support/char_encoding/standard_wide.hpp>
  12. #include <boost/spirit/home/support/char_encoding/ascii.hpp>
  13. #include <boost/spirit/home/support/char_encoding/iso8859_1.hpp>
  14. #include <boost/spirit/home/x3/char/char_class_tags.hpp>
  15. namespace boost { namespace spirit { namespace x3
  16. {
  17. ///////////////////////////////////////////////////////////////////////////
  18. template <typename Encoding>
  19. struct char_class_base
  20. {
  21. typedef typename Encoding::char_type char_type;
  22. #define BOOST_SPIRIT_X3_CLASSIFY(name) \
  23. template <typename Char> \
  24. static bool \
  25. is(name##_tag, Char ch) \
  26. { \
  27. return Encoding::is##name \
  28. BOOST_PREVENT_MACRO_SUBSTITUTION \
  29. (detail::cast_char<char_type>(ch)); \
  30. } \
  31. /***/
  32. BOOST_SPIRIT_X3_CLASSIFY(char)
  33. BOOST_SPIRIT_X3_CLASSIFY(alnum)
  34. BOOST_SPIRIT_X3_CLASSIFY(alpha)
  35. BOOST_SPIRIT_X3_CLASSIFY(digit)
  36. BOOST_SPIRIT_X3_CLASSIFY(xdigit)
  37. BOOST_SPIRIT_X3_CLASSIFY(cntrl)
  38. BOOST_SPIRIT_X3_CLASSIFY(graph)
  39. BOOST_SPIRIT_X3_CLASSIFY(lower)
  40. BOOST_SPIRIT_X3_CLASSIFY(print)
  41. BOOST_SPIRIT_X3_CLASSIFY(punct)
  42. BOOST_SPIRIT_X3_CLASSIFY(space)
  43. BOOST_SPIRIT_X3_CLASSIFY(blank)
  44. BOOST_SPIRIT_X3_CLASSIFY(upper)
  45. #undef BOOST_SPIRIT_X3_CLASSIFY
  46. };
  47. template <typename Encoding, typename Tag>
  48. struct char_class
  49. : char_parser<char_class<Encoding, Tag>>
  50. {
  51. typedef Encoding encoding;
  52. typedef Tag tag;
  53. typedef typename Encoding::char_type char_type;
  54. typedef char_type attribute_type;
  55. static bool const has_attribute = true;
  56. template <typename Char, typename Context>
  57. bool test(Char ch, Context const& context) const
  58. {
  59. return encoding::ischar(ch)
  60. && char_class_base<Encoding>::is(
  61. get_case_compare<Encoding>(context).get_char_class_tag(tag()), ch);
  62. }
  63. };
  64. #define BOOST_SPIRIT_X3_CHAR_CLASS(encoding, name) \
  65. typedef char_class<char_encoding::encoding, name##_tag> name##_type; \
  66. name##_type const name = name##_type(); \
  67. /***/
  68. #define BOOST_SPIRIT_X3_CHAR_CLASSES(encoding) \
  69. namespace encoding \
  70. { \
  71. BOOST_SPIRIT_X3_CHAR_CLASS(encoding, alnum) \
  72. BOOST_SPIRIT_X3_CHAR_CLASS(encoding, alpha) \
  73. BOOST_SPIRIT_X3_CHAR_CLASS(encoding, digit) \
  74. BOOST_SPIRIT_X3_CHAR_CLASS(encoding, xdigit) \
  75. BOOST_SPIRIT_X3_CHAR_CLASS(encoding, cntrl) \
  76. BOOST_SPIRIT_X3_CHAR_CLASS(encoding, graph) \
  77. BOOST_SPIRIT_X3_CHAR_CLASS(encoding, lower) \
  78. BOOST_SPIRIT_X3_CHAR_CLASS(encoding, print) \
  79. BOOST_SPIRIT_X3_CHAR_CLASS(encoding, punct) \
  80. BOOST_SPIRIT_X3_CHAR_CLASS(encoding, space) \
  81. BOOST_SPIRIT_X3_CHAR_CLASS(encoding, blank) \
  82. BOOST_SPIRIT_X3_CHAR_CLASS(encoding, upper) \
  83. } \
  84. /***/
  85. BOOST_SPIRIT_X3_CHAR_CLASSES(standard)
  86. #ifndef BOOST_SPIRIT_NO_STANDARD_WIDE
  87. BOOST_SPIRIT_X3_CHAR_CLASSES(standard_wide)
  88. #endif
  89. BOOST_SPIRIT_X3_CHAR_CLASSES(ascii)
  90. BOOST_SPIRIT_X3_CHAR_CLASSES(iso8859_1)
  91. #undef BOOST_SPIRIT_X3_CHAR_CLASS
  92. #undef BOOST_SPIRIT_X3_CHAR_CLASSES
  93. using standard::alnum_type;
  94. using standard::alpha_type;
  95. using standard::digit_type;
  96. using standard::xdigit_type;
  97. using standard::cntrl_type;
  98. using standard::graph_type;
  99. using standard::lower_type;
  100. using standard::print_type;
  101. using standard::punct_type;
  102. using standard::space_type;
  103. using standard::blank_type;
  104. using standard::upper_type;
  105. using standard::alnum;
  106. using standard::alpha;
  107. using standard::digit;
  108. using standard::xdigit;
  109. using standard::cntrl;
  110. using standard::graph;
  111. using standard::lower;
  112. using standard::print;
  113. using standard::punct;
  114. using standard::space;
  115. using standard::blank;
  116. using standard::upper;
  117. }}}
  118. #endif