@@ -797,17 +797,6 @@ namespace vcpkg
797
797
{
798
798
SpdxLicenseExpressionParser (StringView sv, StringView origin) : ParserBase(sv, origin, {0 , 0 }) { }
799
799
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
- }
811
800
static constexpr bool is_idstring_element (char32_t ch) { return is_alphanumdash (ch) || ch == ' .' ; }
812
801
813
802
enum class SpdxLicenseTokenKind
@@ -818,9 +807,7 @@ namespace vcpkg
818
807
And,
819
808
Or,
820
809
With,
821
- LicenseId,
822
- ExceptionId,
823
- UnknownId,
810
+ IdString,
824
811
End
825
812
};
826
813
@@ -831,6 +818,28 @@ namespace vcpkg
831
818
SourceLoc loc;
832
819
};
833
820
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
+
834
843
std::vector<SpdxLicenseToken> tokenize ()
835
844
{
836
845
std::vector<SpdxLicenseToken> result;
@@ -913,58 +922,15 @@ namespace vcpkg
913
922
break ;
914
923
}
915
924
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});
961
926
if (cur () == ' +' )
962
927
{
963
928
// note that + must not be preceeded by whitespace
964
929
result.push_back (
965
930
{SpdxLicenseTokenKind::Plus, StringView{it ().pointer_to_current (), 1 }, loc});
966
931
next ();
967
932
}
933
+
968
934
break ;
969
935
}
970
936
}
@@ -1026,9 +992,7 @@ namespace vcpkg
1026
992
// note that the 'empty' case got handled emitting msgEmptyLicenseExpression above
1027
993
add_error (msg::format (msgLicenseExpressionExpectCompoundFoundWith), current_token->loc );
1028
994
return {};
1029
- case SpdxLicenseTokenKind::LicenseId:
1030
- case SpdxLicenseTokenKind::ExceptionId:
1031
- case SpdxLicenseTokenKind::UnknownId:
995
+ case SpdxLicenseTokenKind::IdString:
1032
996
// note that the 'empty' case got handled emitting msgEmptyLicenseExpression above
1033
997
if (result.outcome == TryMatchOutcome::Success)
1034
998
{
@@ -1107,14 +1071,11 @@ namespace vcpkg
1107
1071
add_error (msg::format (msgLicenseExpressionExpectLicenseFoundCompound, msg::value = current_token->text ),
1108
1072
current_token->loc );
1109
1073
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:
1116
1075
{
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 ()};
1118
1079
++current_token;
1119
1080
if (current_token->kind == SpdxLicenseTokenKind::Plus)
1120
1081
{
@@ -1141,18 +1102,18 @@ namespace vcpkg
1141
1102
msg::value = current_token->text ),
1142
1103
current_token->loc );
1143
1104
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);
1151
1111
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 ());
1153
1113
++current_token;
1154
1114
this_result.outcome = TryMatchOutcome::SuccessMatchedWith;
1155
1115
break ;
1116
+ }
1156
1117
case SpdxLicenseTokenKind::End:
1157
1118
add_error (msg::format (msgLicenseExpressionExpectExceptionFoundEof), current_token->loc );
1158
1119
return {TryMatchOutcome::Failure};
0 commit comments