fail_at_first_char_expected.qbk 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. [#fail_at_first_char_expected]
  2. [section fail_at_first_char_expected]
  3. [h1 Synopsis]
  4. template <class P>
  5. struct fail_at_first_char_expected;
  6. This is a [link parser_combinator parser combinator].
  7. [table Arguments
  8. [[Name] [Type]]
  9. [[`P`] [[link parser parser]]]
  10. ]
  11. [h1 Description]
  12. It tries to parse the input using `P`. When `P` rejects the input without
  13. consuming any character, `fail_at_first_char_expected` accepts the input.
  14. Otherwise (when `P` accepts the input or when it consumes characters before
  15. rejecting the input) `fail_at_first_char_expected` rejects the input.
  16. [h1 Header]
  17. #include <boost/metaparse/fail_at_first_char_expected.hpp>
  18. [h1 Expression semantics]
  19. For any `p` parser, `s` compile-time string and `pos` source position:
  20. When `is_error<p::apply<s, pos>>::type` is false, the following are equivalent:
  21. fail_at_first_char_expected<p>::apply<s, pos>::type
  22. reject<error::expected_to_fail, pos>
  23. When `is_error<p::apply<s, pos>>::type` is true and
  24. `boost::mpl::equal_to<pos, get_position<p::apply<s, pos>>::type>::type` is also
  25. true, the following are equivalent:
  26. get_remaining<except<p, c, msg>, s, pos>::type
  27. accept</* unspecified */, s, pos>
  28. Otherwise the following are equivalent:
  29. get_remaining<except<p, c, msg>, s, pos>::type
  30. p::apply<s, pos>::type
  31. [h1 Example]
  32. #include <boost/metaparse/fail_at_first_char_expected.hpp>
  33. #include <boost/metaparse/int_.hpp>
  34. #include <boost/metaparse/lit_c.hpp>
  35. #include <boost/metaparse/foldl_start_with_parser.hpp>
  36. #include <boost/metaparse/first_of.hpp>
  37. #include <boost/metaparse/last_of.hpp>
  38. #include <boost/metaparse/string.hpp>
  39. #include <boost/metaparse/start.hpp>
  40. #include <boost/metaparse/is_error.hpp>
  41. #include <boost/metaparse/get_result.hpp>
  42. #include <boost/mpl/lambda.hpp>
  43. #include <boost/mpl/plus.hpp>
  44. using namespace boost::metaparse;
  45. using plus_int = last_of<lit_c<'+'>, int_>;
  46. using plus_exp =
  47. first_of<
  48. foldl_start_with_parser<
  49. plus_int,
  50. int_,
  51. boost::mpl::lambda<boost::mpl::plus<boost::mpl::_1, boost::mpl::_2>>::type
  52. >,
  53. fail_at_first_char_expected<plus_int>
  54. >;
  55. static_assert(
  56. get_result<
  57. plus_exp::apply<BOOST_METAPARSE_STRING("1+2+3"), start>
  58. >::type::value == 6,
  59. "it should accept the input when it is a list of '+' separated ints"
  60. );
  61. static_assert(
  62. is_error<
  63. plus_exp::apply<BOOST_METAPARSE_STRING("1+2+3+"), start>
  64. >::type::value,
  65. "it should reject the input when it has an extra +"
  66. );
  67. [endsect]