Fix typo in grammar.
Add lexertl.
This commit is contained in:
73
inc/lexertl/partition/charset.hpp
Normal file
73
inc/lexertl/partition/charset.hpp
Normal file
@@ -0,0 +1,73 @@
|
||||
// charset.hpp
|
||||
// Copyright (c) 2005-2012 Ben Hanson (http://www.benhanson.net/)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file licence_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef LEXERTL_CHARSET_HPP
|
||||
#define LEXERTL_CHARSET_HPP
|
||||
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <set>
|
||||
#include "../size_t.hpp"
|
||||
#include "../string_token.hpp"
|
||||
|
||||
namespace lexertl
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
template<typename char_type, typename id_type>
|
||||
struct basic_charset
|
||||
{
|
||||
typedef basic_string_token<char_type> token;
|
||||
typedef std::set<id_type> index_set;
|
||||
|
||||
token _token;
|
||||
index_set _index_set;
|
||||
|
||||
basic_charset () :
|
||||
_token (),
|
||||
_index_set ()
|
||||
{
|
||||
}
|
||||
|
||||
basic_charset (const token &token_, const std::size_t index_) :
|
||||
_token (token_),
|
||||
_index_set ()
|
||||
{
|
||||
_index_set.insert (index_);
|
||||
}
|
||||
|
||||
bool empty () const
|
||||
{
|
||||
return _token.empty () && _index_set.empty ();
|
||||
}
|
||||
|
||||
void intersect (basic_charset &rhs_, basic_charset &overlap_)
|
||||
{
|
||||
_token.intersect (rhs_._token, overlap_._token);
|
||||
|
||||
if (!overlap_._token.empty ())
|
||||
{
|
||||
std::merge (_index_set.begin (), _index_set.end (),
|
||||
rhs_._index_set.begin (), rhs_._index_set.end (),
|
||||
std::inserter (overlap_._index_set,
|
||||
overlap_._index_set.end ()));
|
||||
|
||||
if (_token.empty ())
|
||||
{
|
||||
_index_set.clear ();
|
||||
}
|
||||
|
||||
if (rhs_._token.empty ())
|
||||
{
|
||||
rhs_._index_set.clear ();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
134
inc/lexertl/partition/equivset.hpp
Normal file
134
inc/lexertl/partition/equivset.hpp
Normal file
@@ -0,0 +1,134 @@
|
||||
// equivset.hpp
|
||||
// Copyright (c) 2005-2012 Ben Hanson (http://www.benhanson.net/)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file licence_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
#ifndef LEXERTL_EQUIVSET_HPP
|
||||
#define LEXERTL_EQUIVSET_HPP
|
||||
|
||||
#include <algorithm>
|
||||
#include "../parser/tree/node.hpp"
|
||||
#include <set>
|
||||
|
||||
namespace lexertl
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
template<typename id_type>
|
||||
struct basic_equivset
|
||||
{
|
||||
typedef std::set<id_type> index_set;
|
||||
typedef std::vector<id_type> index_vector;
|
||||
// Not owner of nodes:
|
||||
typedef basic_node<id_type> node;
|
||||
typedef std::vector<node *> node_vector;
|
||||
|
||||
index_vector _index_vector;
|
||||
id_type _id;
|
||||
bool _greedy;
|
||||
node_vector _followpos;
|
||||
|
||||
basic_equivset () :
|
||||
_index_vector (),
|
||||
_id (0),
|
||||
_greedy (true),
|
||||
_followpos ()
|
||||
{
|
||||
}
|
||||
|
||||
basic_equivset (const index_set &index_set_, const id_type id_,
|
||||
const bool greedy_, const node_vector &followpos_) :
|
||||
_index_vector (index_set_.begin (), index_set_.end ()),
|
||||
_id (id_),
|
||||
_greedy (greedy_),
|
||||
_followpos (followpos_)
|
||||
{
|
||||
}
|
||||
|
||||
bool empty () const
|
||||
{
|
||||
return _index_vector.empty () && _followpos.empty ();
|
||||
}
|
||||
|
||||
void intersect (basic_equivset &rhs_, basic_equivset &overlap_)
|
||||
{
|
||||
intersect_indexes (rhs_._index_vector, overlap_._index_vector);
|
||||
|
||||
if (!overlap_._index_vector.empty ())
|
||||
{
|
||||
// Note that the LHS takes priority in order to
|
||||
// respect rule ordering priority in the lex spec.
|
||||
overlap_._id = _id;
|
||||
overlap_._greedy = _greedy;
|
||||
overlap_._followpos = _followpos;
|
||||
|
||||
typename node_vector::const_iterator overlap_begin_ =
|
||||
overlap_._followpos.begin ();
|
||||
typename node_vector::const_iterator overlap_end_ =
|
||||
overlap_._followpos.end ();
|
||||
typename node_vector::const_iterator rhs_iter_ =
|
||||
rhs_._followpos.begin ();
|
||||
typename node_vector::const_iterator rhs_end_ =
|
||||
rhs_._followpos.end ();
|
||||
|
||||
for (; rhs_iter_ != rhs_end_; ++rhs_iter_)
|
||||
{
|
||||
node *node_ = *rhs_iter_;
|
||||
|
||||
if (std::find (overlap_begin_, overlap_end_, node_) ==
|
||||
overlap_end_)
|
||||
{
|
||||
overlap_._followpos.push_back (node_);
|
||||
overlap_begin_ = overlap_._followpos.begin ();
|
||||
overlap_end_ = overlap_._followpos.end ();
|
||||
}
|
||||
}
|
||||
|
||||
if (_index_vector.empty ())
|
||||
{
|
||||
_followpos.clear ();
|
||||
}
|
||||
|
||||
if (rhs_._index_vector.empty ())
|
||||
{
|
||||
rhs_._followpos.clear ();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
void intersect_indexes (index_vector &rhs_, index_vector &overlap_)
|
||||
{
|
||||
typename index_vector::iterator iter_ = _index_vector.begin ();
|
||||
typename index_vector::iterator end_ = _index_vector.end ();
|
||||
typename index_vector::iterator rhs_iter_ = rhs_.begin ();
|
||||
typename index_vector::iterator rhs_end_ = rhs_.end ();
|
||||
|
||||
while (iter_ != end_ && rhs_iter_ != rhs_end_)
|
||||
{
|
||||
const id_type index_ = *iter_;
|
||||
const id_type rhs_index_ = *rhs_iter_;
|
||||
|
||||
if (index_ < rhs_index_)
|
||||
{
|
||||
++iter_;
|
||||
}
|
||||
else if (index_ > rhs_index_)
|
||||
{
|
||||
++rhs_iter_;
|
||||
}
|
||||
else
|
||||
{
|
||||
overlap_.push_back (index_);
|
||||
iter_ = _index_vector.erase (iter_);
|
||||
end_ = _index_vector.end ();
|
||||
rhs_iter_ = rhs_.erase (rhs_iter_);
|
||||
rhs_end_ = rhs_.end ();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user