From ee1006fcdc873d79477cc4ffe103144424baa3d5 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Wed, 26 Oct 2016 21:16:30 +0200 Subject: [PATCH] template RegExp datatypes part I --- alib2data/src/automaton/FSM/ExtendedNFA.cpp | 42 ++-- alib2data/src/automaton/FSM/ExtendedNFA.h | 16 +- .../common/AutomatonFromXMLParser.cpp | 8 - .../automaton/common/AutomatonFromXMLParser.h | 12 +- .../common/AutomatonToXMLComposer.cpp | 6 - .../automaton/common/AutomatonToXMLComposer.h | 10 +- alib2data/src/regexp/RegExp.cpp | 16 +- alib2data/src/regexp/RegExpFeatures.h | 20 ++ .../src/regexp/common/RegExpFromXMLParser.cpp | 172 -------------- .../src/regexp/common/RegExpFromXMLParser.h | 210 ++++++++++++++++-- .../src/regexp/common/RegExpToXMLComposer.cpp | 97 -------- .../src/regexp/common/RegExpToXMLComposer.h | 134 +++++++++-- alib2data/src/regexp/formal/FormalRegExp.cpp | 101 +-------- alib2data/src/regexp/formal/FormalRegExp.h | 140 ++++++++++-- .../regexp/formal/FormalRegExpAlternation.cpp | 108 --------- .../regexp/formal/FormalRegExpAlternation.h | 167 ++++++++++++-- .../formal/FormalRegExpConcatenation.cpp | 103 --------- .../regexp/formal/FormalRegExpConcatenation.h | 161 ++++++++++++-- .../src/regexp/formal/FormalRegExpElement.cpp | 19 -- .../src/regexp/formal/FormalRegExpElement.h | 66 +++--- .../src/regexp/formal/FormalRegExpEmpty.cpp | 54 ----- .../src/regexp/formal/FormalRegExpEmpty.h | 82 ++++++- .../src/regexp/formal/FormalRegExpEpsilon.cpp | 55 ----- .../src/regexp/formal/FormalRegExpEpsilon.h | 85 +++++-- .../regexp/formal/FormalRegExpIteration.cpp | 76 ------- .../src/regexp/formal/FormalRegExpIteration.h | 124 +++++++++-- .../regexp/formal/FormalRegExpStructure.cpp | 64 ------ .../src/regexp/formal/FormalRegExpStructure.h | 110 +++++++-- .../src/regexp/formal/FormalRegExpSymbol.cpp | 68 ------ .../src/regexp/formal/FormalRegExpSymbol.h | 108 +++++++-- .../src/regexp/unbounded/UnboundedRegExp.cpp | 100 +-------- .../src/regexp/unbounded/UnboundedRegExp.h | 140 ++++++++++-- .../unbounded/UnboundedRegExpAlternation.cpp | 95 -------- .../unbounded/UnboundedRegExpAlternation.h | 136 ++++++++++-- .../UnboundedRegExpConcatenation.cpp | 94 -------- .../unbounded/UnboundedRegExpConcatenation.h | 136 ++++++++++-- .../unbounded/UnboundedRegExpElement.cpp | 19 -- .../regexp/unbounded/UnboundedRegExpElement.h | 55 ++--- .../regexp/unbounded/UnboundedRegExpEmpty.cpp | 55 ----- .../regexp/unbounded/UnboundedRegExpEmpty.h | 84 ++++++- .../unbounded/UnboundedRegExpEpsilon.cpp | 55 ----- .../regexp/unbounded/UnboundedRegExpEpsilon.h | 84 ++++++- .../unbounded/UnboundedRegExpIteration.cpp | 76 ------- .../unbounded/UnboundedRegExpIteration.h | 124 +++++++++-- .../unbounded/UnboundedRegExpStructure.cpp | 64 ------ .../unbounded/UnboundedRegExpStructure.h | 108 +++++++-- .../unbounded/UnboundedRegExpSymbol.cpp | 68 ------ .../regexp/unbounded/UnboundedRegExpSymbol.h | 110 +++++++-- .../test-src/automaton/AutomatonTest.cpp | 24 +- alib2data/test-src/regexp/RegExpTest.cpp | 194 ++++++++-------- 50 files changed, 2203 insertions(+), 2052 deletions(-) delete mode 100644 alib2data/src/regexp/common/RegExpFromXMLParser.cpp delete mode 100644 alib2data/src/regexp/common/RegExpToXMLComposer.cpp delete mode 100644 alib2data/src/regexp/formal/FormalRegExpAlternation.cpp delete mode 100644 alib2data/src/regexp/formal/FormalRegExpConcatenation.cpp delete mode 100644 alib2data/src/regexp/formal/FormalRegExpElement.cpp delete mode 100644 alib2data/src/regexp/formal/FormalRegExpEmpty.cpp delete mode 100644 alib2data/src/regexp/formal/FormalRegExpEpsilon.cpp delete mode 100644 alib2data/src/regexp/formal/FormalRegExpIteration.cpp delete mode 100644 alib2data/src/regexp/formal/FormalRegExpStructure.cpp delete mode 100644 alib2data/src/regexp/formal/FormalRegExpSymbol.cpp delete mode 100644 alib2data/src/regexp/unbounded/UnboundedRegExpAlternation.cpp delete mode 100644 alib2data/src/regexp/unbounded/UnboundedRegExpConcatenation.cpp delete mode 100644 alib2data/src/regexp/unbounded/UnboundedRegExpElement.cpp delete mode 100644 alib2data/src/regexp/unbounded/UnboundedRegExpEmpty.cpp delete mode 100644 alib2data/src/regexp/unbounded/UnboundedRegExpEpsilon.cpp delete mode 100644 alib2data/src/regexp/unbounded/UnboundedRegExpIteration.cpp delete mode 100644 alib2data/src/regexp/unbounded/UnboundedRegExpStructure.cpp delete mode 100644 alib2data/src/regexp/unbounded/UnboundedRegExpSymbol.cpp diff --git a/alib2data/src/automaton/FSM/ExtendedNFA.cpp b/alib2data/src/automaton/FSM/ExtendedNFA.cpp index bcf67af0b4..87242abcfe 100644 --- a/alib2data/src/automaton/FSM/ExtendedNFA.cpp +++ b/alib2data/src/automaton/FSM/ExtendedNFA.cpp @@ -37,12 +37,12 @@ ExtendedNFA::ExtendedNFA ( label::Label initialState ) : ExtendedNFA ( std::set ExtendedNFA::ExtendedNFA ( const CompactNFA < > & other ) : ExtendedNFA ( other.getStates ( ), other.getInputAlphabet ( ), other.getInitialState ( ), other.getFinalStates ( ) ) { for ( const auto & transition : other.getTransitions ( ) ) { - regexp::UnboundedRegExpConcatenation con; + regexp::UnboundedRegExpConcatenation < alphabet::Symbol > con; for ( auto & symbol : transition.first.second ) - con.appendElement ( regexp::UnboundedRegExpSymbol ( symbol ) ); + con.appendElement ( regexp::UnboundedRegExpSymbol < alphabet::Symbol > ( symbol ) ); - std::pair < label::Label, regexp::UnboundedRegExpStructure > key = std::make_pair ( transition.first.first, regexp::UnboundedRegExpStructure ( std::move ( con ) ) ); + std::pair < label::Label, regexp::UnboundedRegExpStructure < alphabet::Symbol > > key = std::make_pair ( transition.first.first, regexp::UnboundedRegExpStructure < alphabet::Symbol > ( std::move ( con ) ) ); transitions[key] = transition.second; } } @@ -50,10 +50,10 @@ ExtendedNFA::ExtendedNFA ( const CompactNFA < > & other ) : ExtendedNFA ( other. ExtendedNFA::ExtendedNFA ( const EpsilonNFA < > & other ) : ExtendedNFA ( other.getStates ( ), other.getInputAlphabet ( ), other.getInitialState ( ), other.getFinalStates ( ) ) { for ( const auto & transition : other.getTransitions ( ) ) { if ( transition.first.second.is < string::Epsilon < > > ( ) ) { - std::pair < label::Label, regexp::UnboundedRegExpStructure > key = std::make_pair ( transition.first.first, regexp::UnboundedRegExpStructure ( ) ); + std::pair < label::Label, regexp::UnboundedRegExpStructure < alphabet::Symbol > > key = std::make_pair ( transition.first.first, regexp::UnboundedRegExpStructure < alphabet::Symbol > ( ) ); transitions[key] = transition.second; } else { - std::pair < label::Label, regexp::UnboundedRegExpStructure > key = std::make_pair ( transition.first.first, regexp::UnboundedRegExpStructure ( regexp::UnboundedRegExpSymbol ( transition.first.second.get < alphabet::Symbol > ( ) ) ) ); + std::pair < label::Label, regexp::UnboundedRegExpStructure < alphabet::Symbol > > key = std::make_pair ( transition.first.first, regexp::UnboundedRegExpStructure < alphabet::Symbol > ( regexp::UnboundedRegExpSymbol < alphabet::Symbol > ( transition.first.second.get < alphabet::Symbol > ( ) ) ) ); transitions[key] = transition.second; } } @@ -61,24 +61,24 @@ ExtendedNFA::ExtendedNFA ( const EpsilonNFA < > & other ) : ExtendedNFA ( other. ExtendedNFA::ExtendedNFA ( const MultiInitialStateNFA < > & other ) : ExtendedNFA ( other.getStates ( ) + std::set < label::Label > { label::createUniqueLabel ( label::InitialStateLabel::INITIAL_STATE_LABEL, other.getStates ( ) ) }, other.getInputAlphabet ( ), label::createUniqueLabel ( label::InitialStateLabel::INITIAL_STATE_LABEL, other.getStates ( ) ), other.getFinalStates ( ) ) { for ( const auto & transition : other.getTransitions ( ) ) { - std::pair < label::Label, regexp::UnboundedRegExpStructure > key = std::make_pair ( transition.first.first, regexp::UnboundedRegExpStructure ( regexp::UnboundedRegExpSymbol ( transition.first.second ) ) ); + std::pair < label::Label, regexp::UnboundedRegExpStructure < alphabet::Symbol > > key = std::make_pair ( transition.first.first, regexp::UnboundedRegExpStructure < alphabet::Symbol > ( regexp::UnboundedRegExpSymbol < alphabet::Symbol > ( transition.first.second ) ) ); transitions[key] = transition.second; } - std::pair < label::Label, regexp::UnboundedRegExpStructure > key = std::make_pair ( this->getInitialState ( ), regexp::UnboundedRegExpStructure ( ) ); + std::pair < label::Label, regexp::UnboundedRegExpStructure < alphabet::Symbol > > key = std::make_pair ( this->getInitialState ( ), regexp::UnboundedRegExpStructure < alphabet::Symbol > ( ) ); transitions[key] = other.getInitialStates ( ); } ExtendedNFA::ExtendedNFA ( const NFA <> & other ) : ExtendedNFA ( other.getStates ( ), other.getInputAlphabet ( ), other.getInitialState ( ), other.getFinalStates ( ) ) { for ( const auto & transition : other.getTransitions ( ) ) { - std::pair < label::Label, regexp::UnboundedRegExpStructure > key = std::make_pair ( transition.first.first, regexp::UnboundedRegExpStructure ( regexp::UnboundedRegExpSymbol ( transition.first.second ) ) ); + std::pair < label::Label, regexp::UnboundedRegExpStructure < alphabet::Symbol > > key = std::make_pair ( transition.first.first, regexp::UnboundedRegExpStructure < alphabet::Symbol > ( regexp::UnboundedRegExpSymbol < alphabet::Symbol > ( transition.first.second ) ) ); transitions[key] = transition.second; } } ExtendedNFA::ExtendedNFA ( const DFA<> & other ) : ExtendedNFA ( other.getStates ( ), other.getInputAlphabet ( ), other.getInitialState ( ), other.getFinalStates ( ) ) { for ( const auto & transition : other.getTransitions ( ) ) { - std::pair < label::Label, regexp::UnboundedRegExpStructure > key = std::make_pair ( transition.first.first, regexp::UnboundedRegExpStructure ( regexp::UnboundedRegExpSymbol ( transition.first.second ) ) ); + std::pair < label::Label, regexp::UnboundedRegExpStructure < alphabet::Symbol > > key = std::make_pair ( transition.first.first, regexp::UnboundedRegExpStructure < alphabet::Symbol > ( regexp::UnboundedRegExpSymbol < alphabet::Symbol > ( transition.first.second ) ) ); transitions[key].insert ( transition.second ); } } @@ -91,7 +91,7 @@ AutomatonBase * ExtendedNFA::plunder ( ) && { return new ExtendedNFA ( std::move ( * this ) ); } -bool ExtendedNFA::addTransition ( label::Label from, regexp::UnboundedRegExpStructure input, label::Label to ) { +bool ExtendedNFA::addTransition ( label::Label from, regexp::UnboundedRegExpStructure < alphabet::Symbol > input, label::Label to ) { if ( !getStates ( ).count ( from ) ) throw AutomatonException ( "State \"" + std::to_string ( from ) + "\" doesn't exist." ); @@ -104,41 +104,41 @@ bool ExtendedNFA::addTransition ( label::Label from, regexp::UnboundedRegExpStru if ( !getStates ( ).count ( to ) ) throw AutomatonException ( "State \"" + std::to_string ( to ) + "\" doesn't exist." ); - std::pair < label::Label, regexp::UnboundedRegExpStructure > key = std::make_pair ( std::move ( from ), std::move ( input ) ); + std::pair < label::Label, regexp::UnboundedRegExpStructure < alphabet::Symbol > > key = std::make_pair ( std::move ( from ), std::move ( input ) ); return transitions[std::move ( key )].insert ( std::move ( to ) ).second; } -bool ExtendedNFA::removeTransition ( const label::Label & from, const regexp::UnboundedRegExpStructure & input, const label::Label & to ) { - std::pair < label::Label, regexp::UnboundedRegExpStructure > key = std::make_pair ( from, input ); +bool ExtendedNFA::removeTransition ( const label::Label & from, const regexp::UnboundedRegExpStructure < alphabet::Symbol > & input, const label::Label & to ) { + std::pair < label::Label, regexp::UnboundedRegExpStructure < alphabet::Symbol > > key = std::make_pair ( from, input ); return transitions[key].erase ( to ); } -const std::map < std::pair < label::Label, regexp::UnboundedRegExpStructure >, std::set < label::Label > > & ExtendedNFA::getTransitions ( ) const { +const std::map < std::pair < label::Label, regexp::UnboundedRegExpStructure < alphabet::Symbol > >, std::set < label::Label > > & ExtendedNFA::getTransitions ( ) const { return transitions; } -std::map < std::pair < label::Label, regexp::UnboundedRegExpStructure >, std::set < label::Label > > ExtendedNFA::getTransitionsFromState ( const label::Label & from ) const { +std::map < std::pair < label::Label, regexp::UnboundedRegExpStructure < alphabet::Symbol > >, std::set < label::Label > > ExtendedNFA::getTransitionsFromState ( const label::Label & from ) const { if ( !getStates ( ).count ( from ) ) throw AutomatonException ( "State \"" + std::to_string ( from ) + "\" doesn't exist" ); - std::map < std::pair < label::Label, regexp::UnboundedRegExpStructure >, std::set < label::Label > > transitionsFromState; + std::map < std::pair < label::Label, regexp::UnboundedRegExpStructure < alphabet::Symbol > >, std::set < label::Label > > transitionsFromState; - for ( const std::pair < const std::pair < label::Label, regexp::UnboundedRegExpStructure >, std::set < label::Label > > & transition : transitions ) + for ( const std::pair < const std::pair < label::Label, regexp::UnboundedRegExpStructure < alphabet::Symbol > >, std::set < label::Label > > & transition : transitions ) if ( transition.first.first == from ) transitionsFromState.insert ( make_pair ( transition.first, transition.second ) ); return transitionsFromState; } -std::map < std::pair < label::Label, regexp::UnboundedRegExpStructure >, std::set < label::Label > > ExtendedNFA::getTransitionsToState ( const label::Label & to ) const { +std::map < std::pair < label::Label, regexp::UnboundedRegExpStructure < alphabet::Symbol > >, std::set < label::Label > > ExtendedNFA::getTransitionsToState ( const label::Label & to ) const { if ( !getStates ( ).count ( to ) ) throw AutomatonException ( "State \"" + std::to_string ( to ) + "\" doesn't exist" ); - std::map < std::pair < label::Label, regexp::UnboundedRegExpStructure >, std::set < label::Label > > transitionsToState; + std::map < std::pair < label::Label, regexp::UnboundedRegExpStructure < alphabet::Symbol > >, std::set < label::Label > > transitionsToState; - for ( const std::pair < const std::pair < label::Label, regexp::UnboundedRegExpStructure >, std::set < label::Label > > & transition : transitions ) + for ( const std::pair < const std::pair < label::Label, regexp::UnboundedRegExpStructure < alphabet::Symbol > >, std::set < label::Label > > & transition : transitions ) if ( transition.second.find ( to ) != transition.second.end ( ) ) transitionsToState.insert ( make_pair ( transition.first, transition.second ) ); @@ -193,7 +193,7 @@ ExtendedNFA ExtendedNFA::parse ( std::deque < sax::Token >::iterator & input ) { void ExtendedNFA::parseTransition ( std::deque < sax::Token >::iterator & input, ExtendedNFA & automaton ) { sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "transition" ); label::Label from = AutomatonFromXMLParser::parseTransitionFrom < label::Label > ( input ); - regexp::UnboundedRegExpStructure inputRegexp = AutomatonFromXMLParser::parseTransitionInputRegexp ( input ); + regexp::UnboundedRegExpStructure < alphabet::Symbol > inputRegexp = AutomatonFromXMLParser::parseTransitionInputRegexp < alphabet::Symbol > ( input ); label::Label to = AutomatonFromXMLParser::parseTransitionTo < label::Label > ( input ); sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "transition" ); diff --git a/alib2data/src/automaton/FSM/ExtendedNFA.h b/alib2data/src/automaton/FSM/ExtendedNFA.h index 04317cbc7b..dc3486f726 100644 --- a/alib2data/src/automaton/FSM/ExtendedNFA.h +++ b/alib2data/src/automaton/FSM/ExtendedNFA.h @@ -33,7 +33,7 @@ class InitialState; class ExtendedNFA : public AutomatonBase, public std::Components < ExtendedNFA, alphabet::Symbol, std::tuple < InputAlphabet >, std::tuple < >, label::Label, std::tuple < States, FinalStates >, std::tuple < InitialState > > { protected: - std::map < std::pair < label::Label, regexp::UnboundedRegExpStructure >, std::set < label::Label > > transitions; + std::map < std::pair < label::Label, regexp::UnboundedRegExpStructure < alphabet::Symbol > >, std::set < label::Label > > transitions; public: explicit ExtendedNFA ( label::Label initialState ); @@ -115,29 +115,29 @@ public: * @param next next state * @throws AutomatonException when transition already exists or when transition contains state or symbol not present in the automaton */ - bool addTransition ( label::Label current, regexp::UnboundedRegExpStructure input, label::Label next ); + bool addTransition ( label::Label current, regexp::UnboundedRegExpStructure < alphabet::Symbol > input, label::Label next ); /** * Removes transition from the automaton. * @param transition transition to remove * @throws AutomatonException when transition doesn't exists. */ - bool removeTransition ( const label::Label & current, const regexp::UnboundedRegExpStructure & input, const label::Label & next ); + bool removeTransition ( const label::Label & current, const regexp::UnboundedRegExpStructure < alphabet::Symbol > & input, const label::Label & next ); /** * @return automaton transitions */ - const std::map < std::pair < label::Label, regexp::UnboundedRegExpStructure >, std::set < label::Label > > & getTransitions ( ) const; + const std::map < std::pair < label::Label, regexp::UnboundedRegExpStructure < alphabet::Symbol > >, std::set < label::Label > > & getTransitions ( ) const; /** * @return automaton transitions from state */ - std::map < std::pair < label::Label, regexp::UnboundedRegExpStructure >, std::set < label::Label > > getTransitionsFromState ( const label::Label & from ) const; + std::map < std::pair < label::Label, regexp::UnboundedRegExpStructure < alphabet::Symbol > >, std::set < label::Label > > getTransitionsFromState ( const label::Label & from ) const; /** * @return automaton transitions to state */ - std::map < std::pair < label::Label, regexp::UnboundedRegExpStructure >, std::set < label::Label > > getTransitionsToState ( const label::Label & from ) const; + std::map < std::pair < label::Label, regexp::UnboundedRegExpStructure < alphabet::Symbol > >, std::set < label::Label > > getTransitionsToState ( const label::Label & from ) const; virtual int compare ( const ObjectBase & other ) const { if ( std::type_index ( typeid ( * this ) ) == std::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other ); @@ -172,7 +172,7 @@ template < > class ComponentConstraint< automaton::ExtendedNFA, alphabet::Symbol, automaton::InputAlphabet > { public: static bool used ( const automaton::ExtendedNFA & automaton, const alphabet::Symbol & symbol ) { - for ( const std::pair < const std::pair < label::Label, regexp::UnboundedRegExpStructure >, std::set < label::Label > > & transition : automaton.getTransitions ( ) ) { + for ( const std::pair < const std::pair < label::Label, regexp::UnboundedRegExpStructure < alphabet::Symbol > >, std::set < label::Label > > & transition : automaton.getTransitions ( ) ) { std::set < alphabet::Symbol > alphabet = transition.first.second.getStructure ( ).computeMinimalAlphabet ( ); if ( alphabet.count ( symbol ) ) return true; @@ -199,7 +199,7 @@ public: if ( automaton.getFinalStates ( ).count ( state ) ) return true; - for ( const std::pair < const std::pair < label::Label, regexp::UnboundedRegExpStructure >, std::set < label::Label > > & transition : automaton.getTransitions ( ) ) + for ( const std::pair < const std::pair < label::Label, regexp::UnboundedRegExpStructure < alphabet::Symbol > >, std::set < label::Label > > & transition : automaton.getTransitions ( ) ) if ( ( transition.first.first == state ) || ( transition.second.find ( state ) != transition.second.end ( ) ) ) return true; diff --git a/alib2data/src/automaton/common/AutomatonFromXMLParser.cpp b/alib2data/src/automaton/common/AutomatonFromXMLParser.cpp index f7447626c8..e6aef492b0 100644 --- a/alib2data/src/automaton/common/AutomatonFromXMLParser.cpp +++ b/alib2data/src/automaton/common/AutomatonFromXMLParser.cpp @@ -32,12 +32,4 @@ Shift AutomatonFromXMLParser::parseTransitionShift ( std::deque < sax::Token >:: return shift; } -regexp::UnboundedRegExpStructure AutomatonFromXMLParser::parseTransitionInputRegexp ( std::deque < sax::Token >::iterator & input ) { - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "input" ); - regexp::UnboundedRegExpStructure result ( alib::xmlApi < regexp::UnboundedRegExpStructure >::parse ( input ) ); - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "input" ); - - return result; -} - } /* namespace automaton */ diff --git a/alib2data/src/automaton/common/AutomatonFromXMLParser.h b/alib2data/src/automaton/common/AutomatonFromXMLParser.h index 8ed3829487..7a85c96a0f 100644 --- a/alib2data/src/automaton/common/AutomatonFromXMLParser.h +++ b/alib2data/src/automaton/common/AutomatonFromXMLParser.h @@ -93,7 +93,8 @@ public: static std::vector<SymbolType> parseTransitionOutputSymbolMultiple(std::deque<sax::Token>::iterator& input); static Shift parseTransitionShift(std::deque<sax::Token>::iterator& input); - static regexp::UnboundedRegExpStructure parseTransitionInputRegexp(std::deque<sax::Token>::iterator& input); + template < class SymbolType > + static regexp::UnboundedRegExpStructure < SymbolType > parseTransitionInputRegexp(std::deque<sax::Token>::iterator& input); }; template<class T> @@ -456,6 +457,15 @@ std::vector < SymbolType > AutomatonFromXMLParser::parseTransitionOutputSymbolMu return outputSymbols; } +template < class SymbolType > +regexp::UnboundedRegExpStructure < SymbolType > AutomatonFromXMLParser::parseTransitionInputRegexp ( std::deque < sax::Token >::iterator & input ) { + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "input" ); + regexp::UnboundedRegExpStructure < SymbolType > result ( alib::xmlApi < regexp::UnboundedRegExpStructure < SymbolType > >::parse ( input ) ); + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "input" ); + + return result; +} + } /* namespace automaton */ #endif /* AUTOMATON_FROM_XML_PARSER_H_ */ diff --git a/alib2data/src/automaton/common/AutomatonToXMLComposer.cpp b/alib2data/src/automaton/common/AutomatonToXMLComposer.cpp index 0a1d851695..afdbf758e5 100644 --- a/alib2data/src/automaton/common/AutomatonToXMLComposer.cpp +++ b/alib2data/src/automaton/common/AutomatonToXMLComposer.cpp @@ -12,12 +12,6 @@ namespace automaton { -void AutomatonToXMLComposer::composeTransitionInputRegexp(std::deque<sax::Token>& out, const regexp::UnboundedRegExpStructure& regexp) { - out.emplace_back("input", sax::Token::TokenType::START_ELEMENT); - alib::xmlApi<regexp::UnboundedRegExpStructure>::compose(out, regexp); - out.emplace_back("input", sax::Token::TokenType::END_ELEMENT); -} - void AutomatonToXMLComposer::composeTransitionShift(std::deque<sax::Token>& out, Shift shift) { out.emplace_back("shift", sax::Token::TokenType::START_ELEMENT); out.emplace_back(SHIFT_NAMES [shiftToInt(shift)], sax::Token::TokenType::CHARACTER); diff --git a/alib2data/src/automaton/common/AutomatonToXMLComposer.h b/alib2data/src/automaton/common/AutomatonToXMLComposer.h index f6f30a8504..bce807c2b8 100644 --- a/alib2data/src/automaton/common/AutomatonToXMLComposer.h +++ b/alib2data/src/automaton/common/AutomatonToXMLComposer.h @@ -88,7 +88,8 @@ public: template<class SymbolType> static void composeTransitionOutputSymbolMultiple(std::deque<sax::Token>&, const std::vector<SymbolType>& symbols); - static void composeTransitionInputRegexp(std::deque<sax::Token>&, const regexp::UnboundedRegExpStructure& regexp); + template < class SymbolType > + static void composeTransitionInputRegexp(std::deque<sax::Token>&, const regexp::UnboundedRegExpStructure < SymbolType > & regexp); static void composeTransitionShift(std::deque<sax::Token>&, Shift shift); }; @@ -356,6 +357,13 @@ void AutomatonToXMLComposer::composeTransitionOutputEpsilonSymbol(std::deque<sax out.emplace_back("output", sax::Token::TokenType::END_ELEMENT); } +template < class SymbolType > +void AutomatonToXMLComposer::composeTransitionInputRegexp(std::deque<sax::Token>& out, const regexp::UnboundedRegExpStructure < SymbolType > & regexp) { + out.emplace_back("input", sax::Token::TokenType::START_ELEMENT); + alib::xmlApi<regexp::UnboundedRegExpStructure < SymbolType > >::compose(out, regexp); + out.emplace_back("input", sax::Token::TokenType::END_ELEMENT); +} + } /* namespace automaton */ #endif /* AUTOMATON_TO_XML_COMPOSER_H_ */ diff --git a/alib2data/src/regexp/RegExp.cpp b/alib2data/src/regexp/RegExp.cpp index d82f1c9e7a..de2e030836 100644 --- a/alib2data/src/regexp/RegExp.cpp +++ b/alib2data/src/regexp/RegExp.cpp @@ -31,29 +31,29 @@ regexp::RegExp regexpFrom ( const char * string ) { } regexp::RegExp regexpFrom ( std::vector < alphabet::Symbol > string ) { - regexp::UnboundedRegExpConcatenation con; + regexp::UnboundedRegExpConcatenation < alphabet::Symbol > con; for ( auto & symbol : string ) - con.appendElement ( regexp::UnboundedRegExpSymbol ( symbol ) ); + con.appendElement ( regexp::UnboundedRegExpSymbol < alphabet::Symbol > ( symbol ) ); - return regexp::RegExp { regexp::UnboundedRegExp ( regexp::UnboundedRegExpStructure ( std::move ( con ) ) ) }; + return regexp::RegExp { regexp::UnboundedRegExp < > ( regexp::UnboundedRegExpStructure < alphabet::Symbol > ( std::move ( con ) ) ) }; } regexp::RegExp regexpFrom ( string::LinearString < > string ) { - regexp::UnboundedRegExpConcatenation con; + regexp::UnboundedRegExpConcatenation < alphabet::Symbol > con; for ( auto & symbol : string.getContent ( ) ) - con.appendElement ( regexp::UnboundedRegExpSymbol ( symbol ) ); + con.appendElement ( regexp::UnboundedRegExpSymbol < alphabet::Symbol > ( symbol ) ); - return regexp::RegExp { regexp::UnboundedRegExp ( regexp::UnboundedRegExpStructure ( std::move ( con ) ) ) }; + return regexp::RegExp { regexp::UnboundedRegExp < > ( regexp::UnboundedRegExpStructure < alphabet::Symbol > ( std::move ( con ) ) ) }; } regexp::RegExp regexpFrom ( alphabet::Symbol symbol ) { - return regexp::RegExp { regexp::UnboundedRegExp ( regexp::UnboundedRegExpStructure ( regexp::UnboundedRegExpSymbol ( std::move ( symbol ) ) ) ) }; + return regexp::RegExp { regexp::UnboundedRegExp < > ( regexp::UnboundedRegExpStructure < alphabet::Symbol > ( regexp::UnboundedRegExpSymbol < alphabet::Symbol > ( std::move ( symbol ) ) ) ) }; } regexp::RegExp regexpFrom ( ) { - return regexp::RegExp { regexp::UnboundedRegExp ( regexp::UnboundedRegExpStructure ( regexp::UnboundedRegExpEmpty { } ) ) }; + return regexp::RegExp { regexp::UnboundedRegExp < > ( regexp::UnboundedRegExpStructure < alphabet::Symbol > ( regexp::UnboundedRegExpEmpty < alphabet::Symbol > { } ) ) }; } } /* namespace regexp */ diff --git a/alib2data/src/regexp/RegExpFeatures.h b/alib2data/src/regexp/RegExpFeatures.h index 71a183c4b5..05d847b5ab 100644 --- a/alib2data/src/regexp/RegExpFeatures.h +++ b/alib2data/src/regexp/RegExpFeatures.h @@ -8,6 +8,8 @@ #ifndef REG_EXP_FEATURES_H_ #define REG_EXP_FEATURES_H_ +#include "../alphabet/Symbol.h" + namespace regexp { enum class FEATURES { @@ -18,27 +20,45 @@ enum class FEATURES { class RegExp; class RegExpBase; +template < class SymbolType = alphabet::Symbol > class UnboundedRegExp; +template < class SymbolType > class UnboundedRegExpStructure; +template < class SymbolType > class UnboundedRegExpElement; +template < class SymbolType > class UnboundedRegExpAlternation; +template < class SymbolType > class UnboundedRegExpConcatenation; +template < class SymbolType > class UnboundedRegExpIteration; +template < class SymbolType > class UnboundedRegExpSymbol; +template < class SymbolType > class UnboundedRegExpEmpty; +template < class SymbolType > class UnboundedRegExpEpsilon; +template < class SymbolType = alphabet::Symbol > class FormalRegExp; +template < class SymbolType > class FormalRegExpStructure; +template < class SymbolType > class FormalRegExpElement; +template < class SymbolType > class FormalRegExpAlternation; +template < class SymbolType > class FormalRegExpConcatenation; +template < class SymbolType > class FormalRegExpIteration; +template < class SymbolType > class FormalRegExpSymbol; +template < class SymbolType > class FormalRegExpEmpty; +template < class SymbolType > class FormalRegExpEpsilon; } /* namespace regexp */ diff --git a/alib2data/src/regexp/common/RegExpFromXMLParser.cpp b/alib2data/src/regexp/common/RegExpFromXMLParser.cpp deleted file mode 100644 index 0acc850bb4..0000000000 --- a/alib2data/src/regexp/common/RegExpFromXMLParser.cpp +++ /dev/null @@ -1,172 +0,0 @@ -/* - * RegExpFromXMLParser.cpp - * - * Created on: Nov 23, 2013 - * Author: Martin Zak - */ - -#include "RegExpFromXMLParser.h" -#include <sax/FromXMLParserHelper.h> -#include <sax/ParserException.h> - -#include "../RegExp.h" -#include "../RegExpClasses.h" - -#include "../unbounded/UnboundedRegExpElements.h" -#include "../formal/FormalRegExpElements.h" -#include <core/xmlApi.hpp> - -namespace regexp { - -std::set < alphabet::Symbol > RegExpFromXMLParser::parseAlphabet ( std::deque < sax::Token >::iterator & input ) { - std::set < alphabet::Symbol > alphabet; - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "alphabet" ); - - while ( sax::FromXMLParserHelper::isTokenType ( input, sax::Token::TokenType::START_ELEMENT ) ) - alphabet.insert ( alib::xmlApi < alphabet::Symbol >::parse ( input ) ); - - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "alphabet" ); - return alphabet; -} - -std::rvalue_ref < UnboundedRegExpElement > RegExpFromXMLParser::parseUnboundedRegExpElement ( std::deque < sax::Token >::iterator & input ) { - if ( sax::FromXMLParserHelper::isToken ( input, sax::Token::TokenType::START_ELEMENT, "empty" ) ) - return parseUnboundedRegExpEmpty ( input ); - else if ( sax::FromXMLParserHelper::isToken ( input, sax::Token::TokenType::START_ELEMENT, "epsilon" ) ) - return parseUnboundedRegExpEpsilon ( input ); - else if ( sax::FromXMLParserHelper::isToken ( input, sax::Token::TokenType::START_ELEMENT, "iteration" ) ) - return parseUnboundedRegExpIteration ( input ); - else if ( sax::FromXMLParserHelper::isToken ( input, sax::Token::TokenType::START_ELEMENT, "alternation" ) ) - return parseUnboundedRegExpAlternation ( input ); - else if ( sax::FromXMLParserHelper::isToken ( input, sax::Token::TokenType::START_ELEMENT, "concatenation" ) ) - return parseUnboundedRegExpConcatenation ( input ); - else - return std::rvalue_ref < UnboundedRegExpElement > ( new UnboundedRegExpSymbol ( alib::xmlApi < alphabet::Symbol >::parse ( input ) ) ); -} - -std::rvalue_ref < UnboundedRegExpElement > RegExpFromXMLParser::parseUnboundedRegExpAlternation ( std::deque < sax::Token >::iterator & input ) { - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "alternation" ); - - UnboundedRegExpAlternation * alternation = new UnboundedRegExpAlternation ( ); - - while ( sax::FromXMLParserHelper::isTokenType ( input, sax::Token::TokenType::START_ELEMENT ) ) - alternation->appendElement ( parseUnboundedRegExpElement ( input ) ); - - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "alternation" ); - return std::rvalue_ref < UnboundedRegExpElement > ( alternation ); -} - -std::rvalue_ref < UnboundedRegExpElement > RegExpFromXMLParser::parseUnboundedRegExpConcatenation ( std::deque < sax::Token >::iterator & input ) { - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "concatenation" ); - - UnboundedRegExpConcatenation * concatenation = new UnboundedRegExpConcatenation ( ); - - while ( sax::FromXMLParserHelper::isTokenType ( input, sax::Token::TokenType::START_ELEMENT ) ) - concatenation->appendElement ( parseUnboundedRegExpElement ( input ) ); - - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "concatenation" ); - return std::rvalue_ref < UnboundedRegExpElement > ( concatenation ); -} - -std::rvalue_ref < UnboundedRegExpElement > RegExpFromXMLParser::parseUnboundedRegExpIteration ( std::deque < sax::Token >::iterator & input ) { - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "iteration" ); - - std::rvalue_ref < UnboundedRegExpElement > iteration ( new UnboundedRegExpIteration ( parseUnboundedRegExpElement ( input ) ) ); - - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "iteration" ); - - return iteration; -} - -std::rvalue_ref < UnboundedRegExpElement > RegExpFromXMLParser::parseUnboundedRegExpEpsilon ( std::deque < sax::Token >::iterator & input ) { - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "epsilon" ); - - std::rvalue_ref < UnboundedRegExpElement > epsilon ( new UnboundedRegExpEpsilon ( ) ); - - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "epsilon" ); - - return epsilon; -} - -std::rvalue_ref < UnboundedRegExpElement > RegExpFromXMLParser::parseUnboundedRegExpEmpty ( std::deque < sax::Token >::iterator & input ) { - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "empty" ); - - std::rvalue_ref < UnboundedRegExpElement > empty ( new UnboundedRegExpEmpty ( ) ); - - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "empty" ); - - return empty; -} - -std::rvalue_ref < FormalRegExpElement > RegExpFromXMLParser::parseFormalRegExpElement ( std::deque < sax::Token >::iterator & input ) { - if ( sax::FromXMLParserHelper::isToken ( input, sax::Token::TokenType::START_ELEMENT, "empty" ) ) - return parseFormalRegExpEmpty ( input ); - else if ( sax::FromXMLParserHelper::isToken ( input, sax::Token::TokenType::START_ELEMENT, "epsilon" ) ) - return parseFormalRegExpEpsilon ( input ); - else if ( sax::FromXMLParserHelper::isToken ( input, sax::Token::TokenType::START_ELEMENT, "iteration" ) ) - return parseFormalRegExpIteration ( input ); - else if ( sax::FromXMLParserHelper::isToken ( input, sax::Token::TokenType::START_ELEMENT, "alternation" ) ) - return parseFormalRegExpAlternation ( input ); - else if ( sax::FromXMLParserHelper::isToken ( input, sax::Token::TokenType::START_ELEMENT, "concatenation" ) ) - return parseFormalRegExpConcatenation ( input ); - else - return std::rvalue_ref < FormalRegExpElement > ( new FormalRegExpSymbol ( alib::xmlApi < alphabet::Symbol >::parse ( input ) ) ); -} - -std::rvalue_ref < FormalRegExpElement > RegExpFromXMLParser::parseFormalRegExpAlternation ( std::deque < sax::Token >::iterator & input ) { - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "alternation" ); - - std::rvalue_ref < FormalRegExpElement > element1 = parseFormalRegExpElement ( input ); - std::rvalue_ref < FormalRegExpElement > element2 = parseFormalRegExpElement ( input ); - - std::rvalue_ref < FormalRegExpElement > alternation ( new FormalRegExpAlternation ( std::move ( element1 ), std::move ( element2 ) ) ); - - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "alternation" ); - - return alternation; -} - -std::rvalue_ref < FormalRegExpElement > RegExpFromXMLParser::parseFormalRegExpConcatenation ( std::deque < sax::Token >::iterator & input ) { - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "concatenation" ); - - std::rvalue_ref < FormalRegExpElement > element1 = parseFormalRegExpElement ( input ); - std::rvalue_ref < FormalRegExpElement > element2 = parseFormalRegExpElement ( input ); - - std::rvalue_ref < FormalRegExpElement > concatenation ( new FormalRegExpConcatenation ( std::move ( element1 ), std::move ( element2 ) ) ); - - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "concatenation" ); - - return concatenation; -} - -std::rvalue_ref < FormalRegExpElement > RegExpFromXMLParser::parseFormalRegExpIteration ( std::deque < sax::Token >::iterator & input ) { - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "iteration" ); - - std::rvalue_ref < FormalRegExpElement > iteration ( new FormalRegExpIteration ( parseFormalRegExpElement ( input ) ) ); - - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "iteration" ); - - return iteration; -} - -std::rvalue_ref < FormalRegExpElement > RegExpFromXMLParser::parseFormalRegExpEpsilon ( std::deque < sax::Token >::iterator & input ) { - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "epsilon" ); - - std::rvalue_ref < FormalRegExpElement > epsilon ( new FormalRegExpEpsilon ( ) ); - - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "epsilon" ); - - return epsilon; -} - -std::rvalue_ref < FormalRegExpElement > RegExpFromXMLParser::parseFormalRegExpEmpty ( std::deque < sax::Token >::iterator & input ) { - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "empty" ); - - std::rvalue_ref < FormalRegExpElement > empty ( new FormalRegExpEmpty ( ) ); - - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "empty" ); - - return empty; -} - -} /* namespace regexp */ diff --git a/alib2data/src/regexp/common/RegExpFromXMLParser.h b/alib2data/src/regexp/common/RegExpFromXMLParser.h index 104ff8f0f3..9365a76695 100644 --- a/alib2data/src/regexp/common/RegExpFromXMLParser.h +++ b/alib2data/src/regexp/common/RegExpFromXMLParser.h @@ -15,6 +15,13 @@ #include "../../alphabet/SymbolFeatures.h" #include <sax/Token.h> +#include <sax/FromXMLParserHelper.h> +#include <sax/ParserException.h> + +#include "../unbounded/UnboundedRegExpElements.h" +#include "../formal/FormalRegExpElements.h" +#include <core/xmlApi.hpp> + namespace regexp { /** @@ -22,25 +29,202 @@ namespace regexp { */ class RegExpFromXMLParser { public: - static std::set < alphabet::Symbol > parseAlphabet ( std::deque < sax::Token >::iterator & input ); + template < class SymbolType > + static std::set < SymbolType > parseAlphabet ( std::deque < sax::Token >::iterator & input ); - static std::rvalue_ref < UnboundedRegExpElement > parseUnboundedRegExpElement ( std::deque < sax::Token >::iterator & input ); + template < class SymbolType > + static std::rvalue_ref < UnboundedRegExpElement < SymbolType > > parseUnboundedRegExpElement ( std::deque < sax::Token >::iterator & input ); - static std::rvalue_ref < UnboundedRegExpElement > parseUnboundedRegExpEpsilon ( std::deque < sax::Token >::iterator & input ); - static std::rvalue_ref < UnboundedRegExpElement > parseUnboundedRegExpEmpty ( std::deque < sax::Token >::iterator & input ); - static std::rvalue_ref < UnboundedRegExpElement > parseUnboundedRegExpIteration ( std::deque < sax::Token >::iterator & input ); - static std::rvalue_ref < UnboundedRegExpElement > parseUnboundedRegExpAlternation ( std::deque < sax::Token >::iterator & input ); - static std::rvalue_ref < UnboundedRegExpElement > parseUnboundedRegExpConcatenation ( std::deque < sax::Token >::iterator & input ); + template < class SymbolType > + static std::rvalue_ref < UnboundedRegExpElement < SymbolType > > parseUnboundedRegExpEpsilon ( std::deque < sax::Token >::iterator & input ); + template < class SymbolType > + static std::rvalue_ref < UnboundedRegExpElement < SymbolType > > parseUnboundedRegExpEmpty ( std::deque < sax::Token >::iterator & input ); + template < class SymbolType > + static std::rvalue_ref < UnboundedRegExpElement < SymbolType > > parseUnboundedRegExpIteration ( std::deque < sax::Token >::iterator & input ); + template < class SymbolType > + static std::rvalue_ref < UnboundedRegExpElement < SymbolType > > parseUnboundedRegExpAlternation ( std::deque < sax::Token >::iterator & input ); + template < class SymbolType > + static std::rvalue_ref < UnboundedRegExpElement < SymbolType > > parseUnboundedRegExpConcatenation ( std::deque < sax::Token >::iterator & input ); - static std::rvalue_ref < FormalRegExpElement > parseFormalRegExpElement ( std::deque < sax::Token >::iterator & input ); + template < class SymbolType > + static std::rvalue_ref < FormalRegExpElement < SymbolType > > parseFormalRegExpElement ( std::deque < sax::Token >::iterator & input ); - static std::rvalue_ref < FormalRegExpElement > parseFormalRegExpEpsilon ( std::deque < sax::Token >::iterator & input ); - static std::rvalue_ref < FormalRegExpElement > parseFormalRegExpEmpty ( std::deque < sax::Token >::iterator & input ); - static std::rvalue_ref < FormalRegExpElement > parseFormalRegExpIteration ( std::deque < sax::Token >::iterator & input ); - static std::rvalue_ref < FormalRegExpElement > parseFormalRegExpAlternation ( std::deque < sax::Token >::iterator & input ); - static std::rvalue_ref < FormalRegExpElement > parseFormalRegExpConcatenation ( std::deque < sax::Token >::iterator & input ); + template < class SymbolType > + static std::rvalue_ref < FormalRegExpElement < SymbolType > > parseFormalRegExpEpsilon ( std::deque < sax::Token >::iterator & input ); + template < class SymbolType > + static std::rvalue_ref < FormalRegExpElement < SymbolType > > parseFormalRegExpEmpty ( std::deque < sax::Token >::iterator & input ); + template < class SymbolType > + static std::rvalue_ref < FormalRegExpElement < SymbolType > > parseFormalRegExpIteration ( std::deque < sax::Token >::iterator & input ); + template < class SymbolType > + static std::rvalue_ref < FormalRegExpElement < SymbolType > > parseFormalRegExpAlternation ( std::deque < sax::Token >::iterator & input ); + template < class SymbolType > + static std::rvalue_ref < FormalRegExpElement < SymbolType > > parseFormalRegExpConcatenation ( std::deque < sax::Token >::iterator & input ); }; +template < class SymbolType > +std::set < SymbolType > RegExpFromXMLParser::parseAlphabet ( std::deque < sax::Token >::iterator & input ) { + std::set < SymbolType > alphabet; + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "alphabet" ); + + while ( sax::FromXMLParserHelper::isTokenType ( input, sax::Token::TokenType::START_ELEMENT ) ) + alphabet.insert ( alib::xmlApi < SymbolType >::parse ( input ) ); + + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "alphabet" ); + return alphabet; +} + +template < class SymbolType > +std::rvalue_ref < UnboundedRegExpElement < SymbolType > > RegExpFromXMLParser::parseUnboundedRegExpElement ( std::deque < sax::Token >::iterator & input ) { + if ( sax::FromXMLParserHelper::isToken ( input, sax::Token::TokenType::START_ELEMENT, "empty" ) ) + return parseUnboundedRegExpEmpty < SymbolType > ( input ); + else if ( sax::FromXMLParserHelper::isToken ( input, sax::Token::TokenType::START_ELEMENT, "epsilon" ) ) + return parseUnboundedRegExpEpsilon < SymbolType > ( input ); + else if ( sax::FromXMLParserHelper::isToken ( input, sax::Token::TokenType::START_ELEMENT, "iteration" ) ) + return parseUnboundedRegExpIteration < SymbolType > ( input ); + else if ( sax::FromXMLParserHelper::isToken ( input, sax::Token::TokenType::START_ELEMENT, "alternation" ) ) + return parseUnboundedRegExpAlternation < SymbolType > ( input ); + else if ( sax::FromXMLParserHelper::isToken ( input, sax::Token::TokenType::START_ELEMENT, "concatenation" ) ) + return parseUnboundedRegExpConcatenation < SymbolType > ( input ); + else + return std::rvalue_ref < UnboundedRegExpElement < SymbolType > > ( new UnboundedRegExpSymbol < SymbolType > ( alib::xmlApi < SymbolType >::parse ( input ) ) ); +} + +template < class SymbolType > +std::rvalue_ref < UnboundedRegExpElement < SymbolType > > RegExpFromXMLParser::parseUnboundedRegExpAlternation ( std::deque < sax::Token >::iterator & input ) { + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "alternation" ); + + UnboundedRegExpAlternation < SymbolType > * alternation = new UnboundedRegExpAlternation < SymbolType > ( ); + + while ( sax::FromXMLParserHelper::isTokenType ( input, sax::Token::TokenType::START_ELEMENT ) ) + alternation->appendElement ( parseUnboundedRegExpElement < SymbolType > ( input ) ); + + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "alternation" ); + return std::rvalue_ref < UnboundedRegExpElement < SymbolType > > ( alternation ); +} + +template < class SymbolType > +std::rvalue_ref < UnboundedRegExpElement < SymbolType > > RegExpFromXMLParser::parseUnboundedRegExpConcatenation ( std::deque < sax::Token >::iterator & input ) { + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "concatenation" ); + + UnboundedRegExpConcatenation < SymbolType > * concatenation = new UnboundedRegExpConcatenation < SymbolType > ( ); + + while ( sax::FromXMLParserHelper::isTokenType ( input, sax::Token::TokenType::START_ELEMENT ) ) + concatenation->appendElement ( parseUnboundedRegExpElement < SymbolType > ( input ) ); + + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "concatenation" ); + return std::rvalue_ref < UnboundedRegExpElement < SymbolType > > ( concatenation ); +} + +template < class SymbolType > +std::rvalue_ref < UnboundedRegExpElement < SymbolType > > RegExpFromXMLParser::parseUnboundedRegExpIteration ( std::deque < sax::Token >::iterator & input ) { + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "iteration" ); + + std::rvalue_ref < UnboundedRegExpElement < SymbolType > > iteration ( new UnboundedRegExpIteration < SymbolType > ( parseUnboundedRegExpElement < SymbolType > ( input ) ) ); + + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "iteration" ); + + return iteration; +} + +template < class SymbolType > +std::rvalue_ref < UnboundedRegExpElement < SymbolType > > RegExpFromXMLParser::parseUnboundedRegExpEpsilon ( std::deque < sax::Token >::iterator & input ) { + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "epsilon" ); + + std::rvalue_ref < UnboundedRegExpElement < SymbolType > > epsilon ( new UnboundedRegExpEpsilon < SymbolType > ( ) ); + + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "epsilon" ); + + return epsilon; +} + +template < class SymbolType > +std::rvalue_ref < UnboundedRegExpElement < SymbolType > > RegExpFromXMLParser::parseUnboundedRegExpEmpty ( std::deque < sax::Token >::iterator & input ) { + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "empty" ); + + std::rvalue_ref < UnboundedRegExpElement < SymbolType > > empty ( new UnboundedRegExpEmpty < SymbolType > ( ) ); + + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "empty" ); + + return empty; +} + +template < class SymbolType > +std::rvalue_ref < FormalRegExpElement < SymbolType > > RegExpFromXMLParser::parseFormalRegExpElement ( std::deque < sax::Token >::iterator & input ) { + if ( sax::FromXMLParserHelper::isToken ( input, sax::Token::TokenType::START_ELEMENT, "empty" ) ) + return parseFormalRegExpEmpty < SymbolType > ( input ); + else if ( sax::FromXMLParserHelper::isToken ( input, sax::Token::TokenType::START_ELEMENT, "epsilon" ) ) + return parseFormalRegExpEpsilon < SymbolType > ( input ); + else if ( sax::FromXMLParserHelper::isToken ( input, sax::Token::TokenType::START_ELEMENT, "iteration" ) ) + return parseFormalRegExpIteration < SymbolType > ( input ); + else if ( sax::FromXMLParserHelper::isToken ( input, sax::Token::TokenType::START_ELEMENT, "alternation" ) ) + return parseFormalRegExpAlternation < SymbolType > ( input ); + else if ( sax::FromXMLParserHelper::isToken ( input, sax::Token::TokenType::START_ELEMENT, "concatenation" ) ) + return parseFormalRegExpConcatenation < SymbolType > ( input ); + else + return std::rvalue_ref < FormalRegExpElement < SymbolType > > ( new FormalRegExpSymbol < SymbolType > ( alib::xmlApi < SymbolType >::parse ( input ) ) ); +} + +template < class SymbolType > +std::rvalue_ref < FormalRegExpElement < SymbolType > > RegExpFromXMLParser::parseFormalRegExpAlternation ( std::deque < sax::Token >::iterator & input ) { + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "alternation" ); + + std::rvalue_ref < FormalRegExpElement < SymbolType > > element1 = parseFormalRegExpElement < SymbolType > ( input ); + std::rvalue_ref < FormalRegExpElement < SymbolType > > element2 = parseFormalRegExpElement < SymbolType > ( input ); + + std::rvalue_ref < FormalRegExpElement < SymbolType > > alternation ( new FormalRegExpAlternation < SymbolType > ( std::move ( element1 ), std::move ( element2 ) ) ); + + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "alternation" ); + + return alternation; +} + +template < class SymbolType > +std::rvalue_ref < FormalRegExpElement < SymbolType > > RegExpFromXMLParser::parseFormalRegExpConcatenation ( std::deque < sax::Token >::iterator & input ) { + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "concatenation" ); + + std::rvalue_ref < FormalRegExpElement < SymbolType > > element1 = parseFormalRegExpElement < SymbolType > ( input ); + std::rvalue_ref < FormalRegExpElement < SymbolType > > element2 = parseFormalRegExpElement < SymbolType > ( input ); + + std::rvalue_ref < FormalRegExpElement < SymbolType > > concatenation ( new FormalRegExpConcatenation < SymbolType > ( std::move ( element1 ), std::move ( element2 ) ) ); + + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "concatenation" ); + + return concatenation; +} + +template < class SymbolType > +std::rvalue_ref < FormalRegExpElement < SymbolType > > RegExpFromXMLParser::parseFormalRegExpIteration ( std::deque < sax::Token >::iterator & input ) { + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "iteration" ); + + std::rvalue_ref < FormalRegExpElement < SymbolType > > iteration ( new FormalRegExpIteration < SymbolType > ( parseFormalRegExpElement < SymbolType > ( input ) ) ); + + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "iteration" ); + + return iteration; +} + +template < class SymbolType > +std::rvalue_ref < FormalRegExpElement < SymbolType > > RegExpFromXMLParser::parseFormalRegExpEpsilon ( std::deque < sax::Token >::iterator & input ) { + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "epsilon" ); + + std::rvalue_ref < FormalRegExpElement < SymbolType > > epsilon ( new FormalRegExpEpsilon < SymbolType > ( ) ); + + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "epsilon" ); + + return epsilon; +} + +template < class SymbolType > +std::rvalue_ref < FormalRegExpElement < SymbolType > > RegExpFromXMLParser::parseFormalRegExpEmpty ( std::deque < sax::Token >::iterator & input ) { + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "empty" ); + + std::rvalue_ref < FormalRegExpElement < SymbolType > > empty ( new FormalRegExpEmpty < SymbolType > ( ) ); + + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "empty" ); + + return empty; +} + } /* namespace regexp */ #endif /* REG_EXP_FROM_XML_PARSER_H_ */ diff --git a/alib2data/src/regexp/common/RegExpToXMLComposer.cpp b/alib2data/src/regexp/common/RegExpToXMLComposer.cpp deleted file mode 100644 index a985ff877e..0000000000 --- a/alib2data/src/regexp/common/RegExpToXMLComposer.cpp +++ /dev/null @@ -1,97 +0,0 @@ -/* - * RegExpToXMLComposer.cpp - * - * Created on: Nov 23, 2013 - * Author: Martin Zak - */ - -#include "RegExpToXMLComposer.h" - -#include "../RegExp.h" -#include "../RegExpClasses.h" - -#include "../unbounded/UnboundedRegExpElements.h" -#include "../formal/FormalRegExpElements.h" -#include <core/xmlApi.hpp> - -namespace regexp { - -void RegExpToXMLComposer::Unbounded::visit(const UnboundedRegExpAlternation& alternation, std::deque<sax::Token>& out) { - out.emplace_back("alternation", sax::Token::TokenType::START_ELEMENT); - for (const auto& element : alternation.getElements()) { - element->accept < void, RegExpToXMLComposer::Unbounded > ( out ); - } - out.emplace_back("alternation", sax::Token::TokenType::END_ELEMENT); -} - -void RegExpToXMLComposer::Unbounded::visit(const UnboundedRegExpConcatenation& concatenation, std::deque<sax::Token>& out) { - out.emplace_back("concatenation", sax::Token::TokenType::START_ELEMENT); - for (auto element : concatenation.getElements()) { - element->accept < void, RegExpToXMLComposer::Unbounded > ( out ); - } - out.emplace_back("concatenation", sax::Token::TokenType::END_ELEMENT); -} - -void RegExpToXMLComposer::Unbounded::visit(const UnboundedRegExpIteration& iteration, std::deque<sax::Token>& out) { - out.emplace_back("iteration", sax::Token::TokenType::START_ELEMENT); - iteration.getElement().accept < void, RegExpToXMLComposer::Unbounded > ( out ); - out.emplace_back("iteration", sax::Token::TokenType::END_ELEMENT); -} - -void RegExpToXMLComposer::Unbounded::visit(const UnboundedRegExpSymbol& symbol, std::deque<sax::Token>& out) { - alib::xmlApi<alphabet::Symbol>::compose(out, symbol.getSymbol()); -} - -void RegExpToXMLComposer::Unbounded::visit(const UnboundedRegExpEpsilon&, std::deque<sax::Token>& out) { - out.emplace_back("epsilon", sax::Token::TokenType::START_ELEMENT); - out.emplace_back("epsilon", sax::Token::TokenType::END_ELEMENT); -} - -void RegExpToXMLComposer::Unbounded::visit(const UnboundedRegExpEmpty&, std::deque<sax::Token>& out) { - out.emplace_back("empty", sax::Token::TokenType::START_ELEMENT); - out.emplace_back("empty", sax::Token::TokenType::END_ELEMENT); -} - -void RegExpToXMLComposer::Formal::visit(const FormalRegExpAlternation& alternation, std::deque<sax::Token>& out) { - out.emplace_back("alternation", sax::Token::TokenType::START_ELEMENT); - alternation.getLeftElement().accept < void, RegExpToXMLComposer::Formal > ( out ); - alternation.getRightElement().accept < void, RegExpToXMLComposer::Formal > ( out ); - out.emplace_back("alternation", sax::Token::TokenType::END_ELEMENT); -} - -void RegExpToXMLComposer::Formal::visit(const FormalRegExpConcatenation& concatenation, std::deque<sax::Token>& out) { - out.emplace_back("concatenation", sax::Token::TokenType::START_ELEMENT); - concatenation.getLeftElement().accept < void, RegExpToXMLComposer::Formal > ( out ); - concatenation.getRightElement().accept < void, RegExpToXMLComposer::Formal > ( out ); - out.emplace_back("concatenation", sax::Token::TokenType::END_ELEMENT); -} - -void RegExpToXMLComposer::Formal::visit(const FormalRegExpIteration& iteration, std::deque<sax::Token>& out) { - out.emplace_back("iteration", sax::Token::TokenType::START_ELEMENT); - iteration.getElement().accept < void, RegExpToXMLComposer::Formal > ( out ); - out.emplace_back("iteration", sax::Token::TokenType::END_ELEMENT); -} - -void RegExpToXMLComposer::Formal::visit(const FormalRegExpSymbol& symbol, std::deque<sax::Token>& out) { - alib::xmlApi<alphabet::Symbol>::compose(out, symbol.getSymbol()); -} - -void RegExpToXMLComposer::Formal::visit(const FormalRegExpEpsilon&, std::deque<sax::Token>& out) { - out.emplace_back("epsilon", sax::Token::TokenType::START_ELEMENT); - out.emplace_back("epsilon", sax::Token::TokenType::END_ELEMENT); -} - -void RegExpToXMLComposer::Formal::visit(const FormalRegExpEmpty&, std::deque<sax::Token>& out) { - out.emplace_back("empty", sax::Token::TokenType::START_ELEMENT); - out.emplace_back("empty", sax::Token::TokenType::END_ELEMENT); -} - -void RegExpToXMLComposer::composeAlphabet(std::deque<sax::Token>& out, const std::set<alphabet::Symbol>& alphabet) { - out.emplace_back("alphabet", sax::Token::TokenType::START_ELEMENT); - for (const auto& symbol : alphabet) { - alib::xmlApi<alphabet::Symbol>::compose(out, symbol); - } - out.emplace_back("alphabet", sax::Token::TokenType::END_ELEMENT); -} - -} /* namespace regexp */ diff --git a/alib2data/src/regexp/common/RegExpToXMLComposer.h b/alib2data/src/regexp/common/RegExpToXMLComposer.h index 09f64ac523..54fde9b71c 100644 --- a/alib2data/src/regexp/common/RegExpToXMLComposer.h +++ b/alib2data/src/regexp/common/RegExpToXMLComposer.h @@ -14,6 +14,10 @@ #include "../formal/FormalRegExpElement.h" #include <sax/Token.h> +#include "../unbounded/UnboundedRegExpElements.h" +#include "../formal/FormalRegExpElements.h" +#include <core/xmlApi.hpp> + namespace regexp { /** @@ -21,29 +25,133 @@ namespace regexp { */ class RegExpToXMLComposer { public: - static void composeAlphabet(std::deque<sax::Token>& out, const std::set<alphabet::Symbol>& alphabet); + template < class SymbolType > + static void composeAlphabet(std::deque<sax::Token>& out, const std::set<SymbolType>& alphabet); class Unbounded { public: - static void visit( const UnboundedRegExpAlternation& alternation, std::deque < sax::Token > & output); - static void visit( const UnboundedRegExpConcatenation& concatenation, std::deque < sax::Token > & output); - static void visit( const UnboundedRegExpIteration& iteration, std::deque < sax::Token > & output); - static void visit( const UnboundedRegExpSymbol& symbol, std::deque < sax::Token > & output); - static void visit( const UnboundedRegExpEpsilon& epsilon, std::deque < sax::Token > & output); - static void visit( const UnboundedRegExpEmpty& empty, std::deque < sax::Token > & output); + template < class SymbolType > + static void visit( const UnboundedRegExpAlternation < SymbolType > & alternation, std::deque < sax::Token > & output); + template < class SymbolType > + static void visit( const UnboundedRegExpConcatenation < SymbolType > & concatenation, std::deque < sax::Token > & output); + template < class SymbolType > + static void visit( const UnboundedRegExpIteration < SymbolType > & iteration, std::deque < sax::Token > & output); + template < class SymbolType > + static void visit( const UnboundedRegExpSymbol < SymbolType > & symbol, std::deque < sax::Token > & output); + template < class SymbolType > + static void visit( const UnboundedRegExpEpsilon < SymbolType > & epsilon, std::deque < sax::Token > & output); + template < class SymbolType > + static void visit( const UnboundedRegExpEmpty < SymbolType > & empty, std::deque < sax::Token > & output); }; class Formal { public: - static void visit( const FormalRegExpAlternation& alternation, std::deque < sax::Token > & output); - static void visit( const FormalRegExpConcatenation& concatenation, std::deque < sax::Token > & output); - static void visit( const FormalRegExpIteration& iteration, std::deque < sax::Token > & output); - static void visit( const FormalRegExpSymbol& symbol, std::deque < sax::Token > & output); - static void visit( const FormalRegExpEpsilon& epsilon, std::deque < sax::Token > & output); - static void visit( const FormalRegExpEmpty& empty, std::deque < sax::Token > & output); + template < class SymbolType > + static void visit( const FormalRegExpAlternation < SymbolType > & alternation, std::deque < sax::Token > & output); + template < class SymbolType > + static void visit( const FormalRegExpConcatenation < SymbolType > & concatenation, std::deque < sax::Token > & output); + template < class SymbolType > + static void visit( const FormalRegExpIteration < SymbolType > & iteration, std::deque < sax::Token > & output); + template < class SymbolType > + static void visit( const FormalRegExpSymbol < SymbolType > & symbol, std::deque < sax::Token > & output); + template < class SymbolType > + static void visit( const FormalRegExpEpsilon < SymbolType > & epsilon, std::deque < sax::Token > & output); + template < class SymbolType > + static void visit( const FormalRegExpEmpty < SymbolType > & empty, std::deque < sax::Token > & output); }; }; +template < class SymbolType > +void RegExpToXMLComposer::Unbounded::visit(const UnboundedRegExpAlternation < SymbolType > & alternation, std::deque<sax::Token>& out) { + out.emplace_back("alternation", sax::Token::TokenType::START_ELEMENT); + for (const auto& element : alternation.getElements()) { + element->template accept < void, RegExpToXMLComposer::Unbounded > ( out ); + } + out.emplace_back("alternation", sax::Token::TokenType::END_ELEMENT); +} + +template < class SymbolType > +void RegExpToXMLComposer::Unbounded::visit(const UnboundedRegExpConcatenation < SymbolType > & concatenation, std::deque<sax::Token>& out) { + out.emplace_back("concatenation", sax::Token::TokenType::START_ELEMENT); + for (auto element : concatenation.getElements()) { + element->template accept < void, RegExpToXMLComposer::Unbounded > ( out ); + } + out.emplace_back("concatenation", sax::Token::TokenType::END_ELEMENT); +} + +template < class SymbolType > +void RegExpToXMLComposer::Unbounded::visit(const UnboundedRegExpIteration < SymbolType > & iteration, std::deque<sax::Token>& out) { + out.emplace_back("iteration", sax::Token::TokenType::START_ELEMENT); + iteration.getElement().template accept < void, RegExpToXMLComposer::Unbounded > ( out ); + out.emplace_back("iteration", sax::Token::TokenType::END_ELEMENT); +} + +template < class SymbolType > +void RegExpToXMLComposer::Unbounded::visit(const UnboundedRegExpSymbol < SymbolType > & symbol, std::deque<sax::Token>& out) { + alib::xmlApi<SymbolType>::compose(out, symbol.getSymbol()); +} + +template < class SymbolType > +void RegExpToXMLComposer::Unbounded::visit(const UnboundedRegExpEpsilon < SymbolType > &, std::deque<sax::Token>& out) { + out.emplace_back("epsilon", sax::Token::TokenType::START_ELEMENT); + out.emplace_back("epsilon", sax::Token::TokenType::END_ELEMENT); +} + +template < class SymbolType > +void RegExpToXMLComposer::Unbounded::visit(const UnboundedRegExpEmpty < SymbolType > &, std::deque<sax::Token>& out) { + out.emplace_back("empty", sax::Token::TokenType::START_ELEMENT); + out.emplace_back("empty", sax::Token::TokenType::END_ELEMENT); +} + +template < class SymbolType > +void RegExpToXMLComposer::Formal::visit(const FormalRegExpAlternation < SymbolType > & alternation, std::deque<sax::Token>& out) { + out.emplace_back("alternation", sax::Token::TokenType::START_ELEMENT); + alternation.getLeftElement().template accept < void, RegExpToXMLComposer::Formal > ( out ); + alternation.getRightElement().template accept < void, RegExpToXMLComposer::Formal > ( out ); + out.emplace_back("alternation", sax::Token::TokenType::END_ELEMENT); +} + +template < class SymbolType > +void RegExpToXMLComposer::Formal::visit(const FormalRegExpConcatenation < SymbolType > & concatenation, std::deque<sax::Token>& out) { + out.emplace_back("concatenation", sax::Token::TokenType::START_ELEMENT); + concatenation.getLeftElement().template accept < void, RegExpToXMLComposer::Formal > ( out ); + concatenation.getRightElement().template accept < void, RegExpToXMLComposer::Formal > ( out ); + out.emplace_back("concatenation", sax::Token::TokenType::END_ELEMENT); +} + +template < class SymbolType > +void RegExpToXMLComposer::Formal::visit(const FormalRegExpIteration < SymbolType > & iteration, std::deque<sax::Token>& out) { + out.emplace_back("iteration", sax::Token::TokenType::START_ELEMENT); + iteration.getElement().template accept < void, RegExpToXMLComposer::Formal > ( out ); + out.emplace_back("iteration", sax::Token::TokenType::END_ELEMENT); +} + +template < class SymbolType > +void RegExpToXMLComposer::Formal::visit(const FormalRegExpSymbol < SymbolType > & symbol, std::deque<sax::Token>& out) { + alib::xmlApi<SymbolType>::compose(out, symbol.getSymbol()); +} + +template < class SymbolType > +void RegExpToXMLComposer::Formal::visit(const FormalRegExpEpsilon < SymbolType > &, std::deque<sax::Token>& out) { + out.emplace_back("epsilon", sax::Token::TokenType::START_ELEMENT); + out.emplace_back("epsilon", sax::Token::TokenType::END_ELEMENT); +} + +template < class SymbolType > +void RegExpToXMLComposer::Formal::visit(const FormalRegExpEmpty < SymbolType > &, std::deque<sax::Token>& out) { + out.emplace_back("empty", sax::Token::TokenType::START_ELEMENT); + out.emplace_back("empty", sax::Token::TokenType::END_ELEMENT); +} + +template < class SymbolType > +void RegExpToXMLComposer::composeAlphabet(std::deque<sax::Token>& out, const std::set<SymbolType>& alphabet) { + out.emplace_back("alphabet", sax::Token::TokenType::START_ELEMENT); + for (const auto& symbol : alphabet) { + alib::xmlApi<SymbolType>::compose(out, symbol); + } + out.emplace_back("alphabet", sax::Token::TokenType::END_ELEMENT); +} + } /* namespace regexp */ #endif /* REG_EXP_TO_XML_COMPOSER_H_ */ diff --git a/alib2data/src/regexp/formal/FormalRegExp.cpp b/alib2data/src/regexp/formal/FormalRegExp.cpp index 49962d3a23..7a64ab1482 100644 --- a/alib2data/src/regexp/formal/FormalRegExp.cpp +++ b/alib2data/src/regexp/formal/FormalRegExp.cpp @@ -6,109 +6,18 @@ */ #include "FormalRegExp.h" -#include <exception/CommonException.h> -#include "FormalRegExpEmpty.h" - -#include <iostream> -#include <algorithm> -#include <sstream> - -#include "../unbounded/UnboundedRegExp.h" - -#include <sax/FromXMLParserHelper.h> -#include "../common/RegExpFromXMLParser.h" -#include "../common/RegExpToXMLComposer.h" #include "../RegExp.h" #include <object/Object.h> #include <core/xmlApi.hpp> -#include <core/castApi.hpp> - -namespace regexp { - -FormalRegExp::FormalRegExp ( std::set < alphabet::Symbol > alphabet, FormalRegExpStructure regExp ) : std::Components < FormalRegExp, alphabet::Symbol, std::tuple < GeneralAlphabet >, std::tuple < > > ( std::make_tuple ( std::move ( alphabet ) ), std::tuple < > ( ) ), regExp ( std::move ( regExp ) ) { - if ( !this->regExp.getStructure ( ).checkAlphabet ( getAlphabet ( ) ) ) - throw exception::CommonException ( "Input symbols not in the alphabet." ); -} - -FormalRegExp::FormalRegExp ( ) : FormalRegExp ( std::set < alphabet::Symbol > ( ), FormalRegExpStructure ( ) ) { -} - -FormalRegExp::FormalRegExp ( FormalRegExpStructure regExp ) : FormalRegExp ( regExp.getStructure ( ).computeMinimalAlphabet ( ), regExp ) { -} - -FormalRegExp::FormalRegExp ( const UnboundedRegExp & other ) : FormalRegExp ( other.getAlphabet ( ), FormalRegExpStructure ( other.getRegExp ( ) ) ) { -} - -RegExpBase * FormalRegExp::clone ( ) const { - return new FormalRegExp ( * this ); -} - -RegExpBase * FormalRegExp::plunder ( ) && { - return new FormalRegExp ( std::move ( * this ) ); -} - -const FormalRegExpStructure & FormalRegExp::getRegExp ( ) const { - return regExp; -} - -FormalRegExpStructure & FormalRegExp::getRegExp ( ) { - return regExp; -} - -void FormalRegExp::setRegExp ( FormalRegExpStructure param ) { - if ( !param.getStructure ( ).checkAlphabet ( getAlphabet ( ) ) ) - throw exception::CommonException ( "Input symbols not in the alphabet." ); - this->regExp = std::move ( param ); -} - -void FormalRegExp::operator >>( std::ostream & out ) const { - out << "(FormalRegExp " << this->regExp.getStructure ( ) << ")"; -} - -int FormalRegExp::compare ( const FormalRegExp & other ) const { - auto first = std::tie ( regExp.getStructure ( ), getAlphabet ( ) ); - auto second = std::tie ( other.regExp.getStructure ( ), other.getAlphabet ( ) ); - - std::compare < decltype ( first ) > comp; - - return comp ( first, second ); -} - -FormalRegExp::operator std::string ( ) const { - std::stringstream ss; - ss << * this; - return ss.str ( ); -} - -FormalRegExp FormalRegExp::parse ( std::deque < sax::Token >::iterator & input ) { - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, FormalRegExp::getXmlTagName() ); - - std::set < alphabet::Symbol > alphabet = RegExpFromXMLParser::parseAlphabet ( input ); - - FormalRegExpStructure element ( alib::xmlApi < regexp::FormalRegExpStructure >::parse ( input ) ); - FormalRegExp regexp ( std::move ( alphabet ), std::move ( element ) ); - - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, FormalRegExp::getXmlTagName() ); - - return regexp; -} - -void FormalRegExp::compose ( std::deque < sax::Token > & out ) const { - out.emplace_back ( FormalRegExp::getXmlTagName(), sax::Token::TokenType::START_ELEMENT ); - RegExpToXMLComposer::composeAlphabet ( out, getAlphabet ( ) ); - alib::xmlApi < FormalRegExpStructure >::compose ( out, this->getRegExp ( ) ); - out.emplace_back ( FormalRegExp::getXmlTagName(), sax::Token::TokenType::END_ELEMENT ); -} - -} /* namespace regexp */ +#include <core/castApi.hpp> namespace alib { -auto formalRegExpParserRegister = xmlApi < regexp::RegExp >::ParserRegister < regexp::FormalRegExp > ( ); -auto formalRegExpParserRegister2 = xmlApi < alib::Object >::ParserRegister < regexp::FormalRegExp > ( ); +auto formalRegExpParserRegister = xmlApi < regexp::RegExp >::ParserRegister < regexp::FormalRegExp < > > ( ); +auto formalRegExpParserRegister2 = xmlApi < alib::Object >::ParserRegister < regexp::FormalRegExp < > > ( ); -auto FormalRegExpFromUnboundedRegExp = castApi::CastRegister < regexp::FormalRegExp, regexp::UnboundedRegExp > ( ); -auto FormalRegExpCastBinder = castApi::CastPoolStringBinder < regexp::FormalRegExp > ( regexp::FormalRegExp::getXmlTagName() ); +auto FormalRegExpFromUnboundedRegExp = castApi::CastRegister < regexp::FormalRegExp < >, regexp::UnboundedRegExp < > > ( ); +auto FormalRegExpCastBinder = castApi::CastPoolStringBinder < regexp::FormalRegExp < > > ( regexp::FormalRegExp < >::getXmlTagName() ); } /* namespace alib */ diff --git a/alib2data/src/regexp/formal/FormalRegExp.h b/alib2data/src/regexp/formal/FormalRegExp.h index bd320398d7..2f9615ecaa 100644 --- a/alib2data/src/regexp/formal/FormalRegExp.h +++ b/alib2data/src/regexp/formal/FormalRegExp.h @@ -10,24 +10,34 @@ #include <string> #include <set> +#include <iostream> +#include <algorithm> +#include <sstream> + #include <core/components.hpp> +#include <sax/FromXMLParserHelper.h> +#include <exception/CommonException.h> #include "../RegExpBase.h" +#include "../RegExpFeatures.h" +#include "../common/RegExpFromXMLParser.h" +#include "../common/RegExpToXMLComposer.h" + #include "FormalRegExpStructure.h" -#include "../../alphabet/Symbol.h" +#include "FormalRegExpEmpty.h" namespace regexp { -class UnboundedRegExp; class GeneralAlphabet; /** * Represents regular expression parsed from the XML. Regular expression is stored * as a tree of RegExpElement. */ -class FormalRegExp : public RegExpBase, public std::Components < FormalRegExp, alphabet::Symbol, std::tuple < GeneralAlphabet >, std::tuple < > > { +template < class SymbolType > +class FormalRegExp : public RegExpBase, public std::Components < FormalRegExp < SymbolType >, SymbolType, std::tuple < GeneralAlphabet >, std::tuple < > > { protected: - FormalRegExpStructure regExp; + FormalRegExpStructure < SymbolType > regExp; public: /** @@ -42,28 +52,28 @@ public: explicit FormalRegExp ( ); - explicit FormalRegExp ( std::set < alphabet::Symbol > alphabet, FormalRegExpStructure regExp ); - explicit FormalRegExp ( FormalRegExpStructure regExp ); - explicit FormalRegExp ( const UnboundedRegExp & other ); + explicit FormalRegExp ( std::set < SymbolType > alphabet, FormalRegExpStructure < SymbolType > regExp ); + explicit FormalRegExp ( FormalRegExpStructure < SymbolType > regExp ); + explicit FormalRegExp ( const UnboundedRegExp < SymbolType > & other ); /** * @return Root node of the regular expression tree */ - const FormalRegExpStructure & getRegExp ( ) const; + const FormalRegExpStructure < SymbolType > & getRegExp ( ) const; /** * @return Root node of the regular expression tree */ - FormalRegExpStructure & getRegExp ( ); + FormalRegExpStructure < SymbolType > & getRegExp ( ); /** * Sets the root node of the regular expression tree * @param regExp root node to set */ - void setRegExp ( FormalRegExpStructure regExp ); + void setRegExp ( FormalRegExpStructure < SymbolType > regExp ); - virtual const std::set < alphabet::Symbol > & getAlphabet ( ) const { - return accessComponent < GeneralAlphabet > ( ).get ( ); + virtual const std::set < SymbolType > & getAlphabet ( ) const { + return this->template accessComponent < GeneralAlphabet > ( ).get ( ); } /** @@ -96,20 +106,116 @@ public: } /* namespace regexp */ +#include "../unbounded/UnboundedRegExp.h" + +namespace regexp { + +template < class SymbolType > +FormalRegExp < SymbolType >::FormalRegExp ( std::set < SymbolType > alphabet, FormalRegExpStructure < SymbolType > regExp ) : std::Components < FormalRegExp < SymbolType >, SymbolType, std::tuple < GeneralAlphabet >, std::tuple < > > ( std::make_tuple ( std::move ( alphabet ) ), std::tuple < > ( ) ), regExp ( std::move ( regExp ) ) { + if ( !this->regExp.getStructure ( ).checkAlphabet ( getAlphabet ( ) ) ) + throw exception::CommonException ( "Input symbols not in the alphabet." ); +} + +template < class SymbolType > +FormalRegExp < SymbolType >::FormalRegExp ( ) : FormalRegExp ( std::set < SymbolType > ( ), FormalRegExpStructure < SymbolType > ( ) ) { +} + +template < class SymbolType > +FormalRegExp < SymbolType >::FormalRegExp ( FormalRegExpStructure < SymbolType > regExp ) : FormalRegExp ( regExp.getStructure ( ).computeMinimalAlphabet ( ), regExp ) { +} + +template < class SymbolType > +FormalRegExp < SymbolType >::FormalRegExp ( const UnboundedRegExp < SymbolType > & other ) : FormalRegExp ( other.getAlphabet ( ), FormalRegExpStructure < SymbolType > ( other.getRegExp ( ) ) ) { +} + +template < class SymbolType > +RegExpBase * FormalRegExp < SymbolType >::clone ( ) const { + return new FormalRegExp ( * this ); +} + +template < class SymbolType > +RegExpBase * FormalRegExp < SymbolType >::plunder ( ) && { + return new FormalRegExp ( std::move ( * this ) ); +} + +template < class SymbolType > +const FormalRegExpStructure < SymbolType > & FormalRegExp < SymbolType >::getRegExp ( ) const { + return regExp; +} + +template < class SymbolType > +FormalRegExpStructure < SymbolType > & FormalRegExp < SymbolType >::getRegExp ( ) { + return regExp; +} + +template < class SymbolType > +void FormalRegExp < SymbolType >::setRegExp ( FormalRegExpStructure < SymbolType > param ) { + if ( !param.getStructure ( ).checkAlphabet ( getAlphabet ( ) ) ) + throw exception::CommonException ( "Input symbols not in the alphabet." ); + + this->regExp = std::move ( param ); +} + +template < class SymbolType > +void FormalRegExp < SymbolType >::operator >>( std::ostream & out ) const { + out << "(FormalRegExp " << this->regExp.getStructure ( ) << ")"; +} + +template < class SymbolType > +int FormalRegExp < SymbolType >::compare ( const FormalRegExp & other ) const { + auto first = std::tie ( regExp.getStructure ( ), getAlphabet ( ) ); + auto second = std::tie ( other.regExp.getStructure ( ), other.getAlphabet ( ) ); + + std::compare < decltype ( first ) > comp; + + return comp ( first, second ); +} + +template < class SymbolType > +FormalRegExp < SymbolType >::operator std::string ( ) const { + std::stringstream ss; + ss << * this; + return ss.str ( ); +} + +template < class SymbolType > +FormalRegExp < SymbolType > FormalRegExp < SymbolType >::parse ( std::deque < sax::Token >::iterator & input ) { + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, FormalRegExp::getXmlTagName() ); + + std::set < SymbolType > alphabet = RegExpFromXMLParser::parseAlphabet < SymbolType > ( input ); + + FormalRegExpStructure < SymbolType > element ( alib::xmlApi < regexp::FormalRegExpStructure < SymbolType > >::parse ( input ) ); + FormalRegExp < SymbolType > regexp ( std::move ( alphabet ), std::move ( element ) ); + + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, FormalRegExp::getXmlTagName() ); + + return regexp; +} + +template < class SymbolType > +void FormalRegExp < SymbolType >::compose ( std::deque < sax::Token > & out ) const { + out.emplace_back ( FormalRegExp::getXmlTagName(), sax::Token::TokenType::START_ELEMENT ); + RegExpToXMLComposer::composeAlphabet ( out, getAlphabet ( ) ); + alib::xmlApi < FormalRegExpStructure < SymbolType > >::compose ( out, this->getRegExp ( ) ); + out.emplace_back ( FormalRegExp::getXmlTagName(), sax::Token::TokenType::END_ELEMENT ); +} + +} /* namespace regexp */ + namespace std { -template < > -class ComponentConstraint< regexp::FormalRegExp, alphabet::Symbol, regexp::GeneralAlphabet > { +template < class SymbolType > +class ComponentConstraint< regexp::FormalRegExp < SymbolType >, SymbolType, regexp::GeneralAlphabet > { public: - static bool used ( const regexp::FormalRegExp & regexp, const alphabet::Symbol & symbol ) { + static bool used ( const regexp::FormalRegExp < SymbolType > & regexp, const SymbolType & symbol ) { return regexp.getRegExp ( ).getStructure ( ).testSymbol ( symbol ); } - static bool available ( const regexp::FormalRegExp &, const alphabet::Symbol & ) { + static bool available ( const regexp::FormalRegExp < SymbolType > &, const SymbolType & ) { return true; } - static void valid ( const regexp::FormalRegExp &, const alphabet::Symbol & ) { + static void valid ( const regexp::FormalRegExp < SymbolType > &, const SymbolType & ) { } }; diff --git a/alib2data/src/regexp/formal/FormalRegExpAlternation.cpp b/alib2data/src/regexp/formal/FormalRegExpAlternation.cpp deleted file mode 100644 index d77b5973ba..0000000000 --- a/alib2data/src/regexp/formal/FormalRegExpAlternation.cpp +++ /dev/null @@ -1,108 +0,0 @@ -/* - * FormalRegExpAlternation.cpp - * - * Created on: Nov 23, 2013 - * Author: Martin Zak - */ - -#include "FormalRegExpAlternation.h" -#include <exception/CommonException.h> -#include "../unbounded/UnboundedRegExpAlternation.h" -#include <sstream> - -namespace regexp { - -FormalRegExpAlternation::FormalRegExpAlternation ( FormalRegExpElement && leftElement, FormalRegExpElement && rightElement ) : BinaryNode < std::smart_ptr < FormalRegExpElement >, std::smart_ptr < const FormalRegExpElement >, FormalRegExpAlternation > ( std::smart_ptr < FormalRegExpElement > ( std::move ( leftElement ).plunder ( ) ), std::smart_ptr < FormalRegExpElement > ( std::move ( rightElement ).plunder ( ) ) ) { -} - -FormalRegExpAlternation::FormalRegExpAlternation ( const FormalRegExpElement & left, const FormalRegExpElement & right ) : FormalRegExpAlternation ( std::move_copy ( left ), std::move_copy ( right ) ) { -} - -const FormalRegExpElement & FormalRegExpAlternation::getLeftElement ( ) const { - return * getLeft ( ); -} - -const FormalRegExpElement & FormalRegExpAlternation::getRightElement ( ) const { - return * getRight ( ); -} - -FormalRegExpElement & FormalRegExpAlternation::getLeftElement ( ) { - return * getLeft ( ); -} - -FormalRegExpElement & FormalRegExpAlternation::getRightElement ( ) { - return * getRight ( ); -} - -void FormalRegExpAlternation::setLeftElement ( FormalRegExpElement && element ) { - setLeft ( std::smart_ptr < FormalRegExpElement > ( std::move ( element ).plunder ( ) ) ); -} - -void FormalRegExpAlternation::setLeftElement ( const FormalRegExpElement & element ) { - setLeftElement ( std::move_copy ( element ) ); -} - -void FormalRegExpAlternation::setRightElement ( FormalRegExpElement && element ) { - setRight ( std::smart_ptr < FormalRegExpElement > ( std::move ( element ).plunder ( ) ) ); -} - -void FormalRegExpAlternation::setRightElement ( const FormalRegExpElement & element ) { - setRightElement ( std::move_copy ( element ) ); -} - -FormalRegExpElement * FormalRegExpAlternation::clone ( ) const { - return new FormalRegExpAlternation ( * this ); -} - -FormalRegExpElement * FormalRegExpAlternation::plunder ( ) && { - return new FormalRegExpAlternation ( std::move ( * this ) ); -} - -std::smart_ptr < UnboundedRegExpElement > FormalRegExpAlternation::asUnbounded ( ) const { - UnboundedRegExpAlternation * res = new UnboundedRegExpAlternation ( ); - - res->appendElement ( std::move ( * getLeftElement ( ).asUnbounded ( ) ) ); - res->appendElement ( std::move ( * getRightElement ( ).asUnbounded ( ) ) ); - - return std::smart_ptr < UnboundedRegExpElement > ( res ); -} - -int FormalRegExpAlternation::compare ( const FormalRegExpAlternation & other ) const { - int res = getLeftElement ( ).compare ( other.getLeftElement ( ) ); - - if ( res == 0 ) res = getRightElement ( ).compare ( other.getRightElement ( ) ); - - return res; -} - -void FormalRegExpAlternation::operator >>( std::ostream & out ) const { - out << "(FormalRegExpAlternation"; - out << " " << getLeft ( ); - out << " " << getRight ( ); - out << ")"; -} - -bool FormalRegExpAlternation::testSymbol ( const alphabet::Symbol & symbol ) const { - if ( getLeft ( )->testSymbol ( symbol ) ) return true; - - if ( getRight ( )->testSymbol ( symbol ) ) return true; - - return false; -} - -void FormalRegExpAlternation::computeMinimalAlphabet ( std::set < alphabet::Symbol > & alphabet ) const { - getLeftElement ( ).computeMinimalAlphabet ( alphabet ); - getRightElement ( ).computeMinimalAlphabet ( alphabet ); -} - -bool FormalRegExpAlternation::checkAlphabet ( const std::set < alphabet::Symbol > & alphabet ) const { - return getLeftElement ( ).checkAlphabet ( alphabet ) && getRightElement ( ).checkAlphabet ( alphabet ); -} - -FormalRegExpAlternation::operator std::string ( ) const { - std::stringstream ss; - ss << * this; - return ss.str ( ); -} - -} /* namespace regexp */ diff --git a/alib2data/src/regexp/formal/FormalRegExpAlternation.h b/alib2data/src/regexp/formal/FormalRegExpAlternation.h index fd534ac2de..d8308e33fa 100644 --- a/alib2data/src/regexp/formal/FormalRegExpAlternation.h +++ b/alib2data/src/regexp/formal/FormalRegExpAlternation.h @@ -9,6 +9,10 @@ #define FORMAL_REG_EXP_ALTERNATION_H_ #include <vector> +#include <sstream> + +#include <exception/CommonException.h> + #include "FormalRegExpElement.h" namespace regexp { @@ -17,86 +21,205 @@ namespace regexp { * Represents alternation operator in the regular expression. Contains list of FormalRegExpElement * as operands of the operator. */ -class FormalRegExpAlternation : public FormalRegExpElement, public std::BinaryNode < std::smart_ptr < FormalRegExpElement >, std::smart_ptr < const FormalRegExpElement >, FormalRegExpAlternation > { +template < class SymbolType > +class FormalRegExpAlternation : public FormalRegExpElement < SymbolType >, public std::BinaryNode < std::smart_ptr < FormalRegExpElement < SymbolType > >, std::smart_ptr < const FormalRegExpElement < SymbolType > >, FormalRegExpAlternation < SymbolType > > { public: - void accept ( FormalRegExpElement::Visitor & visitor ) const { + void accept ( typename FormalRegExpElement < SymbolType >::Visitor & visitor ) const override { visitor.visit ( * this ); } - explicit FormalRegExpAlternation ( FormalRegExpElement && left, FormalRegExpElement && right ); + explicit FormalRegExpAlternation ( FormalRegExpElement < SymbolType > && left, FormalRegExpElement < SymbolType > && right ); - explicit FormalRegExpAlternation ( const FormalRegExpElement & left, const FormalRegExpElement & right ); + explicit FormalRegExpAlternation ( const FormalRegExpElement < SymbolType > & left, const FormalRegExpElement < SymbolType > & right ); /** * @copydoc FormalRegExpElement::clone() const */ - virtual FormalRegExpElement * clone ( ) const; + FormalRegExpElement < SymbolType > * clone ( ) const override; /** * @copydoc FormalRegExpElement::plunder() const */ - virtual FormalRegExpElement * plunder ( ) &&; + FormalRegExpElement < SymbolType > * plunder ( ) && override; /** * @copydoc FormalRegExpElement::clone() const */ - virtual std::smart_ptr < UnboundedRegExpElement > asUnbounded ( ) const; + std::smart_ptr < UnboundedRegExpElement < SymbolType > > asUnbounded ( ) const override; /** * @copydoc FormalRegExpElement::testSymbol() const */ - virtual bool testSymbol ( const alphabet::Symbol & symbol ) const; + bool testSymbol ( const SymbolType & symbol ) const override; /** * @copydoc FormalRegExpElement::computeMinimalAlphabet() */ - virtual void computeMinimalAlphabet ( std::set < alphabet::Symbol > & alphabet ) const; + void computeMinimalAlphabet ( std::set < SymbolType > & alphabet ) const override; /** * @copydoc FormalRegExpElement::checkAlphabet() */ - virtual bool checkAlphabet ( const std::set < alphabet::Symbol > & alphabet ) const; + bool checkAlphabet ( const std::set < SymbolType > & alphabet ) const override; /** * @return elements */ - const FormalRegExpElement & getLeftElement ( ) const; - const FormalRegExpElement & getRightElement ( ) const; + const FormalRegExpElement < SymbolType > & getLeftElement ( ) const; + const FormalRegExpElement < SymbolType > & getRightElement ( ) const; /** * @return elements */ - FormalRegExpElement & getLeftElement ( ); - FormalRegExpElement & getRightElement ( ); + FormalRegExpElement < SymbolType > & getLeftElement ( ); + FormalRegExpElement < SymbolType > & getRightElement ( ); /** * @param element to append */ - void setLeftElement ( FormalRegExpElement && element ); - void setLeftElement ( const FormalRegExpElement & element ); + void setLeftElement ( FormalRegExpElement < SymbolType > && element ); + void setLeftElement ( const FormalRegExpElement < SymbolType > & element ); /** * @param element to append */ - void setRightElement ( FormalRegExpElement && element ); - void setRightElement ( const FormalRegExpElement & element ); + void setRightElement ( FormalRegExpElement < SymbolType > && element ); + void setRightElement ( const FormalRegExpElement < SymbolType > & element ); - virtual int compare ( const FormalRegExpElement & other ) const { + int compare ( const FormalRegExpElement < SymbolType > & other ) const override { if ( std::type_index ( typeid ( * this ) ) == std::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other ); return std::type_index ( typeid ( * this ) ) - std::type_index ( typeid ( other ) ); } - virtual int compare ( const FormalRegExpAlternation & ) const; + int compare ( const FormalRegExpAlternation & ) const; /** * @copydoc FormalRegExpElement::operator>>() const */ - virtual void operator >>( std::ostream & out ) const; + void operator >>( std::ostream & out ) const override; - virtual explicit operator std::string ( ) const; + explicit operator std::string ( ) const override; }; } /* namespace regexp */ +#include "../unbounded/UnboundedRegExpAlternation.h" + +namespace regexp { + +template < class SymbolType > +FormalRegExpAlternation < SymbolType >::FormalRegExpAlternation ( FormalRegExpElement < SymbolType > && leftElement, FormalRegExpElement < SymbolType > && rightElement ) : std::BinaryNode < std::smart_ptr < FormalRegExpElement < SymbolType > >, std::smart_ptr < const FormalRegExpElement < SymbolType > >, FormalRegExpAlternation < SymbolType > > ( std::smart_ptr < FormalRegExpElement < SymbolType > > ( std::move ( leftElement ).plunder ( ) ), std::smart_ptr < FormalRegExpElement < SymbolType > > ( std::move ( rightElement ).plunder ( ) ) ) { +} + +template < class SymbolType > +FormalRegExpAlternation < SymbolType >::FormalRegExpAlternation ( const FormalRegExpElement < SymbolType > & left, const FormalRegExpElement < SymbolType > & right ) : FormalRegExpAlternation ( std::move_copy ( left ), std::move_copy ( right ) ) { +} + +template < class SymbolType > +const FormalRegExpElement < SymbolType > & FormalRegExpAlternation < SymbolType >::getLeftElement ( ) const { + return * this->getLeft ( ); +} + +template < class SymbolType > +const FormalRegExpElement < SymbolType > & FormalRegExpAlternation < SymbolType >::getRightElement ( ) const { + return * this->getRight ( ); +} + +template < class SymbolType > +FormalRegExpElement < SymbolType > & FormalRegExpAlternation < SymbolType >::getLeftElement ( ) { + return * this->getLeft ( ); +} + +template < class SymbolType > +FormalRegExpElement < SymbolType > & FormalRegExpAlternation < SymbolType >::getRightElement ( ) { + return * this->getRight ( ); +} + +template < class SymbolType > +void FormalRegExpAlternation < SymbolType >::setLeftElement ( FormalRegExpElement < SymbolType > && element ) { + setLeft ( std::smart_ptr < FormalRegExpElement < SymbolType > > ( std::move ( element ).plunder ( ) ) ); +} + +template < class SymbolType > +void FormalRegExpAlternation < SymbolType >::setLeftElement ( const FormalRegExpElement < SymbolType > & element ) { + setLeftElement ( std::move_copy ( element ) ); +} + +template < class SymbolType > +void FormalRegExpAlternation < SymbolType >::setRightElement ( FormalRegExpElement < SymbolType > && element ) { + setRight ( std::smart_ptr < FormalRegExpElement < SymbolType > > ( std::move ( element ).plunder ( ) ) ); +} + +template < class SymbolType > +void FormalRegExpAlternation < SymbolType >::setRightElement ( const FormalRegExpElement < SymbolType > & element ) { + setRightElement ( std::move_copy ( element ) ); +} + +template < class SymbolType > +FormalRegExpElement < SymbolType > * FormalRegExpAlternation < SymbolType >::clone ( ) const { + return new FormalRegExpAlternation ( * this ); +} + +template < class SymbolType > +FormalRegExpElement < SymbolType > * FormalRegExpAlternation < SymbolType >::plunder ( ) && { + return new FormalRegExpAlternation ( std::move ( * this ) ); +} + +template < class SymbolType > +std::smart_ptr < UnboundedRegExpElement < SymbolType > > FormalRegExpAlternation < SymbolType >::asUnbounded ( ) const { + UnboundedRegExpAlternation < SymbolType > * res = new UnboundedRegExpAlternation < SymbolType > ( ); + + res->appendElement ( std::move ( * getLeftElement ( ).asUnbounded ( ) ) ); + res->appendElement ( std::move ( * getRightElement ( ).asUnbounded ( ) ) ); + + return std::smart_ptr < UnboundedRegExpElement < SymbolType > > ( res ); +} + +template < class SymbolType > +int FormalRegExpAlternation < SymbolType >::compare ( const FormalRegExpAlternation < SymbolType > & other ) const { + int res = getLeftElement ( ).compare ( other.getLeftElement ( ) ); + + if ( res == 0 ) res = getRightElement ( ).compare ( other.getRightElement ( ) ); + + return res; +} + +template < class SymbolType > +void FormalRegExpAlternation < SymbolType >::operator >>( std::ostream & out ) const { + out << "(FormalRegExpAlternation"; + out << " " << getLeftElement ( ); + out << " " << getRightElement ( ); + out << ")"; +} + +template < class SymbolType > +bool FormalRegExpAlternation < SymbolType >::testSymbol ( const SymbolType & symbol ) const { + if ( getLeftElement ( ).testSymbol ( symbol ) ) return true; + + if ( getRightElement ( ).testSymbol ( symbol ) ) return true; + + return false; +} + +template < class SymbolType > +void FormalRegExpAlternation < SymbolType >::computeMinimalAlphabet ( std::set < SymbolType > & alphabet ) const { + getLeftElement ( ).computeMinimalAlphabet ( alphabet ); + getRightElement ( ).computeMinimalAlphabet ( alphabet ); +} + +template < class SymbolType > +bool FormalRegExpAlternation < SymbolType >::checkAlphabet ( const std::set < SymbolType > & alphabet ) const { + return getLeftElement ( ).checkAlphabet ( alphabet ) && getRightElement ( ).checkAlphabet ( alphabet ); +} + +template < class SymbolType > +FormalRegExpAlternation < SymbolType >::operator std::string ( ) const { + std::stringstream ss; + ss << * this; + return ss.str ( ); +} + +} /* namespace regexp */ + #endif /* FORMAL_REG_EXP_ALTERNATION_H_ */ diff --git a/alib2data/src/regexp/formal/FormalRegExpConcatenation.cpp b/alib2data/src/regexp/formal/FormalRegExpConcatenation.cpp deleted file mode 100644 index 2086479799..0000000000 --- a/alib2data/src/regexp/formal/FormalRegExpConcatenation.cpp +++ /dev/null @@ -1,103 +0,0 @@ -/* - * FormalRegExpConcatenation.cpp - * - * Created on: Nov 27, 2013 - * Author: Martin Zak - */ - -#include "FormalRegExpConcatenation.h" -#include <exception/CommonException.h> -#include "../unbounded/UnboundedRegExpConcatenation.h" - -namespace regexp { - -FormalRegExpConcatenation::FormalRegExpConcatenation ( FormalRegExpElement && leftElement, FormalRegExpElement && rightElement ) : BinaryNode < std::smart_ptr < FormalRegExpElement >, std::smart_ptr < const FormalRegExpElement >, FormalRegExpConcatenation > ( std::smart_ptr < FormalRegExpElement > ( std::move ( leftElement ).plunder ( ) ), std::smart_ptr < FormalRegExpElement > ( std::move ( rightElement ).plunder ( ) ) ) { -} - -FormalRegExpConcatenation::FormalRegExpConcatenation ( const FormalRegExpElement & left, const FormalRegExpElement & right ) : FormalRegExpConcatenation ( std::move_copy ( left ), std::move_copy ( right ) ) { -} - -const FormalRegExpElement & FormalRegExpConcatenation::getLeftElement ( ) const { - return * getLeft ( ); -} - -const FormalRegExpElement & FormalRegExpConcatenation::getRightElement ( ) const { - return * getRight ( ); -} - -FormalRegExpElement & FormalRegExpConcatenation::getLeftElement ( ) { - return * getLeft ( ); -} - -FormalRegExpElement & FormalRegExpConcatenation::getRightElement ( ) { - return * getRight ( ); -} - -void FormalRegExpConcatenation::setLeftElement ( FormalRegExpElement && element ) { - setLeft( std::smart_ptr < FormalRegExpElement > ( std::move ( element ).plunder ( ) ) ); -} - -void FormalRegExpConcatenation::setLeftElement ( const FormalRegExpElement & element ) { - setLeftElement ( std::move_copy ( element ) ); -} - -void FormalRegExpConcatenation::setRightElement ( FormalRegExpElement && element ) { - setRight ( std::smart_ptr < FormalRegExpElement > ( std::move ( element ).plunder ( ) ) ); -} - -void FormalRegExpConcatenation::setRightElement ( const FormalRegExpElement & element ) { - setRightElement ( std::move_copy ( element ) ); -} - -FormalRegExpElement * FormalRegExpConcatenation::clone ( ) const { - return new FormalRegExpConcatenation ( * this ); -} - -std::smart_ptr < UnboundedRegExpElement > FormalRegExpConcatenation::asUnbounded ( ) const { - UnboundedRegExpConcatenation * res = new UnboundedRegExpConcatenation ( ); - - res->appendElement ( std::move ( * getLeftElement ( ).asUnbounded ( ) ) ); - res->appendElement ( std::move ( * getRightElement ( ).asUnbounded ( ) ) ); - - return std::smart_ptr < UnboundedRegExpElement > ( res ); -} - -FormalRegExpElement * FormalRegExpConcatenation::plunder ( ) && { - return new FormalRegExpConcatenation ( std::move ( * this ) ); -} - -int FormalRegExpConcatenation::compare ( const FormalRegExpConcatenation & other ) const { - int res = getLeftElement ( ).compare ( other.getLeftElement ( ) ); - - if ( res == 0 ) res = getRightElement ( ).compare ( other.getRightElement ( ) ); - - return res; -} - -void FormalRegExpConcatenation::operator >>( std::ostream & out ) const { - out << "(FormalRegExpConcatenation"; - out << " " << getLeft ( ); - out << " " << getRight ( ); - out << ")"; -} - -bool FormalRegExpConcatenation::testSymbol ( const alphabet::Symbol & symbol ) const { - return getLeft ( )->testSymbol ( symbol ) && getRight ( )->testSymbol ( symbol ); -} - -void FormalRegExpConcatenation::computeMinimalAlphabet ( std::set < alphabet::Symbol > & alphabet ) const { - getLeftElement ( ).computeMinimalAlphabet ( alphabet ); - getRightElement ( ).computeMinimalAlphabet ( alphabet ); -} - -bool FormalRegExpConcatenation::checkAlphabet ( const std::set < alphabet::Symbol > & alphabet ) const { - return getLeftElement ( ).checkAlphabet ( alphabet ) && getRightElement ( ).checkAlphabet ( alphabet ); -} - -FormalRegExpConcatenation::operator std::string ( ) const { - std::stringstream ss; - ss << * this; - return ss.str ( ); -} - -} /* namespace regexp */ diff --git a/alib2data/src/regexp/formal/FormalRegExpConcatenation.h b/alib2data/src/regexp/formal/FormalRegExpConcatenation.h index 51ce41638e..8a06856cb6 100644 --- a/alib2data/src/regexp/formal/FormalRegExpConcatenation.h +++ b/alib2data/src/regexp/formal/FormalRegExpConcatenation.h @@ -11,6 +11,8 @@ #include <vector> #include <sstream> +#include <exception/CommonException.h> + #include "FormalRegExpElement.h" namespace regexp { @@ -19,82 +21,197 @@ namespace regexp { * Represents concatenation operator in the regular expression. Contains list of FormalRegExpElement * as operands of the operator. */ -class FormalRegExpConcatenation : public FormalRegExpElement, public std::BinaryNode < std::smart_ptr < FormalRegExpElement >, std::smart_ptr < const FormalRegExpElement >, FormalRegExpConcatenation > { +template < class SymbolType > +class FormalRegExpConcatenation : public FormalRegExpElement < SymbolType >, public std::BinaryNode < std::smart_ptr < FormalRegExpElement < SymbolType > >, std::smart_ptr < const FormalRegExpElement < SymbolType > >, FormalRegExpConcatenation < SymbolType > > { public: - void accept ( FormalRegExpElement::Visitor & visitor ) const { + void accept ( typename FormalRegExpElement < SymbolType >::Visitor & visitor ) const override { visitor.visit ( * this ); } - explicit FormalRegExpConcatenation ( FormalRegExpElement && left, FormalRegExpElement && right ); - explicit FormalRegExpConcatenation ( const FormalRegExpElement & left, const FormalRegExpElement & right ); + explicit FormalRegExpConcatenation ( FormalRegExpElement < SymbolType > && left, FormalRegExpElement < SymbolType > && right ); + explicit FormalRegExpConcatenation ( const FormalRegExpElement < SymbolType > & left, const FormalRegExpElement < SymbolType > & right ); /** * @copydoc FormalRegExpElement::clone() const */ - virtual FormalRegExpElement * clone ( ) const; + FormalRegExpElement < SymbolType > * clone ( ) const override; /** * @copydoc FormalRegExpElement::plunder() const */ - virtual FormalRegExpElement * plunder ( ) &&; + FormalRegExpElement < SymbolType > * plunder ( ) && override; /** * @copydoc FormalRegExpElement::clone() const */ - virtual std::smart_ptr < UnboundedRegExpElement > asUnbounded ( ) const; + std::smart_ptr < UnboundedRegExpElement < SymbolType > > asUnbounded ( ) const override; /** * @copydoc FormalRegExpElement::testSymbol() const */ - virtual bool testSymbol ( const alphabet::Symbol & symbol ) const; + bool testSymbol ( const SymbolType & symbol ) const override; /** * @copydoc FormalRegExpElement::computeMinimalAlphabet() */ - virtual void computeMinimalAlphabet ( std::set < alphabet::Symbol > & alphabet ) const; + void computeMinimalAlphabet ( std::set < SymbolType > & alphabet ) const override; /** * @copydoc FormalRegExpElement::checkAlphabet() */ - virtual bool checkAlphabet ( const std::set < alphabet::Symbol > & alphabet ) const; + bool checkAlphabet ( const std::set < SymbolType > & alphabet ) const override; /** * @return elements */ - const FormalRegExpElement & getLeftElement ( ) const; - const FormalRegExpElement & getRightElement ( ) const; + const FormalRegExpElement < SymbolType > & getLeftElement ( ) const; + const FormalRegExpElement < SymbolType > & getRightElement ( ) const; /** * @return elements */ - FormalRegExpElement & getLeftElement ( ); - FormalRegExpElement & getRightElement ( ); + FormalRegExpElement < SymbolType > & getLeftElement ( ); + FormalRegExpElement < SymbolType > & getRightElement ( ); /** * @param element to append */ - void setLeftElement ( FormalRegExpElement && element ); - void setLeftElement ( const FormalRegExpElement & element ); + void setLeftElement ( FormalRegExpElement < SymbolType > && element ); + void setLeftElement ( const FormalRegExpElement < SymbolType > & element ); - void setRightElement ( FormalRegExpElement && element ); - void setRightElement ( const FormalRegExpElement & element ); + void setRightElement ( FormalRegExpElement < SymbolType > && element ); + void setRightElement ( const FormalRegExpElement < SymbolType > & element ); - virtual int compare ( const FormalRegExpElement & other ) const { + int compare ( const FormalRegExpElement < SymbolType > & other ) const override { if ( std::type_index ( typeid ( * this ) ) == std::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other ); return std::type_index ( typeid ( * this ) ) - std::type_index ( typeid ( other ) ); } - virtual int compare ( const FormalRegExpConcatenation & ) const; + int compare ( const FormalRegExpConcatenation & ) const; /** * @copydoc FormalRegExpElement::operator>>() const */ - virtual void operator >>( std::ostream & out ) const; + void operator >>( std::ostream & out ) const override; - virtual explicit operator std::string ( ) const; + explicit operator std::string ( ) const override; }; } /* namespace regexp */ +#include "../unbounded/UnboundedRegExpConcatenation.h" + +namespace regexp { + +template < class SymbolType > +FormalRegExpConcatenation < SymbolType >::FormalRegExpConcatenation ( FormalRegExpElement < SymbolType > && leftElement, FormalRegExpElement < SymbolType > && rightElement ) : std::BinaryNode < std::smart_ptr < FormalRegExpElement < SymbolType > >, std::smart_ptr < const FormalRegExpElement < SymbolType > >, FormalRegExpConcatenation < SymbolType > > ( std::smart_ptr < FormalRegExpElement < SymbolType > > ( std::move ( leftElement ).plunder ( ) ), std::smart_ptr < FormalRegExpElement < SymbolType > > ( std::move ( rightElement ).plunder ( ) ) ) { +} + +template < class SymbolType > +FormalRegExpConcatenation < SymbolType >::FormalRegExpConcatenation ( const FormalRegExpElement < SymbolType > & left, const FormalRegExpElement < SymbolType > & right ) : FormalRegExpConcatenation ( std::move_copy ( left ), std::move_copy ( right ) ) { +} + +template < class SymbolType > +const FormalRegExpElement < SymbolType > & FormalRegExpConcatenation < SymbolType >::getLeftElement ( ) const { + return * this->getLeft ( ); +} + +template < class SymbolType > +const FormalRegExpElement < SymbolType > & FormalRegExpConcatenation < SymbolType >::getRightElement ( ) const { + return * this->getRight ( ); +} + +template < class SymbolType > +FormalRegExpElement < SymbolType > & FormalRegExpConcatenation < SymbolType >::getLeftElement ( ) { + return * this->getLeft ( ); +} + +template < class SymbolType > +FormalRegExpElement < SymbolType > & FormalRegExpConcatenation < SymbolType >::getRightElement ( ) { + return * this->getRight ( ); +} + +template < class SymbolType > +void FormalRegExpConcatenation < SymbolType >::setLeftElement ( FormalRegExpElement < SymbolType > && element ) { + setLeft( std::smart_ptr < FormalRegExpElement < SymbolType > > ( std::move ( element ).plunder ( ) ) ); +} + +template < class SymbolType > +void FormalRegExpConcatenation < SymbolType >::setLeftElement ( const FormalRegExpElement < SymbolType > & element ) { + setLeftElement ( std::move_copy ( element ) ); +} + +template < class SymbolType > +void FormalRegExpConcatenation < SymbolType >::setRightElement ( FormalRegExpElement < SymbolType > && element ) { + setRight ( std::smart_ptr < FormalRegExpElement < SymbolType > > ( std::move ( element ).plunder ( ) ) ); +} + +template < class SymbolType > +void FormalRegExpConcatenation < SymbolType >::setRightElement ( const FormalRegExpElement < SymbolType > & element ) { + setRightElement ( std::move_copy ( element ) ); +} + +template < class SymbolType > +FormalRegExpElement < SymbolType > * FormalRegExpConcatenation < SymbolType >::clone ( ) const { + return new FormalRegExpConcatenation ( * this ); +} + +template < class SymbolType > +std::smart_ptr < UnboundedRegExpElement < SymbolType > > FormalRegExpConcatenation < SymbolType >::asUnbounded ( ) const { + UnboundedRegExpConcatenation < SymbolType > * res = new UnboundedRegExpConcatenation < SymbolType > ( ); + + res->appendElement ( std::move ( * getLeftElement ( ).asUnbounded ( ) ) ); + res->appendElement ( std::move ( * getRightElement ( ).asUnbounded ( ) ) ); + + return std::smart_ptr < UnboundedRegExpElement < SymbolType > > ( res ); +} + +template < class SymbolType > +FormalRegExpElement < SymbolType > * FormalRegExpConcatenation < SymbolType >::plunder ( ) && { + return new FormalRegExpConcatenation ( std::move ( * this ) ); +} + +template < class SymbolType > +int FormalRegExpConcatenation < SymbolType >::compare ( const FormalRegExpConcatenation < SymbolType > & other ) const { + int res = getLeftElement ( ).compare ( other.getLeftElement ( ) ); + + if ( res == 0 ) res = getRightElement ( ).compare ( other.getRightElement ( ) ); + + return res; +} + +template < class SymbolType > +void FormalRegExpConcatenation < SymbolType >::operator >>( std::ostream & out ) const { + out << "(FormalRegExpConcatenation"; + out << " " << getLeftElement ( ); + out << " " << getRightElement ( ); + out << ")"; +} + +template < class SymbolType > +bool FormalRegExpConcatenation < SymbolType >::testSymbol ( const SymbolType & symbol ) const { + return getLeftElement ( ).testSymbol ( symbol ) && this->getRight ( )->testSymbol ( symbol ); +} + +template < class SymbolType > +void FormalRegExpConcatenation < SymbolType >::computeMinimalAlphabet ( std::set < SymbolType > & alphabet ) const { + getLeftElement ( ).computeMinimalAlphabet ( alphabet ); + getRightElement ( ).computeMinimalAlphabet ( alphabet ); +} + +template < class SymbolType > +bool FormalRegExpConcatenation < SymbolType >::checkAlphabet ( const std::set < SymbolType > & alphabet ) const { + return getLeftElement ( ).checkAlphabet ( alphabet ) && getRightElement ( ).checkAlphabet ( alphabet ); +} + +template < class SymbolType > +FormalRegExpConcatenation < SymbolType >::operator std::string ( ) const { + std::stringstream ss; + ss << * this; + return ss.str ( ); +} + +} /* namespace regexp */ + #endif /* FORMAL_REG_EXP_CONCATENATION_H_ */ diff --git a/alib2data/src/regexp/formal/FormalRegExpElement.cpp b/alib2data/src/regexp/formal/FormalRegExpElement.cpp deleted file mode 100644 index 8047bae0a4..0000000000 --- a/alib2data/src/regexp/formal/FormalRegExpElement.cpp +++ /dev/null @@ -1,19 +0,0 @@ -/* - * FormalRegExpElement.cpp - * - * Created on: Nov 23, 2013 - * Author: Martin Zak - */ - -#include "FormalRegExpElement.h" - -namespace regexp { - -std::set < alphabet::Symbol > FormalRegExpElement::computeMinimalAlphabet ( ) const { - std::set < alphabet::Symbol > res; - - computeMinimalAlphabet ( res ); - return res; -} - -} /* namespace regexp */ diff --git a/alib2data/src/regexp/formal/FormalRegExpElement.h b/alib2data/src/regexp/formal/FormalRegExpElement.h index 944dc753f5..25ed2d58cd 100644 --- a/alib2data/src/regexp/formal/FormalRegExpElement.h +++ b/alib2data/src/regexp/formal/FormalRegExpElement.h @@ -9,70 +9,62 @@ #define FORMAL_REG_EXP_ELEMENT_H_ #include <core/xmlApi.hpp> -#include "../../alphabet/Symbol.h" #include <set> #include <tree> #include <core/visitor.hpp> +#include "../RegExpFeatures.h" namespace regexp { -class FormalRegExpAlternation; -class FormalRegExpConcatenation; -class FormalRegExpIteration; -class FormalRegExpSymbol; -class FormalRegExpEmpty; -class FormalRegExpEpsilon; - -class UnboundedRegExpElement; - /** * Abstract class representing element in the formal regular expression. Can be operator or symbol. */ -class FormalRegExpElement : public alib::CommonBase < FormalRegExpElement >, public std::BaseNode < FormalRegExpElement > { +template < class SymbolType > +class FormalRegExpElement : public alib::CommonBase < FormalRegExpElement < SymbolType > >, public std::BaseNode < FormalRegExpElement < SymbolType > > { public: class Visitor { public: - virtual void visit ( const FormalRegExpAlternation & ) = 0; - virtual void visit ( const FormalRegExpConcatenation & ) = 0; - virtual void visit ( const FormalRegExpIteration & ) = 0; - virtual void visit ( const FormalRegExpSymbol & ) = 0; - virtual void visit ( const FormalRegExpEmpty & ) = 0; - virtual void visit ( const FormalRegExpEpsilon & ) = 0; + virtual void visit ( const FormalRegExpAlternation < SymbolType > & ) = 0; + virtual void visit ( const FormalRegExpConcatenation < SymbolType > & ) = 0; + virtual void visit ( const FormalRegExpIteration < SymbolType > & ) = 0; + virtual void visit ( const FormalRegExpSymbol < SymbolType > & ) = 0; + virtual void visit ( const FormalRegExpEmpty < SymbolType > & ) = 0; + virtual void visit ( const FormalRegExpEpsilon < SymbolType > & ) = 0; }; - template < class ReturnType, class Visitorr, class ... Params > - class VisitorContext : public std::VisitorContextAux < ReturnType, Visitorr, Params ... >, public FormalRegExpElement::Visitor { + template < class ReturnType, class Visitor, class ... Params > + class VisitorContext : public std::VisitorContextAux < ReturnType, Visitor, Params ... >, public FormalRegExpElement::Visitor { public: - using std::VisitorContextAux < ReturnType, Visitorr, Params ... >::VisitorContextAux; + using std::VisitorContextAux < ReturnType, Visitor, Params ... >::VisitorContextAux; - void visit ( const FormalRegExpAlternation & inherit ) override { + void visit ( const FormalRegExpAlternation < SymbolType > & inherit ) override { this->call ( inherit, std::make_index_sequence < sizeof ... ( Params ) > { } ); } - void visit ( const FormalRegExpConcatenation & inherit ) override { + void visit ( const FormalRegExpConcatenation < SymbolType > & inherit ) override { this->call ( inherit, std::make_index_sequence < sizeof ... ( Params ) > { } ); } - void visit ( const FormalRegExpIteration & inherit ) override { + void visit ( const FormalRegExpIteration < SymbolType > & inherit ) override { this->call ( inherit, std::make_index_sequence < sizeof ... ( Params ) > { } ); } - void visit ( const FormalRegExpSymbol & inherit ) override { + void visit ( const FormalRegExpSymbol < SymbolType > & inherit ) override { this->call ( inherit, std::make_index_sequence < sizeof ... ( Params ) > { } ); } - void visit ( const FormalRegExpEmpty & inherit ) override { + void visit ( const FormalRegExpEmpty < SymbolType > & inherit ) override { this->call ( inherit, std::make_index_sequence < sizeof ... ( Params ) > { } ); } - void visit ( const FormalRegExpEpsilon & inherit ) override { + void visit ( const FormalRegExpEpsilon < SymbolType > & inherit ) override { this->call ( inherit, std::make_index_sequence < sizeof ... ( Params ) > { } ); } }; - template < class ReturnType, class Visitorr, class ... Params > + template < class ReturnType, class Visitor, class ... Params > ReturnType accept ( Params && ... params ) const { - VisitorContext < ReturnType, Visitorr, Params ... > context ( std::forward < Params > ( params ) ... ); + VisitorContext < ReturnType, Visitor, Params ... > context ( std::forward < Params > ( params ) ... ); accept ( context ); return context.getResult ( ); } @@ -84,7 +76,7 @@ public: * * @return copy of the element */ - virtual std::smart_ptr < UnboundedRegExpElement > asUnbounded ( ) const = 0; + virtual std::smart_ptr < UnboundedRegExpElement < SymbolType > > asUnbounded ( ) const = 0; /** * Traverses the regexp tree looking if particular Symbol is used in the regexp. @@ -92,7 +84,7 @@ public: * @param symbol to test if used in regexp element * @return true if symbol is used by the element and its successor */ - virtual bool testSymbol ( const alphabet::Symbol & symbol ) const = 0; + virtual bool testSymbol ( const SymbolType & symbol ) const = 0; /** * Traverses the regexp tree computing minimal alphabet needed by regexp @@ -100,7 +92,7 @@ public: * @param alphabet All alphabet symbols encountered are added into this set * @return true if symbol is used by the element and its successor */ - virtual void computeMinimalAlphabet ( std::set < alphabet::Symbol > & alphabet ) const = 0; + virtual void computeMinimalAlphabet ( std::set < SymbolType > & alphabet ) const = 0; /** * Traverses the regexp tree and checks whether all symbols in the regexp tree are in the alphabet @@ -108,16 +100,24 @@ public: * @param alphabet * @return true if symbols in the regexp are in the alphabet, false otherwise */ - virtual bool checkAlphabet ( const std::set < alphabet::Symbol > & alphabet ) const = 0; + virtual bool checkAlphabet ( const std::set < SymbolType > & alphabet ) const = 0; /** * Traverses the regexp tree computing minimal alphabet needed by regexp * * @return the minimal alphabet needed by the regexp */ - std::set < alphabet::Symbol > computeMinimalAlphabet ( ) const; + std::set < SymbolType > computeMinimalAlphabet ( ) const; }; +template < class SymbolType > +std::set < SymbolType > FormalRegExpElement < SymbolType >::computeMinimalAlphabet ( ) const { + std::set < SymbolType > res; + + computeMinimalAlphabet ( res ); + return res; +} + } /* namespace regexp */ #endif /* FORMAL_REG_EXP_ELEMENT_H_ */ diff --git a/alib2data/src/regexp/formal/FormalRegExpEmpty.cpp b/alib2data/src/regexp/formal/FormalRegExpEmpty.cpp deleted file mode 100644 index 6de54de689..0000000000 --- a/alib2data/src/regexp/formal/FormalRegExpEmpty.cpp +++ /dev/null @@ -1,54 +0,0 @@ -/* - * FormalRegExpEmpty.cpp - * - * Created on: Jan 30, 2014 - * Author: Jan Travnicek - */ - -#include "FormalRegExpEmpty.h" -#include "../unbounded/UnboundedRegExpEmpty.h" - -namespace regexp { - -FormalRegExpEmpty::FormalRegExpEmpty ( ) { - // so that default constructor is available -} - -FormalRegExpElement * FormalRegExpEmpty::clone ( ) const { - return new FormalRegExpEmpty ( * this ); -} - -FormalRegExpElement * FormalRegExpEmpty::plunder ( ) && { - return new FormalRegExpEmpty ( std::move ( * this ) ); -} - -std::smart_ptr < UnboundedRegExpElement > FormalRegExpEmpty::asUnbounded ( ) const { - return std::smart_ptr < UnboundedRegExpElement > ( new UnboundedRegExpEmpty ( ) ); -} - -int FormalRegExpEmpty::compare ( const FormalRegExpEmpty & ) const { - return 0; -} - -void FormalRegExpEmpty::operator >>( std::ostream & out ) const { - out << "(FormalRegExpEmpty)"; -} - -bool FormalRegExpEmpty::testSymbol ( const alphabet::Symbol & ) const { - return false; -} - -void FormalRegExpEmpty::computeMinimalAlphabet ( std::set < alphabet::Symbol > & ) const { -} - -bool FormalRegExpEmpty::checkAlphabet ( const std::set < alphabet::Symbol > & ) const { - return true; -} - -FormalRegExpEmpty::operator std::string ( ) const { - std::stringstream ss; - ss << * this; - return ss.str ( ); -} - -} /* namespace regexp */ diff --git a/alib2data/src/regexp/formal/FormalRegExpEmpty.h b/alib2data/src/regexp/formal/FormalRegExpEmpty.h index 0609c82297..80876adac2 100644 --- a/alib2data/src/regexp/formal/FormalRegExpEmpty.h +++ b/alib2data/src/regexp/formal/FormalRegExpEmpty.h @@ -16,9 +16,10 @@ namespace regexp { /** * Represents empty regular expression in the regular expression. */ -class FormalRegExpEmpty : public FormalRegExpElement, public std::NullaryNode < std::smart_ptr < FormalRegExpElement >, std::smart_ptr < const FormalRegExpElement >, FormalRegExpSymbol > { +template < class SymbolType > +class FormalRegExpEmpty : public FormalRegExpElement < SymbolType >, public std::NullaryNode < std::smart_ptr < FormalRegExpElement < SymbolType > >, std::smart_ptr < const FormalRegExpElement < SymbolType > >, FormalRegExpSymbol < SymbolType > > { public: - void accept ( FormalRegExpElement::Visitor & visitor ) const { + void accept ( typename FormalRegExpElement < SymbolType >::Visitor & visitor ) const override { visitor.visit ( * this ); } @@ -27,49 +28,106 @@ public: /** * @copydoc FormalRegExpElement::clone() const */ - virtual FormalRegExpElement * clone ( ) const; + FormalRegExpElement < SymbolType > * clone ( ) const override; /** * @copydoc FormalRegExpElement::plunder() const */ - virtual FormalRegExpElement * plunder ( ) &&; + FormalRegExpElement < SymbolType > * plunder ( ) && override; /** * @copydoc FormalRegExpElement::clone() const */ - virtual std::smart_ptr < UnboundedRegExpElement > asUnbounded ( ) const; + std::smart_ptr < UnboundedRegExpElement < SymbolType > > asUnbounded ( ) const override; /** * @copydoc FormalRegExpElement::testSymbol() const */ - virtual bool testSymbol ( const alphabet::Symbol & symbol ) const; + bool testSymbol ( const SymbolType & symbol ) const override; /** * @copydoc RegExpElement::computeMinimalAlphabet() */ - virtual void computeMinimalAlphabet ( std::set < alphabet::Symbol > & alphabet ) const; + void computeMinimalAlphabet ( std::set < SymbolType > & alphabet ) const override; /** * @copydoc FormalRegExpElement::checkAlphabet() */ - virtual bool checkAlphabet ( const std::set < alphabet::Symbol > & alphabet ) const; + bool checkAlphabet ( const std::set < SymbolType > & alphabet ) const override; - virtual int compare ( const FormalRegExpElement & other ) const { + int compare ( const FormalRegExpElement < SymbolType > & other ) const override { if ( std::type_index ( typeid ( * this ) ) == std::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other ); return std::type_index ( typeid ( * this ) ) - std::type_index ( typeid ( other ) ); } - virtual int compare ( const FormalRegExpEmpty & ) const; + int compare ( const FormalRegExpEmpty & ) const; /** * @copydoc FormalRegExpElement::operator>>() const */ - virtual void operator >>( std::ostream & out ) const; + void operator >>( std::ostream & out ) const override; - virtual explicit operator std::string ( ) const; + explicit operator std::string ( ) const override; }; } /* namespace regexp */ +#include "../unbounded/UnboundedRegExpEmpty.h" + +namespace regexp { + +template < class SymbolType > +FormalRegExpEmpty < SymbolType >::FormalRegExpEmpty ( ) { + // so that default constructor is available +} + +template < class SymbolType > +FormalRegExpElement < SymbolType > * FormalRegExpEmpty < SymbolType >::clone ( ) const { + return new FormalRegExpEmpty ( * this ); +} + +template < class SymbolType > +FormalRegExpElement < SymbolType > * FormalRegExpEmpty < SymbolType >::plunder ( ) && { + return new FormalRegExpEmpty ( std::move ( * this ) ); +} + +template < class SymbolType > +std::smart_ptr < UnboundedRegExpElement < SymbolType > > FormalRegExpEmpty < SymbolType >::asUnbounded ( ) const { + return std::smart_ptr < UnboundedRegExpElement < SymbolType > > ( new UnboundedRegExpEmpty < SymbolType > ( ) ); +} + +template < class SymbolType > +int FormalRegExpEmpty < SymbolType >::compare ( const FormalRegExpEmpty & ) const { + return 0; +} + +template < class SymbolType > +void FormalRegExpEmpty < SymbolType >::operator >>( std::ostream & out ) const { + out << "(FormalRegExpEmpty)"; +} + +template < class SymbolType > +bool FormalRegExpEmpty < SymbolType >::testSymbol ( const SymbolType & ) const { + return false; +} + +template < class SymbolType > +void FormalRegExpEmpty < SymbolType >::computeMinimalAlphabet ( std::set < SymbolType > & ) const { +} + +template < class SymbolType > +bool FormalRegExpEmpty < SymbolType >::checkAlphabet ( const std::set < SymbolType > & ) const { + return true; +} + +template < class SymbolType > +FormalRegExpEmpty < SymbolType >::operator std::string ( ) const { + std::stringstream ss; + ss << * this; + return ss.str ( ); +} + +} /* namespace regexp */ + #endif /* FORMAL_REG_EXP_EMPTY_H_ */ diff --git a/alib2data/src/regexp/formal/FormalRegExpEpsilon.cpp b/alib2data/src/regexp/formal/FormalRegExpEpsilon.cpp deleted file mode 100644 index 63eb766935..0000000000 --- a/alib2data/src/regexp/formal/FormalRegExpEpsilon.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/* - * FormalRegExpEpsilon.cpp - * - * Created on: Jan 30, 2014 - * Author: Jan Travnicek - */ - -#include "FormalRegExpEpsilon.h" -#include "../unbounded/UnboundedRegExpEpsilon.h" -#include <sstream> - -namespace regexp { - -FormalRegExpEpsilon::FormalRegExpEpsilon ( ) { - // so that default constructor is available -} - -FormalRegExpElement * FormalRegExpEpsilon::clone ( ) const { - return new FormalRegExpEpsilon ( * this ); -} - -FormalRegExpElement * FormalRegExpEpsilon::plunder ( ) && { - return new FormalRegExpEpsilon ( std::move ( * this ) ); -} - -std::smart_ptr < UnboundedRegExpElement > FormalRegExpEpsilon::asUnbounded ( ) const { - return std::smart_ptr < UnboundedRegExpElement > ( new UnboundedRegExpEpsilon ( ) ); -} - -int FormalRegExpEpsilon::compare ( const FormalRegExpEpsilon & ) const { - return 0; -} - -void FormalRegExpEpsilon::operator >>( std::ostream & out ) const { - out << "(FormalRegExpEpsilon)"; -} - -bool FormalRegExpEpsilon::testSymbol ( const alphabet::Symbol & ) const { - return false; -} - -void FormalRegExpEpsilon::computeMinimalAlphabet ( std::set < alphabet::Symbol > & ) const { -} - -bool FormalRegExpEpsilon::checkAlphabet ( const std::set < alphabet::Symbol > & ) const { - return true; -} - -FormalRegExpEpsilon::operator std::string ( ) const { - std::stringstream ss; - ss << * this; - return ss.str ( ); -} - -} /* namespace regexp */ diff --git a/alib2data/src/regexp/formal/FormalRegExpEpsilon.h b/alib2data/src/regexp/formal/FormalRegExpEpsilon.h index 77deb0fe33..a1aac2390f 100644 --- a/alib2data/src/regexp/formal/FormalRegExpEpsilon.h +++ b/alib2data/src/regexp/formal/FormalRegExpEpsilon.h @@ -8,17 +8,19 @@ #ifndef FORMAL_REG_EXP_EPSILON_H_ #define FORMAL_REG_EXP_EPSILON_H_ -#include "FormalRegExpElement.h" #include <sstream> +#include "FormalRegExpElement.h" + namespace regexp { /** * Represents epsilon in the regular expression. */ -class FormalRegExpEpsilon : public FormalRegExpElement, public std::NullaryNode < std::smart_ptr < FormalRegExpElement >, std::smart_ptr < const FormalRegExpElement >, FormalRegExpSymbol > { +template < class SymbolType > +class FormalRegExpEpsilon : public FormalRegExpElement < SymbolType >, public std::NullaryNode < std::smart_ptr < FormalRegExpElement < SymbolType > >, std::smart_ptr < const FormalRegExpElement < SymbolType > >, FormalRegExpSymbol < SymbolType > > { public: - void accept ( FormalRegExpElement::Visitor & visitor ) const { + void accept ( typename FormalRegExpElement < SymbolType >::Visitor & visitor ) const override { visitor.visit ( * this ); } @@ -27,49 +29,106 @@ public: /** * @copydoc FormalRegExpElement::clone() const */ - virtual FormalRegExpElement * clone ( ) const; + FormalRegExpElement < SymbolType > * clone ( ) const override; /** * @copydoc FormalRegExpElement::plunder() const */ - virtual FormalRegExpElement * plunder ( ) &&; + FormalRegExpElement < SymbolType > * plunder ( ) && override; /** * @copydoc FormalRegExpElement::clone() const */ - virtual std::smart_ptr < UnboundedRegExpElement > asUnbounded ( ) const; + std::smart_ptr < UnboundedRegExpElement < SymbolType > > asUnbounded ( ) const override; /** * @copydoc FormalRegExpElement::testSymbol() const */ - virtual bool testSymbol ( const alphabet::Symbol & symbol ) const; + bool testSymbol ( const SymbolType & symbol ) const override; /** * @copydoc FormalRegExpElement::computeMinimalAlphabet() */ - virtual void computeMinimalAlphabet ( std::set < alphabet::Symbol > & alphabet ) const; + void computeMinimalAlphabet ( std::set < SymbolType > & alphabet ) const override; /** * @copydoc FormalRegExpElement::checkAlphabet() */ - virtual bool checkAlphabet ( const std::set < alphabet::Symbol > & alphabet ) const; + bool checkAlphabet ( const std::set < SymbolType > & alphabet ) const override; - virtual int compare ( const FormalRegExpElement & other ) const { + int compare ( const FormalRegExpElement < SymbolType > & other ) const override { if ( std::type_index ( typeid ( * this ) ) == std::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other ); return std::type_index ( typeid ( * this ) ) - std::type_index ( typeid ( other ) ); } - virtual int compare ( const FormalRegExpEpsilon & ) const; + int compare ( const FormalRegExpEpsilon & ) const; /** * @copydoc FormalRegExpElement::operator>>() const */ - virtual void operator >>( std::ostream & out ) const; + void operator >>( std::ostream & out ) const override; - virtual explicit operator std::string ( ) const; + explicit operator std::string ( ) const override; }; } /* namespace regexp */ +#include "../unbounded/UnboundedRegExpEpsilon.h" + +namespace regexp { + +template < class SymbolType > +FormalRegExpEpsilon < SymbolType >::FormalRegExpEpsilon ( ) { + // so that default constructor is available +} + +template < class SymbolType > +FormalRegExpElement < SymbolType > * FormalRegExpEpsilon < SymbolType >::clone ( ) const { + return new FormalRegExpEpsilon ( * this ); +} + +template < class SymbolType > +FormalRegExpElement < SymbolType > * FormalRegExpEpsilon < SymbolType >::plunder ( ) && { + return new FormalRegExpEpsilon ( std::move ( * this ) ); +} + +template < class SymbolType > +std::smart_ptr < UnboundedRegExpElement < SymbolType > > FormalRegExpEpsilon < SymbolType >::asUnbounded ( ) const { + return std::smart_ptr < UnboundedRegExpElement < SymbolType > > ( new UnboundedRegExpEpsilon < SymbolType > ( ) ); +} + +template < class SymbolType > +int FormalRegExpEpsilon < SymbolType >::compare ( const FormalRegExpEpsilon & ) const { + return 0; +} + +template < class SymbolType > +void FormalRegExpEpsilon < SymbolType >::operator >>( std::ostream & out ) const { + out << "(FormalRegExpEpsilon)"; +} + +template < class SymbolType > +bool FormalRegExpEpsilon < SymbolType >::testSymbol ( const SymbolType & ) const { + return false; +} + +template < class SymbolType > +void FormalRegExpEpsilon < SymbolType >::computeMinimalAlphabet ( std::set < SymbolType > & ) const { +} + +template < class SymbolType > +bool FormalRegExpEpsilon < SymbolType >::checkAlphabet ( const std::set < SymbolType > & ) const { + return true; +} + +template < class SymbolType > +FormalRegExpEpsilon < SymbolType >::operator std::string ( ) const { + std::stringstream ss; + ss << * this; + return ss.str ( ); +} + +} /* namespace regexp */ + #endif /* FORMAL_REG_EXP_EPSILON_H_ */ diff --git a/alib2data/src/regexp/formal/FormalRegExpIteration.cpp b/alib2data/src/regexp/formal/FormalRegExpIteration.cpp deleted file mode 100644 index dc7a7670f3..0000000000 --- a/alib2data/src/regexp/formal/FormalRegExpIteration.cpp +++ /dev/null @@ -1,76 +0,0 @@ -/* - * FormalRegExpIteration.cpp - * - * Created on: Nov 23, 2013 - * Author: Martin Zak - */ - -#include "FormalRegExpIteration.h" -#include <exception/CommonException.h> - -#include "../unbounded/UnboundedRegExpIteration.h" -#include <sstream> - -namespace regexp { - -FormalRegExpIteration::FormalRegExpIteration ( FormalRegExpElement && element ) : UnaryNode < std::smart_ptr < FormalRegExpElement >, std::smart_ptr < const FormalRegExpElement >, FormalRegExpIteration > ( std::smart_ptr < FormalRegExpElement > ( std::move ( element ).plunder ( ) ) ) { -} - -FormalRegExpIteration::FormalRegExpIteration ( const FormalRegExpElement & element ) : FormalRegExpIteration ( std::move_copy ( element ) ) { -} - -const FormalRegExpElement & FormalRegExpIteration::getElement ( ) const { - return * getChild ( ); -} - -FormalRegExpElement & FormalRegExpIteration::getElement ( ) { - return * getChild ( ); -} - -void FormalRegExpIteration::setElement ( FormalRegExpElement && elementParam ) { - setChild ( std::smart_ptr < FormalRegExpElement > ( std::move ( elementParam ).plunder ( ) ) ); -} - -void FormalRegExpIteration::setElement ( const FormalRegExpElement & elementParam ) { - setElement ( std::move_copy ( elementParam ) ); -} - -FormalRegExpElement * FormalRegExpIteration::clone ( ) const { - return new FormalRegExpIteration ( * this ); -} - -FormalRegExpElement * FormalRegExpIteration::plunder ( ) && { - return new FormalRegExpIteration ( std::move ( * this ) ); -} - -std::smart_ptr < UnboundedRegExpElement > FormalRegExpIteration::asUnbounded ( ) const { - return std::smart_ptr < UnboundedRegExpElement > ( new UnboundedRegExpIteration ( * getElement ( ).asUnbounded ( ) ) ); -} - -int FormalRegExpIteration::compare ( const FormalRegExpIteration & other ) const { - return getElement ( ).compare ( other.getElement ( ) ); -} - -void FormalRegExpIteration::operator >>( std::ostream & out ) const { - out << "(RegExpFormalRegExpIteration " << getElement ( ) << ")"; -} - -bool FormalRegExpIteration::testSymbol ( const alphabet::Symbol & symbol ) const { - return getChild ( )->testSymbol ( symbol ); -} - -void FormalRegExpIteration::computeMinimalAlphabet ( std::set < alphabet::Symbol > & alphabet ) const { - getElement ( ).computeMinimalAlphabet ( alphabet ); -} - -bool FormalRegExpIteration::checkAlphabet ( const std::set < alphabet::Symbol > & alphabet ) const { - return getElement ( ).checkAlphabet ( alphabet ); -} - -FormalRegExpIteration::operator std::string ( ) const { - std::stringstream ss; - ss << * this; - return ss.str ( ); -} - -} /* namespace regexp */ diff --git a/alib2data/src/regexp/formal/FormalRegExpIteration.h b/alib2data/src/regexp/formal/FormalRegExpIteration.h index c687dea427..ea2e677d8c 100644 --- a/alib2data/src/regexp/formal/FormalRegExpIteration.h +++ b/alib2data/src/regexp/formal/FormalRegExpIteration.h @@ -8,8 +8,11 @@ #ifndef FORMAL_REG_EXP_ITERATION_H_ #define FORMAL_REG_EXP_ITERATION_H_ +#include <sstream> + +#include <exception/CommonException.h> + #include "FormalRegExpElement.h" -#include "FormalRegExpEmpty.h" namespace regexp { @@ -17,77 +20,160 @@ namespace regexp { * Represents iteration operator in the regular expression. Contains one FormalRegExpElement * as operand. */ -class FormalRegExpIteration : public FormalRegExpElement, public std::UnaryNode < std::smart_ptr < FormalRegExpElement >, std::smart_ptr < const FormalRegExpElement >, FormalRegExpIteration > { +template < class SymbolType > +class FormalRegExpIteration : public FormalRegExpElement < SymbolType >, public std::UnaryNode < std::smart_ptr < FormalRegExpElement < SymbolType > >, std::smart_ptr < const FormalRegExpElement < SymbolType > >, FormalRegExpIteration < SymbolType > > { public: - void accept ( FormalRegExpElement::Visitor & visitor ) const { + void accept ( typename FormalRegExpElement < SymbolType >::Visitor & visitor ) const override { visitor.visit ( * this ); } - explicit FormalRegExpIteration ( FormalRegExpElement && ); - explicit FormalRegExpIteration ( const FormalRegExpElement & ); + explicit FormalRegExpIteration ( FormalRegExpElement < SymbolType > && ); + explicit FormalRegExpIteration ( const FormalRegExpElement < SymbolType > & ); /** * @copydoc FormalRegExpElement::clone() const */ - virtual FormalRegExpElement * clone ( ) const; + FormalRegExpElement < SymbolType > * clone ( ) const override; /** * @copydoc FormalRegExpElement::plunder() const */ - virtual FormalRegExpElement * plunder ( ) &&; + FormalRegExpElement < SymbolType > * plunder ( ) && override; /** * @copydoc FormalRegExpElement::clone() const */ - virtual std::smart_ptr < UnboundedRegExpElement > asUnbounded ( ) const; + std::smart_ptr < UnboundedRegExpElement < SymbolType > > asUnbounded ( ) const override; /** * @copydoc FormalRegExpElement::testSymbol() const */ - virtual bool testSymbol ( const alphabet::Symbol & symbol ) const; + bool testSymbol ( const SymbolType & symbol ) const override; /** * @copydoc FormalRegExpElement::computeMinimalAlphabet() */ - virtual void computeMinimalAlphabet ( std::set < alphabet::Symbol > & alphabet ) const; + void computeMinimalAlphabet ( std::set < SymbolType > & alphabet ) const override; /** * @copydoc FormalRegExpElement::checkAlphabet() */ - virtual bool checkAlphabet ( const std::set < alphabet::Symbol > & alphabet ) const; + bool checkAlphabet ( const std::set < SymbolType > & alphabet ) const override; /** * @return element */ - const FormalRegExpElement & getElement ( ) const; + const FormalRegExpElement < SymbolType > & getElement ( ) const; /** * @return element */ - FormalRegExpElement & getElement ( ); + FormalRegExpElement < SymbolType > & getElement ( ); /** * @param element to iterate */ - void setElement ( FormalRegExpElement && element ); - void setElement ( const FormalRegExpElement & element ); + void setElement ( FormalRegExpElement < SymbolType > && element ); + void setElement ( const FormalRegExpElement < SymbolType > & element ); - virtual int compare ( const FormalRegExpElement & other ) const { + int compare ( const FormalRegExpElement < SymbolType > & other ) const override { if ( std::type_index ( typeid ( * this ) ) == std::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other ); return std::type_index ( typeid ( * this ) ) - std::type_index ( typeid ( other ) ); } - virtual int compare ( const FormalRegExpIteration & ) const; + int compare ( const FormalRegExpIteration & ) const; /** * @copydoc FormalRegExpElement::operator>>() const */ - virtual void operator >>( std::ostream & out ) const; + void operator >>( std::ostream & out ) const override; - virtual explicit operator std::string ( ) const; + explicit operator std::string ( ) const override; }; } /* namespace regexp */ +#include "FormalRegExpEmpty.h" +#include "../unbounded/UnboundedRegExpIteration.h" + +namespace regexp { + +template < class SymbolType > +FormalRegExpIteration < SymbolType >::FormalRegExpIteration ( FormalRegExpElement < SymbolType > && element ) : std::UnaryNode < std::smart_ptr < FormalRegExpElement < SymbolType > >, std::smart_ptr < const FormalRegExpElement < SymbolType > >, FormalRegExpIteration < SymbolType > > ( std::smart_ptr < FormalRegExpElement < SymbolType > > ( std::move ( element ).plunder ( ) ) ) { +} + +template < class SymbolType > +FormalRegExpIteration < SymbolType >::FormalRegExpIteration ( const FormalRegExpElement < SymbolType > & element ) : FormalRegExpIteration ( std::move_copy ( element ) ) { +} + +template < class SymbolType > +const FormalRegExpElement < SymbolType > & FormalRegExpIteration < SymbolType >::getElement ( ) const { + return * this->getChild ( ); +} + +template < class SymbolType > +FormalRegExpElement < SymbolType > & FormalRegExpIteration < SymbolType >::getElement ( ) { + return * this->getChild ( ); +} + +template < class SymbolType > +void FormalRegExpIteration < SymbolType >::setElement ( FormalRegExpElement < SymbolType > && elementParam ) { + setChild ( std::smart_ptr < FormalRegExpElement < SymbolType > > ( std::move ( elementParam ).plunder ( ) ) ); +} + +template < class SymbolType > +void FormalRegExpIteration < SymbolType >::setElement ( const FormalRegExpElement < SymbolType > & elementParam ) { + setElement ( std::move_copy ( elementParam ) ); +} + +template < class SymbolType > +FormalRegExpElement < SymbolType > * FormalRegExpIteration < SymbolType >::clone ( ) const { + return new FormalRegExpIteration ( * this ); +} + +template < class SymbolType > +FormalRegExpElement < SymbolType > * FormalRegExpIteration < SymbolType >::plunder ( ) && { + return new FormalRegExpIteration ( std::move ( * this ) ); +} + +template < class SymbolType > +std::smart_ptr < UnboundedRegExpElement < SymbolType > > FormalRegExpIteration < SymbolType >::asUnbounded ( ) const { + return std::smart_ptr < UnboundedRegExpElement < SymbolType > > ( new UnboundedRegExpIteration < SymbolType > ( * getElement ( ).asUnbounded ( ) ) ); +} + +template < class SymbolType > +int FormalRegExpIteration < SymbolType >::compare ( const FormalRegExpIteration < SymbolType > & other ) const { + return getElement ( ).compare ( other.getElement ( ) ); +} + +template < class SymbolType > +void FormalRegExpIteration < SymbolType >::operator >>( std::ostream & out ) const { + out << "(RegExpFormalRegExpIteration " << getElement ( ) << ")"; +} + +template < class SymbolType > +bool FormalRegExpIteration < SymbolType >::testSymbol ( const SymbolType & symbol ) const { + return getElement ( ).testSymbol ( symbol ); +} + +template < class SymbolType > +void FormalRegExpIteration < SymbolType >::computeMinimalAlphabet ( std::set < SymbolType > & alphabet ) const { + getElement ( ).computeMinimalAlphabet ( alphabet ); +} + +template < class SymbolType > +bool FormalRegExpIteration < SymbolType >::checkAlphabet ( const std::set < SymbolType > & alphabet ) const { + return getElement ( ).checkAlphabet ( alphabet ); +} + +template < class SymbolType > +FormalRegExpIteration < SymbolType >::operator std::string ( ) const { + std::stringstream ss; + ss << * this; + return ss.str ( ); +} + +} /* namespace regexp */ + #endif /* FORMAL_REG_EXP_ITERATION_H_ */ diff --git a/alib2data/src/regexp/formal/FormalRegExpStructure.cpp b/alib2data/src/regexp/formal/FormalRegExpStructure.cpp deleted file mode 100644 index 5e90d8b1f3..0000000000 --- a/alib2data/src/regexp/formal/FormalRegExpStructure.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/* - * FormalRegExpStructure.cpp - * - * Created on: Oct 20, 2016 - * Author: Jan Travnicek - */ - -#include "FormalRegExpStructure.h" -#include "FormalRegExpEmpty.h" - -#include "../unbounded/UnboundedRegExpStructure.h" - -#include "../common/RegExpFromXMLParser.h" -#include "../common/RegExpToXMLComposer.h" -#include <exception/CommonException.h> - -namespace regexp { - -FormalRegExpStructure::FormalRegExpStructure ( FormalRegExpElement && structure ) : structure ( NULL ) { - setStructure ( std::move ( structure ) ); -} - -FormalRegExpStructure::FormalRegExpStructure ( const FormalRegExpElement & structure ) : FormalRegExpStructure ( std::move_copy ( structure ) ) { -} - -FormalRegExpStructure::FormalRegExpStructure ( ) : FormalRegExpStructure ( FormalRegExpEmpty ( ) ) { -} - -FormalRegExpStructure::FormalRegExpStructure ( const UnboundedRegExpStructure & other ) : FormalRegExpStructure ( * other.getStructure ( ).asFormal ( ) ) { -} - -const FormalRegExpElement & FormalRegExpStructure::getStructure ( ) const { - return * structure; -} - -FormalRegExpElement & FormalRegExpStructure::getStructure ( ) { - return * structure; -} - -void FormalRegExpStructure::setStructure ( const FormalRegExpElement & structure ) { - setStructure ( std::move_copy ( structure ) ); -} - -void FormalRegExpStructure::setStructure ( FormalRegExpElement && param ) { - this->structure = std::smart_ptr < FormalRegExpElement > ( std::move ( param ).plunder ( ) ); -} - -} /* namespace regexp */ - -namespace alib { - -regexp::FormalRegExpStructure xmlApi < regexp::FormalRegExpStructure >::parse ( std::deque < sax::Token >::iterator & input ) { - return regexp::FormalRegExpStructure ( regexp::RegExpFromXMLParser::parseFormalRegExpElement ( input ) ); -} - -bool xmlApi < regexp::FormalRegExpStructure >::first ( const std::deque < sax::Token >::const_iterator & ) { - throw exception::CommonException ( "Unimplemented" ); -} - -void xmlApi < regexp::FormalRegExpStructure >::compose ( std::deque < sax::Token > & output, const regexp::FormalRegExpStructure & data ) { - data.getStructure ( ).accept < void, regexp::RegExpToXMLComposer::Formal > ( output ); -} - -} /* namespace alib */ diff --git a/alib2data/src/regexp/formal/FormalRegExpStructure.h b/alib2data/src/regexp/formal/FormalRegExpStructure.h index 533b7f7bd1..a78998324a 100644 --- a/alib2data/src/regexp/formal/FormalRegExpStructure.h +++ b/alib2data/src/regexp/formal/FormalRegExpStructure.h @@ -9,54 +9,60 @@ #define FORMAL_REG_EXP_STRUCTURE_H_ #include <memory> -#include "FormalRegExpElement.h" -namespace regexp { +#include <exception/CommonException.h> + +#include "../RegExpFeatures.h" +#include "../common/RegExpFromXMLParser.h" +#include "../common/RegExpToXMLComposer.h" + +#include "FormalRegExpEmpty.h" -class UnboundedRegExpStructure; +namespace regexp { /** * Represents formal regular expression structure as parsed from the XML. Regular expression is stored * as a tree of FormalRegExpElement. */ +template < class SymbolType > class FormalRegExpStructure { protected: - std::smart_ptr < FormalRegExpElement > structure; + std::smart_ptr < FormalRegExpElement < SymbolType > > structure; public: explicit FormalRegExpStructure ( ); - explicit FormalRegExpStructure ( FormalRegExpElement && structure ); - explicit FormalRegExpStructure ( const FormalRegExpElement & structure ); - explicit FormalRegExpStructure ( const UnboundedRegExpStructure & other ); + explicit FormalRegExpStructure ( FormalRegExpElement < SymbolType > && structure ); + explicit FormalRegExpStructure ( const FormalRegExpElement < SymbolType > & structure ); + explicit FormalRegExpStructure ( const UnboundedRegExpStructure < SymbolType > & other ); /** * @return Root node of the formal regular expression tree */ - const FormalRegExpElement & getStructure ( ) const; + const FormalRegExpElement < SymbolType > & getStructure ( ) const; /** * @return Root node of the formal regular expression tree */ - FormalRegExpElement & getStructure ( ); + FormalRegExpElement < SymbolType > & getStructure ( ); /** * Sets the root node of the formal regular expression tree * @param structure root node to set */ - void setStructure ( FormalRegExpElement && structure ); - void setStructure ( const FormalRegExpElement & structure ); + void setStructure ( FormalRegExpElement < SymbolType > && structure ); + void setStructure ( const FormalRegExpElement < SymbolType > & structure ); - friend std::ostream & operator << ( std::ostream & out, const FormalRegExpStructure & structure ) { + friend std::ostream & operator << ( std::ostream & out, const FormalRegExpStructure < SymbolType > & structure ) { out << structure.getStructure ( ); return out; } - friend bool operator < ( const FormalRegExpStructure & first, const FormalRegExpStructure & second ) { + friend bool operator < ( const FormalRegExpStructure < SymbolType > & first, const FormalRegExpStructure < SymbolType > & second ) { return first.getStructure().compare(second.getStructure()) < 0; } - friend bool operator == ( const FormalRegExpStructure & first, const FormalRegExpStructure & second ) { + friend bool operator == ( const FormalRegExpStructure < SymbolType > & first, const FormalRegExpStructure < SymbolType > & second ) { return first.getStructure().compare(second.getStructure()) == 0; } }; @@ -65,24 +71,86 @@ public: namespace alib { -template < > -struct xmlApi < regexp::FormalRegExpStructure > { - static regexp::FormalRegExpStructure parse ( std::deque < sax::Token >::iterator & input ); +template < class SymbolType > +struct xmlApi < regexp::FormalRegExpStructure < SymbolType > > { + static regexp::FormalRegExpStructure < SymbolType > parse ( std::deque < sax::Token >::iterator & input ); static bool first ( const std::deque < sax::Token >::const_iterator & input ); - static void compose ( std::deque < sax::Token > & output, const regexp::FormalRegExpStructure & data ); + static void compose ( std::deque < sax::Token > & output, const regexp::FormalRegExpStructure < SymbolType > & data ); }; } /* namespace alib */ namespace std { -template < > -struct compare < regexp::FormalRegExpStructure > { - int operator()(const regexp::FormalRegExpStructure& first, const regexp::FormalRegExpStructure& second) const { +template < class SymbolType > +struct compare < regexp::FormalRegExpStructure < SymbolType > > { + int operator()(const regexp::FormalRegExpStructure < SymbolType > & first, const regexp::FormalRegExpStructure < SymbolType > & second) const { return first.getStructure().compare(second.getStructure()); } }; } /* namespace std */ +#include "../unbounded/UnboundedRegExpStructure.h" + +namespace regexp { + +template < class SymbolType > +FormalRegExpStructure < SymbolType >::FormalRegExpStructure ( FormalRegExpElement < SymbolType > && structure ) : structure ( NULL ) { + setStructure ( std::move ( structure ) ); +} + +template < class SymbolType > +FormalRegExpStructure < SymbolType >::FormalRegExpStructure ( const FormalRegExpElement < SymbolType > & structure ) : FormalRegExpStructure ( std::move_copy ( structure ) ) { +} + +template < class SymbolType > +FormalRegExpStructure < SymbolType >::FormalRegExpStructure ( ) : FormalRegExpStructure ( FormalRegExpEmpty < SymbolType > ( ) ) { +} + +template < class SymbolType > +FormalRegExpStructure < SymbolType >::FormalRegExpStructure ( const UnboundedRegExpStructure < SymbolType > & other ) : FormalRegExpStructure ( * other.getStructure ( ).asFormal ( ) ) { +} + +template < class SymbolType > +const FormalRegExpElement < SymbolType > & FormalRegExpStructure < SymbolType >::getStructure ( ) const { + return * structure; +} + +template < class SymbolType > +FormalRegExpElement < SymbolType > & FormalRegExpStructure < SymbolType >::getStructure ( ) { + return * structure; +} + +template < class SymbolType > +void FormalRegExpStructure < SymbolType >::setStructure ( const FormalRegExpElement < SymbolType > & structure ) { + setStructure ( std::move_copy ( structure ) ); +} + +template < class SymbolType > +void FormalRegExpStructure < SymbolType >::setStructure ( FormalRegExpElement < SymbolType > && param ) { + this->structure = std::smart_ptr < FormalRegExpElement < SymbolType > > ( std::move ( param ).plunder ( ) ); +} + +} /* namespace regexp */ + +namespace alib { + +template < class SymbolType > +regexp::FormalRegExpStructure < SymbolType > xmlApi < regexp::FormalRegExpStructure < SymbolType > >::parse ( std::deque < sax::Token >::iterator & input ) { + return regexp::FormalRegExpStructure < SymbolType > ( regexp::RegExpFromXMLParser::parseFormalRegExpElement < SymbolType > ( input ) ); +} + +template < class SymbolType > +bool xmlApi < regexp::FormalRegExpStructure < SymbolType > >::first ( const std::deque < sax::Token >::const_iterator & ) { + throw exception::CommonException ( "Unimplemented" ); +} + +template < class SymbolType > +void xmlApi < regexp::FormalRegExpStructure < SymbolType > >::compose ( std::deque < sax::Token > & output, const regexp::FormalRegExpStructure < SymbolType > & data ) { + data.getStructure ( ).template accept < void, regexp::RegExpToXMLComposer::Formal > ( output ); +} + +} /* namespace alib */ + #endif /* FORMAL_REG_EXP_STRUCTURE_H_ */ diff --git a/alib2data/src/regexp/formal/FormalRegExpSymbol.cpp b/alib2data/src/regexp/formal/FormalRegExpSymbol.cpp deleted file mode 100644 index 34d57eb8d4..0000000000 --- a/alib2data/src/regexp/formal/FormalRegExpSymbol.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/* - * FormalRegExpSymbol.cpp - * - * Created on: Nov 23, 2013 - * Author: Martin Zak - */ - -#include "FormalRegExpSymbol.h" -#include "../unbounded/UnboundedRegExpSymbol.h" -#include <sstream> - -namespace regexp { - -FormalRegExpSymbol::FormalRegExpSymbol ( int number ) : symbol ( alphabet::symbolFrom ( number ) ) { -} - -FormalRegExpSymbol::FormalRegExpSymbol ( char character ) : symbol ( alphabet::symbolFrom ( character ) ) { -} - -FormalRegExpSymbol::FormalRegExpSymbol ( const std::string & label ) : symbol ( alphabet::symbolFrom ( label ) ) { -} - -FormalRegExpSymbol::FormalRegExpSymbol ( alphabet::Symbol symbol ) : symbol ( std::move ( symbol ) ) { -} - -FormalRegExpElement * FormalRegExpSymbol::clone ( ) const { - return new FormalRegExpSymbol ( * this ); -} - -FormalRegExpElement * FormalRegExpSymbol::plunder ( ) && { - return new FormalRegExpSymbol ( std::move ( * this ) ); -} - -std::smart_ptr < UnboundedRegExpElement > FormalRegExpSymbol::asUnbounded ( ) const { - return std::smart_ptr < UnboundedRegExpElement > ( new UnboundedRegExpSymbol ( this->symbol ) ); -} - -int FormalRegExpSymbol::compare ( const FormalRegExpSymbol & other ) const { - return symbol.getData ( ).compare ( other.symbol.getData ( ) ); -} - -void FormalRegExpSymbol::operator >>( std::ostream & out ) const { - out << "(FormalRegExpSymbol " << symbol << ")"; -} - -bool FormalRegExpSymbol::testSymbol ( const alphabet::Symbol & symbol ) const { - return symbol == this->symbol; -} - -void FormalRegExpSymbol::computeMinimalAlphabet ( std::set < alphabet::Symbol > & alphabet ) const { - alphabet.insert ( symbol ); -} - -bool FormalRegExpSymbol::checkAlphabet ( const std::set < alphabet::Symbol > & alphabet ) const { - return alphabet.count ( symbol ); -} - -const alphabet::Symbol & FormalRegExpSymbol::getSymbol ( ) const { - return symbol; -} - -FormalRegExpSymbol::operator std::string ( ) const { - std::stringstream ss; - ss << * this; - return ss.str ( ); -} - -} /* namespace regexp */ diff --git a/alib2data/src/regexp/formal/FormalRegExpSymbol.h b/alib2data/src/regexp/formal/FormalRegExpSymbol.h index 5847ac8cde..738a3cb8c7 100644 --- a/alib2data/src/regexp/formal/FormalRegExpSymbol.h +++ b/alib2data/src/regexp/formal/FormalRegExpSymbol.h @@ -8,19 +8,21 @@ #ifndef FORMAL_REG_EXP_SYMBOL_H_ #define FORMAL_REG_EXP_SYMBOL_H_ +#include <sstream> + #include "FormalRegExpElement.h" -#include "FormalRegExp.h" namespace regexp { /** * Represents symbol in the regular expression. Contains name of the symbol. */ -class FormalRegExpSymbol : public FormalRegExpElement, public std::NullaryNode < std::smart_ptr < FormalRegExpElement >, std::smart_ptr < const FormalRegExpElement >, FormalRegExpSymbol > { - alphabet::Symbol symbol; +template < class SymbolType > +class FormalRegExpSymbol : public FormalRegExpElement < SymbolType >, public std::NullaryNode < std::smart_ptr < FormalRegExpElement < SymbolType > >, std::smart_ptr < const FormalRegExpElement < SymbolType > >, FormalRegExpSymbol < SymbolType > > { + SymbolType symbol; public: - void accept ( FormalRegExpElement::Visitor & visitor ) const { + void accept ( typename FormalRegExpElement < SymbolType >::Visitor & visitor ) const override { visitor.visit ( * this ); } @@ -28,59 +30,133 @@ public: explicit FormalRegExpSymbol ( char character ); explicit FormalRegExpSymbol ( const std::string & label ); - explicit FormalRegExpSymbol ( alphabet::Symbol symbol ); + explicit FormalRegExpSymbol ( SymbolType symbol ); /** * @copydoc FormalRegExpElement::clone() const */ - virtual FormalRegExpElement * clone ( ) const; + FormalRegExpElement < SymbolType > * clone ( ) const override; /** * @copydoc FormalRegExpElement::plunder() const */ - virtual FormalRegExpElement * plunder ( ) &&; + FormalRegExpElement < SymbolType > * plunder ( ) && override; /** * @copydoc FormalRegExpElement::clone() const */ - virtual std::smart_ptr < UnboundedRegExpElement > asUnbounded ( ) const; + std::smart_ptr < UnboundedRegExpElement < SymbolType > > asUnbounded ( ) const override; /** * @copydoc FormalRegExpElement::testSymbol() const */ - virtual bool testSymbol ( const alphabet::Symbol & symbol ) const; + bool testSymbol ( const SymbolType & symbol ) const override; /** * @copydoc FormalRegExpElement::computeMinimalAlphabet() */ - virtual void computeMinimalAlphabet ( std::set < alphabet::Symbol > & alphabet ) const; + void computeMinimalAlphabet ( std::set < SymbolType > & alphabet ) const override; /** * @copydoc FormalRegExpElement::checkAlphabet() */ - virtual bool checkAlphabet ( const std::set < alphabet::Symbol > & alphabet ) const; + bool checkAlphabet ( const std::set < SymbolType > & alphabet ) const override; - virtual int compare ( const FormalRegExpElement & other ) const { + int compare ( const FormalRegExpElement < SymbolType > & other ) const override { if ( std::type_index ( typeid ( * this ) ) == std::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other ); return std::type_index ( typeid ( * this ) ) - std::type_index ( typeid ( other ) ); } - virtual int compare ( const FormalRegExpSymbol & ) const; + int compare ( const FormalRegExpSymbol & ) const; /** * @copydoc FormalRegExpElement::operator>>() const */ - virtual void operator >>( std::ostream & out ) const; + void operator >>( std::ostream & out ) const override; /** * Returns the string representation of RegExp Symbol. */ - const alphabet::Symbol & getSymbol ( ) const; + const SymbolType & getSymbol ( ) const; - virtual explicit operator std::string ( ) const; + explicit operator std::string ( ) const override; }; } /* namespace regexp */ +#include "../unbounded/UnboundedRegExpSymbol.h" + +namespace regexp { + +template < class SymbolType > +FormalRegExpSymbol < SymbolType >::FormalRegExpSymbol ( int number ) : symbol ( alphabet::symbolFrom ( number ) ) { +} + +template < class SymbolType > +FormalRegExpSymbol < SymbolType >::FormalRegExpSymbol ( char character ) : symbol ( alphabet::symbolFrom ( character ) ) { +} + +template < class SymbolType > +FormalRegExpSymbol < SymbolType >::FormalRegExpSymbol ( const std::string & label ) : symbol ( alphabet::symbolFrom ( label ) ) { +} + +template < class SymbolType > +FormalRegExpSymbol < SymbolType >::FormalRegExpSymbol ( SymbolType symbol ) : symbol ( std::move ( symbol ) ) { +} + +template < class SymbolType > +FormalRegExpElement < SymbolType > * FormalRegExpSymbol < SymbolType >::clone ( ) const { + return new FormalRegExpSymbol ( * this ); +} + +template < class SymbolType > +FormalRegExpElement < SymbolType > * FormalRegExpSymbol < SymbolType >::plunder ( ) && { + return new FormalRegExpSymbol ( std::move ( * this ) ); +} + +template < class SymbolType > +std::smart_ptr < UnboundedRegExpElement < SymbolType > > FormalRegExpSymbol < SymbolType >::asUnbounded ( ) const { + return std::smart_ptr < UnboundedRegExpElement < SymbolType > > ( new UnboundedRegExpSymbol < SymbolType > ( this->symbol ) ); +} + +template < class SymbolType > +int FormalRegExpSymbol < SymbolType >::compare ( const FormalRegExpSymbol & other ) const { + return symbol.getData ( ).compare ( other.symbol.getData ( ) ); +} + +template < class SymbolType > +void FormalRegExpSymbol < SymbolType >::operator >>( std::ostream & out ) const { + out << "(FormalRegExpSymbol " << symbol << ")"; +} + +template < class SymbolType > +bool FormalRegExpSymbol < SymbolType >::testSymbol ( const SymbolType & symbol ) const { + return symbol == this->symbol; +} + +template < class SymbolType > +void FormalRegExpSymbol < SymbolType >::computeMinimalAlphabet ( std::set < SymbolType > & alphabet ) const { + alphabet.insert ( symbol ); +} + +template < class SymbolType > +bool FormalRegExpSymbol < SymbolType >::checkAlphabet ( const std::set < SymbolType > & alphabet ) const { + return alphabet.count ( symbol ); +} + +template < class SymbolType > +const SymbolType & FormalRegExpSymbol < SymbolType >::getSymbol ( ) const { + return symbol; +} + +template < class SymbolType > +FormalRegExpSymbol < SymbolType >::operator std::string ( ) const { + std::stringstream ss; + ss << * this; + return ss.str ( ); +} + +} /* namespace regexp */ + #endif /* FORMAL_REG_EXP_SYMBOL_H_ */ diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExp.cpp b/alib2data/src/regexp/unbounded/UnboundedRegExp.cpp index 8444ae4e01..afe48d961a 100644 --- a/alib2data/src/regexp/unbounded/UnboundedRegExp.cpp +++ b/alib2data/src/regexp/unbounded/UnboundedRegExp.cpp @@ -6,109 +6,17 @@ */ #include "UnboundedRegExp.h" -#include <exception/CommonException.h> -#include "UnboundedRegExpEmpty.h" - -#include <iostream> -#include <algorithm> -#include <sstream> - -#include "../formal/FormalRegExp.h" - -#include <sax/FromXMLParserHelper.h> -#include "../common/RegExpFromXMLParser.h" -#include "../common/RegExpToXMLComposer.h" #include "../RegExp.h" #include <object/Object.h> #include <core/xmlApi.hpp> #include <core/castApi.hpp> -namespace regexp { - -UnboundedRegExp::UnboundedRegExp ( std::set < alphabet::Symbol > alphabet, UnboundedRegExpStructure regExp ) : std::Components < UnboundedRegExp, alphabet::Symbol, std::tuple < GeneralAlphabet >, std::tuple < > > ( std::make_tuple ( std::move ( alphabet ) ), std::tuple < > ( ) ), regExp ( std::move ( regExp ) ) { - if ( !this->regExp.getStructure ( ).checkAlphabet ( getAlphabet ( ) ) ) - throw exception::CommonException ( "Input symbols not in the alphabet." ); -} - -UnboundedRegExp::UnboundedRegExp ( ) : UnboundedRegExp ( std::set < alphabet::Symbol > ( ), UnboundedRegExpStructure ( ) ) { -} - -UnboundedRegExp::UnboundedRegExp ( UnboundedRegExpStructure regExp ) : UnboundedRegExp ( regExp.getStructure ( ).computeMinimalAlphabet ( ), regExp ) { -} - -UnboundedRegExp::UnboundedRegExp ( const FormalRegExp & other ) : UnboundedRegExp ( other.getAlphabet ( ), UnboundedRegExpStructure ( other.getRegExp ( ) ) ) { -} - -RegExpBase * UnboundedRegExp::clone ( ) const { - return new UnboundedRegExp ( * this ); -} - -RegExpBase * UnboundedRegExp::plunder ( ) && { - return new UnboundedRegExp ( std::move ( * this ) ); -} - -const UnboundedRegExpStructure & UnboundedRegExp::getRegExp ( ) const { - return regExp; -} - -UnboundedRegExpStructure & UnboundedRegExp::getRegExp ( ) { - return regExp; -} - -void UnboundedRegExp::setRegExp ( UnboundedRegExpStructure param ) { - if ( !param.getStructure ( ).checkAlphabet ( getAlphabet ( ) ) ) - throw exception::CommonException ( "Input symbols not in the alphabet." ); - - this->regExp = std::move ( param ); -} - -void UnboundedRegExp::operator >>( std::ostream & out ) const { - out << "(UnboundedRegExp " << this->regExp.getStructure ( ) << ")"; -} - -int UnboundedRegExp::compare ( const UnboundedRegExp & other ) const { - auto first = std::tie ( regExp.getStructure ( ), getAlphabet ( ) ); - auto second = std::tie ( other.regExp.getStructure ( ), other.getAlphabet ( ) ); - - std::compare < decltype ( first ) > comp; - - return comp ( first, second ); -} - -UnboundedRegExp::operator std::string ( ) const { - std::stringstream ss; - ss << * this; - return ss.str ( ); -} - -UnboundedRegExp UnboundedRegExp::parse ( std::deque < sax::Token >::iterator & input ) { - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, UnboundedRegExp::getXmlTagName() ); - - std::set < alphabet::Symbol > alphabet = RegExpFromXMLParser::parseAlphabet ( input ); - - UnboundedRegExpStructure structure ( alib::xmlApi < regexp::UnboundedRegExpStructure >::parse ( input ) ); - UnboundedRegExp regexp ( std::move ( alphabet ), std::move ( structure ) ); - - sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, UnboundedRegExp::getXmlTagName() ); - - return regexp; -} - -void UnboundedRegExp::compose ( std::deque < sax::Token > & out ) const { - out.emplace_back ( UnboundedRegExp::getXmlTagName(), sax::Token::TokenType::START_ELEMENT ); - RegExpToXMLComposer::composeAlphabet ( out, this->getAlphabet ( ) ); - alib::xmlApi < UnboundedRegExpStructure >::compose ( out, this->getRegExp ( ) ); - out.emplace_back ( UnboundedRegExp::getXmlTagName(), sax::Token::TokenType::END_ELEMENT ); -} - -} /* namespace regexp */ - namespace alib { -auto unboundedRegExpParserRegister = xmlApi < regexp::RegExp >::ParserRegister < regexp::UnboundedRegExp > ( ); -auto unboundedRegExpParserRegister2 = xmlApi < alib::Object >::ParserRegister < regexp::UnboundedRegExp > ( ); +auto unboundedRegExpParserRegister = xmlApi < regexp::RegExp >::ParserRegister < regexp::UnboundedRegExp < > > ( ); +auto unboundedRegExpParserRegister2 = xmlApi < alib::Object >::ParserRegister < regexp::UnboundedRegExp < > > ( ); -auto UnboundedRegExpFromFormalRegExp = castApi::CastRegister < regexp::UnboundedRegExp, regexp::FormalRegExp > ( ); -auto UnboundedRegExpCastBinder = castApi::CastPoolStringBinder < regexp::UnboundedRegExp > ( regexp::UnboundedRegExp::getXmlTagName() ); +auto UnboundedRegExpFromFormalRegExp = castApi::CastRegister < regexp::UnboundedRegExp < >, regexp::FormalRegExp < > > ( ); +auto UnboundedRegExpCastBinder = castApi::CastPoolStringBinder < regexp::UnboundedRegExp < > > ( regexp::UnboundedRegExp < >::getXmlTagName() ); } /* namespace alib */ diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExp.h b/alib2data/src/regexp/unbounded/UnboundedRegExp.h index eab1d42ded..9560e85a3b 100644 --- a/alib2data/src/regexp/unbounded/UnboundedRegExp.h +++ b/alib2data/src/regexp/unbounded/UnboundedRegExp.h @@ -10,24 +10,34 @@ #include <string> #include <set> +#include <iostream> +#include <algorithm> +#include <sstream> + #include <core/components.hpp> +#include <sax/FromXMLParserHelper.h> +#include <exception/CommonException.h> #include "../RegExpBase.h" +#include "../RegExpFeatures.h" +#include "../common/RegExpFromXMLParser.h" +#include "../common/RegExpToXMLComposer.h" + #include "UnboundedRegExpStructure.h" -#include "../../alphabet/Symbol.h" +#include "UnboundedRegExpEmpty.h" namespace regexp { -class FormalRegExp; class GeneralAlphabet; /** * Represents regular expression parsed from the XML. Regular expression is stored * as a tree of RegExpElement. */ -class UnboundedRegExp : public RegExpBase, public std::Components < UnboundedRegExp, alphabet::Symbol, std::tuple < GeneralAlphabet >, std::tuple < > > { +template < class SymbolType > +class UnboundedRegExp : public RegExpBase, public std::Components < UnboundedRegExp < SymbolType >, SymbolType, std::tuple < GeneralAlphabet >, std::tuple < > > { protected: - UnboundedRegExpStructure regExp; + UnboundedRegExpStructure < SymbolType > regExp; public: /** @@ -42,28 +52,28 @@ public: explicit UnboundedRegExp ( ); - explicit UnboundedRegExp ( std::set < alphabet::Symbol > alphabet, UnboundedRegExpStructure regExp ); - explicit UnboundedRegExp ( UnboundedRegExpStructure regExp ); - explicit UnboundedRegExp ( const FormalRegExp & other ); + explicit UnboundedRegExp ( std::set < SymbolType > alphabet, UnboundedRegExpStructure < SymbolType > regExp ); + explicit UnboundedRegExp ( UnboundedRegExpStructure < SymbolType > regExp ); + explicit UnboundedRegExp ( const FormalRegExp < SymbolType > & other ); /** * @return Root node of the regular expression tree */ - const UnboundedRegExpStructure & getRegExp ( ) const; + const UnboundedRegExpStructure < SymbolType > & getRegExp ( ) const; /** * @return Root node of the regular expression tree */ - UnboundedRegExpStructure & getRegExp ( ); + UnboundedRegExpStructure < SymbolType > & getRegExp ( ); /** * Sets the root node of the regular expression tree * @param regExp root node to set */ - void setRegExp ( UnboundedRegExpStructure regExp ); + void setRegExp ( UnboundedRegExpStructure < SymbolType > regExp ); - virtual const std::set < alphabet::Symbol > & getAlphabet ( ) const { - return accessComponent < GeneralAlphabet > ( ).get ( ); + virtual const std::set < SymbolType > & getAlphabet ( ) const { + return this->template accessComponent < GeneralAlphabet > ( ).get ( ); } /** @@ -96,20 +106,116 @@ public: } /* namespace regexp */ +#include "../formal/FormalRegExp.h" + +namespace regexp { + +template < class SymbolType > +UnboundedRegExp < SymbolType >::UnboundedRegExp ( std::set < SymbolType > alphabet, UnboundedRegExpStructure < SymbolType > regExp ) : std::Components < UnboundedRegExp, SymbolType, std::tuple < GeneralAlphabet >, std::tuple < > > ( std::make_tuple ( std::move ( alphabet ) ), std::tuple < > ( ) ), regExp ( std::move ( regExp ) ) { + if ( !this->regExp.getStructure ( ).checkAlphabet ( getAlphabet ( ) ) ) + throw exception::CommonException ( "Input symbols not in the alphabet." ); +} + +template < class SymbolType > +UnboundedRegExp < SymbolType >::UnboundedRegExp ( ) : UnboundedRegExp ( std::set < SymbolType > ( ), UnboundedRegExpStructure < SymbolType > ( ) ) { +} + +template < class SymbolType > +UnboundedRegExp < SymbolType >::UnboundedRegExp ( UnboundedRegExpStructure < SymbolType > regExp ) : UnboundedRegExp ( regExp.getStructure ( ).computeMinimalAlphabet ( ), regExp ) { +} + +template < class SymbolType > +UnboundedRegExp < SymbolType >::UnboundedRegExp ( const FormalRegExp < SymbolType > & other ) : UnboundedRegExp ( other.getAlphabet ( ), UnboundedRegExpStructure < SymbolType > ( other.getRegExp ( ) ) ) { +} + +template < class SymbolType > +RegExpBase * UnboundedRegExp < SymbolType >::clone ( ) const { + return new UnboundedRegExp ( * this ); +} + +template < class SymbolType > +RegExpBase * UnboundedRegExp < SymbolType >::plunder ( ) && { + return new UnboundedRegExp ( std::move ( * this ) ); +} + +template < class SymbolType > +const UnboundedRegExpStructure < SymbolType > & UnboundedRegExp < SymbolType >::getRegExp ( ) const { + return regExp; +} + +template < class SymbolType > +UnboundedRegExpStructure < SymbolType > & UnboundedRegExp < SymbolType >::getRegExp ( ) { + return regExp; +} + +template < class SymbolType > +void UnboundedRegExp < SymbolType >::setRegExp ( UnboundedRegExpStructure < SymbolType > param ) { + if ( !param.getStructure ( ).checkAlphabet ( getAlphabet ( ) ) ) + throw exception::CommonException ( "Input symbols not in the alphabet." ); + + this->regExp = std::move ( param ); +} + +template < class SymbolType > +void UnboundedRegExp < SymbolType >::operator >>( std::ostream & out ) const { + out << "(UnboundedRegExp " << this->regExp.getStructure ( ) << ")"; +} + +template < class SymbolType > +int UnboundedRegExp < SymbolType >::compare ( const UnboundedRegExp & other ) const { + auto first = std::tie ( regExp.getStructure ( ), getAlphabet ( ) ); + auto second = std::tie ( other.regExp.getStructure ( ), other.getAlphabet ( ) ); + + std::compare < decltype ( first ) > comp; + + return comp ( first, second ); +} + +template < class SymbolType > +UnboundedRegExp < SymbolType >::operator std::string ( ) const { + std::stringstream ss; + ss << * this; + return ss.str ( ); +} + +template < class SymbolType > +UnboundedRegExp < SymbolType > UnboundedRegExp < SymbolType >::parse ( std::deque < sax::Token >::iterator & input ) { + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, UnboundedRegExp::getXmlTagName() ); + + std::set < SymbolType > alphabet = RegExpFromXMLParser::parseAlphabet < SymbolType > ( input ); + + UnboundedRegExpStructure < SymbolType > structure ( alib::xmlApi < regexp::UnboundedRegExpStructure < SymbolType > >::parse ( input ) ); + UnboundedRegExp < SymbolType > regexp ( std::move ( alphabet ), std::move ( structure ) ); + + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, UnboundedRegExp::getXmlTagName() ); + + return regexp; +} + +template < class SymbolType > +void UnboundedRegExp < SymbolType >::compose ( std::deque < sax::Token > & out ) const { + out.emplace_back ( UnboundedRegExp::getXmlTagName(), sax::Token::TokenType::START_ELEMENT ); + RegExpToXMLComposer::composeAlphabet ( out, this->getAlphabet ( ) ); + alib::xmlApi < UnboundedRegExpStructure < SymbolType > >::compose ( out, this->getRegExp ( ) ); + out.emplace_back ( UnboundedRegExp::getXmlTagName(), sax::Token::TokenType::END_ELEMENT ); +} + +} /* namespace regexp */ + namespace std { -template < > -class ComponentConstraint< regexp::UnboundedRegExp, alphabet::Symbol, regexp::GeneralAlphabet > { +template < class SymbolType > +class ComponentConstraint< regexp::UnboundedRegExp < SymbolType >, SymbolType, regexp::GeneralAlphabet > { public: - static bool used ( const regexp::UnboundedRegExp & regexp, const alphabet::Symbol & symbol ) { + static bool used ( const regexp::UnboundedRegExp < SymbolType > & regexp, const SymbolType & symbol ) { return regexp.getRegExp ( ).getStructure ( ).testSymbol ( symbol ); } - static bool available ( const regexp::UnboundedRegExp &, const alphabet::Symbol & ) { + static bool available ( const regexp::UnboundedRegExp < SymbolType > &, const SymbolType & ) { return true; } - static void valid ( const regexp::UnboundedRegExp &, const alphabet::Symbol & ) { + static void valid ( const regexp::UnboundedRegExp < SymbolType > &, const SymbolType & ) { } }; diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpAlternation.cpp b/alib2data/src/regexp/unbounded/UnboundedRegExpAlternation.cpp deleted file mode 100644 index 1569a3fc13..0000000000 --- a/alib2data/src/regexp/unbounded/UnboundedRegExpAlternation.cpp +++ /dev/null @@ -1,95 +0,0 @@ -/* - * UnboundedRegExpAlternation.cpp - * - * Created on: Nov 23, 2013 - * Author: Martin Zak - */ - -#include "UnboundedRegExpAlternation.h" -#include <exception/CommonException.h> -#include "../formal/FormalRegExpAlternation.h" -#include "../formal/FormalRegExpEmpty.h" -#include <sstream> - -namespace regexp { - -UnboundedRegExpAlternation::UnboundedRegExpAlternation ( ) { -} - -const std::vector < std::smart_ptr < const UnboundedRegExpElement > > & UnboundedRegExpAlternation::getElements ( ) const { - return getChildren(); -} - -const std::vector < std::smart_ptr < UnboundedRegExpElement > > & UnboundedRegExpAlternation::getElements ( ) { - return getChildren(); -} - -void UnboundedRegExpAlternation::appendElement ( UnboundedRegExpElement && element ) { - pushBackChild ( std::smart_ptr < UnboundedRegExpElement > ( std::move ( element ).plunder ( ) ) ); -} - -void UnboundedRegExpAlternation::appendElement ( const UnboundedRegExpElement & element ) { - appendElement ( std::move_copy ( element ) ); -} - -UnboundedRegExpElement * UnboundedRegExpAlternation::clone ( ) const { - return new UnboundedRegExpAlternation ( * this ); -} - -UnboundedRegExpElement * UnboundedRegExpAlternation::plunder ( ) && { - return new UnboundedRegExpAlternation ( std::move ( * this ) ); -} - -std::smart_ptr < FormalRegExpElement > UnboundedRegExpAlternation::asFormal ( ) const { - if ( getElements ( ).size ( ) == 0 ) return std::smart_ptr < FormalRegExpElement > ( new FormalRegExpEmpty ( ) ); - - std::smart_ptr < FormalRegExpElement > res = getElements ( )[getElements ( ).size ( ) - 1]->asFormal ( ); - - for ( unsigned i = getElements ( ).size ( ) - 1; i >= 1; i-- ) - res = std::smart_ptr < FormalRegExpElement > ( new FormalRegExpAlternation ( std::move ( * getElements ( )[i - 1]->asFormal ( ) ), std::move ( * res ) ) ); - - return res; -} - -int UnboundedRegExpAlternation::compare ( const UnboundedRegExpAlternation & other ) const { - std::compare < std::decay < decltype ( getElements ( ) ) >::type > comp; - - return comp ( getElements ( ), other.getElements ( ) ); -} - -void UnboundedRegExpAlternation::operator >>( std::ostream & out ) const { - out << "(UnboundedRegExpAlternation"; - - for ( const auto & child : getElements() ) - out << " " << * child; - - out << ")"; -} - -bool UnboundedRegExpAlternation::testSymbol ( const alphabet::Symbol & symbol ) const { - for ( const auto & child : getElements() ) - if ( child->testSymbol ( symbol ) ) return true; - - return false; -} - -bool UnboundedRegExpAlternation::checkAlphabet ( const std::set < alphabet::Symbol > & alphabet ) const { - for ( const auto & child : getElements() ) - if ( !child->checkAlphabet ( alphabet ) ) return false; - - return true; -} - - -void UnboundedRegExpAlternation::computeMinimalAlphabet ( std::set < alphabet::Symbol > & alphabet ) const { - for ( const auto & child : getElements() ) - child->computeMinimalAlphabet ( alphabet ); -} - -UnboundedRegExpAlternation::operator std::string ( ) const { - std::stringstream ss; - ss << * this; - return ss.str ( ); -} - -} /* namespace regexp */ diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpAlternation.h b/alib2data/src/regexp/unbounded/UnboundedRegExpAlternation.h index fdccbd1b19..91be2c2f31 100644 --- a/alib2data/src/regexp/unbounded/UnboundedRegExpAlternation.h +++ b/alib2data/src/regexp/unbounded/UnboundedRegExpAlternation.h @@ -9,6 +9,10 @@ #define UNBOUNDED_REG_EXP_ALTERNATION_H_ #include <vector> +#include <sstream> + +#include <exception/CommonException.h> + #include "UnboundedRegExpElement.h" namespace regexp { @@ -17,9 +21,10 @@ namespace regexp { * Represents alternation operator in the regular expression. Contains list of UnboundedRegExpElement * as operands of the operator. */ -class UnboundedRegExpAlternation : public UnboundedRegExpElement, public std::VararyNode < std::smart_ptr < UnboundedRegExpElement >, std::smart_ptr < const UnboundedRegExpElement >, UnboundedRegExpAlternation > { +template < class SymbolType > +class UnboundedRegExpAlternation : public UnboundedRegExpElement < SymbolType >, public std::VararyNode < std::smart_ptr < UnboundedRegExpElement < SymbolType > >, std::smart_ptr < const UnboundedRegExpElement < SymbolType > >, UnboundedRegExpAlternation < SymbolType > > { public: - void accept ( UnboundedRegExpElement::Visitor & visitor ) const { + void accept ( typename UnboundedRegExpElement < SymbolType >::Visitor & visitor ) const override { visitor.visit ( * this ); } @@ -28,65 +33,164 @@ public: /** * @copydoc UnboundedRegExpElement::clone() const */ - virtual UnboundedRegExpElement * clone ( ) const; + UnboundedRegExpElement < SymbolType > * clone ( ) const override; /** * @copydoc UnboundedRegExpElement::plunder() const */ - virtual UnboundedRegExpElement * plunder ( ) &&; + UnboundedRegExpElement < SymbolType > * plunder ( ) && override; /** * @copydoc UnboundedRegExpElement::cloneAsFormal() const */ - virtual std::smart_ptr < FormalRegExpElement > asFormal ( ) const; + std::smart_ptr < FormalRegExpElement < SymbolType > > asFormal ( ) const override; /** * @copydoc UnboundedRegExpElement::testSymbol() const */ - virtual bool testSymbol ( const alphabet::Symbol & symbol ) const; + bool testSymbol ( const SymbolType & symbol ) const override; /** * @copydoc UnboundedRegExpElement::computeMinimalAlphabet() */ - virtual void computeMinimalAlphabet ( std::set < alphabet::Symbol > & alphabet ) const; + void computeMinimalAlphabet ( std::set < SymbolType > & alphabet ) const override; /** * @copydoc UnboundedRegExpElement::checkAlphabet() */ - virtual bool checkAlphabet ( const std::set < alphabet::Symbol > & alphabet ) const; + bool checkAlphabet ( const std::set < SymbolType > & alphabet ) const override; /** * @return elements */ - const std::vector < std::smart_ptr < const UnboundedRegExpElement > > & getElements ( ) const; + const std::vector < std::smart_ptr < const UnboundedRegExpElement < SymbolType > > > & getElements ( ) const; /** * @return elements */ - const std::vector < std::smart_ptr < UnboundedRegExpElement > > & getElements ( ); + const std::vector < std::smart_ptr < UnboundedRegExpElement < SymbolType > > > & getElements ( ); /** * @param element to append */ - void appendElement ( UnboundedRegExpElement && element ); - void appendElement ( const UnboundedRegExpElement & element ); + void appendElement ( UnboundedRegExpElement < SymbolType > && element ); + void appendElement ( const UnboundedRegExpElement < SymbolType > & element ); - virtual int compare ( const UnboundedRegExpElement & other ) const { + int compare ( const UnboundedRegExpElement < SymbolType > & other ) const override { if ( std::type_index ( typeid ( * this ) ) == std::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other ); return std::type_index ( typeid ( * this ) ) - std::type_index ( typeid ( other ) ); } - virtual int compare ( const UnboundedRegExpAlternation & ) const; + int compare ( const UnboundedRegExpAlternation < SymbolType > & ) const; /** * @copydoc UnboundedRegExpElement::operator>>() const */ - virtual void operator >>( std::ostream & out ) const; + void operator >>( std::ostream & out ) const override; - virtual explicit operator std::string ( ) const; + explicit operator std::string ( ) const override; }; } /* namespace regexp */ +#include "../formal/FormalRegExpAlternation.h" +#include "../formal/FormalRegExpEmpty.h" + +namespace regexp { + +template < class SymbolType > +UnboundedRegExpAlternation < SymbolType >::UnboundedRegExpAlternation ( ) { +} + +template < class SymbolType > +const std::vector < std::smart_ptr < const UnboundedRegExpElement < SymbolType > > > & UnboundedRegExpAlternation < SymbolType >::getElements ( ) const { + return this->getChildren(); +} + +template < class SymbolType > +const std::vector < std::smart_ptr < UnboundedRegExpElement < SymbolType > > > & UnboundedRegExpAlternation < SymbolType >::getElements ( ) { + return this->getChildren(); +} + +template < class SymbolType > +void UnboundedRegExpAlternation < SymbolType >::appendElement ( UnboundedRegExpElement < SymbolType > && element ) { + this->pushBackChild ( std::smart_ptr < UnboundedRegExpElement < SymbolType > > ( std::move ( element ).plunder ( ) ) ); +} + +template < class SymbolType > +void UnboundedRegExpAlternation < SymbolType >::appendElement ( const UnboundedRegExpElement < SymbolType > & element ) { + this->appendElement ( std::move_copy ( element ) ); +} + +template < class SymbolType > +UnboundedRegExpElement < SymbolType > * UnboundedRegExpAlternation < SymbolType >::clone ( ) const { + return new UnboundedRegExpAlternation ( * this ); +} + +template < class SymbolType > +UnboundedRegExpElement < SymbolType > * UnboundedRegExpAlternation < SymbolType >::plunder ( ) && { + return new UnboundedRegExpAlternation ( std::move ( * this ) ); +} + +template < class SymbolType > +std::smart_ptr < FormalRegExpElement < SymbolType > > UnboundedRegExpAlternation < SymbolType >::asFormal ( ) const { + if ( getElements ( ).size ( ) == 0 ) return std::smart_ptr < FormalRegExpElement < SymbolType > > ( new FormalRegExpEmpty < SymbolType > ( ) ); + + std::smart_ptr < FormalRegExpElement < SymbolType > > res = getElements ( )[getElements ( ).size ( ) - 1]->asFormal ( ); + + for ( unsigned i = getElements ( ).size ( ) - 1; i >= 1; i-- ) + res = std::smart_ptr < FormalRegExpElement < SymbolType > > ( new FormalRegExpAlternation < SymbolType > ( std::move ( * getElements ( )[i - 1]->asFormal ( ) ), std::move ( * res ) ) ); + + return res; +} + +template < class SymbolType > +int UnboundedRegExpAlternation < SymbolType >::compare ( const UnboundedRegExpAlternation < SymbolType > & other ) const { + std::compare < typename std::decay < decltype ( getElements ( ) ) >::type > comp; + + return comp ( getElements ( ), other.getElements ( ) ); +} + +template < class SymbolType > +void UnboundedRegExpAlternation < SymbolType >::operator >>( std::ostream & out ) const { + out << "(UnboundedRegExpAlternation"; + + for ( const auto & child : getElements() ) + out << " " << * child; + + out << ")"; +} + +template < class SymbolType > +bool UnboundedRegExpAlternation < SymbolType >::testSymbol ( const SymbolType & symbol ) const { + for ( const auto & child : getElements() ) + if ( child->testSymbol ( symbol ) ) return true; + + return false; +} + +template < class SymbolType > +bool UnboundedRegExpAlternation < SymbolType >::checkAlphabet ( const std::set < SymbolType > & alphabet ) const { + for ( const auto & child : getElements() ) + if ( !child->checkAlphabet ( alphabet ) ) return false; + + return true; +} + +template < class SymbolType > +void UnboundedRegExpAlternation < SymbolType >::computeMinimalAlphabet ( std::set < SymbolType > & alphabet ) const { + for ( const auto & child : getElements() ) + child->computeMinimalAlphabet ( alphabet ); +} + +template < class SymbolType > +UnboundedRegExpAlternation < SymbolType >::operator std::string ( ) const { + std::stringstream ss; + ss << * this; + return ss.str ( ); +} + +} /* namespace regexp */ + #endif /* UNBOUNDED_REG_EXP_ALTERNATION_H_ */ diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpConcatenation.cpp b/alib2data/src/regexp/unbounded/UnboundedRegExpConcatenation.cpp deleted file mode 100644 index 51fba8684d..0000000000 --- a/alib2data/src/regexp/unbounded/UnboundedRegExpConcatenation.cpp +++ /dev/null @@ -1,94 +0,0 @@ -/* - * UnboundedRegExpConcatenation.cpp - * - * Created on: Nov 27, 2013 - * Author: Martin Zak - */ - -#include "UnboundedRegExpConcatenation.h" -#include <exception/CommonException.h> -#include "../formal/FormalRegExpConcatenation.h" -#include "../formal/FormalRegExpEpsilon.h" -#include <sstream> - -namespace regexp { - -UnboundedRegExpConcatenation::UnboundedRegExpConcatenation ( ) { -} - -const std::vector < std::smart_ptr < const UnboundedRegExpElement > > & UnboundedRegExpConcatenation::getElements ( ) const { - return getChildren(); -} - -const std::vector < std::smart_ptr < UnboundedRegExpElement > > & UnboundedRegExpConcatenation::getElements ( ) { - return getChildren(); -} - -void UnboundedRegExpConcatenation::appendElement ( UnboundedRegExpElement && element ) { - pushBackChild ( std::smart_ptr < UnboundedRegExpElement > ( std::move ( element ).plunder ( ) ) ); -} - -void UnboundedRegExpConcatenation::appendElement ( const UnboundedRegExpElement & element ) { - appendElement ( std::move_copy ( element ) ); -} - -UnboundedRegExpElement * UnboundedRegExpConcatenation::clone ( ) const { - return new UnboundedRegExpConcatenation ( * this ); -} - -UnboundedRegExpElement * UnboundedRegExpConcatenation::plunder ( ) && { - return new UnboundedRegExpConcatenation ( std::move ( * this ) ); -} - -std::smart_ptr < FormalRegExpElement > UnboundedRegExpConcatenation::asFormal ( ) const { - if ( getElements ( ).size ( ) == 0 ) return std::smart_ptr < FormalRegExpElement > ( new FormalRegExpEpsilon ( ) ); - - std::smart_ptr < FormalRegExpElement > res = getElements ( )[getElements ( ).size ( ) - 1]->asFormal ( ); - - for ( unsigned i = getElements ( ).size ( ) - 1; i >= 1; i-- ) - res = std::smart_ptr < FormalRegExpElement > ( new FormalRegExpConcatenation ( std::move ( * getElements ( )[i - 1]->asFormal ( ) ), std::move ( * res ) ) ); - - return res; -} - -int UnboundedRegExpConcatenation::compare ( const UnboundedRegExpConcatenation & other ) const { - std::compare < std::decay < decltype ( getElements ( ) ) >::type > comp; - - return comp ( getElements ( ), other.getElements ( ) ); -} - -void UnboundedRegExpConcatenation::operator >>( std::ostream & out ) const { - out << "(UnboundedRegExpConcatenation"; - - for ( const auto & child : getElements() ) - out << " " << * child; - - out << ")"; -} - -bool UnboundedRegExpConcatenation::testSymbol ( const alphabet::Symbol & symbol ) const { - for ( const auto & child : getElements() ) - if ( child->testSymbol ( symbol ) ) return true; - - return false; -} - -bool UnboundedRegExpConcatenation::checkAlphabet ( const std::set < alphabet::Symbol > & alphabet ) const { - for ( const auto & child : getElements() ) - if ( !child->checkAlphabet ( alphabet ) ) return false; - - return true; -} - -void UnboundedRegExpConcatenation::computeMinimalAlphabet ( std::set < alphabet::Symbol > & alphabet ) const { - for ( const auto & child : getElements() ) - child->computeMinimalAlphabet ( alphabet ); -} - -UnboundedRegExpConcatenation::operator std::string ( ) const { - std::stringstream ss; - ss << * this; - return ss.str ( ); -} - -} /* namespace regexp */ diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpConcatenation.h b/alib2data/src/regexp/unbounded/UnboundedRegExpConcatenation.h index f077f872b3..f3ef801e1a 100644 --- a/alib2data/src/regexp/unbounded/UnboundedRegExpConcatenation.h +++ b/alib2data/src/regexp/unbounded/UnboundedRegExpConcatenation.h @@ -9,6 +9,10 @@ #define UNBOUNDED_REG_EXP_CONCATENATION_H_ #include <vector> +#include <sstream> + +#include <exception/CommonException.h> + #include "UnboundedRegExpElement.h" namespace regexp { @@ -17,9 +21,10 @@ namespace regexp { * Represents concatenation operator in the regular expression. Contains list of UnboundedRegExpElement * as operands of the operator. */ -class UnboundedRegExpConcatenation : public UnboundedRegExpElement, public std::VararyNode < std::smart_ptr < UnboundedRegExpElement >, std::smart_ptr < const UnboundedRegExpElement >, UnboundedRegExpConcatenation > { +template < class SymbolType > +class UnboundedRegExpConcatenation : public UnboundedRegExpElement < SymbolType >, public std::VararyNode < std::smart_ptr < UnboundedRegExpElement < SymbolType > >, std::smart_ptr < const UnboundedRegExpElement < SymbolType > >, UnboundedRegExpConcatenation < SymbolType > > { public: - void accept ( UnboundedRegExpElement::Visitor & visitor ) const { + void accept ( typename UnboundedRegExpElement < SymbolType >::Visitor & visitor ) const override { visitor.visit ( * this ); } @@ -28,65 +33,164 @@ public: /** * @copydoc UnboundedRegExpElement::clone() const */ - virtual UnboundedRegExpElement * clone ( ) const; + UnboundedRegExpElement < SymbolType > * clone ( ) const override; /** * @copydoc UnboundedRegExpElement::plunder() const */ - virtual UnboundedRegExpElement * plunder ( ) &&; + UnboundedRegExpElement < SymbolType > * plunder ( ) && override; /** * @copydoc UnboundedRegExpElement::cloneAsFormal() const */ - virtual std::smart_ptr < FormalRegExpElement > asFormal ( ) const; + std::smart_ptr < FormalRegExpElement < SymbolType > > asFormal ( ) const override; /** * @copydoc UnboundedRegExpElement::testSymbol() const */ - virtual bool testSymbol ( const alphabet::Symbol & symbol ) const; + bool testSymbol ( const SymbolType & symbol ) const override; /** * @copydoc UnboundedRegExpElement::computeMinimalAlphabet() */ - virtual void computeMinimalAlphabet ( std::set < alphabet::Symbol > & alphabet ) const; + void computeMinimalAlphabet ( std::set < SymbolType > & alphabet ) const override; /** * @copydoc UnboundedRegExpElement::checkAlphabet() */ - virtual bool checkAlphabet ( const std::set < alphabet::Symbol > & alphabet ) const; + bool checkAlphabet ( const std::set < SymbolType > & alphabet ) const override; /** * @return elements */ - const std::vector < std::smart_ptr < const UnboundedRegExpElement > > & getElements ( ) const; + const std::vector < std::smart_ptr < const UnboundedRegExpElement < SymbolType > > > & getElements ( ) const; /** * @return elements */ - const std::vector < std::smart_ptr < UnboundedRegExpElement > > & getElements ( ); + const std::vector < std::smart_ptr < UnboundedRegExpElement < SymbolType > > > & getElements ( ); /** * @param element to append */ - void appendElement ( UnboundedRegExpElement && element ); - void appendElement ( const UnboundedRegExpElement & element ); + void appendElement ( UnboundedRegExpElement < SymbolType > && element ); + void appendElement ( const UnboundedRegExpElement < SymbolType > & element ); - virtual int compare ( const UnboundedRegExpElement & other ) const { + int compare ( const UnboundedRegExpElement < SymbolType > & other ) const override { if ( std::type_index ( typeid ( * this ) ) == std::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other ); return std::type_index ( typeid ( * this ) ) - std::type_index ( typeid ( other ) ); } - virtual int compare ( const UnboundedRegExpConcatenation & ) const; + int compare ( const UnboundedRegExpConcatenation & ) const; /** * @copydoc UnboundedRegExpElement::operator>>() const */ - virtual void operator >>( std::ostream & out ) const; + void operator >>( std::ostream & out ) const override; - virtual explicit operator std::string ( ) const; + explicit operator std::string ( ) const override; }; } /* namespace regexp */ +#include "../formal/FormalRegExpConcatenation.h" +#include "../formal/FormalRegExpEpsilon.h" + +namespace regexp { + +template < class SymbolType > +UnboundedRegExpConcatenation < SymbolType >::UnboundedRegExpConcatenation ( ) { +} + +template < class SymbolType > +const std::vector < std::smart_ptr < const UnboundedRegExpElement < SymbolType > > > & UnboundedRegExpConcatenation < SymbolType >::getElements ( ) const { + return this->getChildren(); +} + +template < class SymbolType > +const std::vector < std::smart_ptr < UnboundedRegExpElement < SymbolType > > > & UnboundedRegExpConcatenation < SymbolType >::getElements ( ) { + return this->getChildren(); +} + +template < class SymbolType > +void UnboundedRegExpConcatenation < SymbolType >::appendElement ( UnboundedRegExpElement < SymbolType > && element ) { + this->pushBackChild ( std::smart_ptr < UnboundedRegExpElement < SymbolType > > ( std::move ( element ).plunder ( ) ) ); +} + +template < class SymbolType > +void UnboundedRegExpConcatenation < SymbolType >::appendElement ( const UnboundedRegExpElement < SymbolType > & element ) { + this->appendElement ( std::move_copy ( element ) ); +} + +template < class SymbolType > +UnboundedRegExpElement < SymbolType > * UnboundedRegExpConcatenation < SymbolType >::clone ( ) const { + return new UnboundedRegExpConcatenation ( * this ); +} + +template < class SymbolType > +UnboundedRegExpElement < SymbolType > * UnboundedRegExpConcatenation < SymbolType >::plunder ( ) && { + return new UnboundedRegExpConcatenation ( std::move ( * this ) ); +} + +template < class SymbolType > +std::smart_ptr < FormalRegExpElement < SymbolType > > UnboundedRegExpConcatenation < SymbolType >::asFormal ( ) const { + if ( getElements ( ).size ( ) == 0 ) return std::smart_ptr < FormalRegExpElement < SymbolType > > ( new FormalRegExpEpsilon < SymbolType > ( ) ); + + std::smart_ptr < FormalRegExpElement < SymbolType > > res = getElements ( )[getElements ( ).size ( ) - 1]->asFormal ( ); + + for ( unsigned i = getElements ( ).size ( ) - 1; i >= 1; i-- ) + res = std::smart_ptr < FormalRegExpElement < SymbolType > > ( new FormalRegExpConcatenation < SymbolType > ( std::move ( * getElements ( )[i - 1]->asFormal ( ) ), std::move ( * res ) ) ); + + return res; +} + +template < class SymbolType > +int UnboundedRegExpConcatenation < SymbolType >::compare ( const UnboundedRegExpConcatenation < SymbolType > & other ) const { + std::compare < typename std::decay < decltype ( getElements ( ) ) >::type > comp; + + return comp ( getElements ( ), other.getElements ( ) ); +} + +template < class SymbolType > +void UnboundedRegExpConcatenation < SymbolType >::operator >>( std::ostream & out ) const { + out << "(UnboundedRegExpConcatenation"; + + for ( const auto & child : getElements() ) + out << " " << * child; + + out << ")"; +} + +template < class SymbolType > +bool UnboundedRegExpConcatenation < SymbolType >::testSymbol ( const SymbolType & symbol ) const { + for ( const auto & child : getElements() ) + if ( child->testSymbol ( symbol ) ) return true; + + return false; +} + +template < class SymbolType > +bool UnboundedRegExpConcatenation < SymbolType >::checkAlphabet ( const std::set < SymbolType > & alphabet ) const { + for ( const auto & child : getElements() ) + if ( !child->checkAlphabet ( alphabet ) ) return false; + + return true; +} + +template < class SymbolType > +void UnboundedRegExpConcatenation < SymbolType >::computeMinimalAlphabet ( std::set < SymbolType > & alphabet ) const { + for ( const auto & child : getElements() ) + child->computeMinimalAlphabet ( alphabet ); +} + +template < class SymbolType > +UnboundedRegExpConcatenation < SymbolType >::operator std::string ( ) const { + std::stringstream ss; + ss << * this; + return ss.str ( ); +} + +} /* namespace regexp */ + #endif /* UNBOUNDED_REG_EXP_CONCATENATION_H_ */ diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpElement.cpp b/alib2data/src/regexp/unbounded/UnboundedRegExpElement.cpp deleted file mode 100644 index ea0a834d6b..0000000000 --- a/alib2data/src/regexp/unbounded/UnboundedRegExpElement.cpp +++ /dev/null @@ -1,19 +0,0 @@ -/* - * UnboundedRegExpElement.cpp - * - * Created on: Nov 23, 2013 - * Author: Martin Zak - */ - -#include "UnboundedRegExpElement.h" - -namespace regexp { - -std::set < alphabet::Symbol > UnboundedRegExpElement::computeMinimalAlphabet ( ) const { - std::set < alphabet::Symbol > res; - - computeMinimalAlphabet ( res ); - return res; -} - -} /* namespace regexp */ diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpElement.h b/alib2data/src/regexp/unbounded/UnboundedRegExpElement.h index cc832c5e5f..7ead105f46 100644 --- a/alib2data/src/regexp/unbounded/UnboundedRegExpElement.h +++ b/alib2data/src/regexp/unbounded/UnboundedRegExpElement.h @@ -13,31 +13,24 @@ #include <set> #include <tree> #include <core/visitor.hpp> +#include "../RegExpFeatures.h" namespace regexp { -class UnboundedRegExpAlternation; -class UnboundedRegExpConcatenation; -class UnboundedRegExpIteration; -class UnboundedRegExpSymbol; -class UnboundedRegExpEmpty; -class UnboundedRegExpEpsilon; - -class FormalRegExpElement; - /** * Abstract class representing element in the regular expression. Can be operator or symbol. */ -class UnboundedRegExpElement : public alib::CommonBase < UnboundedRegExpElement >, public std::BaseNode < UnboundedRegExpElement > { +template < class SymbolType > +class UnboundedRegExpElement : public alib::CommonBase < UnboundedRegExpElement < SymbolType > >, public std::BaseNode < UnboundedRegExpElement < SymbolType > > { public: class Visitor { public: - virtual void visit ( const UnboundedRegExpAlternation & ) = 0; - virtual void visit ( const UnboundedRegExpConcatenation & ) = 0; - virtual void visit ( const UnboundedRegExpIteration & ) = 0; - virtual void visit ( const UnboundedRegExpSymbol & ) = 0; - virtual void visit ( const UnboundedRegExpEmpty & ) = 0; - virtual void visit ( const UnboundedRegExpEpsilon & ) = 0; + virtual void visit ( const UnboundedRegExpAlternation < SymbolType > & ) = 0; + virtual void visit ( const UnboundedRegExpConcatenation < SymbolType > & ) = 0; + virtual void visit ( const UnboundedRegExpIteration < SymbolType > & ) = 0; + virtual void visit ( const UnboundedRegExpSymbol < SymbolType > & ) = 0; + virtual void visit ( const UnboundedRegExpEmpty < SymbolType > & ) = 0; + virtual void visit ( const UnboundedRegExpEpsilon < SymbolType > & ) = 0; }; template < class ReturnType, class Visitorr, class ... Params > @@ -45,27 +38,27 @@ public: public: using std::VisitorContextAux < ReturnType, Visitorr, Params ... >::VisitorContextAux; - void visit ( const UnboundedRegExpAlternation & inherit ) override { + void visit ( const UnboundedRegExpAlternation < SymbolType > & inherit ) override { this->call ( inherit, std::make_index_sequence < sizeof ... ( Params ) > { } ); } - void visit ( const UnboundedRegExpConcatenation & inherit ) override { + void visit ( const UnboundedRegExpConcatenation < SymbolType > & inherit ) override { this->call ( inherit, std::make_index_sequence < sizeof ... ( Params ) > { } ); } - void visit ( const UnboundedRegExpIteration & inherit ) override { + void visit ( const UnboundedRegExpIteration < SymbolType > & inherit ) override { this->call ( inherit, std::make_index_sequence < sizeof ... ( Params ) > { } ); } - void visit ( const UnboundedRegExpSymbol & inherit ) override { + void visit ( const UnboundedRegExpSymbol < SymbolType > & inherit ) override { this->call ( inherit, std::make_index_sequence < sizeof ... ( Params ) > { } ); } - void visit ( const UnboundedRegExpEmpty & inherit ) override { + void visit ( const UnboundedRegExpEmpty < SymbolType > & inherit ) override { this->call ( inherit, std::make_index_sequence < sizeof ... ( Params ) > { } ); } - void visit ( const UnboundedRegExpEpsilon & inherit ) override { + void visit ( const UnboundedRegExpEpsilon < SymbolType > & inherit ) override { this->call ( inherit, std::make_index_sequence < sizeof ... ( Params ) > { } ); } }; @@ -83,7 +76,7 @@ public: * Creates copy of the element. * @return copy of the element */ - virtual std::smart_ptr < FormalRegExpElement > asFormal ( ) const = 0; + virtual std::smart_ptr < FormalRegExpElement < SymbolType > > asFormal ( ) const = 0; /** * Traverses the regexp tree looking if particular Symbol is used in the regexp. @@ -91,7 +84,7 @@ public: * @param symbol to test if used in regexp element * @return true if symbol is used by the element and its successor */ - virtual bool testSymbol ( const alphabet::Symbol & symbol ) const = 0; + virtual bool testSymbol ( const SymbolType & symbol ) const = 0; /** * Traverses the regexp tree computing minimal alphabet needed by regexp @@ -99,7 +92,7 @@ public: * @param alphabet All alphabet symbols encountered are added into this set * @return true if symbol is used by the element and its successor */ - virtual void computeMinimalAlphabet ( std::set < alphabet::Symbol > & alphabet ) const = 0; + virtual void computeMinimalAlphabet ( std::set < SymbolType > & alphabet ) const = 0; /** * Traverses the regexp tree and checks whether all symbols in the regexp tree are in the alphabet @@ -107,14 +100,22 @@ public: * @param alphabet * @return true if symbols in the regexp are in the alphabet, false otherwise */ - virtual bool checkAlphabet ( const std::set < alphabet::Symbol > & alphabet ) const = 0; + virtual bool checkAlphabet ( const std::set < SymbolType > & alphabet ) const = 0; /** * @copydoc RankedNode::computeMinimalAlphabet() */ - std::set < alphabet::Symbol > computeMinimalAlphabet ( ) const; + std::set < SymbolType > computeMinimalAlphabet ( ) const; }; +template < class SymbolType > +std::set < SymbolType > UnboundedRegExpElement < SymbolType >::computeMinimalAlphabet ( ) const { + std::set < SymbolType > res; + + computeMinimalAlphabet ( res ); + return res; +} + } /* namespace regexp */ #endif /* UNBOUNDED_REG_EXP_ELEMENT_H_ */ diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpEmpty.cpp b/alib2data/src/regexp/unbounded/UnboundedRegExpEmpty.cpp deleted file mode 100644 index 10c5cf7b30..0000000000 --- a/alib2data/src/regexp/unbounded/UnboundedRegExpEmpty.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/* - * UnboundedRegExpEmpty.cpp - * - * Created on: Jan 30, 2014 - * Author: Jan Travnicek - */ - -#include "UnboundedRegExpEmpty.h" -#include "../formal/FormalRegExpEmpty.h" -#include <sstream> - -namespace regexp { - -UnboundedRegExpEmpty::UnboundedRegExpEmpty ( ) { - // so that default constructor is available -} - -UnboundedRegExpElement * UnboundedRegExpEmpty::clone ( ) const { - return new UnboundedRegExpEmpty ( * this ); -} - -UnboundedRegExpElement * UnboundedRegExpEmpty::plunder ( ) && { - return new UnboundedRegExpEmpty ( std::move ( * this ) ); -} - -std::smart_ptr < FormalRegExpElement > UnboundedRegExpEmpty::asFormal ( ) const { - return std::smart_ptr < FormalRegExpElement > ( new FormalRegExpEmpty ( ) ); -} - -int UnboundedRegExpEmpty::compare ( const UnboundedRegExpEmpty & ) const { - return 0; -} - -void UnboundedRegExpEmpty::operator >>( std::ostream & out ) const { - out << "(UnboundedRegExpEmpty)"; -} - -bool UnboundedRegExpEmpty::testSymbol ( const alphabet::Symbol & ) const { - return false; -} - -bool UnboundedRegExpEmpty::checkAlphabet ( const std::set < alphabet::Symbol > & ) const { - return true; -} - -void UnboundedRegExpEmpty::computeMinimalAlphabet ( std::set < alphabet::Symbol > & ) const { -} - -UnboundedRegExpEmpty::operator std::string ( ) const { - std::stringstream ss; - ss << * this; - return ss.str ( ); -} - -} /* namespace regexp */ diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpEmpty.h b/alib2data/src/regexp/unbounded/UnboundedRegExpEmpty.h index cdfec770e2..015e3d6b95 100644 --- a/alib2data/src/regexp/unbounded/UnboundedRegExpEmpty.h +++ b/alib2data/src/regexp/unbounded/UnboundedRegExpEmpty.h @@ -8,6 +8,8 @@ #ifndef UNBOUNDED_REG_EXP_EMPTY_H_ #define UNBOUNDED_REG_EXP_EMPTY_H_ +#include <sstream> + #include "UnboundedRegExpElement.h" namespace regexp { @@ -15,9 +17,10 @@ namespace regexp { /** * Represents empty regular expression in the regular expression. */ -class UnboundedRegExpEmpty : public UnboundedRegExpElement, public std::NullaryNode < std::smart_ptr < UnboundedRegExpElement >, std::smart_ptr < const UnboundedRegExpElement >, UnboundedRegExpSymbol > { +template < class SymbolType > +class UnboundedRegExpEmpty : public UnboundedRegExpElement < SymbolType >, public std::NullaryNode < std::smart_ptr < UnboundedRegExpElement < SymbolType > >, std::smart_ptr < const UnboundedRegExpElement < SymbolType > >, UnboundedRegExpSymbol < SymbolType > > { public: - void accept ( UnboundedRegExpElement::Visitor & visitor ) const { + void accept ( typename UnboundedRegExpElement < SymbolType >::Visitor & visitor ) const override { visitor.visit ( * this ); } @@ -26,49 +29,106 @@ public: /** * @copydoc UnboundedRegExpElement::clone() const */ - virtual UnboundedRegExpElement * clone ( ) const; + UnboundedRegExpElement < SymbolType > * clone ( ) const override; /** * @copydoc UnboundedRegExpElement::plunder() const */ - virtual UnboundedRegExpElement * plunder ( ) &&; + UnboundedRegExpElement < SymbolType > * plunder ( ) && override; /** * @copydoc UnboundedRegExpElement::cloneAsFormal() const */ - virtual std::smart_ptr < FormalRegExpElement > asFormal ( ) const; + std::smart_ptr < FormalRegExpElement < SymbolType > > asFormal ( ) const override; /** * @copydoc UnboundedRegExpElement::testSymbol() const */ - virtual bool testSymbol ( const alphabet::Symbol & symbol ) const; + bool testSymbol ( const SymbolType & symbol ) const override; /** * @copydoc RegExpElement::computeMinimalAlphabet() */ - virtual void computeMinimalAlphabet ( std::set < alphabet::Symbol > & alphabet ) const; + void computeMinimalAlphabet ( std::set < SymbolType > & alphabet ) const override; /** * @copydoc UnboundedRegExpElement::checkAlphabet() */ - virtual bool checkAlphabet ( const std::set < alphabet::Symbol > & alphabet ) const; + bool checkAlphabet ( const std::set < SymbolType > & alphabet ) const override; - virtual int compare ( const UnboundedRegExpElement & other ) const { + int compare ( const UnboundedRegExpElement < SymbolType > & other ) const override { if ( std::type_index ( typeid ( * this ) ) == std::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other ); return std::type_index ( typeid ( * this ) ) - std::type_index ( typeid ( other ) ); } - virtual int compare ( const UnboundedRegExpEmpty & ) const; + int compare ( const UnboundedRegExpEmpty & ) const; /** * @copydoc UnboundedRegExpElement::operator>>() const */ - virtual void operator >>( std::ostream & out ) const; + void operator >>( std::ostream & out ) const override; - virtual explicit operator std::string ( ) const; + explicit operator std::string ( ) const override; }; } /* namespace regexp */ +#include "../formal/FormalRegExpEmpty.h" + +namespace regexp { + +template < class SymbolType > +UnboundedRegExpEmpty < SymbolType >::UnboundedRegExpEmpty ( ) { + // so that default constructor is available +} + +template < class SymbolType > +UnboundedRegExpElement < SymbolType > * UnboundedRegExpEmpty < SymbolType >::clone ( ) const { + return new UnboundedRegExpEmpty ( * this ); +} + +template < class SymbolType > +UnboundedRegExpElement < SymbolType > * UnboundedRegExpEmpty < SymbolType >::plunder ( ) && { + return new UnboundedRegExpEmpty ( std::move ( * this ) ); +} + +template < class SymbolType > +std::smart_ptr < FormalRegExpElement < SymbolType > > UnboundedRegExpEmpty < SymbolType >::asFormal ( ) const { + return std::smart_ptr < FormalRegExpElement < SymbolType > > ( new FormalRegExpEmpty < SymbolType > ( ) ); +} + +template < class SymbolType > +int UnboundedRegExpEmpty < SymbolType >::compare ( const UnboundedRegExpEmpty & ) const { + return 0; +} + +template < class SymbolType > +void UnboundedRegExpEmpty < SymbolType >::operator >>( std::ostream & out ) const { + out << "(UnboundedRegExpEmpty)"; +} + +template < class SymbolType > +bool UnboundedRegExpEmpty < SymbolType >::testSymbol ( const SymbolType & ) const { + return false; +} + +template < class SymbolType > +bool UnboundedRegExpEmpty < SymbolType >::checkAlphabet ( const std::set < SymbolType > & ) const { + return true; +} + +template < class SymbolType > +void UnboundedRegExpEmpty < SymbolType >::computeMinimalAlphabet ( std::set < SymbolType > & ) const { +} + +template < class SymbolType > +UnboundedRegExpEmpty < SymbolType >::operator std::string ( ) const { + std::stringstream ss; + ss << * this; + return ss.str ( ); +} + +} /* namespace regexp */ + #endif /* UNBOUNDED_REG_EXP_EMPTY_H_ */ diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpEpsilon.cpp b/alib2data/src/regexp/unbounded/UnboundedRegExpEpsilon.cpp deleted file mode 100644 index a2c4d85bf6..0000000000 --- a/alib2data/src/regexp/unbounded/UnboundedRegExpEpsilon.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/* - * UnboundedRegExpEpsilon.cpp - * - * Created on: Jan 30, 2014 - * Author: Jan Travnicek - */ - -#include "UnboundedRegExpEpsilon.h" -#include "../formal/FormalRegExpEpsilon.h" -#include <sstream> - -namespace regexp { - -UnboundedRegExpEpsilon::UnboundedRegExpEpsilon ( ) { - // so that default constructor is available -} - -UnboundedRegExpElement * UnboundedRegExpEpsilon::clone ( ) const { - return new UnboundedRegExpEpsilon ( * this ); -} - -UnboundedRegExpElement * UnboundedRegExpEpsilon::plunder ( ) && { - return new UnboundedRegExpEpsilon ( std::move ( * this ) ); -} - -std::smart_ptr < FormalRegExpElement > UnboundedRegExpEpsilon::asFormal ( ) const { - return std::smart_ptr < FormalRegExpElement > ( new FormalRegExpEpsilon ( ) ); -} - -int UnboundedRegExpEpsilon::compare ( const UnboundedRegExpEpsilon & ) const { - return 0; -} - -void UnboundedRegExpEpsilon::operator >>( std::ostream & out ) const { - out << "(UnboundedRegExpEpsilon)"; -} - -bool UnboundedRegExpEpsilon::testSymbol ( const alphabet::Symbol & ) const { - return false; -} - -bool UnboundedRegExpEpsilon::checkAlphabet ( const std::set < alphabet::Symbol > & ) const { - return true; -} - -void UnboundedRegExpEpsilon::computeMinimalAlphabet ( std::set < alphabet::Symbol > & ) const { -} - -UnboundedRegExpEpsilon::operator std::string ( ) const { - std::stringstream ss; - ss << * this; - return ss.str ( ); -} - -} /* namespace regexp */ diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpEpsilon.h b/alib2data/src/regexp/unbounded/UnboundedRegExpEpsilon.h index 32b8eeed32..d52576a293 100644 --- a/alib2data/src/regexp/unbounded/UnboundedRegExpEpsilon.h +++ b/alib2data/src/regexp/unbounded/UnboundedRegExpEpsilon.h @@ -8,6 +8,8 @@ #ifndef UNBOUNDED_REG_EXP_EPSILON_H_ #define UNBOUNDED_REG_EXP_EPSILON_H_ +#include <sstream> + #include "UnboundedRegExpElement.h" namespace regexp { @@ -15,9 +17,10 @@ namespace regexp { /** * Represents epsilon in the regular expression. */ -class UnboundedRegExpEpsilon : public UnboundedRegExpElement, public std::NullaryNode < std::smart_ptr < UnboundedRegExpElement >, std::smart_ptr < const UnboundedRegExpElement >, UnboundedRegExpSymbol > { +template < class SymbolType > +class UnboundedRegExpEpsilon : public UnboundedRegExpElement < SymbolType >, public std::NullaryNode < std::smart_ptr < UnboundedRegExpElement < SymbolType > >, std::smart_ptr < const UnboundedRegExpElement < SymbolType > >, UnboundedRegExpSymbol < SymbolType > > { public: - void accept ( UnboundedRegExpElement::Visitor & visitor ) const { + void accept ( typename UnboundedRegExpElement < SymbolType >::Visitor & visitor ) const override { visitor.visit ( * this ); } @@ -26,49 +29,106 @@ public: /** * @copydoc UnboundedRegExpElement::clone() const */ - virtual UnboundedRegExpElement * clone ( ) const; + UnboundedRegExpElement < SymbolType > * clone ( ) const override; /** * @copydoc UnboundedRegExpElement::plunder() const */ - virtual UnboundedRegExpElement * plunder ( ) &&; + UnboundedRegExpElement < SymbolType > * plunder ( ) && override; /** * @copydoc UnboundedRegExpElement::cloneAsFormal() const */ - virtual std::smart_ptr < FormalRegExpElement > asFormal ( ) const; + std::smart_ptr < FormalRegExpElement < SymbolType > > asFormal ( ) const override; /** * @copydoc UnboundedRegExpElement::testSymbol() const */ - virtual bool testSymbol ( const alphabet::Symbol & symbol ) const; + bool testSymbol ( const SymbolType & symbol ) const override; /** * @copydoc UnboundedRegExpElement::computeMinimalAlphabet() */ - virtual void computeMinimalAlphabet ( std::set < alphabet::Symbol > & alphabet ) const; + void computeMinimalAlphabet ( std::set < SymbolType > & alphabet ) const override; /** * @copydoc UnboundedRegExpElement::checkAlphabet() */ - virtual bool checkAlphabet ( const std::set < alphabet::Symbol > & alphabet ) const; + bool checkAlphabet ( const std::set < SymbolType > & alphabet ) const override; - virtual int compare ( const UnboundedRegExpElement & other ) const { + int compare ( const UnboundedRegExpElement < SymbolType > & other ) const override { if ( std::type_index ( typeid ( * this ) ) == std::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other ); return std::type_index ( typeid ( * this ) ) - std::type_index ( typeid ( other ) ); } - virtual int compare ( const UnboundedRegExpEpsilon & ) const; + int compare ( const UnboundedRegExpEpsilon & ) const; /** * @copydoc UnboundedRegExpElement::operator>>() const */ - virtual void operator >>( std::ostream & out ) const; + void operator >>( std::ostream & out ) const override; - virtual explicit operator std::string ( ) const; + explicit operator std::string ( ) const override; }; } /* namespace regexp */ +#include "../formal/FormalRegExpEpsilon.h" + +namespace regexp { + +template < class SymbolType > +UnboundedRegExpEpsilon < SymbolType >::UnboundedRegExpEpsilon ( ) { + // so that default constructor is available +} + +template < class SymbolType > +UnboundedRegExpElement < SymbolType > * UnboundedRegExpEpsilon < SymbolType >::clone ( ) const { + return new UnboundedRegExpEpsilon ( * this ); +} + +template < class SymbolType > +UnboundedRegExpElement < SymbolType > * UnboundedRegExpEpsilon < SymbolType >::plunder ( ) && { + return new UnboundedRegExpEpsilon ( std::move ( * this ) ); +} + +template < class SymbolType > +std::smart_ptr < FormalRegExpElement < SymbolType > > UnboundedRegExpEpsilon < SymbolType >::asFormal ( ) const { + return std::smart_ptr < FormalRegExpElement < SymbolType > > ( new FormalRegExpEpsilon < SymbolType > ( ) ); +} + +template < class SymbolType > +int UnboundedRegExpEpsilon < SymbolType >::compare ( const UnboundedRegExpEpsilon & ) const { + return 0; +} + +template < class SymbolType > +void UnboundedRegExpEpsilon < SymbolType >::operator >>( std::ostream & out ) const { + out << "(UnboundedRegExpEpsilon)"; +} + +template < class SymbolType > +bool UnboundedRegExpEpsilon < SymbolType >::testSymbol ( const SymbolType & ) const { + return false; +} + +template < class SymbolType > +bool UnboundedRegExpEpsilon < SymbolType >::checkAlphabet ( const std::set < SymbolType > & ) const { + return true; +} + +template < class SymbolType > +void UnboundedRegExpEpsilon < SymbolType >::computeMinimalAlphabet ( std::set < SymbolType > & ) const { +} + +template < class SymbolType > +UnboundedRegExpEpsilon < SymbolType >::operator std::string ( ) const { + std::stringstream ss; + ss << * this; + return ss.str ( ); +} + +} /* namespace regexp */ + #endif /* UNBOUNDED_REG_EXP_EPSILON_H_ */ diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpIteration.cpp b/alib2data/src/regexp/unbounded/UnboundedRegExpIteration.cpp deleted file mode 100644 index 462606001d..0000000000 --- a/alib2data/src/regexp/unbounded/UnboundedRegExpIteration.cpp +++ /dev/null @@ -1,76 +0,0 @@ -/* - * UnboundedRegExpIteration.cpp - * - * Created on: Nov 23, 2013 - * Author: Martin Zak - */ - -#include "UnboundedRegExpIteration.h" -#include <exception/CommonException.h> - -#include "../formal/FormalRegExpIteration.h" -#include <sstream> - -namespace regexp { - -UnboundedRegExpIteration::UnboundedRegExpIteration ( UnboundedRegExpElement && element ) : UnaryNode < std::smart_ptr < UnboundedRegExpElement >, std::smart_ptr < const UnboundedRegExpElement >, UnboundedRegExpIteration > ( std::smart_ptr < UnboundedRegExpElement > ( std::move ( element ).plunder ( ) ) ) { -} - -UnboundedRegExpIteration::UnboundedRegExpIteration ( const UnboundedRegExpElement & element ) : UnboundedRegExpIteration ( std::move_copy ( element ) ) { -} - -const UnboundedRegExpElement & UnboundedRegExpIteration::getElement ( ) const { - return * getChild ( ); -} - -UnboundedRegExpElement & UnboundedRegExpIteration::getElement ( ) { - return * getChild ( ); -} - -void UnboundedRegExpIteration::setElement ( UnboundedRegExpElement && elementParam ) { - setChild ( std::smart_ptr < UnboundedRegExpElement > ( std::move ( elementParam ).plunder ( ) ) ); -} - -void UnboundedRegExpIteration::setElement ( const UnboundedRegExpElement & elementParam ) { - setElement ( std::move_copy ( elementParam ) ); -} - -UnboundedRegExpElement * UnboundedRegExpIteration::clone ( ) const { - return new UnboundedRegExpIteration ( * this ); -} - -UnboundedRegExpElement * UnboundedRegExpIteration::plunder ( ) && { - return new UnboundedRegExpIteration ( std::move ( * this ) ); -} - -std::smart_ptr < FormalRegExpElement > UnboundedRegExpIteration::asFormal ( ) const { - return std::smart_ptr < FormalRegExpElement > ( new FormalRegExpIteration ( std::move ( * getElement ( ).asFormal ( ) ) ) ); -} - -int UnboundedRegExpIteration::compare ( const UnboundedRegExpIteration & other ) const { - return getElement ( ).compare ( other.getElement ( ) ); -} - -void UnboundedRegExpIteration::operator >>( std::ostream & out ) const { - out << "(RegExpUnboundedRegExpIteration " << getElement ( ) << ")"; -} - -bool UnboundedRegExpIteration::testSymbol ( const alphabet::Symbol & symbol ) const { - return getElement ( ).testSymbol ( symbol ); -} - -bool UnboundedRegExpIteration::checkAlphabet ( const std::set < alphabet::Symbol > & alphabet ) const { - return getElement ( ).checkAlphabet ( alphabet ); -} - -void UnboundedRegExpIteration::computeMinimalAlphabet ( std::set < alphabet::Symbol > & alphabet ) const { - getElement ( ).computeMinimalAlphabet ( alphabet ); -} - -UnboundedRegExpIteration::operator std::string ( ) const { - std::stringstream ss; - ss << * this; - return ss.str ( ); -} - -} /* namespace regexp */ diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpIteration.h b/alib2data/src/regexp/unbounded/UnboundedRegExpIteration.h index 54d9d0b0e0..b8d82a2524 100644 --- a/alib2data/src/regexp/unbounded/UnboundedRegExpIteration.h +++ b/alib2data/src/regexp/unbounded/UnboundedRegExpIteration.h @@ -8,8 +8,11 @@ #ifndef UNBOUNDED_REG_EXP_ITERATION_H_ #define UNBOUNDED_REG_EXP_ITERATION_H_ +#include <sstream> + +#include <exception/CommonException.h> + #include "UnboundedRegExpElement.h" -#include "UnboundedRegExpEmpty.h" namespace regexp { @@ -17,77 +20,160 @@ namespace regexp { * Represents iteration operator in the regular expression. Contains one UnboundedRegExpElement * as operand. */ -class UnboundedRegExpIteration : public UnboundedRegExpElement, public std::UnaryNode < std::smart_ptr < UnboundedRegExpElement >, std::smart_ptr < const UnboundedRegExpElement >, UnboundedRegExpIteration > { +template < class SymbolType > +class UnboundedRegExpIteration : public UnboundedRegExpElement < SymbolType >, public std::UnaryNode < std::smart_ptr < UnboundedRegExpElement < SymbolType > >, std::smart_ptr < const UnboundedRegExpElement < SymbolType > >, UnboundedRegExpIteration < SymbolType > > { public: - void accept ( UnboundedRegExpElement::Visitor & visitor ) const { + void accept ( typename UnboundedRegExpElement < SymbolType >::Visitor & visitor ) const override { visitor.visit ( * this ); } - explicit UnboundedRegExpIteration ( UnboundedRegExpElement && ); - explicit UnboundedRegExpIteration ( const UnboundedRegExpElement & ); + explicit UnboundedRegExpIteration ( UnboundedRegExpElement < SymbolType > && ); + explicit UnboundedRegExpIteration ( const UnboundedRegExpElement < SymbolType > & ); /** * @copydoc UnboundedRegExpElement::clone() const */ - virtual UnboundedRegExpElement * clone ( ) const; + UnboundedRegExpElement < SymbolType > * clone ( ) const override; /** * @copydoc UnboundedRegExpElement::plunder() const */ - virtual UnboundedRegExpElement * plunder ( ) &&; + UnboundedRegExpElement < SymbolType > * plunder ( ) && override; /** * @copydoc UnboundedRegExpElement::cloneAsFormal() const */ - virtual std::smart_ptr < FormalRegExpElement > asFormal ( ) const; + std::smart_ptr < FormalRegExpElement < SymbolType > > asFormal ( ) const override; /** * @copydoc UnboundedRegExpElement::testSymbol() const */ - virtual bool testSymbol ( const alphabet::Symbol & symbol ) const; + bool testSymbol ( const alphabet::Symbol & symbol ) const override; /** * @copydoc UnboundedRegExpElement::computeMinimalAlphabet() */ - virtual void computeMinimalAlphabet ( std::set < alphabet::Symbol > & alphabet ) const; + void computeMinimalAlphabet ( std::set < alphabet::Symbol > & alphabet ) const override; /** * @copydoc UnboundedRegExpElement::checkAlphabet() */ - virtual bool checkAlphabet ( const std::set < alphabet::Symbol > & alphabet ) const; + bool checkAlphabet ( const std::set < alphabet::Symbol > & alphabet ) const override; /** * @return element */ - const UnboundedRegExpElement & getElement ( ) const; + const UnboundedRegExpElement < SymbolType > & getElement ( ) const; /** * @return element */ - UnboundedRegExpElement & getElement ( ); + UnboundedRegExpElement < SymbolType > & getElement ( ); /** * @param element to iterate */ - void setElement ( UnboundedRegExpElement && element ); - void setElement ( const UnboundedRegExpElement & element ); + void setElement ( UnboundedRegExpElement < SymbolType > && element ); + void setElement ( const UnboundedRegExpElement < SymbolType > & element ); - virtual int compare ( const UnboundedRegExpElement & other ) const { + int compare ( const UnboundedRegExpElement < SymbolType > & other ) const override { if ( std::type_index ( typeid ( * this ) ) == std::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other ); return std::type_index ( typeid ( * this ) ) - std::type_index ( typeid ( other ) ); } - virtual int compare ( const UnboundedRegExpIteration & ) const; + int compare ( const UnboundedRegExpIteration & ) const; /** * @copydoc UnboundedRegExpElement::operator>>() const */ - virtual void operator >>( std::ostream & out ) const; + void operator >>( std::ostream & out ) const override; - virtual explicit operator std::string ( ) const; + explicit operator std::string ( ) const override; }; } /* namespace regexp */ +#include "UnboundedRegExpEmpty.h" +#include "../formal/FormalRegExpIteration.h" + +namespace regexp { + +template < class SymbolType > +UnboundedRegExpIteration < SymbolType >::UnboundedRegExpIteration ( UnboundedRegExpElement < SymbolType > && element ) : std::UnaryNode < std::smart_ptr < UnboundedRegExpElement < SymbolType > >, std::smart_ptr < const UnboundedRegExpElement < SymbolType > >, UnboundedRegExpIteration < SymbolType > > ( std::smart_ptr < UnboundedRegExpElement < SymbolType > > ( std::move ( element ).plunder ( ) ) ) { +} + +template < class SymbolType > +UnboundedRegExpIteration < SymbolType >::UnboundedRegExpIteration ( const UnboundedRegExpElement < SymbolType > & element ) : UnboundedRegExpIteration ( std::move_copy ( element ) ) { +} + +template < class SymbolType > +const UnboundedRegExpElement < SymbolType > & UnboundedRegExpIteration < SymbolType >::getElement ( ) const { + return * this->getChild ( ); +} + +template < class SymbolType > +UnboundedRegExpElement < SymbolType > & UnboundedRegExpIteration < SymbolType >::getElement ( ) { + return * this->getChild ( ); +} + +template < class SymbolType > +void UnboundedRegExpIteration < SymbolType >::setElement ( UnboundedRegExpElement < SymbolType > && elementParam ) { + setChild ( std::smart_ptr < UnboundedRegExpElement < SymbolType > > ( std::move ( elementParam ).plunder ( ) ) ); +} + +template < class SymbolType > +void UnboundedRegExpIteration < SymbolType >::setElement ( const UnboundedRegExpElement < SymbolType > & elementParam ) { + setElement ( std::move_copy ( elementParam ) ); +} + +template < class SymbolType > +UnboundedRegExpElement < SymbolType > * UnboundedRegExpIteration < SymbolType >::clone ( ) const { + return new UnboundedRegExpIteration ( * this ); +} + +template < class SymbolType > +UnboundedRegExpElement < SymbolType > * UnboundedRegExpIteration < SymbolType >::plunder ( ) && { + return new UnboundedRegExpIteration ( std::move ( * this ) ); +} + +template < class SymbolType > +std::smart_ptr < FormalRegExpElement < SymbolType > > UnboundedRegExpIteration < SymbolType >::asFormal ( ) const { + return std::smart_ptr < FormalRegExpElement < SymbolType > > ( new FormalRegExpIteration < SymbolType > ( std::move ( * getElement ( ).asFormal ( ) ) ) ); +} + +template < class SymbolType > +int UnboundedRegExpIteration < SymbolType >::compare ( const UnboundedRegExpIteration < SymbolType > & other ) const { + return getElement ( ).compare ( other.getElement ( ) ); +} + +template < class SymbolType > +void UnboundedRegExpIteration < SymbolType >::operator >>( std::ostream & out ) const { + out << "(RegExpUnboundedRegExpIteration " << getElement ( ) << ")"; +} + +template < class SymbolType > +bool UnboundedRegExpIteration < SymbolType >::testSymbol ( const alphabet::Symbol & symbol ) const { + return getElement ( ).testSymbol ( symbol ); +} + +template < class SymbolType > +bool UnboundedRegExpIteration < SymbolType >::checkAlphabet ( const std::set < alphabet::Symbol > & alphabet ) const { + return getElement ( ).checkAlphabet ( alphabet ); +} + +template < class SymbolType > +void UnboundedRegExpIteration < SymbolType >::computeMinimalAlphabet ( std::set < alphabet::Symbol > & alphabet ) const { + getElement ( ).computeMinimalAlphabet ( alphabet ); +} + +template < class SymbolType > +UnboundedRegExpIteration < SymbolType >::operator std::string ( ) const { + std::stringstream ss; + ss << * this; + return ss.str ( ); +} + +} /* namespace regexp */ + #endif /* UNBOUNDED_REG_EXP_ITERATION_H_ */ diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpStructure.cpp b/alib2data/src/regexp/unbounded/UnboundedRegExpStructure.cpp deleted file mode 100644 index 36f1c9ba71..0000000000 --- a/alib2data/src/regexp/unbounded/UnboundedRegExpStructure.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/* - * UnboundedRegExpStructure.cpp - * - * Created on: Oct 20, 2016 - * Author: Jan Travnicek - */ - -#include "UnboundedRegExpStructure.h" -#include "UnboundedRegExpEmpty.h" - -#include "../formal/FormalRegExpStructure.h" - -#include "../common/RegExpFromXMLParser.h" -#include "../common/RegExpToXMLComposer.h" -#include <exception/CommonException.h> - -namespace regexp { - -UnboundedRegExpStructure::UnboundedRegExpStructure ( UnboundedRegExpElement && structure ) : structure ( NULL ) { - setStructure ( std::move ( structure ) ); -} - -UnboundedRegExpStructure::UnboundedRegExpStructure ( const UnboundedRegExpElement & structure ) : UnboundedRegExpStructure ( std::move_copy ( structure ) ) { -} - -UnboundedRegExpStructure::UnboundedRegExpStructure ( ) : UnboundedRegExpStructure ( UnboundedRegExpEmpty ( ) ) { -} - -UnboundedRegExpStructure::UnboundedRegExpStructure ( const FormalRegExpStructure & other ) : UnboundedRegExpStructure ( * other.getStructure ( ).asUnbounded ( ) ) { -} - -const UnboundedRegExpElement & UnboundedRegExpStructure::getStructure ( ) const { - return * structure; -} - -UnboundedRegExpElement & UnboundedRegExpStructure::getStructure ( ) { - return * structure; -} - -void UnboundedRegExpStructure::setStructure ( const UnboundedRegExpElement & structure ) { - setStructure ( std::move_copy ( structure ) ); -} - -void UnboundedRegExpStructure::setStructure ( UnboundedRegExpElement && param ) { - this->structure = std::smart_ptr < UnboundedRegExpElement > ( std::move ( param ).plunder ( ) ); -} - -} /* namespace regexp */ - -namespace alib { - -regexp::UnboundedRegExpStructure xmlApi < regexp::UnboundedRegExpStructure >::parse ( std::deque < sax::Token >::iterator & input ) { - return regexp::UnboundedRegExpStructure ( regexp::RegExpFromXMLParser::parseUnboundedRegExpElement ( input ) ); -} - -bool xmlApi < regexp::UnboundedRegExpStructure >::first ( const std::deque < sax::Token >::const_iterator & ) { - throw exception::CommonException ( "Unimplemented" ); -} - -void xmlApi < regexp::UnboundedRegExpStructure >::compose ( std::deque < sax::Token > & output, const regexp::UnboundedRegExpStructure & data ) { - data.getStructure ( ).accept < void, regexp::RegExpToXMLComposer::Unbounded > ( output ); -} - -} /* namespace alib */ diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpStructure.h b/alib2data/src/regexp/unbounded/UnboundedRegExpStructure.h index 33b9b65428..6ad94aba63 100644 --- a/alib2data/src/regexp/unbounded/UnboundedRegExpStructure.h +++ b/alib2data/src/regexp/unbounded/UnboundedRegExpStructure.h @@ -8,47 +8,53 @@ #ifndef UNBOUNDED_REG_EXP_STRUCTURE_H_ #define UNBOUNDED_REG_EXP_STRUCTURE_H_ + #include <memory> -#include <compare> -#include "UnboundedRegExpElement.h" -namespace regexp { +#include <exception/CommonException.h> -class FormalRegExpStructure; +#include "../RegExpFeatures.h" +#include "../common/RegExpFromXMLParser.h" +#include "../common/RegExpToXMLComposer.h" + +#include "UnboundedRegExpEmpty.h" + +namespace regexp { /** * Represents unbounded regular expression structure as parsed from the XML. Regular expression is stored * as a tree of UnboundedRegExpElement. */ +template < class SymbolType > class UnboundedRegExpStructure { protected: - std::smart_ptr < UnboundedRegExpElement > structure; + std::smart_ptr < UnboundedRegExpElement < SymbolType > > structure; public: explicit UnboundedRegExpStructure ( ); - explicit UnboundedRegExpStructure ( UnboundedRegExpElement && structure ); - explicit UnboundedRegExpStructure ( const UnboundedRegExpElement & structure ); - explicit UnboundedRegExpStructure ( const FormalRegExpStructure & other ); + explicit UnboundedRegExpStructure ( UnboundedRegExpElement < SymbolType > && structure ); + explicit UnboundedRegExpStructure ( const UnboundedRegExpElement < SymbolType > & structure ); + explicit UnboundedRegExpStructure ( const FormalRegExpStructure < SymbolType > & other ); /** * @return Root node of the unbounded regular expression tree */ - const UnboundedRegExpElement & getStructure ( ) const; + const UnboundedRegExpElement < SymbolType > & getStructure ( ) const; /** * @return Root node of the unbounded regular expression tree */ - UnboundedRegExpElement & getStructure ( ); + UnboundedRegExpElement < SymbolType > & getStructure ( ); /** * Sets the root node of the unbounded regular expression tree * @param structure root node to set */ - void setStructure ( UnboundedRegExpElement && structure ); - void setStructure ( const UnboundedRegExpElement & structure ); + void setStructure ( UnboundedRegExpElement < SymbolType > && structure ); + void setStructure ( const UnboundedRegExpElement < SymbolType > & structure ); - friend std::ostream & operator << ( std::ostream & out, const UnboundedRegExpStructure & structure ) { + friend std::ostream & operator << ( std::ostream & out, const UnboundedRegExpStructure < SymbolType > & structure ) { out << structure.getStructure ( ); return out; } @@ -66,24 +72,86 @@ public: namespace alib { -template < > -struct xmlApi < regexp::UnboundedRegExpStructure > { - static regexp::UnboundedRegExpStructure parse ( std::deque < sax::Token >::iterator & input ); +template < class SymbolType > +struct xmlApi < regexp::UnboundedRegExpStructure < SymbolType > > { + static regexp::UnboundedRegExpStructure < SymbolType > parse ( std::deque < sax::Token >::iterator & input ); static bool first ( const std::deque < sax::Token >::const_iterator & input ); - static void compose ( std::deque < sax::Token > & output, const regexp::UnboundedRegExpStructure & data ); + static void compose ( std::deque < sax::Token > & output, const regexp::UnboundedRegExpStructure < SymbolType > & data ); }; } /* namespace alib */ namespace std { -template < > -struct compare < regexp::UnboundedRegExpStructure > { - int operator()(const regexp::UnboundedRegExpStructure& first, const regexp::UnboundedRegExpStructure& second) const { +template < class SymbolType > +struct compare < regexp::UnboundedRegExpStructure < SymbolType > > { + int operator()(const regexp::UnboundedRegExpStructure < SymbolType > & first, const regexp::UnboundedRegExpStructure < SymbolType > & second) const { return first.getStructure().compare(second.getStructure()); } }; } /* namespace std */ +#include "../formal/FormalRegExpStructure.h" + +namespace regexp { + +template < class SymbolType > +UnboundedRegExpStructure < SymbolType >::UnboundedRegExpStructure ( UnboundedRegExpElement < SymbolType > && structure ) : structure ( NULL ) { + setStructure ( std::move ( structure ) ); +} + +template < class SymbolType > +UnboundedRegExpStructure < SymbolType >::UnboundedRegExpStructure ( const UnboundedRegExpElement < SymbolType > & structure ) : UnboundedRegExpStructure ( std::move_copy ( structure ) ) { +} + +template < class SymbolType > +UnboundedRegExpStructure < SymbolType >::UnboundedRegExpStructure ( ) : UnboundedRegExpStructure < SymbolType > ( UnboundedRegExpEmpty < SymbolType > ( ) ) { +} + +template < class SymbolType > +UnboundedRegExpStructure < SymbolType >::UnboundedRegExpStructure ( const FormalRegExpStructure < SymbolType > & other ) : UnboundedRegExpStructure ( * other.getStructure ( ).asUnbounded ( ) ) { +} + +template < class SymbolType > +const UnboundedRegExpElement < SymbolType > & UnboundedRegExpStructure < SymbolType >::getStructure ( ) const { + return * structure; +} + +template < class SymbolType > +UnboundedRegExpElement < SymbolType > & UnboundedRegExpStructure < SymbolType >::getStructure ( ) { + return * structure; +} + +template < class SymbolType > +void UnboundedRegExpStructure < SymbolType >::setStructure ( const UnboundedRegExpElement < SymbolType > & structure ) { + setStructure ( std::move_copy ( structure ) ); +} + +template < class SymbolType > +void UnboundedRegExpStructure < SymbolType >::setStructure ( UnboundedRegExpElement < SymbolType > && param ) { + this->structure = std::smart_ptr < UnboundedRegExpElement < SymbolType > > ( std::move ( param ).plunder ( ) ); +} + +} /* namespace regexp */ + +namespace alib { + +template < class SymbolType > +regexp::UnboundedRegExpStructure < SymbolType > xmlApi < regexp::UnboundedRegExpStructure < SymbolType > >::parse ( std::deque < sax::Token >::iterator & input ) { + return regexp::UnboundedRegExpStructure < SymbolType > ( regexp::RegExpFromXMLParser::parseUnboundedRegExpElement < SymbolType > ( input ) ); +} + +template < class SymbolType > +bool xmlApi < regexp::UnboundedRegExpStructure < SymbolType > >::first ( const std::deque < sax::Token >::const_iterator & ) { + throw exception::CommonException ( "Unimplemented" ); +} + +template < class SymbolType > +void xmlApi < regexp::UnboundedRegExpStructure < SymbolType > >::compose ( std::deque < sax::Token > & output, const regexp::UnboundedRegExpStructure < SymbolType > & data ) { + data.getStructure ( ).template accept < void, regexp::RegExpToXMLComposer::Unbounded > ( output ); +} + +} /* namespace alib */ + #endif /* UNBOUNDED_REG_EXP_STRUCTURE_H_ */ diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpSymbol.cpp b/alib2data/src/regexp/unbounded/UnboundedRegExpSymbol.cpp deleted file mode 100644 index 2fdda50789..0000000000 --- a/alib2data/src/regexp/unbounded/UnboundedRegExpSymbol.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/* - * UnboundedRegExpSymbol.cpp - * - * Created on: Nov 23, 2013 - * Author: Martin Zak - */ - -#include "UnboundedRegExpSymbol.h" -#include "../formal/FormalRegExpSymbol.h" -#include <sstream> - -namespace regexp { - -UnboundedRegExpSymbol::UnboundedRegExpSymbol ( int number ) : symbol ( alphabet::symbolFrom ( number ) ) { -} - -UnboundedRegExpSymbol::UnboundedRegExpSymbol ( char character ) : symbol ( alphabet::symbolFrom ( character ) ) { -} - -UnboundedRegExpSymbol::UnboundedRegExpSymbol ( const std::string & label ) : symbol ( alphabet::symbolFrom ( label ) ) { -} - -UnboundedRegExpSymbol::UnboundedRegExpSymbol ( alphabet::Symbol symbol ) : symbol ( std::move ( symbol ) ) { -} - -UnboundedRegExpElement * UnboundedRegExpSymbol::clone ( ) const { - return new UnboundedRegExpSymbol ( * this ); -} - -UnboundedRegExpElement * UnboundedRegExpSymbol::plunder ( ) && { - return new UnboundedRegExpSymbol ( std::move ( * this ) ); -} - -std::smart_ptr < FormalRegExpElement > UnboundedRegExpSymbol::asFormal ( ) const { - return std::smart_ptr < FormalRegExpElement > ( new FormalRegExpSymbol ( this->symbol ) ); -} - -int UnboundedRegExpSymbol::compare ( const UnboundedRegExpSymbol & other ) const { - return symbol.getData ( ).compare ( other.symbol.getData ( ) ); -} - -void UnboundedRegExpSymbol::operator >>( std::ostream & out ) const { - out << "(UnboundedRegExpSymbol " << symbol << ")"; -} - -bool UnboundedRegExpSymbol::testSymbol ( const alphabet::Symbol & symbol ) const { - return symbol == this->symbol; -} - -bool UnboundedRegExpSymbol::checkAlphabet ( const std::set < alphabet::Symbol > & alphabet ) const { - return alphabet.count ( symbol ); -} - -void UnboundedRegExpSymbol::computeMinimalAlphabet ( std::set < alphabet::Symbol > & alphabet ) const { - alphabet.insert ( this->symbol ); -} - -const alphabet::Symbol & UnboundedRegExpSymbol::getSymbol ( ) const { - return this->symbol; -} - -UnboundedRegExpSymbol::operator std::string ( ) const { - std::stringstream ss; - ss << * this; - return ss.str ( ); -} - -} /* namespace regexp */ diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpSymbol.h b/alib2data/src/regexp/unbounded/UnboundedRegExpSymbol.h index fdddca278a..564e8c2fc5 100644 --- a/alib2data/src/regexp/unbounded/UnboundedRegExpSymbol.h +++ b/alib2data/src/regexp/unbounded/UnboundedRegExpSymbol.h @@ -8,19 +8,21 @@ #ifndef UNBOUNDED_REG_EXP_SYMBOL_H_ #define UNBOUNDED_REG_EXP_SYMBOL_H_ +#include <sstream> + #include "UnboundedRegExpElement.h" -#include "UnboundedRegExp.h" namespace regexp { /** * Represents symbol in the regular expression. Contains name of the symbol. */ -class UnboundedRegExpSymbol : public UnboundedRegExpElement, public std::NullaryNode < std::smart_ptr < UnboundedRegExpElement >, std::smart_ptr < const UnboundedRegExpElement >, UnboundedRegExpSymbol > { - alphabet::Symbol symbol; +template < class SymbolType > +class UnboundedRegExpSymbol : public UnboundedRegExpElement < SymbolType >, public std::NullaryNode < std::smart_ptr < UnboundedRegExpElement < SymbolType > >, std::smart_ptr < const UnboundedRegExpElement < SymbolType > >, UnboundedRegExpSymbol < SymbolType > > { + SymbolType symbol; public: - void accept ( UnboundedRegExpElement::Visitor & visitor ) const { + void accept ( typename UnboundedRegExpElement < SymbolType >::Visitor & visitor ) const override { visitor.visit ( * this ); } @@ -28,59 +30,135 @@ public: explicit UnboundedRegExpSymbol ( char character ); explicit UnboundedRegExpSymbol ( const std::string & label ); - explicit UnboundedRegExpSymbol ( alphabet::Symbol symbol ); + explicit UnboundedRegExpSymbol ( SymbolType symbol ); /** * @copydoc UnboundedRegExpElement::clone() const */ - virtual UnboundedRegExpElement * clone ( ) const; + UnboundedRegExpElement < SymbolType > * clone ( ) const override; /** * @copydoc UnboundedRegExpElement::plunder() const */ - virtual UnboundedRegExpElement * plunder ( ) &&; + UnboundedRegExpElement < SymbolType > * plunder ( ) && override; /** * @copydoc UnboundedRegExpElement::cloneAsFormal() const */ - virtual std::smart_ptr < FormalRegExpElement > asFormal ( ) const; + std::smart_ptr < FormalRegExpElement < SymbolType > > asFormal ( ) const override; /** * @copydoc UnboundedRegExpElement::testSymbol() const */ - virtual bool testSymbol ( const alphabet::Symbol & symbol ) const; + bool testSymbol ( const SymbolType & symbol ) const override; /** * @copydoc UnboundedRegExpElement::computeMinimalAlphabet() */ - virtual void computeMinimalAlphabet ( std::set < alphabet::Symbol > & alphabet ) const; + void computeMinimalAlphabet ( std::set < SymbolType > & alphabet ) const override; /** * @copydoc UnboundedRegExpElement::checkAlphabet() */ - virtual bool checkAlphabet ( const std::set < alphabet::Symbol > & alphabet ) const; + bool checkAlphabet ( const std::set < SymbolType > & alphabet ) const override; - virtual int compare ( const UnboundedRegExpElement & other ) const { + int compare ( const UnboundedRegExpElement < SymbolType > & other ) const override { if ( std::type_index ( typeid ( * this ) ) == std::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other ); return std::type_index ( typeid ( * this ) ) - std::type_index ( typeid ( other ) ); } - virtual int compare ( const UnboundedRegExpSymbol & ) const; + int compare ( const UnboundedRegExpSymbol & ) const; /** * @copydoc UnboundedRegExpElement::operator>>() const */ - virtual void operator >>( std::ostream & out ) const; + void operator >>( std::ostream & out ) const override; /** * Returns the string representation of RegExp Symbol. */ - const alphabet::Symbol & getSymbol ( ) const; + const SymbolType & getSymbol ( ) const; - virtual explicit operator std::string ( ) const; + explicit operator std::string ( ) const override; }; } /* namespace regexp */ +#include "UnboundedRegExpSymbol.h" +#include "../formal/FormalRegExpSymbol.h" +#include <sstream> + +namespace regexp { + +template < class SymbolType > +UnboundedRegExpSymbol < SymbolType >::UnboundedRegExpSymbol ( int number ) : symbol ( alphabet::symbolFrom ( number ) ) { +} + +template < class SymbolType > +UnboundedRegExpSymbol < SymbolType >::UnboundedRegExpSymbol ( char character ) : symbol ( alphabet::symbolFrom ( character ) ) { +} + +template < class SymbolType > +UnboundedRegExpSymbol < SymbolType >::UnboundedRegExpSymbol ( const std::string & label ) : symbol ( alphabet::symbolFrom ( label ) ) { +} + +template < class SymbolType > +UnboundedRegExpSymbol < SymbolType >::UnboundedRegExpSymbol ( SymbolType symbol ) : symbol ( std::move ( symbol ) ) { +} + +template < class SymbolType > +UnboundedRegExpElement < SymbolType > * UnboundedRegExpSymbol < SymbolType >::clone ( ) const { + return new UnboundedRegExpSymbol ( * this ); +} + +template < class SymbolType > +UnboundedRegExpElement < SymbolType > * UnboundedRegExpSymbol < SymbolType >::plunder ( ) && { + return new UnboundedRegExpSymbol ( std::move ( * this ) ); +} + +template < class SymbolType > +std::smart_ptr < FormalRegExpElement < SymbolType > > UnboundedRegExpSymbol < SymbolType >::asFormal ( ) const { + return std::smart_ptr < FormalRegExpElement < SymbolType > > ( new FormalRegExpSymbol < SymbolType > ( this->symbol ) ); +} + +template < class SymbolType > +int UnboundedRegExpSymbol < SymbolType >::compare ( const UnboundedRegExpSymbol & other ) const { + return symbol.getData ( ).compare ( other.symbol.getData ( ) ); +} + +template < class SymbolType > +void UnboundedRegExpSymbol < SymbolType >::operator >>( std::ostream & out ) const { + out << "(UnboundedRegExpSymbol " << symbol << ")"; +} + +template < class SymbolType > +bool UnboundedRegExpSymbol < SymbolType >::testSymbol ( const SymbolType & symbol ) const { + return symbol == this->symbol; +} + +template < class SymbolType > +bool UnboundedRegExpSymbol < SymbolType >::checkAlphabet ( const std::set < SymbolType > & alphabet ) const { + return alphabet.count ( symbol ); +} + +template < class SymbolType > +void UnboundedRegExpSymbol < SymbolType >::computeMinimalAlphabet ( std::set < SymbolType > & alphabet ) const { + alphabet.insert ( this->symbol ); +} + +template < class SymbolType > +const SymbolType & UnboundedRegExpSymbol < SymbolType >::getSymbol ( ) const { + return this->symbol; +} + +template < class SymbolType > +UnboundedRegExpSymbol < SymbolType >::operator std::string ( ) const { + std::stringstream ss; + ss << * this; + return ss.str ( ); +} + +} /* namespace regexp */ + #endif /* UNBOUNDED_REG_EXP_SYMBOL_H_ */ diff --git a/alib2data/test-src/automaton/AutomatonTest.cpp b/alib2data/test-src/automaton/AutomatonTest.cpp index 98081d720d..be37250b6b 100644 --- a/alib2data/test-src/automaton/AutomatonTest.cpp +++ b/alib2data/test-src/automaton/AutomatonTest.cpp @@ -161,22 +161,22 @@ void AutomatonTest::testExtendedNFAAlphabet() { automaton.setInputAlphabet({alphabet::symbolFrom('a'), alphabet::symbolFrom('b') }); - regexp::UnboundedRegExpConcatenation goodConcat1, goodConcat2; - regexp::UnboundedRegExpConcatenation badConcat; + regexp::UnboundedRegExpConcatenation < alphabet::Symbol > goodConcat1, goodConcat2; + regexp::UnboundedRegExpConcatenation < alphabet::Symbol > badConcat; - goodConcat1.appendElement(regexp::UnboundedRegExpSymbol(alphabet::symbolFrom('a'))); - goodConcat1.appendElement(regexp::UnboundedRegExpSymbol(alphabet::symbolFrom('b'))); + goodConcat1.appendElement(regexp::UnboundedRegExpSymbol < alphabet::Symbol > (alphabet::symbolFrom('a'))); + goodConcat1.appendElement(regexp::UnboundedRegExpSymbol < alphabet::Symbol > (alphabet::symbolFrom('b'))); - goodConcat2.appendElement(regexp::UnboundedRegExpSymbol(alphabet::symbolFrom('b'))); - goodConcat2.appendElement(regexp::UnboundedRegExpSymbol(alphabet::symbolFrom('b'))); + goodConcat2.appendElement(regexp::UnboundedRegExpSymbol < alphabet::Symbol > (alphabet::symbolFrom('b'))); + goodConcat2.appendElement(regexp::UnboundedRegExpSymbol < alphabet::Symbol > (alphabet::symbolFrom('b'))); - badConcat.appendElement(regexp::UnboundedRegExpSymbol(alphabet::symbolFrom('b'))); - badConcat.appendElement(regexp::UnboundedRegExpSymbol(alphabet::symbolFrom('d'))); + badConcat.appendElement(regexp::UnboundedRegExpSymbol < alphabet::Symbol > (alphabet::symbolFrom('b'))); + badConcat.appendElement(regexp::UnboundedRegExpSymbol < alphabet::Symbol > (alphabet::symbolFrom('d'))); - CPPUNIT_ASSERT_THROW(automaton.addTransition(s0, regexp::UnboundedRegExpStructure(badConcat), s1), automaton::AutomatonException); - CPPUNIT_ASSERT_NO_THROW(automaton.addTransition(s0, regexp::UnboundedRegExpStructure(goodConcat1), s1)); - CPPUNIT_ASSERT_NO_THROW(automaton.addTransition(s0, regexp::UnboundedRegExpStructure(goodConcat2), s1)); - CPPUNIT_ASSERT_NO_THROW(automaton.addTransition(s0, regexp::UnboundedRegExpStructure(regexp::UnboundedRegExpEpsilon()), s1)); + CPPUNIT_ASSERT_THROW(automaton.addTransition(s0, regexp::UnboundedRegExpStructure < alphabet::Symbol > (badConcat), s1), automaton::AutomatonException); + CPPUNIT_ASSERT_NO_THROW(automaton.addTransition(s0, regexp::UnboundedRegExpStructure < alphabet::Symbol > (goodConcat1), s1)); + CPPUNIT_ASSERT_NO_THROW(automaton.addTransition(s0, regexp::UnboundedRegExpStructure < alphabet::Symbol > (goodConcat2), s1)); + CPPUNIT_ASSERT_NO_THROW(automaton.addTransition(s0, regexp::UnboundedRegExpStructure < alphabet::Symbol > (regexp::UnboundedRegExpEpsilon < alphabet::Symbol > ()), s1)); } void AutomatonTest::testNPDATransitions() { diff --git a/alib2data/test-src/regexp/RegExpTest.cpp b/alib2data/test-src/regexp/RegExpTest.cpp index e592cd62e1..52d806644c 100644 --- a/alib2data/test-src/regexp/RegExpTest.cpp +++ b/alib2data/test-src/regexp/RegExpTest.cpp @@ -27,63 +27,63 @@ void RegExpTest::tearDown() { void RegExpTest::testCopyConstruct() { { - regexp::UnboundedRegExpSymbol l1 = regexp::UnboundedRegExpSymbol("1"); - regexp::UnboundedRegExpSymbol l2 = regexp::UnboundedRegExpSymbol("2"); + regexp::UnboundedRegExpSymbol < alphabet::Symbol > l1 = regexp::UnboundedRegExpSymbol < alphabet::Symbol >("1"); + regexp::UnboundedRegExpSymbol < alphabet::Symbol > l2 = regexp::UnboundedRegExpSymbol < alphabet::Symbol >("2"); - regexp::UnboundedRegExpConcatenation con = regexp::UnboundedRegExpConcatenation(); + regexp::UnboundedRegExpConcatenation < alphabet::Symbol > con = regexp::UnboundedRegExpConcatenation < alphabet::Symbol >(); con.appendElement(l1); con.appendElement(l2); - regexp::UnboundedRegExpIteration ite = regexp::UnboundedRegExpIteration(l1); + regexp::UnboundedRegExpIteration < alphabet::Symbol > ite = regexp::UnboundedRegExpIteration < alphabet::Symbol >(l1); - regexp::UnboundedRegExpAlternation alt = regexp::UnboundedRegExpAlternation(); + regexp::UnboundedRegExpAlternation < alphabet::Symbol > alt = regexp::UnboundedRegExpAlternation < alphabet::Symbol >(); alt.appendElement(con); alt.appendElement(ite); - regexp::UnboundedRegExp regexp({alphabet::symbolFrom("1"), alphabet::symbolFrom("2"), alphabet::symbolFrom("3")}, regexp::UnboundedRegExpStructure ( alt ) ); + regexp::UnboundedRegExp < alphabet::Symbol > regexp({alphabet::symbolFrom("1"), alphabet::symbolFrom("2"), alphabet::symbolFrom("3")}, regexp::UnboundedRegExpStructure < alphabet::Symbol > ( alt ) ); - regexp::UnboundedRegExp regexp2(regexp); + regexp::UnboundedRegExp < alphabet::Symbol > regexp2(regexp); CPPUNIT_ASSERT( regexp == regexp2 ); - regexp::UnboundedRegExp regexp3(std::move(regexp)); + regexp::UnboundedRegExp < alphabet::Symbol > regexp3(std::move(regexp)); CPPUNIT_ASSERT( regexp2 == regexp3 ); } { - regexp::FormalRegExpSymbol l1 = regexp::FormalRegExpSymbol("1"); - regexp::FormalRegExpSymbol l2 = regexp::FormalRegExpSymbol("2"); + regexp::FormalRegExpSymbol < alphabet::Symbol > l1 = regexp::FormalRegExpSymbol < alphabet::Symbol >("1"); + regexp::FormalRegExpSymbol < alphabet::Symbol > l2 = regexp::FormalRegExpSymbol < alphabet::Symbol >("2"); - regexp::FormalRegExpConcatenation con = regexp::FormalRegExpConcatenation(l1, l2); + regexp::FormalRegExpConcatenation < alphabet::Symbol > con = regexp::FormalRegExpConcatenation < alphabet::Symbol >(l1, l2); - regexp::FormalRegExpIteration ite = regexp::FormalRegExpIteration(l1); + regexp::FormalRegExpIteration < alphabet::Symbol > ite = regexp::FormalRegExpIteration < alphabet::Symbol >(l1); - regexp::FormalRegExpAlternation alt = regexp::FormalRegExpAlternation(con, ite); + regexp::FormalRegExpAlternation < alphabet::Symbol > alt = regexp::FormalRegExpAlternation < alphabet::Symbol >(con, ite); - regexp::FormalRegExp regexp({alphabet::symbolFrom("1"), alphabet::symbolFrom("2"), alphabet::symbolFrom("3")}, regexp::FormalRegExpStructure ( alt ) ); + regexp::FormalRegExp < alphabet::Symbol > regexp({alphabet::symbolFrom("1"), alphabet::symbolFrom("2"), alphabet::symbolFrom("3")}, regexp::FormalRegExpStructure < alphabet::Symbol > ( alt ) ); - regexp::FormalRegExp regexp2(regexp); + regexp::FormalRegExp < alphabet::Symbol > regexp2(regexp); CPPUNIT_ASSERT( regexp == regexp2 ); - regexp::FormalRegExp regexp3(std::move(regexp)); + regexp::FormalRegExp < alphabet::Symbol > regexp3(std::move(regexp)); CPPUNIT_ASSERT( regexp2 == regexp3 ); } { - regexp::FormalRegExpSymbol l1 = regexp::FormalRegExpSymbol("1"); - regexp::FormalRegExpSymbol l2 = regexp::FormalRegExpSymbol("2"); + regexp::FormalRegExpSymbol < alphabet::Symbol > l1 = regexp::FormalRegExpSymbol < alphabet::Symbol >("1"); + regexp::FormalRegExpSymbol < alphabet::Symbol > l2 = regexp::FormalRegExpSymbol < alphabet::Symbol >("2"); - regexp::FormalRegExpConcatenation con = regexp::FormalRegExpConcatenation(l1, l2); + regexp::FormalRegExpConcatenation < alphabet::Symbol > con = regexp::FormalRegExpConcatenation < alphabet::Symbol >(l1, l2); - regexp::FormalRegExpIteration ite = regexp::FormalRegExpIteration(l1); + regexp::FormalRegExpIteration < alphabet::Symbol > ite = regexp::FormalRegExpIteration < alphabet::Symbol >(l1); - regexp::FormalRegExpAlternation alt = regexp::FormalRegExpAlternation(con, ite); + regexp::FormalRegExpAlternation < alphabet::Symbol > alt = regexp::FormalRegExpAlternation < alphabet::Symbol >(con, ite); - regexp::FormalRegExp regexp({alphabet::symbolFrom("1"), alphabet::symbolFrom("3")}, regexp::FormalRegExpStructure ( regexp::FormalRegExpEmpty { } ) ); + regexp::FormalRegExp < alphabet::Symbol > regexp({alphabet::symbolFrom("1"), alphabet::symbolFrom("3")}, regexp::FormalRegExpStructure < alphabet::Symbol > ( regexp::FormalRegExpEmpty < alphabet::Symbol > { } ) ); CPPUNIT_ASSERT_THROW( - regexp.setRegExp ( regexp::FormalRegExpStructure ( alt ) ), + regexp.setRegExp ( regexp::FormalRegExpStructure < alphabet::Symbol > ( alt ) ), exception::CommonException ); } @@ -91,20 +91,20 @@ void RegExpTest::testCopyConstruct() { void RegExpTest::testXMLParser() { - regexp::UnboundedRegExpSymbol l1 = regexp::UnboundedRegExpSymbol("1"); - regexp::UnboundedRegExpSymbol l2 = regexp::UnboundedRegExpSymbol("2"); + regexp::UnboundedRegExpSymbol < alphabet::Symbol > l1 = regexp::UnboundedRegExpSymbol < alphabet::Symbol >("1"); + regexp::UnboundedRegExpSymbol < alphabet::Symbol > l2 = regexp::UnboundedRegExpSymbol < alphabet::Symbol >("2"); - regexp::UnboundedRegExpConcatenation con = regexp::UnboundedRegExpConcatenation(); + regexp::UnboundedRegExpConcatenation < alphabet::Symbol > con = regexp::UnboundedRegExpConcatenation < alphabet::Symbol >(); con.appendElement(l1); con.appendElement(l2); - regexp::UnboundedRegExpIteration ite = regexp::UnboundedRegExpIteration(l1); + regexp::UnboundedRegExpIteration < alphabet::Symbol > ite = regexp::UnboundedRegExpIteration < alphabet::Symbol >(l1); - regexp::UnboundedRegExpAlternation alt = regexp::UnboundedRegExpAlternation(); + regexp::UnboundedRegExpAlternation < alphabet::Symbol > alt = regexp::UnboundedRegExpAlternation < alphabet::Symbol >(); alt.appendElement(con); alt.appendElement(ite); - regexp::UnboundedRegExp unboundedRegexp({alphabet::symbolFrom("1"), alphabet::symbolFrom("2"), alphabet::symbolFrom("3")}, regexp::UnboundedRegExpStructure(alt)); + regexp::UnboundedRegExp < alphabet::Symbol > unboundedRegexp({alphabet::symbolFrom("1"), alphabet::symbolFrom("2"), alphabet::symbolFrom("3")}, regexp::UnboundedRegExpStructure < alphabet::Symbol >(alt)); regexp::RegExp regexp(unboundedRegexp); @@ -128,23 +128,23 @@ void RegExpTest::testXMLParser() { } void RegExpTest::testOrder() { - regexp::UnboundedRegExpSymbol s1("1"); - regexp::UnboundedRegExpEmpty e1; - regexp::UnboundedRegExpEpsilon e2; - regexp::UnboundedRegExpIteration i1(s1); - regexp::UnboundedRegExpConcatenation a1; + regexp::UnboundedRegExpSymbol < alphabet::Symbol > s1("1"); + regexp::UnboundedRegExpEmpty < alphabet::Symbol > e1; + regexp::UnboundedRegExpEpsilon < alphabet::Symbol > e2; + regexp::UnboundedRegExpIteration < alphabet::Symbol > i1(s1); + regexp::UnboundedRegExpConcatenation < alphabet::Symbol > a1; a1.appendElement(s1); a1.appendElement(s1); - regexp::UnboundedRegExpAlternation c1; + regexp::UnboundedRegExpAlternation < alphabet::Symbol > c1; c1.appendElement(s1); c1.appendElement(s1); - regexp::UnboundedRegExp alt1{regexp::UnboundedRegExpStructure(a1)}; - regexp::UnboundedRegExp con1{regexp::UnboundedRegExpStructure(c1)}; - regexp::UnboundedRegExp ite1{regexp::UnboundedRegExpStructure(i1)}; - regexp::UnboundedRegExp emp1{regexp::UnboundedRegExpStructure(e1)}; - regexp::UnboundedRegExp eps1{regexp::UnboundedRegExpStructure(e2)}; - regexp::UnboundedRegExp sym1{regexp::UnboundedRegExpStructure(s1)}; + regexp::UnboundedRegExp < alphabet::Symbol > alt1{regexp::UnboundedRegExpStructure < alphabet::Symbol >(a1)}; + regexp::UnboundedRegExp < alphabet::Symbol > con1{regexp::UnboundedRegExpStructure < alphabet::Symbol >(c1)}; + regexp::UnboundedRegExp < alphabet::Symbol > ite1{regexp::UnboundedRegExpStructure < alphabet::Symbol >(i1)}; + regexp::UnboundedRegExp < alphabet::Symbol > emp1{regexp::UnboundedRegExpStructure < alphabet::Symbol >(e1)}; + regexp::UnboundedRegExp < alphabet::Symbol > eps1{regexp::UnboundedRegExpStructure < alphabet::Symbol >(e2)}; + regexp::UnboundedRegExp < alphabet::Symbol > sym1{regexp::UnboundedRegExpStructure < alphabet::Symbol >(s1)}; CPPUNIT_EXCLUSIVE_OR(alt1 < con1, con1 < alt1); CPPUNIT_EXCLUSIVE_OR(alt1 < ite1, ite1 < alt1); @@ -328,103 +328,103 @@ void RegExpTest::testOrder() { } void RegExpTest::testOrder2() { - regexp::UnboundedRegExpSymbol s1("1"); - regexp::UnboundedRegExpSymbol s2("2"); - regexp::UnboundedRegExpSymbol s3("3"); + regexp::UnboundedRegExpSymbol < alphabet::Symbol > s1("1"); + regexp::UnboundedRegExpSymbol < alphabet::Symbol > s2("2"); + regexp::UnboundedRegExpSymbol < alphabet::Symbol > s3("3"); - regexp::UnboundedRegExpEmpty e1; - regexp::UnboundedRegExpEpsilon e2; + regexp::UnboundedRegExpEmpty < alphabet::Symbol > e1; + regexp::UnboundedRegExpEpsilon < alphabet::Symbol > e2; - regexp::UnboundedRegExpIteration i1(s1); - regexp::UnboundedRegExpIteration i2(s2); - regexp::UnboundedRegExpIteration i3(s3); + regexp::UnboundedRegExpIteration < alphabet::Symbol > i1(s1); + regexp::UnboundedRegExpIteration < alphabet::Symbol > i2(s2); + regexp::UnboundedRegExpIteration < alphabet::Symbol > i3(s3); - regexp::UnboundedRegExpAlternation a1; + regexp::UnboundedRegExpAlternation < alphabet::Symbol > a1; a1.appendElement(s1); a1.appendElement(s1); - regexp::UnboundedRegExpAlternation a2; + regexp::UnboundedRegExpAlternation < alphabet::Symbol > a2; a2.appendElement(s1); a2.appendElement(s2); - regexp::UnboundedRegExpAlternation a3; + regexp::UnboundedRegExpAlternation < alphabet::Symbol > a3; a3.appendElement(s1); a3.appendElement(s3); - regexp::UnboundedRegExpAlternation a4; + regexp::UnboundedRegExpAlternation < alphabet::Symbol > a4; a4.appendElement(s2); a4.appendElement(s1); - regexp::UnboundedRegExpAlternation a5; + regexp::UnboundedRegExpAlternation < alphabet::Symbol > a5; a5.appendElement(s2); a5.appendElement(s2); - regexp::UnboundedRegExpAlternation a6; + regexp::UnboundedRegExpAlternation < alphabet::Symbol > a6; a6.appendElement(s2); a6.appendElement(s3); - regexp::UnboundedRegExpAlternation a7; + regexp::UnboundedRegExpAlternation < alphabet::Symbol > a7; a7.appendElement(s3); a7.appendElement(s1); - regexp::UnboundedRegExpAlternation a8; + regexp::UnboundedRegExpAlternation < alphabet::Symbol > a8; a8.appendElement(s3); a8.appendElement(s2); - regexp::UnboundedRegExpAlternation a9; + regexp::UnboundedRegExpAlternation < alphabet::Symbol > a9; a9.appendElement(s3); a9.appendElement(s3); - regexp::UnboundedRegExpConcatenation c1; + regexp::UnboundedRegExpConcatenation < alphabet::Symbol > c1; c1.appendElement(s1); c1.appendElement(s1); - regexp::UnboundedRegExpConcatenation c2; + regexp::UnboundedRegExpConcatenation < alphabet::Symbol > c2; c2.appendElement(s1); c2.appendElement(s2); - regexp::UnboundedRegExpConcatenation c3; + regexp::UnboundedRegExpConcatenation < alphabet::Symbol > c3; c3.appendElement(s1); c3.appendElement(s3); - regexp::UnboundedRegExpConcatenation c4; + regexp::UnboundedRegExpConcatenation < alphabet::Symbol > c4; c4.appendElement(s2); c4.appendElement(s1); - regexp::UnboundedRegExpConcatenation c5; + regexp::UnboundedRegExpConcatenation < alphabet::Symbol > c5; c5.appendElement(s2); c5.appendElement(s2); - regexp::UnboundedRegExpConcatenation c6; + regexp::UnboundedRegExpConcatenation < alphabet::Symbol > c6; c6.appendElement(s2); c6.appendElement(s3); - regexp::UnboundedRegExpConcatenation c7; + regexp::UnboundedRegExpConcatenation < alphabet::Symbol > c7; c7.appendElement(s3); c7.appendElement(s1); - regexp::UnboundedRegExpConcatenation c8; + regexp::UnboundedRegExpConcatenation < alphabet::Symbol > c8; c8.appendElement(s3); c8.appendElement(s2); - regexp::UnboundedRegExpConcatenation c9; + regexp::UnboundedRegExpConcatenation < alphabet::Symbol > c9; c9.appendElement(s3); c9.appendElement(s3); - regexp::UnboundedRegExp alt1{regexp::UnboundedRegExpStructure(a1)}; - regexp::UnboundedRegExp alt2{regexp::UnboundedRegExpStructure(a2)}; - regexp::UnboundedRegExp alt3{regexp::UnboundedRegExpStructure(a3)}; - regexp::UnboundedRegExp alt4{regexp::UnboundedRegExpStructure(a4)}; - regexp::UnboundedRegExp alt5{regexp::UnboundedRegExpStructure(a5)}; - regexp::UnboundedRegExp alt6{regexp::UnboundedRegExpStructure(a6)}; - regexp::UnboundedRegExp alt7{regexp::UnboundedRegExpStructure(a7)}; - regexp::UnboundedRegExp alt8{regexp::UnboundedRegExpStructure(a8)}; - regexp::UnboundedRegExp alt9{regexp::UnboundedRegExpStructure(a9)}; - - regexp::UnboundedRegExp con1{regexp::UnboundedRegExpStructure(c1)}; - regexp::UnboundedRegExp con2{regexp::UnboundedRegExpStructure(c2)}; - regexp::UnboundedRegExp con3{regexp::UnboundedRegExpStructure(c3)}; - regexp::UnboundedRegExp con4{regexp::UnboundedRegExpStructure(c4)}; - regexp::UnboundedRegExp con5{regexp::UnboundedRegExpStructure(c5)}; - regexp::UnboundedRegExp con6{regexp::UnboundedRegExpStructure(c6)}; - regexp::UnboundedRegExp con7{regexp::UnboundedRegExpStructure(c7)}; - regexp::UnboundedRegExp con8{regexp::UnboundedRegExpStructure(c8)}; - regexp::UnboundedRegExp con9{regexp::UnboundedRegExpStructure(c9)}; - - regexp::UnboundedRegExp ite1{regexp::UnboundedRegExpStructure(i1)}; - regexp::UnboundedRegExp ite2{regexp::UnboundedRegExpStructure(i2)}; - regexp::UnboundedRegExp ite3{regexp::UnboundedRegExpStructure(i3)}; - - regexp::UnboundedRegExp emp1{regexp::UnboundedRegExpStructure(e1)}; - regexp::UnboundedRegExp eps1{regexp::UnboundedRegExpStructure(e2)}; - - regexp::UnboundedRegExp sym1{regexp::UnboundedRegExpStructure(s1)}; - regexp::UnboundedRegExp sym2{regexp::UnboundedRegExpStructure(s2)}; - regexp::UnboundedRegExp sym3{regexp::UnboundedRegExpStructure(s3)}; + regexp::UnboundedRegExp < alphabet::Symbol > alt1{regexp::UnboundedRegExpStructure < alphabet::Symbol >(a1)}; + regexp::UnboundedRegExp < alphabet::Symbol > alt2{regexp::UnboundedRegExpStructure < alphabet::Symbol >(a2)}; + regexp::UnboundedRegExp < alphabet::Symbol > alt3{regexp::UnboundedRegExpStructure < alphabet::Symbol >(a3)}; + regexp::UnboundedRegExp < alphabet::Symbol > alt4{regexp::UnboundedRegExpStructure < alphabet::Symbol >(a4)}; + regexp::UnboundedRegExp < alphabet::Symbol > alt5{regexp::UnboundedRegExpStructure < alphabet::Symbol >(a5)}; + regexp::UnboundedRegExp < alphabet::Symbol > alt6{regexp::UnboundedRegExpStructure < alphabet::Symbol >(a6)}; + regexp::UnboundedRegExp < alphabet::Symbol > alt7{regexp::UnboundedRegExpStructure < alphabet::Symbol >(a7)}; + regexp::UnboundedRegExp < alphabet::Symbol > alt8{regexp::UnboundedRegExpStructure < alphabet::Symbol >(a8)}; + regexp::UnboundedRegExp < alphabet::Symbol > alt9{regexp::UnboundedRegExpStructure < alphabet::Symbol >(a9)}; + + regexp::UnboundedRegExp < alphabet::Symbol > con1{regexp::UnboundedRegExpStructure < alphabet::Symbol >(c1)}; + regexp::UnboundedRegExp < alphabet::Symbol > con2{regexp::UnboundedRegExpStructure < alphabet::Symbol >(c2)}; + regexp::UnboundedRegExp < alphabet::Symbol > con3{regexp::UnboundedRegExpStructure < alphabet::Symbol >(c3)}; + regexp::UnboundedRegExp < alphabet::Symbol > con4{regexp::UnboundedRegExpStructure < alphabet::Symbol >(c4)}; + regexp::UnboundedRegExp < alphabet::Symbol > con5{regexp::UnboundedRegExpStructure < alphabet::Symbol >(c5)}; + regexp::UnboundedRegExp < alphabet::Symbol > con6{regexp::UnboundedRegExpStructure < alphabet::Symbol >(c6)}; + regexp::UnboundedRegExp < alphabet::Symbol > con7{regexp::UnboundedRegExpStructure < alphabet::Symbol >(c7)}; + regexp::UnboundedRegExp < alphabet::Symbol > con8{regexp::UnboundedRegExpStructure < alphabet::Symbol >(c8)}; + regexp::UnboundedRegExp < alphabet::Symbol > con9{regexp::UnboundedRegExpStructure < alphabet::Symbol >(c9)}; + + regexp::UnboundedRegExp < alphabet::Symbol > ite1{regexp::UnboundedRegExpStructure < alphabet::Symbol >(i1)}; + regexp::UnboundedRegExp < alphabet::Symbol > ite2{regexp::UnboundedRegExpStructure < alphabet::Symbol >(i2)}; + regexp::UnboundedRegExp < alphabet::Symbol > ite3{regexp::UnboundedRegExpStructure < alphabet::Symbol >(i3)}; + + regexp::UnboundedRegExp < alphabet::Symbol > emp1{regexp::UnboundedRegExpStructure < alphabet::Symbol >(e1)}; + regexp::UnboundedRegExp < alphabet::Symbol > eps1{regexp::UnboundedRegExpStructure < alphabet::Symbol >(e2)}; + + regexp::UnboundedRegExp < alphabet::Symbol > sym1{regexp::UnboundedRegExpStructure < alphabet::Symbol >(s1)}; + regexp::UnboundedRegExp < alphabet::Symbol > sym2{regexp::UnboundedRegExpStructure < alphabet::Symbol >(s2)}; + regexp::UnboundedRegExp < alphabet::Symbol > sym3{regexp::UnboundedRegExpStructure < alphabet::Symbol >(s3)}; -- GitLab