rule3.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  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. =============================================================================*/
  6. #include <boost/detail/lightweight_test.hpp>
  7. #include <boost/spirit/include/qi_operator.hpp>
  8. #include <boost/spirit/include/qi_char.hpp>
  9. #include <boost/spirit/include/qi_string.hpp>
  10. #include <boost/spirit/include/qi_numeric.hpp>
  11. #include <boost/spirit/include/qi_auxiliary.hpp>
  12. #include <boost/spirit/include/qi_directive.hpp>
  13. #include <boost/spirit/include/qi_nonterminal.hpp>
  14. #include <boost/spirit/include/qi_action.hpp>
  15. #include <boost/spirit/include/phoenix_core.hpp>
  16. #include <boost/spirit/include/phoenix_operator.hpp>
  17. #include <boost/fusion/include/std_pair.hpp>
  18. #include <string>
  19. #include <cstring>
  20. #include <iostream>
  21. #include "test.hpp"
  22. int
  23. main()
  24. {
  25. using spirit_test::test_attr;
  26. using spirit_test::test;
  27. using namespace boost::spirit::ascii;
  28. using namespace boost::spirit::qi::labels;
  29. using boost::spirit::qi::locals;
  30. using boost::spirit::qi::rule;
  31. using boost::spirit::qi::int_;
  32. using boost::spirit::qi::uint_;
  33. using boost::spirit::qi::fail;
  34. using boost::spirit::qi::on_error;
  35. using boost::spirit::qi::debug;
  36. using boost::spirit::qi::lit;
  37. namespace phx = boost::phoenix;
  38. { // synth attribute value-init
  39. std::string s;
  40. rule<char const*, char()> r;
  41. r = alpha[_val += _1];
  42. BOOST_TEST(test_attr("abcdef", +r, s));
  43. BOOST_TEST(s == "abcdef");
  44. }
  45. { // auto rules aliasing tests
  46. char ch = '\0';
  47. rule<char const*, char()> a, b;
  48. a %= b;
  49. b %= alpha;
  50. BOOST_TEST(test("x", a[phx::ref(ch) = _1]));
  51. BOOST_TEST(ch == 'x');
  52. ch = '\0';
  53. BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
  54. BOOST_TEST(ch == 'z');
  55. a = b; // test deduced auto rule behavior
  56. b = alpha;
  57. ch = '\0';
  58. BOOST_TEST(test("x", a[phx::ref(ch) = _1]));
  59. BOOST_TEST(ch == 'x');
  60. ch = '\0';
  61. BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
  62. BOOST_TEST(ch == 'z');
  63. }
  64. { // context (w/arg) tests
  65. char ch;
  66. rule<char const*, char(int)> a; // 1 arg
  67. a = alpha[_val = _1 + _r1];
  68. BOOST_TEST(test("x", a(phx::val(1))[phx::ref(ch) = _1]));
  69. BOOST_TEST(ch == 'x' + 1);
  70. BOOST_TEST(test_attr("a", a(1), ch)); // allow scalars as rule args too.
  71. BOOST_TEST(ch == 'a' + 1);
  72. rule<char const*, char(int, int)> b; // 2 args
  73. b = alpha[_val = _1 + _r1 + _r2];
  74. BOOST_TEST(test_attr("a", b(1, 2), ch));
  75. BOOST_TEST(ch == 'a' + 1 + 2);
  76. }
  77. { // context (w/ reference arg) tests
  78. char ch;
  79. rule<char const*, void(char&)> a; // 1 arg (reference)
  80. a = alpha[_r1 = _1];
  81. BOOST_TEST(test("x", a(phx::ref(ch))));
  82. BOOST_TEST(ch == 'x');
  83. }
  84. { // context (w/locals) tests
  85. rule<char const*, locals<char> > a; // 1 local
  86. a = alpha[_a = _1] >> char_(_a);
  87. BOOST_TEST(test("aa", a));
  88. BOOST_TEST(!test("ax", a));
  89. }
  90. { // context (w/args and locals) tests
  91. rule<char const*, void(int), locals<char> > a; // 1 arg + 1 local
  92. a = alpha[_a = _1 + _r1] >> char_(_a);
  93. BOOST_TEST(test("ab", a(phx::val(1))));
  94. BOOST_TEST(test("xy", a(phx::val(1))));
  95. BOOST_TEST(!test("ax", a(phx::val(1))));
  96. }
  97. { // void() has unused type (void == unused_type)
  98. std::pair<int, char> attr;
  99. rule<char const*, void()> r;
  100. r = char_;
  101. BOOST_TEST(test_attr("123ax", int_ >> char_ >> r, attr));
  102. BOOST_TEST(attr.first == 123);
  103. BOOST_TEST(attr.second == 'a');
  104. }
  105. { // bug: test that injected attributes are ok
  106. rule<char const*, char(int) > r;
  107. // problem code:
  108. r = char_(_r1)[_val = _1];
  109. }
  110. return boost::report_errors();
  111. }