// Copyright (c) 2001-2010 Hartmut Kaiser // Copyright (c) 2001-2010 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_GENERATOR_SEP_07_2009_0417PM) #define BOOST_SPIRIT_CHAR_GENERATOR_SEP_07_2009_0417PM #if defined(_MSC_VER) #pragma once #endif #include #include #include #include #include #include #include #include #include namespace boost { namespace spirit { /////////////////////////////////////////////////////////////////////////// // Enablers /////////////////////////////////////////////////////////////////////////// template <> struct use_operator // enables ~ : mpl::true_ {}; }} namespace boost { namespace spirit { namespace traits // classification { namespace detail { BOOST_MPL_HAS_XXX_TRAIT_DEF(char_generator_id) } template struct is_char_generator : detail::has_char_generator_id {}; }}} namespace boost { namespace spirit { namespace karma { /////////////////////////////////////////////////////////////////////////// // The base char_parser /////////////////////////////////////////////////////////////////////////// template struct char_generator : primitive_generator { typedef CharEncoding char_encoding; typedef Tag tag; typedef Char char_type; struct char_generator_id; // if Attr is unused_type, Derived must supply its own attribute // metafunction template struct attribute { typedef Attr type; }; template < typename OutputIterator, typename Context, typename Delimiter , typename Attribute> bool generate(OutputIterator& sink, Context& context, Delimiter const& d , Attribute const& attr) const { if (!traits::has_optional_value(attr)) return false; Attr ch = Attr(); if (!this->derived().test(traits::extract_from(attr, context), ch, context)) return false; return karma::detail::generate_to(sink, ch, char_encoding(), tag()) && karma::delimit_out(sink, d); // always do post-delimiting } // Requirement: g.test(attr, ch, context) -> bool // // attr: associated attribute // ch: character to be generated (set by test()) // context: enclosing rule context }; /////////////////////////////////////////////////////////////////////////// // negated_char_generator handles ~cg expressions (cg is a char_generator) /////////////////////////////////////////////////////////////////////////// template struct negated_char_generator : char_generator , typename Positive::char_encoding, typename Positive::tag> { negated_char_generator(Positive const& positive) : positive(positive) {} template bool test(Attribute const& attr, CharParam& ch, Context& context) const { return !positive.test(attr, ch, context); } template info what(Context& context) const { return info("not", positive.what(context)); } Positive positive; }; /////////////////////////////////////////////////////////////////////////// // Generator generators: make_xxx function (objects) /////////////////////////////////////////////////////////////////////////// namespace detail { template struct make_negated_char_generator { typedef negated_char_generator result_type; result_type operator()(Positive const& positive) const { return result_type(positive); } }; template struct make_negated_char_generator > { typedef Positive result_type; result_type operator()(negated_char_generator const& ncg) const { return ncg.positive; } }; } template struct make_composite { typedef typename fusion::result_of::value_at_c::type subject; BOOST_SPIRIT_ASSERT_MSG(( traits::is_char_generator::value ), subject_is_not_negatable, (subject)); typedef typename detail::make_negated_char_generator::result_type result_type; result_type operator()(Elements const& elements, unused_type) const { return detail::make_negated_char_generator()( fusion::at_c<0>(elements)); } }; }}} #endif