iter_pos_parser.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 show how a simple custom primitive parser
  6. // component can be written. We develop a custom parser exposing the current
  7. // iterator position as its attribute.
  8. //
  9. // For more information see: http://spirit.sourceforge.net/home/?page_id=567
  10. #include <boost/spirit/include/qi_parse_attr.hpp>
  11. #include <boost/spirit/include/qi_char.hpp>
  12. #include <boost/spirit/include/qi_operator.hpp>
  13. #include <string>
  14. #include "iter_pos.hpp"
  15. namespace qi = boost::spirit::qi;
  16. int main()
  17. {
  18. using custom_parser::iter_pos;
  19. std::string prefix, suffix; // attributes receiving the
  20. std::string::iterator position; // parsed values
  21. std::string input("prefix1234567");
  22. std::string::iterator first = input.begin();
  23. bool result =
  24. qi::parse(first, input.end()
  25. , +qi::alpha >> iter_pos >> +qi::digit
  26. , prefix, position, suffix);
  27. if (result)
  28. {
  29. std::cout << "-------------------------------- \n";
  30. std::cout << "Parsing succeeded\n";
  31. std::cout << "prefix is: " << prefix << "\n";
  32. std::cout << "suffix is: " << suffix << "\n";
  33. std::cout << "position is: " << std::distance(input.begin(), position) << "\n";
  34. std::cout << "-------------------------------- \n";
  35. }
  36. else
  37. {
  38. std::cout << "-------------------------------- \n";
  39. std::cout << "Parsing failed\n";
  40. std::cout << "-------------------------------- \n";
  41. }
  42. return 0;
  43. }