apply a regex solution instead of tokenizer
parent
e4118385a0
commit
b5d286e27b
|
@ -192,115 +192,69 @@ class SpdxLicense
|
||||||
* @param string $license
|
* @param string $license
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
|
*
|
||||||
* @throws \RuntimeException
|
* @throws \RuntimeException
|
||||||
*/
|
*/
|
||||||
private function isValidLicenseString($license)
|
private function isValidLicenseString($license)
|
||||||
{
|
{
|
||||||
$tokens = array(
|
$licenses = array_map('preg_quote', array_keys($this->licenses));
|
||||||
'po' => '\(',
|
sort($licenses);
|
||||||
'pc' => '\)',
|
$licenses = array_reverse($licenses);
|
||||||
'op' => '(?:or|OR|and|AND)',
|
$licenses = implode('|', $licenses);
|
||||||
'wi' => '(?:with|WITH)',
|
|
||||||
'lix' => '(?:NONE|NOASSERTION)',
|
|
||||||
'lir' => 'LicenseRef-\d+',
|
|
||||||
'lic' => '[-_.a-zA-Z0-9]{3,}\+?',
|
|
||||||
'ws' => '\s+',
|
|
||||||
'_' => '.',
|
|
||||||
);
|
|
||||||
|
|
||||||
$next = function () use ($license, $tokens) {
|
$exceptions = array_map('preg_quote', array_keys($this->exceptions));
|
||||||
static $offset = 0;
|
sort($exceptions);
|
||||||
|
$exceptions = array_reverse($exceptions);
|
||||||
|
$exceptions = implode('|', $exceptions);
|
||||||
|
|
||||||
if ($offset >= strlen($license)) {
|
$regex = "{
|
||||||
return null;
|
(?(DEFINE)
|
||||||
}
|
# idstring: 1*( ALPHA / DIGIT / - / . )
|
||||||
|
(?<idstring>[\pL\pN\-\.]{1,})
|
||||||
|
|
||||||
foreach ($tokens as $name => $token) {
|
# license-id: taken from list
|
||||||
if (false === $r = preg_match('{' . $token . '}', $license, $matches, PREG_OFFSET_CAPTURE, $offset)) {
|
(?<licenseid>${licenses})
|
||||||
throw new \RuntimeException('Pattern for token %s failed (regex error).', $name);
|
|
||||||
}
|
|
||||||
if ($r === 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if ($matches[0][1] !== $offset) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$offset += strlen($matches[0][0]);
|
|
||||||
|
|
||||||
return array($name, $matches[0][0]);
|
# license-exception-id: taken from list
|
||||||
}
|
(?<licenseexceptionid>${exceptions})
|
||||||
|
|
||||||
throw new \RuntimeException(
|
# license-ref: [DocumentRef-1*(idstring):]LicenseRef-1*(idstring)
|
||||||
'At least the last pattern needs to match, but it did not (dot-match-all is missing?).'
|
(?<licenseref>(?:DocumentRef-(?&idstring):)?LicenseRef-(?&idstring))
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
$open = 0;
|
# simple-expresssion: license-id / license-id+ / license-ref
|
||||||
$with = false;
|
(?<simple_expression>(?&licenseid)\+? | (?&licenseid) | (?&licenseref))
|
||||||
$require = true;
|
|
||||||
$lastop = null;
|
|
||||||
|
|
||||||
while (list($token, $string) = $next()) {
|
# compound expression: 1*(
|
||||||
switch ($token) {
|
# simple-expression /
|
||||||
case 'po':
|
# simple-expression WITH license-exception-id /
|
||||||
if ($open || !$require || $with) {
|
# compound-expression AND compound-expression /
|
||||||
return false;
|
# compound-expression OR compound-expression
|
||||||
}
|
# ) / ( compound-expression ) )
|
||||||
$open = 1;
|
(?<compound_head>
|
||||||
break;
|
(?&simple_expression) ( \s+ (?:with|WITH) \s+ (?&licenseexceptionid))?
|
||||||
case 'pc':
|
| \( \s* (?&compound_expression) \s*\)
|
||||||
if ($open !== 1 || $require || !$lastop || $with) {
|
)
|
||||||
return false;
|
(?<compound_expression>
|
||||||
}
|
(?&compound_head) (?: \s+ (?:and|AND|or|OR) \s+ (?&compound_expression))?
|
||||||
$open = 2;
|
)
|
||||||
break;
|
|
||||||
case 'op':
|
# license-expression: 1*1(simple-expression / compound-expression)
|
||||||
if ($require || !$open || $with) {
|
(?<license_expression>NONE | NOASSERTION | (?&compound_expression) | (?&simple_expression))
|
||||||
return false;
|
) # end of define
|
||||||
}
|
|
||||||
$lastop || $lastop = $string;
|
^(?&license_expression)$
|
||||||
if ($lastop !== $string) {
|
}x";
|
||||||
return false;
|
|
||||||
}
|
$match = preg_match($regex, $license);
|
||||||
$require = true;
|
|
||||||
break;
|
if (0 === $match) {
|
||||||
case 'wi':
|
return false;
|
||||||
$with = true;
|
|
||||||
break;
|
|
||||||
case 'lix':
|
|
||||||
if ($open || $with) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
goto lir;
|
|
||||||
case 'lic':
|
|
||||||
if ($with && $this->isValidExceptionIdentifier($string)) {
|
|
||||||
$require = true;
|
|
||||||
$with = false;
|
|
||||||
goto lir;
|
|
||||||
}
|
|
||||||
if ($with) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!$this->isValidLicenseIdentifier(rtrim($string, '+'))) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// Fall-through intended
|
|
||||||
case 'lir':
|
|
||||||
lir:
|
|
||||||
if (!$require) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
$require = false;
|
|
||||||
break;
|
|
||||||
case 'ws':
|
|
||||||
break;
|
|
||||||
case '_':
|
|
||||||
return false;
|
|
||||||
default:
|
|
||||||
throw new \RuntimeException(sprintf('Unparsed token: %s.', print_r($token, true)));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return !($open % 2 || $require || $with);
|
if (false === $match) {
|
||||||
|
throw new \RuntimeException('Regex failed to compile/run.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,6 +39,7 @@ class SpdxLicenseTest extends TestCase
|
||||||
"GPL-2.0 with Autoconf-exception-2.0",
|
"GPL-2.0 with Autoconf-exception-2.0",
|
||||||
"GPL-2.0 WITH Autoconf-exception-2.0",
|
"GPL-2.0 WITH Autoconf-exception-2.0",
|
||||||
"GPL-2.0+ WITH Autoconf-exception-2.0",
|
"GPL-2.0+ WITH Autoconf-exception-2.0",
|
||||||
|
"(GPL-3.0 and GPL-2.0 or GPL-3.0+)",
|
||||||
),
|
),
|
||||||
$identifiers
|
$identifiers
|
||||||
);
|
);
|
||||||
|
@ -57,7 +58,6 @@ class SpdxLicenseTest extends TestCase
|
||||||
array(array()),
|
array(array()),
|
||||||
array("The system pwns you"),
|
array("The system pwns you"),
|
||||||
array("()"),
|
array("()"),
|
||||||
array("(MIT)"),
|
|
||||||
array("(MIT"),
|
array("(MIT"),
|
||||||
array("MIT)"),
|
array("MIT)"),
|
||||||
array("MIT NONE"),
|
array("MIT NONE"),
|
||||||
|
@ -66,8 +66,6 @@ class SpdxLicenseTest extends TestCase
|
||||||
array("(MIT and MIT) MIT"),
|
array("(MIT and MIT) MIT"),
|
||||||
array(array("LGPL-2.0", "The system pwns you")),
|
array(array("LGPL-2.0", "The system pwns you")),
|
||||||
array("and GPL-3.0+"),
|
array("and GPL-3.0+"),
|
||||||
array("EUDatagrid and GPL-3.0+"),
|
|
||||||
array("(GPL-3.0 and GPL-2.0 or GPL-3.0+)"),
|
|
||||||
array("(EUDatagrid and GPL-3.0+ and )"),
|
array("(EUDatagrid and GPL-3.0+ and )"),
|
||||||
array("(EUDatagrid xor GPL-3.0+)"),
|
array("(EUDatagrid xor GPL-3.0+)"),
|
||||||
array("(MIT Or MIT)"),
|
array("(MIT Or MIT)"),
|
||||||
|
|
Loading…
Reference in New Issue