diff --git a/alib2/src/FromStringParser.hpp b/alib2/src/FromStringParser.hpp new file mode 100644 index 0000000000000000000000000000000000000000..b8b295700a29319a958d355d12891c38f115b76d --- /dev/null +++ b/alib2/src/FromStringParser.hpp @@ -0,0 +1,45 @@ +/* + * FromStringParser.h + * + * Created on: Nov 23, 2013 + * Author: Jan Travnicek + */ + +#ifndef FROM_STRING_PARSER_H_ +#define FROM_STRING_PARSER_H_ + +#include <set> +#include "exception/AlibException.h" + +namespace alib { + +template<class T, class F> +class FromStringParser { + virtual T parse() = 0; + virtual T parse(const std::set<F>& features) = 0; + virtual bool last() = 0; +public: + virtual bool first() = 0; + T* parsePointer() { + if(first()) { + return new T(parse()); + } else { + return NULL; + } + } + + T parseValue() { + first(); + T res = parse(); + + if(last()) { + return std::move(res); + } else { + throw exception::AlibException(); + } + } +}; + +} /* namespace alib */ + +#endif /* FROM_STRING_PARSER_H_ */ diff --git a/alib2/src/alphabet/SymbolFromStringLexer.cpp b/alib2/src/alphabet/SymbolFromStringLexer.cpp index 52af8b1f474c2ebd1f776aabcc456ec35efad226..75e228ded487e86d400edc153a8b875246639482 100644 --- a/alib2/src/alphabet/SymbolFromStringLexer.cpp +++ b/alib2/src/alphabet/SymbolFromStringLexer.cpp @@ -9,6 +9,7 @@ SymbolFromStringLexer::SymbolFromStringLexer(std::stringstream& in) : m_In(in) { } SymbolFromStringLexer& SymbolFromStringLexer::next() { + if(m_Current.type == TokenType::TEOF) return *this; char character; m_Current.value = ""; diff --git a/alib2/src/alphabet/SymbolFromStringParser.cpp b/alib2/src/alphabet/SymbolFromStringParser.cpp index 0ca379e8c633f49671deebc429f3550ec8148e13..83b11f71659f9d5063a95f59ed8c816b6ce1dfa5 100644 --- a/alib2/src/alphabet/SymbolFromStringParser.cpp +++ b/alib2/src/alphabet/SymbolFromStringParser.cpp @@ -36,19 +36,6 @@ Symbol SymbolFromStringParser::parse(const std::set<FEATURES>& features) { throw exception::AlibException(); } -Symbol SymbolFromStringParser::parseValue() { - first() || m_LabelParser.first(); - - Symbol res = parse(); - - SymbolFromStringLexer::Token token = m_SymbolLexer.next().token(); - if(token.type == SymbolFromStringLexer::TokenType::TEOF) { - return std::move(res); - } else { - throw exception::AlibException(); - } -} - bool SymbolFromStringParser::next() { SymbolFromStringLexer::Token token = m_SymbolLexer.next().token(); if(token.type == SymbolFromStringLexer::TokenType::BLANK || token.type == SymbolFromStringLexer::TokenType::BOTTOM || token.type == SymbolFromStringLexer::TokenType::END) { @@ -63,15 +50,16 @@ bool SymbolFromStringParser::first() { if(token.type == SymbolFromStringLexer::TokenType::BLANK || token.type == SymbolFromStringLexer::TokenType::BOTTOM || token.type == SymbolFromStringLexer::TokenType::END) { return true; } else { - return false; + return token.type == SymbolFromStringLexer::TokenType::ERROR && m_LabelParser.first(); } } -Symbol* SymbolFromStringParser::parsePointer() { - if(first() || (m_SymbolLexer.token().type == SymbolFromStringLexer::TokenType::ERROR && m_LabelParser.first())) { - return new Symbol(parse()); +bool SymbolFromStringParser::last() { + SymbolFromStringLexer::Token token = m_SymbolLexer.next().token(); + if(token.type == SymbolFromStringLexer::TokenType::TEOF) { + return true; } else { - return NULL; + return false; } } diff --git a/alib2/src/alphabet/SymbolFromStringParser.h b/alib2/src/alphabet/SymbolFromStringParser.h index aa510bb978cfbfc6679c1e73464d556323f2d7e1..3a90bff8afcfa092da40d8c1b0c7b2f3e918f1d9 100644 --- a/alib2/src/alphabet/SymbolFromStringParser.h +++ b/alib2/src/alphabet/SymbolFromStringParser.h @@ -14,6 +14,7 @@ #include "SymbolFromStringLexer.h" #include "../label/LabelFromStringParser.h" #include "SymbolFeatures.h" +#include "../FromStringParser.hpp" namespace string { @@ -35,19 +36,18 @@ class FiniteAutomatonFromStringParser; namespace alphabet { -class SymbolFromStringParser { +class SymbolFromStringParser : public alib::FromStringParser<Symbol, FEATURES> { LabeledSymbol parseContent(); SymbolFromStringLexer m_SymbolLexer; label::LabelFromStringParser m_LabelParser; - Symbol parse(); - Symbol parse(const std::set<FEATURES>& features); + virtual Symbol parse(); + virtual Symbol parse(const std::set<FEATURES>& features); bool next(); + virtual bool last(); public: - bool first(); + virtual bool first(); SymbolFromStringParser(std::stringstream&); - Symbol* parsePointer(); - Symbol parseValue(); friend class string::StringFromStringParser; friend class regexp::RegExpFromStringParser; friend class automaton::FiniteAutomatonFromStringParser; diff --git a/alib2/src/automaton/FSM/FiniteAutomatonFromStringLexer.cpp b/alib2/src/automaton/FSM/FiniteAutomatonFromStringLexer.cpp index 11a14a6360d33b4515727f6bad0c6180ace1b51f..c27375864473b11f18b26b5e03558ea74dabeb96 100644 --- a/alib2/src/automaton/FSM/FiniteAutomatonFromStringLexer.cpp +++ b/alib2/src/automaton/FSM/FiniteAutomatonFromStringLexer.cpp @@ -8,6 +8,7 @@ FiniteAutomatonFromStringLexer::FiniteAutomatonFromStringLexer(std::stringstream } FiniteAutomatonFromStringLexer& FiniteAutomatonFromStringLexer::next() { + if(m_Current.type == TokenType::TEOF) return *this; char character; m_Current.value = ""; diff --git a/alib2/src/automaton/FSM/FiniteAutomatonFromStringParser.cpp b/alib2/src/automaton/FSM/FiniteAutomatonFromStringParser.cpp index 11e270da0e926378195f5806c0519780eae55ed9..70d029ac275f44486ccfcb327f532575d12e140b 100644 --- a/alib2/src/automaton/FSM/FiniteAutomatonFromStringParser.cpp +++ b/alib2/src/automaton/FSM/FiniteAutomatonFromStringParser.cpp @@ -35,18 +35,6 @@ Automaton FiniteAutomatonFromStringParser::parse(const std::set<FEATURES>& featu } } -Automaton FiniteAutomatonFromStringParser::parseValue() { - first(); - Automaton res = parse(); - - FiniteAutomatonFromStringLexer::Token token = m_FiniteAutomatonLexer.token(); - if(token.type == FiniteAutomatonFromStringLexer::TokenType::TEOF) { - return std::move(res); - } else { - throw exception::AlibException(); - } -} - bool FiniteAutomatonFromStringParser::first() { FiniteAutomatonFromStringLexer::Token token = m_FiniteAutomatonLexer.next().token(); if(token.type == FiniteAutomatonFromStringLexer::TokenType::EPSILON_NFA || token.type == FiniteAutomatonFromStringLexer::TokenType::NFA || token.type == FiniteAutomatonFromStringLexer::TokenType::DFA) { @@ -65,11 +53,12 @@ bool FiniteAutomatonFromStringParser::next() { } } -Automaton* FiniteAutomatonFromStringParser::parsePointer() { - if(first()) { - return new Automaton(parse()); +bool FiniteAutomatonFromStringParser::last() { + FiniteAutomatonFromStringLexer::Token token = m_FiniteAutomatonLexer.next().token(); + if(token.type == FiniteAutomatonFromStringLexer::TokenType::TEOF) { + return true; } else { - return NULL; + return false; } } diff --git a/alib2/src/automaton/FSM/FiniteAutomatonFromStringParser.h b/alib2/src/automaton/FSM/FiniteAutomatonFromStringParser.h index 6fdcbd3feb0d40995890a57f634aa513bc35d062..5cb570200521d1cdcddfaf449e9dccb142dfbcf7 100644 --- a/alib2/src/automaton/FSM/FiniteAutomatonFromStringParser.h +++ b/alib2/src/automaton/FSM/FiniteAutomatonFromStringParser.h @@ -4,6 +4,7 @@ #include "FiniteAutomatonFromStringLexer.h" #include "../../alphabet/SymbolFromStringParser.h" #include "../../label/LabelFromStringParser.h" +#include "../../FromStringParser.hpp" #include <tuple> @@ -18,15 +19,11 @@ namespace automaton { -class FiniteAutomatonFromStringParser { +class FiniteAutomatonFromStringParser : public alib::FromStringParser<Automaton, FEATURES> { FiniteAutomatonFromStringLexer m_FiniteAutomatonLexer; alphabet::SymbolFromStringParser m_SymbolParser; label::LabelFromStringParser m_LabelParser; - Automaton parse(); - Automaton parse(const std::set<FEATURES>& features); - bool next(); - void initialFinalState(bool& initial, bool& final); EpsilonNFA parseEpsilonNFA(); @@ -35,12 +32,15 @@ class FiniteAutomatonFromStringParser { void parseEpsilonNFATransition(EpsilonNFA& res, const std::vector<std::variant<string::Epsilon, alphabet::Symbol> >& symbols); void parseNFATransition(NFA& res, const std::vector<alphabet::Symbol>& symbols); void parseDFATransition(std::set<State>& states, const std::vector<alphabet::Symbol>& symbols, State*& initialState, std::set<State>& finalStates, std::set<std::tuple<State, alphabet::Symbol, State>>& transitionFunction); + virtual bool last(); + Automaton parse(); + Automaton parse(const std::set<FEATURES>& features); + bool next(); + public: FiniteAutomatonFromStringParser(std::stringstream& input); - Automaton parseValue(); bool first(); - Automaton* parsePointer(); }; diff --git a/alib2/src/label/LabelFromStringLexer.cpp b/alib2/src/label/LabelFromStringLexer.cpp index dade1708287d4c69677897bb2f5d2b4cc5b8e5fb..d7e87c2b6fc29a0b888a68875680753914b2ff7b 100644 --- a/alib2/src/label/LabelFromStringLexer.cpp +++ b/alib2/src/label/LabelFromStringLexer.cpp @@ -8,6 +8,7 @@ LabelFromStringLexer::LabelFromStringLexer(std::stringstream& in) : m_In(in) { } LabelFromStringLexer& LabelFromStringLexer::next() { + if(m_Current.type == TokenType::TEOF) return *this; char character; m_Current.value = ""; diff --git a/alib2/src/label/LabelFromStringParser.cpp b/alib2/src/label/LabelFromStringParser.cpp index 0e154405c259f8b8991f4ab9e07cd693e15adbd6..9eead1afd879c2cd6a4470bd838a9c2a808cba7f 100644 --- a/alib2/src/label/LabelFromStringParser.cpp +++ b/alib2/src/label/LabelFromStringParser.cpp @@ -35,18 +35,6 @@ Label LabelFromStringParser::parse(const std::set<FEATURES>& features) { throw exception::AlibException(); } -Label LabelFromStringParser::parseValue() { - first(); - Label res = parse(); - - LabelFromStringLexer::Token token = m_Lexer.next().token(); - if(token.type == LabelFromStringLexer::TokenType::TEOF) { - return std::move(res); - } else { - throw exception::AlibException(); - } -} - bool LabelFromStringParser::next() { LabelFromStringLexer::Token token = m_Lexer.next().token(); if(token.type == LabelFromStringLexer::TokenType::STRING || token.type == LabelFromStringLexer::TokenType::CHAR || token.type == LabelFromStringLexer::TokenType::INTEGER) { @@ -65,11 +53,12 @@ bool LabelFromStringParser::first() { } } -Label* LabelFromStringParser::parsePointer() { - if(first()) { - return new Label(parse()); +bool LabelFromStringParser::last() { + LabelFromStringLexer::Token token = m_Lexer.next().token(); + if(token.type == LabelFromStringLexer::TokenType::TEOF) { + return true; } else { - return NULL; + return false; } } diff --git a/alib2/src/label/LabelFromStringParser.h b/alib2/src/label/LabelFromStringParser.h index 3119e89ae9d64b64c9fc29203c2045779d1cd353..2f8e3421b25dc5ecad4db9bf72d3ea5d98149ff7 100644 --- a/alib2/src/label/LabelFromStringParser.h +++ b/alib2/src/label/LabelFromStringParser.h @@ -12,6 +12,7 @@ #include "LabelFeatures.h" #include <vector> #include <set> +#include "../FromStringParser.hpp" #include "LabelFromStringLexer.h" @@ -41,17 +42,16 @@ class FiniteAutomatonFromStringParser; namespace label { -class LabelFromStringParser { +class LabelFromStringParser : public alib::FromStringParser<Label, FEATURES> { LabelFromStringLexer m_Lexer; - Label parse(const std::set<FEATURES>&); - Label parse(); + virtual Label parse(const std::set<FEATURES>&); + virtual Label parse(); bool next(); + virtual bool last(); public: - bool first(); + virtual bool first(); LabelFromStringParser(std::stringstream&); - Label* parsePointer(); - Label parseValue(); friend class alphabet::SymbolFromStringParser; friend class regexp::RegExpFromStringParser; friend class string::StringFromStringParser; diff --git a/alib2/src/regexp/RegExpFromStringLexer.cpp b/alib2/src/regexp/RegExpFromStringLexer.cpp index debd44b1a29af924281f972d8d3103b4d6be5141..edb6b431c4f1e88e3e50d3c87256ecb4661f0be3 100644 --- a/alib2/src/regexp/RegExpFromStringLexer.cpp +++ b/alib2/src/regexp/RegExpFromStringLexer.cpp @@ -8,6 +8,7 @@ RegExpFromStringLexer::RegExpFromStringLexer(std::stringstream& in) : m_In(in) { } RegExpFromStringLexer& RegExpFromStringLexer::next() { + if(m_Current.type == TokenType::TEOF) return *this; char character; m_Current.value = ""; diff --git a/alib2/src/regexp/RegExpFromStringParser.cpp b/alib2/src/regexp/RegExpFromStringParser.cpp index 1b5a487827af70fff0fe92157d47097221afcd6d..c7457da7a8029a07b8d288e32a9ed2fa56f41790 100644 --- a/alib2/src/regexp/RegExpFromStringParser.cpp +++ b/alib2/src/regexp/RegExpFromStringParser.cpp @@ -20,24 +20,12 @@ RegExp RegExpFromStringParser::parse(const std::set<FEATURES>& features) { return regexp; } -RegExp RegExpFromStringParser::parseValue() { - first() || m_SymbolParser.first() || m_SymbolParser.m_LabelParser.first(); - RegExp res = parse(); - - RegExpFromStringLexer::Token token = m_RegexpLexer.token(); - if(token.type == RegExpFromStringLexer::TokenType::TEOF) { - return std::move(res); - } else { - throw exception::AlibException(); - } -} - bool RegExpFromStringParser::first() { RegExpFromStringLexer::Token token = m_RegexpLexer.next().token(); if(token.type == RegExpFromStringLexer::TokenType::EMPTY || token.type == RegExpFromStringLexer::TokenType::EPS || token.type == RegExpFromStringLexer::TokenType::LPAR) { return true; } else { - return false; + return token.type == RegExpFromStringLexer::TokenType::ERROR && m_SymbolParser.first(); } } @@ -50,11 +38,12 @@ bool RegExpFromStringParser::next() { } } -RegExp* RegExpFromStringParser::parsePointer() { - if(first() || (m_RegexpLexer.token().type == RegExpFromStringLexer::TokenType::ERROR && m_SymbolParser.first()) || (m_SymbolParser.m_SymbolLexer.token().type == alphabet::SymbolFromStringLexer::TokenType::ERROR && m_SymbolParser.m_LabelParser.first())) { - return new RegExp(parse()); +bool RegExpFromStringParser::last() { + RegExpFromStringLexer::Token token = m_RegexpLexer.next().token(); + if(token.type == RegExpFromStringLexer::TokenType::TEOF) { + return true; } else { - return NULL; + return false; } } @@ -65,7 +54,7 @@ RegExpElement* RegExpFromStringParser::alternation() { RegExpElement* RegExpFromStringParser::alternationCont(RegExpElement* left) { RegExpFromStringLexer::Token token = m_RegexpLexer.token(); if(token.type == RegExpFromStringLexer::TokenType::PLUS) { - first() || m_SymbolParser.first() || m_SymbolParser.m_LabelParser.first(); + next() || m_SymbolParser.first(); try { RegExpElement* right = this->concatenation(); @@ -87,7 +76,7 @@ RegExpElement* RegExpFromStringParser::alternationCont(RegExpElement* left) { RegExpElement* RegExpFromStringParser::alternationContCont(Alternation* res) { RegExpFromStringLexer::Token token = m_RegexpLexer.token(); if(token.type == RegExpFromStringLexer::TokenType::PLUS) { - first() || m_SymbolParser.first() || m_SymbolParser.m_LabelParser.first(); + next() || m_SymbolParser.first(); try { RegExpElement* next = this->concatenation(); @@ -152,7 +141,7 @@ RegExpElement* RegExpFromStringParser::concatenationContCont(Concatenation* res) RegExpElement* RegExpFromStringParser::factor() { RegExpFromStringLexer::Token token = m_RegexpLexer.token(); if(token.type == RegExpFromStringLexer::TokenType::LPAR) { - first() || m_SymbolParser.first() || m_SymbolParser.m_LabelParser.first(); + next() || m_SymbolParser.first(); RegExpElement* base = this->alternation(); token = m_RegexpLexer.token(); @@ -171,7 +160,7 @@ RegExpElement* RegExpFromStringParser::factor() { } RegExpElement* RegExpFromStringParser::star(RegExpElement* elem) { - next() || m_SymbolParser.first() || m_SymbolParser.m_LabelParser.first(); + next() || m_SymbolParser.first(); RegExpFromStringLexer::Token token = m_RegexpLexer.token(); if(token.type == RegExpFromStringLexer::TokenType::STAR) { diff --git a/alib2/src/regexp/RegExpFromStringParser.h b/alib2/src/regexp/RegExpFromStringParser.h index af1457ee14333a3934aa2554696304d8252b4b02..2f9291a33cde6412db9af4a9b45aafe6bc3e5d38 100644 --- a/alib2/src/regexp/RegExpFromStringParser.h +++ b/alib2/src/regexp/RegExpFromStringParser.h @@ -14,10 +14,11 @@ #include "RegExpFromStringLexer.h" #include "../alphabet/SymbolFromStringParser.h" #include "RegExpFeatures.h" +#include "../FromStringParser.hpp" namespace regexp { -class RegExpFromStringParser { +class RegExpFromStringParser : public alib::FromStringParser<RegExp, FEATURES> { RegExpElement* alternation(); RegExpElement* alternationCont(RegExpElement* left); RegExpElement* alternationContCont(Alternation* left); @@ -32,14 +33,13 @@ class RegExpFromStringParser { RegExpFromStringLexer m_RegexpLexer; alphabet::SymbolFromStringParser m_SymbolParser; - RegExp parse(); - RegExp parse(const std::set<FEATURES>& features); + virtual RegExp parse(); + virtual RegExp parse(const std::set<FEATURES>& features); bool next(); + virtual bool last(); public: - bool first(); - RegExp* parsePointer(); + virtual bool first(); RegExpFromStringParser(std::stringstream&); - RegExp parseValue(); }; diff --git a/alib2/src/string/StringFromStringLexer.cpp b/alib2/src/string/StringFromStringLexer.cpp index 8390deabc6432d67578379bafae84ec26e2d6ed2..0d20e3673cf50c1637ab90d0ed7f5c45c845985a 100644 --- a/alib2/src/string/StringFromStringLexer.cpp +++ b/alib2/src/string/StringFromStringLexer.cpp @@ -8,6 +8,7 @@ StringFromStringLexer::StringFromStringLexer(std::stringstream& in) : m_In(in) { } StringFromStringLexer& StringFromStringLexer::next() { + if(m_Current.type == TokenType::TEOF) return *this; char character; m_Current.value = ""; diff --git a/alib2/src/string/StringFromStringParser.cpp b/alib2/src/string/StringFromStringParser.cpp index f6118ce82641ad678f444e5cb4a97353fe6c7ac1..5906a3608ffa438998a867b3af46904eacdd4c19 100644 --- a/alib2/src/string/StringFromStringParser.cpp +++ b/alib2/src/string/StringFromStringParser.cpp @@ -43,18 +43,6 @@ String StringFromStringParser::parse(const std::set<FEATURES>& features) { } } -String StringFromStringParser::parseValue() { - first() || m_SymbolParser.first() || m_SymbolParser.m_LabelParser.first(); - String res = parse(); - - StringFromStringLexer::Token token = m_StringLexer.next().token(); - if(token.type == StringFromStringLexer::TokenType::TEOF) { - return std::move(res); - } else { - throw exception::AlibException(); - } -} - bool StringFromStringParser::first() { StringFromStringLexer::Token token = m_StringLexer.next().token(); if(token.type == StringFromStringLexer::TokenType::EPSILON || token.type == StringFromStringLexer::TokenType::LESS || token.type == StringFromStringLexer::TokenType::QUOTE) { @@ -73,11 +61,12 @@ bool StringFromStringParser::next() { } } -String* StringFromStringParser::parsePointer() { - if(first()) { - return new String(parse()); +bool StringFromStringParser::last() { + StringFromStringLexer::Token token = m_StringLexer.next().token(); + if(token.type == StringFromStringLexer::TokenType::TEOF) { + return true; } else { - return NULL; + return false; } } diff --git a/alib2/src/string/StringFromStringParser.h b/alib2/src/string/StringFromStringParser.h index 8bc0c10ad77c23af28ddd3d7c16bdecd5930500a..53356cb0d02ae4f58663d79704f7a8e4359435a0 100644 --- a/alib2/src/string/StringFromStringParser.h +++ b/alib2/src/string/StringFromStringParser.h @@ -15,23 +15,23 @@ #include "StringFromStringLexer.h" #include "../alphabet/SymbolFromStringParser.h" +#include "../FromStringParser.hpp" namespace string { -class StringFromStringParser { +class StringFromStringParser : public alib::FromStringParser<String, FEATURES> { std::vector<alphabet::Symbol> parseContent(); StringFromStringLexer m_StringLexer; alphabet::SymbolFromStringParser m_SymbolParser; - String parse(); - String parse(const std::set<FEATURES>& features); + virtual String parse(); + virtual String parse(const std::set<FEATURES>& features); bool next(); + virtual bool last(); public: - bool first(); - String* parsePointer(); + virtual bool first(); StringFromStringParser(std::stringstream&); - String parseValue(); };