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/core.hpp> |
---|
10 | #include <boost/spirit/dynamic.hpp> |
---|
11 | #include <boost/spirit/tree/ast.hpp> |
---|
12 | #include <iostream> |
---|
13 | #include <boost/detail/lightweight_test.hpp> |
---|
14 | |
---|
15 | using namespace boost::spirit; |
---|
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 | |
---|
25 | explicit inline var_wrapper(T& t) : parent(t) {} |
---|
26 | |
---|
27 | inline T& operator()() const { return parent::get(); } |
---|
28 | }; |
---|
29 | |
---|
30 | template <typename T> |
---|
31 | inline var_wrapper<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 | |
---|
46 | 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 | } |
---|
55 | const rule <ScannerT, parser_tag<0> >& start() const {return test;} |
---|
56 | }; |
---|
57 | }; |
---|
58 | |
---|
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 | |
---|