Add small struct DeckKeyword::Location to simplify error reporting

This commit is contained in:
Joakim Hove
2019-10-17 15:25:13 +02:00
parent 007abc79f9
commit 2be6235ab7
5 changed files with 28 additions and 15 deletions

View File

@@ -35,6 +35,20 @@ namespace Opm {
class DeckKeyword {
public:
class Location {
public:
std::string filename;
std::size_t lineno;
Location() = default;
Location(std::string fname, std::size_t lno) :
filename(std::move(fname)),
lineno(lno)
{}
};
typedef std::vector< DeckRecord >::const_iterator const_iterator;
explicit DeckKeyword(const ParserKeyword& parserKeyword);
@@ -51,7 +65,7 @@ namespace Opm {
void setLocation(const std::pair<const std::string&, std::size_t>& location);
const std::string& getFileName() const;
int getLineNumber() const;
std::pair<std::string, std::size_t> location() const;
const Location& location() const;
size_t size() const;
@@ -90,8 +104,7 @@ namespace Opm {
friend std::ostream& operator<<(std::ostream& os, const DeckKeyword& keyword);
private:
std::string m_keywordName;
std::string m_fileName;
int m_lineNumber;
Location m_location;
std::vector< DeckRecord > m_recordList;
bool m_isDataKeyword;

View File

@@ -23,6 +23,8 @@
#include <string>
#include <vector>
#include <opm/parser/eclipse/Deck/DeckKeyword.hpp>
namespace Opm {
namespace Action {
@@ -62,7 +64,7 @@ enum class Comparator {
};
Condition(const std::vector<std::string>& tokens, const std::pair<std::string, std::size_t>& location);
Condition(const std::vector<std::string>& tokens, const DeckKeyword::Location& location);
Quantity lhs;

View File

@@ -33,7 +33,6 @@ namespace Opm {
DeckKeyword::DeckKeyword(const ParserKeyword& parserKeyword) :
m_keywordName(parserKeyword.getName()),
m_lineNumber(-1),
m_isDataKeyword(false),
m_slashTerminated(true),
parser_keyword(parserKeyword)
@@ -42,7 +41,6 @@ namespace Opm {
DeckKeyword::DeckKeyword(const ParserKeyword& parserKeyword, const std::string& keywordName) :
m_keywordName(keywordName),
m_lineNumber(-1),
m_isDataKeyword(false),
m_slashTerminated(true),
parser_keyword(parserKeyword)
@@ -190,20 +188,19 @@ namespace Opm {
}
void DeckKeyword::setLocation(const std::pair<const std::string&, std::size_t>& location) {
m_fileName = location.first;
m_lineNumber = location.second;
this->m_location = Location(location.first, location.second);
}
const std::string& DeckKeyword::getFileName() const {
return m_fileName;
return this->m_location.filename;
}
int DeckKeyword::getLineNumber() const {
return m_lineNumber;
return this->m_location.lineno;
}
std::pair<std::string, std::size_t> DeckKeyword::location() const {
return std::make_pair( this->getFileName(), this->getLineNumber() );
const DeckKeyword::Location& DeckKeyword::location() const {
return this->m_location;
}
void DeckKeyword::setDataKeyword(bool isDataKeyword_) {

View File

@@ -84,7 +84,7 @@ void Quantity::add_arg(const std::string& arg) {
this->args.push_back(strip_quotes(arg));
}
Condition::Condition(const std::vector<std::string>& tokens, const std::pair<std::string, std::size_t>& location) {
Condition::Condition(const std::vector<std::string>& tokens, const DeckKeyword::Location& location) {
this->lhs = Quantity(tokens[0]);
std::size_t token_index = 1;
@@ -105,7 +105,7 @@ Condition::Condition(const std::vector<std::string>& tokens, const std::pair<std
}
if (token_index >= tokens.size())
throw std::invalid_argument("Could not determine right hand side / comparator for ACTIONX keyword at " + location.first + ":" + std::to_string(location.second));
throw std::invalid_argument("Could not determine right hand side / comparator for ACTIONX keyword at " + location.filename + ":" + std::to_string(location.lineno));
this->rhs = Quantity(tokens[token_index]);
token_index++;

View File

@@ -34,6 +34,7 @@
#include <opm/parser/eclipse/EclipseState/Schedule/Action/Actions.hpp>
#include <opm/parser/eclipse/EclipseState/Schedule/Action/ActionX.hpp>
#include <opm/parser/eclipse/Deck/Deck.hpp>
#include <opm/parser/eclipse/Deck/DeckKeyword.hpp>
#include <opm/parser/eclipse/Parser/Parser.hpp>
#include <opm/parser/eclipse/Parser/ParseContext.hpp>
#include <opm/parser/eclipse/Parser/ErrorGuard.hpp>
@@ -596,7 +597,7 @@ BOOST_AUTO_TEST_CASE(TestFieldAND) {
BOOST_AUTO_TEST_CASE(Conditions) {
auto location = std::make_pair<std::string, std::size_t>("FILE", 0);
auto location = DeckKeyword::Location("File", 100);
// Missing comparator
BOOST_CHECK_THROW(Action::Condition cond({"WWCT", "OPX"}, location), std::invalid_argument);