Package com.github.zafarkhaja.semver
Class VersionParser
- java.lang.Object
-
- com.github.zafarkhaja.semver.VersionParser
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classVersionParser.CharTypeValid character types.
-
Constructor Summary
Constructors Constructor Description VersionParser(java.lang.String input)Constructs aVersionParserinstance with the input string to parse.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.lang.StringalphanumericIdentifier()Parses the <alphanumeric identifier> non-terminal.private java.lang.StringbuildIdentifier()Parses the <build identifier> non-terminal.private voidcheckForEmptyIdentifier()Checks for empty identifiers in the pre-release version or build metadata.private voidcheckForLeadingZeroes()Checks for leading zeroes in the numeric identifiers.private java.lang.CharacterconsumeNextCharacter(VersionParser.CharType... expected)Tries to consume the next character in the stream.private java.lang.Stringdigits()Parses the <digits> non-terminal.private voidensureValidLookahead(VersionParser.CharType... expected)Checks if the next character in the stream is valid.private VersionParser.CharTypenearestCharType(VersionParser.CharType... types)Finds the nearest character type.private java.lang.StringnumericIdentifier()Parses the <numeric identifier> non-terminal.Versionparse(java.lang.String input)Parses the input string.private MetadataVersionparseBuild()Parses the <build> non-terminal.(package private) static MetadataVersionparseBuild(java.lang.String build)Parses the build metadata.private MetadataVersionparsePreRelease()Parses the <pre-release> non-terminal.(package private) static MetadataVersionparsePreRelease(java.lang.String preRelease)Parses the pre-release version.private VersionparseValidSemVer()Parses the <valid semver> non-terminal.(package private) static VersionparseValidSemVer(java.lang.String version)Parses the whole version including pre-release version and build metadata.private NormalVersionparseVersionCore()Parses the <version core> non-terminal.(package private) static NormalVersionparseVersionCore(java.lang.String versionCore)Parses the version core.private java.lang.StringpreReleaseIdentifier()Parses the <pre-release identifier> non-terminal.
-
-
-
Field Detail
-
chars
private final Stream<java.lang.Character> chars
The stream of characters.
-
-
Method Detail
-
parse
public Version parse(java.lang.String input)
Parses the input string.- Specified by:
parsein 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
static Version parseValidSemVer(java.lang.String version)
Parses the whole version including pre-release version and build metadata.- Parameters:
version- the version string to parse- Returns:
- a valid version object
- Throws:
java.lang.IllegalArgumentException- if the input string isNULLor emptyParseException- when there is a grammar errorUnexpectedCharacterException- when encounters an unexpected character type
-
parseVersionCore
static NormalVersion parseVersionCore(java.lang.String versionCore)
Parses the version core.- Parameters:
versionCore- the version core string to parse- Returns:
- a valid normal version object
- Throws:
java.lang.IllegalArgumentException- if the input string isNULLor emptyParseException- when there is a grammar errorUnexpectedCharacterException- when encounters an unexpected character type
-
parsePreRelease
static MetadataVersion parsePreRelease(java.lang.String preRelease)
Parses the pre-release version.- Parameters:
preRelease- the pre-release version string to parse- Returns:
- a valid pre-release version object
- Throws:
java.lang.IllegalArgumentException- if the input string isNULLor emptyParseException- when there is a grammar errorUnexpectedCharacterException- when encounters an unexpected character type
-
parseBuild
static MetadataVersion parseBuild(java.lang.String build)
Parses the build metadata.- Parameters:
build- the build metadata string to parse- Returns:
- a valid build metadata object
- Throws:
java.lang.IllegalArgumentException- if the input string isNULLor emptyParseException- when there is a grammar errorUnexpectedCharacterException- when encounters an unexpected character type
-
parseValidSemVer
private Version 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
private NormalVersion parseVersionCore()
Parses the <version core> non-terminal.<version core> ::= <major> "." <minor> "." <patch>
- Returns:
- a valid normal version object
-
parsePreRelease
private MetadataVersion 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
private java.lang.String preReleaseIdentifier()
Parses the <pre-release identifier> non-terminal.<pre-release identifier> ::= <alphanumeric identifier> | <numeric identifier>- Returns:
- a single pre-release identifier
-
parseBuild
private MetadataVersion 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
private java.lang.String buildIdentifier()
Parses the <build identifier> non-terminal.<build identifier> ::= <alphanumeric identifier> | <digits>- Returns:
- a single build identifier
-
numericIdentifier
private java.lang.String numericIdentifier()
Parses the <numeric identifier> non-terminal.<numeric identifier> ::= "0" | <positive digit> | <positive digit> <digits>- Returns:
- a string representing the numeric identifier
-
alphanumericIdentifier
private java.lang.String 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
private java.lang.String digits()
Parses the <digits> non-terminal.<digits> ::= <digit> | <digit> <digits>- Returns:
- a string representing the digits
-
nearestCharType
private VersionParser.CharType nearestCharType(VersionParser.CharType... types)
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
private java.lang.Character consumeNextCharacter(VersionParser.CharType... expected)
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
private void ensureValidLookahead(VersionParser.CharType... expected)
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
-
-