From f0770f47fb5da82d53b8c6ca4af33849d699b782 Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Tue, 8 Oct 2013 15:23:45 +0200 Subject: [PATCH] Added filename and line nr to RawRecord --- opm/parser/eclipse/Parser/Parser.cpp | 18 +++++---- opm/parser/eclipse/Parser/Parser.hpp | 4 +- .../Parser/tests/ParserKeywordTests.cpp | 2 +- opm/parser/eclipse/RawDeck/RawKeyword.cpp | 20 +++++++--- opm/parser/eclipse/RawDeck/RawKeyword.hpp | 12 ++++-- .../eclipse/RawDeck/tests/RawKeywordTests.cpp | 39 +++++++++++-------- 6 files changed, 60 insertions(+), 35 deletions(-) diff --git a/opm/parser/eclipse/Parser/Parser.cpp b/opm/parser/eclipse/Parser/Parser.cpp index f4b6e0eac..515cd4c85 100644 --- a/opm/parser/eclipse/Parser/Parser.cpp +++ b/opm/parser/eclipse/Parser/Parser.cpp @@ -85,12 +85,13 @@ namespace Opm { void Parser::parseFile(DeckPtr deck, const boost::filesystem::path& file, const boost::filesystem::path& rootPath, bool parseStrict) const { bool verbose = false; std::ifstream inputstream; + size_t lineNR = 0; inputstream.open(file.string().c_str()); if (inputstream) { RawKeywordPtr rawKeyword; - while (tryParseKeyword(deck, inputstream, rawKeyword, parseStrict)) { + while (tryParseKeyword(deck, file.string() , lineNR , inputstream, rawKeyword, parseStrict)) { if (rawKeyword->getKeywordName() == Opm::RawConsts::include) { RawRecordConstPtr firstRecord = rawKeyword->getRecord(0); std::string includeFileString = firstRecord->getItem(0); @@ -137,11 +138,11 @@ namespace Opm { 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)) { ParserKeywordConstPtr parserKeyword = m_parserKeywords.find(keywordString)->second; if (parserKeyword->getSizeType() == SLASH_TERMINATED) - return RawKeywordPtr(new RawKeyword(keywordString)); + return RawKeywordPtr(new RawKeyword(keywordString , filename , lineNR)); else { size_t targetSize; @@ -157,26 +158,27 @@ namespace Opm { } targetSize = sizeDefinitionItem->getInt(0); } - return RawKeywordPtr(new RawKeyword(keywordString, targetSize , parserKeyword->isTableCollection())); + return RawKeywordPtr(new RawKeyword(keywordString, filename , lineNR , targetSize , parserKeyword->isTableCollection())); } } else { if (strictParsing) { throw std::invalid_argument("Keyword " + keywordString + " not recognized "); } 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; - + while (std::getline(inputstream, line)) { std::string keywordString; + lineNR++; if (rawKeyword == NULL) { if (RawKeyword::tryParseKeyword(line, keywordString)) { - rawKeyword = createRawKeyword(deck, keywordString, strictParsing); + rawKeyword = createRawKeyword(deck, filename , lineNR , keywordString, strictParsing); } } else { if (RawKeyword::useLine(line)) { diff --git a/opm/parser/eclipse/Parser/Parser.hpp b/opm/parser/eclipse/Parser/Parser.hpp index 23f2ed43a..cbe1d676b 100644 --- a/opm/parser/eclipse/Parser/Parser.hpp +++ b/opm/parser/eclipse/Parser/Parser.hpp @@ -56,9 +56,9 @@ namespace Opm { size_t size() const; private: std::map 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; - 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(); }; diff --git a/opm/parser/eclipse/Parser/tests/ParserKeywordTests.cpp b/opm/parser/eclipse/Parser/tests/ParserKeywordTests.cpp index 065736372..966712098 100644 --- a/opm/parser/eclipse/Parser/tests/ParserKeywordTests.cpp +++ b/opm/parser/eclipse/Parser/tests/ParserKeywordTests.cpp @@ -339,7 +339,7 @@ BOOST_AUTO_TEST_CASE(ConstructorIsTableCollection) { BOOST_AUTO_TEST_CASE(ParseEmptyRecord) { ParserKeywordPtr tabdimsKeyword( new ParserKeyword("TEST" , 1)); ParserIntItemConstPtr item(new ParserIntItem(std::string("ITEM") , ALL)); - RawKeywordPtr rawkeyword(new RawKeyword( tabdimsKeyword->getName() , 1)); + RawKeywordPtr rawkeyword(new RawKeyword( tabdimsKeyword->getName() , "FILE" , 10U , 1)); diff --git a/opm/parser/eclipse/RawDeck/RawKeyword.cpp b/opm/parser/eclipse/RawDeck/RawKeyword.cpp index 77a0d3dc8..23ab74af0 100644 --- a/opm/parser/eclipse/RawDeck/RawKeyword.cpp +++ b/opm/parser/eclipse/RawDeck/RawKeyword.cpp @@ -26,13 +26,13 @@ namespace Opm { - RawKeyword::RawKeyword(const std::string& name) { - commonInit(name); + RawKeyword::RawKeyword(const std::string& name, const std::string& filename, size_t lineNR) { + commonInit(name,filename,lineNR); } - RawKeyword::RawKeyword(const std::string& name , size_t inputSize, bool isTableCollection ) { - commonInit(name); + RawKeyword::RawKeyword(const std::string& name , const std::string& filename, size_t lineNR , size_t inputSize, bool isTableCollection ) { + commonInit(name,filename,lineNR); if (isTableCollection) { m_isTableCollection = true; 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 ); + m_filename = filename; + m_lineNR = lineNR; m_isFinished = false; m_fixedSizeKeyword = false; m_isTableCollection = false; @@ -178,5 +180,13 @@ namespace Opm { return m_isFinished; } + const std::string& RawKeyword::getFilename() const { + return m_filename; + } + + size_t RawKeyword::getLineNR() const { + return m_lineNR; + } + } diff --git a/opm/parser/eclipse/RawDeck/RawKeyword.hpp b/opm/parser/eclipse/RawDeck/RawKeyword.hpp index 1898bf71c..c098f9b5f 100644 --- a/opm/parser/eclipse/RawDeck/RawKeyword.hpp +++ b/opm/parser/eclipse/RawDeck/RawKeyword.hpp @@ -36,8 +36,8 @@ namespace Opm { class RawKeyword { public: - RawKeyword(const std::string& name); - RawKeyword(const std::string& name , size_t inputSize , bool isTableCollection = false); + RawKeyword(const std::string& name , const std::string& filename, size_t lineNR); + RawKeyword(const std::string& name , const std::string& filename, size_t lineNR , size_t inputSize , bool isTableCollection = false); const std::string& getKeywordName() const; void addRawRecordString(const std::string& partialRecordString); @@ -52,6 +52,9 @@ namespace Opm { bool isFinished() const; bool isTableCollection() const; + const std::string& getFilename() const; + size_t getLineNR() const; + private: bool m_isTableCollection; @@ -64,7 +67,10 @@ namespace Opm { std::vector m_records; 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); static bool isValidKeyword(const std::string& keywordCandidate); }; diff --git a/opm/parser/eclipse/RawDeck/tests/RawKeywordTests.cpp b/opm/parser/eclipse/RawDeck/tests/RawKeywordTests.cpp index 643e354f3..e2e4ded7f 100644 --- a/opm/parser/eclipse/RawDeck/tests/RawKeywordTests.cpp +++ b/opm/parser/eclipse/RawDeck/tests/RawKeywordTests.cpp @@ -26,45 +26,45 @@ using namespace Opm; BOOST_AUTO_TEST_CASE(RawKeywordGiveKeywordToConstructorKeywordSet) { - RawKeyword keyword("KEYYWORD"); + RawKeyword keyword("KEYYWORD", "FILE" , 10U); BOOST_CHECK(keyword.getKeywordName() == "KEYYWORD"); } 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_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_CHECK_THROW(RawKeyword("Test"), std::invalid_argument); + BOOST_CHECK_THROW(RawKeyword("Test", "FILE" , 10U), std::invalid_argument); } 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) { - RawKeyword keyword("GOODONE"); + RawKeyword keyword("GOODONE", "FILE" , 10U); BOOST_CHECK(keyword.getKeywordName() == "GOODONE"); } BOOST_AUTO_TEST_CASE(RawKeywordSet8CharKeywordWithTrailingWhitespaceKeywordTrimmed) { - RawKeyword keyword("GOODONEE "); + RawKeyword keyword("GOODONEE ", "FILE" , 10U); BOOST_CHECK(keyword.getKeywordName() == "GOODONEE"); } BOOST_AUTO_TEST_CASE(addRecord_singleRecord_recordAdded) { - RawKeyword keyword("TEST"); + RawKeyword keyword("TEST", "FILE" , 10U); keyword.addRawRecordString("test 1 3 4 /"); BOOST_CHECK_EQUAL(1U, keyword.size()); } BOOST_AUTO_TEST_CASE(getRecord_outOfRange_throws) { - RawKeyword keyword("TEST"); + RawKeyword keyword("TEST", "FILE" , 10U); keyword.addRawRecordString("test 1 3 4 /"); 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) { - RawKeyword keyword("TEST"); + RawKeyword keyword("TEST", "FILE" , 10U); BOOST_CHECK( !keyword.isFinished() ); keyword.addRawRecordString("test 1 3 4 /"); @@ -87,14 +87,14 @@ BOOST_AUTO_TEST_CASE(isFinished_undef_size) { BOOST_AUTO_TEST_CASE(isFinished_Fixedsize0) { - RawKeyword keyword("TEST" ,0U); + RawKeyword keyword("TEST" , "FILE" , 10U , 0U); BOOST_CHECK( keyword.isFinished() ); } BOOST_AUTO_TEST_CASE(isFinished_Fixedsize1) { - RawKeyword keyword("TEST" , 1U); + RawKeyword keyword("TEST" , "FILE" , 10U, 1U); BOOST_CHECK( !keyword.isFinished() ); keyword.addRawRecordString("test 1 3 4 /"); BOOST_CHECK( keyword.isFinished() ); @@ -102,7 +102,7 @@ BOOST_AUTO_TEST_CASE(isFinished_Fixedsize1) { BOOST_AUTO_TEST_CASE(isFinished_FixedsizeMulti) { - RawKeyword keyword("TEST" , 4U); + RawKeyword keyword("TEST", "FILE" , 10U , 4U); BOOST_CHECK( !keyword.isFinished() ); keyword.addRawRecordString("test 1 3 4 /"); BOOST_CHECK( !keyword.isFinished() ); @@ -146,16 +146,23 @@ BOOST_AUTO_TEST_CASE(useLine) { BOOST_AUTO_TEST_CASE(isTableCollection) { - RawKeyword keyword1("TEST" , 4U , false); - RawKeyword keyword2("TEST2"); + RawKeyword keyword1("TEST" , "FILE" , 10U, 4U , false); + RawKeyword keyword2("TEST2", "FILE" , 10U); BOOST_CHECK_EQUAL( false , keyword1.isTableCollection()); BOOST_CHECK_EQUAL( false , keyword2.isTableCollection()); } BOOST_AUTO_TEST_CASE(CreateTableCollection) { - RawKeyword keyword1("TEST" , 2, true); + RawKeyword keyword1("TEST" , "FILE" , 10U, 2, true); 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() ); +} + +