1 /*=============================================================================
2 Boost.Wave: A Standard compliant C++ preprocessor library
6 Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
7 Software License, Version 1.0. (See accompanying file
8 LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 =============================================================================*/
11 #if !defined(CPP_INTLIT_GRAMMAR_HPP_2E1E70B1_F15C_4132_8554_10A231B0D91C_INCLUDED)
12 #define CPP_INTLIT_GRAMMAR_HPP_2E1E70B1_F15C_4132_8554_10A231B0D91C_INCLUDED
14 #include <boost/wave/wave_config.hpp>
16 #include <boost/spirit/include/classic_core.hpp>
17 #include <boost/spirit/include/classic_closure.hpp>
18 #include <boost/spirit/include/classic_assign_actor.hpp>
19 #include <boost/spirit/include/classic_push_back_actor.hpp>
21 #include <boost/spirit/include/phoenix1_operators.hpp>
22 #include <boost/spirit/include/phoenix1_primitives.hpp>
23 #include <boost/spirit/include/phoenix1_statements.hpp>
25 #include <boost/wave/cpp_exceptions.hpp>
26 #include <boost/wave/grammars/cpp_literal_grammar_gen.hpp>
28 #if !defined(spirit_append_actor)
29 #define spirit_append_actor(actor) boost::spirit::classic::push_back_a(actor)
30 #define spirit_assign_actor(actor) boost::spirit::classic::assign_a(actor)
31 #endif // !defined(spirit_append_actor)
33 // this must occur after all of the includes and before any code appears
34 #ifdef BOOST_HAS_ABI_HEADERS
35 #include BOOST_ABI_PREFIX
38 ///////////////////////////////////////////////////////////////////////////////
40 // Reusable grammar for parsing of C++ style integer literals
42 ///////////////////////////////////////////////////////////////////////////////
47 ///////////////////////////////////////////////////////////////////////////////
51 : boost::spirit::classic::closure<intlit_closure, uint_literal_type>
57 ///////////////////////////////////////////////////////////////////////////////
58 // define, whether the rule's should generate some debug output
59 #define TRACE_INTLIT_GRAMMAR \
60 bool(BOOST_SPIRIT_DEBUG_FLAGS_CPP & BOOST_SPIRIT_DEBUG_FLAGS_INTLIT_GRAMMAR) \
63 struct intlit_grammar :
64 boost::spirit::classic::grammar<intlit_grammar, closures::intlit_closure::context_t>
66 intlit_grammar(bool &is_unsigned_) : is_unsigned(is_unsigned_)
68 BOOST_SPIRIT_DEBUG_TRACE_GRAMMAR_NAME(*this, "intlit_grammar",
69 TRACE_INTLIT_GRAMMAR);
72 template <typename ScannerT>
75 typedef boost::spirit::classic::rule<ScannerT> rule_t;
78 boost::spirit::classic::subrule<0> sub_int_lit;
79 boost::spirit::classic::subrule<1> oct_lit;
80 boost::spirit::classic::subrule<2> hex_lit;
81 boost::spirit::classic::subrule<3> dec_lit;
83 definition(intlit_grammar const &self)
85 using namespace boost::spirit::classic;
86 namespace phx = phoenix;
91 ( ch_p('0')[self.val = 0] >> (hex_lit | oct_lit)
95 (ch_p('u')[phx::var(self.is_unsigned) = true] || ch_p('l'))
96 | (ch_p('l') || ch_p('u')[phx::var(self.is_unsigned) = true])
101 (ch_p('X') | ch_p('x'))
102 >> uint_parser<uint_literal_type, 16>()
104 self.val = phx::arg1,
105 phx::var(self.is_unsigned) = true
110 !uint_parser<uint_literal_type, 8>()
112 self.val = phx::arg1,
113 phx::var(self.is_unsigned) = true
118 uint_parser<uint_literal_type, 10>()
125 BOOST_SPIRIT_DEBUG_TRACE_RULE(int_lit, TRACE_INTLIT_GRAMMAR);
126 BOOST_SPIRIT_DEBUG_TRACE_RULE(sub_int_lit, TRACE_INTLIT_GRAMMAR);
127 BOOST_SPIRIT_DEBUG_TRACE_RULE(hex_lit, TRACE_INTLIT_GRAMMAR);
128 BOOST_SPIRIT_DEBUG_TRACE_RULE(oct_lit, TRACE_INTLIT_GRAMMAR);
129 BOOST_SPIRIT_DEBUG_TRACE_RULE(dec_lit, TRACE_INTLIT_GRAMMAR);
132 // start rule of this grammar
133 rule_t const& start() const
140 #undef TRACE_INTLIT_GRAMMAR
142 ///////////////////////////////////////////////////////////////////////////////
144 // The following function is defined here, to allow the separation of
145 // the compilation of the intlit_grammar from the function using it.
147 ///////////////////////////////////////////////////////////////////////////////
149 #if BOOST_WAVE_SEPARATE_GRAMMAR_INSTANTIATION != 0
150 #define BOOST_WAVE_INTLITGRAMMAR_GEN_INLINE
152 #define BOOST_WAVE_INTLITGRAMMAR_GEN_INLINE inline
155 template <typename TokenT>
156 BOOST_WAVE_INTLITGRAMMAR_GEN_INLINE
158 intlit_grammar_gen<TokenT>::evaluate(TokenT const &token,
161 using namespace boost::spirit::classic;
163 intlit_grammar g(is_unsigned);
164 uint_literal_type result = 0;
165 typename TokenT::string_type const &token_val = token.get_value();
166 parse_info<typename TokenT::string_type::const_iterator> hit =
167 parse(token_val.begin(), token_val.end(), g[spirit_assign_actor(result)]);
170 BOOST_WAVE_THROW(preprocess_exception, ill_formed_integer_literal,
171 token_val.c_str(), token.get_position());
176 #undef BOOST_WAVE_INTLITGRAMMAR_GEN_INLINE
178 ///////////////////////////////////////////////////////////////////////////////
179 } // namespace grammars
183 // the suffix header occurs after all of the code
184 #ifdef BOOST_HAS_ABI_HEADERS
185 #include BOOST_ABI_SUFFIX
188 #endif // !defined(CPP_INTLIT_GRAMMAR_HPP_2E1E70B1_F15C_4132_8554_10A231B0D91C_INCLUDED)