File indexing completed on 2025-01-31 10:02:06
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef BOOST_SPIRIT_TREE_PARSE_TREE_HPP
0010 #define BOOST_SPIRIT_TREE_PARSE_TREE_HPP
0011
0012 #include <boost/spirit/home/classic/namespace.hpp>
0013 #include <boost/spirit/home/classic/tree/common.hpp>
0014 #include <boost/spirit/home/classic/core/scanner/scanner.hpp>
0015
0016 #include <boost/spirit/home/classic/tree/parse_tree_fwd.hpp>
0017
0018
0019 namespace boost { namespace spirit {
0020
0021 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
0022
0023
0024
0025 template <
0026 typename IteratorT,
0027 typename NodeFactoryT,
0028 typename T
0029 >
0030 struct pt_match_policy :
0031 public common_tree_match_policy<
0032 pt_match_policy<IteratorT, NodeFactoryT, T>,
0033 IteratorT,
0034 NodeFactoryT,
0035 pt_tree_policy<
0036 pt_match_policy<IteratorT, NodeFactoryT, T>,
0037 NodeFactoryT,
0038 T
0039 >,
0040 T
0041 >
0042 {
0043 typedef
0044 common_tree_match_policy<
0045 pt_match_policy<IteratorT, NodeFactoryT, T>,
0046 IteratorT,
0047 NodeFactoryT,
0048 pt_tree_policy<
0049 pt_match_policy<IteratorT, NodeFactoryT, T>,
0050 NodeFactoryT,
0051 T
0052 >,
0053 T
0054 >
0055 common_tree_match_policy_;
0056
0057 pt_match_policy()
0058 {
0059 }
0060
0061 template <typename PolicyT>
0062 pt_match_policy(PolicyT const & policies)
0063 : common_tree_match_policy_(policies)
0064 {
0065 }
0066 };
0067
0068
0069 template <typename MatchPolicyT, typename NodeFactoryT, typename T>
0070 struct pt_tree_policy :
0071 public common_tree_tree_policy<MatchPolicyT, NodeFactoryT>
0072 {
0073 typedef typename MatchPolicyT::match_t match_t;
0074 typedef typename MatchPolicyT::iterator_t iterator_t;
0075
0076 template<typename MatchAT, typename MatchBT>
0077 static void concat(MatchAT& a, MatchBT const& b)
0078 {
0079 BOOST_SPIRIT_ASSERT(a && b);
0080
0081 std::copy(b.trees.begin(), b.trees.end(),
0082 std::back_insert_iterator<typename match_t::container_t>(a.trees));
0083 }
0084
0085 template <typename MatchT, typename Iterator1T, typename Iterator2T>
0086 static void group_match(MatchT& m, parser_id const& id,
0087 Iterator1T const& first, Iterator2T const& last)
0088 {
0089 if (!m)
0090 return;
0091
0092 typedef typename NodeFactoryT::template factory<iterator_t> factory_t;
0093 typedef typename tree_match<iterator_t, NodeFactoryT, T>::container_t
0094 container_t;
0095 typedef typename container_t::iterator cont_iterator_t;
0096
0097 match_t newmatch(m.length(),
0098 factory_t::create_node(first, last, false));
0099
0100 std::swap(newmatch.trees.begin()->children, m.trees);
0101
0102 newmatch.trees.begin()->value.id(id);
0103 for (cont_iterator_t i = newmatch.trees.begin()->children.begin();
0104 i != newmatch.trees.begin()->children.end();
0105 ++i)
0106 {
0107 if (i->value.id() == 0)
0108 i->value.id(id);
0109 }
0110 m = newmatch;
0111 }
0112
0113 template <typename FunctorT, typename MatchT>
0114 static void apply_op_to_match(FunctorT const& op, MatchT& m)
0115 {
0116 op(m);
0117 }
0118 };
0119
0120 namespace impl {
0121
0122 template <typename IteratorT, typename NodeFactoryT, typename T>
0123 struct tree_policy_selector<pt_match_policy<IteratorT, NodeFactoryT, T> >
0124 {
0125 typedef pt_tree_policy<
0126 pt_match_policy<IteratorT, NodeFactoryT, T>,
0127 NodeFactoryT,
0128 T
0129 > type;
0130 };
0131
0132 }
0133
0134
0135
0136 struct gen_pt_node_parser_gen;
0137
0138 template <typename T>
0139 struct gen_pt_node_parser
0140 : public unary<T, parser<gen_pt_node_parser<T> > >
0141 {
0142 typedef gen_pt_node_parser<T> self_t;
0143 typedef gen_pt_node_parser_gen parser_generator_t;
0144 typedef unary_parser_category parser_category_t;
0145
0146 gen_pt_node_parser(T const& a)
0147 : unary<T, parser<gen_pt_node_parser<T> > >(a) {}
0148
0149 template <typename ScannerT>
0150 typename parser_result<self_t, ScannerT>::type
0151 parse(ScannerT const& scan) const
0152 {
0153 typedef typename ScannerT::iteration_policy_t iteration_policy_t;
0154 typedef typename ScannerT::match_policy_t::iterator_t iterator_t;
0155 typedef typename ScannerT::match_policy_t::factory_t factory_t;
0156 typedef pt_match_policy<iterator_t, factory_t> match_policy_t;
0157 typedef typename ScannerT::action_policy_t action_policy_t;
0158 typedef scanner_policies<
0159 iteration_policy_t,
0160 match_policy_t,
0161 action_policy_t
0162 > policies_t;
0163
0164 return this->subject().parse(scan.change_policies(policies_t(scan)));
0165 }
0166 };
0167
0168
0169 struct gen_pt_node_parser_gen
0170 {
0171 template <typename T>
0172 struct result {
0173
0174 typedef gen_pt_node_parser<T> type;
0175 };
0176
0177 template <typename T>
0178 static gen_pt_node_parser<T>
0179 generate(parser<T> const& s)
0180 {
0181 return gen_pt_node_parser<T>(s.derived());
0182 }
0183
0184 template <typename T>
0185 gen_pt_node_parser<T>
0186 operator[](parser<T> const& s) const
0187 {
0188 return gen_pt_node_parser<T>(s.derived());
0189 }
0190 };
0191
0192
0193 const gen_pt_node_parser_gen gen_pt_node_d = gen_pt_node_parser_gen();
0194
0195
0196
0197
0198
0199
0200
0201 template <
0202 typename NodeFactoryT, typename IteratorT, typename ParserT,
0203 typename SkipT
0204 >
0205 inline tree_parse_info<IteratorT, NodeFactoryT>
0206 pt_parse(
0207 IteratorT const& first_,
0208 IteratorT const& last,
0209 parser<ParserT> const& p,
0210 SkipT const& skip,
0211 NodeFactoryT const& = NodeFactoryT())
0212 {
0213 typedef skip_parser_iteration_policy<SkipT> it_policy_t;
0214 typedef pt_match_policy<IteratorT, NodeFactoryT> pt_match_policy_t;
0215 typedef
0216 scanner_policies<it_policy_t, pt_match_policy_t>
0217 scan_policies_t;
0218 typedef scanner<IteratorT, scan_policies_t> scanner_t;
0219
0220 it_policy_t iter_policy(skip);
0221 scan_policies_t policies(iter_policy);
0222 IteratorT first = first_;
0223 scanner_t scan(first, last, policies);
0224 tree_match<IteratorT, NodeFactoryT> hit = p.derived().parse(scan);
0225 return tree_parse_info<IteratorT, NodeFactoryT>(
0226 first, hit, hit && (first == last), hit.length(), hit.trees);
0227 }
0228
0229 template <typename IteratorT, typename ParserT, typename SkipT>
0230 inline tree_parse_info<IteratorT>
0231 pt_parse(
0232 IteratorT const& first,
0233 IteratorT const& last,
0234 parser<ParserT> const& p,
0235 SkipT const& skip)
0236 {
0237 typedef node_val_data_factory<nil_t> default_node_factory_t;
0238 return pt_parse(first, last, p, skip, default_node_factory_t());
0239 }
0240
0241
0242 template <typename IteratorT, typename ParserT>
0243 inline tree_parse_info<IteratorT>
0244 pt_parse(
0245 IteratorT const& first_,
0246 IteratorT const& last,
0247 parser<ParserT> const& parser)
0248 {
0249 typedef pt_match_policy<IteratorT> pt_match_policy_t;
0250 IteratorT first = first_;
0251 scanner<
0252 IteratorT,
0253 scanner_policies<iteration_policy, pt_match_policy_t>
0254 > scan(first, last);
0255 tree_match<IteratorT> hit = parser.derived().parse(scan);
0256 return tree_parse_info<IteratorT>(
0257 first, hit, hit && (first == last), hit.length(), hit.trees);
0258 }
0259
0260
0261 template <typename CharT, typename ParserT, typename SkipT>
0262 inline tree_parse_info<CharT const*>
0263 pt_parse(
0264 CharT const* str,
0265 parser<ParserT> const& p,
0266 SkipT const& skip)
0267 {
0268 CharT const* last = str;
0269 while (*last)
0270 last++;
0271 return pt_parse(str, last, p, skip);
0272 }
0273
0274
0275 template <typename CharT, typename ParserT>
0276 inline tree_parse_info<CharT const*>
0277 pt_parse(
0278 CharT const* str,
0279 parser<ParserT> const& parser)
0280 {
0281 CharT const* last = str;
0282 while (*last)
0283 {
0284 last++;
0285 }
0286 return pt_parse(str, last, parser);
0287 }
0288
0289
0290 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
0291
0292 }}
0293
0294 #endif
0295