www.mooseframework.org
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 112 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 154 of file Parser.C.

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

◆ 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 163 of file Parser.C.

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

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 156 of file Parser.h.

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

◆ getInputFileNames()

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

Definition at line 151 of file Parser.h.

Referenced by parse().

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

◆ getLastInputFileName()

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

Definition at line 284 of file Parser.C.

Referenced by getLastInputFilePath().

285 {
286  if (_input_filenames.empty())
287  mooseError("Parser::getLastInputFileName(): No inputs are set");
288  return _input_filenames.back();
289 }
const std::vector< std::string > _input_filenames
The input file names.
Definition: Parser.h:178
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:284

◆ getLastInputFilePath()

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

Definition at line 171 of file Parser.h.

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

◆ parse()

void Parser::parse ( )

Parses the inputs.

Definition at line 292 of file Parser.C.

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

◆ 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 146 of file Parser.h.

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

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

◆ setAppType()

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

Definition at line 161 of file Parser.h.

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

Member Data Documentation

◆ _app_type

std::string Parser::_app_type
private

The application types extracted from [Application] block.

Definition at line 184 of file Parser.h.

Referenced by getAppType(), parse(), and setAppType().

◆ _input_filenames

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

The input file names.

Definition at line 178 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 181 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 175 of file Parser.h.

Referenced by parse(), and root().


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