mini_xml_karma.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*=============================================================================
  2. Copyright (c) 2001-2010 Joel de Guzman
  3. Copyright (c) 2001-2010 Hartmut Kaiser
  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. ///////////////////////////////////////////////////////////////////////////////
  8. //
  9. // A mini XML-like parser, Karma is used to print out the generated AST
  10. //
  11. // [ JDG March 25, 2007 ] spirit2
  12. // [ HK April 02, 2007 ] spirit2
  13. //
  14. ///////////////////////////////////////////////////////////////////////////////
  15. #include <boost/config/warning_disable.hpp>
  16. #include <boost/spirit/include/qi.hpp>
  17. #include <boost/spirit/include/karma.hpp>
  18. #include <boost/spirit/include/phoenix_core.hpp>
  19. #include <boost/spirit/include/phoenix_operator.hpp>
  20. #include <boost/spirit/include/phoenix_fusion.hpp>
  21. #include <boost/spirit/include/phoenix_function.hpp>
  22. #include <boost/spirit/include/phoenix_stl.hpp>
  23. #include <boost/fusion/include/adapt_struct.hpp>
  24. #include <boost/variant/recursive_variant.hpp>
  25. #include <iostream>
  26. #include <fstream>
  27. #include <string>
  28. #include <vector>
  29. using namespace boost::spirit;
  30. using namespace boost::spirit::ascii;
  31. namespace fusion = boost::fusion;
  32. namespace phoenix = boost::phoenix;
  33. using phoenix::at_c;
  34. using phoenix::push_back;
  35. ///////////////////////////////////////////////////////////////////////////////
  36. // Our mini XML tree representation
  37. ///////////////////////////////////////////////////////////////////////////////
  38. struct mini_xml;
  39. typedef
  40. boost::variant<
  41. boost::recursive_wrapper<mini_xml>
  42. , std::string
  43. >
  44. mini_xml_node;
  45. struct mini_xml
  46. {
  47. std::string name; // tag name
  48. std::vector<mini_xml_node> children; // children
  49. };
  50. // We need to tell fusion about our mini_xml struct
  51. // to make it a first-class fusion citizen
  52. BOOST_FUSION_ADAPT_STRUCT(
  53. mini_xml,
  54. (std::string, name)
  55. (std::vector<mini_xml_node>, children)
  56. )
  57. ///////////////////////////////////////////////////////////////////////////////
  58. // Our mini XML grammar definition
  59. ///////////////////////////////////////////////////////////////////////////////
  60. template <typename Iterator>
  61. struct mini_xml_parser :
  62. qi::grammar<Iterator, mini_xml(), space_type>
  63. {
  64. mini_xml_parser() : mini_xml_parser::base_type(xml)
  65. {
  66. text = lexeme[+(char_ - '<') [_val += _1]];
  67. node = (xml | text) [_val = _1];
  68. start_tag =
  69. '<'
  70. >> !lit('/')
  71. >> lexeme[+(char_ - '>') [_val += _1]]
  72. >> '>'
  73. ;
  74. end_tag =
  75. "</"
  76. >> lit(_r1)
  77. >> '>'
  78. ;
  79. xml =
  80. start_tag [at_c<0>(_val) = _1]
  81. >> *node [push_back(at_c<1>(_val), _1)]
  82. >> end_tag(at_c<0>(_val))
  83. ;
  84. }
  85. qi::rule<Iterator, mini_xml(), space_type> xml;
  86. qi::rule<Iterator, mini_xml_node(), space_type> node;
  87. qi::rule<Iterator, std::string(), space_type> text;
  88. qi::rule<Iterator, std::string(), space_type> start_tag;
  89. qi::rule<Iterator, void(std::string), space_type> end_tag;
  90. };
  91. ///////////////////////////////////////////////////////////////////////////////
  92. // A couple of phoenix functions helping to access the elements of the
  93. // generated AST
  94. ///////////////////////////////////////////////////////////////////////////////
  95. template <typename T>
  96. struct get_element
  97. {
  98. template <typename T1>
  99. struct result { typedef T const& type; };
  100. T const& operator()(mini_xml_node const& node) const
  101. {
  102. return boost::get<T>(node);
  103. }
  104. };
  105. phoenix::function<get_element<std::string> > _string;
  106. phoenix::function<get_element<mini_xml> > _xml;
  107. ///////////////////////////////////////////////////////////////////////////////
  108. // The output grammar defining the format of the generated data
  109. ///////////////////////////////////////////////////////////////////////////////
  110. template <typename OutputIterator>
  111. struct mini_xml_generator
  112. : karma::grammar<OutputIterator, mini_xml()>
  113. {
  114. mini_xml_generator() : mini_xml_generator::base_type(xml)
  115. {
  116. node %= string | xml;
  117. xml =
  118. '<' << string[_1 = at_c<0>(_val)] << '>'
  119. << (*node)[_1 = at_c<1>(_val)]
  120. << "</" << string[_1 = at_c<0>(_val)] << '>'
  121. ;
  122. }
  123. karma::rule<OutputIterator, mini_xml()> xml;
  124. karma::rule<OutputIterator, mini_xml_node()> node;
  125. };
  126. ///////////////////////////////////////////////////////////////////////////////
  127. // Main program
  128. ///////////////////////////////////////////////////////////////////////////////
  129. int main(int argc, char **argv)
  130. {
  131. char const* filename;
  132. if (argc > 1)
  133. {
  134. filename = argv[1];
  135. }
  136. else
  137. {
  138. std::cerr << "Error: No input file provided." << std::endl;
  139. return 1;
  140. }
  141. std::ifstream in(filename, std::ios_base::in);
  142. if (!in)
  143. {
  144. std::cerr << "Error: Could not open input file: "
  145. << filename << std::endl;
  146. return 1;
  147. }
  148. std::string storage; // We will read the contents here.
  149. in.unsetf(std::ios::skipws); // No white space skipping!
  150. std::copy(
  151. std::istream_iterator<char>(in),
  152. std::istream_iterator<char>(),
  153. std::back_inserter(storage));
  154. typedef mini_xml_parser<std::string::const_iterator> mini_xml_parser;
  155. mini_xml_parser xmlin; // Our grammar definition
  156. mini_xml ast; // our tree
  157. std::string::const_iterator iter = storage.begin();
  158. std::string::const_iterator end = storage.end();
  159. bool r = qi::phrase_parse(iter, end, xmlin, space, ast);
  160. if (r && iter == end)
  161. {
  162. std::cout << "-------------------------\n";
  163. std::cout << "Parsing succeeded\n";
  164. std::cout << "-------------------------\n";
  165. typedef std::back_insert_iterator<std::string> outiter_type;
  166. typedef mini_xml_generator<outiter_type> mini_xml_generator;
  167. mini_xml_generator xmlout; // Our grammar definition
  168. std::string generated;
  169. outiter_type outit(generated);
  170. bool r = karma::generate(outit, xmlout, ast);
  171. if (r)
  172. std::cout << generated << std::endl;
  173. return 0;
  174. }
  175. else
  176. {
  177. std::string::const_iterator begin = storage.begin();
  178. std::size_t dist = std::distance(begin, iter);
  179. std::string::const_iterator some =
  180. iter + (std::min)(storage.size()-dist, std::size_t(30));
  181. std::string context(iter, some);
  182. std::cout << "-------------------------\n";
  183. std::cout << "Parsing failed\n";
  184. std::cout << "stopped at: \": " << context << "...\"\n";
  185. std::cout << "-------------------------\n";
  186. return 1;
  187. }
  188. }