| 1 | // Copyright (c) 2001-2012 Hartmut Kaiser |
| 2 | // Copyright (c) 2012 Benjamin Schindler |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | #include <boost/spirit/include/karma.hpp> |
| 8 | |
| 9 | #include <boost/core/lightweight_test.hpp> |
| 10 | #include <boost/fusion/include/adapt_struct.hpp> |
| 11 | #include <set> |
| 12 | |
| 13 | namespace generator |
| 14 | { |
| 15 | struct Enum |
| 16 | { |
| 17 | std::string enumName; |
| 18 | std::vector<std::string> enumEntries; |
| 19 | }; |
| 20 | typedef boost::variant<std::string, Enum> VariantType; |
| 21 | |
| 22 | namespace karma = boost::spirit::karma; |
| 23 | |
| 24 | // Our grammar definition |
| 25 | template<typename Iterator> |
| 26 | struct : karma::grammar<Iterator, VariantType()> |
| 27 | { |
| 28 | () : SettingsHeaderGenerator::base_type(baseRule) |
| 29 | { |
| 30 | using karma::lit; |
| 31 | using karma::string; |
| 32 | |
| 33 | enumRule = lit("enum " ) << string << lit("\n{\n" ) << string % ",\n" << "}" ; |
| 34 | declarationRule = lit("class " ) << string << ';'; |
| 35 | baseRule = (declarationRule | enumRule) << lit("\n" ); |
| 36 | |
| 37 | baseRule.name("base" ); |
| 38 | enumRule.name("enum" ); |
| 39 | declarationRule.name("declaration" ); |
| 40 | } |
| 41 | |
| 42 | karma::rule<Iterator, std::string()> ; |
| 43 | karma::rule<Iterator, Enum()> ; |
| 44 | karma::rule<Iterator, VariantType()> ; |
| 45 | }; |
| 46 | |
| 47 | template <typename OutputIterator> |
| 48 | bool (OutputIterator& sink, VariantType& data) |
| 49 | { |
| 50 | SettingsHeaderGenerator<OutputIterator> generator; |
| 51 | return karma::generate(sink, generator, data); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | BOOST_FUSION_ADAPT_STRUCT(generator::Enum, |
| 56 | (std::string, enumName) |
| 57 | (std::vector<std::string>, enumEntries) |
| 58 | ) |
| 59 | |
| 60 | int main() |
| 61 | { |
| 62 | generator::VariantType variant = "bla" ; |
| 63 | std::string generated; |
| 64 | std::back_insert_iterator<std::string> sink(generated); |
| 65 | BOOST_TEST(generator::generate_header(sink, variant)); |
| 66 | BOOST_TEST(generated == "class bla;\n" ); |
| 67 | |
| 68 | return boost::report_errors(); |
| 69 | } |
| 70 | |
| 71 | |