Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/boost/spirit/tree/parse_tree.hpp @ 47

Last change on this file since 47 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

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