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 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  dict,
85  varExpr.substr(1),
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(expressions::exprString(varExpr, dict));
103  }
104  }
105  }
106 
107  result.shrink();
108  return result;
109 }
110 
112 
113 } // End namespace Foam
114 
115 
116 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
117 
120 (
121  const dictionary& dict,
122  const word& keyword,
123  bool mandatory,
124  label recursionDepth
125 )
126 {
128 
129  // Catch empty keyword as a no-op (eg, when called recursively)
130  if (keyword.empty())
131  {
132  return result;
133  }
134 
135  const entry* eptr = dict.findEntry(keyword, keyType::LITERAL_RECURSIVE);
136 
137  if (!eptr)
138  {
139  if (mandatory)
140  {
142  << "Missing mandatory entry: " << keyword << nl << nl
143  << exit(FatalIOError);
144  }
145 
146  return result;
147  }
148 
149  if (++recursionDepth > maxRecursionDepth_)
150  {
152  << "Exceeded recursion depth (" << maxRecursionDepth_
153  << ") while reading list " << keyword << nl
154  << "Likely caused by circular referencing" << nl
155  << exit(FatalIOError);
156  }
157 
158 
159  ITstream& is = eptr->stream();
160  token firstToken(is);
161 
162  List<string> list;
163 
164  if
165  (
166  firstToken.isLabel()
167  ||
168  (
169  firstToken.type() == token::PUNCTUATION
170  && firstToken.pToken() == token::BEGIN_LIST
171  )
172  )
173  {
174  // A list of strings
175  is.rewind();
176  is >> list;
177  }
178  else if (firstToken.isString())
179  {
180  // A single string
181  list.resize(1);
182  list[0] = firstToken.stringToken();
183  }
184  else
185  {
187  << " Entry '"<< keyword
188  << "' not a string or list of strings" << nl
189  << exit(FatalError);
190 
191  return result;
192  }
193 
194  // Check for excess tokens
195  dict.checkITstream(is, keyword);
196 
197  // Expand List<string> to List<expressions::exprString>
198  return expandExprStrings(list, dict, mandatory, recursionDepth);
199 }
200 
201 
202 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
Foam::ITstream::rewind
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: ITstream.C:356
Foam::token::isLabel
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:481
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::entry::stream
virtual ITstream & stream() const =0
Return token stream, if entry is a primitive entry.
Foam::token::stringToken
const string & stringToken() const
Return const reference to the string contents.
Definition: tokenI.H:644
Foam::stringOps::trim
string trim(const std::string &s)
Return string trimmed of leading and trailing whitespace.
Definition: stringOps.C:1048
Foam::FatalIOError
IOerror FatalIOError
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::token::pToken
punctuationToken pToken() const
Return punctuation character.
Definition: tokenI.H:459
Foam::ITstream
An input stream of tokens.
Definition: ITstream.H:55
Foam::List::resize
void resize(const label newSize)
Adjust allocated size of list.
Definition: ListI.H:139
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
exprTools.H
Foam::token::type
tokenType type() const noexcept
Return the token type.
Definition: tokenI.H:309
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::List< Foam::expressions::exprString >
Foam::token::isString
bool isString() const noexcept
Token is STRING, VARIABLE or VERBATIM string.
Definition: tokenI.H:615
Foam::input
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:55
Foam::exprTools::getList
List< expressions::exprString > getList(const dictionary &dict, const word &keyword, bool mandatory=true, label recursionDepth=0)
Get an expression string list from a dictionary.
Definition: exprTools.C:120
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
DynamicList.H
stringOps.H