source: NonGTP/Boost/boost/spirit/utility/escape_char.hpp @ 857

Revision 857, 6.4 KB checked in by igarcia, 19 years ago (diff)
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_ESCAPE_CHAR_HPP
10#define BOOST_SPIRIT_ESCAPE_CHAR_HPP
11
12///////////////////////////////////////////////////////////////////////////////
13#include <string>
14#include <iterator>
15#include <cctype>
16#include <boost/limits.hpp>
17
18#include <boost/spirit/debug.hpp>
19
20#include <boost/spirit/utility/impl/escape_char.ipp>
21
22///////////////////////////////////////////////////////////////////////////////
23namespace boost { namespace spirit {
24
25///////////////////////////////////////////////////////////////////////////////
26//
27//  escape_char_action class
28//
29//      Links an escape char parser with a user defined semantic action.
30//      The semantic action may be a function or a functor. A function
31//      should be compatible with the interface:
32//
33//          void f(CharT ch);
34//
35//      A functor should have a member operator() with a compatible signature
36//      as above. The matching character is passed into the function/functor.
37//      This is the default class that character parsers use when dealing with
38//      the construct:
39//
40//          p[f]
41//
42//      where p is a parser and f is a function or functor.
43//
44///////////////////////////////////////////////////////////////////////////////
45template <
46    typename ParserT, typename ActionT,
47    unsigned long Flags, typename CharT
48>
49struct escape_char_action
50:   public unary<ParserT,
51        parser<escape_char_action<ParserT, ActionT, Flags, CharT> > >
52{
53    typedef escape_char_action
54        <ParserT, ActionT, Flags, CharT>        self_t;
55    typedef action_parser_category              parser_category_t;
56    typedef unary<ParserT, parser<self_t> >     base_t;
57
58    template <typename ScannerT>
59    struct result
60    {
61        typedef typename match_result<ScannerT, CharT>::type type;
62    };
63
64    escape_char_action(ParserT const& p, ActionT const& a)
65    : base_t(p), actor(a) {}
66
67    template <typename ScannerT>
68    typename parser_result<self_t, ScannerT>::type
69    parse(ScannerT const& scan) const
70    {
71        return impl::escape_char_action_parse<Flags, CharT>::
72            parse(scan, *this);
73    }
74
75    ActionT const& predicate() const { return actor; }
76
77private:
78
79    ActionT actor;
80};
81
82///////////////////////////////////////////////////////////////////////////////
83//
84//  escape_char_parser class
85//
86//      The escape_char_parser helps in conjunction with the escape_char_action
87//      template class (see above) in parsing escaped characters. There are two
88//      different variants of this parser: one for parsing C style escaped
89//      characters and one for parsing LEX style escaped characters.
90//
91//      The C style escaped character parser is generated, when the template
92//      parameter 'Flags' is equal to 'c_escapes' (a constant defined in the
93//      file impl/escape_char.ipp). This parser recognizes all valid C escape
94//      character sequences: '\t', '\b', '\f', '\n', '\r', '\"', '\'', '\\'
95//      and the numeric style escapes '\120' (octal) and '\x2f' (hexadecimal)
96//      and converts these to their character equivalent, for instance the
97//      sequence of a backslash and a 'b' is parsed as the character '\b'.
98//      All other escaped characters are rejected by this parser.
99//
100//      The LEX style escaped character parser is generated, when the template
101//      parameter 'Flags' is equal to 'lex_escapes' (a constant defined in the
102//      file impl/escape_char.ipp). This parser recognizes all the C style
103//      escaped character sequences (as described above) and additionally
104//      does not reject all other escape sequences. All not mentioned escape
105//      sequences are converted by the parser to the plain character, for
106//      instance '\a' will be parsed as 'a'.
107//
108//      All not escaped characters are parsed without modification.
109//
110///////////////////////////////////////////////////////////////////////////////
111
112template <unsigned long Flags, typename CharT>
113struct escape_char_action_parser_gen;
114
115template <unsigned long Flags, typename CharT = char>
116struct escape_char_parser :
117    public parser<escape_char_parser<Flags, CharT> > {
118
119    // only the values c_escapes and lex_escapes are valid for Flags
120    BOOST_STATIC_ASSERT(Flags == c_escapes || Flags == lex_escapes);
121
122    typedef escape_char_parser<Flags, CharT> self_t;
123    typedef
124        escape_char_action_parser_gen<Flags, CharT>
125        action_parser_generator_t;
126
127    template <typename ScannerT>
128    struct result {
129
130        typedef typename match_result<ScannerT, CharT>::type type;
131    };
132
133    template <typename ActionT>
134    escape_char_action<self_t, ActionT, Flags, CharT>
135    operator[](ActionT const& actor) const
136    {
137        return escape_char_action<self_t, ActionT, Flags, CharT>(*this, actor);
138    }
139
140    template <typename ScannerT>
141    typename parser_result<self_t, ScannerT>::type
142    parse(ScannerT const &scan) const
143    {
144        return impl::escape_char_parse<CharT>::parse(scan, *this);
145    }
146};
147
148template <unsigned long Flags, typename CharT>
149struct escape_char_action_parser_gen {
150
151    template <typename ParserT, typename ActionT>
152    static escape_char_action<ParserT, ActionT, Flags, CharT>
153    generate (ParserT const &p, ActionT const &actor)
154    {
155        typedef
156            escape_char_action<ParserT, ActionT, Flags, CharT>
157            action_parser_t;
158        return action_parser_t(p, actor);
159    }
160};
161
162///////////////////////////////////////////////////////////////////////////////
163//
164//  predefined escape_char_parser objects
165//
166//      These objects should be used for generating correct escaped character
167//      parsers.
168//
169///////////////////////////////////////////////////////////////////////////////
170const escape_char_parser<lex_escapes> lex_escape_ch_p =
171    escape_char_parser<lex_escapes>();
172
173const escape_char_parser<c_escapes> c_escape_ch_p =
174    escape_char_parser<c_escapes>();
175
176///////////////////////////////////////////////////////////////////////////////
177}} // namespace boost::spirit
178
179#endif
Note: See TracBrowser for help on using the repository browser.