-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsplitBlock.h
163 lines (158 loc) · 3.16 KB
/
splitBlock.h
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
struct blockSegment
{
public:
vector<u16string> lines;
u16string type;
// 根据代码块类型的不同, 利用type来进行区分
// variable 变量定义
// structDeclear 结构体声明
// structDefine 结构体定义
// functionDeclear 函数声明
// functionDefine 函数定义
};
bool isAlpha(const char16_t& _char)
{
vector<char16_t> alphabet =
{
u'a', u'b', u'c', u'd', u'e',
u'f', u'g', u'h', u'i', u'j',
u'k', u'l', u'm', u'n', u'o',
u'p', u'q', u'r', u's', u't',
u'u', u'v', u'w', u'x', u'y',
u'z', u'A', u'B', u'C', u'D',
u'E', u'F', u'G', u'H', u'I',
u'J', u'K', u'L', u'M', u'N',
u'O', u'P', u'Q', u'R', u'S',
u'T', u'U', u'V', u'W', u'X',
u'Y', u'Z'
};
return find(alphabet.begin(), alphabet.end(), _char) != alphabet.end();
}
// 查找该行最近的分隔符, 特别地, 为了区分函数的声明和定义, 函数的声明将返回(, 函数的定义将返回)
char16_t findSplitSymbol(const vector<u16string>& _lines, const int& rowId = 0)
{
bool isFunction = false;
for (auto i=_lines.begin()+rowId; i!=_lines.end(); i++)
{
for (auto letter: *i)
{
if (letter == u'{' || letter == u';')
{
if (!isFunction)
{
return letter;
}
else
{
if (letter == u'{')
{
return u')';
}
else
{
return u'(';
}
}
}
else if (letter == u'(')
{
isFunction = true;
}
}
}
return u'\0';
}
u16string getFirstWord(const u16string& _str)
{
u16string res;
for (auto letter: _str)
{
if (letter == u' ' || !isAlpha(letter))
{
break;
}
else
{
res += letter;
}
}
return res;
}
vector<blockSegment> splitBlock(const vector<u16string>& _lines)
{
vector<blockSegment> res;
vector<int> splitLineNumber;
int blockLevel = 0;
int rowNumber = -1;
for (auto line: _lines)
{
rowNumber++;
if (line.empty())
{
continue;
}
if (line[0] == u'{')
{
blockLevel++;
}
else if(line[0] == u'}')
{
blockLevel--;
continue;
}
if (blockLevel)
{
continue;
}
splitLineNumber.push_back(rowNumber);
}
for (auto i=splitLineNumber.begin(); i!=splitLineNumber.end(); i++)
{
int beginRowNumber = *i;
int endRowNumber;
if (i+1 == splitLineNumber.end())
{
endRowNumber = rowNumber + 1;
}
else
{
endRowNumber = *(i+1);
}
blockSegment block;
for (int row=beginRowNumber; row!=endRowNumber; row++)
{
block.lines.push_back(_lines[row]);
}
char16_t endChar = findSplitSymbol(_lines, beginRowNumber);
u16string firstWord = getFirstWord(_lines[beginRowNumber]);
if (firstWord == u"int" && endChar == u')')
{
block.type = u"functionDefine";
}
//cout<<beginRowNumber<<"-"<<endRowNumber<<":"<<block.lines.size()<<"@"<<to_bytes(firstWord)<<"#"<<char(endChar)<<endl;
res.push_back(block);
}
return res;
}
vector<u16string> fetchMainFunction(const vector<blockSegment>& _blocks)
{
vector<u16string> res;
for (auto block: _blocks)
{
if (block.type == u"functionDefine")
{
if (block.lines[0].find(u"main") != -1)
{
for (auto i=block.lines.begin()+2; i!=block.lines.end()-1; i++)
{
if (i->find(u"}") != -1)
{
break;
}
res.push_back(*i);
}
}
}
}
return res;
}