eol.hpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Hartmut Kaiser
  3. Copyright (c) 2001-2011 Joel de Guzman
  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. #if !defined(BOOST_SPIRIT_EOL_APRIL_18_2008_0751PM)
  8. #define BOOST_SPIRIT_EOL_APRIL_18_2008_0751PM
  9. #if defined(_MSC_VER)
  10. #pragma once
  11. #endif
  12. #include <boost/mpl/bool.hpp>
  13. #include <boost/spirit/home/qi/domain.hpp>
  14. #include <boost/spirit/home/qi/parser.hpp>
  15. #include <boost/spirit/home/qi/meta_compiler.hpp>
  16. #include <boost/spirit/home/qi/skip_over.hpp>
  17. #include <boost/spirit/home/support/common_terminals.hpp>
  18. namespace boost { namespace spirit
  19. {
  20. ///////////////////////////////////////////////////////////////////////////
  21. // Enablers
  22. ///////////////////////////////////////////////////////////////////////////
  23. template <>
  24. struct use_terminal<qi::domain, tag::eol> // enables eol
  25. : mpl::true_ {};
  26. }}
  27. namespace boost { namespace spirit { namespace qi
  28. {
  29. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  30. using spirit::eol;
  31. #endif
  32. using spirit::eol_type;
  33. struct eol_parser : primitive_parser<eol_parser>
  34. {
  35. template <typename Context, typename Iterator>
  36. struct attribute
  37. {
  38. typedef unused_type type;
  39. };
  40. template <typename Iterator, typename Context
  41. , typename Skipper, typename Attribute>
  42. bool parse(Iterator& first, Iterator const& last
  43. , Context& /*context*/, Skipper const& skipper
  44. , Attribute& /*attr*/) const
  45. {
  46. qi::skip_over(first, last, skipper);
  47. Iterator it = first;
  48. bool matched = false;
  49. if (it != last && *it == '\r') // CR
  50. {
  51. matched = true;
  52. ++it;
  53. }
  54. if (it != last && *it == '\n') // LF
  55. {
  56. matched = true;
  57. ++it;
  58. }
  59. if (!matched)
  60. return false;
  61. first = it;
  62. return true;
  63. }
  64. template <typename Context>
  65. info what(Context& /*context*/) const
  66. {
  67. return info("eol");
  68. }
  69. };
  70. ///////////////////////////////////////////////////////////////////////////
  71. // Parser generators: make_xxx function (objects)
  72. ///////////////////////////////////////////////////////////////////////////
  73. template <typename Modifiers>
  74. struct make_primitive<tag::eol, Modifiers>
  75. {
  76. typedef eol_parser result_type;
  77. result_type operator()(unused_type, unused_type) const
  78. {
  79. return result_type();
  80. }
  81. };
  82. }}}
  83. #endif