grammar.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright (c) 2001-2011 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. #include <boost/config/warning_disable.hpp>
  6. #include <boost/detail/lightweight_test.hpp>
  7. #include <boost/spirit/include/karma_operator.hpp>
  8. #include <boost/spirit/include/karma_char.hpp>
  9. #include <boost/spirit/include/karma_string.hpp>
  10. #include <boost/spirit/include/karma_numeric.hpp>
  11. #include <boost/spirit/include/karma_nonterminal.hpp>
  12. #include <boost/spirit/include/karma_action.hpp>
  13. #include <string>
  14. #include <iostream>
  15. #include "test.hpp"
  16. using namespace spirit_test;
  17. using namespace boost::spirit;
  18. using namespace boost::spirit::ascii;
  19. typedef spirit_test::output_iterator<char>::type outiter_type;
  20. struct num_list : karma::grammar<outiter_type, space_type>
  21. {
  22. num_list() : num_list::base_type(start)
  23. {
  24. using boost::spirit::int_;
  25. num1 = int_(123);
  26. num2 = int_(456);
  27. num3 = int_(789);
  28. start = num1 << ',' << num2 << ',' << num3;
  29. }
  30. karma::rule<outiter_type, space_type> start, num1, num2, num3;
  31. };
  32. int
  33. main()
  34. {
  35. { // simple grammar test
  36. num_list nlist;
  37. BOOST_TEST(test_delimited("123 , 456 , 789 ", nlist, space));
  38. }
  39. { // direct access to the rules
  40. num_list def;
  41. BOOST_TEST(test_delimited("123 ", def.num1, space));
  42. BOOST_TEST(test_delimited("123 , 456 , 789 ", def.start, space));
  43. }
  44. return boost::report_errors();
  45. }