From 58cac3667c02f225c5881c29cce041d5ebc3a60b Mon Sep 17 00:00:00 2001 From: bigguy345 Date: Fri, 2 Jan 2026 14:57:45 +0200 Subject: [PATCH] Added ENUM CONSTANT parsing and validation --- .../interpreter/ErrorUnderlineRenderer.java | 3 +- .../script/interpreter/ScriptDocument.java | 79 +++++++++++++++++-- .../script/interpreter/field/FieldInfo.java | 66 +++++++++++++++- .../interpreter/hover/TokenHoverInfo.java | 29 ++++++- .../interpreter/method/MethodCallInfo.java | 37 +++++---- .../util/script/interpreter/token/Token.java | 3 +- .../script/interpreter/token/TokenType.java | 13 ++- .../interpreter/type/ScriptTypeInfo.java | 44 +++++++++++ 8 files changed, 244 insertions(+), 30 deletions(-) diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/ErrorUnderlineRenderer.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/ErrorUnderlineRenderer.java index b5c8cfd38..1e3bae054 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/ErrorUnderlineRenderer.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/ErrorUnderlineRenderer.java @@ -55,7 +55,8 @@ public class ErrorUnderlineRenderer { boolean isDeclaration = false; int methodStart = call.getMethodNameStart(); for (MethodInfo mi : doc.getAllMethods()) { - if (mi.getDeclarationOffset() <= methodStart && mi.getBodyStart() >= methodStart) { + //!call.isConstructor for enum declarations + if (!call.isConstructor() && mi.getDeclarationOffset() <= methodStart && mi.getBodyStart() >= methodStart) { isDeclaration = true; break; } diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/ScriptDocument.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/ScriptDocument.java index 740870e71..5c23db663 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/ScriptDocument.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/ScriptDocument.java @@ -5,6 +5,7 @@ import noppes.npcs.client.gui.util.script.interpreter.expression.CastExpressionR import noppes.npcs.client.gui.util.script.interpreter.expression.ExpressionNode; import noppes.npcs.client.gui.util.script.interpreter.expression.ExpressionTypeResolver; import noppes.npcs.client.gui.util.script.interpreter.field.AssignmentInfo; +import noppes.npcs.client.gui.util.script.interpreter.field.EnumConstantInfo; import noppes.npcs.client.gui.util.script.interpreter.field.FieldAccessInfo; import noppes.npcs.client.gui.util.script.interpreter.field.FieldInfo; import noppes.npcs.client.gui.util.script.interpreter.method.MethodCallInfo; @@ -42,6 +43,7 @@ import java.util.stream.Collectors; */ public class ScriptDocument { + public static ScriptDocument INSTANCE = null; // For easy access in expressions // ==================== PATTERNS ==================== private static final Pattern WORD_PATTERN = Pattern.compile("[\\p{L}\\p{N}_-]+|\\n|$"); @@ -196,6 +198,7 @@ public class ScriptDocument { linesCount = lines.size(); totalHeight = linesCount * lineHeight; visibleLines = Math.max(height / lineHeight - 1, 1); + INSTANCE = this; } // ==================== TOKENIZATION ==================== @@ -283,7 +286,7 @@ public class ScriptDocument { excludedRanges.addAll(merged); } - private boolean isExcluded(int position) { + public boolean isExcluded(int position) { for (int[] range : excludedRanges) { if (position >= range[0] && position < range[1]) { return true; @@ -498,6 +501,25 @@ public class ScriptDocument { documentation); scriptType.addConstructor(constructorInfo); } + + // Parse enum constants (for enum types only) + if (scriptType.getKind() == TypeInfo.Kind.ENUM) { + List constants = EnumConstantInfo.parseEnumConstants( + scriptType, + bodyText, + bodyStart + 1, + KEYWORD_PATTERN + ); + + for (EnumConstantInfo constant : constants) { + scriptType.addEnumConstant(constant); + if (constant.getConstructorCall() != null) { + + } + // methodCalls.add(constant.getConstructorCall()); + + } + } } /** @@ -1424,6 +1446,9 @@ public class ScriptDocument { // Class/interface/enum declarations markClassDeclarations(marks); + // Enum constants (must be after class declarations so enums are known) + markEnumConstants(marks); + // Keywords and modifiers addPatternMarks(marks, KEYWORD_PATTERN, TokenType.KEYWORD); addPatternMarks(marks, MODIFIER_PATTERN, TokenType.MODIFIER); @@ -1703,6 +1728,29 @@ public class ScriptDocument { } } + /** + * Mark enum constants with ENUM_CONSTANT token type. + * Adds marks for all enum constants in script-defined enums. + */ + private void markEnumConstants(List marks) { + for (ScriptTypeInfo scriptType : scriptTypes.values()) { + if (scriptType.getKind() != TypeInfo.Kind.ENUM) { + continue; + } + + // Mark each enum constant + for (EnumConstantInfo constant : scriptType.getEnumConstants().values()) { + FieldInfo fieldInfo = constant.getFieldInfo(); + int start = fieldInfo.getDeclarationOffset(); + int end = start + fieldInfo.getName().length(); + + // Always use ENUM_CONSTANT token type (blue + bold + italic) + // Errors are shown via underline, not by changing the token type + marks.add(new ScriptLine.Mark(start, end, TokenType.ENUM_CONSTANT, constant)); + } + } + } + /** * Mark the extends clause with proper coloring. * The parent class is colored based on its resolved type. @@ -2243,7 +2291,7 @@ public class ScriptDocument { * @param methodInfo Optional MethodInfo to provide expected parameter types for validation * @return List of parsed arguments with resolved types */ - private List parseMethodArguments(int start, int end, MethodInfo methodInfo) { + public List parseMethodArguments(int start, int end, MethodInfo methodInfo) { List args = new ArrayList<>(); if (start >= end) { @@ -5005,19 +5053,40 @@ public class ScriptDocument { public List getAllMethods() { List allMethods = new ArrayList<>(methods); - for (ScriptTypeInfo scriptType : scriptTypes.values()) + for (ScriptTypeInfo scriptType : scriptTypes.values()) { allMethods.addAll(scriptType.getAllMethodsFlat()); + // Include constructors so their parameters are recognized + allMethods.addAll(scriptType.getConstructors()); + } return allMethods; } public List getMethodCalls() { - return Collections.unmodifiableList(methodCalls); + List allCalls = new ArrayList<>(methodCalls); + + for (EnumConstantInfo constant : getAllEnumConstants()) { + if (constant.getConstructorCall() != null) + allCalls.add(constant.getConstructorCall()); + } + + + return allCalls; } public List getFieldAccesses() { return Collections.unmodifiableList(fieldAccesses); } - + + public List getAllEnumConstants() { + List enums = new ArrayList<>(); + for (ScriptTypeInfo scriptType : scriptTypes.values()) { + if (!scriptType.hasEnumConstants()) + continue; + + enums.addAll(scriptType.getEnumConstants().values()); + } + return enums; + } /** * Find an assignment at the given position, prioritizing LHS over RHS. * Searches across all script fields and external field assignments. diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/field/FieldInfo.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/field/FieldInfo.java index 6fa9bd613..660dad234 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/field/FieldInfo.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/field/FieldInfo.java @@ -21,7 +21,8 @@ public final class FieldInfo { public enum Scope { GLOBAL, // Class-level field LOCAL, // Local variable inside a method - PARAMETER // Method parameter + PARAMETER, // Method parameter + ENUM_CONSTANT // Enum constant value } private final String name; @@ -49,11 +50,17 @@ public final class FieldInfo { // Declaration assignment (for initial value validation) private AssignmentInfo declarationAssignment; + + // For enum constants: the arguments passed to the constructor (null if no args or not enum constant) + private final String enumConstantArgs; + + // For enum constants: the containing enum type + private final TypeInfo containingEnumType; private FieldInfo(String name, Scope scope, TypeInfo declaredType, int declarationOffset, boolean resolved, MethodInfo containingMethod, String documentation, int initStart, int initEnd, int modifiers, - Field reflectionField) { + Field reflectionField, String enumConstantArgs, TypeInfo containingEnumType) { this.name = name; this.scope = scope; this.declaredType = declaredType; @@ -65,6 +72,17 @@ public final class FieldInfo { this.initEnd = initEnd; this.modifiers = modifiers; this.reflectionField = reflectionField; + this.enumConstantArgs = enumConstantArgs; + this.containingEnumType = containingEnumType; + } + + // Private constructor for backward compatibility + private FieldInfo(String name, Scope scope, TypeInfo declaredType, + int declarationOffset, boolean resolved, MethodInfo containingMethod, + String documentation, int initStart, int initEnd, int modifiers, + Field reflectionField) { + this(name, scope, declaredType, declarationOffset, resolved, containingMethod, + documentation, initStart, initEnd, modifiers, reflectionField, null, null); } // Factory methods @@ -129,6 +147,44 @@ public final class FieldInfo { TypeInfo type = TypeInfo.fromClass(field.getType()); return new FieldInfo(name, Scope.GLOBAL, type, -1, true, null, null, -1, -1, field.getModifiers(), field); } + + /** + * Create a FieldInfo for an enum constant. + * @param name The constant name (e.g., "NORTH") + * @param type The enum type itself + * @param declOffset The declaration position + * @param args The constructor arguments (or null if no args) + * @param containingEnum The enum type this constant belongs to + */ + public static FieldInfo enumConstant(String name, TypeInfo type, int declOffset, String args, TypeInfo containingEnum) { + // Enum constants are implicitly public static final + int modifiers = Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL; + return new FieldInfo(name, Scope.ENUM_CONSTANT, type, declOffset, true, null, null, -1, -1, + modifiers, null, args, containingEnum); + } + + // ==================== ERROR HANDLING ==================== + + /** + * Check if this is an enum constant. + */ + public boolean isEnumConstant() { + return scope == Scope.ENUM_CONSTANT; + } + + /** + * Get the enum constant args (for enum constants only). + */ + public String getEnumConstantArgs() { + return enumConstantArgs; + } + + /** + * Get the containing enum type (for enum constants only). + */ + public TypeInfo getContainingEnumType() { + return containingEnumType; + } // ==================== ASSIGNMENT MANAGEMENT ==================== @@ -333,11 +389,17 @@ public final class FieldInfo { } switch (scope) { case GLOBAL: + // Static final fields get special highlighting + if (isStatic() && isFinal()) { + return TokenType.STATIC_FINAL_FIELD; + } return TokenType.GLOBAL_FIELD; case LOCAL: return TokenType.LOCAL_FIELD; case PARAMETER: return TokenType.PARAMETER; + case ENUM_CONSTANT: + return TokenType.ENUM_CONSTANT; default: return TokenType.VARIABLE; } diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/hover/TokenHoverInfo.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/hover/TokenHoverInfo.java index 8335ab6a6..246ab8a55 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/hover/TokenHoverInfo.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/hover/TokenHoverInfo.java @@ -2,6 +2,7 @@ package noppes.npcs.client.gui.util.script.interpreter.hover; import noppes.npcs.client.gui.util.script.interpreter.*; import noppes.npcs.client.gui.util.script.interpreter.field.AssignmentInfo; +import noppes.npcs.client.gui.util.script.interpreter.field.EnumConstantInfo; import noppes.npcs.client.gui.util.script.interpreter.field.FieldAccessInfo; import noppes.npcs.client.gui.util.script.interpreter.field.FieldInfo; import noppes.npcs.client.gui.util.script.interpreter.method.MethodCallInfo; @@ -287,7 +288,12 @@ public class TokenHoverInfo { errors.add(scriptType.getErrorMessage()); } } - + + EnumConstantInfo enumConst = findEnumConstantContainingPosition(token); + if (enumConst != null && enumConst.hasError()) { + errors.add(enumConst.getErrorMessage()); + } + if(token.getType() == TokenType.UNDEFINED_VAR) errors.add("Cannot resolve symbol '" + token.getText() + "'"); @@ -393,6 +399,27 @@ public class TokenHoverInfo { return null; } + private EnumConstantInfo findEnumConstantContainingPosition(Token token) { + ScriptLine line = token.getParentLine(); + if (line == null || line.getParent() == null) { + return null; + } + + ScriptDocument doc = line.getParent(); + int tokenStart = token.getGlobalStart(); + + for (EnumConstantInfo enumConst : doc.getAllEnumConstants()) { + int constStart = enumConst.getDeclarationOffset(); + int constEnd = constStart + enumConst.getName().length(); + + // Token is within the enum constant declaration + if (tokenStart >= constStart && tokenStart <= constEnd) { + return enumConst; + } + } + return null; + } + /** * Find the argument that contains the given position. */ diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/method/MethodCallInfo.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/method/MethodCallInfo.java index 8b7668fdf..247c8b584 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/method/MethodCallInfo.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/method/MethodCallInfo.java @@ -73,7 +73,6 @@ public class MethodCallInfo { (valid ? "" : " INVALID: " + errorMessage) + "}"; } } - /** * Validation error type. */ @@ -307,8 +306,8 @@ public class MethodCallInfo { * Sets error information if validation fails. */ public void validate() { - if (resolvedMethod == null) { - if (isConstructor) { + if (isConstructor) { + if (resolvedMethod == null) { // For constructors, check if the type has any constructors at all if (receiverType != null && receiverType.hasConstructors()) { setError(ErrorType.WRONG_ARG_COUNT, @@ -317,8 +316,8 @@ public class MethodCallInfo { setError(ErrorType.UNRESOLVED_METHOD, "Cannot resolve constructor for '" + methodName + "'"); } - } else { - setError(ErrorType.UNRESOLVED_METHOD, "Cannot resolve method '" + methodName + "'"); + } else if (receiverType != null && arguments.size() == resolvedMethod.getParameterCount()) { + validateArgTypeError(); } return; } @@ -343,9 +342,24 @@ public class MethodCallInfo { } // Check each argument type - for (int i = 0; i < actualCount; i++) { + validateArgTypeError(); + + // Check return type compatibility with expected type (e.g., assignment LHS) + // ALREADY CHECKED AT AssignmentInfo LEVEL + if (expectedType != null && resolvedMethod != null) { + TypeInfo returnType = resolvedMethod.getReturnType(); + if (returnType != null && !TypeChecker.isTypeCompatible(expectedType, returnType)) { + // setError(ErrorType.RETURN_TYPE_MISMATCH, + // "Required type: " + expectedType.getSimpleName() + ", Provided: " + returnType.getSimpleName()); + } + } + } + + public void validateArgTypeError() { + // Check each argument type + for (int i = 0; i < arguments.size(); i++) { Argument arg = arguments.get(i); - FieldInfo para = params.get(i); + FieldInfo para = resolvedMethod.getParameters().get(i); TypeInfo argType = arg.getResolvedType(); TypeInfo paramType = para.getDeclaredType(); @@ -360,15 +374,6 @@ public class MethodCallInfo { setArgTypeError(i, "Cannot resolve type of argument '" + arg.getText() + "'"); } } - - // Check return type compatibility with expected type (e.g., assignment LHS) - if (expectedType != null && resolvedMethod != null) { - TypeInfo returnType = resolvedMethod.getReturnType(); - if (returnType != null && !TypeChecker.isTypeCompatible(expectedType, returnType)) { - // setError(ErrorType.RETURN_TYPE_MISMATCH, - // "Required type: " + expectedType.getSimpleName() + ", Provided: " + returnType.getSimpleName()); - } - } } @Override diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/token/Token.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/token/Token.java index 0371ee17f..35f956f54 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/token/Token.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/token/Token.java @@ -230,7 +230,8 @@ public class Token { public boolean isField() { return type == TokenType.GLOBAL_FIELD || type == TokenType.LOCAL_FIELD || - type == TokenType.PARAMETER; + type == TokenType.PARAMETER || type == TokenType.STATIC_FINAL_FIELD || + type == TokenType.ENUM_CONSTANT; } // ==================== RENDERING ==================== diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/token/TokenType.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/token/TokenType.java index 0b7e8e252..c67174491 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/token/TokenType.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/token/TokenType.java @@ -22,23 +22,25 @@ public enum TokenType { // Type declarations and references INTERFACE_DECL(0x55FFFF, 85), // interface names (aqua) ENUM_DECL(0xFF55FF, 85), // enum names (magenta) + ENUM_CONSTANT(0x55FFFF, 84, true, false), // enum constant values (blue, bold+italic) - like IntelliJ CLASS_DECL(0x00AAAA, 85), // class names in declarations IMPORTED_CLASS(0x00AAAA, 75), // imported class usages TYPE_DECL(0x00AAAA, 70), // package paths, type references - + // Methods METHOD_DECL(0x00AA00, 60), // method declarations (green) METHOD_CALL(0x55FF55, 50), // method calls (bright green) - + // Variables and fields UNDEFINED_VAR(0xAA0000, 20), // unresolved variables (dark red) - high priority PARAMETER(0x5555FF, 36), // method parameters (blue) GLOBAL_FIELD(0x55FFFF, 35), // class-level fields (aqua) LOCAL_FIELD(0xFFFF55, 25), // local variables (yellow) - + STATIC_FINAL_FIELD(0xFF55FF, 36, false, true), // static final fields (magenta, italic) + // Literals LITERAL(0x777777, 40), // numeric and boolean literals - + // Default VARIABLE(0xFFFFFF, 30), // generic variables DEFAULT(0xFFFFFF, 0); // default text color (white) @@ -113,7 +115,10 @@ public enum TokenType { case MODIFIER: return '6'; // gold case ENUM_DECL: + case STATIC_FINAL_FIELD: return 'd'; // magenta + case ENUM_CONSTANT: + return '9'; // blue (same as PARAMETER) case INTERFACE_DECL: case GLOBAL_FIELD: return 'b'; // aqua diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/ScriptTypeInfo.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/ScriptTypeInfo.java index a9bcc0333..dece8ba1e 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/ScriptTypeInfo.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/ScriptTypeInfo.java @@ -1,5 +1,6 @@ package noppes.npcs.client.gui.util.script.interpreter.type; +import noppes.npcs.client.gui.util.script.interpreter.field.EnumConstantInfo; import noppes.npcs.client.gui.util.script.interpreter.field.FieldInfo; import noppes.npcs.client.gui.util.script.interpreter.method.MethodInfo; import noppes.npcs.client.gui.util.script.interpreter.method.MethodSignature; @@ -33,6 +34,9 @@ public class ScriptTypeInfo extends TypeInfo { private final List constructors = new ArrayList<>(); // List of constructors private final List innerClasses = new ArrayList<>(); + // Enum constants (for enum types only) - name -> EnumConstantInfo + private final Map enumConstants = new HashMap<>(); + // Parent class reference (for inner class resolution) private ScriptTypeInfo outerClass; @@ -197,6 +201,46 @@ public class ScriptTypeInfo extends TypeInfo { return new HashMap<>(fields); } + // ==================== ENUM CONSTANT MANAGEMENT ==================== + + /** + * Add an enum constant to this enum type. + * Only valid for enum types. + */ + public void addEnumConstant(EnumConstantInfo constant) { + if (isEnum()) { + enumConstants.put(constant.getFieldInfo().getName(), constant); + } + } + + /** + * Check if this enum has a constant with the given name. + */ + public boolean hasEnumConstant(String constantName) { + return enumConstants.containsKey(constantName); + } + + /** + * Get an enum constant by name. + */ + public EnumConstantInfo getEnumConstant(String constantName) { + return enumConstants.get(constantName); + } + + /** + * Get all enum constants. + */ + public Map getEnumConstants() { + return new HashMap<>(enumConstants); + } + + /** + * Check if this is an enum type and has any constants. + */ + public boolean hasEnumConstants() { + return isEnum() && !enumConstants.isEmpty(); + } + // ==================== METHOD MANAGEMENT ==================== public void addMethod(MethodInfo method) {