Class ExpressionParser
- java.lang.Object
-
- com.github.zafarkhaja.semver.expr.ExpressionParser
-
- All Implemented Interfaces:
Parser<Expression>
public class ExpressionParser extends java.lang.Object implements Parser<Expression>
A parser for the SemVer Expressions.- Since:
- 0.7.0
-
-
Field Summary
Fields Modifier and Type Field Description private Lexer
lexer
The lexer instance used for tokenization of the input string.private Stream<Lexer.Token>
tokens
The stream of tokens produced by the lexer.
-
Constructor Summary
Constructors Constructor Description ExpressionParser(Lexer lexer)
Constructs aExpressionParser
instance with the corresponding lexer.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private Lexer.Token
consumeNextToken(Lexer.Token.Type... expected)
Tries to consume the next token in the stream.private int
intOf(java.lang.String value)
Returns aint
representation of the specified string.private boolean
isHyphenRange()
Determines if the following version terminals are part of the <hyphen-range> non-terminal.private boolean
isPartialVersionRange()
Determines if the following version terminals are part of the <partial-version-range> non-terminal.private boolean
isVersionFollowedBy(Stream.ElementType<Lexer.Token> type)
Determines if the version terminals are followed by the specified token type.private boolean
isWildcardRange()
Determines if the following version terminals are part of the <wildcard-range> non-terminal.static Parser<Expression>
newInstance()
Creates and returns new instance of theExpressionParser
class.Expression
parse(java.lang.String input)
Parses the SemVer Expressions.private CompositeExpression
parseCaretRange()
Parses the <caret-range> non-terminal.private CompositeExpression
parseComparisonRange()
Parses the <comparison-range> non-terminal.private CompositeExpression
parseHyphenRange()
Parses the <hyphen-range> non-terminal.private CompositeExpression
parseMoreExpressions(CompositeExpression expr)
Parses the <more-expr> non-terminal.private CompositeExpression
parsePartialVersionRange()
Parses the <partial-version-range> non-terminal.private CompositeExpression
parseRange()
Parses the <range> non-terminal.private CompositeExpression
parseSemVerExpression()
Parses the <semver-expr> non-terminal.private CompositeExpression
parseTildeRange()
Parses the <tilde-range> non-terminal.private Version
parseVersion()
Parses the <version> non-terminal.private CompositeExpression
parseWildcardRange()
Parses the <wildcard-range> non-terminal.private Version
versionFor(int major)
Creates aVersion
instance for the specified major version.private Version
versionFor(int major, int minor)
Creates aVersion
instance for the specified major and minor versions.private Version
versionFor(int major, int minor, int patch)
Creates aVersion
instance for the specified major, minor and patch versions.
-
-
-
Field Detail
-
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 Detail
-
ExpressionParser
ExpressionParser(Lexer lexer)
Constructs aExpressionParser
instance with the corresponding lexer.- Parameters:
lexer
- the lexer to use for tokenization of the input string
-
-
Method Detail
-
newInstance
public static Parser<Expression> newInstance()
Creates and returns new instance of theExpressionParser
class. This method implements the Static Factory Method pattern.- Returns:
- a new instance of the
ExpressionParser
class
-
parse
public Expression parse(java.lang.String input)
Parses the SemVer Expressions.- Specified by:
parse
in interfaceParser<Expression>
- Parameters:
input
- a string representing the SemVer Expression- Returns:
- the AST for the SemVer Expressions
- Throws:
LexerException
- when encounters an illegal characterUnexpectedTokenException
- 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 orfalse
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 orfalse
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 orfalse
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 alookahead(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 orfalse
otherwise
-
versionFor
private Version versionFor(int major)
Creates aVersion
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 aVersion
instance for the specified major and minor versions.- Parameters:
major
- the major version numberminor
- 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 aVersion
instance for the specified major, minor and patch versions.- Parameters:
major
- the major version numberminor
- the minor version numberpatch
- the patch version number- Returns:
- the version for the specified major, minor and patch versions
-
intOf
private int intOf(java.lang.String value)
Returns aint
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
-
-