-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreprocessor.cpp
More file actions
239 lines (213 loc) · 5.59 KB
/
Preprocessor.cpp
File metadata and controls
239 lines (213 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* @file Preprocessor.cpp
* Implements Preprocessor.h
* @author Tim Beyne
*/
#include "Preprocessor.h"
void trim(std::string& s)
{
while(std::isspace(s[0]))
s.erase(0, 1);
while(std::isspace(s[s.length() - 1]))
s.erase(s.length() - 1, 1);
}
std::string removeWs(const std::string& s)
{
std::string r;
for(char c : s) {
if(!std::isspace(c))
r += c;
}
return r;
}
std::string reduceWs(const std::string& s)
{
std::string r;
for(char c : s) {
if(!std::isspace(c))
r += c;
else if(!std::isspace(r[r.size() - 1]))
r += ' ';
}
return r;
}
std::string file_extension(const std::string& filename)
{
size_t pos = filename.find_last_of('.');
return (pos == std::string::npos) ? "" : filename.substr(pos + 1);
}
std::string file_directory(const std::string& filepath)
{
size_t pos = filepath.find_last_of('/');
return (pos == std::string::npos) ? "" : filepath.substr(0, pos + 1);
}
// Preprocessor implementation starts here
Preprocessor::Preprocessor(const std::string& f)
: file_name(f), lines(), source(), aliases(), rm_ws(false), rd_ws(false) {}
void Preprocessor::parseEscape(std::string& s, const std::string& escp, const std::string& repl)
{
size_t pos = 0;
while((pos = s.find(escp, pos)) != std::string::npos) {
if(s[pos - 1] == '\\') {
pos += escp.length();
continue;
}
s.replace(pos, escp.length(), repl);
}
}
void Preprocessor::parseEscapes(std::string& s)
{
parseEscape(s, "\\n", "\n");
parseEscape(s, "\\s", " ");
parseEscape(s, "\\t", "\t");
parseEscape(s, "\\v", "\v");
}
void Preprocessor::readFile(const std::string& name)
{
HELP_ASSERT(lines.size() == 0);
HELP_ASSERT(source.empty() == true);
std::ifstream ifs(name);
if(!ifs.is_open())
throw Error("cannot open file " + name);
ifs.seekg(0, std::ios::end);
size_t len = ifs.tellg();
ifs.seekg(0, std::ios::beg);
char* buffer = new char[len];
ifs.read(buffer, len);
source.assign(buffer, len);
ifs.close();
delete[] buffer;
}
void Preprocessor::writeFile()
{
std::ofstream ofs(file_name + ".out");
ofs << source;
ofs.flush();
ofs.close();
}
void Preprocessor::processSource()
{
analyseSource();
applyMacros();
}
void Preprocessor::analyseSource()
{
HELP_ASSERT(lines.size() == 0);
std::istringstream iss(source);
while(iss.good()) {
std::string line;
std::getline(iss, line);
lines.push_back(line);
}
addMacros();
}
int Preprocessor::mergeLines(const LineIterator& it)
{
HELP_ASSERT(it != lines.end());
int n = 0;
auto it2 = it + 1;
while(it2 != lines.end() && it2->length() > 0 && it2->at(0) == ' ') {
*it += '\n' + it2->substr(1);
lines.erase(it2);
++n;
}
return n;
}
void Preprocessor::handleIncludeMacro(const std::string& file, const LineIterator& line)
{
std::ifstream ifs(file);
if(!ifs)
throw Error("cannot not open #included file " + file);
std::string l;
LineIterator it = line;
while(std::getline(ifs, l))
it = lines.insert(it, l) + 1;
ifs.close();
}
void Preprocessor::handleSpecialMacro(const LineIterator& line)
{
trim(*line);
if(*line == "skip whitespace")
rm_ws = !rm_ws;
else if(*line == "reduce whitespace")
rd_ws = !rd_ws;
else if(line->substr(0, 7) == "include")
handleIncludeMacro(line->substr(line->find(' ') + 1), line);
}
void Preprocessor::addMacro(const LineIterator& line)
{
*line = line->substr(1);
size_t pos = line->find(":=");
if(pos == std::string::npos)
return handleSpecialMacro(line);
std::string name = line->substr(0, pos);
trim(name);
std::string replacement = line->substr(pos + 2);
trim(replacement);
parseEscapes(replacement);
aliases.push_back(Alias(name, replacement));
}
void Preprocessor::addMacros()
{
auto it = lines.begin();
while(it != lines.end()) {
if(it->length() > 0 && it->at(0) == '#') {
trim(*it);
mergeLines(it);
size_t size = lines.size();
size_t pos = std::distance(lines.begin(), it);
addMacro(it);
it = lines.erase(lines.begin() + pos + lines.size() - size);
} else
++it;
}
}
void Preprocessor::applyMacro(const regex& ex, std::string replacement)
{
source = boost::regex_replace(source, ex, replacement);
}
void Preprocessor::applyMacros()
{
if(!source.empty())
source.clear();
for(std::string s : lines)
source += s + '\n';
trim(source);
// replaces
for(auto alias : aliases)
applyMacro(alias.getRegex(), alias.getReplacement());
}
void Preprocessor::cleanUp()
{
lines.clear();
aliases.clear();
}
void Preprocessor::applyHelpFile()
{
std::string help_file = file_directory(file_name) + file_extension(file_name) + ".help";
std::ifstream hfs(help_file);
if(!hfs.is_open())
return;
hfs.close();
std::cout << "Using HELP file: " << help_file << '.' << std::endl;
readFile(help_file);
analyseSource();
lines.clear();
source.clear();
}
void Preprocessor::process()
{
HELP_ASSERT(lines.size() == 0);
applyHelpFile();
readFile(file_name);
std::string old_src;
do {
old_src = source;
processSource();
cleanUp();
} while(old_src != source); // continue processing until nothing left to process
if(rd_ws)
source = reduceWs(source);
source = rm_ws ? removeWs(source) : source;
writeFile();
}