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