diff --git a/src/main/java/de/zonlykroks/advancedscripts/lexer/ExpressionColorizer.java b/src/main/java/de/zonlykroks/advancedscripts/lexer/ExpressionColorizer.java new file mode 100644 index 0000000..a0e691a --- /dev/null +++ b/src/main/java/de/zonlykroks/advancedscripts/lexer/ExpressionColorizer.java @@ -0,0 +1,134 @@ +package de.zonlykroks.advancedscripts.lexer; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class ExpressionColorizer { + + private ExpressionColorizer() { + throw new IllegalStateException("Utility class"); + } + + public static List colorize(String expression) { + List parts = tokenize(expression); + List tokens = new ArrayList<>(); + for (int i = 0; i < parts.size(); i++) { + String part = parts.get(i); + if ("{".equals(part) || "}".equals(part)) { + tokens.add(new Token(part, TokenTypeColors.OTHER)); + continue; + } + if ("true".equalsIgnoreCase(part) || "false".equalsIgnoreCase(part)) { + tokens.add(new Token(part, TokenTypeColors.BOOLEAN)); + continue; + } + try { + Double.parseDouble(part); + tokens.add(new Token(part, TokenTypeColors.NUMBER)); + continue; + } catch (NumberFormatException ignored) { + } + try { + Long.parseLong(part); + tokens.add(new Token(part, TokenTypeColors.NUMBER)); + continue; + } catch (NumberFormatException ignored) { + } + if (part.contains(".")) { + String[] split = part.split("\\."); + if (split.length == 1) { + tokens.add(new Token(part, TokenTypeColors.VARIABLE)); + continue; + } + if (VariablePrefixes.RPEFIXES.contains(split[0])) { + tokens.add(new Token(split[0], TokenTypeColors.OTHER)); + tokens.add(new Token(".", TokenTypeColors.OTHER)); + split = Arrays.copyOfRange(split, 1, split.length); + } + tokens.add(new Token(split[0], TokenTypeColors.VARIABLE)); + for (int j = 1; j < split.length; j++) { + String s = split[j]; + tokens.add(new Token(".", TokenTypeColors.OTHER)); + if (VariableSuffixes.SUFFIXES.contains(s)) { + tokens.add(new Token(s, TokenTypeColors.OTHER)); + } else { + tokens.add(new Token(s, TokenTypeColors.ERROR)); + } + } + continue; + } + if (Operators.OPERATORS.contains(part)) { + String previous = get(parts, i, -1); + String next = get(parts, i, 1); + if (previous == null || next == null) { + tokens.add(new Token(part, TokenTypeColors.ERROR)); + continue; + } + if (Operators.OPERATORS.contains(previous) || Operators.OPERATORS.contains(next)) { + tokens.add(new Token(part, TokenTypeColors.ERROR)); + continue; + } + if ("{".equals(previous) || "}".equals(next)) { + tokens.add(new Token(part, TokenTypeColors.ERROR)); + continue; + } + tokens.add(new Token(part, TokenTypeColors.OTHER)); + continue; + } + if (part.matches("[+\\-*/%^&|<>=!]+")) { + tokens.add(new Token(part, TokenTypeColors.ERROR)); + continue; + } + tokens.add(new Token(part, TokenTypeColors.VARIABLE)); + } + return tokens; + } + + private static String get(List parts, int index, int direction) { + for (int i = index + direction; i >= 0 && i < parts.size(); i += direction) { + String part = parts.get(i); + if (!part.isEmpty()) return part; + } + return null; + } + + private static List tokenize(String s) { + List tokens = new ArrayList<>(); + StringBuilder token = new StringBuilder(); + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (c == '{' || c == '}' || c == ' ') { + if (token.length() > 0) { + tokens.add(token.toString()); + token = new StringBuilder(); + } + tokens.add(c + ""); + continue; + } + StringBuilder op = new StringBuilder(); + for (int j = i; j < s.length(); j++) { + char k = s.charAt(j); + if (k == '+' || k == '-' || k == '*' || k == '/' || k == '%' || k == '^' || k == '&' || k == '|' || k == '>' || k == '<' || k == '=' || k == '!') { + op.append(k); + } else { + break; + } + } + if (op.length() > 0) { + if (token.length() > 0) { + tokens.add(token.toString()); + token = new StringBuilder(); + } + tokens.add(op.toString()); + i += op.length() - 1; + continue; + } + token.append(c); + } + if (token.length() > 0) { + tokens.add(token.toString()); + } + return tokens; + } +} diff --git a/src/main/java/de/zonlykroks/advancedscripts/lexer/ScriptColorizer.java b/src/main/java/de/zonlykroks/advancedscripts/lexer/ScriptColorizer.java index e90fc9e..54322be 100644 --- a/src/main/java/de/zonlykroks/advancedscripts/lexer/ScriptColorizer.java +++ b/src/main/java/de/zonlykroks/advancedscripts/lexer/ScriptColorizer.java @@ -165,8 +165,7 @@ public class ScriptColorizer { index++; } while (depth != 0 && index <= current.length()); if (depth != 0) return List.of(new Token(current, TokenTypeColors.ERROR)); - String expression = current.substring(0, index); // TODO: colorize expression - return List.of(new Token(expression, TokenTypeColors.OTHER)); + return ExpressionColorizer.colorize(current.substring(0, index)); } private static List parseJumpPoint(String current) {