CASM  1.1.0
A Clusters Approach to Statistical Mechanics
InputParser.hh
Go to the documentation of this file.
1 #ifndef CASM_InputParser
2 #define CASM_InputParser
3 
4 #include <boost/filesystem.hpp>
5 #include <map>
6 #include <memory>
7 #include <set>
8 #include <string>
9 
12 #include "casm/misc/Validator.hh"
13 
14 namespace CASM {
15 
16 class Log;
17 
66 struct KwargsParser : public Validator {
68  jsonParser const &input;
69 
72  fs::path path;
73 
81  jsonParser const &self;
82 
84  bool required;
85 
88  std::string type_name;
89 
100  KwargsParser(jsonParser const &_input, fs::path _path, bool _required);
101 
102  virtual ~KwargsParser() {}
103 
104  // --- Accessing the JSON `input` ---
105 
109  const jsonParser &parent() const;
110 
116  fs::path parent_path() const;
117 
119  std::string name() const;
120 
125  bool exists() const;
126 
128  fs::path relpath(const fs::path &val) const {
129  return path.empty() ? val : path / val;
130  }
131 
132  // --- Subparsers ---
133 
134  typedef std::multimap<fs::path, std::shared_ptr<KwargsParser>> map_type;
135 
137  map_type::const_iterator begin() const;
138 
140  map_type::const_iterator end() const;
141 
143  bool valid() const;
144 
146  std::map<fs::path, std::set<std::string>> all_warnings() const;
147 
149  std::map<fs::path, std::set<std::string>> all_errors() const;
150 
151  using Validator::insert;
152 
160  void insert(fs::path path, const std::shared_ptr<KwargsParser> &subparser);
161 
163  void insert_error(fs::path option, std::string message);
164 
166  void insert_warning(fs::path option, std::string message);
167 
169  bool warn_unnecessary(const std::set<std::string> &expected);
170 
171  private:
172  typedef map_type::value_type PairType;
173 
179 };
180 
229 template <typename T>
230 class InputParser : public KwargsParser {
231  public:
234  std::unique_ptr<T> value;
235 
237  template <typename... Args>
238  InputParser(jsonParser const &_input, Args &&... args);
239 
242  template <typename... Args>
243  InputParser(jsonParser const &_input, fs::path _path, bool _required,
244  Args &&... args);
245 
247  template <typename CustomParse>
248  InputParser(CustomParse f_parse, jsonParser const &_input);
249 
252  template <typename CustomParse>
253  InputParser(CustomParse f_parse, jsonParser const &_input, fs::path _path,
254  bool _required);
255 
264  template <typename RequiredType, typename... Args>
265  std::unique_ptr<RequiredType> require(fs::path option, Args &&... args);
266 
275  template <typename RequiredType, typename... Args>
276  void require(RequiredType &value, fs::path option, Args &&... args);
277 
287  template <typename RequiredType, typename... Args>
288  std::unique_ptr<RequiredType> optional(fs::path option, Args &&... args);
289 
300  template <typename RequiredType, typename... Args>
301  void optional(RequiredType &value, fs::path option, Args &&... args);
302 
313  template <typename RequiredType, typename... Args>
314  RequiredType optional_else(fs::path option, const RequiredType &_default,
315  Args &&... args);
316 
327  template <typename RequiredType, typename... Args>
328  void optional_else(RequiredType &value, fs::path option,
329  const RequiredType &_default, Args &&... args);
330 
344  template <typename RequiredType, typename... Args>
345  std::shared_ptr<InputParser<RequiredType>> subparse(fs::path option,
346  Args &&... args);
347 
351  template <typename RequiredType, typename... Args>
352  std::shared_ptr<InputParser<RequiredType>> subparse_if(fs::path option,
353  Args &&... args);
354 
357  template <typename RequiredType, typename... Args>
358  std::shared_ptr<InputParser<RequiredType>> subparse_else(
359  fs::path option, const RequiredType &_default, Args &&... args);
360 
365  template <typename RequiredType, typename... Args>
366  std::shared_ptr<InputParser<RequiredType>> parse_as(Args &&... args);
367 };
368 
371 class ParentInputParser : public InputParser<std::nullptr_t> {
372  public:
373  std::shared_ptr<jsonParser const> parent_input;
374 
377  : ParentInputParser(std::make_shared<jsonParser const>(input)) {}
378 
380  ParentInputParser(std::shared_ptr<jsonParser const> _parent_input)
381  : InputParser<std::nullptr_t>(*_parent_input),
382  parent_input(_parent_input) {}
383 };
384 
395 int parse_verbosity(KwargsParser &parser, int default_verbosity = 10);
396 
398 void parse(InputParser<std::nullptr_t> &parser);
399 
400 template <typename T>
401 void parse(InputParser<T> &parser);
402 
403 // --- Methods for formatting error and warning messages ---
404 
406 void print_warnings(KwargsParser const &parser, Log &log,
407  std::string header = "Warnings");
408 
410 void print_errors(KwargsParser const &parser, Log &log,
411  std::string header = "Errors");
412 
415 jsonParser make_report(KwargsParser const &parser);
416 
419 template <typename ErrorType>
420 void report_and_throw_if_invalid(KwargsParser const &parser, Log &log,
421  ErrorType error);
422 
423 } // namespace CASM
424 
425 #endif
std::unique_ptr< RequiredType > optional(fs::path option, Args &&... args)
InputParser(jsonParser const &_input, Args &&... args)
Construct parser and use parse(*this)
std::shared_ptr< InputParser< RequiredType > > subparse_else(fs::path option, const RequiredType &_default, Args &&... args)
RequiredType optional_else(fs::path option, const RequiredType &_default, Args &&... args)
std::unique_ptr< RequiredType > require(fs::path option, Args &&... args)
std::shared_ptr< InputParser< RequiredType > > subparse(fs::path option, Args &&... args)
std::shared_ptr< InputParser< RequiredType > > parse_as(Args &&... args)
std::unique_ptr< T > value
Definition: InputParser.hh:234
std::shared_ptr< InputParser< RequiredType > > subparse_if(fs::path option, Args &&... args)
ParentInputParser(std::shared_ptr< jsonParser const > _parent_input)
Construct so that the ParentInputParser owns the jsonParser object.
Definition: InputParser.hh:380
ParentInputParser(jsonParser const &input)
Construct so that the ParentInputParser owns the jsonParser object.
Definition: InputParser.hh:376
std::shared_ptr< jsonParser const > parent_input
Definition: InputParser.hh:373
Main CASM namespace.
Definition: APICommand.hh:8
Log & log()
Definition: Log.hh:424
int parse_verbosity(KwargsParser &parser, int default_verbosity=10)
Definition: InputParser.cc:123
void print_errors(KwargsParser const &parser, Log &log, std::string header="Errors")
Formatted print error messages, including all subparsers.
Definition: InputParser.cc:168
void print_warnings(KwargsParser const &parser, Log &log, std::string header="Warnings")
Formatted print warning messages, including all subparsers.
Definition: InputParser.cc:150
void report_and_throw_if_invalid(KwargsParser const &parser, Log &log, ErrorType error)
jsonParser make_report(KwargsParser const &parser)
Return parser.input with error and warning messages added in place.
Definition: InputParser.cc:214
void parse(InputParser< ConfigEnumOptions > &parser, std::string method_name, PrimClex const &primclex, DataFormatterDictionary< Configuration > const &dict)
Definition: stream_io.hh:24
std::string name() const
Name of this->self, equivalent to this->path.filename().string()
Definition: InputParser.cc:42
bool valid() const
Return true if this and and all subparsers are valid.
Definition: InputParser.cc:56
KwargsParser(jsonParser const &_input, fs::path _path, bool _required)
Definition: InputParser.cc:20
std::map< fs::path, std::set< std::string > > all_errors() const
Return error messages from this and all subparsers.
Definition: InputParser.cc:77
Validator & insert(const Validator &other)
Definition: Validator.hh:19
map_type::value_type PairType
Definition: InputParser.hh:172
map_type::const_iterator begin() const
Begin iterator over subparsers.
Definition: InputParser.cc:48
virtual ~KwargsParser()
Definition: InputParser.hh:102
bool required
If this->input.at(this->path) is required to exist.
Definition: InputParser.hh:84
void insert_warning(fs::path option, std::string message)
Insert a subparser at location option with a single warning message
Definition: InputParser.cc:104
fs::path relpath(const fs::path &val) const
Return this->path / val, ensuring the result is a relative path.
Definition: InputParser.hh:128
jsonParser const & input
Reference to the top of the JSON document being parsed.
Definition: InputParser.hh:68
std::multimap< fs::path, std::shared_ptr< KwargsParser > > map_type
Definition: InputParser.hh:134
std::map< fs::path, std::set< std::string > > all_warnings() const
Return warning messages from this and all subparsers.
Definition: InputParser.cc:61
const jsonParser & parent() const
Definition: InputParser.cc:33
void insert_error(fs::path option, std::string message)
Insert a subparser at location option with a single error message
Definition: InputParser.cc:97
std::string type_name
Definition: InputParser.hh:88
bool exists() const
Definition: InputParser.cc:44
map_type::const_iterator end() const
End iterator over subparsers.
Definition: InputParser.cc:52
fs::path parent_path() const
Definition: InputParser.cc:40
bool warn_unnecessary(const std::set< std::string > &expected)
Insert a warning if any unexpected JSON attributes are found in self.
Definition: InputParser.cc:111
Data structure to hold error and warning messages.
Definition: Validator.hh:10
Validator & insert(const Validator &other)
Definition: Validator.hh:19