Skip to content

Commit

Permalink
[vcpkg] Restore layering and purge unused ParagraphParseResult (micro…
Browse files Browse the repository at this point in the history
…soft#12897)

Co-authored-by: Robert Schumacher <[email protected]>
  • Loading branch information
ras0219 and ras0219-msft authored Aug 14, 2020
1 parent 7c5ea94 commit 74ab3aa
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 200 deletions.
36 changes: 0 additions & 36 deletions toolsrc/include/vcpkg/paragraphparseresult.h

This file was deleted.

127 changes: 0 additions & 127 deletions toolsrc/src/vcpkg/base/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
#include <vcpkg/base/system.print.h>
#include <vcpkg/base/util.h>

#include <vcpkg/packagespec.h>
#include <vcpkg/paragraphparser.h>

#include <utility>

using namespace vcpkg;
Expand Down Expand Up @@ -104,128 +101,4 @@ namespace vcpkg::Parse
// Avoid error loops by skipping to the end
skip_to_eof();
}

static Optional<std::pair<std::string, TextRowCol>> remove_field(Paragraph* fields, const std::string& fieldname)
{
auto it = fields->find(fieldname);
if (it == fields->end())
{
return nullopt;
}

auto value = std::move(it->second);
fields->erase(it);
return value;
}

void ParagraphParser::required_field(const std::string& fieldname, std::pair<std::string&, TextRowCol&> out)
{
auto maybe_field = remove_field(&fields, fieldname);
if (const auto field = maybe_field.get())
out = std::move(*field);
else
missing_fields.push_back(fieldname);
}
void ParagraphParser::optional_field(const std::string& fieldname, std::pair<std::string&, TextRowCol&> out)
{
auto maybe_field = remove_field(&fields, fieldname);
if (auto field = maybe_field.get()) out = std::move(*field);
}
void ParagraphParser::required_field(const std::string& fieldname, std::string& out)
{
TextRowCol ignore;
required_field(fieldname, {out, ignore});
}
std::string ParagraphParser::optional_field(const std::string& fieldname)
{
std::string out;
TextRowCol ignore;
optional_field(fieldname, {out, ignore});
return out;
}
std::string ParagraphParser::required_field(const std::string& fieldname)
{
std::string out;
TextRowCol ignore;
required_field(fieldname, {out, ignore});
return out;
}

std::unique_ptr<ParseControlErrorInfo> ParagraphParser::error_info(const std::string& name) const
{
if (!fields.empty() || !missing_fields.empty())
{
auto err = std::make_unique<ParseControlErrorInfo>();
err->name = name;
err->extra_fields["CONTROL"] = Util::extract_keys(fields);
err->missing_fields["CONTROL"] = std::move(missing_fields);
err->expected_types = std::move(expected_types);
return err;
}
return nullptr;
}

template<class T, class F>
static Optional<std::vector<T>> parse_list_until_eof(StringLiteral plural_item_name, Parse::ParserBase& parser, F f)
{
std::vector<T> ret;
parser.skip_whitespace();
if (parser.at_eof()) return std::vector<T>{};
do
{
auto item = f(parser);
if (!item) return nullopt;
ret.push_back(std::move(item).value_or_exit(VCPKG_LINE_INFO));
parser.skip_whitespace();
if (parser.at_eof()) return {std::move(ret)};
if (parser.cur() != ',')
{
parser.add_error(Strings::concat("expected ',' or end of text in ", plural_item_name, " list"));
return nullopt;
}
parser.next();
parser.skip_whitespace();
} while (true);
}

ExpectedS<std::vector<std::string>> parse_default_features_list(const std::string& str,
StringView origin,
TextRowCol textrowcol)
{
auto parser = Parse::ParserBase(str, origin, textrowcol);
auto opt = parse_list_until_eof<std::string>("default features", parser, &parse_feature_name);
if (!opt) return {parser.get_error()->format(), expected_right_tag};
return {std::move(opt).value_or_exit(VCPKG_LINE_INFO), expected_left_tag};
}
ExpectedS<std::vector<ParsedQualifiedSpecifier>> parse_qualified_specifier_list(const std::string& str,
StringView origin,
TextRowCol textrowcol)
{
auto parser = Parse::ParserBase(str, origin, textrowcol);
auto opt = parse_list_until_eof<ParsedQualifiedSpecifier>(
"dependencies", parser, [](ParserBase& parser) { return parse_qualified_specifier(parser); });
if (!opt) return {parser.get_error()->format(), expected_right_tag};

return {std::move(opt).value_or_exit(VCPKG_LINE_INFO), expected_left_tag};
}
ExpectedS<std::vector<Dependency>> parse_dependencies_list(const std::string& str,
StringView origin,
TextRowCol textrowcol)
{
auto parser = Parse::ParserBase(str, origin, textrowcol);
auto opt = parse_list_until_eof<Dependency>("dependencies", parser, [](ParserBase& parser) {
auto loc = parser.cur_loc();
return parse_qualified_specifier(parser).then([&](ParsedQualifiedSpecifier&& pqs) -> Optional<Dependency> {
if (pqs.triplet)
{
parser.add_error("triplet specifier not allowed in this context", loc);
return nullopt;
}
return Dependency{pqs.name, pqs.features.value_or({}), pqs.platform.value_or({})};
});
});
if (!opt) return {parser.get_error()->format(), expected_right_tag};

return {std::move(opt).value_or_exit(VCPKG_LINE_INFO), expected_left_tag};
}
}
33 changes: 0 additions & 33 deletions toolsrc/src/vcpkg/paragraphparseresult.cpp

This file was deleted.

131 changes: 129 additions & 2 deletions toolsrc/src/vcpkg/paragraphs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,139 @@
#include <vcpkg/base/util.h>

#include <vcpkg/binaryparagraph.h>
#include <vcpkg/paragraphparseresult.h>
#include <vcpkg/paragraphparser.h>
#include <vcpkg/paragraphs.h>

using namespace vcpkg::Parse;
using namespace vcpkg;

namespace vcpkg::Parse
{
static Optional<std::pair<std::string, TextRowCol>> remove_field(Paragraph* fields, const std::string& fieldname)
{
auto it = fields->find(fieldname);
if (it == fields->end())
{
return nullopt;
}

auto value = std::move(it->second);
fields->erase(it);
return value;
}

void ParagraphParser::required_field(const std::string& fieldname, std::pair<std::string&, TextRowCol&> out)
{
auto maybe_field = remove_field(&fields, fieldname);
if (const auto field = maybe_field.get())
out = std::move(*field);
else
missing_fields.push_back(fieldname);
}
void ParagraphParser::optional_field(const std::string& fieldname, std::pair<std::string&, TextRowCol&> out)
{
auto maybe_field = remove_field(&fields, fieldname);
if (auto field = maybe_field.get()) out = std::move(*field);
}
void ParagraphParser::required_field(const std::string& fieldname, std::string& out)
{
TextRowCol ignore;
required_field(fieldname, {out, ignore});
}
std::string ParagraphParser::optional_field(const std::string& fieldname)
{
std::string out;
TextRowCol ignore;
optional_field(fieldname, {out, ignore});
return out;
}
std::string ParagraphParser::required_field(const std::string& fieldname)
{
std::string out;
TextRowCol ignore;
required_field(fieldname, {out, ignore});
return out;
}

std::unique_ptr<ParseControlErrorInfo> ParagraphParser::error_info(const std::string& name) const
{
if (!fields.empty() || !missing_fields.empty())
{
auto err = std::make_unique<ParseControlErrorInfo>();
err->name = name;
err->extra_fields["CONTROL"] = Util::extract_keys(fields);
err->missing_fields["CONTROL"] = std::move(missing_fields);
err->expected_types = std::move(expected_types);
return err;
}
return nullptr;
}

template<class T, class F>
static Optional<std::vector<T>> parse_list_until_eof(StringLiteral plural_item_name, Parse::ParserBase& parser, F f)
{
std::vector<T> ret;
parser.skip_whitespace();
if (parser.at_eof()) return std::vector<T>{};
do
{
auto item = f(parser);
if (!item) return nullopt;
ret.push_back(std::move(item).value_or_exit(VCPKG_LINE_INFO));
parser.skip_whitespace();
if (parser.at_eof()) return {std::move(ret)};
if (parser.cur() != ',')
{
parser.add_error(Strings::concat("expected ',' or end of text in ", plural_item_name, " list"));
return nullopt;
}
parser.next();
parser.skip_whitespace();
} while (true);
}

ExpectedS<std::vector<std::string>> parse_default_features_list(const std::string& str,
StringView origin,
TextRowCol textrowcol)
{
auto parser = Parse::ParserBase(str, origin, textrowcol);
auto opt = parse_list_until_eof<std::string>("default features", parser, &parse_feature_name);
if (!opt) return {parser.get_error()->format(), expected_right_tag};
return {std::move(opt).value_or_exit(VCPKG_LINE_INFO), expected_left_tag};
}
ExpectedS<std::vector<ParsedQualifiedSpecifier>> parse_qualified_specifier_list(const std::string& str,
StringView origin,
TextRowCol textrowcol)
{
auto parser = Parse::ParserBase(str, origin, textrowcol);
auto opt = parse_list_until_eof<ParsedQualifiedSpecifier>(
"dependencies", parser, [](ParserBase& parser) { return parse_qualified_specifier(parser); });
if (!opt) return {parser.get_error()->format(), expected_right_tag};

return {std::move(opt).value_or_exit(VCPKG_LINE_INFO), expected_left_tag};
}
ExpectedS<std::vector<Dependency>> parse_dependencies_list(const std::string& str,
StringView origin,
TextRowCol textrowcol)
{
auto parser = Parse::ParserBase(str, origin, textrowcol);
auto opt = parse_list_until_eof<Dependency>("dependencies", parser, [](ParserBase& parser) {
auto loc = parser.cur_loc();
return parse_qualified_specifier(parser).then([&](ParsedQualifiedSpecifier&& pqs) -> Optional<Dependency> {
if (pqs.triplet)
{
parser.add_error("triplet specifier not allowed in this context", loc);
return nullopt;
}
return Dependency{pqs.name, pqs.features.value_or({}), pqs.platform.value_or({})};
});
});
if (!opt) return {parser.get_error()->format(), expected_right_tag};

return {std::move(opt).value_or_exit(VCPKG_LINE_INFO), expected_left_tag};
}
}

namespace vcpkg::Paragraphs
{
struct PghParser : private Parse::ParserBase
Expand Down Expand Up @@ -91,7 +218,7 @@ namespace vcpkg::Paragraphs

if (auto p = pghs.get())
{
if (p->size() != 1) return std::error_code(ParagraphParseResult::EXPECTED_ONE_PARAGRAPH).message();
if (p->size() != 1) return {"There should be exactly one paragraph", expected_right_tag};
return std::move(p->front());
}
else
Expand Down
2 changes: 0 additions & 2 deletions toolsrc/windows-bootstrap/vcpkglib/vcpkglib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@
<ClInclude Include="..\..\include\vcpkg\metrics.h" />
<ClInclude Include="..\..\include\vcpkg\packagespec.h" />
<ClInclude Include="..\..\include\vcpkg\paragraphparser.h" />
<ClInclude Include="..\..\include\vcpkg\paragraphparseresult.h" />
<ClInclude Include="..\..\include\vcpkg\paragraphs.h" />
<ClInclude Include="..\..\include\vcpkg\portfileprovider.h" />
<ClInclude Include="..\..\include\vcpkg\postbuildlint.h" />
Expand Down Expand Up @@ -308,7 +307,6 @@
<ClCompile Include="..\..\src\vcpkg\platform-expression.cpp" />
<ClCompile Include="..\..\src\vcpkg\metrics.cpp" />
<ClCompile Include="..\..\src\vcpkg\packagespec.cpp" />
<ClCompile Include="..\..\src\vcpkg\paragraphparseresult.cpp" />
<ClCompile Include="..\..\src\vcpkg\paragraphs.cpp" />
<ClCompile Include="..\..\src\vcpkg\portfileprovider.cpp" />
<ClCompile Include="..\..\src\vcpkg\postbuildlint.buildtype.cpp" />
Expand Down

0 comments on commit 74ab3aa

Please sign in to comment.