LCOV - code coverage report
Current view: top level - include/parser - Parser.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: fef103 Lines: 13 13 100.0 %
Date: 2025-09-03 20:01:23 Functions: 13 13 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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       68513 :   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:
      67             :   ParamMap & _map;
      68             : };
      69             : 
      70             : class OverrideParamWalker : public hit::Walker
      71             : {
      72             : public:
      73       68513 :   OverrideParamWalker(const CompileParamWalker::ParamMap & map) : _map(map) {}
      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:
      79             :   const CompileParamWalker::ParamMap & _map;
      80             : };
      81             : 
      82             : /**
      83             :  * Class for parsing input files. This class utilizes the GetPot library for actually tokenizing and
      84             :  * parsing files. It is not currently designed for extensibility. If you wish to build your own
      85             :  * parser, please contact the MOOSE team for guidance.
      86             :  */
      87             : class Parser
      88             : {
      89             : public:
      90             :   /**
      91             :    * Constructor given a list of input files, given in \p input_filenames.
      92             :    *
      93             :    * Optionally, the file contents can be provided via text in \p input_text.
      94             :    */
      95             :   Parser(const std::vector<std::string> & input_filenames,
      96             :          const std::optional<std::vector<std::string>> & input_text = {});
      97             :   /**
      98             :    * Constructor, given a file in \p input_filename.
      99             :    *
     100             :    * Optionally, the file contents can be provided via text in \p input_text.
     101             :    */
     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             : 
     110             :   /**
     111             :    * Parses the inputs
     112             :    */
     113             :   void parse();
     114             : 
     115             :   /**
     116             :    * @return The root HIT node if it exists
     117             :    *
     118             :    * If this is null, it means we haven't parsed yet
     119             :    */
     120             :   ///@{
     121          27 :   const hit::Node * queryRoot() const { return _root.get(); }
     122     4277757 :   hit::Node * queryRoot() { return _root.get(); }
     123             :   ///@}
     124             : 
     125             :   /**
     126             :    * @return The root HIT node with error checking on if it exists
     127             :    *
     128             :    * If it doesn't exist, it means we haven't parsed yet
     129             :    */
     130             :   hit::Node & getRoot();
     131             : 
     132             :   /**
     133             :    * @return The root command line HIT node if it exists
     134             :    *
     135             :    * If this is null, it means we haven't parsed yet
     136             :    */
     137             :   ///@{
     138      262917 :   const hit::Node * queryCommandLineRoot() const { return _cli_root.get(); }
     139             :   hit::Node * queryCommandLineRoot() { return _cli_root.get(); }
     140             :   ///@}
     141             : 
     142             :   /**
     143             :    * @return The root command line HIT node, with error checking on if it exists
     144             :    *
     145             :    * If it doesn't exist, it means we haven't parsed yet
     146             :    */
     147             :   ///@{
     148             :   const hit::Node & getCommandLineRoot() const;
     149             :   hit::Node & getCommandLineRoot();
     150             :   ///@}
     151             : 
     152             :   /**
     153             :    * @return The names of the inputs
     154             :    */
     155      363967 :   const std::vector<std::string> & getInputFileNames() const { return _input_filenames; }
     156             : 
     157             :   /**
     158             :    * @return The input file contents
     159             :    */
     160       68188 :   const std::vector<std::string> & getInputText() const { return _input_text; }
     161             : 
     162             :   /*
     163             :    * Get extracted application type from parser
     164             :    */
     165      192959 :   const std::string & getAppType() const { return _app_type; }
     166             : 
     167             :   /*
     168             :    * Set the application type in parser
     169             :    */
     170       68530 :   void setAppType(const std::string & app_type) { _app_type = app_type; }
     171             : 
     172             :   /**
     173             :    * Sets the HIT parameters from the command line
     174             :    */
     175             :   void setCommandLineParams(const std::vector<std::string> & params);
     176             : 
     177             :   /**
     178             :    * @return The file name of the last input
     179             :    */
     180             :   const std::string & getLastInputFileName() const;
     181             : 
     182             :   /**
     183             :    * @return The path of the last input
     184             :    */
     185        2445 :   std::filesystem::path getLastInputFilePath() const { return getLastInputFileName(); }
     186             : 
     187             :   /**
     188             :    * Set whether or not to throw Parse::Error on errors
     189             :    *
     190             :    * This is used by the MooseServer to capture errors while retaining the root if possible
     191             :    */
     192           7 :   void setThrowOnError(const bool throw_on_error) { _throw_on_error = throw_on_error; }
     193             : 
     194             :   /**
     195             :    * @return Whether or not to throw Parse::Error on errors
     196             :    *
     197             :    * This is used by the MooseServer to capture errors while retaining the root if possible
     198             :    */
     199          39 :   bool getThrowOnError() const { return _throw_on_error; }
     200             : 
     201             :   /**
     202             :    * @returns The variables that have been extracted so far.
     203             :    *
     204             :    * These are the variables that have been used during brace expansion.
     205             :    */
     206       67893 :   const std::set<std::string> & getExtractedVars() const { return _extracted_vars; }
     207             :   /**
     208             :    * Helper for accumulating errors from a walker into an accumulation of errors
     209             :    */
     210             :   ///@{
     211             :   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);
     214             :   ///@}
     215             : 
     216             :   /**
     217             :    * Helper for combining error messages into a single, newline separated message
     218             :    */
     219             :   static std::string joinErrorMessages(const std::vector<hit::ErrorMessage> & error_messages);
     220             : 
     221             :   /**
     222             :    * Helper for throwing an error with the given messages.
     223             :    *
     224             :    * If throwOnError(), throw a Parser::Error (for the MooseServer).
     225             :    * Otherwise, use mooseError() (for standard runs).
     226             :    */
     227             :   void parseError(std::vector<hit::ErrorMessage> messages) const;
     228             : 
     229             : private:
     230             :   /// The root node, which owns the whole tree
     231             :   std::unique_ptr<hit::Node> _root;
     232             : 
     233             :   /// The input file names
     234             :   const std::vector<std::string> _input_filenames;
     235             : 
     236             :   /// The input text (may be filled during parse())
     237             :   std::vector<std::string> _input_text;
     238             : 
     239             :   /// The root node for command line hit arguments
     240             :   std::unique_ptr<hit::Node> _cli_root;
     241             : 
     242             :   /// The application types extracted from [Application] block
     243             :   std::string _app_type;
     244             : 
     245             :   /// Whether or not to throw on error
     246             :   bool _throw_on_error;
     247             : 
     248             :   /// The command line HIT parameters (if any)
     249             :   std::optional<std::vector<std::string>> _command_line_params;
     250             : 
     251             :   /// Variables that have been extracted during brace expansion
     252             :   std::set<std::string> _extracted_vars;
     253             : };

Generated by: LCOV version 1.14