diff --git a/opm/parser/eclipse/Parser/ParseContext.hpp b/opm/parser/eclipse/Parser/ParseContext.hpp index 5264a7829..e9042bd0e 100644 --- a/opm/parser/eclipse/Parser/ParseContext.hpp +++ b/opm/parser/eclipse/Parser/ParseContext.hpp @@ -99,6 +99,10 @@ namespace Opm { method. */ void addKey(const std::string& key); + /* + Add comment here. + */ + const static std::string IGNORE_EXTRA_RECORDS; /* The unknownKeyword field regulates how the parser should react when it encounters an unknwon keyword. Observe that diff --git a/src/opm/parser/eclipse/Parser/ParseContext.cpp b/src/opm/parser/eclipse/Parser/ParseContext.cpp index aefe7342e..495302dcb 100644 --- a/src/opm/parser/eclipse/Parser/ParseContext.cpp +++ b/src/opm/parser/eclipse/Parser/ParseContext.cpp @@ -68,6 +68,7 @@ namespace Opm { } void ParseContext::initDefault() { + addKey(IGNORE_EXTRA_RECORDS); addKey(PARSE_UNKNOWN_KEYWORD); addKey(PARSE_RANDOM_TEXT); addKey(PARSE_RANDOM_SLASH); @@ -243,6 +244,7 @@ namespace Opm { } } + const std::string ParseContext::IGNORE_EXTRA_RECORDS = "IGNORE_EXTRA_RECORDS"; const std::string ParseContext::PARSE_UNKNOWN_KEYWORD = "PARSE_UNKNOWN_KEYWORD"; const std::string ParseContext::PARSE_RANDOM_TEXT = "PARSE_RANDOM_TEXT"; const std::string ParseContext::PARSE_RANDOM_SLASH = "PARSE_RANDOM_SLASH"; diff --git a/src/opm/parser/eclipse/Parser/Parser.cpp b/src/opm/parser/eclipse/Parser/Parser.cpp index 783c9ae3d..57a1b8b0d 100644 --- a/src/opm/parser/eclipse/Parser/Parser.cpp +++ b/src/opm/parser/eclipse/Parser/Parser.cpp @@ -229,6 +229,8 @@ class ParserState { public: std::shared_ptr< RawKeyword > rawKeyword; + ParserKeywordSizeEnum lastSizeType; + string_view nextKeyword = emptystr; Deck deck; const ParseContext& parseContext; @@ -337,7 +339,9 @@ void ParserState::handleRandomText(const string_view& keywordString ) const { std::stringstream msg; std::string trimmedCopy = keywordString.string(); - if (trimmedCopy == "/") { + if (lastSizeType == OTHER_KEYWORD_IN_DECK) + errorKey = ParseContext::IGNORE_EXTRA_RECORDS; + else if (trimmedCopy == "/") { errorKey = ParseContext::PARSE_RANDOM_SLASH; msg << "Extra '/' detected at: " << this->current_path() @@ -484,10 +488,15 @@ bool tryParseKeyword( ParserState& parserState, const Parser& parser ) { if( parserState.rawKeyword == NULL ) { if( RawKeyword::isKeywordPrefix( line, keywordString ) ) { parserState.rawKeyword = createRawKeyword( keywordString, parserState, parser ); + if ( parser.isRecognizedKeyword(line) ) { + const auto* parserKeyword = parser.getParserKeywordFromDeckName( line ); + parserState.lastSizeType = parserKeyword->getSizeType(); + } } else { /* We are looking at some random gibberish?! */ - if (!parserState.unknown_keyword) + if (!parserState.unknown_keyword) { parserState.handleRandomText( line ); + } } } else { if (parserState.rawKeyword->getSizeType() == Raw::UNKNOWN) { diff --git a/tests/parser/ParseContextTests.cpp b/tests/parser/ParseContextTests.cpp index a6373ef15..4a6b1fc87 100644 --- a/tests/parser/ParseContextTests.cpp +++ b/tests/parser/ParseContextTests.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -85,6 +86,34 @@ BOOST_AUTO_TEST_CASE(TestUnkownKeyword) { } +BOOST_AUTO_TEST_CASE(Handle_extra_records) { + const char * deck_string = + "EQLDIMS\n" + " 2 100 20 1 1 /\n" + "\n" + "EQUIL\n" + " 2469 382.4 1705.0 0.0 500 0.0 1 1 20 /\n" + " 2469 382.4 1705.0 0.0 500 0.0 1 1 20 /\n" + " 2470 382.4 1705.0 0.0 500 0.0 1 1 20 /\n" + "GRID\n"; + + ParseContext parseContext; + Parser parser(false); + + parser.addKeyword(); + parser.addKeyword(); + parser.addKeyword(); + BOOST_CHECK_THROW( parser.parseString( deck_string , parseContext ) , std::invalid_argument ); + + parseContext.update(ParseContext::IGNORE_EXTRA_RECORDS , InputError::IGNORE ); + parser.parseString( deck_string , parseContext ); + BOOST_CHECK( parser.hasKeyword( "GRID" ) ); + + parseContext.update(ParseContext::IGNORE_EXTRA_RECORDS , InputError::THROW_EXCEPTION ); + BOOST_CHECK_THROW( parser.parseString( deck_string , parseContext ) , std::invalid_argument); +} + + BOOST_AUTO_TEST_CASE(TestUnkownKeyword_DATA) { const char * deck_string1 = "RUNSPEC\n"