• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2003 Vaclav Vesely
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 //
10 //  This example demonstrates the behaviour of epsilon_p when used as parser
11 //  generator.
12 //
13 //  The "r" is the rule, which is passed as a subject to the epsilon_p parser
14 //  generator. The "r" is the rule with binded semantic actions. But epsilon_p
15 //  parser generator is intended for looking forward and we don't want to
16 //  invoke semantic actions of subject parser. Hence the epsilon_p uses
17 //  the no_actions policy.
18 //
19 //  Because we want to use the "r" rule both in the epsilon_p and outside of it
20 //  we need the "r" to support two differant scanners, one with no_actions
21 //  action policy and one with the default action policy. To achieve this
22 //  we declare the "r" with the no_actions_scanner_list scanner type.
23 //
24 //-----------------------------------------------------------------------------
25 #define BOOST_SPIRIT_RULE_SCANNERTYPE_LIMIT 2
26 
27 #include <boost/assert.hpp>
28 #include <iostream>
29 #include <boost/cstdlib.hpp>
30 #include <boost/spirit/include/classic_core.hpp>
31 #include <boost/spirit/include/phoenix1.hpp>
32 
33 using namespace std;
34 using namespace boost;
35 using namespace BOOST_SPIRIT_CLASSIC_NS;
36 using namespace phoenix;
37 
38 //-----------------------------------------------------------------------------
39 
main()40 int main()
41 {
42     rule<
43         // Support both the default phrase_scanner_t and the modified version
44         // with no_actions action_policy
45         no_actions_scanner_list<phrase_scanner_t>::type
46     > r;
47 
48     int i(0);
49 
50     r = int_p[var(i) += arg1];
51 
52     parse_info<> info = parse(
53         "1",
54 
55         // r rule is used twice but the semantic action is invoked only once
56         epsilon_p(r) >> r,
57 
58         space_p
59     );
60 
61     BOOST_ASSERT(info.full);
62     // Check, that the semantic action was invoked only once
63     BOOST_ASSERT(i == 1);
64 
65     return exit_success;
66 }
67 
68 //-----------------------------------------------------------------------------
69