tree_to_xml.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*=============================================================================
  2. Copyright (c) 2001-2007 Hartmut Kaiser
  3. Copyright (c) 2001-2003 Daniel Nuffer
  4. http://spirit.sourceforge.net/
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #if !defined(TREE_TO_XML_HPP)
  9. #define TREE_TO_XML_HPP
  10. #include <boost/spirit/home/classic/namespace.hpp>
  11. namespace boost { namespace spirit {
  12. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  13. namespace impl {
  14. template <typename CharT> struct default_string;
  15. }
  16. ///////////////////////////////////////////////////////////////////////////////
  17. //
  18. // Dump a parse tree as a xml stream
  19. //
  20. // The functions 'tree_to_xml' can be used to output a parse tree as a xml
  21. // stream into the given ostream. The parameters have the following
  22. // meaning:
  23. //
  24. // mandatory parameters:
  25. // ostrm The output stream used for streaming the parse tree.
  26. // tree The parse tree to output.
  27. //
  28. // optional parameters:
  29. // input_line The input line from which the parse tree was
  30. // generated (if given, it is used to output a comment
  31. // containing this line).
  32. // id_to_name A map, which is used for converting the rule id's contained
  33. // in the parse tree to readable strings. Here a auxiliary
  34. // associative container can be used, which maps a rule_id to
  35. // a std::string (i.e. a std::map<rule_id, std::string>).
  36. // get_token_id
  37. // A function or functor, which takes an instance of a token
  38. // and which should return a token id (i.e. something like
  39. // 'int f(char const c)').
  40. // get_token_value
  41. // A function or functor, which takes an instance of a token
  42. // and which should return a readable representation of this
  43. // token (i.e. something like 'std::string f(char const c)').
  44. //
  45. // The structure of the generated xml stream conforms to the DTD given in the
  46. // file 'parsetree.dtd'. This file is located in the spirit/tree directory.
  47. //
  48. ///////////////////////////////////////////////////////////////////////////////
  49. template <
  50. typename CharT, typename TreeNodeT, typename AssocContainerT,
  51. typename GetIdT, typename GetValueT
  52. >
  53. inline void
  54. basic_tree_to_xml (std::basic_ostream<CharT> &ostrm, TreeNodeT const &tree,
  55. std::basic_string<CharT> const &input_line,
  56. AssocContainerT const& id_to_name, GetIdT const &get_token_id,
  57. GetValueT const &get_token_value);
  58. template <typename CharT, typename TreeNodeT, typename AssocContainerT>
  59. inline void
  60. basic_tree_to_xml (std::basic_ostream<CharT> &ostrm, TreeNodeT const &tree,
  61. std::basic_string<CharT> const &input_line,
  62. AssocContainerT const& id_to_name);
  63. template <typename CharT, typename TreeNodeT>
  64. inline void
  65. basic_tree_to_xml (std::basic_ostream<CharT> &ostrm, TreeNodeT const &tree,
  66. std::basic_string<CharT> const &input_line =
  67. impl::default_string<CharT>::get());
  68. ///////////////////////////////////////////////////////////////////////////
  69. template <
  70. typename TreeNodeT, typename AssocContainerT,
  71. typename GetIdT, typename GetValueT
  72. >
  73. inline void
  74. tree_to_xml (std::ostream &ostrm, TreeNodeT const &tree,
  75. std::string const &input_line, AssocContainerT const& id_to_name,
  76. GetIdT const &get_token_id, GetValueT const &get_token_value)
  77. {
  78. basic_tree_to_xml<char>(ostrm, tree, input_line, id_to_name,
  79. get_token_id, get_token_value);
  80. }
  81. template <typename TreeNodeT, typename AssocContainerT>
  82. inline void
  83. tree_to_xml (std::ostream &ostrm, TreeNodeT const &tree,
  84. std::string const &input_line, AssocContainerT const& id_to_name)
  85. {
  86. basic_tree_to_xml<char>(ostrm, tree, input_line, id_to_name);
  87. }
  88. template <typename TreeNodeT>
  89. inline void
  90. tree_to_xml (std::ostream &ostrm, TreeNodeT const &tree,
  91. std::string const &input_line = "")
  92. {
  93. basic_tree_to_xml<char>(ostrm, tree, input_line);
  94. }
  95. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  96. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  97. #include <boost/spirit/home/classic/tree/impl/tree_to_xml.ipp>
  98. #endif // !defined(TREE_TO_XML_HPP)