stlab.adobe.com Adobe Systems Incorporated
lex_shared_fwd.hpp
Go to the documentation of this file.
1 /*
2  Copyright 2005-2007 Adobe Systems Incorporated
3  Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
4  or a copy at http://stlab.adobe.com/licenses.html)
5 */
6 
7 /*************************************************************************************************/
8 
9 #ifndef ADOBE_LEX_SHARED_FWD_HPP
10 #define ADOBE_LEX_SHARED_FWD_HPP
11 
12 /*************************************************************************************************/
13 
14 #include <adobe/config.hpp>
15 
16 #include <functional>
17 #include <iterator>
18 #include <iterator>
19 #include <utility>
20 
21 #include <boost/operators.hpp>
22 #include <boost/static_assert.hpp>
23 
24 #include <adobe/algorithm/copy.hpp>
27 #include <adobe/algorithm/sort.hpp>
28 #include <adobe/name.hpp>
29 #include <adobe/any_regular.hpp>
30 #include <adobe/utility.hpp>
31 
32 #ifndef NDEBUG
33  #include <iostream>
34 #endif
35 
36 /*************************************************************************************************/
37 
38 namespace adobe {
39 
40 /*************************************************************************************************/
41 
42 typedef const unsigned char* uchar_ptr_t;
43 
44 /*************************************************************************************************/
45 
63 template < typename I1, // models InputIterator
64  typename I2, // models InputIterator
65  typename BP> // models BinaryPredicate
66 inline bool bounded_equal(I1 first1, I1 last1, I2 first2, I2 last2, BP pred)
67 {
68  for (; first1 != last1 && first2 != last2; ++first1, ++first2)
69  if (!pred(*first1, *first2)) return false;
70 
71  return first1 == last1 && first2 == last2;
72 }
73 
74 /*************************************************************************************************/
75 
92 template < typename I1, // models InputIterator
93  typename I2> // models InputIterator
94 inline bool bounded_equal(I1 first1, I1 last1, I2 first2, I2 last2)
95 {
96  typedef typename std::iterator_traits<I1>::value_type value_type;
97 
98  return bounded_equal(first1, last1, first2, last2, std::equal_to<value_type>());
99 }
100 
101 /*************************************************************************************************/
102 
117 template < typename R1, // models InputRange
118  typename R2> // models InputRange
119 inline bool bounded_equal(R1& range1, R2& range2)
120 {
121  return bounded_equal( boost::begin(range1), boost::end(range1),
122  boost::begin(range2), boost::end(range2));
123 }
124 
125 /*************************************************************************************************/
126 
145 typedef std::pair<uchar_ptr_t, uchar_ptr_t> token_range_t;
146 
147 /*************************************************************************************************/
148 
164 inline bool token_range_equal(const token_range_t& x, const token_range_t& y)
165 {
166  return boost::size(x) == boost::size(y) && adobe::bounded_equal(x, y);
167 }
168 
169 /*************************************************************************************************/
170 
194 inline bool token_range_less(const token_range_t& x, const token_range_t& y)
195 {
196  std::size_t sizex(boost::size(x));
197  std::size_t sizey(boost::size(y));
198 
199  if (sizex < sizey) return true;
200  else if (sizey < sizex) return false;
201 
202  std::pair<uchar_ptr_t, uchar_ptr_t> diff(adobe::mismatch(x, boost::begin(y)));
203 
204  if (diff.first == boost::end(x)) return false;
205 
206  return *diff.first < *diff.second;
207 }
208 
209 /*************************************************************************************************/
210 
211 #ifndef NDEBUG
212 
223 inline std::ostream& operator << (std::ostream& s, const token_range_t& x)
224 {
225  adobe::copy(x, std::ostream_iterator<char>(s));
226 
227  return s;
228 }
229 #endif
230 
231 /*************************************************************************************************/
232 
244 template <typename T>
246 {
247  BOOST_STATIC_ASSERT(sizeof(T) == sizeof(unsigned char));
248 
249  T* end(begin);
250 
251  while (*end != 0)
252  ++end;
253 
254  return token_range_t(reinterpret_cast<uchar_ptr_t>(begin), reinterpret_cast<uchar_ptr_t>(end));
255 }
256 
257 /*************************************************************************************************/
258 
259 template <typename E> // E models Enumeration
261 {
263  { }
264 
265  explicit lex_token_t(E enumeration, uchar_ptr_t first = 0, uchar_ptr_t last = 0) :
266  enum_m(enumeration), range_m(first, last)
267  { }
268 
271 };
272 
273 /*************************************************************************************************/
274 
276 
277 /*************************************************************************************************/
278 
279 extern aggregate_name_t eof_k; // EOF token name
280 
281 /*************************************************************************************************/
282 
283 template <typename E> inline E eof_token(); // specialize
284 
285 /*************************************************************************************************/
286 
287 } // namespace adobe
288 
289 /*************************************************************************************************/
290 
291 #endif
292 
293 /*************************************************************************************************/
bool token_range_less(const token_range_t &x, const token_range_t &y)
E eof_token()
lex_token_t(E enumeration, uchar_ptr_t first=0, uchar_ptr_t last=0)
token_range_t static_token_range(T *begin)
std::pair< uchar_ptr_t, uchar_ptr_t > token_range_t
A range of pointers denoting a token within a character stream.
token_range_t range_m
BOOST_STATIC_ASSERT(sizeof(closed_hash_set< int >)==sizeof(void *))
bool token_range_equal(const token_range_t &x, const token_range_t &y)
OutputIterator copy(const InputRange &range, OutputIterator result)
copy implementation
Definition: copy.hpp:43
std::pair< I1, I2 > mismatch(I1 first1, I1 last1, I2 first2, I2 last2)
Definition: mismatch.hpp:60
pair< name_t, any_regular_t > stream_lex_token_t
aggregate_name_t eof_k
bool bounded_equal(I1 first1, I1 last1, I2 first2, I2 last2, BP pred)
boost::range_size< Selection >::type size(const Selection &x)
const unsigned char * uchar_ptr_t
std::ostream & operator<<(std::ostream &s, const extents_t &x)

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google