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

Class for parsing input files. More...

#include <Parser.h>

Classes

struct  Error
 

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...
 
hit::Node & getRoot ()
 
const std::vector< std::string > & getInputFileNames () const
 
const std::vector< std::string > & getInputText () const
 
const std::string & getAppType () const
 
void setAppType (const std::string &app_type)
 
void setCommandLineParams (const std::vector< std::string > &params)
 Sets the HIT parameters from the command line. More...
 
const std::string & getLastInputFileName () const
 
std::filesystem::path getLastInputFilePath () const
 
void setThrowOnError (const bool throw_on_error)
 Set whether or not to throw Parse::Error on errors. More...
 
bool getThrowOnError () const
 
const std::set< std::string > & getExtractedVars () const
 
void parseError (std::vector< hit::ErrorMessage > messages) const
 Helper for throwing an error with the given messages. More...
 
const hit::Node * queryRoot () const
 
hit::Node * queryRoot ()
 
const hit::Node * queryCommandLineRoot () const
 
hit::Node * queryCommandLineRoot ()
 
const hit::Node & getCommandLineRoot () const
 
hit::Node & getCommandLineRoot ()
 

Static Public Member Functions

static std::string joinErrorMessages (const std::vector< hit::ErrorMessage > &error_messages)
 Helper for combining error messages into a single, newline separated message. More...
 
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. More...
 
static void appendErrorMessages (std::vector< hit::ErrorMessage > &to, const hit::Error &error)
 

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...
 
std::vector< std::string > _input_text
 The input text (may be filled during parse()) More...
 
std::unique_ptr< hit::Node > _cli_root
 The root node for command line hit arguments. More...
 
std::string _app_type
 The application types extracted from [Application] block. More...
 
bool _throw_on_error
 Whether or not to throw on error. More...
 
std::optional< std::vector< std::string > > _command_line_params
 The command line HIT parameters (if any) More...
 
std::set< std::string > _extracted_vars
 Variables that have been extracted during brace expansion. 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 87 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 143 of file Parser.C.

145  : _root(nullptr),
146  _input_filenames(input_filenames),
147  _input_text(input_text ? *input_text : std::vector<std::string>()),
148  _cli_root(nullptr),
149  _throw_on_error(false)
150 {
151  if (input_text && _input_filenames.size() != input_text->size())
152  mooseError("Parser: Input text not the same length as input filenames");
153 }
const std::vector< std::string > _input_filenames
The input file names.
Definition: Parser.h:234
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:333
std::unique_ptr< hit::Node > _cli_root
The root node for command line hit arguments.
Definition: Parser.h:240
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
std::unique_ptr< hit::Node > _root
The root node, which owns the whole tree.
Definition: Parser.h:231

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

157  : Parser(std::vector<std::string>{input_filename},
158  input_text ? std::optional<std::vector<std::string>>({*input_text})
159  : std::optional<std::vector<std::string>>())
160 {
161 }
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

Member Function Documentation

◆ appendErrorMessages() [1/2]

void Parser::appendErrorMessages ( std::vector< hit::ErrorMessage > &  to,
const std::vector< hit::ErrorMessage > &  from 
)
static

Helper for accumulating errors from a walker into an accumulation of errors.

Definition at line 471 of file Parser.C.

Referenced by appendErrorMessages(), Moose::Builder::errorCheck(), parse(), Moose::Builder::setMapParameter(), Moose::Builder::setScalarComponentParameter(), Moose::Builder::setScalarParameter(), Moose::Builder::setVectorComponentParameter(), and Moose::Builder::setVectorParameter().

473 {
474  to.insert(to.end(), from.begin(), from.end());
475 }

◆ appendErrorMessages() [2/2]

void Parser::appendErrorMessages ( std::vector< hit::ErrorMessage > &  to,
const hit::Error &  error 
)
static

Definition at line 478 of file Parser.C.

479 {
480  appendErrorMessages(to, error.error_messages);
481 }
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

◆ getAppType()

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

Definition at line 165 of file Parser.h.

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

◆ getCommandLineRoot() [1/2]

const hit::Node & Parser::getCommandLineRoot ( ) const
Returns
The root command line HIT node, with error checking on if it exists

If it doesn't exist, it means we haven't parsed yet

Definition at line 457 of file Parser.C.

Referenced by Moose::Builder::errorCheck(), parse(), and parseError().

458 {
459  if (!queryCommandLineRoot())
460  mooseError("Parser::getCommandLineRoot(): command line root is not set");
461  return *queryCommandLineRoot();
462 }
const hit::Node * queryCommandLineRoot() const
Definition: Parser.h:138
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:333

◆ getCommandLineRoot() [2/2]

hit::Node & Parser::getCommandLineRoot ( )

Definition at line 465 of file Parser.C.

466 {
467  return const_cast<hit::Node &>(std::as_const(*this).getCommandLineRoot());
468 }

◆ getExtractedVars()

const std::set<std::string>& Parser::getExtractedVars ( ) const
inline
Returns
The variables that have been extracted so far.

These are the variables that have been used during brace expansion.

Definition at line 206 of file Parser.h.

Referenced by Moose::Builder::build().

206 { return _extracted_vars; }
std::set< std::string > _extracted_vars
Variables that have been extracted during brace expansion.
Definition: Parser.h:252

◆ getInputFileNames()

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

Definition at line 155 of file Parser.h.

Referenced by parse().

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

◆ getInputText()

const std::vector<std::string>& Parser::getInputText ( ) const
inline
Returns
The input file contents

Definition at line 160 of file Parser.h.

Referenced by parse().

160 { return _input_text; }
std::vector< std::string > _input_text
The input text (may be filled during parse())
Definition: Parser.h:237

◆ getLastInputFileName()

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

Definition at line 286 of file Parser.C.

Referenced by getLastInputFilePath().

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

◆ getLastInputFilePath()

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

Definition at line 185 of file Parser.h.

Referenced by Moose::Builder::getPrimaryFileName().

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

◆ getRoot()

hit::Node & Parser::getRoot ( )
Returns
The root HIT node with error checking on if it exists

If it doesn't exist, it means we haven't parsed yet

Definition at line 449 of file Parser.C.

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

450 {
451  if (!queryRoot())
452  mooseError("Parser::getRoot(): root is not set");
453  return *queryRoot();
454 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:333
const hit::Node * queryRoot() const
Definition: Parser.h:121

◆ getThrowOnError()

bool Parser::getThrowOnError ( ) const
inline
Returns
Whether or not to throw Parse::Error on errors

This is used by the MooseServer to capture errors while retaining the root if possible

Definition at line 199 of file Parser.h.

Referenced by Moose::Builder::errorCheck().

199 { return _throw_on_error; }
bool _throw_on_error
Whether or not to throw on error.
Definition: Parser.h:246

◆ joinErrorMessages()

std::string Parser::joinErrorMessages ( const std::vector< hit::ErrorMessage > &  error_messages)
static

Helper for combining error messages into a single, newline separated message.

Definition at line 484 of file Parser.C.

Referenced by Moose::Builder::errorCheck(), and parseError().

485 {
486  std::vector<std::string> values;
487  for (const auto & em : error_messages)
488  values.push_back(em.prefixed_message);
489  return MooseUtils::stringJoin(values, "\n");
490 }
std::string stringJoin(const std::vector< std::string > &values, const std::string &separator=" ")
Concatenates value into a single string separated by separator.
Definition: MooseUtils.C:1050

◆ parse()

void Parser::parse ( )

Parses the inputs.

Definition at line 299 of file Parser.C.

300 {
301  mooseAssert(!_root && !_cli_root, "Has already parsed");
302 
303  if (getInputFileNames().size() > 1)
304  mooseInfo("Merging inputs ", Moose::stringify(getInputFileNames()));
305 
306  // Correct filenames (default is to use real path)
307  const std::string use_rel_paths_str =
308  std::getenv("MOOSE_RELATIVE_FILEPATHS") ? std::getenv("MOOSE_RELATIVE_FILEPATHS") : "false";
309  const auto use_real_paths = use_rel_paths_str == "0" || use_rel_paths_str == "false";
310  std::vector<std::string> filenames;
311  for (const auto & filename : getInputFileNames())
312  filenames.push_back(use_real_paths ? MooseUtils::realpath(filename) : filename);
313 
314  // Load each input file if text was not provided
315  if (_input_text.empty())
316  for (const auto & filename : filenames)
317  {
318  MooseUtils::checkFileReadable(filename, true);
319  std::ifstream f(filename);
320  _input_text.push_back(
321  std::string((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>()));
322  }
323 
324  CompileParamWalker::ParamMap override_map;
325  CompileParamWalker cpw(override_map);
326  OverrideParamWalker opw(override_map);
327 
328  // Errors from the duplicate param walker, ran within each input
329  // independently first
330  std::vector<hit::ErrorMessage> dw_errors;
331 
332  for (const auto i : index_range(getInputFileNames()))
333  {
334  const auto & filename = filenames[i];
335  const auto & input = getInputText()[i];
336 
337  try
338  {
339  // provide stream to hit parse function to capture any syntax errors,
340  // set parser root node, then throw those errors if any were captured
341  std::vector<hit::ErrorMessage> syntax_errors;
342  std::unique_ptr<hit::Node> root(hit::parse(filename, input, &syntax_errors));
343 
344  DupParamWalker dw;
345  root->walk(&dw, hit::NodeType::Field);
346  appendErrorMessages(dw_errors, dw.errors);
347 
348  if (!queryRoot())
349  _root = std::move(root);
350  else
351  {
352  root->walk(&opw, hit::NodeType::Field);
353  hit::merge(root.get(), &getRoot());
354  }
355 
356  if (!syntax_errors.empty())
357  throw Parser::Error(syntax_errors);
358 
359  getRoot().walk(&cpw, hit::NodeType::Field);
360  }
361  catch (hit::Error & err)
362  {
363  parseError(err.error_messages);
364  }
365  }
366 
367  // warn about overridden parameters in multiple inputs
368  if (!opw.warnings.empty())
369  mooseInfo(Moose::stringify(opw.warnings), "\n");
370 
371  // If we don't have a root (allow no input files),
372  // create an empty one
373  if (!queryRoot())
374  _root.reset(hit::parse("EMPTY", ""));
375 
376  {
377  BadActiveWalker bw;
378  getRoot().walk(&bw, hit::NodeType::Section);
379  if (bw.errors.size())
380  parseError(bw.errors);
381  }
382 
383  {
384  FindAppWalker fw;
385  getRoot().walk(&fw, hit::NodeType::Field);
386  if (fw.getApp())
387  setAppType(*fw.getApp());
388  }
389 
390  // Duplicate parameter errors (within each input file)
391  if (dw_errors.size())
392  parseError(dw_errors);
393 
394  // Merge in command line HIT arguments
395  const auto joined_params =
397  try
398  {
399  _cli_root.reset(hit::parse("CLI_ARGS", joined_params));
401  }
402  catch (hit::Error & err)
403  {
404  parseError(err.error_messages);
405  }
406 
407  std::vector<hit::ErrorMessage> errors;
408 
409  // expand ${bla} parameter values and mark/include variables
410  // used in expansion as "used" (obtained later by the Builder
411  // with getExtractedVars())
412  {
413  hit::RawEvaler raw;
414  hit::EnvEvaler env;
415  hit::ReplaceEvaler repl;
416  FuncParseEvaler fparse_ev;
417  UnitsConversionEvaler units_ev;
418  hit::BraceExpander exw;
419  exw.registerEvaler("raw", raw);
420  exw.registerEvaler("env", env);
421  exw.registerEvaler("fparse", fparse_ev);
422  exw.registerEvaler("replace", repl);
423  exw.registerEvaler("units", units_ev);
424  getRoot().walk(&exw);
425  for (auto & var : exw.used)
426  _extracted_vars.insert(var);
427  Parser::appendErrorMessages(errors, exw.errors);
428  }
429 
430  // Collect duplicate parameters now that we've merged inputs
431  {
432  DupParamWalker dw;
433  getRoot().walk(&dw, hit::NodeType::Field);
435  }
436 
437  // Check bad active now that we've merged inputs
438  {
439  BadActiveWalker bw;
440  getRoot().walk(&bw, hit::NodeType::Section);
442  }
443 
444  if (errors.size())
445  parseError(errors);
446 }
OStreamProxy err
void setAppType(const std::string &app_type)
Definition: Parser.h:170
void parseError(std::vector< hit::ErrorMessage > messages) const
Helper for throwing an error with the given messages.
Definition: Parser.C:493
hit::Node & getRoot()
Definition: Parser.C:449
const std::vector< std::string > & getInputFileNames() const
Definition: Parser.h:155
std::string realpath(const std::string &path)
Wrapper around PetscGetRealPath, which is a cross-platform replacement for realpath.
Definition: MooseUtils.C:1231
const hit::Node * queryRoot() const
Definition: Parser.h:121
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
void mooseInfo(Args &&... args)
Emit an informational message with the given stringified, concatenated args.
Definition: MooseError.h:400
std::unique_ptr< hit::Node > _cli_root
The root node for command line hit arguments.
Definition: Parser.h:240
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::vector< std::string > _input_text
The input text (may be filled during parse())
Definition: Parser.h:237
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:64
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
const std::optional< std::string > & getApp()
Definition: Parser.C:272
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
const hit::Node & getCommandLineRoot() const
Definition: Parser.C:457
const std::vector< std::string > & getInputText() const
Definition: Parser.h:160
std::vector< hit::ErrorMessage > errors
Definition: Parser.h:54
std::optional< std::vector< std::string > > _command_line_params
The command line HIT parameters (if any)
Definition: Parser.h:249
std::string stringJoin(const std::vector< std::string > &values, const std::string &separator=" ")
Concatenates value into a single string separated by separator.
Definition: MooseUtils.C:1050
auto index_range(const T &sizable)

◆ parseError()

void Parser::parseError ( std::vector< hit::ErrorMessage >  messages) const

Helper for throwing an error with the given messages.

If throwOnError(), throw a Parser::Error (for the MooseServer). Otherwise, use mooseError() (for standard runs).

Definition at line 493 of file Parser.C.

Referenced by Moose::Builder::build(), Moose::Builder::errorCheck(), Moose::Builder::extractParams(), and parse().

494 {
495  // Few things about command line arguments...
496  // 1. We don't care to add line and column context for CLI args, because
497  // it doesn't make sense. We go from the full CLI args and pull out
498  // the HIT parameters so "line" 1 might not even be command line
499  // argument 1. So, remove line/column context from all CLI args.
500  // 2. Whenever we have a parameter in input that then gets overridden
501  // by a command line argument, under the hood we're merging two
502  // different HIT trees. However, WASP doesn't currently update the
503  // "filename" context for the updated parameter. Which means that
504  // a param that is in input and then overridden by CLI will have
505  // its location as in input. Which isn't true. So we get around this
506  // by searching the independent CLI args tree for params that we have
507  // errors for. If the associated path is also in CLI args, we manually
508  // set its error to come from CLI args. This should be fixed in
509  // the future with a WASP update.
510  for (auto & em : messages)
511  if (em.node && queryCommandLineRoot())
512  if (getCommandLineRoot().find(em.node->fullpath()))
513  em = hit::ErrorMessage(em.message, "CLI_ARGS");
514 
515  if (_throw_on_error)
516  throw Parser::Error(messages);
517  else
518  mooseError(joinErrorMessages(messages));
519 }
const hit::Node * queryCommandLineRoot() const
Definition: Parser.h:138
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:333
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
bool _throw_on_error
Whether or not to throw on error.
Definition: Parser.h:246
const hit::Node & getCommandLineRoot() const
Definition: Parser.C:457

◆ queryCommandLineRoot() [1/2]

const hit::Node* Parser::queryCommandLineRoot ( ) const
inline
Returns
The root command line HIT node if it exists

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

Definition at line 138 of file Parser.h.

Referenced by getCommandLineRoot(), and parseError().

138 { return _cli_root.get(); }
std::unique_ptr< hit::Node > _cli_root
The root node for command line hit arguments.
Definition: Parser.h:240

◆ queryCommandLineRoot() [2/2]

hit::Node* Parser::queryCommandLineRoot ( )
inline

Definition at line 139 of file Parser.h.

139 { return _cli_root.get(); }
std::unique_ptr< hit::Node > _cli_root
The root node for command line hit arguments.
Definition: Parser.h:240

◆ queryRoot() [1/2]

const hit::Node* Parser::queryRoot ( ) const
inline
Returns
The root HIT node if it exists

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

Definition at line 121 of file Parser.h.

Referenced by getRoot(), and parse().

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

◆ queryRoot() [2/2]

hit::Node* Parser::queryRoot ( )
inline

Definition at line 122 of file Parser.h.

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

◆ setAppType()

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

Definition at line 170 of file Parser.h.

Referenced by parse().

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

◆ setCommandLineParams()

void Parser::setCommandLineParams ( const std::vector< std::string > &  params)

Sets the HIT parameters from the command line.

Definition at line 279 of file Parser.C.

280 {
281  mooseAssert(!_command_line_params, "Already set");
282  _command_line_params = params;
283 }
std::optional< std::vector< std::string > > _command_line_params
The command line HIT parameters (if any)
Definition: Parser.h:249

◆ setThrowOnError()

void Parser::setThrowOnError ( const bool  throw_on_error)
inline

Set whether or not to throw Parse::Error on errors.

This is used by the MooseServer to capture errors while retaining the root if possible

Definition at line 192 of file Parser.h.

192 { _throw_on_error = throw_on_error; }
bool _throw_on_error
Whether or not to throw on error.
Definition: Parser.h:246

Member Data Documentation

◆ _app_type

std::string Parser::_app_type
private

The application types extracted from [Application] block.

Definition at line 243 of file Parser.h.

Referenced by getAppType(), and setAppType().

◆ _cli_root

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

The root node for command line hit arguments.

Definition at line 240 of file Parser.h.

Referenced by parse(), and queryCommandLineRoot().

◆ _command_line_params

std::optional<std::vector<std::string> > Parser::_command_line_params
private

The command line HIT parameters (if any)

Definition at line 249 of file Parser.h.

Referenced by parse(), and setCommandLineParams().

◆ _extracted_vars

std::set<std::string> Parser::_extracted_vars
private

Variables that have been extracted during brace expansion.

Definition at line 252 of file Parser.h.

Referenced by getExtractedVars(), and parse().

◆ _input_filenames

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

The input file names.

Definition at line 234 of file Parser.h.

Referenced by getInputFileNames(), getLastInputFileName(), and Parser().

◆ _input_text

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

The input text (may be filled during parse())

Definition at line 237 of file Parser.h.

Referenced by getInputText(), and parse().

◆ _root

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

The root node, which owns the whole tree.

Definition at line 231 of file Parser.h.

Referenced by parse(), and queryRoot().

◆ _throw_on_error

bool Parser::_throw_on_error
private

Whether or not to throw on error.

Definition at line 246 of file Parser.h.

Referenced by getThrowOnError(), parseError(), and setThrowOnError().


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