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