annotate DEPENDENCIES/generic/include/boost/spirit/home/classic/tree/parse_tree.hpp @ 133:4acb5d8d80b6 tip

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