1 /*==============================================================================
2 Copyright (c) 2001-2011 Hartmut Kaiser
3 Copyright (c) 2010-2011 Bryce Lelbach
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file BOOST_LICENSE_1_0.rst or copy at http://www.boost.org/LICENSE_1_0.txt)
7 ==============================================================================*/
9 #if !defined(BOOST_SPIRIT_UTREE_EXAMPLE_SEXPR_GENERATOR_HPP)
10 #define BOOST_SPIRIT_UTREE_EXAMPLE_SEXPR_GENERATOR_HPP
12 #include <boost/spirit/include/support_utree.hpp>
13 #include <boost/spirit/include/karma.hpp>
20 struct transform_attribute<utree::nil_type, unused_type, karma::domain> {
21 typedef unused_type type;
23 static unused_type pre (utree::nil_type&) { return unused_type(); }
33 namespace karma = boost::spirit::karma;
34 namespace standard = boost::spirit::standard;
36 using boost::spirit::utree;
37 using boost::spirit::utf8_symbol_range_type;
38 using boost::spirit::utf8_string_range_type;
39 using boost::spirit::binary_range_type;
41 struct bool_output_policies : karma::bool_policies<>
43 template <typename CharEncoding, typename Tag, typename Iterator>
44 static bool generate_true(Iterator& sink, bool)
46 return string_inserter<CharEncoding, Tag>::call(sink, "#t");
49 template <typename CharEncoding, typename Tag, typename Iterator>
50 static bool generate_false(Iterator& sink, bool)
52 return string_inserter<CharEncoding, Tag>::call(sink, "#f");
56 template <typename Iterator>
57 struct generator : karma::grammar<Iterator, utree()>
59 typedef boost::iterator_range<utree::const_iterator> utree_list;
61 karma::rule<Iterator, utree()>
64 karma::rule<Iterator, utree_list()>
67 karma::rule<Iterator, utf8_symbol_range_type()>
70 karma::rule<Iterator, utree::nil_type()>
73 karma::rule<Iterator, utf8_string_range_type()>
76 karma::rule<Iterator, binary_range_type()>
79 generator() : generator::base_type(start)
81 using standard::char_;
82 using standard::string;
83 using karma::bool_generator;
84 using karma::uint_generator;
88 using karma::right_align;
90 uint_generator<unsigned char, 16> hex2;
91 bool_generator<bool, bool_output_policies> boolean;
105 list = '(' << -(start % ' ') << ')';
107 utf8 = '"' << *(&char_('"') << "\\\"" | char_) << '"';
111 binary = '#' << *right_align(2, '0')[hex2] << '#';
113 nil_ = karma::attr_cast(lit("nil"));
119 symbol.name("symbol");
120 binary.name("binary");
127 #endif // BOOST_SPIRIT_UTREE_EXAMPLE_SEXPR_GENERATOR_HPP