NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
symdef.cpp
Go to the documentation of this file.
1/*****************************************************************************
2 NumeRe: Framework fuer Numerische Rechnungen
3 Copyright (C) 2021 Erik Haenel et al.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17******************************************************************************/
18
19#include "symdef.hpp"
20#include "utils/tools.hpp"
22
23
24
25
34{
35 m_symDefs.clear();
36}
37
38
47void SymDefManager::resolveSymbols(std::string& sCommandLine) const
48{
49 // Go through all definitions
50 for (auto iter = m_symDefs.begin(); iter != m_symDefs.end(); ++iter)
51 {
52 size_t pos = 0;
53
54 // Find all occurences of the current variable
55 while ((pos = findVariableInExpression(sCommandLine, iter->first, pos)) != std::string::npos)
56 {
57 sCommandLine.replace(pos, iter->first.length(), iter->second);
58 pos += iter->second.length();
59 }
60 }
61}
62
63
72void SymDefManager::createSymbol(const std::string& sCommandLine)
73{
74 // Separate the list of declarations
75 EndlessVector<std::string> symdefs = getAllArguments(sCommandLine);
76
77 // Declare each of them, if the definition operator
78 // is found and symbol and its definitions are of
79 // non-zero length
80 for (std::string symdef : symdefs)
81 {
82 size_t pos = symdef.find(":=");
83
84 if (pos != std::string::npos)
85 {
86 std::string symbol = symdef.substr(0, pos);
87 std::string definition = symdef.substr(pos+2);
88
89 // Enable nested definitions
90 resolveSymbols(definition);
91
92 StripSpaces(symbol);
93 StripSpaces(definition);
94
95 if (definition.back() == ';')
96 {
97 definition.pop_back();
98 StripSpaces(definition);
99 }
100
101 if (symbol.length() && definition.length() && symbol != definition)
102 m_symDefs[symbol] = definition;
103 }
104 }
105}
106
107
117bool SymDefManager::isSymbol(const std::string& sSymbol) const
118{
119 return m_symDefs.find(sSymbol) != m_symDefs.end();
120}
121
122
123
124
This class extends the std::vector for endlessness.
Definition: structures.hpp:838
void resolveSymbols(std::string &sCommandLine) const
Resolve all file-static constant declarations in the current line.
Definition: symdef.cpp:47
void clear()
Remove all file-static constant declarations.
Definition: symdef.cpp:33
bool isSymbol(const std::string &sSymbol) const
Check, whether the passed string is a defined symbol (will be used by the static code analyzer).
Definition: symdef.cpp:117
void createSymbol(const std::string &sCommandLine)
Create one or more new file-static constant declarations for the current file.
Definition: symdef.cpp:72
std::map< std::string, std::string > m_symDefs
Definition: symdef.hpp:34
size_t findVariableInExpression(const std::string &sExpr, const std::string &sVarName, size_t nPosStart)
This function searches for the selected variable in the passed string and returns the position of the...
void StripSpaces(std::string &)
Removes leading and trailing white spaces and tabulator characters.
EndlessVector< StringView > getAllArguments(StringView sArgList)
Splits up the complete argument list and returns them as an EndlessVector.
Definition: tools.cpp:2346