exprTools.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2019-2022 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Note
27  Ideas based on swak4Foam driver code (2010-2018)
28  from Bernhard Gschaider <bgschaid@hfd-research.com>
29 
30 \*---------------------------------------------------------------------------*/
31 
32 #include "exprTools.H"
33 #include "stringOps.H"
34 #include "DynamicList.H"
35 
36 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
37 
38 namespace Foam
39 {
40 
42 
43 // Maximum depth for recursive variable names
44 static constexpr label maxRecursionDepth_ = 100;
45 
46 
47 static List<expressions::exprString> expandExprStrings
48 (
49  const UList<string>& inputs,
50  const dictionary& dict,
51  bool mandatory,
52  label recursionDepth
53 )
54 {
56 
57  DynamicList<expressions::exprString> result;
58 
59  for (const string& input : inputs)
60  {
61  // Allow inline list of semicolon-separated variables
62  const auto varExpressions = stringOps::split<string>(input, ';');
63 
64  for (const auto& subMatch : varExpressions)
65  {
66  string varExpr(stringOps::trim(subMatch.str()));
67 
68  if (varExpr.empty())
69  {
70  continue;
71  }
72 
74 
75  // Expand #otherVariable as dictionary lookup
76  if (varExpr[0] == '#')
77  {
79 
80  List<expressions::exprString> expansions
81  (
83  (
84  varExpr.substr(1),
85  dict,
86  mandatory,
87  recursionDepth
88  )
89  );
90 
93 
94  result.reserve(result.size() + expansions.size());
95  for (expressions::exprString& str : expansions)
96  {
97  result.append(std::move(str));
98  }
99  }
100  else
101  {
102  result.append
103  (
104  expressions::exprString::toExpr(std::move(varExpr), dict)
105  );
106  }
107  }
108  }
109 
110  result.shrink();
111  return result;
112 }
113 
115 
116 } // End namespace Foam
117 
118 
119 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
120 
123 (
124  const word& keyword,
125  const dictionary& dict,
126  bool mandatory,
127  label recursionDepth
128 )
129 {
131 
132  // Catch empty keyword as a no-op (eg, when called recursively)
133  if (keyword.empty())
134  {
135  return result;
136  }
137 
138  const entry* eptr = dict.findEntry(keyword, keyType::LITERAL_RECURSIVE);
139 
140  if (!eptr)
141  {
142  if (mandatory)
143  {
145  << "Missing mandatory entry: " << keyword << nl << nl
146  << exit(FatalIOError);
147  }
148 
149  return result;
150  }
151 
152  if (++recursionDepth > maxRecursionDepth_)
153  {
155  << "Exceeded recursion depth (" << maxRecursionDepth_
156  << ") while reading list " << keyword << nl
157  << "Likely caused by circular referencing" << nl
158  << exit(FatalIOError);
159  }
160 
161 
162  ITstream& is = eptr->stream();
163  token tok(is);
164 
165  List<string> list;
166 
167  if (tok.isLabel() || tok.isPunctuation(token::BEGIN_LIST))
168  {
169  // A list of strings
170  is.rewind();
171  is >> list;
172  }
173  else if (tok.isString())
174  {
175  // A single string
176  list.resize(1);
177  list[0] = tok.stringToken();
178  }
179  else
180  {
182  << " Entry '"<< keyword
183  << "' not a string or list of strings" << nl
184  << exit(FatalIOError);
185 
186  return result;
187  }
188 
189  // Check for excess tokens
190  dict.checkITstream(is, keyword);
191 
192  // Expand List<string> to List<expressions::exprString>
193  return expandExprStrings(list, dict, mandatory, recursionDepth);
194 }
195 
196 
197 // ************************************************************************* //
dictionary dict
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
List< expressions::exprString > getList(const word &keyword, const dictionary &dict, bool mandatory=true, label recursionDepth=0)
Get an expression string list from a dictionary.
Definition: exprTools.C:116
A class for handling words, derived from Foam::string.
Definition: word.H:63
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:33
string trim(const std::string &s)
Return string trimmed of leading and trailing whitespace.
Definition: stringOps.C:1033
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
Namespace for OpenFOAM.
static exprString toExpr(const std::string &str)
Copy convert string to exprString.
Definition: exprStringI.H:111
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...