ParserKeyword.hpp
Go to the documentation of this file.
1/*
2 Copyright 2013 Statoil ASA.
3
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 */
19#ifndef PARSER_KEYWORD_H
20#define PARSER_KEYWORD_H
21
22#include <iosfwd>
23#include <string>
24#include <set>
25
26#include <regex>
27
30
31namespace Json {
32 class JsonObject;
33}
34
35namespace Opm {
36 class Deck;
37 class DeckKeyword;
38 class ParseContext;
39 class ErrorGuard;
40 class ParserDoubleItem;
41 class RawKeyword;
42 class string_view;
43 class ErrorGuard;
44
45 /*
46 Small helper struct to assemble the information needed to infer the size
47 of a keyword based on another keyword in the deck.
48 */
49 struct KeywordSize {
50 KeywordSize(const std::string& in_keyword, const std::string& in_item, int in_shift) :
51 keyword(in_keyword),
52 item(in_item),
53 shift(in_shift)
54 {}
55
57
58 bool operator==(const KeywordSize& other) const {
59 return ((this->keyword == other.keyword) &&
60 (this->item == other.item) &&
61 (this->shift == other.shift));
62 }
63
64 bool operator!=(const KeywordSize& other) const {
65 return !(*this == other);
66 }
67
70 int shift;
71 };
72
74 public:
76 const std::string& sizeKeyword ,
77 const std::string& sizeItem,
78 int size_shift,
79 bool _isTableCollection = false);
80 explicit ParserKeyword(const std::string& name);
81 explicit ParserKeyword(const Json::JsonObject& jsonConfig);
82
83 void setFixedSize( size_t keywordSize);
85 void setTableCollection(bool _isTableCollection);
86 void initSizeKeyword( const std::string& sizeKeyword, const std::string& sizeItem, int size_shift);
87
88
89 typedef std::set<std::string> DeckNameSet;
90 typedef std::set<std::string> SectionNameSet;
91
92
93 static bool validInternalName(const std::string& name);
94 static bool validDeckName(const string_view& name);
95 bool hasMatchRegex() const;
96 void setMatchRegex(const std::string& deckNameRegexp);
97 bool matches(const string_view& ) const;
98 bool hasDimension() const;
101 const ParserRecord& getRecord(size_t recordIndex) const;
102 ParserRecord& getRecord(size_t recordIndex);
103 std::vector< ParserRecord >::const_iterator begin() const;
104 std::vector< ParserRecord >::const_iterator end() const;
105 const std::string className() const;
106 const std::string& getName() const;
107 size_t getFixedSize() const;
108 bool hasFixedSize() const;
109 bool isTableCollection() const;
111 void setDescription(const std::string &description);
112
115 void addDeckName( const std::string& deckName );
117 const std::string& codeEnd() const;
118 DeckNameSet::const_iterator deckNamesBegin() const;
119 DeckNameSet::const_iterator deckNamesEnd() const;
120
122 void addValidSectionName(const std::string& sectionName);
123 bool isValidSection(const std::string& sectionName) const;
124 SectionNameSet::const_iterator validSectionNamesBegin() const;
125 SectionNameSet::const_iterator validSectionNamesEnd() const;
126
127 DeckKeyword parse(const ParseContext& parseContext, ErrorGuard& errors, RawKeyword& rawKeyword, UnitSystem& active_unitsystem, UnitSystem& default_unitsystem, const std::string& filename) const;
130 bool isDataKeyword() const;
131 bool rawStringKeyword() const;
132 bool isCodeKeyword() const;
135 void setAlternatingKeyword(bool alternating);
136 void setDoubleRecordsKeyword(bool double_rec);
137
141
142 bool operator==( const ParserKeyword& ) const;
143 bool operator!=( const ParserKeyword& ) const;
144
145 private:
146 KeywordSize keyword_size;
147 std::string m_name;
148 DeckNameSet m_deckNames;
149 DeckNameSet m_validSectionNames;
150 std::string m_matchRegexString;
151 std::regex m_matchRegex;
152 std::vector< ParserRecord > m_records;
153 enum ParserKeywordSizeEnum m_keywordSizeType;
154 size_t m_fixedSize;
155 bool m_isTableCollection;
156 std::string m_Description;
157 bool raw_string_keyword = false;
158 bool alternating_keyword = false;
159 bool double_records = false;
160 std::string code_end;
161
162 static bool validNameStart(const string_view& name);
163 void initDeckNames( const Json::JsonObject& jsonConfig );
164 void initSectionNames( const Json::JsonObject& jsonConfig );
165 void initMatchRegex( const Json::JsonObject& jsonObject );
166 void initCode( const Json::JsonObject& jsonConfig );
167 void initData( const Json::JsonObject& jsonConfig );
168 void initSize( const Json::JsonObject& jsonConfig );
169 void initSizeKeyword(const Json::JsonObject& sizeObject);
170 void commonInit(const std::string& name, ParserKeywordSizeEnum sizeType);
171 void addItems( const Json::JsonObject& jsonConfig);
172 void parseRecords( const Json::JsonObject& recordsConfig);
173 };
174
175std::ostream& operator<<( std::ostream&, const ParserKeyword& );
176}
177
178#endif
const char *const name
Definition: cJSON.h:258
const char *const string
Definition: cJSON.h:170
Definition: JsonObject.hpp:31
Definition: DeckKeyword.hpp:38
Definition: ErrorGuard.hpp:29
Definition: ParseContext.hpp:84
Definition: ParserKeyword.hpp:73
static bool validDeckName(const string_view &name)
void setFixedSize(size_t keywordSize)
ParserKeyword(const std::string &name)
DeckKeyword parse(const ParseContext &parseContext, ErrorGuard &errors, RawKeyword &rawKeyword, UnitSystem &active_unitsystem, UnitSystem &default_unitsystem, const std::string &filename) const
const std::string className() const
bool isAlternatingKeyword() const
void setMatchRegex(const std::string &deckNameRegexp)
bool operator==(const ParserKeyword &) const
std::vector< ParserRecord >::const_iterator begin() const
void setCodeEnd(const std::string &end)
size_t getFixedSize() const
bool matches(const string_view &) const
DeckNameSet::const_iterator deckNamesEnd() const
bool isDataKeyword() const
void setAlternatingKeyword(bool alternating)
std::set< std::string > DeckNameSet
Definition: ParserKeyword.hpp:89
std::vector< ParserRecord >::const_iterator end() const
enum ParserKeywordSizeEnum getSizeType() const
ParserRecord & getRecord(size_t recordIndex)
void setDoubleRecordsKeyword(bool double_rec)
void setDescription(const std::string &description)
SectionNameSet::const_iterator validSectionNamesEnd() const
bool operator!=(const ParserKeyword &) const
std::string createDeclaration(const std::string &indent) const
ParserKeyword(const Json::JsonObject &jsonConfig)
bool rawStringKeyword() const
void setSizeType(ParserKeywordSizeEnum sizeType)
const std::string & getName() const
void initSizeKeyword(const std::string &sizeKeyword, const std::string &sizeItem, int size_shift)
bool isCodeKeyword() const
void addDeckName(const std::string &deckName)
void addValidSectionName(const std::string &sectionName)
const KeywordSize & getKeywordSize() const
DeckNameSet::const_iterator deckNamesBegin() const
bool isDoubleRecordKeyword() const
std::string createDecl() const
ParserKeyword(const std::string &name, const std::string &sizeKeyword, const std::string &sizeItem, int size_shift, bool _isTableCollection=false)
const std::string & codeEnd() const
bool isTableCollection() const
bool hasDimension() const
std::string getDescription() const
static bool validInternalName(const std::string &name)
void setTableCollection(bool _isTableCollection)
std::set< std::string > SectionNameSet
Definition: ParserKeyword.hpp:90
SectionNameSet::const_iterator validSectionNamesBegin() const
bool hasMultipleDeckNames() const
bool isValidSection(const std::string &sectionName) const
void addRecord(ParserRecord)
void clearValidSectionNames()
bool hasFixedSize() const
const ParserRecord & getRecord(size_t recordIndex) const
void addDataRecord(ParserRecord)
std::string createCode() const
bool hasMatchRegex() const
Definition: ParserRecord.hpp:39
Definition: RawKeyword.hpp:38
Definition: UnitSystem.hpp:32
Definition: Stringview.hpp:48
Definition: JsonObject.hpp:29
Definition: A.hpp:4
std::ostream & operator<<(std::ostream &os, const UniformTableLinear< T > &t)
Definition: UniformTableLinear.hpp:249
ParserKeywordSizeEnum
Definition: ParserEnums.hpp:27
Definition: ParserKeyword.hpp:49
KeywordSize(const std::string &in_keyword, const std::string &in_item, int in_shift)
Definition: ParserKeyword.hpp:50
bool operator==(const KeywordSize &other) const
Definition: ParserKeyword.hpp:58
std::string keyword
Definition: ParserKeyword.hpp:68
bool operator!=(const KeywordSize &other) const
Definition: ParserKeyword.hpp:64
std::string item
Definition: ParserKeyword.hpp:69
int shift
Definition: ParserKeyword.hpp:70
KeywordSize()
Definition: ParserKeyword.hpp:56