]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright (c) 2001-2011 Hartmut Kaiser |
2 | // | |
3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
5 | ||
6 | #if !defined(BOOST_SPIRIT_LEX_PLAIN_RAW_TOKEN_JUN_03_2011_0853PM) | |
7 | #define BOOST_SPIRIT_LEX_PLAIN_RAW_TOKEN_JUN_03_2011_0853PM | |
8 | ||
9 | #if defined(_MSC_VER) | |
10 | #pragma once | |
11 | #endif | |
12 | ||
13 | #include <boost/spirit/home/support/info.hpp> | |
14 | #include <boost/spirit/home/qi/detail/attributes.hpp> | |
15 | #include <boost/spirit/home/support/common_terminals.hpp> | |
16 | #include <boost/spirit/home/support/handles_container.hpp> | |
17 | #include <boost/spirit/home/qi/skip_over.hpp> | |
18 | #include <boost/spirit/home/qi/domain.hpp> | |
19 | #include <boost/spirit/home/qi/parser.hpp> | |
20 | #include <boost/spirit/home/qi/meta_compiler.hpp> | |
21 | #include <boost/spirit/home/qi/detail/assign_to.hpp> | |
22 | #include <boost/range/iterator_range.hpp> | |
23 | #include <boost/fusion/include/vector.hpp> | |
24 | #include <boost/fusion/include/at.hpp> | |
25 | #include <boost/mpl/or.hpp> | |
26 | #include <boost/type_traits/is_integral.hpp> | |
27 | #include <boost/type_traits/is_enum.hpp> | |
28 | #include <boost/lexical_cast.hpp> | |
92f5a8d4 | 29 | #include <iterator> // for std::iterator_traits |
7c673cae FG |
30 | |
31 | namespace boost { namespace spirit | |
32 | { | |
33 | /////////////////////////////////////////////////////////////////////////// | |
34 | // Enablers | |
35 | /////////////////////////////////////////////////////////////////////////// | |
36 | ||
37 | // enables raw_token | |
38 | template <> | |
39 | struct use_terminal<qi::domain, tag::raw_token> | |
40 | : mpl::true_ {}; | |
41 | ||
42 | // enables raw_token(id) | |
43 | template <typename A0> | |
44 | struct use_terminal<qi::domain | |
45 | , terminal_ex<tag::raw_token, fusion::vector1<A0> > | |
46 | > : mpl::or_<is_integral<A0>, is_enum<A0> > {}; | |
47 | ||
48 | // enables *lazy* raw_token(id) | |
49 | template <> | |
50 | struct use_lazy_terminal< | |
51 | qi::domain, tag::raw_token, 1 | |
52 | > : mpl::true_ {}; | |
53 | }} | |
54 | ||
55 | namespace boost { namespace spirit { namespace qi | |
56 | { | |
57 | #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS | |
58 | using spirit::raw_token; | |
59 | #endif | |
60 | using spirit::raw_token_type; | |
61 | ||
62 | /////////////////////////////////////////////////////////////////////////// | |
63 | template <typename TokenId> | |
64 | struct plain_raw_token | |
65 | : primitive_parser<plain_raw_token<TokenId> > | |
66 | { | |
67 | template <typename Context, typename Iterator> | |
68 | struct attribute | |
69 | { | |
70 | typedef unused_type type; | |
71 | }; | |
72 | ||
73 | plain_raw_token(TokenId const& id) | |
74 | : id(id) {} | |
75 | ||
76 | template <typename Iterator, typename Context | |
77 | , typename Skipper, typename Attribute> | |
78 | bool parse(Iterator& first, Iterator const& last | |
79 | , Context& /*context*/, Skipper const& skipper | |
80 | , Attribute& attr) const | |
81 | { | |
82 | qi::skip_over(first, last, skipper); // always do a pre-skip | |
83 | ||
84 | if (first != last) { | |
85 | // simply match the token id with the id this component has | |
86 | // been initialized with | |
87 | ||
88 | typedef typename | |
92f5a8d4 | 89 | std::iterator_traits<Iterator>::value_type |
7c673cae FG |
90 | token_type; |
91 | typedef typename token_type::id_type id_type; | |
92 | ||
93 | token_type const& t = *first; | |
94 | if (id_type(~0) == id_type(id) || id_type(id) == t.id()) { | |
95 | spirit::traits::assign_to(t, attr); | |
96 | ++first; | |
97 | return true; | |
98 | } | |
99 | } | |
100 | return false; | |
101 | } | |
102 | ||
103 | template <typename Context> | |
104 | info what(Context& /*context*/) const | |
105 | { | |
106 | return info("raw_token", | |
107 | "raw_token(" + boost::lexical_cast<utf8_string>(id) + ")"); | |
108 | } | |
109 | ||
110 | TokenId id; | |
111 | }; | |
112 | ||
113 | /////////////////////////////////////////////////////////////////////////// | |
114 | // Parser generators: make_xxx function (objects) | |
115 | /////////////////////////////////////////////////////////////////////////// | |
116 | template <typename Modifiers> | |
117 | struct make_primitive<tag::raw_token, Modifiers> | |
118 | { | |
119 | typedef plain_raw_token<std::size_t> result_type; | |
120 | ||
121 | result_type operator()(unused_type, unused_type) const | |
122 | { | |
123 | return result_type(std::size_t(~0)); | |
124 | } | |
125 | }; | |
126 | ||
127 | template <typename Modifiers, typename TokenId> | |
128 | struct make_primitive<terminal_ex<tag::raw_token, fusion::vector1<TokenId> > | |
129 | , Modifiers> | |
130 | { | |
131 | typedef plain_raw_token<TokenId> result_type; | |
132 | ||
133 | template <typename Terminal> | |
134 | result_type operator()(Terminal const& term, unused_type) const | |
135 | { | |
136 | return result_type(fusion::at_c<0>(term.args)); | |
137 | } | |
138 | }; | |
139 | }}} | |
140 | ||
141 | namespace boost { namespace spirit { namespace traits | |
142 | { | |
143 | /////////////////////////////////////////////////////////////////////////// | |
144 | template<typename Idtype, typename Attr, typename Context, typename Iterator> | |
145 | struct handles_container<qi::plain_raw_token<Idtype>, Attr, Context, Iterator> | |
146 | : mpl::true_ | |
147 | {}; | |
148 | }}} | |
149 | ||
150 | #endif |