Package com.github.zafarkhaja.semver
Class VersionParser
java.lang.Object
com.github.zafarkhaja.semver.VersionParser
A parser for the SemVer Version.
- Since:
- 0.7.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static enum
Valid character types. -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionVersionParser
(String input) Constructs aVersionParser
instance with the input string to parse. -
Method Summary
Modifier and TypeMethodDescriptionprivate String
Parses the <alphanumeric identifier> non-terminal.private String
Parses the <build identifier> non-terminal.private void
Checks for empty identifiers in the pre-release version or build metadata.private void
Checks for leading zeroes in the numeric identifiers.private Character
consumeNextCharacter
(VersionParser.CharType... expected) Tries to consume the next character in the stream.private String
digits()
Parses the <digits> non-terminal.private void
ensureValidLookahead
(VersionParser.CharType... expected) Checks if the next character in the stream is valid.private VersionParser.CharType
nearestCharType
(VersionParser.CharType... types) Finds the nearest character type.private String
Parses the <numeric identifier> non-terminal.Parses the input string.private MetadataVersion
Parses the <build> non-terminal.(package private) static MetadataVersion
parseBuild
(String build) Parses the build metadata.private MetadataVersion
Parses the <pre-release> non-terminal.(package private) static MetadataVersion
parsePreRelease
(String preRelease) Parses the pre-release version.private Version
Parses the <valid semver> non-terminal.(package private) static Version
parseValidSemVer
(String version) Parses the whole version including pre-release version and build metadata.private NormalVersion
Parses the <version core> non-terminal.(package private) static NormalVersion
parseVersionCore
(String versionCore) Parses the version core.private String
Parses the <pre-release identifier> non-terminal.
-
Field Details
-
chars
The stream of characters.
-
-
Constructor Details
-
VersionParser
VersionParser(String input) Constructs aVersionParser
instance with the input string to parse.- Parameters:
input
- the input string to parse- Throws:
IllegalArgumentException
- if the input string isNULL
or empty
-
-
Method Details
-
parse
Parses the input string.- Specified by:
parse
in interfaceParser<Version>
- Parameters:
input
- the input string to parse- Returns:
- a valid version object
- Throws:
ParseException
- when there is a grammar errorUnexpectedCharacterException
- when encounters an unexpected character type
-
parseValidSemVer
Parses the whole version including pre-release version and build metadata.- Parameters:
version
- the version string to parse- Returns:
- a valid version object
- Throws:
IllegalArgumentException
- if the input string isNULL
or emptyParseException
- when there is a grammar errorUnexpectedCharacterException
- when encounters an unexpected character type
-
parseVersionCore
Parses the version core.- Parameters:
versionCore
- the version core string to parse- Returns:
- a valid normal version object
- Throws:
IllegalArgumentException
- if the input string isNULL
or emptyParseException
- when there is a grammar errorUnexpectedCharacterException
- when encounters an unexpected character type
-
parsePreRelease
Parses the pre-release version.- Parameters:
preRelease
- the pre-release version string to parse- Returns:
- a valid pre-release version object
- Throws:
IllegalArgumentException
- if the input string isNULL
or emptyParseException
- when there is a grammar errorUnexpectedCharacterException
- when encounters an unexpected character type
-
parseBuild
Parses the build metadata.- Parameters:
build
- the build metadata string to parse- Returns:
- a valid build metadata object
- Throws:
IllegalArgumentException
- if the input string isNULL
or emptyParseException
- when there is a grammar errorUnexpectedCharacterException
- when encounters an unexpected character type
-
parseValidSemVer
Parses the <valid semver> non-terminal.<valid semver> ::= <version core> | <version core> "-" <pre-release> | <version core> "+" <build> | <version core> "-" <pre-release> "+" <build>
- Returns:
- a valid version object
-
parseVersionCore
Parses the <version core> non-terminal.<version core> ::= <major> "." <minor> "." <patch>
- Returns:
- a valid normal version object
-
parsePreRelease
Parses the <pre-release> non-terminal.<pre-release> ::= <dot-separated pre-release identifiers> <dot-separated pre-release identifiers> ::= <pre-release identifier> | <pre-release identifier> "." <dot-separated pre-release identifiers>
- Returns:
- a valid pre-release version object
-
preReleaseIdentifier
Parses the <pre-release identifier> non-terminal.<pre-release identifier> ::= <alphanumeric identifier> | <numeric identifier>
- Returns:
- a single pre-release identifier
-
parseBuild
Parses the <build> non-terminal.<build> ::= <dot-separated build identifiers> <dot-separated build identifiers> ::= <build identifier> | <build identifier> "." <dot-separated build identifiers>
- Returns:
- a valid build metadata object
-
buildIdentifier
Parses the <build identifier> non-terminal.<build identifier> ::= <alphanumeric identifier> | <digits>
- Returns:
- a single build identifier
-
numericIdentifier
Parses the <numeric identifier> non-terminal.<numeric identifier> ::= "0" | <positive digit> | <positive digit> <digits>
- Returns:
- a string representing the numeric identifier
-
alphanumericIdentifier
Parses the <alphanumeric identifier> non-terminal.<alphanumeric identifier> ::= <non-digit> | <non-digit> <identifier characters> | <identifier characters> <non-digit> | <identifier characters> <non-digit> <identifier characters>
- Returns:
- a string representing the alphanumeric identifier
-
digits
Parses the <digits> non-terminal.<digits> ::= <digit> | <digit> <digits>
- Returns:
- a string representing the digits
-
nearestCharType
Finds the nearest character type.- Parameters:
types
- the character types to choose from- Returns:
- the nearest character type or
EOI
-
checkForLeadingZeroes
private void checkForLeadingZeroes()Checks for leading zeroes in the numeric identifiers.- Throws:
ParseException
- if a numeric identifier has leading zero(es)
-
checkForEmptyIdentifier
private void checkForEmptyIdentifier()Checks for empty identifiers in the pre-release version or build metadata.- Throws:
ParseException
- if the pre-release version or build metadata have empty identifier(s)
-
consumeNextCharacter
Tries to consume the next character in the stream.- Parameters:
expected
- the expected types of the next character- Returns:
- the next character in the stream
- Throws:
UnexpectedCharacterException
- when encounters an unexpected character type
-
ensureValidLookahead
Checks if the next character in the stream is valid.- Parameters:
expected
- the expected types of the next character- Throws:
UnexpectedCharacterException
- if the next character is not valid
-