1 /*=============================================================================
2 Copyright (c) 2001-2003 Joel de Guzman
3 Copyright (c) 2002-2003 Martin Wille
4 Copyright (c) 2003 Hartmut Kaiser
5 http://spirit.sourceforge.net/
7 Use, modification and distribution is subject to the Boost Software
8 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
9 http://www.boost.org/LICENSE_1_0.txt)
10 =============================================================================*/
11 #if !defined BOOST_SPIRIT_GRAMMAR_IPP
12 #define BOOST_SPIRIT_GRAMMAR_IPP
14 #if !defined(BOOST_SPIRIT_SINGLE_GRAMMAR_INSTANCE)
15 #include <boost/spirit/home/classic/core/non_terminal/impl/object_with_id.ipp>
18 #include <boost/move/unique_ptr.hpp>
19 #include <boost/weak_ptr.hpp>
22 #ifdef BOOST_SPIRIT_THREADSAFE
23 #include <boost/spirit/home/classic/core/non_terminal/impl/static.hpp>
24 #include <boost/thread/tss.hpp>
25 #include <boost/thread/mutex.hpp>
26 #include <boost/thread/lock_types.hpp>
29 ///////////////////////////////////////////////////////////////////////////////
30 namespace boost { namespace spirit {
32 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
34 template <typename DerivedT, typename ContextT>
38 //////////////////////////////////
39 template <typename GrammarT, typename ScannerT>
40 struct grammar_definition
42 typedef typename GrammarT::template definition<ScannerT> type;
49 #if !defined(BOOST_SPIRIT_SINGLE_GRAMMAR_INSTANCE)
50 struct grammar_tag {};
52 //////////////////////////////////
53 template <typename GrammarT>
54 struct grammar_helper_base
56 virtual int undefine(GrammarT *) = 0;
57 virtual ~grammar_helper_base() {}
60 //////////////////////////////////
61 template <typename GrammarT>
62 struct grammar_helper_list
64 typedef GrammarT grammar_t;
65 typedef grammar_helper_base<GrammarT> helper_t;
66 typedef std::vector<helper_t*> vector_t;
68 grammar_helper_list() {}
69 grammar_helper_list(grammar_helper_list const& /*x*/)
70 { // Does _not_ copy the helpers member !
73 grammar_helper_list& operator=(grammar_helper_list const& /*x*/)
74 { // Does _not_ copy the helpers member !
78 void push_back(helper_t *helper)
79 { helpers.push_back(helper); }
82 { helpers.pop_back(); }
84 typename vector_t::size_type
86 { return helpers.size(); }
88 typename vector_t::reverse_iterator
90 { return helpers.rbegin(); }
92 typename vector_t::reverse_iterator
94 { return helpers.rend(); }
96 #ifdef BOOST_SPIRIT_THREADSAFE
97 boost::mutex & mutex()
104 #ifdef BOOST_SPIRIT_THREADSAFE
109 //////////////////////////////////
110 struct grammartract_helper_list;
112 #if !defined(BOOST_SPIRIT_SINGLE_GRAMMAR_INSTANCE)
114 struct grammartract_helper_list
116 template<typename GrammarT>
117 static grammar_helper_list<GrammarT>&
118 do_(GrammarT const* g)
126 //////////////////////////////////
127 template <typename GrammarT, typename DerivedT, typename ScannerT>
128 struct grammar_helper : private grammar_helper_base<GrammarT>
130 typedef GrammarT grammar_t;
131 typedef ScannerT scanner_t;
132 typedef DerivedT derived_t;
133 typedef typename grammar_definition<DerivedT, ScannerT>::type definition_t;
135 typedef grammar_helper<grammar_t, derived_t, scanner_t> helper_t;
136 typedef boost::shared_ptr<helper_t> helper_ptr_t;
137 typedef boost::weak_ptr<helper_t> helper_weak_ptr_t;
140 this_() { return this; }
142 grammar_helper(helper_weak_ptr_t& p)
148 define(grammar_t const* target_grammar)
150 grammar_helper_list<GrammarT> &helpers =
151 grammartract_helper_list::do_(target_grammar);
152 typename grammar_t::object_id id = target_grammar->get_object_id();
154 if (definitions.size()<=id)
155 definitions.resize(id*3/2+1);
156 if (definitions[id]!=0)
157 return *definitions[id];
159 boost::movelib::unique_ptr<definition_t>
160 result(new definition_t(target_grammar->derived()));
162 #ifdef BOOST_SPIRIT_THREADSAFE
163 boost::unique_lock<boost::mutex> lock(helpers.mutex());
165 helpers.push_back(this);
168 definitions[id] = result.get();
169 return *(result.release());
173 undefine(grammar_t* target_grammar) BOOST_OVERRIDE
175 typename grammar_t::object_id id = target_grammar->get_object_id();
177 if (definitions.size()<=id)
179 delete definitions[id];
181 if (--definitions_cnt==0)
188 std::vector<definition_t*> definitions;
189 unsigned long definitions_cnt;
193 #endif /* defined(BOOST_SPIRIT_SINGLE_GRAMMAR_INSTANCE) */
195 #ifdef BOOST_SPIRIT_THREADSAFE
196 class get_definition_static_data_tag
198 template<typename DerivedT, typename ContextT, typename ScannerT>
199 friend typename DerivedT::template definition<ScannerT> &
200 get_definition(grammar<DerivedT, ContextT> const* self);
202 get_definition_static_data_tag() {}
206 template<typename DerivedT, typename ContextT, typename ScannerT>
207 inline typename DerivedT::template definition<ScannerT> &
208 get_definition(grammar<DerivedT, ContextT> const* self)
210 #if defined(BOOST_SPIRIT_SINGLE_GRAMMAR_INSTANCE)
212 typedef typename DerivedT::template definition<ScannerT> definition_t;
213 static definition_t def(self->derived());
216 typedef grammar<DerivedT, ContextT> self_t;
217 typedef impl::grammar_helper<self_t, DerivedT, ScannerT> helper_t;
218 typedef typename helper_t::helper_weak_ptr_t ptr_t;
220 # ifdef BOOST_SPIRIT_THREADSAFE
221 boost::thread_specific_ptr<ptr_t> & tld_helper
222 = static_<boost::thread_specific_ptr<ptr_t>,
223 get_definition_static_data_tag>(get_definition_static_data_tag());
225 if (!tld_helper.get())
226 tld_helper.reset(new ptr_t);
227 ptr_t &helper = *tld_helper;
231 if (helper.expired())
232 new helper_t(helper);
233 return helper.lock()->define(self);
240 template <typename RT, typename DefinitionT, typename ScannerT>
242 do_ (RT &result, DefinitionT &def, ScannerT const &scan)
244 result = def.template get_start_parser<N>()->parse(scan);
249 struct call_helper<0> {
251 template <typename RT, typename DefinitionT, typename ScannerT>
253 do_ (RT &result, DefinitionT &def, ScannerT const &scan)
255 result = def.start().parse(scan);
259 //////////////////////////////////
260 template<int N, typename DerivedT, typename ContextT, typename ScannerT>
261 inline typename parser_result<grammar<DerivedT, ContextT>, ScannerT>::type
262 grammar_parser_parse(
263 grammar<DerivedT, ContextT> const* self,
264 ScannerT const &scan)
267 typename parser_result<grammar<DerivedT, ContextT>, ScannerT>::type
269 typedef typename DerivedT::template definition<ScannerT> definition_t;
272 definition_t &def = get_definition<DerivedT, ContextT, ScannerT>(self);
274 call_helper<N>::do_(result, def, scan);
278 //////////////////////////////////
279 template<typename GrammarT>
281 grammar_destruct(GrammarT* self)
283 #if !defined(BOOST_SPIRIT_SINGLE_GRAMMAR_INSTANCE)
284 typedef grammar_helper_list<GrammarT> helper_list_t;
286 helper_list_t& helpers =
287 grammartract_helper_list::do_(self);
289 typedef typename helper_list_t::vector_t::reverse_iterator iterator_t;
291 for (iterator_t i = helpers.rbegin(); i != helpers.rend(); ++i)
292 (*i)->undefine(self);
298 ///////////////////////////////////////////////////////////////////////////
300 // entry_grammar class
302 ///////////////////////////////////////////////////////////////////////////
303 template <typename DerivedT, int N, typename ContextT>
305 : public parser<entry_grammar<DerivedT, N, ContextT> >
309 typedef entry_grammar<DerivedT, N, ContextT> self_t;
310 typedef self_t embed_t;
311 typedef typename ContextT::context_linker_t context_t;
312 typedef typename context_t::attr_t attr_t;
314 template <typename ScannerT>
317 typedef typename match_result<ScannerT, attr_t>::type type;
320 entry_grammar(DerivedT const &p) : target_grammar(p) {}
322 template <typename ScannerT>
323 typename parser_result<self_t, ScannerT>::type
324 parse_main(ScannerT const& scan) const
325 { return impl::grammar_parser_parse<N>(&target_grammar, scan); }
327 template <typename ScannerT>
328 typename parser_result<self_t, ScannerT>::type
329 parse(ScannerT const& scan) const
331 typedef typename parser_result<self_t, ScannerT>::type result_t;
332 typedef parser_scanner_linker<ScannerT> scanner_t;
333 BOOST_SPIRIT_CONTEXT_PARSE(scan, target_grammar, scanner_t,
338 DerivedT const &target_grammar;
343 ///////////////////////////////////////
344 #if !defined(BOOST_SPIRIT_SINGLE_GRAMMAR_INSTANCE)
345 #define BOOST_SPIRIT_GRAMMAR_ID , public impl::object_with_id<impl::grammar_tag>
347 #define BOOST_SPIRIT_GRAMMAR_ID
350 ///////////////////////////////////////
351 #if !defined(BOOST_SPIRIT_SINGLE_GRAMMAR_INSTANCE)
352 #define BOOST_SPIRIT_GRAMMAR_STATE \
354 friend struct impl::grammartract_helper_list; \
355 mutable impl::grammar_helper_list<self_t> helpers;
357 #define BOOST_SPIRIT_GRAMMAR_STATE
360 ///////////////////////////////////////////////////////////////////////////////
361 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
363 }} // namespace boost::spirit