/*============================================================================= Copyright (c) 2001-2007 Joel de Guzman Distributed under 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) ==============================================================================*/ #if !defined(BOOST_SPIRIT_CHAR_APR_16_2006_1051AM) #define BOOST_SPIRIT_CHAR_APR_16_2006_1051AM #include #include #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace spirit { namespace qi { /////////////////////////////////////////////////////////////////////////// // parse any character /////////////////////////////////////////////////////////////////////////// template struct any_char : char_parser, Char> { template static bool test(Component const&, CharParam, Context&) { return true; } template static std::string what(Component const& component, Context const& ctx) { return "any-char"; } }; /////////////////////////////////////////////////////////////////////////// // parse a single character /////////////////////////////////////////////////////////////////////////// template struct literal_char : char_parser, Char> { template struct attribute { typedef unused_type type; // literal parsers have no attribute }; template static bool test(Component const& component, CharParam ch, Context&) { return detail::get_char(fusion::at_c<0>(component.elements)) == ch; } template static std::string what(Component const& component, Context const& ctx) { return std::string("'") + spirit::detail::to_narrow_char( detail::get_char(fusion::at_c<0>(component.elements))) + '\''; } }; /////////////////////////////////////////////////////////////////////////// // parse a character set /////////////////////////////////////////////////////////////////////////// template struct char_set : char_parser, Char> { template static bool test(Component const& component, CharParam ch, Context&) { return component.ptr->test(ch); } template static std::string what(Component const& component, Context const& ctx) { return "char-set"; } }; /////////////////////////////////////////////////////////////////////////// // parse a lazy character /////////////////////////////////////////////////////////////////////////// struct lazy_char : char_parser { template struct attribute { typedef typename result_of::subject::type subject_type; typedef typename remove_reference< typename boost::result_of::type >::type type; }; template static bool test(Component const& component, CharParam ch, Context& context) { return fusion::at_c<0>(component.elements)(unused, context) == ch; } template static std::string what(Component const& component, Context const& ctx) { return std::string("'") + spirit::detail::to_narrow_char( fusion::at_c<0>(component.elements)(unused, ctx)) + '\''; } }; /////////////////////////////////////////////////////////////////////////// // parse a character range /////////////////////////////////////////////////////////////////////////// template struct char_range : char_parser, Char> { template static bool test(Component const& component, CharParam ch, Context&) { return !(ch < fusion::at_c<0>(component.elements)) && !(fusion::at_c<1>(component.elements) < ch); } template static std::string what(Component const& component, Context const& ctx) { std::string result; result += std::string("'") + fusion::at_c<0>(component.elements) + '\''; result += "..."; result += std::string("'") + fusion::at_c<1>(component.elements) + '\''; return result; } }; /////////////////////////////////////////////////////////////////////////// // parse a lazy character range /////////////////////////////////////////////////////////////////////////// struct lazy_char_range : char_parser { template struct attribute { typedef typename result_of::subject::type subject_type; typedef typename remove_reference< typename boost::result_of::type >::type type; }; template static bool test(Component const& component, CharParam ch, Context& context) { return !(ch < fusion::at_c<0>(component.elements)(unused, context)) && !(fusion::at_c<1>(component.elements)(unused, context) < ch); } template static std::string what(Component const& component, Context const& ctx) { return "char-range"; } }; /////////////////////////////////////////////////////////////////////////// // no_case literal_char version /////////////////////////////////////////////////////////////////////////// template struct no_case_literal_char : char_parser, Char> { template struct attribute { typedef unused_type type; // literal parsers have no attribute }; template static bool test(Component const& component, CharParam ch, Context&) { return detail::get_char(fusion::at_c<0>(component.elements)) == ch || detail::get_char(fusion::at_c<1>(component.elements)) == ch ; } template static std::string what(Component const& component, Context const& ctx) { std::string result; result += std::string("'") + spirit::detail::to_narrow_char( detail::get_char(fusion::at_c<0>(component.elements))) + '\''; result += " or "; result += std::string("'") + spirit::detail::to_narrow_char( detail::get_char(fusion::at_c<1>(component.elements))) + '\''; return result; } }; /////////////////////////////////////////////////////////////////////////// // no_case char_range version /////////////////////////////////////////////////////////////////////////// template struct no_case_char_range : char_parser, Char> { template static bool test(Component const& component, CharParam ch, Context&) { return (!(ch < fusion::at_c<0>(component.elements)) && !(fusion::at_c<1>(component.elements) < ch)) || (!(ch < fusion::at_c<2>(component.elements)) && !(fusion::at_c<3>(component.elements) < ch)) ; } template static std::string what(Component const& component, Context const& ctx) { std::string result; result += std::string("'") + fusion::at_c<0>(component.elements) + '\''; result += "..."; result += std::string("'") + fusion::at_c<1>(component.elements) + '\''; result += " or "; result += std::string("'") + fusion::at_c<2>(component.elements) + '\''; result += "..."; result += std::string("'") + fusion::at_c<3>(component.elements) + '\''; return result; } }; template struct char_set_component; }}} namespace boost { namespace spirit { namespace traits { /////////////////////////////////////////////////////////////////////////// // char_set_component generator /////////////////////////////////////////////////////////////////////////// template struct make_component, Elements, Modifier , typename disable_if< is_member_of_modifier >::type > : mpl::identity > { static qi::char_set_component call(Elements const& elements) { return qi::char_set_component( fusion::at_c<0>(elements)); } }; /////////////////////////////////////////////////////////////////////////// // no_case char_set_component generator /////////////////////////////////////////////////////////////////////////// template < typename Domain, typename Elements, typename Modifier, typename Char > struct make_modified_component< Domain, qi::char_set, Elements, Modifier , typename enable_if< is_member_of_modifier >::type > { typedef qi::char_set_component type; typedef typename Modifier::char_set char_set; static type call(Elements const& elements) { return qi::char_set_component( fusion::at_c<0>(elements), char_set()); } }; /////////////////////////////////////////////////////////////////////////// // no_case_literal_char generator /////////////////////////////////////////////////////////////////////////// template < typename Domain, typename Elements, typename Modifier, typename Char > struct make_modified_component< Domain, qi::literal_char, Elements, Modifier , typename enable_if< is_member_of_modifier >::type > { typedef fusion::vector vector_type; typedef component, vector_type> type; static type call(Elements const& elements) { typedef typename Modifier::char_set char_set; Char ch = qi::detail::get_char(fusion::at_c<0>(elements)); vector_type v( char_set::tolower(ch) , char_set::toupper(ch) ); return type(v); } }; /////////////////////////////////////////////////////////////////////////// // no_case_char_range generator /////////////////////////////////////////////////////////////////////////// template < typename Domain, typename Elements, typename Modifier, typename Char > struct make_modified_component< Domain, qi::char_range, Elements, Modifier , typename enable_if< is_member_of_modifier >::type > { typedef fusion::vector vector_type; typedef component, vector_type> type; static type call(Elements const& elements) { typedef typename Modifier::char_set char_set; Char first = fusion::at_c<0>(elements); Char last = fusion::at_c<1>(elements); vector_type v( char_set::tolower(first) , char_set::tolower(last) , char_set::toupper(first) , char_set::toupper(last) ); return type(v); } }; }}} #endif