semi-auto cleanup

This commit is contained in:
gnembon committed 2023-02-04 13:26:21 +01:00
1 parent cad80f82c6
commit 8f61271606
99 files changed
+11463 -8007

No files matched your search

@@ -4,7 +4,7 @@ import carpet.script.value.Value;
public class BreakStatement extends ExitStatement
{
public BreakStatement(Value value)
public BreakStatement(final Value value)
{
super(value);
}
@@ -2,25 +2,28 @@ package carpet.script.exception;
import carpet.script.value.FunctionValue;
import carpet.utils.Messenger;
import java.util.List;
import net.minecraft.commands.CommandSourceStack;
public class CarpetExpressionException extends StacklessRuntimeException implements ResolvedException
{
public final List<FunctionValue> stack;
public CarpetExpressionException(String message, List<FunctionValue> stack)
public CarpetExpressionException(final String message, final List<FunctionValue> stack)
{
super(message);
this.stack = stack;
}
public void printStack(CommandSourceStack source)
public void printStack(final CommandSourceStack source)
{
if (stack != null && !stack.isEmpty())
{
for (FunctionValue fun : stack)
for (final FunctionValue fun : stack)
{
Messenger.m(source, "e ... in "+fun.fullName(), "e /"+(fun.getToken().lineno+1)+":"+(fun.getToken().linepos+1));
Messenger.m(source, "e ... in " + fun.fullName(), "e /" + (fun.getToken().lineno + 1) + ":" + (fun.getToken().linepos + 1));
}
}
}
@@ -4,7 +4,7 @@ import carpet.script.value.Value;
public class ContinueStatement extends ExitStatement
{
public ContinueStatement(Value value)
public ContinueStatement(final Value value)
{
super(value);
}
@@ -6,7 +6,8 @@ import carpet.script.value.Value;
public class ExitStatement extends StacklessRuntimeException
{
public final Value retval;
public ExitStatement(Value value)
public ExitStatement(final Value value)
{
retval = value;
}
@@ -19,20 +19,23 @@ public class ExpressionException extends StacklessRuntimeException implements Re
public final List<FunctionValue> stack = new ArrayList<>();
private final Supplier<String> lazyMessage;
private String cachedMessage = null;
public static void prepareForDoom(){
public static void prepareForDoom()
{
Messenger.c("foo bar");
}
public ExpressionException(Context c, Expression e, String message)
public ExpressionException(final Context c, final Expression e, final String message)
{
this(c, e, Tokenizer.Token.NONE, message);
}
public ExpressionException(Context c, Expression e, Tokenizer.Token t, String message)
public ExpressionException(final Context c, final Expression e, final Tokenizer.Token t, final String message)
{
this(c, e, t, message, Collections.emptyList());
}
public ExpressionException(Context c, Expression e, Tokenizer.Token t, String message, List<FunctionValue> stack)
public ExpressionException(final Context c, final Expression e, final Tokenizer.Token t, final String message, final List<FunctionValue> stack)
{
super("Error");
this.stack.addAll(stack);
@@ -41,7 +44,7 @@ public class ExpressionException extends StacklessRuntimeException implements Re
context = c;
}
public ExpressionException(Context c, Expression e, Tokenizer.Token t, Supplier<String> messageSupplier, List<FunctionValue> stack)
public ExpressionException(final Context c, final Expression e, final Tokenizer.Token t, final Supplier<String> messageSupplier, final List<FunctionValue> stack)
{
super("Error");
this.stack.addAll(stack);
@@ -50,13 +53,13 @@ public class ExpressionException extends StacklessRuntimeException implements Re
context = c;
}
private static List<String> makeError(Expression expr, /*Nullable*/Tokenizer.Token token, String errmessage)
private static List<String> makeError(final Expression expr, /*Nullable*/final Tokenizer.Token token, String errmessage)
{
List<String> errMsg = new ArrayList<>();
errmessage += expr.getModuleName() == null?"":(" in "+expr.getModuleName());
final List<String> errMsg = new ArrayList<>();
errmessage += expr.getModuleName() == null ? "" : (" in " + expr.getModuleName());
if (token != null)
{
List<String> snippet = expr.getExpressionSnippet(token);
final List<String> snippet = expr.getExpressionSnippet(token);
errMsg.addAll(snippet);
if (snippet.size() != 1)
@@ -72,11 +75,11 @@ public class ExpressionException extends StacklessRuntimeException implements Re
return errMsg;
}
synchronized static String makeMessage(Context c, Expression e, Tokenizer.Token t, String message) throws ExpressionException
static synchronized String makeMessage(final Context c, final Expression e, final Tokenizer.Token t, final String message) throws ExpressionException
{
if (c.getErrorSnooper() != null)
{
List<String> alternative = c.getErrorSnooper().apply(e, t, c, message);
final List<String> alternative = c.getErrorSnooper().apply(e, t, c, message);
if (alternative != null)
{
return String.join("\n", alternative);
@@ -84,12 +87,13 @@ public class ExpressionException extends StacklessRuntimeException implements Re
}
return String.join("\n", makeError(e, t, message));
}
@Override
public String getMessage() {
public String getMessage()
{
if (cachedMessage == null)
{
cachedMessage = lazyMessage.get();
cachedMessage = lazyMessage.get();
}
return cachedMessage;
}
@@ -2,7 +2,7 @@ package carpet.script.exception;
public class IntegrityException extends RuntimeException
{
public IntegrityException(String message)
public IntegrityException(final String message)
{
super(message);
}
@@ -12,23 +12,25 @@ import java.util.List;
public class InternalExpressionException extends StacklessRuntimeException
{
public List<FunctionValue> stack = new ArrayList<>();
public InternalExpressionException(String message)
public InternalExpressionException(final String message)
{
super(message);
}
/**
* <p>Promotes this simple exception into one with context and extra information.
*
*
* <p>Provides a cleaner way of handling similar exceptions, in this case
* {@link InternalExpressionException} and {@link ThrowStatement}
* @param c Context
* @param e Expression
* {@link InternalExpressionException} and {@link ThrowStatement}
*
* @param c Context
* @param e Expression
* @param token Token
* @return The new {@link ExpressionException} (or {@link ProcessedThrowStatement}),
* depending on the implementation.
* depending on the implementation.
*/
public ExpressionException promote(Context c, Expression e, Tokenizer.Token token)
public ExpressionException promote(final Context c, final Expression e, final Tokenizer.Token token)
{
return new ExpressionException(c, e, token, getMessage(), stack);
}
@@ -2,18 +2,18 @@ package carpet.script.exception;
/**
* A Scarpet exception that indicates that load of the app has failed.
*
* <p>
* Goes up the stack to the point of app load and gets caught there, preventing the app from loading with
* the given message.
*
*/
public class LoadException extends RuntimeException implements ResolvedException {
public LoadException() {
public class LoadException extends RuntimeException implements ResolvedException
{
public LoadException()
{
super();
}
public LoadException(String message) {
public LoadException(final String message)
{
super(message);
}
}
@@ -8,12 +8,14 @@ import carpet.script.Tokenizer.Token;
import carpet.script.value.FunctionValue;
import carpet.script.value.Value;
public class ProcessedThrowStatement extends ExpressionException {
public class ProcessedThrowStatement extends ExpressionException
{
public final Throwables thrownExceptionType;
public final Value data;
public ProcessedThrowStatement(Context c, Expression e, Token token, List<FunctionValue> stack, Throwables thrownExceptionType, Value data) {
super(c, e, token, () -> "Unhandled "+thrownExceptionType.getId()+" exception: "+data.getString(), stack);
public ProcessedThrowStatement(final Context c, final Expression e, final Token token, final List<FunctionValue> stack, final Throwables thrownExceptionType, final Value data)
{
super(c, e, token, () -> "Unhandled " + thrownExceptionType.getId() + " exception: " + data.getString(), stack);
this.thrownExceptionType = thrownExceptionType;
this.data = data;
}
@@ -4,8 +4,7 @@ import carpet.script.value.Value;
public class ReturnStatement extends ExitStatement
{
public ReturnStatement(Value value)
public ReturnStatement(final Value value)
{
super(value);
}
@@ -3,17 +3,21 @@ package carpet.script.exception;
/**
* A type of {@link RuntimeException} that doesn't spend time producing and filling a stacktrace
*/
abstract class StacklessRuntimeException extends RuntimeException {
public StacklessRuntimeException() {
abstract class StacklessRuntimeException extends RuntimeException
{
public StacklessRuntimeException()
{
super();
}
public StacklessRuntimeException(String message) {
public StacklessRuntimeException(final String message)
{
super(message);
}
@Override
public Throwable fillInStackTrace() {
public Throwable fillInStackTrace()
{
return this;
}
}
@@ -10,21 +10,22 @@ public class ThrowStatement extends InternalExpressionException
{
private final Throwables type;
private final Value data;
/**
* Creates a throw exception from a value, and assigns it a specified message.
* <p>To be used when throwing from Scarpet's {@code throw} function
*
* @param data The value to pass
* @param type Exception type
*/
public ThrowStatement(Value data, Throwables type)
public ThrowStatement(final Value data, final Throwables type)
{
super(type.getId());
this.data = data;
this.type = type;
}
public ThrowStatement(Value data, Throwables parent, String subtype)
public ThrowStatement(final Value data, final Throwables parent, final String subtype)
{
super(subtype);
this.data = data;
@@ -35,20 +36,22 @@ public class ThrowStatement extends InternalExpressionException
* Creates a throw exception.<br>
* Conveniently creates a value from the {@code value} String
* to be used easily in Java code
*
* @param message The message to display when not handled
* @param type An {@link Throwables} containing the inheritance data
* for this exception. When throwing from Java,
* those exceptions should be pre-registered.
* @param type An {@link Throwables} containing the inheritance data
* for this exception. When throwing from Java,
* those exceptions should be pre-registered.
*/
public ThrowStatement(String message, Throwables type)
public ThrowStatement(final String message, final Throwables type)
{
super(type.getId());
this.data = StringValue.of(message);
this.type = type;
}
@Override
public ExpressionException promote(Context c, Expression e, Token token) {
public ExpressionException promote(final Context c, final Expression e, final Token token)
{
return new ProcessedThrowStatement(c, e, token, stack, type, data);
}
}
@@ -7,41 +7,43 @@ import java.util.Map;
* This class contains default Scarpet catchable types, as well as their inheritance and
* methods to check whether filters are compatible with those.
*/
public class Throwables {
public class Throwables
{
private final String id;
private final Throwables parent;
private static final Map<String, Throwables> byId = new HashMap<>();
public static final Throwables THROWN_EXCEPTION_TYPE = register("exception", null);
public static final Throwables VALUE_EXCEPTION = register("value_exception", THROWN_EXCEPTION_TYPE);
public static final Throwables UNKNOWN_ITEM = register("unknown_item", VALUE_EXCEPTION);
public static final Throwables UNKNOWN_BLOCK = register("unknown_block", VALUE_EXCEPTION);
public static final Throwables UNKNOWN_BIOME = register("unknown_biome", VALUE_EXCEPTION);
public static final Throwables UNKNOWN_SOUND = register("unknown_sound", VALUE_EXCEPTION);
public static final Throwables UNKNOWN_PARTICLE = register("unknown_particle", VALUE_EXCEPTION);
public static final Throwables UNKNOWN_POI = register("unknown_poi", VALUE_EXCEPTION);
public static final Throwables UNKNOWN_DIMENSION = register("unknown_dimension", VALUE_EXCEPTION);
public static final Throwables UNKNOWN_STRUCTURE = register("unknown_structure", VALUE_EXCEPTION);
public static final Throwables UNKNOWN_CRITERION = register("unknown_criterion", VALUE_EXCEPTION);
public static final Throwables UNKNOWN_SCREEN = register("unknown_screen", VALUE_EXCEPTION);
public static final Throwables IO_EXCEPTION = register("io_exception", THROWN_EXCEPTION_TYPE);
public static final Throwables NBT_ERROR = register("nbt_error", IO_EXCEPTION);
public static final Throwables JSON_ERROR = register("json_error", IO_EXCEPTION);
public static final Throwables B64_ERROR = register("b64_error", IO_EXCEPTION);
public static final Throwables USER_DEFINED = register("user_exception", THROWN_EXCEPTION_TYPE);
public static final Throwables VALUE_EXCEPTION = register("value_exception", THROWN_EXCEPTION_TYPE);
public static final Throwables UNKNOWN_ITEM = register("unknown_item", VALUE_EXCEPTION);
public static final Throwables UNKNOWN_BLOCK = register("unknown_block", VALUE_EXCEPTION);
public static final Throwables UNKNOWN_BIOME = register("unknown_biome", VALUE_EXCEPTION);
public static final Throwables UNKNOWN_SOUND = register("unknown_sound", VALUE_EXCEPTION);
public static final Throwables UNKNOWN_PARTICLE = register("unknown_particle", VALUE_EXCEPTION);
public static final Throwables UNKNOWN_POI = register("unknown_poi", VALUE_EXCEPTION);
public static final Throwables UNKNOWN_DIMENSION = register("unknown_dimension", VALUE_EXCEPTION);
public static final Throwables UNKNOWN_STRUCTURE = register("unknown_structure", VALUE_EXCEPTION);
public static final Throwables UNKNOWN_CRITERION = register("unknown_criterion", VALUE_EXCEPTION);
public static final Throwables UNKNOWN_SCREEN = register("unknown_screen", VALUE_EXCEPTION);
public static final Throwables IO_EXCEPTION = register("io_exception", THROWN_EXCEPTION_TYPE);
public static final Throwables NBT_ERROR = register("nbt_error", IO_EXCEPTION);
public static final Throwables JSON_ERROR = register("json_error", IO_EXCEPTION);
public static final Throwables B64_ERROR = register("b64_error", IO_EXCEPTION);
public static final Throwables USER_DEFINED = register("user_exception", THROWN_EXCEPTION_TYPE);
/**
* Creates an exception and registers it to be used as parent for
* user defined exceptions in Scarpet's throw function.
* <p>Scarpet exceptions should have a top-level parent being {@link Throwables#THROWN_EXCEPTION_TYPE}
* @param id The value for the exception as a {@link String}.
*
* @param id The value for the exception as a {@link String}.
* @param parent The parent of the exception being created, or <code>null</code> if top-level
* @return The created exception
*/
public static Throwables register(String id, Throwables parent)
public static Throwables register(final String id, final Throwables parent)
{
Throwables exc = new Throwables(id, parent);
final Throwables exc = new Throwables(id, parent);
byId.put(id, exc);
return exc;
}
@@ -50,28 +52,33 @@ public class Throwables {
* Creates a new exception.
* <p>Not suitable for creating exceptions that can't be caught.
* Use an {@link InternalExpressionException} for that
*
* @param id The exception's value as a {@link String}
*/
public Throwables(String id, Throwables parent)
public Throwables(final String id, final Throwables parent)
{
this.id = id;
this.parent = parent;
}
public static Throwables getTypeForException(String type)
public static Throwables getTypeForException(final String type)
{
Throwables properType = byId.get(type);
if (properType == null) throw new InternalExpressionException("Unknown exception type: "+type);
final Throwables properType = byId.get(type);
if (properType == null)
{
throw new InternalExpressionException("Unknown exception type: " + type);
}
return properType;
}
/**
* Checks whether the given filter matches an instance of this exception, by checking equality
* with itself and possible parents.
*
* @param filter The type to check against
* @return Whether or not the given value matches this exception's hierarchy
*/
public boolean isRelevantFor(String filter)
public boolean isRelevantFor(final String filter)
{
return (id.equals(filter) || (parent != null && parent.isRelevantFor(filter)));
}
@@ -83,9 +90,11 @@ public class Throwables {
/**
* Returns the throwable type
*
* @return The type of this exception
*/
public String getId() {
public String getId()
{
return id;
}
}