Class ExpressionParser
java.lang.Object
com.github.zafarkhaja.semver.expr.ExpressionParser
- All Implemented Interfaces:
Parser<Expression>
A parser for the SemVer Expressions.
- Since:
- 0.7.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Lexer
The lexer instance used for tokenization of the input string.private Stream<Lexer.Token>
The stream of tokens produced by the lexer. -
Constructor Summary
ConstructorsConstructorDescriptionExpressionParser
(Lexer lexer) Constructs aExpressionParser
instance with the corresponding lexer. -
Method Summary
Modifier and TypeMethodDescriptionprivate Lexer.Token
consumeNextToken
(Lexer.Token.Type... expected) Tries to consume the next token in the stream.private int
Returns aint
representation of the specified string.private boolean
Determines if the following version terminals are part of the <hyphen-range> non-terminal.private boolean
Determines if the following version terminals are part of the <partial-version-range> non-terminal.private boolean
Determines if the version terminals are followed by the specified token type.private boolean
Determines if the following version terminals are part of the <wildcard-range> non-terminal.static Parser<Expression>
Creates and returns new instance of theExpressionParser
class.Parses the SemVer Expressions.private CompositeExpression
Parses the <caret-range> non-terminal.private CompositeExpression
Parses the <comparison-range> non-terminal.private CompositeExpression
Parses the <hyphen-range> non-terminal.private CompositeExpression
Parses the <more-expr> non-terminal.private CompositeExpression
Parses the <partial-version-range> non-terminal.private CompositeExpression
Parses the <range> non-terminal.private CompositeExpression
Parses the <semver-expr> non-terminal.private CompositeExpression
Parses the <tilde-range> non-terminal.private Version
Parses the <version> non-terminal.private CompositeExpression
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 Details
-
lexer
The lexer instance used for tokenization of the input string. -
tokens
The stream of tokens produced by the lexer.
-
-
Constructor Details
-
ExpressionParser
ExpressionParser(Lexer lexer) Constructs aExpressionParser
instance with the corresponding lexer.- Parameters:
lexer
- the lexer to use for tokenization of the input string
-
-
Method Details
-
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
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
Parses the <semver-expr> non-terminal.<semver-expr> ::= "(" <semver-expr> ")" | "!" "(" <semver-expr> ")" | <semver-expr> <more-expr> | <range>
- Returns:
- the expression AST
-
parseMoreExpressions
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
Parses the <range> non-terminal.<expr> ::= <comparison-range> | <wildcard-expr> | <tilde-range> | <caret-range> | <hyphen-range> | <partial-version-range>
- Returns:
- the expression AST
-
parseComparisonRange
Parses the <comparison-range> non-terminal.<comparison-range> ::= <comparison-op> <version> | <version>
- Returns:
- the expression AST
-
parseTildeRange
Parses the <tilde-range> non-terminal.<tilde-range> ::= "~" <version>
- Returns:
- the expression AST
-
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
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
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
Parses the <partial-version-range> non-terminal.<partial-version-range> ::= <major> | <major> "." <minor>
- Returns:
- the expression AST
-
parseVersion
Parses the <version> non-terminal.<version> ::= <major> | <major> "." <minor> | <major> "." <minor> "." <patch>
- Returns:
- the parsed version
-
isVersionFollowedBy
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
Creates aVersion
instance for the specified major version.- Parameters:
major
- the major version number- Returns:
- the version for the specified major version
-
versionFor
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
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
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
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
-