Skip to content
Snippets Groups Projects
Commit 66c912ce authored by Jan Trávníček's avatar Jan Trávníček
Browse files

redesign templates of LG

parent ca5cbd71
No related branches found
No related tags found
No related merge requests found
...@@ -32,7 +32,6 @@ public: ...@@ -32,7 +32,6 @@ public:
/** /**
* Get rules in most common format of rules as mapping from leftHandSide to rightHandSides represented as vectors of symbols. * Get rules in most common format of rules as mapping from leftHandSide to rightHandSides represented as vectors of symbols.
* *
* \tparam SymbolType the type of symbols in the grammar
* \tparam TerminalSymbolType the type of terminal symbols in the grammar * \tparam TerminalSymbolType the type of terminal symbols in the grammar
* \tparam NonterminalSymbolType the type of nontermnal symbols in the grammar * \tparam NonterminalSymbolType the type of nontermnal symbols in the grammar
* *
...@@ -40,8 +39,8 @@ public: ...@@ -40,8 +39,8 @@ public:
* *
* \returns rules of the grammar in a common representation * \returns rules of the grammar in a common representation
*/ */
template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > template < class TerminalSymbolType, class NonterminalSymbolType >
static bool addRawRule ( LG < SymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ); static bool addRawRule ( LG < TerminalSymbolType, NonterminalSymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide );
   
/** /**
* \override * \override
...@@ -92,8 +91,8 @@ public: ...@@ -92,8 +91,8 @@ public:
static bool addRawRule ( RightRG < TerminalSymbolType, NonterminalSymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ); static bool addRawRule ( RightRG < TerminalSymbolType, NonterminalSymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide );
}; };
   
template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > template < class TerminalSymbolType, class NonterminalSymbolType >
bool AddRawRule::addRawRule ( LG < SymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ) { bool AddRawRule::addRawRule ( LG < TerminalSymbolType, NonterminalSymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ) {
typename ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > >::iterator nonterminalPosition = rightHandSide.begin ( ); typename ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > >::iterator nonterminalPosition = rightHandSide.begin ( );
   
for ( ; nonterminalPosition != rightHandSide.end ( ); ++nonterminalPosition ) for ( ; nonterminalPosition != rightHandSide.end ( ); ++nonterminalPosition )
......
This diff is collapsed.
...@@ -32,14 +32,15 @@ public: ...@@ -32,14 +32,15 @@ public:
/** /**
* Get rules in most common format of rules as mapping from leftHandSide to rightHandSides represented as vectors of symbols. * Get rules in most common format of rules as mapping from leftHandSide to rightHandSides represented as vectors of symbols.
* *
* \tparam SymbolType the type of symbols in the grammar * \tparam TerminalSymbolType the type of terminal symbols in the grammar
* \tparam NonterminalSymbolType the type of nontermnal symbols in the grammar
* *
* \param grammar the source grammar of rules to transform * \param grammar the source grammar of rules to transform
* *
* \returns rules of the grammar in a common representation * \returns rules of the grammar in a common representation
*/ */
template < class SymbolType, class TerminalSymbolType = SymbolType, class NonterminalSymbolType = SymbolType > template < class TerminalSymbolType, class NonterminalSymbolType >
static ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > getRawRules ( const LG < SymbolType > & grammar ); static ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > getRawRules ( const LG < TerminalSymbolType, NonterminalSymbolType > & grammar );
   
/** /**
* \override * \override
...@@ -90,8 +91,8 @@ public: ...@@ -90,8 +91,8 @@ public:
static ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > getRawRules ( const RightRG < TerminalSymbolType, NonterminalSymbolType > & grammar ); static ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > getRawRules ( const RightRG < TerminalSymbolType, NonterminalSymbolType > & grammar );
}; };
   
template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > template < class TerminalSymbolType, class NonterminalSymbolType >
ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > RawRules::getRawRules ( const LG < SymbolType > & grammar ) { ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > RawRules::getRawRules ( const LG < TerminalSymbolType, NonterminalSymbolType > & grammar ) {
ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > res; ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > res;
   
for ( const auto & rule : grammar.getRules ( ) ) for ( const auto & rule : grammar.getRules ( ) )
......
...@@ -33,8 +33,8 @@ ...@@ -33,8 +33,8 @@
   
namespace core { namespace core {
   
template < class SymbolType > template < class TerminalSymbolType, class NonterminalSymbolType >
struct xmlApi < grammar::LG < SymbolType > > { struct xmlApi < grammar::LG < TerminalSymbolType, NonterminalSymbolType > > {
/** /**
* \brief The XML tag name of class. * \brief The XML tag name of class.
* *
...@@ -66,7 +66,7 @@ struct xmlApi < grammar::LG < SymbolType > > { ...@@ -66,7 +66,7 @@ struct xmlApi < grammar::LG < SymbolType > > {
* *
* \returns the new instance of the grammar * \returns the new instance of the grammar
*/ */
static grammar::LG < SymbolType > parse ( ext::deque < sax::Token >::iterator & input ); static grammar::LG < TerminalSymbolType, NonterminalSymbolType > parse ( ext::deque < sax::Token >::iterator & input );
   
/** /**
* Helper for parsing of individual rules of the grammar from a sequence of xml tokens. * Helper for parsing of individual rules of the grammar from a sequence of xml tokens.
...@@ -74,7 +74,7 @@ struct xmlApi < grammar::LG < SymbolType > > { ...@@ -74,7 +74,7 @@ struct xmlApi < grammar::LG < SymbolType > > {
* \params input the iterator to sequence of xml tokens to parse from * \params input the iterator to sequence of xml tokens to parse from
* \params grammar the grammar to add the rule to * \params grammar the grammar to add the rule to
*/ */
static void parseRule ( ext::deque < sax::Token >::iterator & input, grammar::LG < SymbolType > & grammar ); static void parseRule ( ext::deque < sax::Token >::iterator & input, grammar::LG < TerminalSymbolType, NonterminalSymbolType > & grammar );
   
/** /**
* Composing to a sequence of xml tokens helper. * Composing to a sequence of xml tokens helper.
...@@ -82,7 +82,7 @@ struct xmlApi < grammar::LG < SymbolType > > { ...@@ -82,7 +82,7 @@ struct xmlApi < grammar::LG < SymbolType > > {
* \param out sink for new xml tokens representing the grammar * \param out sink for new xml tokens representing the grammar
* \param grammar the grammar to compose * \param grammar the grammar to compose
*/ */
static void compose ( ext::deque < sax::Token > & out, const grammar::LG < SymbolType > & grammar ); static void compose ( ext::deque < sax::Token > & out, const grammar::LG < TerminalSymbolType, NonterminalSymbolType > & grammar );
   
/** /**
* Helper for composing rules of the grammar to a sequence of xml tokens. * Helper for composing rules of the grammar to a sequence of xml tokens.
...@@ -90,18 +90,18 @@ struct xmlApi < grammar::LG < SymbolType > > { ...@@ -90,18 +90,18 @@ struct xmlApi < grammar::LG < SymbolType > > {
* \param out sink for xml tokens representing the rules of the grammar * \param out sink for xml tokens representing the rules of the grammar
* \param grammar the grammar to compose * \param grammar the grammar to compose
*/ */
static void composeRules ( ext::deque < sax::Token > & out, const grammar::LG < SymbolType > & grammar ); static void composeRules ( ext::deque < sax::Token > & out, const grammar::LG < TerminalSymbolType, NonterminalSymbolType > & grammar );
}; };
   
template < class SymbolType > template < class TerminalSymbolType, class NonterminalSymbolType >
grammar::LG < SymbolType > xmlApi < grammar::LG < SymbolType > >::parse ( ext::deque < sax::Token >::iterator & input ) { grammar::LG < TerminalSymbolType, NonterminalSymbolType > xmlApi < grammar::LG < TerminalSymbolType, NonterminalSymbolType > >::parse ( ext::deque < sax::Token >::iterator & input ) {
sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, xmlTagName ( ) ); sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, xmlTagName ( ) );
   
ext::set < SymbolType > nonterminalAlphabet = grammar::GrammarFromXMLParser::parseNonterminalAlphabet < SymbolType > ( input ); ext::set < NonterminalSymbolType > nonterminalAlphabet = grammar::GrammarFromXMLParser::parseNonterminalAlphabet < NonterminalSymbolType > ( input );
ext::set < SymbolType > terminalAlphabet = grammar::GrammarFromXMLParser::parseTerminalAlphabet < SymbolType > ( input ); ext::set < TerminalSymbolType > terminalAlphabet = grammar::GrammarFromXMLParser::parseTerminalAlphabet < TerminalSymbolType > ( input );
SymbolType initialSymbol = grammar::GrammarFromXMLParser::parseInitialSymbol < SymbolType > ( input ); NonterminalSymbolType initialSymbol = grammar::GrammarFromXMLParser::parseInitialSymbol < NonterminalSymbolType > ( input );
   
grammar::LG < SymbolType > grammar ( std::move ( initialSymbol ) ); grammar::LG < TerminalSymbolType, NonterminalSymbolType > grammar ( std::move ( initialSymbol ) );
   
grammar.setNonterminalAlphabet ( std::move ( nonterminalAlphabet ) ); grammar.setNonterminalAlphabet ( std::move ( nonterminalAlphabet ) );
grammar.setTerminalAlphabet ( std::move ( terminalAlphabet ) ); grammar.setTerminalAlphabet ( std::move ( terminalAlphabet ) );
...@@ -112,16 +112,16 @@ grammar::LG < SymbolType > xmlApi < grammar::LG < SymbolType > >::parse ( ext::d ...@@ -112,16 +112,16 @@ grammar::LG < SymbolType > xmlApi < grammar::LG < SymbolType > >::parse ( ext::d
return grammar; return grammar;
} }
   
template < class SymbolType > template < class TerminalSymbolType, class NonterminalSymbolType >
void xmlApi < grammar::LG < SymbolType > >::parseRule ( ext::deque < sax::Token >::iterator & input, grammar::LG < SymbolType > & grammar ) { void xmlApi < grammar::LG < TerminalSymbolType, NonterminalSymbolType > >::parseRule ( ext::deque < sax::Token >::iterator & input, grammar::LG < TerminalSymbolType, NonterminalSymbolType > & grammar ) {
SymbolType lhs = grammar::GrammarFromXMLParser::parseRuleSingleSymbolLHS < SymbolType > ( input ); NonterminalSymbolType lhs = grammar::GrammarFromXMLParser::parseRuleSingleSymbolLHS < NonterminalSymbolType > ( input );
ext::vector < ext::variant < SymbolType, SymbolType > > rhs = grammar::GrammarFromXMLParser::parseRuleRHS < ext::variant < SymbolType, SymbolType > > ( input ); ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rhs = grammar::GrammarFromXMLParser::parseRuleRHS < ext::variant < TerminalSymbolType, NonterminalSymbolType > > ( input );
   
grammar::AddRawRule::addRawRule ( grammar, std::move ( lhs ), std::move ( rhs ) ); grammar::AddRawRule::addRawRule ( grammar, std::move ( lhs ), std::move ( rhs ) );
} }
   
template < class SymbolType > template < class TerminalSymbolType, class NonterminalSymbolType >
void xmlApi < grammar::LG < SymbolType > >::compose ( ext::deque < sax::Token > & out, const grammar::LG < SymbolType > & grammar ) { void xmlApi < grammar::LG < TerminalSymbolType, NonterminalSymbolType > >::compose ( ext::deque < sax::Token > & out, const grammar::LG < TerminalSymbolType, NonterminalSymbolType > & grammar ) {
out.emplace_back ( xmlTagName ( ), sax::Token::TokenType::START_ELEMENT ); out.emplace_back ( xmlTagName ( ), sax::Token::TokenType::START_ELEMENT );
   
grammar::GrammarToXMLComposer::composeNonterminalAlphabet ( out, grammar.getNonterminalAlphabet ( ) ); grammar::GrammarToXMLComposer::composeNonterminalAlphabet ( out, grammar.getNonterminalAlphabet ( ) );
...@@ -132,8 +132,8 @@ void xmlApi < grammar::LG < SymbolType > >::compose ( ext::deque < sax::Token > ...@@ -132,8 +132,8 @@ void xmlApi < grammar::LG < SymbolType > >::compose ( ext::deque < sax::Token >
out.emplace_back ( xmlTagName ( ), sax::Token::TokenType::END_ELEMENT ); out.emplace_back ( xmlTagName ( ), sax::Token::TokenType::END_ELEMENT );
} }
   
template < class SymbolType > template < class TerminalSymbolType, class NonterminalSymbolType >
void xmlApi < grammar::LG < SymbolType > >::composeRules ( ext::deque < sax::Token > & out, const grammar::LG < SymbolType > & grammar ) { void xmlApi < grammar::LG < TerminalSymbolType, NonterminalSymbolType > >::composeRules ( ext::deque < sax::Token > & out, const grammar::LG < TerminalSymbolType, NonterminalSymbolType > & grammar ) {
out.emplace_back ( "rules", sax::Token::TokenType::START_ELEMENT ); out.emplace_back ( "rules", sax::Token::TokenType::START_ELEMENT );
   
for ( const auto & rule : grammar.getRules ( ) ) for ( const auto & rule : grammar.getRules ( ) )
......
...@@ -155,18 +155,18 @@ void GrammarTest::testRegularParser() { ...@@ -155,18 +155,18 @@ void GrammarTest::testRegularParser() {
   
void GrammarTest::testContextFreeParser() { void GrammarTest::testContextFreeParser() {
{ {
grammar::LG < > grammar(DefaultSymbolType(1)); grammar::LG < std::string, int > grammar ( 1 );
   
grammar.addNonterminalSymbol(DefaultSymbolType(1)); grammar.addNonterminalSymbol ( 1 );
grammar.addNonterminalSymbol(DefaultSymbolType(2)); grammar.addNonterminalSymbol ( 2 );
grammar.addNonterminalSymbol(DefaultSymbolType(3)); grammar.addNonterminalSymbol ( 3 );
grammar.addTerminalSymbol(DefaultSymbolType("a")); grammar.addTerminalSymbol ( "a" );
grammar.addTerminalSymbol(DefaultSymbolType("b")); grammar.addTerminalSymbol ( "b" );
   
grammar.addRule(DefaultSymbolType(1), ext::make_tuple(ext::vector<DefaultSymbolType> {DefaultSymbolType("a")}, DefaultSymbolType(2), ext::vector<DefaultSymbolType>{})); grammar.addRule ( 1, ext::make_tuple ( ext::vector < std::string > { "a" }, 2, ext::vector < std::string > { } ) );
grammar.addRule(DefaultSymbolType(2), ext::make_tuple(ext::vector<DefaultSymbolType> {DefaultSymbolType("b")}, DefaultSymbolType(3), ext::vector<DefaultSymbolType>{})); grammar.addRule ( 2, ext::make_tuple ( ext::vector < std::string > { "b" }, 3, ext::vector < std::string > { } ) );
grammar.addRule(DefaultSymbolType(1), ext::make_tuple(ext::vector<DefaultSymbolType> {}, DefaultSymbolType(3), ext::vector<DefaultSymbolType>{})); grammar.addRule ( 1, ext::make_tuple ( ext::vector < std::string > { }, 3, ext::vector < std::string > { } ) );
grammar.addRule(DefaultSymbolType(1), ext::make_tuple(ext::vector<DefaultSymbolType> {DefaultSymbolType("a"), DefaultSymbolType("a"), DefaultSymbolType("a")}, DefaultSymbolType(2), ext::vector<DefaultSymbolType>{})); grammar.addRule ( 1, ext::make_tuple ( ext::vector < std::string > { "a", "a", "a"}, 2, ext::vector < std::string > { } ) );
   
CPPUNIT_ASSERT( grammar == grammar ); CPPUNIT_ASSERT( grammar == grammar );
{ {
...@@ -174,7 +174,7 @@ void GrammarTest::testContextFreeParser() { ...@@ -174,7 +174,7 @@ void GrammarTest::testContextFreeParser() {
std::string tmp = sax::SaxComposeInterface::composeMemory ( tokens ); std::string tmp = sax::SaxComposeInterface::composeMemory ( tokens );
   
ext::deque<sax::Token> tokens2 = sax::SaxParseInterface::parseMemory ( tmp ); ext::deque<sax::Token> tokens2 = sax::SaxParseInterface::parseMemory ( tmp );
grammar::LG < > grammar2 = factory::XmlDataFactory::fromTokens (std::move(tokens2)); grammar::LG < std::string, int > grammar2 = factory::XmlDataFactory::fromTokens (std::move(tokens2));
   
CPPUNIT_ASSERT( grammar == grammar2 ); CPPUNIT_ASSERT( grammar == grammar2 );
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment