added trace information to thrown exceptions and clean some of the docs

This commit is contained in:
gnembon committed 2021-03-09 04:57:23 -04:00
1 parent d48f78ff7b
commit d72a617290
5 files changed
+111 -67

No files matched your search

@@ -406,6 +406,7 @@ public class Tokenizer implements Iterator<Tokenizer.Token>
public int pos;
public int linepos;
public int lineno;
public static final Token NONE = new Token();
public Token morphedInto(TokenType newType, String newSurface)
{
Token created = new Token();
@@ -14,26 +14,29 @@ import java.util.function.Supplier;
public class ExpressionException extends RuntimeException implements ResolvedException
{
public final Context context;
public final Tokenizer.Token token;
public final List<FunctionValue> stack = new ArrayList<>();
private final Supplier<String> lazyStacktrace;
private String cachedMessage = null;
public ExpressionException(Context c, Expression e, String message)
{
this(c, e, null, message);
this(c, e, Tokenizer.Token.NONE, message);
}
public ExpressionException(Context c, Expression e, Tokenizer.Token t, String message)
{
super("Error");
lazyStacktrace = () -> makeMessage(c, e, t, message);
token = t;
context = c;
}
public ExpressionException(Context c, Expression e, Tokenizer.Token t, String message, List<FunctionValue> stack)
{
super("Error");
this.stack.addAll(stack);
lazyStacktrace = () -> makeMessage(c, e, t, message);
lazyStacktrace = () -> makeMessage(c, e, t, message);
token = t;
context = c;
}
@@ -41,7 +44,8 @@ public class ExpressionException extends RuntimeException implements ResolvedExc
{
super("Error");
this.stack.addAll(stack);
lazyStacktrace = messageSupplier;
lazyStacktrace = () -> makeMessage(c, e, t, messageSupplier.get());
token = t;
context = c;
}
@@ -13,24 +13,24 @@ public class Throwables {
private final String id;
private final Throwables parent;
private static final Map<String, Throwables> byValue = new HashMap<>();
private static final Map<String, Throwables> byId = new HashMap<>();
private static final Map<String, Set<Throwables>> handles = 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_TYPE = register("unknown_poi_type", 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 FILE_READ_EXCEPTION = register("file_read_exception", THROWN_EXCEPTION_TYPE);
public static final Throwables NBT_READ = register("nbt_read_exception", FILE_READ_EXCEPTION);
public static final Throwables JSON_READ = register("json_read_exception", FILE_READ_EXCEPTION);
public static final Throwables USER_DEFINED = register("user_defined_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_TYPE = register("unknown_poi_type", 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 IO_EXCEPTION = register("io_exception", THROWN_EXCEPTION_TYPE);
public static final Throwables NBT_READ = register("nbt_read_error", IO_EXCEPTION);
public static final Throwables JSON_READ = register("json_read_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
@@ -43,7 +43,7 @@ public class Throwables {
public static Throwables register(String id, Throwables parent)
{
Throwables exc = new Throwables(id, parent);
byValue.put(id, exc);
byId.put(id, exc);
Set<Throwables> itHandles = new HashSet<>();
itHandles.add(exc);
while (parent!=null)
@@ -69,7 +69,7 @@ public class Throwables {
public static Throwables getTypeForException(String type)
{
Throwables properType = byValue.get(type);
Throwables properType = byId.get(type);
if (properType == null) throw new InternalExpressionException("Unknown exception type: "+type);
return properType;
}
@@ -8,7 +8,14 @@ import carpet.script.exception.InternalExpressionException;
import carpet.script.exception.ProcessedThrowStatement;
import carpet.script.exception.ThrowStatement;
import carpet.script.exception.Throwables;
import carpet.script.value.ListValue;
import carpet.script.value.MapValue;
import carpet.script.value.NumericValue;
import carpet.script.value.StringValue;
import carpet.script.value.Value;
import com.google.common.collect.ImmutableMap;
import java.util.stream.Collectors;
public class ControlFlow {
public static void apply(Expression expression) // public just to get the javadoc right
@@ -85,6 +92,25 @@ public class ControlFlow {
LazyValue __ = c.getVariable("_");
c.setVariable("_", (__c, __t) -> ret.data.reboundedTo("_"));
LazyValue _trace = c.getVariable("_trace");
c.setVariable("_trace", (__c, __t) -> MapValue.wrap(ImmutableMap.of(
StringValue.of("stack"), ListValue.wrap(ret.stack.stream().map(f -> ListValue.of(
StringValue.of(f.getModule().getName()),
StringValue.of(f.getString()),
NumericValue.of(f.getToken().lineno+1),
NumericValue.of(f.getToken().linepos+1)
)).collect(Collectors.toList())),
StringValue.of("locals"), MapValue.wrap(ret.context.variables.entrySet().stream().collect(Collectors.toMap(
e -> StringValue.of(e.getKey()),
e -> e.getValue().evalValue(ret.context)
))),
StringValue.of("token"), ListValue.of(
StringValue.of(ret.token.surface),
NumericValue.of(ret.token.lineno+1),
NumericValue.of(ret.token.linepos+1)
)
)));
if (lv.size() == 2)
{
@@ -105,7 +131,10 @@ public class ControlFlow {
}
}
c.setVariable("_", __);
if (_trace != null)
c.setVariable("_trace", _trace);
else
c.delVariable("_trace");
if (val == null) // not handled
{
throw ret;