Skip to content

Commit d64a17d

Browse files
committed
Smash the id kind token types.
1 parent 4fc5374 commit d64a17d

File tree

1 file changed

+38
-77
lines changed

1 file changed

+38
-77
lines changed

src/vcpkg/sourceparagraph.cpp

Lines changed: 38 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -797,17 +797,6 @@ namespace vcpkg
797797
{
798798
SpdxLicenseExpressionParser(StringView sv, StringView origin) : ParserBase(sv, origin, {0, 0}) { }
799799

800-
static const StringLiteral* case_insensitive_find(View<StringLiteral> lst, StringView id)
801-
{
802-
auto it =
803-
Util::find_if(lst, [id](StringLiteral el) { return Strings::case_insensitive_ascii_equals(id, el); });
804-
if (it == lst.end())
805-
{
806-
return nullptr;
807-
}
808-
809-
return it;
810-
}
811800
static constexpr bool is_idstring_element(char32_t ch) { return is_alphanumdash(ch) || ch == '.'; }
812801

813802
enum class SpdxLicenseTokenKind
@@ -818,9 +807,7 @@ namespace vcpkg
818807
And,
819808
Or,
820809
With,
821-
LicenseId,
822-
ExceptionId,
823-
UnknownId,
810+
IdString,
824811
End
825812
};
826813

@@ -831,6 +818,28 @@ namespace vcpkg
831818
SourceLoc loc;
832819
};
833820

821+
StringView match_idstring_to_allowlist(StringView token,
822+
const SourceLoc& loc,
823+
msg::MessageT<msg::value_t> error_message,
824+
View<StringLiteral> valid_ids)
825+
{
826+
if (token.starts_with("LicenseRef-"))
827+
{
828+
return token;
829+
}
830+
831+
auto it = Util::find_if(
832+
valid_ids, [token](StringLiteral el) { return Strings::case_insensitive_ascii_equals(token, el); });
833+
if (it == valid_ids.end())
834+
{
835+
add_warning(msg::format(error_message, msg::value = token), loc);
836+
return token;
837+
}
838+
839+
// case normalization
840+
return *it;
841+
}
842+
834843
std::vector<SpdxLicenseToken> tokenize()
835844
{
836845
std::vector<SpdxLicenseToken> result;
@@ -913,58 +922,15 @@ namespace vcpkg
913922
break;
914923
}
915924

916-
if (auto matched_license = case_insensitive_find(VALID_LICENSES, token))
917-
{
918-
// case normalization
919-
result.push_back({SpdxLicenseTokenKind::LicenseId, *matched_license, loc});
920-
if (cur() == '+')
921-
{
922-
// note that + must not be preceeded by whitespace
923-
result.push_back(
924-
{SpdxLicenseTokenKind::Plus, StringView{it().pointer_to_current(), 1}, loc});
925-
next();
926-
}
927-
928-
break;
929-
}
930-
931-
if (auto matched_exception = case_insensitive_find(VALID_EXCEPTIONS, token))
932-
{
933-
// case normalization
934-
result.push_back({SpdxLicenseTokenKind::ExceptionId, *matched_exception, loc});
935-
if (cur() == '+')
936-
{
937-
// note that + must not be preceeded by whitespace
938-
result.push_back(
939-
{SpdxLicenseTokenKind::Plus, StringView{it().pointer_to_current(), 1}, loc});
940-
next();
941-
}
942-
943-
break;
944-
}
945-
946-
if (token.starts_with("LicenseRef-"))
947-
{
948-
result.push_back({SpdxLicenseTokenKind::LicenseId, token, loc});
949-
if (cur() == '+')
950-
{
951-
// note that + must not be preceeded by whitespace
952-
result.push_back(
953-
{SpdxLicenseTokenKind::Plus, StringView{it().pointer_to_current(), 1}, loc});
954-
next();
955-
}
956-
957-
break;
958-
}
959-
960-
result.push_back({SpdxLicenseTokenKind::UnknownId, token, loc});
925+
result.push_back({SpdxLicenseTokenKind::IdString, token, loc});
961926
if (cur() == '+')
962927
{
963928
// note that + must not be preceeded by whitespace
964929
result.push_back(
965930
{SpdxLicenseTokenKind::Plus, StringView{it().pointer_to_current(), 1}, loc});
966931
next();
967932
}
933+
968934
break;
969935
}
970936
}
@@ -1026,9 +992,7 @@ namespace vcpkg
1026992
// note that the 'empty' case got handled emitting msgEmptyLicenseExpression above
1027993
add_error(msg::format(msgLicenseExpressionExpectCompoundFoundWith), current_token->loc);
1028994
return {};
1029-
case SpdxLicenseTokenKind::LicenseId:
1030-
case SpdxLicenseTokenKind::ExceptionId:
1031-
case SpdxLicenseTokenKind::UnknownId:
995+
case SpdxLicenseTokenKind::IdString:
1032996
// note that the 'empty' case got handled emitting msgEmptyLicenseExpression above
1033997
if (result.outcome == TryMatchOutcome::Success)
1034998
{
@@ -1107,14 +1071,11 @@ namespace vcpkg
11071071
add_error(msg::format(msgLicenseExpressionExpectLicenseFoundCompound, msg::value = current_token->text),
11081072
current_token->loc);
11091073
return {TryMatchOutcome::Failure};
1110-
case SpdxLicenseTokenKind::ExceptionId:
1111-
case SpdxLicenseTokenKind::UnknownId:
1112-
add_warning(msg::format(msgLicenseExpressionUnknownLicense, msg::value = current_token->text),
1113-
current_token->loc);
1114-
[[fallthrough]];
1115-
case SpdxLicenseTokenKind::LicenseId:
1074+
case SpdxLicenseTokenKind::IdString:
11161075
{
1117-
TryMatchResult this_result{TryMatchOutcome::Success, current_token->text.to_string()};
1076+
auto matched_license = match_idstring_to_allowlist(
1077+
current_token->text, current_token->loc, msgLicenseExpressionUnknownLicense, VALID_LICENSES);
1078+
TryMatchResult this_result{TryMatchOutcome::Success, matched_license.to_string()};
11181079
++current_token;
11191080
if (current_token->kind == SpdxLicenseTokenKind::Plus)
11201081
{
@@ -1141,18 +1102,18 @@ namespace vcpkg
11411102
msg::value = current_token->text),
11421103
current_token->loc);
11431104
return {TryMatchOutcome::Failure};
1144-
case SpdxLicenseTokenKind::LicenseId:
1145-
case SpdxLicenseTokenKind::UnknownId:
1146-
add_warning(
1147-
msg::format(msgLicenseExpressionUnknownException, msg::value = current_token->text),
1148-
current_token->loc);
1149-
[[fallthrough]];
1150-
case SpdxLicenseTokenKind::ExceptionId:
1105+
case SpdxLicenseTokenKind::IdString:
1106+
{
1107+
auto matched_exception = match_idstring_to_allowlist(current_token->text,
1108+
current_token->loc,
1109+
msgLicenseExpressionUnknownException,
1110+
VALID_EXCEPTIONS);
11511111
this_result.license_text.append(" WITH ");
1152-
this_result.license_text.append(current_token->text.data(), current_token->text.size());
1112+
this_result.license_text.append(matched_exception.data(), matched_exception.size());
11531113
++current_token;
11541114
this_result.outcome = TryMatchOutcome::SuccessMatchedWith;
11551115
break;
1116+
}
11561117
case SpdxLicenseTokenKind::End:
11571118
add_error(msg::format(msgLicenseExpressionExpectExceptionFoundEof), current_token->loc);
11581119
return {TryMatchOutcome::Failure};

0 commit comments

Comments
 (0)