Merge branch 'store-file-and-line' into keyword-status

This commit is contained in:
Joakim Hove 2013-10-08 15:28:21 +02:00
commit 997d53ea99
6 changed files with 60 additions and 35 deletions

View File

@ -85,12 +85,13 @@ namespace Opm {
void Parser::parseFile(DeckPtr deck, const boost::filesystem::path& file, const boost::filesystem::path& rootPath, bool parseStrict) const { void Parser::parseFile(DeckPtr deck, const boost::filesystem::path& file, const boost::filesystem::path& rootPath, bool parseStrict) const {
bool verbose = false; bool verbose = false;
std::ifstream inputstream; std::ifstream inputstream;
size_t lineNR = 0;
inputstream.open(file.string().c_str()); inputstream.open(file.string().c_str());
if (inputstream) { if (inputstream) {
RawKeywordPtr rawKeyword; RawKeywordPtr rawKeyword;
while (tryParseKeyword(deck, inputstream, rawKeyword, parseStrict)) { while (tryParseKeyword(deck, file.string() , lineNR , inputstream, rawKeyword, parseStrict)) {
if (rawKeyword->getKeywordName() == Opm::RawConsts::include) { if (rawKeyword->getKeywordName() == Opm::RawConsts::include) {
RawRecordConstPtr firstRecord = rawKeyword->getRecord(0); RawRecordConstPtr firstRecord = rawKeyword->getRecord(0);
std::string includeFileString = firstRecord->getItem(0); std::string includeFileString = firstRecord->getItem(0);
@ -139,11 +140,11 @@ namespace Opm {
throw std::invalid_argument("Input JSON object is not an array"); throw std::invalid_argument("Input JSON object is not an array");
} }
RawKeywordPtr Parser::createRawKeyword(const DeckConstPtr deck, const std::string& keywordString, bool strictParsing) const { RawKeywordPtr Parser::createRawKeyword(const DeckConstPtr deck, const std::string& filename , size_t lineNR , const std::string& keywordString, bool strictParsing) const {
if (hasKeyword(keywordString)) { if (hasKeyword(keywordString)) {
ParserKeywordConstPtr parserKeyword = m_parserKeywords.find(keywordString)->second; ParserKeywordConstPtr parserKeyword = m_parserKeywords.find(keywordString)->second;
if (parserKeyword->getSizeType() == SLASH_TERMINATED) if (parserKeyword->getSizeType() == SLASH_TERMINATED)
return RawKeywordPtr(new RawKeyword(keywordString)); return RawKeywordPtr(new RawKeyword(keywordString , filename , lineNR));
else { else {
size_t targetSize; size_t targetSize;
@ -159,26 +160,27 @@ namespace Opm {
} }
targetSize = sizeDefinitionItem->getInt(0); targetSize = sizeDefinitionItem->getInt(0);
} }
return RawKeywordPtr(new RawKeyword(keywordString, targetSize , parserKeyword->isTableCollection())); return RawKeywordPtr(new RawKeyword(keywordString, filename , lineNR , targetSize , parserKeyword->isTableCollection()));
} }
} else { } else {
if (strictParsing) { if (strictParsing) {
throw std::invalid_argument("Keyword " + keywordString + " not recognized "); throw std::invalid_argument("Keyword " + keywordString + " not recognized ");
} else { } else {
return RawKeywordPtr(new RawKeyword(keywordString, 0)); return RawKeywordPtr(new RawKeyword(keywordString, filename , lineNR , 0));
} }
} }
} }
bool Parser::tryParseKeyword(const DeckConstPtr deck, std::ifstream& inputstream, RawKeywordPtr& rawKeyword, bool strictParsing) const { bool Parser::tryParseKeyword(const DeckConstPtr deck, const std::string& filename , size_t& lineNR , std::ifstream& inputstream, RawKeywordPtr& rawKeyword, bool strictParsing) const {
std::string line; std::string line;
while (std::getline(inputstream, line)) { while (std::getline(inputstream, line)) {
std::string keywordString; std::string keywordString;
lineNR++;
if (rawKeyword == NULL) { if (rawKeyword == NULL) {
if (RawKeyword::tryParseKeyword(line, keywordString)) { if (RawKeyword::tryParseKeyword(line, keywordString)) {
rawKeyword = createRawKeyword(deck, keywordString, strictParsing); rawKeyword = createRawKeyword(deck, filename , lineNR , keywordString, strictParsing);
} }
} else { } else {
if (RawKeyword::useLine(line)) { if (RawKeyword::useLine(line)) {

View File

@ -56,9 +56,9 @@ namespace Opm {
size_t size() const; size_t size() const;
private: private:
std::map<std::string, ParserKeywordConstPtr> m_parserKeywords; std::map<std::string, ParserKeywordConstPtr> m_parserKeywords;
bool tryParseKeyword(const DeckConstPtr deck , std::ifstream& inputstream , RawKeywordPtr& rawKeyword, bool strictParsing) const; bool tryParseKeyword(const DeckConstPtr deck , const std::string& filename , size_t& lineNR , std::ifstream& inputstream , RawKeywordPtr& rawKeyword, bool strictParsing) const;
void parseFile(DeckPtr deck , const boost::filesystem::path& file, const boost::filesystem::path& rootPath, bool strictParsing) const; void parseFile(DeckPtr deck , const boost::filesystem::path& file, const boost::filesystem::path& rootPath, bool strictParsing) const;
RawKeywordPtr createRawKeyword(const DeckConstPtr deck , const std::string& keywordString, bool strictParsing) const; RawKeywordPtr createRawKeyword(const DeckConstPtr deck , const std::string& filename , size_t lineNR , const std::string& keywordString, bool strictParsing) const;
void addDefaultKeywords(); void addDefaultKeywords();
}; };

View File

@ -355,7 +355,7 @@ BOOST_AUTO_TEST_CASE(ConstructorIsTableCollection) {
BOOST_AUTO_TEST_CASE(ParseEmptyRecord) { BOOST_AUTO_TEST_CASE(ParseEmptyRecord) {
ParserKeywordPtr tabdimsKeyword( new ParserKeyword("TEST" , 1)); ParserKeywordPtr tabdimsKeyword( new ParserKeyword("TEST" , 1));
ParserIntItemConstPtr item(new ParserIntItem(std::string("ITEM") , ALL)); ParserIntItemConstPtr item(new ParserIntItem(std::string("ITEM") , ALL));
RawKeywordPtr rawkeyword(new RawKeyword( tabdimsKeyword->getName() , 1)); RawKeywordPtr rawkeyword(new RawKeyword( tabdimsKeyword->getName() , "FILE" , 10U , 1));

View File

@ -26,13 +26,13 @@
namespace Opm { namespace Opm {
RawKeyword::RawKeyword(const std::string& name) { RawKeyword::RawKeyword(const std::string& name, const std::string& filename, size_t lineNR) {
commonInit(name); commonInit(name,filename,lineNR);
} }
RawKeyword::RawKeyword(const std::string& name , size_t inputSize, bool isTableCollection ) { RawKeyword::RawKeyword(const std::string& name , const std::string& filename, size_t lineNR , size_t inputSize, bool isTableCollection ) {
commonInit(name); commonInit(name,filename,lineNR);
if (isTableCollection) { if (isTableCollection) {
m_isTableCollection = true; m_isTableCollection = true;
m_numTables = inputSize; m_numTables = inputSize;
@ -47,8 +47,10 @@ namespace Opm {
} }
void RawKeyword::commonInit(const std::string& name) { void RawKeyword::commonInit(const std::string& name , const std::string& filename, size_t lineNR) {
setKeywordName( name ); setKeywordName( name );
m_filename = filename;
m_lineNR = lineNR;
m_isFinished = false; m_isFinished = false;
m_fixedSizeKeyword = false; m_fixedSizeKeyword = false;
m_isTableCollection = false; m_isTableCollection = false;
@ -178,5 +180,13 @@ namespace Opm {
return m_isFinished; return m_isFinished;
} }
const std::string& RawKeyword::getFilename() const {
return m_filename;
}
size_t RawKeyword::getLineNR() const {
return m_lineNR;
}
} }

View File

@ -36,8 +36,8 @@ namespace Opm {
class RawKeyword { class RawKeyword {
public: public:
RawKeyword(const std::string& name); RawKeyword(const std::string& name , const std::string& filename, size_t lineNR);
RawKeyword(const std::string& name , size_t inputSize , bool isTableCollection = false); RawKeyword(const std::string& name , const std::string& filename, size_t lineNR , size_t inputSize , bool isTableCollection = false);
const std::string& getKeywordName() const; const std::string& getKeywordName() const;
void addRawRecordString(const std::string& partialRecordString); void addRawRecordString(const std::string& partialRecordString);
@ -52,6 +52,9 @@ namespace Opm {
bool isFinished() const; bool isFinished() const;
bool isTableCollection() const; bool isTableCollection() const;
const std::string& getFilename() const;
size_t getLineNR() const;
private: private:
bool m_isTableCollection; bool m_isTableCollection;
@ -64,7 +67,10 @@ namespace Opm {
std::vector<RawRecordPtr> m_records; std::vector<RawRecordPtr> m_records;
std::string m_partialRecordString; std::string m_partialRecordString;
void commonInit(const std::string& name); size_t m_lineNR;
std::string m_filename;
void commonInit(const std::string& name,const std::string& filename, size_t lineNR);
void setKeywordName(const std::string& keyword); void setKeywordName(const std::string& keyword);
static bool isValidKeyword(const std::string& keywordCandidate); static bool isValidKeyword(const std::string& keywordCandidate);
}; };

View File

@ -26,45 +26,45 @@
using namespace Opm; using namespace Opm;
BOOST_AUTO_TEST_CASE(RawKeywordGiveKeywordToConstructorKeywordSet) { BOOST_AUTO_TEST_CASE(RawKeywordGiveKeywordToConstructorKeywordSet) {
RawKeyword keyword("KEYYWORD"); RawKeyword keyword("KEYYWORD", "FILE" , 10U);
BOOST_CHECK(keyword.getKeywordName() == "KEYYWORD"); BOOST_CHECK(keyword.getKeywordName() == "KEYYWORD");
} }
BOOST_AUTO_TEST_CASE(RawKeywordGiveKeywordToConstructorTooLongThrows) { BOOST_AUTO_TEST_CASE(RawKeywordGiveKeywordToConstructorTooLongThrows) {
BOOST_CHECK_THROW(RawKeyword keyword("KEYYYWORD"), std::invalid_argument); BOOST_CHECK_THROW(RawKeyword keyword("KEYYYWORD", "FILE" , 10U), std::invalid_argument);
} }
BOOST_AUTO_TEST_CASE(RawKeywordSetKeywordInitialWhitespaceInKeywordThrows) { BOOST_AUTO_TEST_CASE(RawKeywordSetKeywordInitialWhitespaceInKeywordThrows) {
BOOST_CHECK_THROW(RawKeyword(" TELONG"), std::invalid_argument); BOOST_CHECK_THROW(RawKeyword(" TELONG", "FILE" , 10U), std::invalid_argument);
} }
BOOST_AUTO_TEST_CASE(constructor_mixedCaseName_throws) { BOOST_AUTO_TEST_CASE(constructor_mixedCaseName_throws) {
BOOST_CHECK_THROW(RawKeyword("Test"), std::invalid_argument); BOOST_CHECK_THROW(RawKeyword("Test", "FILE" , 10U), std::invalid_argument);
} }
BOOST_AUTO_TEST_CASE(RawKeywordSetKeywordInitialTabInKeywordThrows) { BOOST_AUTO_TEST_CASE(RawKeywordSetKeywordInitialTabInKeywordThrows) {
BOOST_CHECK_THROW( RawKeyword("\tTELONG"), std::invalid_argument); BOOST_CHECK_THROW( RawKeyword("\tTELONG", "FILE" , 10U), std::invalid_argument);
} }
BOOST_AUTO_TEST_CASE(RawKeywordSetCorrectLenghtKeywordNoError) { BOOST_AUTO_TEST_CASE(RawKeywordSetCorrectLenghtKeywordNoError) {
RawKeyword keyword("GOODONE"); RawKeyword keyword("GOODONE", "FILE" , 10U);
BOOST_CHECK(keyword.getKeywordName() == "GOODONE"); BOOST_CHECK(keyword.getKeywordName() == "GOODONE");
} }
BOOST_AUTO_TEST_CASE(RawKeywordSet8CharKeywordWithTrailingWhitespaceKeywordTrimmed) { BOOST_AUTO_TEST_CASE(RawKeywordSet8CharKeywordWithTrailingWhitespaceKeywordTrimmed) {
RawKeyword keyword("GOODONEE "); RawKeyword keyword("GOODONEE ", "FILE" , 10U);
BOOST_CHECK(keyword.getKeywordName() == "GOODONEE"); BOOST_CHECK(keyword.getKeywordName() == "GOODONEE");
} }
BOOST_AUTO_TEST_CASE(addRecord_singleRecord_recordAdded) { BOOST_AUTO_TEST_CASE(addRecord_singleRecord_recordAdded) {
RawKeyword keyword("TEST"); RawKeyword keyword("TEST", "FILE" , 10U);
keyword.addRawRecordString("test 1 3 4 /"); keyword.addRawRecordString("test 1 3 4 /");
BOOST_CHECK_EQUAL(1U, keyword.size()); BOOST_CHECK_EQUAL(1U, keyword.size());
} }
BOOST_AUTO_TEST_CASE(getRecord_outOfRange_throws) { BOOST_AUTO_TEST_CASE(getRecord_outOfRange_throws) {
RawKeyword keyword("TEST"); RawKeyword keyword("TEST", "FILE" , 10U);
keyword.addRawRecordString("test 1 3 4 /"); keyword.addRawRecordString("test 1 3 4 /");
BOOST_CHECK_THROW(keyword.getRecord(1), std::range_error); BOOST_CHECK_THROW(keyword.getRecord(1), std::range_error);
} }
@ -72,7 +72,7 @@ BOOST_AUTO_TEST_CASE(getRecord_outOfRange_throws) {
BOOST_AUTO_TEST_CASE(isFinished_undef_size) { BOOST_AUTO_TEST_CASE(isFinished_undef_size) {
RawKeyword keyword("TEST"); RawKeyword keyword("TEST", "FILE" , 10U);
BOOST_CHECK( !keyword.isFinished() ); BOOST_CHECK( !keyword.isFinished() );
keyword.addRawRecordString("test 1 3 4 /"); keyword.addRawRecordString("test 1 3 4 /");
@ -87,14 +87,14 @@ BOOST_AUTO_TEST_CASE(isFinished_undef_size) {
BOOST_AUTO_TEST_CASE(isFinished_Fixedsize0) { BOOST_AUTO_TEST_CASE(isFinished_Fixedsize0) {
RawKeyword keyword("TEST" ,0U); RawKeyword keyword("TEST" , "FILE" , 10U , 0U);
BOOST_CHECK( keyword.isFinished() ); BOOST_CHECK( keyword.isFinished() );
} }
BOOST_AUTO_TEST_CASE(isFinished_Fixedsize1) { BOOST_AUTO_TEST_CASE(isFinished_Fixedsize1) {
RawKeyword keyword("TEST" , 1U); RawKeyword keyword("TEST" , "FILE" , 10U, 1U);
BOOST_CHECK( !keyword.isFinished() ); BOOST_CHECK( !keyword.isFinished() );
keyword.addRawRecordString("test 1 3 4 /"); keyword.addRawRecordString("test 1 3 4 /");
BOOST_CHECK( keyword.isFinished() ); BOOST_CHECK( keyword.isFinished() );
@ -102,7 +102,7 @@ BOOST_AUTO_TEST_CASE(isFinished_Fixedsize1) {
BOOST_AUTO_TEST_CASE(isFinished_FixedsizeMulti) { BOOST_AUTO_TEST_CASE(isFinished_FixedsizeMulti) {
RawKeyword keyword("TEST" , 4U); RawKeyword keyword("TEST", "FILE" , 10U , 4U);
BOOST_CHECK( !keyword.isFinished() ); BOOST_CHECK( !keyword.isFinished() );
keyword.addRawRecordString("test 1 3 4 /"); keyword.addRawRecordString("test 1 3 4 /");
BOOST_CHECK( !keyword.isFinished() ); BOOST_CHECK( !keyword.isFinished() );
@ -146,16 +146,23 @@ BOOST_AUTO_TEST_CASE(useLine) {
BOOST_AUTO_TEST_CASE(isTableCollection) { BOOST_AUTO_TEST_CASE(isTableCollection) {
RawKeyword keyword1("TEST" , 4U , false); RawKeyword keyword1("TEST" , "FILE" , 10U, 4U , false);
RawKeyword keyword2("TEST2"); RawKeyword keyword2("TEST2", "FILE" , 10U);
BOOST_CHECK_EQUAL( false , keyword1.isTableCollection()); BOOST_CHECK_EQUAL( false , keyword1.isTableCollection());
BOOST_CHECK_EQUAL( false , keyword2.isTableCollection()); BOOST_CHECK_EQUAL( false , keyword2.isTableCollection());
} }
BOOST_AUTO_TEST_CASE(CreateTableCollection) { BOOST_AUTO_TEST_CASE(CreateTableCollection) {
RawKeyword keyword1("TEST" , 2, true); RawKeyword keyword1("TEST" , "FILE" , 10U, 2, true);
BOOST_CHECK_EQUAL( true , keyword1.isTableCollection()); BOOST_CHECK_EQUAL( true , keyword1.isTableCollection());
} }
BOOST_AUTO_TEST_CASE(CreateWithFileAndLine) {
RawKeyword keyword1("TEST" , "XXX", 100);
BOOST_CHECK_EQUAL( "XXX" , keyword1.getFilename());
BOOST_CHECK_EQUAL( 100U , keyword1.getLineNR() );
}