<commit_before>#include <string>
#include <map>
#include <fstream>
#include <iomanip>

#include "MooseSystem.h"
#include "Parser.h"
#include "getpot.h"
#include "InputParameters.h"
#include "ParserBlockFactory.h"
#include "KernelFactory.h"
#include "AuxFactory.h"
#include "BCFactory.h"
#include "MaterialFactory.h"
#include "InitialConditionFactory.h"
#include "ExecutionerFactory.h"

// Static Data initialization
const std::string Parser::_show_tree = "--show_tree";

 
Parser::Parser(MooseSystem & moose_system, const std::string &dump_string)
  :_moose_system(moose_system),
   _input_filename(""),
   _input_tree(NULL),
   _getpot_initialized(false),
   _tree_printed(false),
   _dump_string(dump_string)
{
  if (Moose::command_line != NULL)
  {
    if (Moose::command_line->search("-h") || Moose::command_line->search("--help")) 
    {
      printUsage();
      exit(0);
    }
    if (Moose::command_line->search(dump_string))
    {
      buildFullTree();
      exit(0);
    }
  }
  else
    printUsage();
}

Parser::~Parser() 
{
  if (_input_tree != NULL) 
  {
    delete (_input_tree);
  }
}

void
Parser::parse(const std::string &input_filename)
{
  std::vector<std::string> elements;
  std::string curr_value, curr_identifier;
  ParserBlock *curr_block, *t;

  _input_filename = input_filename;
  
  checkInputFile();
  
  // GetPot object
  _getpot_file.parse_input_file(_input_filename);
  _getpot_initialized = true;
  
  InputParameters root_params = validParams<ParserBlock>();
  root_params.set<ParserBlock *>("parent") = NULL;
  root_params.set<Parser *>("parser_handle") = this;
  _input_tree = new ParserBlock("/", _moose_system, root_params);
  
  _section_names = _getpot_file.get_section_names();

  /**
   * The variable_names function returns section names and variable names in
   * one large string so we can build the data structure in one pass.
   * (.i.e) /Variables/temp/InitialCondition will be split on slashes and
   * a tree of three levels will be build for each remaining piece of text
   */
  for (std::vector<std::string>::iterator i=_section_names.begin(); i != _section_names.end(); ++i) 
  {
    elements.clear();
    tokenize(*i, elements);
    curr_block = _input_tree;
    curr_identifier = "";

    for (int j=0; j<elements.size(); ++j) 
    {
      if (! curr_block->checkActive(elements[j]) )
        continue;
      
      // Add slashes as seperators back into our text strings as we build them up but don't
      // start with an initial slash
      if (j)
        curr_identifier += "/";
      curr_identifier += elements[j];

      std::vector<ParserBlock *>::reverse_iterator last = curr_block->_children.rbegin();
      // See if this block is already in the tree from a previous section string iteration
      if (last == curr_block->_children.rend() || (*last)->getID() != curr_identifier) 
      {
        InputParameters params = ParserBlockFactory::instance()->getValidParams(curr_identifier);
        params.set<ParserBlock *>("parent") = curr_block;
        params.set<Parser *>("parser_handle") = this;

        curr_block->_children.push_back(ParserBlockFactory::instance()->add(curr_identifier, _moose_system, params));          
      }

      curr_block = *(curr_block->_children.rbegin());
    }

    // Extract all the requested parameters from the input file
    extractParams(curr_block->getID(), curr_block->getBlockParams());
    extractParams(curr_block->getID(), curr_block->getClassParams());
  }

  fixupOptionalBlocks();

  if (!_tree_printed) 
  {
#ifdef DEBUG
    _input_tree->printBlockData();
    _tree_printed = true;
#else
    if (Moose::command_line && Moose::command_line->search("--show-tree")) 
    {
      _input_tree->printBlockData();
      _tree_printed = true;
    }
#endif
  }
}

void
Parser::buildFullTree()
{
  std::string prefix;
  ParserBlock *curr_block;

  // Build the root of the tree
  InputParameters params = validParams<ParserBlock>();
  params.set<ParserBlock *>("parent") = NULL;
  params.set<Parser *>("parser_handle") = this;
  _input_tree = new ParserBlock("/", _moose_system, params);
  
  curr_block = _input_tree;

  // Build all the ParserBlock Types
  params.set<ParserBlock *>("parent") = curr_block;
  for (ParserBlockNamesIterator i = ParserBlockFactory::instance()->registeredParserBlocksBegin();
       i != ParserBlockFactory::instance()->registeredParserBlocksEnd(); ++i)
  {
    // skip the generic matches here because they don't correlate to a real instance and well pick them up manually below
    if (i->find('*') != std::string::npos || i->find("Executioner") != std::string::npos)
      continue;

    // std::string matched_identifier = ParserBlockFactory::instance()->isRegistered(*i);
    curr_block->_children.push_back(ParserBlockFactory::instance()->add(*i, _moose_system, params));
  }
  
  {
    // Manually add a sample variable
    prefix = "Variables/";
    std::string var_name("SampleVar");
    curr_block = curr_block->locateBlock("Variables");
    mooseAssert(curr_block != NULL, "A Variables ParserBlock does not appear to exist");
    
    params = ParserBlockFactory::instance()->getValidParams(prefix + var_name);
    params.set<ParserBlock *>("parent") = curr_block;
    params.set<Parser *>("parser_handle") = this;
    curr_block->_children.push_back(ParserBlockFactory::instance()->add(prefix + var_name, _moose_system, params));
    
    // Add all the IC Blocks<commit_msg>fixing bug that prevent some parser blocks to not display parameters during --dump<commit_after>#include <string>
#include <map>
#include <fstream>
#include <iomanip>

#include "MooseSystem.h"
#include "Parser.h"
#include "getpot.h"
#include "InputParameters.h"
#include "ParserBlockFactory.h"
#include "KernelFactory.h"
#include "AuxFactory.h"
#include "BCFactory.h"
#include "MaterialFactory.h"
#include "InitialConditionFactory.h"
#include "ExecutionerFactory.h"

// Static Data initialization
const std::string Parser::_show_tree = "--show_tree";

 
Parser::Parser(MooseSystem & moose_system, const std::string &dump_string)
  :_moose_system(moose_system),
   _input_filename(""),
   _input_tree(NULL),
   _getpot_initialized(false),
   _tree_printed(false),
   _dump_string(dump_string)
{
  if (Moose::command_line != NULL)
  {
    if (Moose::command_line->search("-h") || Moose::command_line->search("--help")) 
    {
      printUsage();
      exit(0);
    }
    if (Moose::command_line->search(dump_string))
    {
      buildFullTree();
      exit(0);
    }
  }
  else
    printUsage();
}

Parser::~Parser() 
{
  if (_input_tree != NULL) 
  {
    delete (_input_tree);
  }
}

void
Parser::parse(const std::string &input_filename)
{
  std::vector<std::string> elements;
  std::string curr_value, curr_identifier;
  ParserBlock *curr_block, *t;

  _input_filename = input_filename;
  
  checkInputFile();
  
  // GetPot object
  _getpot_file.parse_input_file(_input_filename);
  _getpot_initialized = true;
  
  InputParameters root_params = validParams<ParserBlock>();
  root_params.set<ParserBlock *>("parent") = NULL;
  root_params.set<Parser *>("parser_handle") = this;
  _input_tree = new ParserBlock("/", _moose_system, root_params);
  
  _section_names = _getpot_file.get_section_names();

  /**
   * The variable_names function returns section names and variable names in
   * one large string so we can build the data structure in one pass.
   * (.i.e) /Variables/temp/InitialCondition will be split on slashes and
   * a tree of three levels will be build for each remaining piece of text
   */
  for (std::vector<std::string>::iterator i=_section_names.begin(); i != _section_names.end(); ++i) 
  {
    elements.clear();
    tokenize(*i, elements);
    curr_block = _input_tree;
    curr_identifier = "";

    for (int j=0; j<elements.size(); ++j) 
    {
      if (! curr_block->checkActive(elements[j]) )
        continue;
      
      // Add slashes as seperators back into our text strings as we build them up but don't
      // start with an initial slash
      if (j)
        curr_identifier += "/";
      curr_identifier += elements[j];

      std::vector<ParserBlock *>::reverse_iterator last = curr_block->_children.rbegin();
      // See if this block is already in the tree from a previous section string iteration
      if (last == curr_block->_children.rend() || (*last)->getID() != curr_identifier) 
      {
        InputParameters params = ParserBlockFactory::instance()->getValidParams(curr_identifier);
        params.set<ParserBlock *>("parent") = curr_block;
        params.set<Parser *>("parser_handle") = this;

        curr_block->_children.push_back(ParserBlockFactory::instance()->add(curr_identifier, _moose_system, params));          
      }

      curr_block = *(curr_block->_children.rbegin());
    }

    // Extract all the requested parameters from the input file
    extractParams(curr_block->getID(), curr_block->getBlockParams());
    extractParams(curr_block->getID(), curr_block->getClassParams());
  }

  fixupOptionalBlocks();

  if (!_tree_printed) 
  {
#ifdef DEBUG
    _input_tree->printBlockData();
    _tree_printed = true;
#else
    if (Moose::command_line && Moose::command_line->search("--show-tree")) 
    {
      _input_tree->printBlockData();
      _tree_printed = true;
    }
#endif
  }
}

void
Parser::buildFullTree()
{
  std::string prefix;
  ParserBlock *curr_block;

  // Build the root of the tree
  InputParameters params = validParams<ParserBlock>();
  params.set<ParserBlock *>("parent") = NULL;
  params.set<Parser *>("parser_handle") = this;
  _input_tree = new ParserBlock("/", _moose_system, params);
  
  curr_block = _input_tree;

  // Build all the base level ParserBlock types that are not generic
  for (ParserBlockNamesIterator i = ParserBlockFactory::instance()->registeredParserBlocksBegin();
       i != ParserBlockFactory::instance()->registeredParserBlocksEnd(); ++i)
  {
    // skip the generic matches here because they don't correlate to a real instance and well pick them up manually below
    if (i->find('*') != std::string::npos || i->find("Executioner") != std::string::npos)
      continue;

    // std::string matched_identifier = ParserBlockFactory::instance()->isRegistered(*i);
    params = ParserBlockFactory::instance()->getValidParams(*i);
    params.set<ParserBlock *>("parent") = curr_block;
    params.set<Parser *>("parser_handle") = this;
    curr_block->_children.push_back(ParserBlockFactory::instance()->add(*i, _moose_system, params));
  }
  
  {
    // Manually add a sample variable
    prefix = "Variables/";
    std::string var_name("SampleVar");
    curr_block = curr_block->locateBlock("Variables");
    mooseAssert(curr_block != NULL, "A Variables ParserBlock does not appear to exist");
    
    params = ParserBlockFactory::instance()->getValidParams(prefix + var_name);
    params.set<ParserBlock *>("parent") = curr_block;
    params.set<Parser *>("parser_handle") = this;
    curr_block->_children.push_back(ParserBlockFactory::instance()->add(prefix + var_name, _moose_system, params));
    
    // Add all the IC Blocks
{'fraction_non_alphanumeric': 0.13734874480765757, 'fraction_numerical': 0.000541809644211667, 'mean_word_length': 4.043260473588343, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 99, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7222435', 'n_tokens_mistral': 3446, 'n_tokens_neox': 3424, 'n_words': 774}