MCPcopy Create free account
hub / github.com/KratosMultiphysics/Kratos / InitializeParser

Method InitializeParser

kratos/utilities/function_parser_utility.cpp:156–250  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

154/***********************************************************************************/
155
156void BasicGenericFunctionUtility::InitializeParser()
157{
158 // Initialize
159 if (mpTinyExpr[0] == nullptr) {
160 int err;
161
162 /* Defining table */
163 double& x = mValues[0];
164 double& y = mValues[1];
165 double& z = mValues[2];
166 double& t = mValues[3];
167 double& X = mValues[4];
168 double& Y = mValues[5];
169 double& Z = mValues[6];
170
171 /* Store variable names and pointers. */
172 const te_variable vars[] = {{"x", &x}, {"y", &y}, {"z", &z}, {"t", &t}, {"X", &X}, {"Y", &Y}, {"Z", &Z}};
173
174 auto fct_checker = [](const bool IsValid, const std::string& rFunction, const int ErrPos){
175 if (IsValid) return;
176
177 std::stringstream ss;
178 ss << "\nParsing error in function: " << rFunction << '\n';
179 ss << "Error occurred near here : ";
180 for(int i=0; i<ErrPos-1; ++i) ss << ' ';
181 ss << "^ (char ["<< ErrPos-1 << "])\n";
182 ss << "Check your locale (e.g. if \".\" or \",\" is used as decimal point)";
183
184 KRATOS_ERROR << ss.str() << std::endl;
185 };
186
187 /* Compile the expression with variables. */
188 const bool python_like_ternary = StringUtilities::ContainsPartialString(mFunctionBody, "if") ? true : false;
189 if (!python_like_ternary) {
190 mpTinyExpr[0] = te_compile(mFunctionBody.c_str(), vars, 7, &err);
191 fct_checker(mpTinyExpr[0], mFunctionBody, err);
192 } else { // Ternary operator
193 mpTinyExpr.resize(3, nullptr);
194 std::string condition, first_function, second_function;
195
196 // C like ternary operator
197 std::vector<std::string> splitted_string;
198 KRATOS_ERROR_IF_NOT(StringUtilities::ContainsPartialString(mFunctionBody, "else")) << "Parsing error in function: " << mFunctionBody << " if defined, but not else" << std::endl;
199 std::string aux_string = StringUtilities::ReplaceAllSubstrings(mFunctionBody, "if", "$");
200 KRATOS_ERROR_IF(splitted_string.size() > 2) << "Nested ternary functions not supported" << std::endl;
201 splitted_string = StringUtilities::SplitStringByDelimiter(aux_string, '$');
202 first_function = splitted_string[0];
203 aux_string = StringUtilities::ReplaceAllSubstrings(splitted_string[1], "else", "$");
204 splitted_string = StringUtilities::SplitStringByDelimiter(aux_string, '$');
205 condition = splitted_string[0];
206 second_function = splitted_string[1];
207
208 // Parsing the functions
209 mpTinyExpr[1] = te_compile(first_function.c_str(), vars, 7, &err);
210 fct_checker(mpTinyExpr[1], first_function, err);
211
212 mpTinyExpr[2] = te_compile(second_function.c_str(), vars, 7, &err);
213 fct_checker(mpTinyExpr[2], second_function, err);

Callers

nothing calls this directly

Calls 7

ContainsPartialStringFunction · 0.85
te_compileFunction · 0.85
ReplaceAllSubstringsFunction · 0.85
SplitStringByDelimiterFunction · 0.85
strMethod · 0.45
resizeMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected