dictionary: Standardised and centralised the argument list parser

to simplify maintenance
This commit is contained in:
Henry Weller
2020-09-19 15:52:09 +01:00
parent ad33cc2cc8
commit 560db98b34
5 changed files with 108 additions and 156 deletions

View File

@ -276,75 +276,10 @@ void substitute(dictionary& dict, string substitutions)
}
word funcName;
int argLevel = 0;
wordReList args;
List<Tuple2<word, string>> namedArgs;
bool namedArg = false;
word argName;
word::size_type start = 0;
word::size_type i = 0;
for
(
word::const_iterator iter = substitutions.begin();
iter != substitutions.end();
++iter
)
{
char c = *iter;
if (c == '(')
{
if (argLevel == 0)
{
funcName = substitutions(start, i - start);
start = i+1;
}
++argLevel;
}
else if (c == ',' || c == ')')
{
if (argLevel == 1)
{
if (namedArg)
{
namedArgs.append
(
Tuple2<word, string>
(
argName,
substitutions(start, i - start)
)
);
namedArg = false;
}
else
{
FatalIOErrorInFunction(dict)
<< "Unnamed substitution" << exit(FatalIOError);
}
start = i+1;
}
if (c == ')')
{
if (argLevel == 1)
{
break;
}
--argLevel;
}
}
else if (c == '=')
{
argName = substitutions(start, i - start);
string::stripInvalid<variable>(argName);
start = i+1;
namedArg = true;
}
++i;
}
dictArgList(substitutions, funcName, args, namedArgs);
forAll(namedArgs, i)
{

View File

@ -1556,6 +1556,89 @@ Foam::dictionary Foam::operator|
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
void Foam::dictArgList
(
const string& funcArgs,
word& funcName,
wordReList& args,
List<Tuple2<word, string>>& namedArgs
)
{
funcName = funcArgs;
int argLevel = 0;
bool namedArg = false;
word argName;
word::size_type start = 0;
word::size_type i = 0;
for
(
word::const_iterator iter = funcArgs.begin();
iter != funcArgs.end();
++iter
)
{
char c = *iter;
if (c == '(')
{
if (argLevel == 0)
{
funcName = funcArgs(start, i - start);
start = i+1;
}
++argLevel;
}
else if (c == ',' || c == ')')
{
if (argLevel == 1)
{
if (namedArg)
{
namedArgs.append
(
Tuple2<word, string>
(
argName,
funcArgs(start, i - start)
)
);
namedArg = false;
}
else
{
args.append(wordRe(funcArgs(start, i - start)));
}
start = i+1;
}
if (c == ')')
{
if (argLevel == 1)
{
break;
}
--argLevel;
}
}
else if (c == '=')
{
argName = funcArgs(start, i - start);
string::stripInvalid<variable>(argName);
start = i+1;
namedArg = true;
}
++i;
}
// Strip whitespace from the function name
string::stripInvalid<word>(funcName);
}
Foam::Pair<Foam::word> Foam::dictAndKeyword(const word& scopedName)
{
string::size_type i = scopedName.find_last_of

View File

@ -53,12 +53,11 @@ SourceFiles
#include "entry.H"
#include "IDLList.H"
#include "DLList.H"
#include "fileName.H"
#include "ITstream.H"
#include "HashTable.H"
#include "wordList.H"
#include "className.H"
#include "ITstream.H"
#include "wordReList.H"
#include "Pair.H"
#include "className.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -634,6 +633,15 @@ dictionary operator|(const dictionary& dict1, const dictionary& dict2);
// Global Functions
//- Parse dictionary substitution argument list
void dictArgList
(
const string& funcArgs,
word& funcName,
wordReList& args,
List<Tuple2<word, string>>& namedArgs
);
//- Extracts dict name and keyword
Pair<word> dictAndKeyword(const word& scopedName);

View File

@ -24,15 +24,11 @@ License
\*---------------------------------------------------------------------------*/
#include "functionObjectList.H"
#include "Time.H"
#include "mapPolyMesh.H"
#include "argList.H"
#include "timeControlFunctionObject.H"
#include "dictionaryEntry.H"
#include "stringOps.H"
#include "Tuple2.H"
#include "etcFiles.H"
#include "IOdictionary.H"
#include "wordAndDictionary.H"
@ -221,88 +217,18 @@ void Foam::functionObjectList::checkUnsetEntries
bool Foam::functionObjectList::readFunctionObject
(
const string& funcCall,
const string& funcArgs,
dictionary& functionsDict,
const string& context,
HashSet<word>& requiredFields,
const word& region
)
{
word funcName(funcCall);
int argLevel = 0;
word funcName;
wordReList args;
List<Tuple2<word, string>> namedArgs;
bool namedArg = false;
word argName;
word::size_type start = 0;
word::size_type i = 0;
for
(
word::const_iterator iter = funcCall.begin();
iter != funcCall.end();
++iter
)
{
char c = *iter;
if (c == '(')
{
if (argLevel == 0)
{
funcName = funcCall(start, i - start);
start = i+1;
}
++argLevel;
}
else if (c == ',' || c == ')')
{
if (argLevel == 1)
{
if (namedArg)
{
namedArgs.append
(
Tuple2<word, string>
(
argName,
funcCall(start, i - start)
)
);
namedArg = false;
}
else
{
args.append(wordRe(funcCall(start, i - start)));
}
start = i+1;
}
if (c == ')')
{
if (argLevel == 1)
{
break;
}
--argLevel;
}
}
else if (c == '=')
{
argName = funcCall(start, i - start);
string::stripInvalid<variable>(argName);
start = i+1;
namedArg = true;
}
++i;
}
// Strip whitespace from the function name
string::stripInvalid<word>(funcName);
dictArgList(funcArgs, funcName, args, namedArgs);
// Search for the functionObject dictionary
fileName path = findDict(funcName, region);
@ -398,9 +324,9 @@ bool Foam::functionObjectList::readFunctionObject
funcDict.set("region", region);
}
const word funcCallKeyword = string::validate<word>(funcCall);
const word funcArgsKeyword = string::validate<word>(funcArgs);
dictionary funcArgsDict;
funcArgsDict.add(funcCallKeyword, funcDict);
funcArgsDict.add(funcArgsKeyword, funcDict);
// Re-parse the funcDict to execute the functionEntries
// now that the function argument entries have been added
@ -416,16 +342,16 @@ bool Foam::functionObjectList::readFunctionObject
}
// Check for anything in the configuration that has not been set
checkUnsetEntries(funcCall, funcArgsDict, funcDict0, context);
checkUnsetEntries(funcArgs, funcArgsDict, funcDict0, context);
// Lookup the field, fields and objects entries from the now expanded
// funcDict and insert into the requiredFields
dictionary& expandedFuncDict = funcArgsDict.subDict(funcCallKeyword);
dictionary& expandedFuncDict = funcArgsDict.subDict(funcArgsKeyword);
if (functionObject::debug)
{
InfoInFunction
<< nl << incrIndent << indent
<< funcCall << expandedFuncDict
<< funcArgs << expandedFuncDict
<< decrIndent << endl;
}
if (expandedFuncDict.found("field"))
@ -451,7 +377,7 @@ bool Foam::functionObjectList::readFunctionObject
// Merge this functionObject dictionary into functionsDict
functionsDict.merge(funcArgsDict);
functionsDict.subDict(funcCallKeyword).name() = funcDict.name();
functionsDict.subDict(funcArgsKeyword).name() = funcDict.name();
return true;
}

View File

@ -222,7 +222,7 @@ public:
);
//- Read the specified functionObject configuration dictionary
// parsing the optional arguments included in the string 'funcCall',
// parsing the optional arguments included in the string 'funcArgs',
// inserting 'field' or 'fields' entries as required and merging the
// resulting functionObject dictionary into 'functionsDict'.
// Any fields required to execute the functionObject are added to
@ -242,7 +242,7 @@ public:
// fields (p U);
static bool readFunctionObject
(
const string& funcCall,
const string& funcArgs,
dictionary& functionsDict,
const string& context,
HashSet<word>& requiredFields,