display_attribute_type.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. // This example implements a simple utility allowing to print the attribute
  6. // type as it is exposed by an arbitrary Qi parser expression. Just insert
  7. // your expression below, compile and run this example to see what Qi is
  8. // seeing!
  9. #if !defined (DISPLAY_ATTRIBUTE_OF_PARSER_JAN_2010_30_0722PM)
  10. #define DISPLAY_ATTRIBUTE_OF_PARSER_JAN_2010_30_0722PM
  11. #include <iostream>
  12. #include <boost/spirit/include/qi.hpp>
  13. namespace tools
  14. {
  15. namespace spirit = boost::spirit;
  16. template <typename Expr, typename Iterator = spirit::unused_type>
  17. struct attribute_of_parser
  18. {
  19. typedef typename spirit::result_of::compile<
  20. spirit::qi::domain, Expr
  21. >::type parser_expression_type;
  22. typedef typename spirit::traits::attribute_of<
  23. parser_expression_type, spirit::unused_type, Iterator
  24. >::type type;
  25. };
  26. template <typename T>
  27. void display_attribute_of_parser(T const &)
  28. {
  29. // Report invalid expression error as early as possible.
  30. // If you got an error_invalid_expression error message here,
  31. // then the expression (expr) is not a valid spirit qi expression.
  32. BOOST_SPIRIT_ASSERT_MATCH(spirit::qi::domain, T);
  33. typedef typename attribute_of_parser<T>::type type;
  34. std::cout << typeid(type).name() << std::endl;
  35. }
  36. template <typename T>
  37. void display_attribute_of_parser(std::ostream& os, T const &)
  38. {
  39. // Report invalid expression error as early as possible.
  40. // If you got an error_invalid_expression error message here,
  41. // then the expression (expr) is not a valid spirit qi expression.
  42. BOOST_SPIRIT_ASSERT_MATCH(spirit::qi::domain, T);
  43. typedef typename attribute_of_parser<T>::type type;
  44. os << typeid(type).name() << std::endl;
  45. }
  46. }
  47. #endif