diff --git a/generate-parser.js b/generate-parser.js new file mode 100644 index 0000000..bcb2e4e --- /dev/null +++ b/generate-parser.js @@ -0,0 +1,87 @@ +var Generator = require('jison').Generator +var options = { + type: 'slr', + moduleType: 'commonjs', + moduleName: 'spdxparse' } + +var words = [ 'AND', 'OR', 'WITH' ] + +var quote = function(argument) { + return '\'' + argument + '\'' } + +var regexEscape = function(s) { + return s.replace(/[\^\\$*+?.()|{}\[\]\/]/g, '\\$&') } + +var handleLicensesAndExceptions = function() { + var ids = require('spdx-license-ids') + var exceptions = require('spdx-exceptions') + + // Sort tokens longest-first (both license ids and exception strings) + var tokens = ids.concat(exceptions) + tokens.sort(function(a, b) { return ( b.length - a.length ) }) + return tokens.map(function(t) { + var type = ( (ids.indexOf(t) >= 0) ? 'LICENSE' : 'EXCEPTION' ) + return [ regexEscape(t), ( 'return ' + quote(type) ) ] }) } + +var grammar = { + lex: { + macros: { }, + rules: [ + [ '$', 'return ' + quote('EOS') ], + [ '\\s+', '/* skip whitespace */' ], + [ '\\+', 'return ' + quote('PLUS') ], + [ '\\(', 'return ' + quote('OPEN') ], + [ '\\)', 'return ' + quote('CLOSE') ], + [ ':', 'return ' + quote('COLON') ], + [ 'DocumentRef-([0-9A-Za-z-+.]+)', + 'return ' + quote('DOCUMENTREF') ], + [ 'LicenseRef-([0-9A-Za-z-+.]+)', + 'return ' + quote('LICENSEREF') ] ] + .concat(words.map(function(word) { + return [ word, 'return ' + quote(word) ] })) + .concat(handleLicensesAndExceptions()) }, + operators: [ + [ 'left', 'OR' ], + [ 'left', 'AND' ], + [ 'right', 'PLUS', 'WITH' ] ], + tokens: [ + 'CLOSE', + 'COLON', + 'EXCEPTION', + 'LICENSE', + 'LICENSEREF', + 'OPEN', + 'PLUS' ] + .concat(words) + .join(' '), + start: 'start', + bnf: { + start: [ + [ 'expression EOS', 'return $$ = $1' ] ], + simpleExpression: [ + [ 'LICENSE', + '$$ = { license: yytext }' ], + [ 'LICENSE PLUS', + '$$ = { license: $1, plus: true }' ], + [ 'LICENSEREF', + '$$ = { license: yytext }' ], + [ 'DOCUMENTREF COLON LICENSEREF', + '$$ = { license: yytext }' ] ], + expression: [ + [ 'simpleExpression', + '$$ = $1' ], + [ 'simpleExpression WITH EXCEPTION', + [ '$$ = { exception: $3 }', + '$$.license = $1.license', + 'if ($1.hasOwnProperty(\'plus\')) {', + ' $$.plus = $1.plus', + '}' ] + .join('\n') ], + [ 'expression AND expression', + '$$ = { conjunction: \'and\', left: $1, right: $3 }' ], + [ 'expression OR expression', + '$$ = { conjunction: \'or\', left: $1, right: $3 }' ], + [ 'OPEN expression CLOSE', + '$$ = $2' ] ] } } + +console.log(new Generator(grammar, options).generate()) diff --git a/nodejs-spdx-expression-parse.spec b/nodejs-spdx-expression-parse.spec index 225b435..2778bbf 100644 --- a/nodejs-spdx-expression-parse.spec +++ b/nodejs-spdx-expression-parse.spec @@ -1,4 +1,51 @@ +%{?nodejs_find_provides_and_requires} +%global packagename spdx-expression-parse +%global enable_tests 1 +Name: nodejs-spdx-expression-parse +Version: 1.0.2 +Release: 1 +Summary: Parse SPDX license expressions +License: MIT and CC-BY +URL: https://github.com/kemitchell/spdx-expression-parse.js.git +Source0: https://registry.npmjs.org/spdx-expression-parse/-/spdx-expression-parse-%{version}.tgz +Source1: https://raw.githubusercontent.com/kemitchell/spdx-expression-parse.js/v%{version}/generate-parser.js +ExclusiveArch: %{nodejs_arches} noarch +BuildArch: noarch +BuildRequires: nodejs-packaging uglify-js npm(jison) npm(spdx-license-ids) +BuildRequires: npm(spdx-exceptions) +%if 0%{?enable_tests} +%endif +Requires: nodejs +%description +Parse SPDX license expressions + +%prep +%setup -q -n package +cp -p %{SOURCE1} . +rm parser.generated.js + +%build +%nodejs_symlink_deps --check +%__nodejs generate-parser.js | %{_bindir}/uglifyjs > parser.generated.js + +%install +mkdir -p %{buildroot}%{nodejs_sitelib}/%{packagename} +cp -pr package.json *.js \ + %{buildroot}%{nodejs_sitelib}/%{packagename} +%nodejs_symlink_deps + +%check +%{__nodejs} -e 'require("./")' +%if 0%{?enable_tests} +echo "There are no tests..." +%endif + +%files +%{!?_licensedir:%global license %doc} +%doc *.md +%license LICENSE +%{nodejs_sitelib}/%{packagename} %changelog -* Fri Aug 21 2020 wangchong - -1 +* Fri Aug 21 2020 wangchong - 1.0.2-1 - package init diff --git a/nodejs-spdx-expression-parse.yaml b/nodejs-spdx-expression-parse.yaml index 4e5a19c..7237a42 100644 --- a/nodejs-spdx-expression-parse.yaml +++ b/nodejs-spdx-expression-parse.yaml @@ -1,4 +1,4 @@ version-control: github -src_repo: sctp/lksctp-tools +src_repo: kemitchell/spdx-expression-parse.js tag_prefix: ^v seperator: . diff --git a/spdx-expression-parse-1.0.2.tgz b/spdx-expression-parse-1.0.2.tgz new file mode 100644 index 0000000..32ed791 Binary files /dev/null and b/spdx-expression-parse-1.0.2.tgz differ