Add commit 43aab01 of BenHanson/lexertl14 from github

This commit is contained in:
Iain Benson
2018-11-16 22:34:49 +00:00
committed by Luis Ángel San Martín
parent c4f792bd40
commit d3de52ca82
37 changed files with 12723 additions and 1 deletions

View File

@ -0,0 +1,111 @@
// end_node.hpp
// Copyright (c) 2005-2018 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_END_NODE_HPP
#define LEXERTL_END_NODE_HPP
#include "node.hpp"
namespace lexertl
{
namespace detail
{
template<typename id_type>
class basic_end_node : public basic_node<id_type>
{
public:
using node = basic_node<id_type>;
using bool_stack = typename node::bool_stack;
using const_node_stack = typename node::const_node_stack;
using node_ptr_vector = typename node::node_ptr_vector;
using node_stack = typename node::node_stack;
using node_type = typename node::node_type;
using node_vector = typename node::node_vector;
basic_end_node(const id_type id_, const id_type user_id_,
const id_type next_dfa_, const id_type push_dfa_,
const bool pop_dfa_) :
node(false),
_id(id_),
_user_id(user_id_),
_next_dfa(next_dfa_),
_push_dfa(push_dfa_),
_pop_dfa(pop_dfa_),
_followpos()
{
node::_firstpos.push_back(this);
node::_lastpos.push_back(this);
}
virtual ~basic_end_node() override
{
}
virtual node_type what_type() const override
{
return node::END;
}
virtual bool traverse(const_node_stack &/*node_stack_*/,
bool_stack &/*perform_op_stack_*/) const override
{
return false;
}
virtual const node_vector &followpos() const override
{
// _followpos is always empty..!
return _followpos;
}
virtual bool end_state() const override
{
return true;
}
virtual id_type id() const override
{
return _id;
}
virtual id_type user_id() const override
{
return _user_id;
}
virtual id_type next_dfa() const override
{
return _next_dfa;
}
virtual id_type push_dfa() const override
{
return _push_dfa;
}
virtual bool pop_dfa() const override
{
return _pop_dfa;
}
private:
id_type _id;
id_type _user_id;
id_type _next_dfa;
id_type _push_dfa;
bool _pop_dfa;
node_vector _followpos;
virtual void copy_node(node_ptr_vector &/*node_ptr_vector_*/,
node_stack &/*new_node_stack_*/, bool_stack &/*perform_op_stack_*/,
bool &/*down_*/) const override
{
// Nothing to do, as end_nodes are not copied.
}
};
}
}
#endif

View File

@ -0,0 +1,96 @@
// iteration_node.hpp
// Copyright (c) 2005-2018 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_ITERATION_NODE_HPP
#define LEXERTL_ITERATION_NODE_HPP
#include "node.hpp"
namespace lexertl
{
namespace detail
{
template<typename id_type>
class basic_iteration_node : public basic_node<id_type>
{
public:
using node = basic_node<id_type>;
using bool_stack = typename node::bool_stack;
using const_node_stack = typename node::const_node_stack;
using node_ptr_vector = typename node::node_ptr_vector;
using node_stack = typename node::node_stack;
using node_type = typename node::node_type;
using node_vector = typename node::node_vector;
basic_iteration_node(observer_ptr<node> next_, const bool greedy_) :
node(true),
_next(next_),
_greedy(greedy_)
{
_next->append_firstpos(node::_firstpos);
_next->append_lastpos(node::_lastpos);
for (observer_ptr<node> node_ : node::_lastpos)
{
node_->append_followpos(node::_firstpos);
}
for (observer_ptr<node> node_ : node::_firstpos)
{
node_->greedy(greedy_);
}
}
virtual ~basic_iteration_node() override
{
}
virtual node_type what_type() const override
{
return node::ITERATION;
}
virtual bool traverse(const_node_stack &node_stack_,
bool_stack &perform_op_stack_) const override
{
perform_op_stack_.push(true);
node_stack_.push(_next);
return true;
}
private:
observer_ptr<node> _next;
bool _greedy;
virtual void copy_node(node_ptr_vector &node_ptr_vector_,
node_stack &new_node_stack_, bool_stack &perform_op_stack_,
bool &down_) const override
{
if (perform_op_stack_.top())
{
observer_ptr<node> ptr_ = new_node_stack_.top();
node_ptr_vector_.emplace_back
(std::make_unique<basic_iteration_node>(ptr_, _greedy));
new_node_stack_.top() = node_ptr_vector_.back().get();
}
else
{
down_ = true;
}
perform_op_stack_.pop();
}
// No copy construction.
basic_iteration_node(const basic_iteration_node &) = delete;
// No assignment.
const basic_iteration_node &operator =
(const basic_iteration_node &) = delete;
};
}
}
#endif

View File

@ -0,0 +1,110 @@
// leaf_node.hpp
// Copyright (c) 2005-2018 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_LEAF_NODE_HPP
#define LEXERTL_LEAF_NODE_HPP
#include "../../enums.hpp" // null_token
#include "node.hpp"
namespace lexertl
{
namespace detail
{
template<typename id_type>
class basic_leaf_node : public basic_node<id_type>
{
public:
using node = basic_node<id_type>;
using bool_stack = typename node::bool_stack;
using const_node_stack = typename node::const_node_stack;
using node_ptr_vector = typename node::node_ptr_vector;
using node_stack = typename node::node_stack;
using node_type = typename node::node_type;
using node_vector = typename node::node_vector;
basic_leaf_node(const id_type token_, const bool greedy_) :
node(token_ == node::null_token()),
_token(token_),
_set_greedy(!greedy_),
_greedy(greedy_),
_followpos()
{
if (!node::_nullable)
{
node::_firstpos.push_back(this);
node::_lastpos.push_back(this);
}
}
virtual ~basic_leaf_node() override
{
}
virtual void append_followpos(const node_vector &followpos_) override
{
_followpos.insert(_followpos.end(),
followpos_.begin(), followpos_.end());
}
virtual node_type what_type() const override
{
return node::LEAF;
}
virtual bool traverse(const_node_stack &/*node_stack_*/,
bool_stack &/*perform_op_stack_*/) const override
{
return false;
}
virtual id_type token() const override
{
return _token;
}
virtual void greedy(const bool greedy_) override
{
if (!_set_greedy)
{
_greedy = greedy_;
_set_greedy = true;
}
}
virtual bool greedy() const override
{
return _greedy;
}
virtual const node_vector &followpos() const override
{
return _followpos;
}
virtual node_vector &followpos() override
{
return _followpos;
}
private:
id_type _token;
bool _set_greedy;
bool _greedy;
node_vector _followpos;
virtual void copy_node(node_ptr_vector &node_ptr_vector_,
node_stack &new_node_stack_, bool_stack &/*perform_op_stack_*/,
bool &/*down_*/) const override
{
node_ptr_vector_.emplace_back(std::make_unique<basic_leaf_node>
(_token, _greedy));
new_node_stack_.push(node_ptr_vector_.back().get());
}
};
}
}
#endif

View File

@ -0,0 +1,242 @@
// node.hpp
// Copyright (c) 2005-2018 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_NODE_HPP
#define LEXERTL_NODE_HPP
#include <assert.h>
#include <memory>
#include "../../observer_ptr.hpp"
#include "../../runtime_error.hpp"
#include <stack>
#include <vector>
namespace lexertl
{
namespace detail
{
template<typename id_type>
class basic_node
{
public:
enum node_type {LEAF, SEQUENCE, SELECTION, ITERATION, END};
using bool_stack = std::stack<bool>;
using node_stack = std::stack<observer_ptr<basic_node>>;
using const_node_stack = std::stack<observer_ptr<const basic_node>>;
using node_vector = std::vector<observer_ptr<basic_node>>;
using node_ptr_vector = std::vector<std::unique_ptr<basic_node>>;
basic_node() :
_nullable(false),
_firstpos(),
_lastpos()
{
}
basic_node(const bool nullable_) :
_nullable(nullable_),
_firstpos(),
_lastpos()
{
}
virtual ~basic_node()
{
}
static id_type null_token()
{
return static_cast<id_type>(~0);
}
bool nullable() const
{
return _nullable;
}
void append_firstpos(node_vector &firstpos_) const
{
firstpos_.insert(firstpos_.end(),
_firstpos.begin(), _firstpos.end());
}
void append_lastpos(node_vector &lastpos_) const
{
lastpos_.insert(lastpos_.end(),
_lastpos.begin(), _lastpos.end());
}
virtual void append_followpos(const node_vector &/*followpos_*/)
{
throw runtime_error("Internal error node::append_followpos().");
}
observer_ptr<basic_node> copy(node_ptr_vector &node_ptr_vector_) const
{
observer_ptr<basic_node> new_root_ = nullptr;
const_node_stack node_stack_;
bool_stack perform_op_stack_;
bool down_ = true;
node_stack new_node_stack_;
node_stack_.push(this);
while (!node_stack_.empty())
{
while (down_)
{
down_ = node_stack_.top()->traverse(node_stack_,
perform_op_stack_);
}
while (!down_ && !node_stack_.empty())
{
observer_ptr<const basic_node> top_ = node_stack_.top();
top_->copy_node(node_ptr_vector_, new_node_stack_,
perform_op_stack_, down_);
if (!down_) node_stack_.pop();
}
}
assert(new_node_stack_.size() == 1);
new_root_ = new_node_stack_.top();
new_node_stack_.pop();
return new_root_;
}
virtual node_type what_type() const = 0;
virtual bool traverse(const_node_stack &node_stack_,
bool_stack &perform_op_stack_) const = 0;
node_vector &firstpos()
{
return _firstpos;
}
const node_vector &firstpos() const
{
return _firstpos;
}
// _lastpos modified externally, so not const &
node_vector &lastpos()
{
return _lastpos;
}
virtual bool end_state() const
{
return false;
}
virtual id_type id() const
{
throw runtime_error("Internal error node::id().");
#ifdef __SUNPRO_CC
// Stop bogus Solaris compiler warning
return id_type();
#endif
}
virtual id_type user_id() const
{
throw runtime_error("Internal error node::user_id().");
#ifdef __SUNPRO_CC
// Stop bogus Solaris compiler warning
return id_type();
#endif
}
virtual id_type next_dfa() const
{
throw runtime_error("Internal error node::next_dfa().");
#ifdef __SUNPRO_CC
// Stop bogus Solaris compiler warning
return id_type();
#endif
}
virtual id_type push_dfa() const
{
throw runtime_error("Internal error node::push_dfa().");
#ifdef __SUNPRO_CC
// Stop bogus Solaris compiler warning
return id_type();
#endif
}
virtual bool pop_dfa() const
{
throw runtime_error("Internal error node::pop_dfa().");
#ifdef __SUNPRO_CC
// Stop bogus Solaris compiler warning
return false;
#endif
}
virtual id_type token() const
{
throw runtime_error("Internal error node::token().");
#ifdef __SUNPRO_CC
// Stop bogus Solaris compiler warning
return id_type();
#endif
}
virtual void greedy(const bool /*greedy_*/)
{
throw runtime_error("Internal error node::greedy(bool).");
}
virtual bool greedy() const
{
throw runtime_error("Internal error node::greedy().");
#ifdef __SUNPRO_CC
// Stop bogus Solaris compiler warning
return false;
#endif
}
virtual const node_vector &followpos() const
{
throw runtime_error("Internal error node::followpos().");
#ifdef __SUNPRO_CC
// Stop bogus Solaris compiler warning
return firstpos;
#endif
}
virtual node_vector &followpos()
{
throw runtime_error("Internal error node::followpos().");
#ifdef __SUNPRO_CC
// Stop bogus Solaris compiler warning
return firstpos;
#endif
}
protected:
const bool _nullable;
node_vector _firstpos;
node_vector _lastpos;
virtual void copy_node(node_ptr_vector &node_ptr_vector_,
node_stack &new_node_stack_, bool_stack &perform_op_stack_,
bool &down_) const = 0;
private:
// No copy construction.
basic_node(const basic_node &) = delete;
// No assignment.
const basic_node &operator =(const basic_node &) = delete;
};
}
}
#endif

View File

@ -0,0 +1,104 @@
// selection_node.hpp
// Copyright (c) 2005-2018 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_SELECTION_NODE_HPP
#define LEXERTL_SELECTION_NODE_HPP
#include "node.hpp"
namespace lexertl
{
namespace detail
{
template<typename id_type>
class basic_selection_node : public basic_node<id_type>
{
public:
using node = basic_node<id_type>;
using bool_stack = typename node::bool_stack;
using const_node_stack = typename node::const_node_stack;
using node_ptr_vector = typename node::node_ptr_vector;
using node_stack = typename node::node_stack;
using node_type = typename node::node_type;
basic_selection_node(observer_ptr<node> left_, observer_ptr<node> right_) :
node(left_->nullable() || right_->nullable()),
_left(left_),
_right(right_)
{
_left->append_firstpos(node::_firstpos);
_right->append_firstpos(node::_firstpos);
_left->append_lastpos(node::_lastpos);
_right->append_lastpos(node::_lastpos);
}
virtual ~basic_selection_node() override
{
}
virtual node_type what_type() const override
{
return node::SELECTION;
}
virtual bool traverse(const_node_stack &node_stack_,
bool_stack &perform_op_stack_) const override
{
perform_op_stack_.push(true);
switch (_right->what_type())
{
case node::SEQUENCE:
case node::SELECTION:
case node::ITERATION:
perform_op_stack_.push(false);
break;
default:
break;
}
node_stack_.push(_right);
node_stack_.push(_left);
return true;
}
private:
observer_ptr<node> _left;
observer_ptr<node> _right;
virtual void copy_node(node_ptr_vector &node_ptr_vector_,
node_stack &new_node_stack_, bool_stack &perform_op_stack_,
bool &down_) const override
{
if (perform_op_stack_.top())
{
observer_ptr<node> rhs_ = new_node_stack_.top();
new_node_stack_.pop();
observer_ptr<node> lhs_ = new_node_stack_.top();
node_ptr_vector_.emplace_back
(std::make_unique<basic_selection_node>(lhs_, rhs_));
new_node_stack_.top() = node_ptr_vector_.back().get();
}
else
{
down_ = true;
}
perform_op_stack_.pop();
}
// No copy construction.
basic_selection_node(const basic_selection_node &) = delete;
// No assignment.
const basic_selection_node &operator =
(const basic_selection_node &) = delete;
};
}
}
#endif

View File

@ -0,0 +1,121 @@
// sequence_node.hpp
// Copyright (c) 2005-2018 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_SEQUENCE_NODE_HPP
#define LEXERTL_SEQUENCE_NODE_HPP
#include "node.hpp"
namespace lexertl
{
namespace detail
{
template<typename id_type>
class basic_sequence_node : public basic_node<id_type>
{
public:
using node = basic_node<id_type>;
using bool_stack = typename node::bool_stack;
using const_node_stack = typename node::const_node_stack;
using node_ptr_vector = typename node::node_ptr_vector;
using node_stack = typename node::node_stack;
using node_type = typename node::node_type;
using node_vector = typename node::node_vector;
basic_sequence_node(observer_ptr<node> left_, observer_ptr<node> right_) :
node(left_->nullable() && right_->nullable()),
_left(left_),
_right(right_)
{
_left->append_firstpos(node::_firstpos);
if (_left->nullable())
{
_right->append_firstpos(node::_firstpos);
}
if (_right->nullable())
{
_left->append_lastpos(node::_lastpos);
}
_right->append_lastpos(node::_lastpos);
node_vector &lastpos_ = _left->lastpos();
const node_vector &firstpos_ = _right->firstpos();
for (observer_ptr<node> node_ : lastpos_)
{
node_->append_followpos(firstpos_);
}
}
virtual ~basic_sequence_node() override
{
}
virtual node_type what_type() const override
{
return node::SEQUENCE;
}
virtual bool traverse(const_node_stack &node_stack_,
bool_stack &perform_op_stack_) const override
{
perform_op_stack_.push(true);
switch (_right->what_type())
{
case node::SEQUENCE:
case node::SELECTION:
case node::ITERATION:
perform_op_stack_.push(false);
break;
default:
break;
}
node_stack_.push(_right);
node_stack_.push(_left);
return true;
}
private:
observer_ptr<node> _left;
observer_ptr<node> _right;
virtual void copy_node(node_ptr_vector &node_ptr_vector_,
node_stack &new_node_stack_, bool_stack &perform_op_stack_,
bool &down_) const override
{
if (perform_op_stack_.top())
{
observer_ptr<node> rhs_ = new_node_stack_.top();
new_node_stack_.pop();
observer_ptr<node> lhs_ = new_node_stack_.top();
node_ptr_vector_.emplace_back
(std::make_unique<basic_sequence_node>(lhs_, rhs_));
new_node_stack_.top() = node_ptr_vector_.back().get();
}
else
{
down_ = true;
}
perform_op_stack_.pop();
}
// No copy construction.
basic_sequence_node(const basic_sequence_node &) = delete;
// No assignment.
const basic_sequence_node &operator =(const basic_sequence_node &) = delete;
};
}
}
#endif