From a16e639d1c87c0a5305ca0065faf5082f4744a42 Mon Sep 17 00:00:00 2001 From: JasperLorelai Date: Wed, 27 Mar 2024 17:02:25 +0100 Subject: [PATCH] feat: Dot-notation function invocation --- .../eu/jasperlorelai/circe/parser/Parser.java | 29 ++++++++++++++++--- .../circe/parser/expression/FunctionNode.java | 7 ++++- .../circe/parser/function/CalcFunction.java | 11 ++++--- .../circe/parser/function/CharAtFunction.java | 19 ++++++++---- .../parser/function/CharCodeAtFunction.java | 19 ++++++++---- .../parser/function/ContainsFunction.java | 23 ++++++++++----- .../parser/function/ElementAtFunction.java | 19 ++++++++---- .../parser/function/EndsWithFunction.java | 19 ++++++++---- .../circe/parser/function/EqualsFunction.java | 21 ++++++++++---- .../parser/function/IndexOfFunction.java | 23 ++++++++++----- .../parser/function/IsEmptyFunction.java | 18 ++++++------ .../circe/parser/function/JoinFunction.java | 19 ++++++++---- .../parser/function/LastIndexOfFunction.java | 23 ++++++++++----- .../parser/function/LowercaseFunction.java | 12 ++++---- .../circe/parser/function/MatchFunction.java | 19 ++++++++---- .../circe/parser/function/PadEndFunction.java | 23 +++++++++++---- .../parser/function/PadStartFunction.java | 23 +++++++++++---- .../circe/parser/function/PopFunction.java | 10 +++---- .../circe/parser/function/PushFunction.java | 21 +++++++++----- .../circe/parser/function/RepeatFunction.java | 19 ++++++++---- .../parser/function/ReplaceAllFunction.java | 17 ++++++++--- .../parser/function/ReplaceFirstFunction.java | 17 ++++++++--- .../parser/function/ReverseArrayFunction.java | 11 ++++--- .../function/ReverseStringFunction.java | 12 ++++---- .../circe/parser/function/RoundFunction.java | 12 ++++---- .../circe/parser/function/ShiftFunction.java | 12 ++++---- .../circe/parser/function/SizeFunction.java | 14 ++++----- .../circe/parser/function/SliceFunction.java | 17 ++++++++--- .../circe/parser/function/SpliceFunction.java | 19 ++++++++---- .../circe/parser/function/SplitFunction.java | 19 ++++++++---- .../parser/function/StartsWithFunction.java | 19 ++++++++---- .../parser/function/SubstringFunction.java | 17 ++++++++--- .../parser/function/SubtractFunction.java | 19 ++++++++---- .../parser/function/ToArrayFunction.java | 15 +++++----- .../parser/function/ToNumberFunction.java | 12 ++++---- .../parser/function/ToStringFunction.java | 19 ++++++++---- .../circe/parser/function/TrimFunction.java | 12 ++++---- .../parser/function/UnicodeFunction.java | 12 ++++---- .../parser/function/UnshiftFunction.java | 21 +++++++++----- .../parser/function/UppercaseFunction.java | 12 ++++---- .../circe/parser/function/util/Function.java | 14 ++++++--- .../function/util/ZeroParamFunction.java | 20 +++++++++++++ .../tokenizer/type/SpecialTokenType.java | 1 + .../eu/jasperlorelai/circe/CirceTests.java | 6 ++++ 44 files changed, 478 insertions(+), 248 deletions(-) create mode 100644 src/main/java/eu/jasperlorelai/circe/parser/function/util/ZeroParamFunction.java diff --git a/src/main/java/eu/jasperlorelai/circe/parser/Parser.java b/src/main/java/eu/jasperlorelai/circe/parser/Parser.java index 87a3136..41c082c 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/Parser.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/Parser.java @@ -3,6 +3,8 @@ import java.util.List; import java.util.ArrayList; +import org.jetbrains.annotations.Nullable; + import eu.jasperlorelai.circe.exeption.*; import eu.jasperlorelai.circe.tokenizer.type.*; import eu.jasperlorelai.circe.parser.expression.*; @@ -10,6 +12,7 @@ import eu.jasperlorelai.circe.tokenizer.Tokenizer.Token; import eu.jasperlorelai.circe.tokenizer.type.util.TokenType; import eu.jasperlorelai.circe.parser.function.util.Variables; +import eu.jasperlorelai.circe.tokenizer.type.util.TokenSuperType; import eu.jasperlorelai.circe.parser.expression.util.ExpressionNode; public class Parser { @@ -46,11 +49,23 @@ public String parse(String string) { private ExpressionNode expression() { if (lookahead == null) return null; - return switch (lookahead.tokenType().type()) { - case FUNCTION -> function(); + ExpressionNode node = switch (lookahead.tokenType().type()) { + case FUNCTION -> function(null); case LITERAL -> literal(); case SPECIAL -> special(); }; + return tryInvoke(node); + } + + private ExpressionNode tryInvoke(ExpressionNode node) { + if (lookahead == null || lookahead.tokenType() != SpecialTokenType.DOT) return node; + + nextToken(); + if (lookahead == null) throw new ParserException("Expected function name after '.', instead received nothing."); + if (lookahead.tokenType().type() != TokenSuperType.FUNCTION) { + throw new ParserException("Expected function name after '.', instead received: " + lookahead.tokenType() + " '" + lookahead.value() + "'"); + } + return tryInvoke(function(node)); } private ExpressionNode special() { @@ -63,7 +78,7 @@ private ExpressionNode special() { }; } - private ExpressionNode function() { + private ExpressionNode function(@Nullable ExpressionNode target) { if (lookahead == null) return null; String functionName = lookahead.value(); FunctionNode node = new FunctionNode(lookahead); @@ -101,7 +116,13 @@ private ExpressionNode function() { if (awaitingArg) throw new ParserException("Expected argument after ',' in function '" + functionName + "'."); if (!closed) throw new ParserException("Function '" + functionName + "' does not end with ')'"); - node.process(arguments); + + if (target == null) { + if (arguments.isEmpty()) throw new ParserException("Function '" + functionName + "' is not invoked on any target expression."); + else target = arguments.remove(0); + } + + node.process(target, arguments); return node; } diff --git a/src/main/java/eu/jasperlorelai/circe/parser/expression/FunctionNode.java b/src/main/java/eu/jasperlorelai/circe/parser/expression/FunctionNode.java index 0108e5d..ebdb879 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/expression/FunctionNode.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/expression/FunctionNode.java @@ -23,8 +23,9 @@ public FunctionNode(Token token) { function = type.createFunction(); } - public void process(List arguments) { + public void process(ExpressionNode target, List arguments) { // Convert all function nodes to other node types. + if (target instanceof FunctionNode fun) target = fun.getFunction().execute(); List localArgs = new ArrayList<>(); for (ExpressionNode node : arguments) { if (node instanceof FunctionNode fun) localArgs.add(fun.getFunction().execute()); @@ -35,6 +36,10 @@ public void process(List arguments) { List paramTypes = function.getParameterTypes(); if (localArgs.size() != paramTypes.size()) throw new ParserException("Function '" + functionName + "' expects " + paramTypes.size() + " arguments, but the supplied amount was " + localArgs.size()); + ParameterType targetType = function.getTargetType(); + if (!targetType.isValid(target.getType())) throw new ParserException("Function '" + functionName + "' has target of " + target.getType() + " type defined when '" + targetType + " was expected'."); + function.initializeTarget(target); + for (int i = 0; i < paramTypes.size(); i++) { ParameterType parameter = paramTypes.get(i); NodeType argument = localArgs.get(i).getType(); diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/CalcFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/CalcFunction.java index 9a8f35c..19bba75 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/CalcFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/CalcFunction.java @@ -1,7 +1,6 @@ package eu.jasperlorelai.circe.parser.function; import java.util.Map; -import java.util.List; import java.util.HashMap; import org.jetbrains.annotations.NotNull; @@ -13,19 +12,19 @@ import eu.jasperlorelai.circe.parser.expression.util.*; import eu.jasperlorelai.circe.exeption.FunctionCallException; -public class CalcFunction extends Function { +public class CalcFunction extends ZeroParamFunction { private StringLiteralNode string; @NotNull @Override - public List getParameterTypes() { - return NodeType.STRING.asParameterTypes(); + public ParameterType getTargetType() { + return NodeType.STRING.asParameterType(); } @Override - public void initializeArguments(List arguments) { - string = castString(arguments.get(0)); + public void initializeTarget(ExpressionNode target) { + string = castString(target); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/CharAtFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/CharAtFunction.java index 8a5fffe..adf2948 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/CharAtFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/CharAtFunction.java @@ -14,19 +14,26 @@ public class CharAtFunction extends Function { private StringLiteralNode string; private NumberLiteralNode index; + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.STRING.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + string = castString(target); + } + @NotNull @Override public List getParameterTypes() { - return List.of( - NodeType.STRING.asParameterType(), - NodeType.NUMBER.asParameterType() - ); + return NodeType.NUMBER.asParameterTypes(); } @Override public void initializeArguments(List arguments) { - string = castString(arguments.get(0)); - index = castNumber(arguments.get(1)); + index = castNumber(arguments.get(0)); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/CharCodeAtFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/CharCodeAtFunction.java index bf00d14..32aeb60 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/CharCodeAtFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/CharCodeAtFunction.java @@ -14,19 +14,26 @@ public class CharCodeAtFunction extends Function { private StringLiteralNode string; private NumberLiteralNode index; + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.STRING.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + string = castString(target); + } + @NotNull @Override public List getParameterTypes() { - return List.of( - NodeType.STRING.asParameterType(), - NodeType.NUMBER.asParameterType() - ); + return NodeType.NUMBER.asParameterTypes(); } @Override public void initializeArguments(List arguments) { - string = castString(arguments.get(0)); - index = castNumber(arguments.get(1)); + index = castNumber(arguments.get(0)); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/ContainsFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/ContainsFunction.java index 81bcba0..3fe86c3 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/ContainsFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/ContainsFunction.java @@ -17,21 +17,28 @@ public class ContainsFunction extends Function { private StringLiteralNode elementString; private NumberLiteralNode elementNumber; + @NotNull + @Override + public ParameterType getTargetType() { + return ParameterType.create(NodeType.STRING, NodeType.ARRAY); + } + + @Override + public void initializeTarget(ExpressionNode target) { + array = castArray(target); + string = castString(target); + } + @NotNull @Override public List getParameterTypes() { - return List.of( - ParameterType.create(NodeType.STRING, NodeType.ARRAY), - ParameterType.create(NodeType.STRING, NodeType.NUMBER) - ); + return List.of(ParameterType.create(NodeType.STRING, NodeType.NUMBER)); } @Override public void initializeArguments(List arguments) { - array = castArray(arguments.get(0)); - string = castString(arguments.get(0)); - elementString = castString(arguments.get(1)); - elementNumber = castNumber(arguments.get(1)); + elementString = castString(arguments.get(0)); + elementNumber = castNumber(arguments.get(0)); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/ElementAtFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/ElementAtFunction.java index 74d313e..8c28422 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/ElementAtFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/ElementAtFunction.java @@ -14,19 +14,26 @@ public class ElementAtFunction extends Function { private ArrayNode array; private NumberLiteralNode index; + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.ARRAY.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + array = castArray(target); + } + @NotNull @Override public List getParameterTypes() { - return List.of( - NodeType.ARRAY.asParameterType(), - NodeType.NUMBER.asParameterType() - ); + return NodeType.NUMBER.asParameterTypes(); } @Override public void initializeArguments(List arguments) { - array = castArray(arguments.get(0)); - index = castNumber(arguments.get(1)); + index = castNumber(arguments.get(0)); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/EndsWithFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/EndsWithFunction.java index a0fcd1b..f92701c 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/EndsWithFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/EndsWithFunction.java @@ -13,19 +13,26 @@ public class EndsWithFunction extends Function { private StringLiteralNode string; private StringLiteralNode substring; + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.STRING.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + string = castString(target); + } + @NotNull @Override public List getParameterTypes() { - return List.of( - NodeType.STRING.asParameterType(), - NodeType.STRING.asParameterType() - ); + return NodeType.STRING.asParameterTypes(); } @Override public void initializeArguments(List arguments) { - string = castString(arguments.get(0)); - substring = castString(arguments.get(1)); + substring = castString(arguments.get(0)); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/EqualsFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/EqualsFunction.java index 22a7d2e..1a0f684 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/EqualsFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/EqualsFunction.java @@ -16,19 +16,28 @@ public class EqualsFunction extends Function { private ArrayNode arrayFirst; private ArrayNode arraySecond; + @NotNull + @Override + public ParameterType getTargetType() { + return ParameterType.create(NodeType.STRING, NodeType.ARRAY); + } + + @Override + public void initializeTarget(ExpressionNode target) { + stringFirst = castString(target); + arrayFirst = castArray(target); + } + @NotNull @Override public List getParameterTypes() { - ParameterType arrayOrString = ParameterType.create(NodeType.STRING, NodeType.ARRAY); - return List.of(arrayOrString, arrayOrString); + return List.of(ParameterType.create(NodeType.STRING, NodeType.ARRAY)); } @Override public void initializeArguments(List arguments) { - stringFirst = castString(arguments.get(0)); - stringSecond = castString(arguments.get(1)); - arrayFirst = castArray(arguments.get(0)); - arraySecond = castArray(arguments.get(1)); + stringSecond = castString(arguments.get(0)); + arraySecond = castArray(arguments.get(0)); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/IndexOfFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/IndexOfFunction.java index fea28c3..5f6eaea 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/IndexOfFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/IndexOfFunction.java @@ -17,21 +17,28 @@ public class IndexOfFunction extends Function { private StringLiteralNode elementString; private NumberLiteralNode elementNumber; + @NotNull + @Override + public ParameterType getTargetType() { + return ParameterType.create(NodeType.STRING, NodeType.ARRAY); + } + + @Override + public void initializeTarget(ExpressionNode target) { + string = castString(target); + array = castArray(target); + } + @NotNull @Override public List getParameterTypes() { - return List.of( - ParameterType.create(NodeType.STRING, NodeType.ARRAY), - ParameterType.create(NodeType.STRING, NodeType.NUMBER) - ); + return List.of(ParameterType.create(NodeType.STRING, NodeType.NUMBER)); } @Override public void initializeArguments(List arguments) { - string = castString(arguments.get(0)); - array = castArray(arguments.get(0)); - elementString = castString(arguments.get(1)); - elementNumber = castNumber(arguments.get(1)); + elementString = castString(arguments.get(0)); + elementNumber = castNumber(arguments.get(0)); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/IsEmptyFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/IsEmptyFunction.java index 7b1ba0e..780f0a1 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/IsEmptyFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/IsEmptyFunction.java @@ -1,7 +1,5 @@ package eu.jasperlorelai.circe.parser.function; -import java.util.List; - import org.jetbrains.annotations.NotNull; import eu.jasperlorelai.circe.parser.function.util.*; @@ -9,24 +7,26 @@ import eu.jasperlorelai.circe.parser.expression.ArrayNode; import eu.jasperlorelai.circe.parser.expression.StringLiteralNode; -public class IsEmptyFunction extends Function { +public class IsEmptyFunction extends ZeroParamFunction { private StringLiteralNode string; private ArrayNode array; + @NotNull @Override - public @NotNull List getParameterTypes() { - return List.of(ParameterType.create(NodeType.STRING, NodeType.ARRAY)); + public ParameterType getTargetType() { + return ParameterType.create(NodeType.STRING, NodeType.ARRAY); } @Override - public void initializeArguments(List arguments) { - string = castString(arguments.get(0)); - array = castArray(arguments.get(0)); + public void initializeTarget(ExpressionNode target) { + string = castString(target); + array = castArray(target); } + @NotNull @Override - public @NotNull ExpressionNode execute() { + public ExpressionNode execute() { return stringFromBool(string == null ? array.array().isEmpty() : string.quoteless().isEmpty()); } diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/JoinFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/JoinFunction.java index f5138cb..4df839b 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/JoinFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/JoinFunction.java @@ -15,19 +15,26 @@ public class JoinFunction extends Function { private ArrayNode array; private StringLiteralNode delimiter; + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.ARRAY.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + array = castArray(target); + } + @NotNull @Override public List getParameterTypes() { - return List.of( - NodeType.ARRAY.asParameterType(), - NodeType.STRING.asParameterType() - ); + return NodeType.STRING.asParameterTypes(); } @Override public void initializeArguments(List arguments) { - array = castArray(arguments.get(0)); - delimiter = castString(arguments.get(1)); + delimiter = castString(arguments.get(0)); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/LastIndexOfFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/LastIndexOfFunction.java index 8eddf83..83d8205 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/LastIndexOfFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/LastIndexOfFunction.java @@ -17,21 +17,28 @@ public class LastIndexOfFunction extends Function { private StringLiteralNode elementString; private NumberLiteralNode elementNumber; + @NotNull + @Override + public ParameterType getTargetType() { + return ParameterType.create(NodeType.STRING, NodeType.ARRAY); + } + + @Override + public void initializeTarget(ExpressionNode target) { + string = castString(target); + array = castArray(target); + } + @NotNull @Override public List getParameterTypes() { - return List.of( - ParameterType.create(NodeType.STRING, NodeType.ARRAY), - ParameterType.create(NodeType.STRING, NodeType.NUMBER) - ); + return List.of(ParameterType.create(NodeType.STRING, NodeType.NUMBER)); } @Override public void initializeArguments(List arguments) { - string = castString(arguments.get(0)); - array = castArray(arguments.get(0)); - elementString = castString(arguments.get(1)); - elementNumber = castNumber(arguments.get(1)); + elementString = castString(arguments.get(0)); + elementNumber = castNumber(arguments.get(0)); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/LowercaseFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/LowercaseFunction.java index 9b240ff..c8220a1 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/LowercaseFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/LowercaseFunction.java @@ -1,26 +1,24 @@ package eu.jasperlorelai.circe.parser.function; -import java.util.List; - import org.jetbrains.annotations.NotNull; import eu.jasperlorelai.circe.parser.function.util.*; import eu.jasperlorelai.circe.parser.expression.util.*; import eu.jasperlorelai.circe.parser.expression.StringLiteralNode; -public class LowercaseFunction extends Function { +public class LowercaseFunction extends ZeroParamFunction { private StringLiteralNode string; @NotNull @Override - public List getParameterTypes() { - return NodeType.STRING.asParameterTypes(); + public ParameterType getTargetType() { + return NodeType.STRING.asParameterType(); } @Override - public void initializeArguments(List arguments) { - string = castString(arguments.get(0)); + public void initializeTarget(ExpressionNode target) { + string = castString(target); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/MatchFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/MatchFunction.java index 22c1099..7aae10e 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/MatchFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/MatchFunction.java @@ -17,19 +17,26 @@ public class MatchFunction extends Function { private StringLiteralNode string; private StringLiteralNode pattern; + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.STRING.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + string = castString(target); + } + @NotNull @Override public List getParameterTypes() { - return List.of( - NodeType.STRING.asParameterType(), - NodeType.STRING.asParameterType() - ); + return NodeType.STRING.asParameterTypes(); } @Override public void initializeArguments(List arguments) { - string = castString(arguments.get(0)); - pattern = castString(arguments.get(1)); + pattern = castString(arguments.get(0)); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/PadEndFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/PadEndFunction.java index e182dac..ea623d6 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/PadEndFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/PadEndFunction.java @@ -17,10 +17,21 @@ public class PadEndFunction extends Function { private NumberLiteralNode minLength; private StringLiteralNode padChar; + @NotNull @Override - public @NotNull List getParameterTypes() { + public ParameterType getTargetType() { + return NodeType.STRING.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + string = castString(target); + } + + @NotNull + @Override + public List getParameterTypes() { return List.of( - NodeType.STRING.asParameterType(), NodeType.NUMBER.asParameterType(), NodeType.STRING.asParameterType() ); @@ -28,13 +39,13 @@ public class PadEndFunction extends Function { @Override public void initializeArguments(List arguments) { - string = castString(arguments.get(0)); - minLength = castNumber(arguments.get(1)); - padChar = castString(arguments.get(2)); + minLength = castNumber(arguments.get(0)); + padChar = castString(arguments.get(1)); } + @NotNull @Override - public @NotNull ExpressionNode execute() { + public ExpressionNode execute() { StringBuilder newString = new StringBuilder(string.quoteless()); while (minLength.valueInteger() > newString.length()) newString.append(padChar.quoteless()); return stringNode(newString.toString()); diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/PadStartFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/PadStartFunction.java index 4cc3c56..d54380e 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/PadStartFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/PadStartFunction.java @@ -17,10 +17,21 @@ public class PadStartFunction extends Function { private NumberLiteralNode minLength; private StringLiteralNode padChar; + @NotNull @Override - public @NotNull List getParameterTypes() { + public ParameterType getTargetType() { + return NodeType.STRING.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + string = castString(target); + } + + @NotNull + @Override + public List getParameterTypes() { return List.of( - NodeType.STRING.asParameterType(), NodeType.NUMBER.asParameterType(), NodeType.STRING.asParameterType() ); @@ -28,13 +39,13 @@ public class PadStartFunction extends Function { @Override public void initializeArguments(List arguments) { - string = castString(arguments.get(0)); - minLength = castNumber(arguments.get(1)); - padChar = castString(arguments.get(2)); + minLength = castNumber(arguments.get(0)); + padChar = castString(arguments.get(1)); } + @NotNull @Override - public @NotNull ExpressionNode execute() { + public ExpressionNode execute() { StringBuilder newString = new StringBuilder(); String quoteless = string.quoteless(); int length = quoteless.length(); diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/PopFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/PopFunction.java index b37b447..ec2b341 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/PopFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/PopFunction.java @@ -8,19 +8,19 @@ import eu.jasperlorelai.circe.parser.expression.util.*; import eu.jasperlorelai.circe.parser.expression.ArrayNode; -public class PopFunction extends Function { +public class PopFunction extends ZeroParamFunction { private ArrayNode array; @NotNull @Override - public List getParameterTypes() { - return NodeType.ARRAY.asParameterTypes(); + public ParameterType getTargetType() { + return NodeType.ARRAY.asParameterType(); } @Override - public void initializeArguments(List arguments) { - array = castArray(arguments.get(0)); + public void initializeTarget(ExpressionNode target) { + array = castArray(target); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/PushFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/PushFunction.java index b027787..d767822 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/PushFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/PushFunction.java @@ -14,20 +14,27 @@ public class PushFunction extends Function { private StringLiteralNode elementString; private NumberLiteralNode elementNumber; + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.ARRAY.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + array = castArray(target); + } + @NotNull @Override public List getParameterTypes() { - return List.of( - NodeType.ARRAY.asParameterType(), - ParameterType.create(NodeType.STRING, NodeType.NUMBER) - ); + return List.of(ParameterType.create(NodeType.STRING, NodeType.NUMBER)); } @Override public void initializeArguments(List arguments) { - array = castArray(arguments.get(0)); - elementString = castString(arguments.get(1)); - elementNumber = castNumber(arguments.get(1)); + elementString = castString(arguments.get(0)); + elementNumber = castNumber(arguments.get(0)); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/RepeatFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/RepeatFunction.java index 5c05b0c..3679ab7 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/RepeatFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/RepeatFunction.java @@ -14,19 +14,26 @@ public class RepeatFunction extends Function { private StringLiteralNode string; private NumberLiteralNode amount; + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.STRING.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + string = castString(target); + } + @NotNull @Override public List getParameterTypes() { - return List.of( - NodeType.STRING.asParameterType(), - NodeType.NUMBER.asParameterType() - ); + return NodeType.NUMBER.asParameterTypes(); } @Override public void initializeArguments(List arguments) { - string = castString(arguments.get(0)); - amount = castNumber(arguments.get(1)); + amount = castNumber(arguments.get(0)); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/ReplaceAllFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/ReplaceAllFunction.java index 01857ff..99434b6 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/ReplaceAllFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/ReplaceAllFunction.java @@ -15,11 +15,21 @@ public class ReplaceAllFunction extends Function { private StringLiteralNode pattern; private StringLiteralNode replacement; + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.STRING.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + string = castString(target); + } + @NotNull @Override public List getParameterTypes() { return List.of( - NodeType.STRING.asParameterType(), NodeType.STRING.asParameterType(), NodeType.STRING.asParameterType() ); @@ -27,9 +37,8 @@ public List getParameterTypes() { @Override public void initializeArguments(List arguments) { - string = castString(arguments.get(0)); - pattern = castString(arguments.get(1)); - replacement = castString(arguments.get(2)); + pattern = castString(arguments.get(0)); + replacement = castString(arguments.get(1)); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/ReplaceFirstFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/ReplaceFirstFunction.java index f4e2c1e..bebcf33 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/ReplaceFirstFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/ReplaceFirstFunction.java @@ -15,11 +15,21 @@ public class ReplaceFirstFunction extends Function { private StringLiteralNode pattern; private StringLiteralNode replacement; + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.STRING.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + string = castString(target); + } + @NotNull @Override public List getParameterTypes() { return List.of( - NodeType.STRING.asParameterType(), NodeType.STRING.asParameterType(), NodeType.STRING.asParameterType() ); @@ -27,9 +37,8 @@ public List getParameterTypes() { @Override public void initializeArguments(List arguments) { - string = castString(arguments.get(0)); - pattern = castString(arguments.get(1)); - replacement = castString(arguments.get(2)); + pattern = castString(arguments.get(0)); + replacement = castString(arguments.get(1)); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/ReverseArrayFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/ReverseArrayFunction.java index ec176e8..6d6c8ac 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/ReverseArrayFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/ReverseArrayFunction.java @@ -1,6 +1,5 @@ package eu.jasperlorelai.circe.parser.function; -import java.util.List; import java.util.Collections; import org.jetbrains.annotations.NotNull; @@ -9,19 +8,19 @@ import eu.jasperlorelai.circe.parser.expression.util.*; import eu.jasperlorelai.circe.parser.expression.ArrayNode; -public class ReverseArrayFunction extends Function { +public class ReverseArrayFunction extends ZeroParamFunction { private ArrayNode array; @NotNull @Override - public List getParameterTypes() { - return NodeType.ARRAY.asParameterTypes(); + public ParameterType getTargetType() { + return NodeType.ARRAY.asParameterType(); } @Override - public void initializeArguments(List arguments) { - array = castArray(arguments.get(0)); + public void initializeTarget(ExpressionNode target) { + array = castArray(target); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/ReverseStringFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/ReverseStringFunction.java index 7ca16de..ff5d7ae 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/ReverseStringFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/ReverseStringFunction.java @@ -1,26 +1,24 @@ package eu.jasperlorelai.circe.parser.function; -import java.util.List; - import org.jetbrains.annotations.NotNull; import eu.jasperlorelai.circe.parser.function.util.*; import eu.jasperlorelai.circe.parser.expression.util.*; import eu.jasperlorelai.circe.parser.expression.StringLiteralNode; -public class ReverseStringFunction extends Function { +public class ReverseStringFunction extends ZeroParamFunction { private StringLiteralNode string; @NotNull @Override - public List getParameterTypes() { - return NodeType.STRING.asParameterTypes(); + public ParameterType getTargetType() { + return NodeType.STRING.asParameterType(); } @Override - public void initializeArguments(List arguments) { - string = castString(arguments.get(0)); + public void initializeTarget(ExpressionNode target) { + string = castString(target); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/RoundFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/RoundFunction.java index 023f907..5bb6f3f 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/RoundFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/RoundFunction.java @@ -1,26 +1,24 @@ package eu.jasperlorelai.circe.parser.function; -import java.util.List; - import org.jetbrains.annotations.NotNull; import eu.jasperlorelai.circe.parser.function.util.*; import eu.jasperlorelai.circe.parser.expression.util.*; import eu.jasperlorelai.circe.parser.expression.NumberLiteralNode; -public class RoundFunction extends Function { +public class RoundFunction extends ZeroParamFunction { private NumberLiteralNode number; @NotNull @Override - public List getParameterTypes() { - return NodeType.NUMBER.asParameterTypes(); + public ParameterType getTargetType() { + return NodeType.NUMBER.asParameterType(); } @Override - public void initializeArguments(List arguments) { - number = castNumber(arguments.get(0)); + public void initializeTarget(ExpressionNode target) { + number = castNumber(target); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/ShiftFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/ShiftFunction.java index 6257314..b98e7ac 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/ShiftFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/ShiftFunction.java @@ -1,26 +1,24 @@ package eu.jasperlorelai.circe.parser.function; -import java.util.List; - import org.jetbrains.annotations.NotNull; import eu.jasperlorelai.circe.parser.function.util.*; import eu.jasperlorelai.circe.parser.expression.util.*; import eu.jasperlorelai.circe.parser.expression.ArrayNode; -public class ShiftFunction extends Function { +public class ShiftFunction extends ZeroParamFunction { private ArrayNode array; @NotNull @Override - public List getParameterTypes() { - return NodeType.ARRAY.asParameterTypes(); + public ParameterType getTargetType() { + return NodeType.ARRAY.asParameterType(); } @Override - public void initializeArguments(List arguments) { - array = castArray(arguments.get(0)); + public void initializeTarget(ExpressionNode target) { + array = castArray(target); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/SizeFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/SizeFunction.java index 5ce2523..56dad65 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/SizeFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/SizeFunction.java @@ -1,7 +1,5 @@ package eu.jasperlorelai.circe.parser.function; -import java.util.List; - import org.jetbrains.annotations.NotNull; import eu.jasperlorelai.circe.parser.function.util.*; @@ -9,21 +7,21 @@ import eu.jasperlorelai.circe.parser.expression.ArrayNode; import eu.jasperlorelai.circe.parser.expression.StringLiteralNode; -public class SizeFunction extends Function { +public class SizeFunction extends ZeroParamFunction { private ArrayNode array; private StringLiteralNode string; @NotNull @Override - public List getParameterTypes() { - return List.of(ParameterType.create(NodeType.ARRAY, NodeType.STRING)); + public ParameterType getTargetType() { + return ParameterType.create(NodeType.ARRAY, NodeType.STRING); } @Override - public void initializeArguments(List arguments) { - array = castArray(arguments.get(0)); - string = castString(arguments.get(0)); + public void initializeTarget(ExpressionNode target) { + array = castArray(target); + string = castString(target); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/SliceFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/SliceFunction.java index fb32713..5a71dff 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/SliceFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/SliceFunction.java @@ -16,11 +16,21 @@ public class SliceFunction extends Function { private NumberLiteralNode start; private NumberLiteralNode end; + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.ARRAY.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + array = castArray(target); + } + @NotNull @Override public List getParameterTypes() { return List.of( - NodeType.ARRAY.asParameterType(), NodeType.NUMBER.asParameterType(), NodeType.NUMBER.asParameterType() ); @@ -28,9 +38,8 @@ public List getParameterTypes() { @Override public void initializeArguments(List arguments) { - array = castArray(arguments.get(0)); - start = castNumber(arguments.get(1)); - end = castNumber(arguments.get(2)); + start = castNumber(arguments.get(0)); + end = castNumber(arguments.get(1)); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/SpliceFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/SpliceFunction.java index df83ef6..2539260 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/SpliceFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/SpliceFunction.java @@ -17,11 +17,21 @@ public class SpliceFunction extends Function { private NumberLiteralNode deleteCount; private ArrayNode arrayAdd; + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.ARRAY.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + array = castArray(target); + } + @NotNull @Override public List getParameterTypes() { return List.of( - NodeType.ARRAY.asParameterType(), NodeType.NUMBER.asParameterType(), NodeType.NUMBER.asParameterType(), NodeType.ARRAY.asParameterType() @@ -30,10 +40,9 @@ public List getParameterTypes() { @Override public void initializeArguments(List arguments) { - array = castArray(arguments.get(0)); - index = castNumber(arguments.get(1)); - deleteCount = castNumber(arguments.get(2)); - arrayAdd = castArray(arguments.get(3)); + index = castNumber(arguments.get(0)); + deleteCount = castNumber(arguments.get(1)); + arrayAdd = castArray(arguments.get(2)); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/SplitFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/SplitFunction.java index 7193f91..db5fbe7 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/SplitFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/SplitFunction.java @@ -17,21 +17,30 @@ public class SplitFunction extends Function { private StringLiteralNode pattern; private NumberLiteralNode limit; + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.STRING.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + string = castString(target); + } + @NotNull @Override public List getParameterTypes() { return List.of( NodeType.STRING.asParameterType(), - NodeType.STRING.asParameterType(), - NodeType.NUMBER.asParameterType() + NodeType.NUMBER.asParameterType() ); } @Override public void initializeArguments(List arguments) { - string = castString(arguments.get(0)); - pattern = castString(arguments.get(1)); - limit = castNumber(arguments.get(2)); + pattern = castString(arguments.get(0)); + limit = castNumber(arguments.get(1)); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/StartsWithFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/StartsWithFunction.java index b37bf9b..76cdcff 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/StartsWithFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/StartsWithFunction.java @@ -13,19 +13,26 @@ public class StartsWithFunction extends Function { private StringLiteralNode string; private StringLiteralNode substring; + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.STRING.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + string = castString(target); + } + @NotNull @Override public List getParameterTypes() { - return List.of( - NodeType.STRING.asParameterType(), - NodeType.STRING.asParameterType() - ); + return NodeType.STRING.asParameterTypes(); } @Override public void initializeArguments(List arguments) { - string = castString(arguments.get(0)); - substring = castString(arguments.get(1)); + substring = castString(arguments.get(0)); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/SubstringFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/SubstringFunction.java index 9daff18..2b3b907 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/SubstringFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/SubstringFunction.java @@ -15,11 +15,21 @@ public class SubstringFunction extends Function { private NumberLiteralNode startIndex; private NumberLiteralNode endIndex; + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.STRING.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + string = castString(target); + } + @NotNull @Override public List getParameterTypes() { return List.of( - NodeType.STRING.asParameterType(), NodeType.NUMBER.asParameterType(), NodeType.NUMBER.asParameterType() ); @@ -27,9 +37,8 @@ public List getParameterTypes() { @Override public void initializeArguments(List arguments) { - string = castString(arguments.get(0)); - startIndex = castNumber(arguments.get(1)); - endIndex = castNumber(arguments.get(2)); + startIndex = castNumber(arguments.get(0)); + endIndex = castNumber(arguments.get(1)); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/SubtractFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/SubtractFunction.java index 851b602..7009215 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/SubtractFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/SubtractFunction.java @@ -14,19 +14,26 @@ public class SubtractFunction extends Function { private ArrayNode arrayFirst; private ArrayNode arraySecond; + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.ARRAY.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + arrayFirst = castArray(target); + } + @NotNull @Override public List getParameterTypes() { - return List.of( - NodeType.ARRAY.asParameterType(), - NodeType.ARRAY.asParameterType() - ); + return NodeType.ARRAY.asParameterTypes(); } @Override public void initializeArguments(List arguments) { - arrayFirst = castArray(arguments.get(0)); - arraySecond = castArray(arguments.get(1)); + arraySecond = castArray(arguments.get(0)); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/ToArrayFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/ToArrayFunction.java index db657e2..44620ec 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/ToArrayFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/ToArrayFunction.java @@ -1,6 +1,5 @@ package eu.jasperlorelai.circe.parser.function; -import java.util.List; import java.util.Collections; import org.jetbrains.annotations.NotNull; @@ -9,23 +8,25 @@ import eu.jasperlorelai.circe.parser.expression.util.*; import eu.jasperlorelai.circe.parser.expression.ArrayNode; -public class ToArrayFunction extends Function { +public class ToArrayFunction extends ZeroParamFunction { private ExpressionNode node; + @NotNull @Override - public @NotNull List getParameterTypes() { + public ParameterType getTargetType() { // Any - return List.of(ParameterType.create(NodeType.values())); + return ParameterType.create(NodeType.values()); } @Override - public void initializeArguments(List arguments) { - node = arguments.get(0); + public void initializeTarget(ExpressionNode target) { + node = target; } + @NotNull @Override - public @NotNull ExpressionNode execute() { + public ExpressionNode execute() { return new ArrayNode(Collections.singletonList(node)); } diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/ToNumberFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/ToNumberFunction.java index 0fa7ce3..d60c5a5 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/ToNumberFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/ToNumberFunction.java @@ -1,7 +1,5 @@ package eu.jasperlorelai.circe.parser.function; -import java.util.List; - import org.jetbrains.annotations.NotNull; import eu.jasperlorelai.circe.parser.function.util.*; @@ -9,19 +7,19 @@ import eu.jasperlorelai.circe.parser.expression.StringLiteralNode; import eu.jasperlorelai.circe.parser.expression.NumberLiteralNode; -public class ToNumberFunction extends Function { +public class ToNumberFunction extends ZeroParamFunction { private StringLiteralNode string; @NotNull @Override - public List getParameterTypes() { - return NodeType.STRING.asParameterTypes(); + public ParameterType getTargetType() { + return NodeType.STRING.asParameterType(); } @Override - public void initializeArguments(List arguments) { - string = castString(arguments.get(0)); + public void initializeTarget(ExpressionNode target) { + string = castString(target); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/ToStringFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/ToStringFunction.java index 945494f..21edc5e 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/ToStringFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/ToStringFunction.java @@ -13,19 +13,26 @@ public class ToStringFunction extends Function { private NumberLiteralNode number; private NumberLiteralNode radix; + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.NUMBER.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + number = castNumber(target); + } + @NotNull @Override public List getParameterTypes() { - return List.of( - NodeType.NUMBER.asParameterType(), - NodeType.NUMBER.asParameterType() - ); + return NodeType.NUMBER.asParameterTypes(); } @Override public void initializeArguments(List arguments) { - number = castNumber(arguments.get(0)); - radix = castNumber(arguments.get(1)); + radix = castNumber(arguments.get(0)); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/TrimFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/TrimFunction.java index 7ebb3db..429d8eb 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/TrimFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/TrimFunction.java @@ -1,26 +1,24 @@ package eu.jasperlorelai.circe.parser.function; -import java.util.List; - import org.jetbrains.annotations.NotNull; import eu.jasperlorelai.circe.parser.function.util.*; import eu.jasperlorelai.circe.parser.expression.util.*; import eu.jasperlorelai.circe.parser.expression.StringLiteralNode; -public class TrimFunction extends Function { +public class TrimFunction extends ZeroParamFunction { private StringLiteralNode string; @NotNull @Override - public List getParameterTypes() { - return NodeType.STRING.asParameterTypes(); + public ParameterType getTargetType() { + return NodeType.STRING.asParameterType(); } @Override - public void initializeArguments(List arguments) { - string = castString(arguments.get(0)); + public void initializeTarget(ExpressionNode target) { + string = castString(target); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/UnicodeFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/UnicodeFunction.java index 141eab6..9dffb5a 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/UnicodeFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/UnicodeFunction.java @@ -1,26 +1,24 @@ package eu.jasperlorelai.circe.parser.function; -import java.util.List; - import org.jetbrains.annotations.NotNull; import eu.jasperlorelai.circe.parser.function.util.*; import eu.jasperlorelai.circe.parser.expression.util.*; import eu.jasperlorelai.circe.parser.expression.NumberLiteralNode; -public class UnicodeFunction extends Function { +public class UnicodeFunction extends ZeroParamFunction { private NumberLiteralNode code; @NotNull @Override - public List getParameterTypes() { - return NodeType.NUMBER.asParameterTypes(); + public ParameterType getTargetType() { + return NodeType.NUMBER.asParameterType(); } @Override - public void initializeArguments(List arguments) { - code = castNumber(arguments.get(0)); + public void initializeTarget(ExpressionNode target) { + code = castNumber(target); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/UnshiftFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/UnshiftFunction.java index f67d837..88f1f52 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/UnshiftFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/UnshiftFunction.java @@ -17,20 +17,27 @@ public class UnshiftFunction extends Function { private StringLiteralNode elementString; private NumberLiteralNode elementNumber; + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.ARRAY.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + array = castArray(target); + } + @NotNull @Override public List getParameterTypes() { - return List.of( - NodeType.ARRAY.asParameterType(), - ParameterType.create(NodeType.STRING, NodeType.NUMBER) - ); + return List.of(ParameterType.create(NodeType.STRING, NodeType.NUMBER)); } @Override public void initializeArguments(List arguments) { - array = castArray(arguments.get(0)); - elementString = castString(arguments.get(1)); - elementNumber = castNumber(arguments.get(1)); + elementString = castString(arguments.get(0)); + elementNumber = castNumber(arguments.get(0)); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/UppercaseFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/UppercaseFunction.java index 18f5ed6..fd7579f 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/UppercaseFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/UppercaseFunction.java @@ -1,26 +1,24 @@ package eu.jasperlorelai.circe.parser.function; -import java.util.List; - import org.jetbrains.annotations.NotNull; import eu.jasperlorelai.circe.parser.function.util.*; import eu.jasperlorelai.circe.parser.expression.util.*; import eu.jasperlorelai.circe.parser.expression.StringLiteralNode; -public class UppercaseFunction extends Function { +public class UppercaseFunction extends ZeroParamFunction { private StringLiteralNode string; @NotNull @Override - public List getParameterTypes() { - return NodeType.STRING.asParameterTypes(); + public ParameterType getTargetType() { + return NodeType.STRING.asParameterType(); } @Override - public void initializeArguments(List arguments) { - string = castString(arguments.get(0)); + public void initializeTarget(ExpressionNode target) { + string = castString(target); } @NotNull diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/util/Function.java b/src/main/java/eu/jasperlorelai/circe/parser/function/util/Function.java index ddfbeac..24a5b62 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/util/Function.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/util/Function.java @@ -1,7 +1,7 @@ package eu.jasperlorelai.circe.parser.function.util; import java.util.List; -import java.util.ArrayList; +import java.util.stream.Collectors; import org.jetbrains.annotations.NotNull; @@ -21,9 +21,14 @@ public Function() { functionName = Character.toLowerCase(name.charAt(0)) + name.substring(1, name.lastIndexOf("Function")); } + @NotNull + public abstract ParameterType getTargetType(); + @NotNull public abstract List getParameterTypes(); + public abstract void initializeTarget(ExpressionNode target); + public abstract void initializeArguments(List arguments); @NotNull @@ -66,9 +71,10 @@ protected static StringLiteralNode stringFromBool(boolean bool) { } protected static ArrayNode arrayNode(List list) { - List newList = new ArrayList<>(); - list.forEach(item -> newList.add(new StringLiteralNode(quote(item)))); - return new ArrayNode(newList); + return new ArrayNode(list.stream() + .map(item -> new StringLiteralNode(quote(item))) + .collect(Collectors.toList()) + ); } } diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/util/ZeroParamFunction.java b/src/main/java/eu/jasperlorelai/circe/parser/function/util/ZeroParamFunction.java new file mode 100644 index 0000000..1bf4596 --- /dev/null +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/util/ZeroParamFunction.java @@ -0,0 +1,20 @@ +package eu.jasperlorelai.circe.parser.function.util; + +import java.util.List; + +import org.jetbrains.annotations.NotNull; + +import eu.jasperlorelai.circe.parser.expression.util.ExpressionNode; + +public abstract class ZeroParamFunction extends Function { + + @NotNull + @Override + public final List getParameterTypes() { + return List.of(); + } + + @Override + public final void initializeArguments(List arguments) {} + +} diff --git a/src/main/java/eu/jasperlorelai/circe/tokenizer/type/SpecialTokenType.java b/src/main/java/eu/jasperlorelai/circe/tokenizer/type/SpecialTokenType.java index 30b72bf..1158033 100644 --- a/src/main/java/eu/jasperlorelai/circe/tokenizer/type/SpecialTokenType.java +++ b/src/main/java/eu/jasperlorelai/circe/tokenizer/type/SpecialTokenType.java @@ -17,6 +17,7 @@ public enum SpecialTokenType implements TokenType { DOLLAR("\\$"), EQUALS("="), COMMA(","), + DOT("\\."), COMMA_COLON(";"), WHITESPACE("[\n\r\s]+"), SINGLE_COMMENT("//.*[\n\r]"), diff --git a/src/test/java/eu/jasperlorelai/circe/CirceTests.java b/src/test/java/eu/jasperlorelai/circe/CirceTests.java index 34d1c31..4194771 100644 --- a/src/test/java/eu/jasperlorelai/circe/CirceTests.java +++ b/src/test/java/eu/jasperlorelai/circe/CirceTests.java @@ -458,5 +458,11 @@ void test66() { assertEquals("#ffffff", new Parser().parse(code)); } + @Test + void test67() { + String code = "\"Hello world!\".split(\" \", 0).elementAt(1).charAt(0)"; + assertEquals("w", new Parser().parse(code)); + } + }