www.mooseframework.org
Parser.h
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #ifndef PARSER_H
16 #define PARSER_H
17 
18 // MOOSE includes
19 #include "ConsoleStreamInterface.h"
20 #include "MooseTypes.h"
21 #include "InputParameters.h"
22 #include "Syntax.h"
23 
24 #include "hit.h"
25 
26 #include <vector>
27 #include <string>
28 
29 // Forward declarations
30 class ActionWarehouse;
31 class SyntaxTree;
32 class MooseApp;
33 class Factory;
34 class ActionFactory;
35 class GlobalParamsAction;
36 class JsonSyntaxTree;
37 
38 inline std::string
39 errormsg(std::string /*fname*/, hit::Node * /*n*/)
40 {
41  return "";
42 }
43 
44 template <typename T, typename... Args>
45 std::string
46 errormsg(std::string fname, hit::Node * n, T arg, Args... args)
47 {
48  std::stringstream ss;
49  if (n && fname.size() > 0)
50  ss << fname << ":" << n->line() << ": ";
51  else if (fname.size() > 0)
52  ss << fname << ":0: ";
53  ss << arg;
54  ss << errormsg("", nullptr, args...);
55  return ss.str();
56 }
57 
58 // Expands ${...} substitution expressions with variable values from the tree.
59 class ExpandWalker : public hit::Walker
60 {
61 public:
62  ExpandWalker(std::string fname) : _fname(fname) {}
63  virtual void
64  walk(const std::string & /*fullpath*/, const std::string & /*nodepath*/, hit::Node * n) override
65  {
66  auto f = dynamic_cast<hit::Field *>(n);
67  auto s = f->val();
68 
69  auto start = s.find("${");
70  while (start < s.size())
71  {
72  auto end = s.find("}", start);
73  if (end != std::string::npos)
74  {
75  auto var = s.substr(start + 2, end - (start + 2));
76  auto curr = n;
77  while ((curr = curr->parent()))
78  {
79  auto src = curr->find(var);
80  if (src && src != n && src->type() == hit::NodeType::Field)
81  {
82  used.push_back(hit::pathJoin({curr->fullpath(), var}));
83  s = s.substr(0, start) + curr->param<std::string>(var) +
84  s.substr(end + 1, s.size() - (end + 1));
85 
86  if (end + 1 - start == f->val().size())
87  f->setVal(s, dynamic_cast<hit::Field *>(curr->find(var))->kind());
88  else
89  f->setVal(s);
90 
91  // move end back to the position of the end of the replacement text - not the replaced
92  // text since the former is the one relevant to the string for remaining replacements.
93  end = start + curr->param<std::string>(var).size();
94  break;
95  }
96  }
97 
98  if (curr == nullptr)
99  errors.push_back(
100  errormsg(_fname, n, "no variable '", var, "' found for substitution expression"));
101  }
102  else
103  errors.push_back(errormsg(_fname, n, "missing substitution expression terminator '}'"));
104  start = s.find("${", end);
105  }
106  }
107 
108  std::vector<std::string> used;
109  std::vector<std::string> errors;
110 
111 private:
112  std::string _fname;
113 };
114 
120 class Parser : public ConsoleStreamInterface, public hit::Walker
121 {
122 public:
124  {
126  YAML
127  };
128 
129  Parser(MooseApp & app, ActionWarehouse & action_wh);
130 
131  virtual ~Parser();
132 
134  std::string getSyntaxByAction(const std::string & action, const std::string & task)
135  {
136  return _syntax.getSyntaxByAction(action, task);
137  }
138 
142  std::string getFileName(bool stripLeadingPath = true) const;
143 
148  void parse(const std::string & input_filename);
149 
155  void extractParams(const std::string & prefix, InputParameters & p);
156 
160  void initSyntaxFormatter(SyntaxFormatterType type, bool dump_mode);
161 
165  void buildFullTree(const std::string & search_string);
166 
170  void buildJsonSyntaxTree(JsonSyntaxTree & tree) const;
171 
172  void walk(const std::string & fullpath, const std::string & nodepath, hit::Node * n);
173 
174  void errorCheck(const Parallel::Communicator & comm, bool warn_unused, bool err_unused);
175 
176  std::vector<std::string> listValidParams(std::string & section_name);
177 
178 protected:
183  template <typename T, typename Base>
185  void setScalarParameter(const std::string & full_name,
186  const std::string & short_name,
187  InputParameters::Parameter<T> * param,
188  bool in_global,
189  GlobalParamsAction * global_block);
190 
191  template <typename T, typename UP_T, typename Base>
192  void setScalarValueTypeParameter(const std::string & full_name,
193  const std::string & short_name,
194  InputParameters::Parameter<T> * param,
195  bool in_global,
196  GlobalParamsAction * global_block);
197 
199  template <typename T, typename Base>
200  void setVectorParameter(const std::string & full_name,
201  const std::string & short_name,
202  InputParameters::Parameter<std::vector<T>> * param,
203  bool in_global,
204  GlobalParamsAction * global_block);
205 
210  template <typename T>
211  void setFilePathParam(const std::string & full_name,
212  const std::string & short_name,
213  InputParameters::Parameter<T> * param,
214  InputParameters & params,
215  bool in_global,
216  GlobalParamsAction * global_block);
217 
222  template <typename T>
223  void setVectorFilePathParam(const std::string & full_name,
224  const std::string & short_name,
225  InputParameters::Parameter<std::vector<T>> * param,
226  InputParameters & params,
227  bool in_global,
228  GlobalParamsAction * global_block);
233  template <typename T>
234  void setDoubleIndexParameter(const std::string & full_name,
235  const std::string & short_name,
236  InputParameters::Parameter<std::vector<std::vector<T>>> * param,
237  bool in_global,
238  GlobalParamsAction * global_block);
239 
244  template <typename T>
245  void setScalarComponentParameter(const std::string & full_name,
246  const std::string & short_name,
247  InputParameters::Parameter<T> * param,
248  bool in_global,
249  GlobalParamsAction * global_block);
250 
255  template <typename T>
256  void setVectorComponentParameter(const std::string & full_name,
257  const std::string & short_name,
258  InputParameters::Parameter<std::vector<T>> * param,
259  bool in_global,
260  GlobalParamsAction * global_block);
261 
262  std::unique_ptr<hit::Node> _cli_root = nullptr;
263  std::unique_ptr<hit::Node> _root = nullptr;
264  std::vector<std::string> _secs_need_first;
265 
276 
278  std::unique_ptr<SyntaxTree> _syntax_formatter;
279 
281  std::string _input_filename;
282 
284  std::set<std::string> _extracted_vars;
285 
288 
291 
293  std::ostringstream * _current_error_stream;
294 
295 private:
296  std::string _errmsg;
297  std::string _warnmsg;
298  std::string hitCLIFilter(std::string appname, int argc, char * argv[]);
299  void walkRaw(std::string fullpath, std::string nodepath, hit::Node * n);
300 };
301 
302 #endif // PARSER_H
MooseApp & _app
The MooseApp this Parser is part of.
Definition: Parser.h:267
std::string _warnmsg
Definition: Parser.h:297
Generic factory class for build all sorts of objects.
Definition: Factory.h:152
std::vector< std::string > _secs_need_first
Definition: Parser.h:264
bool _sections_read
Boolean to indicate whether parsing has started (sections have been extracted)
Definition: Parser.h:287
SyntaxFormatterType
Definition: Parser.h:123
std::string getSyntaxByAction(const std::string &action, const std::string &task)
Retrieve the Syntax associated with the passed Action and task.
Definition: Parser.h:134
Base class for MOOSE-based applications.
Definition: MooseApp.h:58
Storage for action instances.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
ExpandWalker(std::string fname)
Definition: Parser.h:62
Syntax & _syntax
Reference to an object that defines input file syntax.
Definition: Parser.h:275
Holds the syntax in a Json::Value tree.
std::string _input_filename
The input file name that is used for parameter extraction.
Definition: Parser.h:281
Specialized factory for generic Action System objects.
Definition: ActionFactory.h:70
An inteface for the _console for outputting to the Console object.
std::vector< std::string > errors
Definition: Parser.h:109
std::unique_ptr< SyntaxTree > _syntax_formatter
Object for holding the syntax parse tree.
Definition: Parser.h:278
std::string _errmsg
Definition: Parser.h:296
std::set< std::string > _extracted_vars
The set of all variables extracted from the input file.
Definition: Parser.h:284
ActionWarehouse & _action_wh
Action warehouse that will be filled by actions.
Definition: Parser.h:271
std::ostringstream * _current_error_stream
The current stream object used for capturing errors during extraction.
Definition: Parser.h:293
virtual void walk(const std::string &, const std::string &, hit::Node *n) override
Definition: Parser.h:64
MatType type
std::vector< std::string > used
Definition: Parser.h:108
PetscInt n
std::string errormsg(std::string, hit::Node *)
Definition: Parser.h:39
MPI_Comm comm
Holding syntax for parsing input files.
Definition: Syntax.h:26
std::string _fname
Definition: Parser.h:112
InputParameters * _current_params
The current parameter object for which parameters are being extracted.
Definition: Parser.h:290
Factory & _factory
The Factory associated with that MooseApp.
Definition: Parser.h:269
ActionFactory & _action_factory
The Factory that builds actions.
Definition: Parser.h:273
Class for parsing input files.
Definition: Parser.h:120