Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/boost/spirit/utility/escape_char.hpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

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