Class ExpressionParser

java.lang.Object
com.github.zafarkhaja.semver.expr.ExpressionParser
All Implemented Interfaces:
Parser<Expression>

public class ExpressionParser extends Object implements Parser<Expression>
A parser for the SemVer Expressions.
Since:
0.7.0
  • Field Details

    • lexer

      private final Lexer lexer
      The lexer instance used for tokenization of the input string.
    • tokens

      private Stream<Lexer.Token> tokens
      The stream of tokens produced by the lexer.
  • Constructor Details

    • ExpressionParser

      ExpressionParser(Lexer lexer)
      Constructs a ExpressionParser instance with the corresponding lexer.
      Parameters:
      lexer - the lexer to use for tokenization of the input string
  • Method Details

    • newInstance

      public static Parser<Expression> newInstance()
      Creates and returns new instance of the ExpressionParser class. This method implements the Static Factory Method pattern.
      Returns:
      a new instance of the ExpressionParser class
    • parse

      public Expression parse(String input)
      Parses the SemVer Expressions.
      Specified by:
      parse in interface Parser<Expression>
      Parameters:
      input - a string representing the SemVer Expression
      Returns:
      the AST for the SemVer Expressions
      Throws:
      LexerException - when encounters an illegal character
      UnexpectedTokenException - when consumes a token of an unexpected type
    • parseSemVerExpression

      private CompositeExpression parseSemVerExpression()
      Parses the <semver-expr> non-terminal.
       
       <semver-expr> ::= "(" <semver-expr> ")"
                       | "!" "(" <semver-expr> ")"
                       | <semver-expr> <more-expr>
                       | <range>
       
       
      Returns:
      the expression AST
    • parseMoreExpressions

      private CompositeExpression parseMoreExpressions(CompositeExpression expr)
      Parses the <more-expr> non-terminal.
       
       <more-expr> ::= <boolean-op> <semver-expr> | epsilon
       
       
      Parameters:
      expr - the left-hand expression of the logical operators
      Returns:
      the expression AST
    • parseRange

      private CompositeExpression parseRange()
      Parses the <range> non-terminal.
       
       <expr> ::= <comparison-range>
                | <wildcard-expr>
                | <tilde-range>
                | <caret-range>
                | <hyphen-range>
                | <partial-version-range>
       
       
      Returns:
      the expression AST
    • parseComparisonRange

      private CompositeExpression parseComparisonRange()
      Parses the <comparison-range> non-terminal.
       
       <comparison-range> ::= <comparison-op> <version> | <version>
       
       
      Returns:
      the expression AST
    • parseTildeRange

      private CompositeExpression parseTildeRange()
      Parses the <tilde-range> non-terminal.
       
       <tilde-range> ::= "~" <version>
       
       
      Returns:
      the expression AST
    • parseCaretRange

      private CompositeExpression parseCaretRange()
      Parses the <caret-range> non-terminal.
       
       <caret-range> ::= "^" <version>
       
       
      Returns:
      the expression AST
    • isWildcardRange

      private boolean isWildcardRange()
      Determines if the following version terminals are part of the <wildcard-range> non-terminal.
      Returns:
      true if the following version terminals are part of the <wildcard-range> non-terminal or false otherwise
    • parseWildcardRange

      private CompositeExpression parseWildcardRange()
      Parses the <wildcard-range> non-terminal.
       
       <wildcard-range> ::= <wildcard>
                          | <major> "." <wildcard>
                          | <major> "." <minor> "." <wildcard>
      
       <wildcard> ::= "*" | "x" | "X"
       
       
      Returns:
      the expression AST
    • isHyphenRange

      private boolean isHyphenRange()
      Determines if the following version terminals are part of the <hyphen-range> non-terminal.
      Returns:
      true if the following version terminals are part of the <hyphen-range> non-terminal or false otherwise
    • parseHyphenRange

      private CompositeExpression parseHyphenRange()
      Parses the <hyphen-range> non-terminal.
       
       <hyphen-range> ::= <version> "-" <version>
       
       
      Returns:
      the expression AST
    • isPartialVersionRange

      private boolean isPartialVersionRange()
      Determines if the following version terminals are part of the <partial-version-range> non-terminal.
      Returns:
      true if the following version terminals are part of the <partial-version-range> non-terminal or false otherwise
    • parsePartialVersionRange

      private CompositeExpression parsePartialVersionRange()
      Parses the <partial-version-range> non-terminal.
       
       <partial-version-range> ::= <major> | <major> "." <minor>
       
       
      Returns:
      the expression AST
    • parseVersion

      private Version parseVersion()
      Parses the <version> non-terminal.
       
       <version> ::= <major>
                   | <major> "." <minor>
                   | <major> "." <minor> "." <patch>
       
       
      Returns:
      the parsed version
    • isVersionFollowedBy

      private boolean isVersionFollowedBy(Stream.ElementType<Lexer.Token> type)
      Determines if the version terminals are followed by the specified token type. This method is essentially a lookahead(k) method which allows to solve the grammar's ambiguities.
      Parameters:
      type - the token type to check
      Returns:
      true if the version terminals are followed by the specified token type or false otherwise
    • versionFor

      private Version versionFor(int major)
      Creates a Version instance for the specified major version.
      Parameters:
      major - the major version number
      Returns:
      the version for the specified major version
    • versionFor

      private Version versionFor(int major, int minor)
      Creates a Version instance for the specified major and minor versions.
      Parameters:
      major - the major version number
      minor - the minor version number
      Returns:
      the version for the specified major and minor versions
    • versionFor

      private Version versionFor(int major, int minor, int patch)
      Creates a Version instance for the specified major, minor and patch versions.
      Parameters:
      major - the major version number
      minor - the minor version number
      patch - the patch version number
      Returns:
      the version for the specified major, minor and patch versions
    • intOf

      private int intOf(String value)
      Returns a int representation of the specified string.
      Parameters:
      value - the string to convert into an integer
      Returns:
      the integer value of the specified string
    • consumeNextToken

      private Lexer.Token consumeNextToken(Lexer.Token.Type... expected)
      Tries to consume the next token in the stream.
      Parameters:
      expected - the expected types of the next token
      Returns:
      the next token in the stream
      Throws:
      UnexpectedTokenException - when encounters an unexpected token type