1 /*=============================================================================
2     Copyright (c) 2001-2003 Daniel Nuffer
3     Copyright (c) 2001-2007 Hartmut Kaiser
4     http://spirit.sourceforge.net/
5 
6   Distributed under the Boost Software License, Version 1.0. (See accompanying
7   file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 #ifndef BOOST_SPIRIT_TREE_PARSE_TREE_HPP
10 #define BOOST_SPIRIT_TREE_PARSE_TREE_HPP
11 
12 #include <boost/spirit/home/classic/namespace.hpp>
13 #include <boost/spirit/home/classic/tree/common.hpp>
14 #include <boost/spirit/home/classic/core/scanner/scanner.hpp>
15 
16 #include <boost/spirit/home/classic/tree/parse_tree_fwd.hpp>
17 
18 ///////////////////////////////////////////////////////////////////////////////
19 namespace boost { namespace spirit {
20 
21 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
22 
23 //////////////////////////////////
24 // pt_match_policy is simply an id so the correct specialization of tree_policy can be found.
25 template <
26     typename IteratorT,
27     typename NodeFactoryT,
28     typename T
29 >
30 struct pt_match_policy :
31     public common_tree_match_policy<
32         pt_match_policy<IteratorT, NodeFactoryT, T>,
33         IteratorT,
34         NodeFactoryT,
35         pt_tree_policy<
36             pt_match_policy<IteratorT, NodeFactoryT, T>,
37             NodeFactoryT,
38             T
39         >,
40         T
41     >
42 {
43     typedef
44         common_tree_match_policy<
45             pt_match_policy<IteratorT, NodeFactoryT, T>,
46             IteratorT,
47             NodeFactoryT,
48             pt_tree_policy<
49                 pt_match_policy<IteratorT, NodeFactoryT, T>,
50                 NodeFactoryT,
51                 T
52             >,
53             T
54         >
55     common_tree_match_policy_;
56 
pt_match_policyboost::spirit::pt_match_policy57     pt_match_policy()
58     {
59     }
60 
61     template <typename PolicyT>
pt_match_policyboost::spirit::pt_match_policy62     pt_match_policy(PolicyT const & policies)
63         : common_tree_match_policy_(policies)
64     {
65     }
66 };
67 
68 //////////////////////////////////
69 template <typename MatchPolicyT, typename NodeFactoryT, typename T>
70 struct pt_tree_policy :
71     public common_tree_tree_policy<MatchPolicyT, NodeFactoryT>
72 {
73     typedef typename MatchPolicyT::match_t match_t;
74     typedef typename MatchPolicyT::iterator_t iterator_t;
75 
76     template<typename MatchAT, typename MatchBT>
concatboost::spirit::pt_tree_policy77     static void concat(MatchAT& a, MatchBT const& b)
78     {
79         typedef typename match_t::attr_t attr_t;
80         BOOST_SPIRIT_ASSERT(a && b);
81 
82         std::copy(b.trees.begin(), b.trees.end(),
83             std::back_insert_iterator<typename match_t::container_t>(a.trees));
84     }
85 
86     template <typename MatchT, typename Iterator1T, typename Iterator2T>
group_matchboost::spirit::pt_tree_policy87     static void group_match(MatchT& m, parser_id const& id,
88             Iterator1T const& first, Iterator2T const& last)
89     {
90         if (!m)
91             return;
92 
93         typedef typename NodeFactoryT::template factory<iterator_t> factory_t;
94         typedef typename tree_match<iterator_t, NodeFactoryT, T>::container_t
95             container_t;
96         typedef typename container_t::iterator cont_iterator_t;
97 
98         match_t newmatch(m.length(),
99                 factory_t::create_node(first, last, false));
100 
101         std::swap(newmatch.trees.begin()->children, m.trees);
102         // set this node and all it's unset children's rule_id
103         newmatch.trees.begin()->value.id(id);
104         for (cont_iterator_t i = newmatch.trees.begin()->children.begin();
105                 i != newmatch.trees.begin()->children.end();
106                 ++i)
107         {
108             if (i->value.id() == 0)
109                 i->value.id(id);
110         }
111         m = newmatch;
112     }
113 
114     template <typename FunctorT, typename MatchT>
apply_op_to_matchboost::spirit::pt_tree_policy115     static void apply_op_to_match(FunctorT const& op, MatchT& m)
116     {
117         op(m);
118     }
119 };
120 
121 namespace impl {
122 
123     template <typename IteratorT, typename NodeFactoryT, typename T>
124     struct tree_policy_selector<pt_match_policy<IteratorT, NodeFactoryT, T> >
125     {
126         typedef pt_tree_policy<
127             pt_match_policy<IteratorT, NodeFactoryT, T>,
128             NodeFactoryT,
129             T
130         > type;
131     };
132 
133 } // namespace impl
134 
135 
136 //////////////////////////////////
137 struct gen_pt_node_parser_gen;
138 
139 template <typename T>
140 struct gen_pt_node_parser
141 :   public unary<T, parser<gen_pt_node_parser<T> > >
142 {
143     typedef gen_pt_node_parser<T> self_t;
144     typedef gen_pt_node_parser_gen parser_generator_t;
145     typedef unary_parser_category parser_category_t;
146 
gen_pt_node_parserboost::spirit::gen_pt_node_parser147     gen_pt_node_parser(T const& a)
148     : unary<T, parser<gen_pt_node_parser<T> > >(a) {}
149 
150     template <typename ScannerT>
151     typename parser_result<self_t, ScannerT>::type
parseboost::spirit::gen_pt_node_parser152     parse(ScannerT const& scan) const
153     {
154         typedef typename ScannerT::iteration_policy_t iteration_policy_t;
155         typedef typename ScannerT::match_policy_t::iterator_t iterator_t;
156         typedef typename ScannerT::match_policy_t::factory_t factory_t;
157         typedef pt_match_policy<iterator_t, factory_t> match_policy_t;
158         typedef typename ScannerT::action_policy_t action_policy_t;
159         typedef scanner_policies<
160             iteration_policy_t,
161             match_policy_t,
162             action_policy_t
163         > policies_t;
164 
165         return this->subject().parse(scan.change_policies(policies_t(scan)));
166     }
167 };
168 
169 //////////////////////////////////
170 struct gen_pt_node_parser_gen
171 {
172     template <typename T>
173     struct result {
174 
175         typedef gen_pt_node_parser<T> type;
176     };
177 
178     template <typename T>
179     static gen_pt_node_parser<T>
generateboost::spirit::gen_pt_node_parser_gen180     generate(parser<T> const& s)
181     {
182         return gen_pt_node_parser<T>(s.derived());
183     }
184 
185     template <typename T>
186     gen_pt_node_parser<T>
operator []boost::spirit::gen_pt_node_parser_gen187     operator[](parser<T> const& s) const
188     {
189         return gen_pt_node_parser<T>(s.derived());
190     }
191 };
192 
193 //////////////////////////////////
194 const gen_pt_node_parser_gen gen_pt_node_d = gen_pt_node_parser_gen();
195 
196 
197 ///////////////////////////////////////////////////////////////////////////////
198 //
199 //  Parse functions for parse trees
200 //
201 ///////////////////////////////////////////////////////////////////////////////
202 template <
203     typename NodeFactoryT, typename IteratorT, typename ParserT,
204     typename SkipT
205 >
206 inline tree_parse_info<IteratorT, NodeFactoryT>
pt_parse(IteratorT const & first_,IteratorT const & last,parser<ParserT> const & p,SkipT const & skip,NodeFactoryT const &=NodeFactoryT ())207 pt_parse(
208     IteratorT const&        first_,
209     IteratorT const&        last,
210     parser<ParserT> const&  p,
211     SkipT const&            skip,
212     NodeFactoryT const&   /*dummy_*/ = NodeFactoryT())
213 {
214     typedef skip_parser_iteration_policy<SkipT> iter_policy_t;
215     typedef pt_match_policy<IteratorT, NodeFactoryT> pt_match_policy_t;
216     typedef
217         scanner_policies<iter_policy_t, pt_match_policy_t>
218         scanner_policies_t;
219     typedef scanner<IteratorT, scanner_policies_t> scanner_t;
220 
221     iter_policy_t iter_policy(skip);
222     scanner_policies_t policies(iter_policy);
223     IteratorT first = first_;
224     scanner_t scan(first, last, policies);
225     tree_match<IteratorT, NodeFactoryT> hit = p.derived().parse(scan);
226     return tree_parse_info<IteratorT, NodeFactoryT>(
227         first, hit, hit && (first == last), hit.length(), hit.trees);
228 }
229 
230 template <typename IteratorT, typename ParserT, typename SkipT>
231 inline tree_parse_info<IteratorT>
pt_parse(IteratorT const & first,IteratorT const & last,parser<ParserT> const & p,SkipT const & skip)232 pt_parse(
233     IteratorT const&        first,
234     IteratorT const&        last,
235     parser<ParserT> const&  p,
236     SkipT const&            skip)
237 {
238     typedef node_val_data_factory<nil_t> default_node_factory_t;
239     return pt_parse(first, last, p, skip, default_node_factory_t());
240 }
241 
242 //////////////////////////////////
243 template <typename IteratorT, typename ParserT>
244 inline tree_parse_info<IteratorT>
pt_parse(IteratorT const & first_,IteratorT const & last,parser<ParserT> const & parser)245 pt_parse(
246     IteratorT const&        first_,
247     IteratorT const&        last,
248     parser<ParserT> const&  parser)
249 {
250     typedef pt_match_policy<IteratorT> pt_match_policy_t;
251     IteratorT first = first_;
252     scanner<
253         IteratorT,
254         scanner_policies<iteration_policy, pt_match_policy_t>
255     > scan(first, last);
256     tree_match<IteratorT> hit = parser.derived().parse(scan);
257     return tree_parse_info<IteratorT>(
258         first, hit, hit && (first == last), hit.length(), hit.trees);
259 }
260 
261 //////////////////////////////////
262 template <typename CharT, typename ParserT, typename SkipT>
263 inline tree_parse_info<CharT const*>
pt_parse(CharT const * str,parser<ParserT> const & p,SkipT const & skip)264 pt_parse(
265     CharT const*            str,
266     parser<ParserT> const&  p,
267     SkipT const&            skip)
268 {
269     CharT const* last = str;
270     while (*last)
271         last++;
272     return pt_parse(str, last, p, skip);
273 }
274 
275 //////////////////////////////////
276 template <typename CharT, typename ParserT>
277 inline tree_parse_info<CharT const*>
pt_parse(CharT const * str,parser<ParserT> const & parser)278 pt_parse(
279     CharT const*            str,
280     parser<ParserT> const&  parser)
281 {
282     CharT const* last = str;
283     while (*last)
284     {
285         last++;
286     }
287     return pt_parse(str, last, parser);
288 }
289 
290 ///////////////////////////////////////////////////////////////////////////////
291 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
292 
293 }} // namespace BOOST_SPIRIT_CLASSIC_NS
294 
295 #endif
296 
297