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 BOOST_SPIRIT_ASSERT(a && b);
80
81 std::copy(b.trees.begin(), b.trees.end(),
82 std::back_insert_iterator<typename match_t::container_t>(a.trees));
83 }
84
85 template <typename MatchT, typename Iterator1T, typename Iterator2T>
group_matchboost::spirit::pt_tree_policy86 static void group_match(MatchT& m, parser_id const& id,
87 Iterator1T const& first, Iterator2T const& last)
88 {
89 if (!m)
90 return;
91
92 typedef typename NodeFactoryT::template factory<iterator_t> factory_t;
93 typedef typename tree_match<iterator_t, NodeFactoryT, T>::container_t
94 container_t;
95 typedef typename container_t::iterator cont_iterator_t;
96
97 match_t newmatch(m.length(),
98 factory_t::create_node(first, last, false));
99
100 std::swap(newmatch.trees.begin()->children, m.trees);
101 // set this node and all it's unset children's rule_id
102 newmatch.trees.begin()->value.id(id);
103 for (cont_iterator_t i = newmatch.trees.begin()->children.begin();
104 i != newmatch.trees.begin()->children.end();
105 ++i)
106 {
107 if (i->value.id() == 0)
108 i->value.id(id);
109 }
110 m = newmatch;
111 }
112
113 template <typename FunctorT, typename MatchT>
apply_op_to_matchboost::spirit::pt_tree_policy114 static void apply_op_to_match(FunctorT const& op, MatchT& m)
115 {
116 op(m);
117 }
118 };
119
120 namespace impl {
121
122 template <typename IteratorT, typename NodeFactoryT, typename T>
123 struct tree_policy_selector<pt_match_policy<IteratorT, NodeFactoryT, T> >
124 {
125 typedef pt_tree_policy<
126 pt_match_policy<IteratorT, NodeFactoryT, T>,
127 NodeFactoryT,
128 T
129 > type;
130 };
131
132 } // namespace impl
133
134
135 //////////////////////////////////
136 struct gen_pt_node_parser_gen;
137
138 template <typename T>
139 struct gen_pt_node_parser
140 : public unary<T, parser<gen_pt_node_parser<T> > >
141 {
142 typedef gen_pt_node_parser<T> self_t;
143 typedef gen_pt_node_parser_gen parser_generator_t;
144 typedef unary_parser_category parser_category_t;
145
gen_pt_node_parserboost::spirit::gen_pt_node_parser146 gen_pt_node_parser(T const& a)
147 : unary<T, parser<gen_pt_node_parser<T> > >(a) {}
148
149 template <typename ScannerT>
150 typename parser_result<self_t, ScannerT>::type
parseboost::spirit::gen_pt_node_parser151 parse(ScannerT const& scan) const
152 {
153 typedef typename ScannerT::iteration_policy_t iteration_policy_t;
154 typedef typename ScannerT::match_policy_t::iterator_t iterator_t;
155 typedef typename ScannerT::match_policy_t::factory_t factory_t;
156 typedef pt_match_policy<iterator_t, factory_t> match_policy_t;
157 typedef typename ScannerT::action_policy_t action_policy_t;
158 typedef scanner_policies<
159 iteration_policy_t,
160 match_policy_t,
161 action_policy_t
162 > policies_t;
163
164 return this->subject().parse(scan.change_policies(policies_t(scan)));
165 }
166 };
167
168 //////////////////////////////////
169 struct gen_pt_node_parser_gen
170 {
171 template <typename T>
172 struct result {
173
174 typedef gen_pt_node_parser<T> type;
175 };
176
177 template <typename T>
178 static gen_pt_node_parser<T>
generateboost::spirit::gen_pt_node_parser_gen179 generate(parser<T> const& s)
180 {
181 return gen_pt_node_parser<T>(s.derived());
182 }
183
184 template <typename T>
185 gen_pt_node_parser<T>
operator []boost::spirit::gen_pt_node_parser_gen186 operator[](parser<T> const& s) const
187 {
188 return gen_pt_node_parser<T>(s.derived());
189 }
190 };
191
192 //////////////////////////////////
193 const gen_pt_node_parser_gen gen_pt_node_d = gen_pt_node_parser_gen();
194
195
196 ///////////////////////////////////////////////////////////////////////////////
197 //
198 // Parse functions for parse trees
199 //
200 ///////////////////////////////////////////////////////////////////////////////
201 template <
202 typename NodeFactoryT, typename IteratorT, typename ParserT,
203 typename SkipT
204 >
205 inline tree_parse_info<IteratorT, NodeFactoryT>
pt_parse(IteratorT const & first_,IteratorT const & last,parser<ParserT> const & p,SkipT const & skip,NodeFactoryT const &=NodeFactoryT ())206 pt_parse(
207 IteratorT const& first_,
208 IteratorT const& last,
209 parser<ParserT> const& p,
210 SkipT const& skip,
211 NodeFactoryT const& /*dummy_*/ = NodeFactoryT())
212 {
213 typedef skip_parser_iteration_policy<SkipT> it_policy_t;
214 typedef pt_match_policy<IteratorT, NodeFactoryT> pt_match_policy_t;
215 typedef
216 scanner_policies<it_policy_t, pt_match_policy_t>
217 scan_policies_t;
218 typedef scanner<IteratorT, scan_policies_t> scanner_t;
219
220 it_policy_t iter_policy(skip);
221 scan_policies_t policies(iter_policy);
222 IteratorT first = first_;
223 scanner_t scan(first, last, policies);
224 tree_match<IteratorT, NodeFactoryT> hit = p.derived().parse(scan);
225 return tree_parse_info<IteratorT, NodeFactoryT>(
226 first, hit, hit && (first == last), hit.length(), hit.trees);
227 }
228
229 template <typename IteratorT, typename ParserT, typename SkipT>
230 inline tree_parse_info<IteratorT>
pt_parse(IteratorT const & first,IteratorT const & last,parser<ParserT> const & p,SkipT const & skip)231 pt_parse(
232 IteratorT const& first,
233 IteratorT const& last,
234 parser<ParserT> const& p,
235 SkipT const& skip)
236 {
237 typedef node_val_data_factory<nil_t> default_node_factory_t;
238 return pt_parse(first, last, p, skip, default_node_factory_t());
239 }
240
241 //////////////////////////////////
242 template <typename IteratorT, typename ParserT>
243 inline tree_parse_info<IteratorT>
pt_parse(IteratorT const & first_,IteratorT const & last,parser<ParserT> const & parser)244 pt_parse(
245 IteratorT const& first_,
246 IteratorT const& last,
247 parser<ParserT> const& parser)
248 {
249 typedef pt_match_policy<IteratorT> pt_match_policy_t;
250 IteratorT first = first_;
251 scanner<
252 IteratorT,
253 scanner_policies<iteration_policy, pt_match_policy_t>
254 > scan(first, last);
255 tree_match<IteratorT> hit = parser.derived().parse(scan);
256 return tree_parse_info<IteratorT>(
257 first, hit, hit && (first == last), hit.length(), hit.trees);
258 }
259
260 //////////////////////////////////
261 template <typename CharT, typename ParserT, typename SkipT>
262 inline tree_parse_info<CharT const*>
pt_parse(CharT const * str,parser<ParserT> const & p,SkipT const & skip)263 pt_parse(
264 CharT const* str,
265 parser<ParserT> const& p,
266 SkipT const& skip)
267 {
268 CharT const* last = str;
269 while (*last)
270 last++;
271 return pt_parse(str, last, p, skip);
272 }
273
274 //////////////////////////////////
275 template <typename CharT, typename ParserT>
276 inline tree_parse_info<CharT const*>
pt_parse(CharT const * str,parser<ParserT> const & parser)277 pt_parse(
278 CharT const* str,
279 parser<ParserT> const& parser)
280 {
281 CharT const* last = str;
282 while (*last)
283 {
284 last++;
285 }
286 return pt_parse(str, last, parser);
287 }
288
289 ///////////////////////////////////////////////////////////////////////////////
290 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
291
292 }} // namespace BOOST_SPIRIT_CLASSIC_NS
293
294 #endif
295
296