/*============================================================================= Copyright (c) 2001-2003 Daniel Nuffer http://spirit.sourceforge.net/ Use, modification and distribution is subject to 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) =============================================================================*/ #ifndef BOOST_SPIRIT_ESCAPE_CHAR_HPP #define BOOST_SPIRIT_ESCAPE_CHAR_HPP /////////////////////////////////////////////////////////////////////////////// #include #include #include #include #include #include #include /////////////////////////////////////////////////////////////////////////////// namespace boost { namespace spirit { /////////////////////////////////////////////////////////////////////////////// // // escape_char_action class // // Links an escape char parser with a user defined semantic action. // The semantic action may be a function or a functor. A function // should be compatible with the interface: // // void f(CharT ch); // // A functor should have a member operator() with a compatible signature // as above. The matching character is passed into the function/functor. // This is the default class that character parsers use when dealing with // the construct: // // p[f] // // where p is a parser and f is a function or functor. // /////////////////////////////////////////////////////////////////////////////// template < typename ParserT, typename ActionT, unsigned long Flags, typename CharT > struct escape_char_action : public unary > > { typedef escape_char_action self_t; typedef action_parser_category parser_category_t; typedef unary > base_t; template struct result { typedef typename match_result::type type; }; escape_char_action(ParserT const& p, ActionT const& a) : base_t(p), actor(a) {} template typename parser_result::type parse(ScannerT const& scan) const { return impl::escape_char_action_parse:: parse(scan, *this); } ActionT const& predicate() const { return actor; } private: ActionT actor; }; /////////////////////////////////////////////////////////////////////////////// // // escape_char_parser class // // The escape_char_parser helps in conjunction with the escape_char_action // template class (see above) in parsing escaped characters. There are two // different variants of this parser: one for parsing C style escaped // characters and one for parsing LEX style escaped characters. // // The C style escaped character parser is generated, when the template // parameter 'Flags' is equal to 'c_escapes' (a constant defined in the // file impl/escape_char.ipp). This parser recognizes all valid C escape // character sequences: '\t', '\b', '\f', '\n', '\r', '\"', '\'', '\\' // and the numeric style escapes '\120' (octal) and '\x2f' (hexadecimal) // and converts these to their character equivalent, for instance the // sequence of a backslash and a 'b' is parsed as the character '\b'. // All other escaped characters are rejected by this parser. // // The LEX style escaped character parser is generated, when the template // parameter 'Flags' is equal to 'lex_escapes' (a constant defined in the // file impl/escape_char.ipp). This parser recognizes all the C style // escaped character sequences (as described above) and additionally // does not reject all other escape sequences. All not mentioned escape // sequences are converted by the parser to the plain character, for // instance '\a' will be parsed as 'a'. // // All not escaped characters are parsed without modification. // /////////////////////////////////////////////////////////////////////////////// template struct escape_char_action_parser_gen; template struct escape_char_parser : public parser > { // only the values c_escapes and lex_escapes are valid for Flags BOOST_STATIC_ASSERT(Flags == c_escapes || Flags == lex_escapes); typedef escape_char_parser self_t; typedef escape_char_action_parser_gen action_parser_generator_t; template struct result { typedef typename match_result::type type; }; template escape_char_action operator[](ActionT const& actor) const { return escape_char_action(*this, actor); } template typename parser_result::type parse(ScannerT const &scan) const { return impl::escape_char_parse::parse(scan, *this); } }; template struct escape_char_action_parser_gen { template static escape_char_action generate (ParserT const &p, ActionT const &actor) { typedef escape_char_action action_parser_t; return action_parser_t(p, actor); } }; /////////////////////////////////////////////////////////////////////////////// // // predefined escape_char_parser objects // // These objects should be used for generating correct escaped character // parsers. // /////////////////////////////////////////////////////////////////////////////// const escape_char_parser lex_escape_ch_p = escape_char_parser(); const escape_char_parser c_escape_ch_p = escape_char_parser(); /////////////////////////////////////////////////////////////////////////////// }} // namespace boost::spirit #endif