confix.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (c) 2001-2010 Hartmut Kaiser
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/config/warning_disable.hpp>
  6. #include <boost/detail/lightweight_test.hpp>
  7. #include <boost/spirit/include/karma_auxiliary.hpp>
  8. #include <boost/spirit/include/karma_char.hpp>
  9. #include <boost/spirit/include/karma_string.hpp>
  10. #include <boost/spirit/include/karma_generate.hpp>
  11. #include <boost/spirit/repository/include/karma_confix.hpp>
  12. #include <iostream>
  13. #include "test.hpp"
  14. namespace html
  15. {
  16. namespace spirit = boost::spirit;
  17. namespace repo = boost::spirit::repository;
  18. ///////////////////////////////////////////////////////////////////////////////
  19. // define a HTML tag helper generator
  20. namespace traits
  21. {
  22. template <typename Prefix, typename Suffix = Prefix>
  23. struct confix_spec
  24. : spirit::result_of::terminal<repo::tag::confix(Prefix, Suffix)>
  25. {};
  26. }
  27. template <typename Prefix, typename Suffix>
  28. inline typename traits::confix_spec<Prefix, Suffix>::type
  29. confix_spec(Prefix const& prefix, Suffix const& suffix)
  30. {
  31. return repo::confix(prefix, suffix);
  32. }
  33. ///////////////////////////////////////////////////////////////////////////
  34. template <typename Char, typename Traits, typename Allocator>
  35. inline typename traits::confix_spec<
  36. std::basic_string<Char, Traits, Allocator>
  37. >::type
  38. tag (std::basic_string<Char, Traits, Allocator> const& tagname)
  39. {
  40. typedef std::basic_string<Char, Traits, Allocator> string_type;
  41. return confix_spec(string_type("<") + tagname + ">"
  42. , string_type("</") + tagname + ">");
  43. }
  44. inline traits::confix_spec<std::string>::type
  45. tag (char const* tagname)
  46. {
  47. return tag(std::string(tagname));
  48. }
  49. ///////////////////////////////////////////////////////////////////////////
  50. typedef traits::confix_spec<std::string>::type html_tag_type;
  51. html_tag_type const ol = tag("ol");
  52. }
  53. ///////////////////////////////////////////////////////////////////////////////
  54. int main()
  55. {
  56. using namespace spirit_test;
  57. using namespace boost::spirit;
  58. using namespace boost::spirit::repository;
  59. {
  60. using namespace boost::spirit::ascii;
  61. BOOST_TEST((test("<tag>a</tag>",
  62. confix("<tag>", "</tag>")[char_('a')])));
  63. BOOST_TEST((test("<tag>a</tag>",
  64. confix("<tag>", "</tag>")[char_], 'a')));
  65. BOOST_TEST((test("// some C++ comment\n",
  66. confix(string("//"), eol)[" some C++ comment"])));
  67. BOOST_TEST((test("// some C++ comment\n",
  68. confix(string("//"), eol)[string], " some C++ comment")));
  69. BOOST_TEST((test("<ol>some text</ol>", html::ol["some text"])));
  70. BOOST_TEST((test("<ol>some text</ol>", html::ol[string], "some text")));
  71. }
  72. {
  73. using namespace boost::spirit::standard_wide;
  74. BOOST_TEST((test(L"<tag>a</tag>",
  75. confix(L"<tag>", L"</tag>")[char_(L'a')])));
  76. BOOST_TEST((test(L"// some C++ comment\n",
  77. confix(string(L"//"), eol)[L" some C++ comment"])));
  78. BOOST_TEST((test(L"<ol>some text</ol>", html::ol[L"some text"])));
  79. }
  80. {
  81. using namespace boost::spirit::ascii;
  82. BOOST_TEST((test_delimited("<tag> a </tag> ",
  83. confix("<tag>", "</tag>")[char_('a')], space)));
  84. BOOST_TEST((test_delimited("// some C++ comment \n ",
  85. confix(string("//"), eol)["some C++ comment"], space)));
  86. BOOST_TEST((test_delimited("<ol> some text </ol> ",
  87. html::ol["some text"], space)));
  88. }
  89. {
  90. using namespace boost::spirit::standard_wide;
  91. BOOST_TEST((test_delimited(L"<tag> a </tag> ",
  92. confix(L"<tag>", L"</tag>")[char_(L'a')], space)));
  93. BOOST_TEST((test_delimited(L"// some C++ comment \n ",
  94. confix(string(L"//"), eol)[L"some C++ comment"], space)));
  95. BOOST_TEST((test_delimited(L"<ol> some text </ol> ",
  96. html::ol[L"some text"], space)));
  97. }
  98. return boost::report_errors();
  99. }