distinct.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright (c) 2001-2010 Hartmut Kaiser
  2. //
  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. // The purpose of this example is to demonstrate different use cases for the
  6. // distinct parser.
  7. #include <iostream>
  8. #include <string>
  9. #include <vector>
  10. //[qi_distinct_includes
  11. #include <boost/spirit/include/qi.hpp>
  12. #include <boost/spirit/repository/include/qi_distinct.hpp>
  13. //]
  14. //[qi_distinct_namespace
  15. using namespace boost::spirit;
  16. using namespace boost::spirit::ascii;
  17. using boost::spirit::repository::distinct;
  18. //]
  19. int main()
  20. {
  21. //[qi_distinct_description_ident
  22. {
  23. std::string str("description ident");
  24. std::string::iterator first(str.begin());
  25. bool r = qi::phrase_parse(first, str.end()
  26. , distinct(alnum | '_')["description"] >> -lit("--") >> +(alnum | '_')
  27. , space);
  28. BOOST_ASSERT(r && first == str.end());
  29. }
  30. //]
  31. //[qi_distinct_description__ident
  32. {
  33. std::string str("description--ident");
  34. std::string::iterator first(str.begin());
  35. bool r = qi::phrase_parse(first, str.end()
  36. , distinct(alnum | '_')["description"] >> -lit("--") >> +(alnum | '_')
  37. , space);
  38. BOOST_ASSERT(r && first == str.end());
  39. }
  40. //]
  41. //[qi_distinct_description_ident_error
  42. {
  43. std::string str("description-ident");
  44. std::string::iterator first(str.begin());
  45. bool r = qi::phrase_parse(first, str.end()
  46. , distinct(alnum | '_')["description"] >> -lit("--") >> +(alnum | '_')
  47. , space);
  48. BOOST_ASSERT(!r && first == str.begin());
  49. }
  50. //]
  51. return 0;
  52. }