diff --git a/src/main/java/eu/jasperlorelai/circe/CirceExpansion.java b/src/main/java/eu/jasperlorelai/circe/CirceExpansion.java index f96a887..0146b16 100644 --- a/src/main/java/eu/jasperlorelai/circe/CirceExpansion.java +++ b/src/main/java/eu/jasperlorelai/circe/CirceExpansion.java @@ -14,71 +14,71 @@ public class CirceExpansion extends PlaceholderExpansion { - private static final String IDENTIFIER = "circe"; - private static final String AUTHOR = "JasperLorelai"; - private static final String VERSION = "0.1.0"; - private static final String NAME = "Circe"; - - @NotNull - public String getIdentifier() { - return IDENTIFIER; - } - - @NotNull - public String getAuthor() { - return AUTHOR; - } - - @NotNull - public String getVersion() { - return VERSION; - } - - @NotNull - public String getName() { - return NAME; - } - - public String onRequest(OfflinePlayer player, @NotNull String identifier) { - String code = identifier; - if (code.startsWith("file:")) { - String fileName = code.replaceFirst("file:", "") - .replaceFirst("\\.circe$", "") - .trim(); - - // Read the file. - File folder = new File(getPlaceholderAPI().getDataFolder(), "circe"); - try { - //noinspection ResultOfMethodCallIgnored - folder.mkdir(); - File file = new File(folder, fileName + ".circe"); - BufferedReader reader = new BufferedReader(new FileReader(file)); - StringBuilder string = new StringBuilder(); - String line; - while ((line = reader.readLine()) != null) { - string.append(line).append(System.lineSeparator()); - } - code = string.toString(); - reader.close(); - } catch (SecurityException | IOException e) { - severe("Failed read file: circe/" + fileName + ".circe"); - } - } - - code = bracketPlaceholders(player, code); - boolean hasDebug = code.startsWith("debug;"); - if (hasDebug) code = code.replaceFirst("debug;", ""); - - try { - code = new Parser().parse(code); - } catch (UnknownTokenException | ParserException | FunctionCallException exception) { - if (hasDebug) return exception.getClass().getSimpleName() + ": " + exception.getMessage(); - } - return bracketPlaceholders(player, code); - } - - private static String bracketPlaceholders(OfflinePlayer player, String string) { - return PlaceholderAPI.setBracketPlaceholders(player, string); - } + private static final String IDENTIFIER = "circe"; + private static final String AUTHOR = "JasperLorelai"; + private static final String VERSION = "0.1.0"; + private static final String NAME = "Circe"; + + @NotNull + public String getIdentifier() { + return IDENTIFIER; + } + + @NotNull + public String getAuthor() { + return AUTHOR; + } + + @NotNull + public String getVersion() { + return VERSION; + } + + @NotNull + public String getName() { + return NAME; + } + + public String onRequest(OfflinePlayer player, @NotNull String identifier) { + String code = identifier; + if (code.startsWith("file:")) { + String fileName = code.replaceFirst("file:", "") + .replaceFirst("\\.circe$", "") + .trim(); + + // Read the file. + File folder = new File(getPlaceholderAPI().getDataFolder(), "circe"); + try { + //noinspection ResultOfMethodCallIgnored + folder.mkdir(); + File file = new File(folder, fileName + ".circe"); + BufferedReader reader = new BufferedReader(new FileReader(file)); + StringBuilder string = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + string.append(line).append(System.lineSeparator()); + } + code = string.toString(); + reader.close(); + } catch (SecurityException | IOException e) { + severe("Failed read file: circe/" + fileName + ".circe"); + } + } + + code = bracketPlaceholders(player, code); + boolean hasDebug = code.startsWith("debug;"); + if (hasDebug) code = code.replaceFirst("debug;", ""); + + try { + code = new Parser().parse(code); + } catch (UnknownTokenException | ParserException | FunctionCallException exception) { + if (hasDebug) return exception.getClass().getSimpleName() + ": " + exception.getMessage(); + } + return bracketPlaceholders(player, code); + } + + private static String bracketPlaceholders(OfflinePlayer player, String string) { + return PlaceholderAPI.setBracketPlaceholders(player, string); + } } diff --git a/src/main/java/eu/jasperlorelai/circe/exeption/FunctionCallException.java b/src/main/java/eu/jasperlorelai/circe/exeption/FunctionCallException.java index c70b57c..9b8dc5e 100644 --- a/src/main/java/eu/jasperlorelai/circe/exeption/FunctionCallException.java +++ b/src/main/java/eu/jasperlorelai/circe/exeption/FunctionCallException.java @@ -2,8 +2,8 @@ public class FunctionCallException extends RuntimeException { - public FunctionCallException(String message) { - super(message); - } + public FunctionCallException(String message) { + super(message); + } } diff --git a/src/main/java/eu/jasperlorelai/circe/exeption/ParserException.java b/src/main/java/eu/jasperlorelai/circe/exeption/ParserException.java index 821b20c..afca534 100644 --- a/src/main/java/eu/jasperlorelai/circe/exeption/ParserException.java +++ b/src/main/java/eu/jasperlorelai/circe/exeption/ParserException.java @@ -2,8 +2,8 @@ public class ParserException extends RuntimeException { - public ParserException(String message) { - super(message); - } + public ParserException(String message) { + super(message); + } } diff --git a/src/main/java/eu/jasperlorelai/circe/exeption/UnknownTokenException.java b/src/main/java/eu/jasperlorelai/circe/exeption/UnknownTokenException.java index 31b5c4f..88e8e2f 100644 --- a/src/main/java/eu/jasperlorelai/circe/exeption/UnknownTokenException.java +++ b/src/main/java/eu/jasperlorelai/circe/exeption/UnknownTokenException.java @@ -2,8 +2,8 @@ public class UnknownTokenException extends RuntimeException { - public UnknownTokenException(String message) { - super(message); - } + public UnknownTokenException(String message) { + super(message); + } } diff --git a/src/main/java/eu/jasperlorelai/circe/parser/Parser.java b/src/main/java/eu/jasperlorelai/circe/parser/Parser.java index 41c082c..0d19ca6 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/Parser.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/Parser.java @@ -17,190 +17,190 @@ public class Parser { - private Tokenizer tokenizer; - private Token lookahead; - - private Token nextToken() { - return (lookahead = tokenizer.getNextToken()); - } - - public String parse(String string) { - tokenizer = Tokenizer.create(); - tokenizer.tokenize(string); - // First token - if (nextToken() == null) return ""; - - Variables.getVariables().clear(); - ExpressionNode expression = expression(); - if (lookahead != null && lookahead.tokenType() != SpecialTokenType.COMMA_COLON) throw new ParserException("Expected end of input but found " + lookahead.tokenType() + ": '" + lookahead.value() + "'"); - - if (expression == null) return ""; - - /* - * If we want the string node to be quoteless, we have to execute the function - * expression manually, otherwise we'll get the right value() but of function node type. - */ - if (expression instanceof FunctionNode functionNode) { - expression = functionNode.getFunction().execute(); - } - if (expression instanceof StringLiteralNode stringNode) return stringNode.quoteless(); - return expression.value(); - } - - private ExpressionNode expression() { - if (lookahead == null) return null; - 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() { - if (lookahead == null) return null; - TokenType tokenType = lookahead.tokenType(); - return switch ((SpecialTokenType) tokenType) { - case DOLLAR -> variable(); - case BRACKET_OPEN -> array(); - default -> throw new ParserException("Expected expression, instead received: " + tokenType + " '" + lookahead.value() + "'"); - }; - } - - private ExpressionNode function(@Nullable ExpressionNode target) { - if (lookahead == null) return null; - String functionName = lookahead.value(); - FunctionNode node = new FunctionNode(lookahead); - nextToken(); - // Open bracket. - if (lookahead != null && lookahead.tokenType() != SpecialTokenType.PARENTHESES_OPEN) { - throw new ParserException("Function '" + functionName + "' does not start with '('"); - } - nextToken(); - - // Collect function arguments. - boolean closed = false; - boolean awaitingArg = true; - List arguments = new ArrayList<>(); - while (lookahead != null) { - // Check for closed bracket. - if (lookahead.tokenType() == SpecialTokenType.PARENTHESES_CLOSE) { - if (arguments.isEmpty()) awaitingArg = false; - closed = true; - nextToken(); - break; - } - - // Check for argument of "expression" type. - if (awaitingArg) { - arguments.add(expression()); - awaitingArg = false; - continue; - } - // Check for comma instead. - if (lookahead.tokenType() == SpecialTokenType.COMMA) awaitingArg = true; - else throw new ParserException("Unknown symbol " + lookahead.tokenType() + " in function '" + functionName + "': " + lookahead.value()); - nextToken(); - } - - if (awaitingArg) throw new ParserException("Expected argument after ',' in function '" + functionName + "'."); - if (!closed) throw new ParserException("Function '" + functionName + "' does not end with ')'"); - - 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; - } - - private ExpressionNode literal() { - if (lookahead == null) return null; - String value = lookahead.value(); - ExpressionNode node = switch ((LiteralTokenType) lookahead.tokenType()) { - case NUMBER -> new NumberLiteralNode(value); - case STRING -> new StringLiteralNode(value); - }; - nextToken(); - return node; - } - - // Array follows JSON style. - private ExpressionNode array() { - List list = new ArrayList<>(); - // Open bracket already processed. - nextToken(); - - // Collect elements in array. - boolean closed = false; - boolean awaitingItem = true; - while (lookahead != null) { - if (lookahead.tokenType() == SpecialTokenType.BRACKET_CLOSE) { - if (list.isEmpty()) awaitingItem = false; - closed = true; - nextToken(); - break; - } - - // Check for item of "expression" type. - if (awaitingItem) { - list.add(expression()); - awaitingItem = false; - continue; - } - // Check for comma instead. - if (lookahead.tokenType() == SpecialTokenType.COMMA) awaitingItem = true; - else throw new ParserException("Unknown symbol " + lookahead.tokenType() + " + in array: " + lookahead.value()); - nextToken(); - } - - if (awaitingItem) throw new ParserException("Expected element after ',' in array."); - if (!closed) throw new ParserException("Array does not end with ']'"); - return new ArrayNode(list); - } - - private ExpressionNode variable() { - // Collect the variable name. - nextToken(); - if (lookahead == null || lookahead.tokenType() != SpecialTokenType.VARIABLE_NAME) throw new ParserException("Invalid variable reference/declaration."); - String variableName = lookahead.value(); - nextToken(); - - // Check for equals after var name. If not present, this is a variable reference. - if (lookahead == null || lookahead.tokenType() != SpecialTokenType.EQUALS) { - ExpressionNode node = Variables.getVariables().get(variableName); - if (node == null) throw new ParserException("Invalid variable reference: $" + variableName); - return node; - } - // Since the equals was present, this is variable declaration. - if (Variables.getVariables().containsKey(variableName)) { - throw new ParserException("Variable '" + variableName + "' has already been declared."); - } - - // Collect expression to store. - nextToken(); - ExpressionNode node = expression(); - if (node == null || lookahead == null || lookahead.tokenType() != SpecialTokenType.COMMA_COLON) { - throw new ParserException("Invalid variable declaration: " + variableName); - } - node = node instanceof FunctionNode fun ? fun.getFunction().execute() : node; - - // Store. - Variables.getVariables().put(variableName, node); - nextToken(); - return expression(); - } + private Tokenizer tokenizer; + private Token lookahead; + + private Token nextToken() { + return (lookahead = tokenizer.getNextToken()); + } + + public String parse(String string) { + tokenizer = Tokenizer.create(); + tokenizer.tokenize(string); + // First token + if (nextToken() == null) return ""; + + Variables.getVariables().clear(); + ExpressionNode expression = expression(); + if (lookahead != null && lookahead.tokenType() != SpecialTokenType.COMMA_COLON) throw new ParserException("Expected end of input but found " + lookahead.tokenType() + ": '" + lookahead.value() + "'"); + + if (expression == null) return ""; + + /* + * If we want the string node to be quoteless, we have to execute the function + * expression manually, otherwise we'll get the right value() but of function node type. + */ + if (expression instanceof FunctionNode functionNode) { + expression = functionNode.getFunction().execute(); + } + if (expression instanceof StringLiteralNode stringNode) return stringNode.quoteless(); + return expression.value(); + } + + private ExpressionNode expression() { + if (lookahead == null) return null; + 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() { + if (lookahead == null) return null; + TokenType tokenType = lookahead.tokenType(); + return switch ((SpecialTokenType) tokenType) { + case DOLLAR -> variable(); + case BRACKET_OPEN -> array(); + default -> throw new ParserException("Expected expression, instead received: " + tokenType + " '" + lookahead.value() + "'"); + }; + } + + private ExpressionNode function(@Nullable ExpressionNode target) { + if (lookahead == null) return null; + String functionName = lookahead.value(); + FunctionNode node = new FunctionNode(lookahead); + nextToken(); + // Open bracket. + if (lookahead != null && lookahead.tokenType() != SpecialTokenType.PARENTHESES_OPEN) { + throw new ParserException("Function '" + functionName + "' does not start with '('"); + } + nextToken(); + + // Collect function arguments. + boolean closed = false; + boolean awaitingArg = true; + List arguments = new ArrayList<>(); + while (lookahead != null) { + // Check for closed bracket. + if (lookahead.tokenType() == SpecialTokenType.PARENTHESES_CLOSE) { + if (arguments.isEmpty()) awaitingArg = false; + closed = true; + nextToken(); + break; + } + + // Check for argument of "expression" type. + if (awaitingArg) { + arguments.add(expression()); + awaitingArg = false; + continue; + } + // Check for comma instead. + if (lookahead.tokenType() == SpecialTokenType.COMMA) awaitingArg = true; + else throw new ParserException("Unknown symbol " + lookahead.tokenType() + " in function '" + functionName + "': " + lookahead.value()); + nextToken(); + } + + if (awaitingArg) throw new ParserException("Expected argument after ',' in function '" + functionName + "'."); + if (!closed) throw new ParserException("Function '" + functionName + "' does not end with ')'"); + + 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; + } + + private ExpressionNode literal() { + if (lookahead == null) return null; + String value = lookahead.value(); + ExpressionNode node = switch ((LiteralTokenType) lookahead.tokenType()) { + case NUMBER -> new NumberLiteralNode(value); + case STRING -> new StringLiteralNode(value); + }; + nextToken(); + return node; + } + + // Array follows JSON style. + private ExpressionNode array() { + List list = new ArrayList<>(); + // Open bracket already processed. + nextToken(); + + // Collect elements in array. + boolean closed = false; + boolean awaitingItem = true; + while (lookahead != null) { + if (lookahead.tokenType() == SpecialTokenType.BRACKET_CLOSE) { + if (list.isEmpty()) awaitingItem = false; + closed = true; + nextToken(); + break; + } + + // Check for item of "expression" type. + if (awaitingItem) { + list.add(expression()); + awaitingItem = false; + continue; + } + // Check for comma instead. + if (lookahead.tokenType() == SpecialTokenType.COMMA) awaitingItem = true; + else throw new ParserException("Unknown symbol " + lookahead.tokenType() + " + in array: " + lookahead.value()); + nextToken(); + } + + if (awaitingItem) throw new ParserException("Expected element after ',' in array."); + if (!closed) throw new ParserException("Array does not end with ']'"); + return new ArrayNode(list); + } + + private ExpressionNode variable() { + // Collect the variable name. + nextToken(); + if (lookahead == null || lookahead.tokenType() != SpecialTokenType.VARIABLE_NAME) throw new ParserException("Invalid variable reference/declaration."); + String variableName = lookahead.value(); + nextToken(); + + // Check for equals after var name. If not present, this is a variable reference. + if (lookahead == null || lookahead.tokenType() != SpecialTokenType.EQUALS) { + ExpressionNode node = Variables.getVariables().get(variableName); + if (node == null) throw new ParserException("Invalid variable reference: $" + variableName); + return node; + } + // Since the equals was present, this is variable declaration. + if (Variables.getVariables().containsKey(variableName)) { + throw new ParserException("Variable '" + variableName + "' has already been declared."); + } + + // Collect expression to store. + nextToken(); + ExpressionNode node = expression(); + if (node == null || lookahead == null || lookahead.tokenType() != SpecialTokenType.COMMA_COLON) { + throw new ParserException("Invalid variable declaration: " + variableName); + } + node = node instanceof FunctionNode fun ? fun.getFunction().execute() : node; + + // Store. + Variables.getVariables().put(variableName, node); + nextToken(); + return expression(); + } } diff --git a/src/main/java/eu/jasperlorelai/circe/parser/expression/ArrayNode.java b/src/main/java/eu/jasperlorelai/circe/parser/expression/ArrayNode.java index 528eb4a..e16a406 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/expression/ArrayNode.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/expression/ArrayNode.java @@ -10,29 +10,29 @@ public record ArrayNode(List array) implements ExpressionNode { - public ArrayNode(List array) { - List temp = new ArrayList<>(); - array.forEach(item -> { - if (item instanceof FunctionNode fun) temp.add(fun.getFunction().execute()); - else temp.add(item); - }); - this.array = temp; - } - - @NotNull - @Override - public NodeType getType() { - return NodeType.ARRAY; - } - - public List stringArray() { - return array.stream().map(ExpressionNode::value).toList(); - } - - @NotNull - @Override - public String value() { - return "[" + String.join(", ", stringArray()) + "]"; - } + public ArrayNode(List array) { + List temp = new ArrayList<>(); + array.forEach(item -> { + if (item instanceof FunctionNode fun) temp.add(fun.getFunction().execute()); + else temp.add(item); + }); + this.array = temp; + } + + @NotNull + @Override + public NodeType getType() { + return NodeType.ARRAY; + } + + public List stringArray() { + return array.stream().map(ExpressionNode::value).toList(); + } + + @NotNull + @Override + public String value() { + return "[" + String.join(", ", stringArray()) + "]"; + } } 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 ebdb879..860a2a4 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/expression/FunctionNode.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/expression/FunctionNode.java @@ -13,57 +13,57 @@ public class FunctionNode implements ExpressionNode { - private final String functionName; - private final Function function; - - - public FunctionNode(Token token) { - FunctionTokenType type = (FunctionTokenType) token.tokenType(); - functionName = type.functionName(); - function = type.createFunction(); - } - - 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()); - else localArgs.add(node); - } - - // Validate that argument type matches parameter type. - 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(); - if (parameter.isValid(argument)) continue; - throw new ParserException("Function '" + functionName + "' has argument of " + argument + " type defined at position " + (i + 1) + " when " + parameter + " was expected."); - } - - function.initializeArguments(localArgs); - } - - @NotNull - @Override - public NodeType getType() { - return NodeType.FUNCTION; - } - - public Function getFunction() { - return function; - } - - @NotNull - @Override - public String value() { - return getFunction().execute().value(); - } + private final String functionName; + private final Function function; + + + public FunctionNode(Token token) { + FunctionTokenType type = (FunctionTokenType) token.tokenType(); + functionName = type.functionName(); + function = type.createFunction(); + } + + 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()); + else localArgs.add(node); + } + + // Validate that argument type matches parameter type. + 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(); + if (parameter.isValid(argument)) continue; + throw new ParserException("Function '" + functionName + "' has argument of " + argument + " type defined at position " + (i + 1) + " when " + parameter + " was expected."); + } + + function.initializeArguments(localArgs); + } + + @NotNull + @Override + public NodeType getType() { + return NodeType.FUNCTION; + } + + public Function getFunction() { + return function; + } + + @NotNull + @Override + public String value() { + return getFunction().execute().value(); + } } diff --git a/src/main/java/eu/jasperlorelai/circe/parser/expression/NumberLiteralNode.java b/src/main/java/eu/jasperlorelai/circe/parser/expression/NumberLiteralNode.java index 10ec427..81b9751 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/expression/NumberLiteralNode.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/expression/NumberLiteralNode.java @@ -6,33 +6,33 @@ public class NumberLiteralNode implements ExpressionNode { - private final String valueString; - private final double valueDouble; - private final int valueInteger; - - public NumberLiteralNode(String value) { - valueString = value; - valueDouble = Double.parseDouble(value); - valueInteger = (int) Math.round(valueDouble); - } - - @NotNull - @Override - public NodeType getType() { - return NodeType.NUMBER; - } - - @NotNull - public String value() { - return valueString; - } - - public double valueDouble() { - return valueDouble; - } - - public int valueInteger() { - return valueInteger; - } + private final String valueString; + private final double valueDouble; + private final int valueInteger; + + public NumberLiteralNode(String value) { + valueString = value; + valueDouble = Double.parseDouble(value); + valueInteger = (int) Math.round(valueDouble); + } + + @NotNull + @Override + public NodeType getType() { + return NodeType.NUMBER; + } + + @NotNull + public String value() { + return valueString; + } + + public double valueDouble() { + return valueDouble; + } + + public int valueInteger() { + return valueInteger; + } } diff --git a/src/main/java/eu/jasperlorelai/circe/parser/expression/StringLiteralNode.java b/src/main/java/eu/jasperlorelai/circe/parser/expression/StringLiteralNode.java index 57ca24b..4fb7964 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/expression/StringLiteralNode.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/expression/StringLiteralNode.java @@ -6,20 +6,20 @@ public record StringLiteralNode(String value) implements ExpressionNode { - @NotNull - @Override - public NodeType getType() { - return NodeType.STRING; - } + @NotNull + @Override + public NodeType getType() { + return NodeType.STRING; + } - public String quoteless() { - return value.substring(1, value.length() - 1).replaceAll("\\\\\"", "\""); - } + public String quoteless() { + return value.substring(1, value.length() - 1).replaceAll("\\\\\"", "\""); + } - @Override - @NotNull - public String value() { - return value; - } + @Override + @NotNull + public String value() { + return value; + } } diff --git a/src/main/java/eu/jasperlorelai/circe/parser/expression/util/ExpressionNode.java b/src/main/java/eu/jasperlorelai/circe/parser/expression/util/ExpressionNode.java index 35fac30..1a133f0 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/expression/util/ExpressionNode.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/expression/util/ExpressionNode.java @@ -4,10 +4,10 @@ public interface ExpressionNode { - @NotNull - NodeType getType(); + @NotNull + NodeType getType(); - @NotNull - String value(); + @NotNull + String value(); } diff --git a/src/main/java/eu/jasperlorelai/circe/parser/expression/util/NodeType.java b/src/main/java/eu/jasperlorelai/circe/parser/expression/util/NodeType.java index 0b080e9..d2d9bf3 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/expression/util/NodeType.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/expression/util/NodeType.java @@ -5,18 +5,20 @@ import eu.jasperlorelai.circe.parser.function.util.ParameterType; public enum NodeType { - STRING, - NUMBER, - ARRAY, - FUNCTION - ; - - public ParameterType asParameterType() { - return ParameterType.create(this); - } - - public List asParameterTypes() { - return List.of(asParameterType()); - } + + STRING, + NUMBER, + ARRAY, + FUNCTION + + ; + + public ParameterType asParameterType() { + return ParameterType.create(this); + } + + public List asParameterTypes() { + return List.of(asParameterType()); + } } 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 19bba75..856487a 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/CalcFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/CalcFunction.java @@ -14,38 +14,38 @@ public class CalcFunction extends ZeroParamFunction { - private StringLiteralNode string; - - @NotNull - @Override - public ParameterType getTargetType() { - return NodeType.STRING.asParameterType(); - } - - @Override - public void initializeTarget(ExpressionNode target) { - string = castString(target); - } - - @NotNull - @Override - public ExpressionNode execute() { - try { - Map variableMap = new HashMap<>(); - Variables.getVariables().forEach((name, value) -> { - if (!(value instanceof NumberLiteralNode number)) return; - variableMap.put(name, number.valueDouble()); - }); - - return new NumberLiteralNode(new ExpressionBuilder(string.quoteless()) - .variables(variableMap.keySet()) - .build() - .setVariables(variableMap) - .evaluate() + "" - ); - } catch (IllegalArgumentException | FunctionCallException e) { - throw formatException(e); - } - } + private StringLiteralNode string; + + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.STRING.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + string = castString(target); + } + + @NotNull + @Override + public ExpressionNode execute() { + try { + Map variableMap = new HashMap<>(); + Variables.getVariables().forEach((name, value) -> { + if (!(value instanceof NumberLiteralNode number)) return; + variableMap.put(name, number.valueDouble()); + }); + + return new NumberLiteralNode(new ExpressionBuilder(string.quoteless()) + .variables(variableMap.keySet()) + .build() + .setVariables(variableMap) + .evaluate() + "" + ); + } catch (IllegalArgumentException | FunctionCallException e) { + throw formatException(e); + } + } } 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 adf2948..9e57c18 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/CharAtFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/CharAtFunction.java @@ -11,39 +11,39 @@ 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 NodeType.NUMBER.asParameterTypes(); - } - - @Override - public void initializeArguments(List arguments) { - index = castNumber(arguments.get(0)); - } - - @NotNull - @Override - public ExpressionNode execute() { - try { - return stringNode(string.quoteless().charAt(index.valueInteger()) + ""); - } catch (IndexOutOfBoundsException e) { - throw formatException(e); - } - } + 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 NodeType.NUMBER.asParameterTypes(); + } + + @Override + public void initializeArguments(List arguments) { + index = castNumber(arguments.get(0)); + } + + @NotNull + @Override + public ExpressionNode execute() { + try { + return stringNode(string.quoteless().charAt(index.valueInteger()) + ""); + } catch (IndexOutOfBoundsException e) { + throw formatException(e); + } + } } 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 32aeb60..38af37c 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/CharCodeAtFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/CharCodeAtFunction.java @@ -11,39 +11,39 @@ 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 NodeType.NUMBER.asParameterTypes(); - } - - @Override - public void initializeArguments(List arguments) { - index = castNumber(arguments.get(0)); - } - - @NotNull - @Override - public ExpressionNode execute() { - try { - return numberNode(string.quoteless().charAt(index.valueInteger())); - } catch (IndexOutOfBoundsException e) { - throw formatException(e); - } - } + 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 NodeType.NUMBER.asParameterTypes(); + } + + @Override + public void initializeArguments(List arguments) { + index = castNumber(arguments.get(0)); + } + + @NotNull + @Override + public ExpressionNode execute() { + try { + return numberNode(string.quoteless().charAt(index.valueInteger())); + } catch (IndexOutOfBoundsException e) { + throw formatException(e); + } + } } 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 3fe86c3..9314e2d 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/ContainsFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/ContainsFunction.java @@ -12,41 +12,41 @@ public class ContainsFunction extends Function { - private ArrayNode array; - private StringLiteralNode string; - 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.NUMBER)); - } - - @Override - public void initializeArguments(List arguments) { - elementString = castString(arguments.get(0)); - elementNumber = castNumber(arguments.get(0)); - } - - @NotNull - @Override - public ExpressionNode execute() { - ExpressionNode node = elementString == null ? elementNumber : elementString; - if (array == null) return stringFromBool(string.quoteless().contains(valueQuoteless(node))); - else return stringFromBool(array.stringArray().contains(node.value())); - } + private ArrayNode array; + private StringLiteralNode string; + 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.NUMBER)); + } + + @Override + public void initializeArguments(List arguments) { + elementString = castString(arguments.get(0)); + elementNumber = castNumber(arguments.get(0)); + } + + @NotNull + @Override + public ExpressionNode execute() { + ExpressionNode node = elementString == null ? elementNumber : elementString; + if (array == null) return stringFromBool(string.quoteless().contains(valueQuoteless(node))); + else return stringFromBool(array.stringArray().contains(node.value())); + } } 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 8c28422..c1ceb22 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/ElementAtFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/ElementAtFunction.java @@ -11,39 +11,39 @@ 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 NodeType.NUMBER.asParameterTypes(); - } - - @Override - public void initializeArguments(List arguments) { - index = castNumber(arguments.get(0)); - } - - @NotNull - @Override - public ExpressionNode execute() { - try { - return array.array().get(index.valueInteger()); - } catch (IndexOutOfBoundsException e) { - throw formatException(e); - } - } + 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 NodeType.NUMBER.asParameterTypes(); + } + + @Override + public void initializeArguments(List arguments) { + index = castNumber(arguments.get(0)); + } + + @NotNull + @Override + public ExpressionNode execute() { + try { + return array.array().get(index.valueInteger()); + } catch (IndexOutOfBoundsException e) { + throw formatException(e); + } + } } 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 f92701c..1f27c50 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/EndsWithFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/EndsWithFunction.java @@ -10,35 +10,35 @@ 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 NodeType.STRING.asParameterTypes(); - } - - @Override - public void initializeArguments(List arguments) { - substring = castString(arguments.get(0)); - } - - @NotNull - @Override - public ExpressionNode execute() { - return stringFromBool(string.quoteless().endsWith(substring.quoteless())); - } + 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 NodeType.STRING.asParameterTypes(); + } + + @Override + public void initializeArguments(List arguments) { + substring = castString(arguments.get(0)); + } + + @NotNull + @Override + public ExpressionNode execute() { + return stringFromBool(string.quoteless().endsWith(substring.quoteless())); + } } 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 1a0f684..19d79b5 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/EqualsFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/EqualsFunction.java @@ -11,45 +11,45 @@ public class EqualsFunction extends Function { - private StringLiteralNode stringFirst; - private StringLiteralNode stringSecond; - 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() { - return List.of(ParameterType.create(NodeType.STRING, NodeType.ARRAY)); - } - - @Override - public void initializeArguments(List arguments) { - stringSecond = castString(arguments.get(0)); - arraySecond = castArray(arguments.get(0)); - } - - @NotNull - @Override - public ExpressionNode execute() { - if (arrayFirst != null && arraySecond != null) { - return stringFromBool(arrayFirst.value().equals(arraySecond.value())); - } - if (stringFirst != null && stringSecond != null) { - return stringFromBool(stringFirst.quoteless().equals(stringSecond.quoteless())); - } - return stringFromBool(false); - } + private StringLiteralNode stringFirst; + private StringLiteralNode stringSecond; + 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() { + return List.of(ParameterType.create(NodeType.STRING, NodeType.ARRAY)); + } + + @Override + public void initializeArguments(List arguments) { + stringSecond = castString(arguments.get(0)); + arraySecond = castArray(arguments.get(0)); + } + + @NotNull + @Override + public ExpressionNode execute() { + if (arrayFirst != null && arraySecond != null) { + return stringFromBool(arrayFirst.value().equals(arraySecond.value())); + } + if (stringFirst != null && stringSecond != null) { + return stringFromBool(stringFirst.quoteless().equals(stringSecond.quoteless())); + } + return stringFromBool(false); + } } 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 5f6eaea..d62483b 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/IndexOfFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/IndexOfFunction.java @@ -12,41 +12,41 @@ public class IndexOfFunction extends Function { - private StringLiteralNode string; - private ArrayNode array; - 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.NUMBER)); - } - - @Override - public void initializeArguments(List arguments) { - elementString = castString(arguments.get(0)); - elementNumber = castNumber(arguments.get(0)); - } - - @NotNull - @Override - public ExpressionNode execute() { - ExpressionNode node = elementString == null ? elementNumber : elementString; - if (string == null) return numberNode(array.stringArray().indexOf(node.value())); - else return numberNode(string.quoteless().indexOf(valueQuoteless(node))); - } + private StringLiteralNode string; + private ArrayNode array; + 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.NUMBER)); + } + + @Override + public void initializeArguments(List arguments) { + elementString = castString(arguments.get(0)); + elementNumber = castNumber(arguments.get(0)); + } + + @NotNull + @Override + public ExpressionNode execute() { + ExpressionNode node = elementString == null ? elementNumber : elementString; + if (string == null) return numberNode(array.stringArray().indexOf(node.value())); + else return numberNode(string.quoteless().indexOf(valueQuoteless(node))); + } } 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 780f0a1..f11c1ff 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/IsEmptyFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/IsEmptyFunction.java @@ -9,25 +9,25 @@ public class IsEmptyFunction extends ZeroParamFunction { - private StringLiteralNode string; - private ArrayNode array; - - @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 ExpressionNode execute() { - return stringFromBool(string == null ? array.array().isEmpty() : string.quoteless().isEmpty()); - } + private StringLiteralNode string; + private ArrayNode array; + + @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 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 4df839b..c0496db 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/JoinFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/JoinFunction.java @@ -12,39 +12,39 @@ 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 NodeType.STRING.asParameterTypes(); - } - - @Override - public void initializeArguments(List arguments) { - delimiter = castString(arguments.get(0)); - } - - @NotNull - @Override - public ExpressionNode execute() { - return stringNode(array.array() - .stream() - .map(Function::valueQuoteless) - .collect(Collectors.joining(delimiter.quoteless())) - ); - } + 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 NodeType.STRING.asParameterTypes(); + } + + @Override + public void initializeArguments(List arguments) { + delimiter = castString(arguments.get(0)); + } + + @NotNull + @Override + public ExpressionNode execute() { + return stringNode(array.array() + .stream() + .map(Function::valueQuoteless) + .collect(Collectors.joining(delimiter.quoteless())) + ); + } } 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 83d8205..d9badb1 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/LastIndexOfFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/LastIndexOfFunction.java @@ -12,41 +12,41 @@ public class LastIndexOfFunction extends Function { - private StringLiteralNode string; - private ArrayNode array; - 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.NUMBER)); - } - - @Override - public void initializeArguments(List arguments) { - elementString = castString(arguments.get(0)); - elementNumber = castNumber(arguments.get(0)); - } - - @NotNull - @Override - public ExpressionNode execute() { - ExpressionNode node = elementString == null ? elementNumber : elementString; - if (string == null) return numberNode(array.stringArray().lastIndexOf(node.value())); - else return numberNode(string.quoteless().lastIndexOf(valueQuoteless(node))); - } + private StringLiteralNode string; + private ArrayNode array; + 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.NUMBER)); + } + + @Override + public void initializeArguments(List arguments) { + elementString = castString(arguments.get(0)); + elementNumber = castNumber(arguments.get(0)); + } + + @NotNull + @Override + public ExpressionNode execute() { + ExpressionNode node = elementString == null ? elementNumber : elementString; + if (string == null) return numberNode(array.stringArray().lastIndexOf(node.value())); + else return numberNode(string.quoteless().lastIndexOf(valueQuoteless(node))); + } } 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 c8220a1..578e978 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/LowercaseFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/LowercaseFunction.java @@ -8,23 +8,23 @@ public class LowercaseFunction extends ZeroParamFunction { - private StringLiteralNode string; - - @NotNull - @Override - public ParameterType getTargetType() { - return NodeType.STRING.asParameterType(); - } - - @Override - public void initializeTarget(ExpressionNode target) { - string = castString(target); - } - - @NotNull - @Override - public ExpressionNode execute() { - return stringNode(string.quoteless().toLowerCase()); - } + private StringLiteralNode string; + + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.STRING.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + string = castString(target); + } + + @NotNull + @Override + public ExpressionNode execute() { + return stringNode(string.quoteless().toLowerCase()); + } } 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 7aae10e..d87b856 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/MatchFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/MatchFunction.java @@ -14,46 +14,46 @@ 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 NodeType.STRING.asParameterTypes(); - } - - @Override - public void initializeArguments(List arguments) { - pattern = castString(arguments.get(0)); - } - - @NotNull - @Override - public ExpressionNode execute() { - try { - List list = new ArrayList<>(); - Matcher matcher = Pattern.compile(pattern.quoteless()).matcher(string.quoteless()); - while (matcher.find()) { - for (int j = 0; j <= matcher.groupCount(); j++) { - list.add(matcher.group(j)); - } - } - return arrayNode(list); - } catch (PatternSyntaxException e) { - throw formatException(e); - } - } + 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 NodeType.STRING.asParameterTypes(); + } + + @Override + public void initializeArguments(List arguments) { + pattern = castString(arguments.get(0)); + } + + @NotNull + @Override + public ExpressionNode execute() { + try { + List list = new ArrayList<>(); + Matcher matcher = Pattern.compile(pattern.quoteless()).matcher(string.quoteless()); + while (matcher.find()) { + for (int j = 0; j <= matcher.groupCount(); j++) { + list.add(matcher.group(j)); + } + } + return arrayNode(list); + } catch (PatternSyntaxException e) { + throw formatException(e); + } + } } 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 ea623d6..42100ae 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/PadEndFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/PadEndFunction.java @@ -13,42 +13,42 @@ public class PadEndFunction extends Function { - private StringLiteralNode string; - private NumberLiteralNode minLength; - private StringLiteralNode padChar; - - @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.NUMBER.asParameterType(), - NodeType.STRING.asParameterType() - ); - } - - @Override - public void initializeArguments(List arguments) { - minLength = castNumber(arguments.get(0)); - padChar = castString(arguments.get(1)); - } - - @NotNull - @Override - public ExpressionNode execute() { - StringBuilder newString = new StringBuilder(string.quoteless()); - while (minLength.valueInteger() > newString.length()) newString.append(padChar.quoteless()); - return stringNode(newString.toString()); - } + private StringLiteralNode string; + private NumberLiteralNode minLength; + private StringLiteralNode padChar; + + @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.NUMBER.asParameterType(), + NodeType.STRING.asParameterType() + ); + } + + @Override + public void initializeArguments(List arguments) { + minLength = castNumber(arguments.get(0)); + padChar = castString(arguments.get(1)); + } + + @NotNull + @Override + 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 d54380e..247a159 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/PadStartFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/PadStartFunction.java @@ -13,45 +13,45 @@ public class PadStartFunction extends Function { - private StringLiteralNode string; - private NumberLiteralNode minLength; - private StringLiteralNode padChar; - - @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.NUMBER.asParameterType(), - NodeType.STRING.asParameterType() - ); - } - - @Override - public void initializeArguments(List arguments) { - minLength = castNumber(arguments.get(0)); - padChar = castString(arguments.get(1)); - } - - @NotNull - @Override - public ExpressionNode execute() { - StringBuilder newString = new StringBuilder(); - String quoteless = string.quoteless(); - int length = quoteless.length(); - while (minLength.valueInteger() > newString.length() + length) newString.append(padChar.quoteless()); - newString.append(quoteless); - return stringNode(newString.toString()); - } + private StringLiteralNode string; + private NumberLiteralNode minLength; + private StringLiteralNode padChar; + + @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.NUMBER.asParameterType(), + NodeType.STRING.asParameterType() + ); + } + + @Override + public void initializeArguments(List arguments) { + minLength = castNumber(arguments.get(0)); + padChar = castString(arguments.get(1)); + } + + @NotNull + @Override + public ExpressionNode execute() { + StringBuilder newString = new StringBuilder(); + String quoteless = string.quoteless(); + int length = quoteless.length(); + while (minLength.valueInteger() > newString.length() + length) newString.append(padChar.quoteless()); + newString.append(quoteless); + return stringNode(newString.toString()); + } } 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 ec2b341..41e12cd 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/PopFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/PopFunction.java @@ -10,28 +10,28 @@ public class PopFunction extends ZeroParamFunction { - private ArrayNode array; - - @NotNull - @Override - public ParameterType getTargetType() { - return NodeType.ARRAY.asParameterType(); - } - - @Override - public void initializeTarget(ExpressionNode target) { - array = castArray(target); - } - - @NotNull - @Override - public ExpressionNode execute() { - List list = array.array(); - try { - return list.get(list.size() - 1); - } catch (IndexOutOfBoundsException e) { - throw formatException(e); - } - } + private ArrayNode array; + + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.ARRAY.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + array = castArray(target); + } + + @NotNull + @Override + public ExpressionNode execute() { + List list = array.array(); + try { + return list.get(list.size() - 1); + } catch (IndexOutOfBoundsException e) { + throw formatException(e); + } + } } 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 d767822..82fd3fe 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/PushFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/PushFunction.java @@ -10,38 +10,38 @@ public class PushFunction extends Function { - private ArrayNode array; - 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(ParameterType.create(NodeType.STRING, NodeType.NUMBER)); - } - - @Override - public void initializeArguments(List arguments) { - elementString = castString(arguments.get(0)); - elementNumber = castNumber(arguments.get(0)); - } - - @NotNull - @Override - public ExpressionNode execute() { - array.array().add(elementString == null ? elementNumber : elementString); - return array; - } + private ArrayNode array; + 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(ParameterType.create(NodeType.STRING, NodeType.NUMBER)); + } + + @Override + public void initializeArguments(List arguments) { + elementString = castString(arguments.get(0)); + elementNumber = castNumber(arguments.get(0)); + } + + @NotNull + @Override + public ExpressionNode execute() { + array.array().add(elementString == null ? elementNumber : elementString); + return array; + } } 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 3679ab7..03dfaa1 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/RepeatFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/RepeatFunction.java @@ -11,39 +11,39 @@ 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 NodeType.NUMBER.asParameterTypes(); - } - - @Override - public void initializeArguments(List arguments) { - amount = castNumber(arguments.get(0)); - } - - @NotNull - @Override - public ExpressionNode execute() { - try { - return stringNode(string.quoteless().repeat(amount.valueInteger())); - } catch (IllegalArgumentException e) { - throw formatException(e); - } - } + 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 NodeType.NUMBER.asParameterTypes(); + } + + @Override + public void initializeArguments(List arguments) { + amount = castNumber(arguments.get(0)); + } + + @NotNull + @Override + public ExpressionNode execute() { + try { + return stringNode(string.quoteless().repeat(amount.valueInteger())); + } catch (IllegalArgumentException e) { + throw formatException(e); + } + } } 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 99434b6..4589e09 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/ReplaceAllFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/ReplaceAllFunction.java @@ -11,44 +11,44 @@ public class ReplaceAllFunction extends Function { - private StringLiteralNode string; - 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() - ); - } - - @Override - public void initializeArguments(List arguments) { - pattern = castString(arguments.get(0)); - replacement = castString(arguments.get(1)); - } - - @NotNull - @Override - public ExpressionNode execute() { - try { - return stringNode(string.quoteless().replaceAll(pattern.quoteless(), replacement.quoteless())); - } catch (PatternSyntaxException e) { - throw formatException(e); - } - } + private StringLiteralNode string; + 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() + ); + } + + @Override + public void initializeArguments(List arguments) { + pattern = castString(arguments.get(0)); + replacement = castString(arguments.get(1)); + } + + @NotNull + @Override + public ExpressionNode execute() { + try { + return stringNode(string.quoteless().replaceAll(pattern.quoteless(), replacement.quoteless())); + } catch (PatternSyntaxException e) { + throw formatException(e); + } + } } 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 bebcf33..9552fbb 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/ReplaceFirstFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/ReplaceFirstFunction.java @@ -11,44 +11,44 @@ public class ReplaceFirstFunction extends Function { - private StringLiteralNode string; - 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() - ); - } - - @Override - public void initializeArguments(List arguments) { - pattern = castString(arguments.get(0)); - replacement = castString(arguments.get(1)); - } - - @NotNull - @Override - public ExpressionNode execute() { - try { - return stringNode(string.quoteless().replaceFirst(pattern.quoteless(), replacement.quoteless())); - } catch (PatternSyntaxException e) { - throw formatException(e); - } - } + private StringLiteralNode string; + 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() + ); + } + + @Override + public void initializeArguments(List arguments) { + pattern = castString(arguments.get(0)); + replacement = castString(arguments.get(1)); + } + + @NotNull + @Override + public ExpressionNode execute() { + try { + return stringNode(string.quoteless().replaceFirst(pattern.quoteless(), replacement.quoteless())); + } catch (PatternSyntaxException e) { + throw formatException(e); + } + } } 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 6d6c8ac..ceb23b8 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/ReverseArrayFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/ReverseArrayFunction.java @@ -10,24 +10,24 @@ public class ReverseArrayFunction extends ZeroParamFunction { - private ArrayNode array; - - @NotNull - @Override - public ParameterType getTargetType() { - return NodeType.ARRAY.asParameterType(); - } - - @Override - public void initializeTarget(ExpressionNode target) { - array = castArray(target); - } - - @NotNull - @Override - public ExpressionNode execute() { - Collections.reverse(array.array()); - return array; - } + private ArrayNode array; + + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.ARRAY.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + array = castArray(target); + } + + @NotNull + @Override + public ExpressionNode execute() { + Collections.reverse(array.array()); + return array; + } } 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 ff5d7ae..88774fb 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/ReverseStringFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/ReverseStringFunction.java @@ -8,23 +8,23 @@ public class ReverseStringFunction extends ZeroParamFunction { - private StringLiteralNode string; - - @NotNull - @Override - public ParameterType getTargetType() { - return NodeType.STRING.asParameterType(); - } - - @Override - public void initializeTarget(ExpressionNode target) { - string = castString(target); - } - - @NotNull - @Override - public ExpressionNode execute() { - return stringNode(new StringBuilder(string.quoteless()).reverse().toString()); - } + private StringLiteralNode string; + + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.STRING.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + string = castString(target); + } + + @NotNull + @Override + public ExpressionNode execute() { + return stringNode(new StringBuilder(string.quoteless()).reverse().toString()); + } } 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 5bb6f3f..045fb9c 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/RoundFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/RoundFunction.java @@ -8,23 +8,23 @@ public class RoundFunction extends ZeroParamFunction { - private NumberLiteralNode number; - - @NotNull - @Override - public ParameterType getTargetType() { - return NodeType.NUMBER.asParameterType(); - } - - @Override - public void initializeTarget(ExpressionNode target) { - number = castNumber(target); - } - - @NotNull - @Override - public ExpressionNode execute() { - return numberNode(number.valueInteger()); - } + private NumberLiteralNode number; + + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.NUMBER.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + number = castNumber(target); + } + + @NotNull + @Override + public ExpressionNode execute() { + return numberNode(number.valueInteger()); + } } 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 b98e7ac..e861735 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/ShiftFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/ShiftFunction.java @@ -8,27 +8,27 @@ public class ShiftFunction extends ZeroParamFunction { - private ArrayNode array; - - @NotNull - @Override - public ParameterType getTargetType() { - return NodeType.ARRAY.asParameterType(); - } - - @Override - public void initializeTarget(ExpressionNode target) { - array = castArray(target); - } - - @NotNull - @Override - public ExpressionNode execute() { - try { - return array.array().get(0); - } catch (IndexOutOfBoundsException e) { - throw formatException(e); - } - } + private ArrayNode array; + + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.ARRAY.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + array = castArray(target); + } + + @NotNull + @Override + public ExpressionNode execute() { + try { + return array.array().get(0); + } catch (IndexOutOfBoundsException e) { + throw formatException(e); + } + } } 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 56dad65..3111d82 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/SizeFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/SizeFunction.java @@ -9,25 +9,25 @@ public class SizeFunction extends ZeroParamFunction { - private ArrayNode array; - private StringLiteralNode string; - - @NotNull - @Override - public ParameterType getTargetType() { - return ParameterType.create(NodeType.ARRAY, NodeType.STRING); - } - - @Override - public void initializeTarget(ExpressionNode target) { - array = castArray(target); - string = castString(target); - } - - @NotNull - @Override - public ExpressionNode execute() { - return numberNode(array == null ? string.quoteless().length() : array.array().size()); - } + private ArrayNode array; + private StringLiteralNode string; + + @NotNull + @Override + public ParameterType getTargetType() { + return ParameterType.create(NodeType.ARRAY, NodeType.STRING); + } + + @Override + public void initializeTarget(ExpressionNode target) { + array = castArray(target); + string = castString(target); + } + + @NotNull + @Override + public ExpressionNode execute() { + return numberNode(array == null ? string.quoteless().length() : array.array().size()); + } } 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 5a71dff..1a0c855 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/SliceFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/SliceFunction.java @@ -12,49 +12,49 @@ public class SliceFunction extends Function { - private ArrayNode array; - 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.NUMBER.asParameterType(), - NodeType.NUMBER.asParameterType() - ); - } - - @Override - public void initializeArguments(List arguments) { - start = castNumber(arguments.get(0)); - end = castNumber(arguments.get(1)); - } - - @NotNull - @Override - public ExpressionNode execute() { - List newArray = new ArrayList<>(); - List list = array.array(); - for (int i = start.valueInteger(); i < Math.max(list.size() - 1, end.valueInteger()); i++) { - try { - newArray.add(valueQuoteless(list.get(i))); - } catch (IndexOutOfBoundsException e) { - throw formatException(e); - } - } - return arrayNode(newArray); - } + private ArrayNode array; + 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.NUMBER.asParameterType(), + NodeType.NUMBER.asParameterType() + ); + } + + @Override + public void initializeArguments(List arguments) { + start = castNumber(arguments.get(0)); + end = castNumber(arguments.get(1)); + } + + @NotNull + @Override + public ExpressionNode execute() { + List newArray = new ArrayList<>(); + List list = array.array(); + for (int i = start.valueInteger(); i < Math.max(list.size() - 1, end.valueInteger()); i++) { + try { + newArray.add(valueQuoteless(list.get(i))); + } catch (IndexOutOfBoundsException e) { + throw formatException(e); + } + } + return arrayNode(newArray); + } } 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 2539260..f7fd5fc 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/SpliceFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/SpliceFunction.java @@ -12,57 +12,57 @@ public class SpliceFunction extends Function { - private ArrayNode array; - private NumberLiteralNode index; - private NumberLiteralNode deleteCount; - private ArrayNode arrayAdd; + private ArrayNode array; + private NumberLiteralNode index; + private NumberLiteralNode deleteCount; + private ArrayNode arrayAdd; - @NotNull - @Override - public ParameterType getTargetType() { - return NodeType.ARRAY.asParameterType(); - } + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.ARRAY.asParameterType(); + } - @Override - public void initializeTarget(ExpressionNode target) { - array = castArray(target); - } + @Override + public void initializeTarget(ExpressionNode target) { + array = castArray(target); + } - @NotNull - @Override - public List getParameterTypes() { - return List.of( - NodeType.NUMBER.asParameterType(), - NodeType.NUMBER.asParameterType(), - NodeType.ARRAY.asParameterType() - ); - } + @NotNull + @Override + public List getParameterTypes() { + return List.of( + NodeType.NUMBER.asParameterType(), + NodeType.NUMBER.asParameterType(), + NodeType.ARRAY.asParameterType() + ); + } - @Override - public void initializeArguments(List arguments) { - index = castNumber(arguments.get(0)); - deleteCount = castNumber(arguments.get(1)); - arrayAdd = castArray(arguments.get(2)); - } + @Override + public void initializeArguments(List arguments) { + index = castNumber(arguments.get(0)); + deleteCount = castNumber(arguments.get(1)); + arrayAdd = castArray(arguments.get(2)); + } - @NotNull - @Override - public ExpressionNode execute() { - List newArray = new ArrayList<>(); - List list = array.array(); - int start = index.valueInteger(); - try { - for (int i = 0; i < start; i++) { - newArray.add(valueQuoteless(list.get(i))); - } - arrayAdd.array().forEach(item -> newArray.add(valueQuoteless(item))); - for (int i = start + deleteCount.valueInteger(); i < list.size(); i++) { - newArray.add(valueQuoteless(list.get(i))); - } - } catch (IndexOutOfBoundsException e) { - throw formatException(e); - } - return arrayNode(newArray); - } + @NotNull + @Override + public ExpressionNode execute() { + List newArray = new ArrayList<>(); + List list = array.array(); + int start = index.valueInteger(); + try { + for (int i = 0; i < start; i++) { + newArray.add(valueQuoteless(list.get(i))); + } + arrayAdd.array().forEach(item -> newArray.add(valueQuoteless(item))); + for (int i = start + deleteCount.valueInteger(); i < list.size(); i++) { + newArray.add(valueQuoteless(list.get(i))); + } + } catch (IndexOutOfBoundsException e) { + throw formatException(e); + } + return arrayNode(newArray); + } } 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 db5fbe7..aef5719 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/SplitFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/SplitFunction.java @@ -13,44 +13,44 @@ public class SplitFunction extends Function { - private StringLiteralNode string; - 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.NUMBER.asParameterType() - ); - } - - @Override - public void initializeArguments(List arguments) { - pattern = castString(arguments.get(0)); - limit = castNumber(arguments.get(1)); - } - - @NotNull - @Override - public ExpressionNode execute() { - try { - return arrayNode(Arrays.asList(string.quoteless().split(pattern.quoteless(), limit.valueInteger()))); - } catch (PatternSyntaxException e) { - throw formatException(e); - } - } + private StringLiteralNode string; + 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.NUMBER.asParameterType() + ); + } + + @Override + public void initializeArguments(List arguments) { + pattern = castString(arguments.get(0)); + limit = castNumber(arguments.get(1)); + } + + @NotNull + @Override + public ExpressionNode execute() { + try { + return arrayNode(Arrays.asList(string.quoteless().split(pattern.quoteless(), limit.valueInteger()))); + } catch (PatternSyntaxException e) { + throw formatException(e); + } + } } 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 76cdcff..f9af4d5 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/StartsWithFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/StartsWithFunction.java @@ -10,35 +10,35 @@ 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 NodeType.STRING.asParameterTypes(); - } - - @Override - public void initializeArguments(List arguments) { - substring = castString(arguments.get(0)); - } - - @NotNull - @Override - public ExpressionNode execute() { - return stringFromBool(string.quoteless().startsWith(substring.quoteless())); - } + 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 NodeType.STRING.asParameterTypes(); + } + + @Override + public void initializeArguments(List arguments) { + substring = castString(arguments.get(0)); + } + + @NotNull + @Override + public ExpressionNode execute() { + return stringFromBool(string.quoteless().startsWith(substring.quoteless())); + } } 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 2b3b907..9460b16 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/SubstringFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/SubstringFunction.java @@ -11,44 +11,44 @@ public class SubstringFunction extends Function { - private StringLiteralNode string; - 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.NUMBER.asParameterType(), - NodeType.NUMBER.asParameterType() - ); - } - - @Override - public void initializeArguments(List arguments) { - startIndex = castNumber(arguments.get(0)); - endIndex = castNumber(arguments.get(1)); - } - - @NotNull - @Override - public ExpressionNode execute() { - try { - return stringNode(string.quoteless().substring(startIndex.valueInteger(), endIndex.valueInteger())); - } catch (IndexOutOfBoundsException e) { - throw formatException(e); - } - } + private StringLiteralNode string; + 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.NUMBER.asParameterType(), + NodeType.NUMBER.asParameterType() + ); + } + + @Override + public void initializeArguments(List arguments) { + startIndex = castNumber(arguments.get(0)); + endIndex = castNumber(arguments.get(1)); + } + + @NotNull + @Override + public ExpressionNode execute() { + try { + return stringNode(string.quoteless().substring(startIndex.valueInteger(), endIndex.valueInteger())); + } catch (IndexOutOfBoundsException e) { + throw formatException(e); + } + } } 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 7009215..03d4c71 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/SubtractFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/SubtractFunction.java @@ -11,41 +11,41 @@ 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 NodeType.ARRAY.asParameterTypes(); - } - - @Override - public void initializeArguments(List arguments) { - arraySecond = castArray(arguments.get(0)); - } - - @NotNull - @Override - public ExpressionNode execute() { - List list = new ArrayList<>(); - List listSecond = arraySecond.stringArray(); - arrayFirst.array().forEach(item -> { - if (listSecond.contains(item.value())) return; - list.add(valueQuoteless(item)); - }); - return arrayNode(list); - } + 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 NodeType.ARRAY.asParameterTypes(); + } + + @Override + public void initializeArguments(List arguments) { + arraySecond = castArray(arguments.get(0)); + } + + @NotNull + @Override + public ExpressionNode execute() { + List list = new ArrayList<>(); + List listSecond = arraySecond.stringArray(); + arrayFirst.array().forEach(item -> { + if (listSecond.contains(item.value())) return; + list.add(valueQuoteless(item)); + }); + return arrayNode(list); + } } 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 44620ec..42e1bf1 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/ToArrayFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/ToArrayFunction.java @@ -10,24 +10,24 @@ public class ToArrayFunction extends ZeroParamFunction { - private ExpressionNode node; - - @NotNull - @Override - public ParameterType getTargetType() { - // Any - return ParameterType.create(NodeType.values()); - } - - @Override - public void initializeTarget(ExpressionNode target) { - node = target; - } - - @NotNull - @Override - public ExpressionNode execute() { - return new ArrayNode(Collections.singletonList(node)); - } + private ExpressionNode node; + + @NotNull + @Override + public ParameterType getTargetType() { + // Any + return ParameterType.create(NodeType.values()); + } + + @Override + public void initializeTarget(ExpressionNode target) { + node = target; + } + + @NotNull + @Override + 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 d60c5a5..5391e98 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/ToNumberFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/ToNumberFunction.java @@ -9,27 +9,27 @@ public class ToNumberFunction extends ZeroParamFunction { - private StringLiteralNode string; - - @NotNull - @Override - public ParameterType getTargetType() { - return NodeType.STRING.asParameterType(); - } - - @Override - public void initializeTarget(ExpressionNode target) { - string = castString(target); - } - - @NotNull - @Override - public ExpressionNode execute() { - try { - return new NumberLiteralNode(Double.parseDouble(string.quoteless()) + ""); - } catch (NumberFormatException e) { - throw formatException(e); - } - } + private StringLiteralNode string; + + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.STRING.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + string = castString(target); + } + + @NotNull + @Override + public ExpressionNode execute() { + try { + return new NumberLiteralNode(Double.parseDouble(string.quoteless()) + ""); + } catch (NumberFormatException e) { + throw formatException(e); + } + } } 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 21edc5e..bd76484 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/ToStringFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/ToStringFunction.java @@ -10,35 +10,35 @@ 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 NodeType.NUMBER.asParameterTypes(); - } - - @Override - public void initializeArguments(List arguments) { - radix = castNumber(arguments.get(0)); - } - - @NotNull - @Override - public ExpressionNode execute() { - return stringNode(Integer.toString(number.valueInteger(), radix.valueInteger())); - } + 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 NodeType.NUMBER.asParameterTypes(); + } + + @Override + public void initializeArguments(List arguments) { + radix = castNumber(arguments.get(0)); + } + + @NotNull + @Override + public ExpressionNode execute() { + return stringNode(Integer.toString(number.valueInteger(), radix.valueInteger())); + } } 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 429d8eb..e967f2f 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/TrimFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/TrimFunction.java @@ -8,23 +8,23 @@ public class TrimFunction extends ZeroParamFunction { - private StringLiteralNode string; - - @NotNull - @Override - public ParameterType getTargetType() { - return NodeType.STRING.asParameterType(); - } - - @Override - public void initializeTarget(ExpressionNode target) { - string = castString(target); - } - - @NotNull - @Override - public ExpressionNode execute() { - return stringNode(string.quoteless().trim()); - } + private StringLiteralNode string; + + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.STRING.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + string = castString(target); + } + + @NotNull + @Override + public ExpressionNode execute() { + return stringNode(string.quoteless().trim()); + } } 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 9dffb5a..2a53b0e 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/UnicodeFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/UnicodeFunction.java @@ -8,27 +8,27 @@ public class UnicodeFunction extends ZeroParamFunction { - private NumberLiteralNode code; - - @NotNull - @Override - public ParameterType getTargetType() { - return NodeType.NUMBER.asParameterType(); - } - - @Override - public void initializeTarget(ExpressionNode target) { - code = castNumber(target); - } - - @NotNull - @Override - public ExpressionNode execute() { - try { - return stringNode(String.valueOf(Character.toChars(code.valueInteger()))); - } catch (IllegalArgumentException e) { - throw formatException(e); - } - } + private NumberLiteralNode code; + + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.NUMBER.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + code = castNumber(target); + } + + @NotNull + @Override + public ExpressionNode execute() { + try { + return stringNode(String.valueOf(Character.toChars(code.valueInteger()))); + } catch (IllegalArgumentException e) { + throw formatException(e); + } + } } 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 88f1f52..e374cba 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/UnshiftFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/UnshiftFunction.java @@ -13,40 +13,40 @@ public class UnshiftFunction extends Function { - private ArrayNode array; - 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(ParameterType.create(NodeType.STRING, NodeType.NUMBER)); - } - - @Override - public void initializeArguments(List arguments) { - elementString = castString(arguments.get(0)); - elementNumber = castNumber(arguments.get(0)); - } - - @NotNull - @Override - public ExpressionNode execute() { - List list = new ArrayList<>(); - list.add(elementString == null ? elementNumber.value() : elementString.quoteless()); - array.array().forEach(item -> list.add(valueQuoteless(item))); - return arrayNode(list); - } + private ArrayNode array; + 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(ParameterType.create(NodeType.STRING, NodeType.NUMBER)); + } + + @Override + public void initializeArguments(List arguments) { + elementString = castString(arguments.get(0)); + elementNumber = castNumber(arguments.get(0)); + } + + @NotNull + @Override + public ExpressionNode execute() { + List list = new ArrayList<>(); + list.add(elementString == null ? elementNumber.value() : elementString.quoteless()); + array.array().forEach(item -> list.add(valueQuoteless(item))); + return arrayNode(list); + } } 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 fd7579f..d51f060 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/UppercaseFunction.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/UppercaseFunction.java @@ -8,23 +8,23 @@ public class UppercaseFunction extends ZeroParamFunction { - private StringLiteralNode string; - - @NotNull - @Override - public ParameterType getTargetType() { - return NodeType.STRING.asParameterType(); - } - - @Override - public void initializeTarget(ExpressionNode target) { - string = castString(target); - } - - @NotNull - @Override - public ExpressionNode execute() { - return stringNode(string.quoteless().toUpperCase()); - } + private StringLiteralNode string; + + @NotNull + @Override + public ParameterType getTargetType() { + return NodeType.STRING.asParameterType(); + } + + @Override + public void initializeTarget(ExpressionNode target) { + string = castString(target); + } + + @NotNull + @Override + public ExpressionNode execute() { + return stringNode(string.quoteless().toUpperCase()); + } } 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 24a5b62..ee42ec9 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 @@ -14,67 +14,67 @@ */ public abstract class Function { - private final String functionName; + private final String functionName; - public Function() { - String name = getClass().getSimpleName(); - functionName = Character.toLowerCase(name.charAt(0)) + name.substring(1, name.lastIndexOf("Function")); - } + public Function() { + String name = getClass().getSimpleName(); + functionName = Character.toLowerCase(name.charAt(0)) + name.substring(1, name.lastIndexOf("Function")); + } - @NotNull - public abstract ParameterType getTargetType(); + @NotNull + public abstract ParameterType getTargetType(); - @NotNull - public abstract List getParameterTypes(); + @NotNull + public abstract List getParameterTypes(); - public abstract void initializeTarget(ExpressionNode target); + public abstract void initializeTarget(ExpressionNode target); - public abstract void initializeArguments(List arguments); + public abstract void initializeArguments(List arguments); - @NotNull - public abstract ExpressionNode execute(); + @NotNull + public abstract ExpressionNode execute(); - protected FunctionCallException formatException(RuntimeException exception) { - return new FunctionCallException(functionName + ": \"" + exception.getMessage() + '"'); - } + protected FunctionCallException formatException(RuntimeException exception) { + return new FunctionCallException(functionName + ": \"" + exception.getMessage() + '"'); + } - protected StringLiteralNode castString(ExpressionNode node) { - return node instanceof StringLiteralNode string ? string : null; - } + protected StringLiteralNode castString(ExpressionNode node) { + return node instanceof StringLiteralNode string ? string : null; + } - protected NumberLiteralNode castNumber(ExpressionNode node) { - return node instanceof NumberLiteralNode number ? number : null; - } + protected NumberLiteralNode castNumber(ExpressionNode node) { + return node instanceof NumberLiteralNode number ? number : null; + } - protected ArrayNode castArray(ExpressionNode node) { - return node instanceof ArrayNode array ? array : null; - } + protected ArrayNode castArray(ExpressionNode node) { + return node instanceof ArrayNode array ? array : null; + } - protected static String valueQuoteless(ExpressionNode node) { - return node instanceof StringLiteralNode string ? string.quoteless() : node.value(); - } + protected static String valueQuoteless(ExpressionNode node) { + return node instanceof StringLiteralNode string ? string.quoteless() : node.value(); + } - protected static String quote(String string) { - return '"' + string + '"'; - } + protected static String quote(String string) { + return '"' + string + '"'; + } - protected static StringLiteralNode stringNode(String value) { - return new StringLiteralNode(quote(value)); - } + protected static StringLiteralNode stringNode(String value) { + return new StringLiteralNode(quote(value)); + } - protected static NumberLiteralNode numberNode(int value) { - return new NumberLiteralNode(value + ""); - } + protected static NumberLiteralNode numberNode(int value) { + return new NumberLiteralNode(value + ""); + } - protected static StringLiteralNode stringFromBool(boolean bool) { - return stringNode(Boolean.toString(bool)); - } + protected static StringLiteralNode stringFromBool(boolean bool) { + return stringNode(Boolean.toString(bool)); + } - protected static ArrayNode arrayNode(List list) { - return new ArrayNode(list.stream() - .map(item -> new StringLiteralNode(quote(item))) - .collect(Collectors.toList()) - ); - } + protected static ArrayNode arrayNode(List list) { + 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/ParameterType.java b/src/main/java/eu/jasperlorelai/circe/parser/function/util/ParameterType.java index 81bcc45..c7f85a1 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/util/ParameterType.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/util/ParameterType.java @@ -9,23 +9,23 @@ public class ParameterType { - private final Set validTypes = new HashSet<>(); + private final Set validTypes = new HashSet<>(); - private ParameterType() {} + private ParameterType() {} - public static ParameterType create(NodeType ...validTypes) { - ParameterType type = new ParameterType(); - type.validTypes.addAll(Arrays.asList(validTypes)); - return type; - } + public static ParameterType create(NodeType ...validTypes) { + ParameterType type = new ParameterType(); + type.validTypes.addAll(Arrays.asList(validTypes)); + return type; + } - public boolean isValid(NodeType type) { - return validTypes.contains(type); - } + public boolean isValid(NodeType type) { + return validTypes.contains(type); + } - @Override - public String toString() { - return validTypes.stream().map(Enum::name).collect(Collectors.joining(" or ")); - } + @Override + public String toString() { + return validTypes.stream().map(Enum::name).collect(Collectors.joining(" or ")); + } } diff --git a/src/main/java/eu/jasperlorelai/circe/parser/function/util/Variables.java b/src/main/java/eu/jasperlorelai/circe/parser/function/util/Variables.java index 2c40373..1e44fac 100644 --- a/src/main/java/eu/jasperlorelai/circe/parser/function/util/Variables.java +++ b/src/main/java/eu/jasperlorelai/circe/parser/function/util/Variables.java @@ -7,10 +7,10 @@ public class Variables { - private static final Map variables = new HashMap<>(); + private static final Map variables = new HashMap<>(); - public static Map getVariables() { - return variables; - } + public static Map getVariables() { + return variables; + } } diff --git a/src/main/java/eu/jasperlorelai/circe/tokenizer/Tokenizer.java b/src/main/java/eu/jasperlorelai/circe/tokenizer/Tokenizer.java index d270e3a..1b27fe2 100644 --- a/src/main/java/eu/jasperlorelai/circe/tokenizer/Tokenizer.java +++ b/src/main/java/eu/jasperlorelai/circe/tokenizer/Tokenizer.java @@ -10,58 +10,58 @@ public class Tokenizer { - private static final List IGNORED_TOKENS = List.of( - SpecialTokenType.WHITESPACE, - SpecialTokenType.SINGLE_COMMENT, - SpecialTokenType.MULTI_COMMENT - ); + private static final List IGNORED_TOKENS = List.of( + SpecialTokenType.WHITESPACE, + SpecialTokenType.SINGLE_COMMENT, + SpecialTokenType.MULTI_COMMENT + ); - private List tokens; - private int pointer; + private List tokens; + private int pointer; - public record Token(TokenType tokenType, String value) {} + public record Token(TokenType tokenType, String value) {} - private Tokenizer() { - tokens = new ArrayList<>(); - pointer = 0; - } + private Tokenizer() { + tokens = new ArrayList<>(); + pointer = 0; + } - public static Tokenizer create() { - return new Tokenizer(); - } + public static Tokenizer create() { + return new Tokenizer(); + } - /** - * @throws UnknownTokenException if the string contains an unknown token - */ - public void tokenize(String string) { - tokens = new ArrayList<>(); + /** + * @throws UnknownTokenException if the string contains an unknown token + */ + public void tokenize(String string) { + tokens = new ArrayList<>(); - while (!string.isEmpty()) { - boolean hasMatch = false; - for (TokenType tokenType : TokenTypeStore.getTypes()) { - Matcher matcher = tokenType.regex().matcher(string); - if (!matcher.find()) continue; - hasMatch = true; + while (!string.isEmpty()) { + boolean hasMatch = false; + for (TokenType tokenType : TokenTypeStore.getTypes()) { + Matcher matcher = tokenType.regex().matcher(string); + if (!matcher.find()) continue; + hasMatch = true; - String token = matcher.group().trim(); - if (!IGNORED_TOKENS.contains(tokenType)) { - tokens.add(new Token(tokenType, token)); - } + String token = matcher.group().trim(); + if (!IGNORED_TOKENS.contains(tokenType)) { + tokens.add(new Token(tokenType, token)); + } - string = matcher.replaceFirst(""); - break; - } - if (hasMatch) continue; - throw new UnknownTokenException("Unknown token: " + string); - } - } + string = matcher.replaceFirst(""); + break; + } + if (hasMatch) continue; + throw new UnknownTokenException("Unknown token: " + string); + } + } - public boolean hasMoreTokens() { - return pointer < tokens.size(); - } + public boolean hasMoreTokens() { + return pointer < tokens.size(); + } - public Token getNextToken() { - return hasMoreTokens() ? tokens.get(pointer++) : null; - } + public Token getNextToken() { + return hasMoreTokens() ? tokens.get(pointer++) : null; + } } diff --git a/src/main/java/eu/jasperlorelai/circe/tokenizer/type/FunctionTokenType.java b/src/main/java/eu/jasperlorelai/circe/tokenizer/type/FunctionTokenType.java index 77d50f5..3a58354 100644 --- a/src/main/java/eu/jasperlorelai/circe/tokenizer/type/FunctionTokenType.java +++ b/src/main/java/eu/jasperlorelai/circe/tokenizer/type/FunctionTokenType.java @@ -13,87 +13,87 @@ public enum FunctionTokenType implements TokenType { - // Special - TO_STRING(ToStringFunction.class), - TO_NUMBER(ToNumberFunction.class), - TO_ARRAY(ToArrayFunction.class), - ROUND(RoundFunction.class), - CALC(CalcFunction.class), - - // Merged - CONTAINS(ContainsFunction.class), - EQUALS(EqualsFunction.class), - INDEX_OF(IndexOfFunction.class), - LAST_INDEX_OF(LastIndexOfFunction.class), - SIZE(SizeFunction.class), - IS_EMPTY(IsEmptyFunction.class), - - // String - CHAR_AT(CharAtFunction.class), - CHAR_CODE_AT(CharCodeAtFunction.class), - ENDS_WITH(EndsWithFunction.class), - LOWERCASE(LowercaseFunction.class), - MATCH(MatchFunction.class), - PAD_END(PadEndFunction.class), - PAD_START(PadStartFunction.class), - REPEAT(RepeatFunction.class), - REPLACE_FIRST(ReplaceFirstFunction.class), - REPLACE_ALL(ReplaceAllFunction.class), - REVERSE_STRING(ReverseStringFunction.class), - SPLIT(SplitFunction.class), - STARTS_WITH(StartsWithFunction.class), - SUBSTRING(SubstringFunction.class), - TRIM(TrimFunction.class), - UNICODE(UnicodeFunction.class), - UPPERCASE(UppercaseFunction.class), - - // Array - ELEMENT_AT(ElementAtFunction.class), - JOIN(JoinFunction.class), - REVERSE_ARRAY(ReverseArrayFunction.class), - POP(PopFunction.class), - PUSH(PushFunction.class), - SHIFT(ShiftFunction.class), - SLICE(SliceFunction.class), - SPLICE(SpliceFunction.class), - SUBTRACT(SubtractFunction.class), - UNSHIFT(UnshiftFunction.class), - - ; - - private final Class functionClass; - private final String functionName; - private final Pattern pattern; - - FunctionTokenType(Class functionClass) { - this.functionClass = functionClass; - String name = functionClass.getSimpleName(); - functionName = Character.toLowerCase(name.charAt(0)) + name.substring(1, name.lastIndexOf("Function")); - pattern = createPattern(functionName); - } - - public String functionName() { - return functionName; - } - - @NotNull - @Override - public Pattern regex() { - return pattern; - } - - @NotNull - @Override - public TokenSuperType type() { - return TokenSuperType.FUNCTION; - } - - public Function createFunction() { - try { - return functionClass.getDeclaredConstructor().newInstance(); - } catch (InstantiationException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { - throw new RuntimeException(e); - } - } + // Special + TO_STRING(ToStringFunction.class), + TO_NUMBER(ToNumberFunction.class), + TO_ARRAY(ToArrayFunction.class), + ROUND(RoundFunction.class), + CALC(CalcFunction.class), + + // Merged + CONTAINS(ContainsFunction.class), + EQUALS(EqualsFunction.class), + INDEX_OF(IndexOfFunction.class), + LAST_INDEX_OF(LastIndexOfFunction.class), + SIZE(SizeFunction.class), + IS_EMPTY(IsEmptyFunction.class), + + // String + CHAR_AT(CharAtFunction.class), + CHAR_CODE_AT(CharCodeAtFunction.class), + ENDS_WITH(EndsWithFunction.class), + LOWERCASE(LowercaseFunction.class), + MATCH(MatchFunction.class), + PAD_END(PadEndFunction.class), + PAD_START(PadStartFunction.class), + REPEAT(RepeatFunction.class), + REPLACE_FIRST(ReplaceFirstFunction.class), + REPLACE_ALL(ReplaceAllFunction.class), + REVERSE_STRING(ReverseStringFunction.class), + SPLIT(SplitFunction.class), + STARTS_WITH(StartsWithFunction.class), + SUBSTRING(SubstringFunction.class), + TRIM(TrimFunction.class), + UNICODE(UnicodeFunction.class), + UPPERCASE(UppercaseFunction.class), + + // Array + ELEMENT_AT(ElementAtFunction.class), + JOIN(JoinFunction.class), + REVERSE_ARRAY(ReverseArrayFunction.class), + POP(PopFunction.class), + PUSH(PushFunction.class), + SHIFT(ShiftFunction.class), + SLICE(SliceFunction.class), + SPLICE(SpliceFunction.class), + SUBTRACT(SubtractFunction.class), + UNSHIFT(UnshiftFunction.class), + + ; + + private final Class functionClass; + private final String functionName; + private final Pattern pattern; + + FunctionTokenType(Class functionClass) { + this.functionClass = functionClass; + String name = functionClass.getSimpleName(); + functionName = Character.toLowerCase(name.charAt(0)) + name.substring(1, name.lastIndexOf("Function")); + pattern = createPattern(functionName); + } + + public String functionName() { + return functionName; + } + + @NotNull + @Override + public Pattern regex() { + return pattern; + } + + @NotNull + @Override + public TokenSuperType type() { + return TokenSuperType.FUNCTION; + } + + public Function createFunction() { + try { + return functionClass.getDeclaredConstructor().newInstance(); + } catch (InstantiationException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { + throw new RuntimeException(e); + } + } } diff --git a/src/main/java/eu/jasperlorelai/circe/tokenizer/type/LiteralTokenType.java b/src/main/java/eu/jasperlorelai/circe/tokenizer/type/LiteralTokenType.java index e72b5f4..be686ec 100644 --- a/src/main/java/eu/jasperlorelai/circe/tokenizer/type/LiteralTokenType.java +++ b/src/main/java/eu/jasperlorelai/circe/tokenizer/type/LiteralTokenType.java @@ -10,26 +10,27 @@ import eu.jasperlorelai.circe.tokenizer.type.util.TokenSuperType; public enum LiteralTokenType implements TokenType { - NUMBER("-?(\\d*\\.)?\\d+"), - STRING("\"(?:[^\"\\\\]|\\\\.)*\""), - ; - - private final Pattern pattern; - - LiteralTokenType(@Language("RegExp") String regex) { - pattern = createPattern(regex); - } - - @NotNull - @Override - public Pattern regex() { - return pattern; - } - - @NotNull - @Override - public TokenSuperType type() { - return TokenSuperType.LITERAL; - } + + NUMBER("-?(\\d*\\.)?\\d+"), + STRING("\"(?:[^\"\\\\]|\\\\.)*\""), + ; + + private final Pattern pattern; + + LiteralTokenType(@Language("RegExp") String regex) { + pattern = createPattern(regex); + } + + @NotNull + @Override + public Pattern regex() { + return pattern; + } + + @NotNull + @Override + public TokenSuperType type() { + return TokenSuperType.LITERAL; + } } 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 1158033..06f996c 100644 --- a/src/main/java/eu/jasperlorelai/circe/tokenizer/type/SpecialTokenType.java +++ b/src/main/java/eu/jasperlorelai/circe/tokenizer/type/SpecialTokenType.java @@ -9,37 +9,38 @@ import eu.jasperlorelai.circe.tokenizer.type.util.*; public enum SpecialTokenType implements TokenType { - VARIABLE_NAME("[a-zA-Z_]\\w*"), - PARENTHESES_OPEN("\\("), - PARENTHESES_CLOSE("\\)"), - BRACKET_OPEN("\\["), - BRACKET_CLOSE("]"), - DOLLAR("\\$"), - EQUALS("="), - COMMA(","), - DOT("\\."), - COMMA_COLON(";"), - WHITESPACE("[\n\r\s]+"), - SINGLE_COMMENT("//.*[\n\r]"), - MULTI_COMMENT("/\\*(.|[\n\r])*\\*/"), - ; - - private final Pattern pattern; - - SpecialTokenType(@Language("RegExp") String regex) { - pattern = createPattern(regex); - } - - @NotNull - @Override - public Pattern regex() { - return pattern; - } - - @NotNull - @Override - public TokenSuperType type() { - return TokenSuperType.SPECIAL; - } + + VARIABLE_NAME("[a-zA-Z_]\\w*"), + PARENTHESES_OPEN("\\("), + PARENTHESES_CLOSE("\\)"), + BRACKET_OPEN("\\["), + BRACKET_CLOSE("]"), + DOLLAR("\\$"), + EQUALS("="), + COMMA(","), + DOT("\\."), + COMMA_COLON(";"), + WHITESPACE("[\n\r\s]+"), + SINGLE_COMMENT("//.*[\n\r]"), + MULTI_COMMENT("/\\*(.|[\n\r])*\\*/"), + ; + + private final Pattern pattern; + + SpecialTokenType(@Language("RegExp") String regex) { + pattern = createPattern(regex); + } + + @NotNull + @Override + public Pattern regex() { + return pattern; + } + + @NotNull + @Override + public TokenSuperType type() { + return TokenSuperType.SPECIAL; + } } diff --git a/src/main/java/eu/jasperlorelai/circe/tokenizer/type/util/TokenSuperType.java b/src/main/java/eu/jasperlorelai/circe/tokenizer/type/util/TokenSuperType.java index f4e49bb..567d9dd 100644 --- a/src/main/java/eu/jasperlorelai/circe/tokenizer/type/util/TokenSuperType.java +++ b/src/main/java/eu/jasperlorelai/circe/tokenizer/type/util/TokenSuperType.java @@ -1,7 +1,7 @@ package eu.jasperlorelai.circe.tokenizer.type.util; public enum TokenSuperType { - FUNCTION, - LITERAL, - SPECIAL + FUNCTION, + LITERAL, + SPECIAL } diff --git a/src/main/java/eu/jasperlorelai/circe/tokenizer/type/util/TokenType.java b/src/main/java/eu/jasperlorelai/circe/tokenizer/type/util/TokenType.java index 2d84389..8f0e9a2 100644 --- a/src/main/java/eu/jasperlorelai/circe/tokenizer/type/util/TokenType.java +++ b/src/main/java/eu/jasperlorelai/circe/tokenizer/type/util/TokenType.java @@ -6,14 +6,15 @@ public interface TokenType { - @NotNull Pattern regex(); + @NotNull + Pattern regex(); - @NotNull - TokenSuperType type(); + @NotNull + TokenSuperType type(); - default Pattern createPattern(String regex) { - if (regex.charAt(0) != '^') regex = "^" + regex; - return Pattern.compile(regex); - } + default Pattern createPattern(String regex) { + if (regex.charAt(0) != '^') regex = "^" + regex; + return Pattern.compile(regex); + } } diff --git a/src/main/java/eu/jasperlorelai/circe/tokenizer/type/util/TokenTypeStore.java b/src/main/java/eu/jasperlorelai/circe/tokenizer/type/util/TokenTypeStore.java index 446d0a1..3b6b45f 100644 --- a/src/main/java/eu/jasperlorelai/circe/tokenizer/type/util/TokenTypeStore.java +++ b/src/main/java/eu/jasperlorelai/circe/tokenizer/type/util/TokenTypeStore.java @@ -7,19 +7,19 @@ public class TokenTypeStore { - private static final List types = new ArrayList<>(); - static { - add(FunctionTokenType.values()); - add(LiteralTokenType.values()); - add(SpecialTokenType.values()); - } + private static final List types = new ArrayList<>(); + static { + add(FunctionTokenType.values()); + add(LiteralTokenType.values()); + add(SpecialTokenType.values()); + } - private static void add(TokenType[] types) { - getTypes().addAll(List.of(types)); - } + private static void add(TokenType[] types) { + getTypes().addAll(List.of(types)); + } - public static List getTypes() { - return types; - } + public static List getTypes() { + return types; + } }