https://mooseframework.inl.gov
Public Member Functions | Private Attributes | List of all members
Parser Class Reference

Class for parsing input files. More...

#include <Parser.h>

Public Member Functions

 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. More...
 
 Parser (const std::string &input_filename, const std::optional< std::string > &input_text={})
 Constructor, given a file in input_filename. More...
 
void parse ()
 Parses the inputs. More...
 
void extractParams (const std::string &prefix, InputParameters &p)
 This function attempts to extract values from the input file based on the contents of the passed parameters objects. More...
 
hit::Node * root ()
 
const std::vector< std::string > & getInputFileNames () const
 
const std::string & getAppType () const
 
void setAppType (const std::string &app_type)
 
const std::string & getLastInputFileName () const
 
std::filesystem::path getLastInputFilePath () const
 

Private Attributes

std::unique_ptr< hit::Node > _root
 The root node, which owns the whole tree. More...
 
const std::vector< std::string > _input_filenames
 The input file names. More...
 
const std::optional< std::vector< std::string > > _input_text
 The optional input text contents (to support not reading by file) More...
 
std::string _app_type
 The application types extracted from [Application] block. More...
 

Detailed Description

Class for parsing input files.

This class utilizes the GetPot library for actually tokenizing and parsing files. It is not currently designed for extensibility. If you wish to build your own parser, please contact the MOOSE team for guidance.

Definition at line 100 of file Parser.h.

Constructor & Destructor Documentation

◆ Parser() [1/2]

Parser::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.

Optionally, the file contents can be provided via text in input_text.

Definition at line 155 of file Parser.C.

157  : _root(nullptr),
158  _input_filenames(input_filenames),
159  _input_text(input_text),
160  _app_type(std::string())
161 {
162 }
const std::vector< std::string > _input_filenames
The input file names.
Definition: Parser.h:166
const std::optional< std::vector< std::string > > _input_text
The optional input text contents (to support not reading by file)
Definition: Parser.h:169
std::string _app_type
The application types extracted from [Application] block.
Definition: Parser.h:172
std::unique_ptr< hit::Node > _root
The root node, which owns the whole tree.
Definition: Parser.h:163

◆ Parser() [2/2]

Parser::Parser ( const std::string &  input_filename,
const std::optional< std::string > &  input_text = {} 
)

Constructor, given a file in input_filename.

Optionally, the file contents can be provided via text in input_text.

Definition at line 164 of file Parser.C.

165  : Parser(std::vector<std::string>{input_filename},
166  input_text ? std::optional<std::vector<std::string>>({*input_text})
167  : std::optional<std::vector<std::string>>())
168 {
169 }
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:155

Member Function Documentation

◆ extractParams()

void Parser::extractParams ( const std::string &  prefix,
InputParameters p 
)

This function attempts to extract values from the input file based on the contents of the passed parameters objects.

It handles a number of various types with dynamic casting including vector types

◆ getAppType()

const std::string& Parser::getAppType ( ) const
inline

Definition at line 144 of file Parser.h.

144 { return _app_type; }
std::string _app_type
The application types extracted from [Application] block.
Definition: Parser.h:172

◆ getInputFileNames()

const std::vector<std::string>& Parser::getInputFileNames ( ) const
inline
Returns
The names of the inputs

Definition at line 139 of file Parser.h.

Referenced by parse().

139 { return _input_filenames; }
const std::vector< std::string > _input_filenames
The input file names.
Definition: Parser.h:166

◆ getLastInputFileName()

const std::string & Parser::getLastInputFileName ( ) const
Returns
The file name of the last input

Definition at line 291 of file Parser.C.

Referenced by getLastInputFilePath().

292 {
293  if (_input_filenames.empty())
294  mooseError("Parser::getLastInputFileName(): No inputs are set");
295  return _input_filenames.back();
296 }
const std::vector< std::string > _input_filenames
The input file names.
Definition: Parser.h:166
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302

◆ getLastInputFilePath()

std::filesystem::path Parser::getLastInputFilePath ( ) const
inline
Returns
The path of the last input

Definition at line 159 of file Parser.h.

159 { return getLastInputFileName(); }
const std::string & getLastInputFileName() const
Definition: Parser.C:291

◆ parse()

void Parser::parse ( )

Parses the inputs.

Definition at line 299 of file Parser.C.

300 {
301  _root.reset();
302 
303  if (_input_text && _input_text->size() != getInputFileNames().size())
304  mooseError("Input text is not the same size as the input filenames");
305 
306  if (getInputFileNames().size() > 1)
307  mooseInfo("Merging inputs ", Moose::stringify(getInputFileNames()));
308 
309  CompileParamWalker::ParamMap override_map;
310  CompileParamWalker cpw(override_map);
311  OverrideParamWalker opw(override_map);
312 
313  std::string errmsg;
314  std::vector<std::string> dw_errmsg;
315 
316  // Whether or not to use real filepaths (from env)
317  const std::string use_rel_paths_str =
318  std::getenv("MOOSE_RELATIVE_FILEPATHS") ? std::getenv("MOOSE_RELATIVE_FILEPATHS") : "false";
319  const auto use_real_paths = use_rel_paths_str == "0" || use_rel_paths_str == "false";
320 
321  for (const auto i : index_range(getInputFileNames()))
322  {
323  const auto & input_filename = getInputFileNames()[i];
324  const auto corrected_filename =
325  use_real_paths ? MooseUtils::realpath(input_filename) : input_filename;
326 
327  // Parse the input text string if provided, otherwise read file from disk
328  std::string input;
329  std::string errmsg;
330  if (_input_text)
331  input = (*_input_text)[i];
332  else
333  {
334  MooseUtils::checkFileReadable(corrected_filename, true);
335  std::ifstream f(corrected_filename);
336  input = std::string((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
337  }
338 
339  try
340  {
341  // provide stream to hit parse function to capture any syntax errors,
342  // set parser root node, then throw those errors if any were captured
343  std::stringstream input_errors;
344  std::unique_ptr<hit::Node> root(hit::parse(corrected_filename, input, &input_errors));
345  hit::explode(root.get());
346  DupParamWalker dw;
347  root->walk(&dw, hit::NodeType::Field);
348  if (!_root)
349  _root = std::move(root);
350  else
351  {
352  root->walk(&opw, hit::NodeType::Field);
353  hit::merge(root.get(), _root.get());
354  }
355 
356  if (!input_errors.str().empty())
357  throw hit::ParseError(input_errors.str());
358 
359  for (auto & msg : dw.errors)
360  errmsg += msg + "\n";
361 
362  dw_errmsg.push_back(errmsg);
363  _root->walk(&cpw, hit::NodeType::Field);
364  }
365  catch (hit::ParseError & err)
366  {
367  mooseError(err.what());
368  }
369  }
370 
371  // warn about overridden parameters in multiple inputs
372  if (!opw.warnings.empty())
373  mooseInfo(Moose::stringify(opw.warnings), "\n");
374 
375  // do as much error checking as early as possible so that errors are more useful instead
376  // of surprising and disconnected from what caused them.
377  BadActiveWalker bw;
378  _root->walk(&bw, hit::NodeType::Section);
379 
380  FindAppWalker fw;
381  _root->walk(&fw, hit::NodeType::Field);
382  if (fw.getApp())
383  setAppType(*fw.getApp());
384 
385  for (auto & msg : bw.errors)
386  errmsg += msg + "\n";
387 
388  // Print parse errors related to bad active early
389  if (errmsg.size() > 0)
390  mooseError(errmsg);
391 
392  for (auto & msg : dw_errmsg)
393  if (msg.size() > 0)
394  mooseError(msg);
395 }
OStreamProxy err
void setAppType(const std::string &app_type)
Definition: Parser.h:149
const std::optional< std::vector< std::string > > _input_text
The optional input text contents (to support not reading by file)
Definition: Parser.h:169
std::vector< std::string > errors
Definition: Parser.h:67
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
const std::vector< std::string > & getInputFileNames() const
Definition: Parser.h:139
std::string realpath(const std::string &path)
Wrapper around PetscGetRealPath, which is a cross-platform replacement for realpath.
Definition: MooseUtils.C:1231
void mooseInfo(Args &&... args)
Emit an informational message with the given stringified, concatenated args.
Definition: MooseError.h:369
constexpr auto merge(std::index_sequence< first... >, std::index_sequence< second... >)
Merge two index sequences into one.
bool checkFileReadable(const std::string &filename, bool check_line_endings=false, bool throw_on_unreadable=true, bool check_for_git_lfs_pointer=true)
Checks to see if a file is readable (exists and permissions)
Definition: MooseUtils.C:250
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:64
std::map< std::string, hit::Node * > ParamMap
Definition: Parser.h:73
hit::Node * root()
Definition: Parser.h:134
const std::optional< std::string > & getApp()
Definition: Parser.C:284
std::unique_ptr< hit::Node > _root
The root node, which owns the whole tree.
Definition: Parser.h:163
auto index_range(const T &sizable)

◆ root()

hit::Node* Parser::root ( )
inline
Returns
The root HIT node, if any

If this is null, it means that we haven't parsed yet

Definition at line 134 of file Parser.h.

Referenced by ActionFactory::create(), and parse().

134 { return _root.get(); }
std::unique_ptr< hit::Node > _root
The root node, which owns the whole tree.
Definition: Parser.h:163

◆ setAppType()

void Parser::setAppType ( const std::string &  app_type)
inline

Definition at line 149 of file Parser.h.

Referenced by parse().

149 { _app_type = app_type; }
std::string _app_type
The application types extracted from [Application] block.
Definition: Parser.h:172

Member Data Documentation

◆ _app_type

std::string Parser::_app_type
private

The application types extracted from [Application] block.

Definition at line 172 of file Parser.h.

Referenced by getAppType(), and setAppType().

◆ _input_filenames

const std::vector<std::string> Parser::_input_filenames
private

The input file names.

Definition at line 166 of file Parser.h.

Referenced by getInputFileNames(), and getLastInputFileName().

◆ _input_text

const std::optional<std::vector<std::string> > Parser::_input_text
private

The optional input text contents (to support not reading by file)

Definition at line 169 of file Parser.h.

Referenced by parse().

◆ _root

std::unique_ptr<hit::Node> Parser::_root
private

The root node, which owns the whole tree.

Definition at line 163 of file Parser.h.

Referenced by parse(), and root().


The documentation for this class was generated from the following files: