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-2021 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
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
26Note
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
38namespace Foam
39{
40
42
43// Maximum depth for recursive variable names
44static constexpr label maxRecursionDepth_ = 100;
45
46
47static 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 tok(is);
161
162 List<string> list;
163
164 if (tok.isLabel() || tok.isPunctuation(token::BEGIN_LIST))
165 {
166 // A list of strings
167 is.rewind();
168 is >> list;
169 }
170 else if (tok.isString())
171 {
172 // A single string
173 list.resize(1);
174 list[0] = tok.stringToken();
175 }
176 else
177 {
179 << " Entry '"<< keyword
180 << "' not a string or list of strings" << nl
181 << exit(FatalIOError);
182
183 return result;
184 }
185
186 // Check for excess tokens
187 dict.checkITstream(is, keyword);
188
189 // Expand List<string> to List<expressions::exprString>
190 return expandExprStrings(list, dict, mandatory, recursionDepth);
191}
192
193
194// ************************************************************************* //
An input stream of tokens.
Definition: ITstream.H:56
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: ITstream.C:561
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:70
virtual ITstream & stream() const =0
Return token stream, if entry is a primitive entry.
A token holds an item read from Istream.
Definition: token.H:69
bool isPunctuation() const noexcept
Token is PUNCTUATION.
Definition: tokenI.H:459
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:497
const string & stringToken() const
Return const reference to the string contents.
Definition: tokenI.H:689
bool isString() const noexcept
Token is string-variant (STRING, EXPRESSION, VARIABLE, VERBATIM)
Definition: tokenI.H:653
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
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
string trim(const std::string &s)
Return string trimmed of leading and trailing whitespace.
Definition: stringOps.C:1046
Namespace for OpenFOAM.
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:55
IOerror FatalIOError
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict