-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathpr_parser_impl.cpp
More file actions
196 lines (156 loc) · 4.33 KB
/
pr_parser_impl.cpp
File metadata and controls
196 lines (156 loc) · 4.33 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
#include "parser/impl/pr_parser_impl.hpp"
#include "parser/api/pr_include_file.hpp"
#include "fs/api/fs_file.hpp"
#include <fstream>
#include <vector>
#include <optional>
//------------------------------------------------------------------------------
namespace parser {
//------------------------------------------------------------------------------
Parser::IncludeFiles ParserImpl::parseFile( const fs::File & _file ) const
{
IncludeFiles result;
std::size_t lineNumber = 1;
while( !_file.eof() )
{
std::string line = _file.getLine();
if( auto includeFileOpt = parseLine( line, lineNumber ); includeFileOpt )
{
result.push_back( *includeFileOpt );
}
++lineNumber;
}
return result;
}
//------------------------------------------------------------------------------
ParserImpl::IncludeFileOpt ParserImpl::parseLine(
std::string_view _line,
std::size_t _lineNumber
) const
{
static constexpr size_t minimumSize = std::string_view{"#include<>"}.size();
IncludeFileOpt resutl;
const size_t size = _line.size();
if( size <= minimumSize )
return resutl;
for( size_t i = 0; i < size; ++i )
{
const char str_char = _line[i];
switch( str_char )
{
case '/' :
i = findComentEnd( _line, i );
break;
case '\"':
i = findEndOfString( _line, i );
break;
case '#' :
if( auto indexOpt = findInclude( _line, i ); indexOpt )
return parseInclude( _line, _lineNumber, *indexOpt );
else // if found # and it's not include it doesn't need analyze
return resutl;
}
}
return resutl;
}
//------------------------------------------------------------------------------
std::size_t ParserImpl::findComentEnd(
std::string_view _line,
std::size_t _index
) noexcept
{
const size_t nextIndex = _index + 1;
const size_t size = _line.size();
if( nextIndex >= size )
return size;
const char nextChar = _line[ nextIndex ];
if( nextChar == '/' )
return size;
if( nextChar != '*' )
return _index;
const auto pos = _line.find( "*/", nextIndex );
return ( pos == std::string::npos ? size : pos + 1 );
}
//------------------------------------------------------------------------------
std::size_t ParserImpl::findEndOfString(
std::string_view _line,
std::size_t _index
) noexcept
{
const size_t size = _line.size();
for( size_t i = _index + 1 ; i < size ; ++i )
{
if( _line[i] == '\"' )
return i;
}
return 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 str_char = _line[ i ];
if( str_char == ' ' || str_char == '\t' )
{
if( isStartedCheckPhase )
return std::nullopt;
}
else
{
isStartedCheckPhase = true;
const char includeChar = includePhase[indexPhase];
if( str_char == includeChar )
{
++indexPhase;
if( indexPhase >= includePhaseSize )
return i;
}
else
{
return std::nullopt;
}
}
}
return std::nullopt;
}
//------------------------------------------------------------------------------
ParserImpl::IncludeFileOpt ParserImpl::parseInclude(
std::string_view _line,
std::size_t _lineNumber,
std::size_t _index
)
{
const size_t startPosSystem = _line.find( '<', _index );
const size_t startPosUser = _line.find( '"', _index );
if( startPosSystem == std::string::npos && startPosUser == std::string::npos )
{
// it's strange that after #include don't exist < or "
return std::nullopt;
}
const bool isSystem = startPosSystem != std::string::npos;
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_view name = _line.substr(
startPosName,
endPosName - startPosName
);
IncludeFileLocation location{ _lineNumber, startPosName + 1, endPosName + 1 };
return IncludeFile{ location, name, isSystem };
}
//------------------------------------------------------------------------------
}