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