Skip to content

Commit d063f02

Browse files
committed
Now safely detecting missing @file and @uri arguments.
1 parent fa50534 commit d063f02

22 files changed

+1375
-609
lines changed

msvc/yw-extract-tests/yw-extract-tests.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
<ClCompile Include="..\..\test\yw-extract-tests\annotation_listener_tests.cpp" />
169169
<ClCompile Include="..\..\test\yw-extract-tests\outline_exporter_tests.cpp" />
170170
<ClCompile Include="..\..\test\yw-extract-tests\source_loader_tests.cpp" />
171+
<ClCompile Include="..\..\test\yw-extract-tests\uri_listener_tests.cpp" />
171172
<ClCompile Include="stdafx.cpp">
172173
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
173174
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>

msvc/yw-extract-tests/yw-extract-tests.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,8 @@
3838
<ClCompile Include="..\..\test\yw-extract-tests\annotation_extractor_tests.cpp">
3939
<Filter>Source Files</Filter>
4040
</ClCompile>
41+
<ClCompile Include="..\..\test\yw-extract-tests\uri_listener_tests.cpp">
42+
<Filter>Source Files</Filter>
43+
</ClCompile>
4144
</ItemGroup>
4245
</Project>

msvc/yw-graph-tests/yw-graph-tests.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@
176176
<ClCompile Include="..\..\test\yw-graph-tests\missing_port_argument_tests.cpp" />
177177
<ClCompile Include="..\..\test\yw-graph-tests\missing_port_as_argument_tests.cpp" />
178178
<ClCompile Include="..\..\test\yw-graph-tests\missing_port_desc_argument_tests.cpp" />
179+
<ClCompile Include="..\..\test\yw-graph-tests\missing_uri_argument_tests.cpp" />
179180
<ClCompile Include="..\..\test\yw-graph-tests\yw_graph_tests.cpp" />
180181
<ClCompile Include="..\..\test\yw-graph-tests\yw_graph_cli_tests.cpp" />
181182
<ClCompile Include="..\..\test\yw-graph-tests\yw_graph_config_tests.cpp" />

msvc/yw-graph-tests/yw-graph-tests.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,8 @@
6565
<ClCompile Include="..\..\test\yw-graph-tests\missing_file_argument_tests.cpp">
6666
<Filter>Source Files</Filter>
6767
</ClCompile>
68+
<ClCompile Include="..\..\test\yw-graph-tests\missing_uri_argument_tests.cpp">
69+
<Filter>Source Files</Filter>
70+
</ClCompile>
6871
</ItemGroup>
6972
</Project>

msvc/yw-parse-tests/yw-parse-tests.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@
170170
<ClCompile Include="..\..\test\yw-parse-tests\block_context_tests.cpp" />
171171
<ClCompile Include="..\..\test\yw-parse-tests\block_desc_context_tests.cpp" />
172172
<ClCompile Include="..\..\test\yw-parse-tests\block_listener_tests.cpp" />
173+
<ClCompile Include="..\..\test\yw-parse-tests\file_context_tests.cpp" />
173174
<ClCompile Include="..\..\test\yw-parse-tests\io_context_tests.cpp" />
174175
<ClCompile Include="..\..\test\yw-parse-tests\io_listener_tests.cpp" />
175176
<ClCompile Include="..\..\test\yw-parse-tests\misplaced_begin_child_exception_tests.cpp" />
@@ -180,6 +181,7 @@
180181
<ClCompile Include="..\..\test\yw-parse-tests\script_listener_tests.cpp" />
181182
<ClCompile Include="..\..\test\yw-parse-tests\parsing_exception_tests.cpp" />
182183
<ClCompile Include="..\..\test\yw-parse-tests\unexpected_annotation_exception_tests.cpp" />
184+
<ClCompile Include="..\..\test\yw-parse-tests\uri_context_tests.cpp" />
183185
<ClCompile Include="stdafx.cpp">
184186
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
185187
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>

msvc/yw-parse-tests/yw-parse-tests.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,11 @@
6969
<ClCompile Include="..\..\test\yw-parse-tests\port_desc_context_tests.cpp">
7070
<Filter>Source Files</Filter>
7171
</ClCompile>
72+
<ClCompile Include="..\..\test\yw-parse-tests\uri_context_tests.cpp">
73+
<Filter>Source Files</Filter>
74+
</ClCompile>
75+
<ClCompile Include="..\..\test\yw-parse-tests\file_context_tests.cpp">
76+
<Filter>Source Files</Filter>
77+
</ClCompile>
7278
</ItemGroup>
7379
</Project>

src/yw-db/annotation.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ namespace yw {
2020
OUT = 5,
2121
PARAM = 6,
2222
RETURN = 7,
23-
AS = 8
23+
AS = 8,
24+
FILE = 9,
25+
URI = 10
2426
};
2527

2628
nullable_row_id id;
@@ -67,7 +69,7 @@ namespace yw {
6769

6870
static std::string to_string(Tag tag) {
6971
static const std::vector<std::string> names{
70-
"NONE", "BEGIN", "END", "DESC", "IN", "OUT", "PARAM", "RETURN", "AS"
72+
"NONE", "BEGIN", "END", "DESC", "IN", "OUT", "PARAM", "RETURN", "AS", "FILE", "URI"
7173
};
7274
return names[static_cast<int>(tag)];
7375
}

src/yw-extract/annotation_accessors.cpp

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ namespace yw {
9999
return keyword->getText();
100100
}
101101

102+
std::string safelyGetUriKeywordText(YWParser::UriContext* uri) noexcept {
103+
antlr4::tree::TerminalNode* keyword;
104+
if (uri == nullptr || (keyword = uri->UriKeyword()) == nullptr) {
105+
std::cerr << "yw::extract::safelyGetUriKeywordText() encountered null pointer" << std::endl;
106+
return "NULL URI KEYWORD";
107+
}
108+
return keyword->getText();
109+
}
110+
102111
Annotation::Tag safelyGetPortTagFromPortContext(YWParser::PortContext *port)
103112
{
104113
YWParser::PortKeywordContext* portKeyword;
@@ -258,7 +267,7 @@ namespace yw {
258267
}
259268

260269

261-
std::string safelyGetPathTemplateFromFileResourceContext(YWParser::FileContext *file) {
270+
std::tuple<std::string, std::string> safelyGetComponentsFromFileResourceContext(YWParser::FileContext *file) {
262271

263272
YWParser::PathTemplateContext* pathTemplate;
264273
std::string pathTemplateText;
@@ -275,9 +284,44 @@ namespace yw {
275284
safelyGetStartLineNumber(file)
276285
);
277286
}
278-
return pathTemplateText;
287+
return std::tuple<std::string, std::string>{ pathTemplateText, pathTemplate->getText() };
279288
}
280289

290+
std::tuple<std::string, std::string, std::string> safelyGetComponentsFromUriResourceContext(YWParser::UriContext *uri) {
291+
292+
YWParser::PathTemplateContext* pathTemplate;
293+
YWParser::UriTemplateContext* uriTemplate;
294+
YWParser::SchemeContext* scheme;
295+
std::string schemeText;
296+
std::string pathTemplateText;
297+
std::string uriTemplateText;
298+
299+
if (uri == nullptr ||
300+
(uriTemplate = uri->uriTemplate()) == nullptr ||
301+
(uriTemplateText = uriTemplate->getText()).empty() ||
302+
(pathTemplate = uriTemplate->pathTemplate()) == nullptr ||
303+
(pathTemplateText = pathTemplate->getText()) == "<missing WORD>" ||
304+
pathTemplateText.empty()
305+
) {
306+
throw yw::parse::MissingArgumentException(
307+
safelyGetUriKeywordText(uri),
308+
"path template",
309+
safelyGetStartColumnNumber(uri),
310+
safelyGetStartLineNumber(uri)
311+
);
312+
}
313+
314+
if ((scheme = uriTemplate->scheme()) != nullptr) {
315+
schemeText = scheme->getText();
316+
}
317+
318+
return std::tuple<std::string, std::string, std::string> { schemeText, pathTemplateText, uriTemplateText };
319+
}
320+
321+
//flowTemplateScheme = context->uri()->uriTemplate()->scheme()->getText();
322+
//flowTemplatePath = context->uri()->uriTemplate()->pathTemplate()->getText();
323+
324+
281325
std::string safelyGetAliasNameFromAliasContext(YWParser::AliasContext *alias) {
282326

283327
YWParser::DataNameContext* dataName;

src/yw-extract/annotation_listener.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,34 @@ namespace yw {
157157
primaryAnnotationStack.pop();
158158
}
159159

160+
void AnnotationListener::enterResource(YWParser::ResourceContext *context) {
161+
auto lineId = getLineId(context);
162+
auto rangeInLine = getRangeInLine(context);
163+
164+
if (context->file() != nullptr) {
165+
auto uri = safelyGetComponentsFromFileResourceContext(context->file());
166+
flowTemplateScheme = null_string;
167+
flowTemplatePath = std::get<0>(uri);
168+
auto flowTemplateText = std::get<1>(uri);
169+
auto fileKeywordText = safelyGetFileKeywordText(context->file());
170+
ywdb.insert(Annotation{ auto_id, extractionId, Tag::FILE, currentPrimaryAnnotation->id, lineId,
171+
currentRankOnLine++, rangeInLine.start, rangeInLine.end,
172+
fileKeywordText, nullable_string(flowTemplateText) });
173+
}
174+
else if (context->uri() != nullptr) {
175+
auto uri = safelyGetComponentsFromUriResourceContext(context->uri());
176+
flowTemplateScheme = std::get<0>(uri);
177+
flowTemplatePath = std::get<1>(uri);
178+
auto uriTemplateText = std::get<2>(uri);
179+
auto uriKeywordText = safelyGetUriKeywordText(context->uri());
180+
ywdb.insert(Annotation{ auto_id, extractionId, Tag::URI, currentPrimaryAnnotation->id, lineId,
181+
currentRankOnLine++, rangeInLine.start, rangeInLine.end,
182+
uriKeywordText, nullable_string(uriTemplateText) });
183+
}
184+
else {
185+
flowTemplateScheme = null_string;
186+
flowTemplatePath = null_string;
187+
}
188+
}
160189
}
161190
}

src/yw-extract/annotation_listener.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ namespace yw {
2828
StderrRecorder stderrRecorder;
2929
long currentLineNumber = 0;
3030
long currentRankOnLine = 0;
31+
nullable_string flowTemplateScheme;
32+
nullable_string flowTemplatePath;
3133

3234
public:
3335
AnnotationListener(
@@ -53,6 +55,7 @@ namespace yw {
5355
void enterAlias(YWParser::AliasContext *context) override;
5456
void enterIo(YWParser::IoContext *context) override;
5557
void exitIo(YWParser::IoContext *context) override;
58+
void enterResource(YWParser::ResourceContext *context);
5659

5760
protected:
5861
bool AnnotationListener::inProgramBlock();
@@ -70,14 +73,16 @@ namespace yw {
7073
std::string safelyGetPortDescKeywordText(YWParser::PortDescContext *desc) noexcept;
7174
std::string safelyGetPortKeywordText(YWParser::PortContext* port) noexcept;
7275
std::string safelyGetFileKeywordText(YWParser::FileContext* file) noexcept;
76+
std::string safelyGetUriKeywordText(YWParser::UriContext* uri) noexcept;
7377

7478
std::string safelyGetBlockNameFromBeginContext(YWParser::BeginContext *begin);
7579
nullable_string safelyGetOptionalBlockNameFromEndContext(YWParser::EndContext *end);
7680
std::string safelyDescriptionTextFromBlockDescContext(YWParser::BlockDescContext *desc);
7781
std::string safelyDescriptionTextFromPortDescContext(YWParser::PortDescContext *desc);
7882
std::string safelyGetPortNameFromPortNameContext(YWParser::PortNameContext *portName);
7983
std::string safelyGetAliasNameFromAliasContext(YWParser::AliasContext *alias);
80-
std::string safelyGetPathTemplateFromFileResourceContext(YWParser::FileContext *file);
84+
std::tuple<std::string, std::string> safelyGetComponentsFromFileResourceContext(YWParser::FileContext *file);
85+
std::tuple<std::string, std::string, std::string> safelyGetComponentsFromUriResourceContext(YWParser::UriContext *uri);
8186

8287
yw::db::Annotation::Tag safelyGetPortTagFromPortContext(YWParser::PortContext *port);
8388
yw::db::Flow::Direction safelyGetPortDirection(YWParser::PortContext *port);

0 commit comments

Comments
 (0)