Skip to content

Commit 0882718

Browse files
committed
Clean up script debugger.
1 parent 4db98e4 commit 0882718

15 files changed

Lines changed: 175 additions & 68 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) 2022 ByteSkript org (Moderocky)
3+
* View the full licence information and permissions:
4+
* https://github.com/Moderocky/ByteSkript/blob/master/LICENSE
5+
*/
6+
7+
package org.byteskript.skript.api;
8+
9+
import mx.kenzie.foundation.Type;
10+
11+
import java.util.Objects;
12+
13+
public interface DebugTypeMeta {
14+
15+
default String debug(Object meta) {
16+
if (meta instanceof Type type) return type.getSimpleName();
17+
return Objects.toString(meta);
18+
}
19+
20+
}

src/main/java/org/byteskript/skript/api/syntax/EventHolder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.lang.reflect.Modifier;
2121
import java.time.Instant;
22+
import java.util.regex.Matcher;
2223

2324
public abstract class EventHolder extends TriggerHolder {
2425
public EventHolder(Library provider, String... patterns) {
@@ -65,7 +66,7 @@ public Type returnType(Context context, Pattern.Match match) {
6566
}
6667

6768
@Override
68-
public Type[] parameters(Context context, Pattern.Match match) {
69+
public Type[] parameters(Context context, Matcher match) {
6970
return new Type[0];
7071
}
7172

src/main/java/org/byteskript/skript/api/syntax/SimpleEntry.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88

99
import mx.kenzie.foundation.Type;
1010
import mx.kenzie.foundation.compiler.State;
11+
import org.byteskript.skript.api.DebugTypeMeta;
1112
import org.byteskript.skript.api.LanguageElement;
1213
import org.byteskript.skript.api.Library;
1314
import org.byteskript.skript.compiler.CommonTypes;
1415
import org.byteskript.skript.compiler.CompileState;
1516
import org.byteskript.skript.compiler.Context;
1617
import org.byteskript.skript.compiler.Pattern;
1718

18-
public abstract class SimpleEntry extends Element {
19+
public abstract class SimpleEntry extends Element implements DebugTypeMeta {
1920
public SimpleEntry(Library provider, LanguageElement type, String... patterns) {
2021
super(provider, type, patterns);
2122
}

src/main/java/org/byteskript/skript/api/syntax/TriggerHolder.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.byteskript.skript.compiler.Pattern;
1616

1717
import java.lang.reflect.Modifier;
18+
import java.util.regex.Matcher;
1819

1920
public abstract class TriggerHolder extends Member {
2021
public TriggerHolder(Library provider, LanguageElement type, String... patterns) {
@@ -27,7 +28,7 @@ public void compile(Context context, Pattern.Match match) {
2728
.addMethod(callSiteName(context, match))
2829
.addModifiers(Modifier.STATIC)
2930
.setReturnType(returnType(context, match))
30-
.addParameter(parameters(context, match));
31+
.addParameter(parameters(context, match.matcher()));
3132
context.setMethod(method, true);
3233
context.setState(CompileState.MEMBER_BODY);
3334
}
@@ -36,6 +37,8 @@ public void compile(Context context, Pattern.Match match) {
3637

3738
public abstract Type returnType(Context context, Pattern.Match match);
3839

39-
public abstract Type[] parameters(Context context, Pattern.Match match);
40+
public Type[] parameters(Context context, Matcher match) {
41+
return new Type[0];
42+
}
4043

4144
}

src/main/java/org/byteskript/skript/compiler/DebugSkriptCompiler.java

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,24 @@
88

99
import mx.kenzie.foundation.Type;
1010
import mx.kenzie.foundation.language.PostCompileClass;
11-
import mx.kenzie.jupiter.stream.OutputStreamController;
1211
import org.byteskript.skript.api.Library;
1312

14-
import java.io.IOException;
1513
import java.io.InputStream;
14+
import java.io.OutputStream;
15+
import java.io.PrintStream;
1616
import java.util.function.Consumer;
1717

1818
public class DebugSkriptCompiler extends SimpleSkriptCompiler {
19-
final OutputStreamController controller;
19+
final PrintStream stream;
2020

21-
public DebugSkriptCompiler(OutputStreamController controller, Library... libraries) {
21+
public DebugSkriptCompiler(OutputStream stream, Library... libraries) {
2222
super(libraries);
23-
this.controller = controller;
23+
this.stream = new PrintStream(stream);
2424
}
2525

2626
@Override
2727
protected void compileLine(String line, FileContext context) {
28-
if (line.isBlank()) {
29-
try {
30-
this.controller.write("\n");
31-
} catch (IOException ignore) {
32-
}
33-
}
28+
if (line.isBlank()) this.stream.print("\n");
3429
final ElementTree tree = this.parseLine(line, context);
3530
if (tree == null) return;
3631
this.debug(tree, context);
@@ -44,21 +39,17 @@ protected void compileLine(String line, FileContext context) {
4439

4540
@Override
4641
public PostCompileClass[] compile(InputStream stream, Type path) {
47-
try {
48-
this.controller.write("\n\n");
49-
this.controller.write("--" + path.internalName());
50-
this.controller.write("\n");
51-
} catch (IOException ignored) {}
42+
this.stream.print("\n");
43+
this.stream.print("--" + path.internalName());
44+
this.stream.print("\n");
5245
return super.compile(stream, path);
5346
}
5447

5548
@Override
5649
public PostCompileClass[] compile(String source, Type path) {
57-
try {
58-
this.controller.write("\n\n");
59-
this.controller.write("--" + path.internalName());
60-
this.controller.write("\n");
61-
} catch (IOException ignored) {}
50+
this.stream.print("\n\n");
51+
this.stream.print("--" + path.internalName());
52+
this.stream.print("\n");
6253
return super.compile(source, path);
6354
}
6455

@@ -68,11 +59,9 @@ protected FileContext createContext(Type path) {
6859
}
6960

7061
protected void debug(ElementTree tree, FileContext context) {
71-
try {
72-
this.controller.write("\n");
73-
for (int i = 0; i < context.lineIndent; i++) this.controller.write("\t");
74-
this.controller.write(tree.toString(context));
75-
} catch (Throwable ignored) {}
62+
this.stream.print("\n");
63+
for (int i = 0; i < context.lineIndent; i++) this.stream.print("\t");
64+
this.stream.print(tree.toString(context));
7665
}
7766

7867
}

src/main/java/org/byteskript/skript/compiler/ElementTree.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88

99
import mx.kenzie.foundation.MethodErasure;
1010
import mx.kenzie.foundation.Type;
11+
import org.byteskript.skript.api.DebugTypeMeta;
1112
import org.byteskript.skript.api.HandlerType;
1213
import org.byteskript.skript.api.SyntaxElement;
1314
import org.byteskript.skript.api.syntax.Literal;
1415
import org.byteskript.skript.api.syntax.Section;
1516
import org.byteskript.skript.api.syntax.TriggerHolder;
1617
import org.byteskript.skript.error.ScriptCompileError;
1718
import org.byteskript.skript.lang.handler.StandardHandlers;
18-
import org.byteskript.skript.lang.syntax.entry.EntryTriggerSection;
19+
import org.byteskript.skript.lang.syntax.function.MemberFunction;
1920
import org.byteskript.skript.lang.syntax.variable.ExprVariable;
2021

2122
import java.util.*;
@@ -186,7 +187,22 @@ public String toString(Context context) {
186187
builder.append(match.matcher().group());
187188
} else if (current instanceof ExprVariable) {
188189
builder.append(match.matcher().group("name"));
189-
} else if (current instanceof TriggerHolder && context != null && context.getLine() == this && context.getMethod() != null) {
190+
} else if (current instanceof DebugTypeMeta debug) {
191+
final Object meta = this.match.meta();
192+
if (meta != null) builder.append(debug.debug(meta));
193+
} else if (current instanceof MemberFunction) {
194+
boolean comma = false;
195+
final String[] names = this.match.meta();
196+
if (names != null) for (final String name : names) {
197+
if (comma) builder.append(", ");
198+
else comma = true;
199+
builder.append(name);
200+
}
201+
} else if (current instanceof TriggerHolder
202+
&& context != null
203+
&& context.getLine() == this
204+
&& context.getMethod() != null
205+
) {
190206
final MethodErasure erasure = context.getMethod().getErasure();
191207
boolean comma = false;
192208
for (final Type type : erasure.parameterTypes()) {
@@ -205,12 +221,12 @@ public String toString(Context context) {
205221
builder.append(')');
206222
if (context == null) return builder.toString();
207223
if (context.isSectionHeader() && context.getLine() == this) builder.append(':');
208-
if (current instanceof EntryTriggerSection && context.getLine() == this && context.getMethod() != null) {
209-
final MethodErasure erasure = context.getMethod().getErasure();
210-
builder.append(" // ");
211-
builder.append(erasure.name());
212-
builder.append(erasure.getDescriptor());
213-
}
224+
// if (current instanceof EntryTriggerSection && context.getLine() == this && context.getMethod() != null) {
225+
// final MethodErasure erasure = context.getMethod().getErasure();
226+
// builder.append(" // ");
227+
// builder.append(erasure.name());
228+
// builder.append(erasure.getDescriptor());
229+
// }
214230
return builder.toString();
215231
}
216232
}

src/main/java/org/byteskript/skript/compiler/SkriptLangSpec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
import org.byteskript.skript.lang.syntax.control.EffectRemove;
3232
import org.byteskript.skript.lang.syntax.control.EffectSet;
3333
import org.byteskript.skript.lang.syntax.dictionary.EffectImportFunction;
34-
import org.byteskript.skript.lang.syntax.dictionary.EffectUseLibrary;
3534
import org.byteskript.skript.lang.syntax.dictionary.EffectImportType;
35+
import org.byteskript.skript.lang.syntax.dictionary.EffectUseLibrary;
3636
import org.byteskript.skript.lang.syntax.dictionary.MemberDictionary;
3737
import org.byteskript.skript.lang.syntax.entry.*;
3838
import org.byteskript.skript.lang.syntax.entry.syntax.*;

src/main/java/org/byteskript/skript/lang/syntax/dictionary/EffectUseLibrary.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package org.byteskript.skript.lang.syntax.dictionary;
88

99
import mx.kenzie.foundation.Type;
10+
import org.byteskript.skript.api.DebugTypeMeta;
1011
import org.byteskript.skript.api.note.Documentation;
1112
import org.byteskript.skript.api.syntax.Effect;
1213
import org.byteskript.skript.compiler.*;
@@ -16,7 +17,6 @@
1617

1718
import java.lang.reflect.Method;
1819
import java.lang.reflect.Modifier;
19-
import java.util.Arrays;
2020

2121
@Documentation(
2222
name = "Use Library",
@@ -35,7 +35,7 @@
3535
"""
3636
}
3737
)
38-
public class EffectUseLibrary extends Effect {
38+
public class EffectUseLibrary extends Effect implements DebugTypeMeta {
3939

4040
public EffectUseLibrary() {
4141
super(SkriptLangSpec.LIBRARY, StandardElements.EFFECT, "use %Library%");
@@ -59,7 +59,8 @@ public void compile(Context context, Pattern.Match match) {
5959
tree.compile = false;
6060
final Type type = tree.match().meta();
6161
final Class<?> cls = type.findClass();
62-
if (cls == null) throw new ScriptCompileError(context.lineNumber(), "Root library '" + type.getSimpleName() + "' was not found.");
62+
if (cls == null)
63+
throw new ScriptCompileError(context.lineNumber(), "Root library '" + type.getSimpleName() + "' was not found.");
6364
for (final Method method : cls.getMethods()) {
6465
if (!Modifier.isStatic(method.getModifiers())) continue;
6566
if (!Modifier.isPublic(method.getModifiers())) continue;

src/main/java/org/byteskript/skript/lang/syntax/function/MemberFunction.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,21 @@ public Pattern.Match match(String thing, Context context) {
5151
if (!thing.startsWith("function ")) return null;
5252
if (!thing.contains("(") || !thing.contains(")")) return null;
5353
final Matcher matcher = PATTERN.matcher(thing);
54-
if (matcher.find() && matcher.group("name") != null && matcher.group("params") != null)
55-
return new Pattern.Match(matcher);
56-
return null;
54+
if (matcher.find() && matcher.group("name") != null && matcher.group("params") != null) {
55+
return new Pattern.Match(matcher, this.parameterNames(context, matcher));
56+
} else return null;
57+
}
58+
59+
public String[] parameterNames(Context context, Matcher matcher) {
60+
final String string = matcher.group("params");
61+
final String[] strings = string.split(",");
62+
final List<String> names = new ArrayList<>();
63+
for (String s : strings) {
64+
final String name = s.trim();
65+
if (name.isEmpty()) throw new ScriptParseError(context.lineNumber(), "Empty function parameter.");
66+
names.add(name);
67+
}
68+
return names.toArray(new String[0]);
5769
}
5870

5971
@Override
@@ -107,8 +119,8 @@ public Type returnType(Context context, Pattern.Match match) {
107119
}
108120

109121
@Override
110-
public Type[] parameters(Context context, Pattern.Match match) {
111-
final String string = match.matcher().group("params");
122+
public Type[] parameters(Context context, Matcher matcher) {
123+
final String string = matcher.group("params");
112124
final String[] strings = string.split(",");
113125
final List<Type> types = new ArrayList<>();
114126
for (String s : strings) {

src/main/java/org/byteskript/skript/lang/syntax/function/MemberFunctionNoArgs.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public Type returnType(Context context, Pattern.Match match) {
6262
}
6363

6464
@Override
65-
public Type[] parameters(Context context, Pattern.Match match) {
65+
public Type[] parameters(Context context, Matcher match) {
6666
return new Type[0];
6767
}
6868
}

0 commit comments

Comments
 (0)