From 74ab3aae01243119be2c2cb5edb0c187c76002f6 Mon Sep 17 00:00:00 2001 From: ras0219 <533828+ras0219@users.noreply.github.com> Date: Fri, 14 Aug 2020 11:05:18 -0700 Subject: [PATCH] [vcpkg] Restore layering and purge unused ParagraphParseResult (#12897) Co-authored-by: Robert Schumacher --- toolsrc/include/vcpkg/paragraphparseresult.h | 36 ----- toolsrc/src/vcpkg/base/parse.cpp | 127 ----------------- toolsrc/src/vcpkg/paragraphparseresult.cpp | 33 ----- toolsrc/src/vcpkg/paragraphs.cpp | 131 +++++++++++++++++- .../vcpkglib/vcpkglib.vcxproj | 2 - 5 files changed, 129 insertions(+), 200 deletions(-) delete mode 100644 toolsrc/include/vcpkg/paragraphparseresult.h delete mode 100644 toolsrc/src/vcpkg/paragraphparseresult.cpp diff --git a/toolsrc/include/vcpkg/paragraphparseresult.h b/toolsrc/include/vcpkg/paragraphparseresult.h deleted file mode 100644 index 558715bbcc..0000000000 --- a/toolsrc/include/vcpkg/paragraphparseresult.h +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once - -#include - -namespace vcpkg -{ - enum class ParagraphParseResult - { - SUCCESS = 0, - EXPECTED_ONE_PARAGRAPH - }; - - struct ParagraphParseResultCategoryImpl final : std::error_category - { - virtual const char* name() const noexcept override; - - virtual std::string message(int ev) const noexcept override; - }; - - const std::error_category& paragraph_parse_result_category(); - - std::error_code make_error_code(ParagraphParseResult e); - - ParagraphParseResult to_paragraph_parse_result(int i); - - ParagraphParseResult to_paragraph_parse_result(std::error_code ec); -} - -namespace std -{ - // Enable implicit conversion to std::error_code - template<> - struct is_error_code_enum : ::std::true_type - { - }; -} \ No newline at end of file diff --git a/toolsrc/src/vcpkg/base/parse.cpp b/toolsrc/src/vcpkg/base/parse.cpp index cd82f32120..669321c056 100644 --- a/toolsrc/src/vcpkg/base/parse.cpp +++ b/toolsrc/src/vcpkg/base/parse.cpp @@ -2,9 +2,6 @@ #include #include -#include -#include - #include using namespace vcpkg; @@ -104,128 +101,4 @@ namespace vcpkg::Parse // Avoid error loops by skipping to the end skip_to_eof(); } - - static Optional> 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 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 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 ParagraphParser::error_info(const std::string& name) const - { - if (!fields.empty() || !missing_fields.empty()) - { - auto err = std::make_unique(); - 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 - static Optional> parse_list_until_eof(StringLiteral plural_item_name, Parse::ParserBase& parser, F f) - { - std::vector ret; - parser.skip_whitespace(); - if (parser.at_eof()) return std::vector{}; - 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> 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("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> 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( - "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> parse_dependencies_list(const std::string& str, - StringView origin, - TextRowCol textrowcol) - { - auto parser = Parse::ParserBase(str, origin, textrowcol); - auto opt = parse_list_until_eof("dependencies", parser, [](ParserBase& parser) { - auto loc = parser.cur_loc(); - return parse_qualified_specifier(parser).then([&](ParsedQualifiedSpecifier&& pqs) -> Optional { - 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}; - } } diff --git a/toolsrc/src/vcpkg/paragraphparseresult.cpp b/toolsrc/src/vcpkg/paragraphparseresult.cpp deleted file mode 100644 index 1565cbe6a6..0000000000 --- a/toolsrc/src/vcpkg/paragraphparseresult.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include - -#include - -namespace vcpkg -{ - const char* ParagraphParseResultCategoryImpl::name() const noexcept { return "ParagraphParseResult"; } - - std::string ParagraphParseResultCategoryImpl::message(int ev) const noexcept - { - switch (static_cast(ev)) - { - case ParagraphParseResult::SUCCESS: return "OK"; - case ParagraphParseResult::EXPECTED_ONE_PARAGRAPH: return "There should be exactly one paragraph"; - default: Checks::unreachable(VCPKG_LINE_INFO); - } - } - - const std::error_category& paragraph_parse_result_category() - { - static ParagraphParseResultCategoryImpl instance; - return instance; - } - - std::error_code make_error_code(ParagraphParseResult e) - { - return std::error_code(static_cast(e), paragraph_parse_result_category()); - } - - ParagraphParseResult to_paragraph_parse_result(int i) { return static_cast(i); } - - ParagraphParseResult to_paragraph_parse_result(std::error_code ec) { return to_paragraph_parse_result(ec.value()); } -} diff --git a/toolsrc/src/vcpkg/paragraphs.cpp b/toolsrc/src/vcpkg/paragraphs.cpp index 414c464d0f..a1d6970630 100644 --- a/toolsrc/src/vcpkg/paragraphs.cpp +++ b/toolsrc/src/vcpkg/paragraphs.cpp @@ -5,12 +5,139 @@ #include #include -#include +#include #include using namespace vcpkg::Parse; using namespace vcpkg; +namespace vcpkg::Parse +{ + static Optional> 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 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 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 ParagraphParser::error_info(const std::string& name) const + { + if (!fields.empty() || !missing_fields.empty()) + { + auto err = std::make_unique(); + 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 + static Optional> parse_list_until_eof(StringLiteral plural_item_name, Parse::ParserBase& parser, F f) + { + std::vector ret; + parser.skip_whitespace(); + if (parser.at_eof()) return std::vector{}; + 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> 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("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> 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( + "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> parse_dependencies_list(const std::string& str, + StringView origin, + TextRowCol textrowcol) + { + auto parser = Parse::ParserBase(str, origin, textrowcol); + auto opt = parse_list_until_eof("dependencies", parser, [](ParserBase& parser) { + auto loc = parser.cur_loc(); + return parse_qualified_specifier(parser).then([&](ParsedQualifiedSpecifier&& pqs) -> Optional { + 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 @@ -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 diff --git a/toolsrc/windows-bootstrap/vcpkglib/vcpkglib.vcxproj b/toolsrc/windows-bootstrap/vcpkglib/vcpkglib.vcxproj index 660459024a..909f6e8706 100644 --- a/toolsrc/windows-bootstrap/vcpkglib/vcpkglib.vcxproj +++ b/toolsrc/windows-bootstrap/vcpkglib/vcpkglib.vcxproj @@ -222,7 +222,6 @@ - @@ -308,7 +307,6 @@ -