1
0
Fork
You've already forked ExpressionParser
0
The Expression parsing system from AdvancedTabOverlay and BungeeTabListPlus, extracted and turned into their own library to use!
Java 100%
Find a file
Andre601 e637fff5df
All checks were successful
ci/woodpecker/push/update_docs Pipeline was successful
Don't wrap NumberFormat#parse in a try-catch
2025年07月04日 23:53:31 +02:00
.woodpecker Add manual trigger 2025年02月04日 01:22:46 +00:00
src Don't wrap NumberFormat#parse in a try-catch 2025年07月04日 23:53:31 +02:00
.gitignore Version 1.6.0 2025年02月04日 02:09:06 +01:00
LICENSE Initial commit 2024年02月23日 16:21:28 +01:00
pom.xml Don't wrap NumberFormat#parse in a try-catch 2025年07月04日 23:53:31 +02:00
README.md Update readme and change createDefault method 2024年03月07日 19:56:07 +01:00

ExpressionParser

ExpressionParser is a copy of the original expression parser made by @CodeCrafter47 for the TabOverlayCommon project used by his plugins BungeeTabListPlus and AdvancedTabOverlay and shared with his permission.

It allows you to parse simple to complex expressions using Tokenization, while also allowing you to further extend and customize it.

License

This project is licensed under GNU General Public License v3.0 and as such is freely available for everyone.
Original credit and copyright goes to CodeCrafter47 for the original Expression parser system.

How it works

To parse a String into a ExpressionTemplate one has to create new instances of the ExpressionTokenizer and ExpressionTemplateParser, and call their respective parse method.
The ExpressionTokenizer takes a Iterable of TokenReaders while the ExpressionTemplateParser takes a ImmutableMap of Token keys with Operator values.

For the sake of demonstration are we using DefaultExpressionParserEngine, which already creates the necessary instances for us through the provided Lists and Map.
When calling the compile(String, ParseWarnCollector) method will the DefaultExpressionParserEngine call the parse(String, ParseWarnCollector) method of ExpressionTokenizer and parse(List<Token>, ParseWarnCollector) of the ExpressionTemplateParser.

The ExpressionTokenizer will iterate through the String one character at a time, skipping whitespaces in the process.
For each Iteration is it going through a list of TokenReaders to see if any returns a non-null Token. Should one be found will it be added to the List of Tokens the ExpressionTokenizer returns. The TokenReader returning a valid token also updates the position in the String for the next iteration.

The List of Tokens is being given to the ExpressionTemplateParser which first will try to turn as many of the tokens into a single ExpressionTemplate.
It does so by iterating through a list of ValueReaders, giving each the list of Tokens to try and convert. Should no valid ValueReader be found will an exception be thrown, which is caught by ExpressionTemplateParser, added to the ParseWarnCollector before returning null to cancel the parsing.
Should, however, a valid ExpressionTemplate be found, will it be added to a list before moving on to finding Operators.

To find Operators, the ExpressionTemplateParser first checks if the list of Tokens still has entries left. Should this be the case will the first entry be removed from the list, which also gives the Token entry that was removed.
This Token is then used as a key for the ImmutableMap containing Token keys and Operator values. Should no entry be found will a warning be added to the ParseWarnCollector before null is returned to stop the parsing.
Should a Operator be found will it be added to a list of Operators before continuing with parsing the remaining tokens the same way like in the start. Should the list at this point be empty is a warning added to the ParseWarnCollector before null is returned to stop the parsing.
In the next step is the list of Operators iterated through, prioritizing Operators with a higher priority. The Operator is used to create a new ExpressionTemplate using the two ExpressionTemplates that exist before and after the operator in the String. In the case of a ListOperator are the different ExpressionTemplates created by the Operators AND-ed together.

As a final step is the List of ExpressionTemplates updated before returning the very first entry of the list.

Getting the library

Note

Replace {VERSION} with the latest available release listed in the Packages tab.

Maven (pom.xml)

<repositories>
 <repository>
 <id>codeberg</id>
 <url>https://codeberg.org/api/packages/Andre601/maven/</url>
 </repository>
</repositories>
<dependencies>
 <dependency>
 <groupId>ch.andre601</groupId>
 <artifactId>expressionparser</artifactId>
 <version>{VERSION}</version>
 <scope>compile</scope> <!-- Includes the project -->
 </dependency>
</dependencies>

Gradle (build.gradle)

repositories {
 maven { url = "https://codeberg.org/api/packages/Andre601/maven/" }
}
dependencies {
 implementation "ch.andre601:expressionparser:{VERSION}"
}

Use ExpressionParser

The ExpressionParser offers a DefaultExpressionParserEngine class that can be used to parse expressions straight away.

To create a new instance, one can use two possible options:

  • Call the static createDefault() method of the DefaultExpressionParserEngine to retrieve an instance with default values applied.
  • Create a new DefaultBuilder instance and later build() it. You need to add values (i.e. through addDefaults()) yourself.

Once you obtained an Instance, you can call the compile(String, ParseWarnCollector) method to parse the provided String into a ExpressionTemplate which would hold the final result of your expression.

Adding own Tokens

Adding your own Tokens to the ExpressionParser is explained in detail on the dedicated Wiki Page.

Adding own Operators

Adding your own Operators to the ExpressionParser is explained in detail on the dedicated Wiki Page.