LCOV - code coverage report
Current view: top level - src/utils - SyntaxTree.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 419b9d Lines: 109 110 99.1 %
Date: 2025-08-08 20:01:16 Functions: 13 14 92.9 %
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             : #include "SyntaxTree.h"
      11             : 
      12             : #include "InputParameters.h"
      13             : #include "MooseUtils.h"
      14             : #include "Parser.h"
      15             : 
      16             : #include <algorithm>
      17             : #include <cctype>
      18             : 
      19       69028 : SyntaxTree::SyntaxTree(bool use_long_names)
      20       69028 :   : SyntaxFormatterInterface(), _use_long_names(use_long_names)
      21             : {
      22       69028 : }
      23             : 
      24       69028 : SyntaxTree::~SyntaxTree() = default;
      25             : 
      26             : void
      27     3360571 : SyntaxTree::insertNode(std::string syntax,
      28             :                        const std::string & action,
      29             :                        bool is_action_params,
      30             :                        InputParameters * params)
      31             : {
      32     3360571 :   if (!_root)
      33       34539 :     _root = std::make_unique<TreeNode>("", *this);
      34             : 
      35     3360571 :   _root->insertNode(syntax, action, is_action_params, params);
      36     3360571 : }
      37             : 
      38             : std::string
      39       34539 : SyntaxTree::print(const std::string & search_string)
      40             : {
      41       34539 :   bool found = false;
      42       34539 :   std::string output;
      43             : 
      44             :   // Clear the list of "seen" parameters before printing the tree
      45       34539 :   _params_printed.clear();
      46             : 
      47       34539 :   if (_root)
      48       34539 :     output = _root->print(-1, search_string, found);
      49             : 
      50       34539 :   if (found)
      51       69078 :     return preamble() + output + postscript();
      52             :   else
      53           0 :     return "";
      54       34539 : }
      55             : 
      56             : void
      57    18494715 : SyntaxTree::seenIt(const std::string & prefix, const std::string & item)
      58             : {
      59    18494715 :   _params_printed.insert(prefix + item);
      60    18494715 : }
      61             : 
      62             : bool
      63    67045116 : SyntaxTree::haveSeenIt(const std::string & prefix, const std::string & item) const
      64             : {
      65    67045116 :   return _params_printed.find(prefix + item) != _params_printed.end();
      66             : }
      67             : 
      68      734066 : SyntaxTree::TreeNode::TreeNode(const std::string & name,
      69             :                                SyntaxTree & syntax_tree,
      70             :                                const std::string * action,
      71             :                                InputParameters * params,
      72      734066 :                                TreeNode * parent)
      73      734066 :   : _name(name), _parent(parent), _syntax_tree(syntax_tree)
      74             : {
      75      734066 :   if (action)
      76      495628 :     _action_params.emplace(*action, std::make_unique<InputParameters>(*params));
      77      734066 : }
      78             : 
      79      734066 : SyntaxTree::TreeNode::~TreeNode() = default;
      80             : 
      81             : void
      82     4304478 : SyntaxTree::TreeNode::insertNode(std::string & syntax,
      83             :                                  const std::string & action,
      84             :                                  bool is_action_params,
      85             :                                  InputParameters * params)
      86             : {
      87     4304478 :   std::string::size_type pos = syntax.find_first_of("/");
      88     4304478 :   std::string item;
      89     4304478 :   bool is_leaf = true;
      90             : 
      91     4304478 :   item = syntax.substr(0, pos);
      92     4304478 :   if (pos != std::string::npos)
      93             :   {
      94      943907 :     syntax = syntax.substr(pos + 1);
      95      943907 :     is_leaf = false;
      96             :   }
      97             : 
      98     4304478 :   bool node_created = false;
      99     4304478 :   if (_children.find(item) == _children.end())
     100             :   {
     101     1399054 :     _children[item] = std::make_unique<TreeNode>(
     102     1399054 :         item, _syntax_tree, is_leaf && is_action_params ? &action : NULL, params, this);
     103      699527 :     if (is_leaf && !is_action_params)
     104       44586 :       _children[item]->insertParams(action, is_action_params, params);
     105      699527 :     node_created = true;
     106             :   }
     107             : 
     108     4304478 :   if (!is_leaf)
     109      943907 :     _children[item]->insertNode(syntax, action, is_action_params, params);
     110     3360571 :   else if (!node_created)
     111     2820357 :     _children[item]->insertParams(action, is_action_params, params);
     112     4304478 : }
     113             : 
     114             : void
     115     2864943 : SyntaxTree::TreeNode::insertParams(const std::string & action,
     116             :                                    bool is_action_params,
     117             :                                    InputParameters * params)
     118             : {
     119     2864943 :   if (is_action_params)
     120     2193701 :     _action_params.emplace(action, std::make_unique<InputParameters>(*params));
     121             :   else
     122      671242 :     _moose_object_params.emplace(action, std::make_unique<InputParameters>(*params));
     123     2864943 : }
     124             : 
     125             : std::string
     126      794846 : SyntaxTree::TreeNode::print(short depth, const std::string & search_string, bool & found)
     127             : {
     128      794846 :   std::string doc = "";
     129      794846 :   std::string long_name(getLongName());
     130      794846 :   std::string name(_syntax_tree.isLongNames() ? long_name : _name);
     131      794846 :   std::string out;
     132             : 
     133      794846 :   if (depth < 0)
     134             :   {
     135      341751 :     for (const auto & c_it : _children)
     136             :     {
     137      307212 :       bool local_found = false;
     138      307212 :       std::string local_out(c_it.second->print(depth + 1, search_string, local_found));
     139      307212 :       found |= local_found; // Update the current frame's found variable
     140      307212 :       if (local_found)
     141      306488 :         out += local_out;
     142      307212 :     }
     143       34539 :     return out;
     144             :   }
     145             : 
     146             :   // GlobalParamsAction is special - we need to just always print it out
     147             :   // if (_name == "GlobalParamsAction")
     148             :   //   found = true;
     149             : 
     150      760307 :   std::string indent((depth + 1) * 2, ' ');
     151             : 
     152             :   std::multimap<std::string, std::unique_ptr<InputParameters>>::const_iterator it =
     153      760307 :       _moose_object_params.begin();
     154             :   do
     155             :   {
     156      930739 :     bool local_found = false;
     157      930739 :     std::string local_out;
     158             : 
     159             :     // Compare the block name, if it's matched we are going to pass an empty search string
     160             :     // which means match ALL parameters
     161      930739 :     std::string local_search_string;
     162      930739 :     if (MooseUtils::wildCardMatch(name, search_string))
     163      907620 :       found = true;
     164             :     else
     165       23119 :       local_search_string = search_string;
     166             : 
     167      930739 :     if (it != _moose_object_params.end())
     168      731974 :       doc = it->second->getClassDescription();
     169      930739 :     local_out += _syntax_tree.printBlockOpen(name, depth, doc);
     170             : 
     171     5711178 :     for (const auto & a_it : _action_params)
     172     4780439 :       if (a_it.first != "EmptyAction")
     173             :       {
     174     9560758 :         local_out += _syntax_tree.printParams(
     175     9560758 :             name, long_name, *(a_it.second), depth, local_search_string, local_found);
     176     4780379 :         found |= local_found; // Update the current frame's found variable
     177             :         // DEBUG
     178             :         // Moose::out << "\n" << indent << "(" << ait->first << ")";
     179             :         // DEBUG
     180             :       }
     181             : 
     182      930739 :     if (it != _moose_object_params.end())
     183             :     {
     184     1463948 :       local_out += _syntax_tree.printParams(
     185     1463948 :           name, long_name, *it->second, depth, local_search_string, local_found);
     186      731974 :       found |= local_found;
     187             :       // DEBUG
     188             :       // Moose::out << "\n" << indent << "{" << it->first << "}";
     189             :       // DEBUG
     190             :     }
     191             : 
     192      930739 :     local_out += _syntax_tree.preTraverse(depth);
     193             : 
     194     1383834 :     for (const auto & c_it : _children)
     195             :     {
     196      453095 :       bool child_found = false;
     197      453095 :       std::string child_out(c_it.second->print(depth + 1, local_search_string, child_found));
     198      453095 :       found |= child_found; // Update the current frame's found variable
     199             : 
     200      453095 :       if (child_found)
     201      430700 :         local_out += child_out;
     202      453095 :     }
     203             : 
     204      930739 :     local_out += _syntax_tree.printBlockClose(name, depth);
     205             : 
     206      930739 :     if (found)
     207      907620 :       out += local_out;
     208             : 
     209             :     // If there are no moose object params then we have to be careful about how we
     210             :     // increment the iterator.  We only want to increment if we aren't already
     211             :     // at the end.
     212      930739 :   } while (it != _moose_object_params.end() && ++it != _moose_object_params.end());
     213             : 
     214      760307 :   return out;
     215      794846 : }
     216             : 
     217             : std::string
     218     2035585 : SyntaxTree::TreeNode::getLongName(const std::string & delim) const
     219             : {
     220     2035585 :   if (_parent)
     221     2481478 :     return _parent->getLongName(delim) + delim + _name;
     222             :   else
     223      794846 :     return _name;
     224             : }
     225             : 
     226             : bool
     227      794846 : SyntaxTree::isLongNames() const
     228             : {
     229      794846 :   return _use_long_names;
     230             : }

Generated by: LCOV version 1.14