spdx-vir 0.0.0 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE-CC0 +121 -0
- package/LICENSE-MIT +21 -0
- package/README.md +54 -0
- package/dist/api.d.ts +14 -0
- package/dist/api.js +25 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/parse-tokens.d.ts +72 -0
- package/dist/parse-tokens.js +174 -0
- package/dist/scan-expression.d.ts +54 -0
- package/dist/scan-expression.js +170 -0
- package/dist/spdx-exceptions.d.ts +6 -0
- package/dist/spdx-exceptions.js +983 -0
- package/dist/spdx-license-ids.d.ts +6 -0
- package/dist/spdx-license-ids.js +9128 -0
- package/dist/spdx-parse-error.d.ts +8 -0
- package/dist/spdx-parse-error.js +8 -0
- package/package.json +85 -11
package/LICENSE-CC0
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
CC0 1.0 Universal
|
|
2
|
+
|
|
3
|
+
Creative Commons Legal Code
|
|
4
|
+
|
|
5
|
+
CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
|
|
6
|
+
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
|
|
7
|
+
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
|
|
8
|
+
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
|
|
9
|
+
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
|
|
10
|
+
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
|
|
11
|
+
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
|
|
12
|
+
HEREUNDER.
|
|
13
|
+
|
|
14
|
+
Statement of Purpose
|
|
15
|
+
|
|
16
|
+
The laws of most jurisdictions throughout the world automatically confer
|
|
17
|
+
exclusive Copyright and Related Rights (defined below) upon the creator
|
|
18
|
+
and subsequent owner(s) (each and all, an "owner") of an original work of
|
|
19
|
+
authorship and/or a database (each, a "Work").
|
|
20
|
+
|
|
21
|
+
Certain owners wish to permanently relinquish those rights to a Work for
|
|
22
|
+
the purpose of contributing to a commons of creative, cultural and
|
|
23
|
+
scientific works ("Commons") that the public can reliably and without fear
|
|
24
|
+
of later claims of infringement build upon, modify, incorporate in other
|
|
25
|
+
works, reuse and redistribute as freely as possible in any form whatsoever
|
|
26
|
+
and for any purposes, including without limitation commercial purposes.
|
|
27
|
+
These owners may contribute to the Commons to promote the ideal of a free
|
|
28
|
+
culture and the further production of creative, cultural and scientific
|
|
29
|
+
works, or to gain reputation or greater distribution for their Work in
|
|
30
|
+
part through the use and efforts of others.
|
|
31
|
+
|
|
32
|
+
For these and/or other purposes and motivations, and without any
|
|
33
|
+
expectation of additional consideration or compensation, the person
|
|
34
|
+
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
|
|
35
|
+
is an owner of Copyright and Related Rights in the Work, voluntarily
|
|
36
|
+
elects to apply CC0 to the Work and publicly distribute the Work under its
|
|
37
|
+
terms, with knowledge of his or her Copyright and Related Rights in the
|
|
38
|
+
Work and the meaning and intended legal effect of CC0 on those rights.
|
|
39
|
+
|
|
40
|
+
1. Copyright and Related Rights. A Work made available under CC0 may be
|
|
41
|
+
protected by copyright and related or neighboring rights ("Copyright and
|
|
42
|
+
Related Rights"). Copyright and Related Rights include, but are not
|
|
43
|
+
limited to, the following:
|
|
44
|
+
|
|
45
|
+
i. the right to reproduce, adapt, distribute, perform, display,
|
|
46
|
+
communicate, and translate a Work;
|
|
47
|
+
ii. moral rights retained by the original author(s) and/or performer(s);
|
|
48
|
+
iii. publicity and privacy rights pertaining to a person's image or
|
|
49
|
+
likeness depicted in a Work;
|
|
50
|
+
iv. rights protecting against unfair competition in regards to a Work,
|
|
51
|
+
subject to the limitations in paragraph 4(a), below;
|
|
52
|
+
v. rights protecting the extraction, dissemination, use and reuse of data
|
|
53
|
+
in a Work;
|
|
54
|
+
vi. database rights (such as those arising under Directive 96/9/EC of the
|
|
55
|
+
European Parliament and of the Council of 11 March 1996 on the legal
|
|
56
|
+
protection of databases, and under any national implementation
|
|
57
|
+
thereof, including any amended or successor version of such
|
|
58
|
+
directive); and
|
|
59
|
+
vii. other similar, equivalent or corresponding rights throughout the
|
|
60
|
+
world based on applicable law or treaty, and any national
|
|
61
|
+
implementations thereof.
|
|
62
|
+
|
|
63
|
+
2. Waiver. To the greatest extent permitted by, but not in contravention
|
|
64
|
+
of, applicable law, Affirmer hereby overtly, fully, permanently,
|
|
65
|
+
irrevocably and unconditionally waives, abandons, and surrenders all of
|
|
66
|
+
Affirmer's Copyright and Related Rights and associated claims and causes
|
|
67
|
+
of action, whether now known or unknown (including existing as well as
|
|
68
|
+
future claims and causes of action), in the Work (i) in all territories
|
|
69
|
+
worldwide, (ii) for the maximum duration provided by applicable law or
|
|
70
|
+
treaty (including future time extensions), (iii) in any current or future
|
|
71
|
+
medium and for any number of copies, and (iv) for any purpose whatsoever,
|
|
72
|
+
including without limitation commercial, advertising or promotional
|
|
73
|
+
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
|
|
74
|
+
member of the public at large and to the detriment of Affirmer's heirs and
|
|
75
|
+
successors, fully intending that such Waiver shall not be subject to
|
|
76
|
+
revocation, rescission, cancellation, termination, or any other legal or
|
|
77
|
+
equitable action to disrupt the quiet enjoyment of the Work by the public
|
|
78
|
+
as contemplated by Affirmer's express Statement of Purpose.
|
|
79
|
+
|
|
80
|
+
3. Public License Fallback. Should any part of the Waiver for any reason
|
|
81
|
+
be judged legally invalid or ineffective under applicable law, then the
|
|
82
|
+
Waiver shall be preserved to the maximum extent permitted taking into
|
|
83
|
+
account Affirmer's express Statement of Purpose. In addition, to the
|
|
84
|
+
extent the Waiver is so judged Affirmer hereby grants to each affected
|
|
85
|
+
person a royalty-free, non transferable, non sublicensable, non exclusive,
|
|
86
|
+
irrevocable and unconditional license to exercise Affirmer's Copyright and
|
|
87
|
+
Related Rights in the Work (i) in all territories worldwide, (ii) for the
|
|
88
|
+
maximum duration provided by applicable law or treaty (including future
|
|
89
|
+
time extensions), (iii) in any current or future medium and for any number
|
|
90
|
+
of copies, and (iv) for any purpose whatsoever, including without
|
|
91
|
+
limitation commercial, advertising or promotional purposes (the
|
|
92
|
+
"License"). The License shall be deemed effective as of the date CC0 was
|
|
93
|
+
applied by Affirmer to the Work. Should any part of the License for any
|
|
94
|
+
reason be judged legally invalid or ineffective under applicable law, such
|
|
95
|
+
partial invalidity or ineffectiveness shall not invalidate the remainder
|
|
96
|
+
of the License, and in such case Affirmer hereby affirms that he or she
|
|
97
|
+
will not (i) exercise any of his or her remaining Copyright and Related
|
|
98
|
+
Rights in the Work or (ii) assert any associated claims and causes of
|
|
99
|
+
action with respect to the Work, in either case contrary to Affirmer's
|
|
100
|
+
express Statement of Purpose.
|
|
101
|
+
|
|
102
|
+
4. Limitations and Disclaimers.
|
|
103
|
+
|
|
104
|
+
a. No trademark or patent rights held by Affirmer are waived, abandoned,
|
|
105
|
+
surrendered, licensed or otherwise affected by this document.
|
|
106
|
+
b. Affirmer offers the Work as-is and makes no representations or
|
|
107
|
+
warranties of any kind concerning the Work, express, implied,
|
|
108
|
+
statutory or otherwise, including without limitation warranties of
|
|
109
|
+
title, merchantability, fitness for a particular purpose, non
|
|
110
|
+
infringement, or the absence of latent or other defects, accuracy, or
|
|
111
|
+
the present or absence of errors, whether or not discoverable, all to
|
|
112
|
+
the greatest extent permissible under applicable law.
|
|
113
|
+
c. Affirmer disclaims responsibility for clearing rights of other persons
|
|
114
|
+
that may apply to the Work or any use thereof, including without
|
|
115
|
+
limitation any person's Copyright and Related Rights in the Work.
|
|
116
|
+
Further, Affirmer disclaims responsibility for obtaining any necessary
|
|
117
|
+
consents, permissions or other rights required for any use of the
|
|
118
|
+
Work.
|
|
119
|
+
d. Affirmer understands and acknowledges that Creative Commons is not a
|
|
120
|
+
party to this document and has no duty or obligation with respect to
|
|
121
|
+
this CC0 or use of the Work.
|
package/LICENSE-MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 electrovir
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# spdx-vir
|
|
2
|
+
|
|
3
|
+
Parses and validates license strings strictly according to the SPDX grammar: https://spdx.github.io/spdx-spec/v2.3/SPDX-license-expressions.
|
|
4
|
+
|
|
5
|
+
License ids have been extracted from https://spdx.org/licenses/licenses.json and exceptions from https://spdx.org/licenses/exceptions.json.
|
|
6
|
+
|
|
7
|
+
Based on https://www.npmjs.com/package/spdx-expression-parse but more strict to the grammar and written in modern ESM syntax.
|
|
8
|
+
|
|
9
|
+
Test it out here: https://codepen.io/electrovir/pen/yyVjVQR
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm i spdx-vir
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
Validating expressions:
|
|
20
|
+
|
|
21
|
+
<!-- example-link: src/readme-examples/validate.example.ts -->
|
|
22
|
+
|
|
23
|
+
```TypeScript
|
|
24
|
+
import {isValidSpdxExpression} from 'spdx-vir';
|
|
25
|
+
|
|
26
|
+
// returns true
|
|
27
|
+
isValidSpdxExpression('(MIT OR CC0-1.0)');
|
|
28
|
+
|
|
29
|
+
// returns false
|
|
30
|
+
isValidSpdxExpression('(MIT or CC0 1.0)');
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Parsing expressions:
|
|
34
|
+
|
|
35
|
+
<!-- example-link: src/readme-examples/parse.example.ts -->
|
|
36
|
+
|
|
37
|
+
```TypeScript
|
|
38
|
+
import {parseSpdxExpression, SpdxConjunction} from 'spdx-vir';
|
|
39
|
+
|
|
40
|
+
parseSpdxExpression('(MIT OR CC0-1.0)');
|
|
41
|
+
// outputs the following:
|
|
42
|
+
const output = {
|
|
43
|
+
conjunction: SpdxConjunction.OR,
|
|
44
|
+
left: {
|
|
45
|
+
license: 'MIT',
|
|
46
|
+
},
|
|
47
|
+
right: {
|
|
48
|
+
license: 'CC0-1.0',
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
// throws a SpdxParseError
|
|
53
|
+
parseSpdxExpression('(MIT or CC0 1.0)');
|
|
54
|
+
```
|
package/dist/api.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type SpdxExpression } from './parse-tokens.js';
|
|
2
|
+
/**
|
|
3
|
+
* Parse the given source string as SPDX license syntax.
|
|
4
|
+
*
|
|
5
|
+
* @category Main
|
|
6
|
+
* @throws SpdxParseError when parsing fails.
|
|
7
|
+
*/
|
|
8
|
+
export declare function parseSpdxExpression(source: string): SpdxExpression;
|
|
9
|
+
/**
|
|
10
|
+
* Checks if the given source string is valid SPDX license syntax.
|
|
11
|
+
*
|
|
12
|
+
* @category Main
|
|
13
|
+
*/
|
|
14
|
+
export declare function isValidSpdxExpression(source: string): boolean;
|
package/dist/api.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { parseRawTokens } from './parse-tokens.js';
|
|
2
|
+
import { scanRawExpression } from './scan-expression.js';
|
|
3
|
+
/**
|
|
4
|
+
* Parse the given source string as SPDX license syntax.
|
|
5
|
+
*
|
|
6
|
+
* @category Main
|
|
7
|
+
* @throws SpdxParseError when parsing fails.
|
|
8
|
+
*/
|
|
9
|
+
export function parseSpdxExpression(source) {
|
|
10
|
+
return parseRawTokens(scanRawExpression(source));
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Checks if the given source string is valid SPDX license syntax.
|
|
14
|
+
*
|
|
15
|
+
* @category Main
|
|
16
|
+
*/
|
|
17
|
+
export function isValidSpdxExpression(source) {
|
|
18
|
+
try {
|
|
19
|
+
parseSpdxExpression(source);
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { type SpdxToken } from './scan-expression.js';
|
|
2
|
+
/**
|
|
3
|
+
* The logical conjunction joining two halves of an SPDX expression.
|
|
4
|
+
*
|
|
5
|
+
* @category Internal
|
|
6
|
+
*/
|
|
7
|
+
export declare enum SpdxConjunction {
|
|
8
|
+
AND = "AND",
|
|
9
|
+
OR = "OR"
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* A single license node within a parsed SPDX expression.
|
|
13
|
+
*
|
|
14
|
+
* @category Internal
|
|
15
|
+
*/
|
|
16
|
+
export type SpdxLicenseInfo = {
|
|
17
|
+
license: string;
|
|
18
|
+
plus?: boolean;
|
|
19
|
+
exception?: string;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* A binary node joining two SPDX expressions with a conjunction.
|
|
23
|
+
*
|
|
24
|
+
* @category Internal
|
|
25
|
+
*/
|
|
26
|
+
export type SpdxConjunctionInfo = {
|
|
27
|
+
left: SpdxExpression;
|
|
28
|
+
conjunction: SpdxConjunction;
|
|
29
|
+
right: SpdxExpression;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* A parsed SPDX expression: either a single license or a conjunction of two expressions.
|
|
33
|
+
*
|
|
34
|
+
* @category Internal
|
|
35
|
+
*/
|
|
36
|
+
export type SpdxExpression = SpdxLicenseInfo | SpdxConjunctionInfo;
|
|
37
|
+
/**
|
|
38
|
+
* Parses a flat list of {@link SpdxToken}s into an {@link SpdxExpression} tree.
|
|
39
|
+
*
|
|
40
|
+
* The ABNF grammar in the spec is totally ambiguous. This parser follows the operator precedence
|
|
41
|
+
* defined in the `Order of Precedence and Parentheses` section.
|
|
42
|
+
*
|
|
43
|
+
* Ported from
|
|
44
|
+
* https://github.com/jslicense/spdx-expression-parse.js/blob/bb753f168e3364649eee90fb5d04e418e2d64d7b/parse.js,
|
|
45
|
+
* which has the following license:
|
|
46
|
+
*
|
|
47
|
+
* The MIT License
|
|
48
|
+
*
|
|
49
|
+
* Copyright (c) 2015 Kyle E. Mitchell & other authors listed in AUTHORS
|
|
50
|
+
*
|
|
51
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
52
|
+
* a copy of this software and associated documentation files (the
|
|
53
|
+
* "Software"), to deal in the Software without restriction, including
|
|
54
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
|
55
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
56
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
|
57
|
+
* the following conditions:
|
|
58
|
+
*
|
|
59
|
+
* The above copyright notice and this permission notice shall be included
|
|
60
|
+
* in all copies or substantial portions of the Software.
|
|
61
|
+
*
|
|
62
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
63
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
64
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
65
|
+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
66
|
+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
67
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
68
|
+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
69
|
+
*
|
|
70
|
+
* @category Internal
|
|
71
|
+
*/
|
|
72
|
+
export declare function parseRawTokens(tokens: ReadonlyArray<SpdxToken>): SpdxExpression;
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { SpdxTokenType } from './scan-expression.js';
|
|
2
|
+
import { SpdxParseError } from './spdx-parse-error.js';
|
|
3
|
+
/**
|
|
4
|
+
* The logical conjunction joining two halves of an SPDX expression.
|
|
5
|
+
*
|
|
6
|
+
* @category Internal
|
|
7
|
+
*/
|
|
8
|
+
export var SpdxConjunction;
|
|
9
|
+
(function (SpdxConjunction) {
|
|
10
|
+
SpdxConjunction["AND"] = "AND";
|
|
11
|
+
SpdxConjunction["OR"] = "OR";
|
|
12
|
+
})(SpdxConjunction || (SpdxConjunction = {}));
|
|
13
|
+
/**
|
|
14
|
+
* Parses a flat list of {@link SpdxToken}s into an {@link SpdxExpression} tree.
|
|
15
|
+
*
|
|
16
|
+
* The ABNF grammar in the spec is totally ambiguous. This parser follows the operator precedence
|
|
17
|
+
* defined in the `Order of Precedence and Parentheses` section.
|
|
18
|
+
*
|
|
19
|
+
* Ported from
|
|
20
|
+
* https://github.com/jslicense/spdx-expression-parse.js/blob/bb753f168e3364649eee90fb5d04e418e2d64d7b/parse.js,
|
|
21
|
+
* which has the following license:
|
|
22
|
+
*
|
|
23
|
+
* The MIT License
|
|
24
|
+
*
|
|
25
|
+
* Copyright (c) 2015 Kyle E. Mitchell & other authors listed in AUTHORS
|
|
26
|
+
*
|
|
27
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
28
|
+
* a copy of this software and associated documentation files (the
|
|
29
|
+
* "Software"), to deal in the Software without restriction, including
|
|
30
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
|
31
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
32
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
|
33
|
+
* the following conditions:
|
|
34
|
+
*
|
|
35
|
+
* The above copyright notice and this permission notice shall be included
|
|
36
|
+
* in all copies or substantial portions of the Software.
|
|
37
|
+
*
|
|
38
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
39
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
40
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
41
|
+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
42
|
+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
43
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
44
|
+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
45
|
+
*
|
|
46
|
+
* @category Internal
|
|
47
|
+
*/
|
|
48
|
+
export function parseRawTokens(tokens) {
|
|
49
|
+
let index = 0;
|
|
50
|
+
function hasMore() {
|
|
51
|
+
return index < tokens.length;
|
|
52
|
+
}
|
|
53
|
+
function token() {
|
|
54
|
+
return hasMore() ? tokens[index] : undefined;
|
|
55
|
+
}
|
|
56
|
+
function next() {
|
|
57
|
+
/**
|
|
58
|
+
* `next` is only ever called after a `token()` check has already confirmed that a token
|
|
59
|
+
* exists, so `hasMore()` is always true at this point. This guard is kept as an invariant
|
|
60
|
+
* safeguard but is unreachable in practice, hence the coverage exclusion.
|
|
61
|
+
*/
|
|
62
|
+
/* c8 ignore next 3 */
|
|
63
|
+
if (!hasMore()) {
|
|
64
|
+
throw new SpdxParseError('Unexpected end of expression');
|
|
65
|
+
}
|
|
66
|
+
index++;
|
|
67
|
+
}
|
|
68
|
+
function parseOperator(operator) {
|
|
69
|
+
const currentToken = token();
|
|
70
|
+
if (currentToken &&
|
|
71
|
+
currentToken.type === SpdxTokenType.Operator &&
|
|
72
|
+
operator === currentToken.string) {
|
|
73
|
+
next();
|
|
74
|
+
return currentToken.string;
|
|
75
|
+
}
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
function parseWith() {
|
|
79
|
+
if (parseOperator('WITH')) {
|
|
80
|
+
const currentToken = token();
|
|
81
|
+
if (currentToken && currentToken.type === SpdxTokenType.Exception) {
|
|
82
|
+
next();
|
|
83
|
+
return currentToken.string;
|
|
84
|
+
}
|
|
85
|
+
throw new SpdxParseError('Expected exception after `WITH`');
|
|
86
|
+
}
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
function parseLicenseRef() {
|
|
90
|
+
/**
|
|
91
|
+
* Everything is concatenated into one string for backward-compatibility but it could be
|
|
92
|
+
* better to return a nice structure.
|
|
93
|
+
*/
|
|
94
|
+
const begin = index;
|
|
95
|
+
let string = '';
|
|
96
|
+
const documentRefToken = token();
|
|
97
|
+
if (documentRefToken && documentRefToken.type === SpdxTokenType.DocumentRef) {
|
|
98
|
+
next();
|
|
99
|
+
string += 'DocumentRef-' + documentRefToken.string + ':';
|
|
100
|
+
if (!parseOperator(':')) {
|
|
101
|
+
throw new SpdxParseError('Expected `:` after `DocumentRef-...`');
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const licenseRefToken = token();
|
|
105
|
+
if (licenseRefToken && licenseRefToken.type === SpdxTokenType.LicenseRef) {
|
|
106
|
+
next();
|
|
107
|
+
string += 'LicenseRef-' + licenseRefToken.string;
|
|
108
|
+
return {
|
|
109
|
+
license: string,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
index = begin;
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
function parseLicense() {
|
|
116
|
+
const currentToken = token();
|
|
117
|
+
if (currentToken && currentToken.type === SpdxTokenType.License) {
|
|
118
|
+
next();
|
|
119
|
+
const node = {
|
|
120
|
+
license: currentToken.string,
|
|
121
|
+
};
|
|
122
|
+
if (parseOperator('+')) {
|
|
123
|
+
node.plus = true;
|
|
124
|
+
}
|
|
125
|
+
const exception = parseWith();
|
|
126
|
+
if (exception) {
|
|
127
|
+
node.exception = exception;
|
|
128
|
+
}
|
|
129
|
+
return node;
|
|
130
|
+
}
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
function parseParenthesizedExpression() {
|
|
134
|
+
const left = parseOperator('(');
|
|
135
|
+
if (!left) {
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
const expr = parseExpression();
|
|
139
|
+
if (!parseOperator(')')) {
|
|
140
|
+
throw new SpdxParseError('Expected `)`');
|
|
141
|
+
}
|
|
142
|
+
return expr;
|
|
143
|
+
}
|
|
144
|
+
function parseAtom() {
|
|
145
|
+
return parseParenthesizedExpression() || parseLicenseRef() || parseLicense();
|
|
146
|
+
}
|
|
147
|
+
function makeBinaryOpParser(operator, conjunction, nextParser) {
|
|
148
|
+
return function parseBinaryOp() {
|
|
149
|
+
const left = nextParser();
|
|
150
|
+
if (!left) {
|
|
151
|
+
return undefined;
|
|
152
|
+
}
|
|
153
|
+
else if (!parseOperator(operator)) {
|
|
154
|
+
return left;
|
|
155
|
+
}
|
|
156
|
+
const right = parseBinaryOp();
|
|
157
|
+
if (!right) {
|
|
158
|
+
throw new SpdxParseError('Expected expression');
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
left,
|
|
162
|
+
conjunction,
|
|
163
|
+
right,
|
|
164
|
+
};
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
const parseAnd = makeBinaryOpParser('AND', SpdxConjunction.AND, parseAtom);
|
|
168
|
+
const parseExpression = makeBinaryOpParser('OR', SpdxConjunction.OR, parseAnd);
|
|
169
|
+
const node = parseExpression();
|
|
170
|
+
if (!node || hasMore()) {
|
|
171
|
+
throw new SpdxParseError('Syntax error');
|
|
172
|
+
}
|
|
173
|
+
return node;
|
|
174
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The kind of token produced by {@link scanRawExpression} when reading an SPDX expression.
|
|
3
|
+
*
|
|
4
|
+
* @category Internal
|
|
5
|
+
*/
|
|
6
|
+
export declare enum SpdxTokenType {
|
|
7
|
+
Operator = "OPERATOR",
|
|
8
|
+
DocumentRef = "DOCUMENTREF",
|
|
9
|
+
LicenseRef = "LICENSEREF",
|
|
10
|
+
License = "LICENSE",
|
|
11
|
+
Exception = "EXCEPTION"
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* A single lexical token scanned from an SPDX expression.
|
|
15
|
+
*
|
|
16
|
+
* @category Internal
|
|
17
|
+
*/
|
|
18
|
+
export type SpdxToken = {
|
|
19
|
+
type: SpdxTokenType;
|
|
20
|
+
string: string;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Scans an SPDX expression string into a flat list of {@link SpdxToken}s.
|
|
24
|
+
*
|
|
25
|
+
* Ported from
|
|
26
|
+
* https://github.com/jslicense/spdx-expression-parse.js/blob/bb753f168e3364649eee90fb5d04e418e2d64d7b/scan.js,
|
|
27
|
+
* which has the following license:
|
|
28
|
+
*
|
|
29
|
+
* The MIT License
|
|
30
|
+
*
|
|
31
|
+
* Copyright (c) 2015 Kyle E. Mitchell & other authors listed in AUTHORS
|
|
32
|
+
*
|
|
33
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
34
|
+
* a copy of this software and associated documentation files (the
|
|
35
|
+
* "Software"), to deal in the Software without restriction, including
|
|
36
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
|
37
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
38
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
|
39
|
+
* the following conditions:
|
|
40
|
+
*
|
|
41
|
+
* The above copyright notice and this permission notice shall be included
|
|
42
|
+
* in all copies or substantial portions of the Software.
|
|
43
|
+
*
|
|
44
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
45
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
46
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
47
|
+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
48
|
+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
49
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
50
|
+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
51
|
+
*
|
|
52
|
+
* @category Internal
|
|
53
|
+
*/
|
|
54
|
+
export declare function scanRawExpression(source: string): SpdxToken[];
|