From 159abf6286af5f8f587d98fc283c4bac2f3f0233 Mon Sep 17 00:00:00 2001 From: Flowsqy <47575244+Flowsqy@users.noreply.github.com> Date: Wed, 23 Feb 2022 16:31:33 +0100 Subject: [PATCH] Add final parse method --- .../config/hologram/parser/FormatParser.java | 83 +++++++++++++++++-- .../config/hologram/parser/ParserResult.java | 79 ++++++++++++++++++ 2 files changed, 156 insertions(+), 6 deletions(-) create mode 100644 plugin/src/main/java/de/epiceric/shopchest/config/hologram/parser/ParserResult.java diff --git a/plugin/src/main/java/de/epiceric/shopchest/config/hologram/parser/FormatParser.java b/plugin/src/main/java/de/epiceric/shopchest/config/hologram/parser/FormatParser.java index 7745a9d..b22de0f 100644 --- a/plugin/src/main/java/de/epiceric/shopchest/config/hologram/parser/FormatParser.java +++ b/plugin/src/main/java/de/epiceric/shopchest/config/hologram/parser/FormatParser.java @@ -11,7 +11,7 @@ import java.util.function.Function; public class FormatParser { - public List> getTokens(String string) { + private List> getTokens(String string) { final List> tokens = new LinkedList<>(); final char[] chars = string.toCharArray(); @@ -83,7 +83,7 @@ public class FormatParser { return new ArrayList<>(tokens); } - public Token getToken(StringBuilder currentToken) { + private Token getToken(StringBuilder currentToken) { // If it's empty, don't need to add a token if (currentToken.length() == 0) { return null; @@ -151,7 +151,7 @@ public class FormatParser { return token; } - public List> createNode(Iterable> tokens) { + private List> createNode(Iterable> tokens) { final Iterator> tokenIterator = tokens.iterator(); final Counter counter = new Counter(); final List> resolvedTokens = resolveNode(tokenIterator, counter).getValue(); @@ -165,7 +165,7 @@ public class FormatParser { return resolvedTokens; } - public Token>> resolveNode(Iterator> tokens, Counter counter) { + private Token>> resolveNode(Iterator> tokens, Counter counter) { final List> nodeTokens = new LinkedList<>(); while (tokens.hasNext()) { final Token token = tokens.next(); @@ -213,7 +213,7 @@ public class FormatParser { return null; } - public

Token createFunctions(Iterable> tokens, Function providerFunction, Map> providerTypes) { + private

Token createFunctions(Iterable> tokens, Function providerFunction, Map> providerTypes) { Chain> tokensChain = Chain.getChain(tokens); if (tokensChain == null) { return null; @@ -658,7 +658,7 @@ public class FormatParser { // It uses a valid provided value if (provided != null) { final Class providedClass = providerTypes.get(provided); - // The provided value is a number + // The provided value is a string if (providedClass == String.class) { // Return the provided key return new MapProvider.StringMapProvider<>(provided); @@ -675,4 +675,75 @@ public class FormatParser { return null; } + public

ParserResult

parse(String input, Function providerFunction, Map> providerTypes) { + final List> tokens = getTokens(input); + final List> tokenNode = createNode(tokens); + final Token token = createFunctions(tokenNode, providerFunction, providerTypes); + + if (token == null) { + return new ParserResult<>(null, null, null, null); + } + + // Constant check + if (token.getType() == Token.VALUE) { + final String value = (String) token.getValue(); + final P provided = providerFunction.apply(value); + // It uses a valid provided value + if (provided != null) { + final Class providedClass = providerTypes.get(provided); + // The provided value is a number + if (providedClass == String.class) { + // Return the provided key + return new ParserResult<>( + null, + null, + new MapProvider.StringMapProvider<>(provided), + null + ); + } + if (providedClass == Boolean.class) { + return new ParserResult<>( + new ProviderCondition<>(new MapProvider.BooleanMapProvider<>(provided)), + null, + null, + null + ); + } + if (providedClass == Double.class) { + return new ParserResult<>( + null, + null, + new MapProvider.DoubleMapProvider<>(provided), + null + ); + } + // Normally impossible + throw new RuntimeException("'" + value + "' can not be used as constant, its type is not handled"); + } else { + throw new RuntimeException("'" + value + "' does not exist"); + } + } else if (token.getType() == Token.DOUBLE || token.getType() == Token.STRING) { + return new ParserResult<>( + null, + null, + null, + token.getValue() + ); + } else if (token.getType() == Token.CALCULATION) { + return new ParserResult<>(null, + cast(token.getValue()), + null, + null + ); + } else if (token.getType() == Token.CONDITION) { + return new ParserResult<>( + cast(token.getValue()), + null, + null, + null + ); + } + throw new RuntimeException("Can not figure out what is the type of the parsed input"); + } + } diff --git a/plugin/src/main/java/de/epiceric/shopchest/config/hologram/parser/ParserResult.java b/plugin/src/main/java/de/epiceric/shopchest/config/hologram/parser/ParserResult.java new file mode 100644 index 0000000..a0cea53 --- /dev/null +++ b/plugin/src/main/java/de/epiceric/shopchest/config/hologram/parser/ParserResult.java @@ -0,0 +1,79 @@ +package de.epiceric.shopchest.config.hologram.parser; + +import de.epiceric.shopchest.config.hologram.calculation.Calculation; +import de.epiceric.shopchest.config.hologram.condition.Condition; + +import java.util.Map; +import java.util.Objects; +import java.util.function.Function; + +public class ParserResult

{ + + private final Condition> condition; + private final Calculation> calculation; + private final Function, ?> value; + private final Object constant; + + public ParserResult(Condition> condition, Calculation> calculation, Function, ?> value, Object constant) { + this.condition = condition; + this.calculation = calculation; + this.value = value; + this.constant = constant; + } + + public Condition> getCondition() { + return condition; + } + + public Calculation> getCalculation() { + return calculation; + } + + public Function, ?> getValue() { + return value; + } + + public Object getConstant() { + return constant; + } + + public boolean isCondition() { + return condition != null; + } + + public boolean isCalculation() { + return calculation != null; + } + + public boolean isValue() { + return value != null; + } + + public boolean isConstant() { + return constant != null; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ParserResult that = (ParserResult) o; + return Objects.equals(condition, that.condition) && Objects.equals(calculation, that.calculation) && Objects.equals(value, that.value) && Objects.equals(constant, that.constant); + } + + @Override + public int hashCode() { + return Objects.hash(condition, calculation, value, constant); + } + + @Override + public String toString() { + return "ParserResult{" + + "condition=" + condition + + ", calculation=" + calculation + + ", value=" + value + + ", constant=" + constant + + '}'; + } + +}