Chris@16: /*============================================================================= Chris@16: Copyright (c) 2001-2011 Joel de Guzman Chris@16: Copyright (c) 2001-2011 Hartmut Kaiser Chris@16: Copyright (c) 2010 Bryce Lelbach 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: #if !defined(BOOST_SPIRIT_LIT_APR_18_2006_1125PM) Chris@16: #define BOOST_SPIRIT_LIT_APR_18_2006_1125PM Chris@16: Chris@16: #if defined(_MSC_VER) Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { namespace spirit Chris@16: { Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: // Enablers Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template Chris@16: struct use_terminal >::type> // enables strings Chris@16: : mpl::true_ {}; Chris@16: Chris@16: template Chris@16: struct use_terminal // enables string(str) Chris@16: , fusion::vector1 > Chris@16: > : traits::is_string {}; Chris@16: Chris@16: template // enables string(f) Chris@16: struct use_lazy_terminal< Chris@16: qi::domain Chris@16: , tag::char_code Chris@16: , 1 /*arity*/ Chris@16: > : mpl::true_ {}; Chris@16: Chris@16: // enables lit(...) Chris@16: template Chris@16: struct use_terminal > Chris@16: , typename enable_if >::type> Chris@16: : mpl::true_ {}; Chris@16: }} Chris@16: Chris@16: namespace boost { namespace spirit { namespace qi Chris@16: { Chris@16: #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS Chris@16: using spirit::lit; Chris@16: #endif Chris@16: using spirit::lit_type; Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: // Parse for literal strings Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template Chris@16: struct literal_string Chris@16: : primitive_parser > Chris@16: { Chris@16: typedef typename Chris@16: remove_const::type>::type Chris@16: char_type; Chris@16: typedef std::basic_string string_type; Chris@16: Chris@16: literal_string(typename add_reference::type str_) Chris@16: : str(str_) Chris@16: {} Chris@16: Chris@16: template Chris@16: struct attribute Chris@16: { Chris@16: typedef typename mpl::if_c< Chris@16: no_attribute, unused_type, string_type>::type Chris@16: type; Chris@16: }; Chris@16: Chris@16: template Chris@16: bool parse(Iterator& first, Iterator const& last Chris@16: , Context& /*context*/, Skipper const& skipper, Attribute& attr_) const Chris@16: { Chris@16: qi::skip_over(first, last, skipper); Chris@16: return detail::string_parse(str, first, last, attr_); Chris@16: } Chris@16: Chris@16: template Chris@16: info what(Context& /*context*/) const Chris@16: { Chris@16: return info("literal-string", str); Chris@16: } Chris@16: Chris@16: String str; Chris@16: Chris@16: private: Chris@16: // silence MSVC warning C4512: assignment operator could not be generated Chris@16: literal_string& operator= (literal_string const&); Chris@16: }; Chris@16: Chris@16: template Chris@16: struct no_case_literal_string Chris@16: : primitive_parser > Chris@16: { Chris@16: typedef typename Chris@16: remove_const::type>::type Chris@16: char_type; Chris@16: typedef std::basic_string string_type; Chris@16: Chris@16: template Chris@16: no_case_literal_string(char_type const* in, CharEncoding encoding) Chris@16: : str_lo(in) Chris@16: , str_hi(in) Chris@16: { Chris@16: #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600)) Chris@16: encoding; // suppresses warning: C4100: 'encoding' : unreferenced formal parameter Chris@16: #endif Chris@16: typename string_type::iterator loi = str_lo.begin(); Chris@16: typename string_type::iterator hii = str_hi.begin(); Chris@16: Chris@16: for (; loi != str_lo.end(); ++loi, ++hii, ++in) Chris@16: { Chris@16: typedef typename CharEncoding::char_type encoded_char_type; Chris@16: Chris@16: *loi = static_cast(encoding.tolower(encoded_char_type(*loi))); Chris@16: *hii = static_cast(encoding.toupper(encoded_char_type(*hii))); Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: struct attribute Chris@16: { Chris@16: typedef typename mpl::if_c< Chris@16: no_attribute, unused_type, string_type>::type Chris@16: type; Chris@16: }; Chris@16: Chris@16: template Chris@16: bool parse(Iterator& first, Iterator const& last Chris@16: , Context& /*context*/, Skipper const& skipper, Attribute& attr_) const Chris@16: { Chris@16: qi::skip_over(first, last, skipper); Chris@16: return detail::string_parse(str_lo, str_hi, first, last, attr_); Chris@16: } Chris@16: Chris@16: template Chris@16: info what(Context& /*context*/) const Chris@16: { Chris@16: return info("no-case-literal-string", str_lo); Chris@16: } Chris@16: Chris@16: string_type str_lo, str_hi; Chris@16: }; Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: // Parser generators: make_xxx function (objects) Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template Chris@16: struct make_primitive >::type> Chris@16: { Chris@16: typedef has_modifier > no_case; Chris@16: Chris@16: typedef typename add_const::type const_string; Chris@16: typedef typename mpl::if_< Chris@16: no_case Chris@16: , no_case_literal_string Chris@16: , literal_string >::type Chris@16: result_type; Chris@16: Chris@16: result_type operator()( Chris@16: typename add_reference::type str, unused_type) const Chris@16: { Chris@16: return op(str, no_case()); Chris@16: } Chris@16: Chris@16: template Chris@16: result_type op(String const& str, mpl::false_) const Chris@16: { Chris@16: return result_type(str); Chris@16: } Chris@16: Chris@16: template Chris@16: result_type op(String const& str, mpl::true_) const Chris@16: { Chris@16: typename spirit::detail::get_encoding::type encoding; Chris@16: return result_type(traits::get_c_string(str), encoding); Chris@16: } Chris@16: }; Chris@16: Chris@16: // lit("...") Chris@16: template Chris@16: struct make_primitive< Chris@16: terminal_ex > Chris@16: , Modifiers Chris@16: , typename enable_if >::type> Chris@16: { Chris@16: typedef has_modifier > no_case; Chris@16: Chris@16: typedef typename add_const::type const_string; Chris@16: typedef typename mpl::if_< Chris@16: no_case Chris@16: , no_case_literal_string Chris@16: , literal_string >::type Chris@16: result_type; Chris@16: Chris@16: template Chris@16: result_type operator()(Terminal const& term, unused_type) const Chris@16: { Chris@16: return op(fusion::at_c<0>(term.args), no_case()); Chris@16: } Chris@16: Chris@16: template Chris@16: result_type op(String const& str, mpl::false_) const Chris@16: { Chris@16: return result_type(str); Chris@16: } Chris@16: Chris@16: template Chris@16: result_type op(String const& str, mpl::true_) const Chris@16: { Chris@16: typedef typename traits::char_encoding_from_char< Chris@16: typename traits::char_type_of::type>::type encoding_type; Chris@16: typename spirit::detail::get_encoding::type encoding; Chris@16: return result_type(traits::get_c_string(str), encoding); Chris@16: } Chris@16: }; Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: // string("...") Chris@16: template Chris@16: struct make_primitive< Chris@16: terminal_ex< Chris@16: tag::char_code Chris@16: , fusion::vector1 > Chris@16: , Modifiers> Chris@16: { Chris@16: typedef CharEncoding encoding; Chris@16: typedef has_modifier > no_case; Chris@16: Chris@16: typedef typename add_const::type const_string; Chris@16: typedef typename mpl::if_< Chris@16: no_case Chris@16: , no_case_literal_string Chris@16: , literal_string >::type Chris@16: result_type; Chris@16: Chris@16: template Chris@16: result_type operator()(Terminal const& term, unused_type) const Chris@16: { Chris@16: return op(fusion::at_c<0>(term.args), no_case()); Chris@16: } Chris@16: Chris@16: template Chris@16: result_type op(String const& str, mpl::false_) const Chris@16: { Chris@16: return result_type(str); Chris@16: } Chris@16: Chris@16: template Chris@16: result_type op(String const& str, mpl::true_) const Chris@16: { Chris@16: return result_type(traits::get_c_string(str), encoding()); Chris@16: } Chris@16: }; Chris@16: }}} Chris@16: Chris@16: namespace boost { namespace spirit { namespace traits Chris@16: { Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template Chris@16: struct handles_container Chris@16: , Attribute, Context, Iterator> Chris@16: : mpl::true_ {}; Chris@16: Chris@16: template Chris@16: struct handles_container Chris@16: , Attribute, Context, Iterator> Chris@16: : mpl::true_ {}; Chris@16: }}} Chris@16: Chris@16: #endif