Support syntax-only SPDX expression parsing - #25
Conversation
|
@andrew I've prepared the follow-up fix for git-pkgs/licenses#2 and validated it locally against this branch. Since it depends on the new ParseSyntax API, I'll open the PR once you have time to review and merge this one. |
andrew
left a comment
There was a problem hiding this comment.
Please address these before merging:
- Validate
LicenseRefandDocumentRefcomponents withvalidIdentifier.ParseSyntaxcurrently accepts malformed values such asLicenseRef-,LicenseRef-invalid/value, andDocumentRef-vendor; some are silently rewritten into different references. - Extract identifier resolution from
parseAtom. The added nesting raises its cognitive complexity to 34, above the review threshold of 30.
|
Thanks for the review, I’ve addressed both points:
|
There was a problem hiding this comment.
Pull request overview
This PR introduces a new public API, ParseSyntax, to parse SPDX license expressions in a syntax-only mode that validates grammar and identifier shape without requiring license/exception identifiers to exist in the SPDX identifier list bundled with this module. This supports callers that validate identifiers against a separately pinned corpus while still enforcing expression structure and existing size/depth limits.
Changes:
- Add
ParseSyntaxand plumb anallowUnknownIdentifiersmode through the parser so unknown-but-well-formed identifiers can be preserved. - Refactor parsing entrypoints to share a
(*parser).parse()helper and add identifier/reference validation helpers. - Add a new test suite covering syntax-only parsing behavior, strict-mode rejection, malformed expressions, and limits.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| README.md | Documents the new ParseSyntax API and when to use it vs ParseStrict. |
| parse.go | Adds syntax-only parsing mode and identifier/reference validation helpers. |
| parse_syntax_test.go | Adds regression tests for unknown identifiers, AST equivalence, malformed expressions, and limits. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Closes #24
Summary
Add
ParseSyntax, a syntax-only SPDX expression parser that does not require bare license and exception identifiers to exist in the SPDX identifier list bundled bygithub/go-spdx.This allows callers with a separately pinned identifier source—such as
git-pkgs/licensesand its ScanCode corpus—to parse an expression before resolving its identifiers independently.API
Unlike
ParseStrict, this succeeds when an unknown identifier is syntactically valid:Behavior
ParseSyntax:AND,OR,WITH, parentheses, and+LicenseRef-*andDocumentRef-*:LicenseRef-*ParseStrictretains its existing behavior and continues to require identifiers from the bundled SPDX list.Testing
Added regression coverage for:
ParseSyntaxandParseStrictfor known expressionsWITH,+, and license referencesValidation
gofmtgit diff --checkgo build ./...go vet ./...go test -count=1 ./...go test -race -count=1 ./...golangci-lintv2.12.2 — 0 issuesFollow-up
This unblocks
git-pkgs/licenses#2, which can useParseSyntaxand resolve identifiers against its pinned ScanCode mapping.