From f81b6d49172bbf0185291dfa57b9dbe1c1b52084 Mon Sep 17 00:00:00 2001 From: Matthias Volk Date: Thu, 15 Feb 2018 16:10:00 +0100 Subject: [PATCH] Added NumberParser --- src/storm-dft/parser/DFTGalileoParser.cpp | 7 +++---- src/storm/parser/DirectEncodingParser.cpp | 8 ++++---- src/storm/parser/ValueParser.cpp | 8 +------- src/storm/parser/ValueParser.h | 24 ++++++++++++++++++++--- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/storm-dft/parser/DFTGalileoParser.cpp b/src/storm-dft/parser/DFTGalileoParser.cpp index 8a02ce1fe..b2d145277 100644 --- a/src/storm-dft/parser/DFTGalileoParser.cpp +++ b/src/storm-dft/parser/DFTGalileoParser.cpp @@ -5,7 +5,6 @@ #include #include -#include #include #include "storm/exceptions/NotImplementedException.h" #include "storm/exceptions/FileIoException.h" @@ -114,12 +113,12 @@ namespace storm { } else if (type == "or") { success = builder.addOrElement(name, childNames); } else if (boost::starts_with(type, "vot")) { - unsigned threshold = boost::lexical_cast(type.substr(3)); + unsigned threshold = NumberParser::parse(type.substr(3)); success = builder.addVotElement(name, threshold, childNames); } else if (type.find("of") != std::string::npos) { size_t pos = type.find("of"); - unsigned threshold = boost::lexical_cast(type.substr(0, pos)); - unsigned count = boost::lexical_cast(type.substr(pos + 2)); + unsigned threshold = NumberParser::parse(type.substr(0, pos)); + unsigned count = NumberParser::parse(type.substr(pos + 2)); STORM_LOG_THROW(count == childNames.size(), storm::exceptions::WrongFormatException, "Voting gate number " << count << " does not correspond to number of children " << childNames.size() << "in line " << lineNo << "."); success = builder.addVotElement(name, threshold, childNames); } else if (type == "pand") { diff --git a/src/storm/parser/DirectEncodingParser.cpp b/src/storm/parser/DirectEncodingParser.cpp index 68530a5a7..288eda7e5 100644 --- a/src/storm/parser/DirectEncodingParser.cpp +++ b/src/storm/parser/DirectEncodingParser.cpp @@ -79,7 +79,7 @@ namespace storm { if(line == "@nr_states") { STORM_LOG_THROW(nrStates == 0, storm::exceptions::WrongFormatException, "Number states declared twice"); std::getline(file, line); - nrStates = boost::lexical_cast(line); + nrStates = NumberParser::parse(line); } if(line == "@model") { @@ -132,7 +132,7 @@ namespace storm { size_t parsedId; size_t posId = line.find(" "); if (posId != std::string::npos) { - parsedId = boost::lexical_cast(line.substr(0, posId)); + parsedId = NumberParser::parse(line.substr(0, posId)); // Parse rewards and labels line = line.substr(posId+1); @@ -167,7 +167,7 @@ namespace storm { } } else { // Only state id given - parsedId = boost::lexical_cast(line); + parsedId = NumberParser::parse(line); } STORM_LOG_TRACE("New state " << state); STORM_LOG_ASSERT(state == parsedId, "State ids do not correspond."); @@ -201,7 +201,7 @@ namespace storm { // New transition size_t posColon = line.find(":"); STORM_LOG_ASSERT(posColon != std::string::npos, "':' not found."); - size_t target = boost::lexical_cast(line.substr(2, posColon-3)); + size_t target = NumberParser::parse(line.substr(2, posColon-3)); std::string valueStr = line.substr(posColon+2); ValueType value = valueParser.parseValue(valueStr); STORM_LOG_TRACE("Transition " << row << " -> " << target << ": " << value); diff --git a/src/storm/parser/ValueParser.cpp b/src/storm/parser/ValueParser.cpp index 96ed0015b..17495b0d9 100644 --- a/src/storm/parser/ValueParser.cpp +++ b/src/storm/parser/ValueParser.cpp @@ -1,7 +1,6 @@ #include "storm/parser/ValueParser.h" #include "storm/exceptions/NotSupportedException.h" -#include "storm/exceptions/WrongFormatException.h" namespace storm { namespace parser { @@ -21,12 +20,7 @@ namespace storm { template<> double ValueParser::parseValue(std::string const& value) const { - try { - return boost::lexical_cast(value); - } - catch(boost::bad_lexical_cast &) { - STORM_LOG_THROW(false, storm::exceptions::WrongFormatException, "Could not parse value '" << value << "'."); - } + return NumberParser::parse(value); } template<> diff --git a/src/storm/parser/ValueParser.h b/src/storm/parser/ValueParser.h index ab2e741b6..db948b75e 100644 --- a/src/storm/parser/ValueParser.h +++ b/src/storm/parser/ValueParser.h @@ -4,6 +4,7 @@ #include "storm/storage/expressions/ExpressionManager.h" #include "storm/parser/ExpressionParser.h" #include "storm/storage/expressions/ExpressionEvaluator.h" +#include "storm/exceptions/WrongFormatException.h" namespace storm { namespace parser { @@ -39,14 +40,31 @@ namespace storm { private: std::shared_ptr manager; - storm::parser::ExpressionParser parser; - storm::expressions::ExpressionEvaluator evaluator; - std::unordered_map identifierMapping; }; + template + class NumberParser { + public: + /*! + * Parse number from string. + * + * @param value String containing the value. + * + * @return NumberType. + */ + static NumberType parse(std::string const& value) { + try { + return boost::lexical_cast(value); + } + catch(boost::bad_lexical_cast &) { + STORM_LOG_THROW(false, storm::exceptions::WrongFormatException, "Could not parse value '" << value << "' into " << typeid(NumberType).name() << "."); + } + } + }; + } // namespace parser } // namespace storm