Parser.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 
20 #ifndef OPM_PARSER_HPP
21 #define OPM_PARSER_HPP
22 #include <string>
23 #include <map>
24 #include <fstream>
25 #include <memory>
26 
27 #include <boost/filesystem.hpp>
28 #include <boost/algorithm/string.hpp>
29 
30 #include <opm/json/JsonObject.hpp>
31 
36 
37 namespace Opm {
38 
39  struct ParserState;
40 
44 
45  class Parser {
46  public:
47  Parser(bool addDefault = true);
48 
49  static std::string stripComments(const std::string& inputString);
50 
52  DeckPtr parseFile(const std::string &dataFile, const ParseMode& parseMode) const;
53  DeckPtr parseString(const std::string &data, const ParseMode& parseMode) const;
54  DeckPtr parseStream(std::shared_ptr<std::istream> inputStream , const ParseMode& parseMode) const;
55 
56 
58  void addParserKeyword(ParserKeywordConstPtr parserKeyword);
59  bool dropParserKeyword(const std::string& parserKeywordName);
60  ParserKeywordConstPtr getKeyword(const std::string& name) const;
61 
62  bool isRecognizedKeyword( const std::string& deckKeywordName) const;
63  ParserKeywordConstPtr getParserKeywordFromDeckName(const std::string& deckKeywordName) const;
64  std::vector<std::string> getAllDeckNames () const;
65 
66  void loadKeywords(const Json::JsonObject& jsonKeywords);
67  bool loadKeywordFromFile(const boost::filesystem::path& configFile);
68 
69  void loadKeywordsFromDirectory(const boost::filesystem::path& directory , bool recursive = true);
70  void applyUnitsToDeck(DeckPtr deck) const;
71 
77  size_t size() const;
78 
83  bool hasInternalKeyword(const std::string& internalKeywordName) const;
84 
88  ParserKeywordConstPtr getParserKeywordFromInternalName(const std::string& internalKeywordName) const;
89 
90 
91  template <class T>
92  void addKeyword() {
93  addParserKeyword( std::make_shared<T>());
94  }
95 
96 
97  private:
98  // associative map of the parser internal name and the corresponding ParserKeyword object
99  std::map<std::string, ParserKeywordConstPtr> m_internalParserKeywords;
100  // associative map of deck names and the corresponding ParserKeyword object
101  std::map<std::string, ParserKeywordConstPtr> m_deckParserKeywords;
102  // associative map of the parser internal names and the corresponding
103  // ParserKeyword object for keywords which match a regular expression
104  std::map<std::string, ParserKeywordConstPtr> m_wildCardKeywords;
105 
106  bool hasWildCardKeyword(const std::string& keyword) const;
107  ParserKeywordConstPtr matchingKeyword(const std::string& keyword) const;
108 
109  bool tryParseKeyword(std::shared_ptr<ParserState> parserState) const;
110  bool parseState(std::shared_ptr<ParserState> parserState) const;
111  RawKeywordPtr createRawKeyword(const std::string& keywordString, std::shared_ptr<ParserState> parserState) const;
112  void addDefaultKeywords();
113 
114  boost::filesystem::path getIncludeFilePath(std::shared_ptr<ParserState> parserState, std::string path) const;
115  std::string doSpecialHandlingForTitleKeyword(std::string line, std::shared_ptr<ParserState> parserState) const;
116  };
117 
118 
119  typedef std::shared_ptr<Parser> ParserPtr;
120  typedef std::shared_ptr<const Parser> ParserConstPtr;
121 } // namespace Opm
122 #endif /* PARSER_H */
123 
bool loadKeywordFromFile(const boost::filesystem::path &configFile)
bool hasInternalKeyword(const std::string &internalKeywordName) const
Returns whether the parser knows about an keyword with a given internal name.
std::vector< std::string > getAllDeckNames() const
void addKeyword()
Definition: Parser.hpp:92
void loadKeywordsFromDirectory(const boost::filesystem::path &directory, bool recursive=true)
Parser(bool addDefault=true)
bool isRecognizedKeyword(const std::string &deckKeywordName) const
std::shared_ptr< RawKeyword > RawKeywordPtr
Definition: RawKeyword.hpp:79
Definition: Deck.hpp:29
Definition: Parser.hpp:45
static std::string stripComments(const std::string &inputString)
size_t size() const
Returns the approximate number of recognized keywords in decks.
std::shared_ptr< const Parser > ParserConstPtr
Definition: Parser.hpp:120
void loadKeywords(const Json::JsonObject &jsonKeywords)
void addParserKeyword(ParserKeywordConstPtr parserKeyword)
Method to add ParserKeyword instances, these holding type and size information about the keywords and...
DeckPtr parseStream(std::shared_ptr< std::istream > inputStream, const ParseMode &parseMode) const
DeckPtr parseFile(const std::string &dataFile, const ParseMode &parseMode) const
The starting point of the parsing process. The supplied file is parsed, and the resulting Deck is ret...
std::shared_ptr< Parser > ParserPtr
Definition: Parser.hpp:119
Definition: JsonObject.hpp:31
ParserKeywordConstPtr getKeyword(const std::string &name) const
Definition: ParseMode.hpp:78
void applyUnitsToDeck(DeckPtr deck) const
DeckPtr parseString(const std::string &data, const ParseMode &parseMode) const
std::shared_ptr< const ParserKeyword > ParserKeywordConstPtr
Definition: ParserKeyword.hpp:50
ParserKeywordConstPtr getParserKeywordFromInternalName(const std::string &internalKeywordName) const
Retrieve a ParserKeyword object given an internal keyword name.
std::shared_ptr< Deck > DeckPtr
Definition: Deck.hpp:85
ParserKeywordConstPtr getParserKeywordFromDeckName(const std::string &deckKeywordName) const
bool dropParserKeyword(const std::string &parserKeywordName)