Add parse option keep '/' in data sections
The records are norally terminated on the first unquoted '/', but for the UDQ and ACTIONX keywords the data sections of a keyword contain mathematical expressions which can contain '/' literals. This commit adds a per-keyword ability to terminate the records on the last '/' instead of the first '/'.
This commit is contained in:
parent
03a2bbdf80
commit
af7693ef6b
@ -128,6 +128,7 @@ namespace Opm {
|
||||
enum ParserKeywordSizeEnum getSizeType() const;
|
||||
const KeywordSize& getKeywordSize() const;
|
||||
bool isDataKeyword() const;
|
||||
bool slashTerminatedRecords() const;
|
||||
|
||||
std::string createDeclaration(const std::string& indent) const;
|
||||
std::string createDecl() const;
|
||||
@ -149,6 +150,7 @@ namespace Opm {
|
||||
size_t m_fixedSize;
|
||||
bool m_isTableCollection;
|
||||
std::string m_Description;
|
||||
bool slash_terminated_records = true;
|
||||
|
||||
static bool validNameStart(const string_view& name);
|
||||
void initDeckNames( const Json::JsonObject& jsonConfig );
|
||||
|
@ -54,10 +54,12 @@ namespace Opm {
|
||||
|
||||
bool operator==( const ParserRecord& ) const;
|
||||
bool operator!=( const ParserRecord& ) const;
|
||||
bool slashTerminatedRecords() const;
|
||||
|
||||
private:
|
||||
bool m_dataRecord;
|
||||
std::vector< ParserItem > m_items;
|
||||
bool slash_terminated_records = true;
|
||||
};
|
||||
|
||||
std::ostream& operator<<( std::ostream&, const ParserRecord& );
|
||||
|
@ -72,7 +72,7 @@ namespace Opm {
|
||||
iterator end();
|
||||
|
||||
bool is_title() const;
|
||||
|
||||
bool slash_terminated_records = true;
|
||||
private:
|
||||
Raw::KeywordSizeEnum m_sizeType;
|
||||
bool m_isFinished = false;
|
||||
|
@ -140,6 +140,30 @@ inline string_view strip_slash( string_view view ) {
|
||||
return { begin, slash };
|
||||
}
|
||||
|
||||
inline string_view strip_last_slash( string_view view ) {
|
||||
auto begin = view.begin();
|
||||
auto end = view.end();
|
||||
auto slash = end;
|
||||
|
||||
while (true) {
|
||||
if (slash == begin)
|
||||
break;
|
||||
|
||||
if (*slash == '/')
|
||||
break;
|
||||
|
||||
slash--;
|
||||
}
|
||||
if (slash == begin)
|
||||
slash = end;
|
||||
|
||||
/* we want to preserve terminating slashes */
|
||||
if( slash != end ) ++slash;
|
||||
|
||||
return { begin, slash };
|
||||
}
|
||||
|
||||
|
||||
inline bool getline( string_view& input, string_view& line ) {
|
||||
if( input.empty() ) return false;
|
||||
|
||||
@ -168,7 +192,7 @@ inline std::string clean( const std::string& str ) {
|
||||
string_view input( str ), line;
|
||||
auto dsti = dst.begin();
|
||||
while( getline( input, line ) ) {
|
||||
line = trim( strip_slash( strip_comments( line ) ) );
|
||||
line = trim( strip_comments(line));
|
||||
|
||||
std::copy( line.begin(), line.end(), dsti );
|
||||
dsti += std::distance( line.begin(), line.end() );
|
||||
@ -419,6 +443,7 @@ void ParserState::addPathAlias( const std::string& alias, const std::string& pat
|
||||
|
||||
|
||||
std::shared_ptr<RawKeyword> createRawKeyword(const ParserKeyword* parserKeyword, const std::string& keywordString, ParserState& parserState, const Parser& parser) {
|
||||
bool slash_terminated_records = parserKeyword->slashTerminatedRecords();
|
||||
|
||||
if( parserKeyword->getSizeType() == SLASH_TERMINATED || parserKeyword->getSizeType() == UNKNOWN) {
|
||||
|
||||
@ -426,17 +451,21 @@ std::shared_ptr<RawKeyword> createRawKeyword(const ParserKeyword* parserKeyword,
|
||||
? Raw::SLASH_TERMINATED
|
||||
: Raw::UNKNOWN;
|
||||
|
||||
return std::make_shared< RawKeyword >( keywordString, rawSizeType,
|
||||
parserState.current_path().string(),
|
||||
parserState.line() );
|
||||
auto raw_keyword = std::make_shared< RawKeyword >( keywordString, rawSizeType,
|
||||
parserState.current_path().string(),
|
||||
parserState.line() );
|
||||
raw_keyword->slash_terminated_records = slash_terminated_records;
|
||||
return raw_keyword;
|
||||
}
|
||||
|
||||
if( parserKeyword->hasFixedSize() ) {
|
||||
return std::make_shared< RawKeyword >( keywordString,
|
||||
parserState.current_path().string(),
|
||||
parserState.line(),
|
||||
parserKeyword->getFixedSize(),
|
||||
parserKeyword->isTableCollection() );
|
||||
auto raw_keyword = std::make_shared< RawKeyword >( keywordString,
|
||||
parserState.current_path().string(),
|
||||
parserState.line(),
|
||||
parserKeyword->getFixedSize(),
|
||||
parserKeyword->isTableCollection() );
|
||||
raw_keyword->slash_terminated_records = slash_terminated_records;
|
||||
return raw_keyword;
|
||||
}
|
||||
|
||||
const auto& keyword_size = parserKeyword->getKeywordSize();
|
||||
@ -446,11 +475,13 @@ std::shared_ptr<RawKeyword> createRawKeyword(const ParserKeyword* parserKeyword,
|
||||
const auto& sizeDefinitionKeyword = deck.getKeyword(keyword_size.keyword);
|
||||
const auto& record = sizeDefinitionKeyword.getRecord(0);
|
||||
const auto targetSize = record.getItem( keyword_size.item ).get< int >( 0 ) + keyword_size.shift;
|
||||
return std::make_shared< RawKeyword >( keywordString,
|
||||
parserState.current_path().string(),
|
||||
parserState.line(),
|
||||
targetSize,
|
||||
parserKeyword->isTableCollection() );
|
||||
auto raw_keyword = std::make_shared< RawKeyword >( keywordString,
|
||||
parserState.current_path().string(),
|
||||
parserState.line(),
|
||||
targetSize,
|
||||
parserKeyword->isTableCollection() );
|
||||
raw_keyword->slash_terminated_records = slash_terminated_records;
|
||||
return raw_keyword;
|
||||
}
|
||||
|
||||
std::string msg = "Expected the kewyord: " +keyword_size.keyword
|
||||
@ -462,11 +493,13 @@ std::shared_ptr<RawKeyword> createRawKeyword(const ParserKeyword* parserKeyword,
|
||||
const auto& int_item = record.get( keyword_size.item);
|
||||
|
||||
const auto targetSize = int_item.getDefault< int >( ) + keyword_size.shift;
|
||||
return std::make_shared< RawKeyword >( keywordString,
|
||||
parserState.current_path().string(),
|
||||
parserState.line(),
|
||||
targetSize,
|
||||
parserKeyword->isTableCollection() );
|
||||
auto raw_keyword = std::make_shared< RawKeyword >( keywordString,
|
||||
parserState.current_path().string(),
|
||||
parserState.line(),
|
||||
targetSize,
|
||||
parserKeyword->isTableCollection() );
|
||||
raw_keyword->slash_terminated_records = slash_terminated_records;
|
||||
return raw_keyword;
|
||||
}
|
||||
|
||||
|
||||
@ -561,6 +594,12 @@ bool tryParseKeyword( ParserState& parserState, const Parser& parser ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (parserState.rawKeyword->slash_terminated_records)
|
||||
line = strip_slash(line);
|
||||
else
|
||||
line = strip_last_slash(line);
|
||||
|
||||
parserState.rawKeyword->addRawRecordString(line);
|
||||
}
|
||||
|
||||
|
@ -423,6 +423,8 @@ void set_dimensions( ParserItem& item,
|
||||
|
||||
void ParserKeyword::addRecord( ParserRecord record ) {
|
||||
m_records.push_back( std::move( record ) );
|
||||
if (!record.slashTerminatedRecords())
|
||||
this->slash_terminated_records = false;
|
||||
}
|
||||
|
||||
|
||||
@ -517,6 +519,9 @@ void set_dimensions( ParserItem& item,
|
||||
return m_keywordSizeType;
|
||||
}
|
||||
|
||||
bool ParserKeyword::slashTerminatedRecords() const {
|
||||
return this->slash_terminated_records;
|
||||
}
|
||||
|
||||
const KeywordSize& ParserKeyword::getKeywordSize() const {
|
||||
return keyword_size;
|
||||
|
@ -46,6 +46,10 @@ namespace {
|
||||
return m_items.size();
|
||||
}
|
||||
|
||||
bool ParserRecord::slashTerminatedRecords() const {
|
||||
return this->slash_terminated_records;
|
||||
}
|
||||
|
||||
void ParserRecord::addItem( ParserItem item ) {
|
||||
if (m_dataRecord)
|
||||
throw std::invalid_argument("Record is already marked as DataRecord - can not add items");
|
||||
@ -57,6 +61,9 @@ namespace {
|
||||
if( itr != this->m_items.end() )
|
||||
throw std::invalid_argument("Itemname: " + item.name() + " already exists.");
|
||||
|
||||
if (item.parseRaw())
|
||||
this->slash_terminated_records = false;
|
||||
|
||||
this->m_items.push_back( std::move( item ) );
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user