-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreprocessor.cpp
100 lines (93 loc) · 2.52 KB
/
Preprocessor.cpp
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
/*====================================================
This cpp file contains the implementation of class
Preprocessor.
Steps:
1 rune-replace: **planed to do another thing but now
**just do some stripping and replacing.
2 decomment: //remove comments and empty lines;
**now it remove comments but let them
**become empty lines and let the empty
**lines remain.
3 separate: add EOL and indent seperator;
======================================================*/
#include "Preprocessor.h"
using std::string;
using std::vector;
Preprocessor::Preprocessor()
{
//nothing to do with init.
}
vector<string> Preprocessor::preprocess(std::string const raw)
{
string copy_raw = raw;
rune_replace(copy_raw);
if (copy_raw.back() != '\n')
copy_raw.push_back('\n');
decomment(copy_raw);
vector<string> lines;
lines = separate(copy_raw);
return lines;
}
void Preprocessor::decomment(string &raw)
{
//remove comments, erase from a '#' to next '\n'
size_t begin_pos;
while ((begin_pos = raw.find('#')) != string::npos)
{
auto end_pos = raw.find('\n', begin_pos);
raw.erase(begin_pos, end_pos - begin_pos);
}
}
vector<string> Preprocessor::separate(string const raw)
{
//separate raw string with '\n', return vector of string
vector<string> lines;
string tmp;
string::size_type pos_begin = 0;
string::size_type new_line_pos = 0;
while (pos_begin != string::npos)
{
new_line_pos = raw.find('\n', pos_begin);
if (new_line_pos != string::npos)
{
tmp = raw.substr(pos_begin, new_line_pos - pos_begin);
pos_begin = new_line_pos + 1;
}
else
{
tmp = raw.substr(pos_begin);
pos_begin = new_line_pos;
}
if (!tmp.empty() || 1)
{
lines.push_back(tmp);
tmp.clear();
}
}
return lines;
}
void Preprocessor::rune_replace(string &raw)
{
//Up to now, rune_replace() will do following things:
//1. add space before ')' and after '(';
auto strit = raw.begin();
while (strit != raw.end())
{
if (*strit == '(')
{
*strit = ' ';
strit = raw.insert(strit, '(');
strit++;
}
if (*strit == ')')
{
strit = raw.insert(strit, ' ');
strit++;
}
if (*strit == '\r')
{
strit = raw.erase(strit);
}
strit++;
}
}