os/ossrv/ossrv_pub/boost_apis/boost/spirit/tree/ast.hpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*=============================================================================
sl@0
     2
    Copyright (c) 2001-2003 Daniel Nuffer
sl@0
     3
    http://spirit.sourceforge.net/
sl@0
     4
sl@0
     5
    Use, modification and distribution is subject to the Boost Software
sl@0
     6
    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
sl@0
     7
    http://www.boost.org/LICENSE_1_0.txt)
sl@0
     8
=============================================================================*/
sl@0
     9
#ifndef BOOST_SPIRIT_TREE_AST_HPP
sl@0
    10
#define BOOST_SPIRIT_TREE_AST_HPP
sl@0
    11
sl@0
    12
#include <boost/spirit/tree/common.hpp>
sl@0
    13
#include <boost/spirit/core/scanner/scanner.hpp>
sl@0
    14
sl@0
    15
#include <boost/spirit/tree/ast_fwd.hpp>
sl@0
    16
sl@0
    17
///////////////////////////////////////////////////////////////////////////////
sl@0
    18
namespace boost { namespace spirit {
sl@0
    19
sl@0
    20
sl@0
    21
//////////////////////////////////
sl@0
    22
//  ast_match_policy is simply an id so the correct specialization of
sl@0
    23
//  tree_policy can be found.
sl@0
    24
template <
sl@0
    25
    typename IteratorT,
sl@0
    26
    typename NodeFactoryT
sl@0
    27
>
sl@0
    28
struct ast_match_policy :
sl@0
    29
    public common_tree_match_policy<
sl@0
    30
        ast_match_policy<IteratorT, NodeFactoryT>,
sl@0
    31
        IteratorT,
sl@0
    32
        NodeFactoryT,
sl@0
    33
        ast_tree_policy<
sl@0
    34
            ast_match_policy<IteratorT, NodeFactoryT>,
sl@0
    35
            NodeFactoryT
sl@0
    36
        >
sl@0
    37
    >
sl@0
    38
{
sl@0
    39
    typedef
sl@0
    40
        common_tree_match_policy<
sl@0
    41
            ast_match_policy<IteratorT, NodeFactoryT>,
sl@0
    42
            IteratorT,
sl@0
    43
            NodeFactoryT,
sl@0
    44
            ast_tree_policy<
sl@0
    45
                ast_match_policy<IteratorT, NodeFactoryT>,
sl@0
    46
                NodeFactoryT
sl@0
    47
            >
sl@0
    48
        >
sl@0
    49
    common_tree_match_policy_;
sl@0
    50
sl@0
    51
    ast_match_policy()
sl@0
    52
    {
sl@0
    53
    }
sl@0
    54
sl@0
    55
    template <typename PolicyT>
sl@0
    56
    ast_match_policy(PolicyT const & policies)
sl@0
    57
        : common_tree_match_policy_(policies)
sl@0
    58
    {
sl@0
    59
    }
sl@0
    60
};
sl@0
    61
sl@0
    62
//////////////////////////////////
sl@0
    63
template <typename MatchPolicyT, typename NodeFactoryT>
sl@0
    64
struct ast_tree_policy :
sl@0
    65
    public common_tree_tree_policy<MatchPolicyT, NodeFactoryT>
sl@0
    66
{
sl@0
    67
    typedef
sl@0
    68
        typename common_tree_tree_policy<MatchPolicyT, NodeFactoryT>::match_t
sl@0
    69
        match_t;
sl@0
    70
    typedef typename MatchPolicyT::iterator_t iterator_t;
sl@0
    71
sl@0
    72
    static void concat(match_t& a, match_t const& b)
sl@0
    73
    {
sl@0
    74
        BOOST_SPIRIT_ASSERT(a && b);
sl@0
    75
sl@0
    76
#if defined(BOOST_SPIRIT_DEBUG) && \
sl@0
    77
    (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
sl@0
    78
        BOOST_SPIRIT_DEBUG_OUT << "\n>>>AST concat. a = " << a <<
sl@0
    79
            "\n\tb = " << b << "<<<\n";
sl@0
    80
#endif
sl@0
    81
        typedef typename tree_match<iterator_t, NodeFactoryT>::container_t
sl@0
    82
            container_t;
sl@0
    83
sl@0
    84
        // test for size() is nessecary, because no_tree_gen_node leaves a.trees
sl@0
    85
        // and/or b.trees empty
sl@0
    86
        if (0 != b.trees.size() && b.trees.begin()->value.is_root())
sl@0
    87
        {
sl@0
    88
            BOOST_SPIRIT_ASSERT(b.trees.size() == 1);
sl@0
    89
sl@0
    90
            container_t tmp;
sl@0
    91
            std::swap(a.trees, tmp); // save a into tmp
sl@0
    92
            std::swap(b.trees, a.trees); // make b.trees[0] be new root (a.trees[0])
sl@0
    93
            container_t* pnon_root_trees = &a.trees;
sl@0
    94
            while (pnon_root_trees->size() > 0 &&
sl@0
    95
                    pnon_root_trees->begin()->value.is_root())
sl@0
    96
            {
sl@0
    97
                pnon_root_trees = & pnon_root_trees->begin()->children;
sl@0
    98
            }
sl@0
    99
            pnon_root_trees->insert(pnon_root_trees->begin(),
sl@0
   100
                    tmp.begin(), tmp.end());
sl@0
   101
        }
sl@0
   102
        else if (0 != a.trees.size() && a.trees.begin()->value.is_root())
sl@0
   103
        {
sl@0
   104
            BOOST_SPIRIT_ASSERT(a.trees.size() == 1);
sl@0
   105
sl@0
   106
#if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
sl@0
   107
            a.trees.begin()->children.reserve(a.trees.begin()->children.size() + b.trees.size());
sl@0
   108
#endif
sl@0
   109
            std::copy(b.trees.begin(),
sl@0
   110
                 b.trees.end(),
sl@0
   111
                 std::back_insert_iterator<container_t>(
sl@0
   112
                     a.trees.begin()->children));
sl@0
   113
        }
sl@0
   114
        else
sl@0
   115
        {
sl@0
   116
#if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
sl@0
   117
            a.trees.reserve(a.trees.size() + b.trees.size());
sl@0
   118
#endif
sl@0
   119
            std::copy(b.trees.begin(),
sl@0
   120
                 b.trees.end(),
sl@0
   121
                 std::back_insert_iterator<container_t>(a.trees));
sl@0
   122
        }
sl@0
   123
sl@0
   124
#if defined(BOOST_SPIRIT_DEBUG) && \
sl@0
   125
    (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
sl@0
   126
        BOOST_SPIRIT_DEBUG_OUT << ">>>after AST concat. a = " << a << "<<<\n\n";
sl@0
   127
#endif
sl@0
   128
sl@0
   129
        return;
sl@0
   130
    }
sl@0
   131
sl@0
   132
    template <typename MatchT, typename Iterator1T, typename Iterator2T>
sl@0
   133
    static void group_match(MatchT& m, parser_id const& id,
sl@0
   134
            Iterator1T const& first, Iterator2T const& last)
sl@0
   135
    {
sl@0
   136
        if (!m)
sl@0
   137
            return;
sl@0
   138
sl@0
   139
        typedef typename tree_match<iterator_t, NodeFactoryT>::container_t
sl@0
   140
            container_t;
sl@0
   141
        typedef typename container_t::iterator cont_iterator_t;
sl@0
   142
        typedef typename NodeFactoryT::template factory<iterator_t> factory_t;
sl@0
   143
sl@0
   144
        if (m.trees.size() == 1
sl@0
   145
#ifdef BOOST_SPIRIT_NO_TREE_NODE_COLLAPSING
sl@0
   146
            && !(id.to_long() && m.trees.begin()->value.id().to_long())
sl@0
   147
#endif
sl@0
   148
            )
sl@0
   149
        {
sl@0
   150
            // set rule_id's.  There may have been multiple nodes created.
sl@0
   151
            // Because of root_node[] they may be left-most children of the top
sl@0
   152
            // node.
sl@0
   153
            container_t* punset_id = &m.trees;
sl@0
   154
            while (punset_id->size() > 0 &&
sl@0
   155
                    punset_id->begin()->value.id() == 0)
sl@0
   156
            {
sl@0
   157
                punset_id->begin()->value.id(id);
sl@0
   158
                punset_id = &punset_id->begin()->children;
sl@0
   159
            }
sl@0
   160
sl@0
   161
            m.trees.begin()->value.is_root(false);
sl@0
   162
        }
sl@0
   163
        else
sl@0
   164
        {
sl@0
   165
            match_t newmatch(m.length(),
sl@0
   166
                m.trees.empty() ? 
sl@0
   167
                    factory_t::empty_node() : 
sl@0
   168
                    factory_t::create_node(first, last, false));
sl@0
   169
sl@0
   170
            std::swap(newmatch.trees.begin()->children, m.trees);
sl@0
   171
            // set this node and all it's unset children's rule_id
sl@0
   172
            newmatch.trees.begin()->value.id(id);
sl@0
   173
            for (cont_iterator_t i = newmatch.trees.begin();
sl@0
   174
                 i != newmatch.trees.end();
sl@0
   175
                 ++i)
sl@0
   176
            {
sl@0
   177
                if (i->value.id() == 0)
sl@0
   178
                    i->value.id(id);
sl@0
   179
            }
sl@0
   180
            m = newmatch;
sl@0
   181
        }
sl@0
   182
    }
sl@0
   183
sl@0
   184
    template <typename FunctorT>
sl@0
   185
    static void apply_op_to_match(FunctorT const& op, match_t& m)
sl@0
   186
    {
sl@0
   187
        op(m);
sl@0
   188
    }
sl@0
   189
};
sl@0
   190
sl@0
   191
namespace impl {
sl@0
   192
sl@0
   193
    template <typename IteratorT, typename NodeFactoryT>
sl@0
   194
    struct tree_policy_selector<ast_match_policy<IteratorT, NodeFactoryT> >
sl@0
   195
    {
sl@0
   196
        typedef ast_tree_policy<
sl@0
   197
            ast_match_policy<IteratorT, NodeFactoryT>, NodeFactoryT> type;
sl@0
   198
    };
sl@0
   199
sl@0
   200
} // namespace impl
sl@0
   201
sl@0
   202
sl@0
   203
//////////////////////////////////
sl@0
   204
struct gen_ast_node_parser_gen;
sl@0
   205
sl@0
   206
template <typename T>
sl@0
   207
struct gen_ast_node_parser
sl@0
   208
:   public unary<T, parser<gen_ast_node_parser<T> > >
sl@0
   209
{
sl@0
   210
    typedef gen_ast_node_parser<T> self_t;
sl@0
   211
    typedef gen_ast_node_parser_gen parser_generator_t;
sl@0
   212
    typedef unary_parser_category parser_category_t;
sl@0
   213
//    typedef gen_ast_node_parser<T> const &embed_t;
sl@0
   214
sl@0
   215
    gen_ast_node_parser(T const& a)
sl@0
   216
    : unary<T, parser<gen_ast_node_parser<T> > >(a) {}
sl@0
   217
sl@0
   218
    template <typename ScannerT>
sl@0
   219
    typename parser_result<self_t, ScannerT>::type
sl@0
   220
    parse(ScannerT const& scan) const
sl@0
   221
    {
sl@0
   222
        typedef typename ScannerT::iteration_policy_t iteration_policy_t;
sl@0
   223
        typedef typename ScannerT::match_policy_t::iterator_t iterator_t;
sl@0
   224
        typedef typename ScannerT::match_policy_t::factory_t factory_t;
sl@0
   225
        typedef ast_match_policy<iterator_t, factory_t> match_policy_t;
sl@0
   226
        typedef typename ScannerT::action_policy_t action_policy_t;
sl@0
   227
        typedef scanner_policies<
sl@0
   228
            iteration_policy_t,
sl@0
   229
            match_policy_t,
sl@0
   230
            action_policy_t
sl@0
   231
        > policies_t;
sl@0
   232
sl@0
   233
        return this->subject().parse(scan.change_policies(policies_t(scan)));
sl@0
   234
    }
sl@0
   235
};
sl@0
   236
sl@0
   237
//////////////////////////////////
sl@0
   238
struct gen_ast_node_parser_gen
sl@0
   239
{
sl@0
   240
    template <typename T>
sl@0
   241
    struct result {
sl@0
   242
sl@0
   243
        typedef gen_ast_node_parser<T> type;
sl@0
   244
    };
sl@0
   245
sl@0
   246
    template <typename T>
sl@0
   247
    static gen_ast_node_parser<T>
sl@0
   248
    generate(parser<T> const& s)
sl@0
   249
    {
sl@0
   250
        return gen_ast_node_parser<T>(s.derived());
sl@0
   251
    }
sl@0
   252
sl@0
   253
    template <typename T>
sl@0
   254
    gen_ast_node_parser<T>
sl@0
   255
    operator[](parser<T> const& s) const
sl@0
   256
    {
sl@0
   257
        return gen_ast_node_parser<T>(s.derived());
sl@0
   258
    }
sl@0
   259
};
sl@0
   260
sl@0
   261
//////////////////////////////////
sl@0
   262
const gen_ast_node_parser_gen gen_ast_node_d = gen_ast_node_parser_gen();
sl@0
   263
sl@0
   264
sl@0
   265
//////////////////////////////////
sl@0
   266
struct root_node_op
sl@0
   267
{
sl@0
   268
    template <typename MatchT>
sl@0
   269
    void operator()(MatchT& m) const
sl@0
   270
    {
sl@0
   271
        BOOST_SPIRIT_ASSERT(m.trees.size() > 0);
sl@0
   272
        m.trees.begin()->value.is_root(true);
sl@0
   273
    }
sl@0
   274
};
sl@0
   275
sl@0
   276
const node_parser_gen<root_node_op> root_node_d =
sl@0
   277
    node_parser_gen<root_node_op>();
sl@0
   278
sl@0
   279
///////////////////////////////////////////////////////////////////////////////
sl@0
   280
//
sl@0
   281
//  Parse functions for ASTs
sl@0
   282
//
sl@0
   283
///////////////////////////////////////////////////////////////////////////////
sl@0
   284
template <
sl@0
   285
    typename AstFactoryT, typename IteratorT, typename ParserT, 
sl@0
   286
    typename SkipT
sl@0
   287
>
sl@0
   288
inline tree_parse_info<IteratorT, AstFactoryT>
sl@0
   289
ast_parse(
sl@0
   290
    IteratorT const&        first_,
sl@0
   291
    IteratorT const&        last_,
sl@0
   292
    parser<ParserT> const&  parser,
sl@0
   293
    SkipT const&            skip_,
sl@0
   294
    AstFactoryT const &   /*dummy_*/ = AstFactoryT())
sl@0
   295
{
sl@0
   296
    typedef skip_parser_iteration_policy<SkipT> iter_policy_t;
sl@0
   297
    typedef ast_match_policy<IteratorT, AstFactoryT> ast_match_policy_t;
sl@0
   298
    typedef
sl@0
   299
        scanner_policies<iter_policy_t, ast_match_policy_t>
sl@0
   300
        scanner_policies_t;
sl@0
   301
    typedef scanner<IteratorT, scanner_policies_t> scanner_t;
sl@0
   302
sl@0
   303
    iter_policy_t iter_policy(skip_);
sl@0
   304
    scanner_policies_t policies(iter_policy);
sl@0
   305
    IteratorT first = first_;
sl@0
   306
    scanner_t scan(first, last_, policies);
sl@0
   307
    tree_match<IteratorT, AstFactoryT> hit = parser.derived().parse(scan);
sl@0
   308
    scan.skip(scan);
sl@0
   309
    return tree_parse_info<IteratorT, AstFactoryT>(
sl@0
   310
        first, hit, hit && (first == last_), hit.length(), hit.trees);
sl@0
   311
}
sl@0
   312
sl@0
   313
//////////////////////////////////
sl@0
   314
template <typename IteratorT, typename ParserT, typename SkipT>
sl@0
   315
inline tree_parse_info<IteratorT>
sl@0
   316
ast_parse(
sl@0
   317
    IteratorT const&        first_,
sl@0
   318
    IteratorT const&        last_,
sl@0
   319
    parser<ParserT> const&  parser,
sl@0
   320
    SkipT const&            skip_)
sl@0
   321
{
sl@0
   322
    typedef node_val_data_factory<nil_t> default_factory_t;
sl@0
   323
    return ast_parse(first_, last_, parser, skip_, default_factory_t());
sl@0
   324
}
sl@0
   325
  
sl@0
   326
//////////////////////////////////
sl@0
   327
template <typename IteratorT, typename ParserT>
sl@0
   328
inline tree_parse_info<IteratorT>
sl@0
   329
ast_parse(
sl@0
   330
    IteratorT const&        first_,
sl@0
   331
    IteratorT const&        last,
sl@0
   332
    parser<ParserT> const&  parser)
sl@0
   333
{
sl@0
   334
    typedef ast_match_policy<IteratorT> ast_match_policy_t;
sl@0
   335
    IteratorT first = first_;
sl@0
   336
    scanner<
sl@0
   337
        IteratorT,
sl@0
   338
        scanner_policies<iteration_policy, ast_match_policy_t>
sl@0
   339
    > scan(first, last);
sl@0
   340
    tree_match<IteratorT> hit = parser.derived().parse(scan);
sl@0
   341
    return tree_parse_info<IteratorT>(
sl@0
   342
        first, hit, hit && (first == last), hit.length(), hit.trees);
sl@0
   343
}
sl@0
   344
sl@0
   345
//////////////////////////////////
sl@0
   346
template <typename CharT, typename ParserT, typename SkipT>
sl@0
   347
inline tree_parse_info<CharT const*>
sl@0
   348
ast_parse(
sl@0
   349
    CharT const*            str,
sl@0
   350
    parser<ParserT> const&  parser,
sl@0
   351
    SkipT const&            skip)
sl@0
   352
{
sl@0
   353
    CharT const* last = str;
sl@0
   354
    while (*last)
sl@0
   355
        last++;
sl@0
   356
    return ast_parse(str, last, parser, skip);
sl@0
   357
}
sl@0
   358
sl@0
   359
//////////////////////////////////
sl@0
   360
template <typename CharT, typename ParserT>
sl@0
   361
inline tree_parse_info<CharT const*>
sl@0
   362
ast_parse(
sl@0
   363
    CharT const*            str,
sl@0
   364
    parser<ParserT> const&  parser)
sl@0
   365
{
sl@0
   366
    CharT const* last = str;
sl@0
   367
    while (*last)
sl@0
   368
    {
sl@0
   369
        last++;
sl@0
   370
    }
sl@0
   371
    return ast_parse(str, last, parser);
sl@0
   372
}
sl@0
   373
sl@0
   374
///////////////////////////////////////////////////////////////////////////////
sl@0
   375
}} // namespace boost::spirit
sl@0
   376
sl@0
   377
#endif
sl@0
   378