regex.ipp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*=============================================================================
  2. Copyright (c) 2002-2003 Hartmut Kaiser
  3. http://spirit.sourceforge.net/
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #ifndef BOOST_SPIRIT_REGEX_IPP
  9. #define BOOST_SPIRIT_REGEX_IPP
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include <boost/spirit/home/classic/core/primitives/impl/primitives.ipp>
  12. ///////////////////////////////////////////////////////////////////////////////
  13. namespace boost { namespace spirit {
  14. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  15. namespace impl {
  16. ///////////////////////////////////////////////////////////////////////////////
  17. //
  18. inline const char* rx_prefix(char) { return "\\A"; }
  19. inline const wchar_t* rx_prefix(wchar_t) { return L"\\A"; }
  20. ///////////////////////////////////////////////////////////////////////////////
  21. //
  22. // rx_parser class
  23. //
  24. ///////////////////////////////////////////////////////////////////////////////
  25. template <typename CharT = char>
  26. class rx_parser : public parser<rx_parser<CharT> > {
  27. public:
  28. typedef std::basic_string<CharT> string_t;
  29. typedef rx_parser<CharT> self_t;
  30. rx_parser(CharT const *first, CharT const *last)
  31. {
  32. rxstr = string_t(rx_prefix(CharT())) + string_t(first, last);
  33. }
  34. rx_parser(CharT const *first)
  35. {
  36. rxstr = string_t(rx_prefix(CharT())) +
  37. string_t(first, impl::get_last(first));
  38. }
  39. template <typename ScannerT>
  40. typename parser_result<self_t, ScannerT>::type
  41. parse(ScannerT const& scan) const
  42. {
  43. boost::match_results<typename ScannerT::iterator_t> what;
  44. boost::regex_search(scan.first, scan.last, what, rxstr,
  45. boost::match_default);
  46. if (!what[0].matched)
  47. return scan.no_match();
  48. scan.first = what[0].second;
  49. return scan.create_match(what[0].length(), nil_t(),
  50. what[0].first, scan.first);
  51. }
  52. private:
  53. #if BOOST_VERSION >= 013300
  54. boost::basic_regex<CharT> rxstr; // regular expression to match
  55. #else
  56. boost::reg_expression<CharT> rxstr; // regular expression to match
  57. #endif
  58. };
  59. } // namespace impl
  60. ///////////////////////////////////////////////////////////////////////////////
  61. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  62. }} // namespace boost::spirit
  63. #endif // BOOST_SPIRIT_REGEX_IPP