From 8de90acc6791a62d4b2e48e9bd05daa0d5cfe4b6 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 27 Mar 2015 03:38:01 +0100 Subject: Generate items from grammar --- src/item.hpp | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/item.hpp (limited to 'src/item.hpp') diff --git a/src/item.hpp b/src/item.hpp new file mode 100644 index 0000000..58987b8 --- /dev/null +++ b/src/item.hpp @@ -0,0 +1,80 @@ +/* + Copyright (c) 2015, Matthias Schiffer + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +#pragma once + +#include "symbol.hpp" + +#include + + +namespace solar { + +struct item_t : public std::tuple, unsigned> { + item_t(const std::string &lhs) + : std::tuple, unsigned>(lhs, std::vector(), 0) {} + + const std::string & get_lhs() const { + return std::get<0>(*this); + } + + std::string & get_lhs() { + return std::get<0>(*this); + } + + const std::vector & get_rhs() const { + return std::get<1>(*this); + } + + std::vector & get_rhs() { + return std::get<1>(*this); + } + + unsigned get_point() const { + return std::get<2>(*this); + } + + unsigned & get_point() { + return std::get<2>(*this); + } + + bool can_shift() const { + return get_point() < get_rhs().size(); + } + + void shift() { + get_point()++; + } + + symbol_t get_next_symbol() const { + if (can_shift()) + return get_rhs()[get_point()]; + else + return symbol_t::make_end(); + } +}; + +} -- cgit v1.2.3