Documentation
¶
Overview ¶
Package parser provides a grammar-based version string parser using participle. The grammar supports SemVer 2.0.0, Go module versions, Microsoft assembly versions, and various extensions as documented in docs/grammar/version.ebnf.
Index ¶
- Constants
- Variables
- func EBNF() string
- type BuildMetadata
- type Builder
- func (b *Builder) Alpha(n ...int) *Builder
- func (b *Builder) Beta(n ...int) *Builder
- func (b *Builder) Build() (*Version, error)
- func (b *Builder) BuildMetadata(m string) *Builder
- func (b *Builder) ClearBuildMetadata() *Builder
- func (b *Builder) ClearMetadata() *Builder
- func (b *Builder) ClearPreRelease() *Builder
- func (b *Builder) ClearRevision() *Builder
- func (b *Builder) CoreString() string
- func (b *Builder) DecrementMajor() *Builder
- func (b *Builder) DecrementMinor() *Builder
- func (b *Builder) DecrementPatch() *Builder
- func (b *Builder) Dev(n ...int) *Builder
- func (b *Builder) Error() error
- func (b *Builder) GetBuildMetadata() string
- func (b *Builder) GetMajor() int
- func (b *Builder) GetMinor() int
- func (b *Builder) GetPatch() int
- func (b *Builder) GetPreRelease() string
- func (b *Builder) GetPrefix() string
- func (b *Builder) GetRevision() *int
- func (b *Builder) HasError() bool
- func (b *Builder) IncrementMajor() *Builder
- func (b *Builder) IncrementMinor() *Builder
- func (b *Builder) IncrementPatch() *Builder
- func (b *Builder) Major(n int) *Builder
- func (b *Builder) Metadata(m string) *Builder
- func (b *Builder) Minor(n int) *Builder
- func (b *Builder) MustBuild() *Version
- func (b *Builder) NoPrefix() *Builder
- func (b *Builder) Patch(n int) *Builder
- func (b *Builder) PreRelease(pr string) *Builder
- func (b *Builder) Prefix(p string) *Builder
- func (b *Builder) RC(n int) *Builder
- func (b *Builder) Release() *Builder
- func (b *Builder) Revision(n int) *Builder
- func (b *Builder) Snapshot() *Builder
- func (b *Builder) String() string
- func (b *Builder) WithBuildMetadata(m string) *Builder
- func (b *Builder) WithPreRelease(pr string) *Builder
- func (b *Builder) WithPrefix(p string) *Builder
- type Identifier
- type PreRelease
- type Version
- func (v *Version) AssemblyVersion() string
- func (v *Version) BuildMetadataString() string
- func (v *Version) Clone() *Version
- func (v *Version) CoreVersion() string
- func (v *Version) FullString() string
- func (v *Version) HasBuildMetadata() bool
- func (v *Version) HasPrefix() bool
- func (v *Version) IncrementMajor()
- func (v *Version) IncrementMinor()
- func (v *Version) IncrementPatch()
- func (v *Version) IsAssemblyVersion() bool
- func (v *Version) IsPartial() bool
- func (v *Version) IsPreRelease() bool
- func (v *Version) Major() int
- func (v *Version) Minor() int
- func (v *Version) Patch() int
- func (v *Version) PreReleaseString() string
- func (v *Version) Revision() int
- func (v *Version) SetBuildMetadata(metadata string)
- func (v *Version) SetMajor(major int)
- func (v *Version) SetMinor(minor int)
- func (v *Version) SetPatch(patch int)
- func (v *Version) SetPreRelease(preRelease string)
- func (v *Version) SetPrefix(prefix string)
- func (v *Version) String() string
- func (v *Version) ToVersionData() (prefix string, major, minor, patch int, revision *int, ...)
- func (v *Version) Validate() error
- type VersionCore
- type VersionFile
Constants ¶
const ( LogVersionParsed = "version_parsed" LogVersionValidated = "version_validated" LogParseError = "parse_error" LogValidationError = "validation_error" )
Log messages for structured logging.
const ( ErrEmptyVersion = "version string cannot be empty" ErrLeadingZero = "numeric identifier cannot have leading zeros" ErrEmptyIdentifier = "identifier cannot be empty" ErrMajorRequired = "major version is required" ErrNegativeVersion = "version component cannot be negative" ErrInvalidPreRelease = "invalid pre-release identifier" ErrInvalidBuildMetadata = "invalid build metadata" )
Error constants for parser validation.
Variables ¶
var Parser *participle.Parser[VersionFile]
Parser is the version string parser built from the grammar.
var VersionLexer = lexer.MustSimple([]lexer.SimpleRule{
{Name: "Whitespace", Pattern: `[ \t]+`},
{Name: "Dot", Pattern: `\.`},
{Name: "Plus", Pattern: `\+`},
{Name: "Dashes", Pattern: `--+`},
{Name: "Dash", Pattern: `-`},
{Name: "Prefix", Pattern: `[vV]`},
{Name: "Mixed", Pattern: `[0-9]+[a-zA-Z][a-zA-Z0-9-]*`},
{Name: "Ident", Pattern: `[a-zA-Z][a-zA-Z0-9-]*`},
{Name: "Number", Pattern: `[0-9]+`},
})
VersionLexer defines the lexical tokens for version strings. Token order matters - more specific patterns must come before general ones.
The lexer uses several token types:
- Number: pure digits (for version components)
- Prefix: v or V (for version prefix)
- Ident: alphanumeric with optional dashes (for pre-release/metadata)
- Mixed: digit-starting alphanumeric (like "5114f85")
- Dashes: two or more dashes (valid SemVer identifier like "--")
Per SemVer 2.0.0, identifiers can be purely dashes (e.g., "1.0.0-x-y-z.--")
Functions ¶
Types ¶
type BuildMetadata ¶
type BuildMetadata struct {
Identifiers []*Identifier `parser:"Plus @@ ( Dot @@ )*"`
}
BuildMetadata represents the build metadata portion of a version. Grammar: build-metadata = "+", identifier, { ".", identifier } ;
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder provides a fluent API for constructing and mutating versions. It can be seeded from a parsed Version or created from scratch.
Example usage:
// From scratch
v, err := NewBuilder().
Major(1).
Minor(2).
Patch(3).
PreRelease("alpha.1").
Build()
// From existing version
parsed, _ := Parse("v1.2.3-alpha")
v, err := FromVersion(parsed).
IncrementMinor().
PreRelease("beta.1").
Build()
func FromString ¶
FromString parses a version string and creates a builder from it. If parsing fails, Build() will return the parse error.
func FromVersion ¶
FromVersion creates a builder seeded with values from an existing Version. The builder is a copy - modifications don't affect the original.
func NewBuilder ¶
func NewBuilder() *Builder
NewBuilder creates a new version builder starting at 0.0.0.
func (*Builder) Build ¶
Build constructs the version string, parses it back through the grammar, and validates the result. This round-trip ensures the built version is always grammatically correct and parseable.
Returns an error if:
- Any setter recorded an error (e.g., negative version number)
- The constructed string fails to parse (grammar violation)
- The parsed version fails validation (e.g., leading zeros)
func (*Builder) BuildMetadata ¶
BuildMetadata sets the build metadata (e.g., "build.123", "sha.abc123"). Pass empty string to clear the metadata.
func (*Builder) ClearBuildMetadata ¶
ClearBuildMetadata is an alias for ClearMetadata.
func (*Builder) ClearMetadata ¶
ClearMetadata removes the build metadata.
func (*Builder) ClearPreRelease ¶
ClearPreRelease removes the pre-release identifier.
func (*Builder) ClearRevision ¶
ClearRevision removes the revision component, making this a 3-component version.
func (*Builder) CoreString ¶
CoreString returns just Major.Minor.Patch without prefix, pre-release, or metadata.
func (*Builder) DecrementMajor ¶
DecrementMajor decrements major, resets minor, patch, and revision. Sets error if major would go below 0.
func (*Builder) DecrementMinor ¶
DecrementMinor decrements minor, resets patch and revision. Sets error if minor would go below 0.
func (*Builder) DecrementPatch ¶
DecrementPatch decrements patch, resets revision. Sets error if patch would go below 0.
func (*Builder) GetBuildMetadata ¶
GetBuildMetadata returns the current build metadata.
func (*Builder) GetPreRelease ¶
GetPreRelease returns the current pre-release string.
func (*Builder) GetRevision ¶
GetRevision returns the current revision (nil if not set).
func (*Builder) IncrementMajor ¶
IncrementMajor increments major, resets minor, patch, and revision to 0, clears pre-release.
func (*Builder) IncrementMinor ¶
IncrementMinor increments minor, resets patch and revision, clears pre-release.
func (*Builder) IncrementPatch ¶
IncrementPatch increments patch, resets revision, clears pre-release.
func (*Builder) MustBuild ¶
MustBuild calls Build and panics on error. Use only when you're certain the version is valid.
func (*Builder) PreRelease ¶
PreRelease sets the pre-release identifier (e.g., "alpha.1", "beta", "rc.2"). Pass empty string to clear the pre-release.
func (*Builder) Release ¶
Release clears pre-release and metadata, producing a clean release version.
func (*Builder) Revision ¶
Revision sets the revision number (4th component for assembly versions). Use nil or call ClearRevision() to remove the revision component.
func (*Builder) String ¶
String returns the version string without building/validating. Useful for debugging or when you just need the string representation.
func (*Builder) WithBuildMetadata ¶
WithBuildMetadata is an alias for BuildMetadata.
func (*Builder) WithPreRelease ¶
WithPreRelease is an alias for PreRelease.
func (*Builder) WithPrefix ¶
WithPrefix is an alias for Prefix.
type Identifier ¶
type Identifier struct {
Number *string `parser:" @Number"`
Ident *string `parser:"| @Ident"`
Mixed *string `parser:"| @Mixed"`
Dashes *string `parser:"| @Dashes"`
}
Identifier represents a single pre-release or build metadata identifier. Can be numeric, alphanumeric, mixed (like "5114f85"), or dashes only (like "--").
func (*Identifier) IsNumeric ¶
func (id *Identifier) IsNumeric() bool
IsNumeric returns true if the identifier is purely numeric.
func (*Identifier) String ¶
func (id *Identifier) String() string
String returns the identifier value as a string.
type PreRelease ¶
type PreRelease struct {
Identifiers []*Identifier `parser:"Dash @@ ( Dot @@ )*"`
}
PreRelease represents the pre-release portion of a version. Grammar: pre-release = "-", identifier, { ".", identifier } ; Identifiers can be pure numbers, pure letters, or mixed alphanumeric.
type Version ¶
type Version struct {
Prefix string `parser:"@Prefix?"`
Core *VersionCore `parser:"@@"`
PreRelease *PreRelease `parser:"@@?"`
BuildMetadata *BuildMetadata `parser:"@@?"`
Raw string // Original input string (set after parsing)
}
Version represents a parsed version string with all components. Grammar: version = [ prefix ], version-core ;
func FromVersionData ¶
func FromVersionData(prefix string, major, minor, patch int, revision *int, preRelease, buildMetadata string) *Version
FromVersionData creates a parser.Version from version data components. This is used when constructing versions programmatically.
func Parse ¶
Parse parses a version string and returns the parsed Version. Returns an error if the string is not a valid version. Only v/V prefixes are supported per SemVer convention.
func ParseLenient ¶
ParseLenient parses a version string, returning a best-effort Version. Unlike Parse, it does not return an error for minor validation issues. Use this for reading existing VERSION files that may have legacy formats.
func (*Version) AssemblyVersion ¶
AssemblyVersion returns a 4-component assembly version. Format: Major.Minor.Patch.Revision (Revision defaults to 0)
func (*Version) BuildMetadataString ¶
BuildMetadataString returns the build metadata as a string.
func (*Version) CoreVersion ¶
CoreVersion returns just Major.Minor.Patch[.Revision] without pre-release or metadata.
func (*Version) FullString ¶
FullString returns the version with prefix. Format: [Prefix]Major.Minor.Patch[-PreRelease][+BuildMetadata]
func (*Version) HasBuildMetadata ¶
HasBuildMetadata returns true if the version has build metadata.
func (*Version) IncrementMajor ¶
func (v *Version) IncrementMajor()
IncrementMajor increments the major version, resets minor, patch, and revision.
func (*Version) IncrementMinor ¶
func (v *Version) IncrementMinor()
IncrementMinor increments the minor version, resets patch and revision.
func (*Version) IncrementPatch ¶
func (v *Version) IncrementPatch()
IncrementPatch increments the patch version, resets revision.
func (*Version) IsAssemblyVersion ¶
IsAssemblyVersion returns true if this is a 4-component assembly version.
func (*Version) IsPartial ¶
IsPartial returns true if the version is missing minor or patch components.
func (*Version) IsPreRelease ¶
IsPreRelease returns true if the version has a pre-release tag.
func (*Version) PreReleaseString ¶
PreReleaseString returns the pre-release portion as a string.
func (*Version) Revision ¶
Revision returns the revision number for assembly versions (0 if not specified).
func (*Version) SetBuildMetadata ¶
SetBuildMetadata updates the build metadata.
func (*Version) SetMajor ¶
SetMajor updates the major version and resets minor, patch, and pre-release.
func (*Version) SetPreRelease ¶
SetPreRelease updates the pre-release identifier.
func (*Version) String ¶
String returns the version string without prefix. Format: Major.Minor.Patch[.Revision][-PreRelease][+BuildMetadata]
func (*Version) ToVersionData ¶
func (v *Version) ToVersionData() (prefix string, major, minor, patch int, revision *int, preRelease, buildMetadata, raw string)
ToVersionData converts a parser.Version to the data needed for the version package. Returns: prefix, major, minor, patch, revision, preRelease, buildMetadata, raw
type VersionCore ¶
type VersionCore struct {
Major int `parser:"@Number"`
Minor *int `parser:"( Dot @Number"`
Patch *int `parser:" ( Dot @Number"`
Revision *int `parser:" ( Dot @Number )? )? )?"`
}
VersionCore represents the numeric parts of a version. Grammar: version-core = major [ "." minor [ "." patch [ "." revision ] ] ] ;
type VersionFile ¶
type VersionFile struct {
Version *Version `parser:"@@"`
}
VersionFile represents the content of a VERSION file. Grammar: version-file = version, [ newline ] ;