Created
October 8, 2017 15:13
-
-
Save weltling/a350ab7ae710232bea3799e33189191a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Based on lexertl/iterator.hpp | |
#ifndef PARLE_LEXER_ITERATOR_HPP | |
#define PARLE_LEXER_ITERATOR_HPP | |
#include <iterator> | |
#include "lexertl/lookup.hpp" | |
#undef lookup | |
namespace parle | |
{ | |
namespace lexer | |
{ | |
template<typename iter, typename sm_type, typename results> | |
class iterator | |
{ | |
public: | |
using value_type = results; | |
using difference_type = ptrdiff_t; | |
using pointer = const value_type *; | |
using reference = const value_type &; | |
using iterator_category = std::forward_iterator_tag; | |
iterator() : | |
_results(iter(), iter()), | |
_sm(nullptr) | |
{ | |
} | |
iterator(const iter &start_, const iter &end_, const sm_type &sm) : | |
_results(start_, end_), | |
_sm(&sm) | |
{ | |
// lookup(); | |
} | |
void advance() | |
{ | |
lookup(); | |
} | |
// Only need this because of warnings with gcc with -Weffc++ | |
iterator(const iterator &rhs_) | |
{ | |
_results = rhs_._results; | |
_sm = rhs_._sm; | |
} | |
// Only need this because of warnings with gcc with -Weffc++ | |
iterator &operator =(const iterator &rhs_) | |
{ | |
if (&rhs_ != this) | |
{ | |
_results = rhs_._results; | |
_sm = rhs_._sm; | |
} | |
return *this; | |
} | |
iterator &operator ++() | |
{ | |
lookup(); | |
return *this; | |
} | |
iterator operator ++(int) | |
{ | |
iterator iter_ = *this; | |
lookup(); | |
return iter_; | |
} | |
const value_type &operator *() const | |
{ | |
return _results; | |
} | |
value_type &operator *() | |
{ | |
return _results; | |
} | |
const value_type *operator ->() const | |
{ | |
return &_results; | |
} | |
bool operator ==(const iterator &rhs_) const | |
{ | |
return _sm == rhs_._sm && (_sm == nullptr ? true : | |
_results == rhs_._results); | |
} | |
bool operator !=(const iterator &rhs_) const | |
{ | |
return !(*this == rhs_); | |
} | |
private: | |
value_type _results; | |
const sm_type *_sm; | |
void lookup() | |
{ | |
lexertl::lookup(*_sm, _results); | |
if (_results.first == _results.eoi) | |
{ | |
_sm = nullptr; | |
} | |
} | |
}; | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment