bool.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*=============================================================================
  2. Copyright (c) 2009 Hartmut Kaiser
  3. Copyright (c) 2014 Joel de Guzman
  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(SPIRIT_X3_BOOL_SEP_29_2009_0709AM)
  8. #define SPIRIT_X3_BOOL_SEP_29_2009_0709AM
  9. #include <boost/spirit/home/x3/core/parser.hpp>
  10. #include <boost/spirit/home/x3/core/skip_over.hpp>
  11. #include <boost/spirit/home/x3/numeric/bool_policies.hpp>
  12. namespace boost { namespace spirit { namespace x3
  13. {
  14. template <typename T, typename Encoding, typename BoolPolicies = bool_policies<T>>
  15. struct bool_parser : parser<bool_parser<T, Encoding, BoolPolicies>>
  16. {
  17. typedef Encoding encoding;
  18. typedef T attribute_type;
  19. static bool const has_attribute = true;
  20. bool_parser()
  21. : policies() {}
  22. bool_parser(BoolPolicies const& policies)
  23. : policies(policies) {}
  24. template <typename Iterator, typename Context>
  25. bool parse(Iterator& first, Iterator const& last
  26. , Context const& context, unused_type, T& attr) const
  27. {
  28. x3::skip_over(first, last, context);
  29. return policies.parse_true(first, last, attr, get_case_compare<encoding>(context))
  30. || policies.parse_false(first, last, attr, get_case_compare<encoding>(context));
  31. }
  32. template <typename Iterator, typename Context, typename Attribute>
  33. bool parse(Iterator& first, Iterator const& last
  34. , Context const& context, unused_type, Attribute& attr_param) const
  35. {
  36. // this case is called when Attribute is not T
  37. T attr_;
  38. if (parse(first, last, context, unused, attr_))
  39. {
  40. traits::move_to(attr_, attr_param);
  41. return true;
  42. }
  43. return false;
  44. }
  45. BoolPolicies policies;
  46. };
  47. template <typename T, typename Encoding, typename BoolPolicies = bool_policies<T>>
  48. struct literal_bool_parser : parser<bool_parser<T, Encoding, BoolPolicies>>
  49. {
  50. typedef Encoding encoding;
  51. typedef T attribute_type;
  52. static bool const has_attribute = true;
  53. template <typename Value>
  54. literal_bool_parser(Value const& n)
  55. : policies(), n_(n) {}
  56. template <typename Value>
  57. literal_bool_parser(Value const& n, BoolPolicies const& policies)
  58. : policies(policies), n_(n) {}
  59. template <typename Iterator, typename Context>
  60. bool parse_main(Iterator& first, Iterator const& last
  61. , Context const& context, T& attr) const
  62. {
  63. x3::skip_over(first, last, context);
  64. return (n_ && policies.parse_true(first, last, attr, get_case_compare<encoding>(context)))
  65. || (!n_ && policies.parse_false(first, last, attr, get_case_compare<encoding>(context)));
  66. }
  67. template <typename Iterator, typename Context>
  68. bool parse(Iterator& first, Iterator const& last
  69. , Context const& context, unused_type, T& attr) const
  70. {
  71. return parse_main(first, last, context, attr);
  72. }
  73. template <typename Iterator, typename Context, typename Attribute>
  74. bool parse(Iterator& first, Iterator const& last
  75. , Context const& context, unused_type, Attribute& attr_param) const
  76. {
  77. // this case is called when Attribute is not T
  78. T attr_;
  79. if (parse_main(first, last, context, attr_))
  80. {
  81. traits::move_to(attr_, attr_param);
  82. return true;
  83. }
  84. return false;
  85. }
  86. BoolPolicies policies;
  87. T n_;
  88. };
  89. namespace standard
  90. {
  91. typedef bool_parser<bool, char_encoding::standard> bool_type;
  92. bool_type const bool_ = {};
  93. typedef literal_bool_parser<bool, char_encoding::standard> true_type;
  94. true_type const true_ = { true };
  95. typedef literal_bool_parser<bool, char_encoding::standard> false_type;
  96. false_type const false_ = { false };
  97. }
  98. #ifndef BOOST_SPIRIT_NO_STANDARD_WIDE
  99. namespace standard_wide
  100. {
  101. typedef bool_parser<bool, char_encoding::standard_wide> bool_type;
  102. bool_type const bool_ = {};
  103. typedef literal_bool_parser<bool, char_encoding::standard_wide> true_type;
  104. true_type const true_ = { true };
  105. typedef literal_bool_parser<bool, char_encoding::standard_wide> false_type;
  106. false_type const false_ = { false };
  107. }
  108. #endif
  109. namespace ascii
  110. {
  111. typedef bool_parser<bool, char_encoding::ascii> bool_type;
  112. bool_type const bool_ = {};
  113. typedef literal_bool_parser<bool, char_encoding::ascii> true_type;
  114. true_type const true_ = { true };
  115. typedef literal_bool_parser<bool, char_encoding::ascii> false_type;
  116. false_type const false_ = { false };
  117. }
  118. namespace iso8859_1
  119. {
  120. typedef bool_parser<bool, char_encoding::iso8859_1> bool_type;
  121. bool_type const bool_ = {};
  122. typedef literal_bool_parser<bool, char_encoding::iso8859_1> true_type;
  123. true_type const true_ = { true };
  124. typedef literal_bool_parser<bool, char_encoding::iso8859_1> false_type;
  125. false_type const false_ = { false };
  126. }
  127. using standard::bool_;
  128. using standard::true_;
  129. using standard::false_;
  130. }}}
  131. #endif