summaryrefslogtreecommitdiffstats
blob: 30748c574dc929ff3d74bda3bc593103b0555bbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//  Copyright (c) 2001-2011 Hartmut Kaiser
// 
//  Distributed under the Boost Software License, Version 1.0. (See accompanying 
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#if !defined(BOOST_SPIRIT_LEX_LEXER_SEMANTIC_ACTION_DATA_JUN_10_2009_0417PM)
#define BOOST_SPIRIT_LEX_LEXER_SEMANTIC_ACTION_DATA_JUN_10_2009_0417PM

#if defined(_MSC_VER)
#pragma once
#endif

#include <boost/spirit/home/lex/lexer/pass_flags.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/function.hpp>
#include <vector>

namespace boost { namespace spirit { namespace lex { namespace lexertl
{ 
    namespace detail
    {
        ///////////////////////////////////////////////////////////////////////
        template <typename Iterator, typename SupportsState, typename Data>
        struct semantic_actions;

        // This specialization of semantic_actions will be used if the token
        // type (lexer definition) does not support states, which simplifies 
        // the data structures used to store the semantic action function 
        // objects.
        template <typename Iterator, typename Data>
        struct semantic_actions<Iterator, mpl::false_, Data>
        {
            typedef void functor_type(Iterator&, Iterator&
              , BOOST_SCOPED_ENUM(pass_flags)&, std::size_t&, Data&);
            typedef boost::function<functor_type> functor_wrapper_type;

            // add a semantic action function object
            template <typename F>
            void add_action(std::size_t unique_id, std::size_t, F act) 
            {
                if (actions_.size() <= unique_id)
                    actions_.resize(unique_id + 1); 

                actions_[unique_id] = act;
            }

            // try to invoke a semantic action for the given token (unique_id)
            BOOST_SCOPED_ENUM(pass_flags) invoke_actions(std::size_t /*state*/
              , std::size_t& id, std::size_t unique_id, Iterator& end
              , Data& data) const
            {
                // if there is nothing to invoke, continue with 'match'
                if (unique_id >= actions_.size() || !actions_[unique_id]) 
                    return pass_flags::pass_normal;

                // Note: all arguments might be changed by the invoked semantic 
                //       action
                BOOST_SCOPED_ENUM(pass_flags) match = pass_flags::pass_normal;
                actions_[unique_id](data.get_first(), end, match, id, data);
                return match;
            }

            std::vector<functor_wrapper_type> actions_;
        }; 

        // This specialization of semantic_actions will be used if the token
        // type (lexer definition) needs to support states, resulting in a more
        // complex data structure needed for storing the semantic action 
        // function objects.
        template <typename Iterator, typename Data>
        struct semantic_actions<Iterator, mpl::true_, Data>
        {
            typedef void functor_type(Iterator&, Iterator&
              , BOOST_SCOPED_ENUM(pass_flags)&, std::size_t&, Data&);
            typedef boost::function<functor_type> functor_wrapper_type;

            // add a semantic action function object
            template <typename F>
            void add_action(std::size_t unique_id, std::size_t state, F act) 
            {
                if (actions_.size() <= state)
                    actions_.resize(state + 1); 

                std::vector<functor_wrapper_type>& actions (actions_[state]);
                if (actions.size() <= unique_id)
                    actions.resize(unique_id + 1); 

                actions[unique_id] = act;
            }

            // try to invoke a semantic action for the given token (unique_id)
            BOOST_SCOPED_ENUM(pass_flags) invoke_actions(std::size_t state
              , std::size_t& id, std::size_t unique_id, Iterator& end
              , Data& data) const
            {
                // if there is no action defined for this state, return match
                if (state >= actions_.size())
                    return pass_flags::pass_normal;

                // if there is nothing to invoke, continue with 'match'
                std::vector<functor_wrapper_type> const& actions = actions_[state];
                if (unique_id >= actions.size() || !actions[unique_id]) 
                    return pass_flags::pass_normal;

                // set token value 
                data.set_end(end);

                // Note: all arguments might be changed by the invoked semantic 
                //       action
                BOOST_SCOPED_ENUM(pass_flags) match = pass_flags::pass_normal;
                actions[unique_id](data.get_first(), end, match, id, data);
                return match;
            }

            std::vector<std::vector<functor_wrapper_type> > actions_;
        }; 
    }

}}}}

#endif