functor_parser.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*=============================================================================
  2. Copyright (c) 2002-2003 Joel de Guzman
  3. Copyright (c) 2002-2003 Juan Carlos Arevalo-Baeza
  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. #ifndef BOOST_SPIRIT_FUNCTOR_PARSER_HPP
  9. #define BOOST_SPIRIT_FUNCTOR_PARSER_HPP
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include <boost/spirit/home/classic/namespace.hpp>
  12. #include <boost/spirit/home/classic/core/parser.hpp>
  13. ///////////////////////////////////////////////////////////////////////////////
  14. namespace boost { namespace spirit {
  15. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  16. ///////////////////////////////////////////////////////////////////////////
  17. //
  18. // functor_parser class
  19. //
  20. // Once a functor parser has been defined, you can build a real
  21. // parser from it by passing it to this class as the template
  22. // parameter.
  23. //
  24. ///////////////////////////////////////////////////////////////////////////
  25. template < class FunctorT >
  26. struct functor_parser : public parser<functor_parser<FunctorT> >
  27. {
  28. FunctorT functor;
  29. functor_parser(): functor() {}
  30. functor_parser(FunctorT const& functor_): functor(functor_) {}
  31. typedef typename FunctorT::result_t functor_result_t;
  32. typedef functor_parser<FunctorT> self_t;
  33. template <typename ScannerT>
  34. struct result
  35. {
  36. typedef typename match_result<ScannerT, functor_result_t>::type
  37. type;
  38. };
  39. template <typename ScannerT>
  40. typename parser_result<self_t, ScannerT>::type
  41. parse(ScannerT const& scan) const
  42. {
  43. typedef typename ScannerT::iterator_t iterator_t;
  44. iterator_t const s(scan.first);
  45. functor_result_t functor_result;
  46. std::ptrdiff_t len = functor(scan, functor_result);
  47. if (len < 0)
  48. return scan.no_match();
  49. else
  50. return scan.create_match(std::size_t(len), functor_result, s, scan.first);
  51. }
  52. };
  53. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  54. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  55. #endif