Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion spdxexp/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ func (t *tokenStream) parseLicenseRef() *node {
ref := referenceNodePartial{documentRef: "", hasDocumentRef: false, licenseRef: ""}

token := t.peek()
if token == nil {
// end of stream, so no license reference to parse
return nil
}
if token.role == documentRefToken {
ref.documentRef = token.value
ref.hasDocumentRef = true
Expand All @@ -294,6 +298,13 @@ func (t *tokenStream) parseLicenseRef() *node {
}

token = t.peek()
if token == nil && ref.hasDocumentRef {
t.err = errors.New("expected 'LicenseRef-...' after 'DocumentRef-...'")
return nil
} else if token == nil {
// not found is not an error as long as DocumentRef and : weren't the previous tokens
return nil
}
if token.role != licenseRefToken && ref.hasDocumentRef {
t.err = errors.New("expected 'LicenseRef-...' after 'DocumentRef-...'")
return nil
Expand All @@ -315,7 +326,7 @@ func (t *tokenStream) parseLicenseRef() *node {
// an error is returned. Advances the index if a valid license is found.
func (t *tokenStream) parseLicense() *node {
token := t.peek()
if token.role != licenseToken {
if token == nil || token.role != licenseToken {
return nil
}
t.next()
Expand Down Expand Up @@ -361,6 +372,10 @@ func (t *tokenStream) parseLicense() *node {
// Advances the index if the operator is found.
func (t *tokenStream) parseOperator(operator string) *string {
token := t.peek()
if token == nil {
// end of stream, so the requested operator is not present
return nil
}
if token.role == operatorToken && token.value == operator {
t.next()
return &(token.value)
Expand Down
3 changes: 3 additions & 0 deletions spdxexp/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ func TestParse(t *testing.T) {
{"invalid license", "NON-EXISTENT-LICENSE", nil, "",
errors.New("unknown license 'NON-EXISTENT-LICENSE' at offset 0")},

{"dangling open parenthesis", "(", nil, "",
errors.New("expected node, but found none")},

{"OR Expression", "MIT OR Apache-2.0",
&node{
role: expressionNode,
Expand Down