-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathpr_parser_impl.cpp
More file actions
362 lines (295 loc) · 7.53 KB
/
pr_parser_impl.cpp
File metadata and controls
362 lines (295 loc) · 7.53 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include "parser/impl/pr_parser_impl.hpp"
#include "parser/api/pr_include_file.hpp"
#include "parser/impl/pr_parser_context.hpp"
#include "fs/api/fs_file.hpp"
#include <algorithm>
#include <cassert>
#include <fstream>
#include <optional>
#include <vector>
//------------------------------------------------------------------------------
namespace parser
{
//------------------------------------------------------------------------------
Parser::IncludeFiles ParserImpl::parseFile( const fs::File & _file ) const
{
IncludeFiles result;
ParserContext context;
while( !_file.eof() )
{
const std::string line = _file.getLine();
context.setCurrentLine( line );
auto includeFileOpt = parseLine( context );
if( includeFileOpt )
{
result.push_back( *includeFileOpt );
}
context.increaseLineNumber();
}
return result;
}
//------------------------------------------------------------------------------
ParserImpl::IncludeFileOpt ParserImpl::parseLine( ParserContext & _context )
{
constexpr size_t minimumSize = std::string_view{ "/*" }.size() - 1;
IncludeFileOpt resutl;
const std::string & line = _context.getCurrentLine();
const size_t size = line.size();
if( size <= minimumSize )
{
return resutl;
}
const size_t startPos = getStartPos( _context );
for( size_t i = startPos; i < size; ++i )
{
const char currentChar = line[i];
switch( currentChar )
{
case '/':
{
i = findComentEnd( _context, i );
break;
}
case '"':
{
i = findEndOfString( _context, i );
break;
}
case 'R':
{
i = findEndOfRawString( _context, i );
break;
}
case '#':
{
if( auto indexOpt = findInclude( line, i ); indexOpt )
{
return parseInclude( _context, *indexOpt );
}
// if found # and it's not include it doesn't need analyze
return resutl;
break;
}
default:
break;
}
}
return resutl;
}
//------------------------------------------------------------------------------
std::size_t ParserImpl::getStartPos( ParserContext & _context ) noexcept
{
const std::size_t index = 0;
if( _context.isEnableMultilineComment() )
{
return findComentEnd( _context, index );
}
if( _context.isEnableMultilineString() )
{
return findEndOfString( _context, index );
}
if( _context.isEnableRawString() )
{
return findEndOfRawString( _context, index );
}
return 0;
}
//------------------------------------------------------------------------------
std::size_t ParserImpl::getStartPosInInclude(
const ParserContext & _conext, std::size_t _index, char _char )
{
const std::string & line = _conext.getCurrentLine();
const std::size_t count = line.size();
for( std::size_t i = _index; i < count; ++i )
{
const char currentChar = line[i];
if( currentChar == _char )
{
return i;
}
if( currentChar == '/' )
{
const std::size_t nextPos = i + 1;
if( nextPos >= count )
{
continue;
}
const char nextChar = line[nextPos];
if( nextChar == '/' )
{
return std::string::npos;
}
if( nextChar == '*' )
{
const auto endPos = line.find( "*/", nextPos );
if( endPos == std::string::npos )
{
return std::string::npos;
}
i = endPos + 1;
}
}
}
return std::string::npos;
}
//------------------------------------------------------------------------------
std::size_t ParserImpl::findComentEnd(
ParserContext & _context, std::size_t _index ) noexcept
{
const size_t nextIndex =
_context.isEnableMultilineComment() ? 0 : _index + 1;
const std::string & line = _context.getCurrentLine();
const size_t size = line.size();
if( nextIndex >= size )
{
return size;
}
const char nextChar = line[nextIndex];
if( !_context.isEnableMultilineComment() )
{
if( nextChar == '/' )
{
return size;
}
if( nextChar != '*' )
{
return _index;
}
assert( nextChar == '*' );
}
const auto pos = line.find( "*/", nextIndex );
_context.setMultilineComment( pos == std::string::npos );
return _context.isEnableMultilineComment() ? size : pos + 1;
}
//------------------------------------------------------------------------------
std::size_t ParserImpl::findEndOfString(
ParserContext & _context, std::size_t _index ) noexcept
{
const std::string & line = _context.getCurrentLine();
const size_t size = line.size();
for( size_t i = _index + 1; i < size; ++i )
{
const char currentChar = line[i];
switch( currentChar )
{
case '\"':
{
_context.setMultilineString( false );
return i;
}
case '\\':
{
if( i == size - 1 )
{
_context.setMultilineString( true );
return size;
}
const size_t nextPost = i + 1;
assert( nextPost < size );
if( nextPost < size && line[nextPost] == '\"' )
{
i = nextPost;
}
break;
}
default:
break;
}
}
return size;
}
//------------------------------------------------------------------------------
std::size_t ParserImpl::findEndOfRawString(
ParserContext & _context, std::size_t _index ) noexcept
{
const std::string & line = _context.getCurrentLine();
if( !_context.isEnableRawString() )
{
const auto pos = line.find( "\"(", _index + 1 );
if( pos == std::string::npos )
{
return _index;
}
_index += 3;
_context.seEnableRawString( true );
}
const auto pos = line.find( ")\"", _index );
if( pos != std::string::npos )
{
_context.seEnableRawString( false );
return pos + 2;
}
return line.size();
}
//------------------------------------------------------------------------------
std::optional< std::size_t >
ParserImpl::findInclude( std::string_view _line, std::size_t _index )
{
static const std::string includePhase{ "include" };
static const size_t includePhaseSize = includePhase.size();
const size_t size = _line.size();
bool isStartedCheckPhase = false;
size_t indexPhase = 0;
for( size_t i = _index + 1; i < size; ++i )
{
const char currentChar = _line[i];
if( currentChar == ' ' || currentChar == '\t' )
{
if( isStartedCheckPhase )
{
return std::nullopt;
}
}
else
{
isStartedCheckPhase = true;
const char includeChar = includePhase[indexPhase];
if( currentChar == includeChar )
{
++indexPhase;
if( indexPhase >= includePhaseSize )
{
return i;
}
}
else
{
return std::nullopt;
}
}
}
return std::nullopt;
}
//------------------------------------------------------------------------------
ParserImpl::IncludeFileOpt
ParserImpl::parseInclude( const ParserContext & _context, std::size_t _index )
{
const std::string & line = _context.getCurrentLine();
const size_t startPosSystem = getStartPosInInclude( _context, _index, '<' );
const size_t startPosUser = getStartPosInInclude( _context, _index, '"' );
if( startPosSystem == std::string::npos &&
startPosUser == std::string::npos )
{
// it's strange that after #include don't exist < or "
return std::nullopt;
}
const size_t startPos = std::min( startPosSystem, startPosUser );
const bool isSystem =
startPosSystem != std::string::npos && startPosSystem == startPos;
const size_t startPosName =
( isSystem ? startPosSystem : startPosUser ) + 1;
const char endChar = isSystem ? '>' : '"';
size_t endPosName = line.find( endChar, startPosName );
if( endPosName == std::string::npos )
{
// it's strange, include isn't closed
return std::nullopt;
}
const std::string name =
line.substr( startPosName, endPosName - startPosName );
IncludeFileLocation location{
_context.getLineNumber(), startPosName + 1, endPosName + 1 };
return IncludeFile{ location, name, isSystem };
}
//------------------------------------------------------------------------------
}