if_p_int_as_condition_test.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*=============================================================================
  2. Copyright (c) 2004 Martin Wille
  3. http://spirit.sourceforge.net/
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #include <boost/spirit/include/classic_core.hpp>
  9. #include <boost/spirit/include/classic_dynamic.hpp>
  10. #include <boost/spirit/include/classic_ast.hpp>
  11. #include <iostream>
  12. #include <boost/detail/lightweight_test.hpp>
  13. using namespace BOOST_SPIRIT_CLASSIC_NS;
  14. int the_var_to_be_tested = 0;
  15. namespace local
  16. {
  17. template <typename T>
  18. struct var_wrapper : public ::boost::reference_wrapper<T>
  19. {
  20. typedef ::boost::reference_wrapper<T> parent;
  21. explicit inline var_wrapper(T& t) : parent(t) {}
  22. inline T& operator()() const { return parent::get(); }
  23. };
  24. template <typename T>
  25. inline var_wrapper<T>
  26. var(T& t)
  27. {
  28. return var_wrapper<T>(t);
  29. }
  30. }
  31. struct test_grammar : public grammar <test_grammar>
  32. {
  33. template <typename ScannerT>
  34. struct definition
  35. {
  36. rule <ScannerT, parser_tag <0> > test;
  37. definition(const test_grammar& /*self*/)
  38. {
  39. test
  40. = if_p(local::var(the_var_to_be_tested))
  41. [
  42. real_p
  43. ];
  44. }
  45. const rule <ScannerT, parser_tag<0> >& start() const {return test;}
  46. };
  47. };
  48. int main()
  49. {
  50. test_grammar gram;
  51. tree_parse_info <const char*> result;
  52. //predictably fails
  53. the_var_to_be_tested = 0;
  54. result = ast_parse("1.50", gram, space_p);
  55. std::cout << "Testing if_p against: " << the_var_to_be_tested << std::endl;
  56. std::cout << "success: " << result.full << std::endl;
  57. BOOST_TEST(!result.full);
  58. //predicatably succeeds
  59. the_var_to_be_tested = 1;
  60. result = ast_parse("1.50", gram, space_p);
  61. std::cout << "Testing if_p against: " << the_var_to_be_tested << std::endl;
  62. std::cout << "success: " << result.full << std::endl;
  63. BOOST_TEST(result.full);
  64. //should succeed
  65. the_var_to_be_tested = 2;
  66. result = ast_parse("1.50", gram, space_p);
  67. std::cout << "Testing if_p against: " << the_var_to_be_tested << std::endl;
  68. std::cout << "success: " << result.full << std::endl;
  69. BOOST_TEST(result.full);
  70. return boost::report_errors();
  71. }