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