VM2D 1.14
Vortex methods for 2D flows simulation
Loading...
Searching...
No Matches
Preprocessor.cpp
Go to the documentation of this file.
1/*--------------------------------*- VM2D -*-----------------*---------------*\
2| ## ## ## ## #### ##### | | Version 1.14 |
3| ## ## ### ### ## ## ## ## | VM2D: Vortex Method | 2026/03/06 |
4| ## ## ## # ## ## ## ## | for 2D Flow Simulation *----------------*
5| #### ## ## ## ## ## | Open Source Code |
6| ## ## ## ###### ##### | https://www.github.com/vortexmethods/VM2D |
7| |
8| Copyright (C) 2017-2026 I. Marchevsky, K. Sokol, E. Ryatina, A. Kolganova |
9*-----------------------------------------------------------------------------*
10| File name: Preprocessor.cpp |
11| Info: Source code of VM2D |
12| |
13| This file is part of VM2D. |
14| VM2D is free software: you can redistribute it and/or modify it |
15| under the terms of the GNU General Public License as published by |
16| the Free Software Foundation, either version 3 of the License, or |
17| (at your option) any later version. |
18| |
19| VM2D is distributed in the hope that it will be useful, but WITHOUT |
20| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
21| FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
22| for more details. |
23| |
24| You should have received a copy of the GNU General Public License |
25| along with VM2D. If not, see <http://www.gnu.org/licenses/>. |
26\*---------------------------------------------------------------------------*/
27
28
40#include <algorithm>
41
42#include "Preprocessor.h"
43
44using namespace VMlib;
45
46// Конструктор
47Preprocessor::Preprocessor(const std::string& fileName)
48 :
49 inStringLastEscape(false),
50 inCharLastEscape(false),
51 inInlineLastEscape(false),
52 inMultilineLastStar(false),
53 currentParser(&Preprocessor::normalParser)
54{
55#pragma warning (push)
56#pragma warning (disable: 4996)
57 FILE * inputFile = fopen(fileName.c_str(), "r");
58#pragma warning (pop)
59
60 int symbol;
61
62 while ((symbol = fgetc(inputFile)) != EOF)
63 {
64 initialInput.push_back(symbol);
65 (this->*currentParser)(symbol, intermediateOutput);
66 }
67
68 fclose(inputFile);
69
70 std::stringstream ss(intermediateOutput);
71
72 std::string readline;
73
74 while (ss.good())
75 {
76 getline(ss, readline);
77
78 readline.erase(std::remove(readline.begin(), readline.end(), ' '), readline.end());
79 readline.erase(std::remove(readline.begin(), readline.end(), '\t'), readline.end());
80
81#if defined(_WIN32)
82#else
83 readline.erase(std::remove(readline.begin(), readline.end(), 0x0D), readline.end());
84#endif
85
86 resultStream << readline << std::endl;
87 }
88
90}//Preprocessor(...)
91
92
93// Базовая функция обработки символа
95{
96 switch (ch)
97 {
98 case '\n':
99 return ' ';
100
101 case ';':
102 return '\n';
103
104 default:
105 return ch;
106 }
107}//processSymbol(...)
108
109
110// Обработчик символа в режиме обычного состояния парcера
111void Preprocessor::normalParser(char ch, std::string& str)
112{
113 switch (ch)
114 {
115 case '\"':
117 break;
118
119 case '\'':
121 break;
122
123 case '/':
125 return;
126 }
127 str.push_back(processSymbol(ch));
128}//normalParser(...)
129
130
131// Обработчик символа в режиме парсера строки (внутри двойных кавычек)
132void Preprocessor::inStringParser(char ch, std::string& str)
133{
134 str.push_back(ch);
135 if (ch == '\"' && !inStringLastEscape)
137 inStringLastEscape = (ch == '\\') && !inStringLastEscape;
138}//inStringParser(...)
139
140
141// Обработчик символа в режиме парсера символа (внутри одинарных кавычек)
142void Preprocessor::inCharParser(char ch, std::string& str)
143{
144 str.push_back(ch);
145 if (ch == '\'' && !inCharLastEscape)
147 inCharLastEscape = (ch == '\\') && !inCharLastEscape;
148}//inCharParser(...)
149
150
151// Обработчик символа в режиме парсера выражения после слэша
152void Preprocessor::afterSlashParser(char ch, std::string& str)
153{
154 switch (ch)
155 {
156 case '/':
158 return;
159
160 case '*':
162 return;
163
164 default:
165 str.push_back(processSymbol(ch));
166 }
167}//afterSlashParser(...)
168
169
170// Обработчик символа в режиме парсера однострочного комментария (после //)
171void Preprocessor::inInlineCommentParser(char ch, std::string& str)
172{
173 if (ch == '\n' && !inInlineLastEscape)
174 {
175 str.push_back(processSymbol(ch));
177 }
178 inInlineLastEscape = (ch == '\\') && !inInlineLastEscape;
179}//inInlineCommentParser(...)
180
181
182// Обработчик символа в режиме парсера многострочного комментария
183void Preprocessor::inMultilineCommentParser(char ch, std::string& str)
184{
185 if (ch == '/' && inMultilineLastStar)
186 {
187 str.push_back(' ');
189 }
190 inMultilineLastStar = (ch == '*');
191}//inMultilineCommentParser(...)
Заголовочный файл с описанием класса Preprocessor.
Класс, позволяющий выполнять предварительную обработку файлов
std::string intermediateOutput
Строка, содержащая результат промежуточной обработки файла
void afterSlashParser(char ch, std::string &str)
Обработчик символа в режиме парсера выражения после слэша
void inStringParser(char ch, std::string &str)
Обработчик символа в режиме парсера строки (внутри двойных кавычек)
void(Preprocessor::* currentParser)(char ch, std::string &str)
Опредедение currentParser как указателя на функцию-члена класса
void inMultilineCommentParser(char ch, std::string &str)
Обработчик символа в режиме парсера многострочного комментария
std::string initialInput
Строка, содержащая исходный файл в первоначальном виде
bool inInlineLastEscape
Признак встречи слэша внутри однострочного комментария (после //)
void normalParser(char ch, std::string &str)
Обработчик символа в режиме обычного состояния парcера
bool inMultilineLastStar
Признак встречи звездочки внутри многострочного комментария (после '/*')
char processSymbol(char ch)
Базовая функция обработки символа В зависимости от входого символа возвращает на выход:
bool inCharLastEscape
Признак встречи слэша внутри символа (внутри одинарных кавычек)
void inInlineCommentParser(char ch, std::string &str)
Обработчик символа в режиме парсера однострочного комментария (после //)
std::string resultString
Строка, содержащая окончательный результат обработки файла
bool inStringLastEscape
Признак встречи слэша внутри строки (внутри двойных кавычек)
std::stringstream resultStream
void inCharParser(char ch, std::string &str)
Обработчик символа в режиме парсера символа (внутри одинарных кавычек)
Preprocessor(const std::string &fileName)
Конструктор, принимающий на вход имя обрабатываемого файла