1 | /*============================================================================= |
---|
2 | Copyright (c) 2002-2003 Joel de Guzman |
---|
3 | Copyright (c) 2002-2003 Hartmut Kaiser |
---|
4 | http://spirit.sourceforge.net/ |
---|
5 | |
---|
6 | Use, modification and distribution is subject to the Boost Software |
---|
7 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
8 | http://www.boost.org/LICENSE_1_0.txt) |
---|
9 | =============================================================================*/ |
---|
10 | #if !defined(BOOST_SPIRIT_AS_PARSER_HPP) |
---|
11 | #define BOOST_SPIRIT_AS_PARSER_HPP |
---|
12 | |
---|
13 | #include <boost/spirit/core/primitives/primitives.hpp> |
---|
14 | |
---|
15 | namespace boost { namespace spirit { |
---|
16 | |
---|
17 | /////////////////////////////////////////////////////////////////////////// |
---|
18 | // |
---|
19 | // Helper templates to derive the parser type from an auxilliary type |
---|
20 | // and to generate an object of the required parser type given an |
---|
21 | // auxilliary object. Supported types to convert are parsers, |
---|
22 | // single characters and character strings. |
---|
23 | // |
---|
24 | /////////////////////////////////////////////////////////////////////////// |
---|
25 | namespace impl |
---|
26 | { |
---|
27 | template<typename T> |
---|
28 | struct default_as_parser |
---|
29 | { |
---|
30 | typedef T type; |
---|
31 | static type const& convert(type const& p) |
---|
32 | { |
---|
33 | return p; |
---|
34 | } |
---|
35 | }; |
---|
36 | |
---|
37 | struct char_as_parser |
---|
38 | { |
---|
39 | typedef chlit<char> type; |
---|
40 | static type convert(char ch) |
---|
41 | { |
---|
42 | return type(ch); |
---|
43 | } |
---|
44 | }; |
---|
45 | |
---|
46 | struct wchar_as_parser |
---|
47 | { |
---|
48 | typedef chlit<wchar_t> type; |
---|
49 | static type convert(wchar_t ch) |
---|
50 | { |
---|
51 | return type(ch); |
---|
52 | } |
---|
53 | }; |
---|
54 | |
---|
55 | struct string_as_parser |
---|
56 | { |
---|
57 | typedef strlit<char const*> type; |
---|
58 | static type convert(char const* str) |
---|
59 | { |
---|
60 | return type(str); |
---|
61 | } |
---|
62 | }; |
---|
63 | |
---|
64 | struct wstring_as_parser |
---|
65 | { |
---|
66 | typedef strlit<wchar_t const*> type; |
---|
67 | static type convert(wchar_t const* str) |
---|
68 | { |
---|
69 | return type(str); |
---|
70 | } |
---|
71 | }; |
---|
72 | } |
---|
73 | |
---|
74 | template<typename T> |
---|
75 | struct as_parser : impl::default_as_parser<T> {}; |
---|
76 | |
---|
77 | template<> |
---|
78 | struct as_parser<char> : impl::char_as_parser {}; |
---|
79 | |
---|
80 | template<> |
---|
81 | struct as_parser<wchar_t> : impl::wchar_as_parser {}; |
---|
82 | |
---|
83 | template<> |
---|
84 | struct as_parser<char*> : impl::string_as_parser {}; |
---|
85 | |
---|
86 | template<> |
---|
87 | struct as_parser<char const*> : impl::string_as_parser {}; |
---|
88 | |
---|
89 | template<> |
---|
90 | struct as_parser<wchar_t*> : impl::wstring_as_parser {}; |
---|
91 | |
---|
92 | template<> |
---|
93 | struct as_parser<wchar_t const*> : impl::wstring_as_parser {}; |
---|
94 | |
---|
95 | template<int N> |
---|
96 | struct as_parser<char[N]> : impl::string_as_parser {}; |
---|
97 | |
---|
98 | template<int N> |
---|
99 | struct as_parser<wchar_t[N]> : impl::wstring_as_parser {}; |
---|
100 | |
---|
101 | template<int N> |
---|
102 | struct as_parser<char const[N]> : impl::string_as_parser {}; |
---|
103 | |
---|
104 | template<int N> |
---|
105 | struct as_parser<wchar_t const[N]> : impl::wstring_as_parser {}; |
---|
106 | |
---|
107 | }} // namespace boost::spirit |
---|
108 | |
---|
109 | #endif |
---|