Skip to content
Merged
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
7 changes: 7 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ recursive-descent grammar of pypa/packaging
marker productions (`_parse_marker` and friends) and `process_env_var`,
licensed under the Apache License, Version 2.0.

The requirement/ package adapts the dependency-specifier grammar of
pypa/packaging (https://github.com/pypa/packaging), specifically
packaging/_parser.py's `_parse_requirement` and packaging/requirements.py's
`Requirement`, licensed under the Apache License, Version 2.0; and Astral
uv's (https://github.com/astral-sh/uv) `uv-pep508` crate, dual-licensed
under the Apache License, Version 2.0, or the MIT license.

The marker/ package's Environment platform mapping (os_name, sys_platform,
platform_system, platform_release, platform_version) adapts the
linux/macos/windows cases of Astral uv's platform-tag logic
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Part of [RFD 0001 — Native Go PyPI Dependency Resolution](https://github.com/r
| Package | Scope |
|---|---|
| [`version/`](https://github.com/rstudio/go-pep440-version) | PEP 440 versions and specifiers |
| `requirement/` | PEP 508 dependency specifiers |
| `requirement/` | PEP 508 dependency specifiers (implemented) |
| `marker/` | PEP 508 environment markers (implemented) |
| `extras/` | PEP 685 extra-name normalization (implemented) |
| `reqtxt/` | pip `requirements.txt` format |
Expand Down
4 changes: 2 additions & 2 deletions internal/pep508/doc.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

// Package pep508 implements the shared, position-tracking tokenizer used to
// parse PEP 508 environment markers (package marker/) and, eventually, PEP
// 508 dependency-specifier requirement strings (package requirement/).
// parse PEP 508 environment markers (package marker/) and PEP 508
// dependency-specifier requirement strings (package requirement/).
//
// It is a Go port of pypa/packaging's _tokenizer.py: a Tokenizer holds the
// source string and a byte offset, and exposes a parser-driven API (check,
Expand Down
236 changes: 236 additions & 0 deletions internal/pep508/requirement.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

package pep508

import "strings"

// RawRequirement is the raw, structurally-parsed result of a PEP 508
// requirement string, before the public requirement/ package applies name
// canonicalization (none - the caller's concern), extras normalization
// (extras.Normalize), version-specifier delegation (version.NewSpecifiers),
// and marker wrapping (marker.FromExpr).
//
// Fields are populated exactly as parsed: Name is not canonicalized, Extras
// are not normalized, Specifier is the raw (un-parenthesized) specifier
// text handed verbatim to version.NewSpecifiers, and Marker is the
// pep508.Expr returned by ParseMarker (nil if there was no "; marker"
// clause at all - as opposed to a Marker.ast of nil, which package marker's
// zero value also represents "no clause").
type RawRequirement struct {
Name string
Extras []string
Specifier string
URL string
Marker Expr
}

// MarkerClauseError wraps an error that occurred while parsing a
// requirement's "; marker" clause (via ParseMarker), distinguishing it from
// a *SyntaxError raised anywhere else in the requirement grammar (name,
// extras, versionspec, url). Both are *SyntaxError under the hood - this
// wrapper is what lets a caller (package requirement's Parse) tell "bad
// marker" apart from "malformed requirement syntax" via errors.As, without
// requiring a second, unrelated error type from the marker grammar itself.
type MarkerClauseError struct {
Err error
}

func (e *MarkerClauseError) Error() string { return e.Err.Error() }
func (e *MarkerClauseError) Unwrap() error { return e.Err }

// ParseRequirement parses s (via t) as a complete PEP 508 dependency
// specifier and verifies nothing but end-of-input follows, mirroring
// packaging's _parse_requirement (_parser.py) and its wrapper parse_requirement.
//
// Grammar (PEP 508's formal grammar, as packaging implements it):
//
// specification = wsp* ( url_req | name_req ) wsp*
// name_req = name wsp* extras? wsp* versionspec? wsp* quoted_marker?
// url_req = name wsp* extras? wsp* urlspec wsp+ quoted_marker?
// name = identifier
// extras = '[' wsp* extras_list? wsp* ']'
// extras_list = identifier (wsp* ',' wsp* identifier)*
// versionspec = ( '(' version_many ')' ) | version_many
// version_many = version_one (wsp* ',' version_one)*
// urlspec = '@' wsp* <URI_reference>
// quoted_marker = ';' wsp* marker
//
// This drives the SHARED tokenizer end-to-end in one pass - name, then
// optional extras, then optional (versionspec | "@" url), then, if a ";"
// follows, the marker clause via ParseMarker from the tokenizer's current
// position - rather than pre-splitting the string on ";" (which would
// mishandle a URL requirement whose URL itself contains ";") or re-lexing a
// marker substring with a second tokenizer.
func ParseRequirement(t *Tokenizer) (RawRequirement, error) {
t.consume(WS)
nameTok, err := t.expect(Identifier, "package name")
if err != nil {
return RawRequirement{}, err
}
req := RawRequirement{Name: nameTok.Text}
t.consume(WS)

extras, err := parseExtras(t)
if err != nil {
return RawRequirement{}, err
}
req.Extras = extras
t.consume(WS)

if err := parseRequirementDetails(t, &req); err != nil {
return RawRequirement{}, err
}
t.consume(WS)

if _, err := t.expect(End, "end of requirement string"); err != nil {
return RawRequirement{}, err
}
return req, nil
}

// parseExtras parses packaging's "extras" production. A missing "[" is not
// an error - extras are entirely optional - and an empty "[]" is valid
// (returning a nil/empty slice, not an error).
func parseExtras(t *Tokenizer) ([]string, error) {
if !t.check(LBracket) {
return nil, nil
}
t.read()
t.consume(WS)

var names []string
if t.check(Identifier) {
names = append(names, t.read().Text)
t.consume(WS)
for t.check(Comma) {
t.read()
t.consume(WS)
idTok, err := t.expect(Identifier, "extra name after ','")
if err != nil {
return nil, err
}
names = append(names, idTok.Text)
t.consume(WS)
}
}

if _, err := t.expect(RBracket, "']'"); err != nil {
return nil, err
}
return names, nil
}

// parseRequirementDetails parses packaging's "requirement_details":
// optionally either a version specifier or an "@ url" clause, followed by
// an optional "; marker" clause, and stores the results into req.
//
// The "@ url" branch enforces PEP 508's url_req asymmetry: mandatory
// whitespace (or end-of-input) must follow the URL before anything else can
// be parsed - including a following marker clause. Without that whitespace,
// there is no way to tell "the URL ends here" from "the URL continues", so
// (correctly, per PEP 508) everything up to the next whitespace or
// end-of-input is simply part of the URL, semicolons included.
func parseRequirementDetails(t *Tokenizer, req *RawRequirement) error {
if t.check(AT) {
t.read()
t.consume(WS)
urlTok, err := t.expect(URL, "URL after '@'")
if err != nil {
return err
}
req.URL = urlTok.Text

if t.peek(End) {
return nil
}
if _, err := t.expect(WS, "whitespace after URL"); err != nil {
return err
}
if t.peek(End) {
return nil
}
return parseMarkerClause(t, req)
}

spec, err := parseVersionSpec(t)
if err != nil {
return err
}
req.Specifier = spec
t.consume(WS)
return parseMarkerClause(t, req)
}

// parseVersionSpec parses packaging's "versionspec": an optionally
// parenthesized, comma-separated run of version-specifier clauses. It
// returns the raw specifier text (parens stripped, clauses rejoined with
// ","), or "" if no version specifier is present at all - which is legal,
// since versionspec is entirely optional in name_req.
func parseVersionSpec(t *Tokenizer) (string, error) {
t.consume(WS)
parenthesized := t.check(LParen)
if parenthesized {
t.read()
t.consume(WS)
}

var clauses []string
if t.check(Specifier) {
clauses = append(clauses, normalizeSpecifierClause(t.read().Text))
t.consume(WS)
for t.check(Comma) {
t.read()
t.consume(WS)
specTok, err := t.expect(Specifier, "version specifier after ','")
if err != nil {
return "", err
}
clauses = append(clauses, normalizeSpecifierClause(specTok.Text))
t.consume(WS)
}
} else if parenthesized {
return "", t.NewSyntaxError("Expected version specifier inside parentheses")
}

if parenthesized {
if _, err := t.expect(RParen, "')'"); err != nil {
return "", err
}
}

return strings.Join(clauses, ","), nil
}

// normalizeSpecifierClause strips whitespace from a single tokenized
// Specifier clause's text (the Specifier rule permissively allows "\s*"
// between the operator and the version, e.g. ">= 1.0"), producing a
// canonical, whitespace-free clause for joining into the raw specifier
// string handed to version.NewSpecifiers - which is agnostic to whether
// the input had that whitespace, but round-tripping a canonical form is
// cleaner than preserving arbitrary user spacing verbatim.
func normalizeSpecifierClause(s string) string {
return strings.Join(strings.Fields(s), "")
}

// parseMarkerClause parses packaging's "quoted_marker" (';' wsp* marker) if
// a ';' is present at the current position; it is a no-op otherwise, since
// the marker clause is always optional. The marker itself is parsed by
// ParseMarker directly from t's current (mid-stream) position - the same
// shared tokenizer, no substring, no second lexer - and any error it
// returns is wrapped in *MarkerClauseError so the caller can distinguish a
// bad marker from a bad requirement syntax error elsewhere.
func parseMarkerClause(t *Tokenizer, req *RawRequirement) error {
if !t.check(Semicolon) {
return nil
}
t.read()
t.consume(WS)

expr, err := ParseMarker(t)
if err != nil {
return &MarkerClauseError{Err: err}
}
req.Marker = expr
t.consume(WS)
return nil
}
Loading