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 LexerThe 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 aExpressionParserinstance with the corresponding lexer. -
Method Summary
Modifier and TypeMethodDescriptionprivate Lexer.TokenconsumeNextToken(Lexer.Token.Type... expected) Tries to consume the next token in the stream.private intReturns aintrepresentation of the specified string.private booleanDetermines if the following version terminals are part of the <hyphen-range> non-terminal.private booleanDetermines if the following version terminals are part of the <partial-version-range> non-terminal.private booleanDetermines if the version terminals are followed by the specified token type.private booleanDetermines if the following version terminals are part of the <wildcard-range> non-terminal.static Parser<Expression> Creates and returns new instance of theExpressionParserclass.Parses the SemVer Expressions.private CompositeExpressionParses the <caret-range> non-terminal.private CompositeExpressionParses the <comparison-range> non-terminal.private CompositeExpressionParses the <hyphen-range> non-terminal.private CompositeExpressionParses the <more-expr> non-terminal.private CompositeExpressionParses the <partial-version-range> non-terminal.private CompositeExpressionParses the <range> non-terminal.private CompositeExpressionParses the <semver-expr> non-terminal.private CompositeExpressionParses the <tilde-range> non-terminal.private VersionParses the <version> non-terminal.private CompositeExpressionParses 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 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 aExpressionParserinstance 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 theExpressionParserclass. This method implements the Static Factory Method pattern.- Returns:
- a new instance of the
ExpressionParserclass
-
parse
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
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:
trueif the following version terminals are part of the <wildcard-range> non-terminal orfalseotherwise
-
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
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
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:
trueif the version terminals are followed by the specified token type orfalseotherwise
-
versionFor
Creates aVersioninstance for the specified major version.- Parameters:
major- the major version number- Returns:
- the version for the specified major version
-
versionFor
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
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
Returns aintrepresentation 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
-