lex_input_policy.hpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright (c) 2001 Daniel C. Nuffer
  2. // Copyright (c) 2001-2011 Hartmut Kaiser
  3. //
  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. #if !defined(BOOST_SPIRIT_ITERATOR_LEX_INPUT_POLICY_MAR_16_2007_1205PM)
  7. #define BOOST_SPIRIT_ITERATOR_LEX_INPUT_POLICY_MAR_16_2007_1205PM
  8. #include <boost/spirit/home/support/iterators/multi_pass_fwd.hpp>
  9. #include <boost/spirit/home/support/iterators/detail/multi_pass.hpp>
  10. namespace boost { namespace spirit { namespace iterator_policies
  11. {
  12. ///////////////////////////////////////////////////////////////////////////////
  13. // class lex_input
  14. // Implementation of the InputPolicy used by multi_pass
  15. //
  16. // The lex_input class gets tokens (integers) from yylex()
  17. ///////////////////////////////////////////////////////////////////////////
  18. struct lex_input
  19. {
  20. typedef int value_type;
  21. ///////////////////////////////////////////////////////////////////////
  22. template <typename T>
  23. class unique : public detail::default_input_policy
  24. {
  25. public:
  26. typedef std::ptrdiff_t difference_type;
  27. typedef std::ptrdiff_t distance_type;
  28. typedef int* pointer;
  29. typedef int& reference;
  30. protected:
  31. unique() {}
  32. explicit unique(T) {}
  33. public:
  34. template <typename MultiPass>
  35. static typename MultiPass::reference get_input(MultiPass& mp)
  36. {
  37. value_type& curtok = mp.shared()->curtok;
  38. if (-1 == curtok)
  39. {
  40. extern int yylex();
  41. curtok = yylex();
  42. }
  43. return curtok;
  44. }
  45. template <typename MultiPass>
  46. static void advance_input(MultiPass& mp)
  47. {
  48. extern int yylex();
  49. mp.shared()->curtok = yylex();
  50. }
  51. // test, whether we reached the end of the underlying stream
  52. template <typename MultiPass>
  53. static bool input_at_eof(MultiPass const& mp)
  54. {
  55. return mp.shared()->curtok == 0;
  56. }
  57. template <typename MultiPass>
  58. static bool input_is_valid(MultiPass const&, value_type const& t)
  59. {
  60. return -1 != t;
  61. }
  62. };
  63. ///////////////////////////////////////////////////////////////////////
  64. template <typename T>
  65. struct shared
  66. {
  67. explicit shared(T) : curtok(-1) {}
  68. value_type curtok;
  69. };
  70. };
  71. }}}
  72. #endif