Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/boost/wave/grammars/cpp_intlit_grammar.hpp @ 33

Last change on this file since 33 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 6.4 KB
Line 
1/*=============================================================================
2    Boost.Wave: A Standard compliant C++ preprocessor library
3
4    http://www.boost.org/
5
6    Copyright (c) 2001-2007 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=============================================================================*/
10
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
13
14#include <boost/spirit/core.hpp>
15#include <boost/spirit/attribute/closure.hpp>
16#if SPIRIT_VERSION >= 0x1700
17#include <boost/spirit/actor/assign_actor.hpp>
18#include <boost/spirit/actor/push_back_actor.hpp>
19#endif // SPIRIT_VERSION >= 0x1700
20
21#include <boost/spirit/phoenix/operators.hpp>
22#include <boost/spirit/phoenix/primitives.hpp>
23#include <boost/spirit/phoenix/statements.hpp>
24
25#include <boost/wave/wave_config.hpp>
26#include <boost/wave/cpp_exceptions.hpp>
27#include <boost/wave/grammars/cpp_literal_grammar_gen.hpp>
28
29#if !defined(spirit_append_actor)
30#if SPIRIT_VERSION >= 0x1700
31#define spirit_append_actor(actor) boost::spirit::push_back_a(actor)
32#define spirit_assign_actor(actor) boost::spirit::assign_a(actor)
33#else
34#define spirit_append_actor(actor) boost::spirit::append(actor)
35#define spirit_assign_actor(actor) boost::spirit::assign(actor)
36#endif // SPIRIT_VERSION >= 0x1700
37#endif // !defined(spirit_append_actor)
38
39// this must occur after all of the includes and before any code appears
40#ifdef BOOST_HAS_ABI_HEADERS
41#include BOOST_ABI_PREFIX
42#endif
43
44///////////////////////////////////////////////////////////////////////////////
45//
46//  Reusable grammar for parsing of C++ style integer literals
47//
48///////////////////////////////////////////////////////////////////////////////
49namespace boost {
50namespace wave { 
51namespace grammars {
52
53namespace closures {
54
55    struct intlit_closure 
56    :   boost::spirit::closure<intlit_closure, unsigned long> 
57    {
58        member1 val;
59    };
60}
61
62///////////////////////////////////////////////////////////////////////////////
63//  define, whether the rule's should generate some debug output
64#define TRACE_INTLIT_GRAMMAR \
65    bool(BOOST_SPIRIT_DEBUG_FLAGS_CPP & BOOST_SPIRIT_DEBUG_FLAGS_INTLIT_GRAMMAR) \
66    /**/
67
68struct intlit_grammar :
69    boost::spirit::grammar<intlit_grammar, closures::intlit_closure::context_t>
70{
71    intlit_grammar(bool &is_unsigned_) : is_unsigned(is_unsigned_)
72    {
73        BOOST_SPIRIT_DEBUG_TRACE_GRAMMAR_NAME(*this, "intlit_grammar", 
74            TRACE_INTLIT_GRAMMAR);
75    }
76   
77    template <typename ScannerT>
78    struct definition
79    {
80        typedef boost::spirit::rule<ScannerT> rule_t;
81
82        rule_t int_lit;
83        boost::spirit::subrule<0> sub_int_lit;
84        boost::spirit::subrule<1> oct_lit;
85        boost::spirit::subrule<2> hex_lit;
86        boost::spirit::subrule<3> dec_lit;
87
88        definition(intlit_grammar const &self)
89        {
90            using namespace boost::spirit;
91            using namespace phoenix;
92           
93            int_lit = (
94                    sub_int_lit = 
95                        (    ch_p('0')[self.val = 0] >> (hex_lit | oct_lit)
96                        |   dec_lit
97                        )
98                        >> !as_lower_d[
99                                (ch_p('u')[var(self.is_unsigned) = true] || ch_p('l')) 
100                            |   (ch_p('l') || ch_p('u')[var(self.is_unsigned) = true])
101                            ]
102                    ,
103
104                    hex_lit =
105                            (ch_p('X') | ch_p('x'))
106                        >>  uint_parser<unsigned long, 16>()
107                            [
108                                self.val = arg1,
109                                var(self.is_unsigned) = true
110                            ]
111                    ,
112                       
113                    oct_lit =
114                       !uint_parser<unsigned long, 8>()
115                        [
116                            self.val = arg1,
117                            var(self.is_unsigned) = true
118                        ]
119                    ,
120                       
121                    dec_lit =
122                        int_parser<long, 10>()
123                        [
124                            self.val = arg1
125                        ]
126                    )
127                ;
128               
129            BOOST_SPIRIT_DEBUG_TRACE_RULE(int_lit, TRACE_INTLIT_GRAMMAR);
130            BOOST_SPIRIT_DEBUG_TRACE_RULE(sub_int_lit, TRACE_INTLIT_GRAMMAR);
131            BOOST_SPIRIT_DEBUG_TRACE_RULE(hex_lit, TRACE_INTLIT_GRAMMAR);
132            BOOST_SPIRIT_DEBUG_TRACE_RULE(oct_lit, TRACE_INTLIT_GRAMMAR);
133            BOOST_SPIRIT_DEBUG_TRACE_RULE(dec_lit, TRACE_INTLIT_GRAMMAR);
134        }
135
136    // start rule of this grammar
137        rule_t const& start() const
138        { return int_lit; }
139    };
140   
141    bool &is_unsigned;
142};
143
144#undef TRACE_INTLIT_GRAMMAR
145
146///////////////////////////////////////////////////////////////////////////////
147// 
148//  The following function is defined here, to allow the separation of
149//  the compilation of the intlit_grammar from the function using it.
150// 
151///////////////////////////////////////////////////////////////////////////////
152
153#if BOOST_WAVE_SEPARATE_GRAMMAR_INSTANTIATION != 0
154#define BOOST_WAVE_INTLITGRAMMAR_GEN_INLINE
155#else
156#define BOOST_WAVE_INTLITGRAMMAR_GEN_INLINE inline
157#endif
158
159template <typename TokenT>
160BOOST_WAVE_INTLITGRAMMAR_GEN_INLINE
161unsigned long 
162intlit_grammar_gen<TokenT>::evaluate(TokenT const &token, 
163    bool &is_unsigned)
164{
165    using namespace boost::spirit;
166   
167intlit_grammar g(is_unsigned);
168unsigned long result = 0;
169typename TokenT::string_type const &token_val = token.get_value();
170parse_info<typename TokenT::string_type::const_iterator> hit =
171    parse(token_val.begin(), token_val.end(), g[spirit_assign_actor(result)]);
172
173    if (!hit.hit) {
174        BOOST_WAVE_THROW(preprocess_exception, ill_formed_integer_literal, 
175            token_val.c_str(), token.get_position());
176    }
177    return result;
178}
179
180#undef BOOST_WAVE_INTLITGRAMMAR_GEN_INLINE
181
182///////////////////////////////////////////////////////////////////////////////
183}   // namespace grammars
184}   // namespace wave
185}   // namespace boost
186
187// the suffix header occurs after all of the code
188#ifdef BOOST_HAS_ABI_HEADERS
189#include BOOST_ABI_SUFFIX
190#endif
191
192#endif // !defined(CPP_INTLIT_GRAMMAR_HPP_2E1E70B1_F15C_4132_8554_10A231B0D91C_INCLUDED)
Note: See TracBrowser for help on using the repository browser.