From fbd0101cbcc040c13ecc072563464466374c198f Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Fri, 21 Jul 2017 07:59:28 +0200 Subject: [PATCH] variadic number of tokens to match, code organize --- alib2cli/src/parser/Parser.h | 24 ++++++++------------- alib2common/src/common/createUnique.hpp | 19 +++-------------- alib2std/src/extensions/algorithm.hpp | 28 +++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 31 deletions(-) diff --git a/alib2cli/src/parser/Parser.h b/alib2cli/src/parser/Parser.h index 11b227b761..35d4bb570a 100644 --- a/alib2cli/src/parser/Parser.h +++ b/alib2cli/src/parser/Parser.h @@ -9,6 +9,8 @@ #include <exception/CommonException.h> +#include <algorithm> + namespace cli { class Parser { @@ -19,27 +21,19 @@ public: Parser ( cli::Lexer lexer ) : m_lexer ( std::move ( lexer ) ), m_current ( m_lexer.nextToken ( ) ) { } - bool check ( cli::Lexer::TokenType type, cli::Lexer::TokenType type2 ) const { - return m_current.m_type == type || m_current.m_type == type2; - } - - bool check ( cli::Lexer::TokenType type ) const { - return m_current.m_type == type; + template < class ... Tokens > + bool check ( Tokens ... tokens ) const { + // TODO repace in c++17 with fold expressions + return std::orAll ( m_current.m_type == tokens ... ); } bool check_nonreserved_kw ( const std::string & kw ) const { return m_current.m_type == Lexer::TokenType::IDENTIFIER && m_current.m_value == kw; } - bool match ( cli::Lexer::TokenType type, cli::Lexer::TokenType type2 ) { - if ( ! check ( type, type2 ) ) - throw exception::CommonException ( "Mismatched token while matching a token." ); - m_current = m_lexer.nextToken ( ); - return true; - } - - bool match ( cli::Lexer::TokenType type ) { - if ( ! check ( type ) ) + template < class ... Tokens > + bool match ( Tokens ... tokens ) { + if ( ! check ( tokens ... ) ) throw exception::CommonException ( "Mismatched token while matching a token." ); m_current = m_lexer.nextToken ( ); return true; diff --git a/alib2common/src/common/createUnique.hpp b/alib2common/src/common/createUnique.hpp index dd64e2517b..2e89d88817 100644 --- a/alib2common/src/common/createUnique.hpp +++ b/alib2common/src/common/createUnique.hpp @@ -12,23 +12,10 @@ #include <set> #include "exception/CommonException.h" #include <climits> +#include <algorithm> namespace common { -// TODO repace in c++11 with fold expansion -template < class ... Ts > -inline bool andAll ( Ts ... ); - -template < > -inline bool andAll ( ) { - return true; -} - -template < class T, class ... Ts > -inline bool andAll ( T value, Ts ... other ) { - return value && andAll ( other ... ); -} - /** * Creates and adds unique state to grammar. If given state name is * already used, appends apostrophe or integer suffix @@ -42,8 +29,8 @@ typename std::enable_if < std::all_same < std::set < T >, Alphabets ... >::value unsigned i = 0; do { - // TODO repace in c++11 with fold expansion - if ( andAll ( alphabets.count ( attempt ) == 0 ... ) ) + // TODO repace in c++17 with fold expressions + if ( std::andAll ( alphabets.count ( attempt ) == 0 ... ) ) return attempt; ++attempt; diff --git a/alib2std/src/extensions/algorithm.hpp b/alib2std/src/extensions/algorithm.hpp index 87faaff269..9e0b179978 100644 --- a/alib2std/src/extensions/algorithm.hpp +++ b/alib2std/src/extensions/algorithm.hpp @@ -49,6 +49,34 @@ bool binary_contains(InputIt first, InputIt last, const Element& elem) { return binary_search(first, last, elem) != last; } +// TODO repace in c++17 with fold expressions +template < class ... Ts > +inline bool andAll ( Ts ... ); + +template < > +inline bool andAll ( ) { + return true; +} + +template < class T, class ... Ts > +inline bool andAll ( T value, Ts ... other ) { + return value && andAll ( other ... ); +} + +// TODO repace in c++17 with fold expressions +template < class ... Ts > +inline bool orAll ( Ts ... ); + +template < > +inline bool orAll ( ) { + return false; +} + +template < class T, class ... Ts > +inline bool orAll ( T value, Ts ... other ) { + return value || orAll ( other ... ); +} + } /* namespace std */ #endif /* __ALGORITHM_HPP_ */ -- GitLab