My Project
ParserKeyword.hpp
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 <optional>
24#include <regex>
25#include <string>
26#include <unordered_set>
27#include <utility>
28#include <variant>
29
30#include <opm/input/eclipse/Parser/ParserEnums.hpp>
31#include <opm/input/eclipse/Parser/ParserRecord.hpp>
32
33namespace Json {
34 class JsonObject;
35}
36
37namespace Opm {
38 class Deck;
39 class DeckKeyword;
40 class ParseContext;
41 class ErrorGuard;
42 class ParserDoubleItem;
43 class RawKeyword;
44 class ErrorGuard;
45
46 /*
47 Small helper struct to assemble the information needed to infer the size
48 of a keyword based on another keyword in the deck.
49 */
51 public:
53 KeywordSize(const std::string& in_keyword, const std::string& in_item, int in_shift);
54 KeywordSize(const std::string& in_keyword, const std::string& in_item);
55 KeywordSize(const std::string& in_keyword, const std::string& in_item, bool table_collection, int in_shift);
56
57 KeywordSize(std::size_t min_size, const std::string& in_keyword, const std::string& in_item, bool table_collection, int in_shift);
58 explicit KeywordSize(ParserKeywordSizeEnum size_type);
59 explicit KeywordSize(std::size_t fixed_size);
60 KeywordSize(std::size_t fixed_size, bool code);
61 KeywordSize(std::size_t min_size, std::size_t fixed_size, bool code);
62
63 bool table_collection() const;
64 ParserKeywordSizeEnum size_type() const;
65 bool code() const;
66 int size_shift() const;
67 const std::string& keyword() const;
68 const std::string& item() const;
69 std::optional<std::size_t> min_size() const;
70 void min_size(int s);
71 const std::optional<std::variant<std::size_t, std::pair<std::string, std::string>>>& max_size() const;
72 std::string construct() const;
73
74 bool operator==(const KeywordSize& ) const;
75 bool operator!=(const KeywordSize& other) const;
76 private:
77 int shift{0};
78 bool is_table_collection{false};
79 ParserKeywordSizeEnum m_size_type;
80 std::optional<std::size_t> m_min_size;
81 std::optional<std::variant<std::size_t, std::pair<std::string, std::string>>> m_max_size;
82 bool is_code{false};
83 };
84
86 public:
87 ParserKeyword(const std::string& name, KeywordSize kw_size);
88 explicit ParserKeyword(const std::string& name);
89 explicit ParserKeyword(const Json::JsonObject& jsonConfig);
90
91 void initSizeKeyword( const std::string& sizeKeyword, const std::string& sizeItem, bool table_collection, int size_shift);
92
93
94 static bool validInternalName(const std::string& name);
95 static bool validDeckName(const std::string_view& name);
96 bool hasMatchRegex() const;
97 void setMatchRegex(const std::string& deckNameRegexp);
98 bool matches(const std::string_view& ) const;
99 bool hasDimension() const;
100 void addRecord( ParserRecord );
101 void addDataRecord( ParserRecord );
102 const ParserRecord& getRecord(size_t recordIndex) const;
103 ParserRecord& getRecord(size_t recordIndex);
104 std::vector< ParserRecord >::const_iterator begin() const;
105 std::vector< ParserRecord >::const_iterator end() const;
106 const std::string className() const;
107 const std::string& getName() const;
108 std::optional<std::size_t> min_size() const;
109 size_t getFixedSize() const;
110 bool hasFixedSize() const;
111 bool isTableCollection() const;
112 std::string getDescription() const;
113 void setDescription(const std::string &description);
114
115 bool hasMultipleDeckNames() const;
116 void clearDeckNames();
117 void addDeckName( const std::string& deckName );
118 void setCodeEnd(const std::string& end);
119 const std::unordered_set<std::string>& deck_names() const;
120 const std::string& codeEnd() const;
121
122 const std::vector<std::string>& requiredKeywords() const;
123 const std::vector<std::string>& prohibitedKeywords() const;
124 void setRequiredKeywords(const std::vector<std::string>&);
125 void setProhibitedKeywords(const std::vector<std::string>&);
126
127 void clearValidSectionNames();
128 void addValidSectionName(const std::string& sectionName);
129 bool isValidSection(const std::string& sectionName) const;
130 const std::unordered_set<std::string>& sections() const;
131
132 DeckKeyword parse(const ParseContext& parseContext, ErrorGuard& errors, RawKeyword& rawKeyword, UnitSystem& active_unitsystem, UnitSystem& default_unitsystem) const;
133 enum ParserKeywordSizeEnum getSizeType() const;
134 const KeywordSize& getKeywordSize() const;
135 bool isDataKeyword() const;
136 bool rawStringKeyword() const;
137 bool isCodeKeyword() const;
138 bool isAlternatingKeyword() const;
139 bool isDoubleRecordKeyword() const;
140 void setAlternatingKeyword(bool alternating);
141 void setDoubleRecordsKeyword(bool double_rec);
142
143 std::string createDeclaration(const std::string& indent) const;
144 std::string createDecl() const;
145 std::string createCode() const;
146
147 bool operator==( const ParserKeyword& ) const;
148 bool operator!=( const ParserKeyword& ) const;
149
150 private:
151 std::string m_name;
152 KeywordSize keyword_size;
153 std::unordered_set<std::string> m_deckNames;
154 std::unordered_set<std::string> m_validSectionNames;
155 std::string m_matchRegexString;
156 std::regex m_matchRegex;
157 std::vector< ParserRecord > m_records;
158 std::string m_Description;
159 bool raw_string_keyword = false;
160 bool alternating_keyword = false;
161 bool double_records = false;
162 std::string code_end;
163 std::vector<std::string> m_requires;
164 std::vector<std::string> m_prohibits;
165
166 static bool validNameStart(const std::string_view& name);
167 void initDeckNames( const Json::JsonObject& jsonConfig );
168 void initSectionNames( const Json::JsonObject& jsonConfig );
169 void initMatchRegex( const Json::JsonObject& jsonObject );
170 void initCode( const Json::JsonObject& jsonConfig );
171 void initData( const Json::JsonObject& jsonConfig );
172 void initProhibitedKeywords(const Json::JsonObject& keywordList);
173 void initRequiredKeywords(const Json::JsonObject& keywordList);
174 void initSize( const Json::JsonObject& jsonConfig );
175 void initSizeKeyword(bool table_collection, const Json::JsonObject& sizeObject);
176 void addItems( const Json::JsonObject& jsonConfig);
177 void parseRecords( const Json::JsonObject& recordsConfig);
178 };
179
180std::ostream& operator<<( std::ostream&, const ParserKeyword& );
181}
182
183#endif
Definition: JsonObject.hpp:32
Definition: DeckKeyword.hpp:36
Definition: ErrorGuard.hpp:29
Definition: ParserKeyword.hpp:50
Definition: ParseContext.hpp:88
Definition: ParserKeyword.hpp:85
Definition: ParserRecord.hpp:39
Definition: UnitSystem.hpp:33
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:29