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 LexerlexerThe lexer instance used for tokenization of the input string.private Stream<Lexer.Token>tokensThe stream of tokens produced by the lexer.
-
Constructor Summary
Constructors Constructor Description ExpressionParser(Lexer lexer)Constructs aExpressionParserinstance with the corresponding lexer.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private Lexer.TokenconsumeNextToken(Lexer.Token.Type... expected)Tries to consume the next token in the stream.private intintOf(java.lang.String value)Returns aintrepresentation of the specified string.private booleanisHyphenRange()Determines if the following version terminals are part of the <hyphen-range> non-terminal.private booleanisPartialVersionRange()Determines if the following version terminals are part of the <partial-version-range> non-terminal.private booleanisVersionFollowedBy(Stream.ElementType<Lexer.Token> type)Determines if the version terminals are followed by the specified token type.private booleanisWildcardRange()Determines if the following version terminals are part of the <wildcard-range> non-terminal.static Parser<Expression>newInstance()Creates and returns new instance of theExpressionParserclass.Expressionparse(java.lang.String input)Parses the SemVer Expressions.private CompositeExpressionparseCaretRange()Parses the <caret-range> non-terminal.private CompositeExpressionparseComparisonRange()Parses the <comparison-range> non-terminal.private CompositeExpressionparseHyphenRange()Parses the <hyphen-range> non-terminal.private CompositeExpressionparseMoreExpressions(CompositeExpression expr)Parses the <more-expr> non-terminal.private CompositeExpressionparsePartialVersionRange()Parses the <partial-version-range> non-terminal.private CompositeExpressionparseRange()Parses the <range> non-terminal.private CompositeExpressionparseSemVerExpression()Parses the <semver-expr> non-terminal.private CompositeExpressionparseTildeRange()Parses the <tilde-range> non-terminal.private VersionparseVersion()Parses the <version> non-terminal.private CompositeExpressionparseWildcardRange()Parses the <wildcard-range> non-terminal.private VersionversionFor(int major)Creates aVersioninstance for the specified major version.private VersionversionFor(int major, int minor)Creates aVersioninstance for the specified major and minor versions.private VersionversionFor(int major, int minor, int patch)Creates aVersioninstance 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 aExpressionParserinstance 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 theExpressionParserclass. This method implements the Static Factory Method pattern.- Returns:
- a new instance of the
ExpressionParserclass
-
parse
public Expression parse(java.lang.String input)
Parses the SemVer Expressions.- Specified by:
parsein 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:
trueif the following version terminals are part of the <wildcard-range> non-terminal orfalseotherwise
-
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:
trueif the following version terminals are part of the <hyphen-range> non-terminal orfalseotherwise
-
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:
trueif the following version terminals are part of the <partial-version-range> non-terminal orfalseotherwise
-
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:
trueif the version terminals are followed by the specified token type orfalseotherwise
-
versionFor
private Version versionFor(int major)
Creates aVersioninstance 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 aVersioninstance 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 aVersioninstance 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 aintrepresentation 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
-
-