My Project
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/EclipseState/EclipseState.hpp>
35 #include <opm/input/eclipse/Parser/ParserKeyword.hpp>
36 
37 namespace Json {
38  class JsonObject;
39 }
40 
41 namespace Opm {
42 
43  namespace Ecl {
44 
45  enum SectionType {
46  GRID, PROPS, REGIONS, SOLUTION, SUMMARY, SCHEDULE
47  };
48  }
49 
50  class Deck;
51  class ParseContext;
52  class ErrorGuard;
53  class RawKeyword;
54 
58 
59  class Parser {
60  public:
61  explicit Parser(bool addDefault = true);
62 
63  static std::string stripComments(const std::string& inputString);
64 
66  Deck parseFile(const std::string &dataFile,
67  const ParseContext&,
68  ErrorGuard& errors,
69  const std::vector<Opm::Ecl::SectionType>& sections = {}) const;
70 
71  Deck parseFile(const std::string&,
72  const ParseContext&) const;
73 
74  Deck parseFile(const std::string&,
75  const ParseContext&,
76  const std::vector<Opm::Ecl::SectionType>& sections
77  ) const;
78 
79  Deck parseFile(const std::string& datafile) const;
80 
81  Deck parseString(const std::string &data,
82  const ParseContext&,
83  ErrorGuard& errors) const;
84  Deck parseString(const std::string &data, const ParseContext& ) const;
85  Deck parseString(const std::string &data) const;
86 
87  Deck parseStream(std::unique_ptr<std::istream>&& inputStream , const ParseContext& parseContext, ErrorGuard& errors) const;
88 
90  void addParserKeyword(const Json::JsonObject& jsonKeyword);
91  void addParserKeyword(ParserKeyword parserKeyword);
92 
96  bool hasKeyword( const std::string& ) const;
97  const ParserKeyword& getKeyword(const std::string& name) const;
98 
99  bool isRecognizedKeyword( const std::string_view& deckKeywordName) const;
100  const ParserKeyword& getParserKeywordFromDeckName(const std::string_view& deckKeywordName) const;
101  std::vector<std::string> getAllDeckNames () const;
102 
103  void loadKeywords(const Json::JsonObject& jsonKeywords);
104  bool loadKeywordFromFile(const std::filesystem::path& configFile);
105 
106  void loadKeywordsFromDirectory(const std::filesystem::path& directory , bool recursive = true);
107  void applyUnitsToDeck(Deck& deck) const;
108 
114  size_t size() const;
115 
116  template <class T>
117  void addKeyword() {
118  addParserKeyword( T() );
119  }
120 
121  static EclipseState parse(const Deck& deck, const ParseContext& context);
122  static EclipseState parse(const std::string &filename, const ParseContext& context, ErrorGuard& errors);
123  static EclipseState parseData(const std::string &data, const ParseContext& context, ErrorGuard& errors);
124 
128  static EclipseGrid parseGrid(const std::string &filename,
129  const ParseContext& context,
130  ErrorGuard& errors);
131 
135  static EclipseGrid parseGrid(const Deck& deck,
136  const ParseContext& context);
137 
141  static EclipseGrid parseGridData(const std::string &data,
142  const ParseContext& context,
143  ErrorGuard& errors);
144 
145  const std::vector<std::pair<std::string,std::string>> codeKeywords() const;
146 
147  private:
148  bool hasWildCardKeyword(const std::string& keyword) const;
149  const ParserKeyword* matchingKeyword(const std::string_view& keyword) const;
150  void addDefaultKeywords();
151 
152  // std::vector< std::unique_ptr< const ParserKeyword > > keyword_storage;
153  std::list<ParserKeyword> keyword_storage;
154 
155  // associative map of deck names and the corresponding ParserKeyword object
156  std::map< std::string_view, const ParserKeyword* > m_deckParserKeywords;
157 
158  // associative map of the parser internal names and the corresponding
159  // ParserKeyword object for keywords which match a regular expression
160  std::map< std::string_view, const ParserKeyword* > m_wildCardKeywords;
161 
162  std::vector<std::pair<std::string,std::string>> code_keywords;
163  };
164 
165 } // namespace Opm
166 #endif /* PARSER_H */
167 
Definition: JsonObject.hpp:32
Definition: Deck.hpp:63
About cell information and dimension: The actual grid information is held in a pointer to an ERT ecl_...
Definition: EclipseGrid.hpp:54
Definition: EclipseState.hpp:55
Definition: ErrorGuard.hpp:29
Definition: ParseContext.hpp:88
Definition: ParserKeyword.hpp:85
The hub of the parsing process.
Definition: Parser.hpp:59
void addParserKeyword(const Json::JsonObject &jsonKeyword)
Method to add ParserKeyword instances, these holding type and size information about the keywords and...
static EclipseGrid parseGrid(const Deck &deck, const ParseContext &context)
Parses the provided deck.
static EclipseGrid parseGridData(const std::string &data, const ParseContext &context, ErrorGuard &errors)
Parses the provided deck string.
bool hasKeyword(const std::string &) const
Returns whether the parser knows about a keyword.
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...
size_t size() const
Returns the approximate number of recognized keywords in decks.
static EclipseGrid parseGrid(const std::string &filename, const ParseContext &context, ErrorGuard &errors)
Parses the deck specified in filename.
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:29