spdxexpr

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2026 License: Apache-2.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package spdxexpr parses SPDX 2.3 license expressions with a bounded grammar and a pinned license list. It preserves AND, OR, parentheses, and WITH exceptions as a tree; it never flattens an expression into a bag of names, never maps NOASSERTION/NONE/UNLICENSED or unknown identifiers to a license, and reports what it could not recognize.

Grammar (SPDX 2.3 Annex D):

expression  := disjunction
disjunction := conjunction ( "OR" conjunction )*
conjunction := primary ( "AND" primary )*
primary     := simple [ "WITH" exception ] | "(" expression ")"
simple      := license-id [ "+" ] | "LicenseRef-" idstring | "DocumentRef-" idstring ":" "LicenseRef-" idstring

Index

Constants

View Source
const ListVersion = "3.27.0"

ListVersion is the pinned SPDX License List release embedded in this package. Recorded on every parsed evidence row so a list upgrade is a new resolver version, not a silent change.

Variables

This section is empty.

Functions

func Count

func Count() (licenses, exceptions int)

Count reports the embedded list sizes for tests and diagnostics.

func Equal

func Equal(left, right *Node) bool

Equal reports structural equality (same tree, same identifiers).

Types

type Kind

type Kind string

Kind is the node type of a parsed expression.

const (
	KindLicense   Kind = "license"
	KindWith      Kind = "with"
	KindAnd       Kind = "and"
	KindOr        Kind = "or"
	KindReference Kind = "license_ref"
)

type Licenses

type Licenses interface {
	License(id string) (canonical string, deprecated, ok bool)
	Exception(id string) (canonical string, deprecated, ok bool)
}

Licenses returns whether an identifier is on the pinned list, its canonical casing, and whether it is deprecated.

func Pinned

func Pinned() Licenses

Pinned returns the embedded license list.

type Node

type Node struct {
	Kind Kind `json:"kind"`
	// ID is the license identifier for KindLicense (canonical list casing),
	// or the full LicenseRef-/DocumentRef- identifier for KindReference.
	ID string `json:"id,omitempty"`
	// OrLater is the "+" suffix on a license identifier.
	OrLater bool `json:"or_later,omitempty"`
	// Exception is the exception identifier for KindWith; Children[0] is the
	// licensed term the exception applies to.
	Exception string  `json:"exception,omitempty"`
	Children  []*Node `json:"children,omitempty"`
	// Known is false for KindLicense/KindWith identifiers that are not on the
	// pinned list. It is always true for KindReference nodes.
	Known bool `json:"known"`
	// Deprecated marks identifiers the list marks deprecated (kept, flagged).
	Deprecated bool `json:"deprecated,omitempty"`
}

Node is one node of the expression tree. Children is ordered as written.

func (*Node) String

func (node *Node) String() string

String renders the canonical expression. Compound children of a different operator are parenthesized so structure is never lost.

func (*Node) Terms

func (node *Node) Terms() []string

Terms returns the distinct leaf identifiers (licenses with their "+" and WITH exception rendered, and references) in sorted order. It is a display aid; policy evaluation must walk the tree.

type Result

type Result struct {
	Raw        string
	Status     Status
	Expression *Node
	// Normalized is the canonical rendering (list casing, single spaces,
	// minimal parentheses preserved as written). Empty unless Status is
	// parsed or unknown_terms.
	Normalized string
	// UnknownTerms lists identifiers not on the pinned list, in order.
	UnknownTerms []string
	// References lists LicenseRef-/DocumentRef- identifiers, in order.
	References []string
	// Deprecated lists deprecated identifiers used, in order.
	Deprecated []string
	// Problem explains StatusInvalid without echoing more than a bounded
	// prefix of the input.
	Problem string
}

Result is the outcome of Parse.

func Parse

func Parse(raw string) Result

Parse classifies and parses a raw license value using the pinned list.

func ParseWith

func ParseWith(raw string, licenses Licenses) Result

ParseWith parses against a caller-supplied list (tests, future versions).

type Status

type Status string

Status classifies a raw license value before or after parsing.

const (
	// StatusParsed: a syntactically valid expression whose every identifier
	// is on the pinned list (LicenseRef/DocumentRef identifiers are allowed
	// and reported separately).
	StatusParsed Status = "parsed"
	// StatusUnknownTerms: valid syntax with at least one identifier that is
	// not on the pinned list and not a LicenseRef.
	StatusUnknownTerms Status = "unknown_terms"
	// StatusNoAssertion: NOASSERTION (or empty): no claim was made.
	StatusNoAssertion Status = "no_assertion"
	// StatusNone: NONE: the producer asserts there is no license information.
	StatusNone Status = "none"
	// StatusUnlicensed: npm's UNLICENSED sentinel: use is not granted.
	StatusUnlicensed Status = "unlicensed"
	// StatusInvalid: the value is not an SPDX expression (free text, "SEE
	// LICENSE IN ...", unbalanced parentheses, bad operators).
	StatusInvalid Status = "invalid"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL