https://mooseframework.inl.gov
Parser.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 #include "MooseTypes.h"
13 
14 #include "hit/hit.h"
15 
16 #include <vector>
17 #include <string>
18 #include <iomanip>
19 #include <optional>
20 #include <filesystem>
21 
22 class FuncParseEvaler : public hit::Evaler
23 {
24 public:
25  virtual std::string
26  eval(hit::Field * n, const std::list<std::string> & args, hit::BraceExpander & exp);
27 };
28 
29 class UnitsConversionEvaler : public hit::Evaler
30 {
31 public:
32  virtual std::string
33  eval(hit::Field * n, const std::list<std::string> & args, hit::BraceExpander & exp);
34 };
35 
36 class DupParamWalker : public hit::Walker
37 {
38 public:
39  virtual void
40  walk(const std::string & fullpath, const std::string & /*nodepath*/, hit::Node * n) override;
41 
42  std::vector<hit::ErrorMessage> errors;
43 
44 private:
45  std::map<std::string, hit::Node *> _have;
46 };
47 
48 class BadActiveWalker : public hit::Walker
49 {
50 public:
51  virtual void walk(const std::string & /*fullpath*/,
52  const std::string & /*nodepath*/,
53  hit::Node * section) override;
54  std::vector<hit::ErrorMessage> errors;
55 };
56 
57 class CompileParamWalker : public hit::Walker
58 {
59 public:
60  typedef std::map<std::string, hit::Node *> ParamMap;
61  CompileParamWalker(ParamMap & map) : _map(map) {};
62 
63  virtual void
64  walk(const std::string & fullpath, const std::string & /*nodepath*/, hit::Node * n) override;
65 
66 private:
68 };
69 
70 class OverrideParamWalker : public hit::Walker
71 {
72 public:
74 
75  void walk(const std::string & fullpath, const std::string & /*nodepath*/, hit::Node * n) override;
76  std::vector<std::string> warnings;
77 
78 private:
80 };
81 
87 class Parser
88 {
89 public:
95  Parser(const std::vector<std::string> & input_filenames,
96  const std::optional<std::vector<std::string>> & input_text = {});
102  Parser(const std::string & input_filename, const std::optional<std::string> & input_text = {});
103 
104  struct Error : public hit::Error
105  {
106  Error() = delete;
107  Error(const std::vector<hit::ErrorMessage> & error_messages);
108  };
109 
113  void parse();
114 
120  const hit::Node * queryRoot() const { return _root.get(); }
122  hit::Node * queryRoot() { return _root.get(); }
124 
130  hit::Node & getRoot();
131 
137  const hit::Node * queryCommandLineRoot() const { return _cli_root.get(); }
139  hit::Node * queryCommandLineRoot() { return _cli_root.get(); }
141 
147  const hit::Node & getCommandLineRoot() const;
149  hit::Node & getCommandLineRoot();
151 
155  const std::vector<std::string> & getInputFileNames() const { return _input_filenames; }
156 
160  const std::vector<std::string> & getInputText() const { return _input_text; }
161 
162  /*
163  * Get extracted application type from parser
164  */
165  const std::string & getAppType() const { return _app_type; }
166 
167  /*
168  * Set the application type in parser
169  */
170  void setAppType(const std::string & app_type) { _app_type = app_type; }
171 
175  void setCommandLineParams(const std::vector<std::string> & params);
176 
180  const std::string & getLastInputFileName() const;
181 
185  std::filesystem::path getLastInputFilePath() const { return getLastInputFileName(); }
186 
192  void setThrowOnError(const bool throw_on_error) { _throw_on_error = throw_on_error; }
193 
199  bool getThrowOnError() const { return _throw_on_error; }
200 
206  const std::set<std::string> & getExtractedVars() const { return _extracted_vars; }
210  static void appendErrorMessages(std::vector<hit::ErrorMessage> & to,
212  const std::vector<hit::ErrorMessage> & from);
213  static void appendErrorMessages(std::vector<hit::ErrorMessage> & to, const hit::Error & error);
215 
219  static std::string joinErrorMessages(const std::vector<hit::ErrorMessage> & error_messages);
220 
227  void parseError(std::vector<hit::ErrorMessage> messages) const;
228 
229 private:
231  std::unique_ptr<hit::Node> _root;
232 
234  const std::vector<std::string> _input_filenames;
235 
237  std::vector<std::string> _input_text;
238 
240  std::unique_ptr<hit::Node> _cli_root;
241 
243  std::string _app_type;
244 
247 
249  std::optional<std::vector<std::string>> _command_line_params;
250 
252  std::set<std::string> _extracted_vars;
253 };
const std::vector< std::string > _input_filenames
The input file names.
Definition: Parser.h:234
void setAppType(const std::string &app_type)
Definition: Parser.h:170
const hit::Node * queryCommandLineRoot() const
Definition: Parser.h:138
void parseError(std::vector< hit::ErrorMessage > messages) const
Helper for throwing an error with the given messages.
Definition: Parser.C:493
void parse()
Parses the inputs.
Definition: Parser.C:299
CompileParamWalker(ParamMap &map)
Definition: Parser.h:61
auto exp(const T &)
ParamMap & _map
Definition: Parser.h:67
void setCommandLineParams(const std::vector< std::string > &params)
Sets the HIT parameters from the command line.
Definition: Parser.C:279
virtual std::string eval(hit::Field *n, const std::list< std::string > &args, hit::BraceExpander &exp)
Definition: Parser.C:83
const std::string & getLastInputFileName() const
Definition: Parser.C:286
hit::Node & getRoot()
Definition: Parser.C:449
const std::vector< std::string > & getInputFileNames() const
Definition: Parser.h:155
const std::set< std::string > & getExtractedVars() const
Definition: Parser.h:206
std::vector< std::string > warnings
Definition: Parser.h:76
std::string _app_type
The application types extracted from [Application] block.
Definition: Parser.h:243
const hit::Node * queryRoot() const
Definition: Parser.h:121
const std::string & getAppType() const
Definition: Parser.h:165
std::vector< hit::ErrorMessage > errors
Definition: Parser.h:42
static void appendErrorMessages(std::vector< hit::ErrorMessage > &to, const std::vector< hit::ErrorMessage > &from)
Helper for accumulating errors from a walker into an accumulation of errors.
Definition: Parser.C:471
virtual void walk(const std::string &fullpath, const std::string &, hit::Node *n) override
Definition: Parser.C:185
std::unique_ptr< hit::Node > _cli_root
The root node for command line hit arguments.
Definition: Parser.h:240
static std::string joinErrorMessages(const std::vector< hit::ErrorMessage > &error_messages)
Helper for combining error messages into a single, newline separated message.
Definition: Parser.C:484
Parser(const std::vector< std::string > &input_filenames, const std::optional< std::vector< std::string >> &input_text={})
Constructor given a list of input files, given in input_filenames.
Definition: Parser.C:143
std::map< std::string, hit::Node * > _have
Definition: Parser.h:45
bool getThrowOnError() const
Definition: Parser.h:199
bool _throw_on_error
Whether or not to throw on error.
Definition: Parser.h:246
std::vector< std::string > _input_text
The input text (may be filled during parse())
Definition: Parser.h:237
hit::Node * queryRoot()
Definition: Parser.h:122
std::map< std::string, hit::Node * > ParamMap
Definition: Parser.h:60
std::set< std::string > _extracted_vars
Variables that have been extracted during brace expansion.
Definition: Parser.h:252
virtual void walk(const std::string &fullpath, const std::string &, hit::Node *n) override
Definition: Parser.C:164
std::unique_ptr< hit::Node > _root
The root node, which owns the whole tree.
Definition: Parser.h:231
void walk(const std::string &fullpath, const std::string &, hit::Node *n) override
Definition: Parser.C:194
const hit::Node & getCommandLineRoot() const
Definition: Parser.C:457
hit::Node * queryCommandLineRoot()
Definition: Parser.h:139
Error()=delete
const std::vector< std::string > & getInputText() const
Definition: Parser.h:160
std::vector< hit::ErrorMessage > errors
Definition: Parser.h:54
std::filesystem::path getLastInputFilePath() const
Definition: Parser.h:185
std::optional< std::vector< std::string > > _command_line_params
The command line HIT parameters (if any)
Definition: Parser.h:249
virtual std::string eval(hit::Field *n, const std::list< std::string > &args, hit::BraceExpander &exp)
Definition: Parser.C:29
OverrideParamWalker(const CompileParamWalker::ParamMap &map)
Definition: Parser.h:73
Class for parsing input files.
Definition: Parser.h:87
virtual void walk(const std::string &, const std::string &, hit::Node *section) override
Definition: Parser.C:210
const CompileParamWalker::ParamMap & _map
Definition: Parser.h:79
void setThrowOnError(const bool throw_on_error)
Set whether or not to throw Parse::Error on errors.
Definition: Parser.h:192