Class VersionParser

java.lang.Object
com.github.zafarkhaja.semver.VersionParser
All Implemented Interfaces:
Parser<Version>

class VersionParser extends Object implements Parser<Version>
A parser for the SemVer Version.
Since:
0.7.0
  • Field Details

  • Constructor Details

    • VersionParser

      VersionParser(String input)
      Constructs a VersionParser instance with the input string to parse.
      Parameters:
      input - the input string to parse
      Throws:
      IllegalArgumentException - if the input string is NULL or empty
  • Method Details

    • parse

      public Version parse(String input)
      Parses the input string.
      Specified by:
      parse in interface Parser<Version>
      Parameters:
      input - the input string to parse
      Returns:
      a valid version object
      Throws:
      ParseException - when there is a grammar error
      UnexpectedCharacterException - when encounters an unexpected character type
    • parseValidSemVer

      static Version parseValidSemVer(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:
      IllegalArgumentException - if the input string is NULL or empty
      ParseException - when there is a grammar error
      UnexpectedCharacterException - when encounters an unexpected character type
    • parseVersionCore

      static NormalVersion parseVersionCore(String versionCore)
      Parses the version core.
      Parameters:
      versionCore - the version core string to parse
      Returns:
      a valid normal version object
      Throws:
      IllegalArgumentException - if the input string is NULL or empty
      ParseException - when there is a grammar error
      UnexpectedCharacterException - when encounters an unexpected character type
    • parsePreRelease

      static MetadataVersion parsePreRelease(String preRelease)
      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 is NULL or empty
      ParseException - when there is a grammar error
      UnexpectedCharacterException - when encounters an unexpected character type
    • parseBuild

      static MetadataVersion parseBuild(String build)
      Parses the build metadata.
      Parameters:
      build - the build metadata string to parse
      Returns:
      a valid build metadata object
      Throws:
      IllegalArgumentException - if the input string is NULL or empty
      ParseException - when there is a grammar error
      UnexpectedCharacterException - 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 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 String buildIdentifier()
      Parses the <build identifier> non-terminal.
       
       <build identifier> ::= <alphanumeric identifier>
                            | <digits>
       
       
      Returns:
      a single build identifier
    • numericIdentifier

      private 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 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 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 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