opm-common
Parser.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 
20 #ifndef OPM_PARSER_HPP
21 #define OPM_PARSER_HPP
22 
23 #include <opm/input/eclipse/Parser/ParserKeyword.hpp>
24 
25 #include <cstddef>
26 #include <filesystem>
27 #include <iosfwd>
28 #include <list>
29 #include <map>
30 #include <memory>
31 #include <string>
32 #include <utility>
33 #include <vector>
34 
35 namespace Json {
36  class JsonObject;
37 }
38 
39 namespace Opm {
40 
41  namespace Ecl {
42 
43  enum SectionType {
44  RUNSPEC, GRID, EDIT, PROPS, REGIONS, SOLUTION, SUMMARY, SCHEDULE
45  };
46  }
47 
48  class Deck;
49  class EclipseGrid;
50  class EclipseState;
51  class ParseContext;
52  class ErrorGuard;
53  class RawKeyword;
54  class Python;
55 
59 
60  class Parser {
61  public:
62  explicit Parser(bool addDefault = true);
63  explicit Parser(std::shared_ptr<Python> python, bool addDefault = true);
64 
65  static std::string stripComments(const std::string& inputString);
66 
68  Deck parseFile(const std::string &dataFile,
69  const ParseContext&,
70  ErrorGuard& errors,
71  const std::vector<Opm::Ecl::SectionType>& sections = {}) const;
72 
73  Deck parseFile(const std::string&,
74  const ParseContext&) const;
75 
76  Deck parseFile(const std::string&,
77  const ParseContext&,
78  const std::vector<Opm::Ecl::SectionType>& sections
79  ) const;
80 
81  Deck parseFile(const std::string& datafile) const;
82 
83  Deck parseString(const std::string &data,
84  const ParseContext&,
85  ErrorGuard& errors) const;
86  Deck parseString(const std::string &data, const ParseContext& ) const;
87  Deck parseString(const std::string &data) const;
88 
89  Deck parseStream(std::unique_ptr<std::istream>&& inputStream , const ParseContext& parseContext, ErrorGuard& errors) const;
90 
92  void addParserKeyword(const Json::JsonObject& jsonKeyword);
93  void addParserKeyword(ParserKeyword parserKeyword);
94 
98  bool hasKeyword( const std::string& ) const;
99  const ParserKeyword& getKeyword(const std::string& name) const;
100 
112  bool isRecognizedKeyword(std::string_view deckKeywordName) const;
113 
123  bool isBaseRecognizedKeyword(std::string_view deckKeywordName) const;
124 
125  const ParserKeyword& getParserKeywordFromDeckName(const std::string_view& deckKeywordName) const;
126  std::vector<std::string> getAllDeckNames () const;
127 
128  void loadKeywords(const Json::JsonObject& jsonKeywords);
129  bool loadKeywordFromFile(const std::filesystem::path& configFile);
130 
131  void loadKeywordsFromDirectory(const std::filesystem::path& directory , bool recursive = true);
132  void applyUnitsToDeck(Deck& deck) const;
133 
139  std::size_t size() const;
140 
141  template <class T>
142  void addKeyword() {
143  addParserKeyword( T() );
144  }
145 
146  static EclipseState parse(const Deck& deck, const ParseContext& context);
147  static EclipseState parse(const std::string &filename, const ParseContext& context, ErrorGuard& errors);
148  static EclipseState parseData(const std::string &data, const ParseContext& context, ErrorGuard& errors);
149 
153  static EclipseGrid parseGrid(const std::string &filename,
154  const ParseContext& context,
155  ErrorGuard& errors);
156 
160  static EclipseGrid parseGrid(const Deck& deck,
161  const ParseContext& context);
162 
166  static EclipseGrid parseGridData(const std::string &data,
167  const ParseContext& context,
168  ErrorGuard& errors);
169 
170  const std::vector<std::pair<std::string,std::string>> codeKeywords() const;
171 
172  bool silent() const { return silentMode; }
173  void silent(bool newSilentMode) { silentMode = newSilentMode; }
174 
175  static constexpr int SILENT_MODE_MIN_DEBUG_VERBOSITY_LEVEL {3}; // Debug level at which to emit silenced messeages to the debug log
176 
177  private:
178  std::shared_ptr<Python> m_python{};
179 
180  bool silentMode {false}; // Silence information messages (warnings and errors are still emitted)
181 
182  // std::vector< std::unique_ptr< const ParserKeyword > > keyword_storage;
183  std::list<ParserKeyword> keyword_storage{};
184 
185  // associative map of deck names and the corresponding ParserKeyword object
186  std::map<std::string_view, const ParserKeyword*> m_deckParserKeywords{};
187 
188  // associative map of the parser internal names and the corresponding
189  // ParserKeyword object for keywords which match a regular expression
190  std::map<std::string_view, const ParserKeyword*> m_wildCardKeywords{};
191 
192  std::vector<std::pair<std::string,std::string>> code_keywords{};
193 
194  bool hasWildCardKeyword(const std::string& keyword) const;
195 
196  const ParserKeyword* matchingKeyword(const std::string_view& keyword) const;
197  void addDefaultKeywords();
198  };
199 
200 } // namespace Opm
201 #endif /* PARSER_H */
static EclipseGrid parseGridData(const std::string &data, const ParseContext &context, ErrorGuard &errors)
Parses the provided deck string.
Definition: Parser.cpp:1657
bool hasKeyword(const std::string &) const
Returns whether the parser knows about a keyword.
Definition: Parser.cpp:1837
The hub of the parsing process.
Definition: Parser.hpp:60
bool isRecognizedKeyword(std::string_view deckKeywordName) const
Whether or not string is a valid keyword.
Definition: Parser.cpp:1782
About cell information and dimension: The actual grid information is held in a pointer to an ERT ecl_...
Definition: EclipseGrid.hpp:63
std::size_t size() const
Returns the approximate number of recognized keywords in decks.
Definition: Parser.cpp:1766
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:30
bool isBaseRecognizedKeyword(std::string_view deckKeywordName) const
Whether or not string is a valid keyword.
Definition: Parser.cpp:1792
Definition: EclipseState.hpp:66
void addParserKeyword(const Json::JsonObject &jsonKeyword)
Method to add ParserKeyword instances, these holding type and size information about the keywords and...
Definition: Parser.cpp:1833
Definition: Parser.hpp:35
Definition: ParserKeyword.hpp:86
static EclipseGrid parseGrid(const std::string &filename, const ParseContext &context, ErrorGuard &errors)
Parses the deck specified in filename.
Definition: Parser.cpp:1644
Control parser behaviour in failure conditions.
Definition: ParseContext.hpp:114
Deck parseFile(const std::string &dataFile, const ParseContext &, ErrorGuard &errors, const std::vector< Opm::Ecl::SectionType > &sections={}) const
The starting point of the parsing process. The supplied file is parsed, and the resulting Deck is ret...
Definition: Parser.cpp:1666
Definition: Deck.hpp:46
Definition: JsonObject.hpp:31
Definition: ErrorGuard.hpp:30