|
| 1 | +package mx.kenzie.skript.lang.syntax.flow.error; |
| 2 | + |
| 3 | +import mx.kenzie.foundation.MethodBuilder; |
| 4 | +import mx.kenzie.foundation.WriteInstruction; |
| 5 | +import mx.kenzie.foundation.compiler.State; |
| 6 | +import mx.kenzie.skript.api.syntax.Section; |
| 7 | +import mx.kenzie.skript.compiler.*; |
| 8 | +import mx.kenzie.skript.compiler.structure.PreVariable; |
| 9 | +import mx.kenzie.skript.compiler.structure.SectionMeta; |
| 10 | +import mx.kenzie.skript.compiler.structure.TryCatchTree; |
| 11 | +import mx.kenzie.skript.error.ScriptCompileError; |
| 12 | +import mx.kenzie.skript.error.ScriptParseError; |
| 13 | +import mx.kenzie.skript.lang.element.StandardElements; |
| 14 | +import mx.kenzie.skript.lang.handler.StandardHandlers; |
| 15 | +import mx.kenzie.skript.lang.syntax.variable.VariableExpression; |
| 16 | +import org.objectweb.asm.Label; |
| 17 | +import org.objectweb.asm.Opcodes; |
| 18 | + |
| 19 | +public class CatchSection extends Section { |
| 20 | + |
| 21 | + public CatchSection() { |
| 22 | + super(SkriptLangSpec.LIBRARY, StandardElements.SECTION, "catch %Variable%"); |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public void preCompile(Context context, Pattern.Match match) throws Throwable { |
| 27 | + final ElementTree holder = context.getLine().nested()[0]; |
| 28 | + if (!(holder.current() instanceof VariableExpression)) |
| 29 | + throw new ScriptParseError(context.lineNumber(), "The error must be a variable: 'catch {varname}'"); |
| 30 | + holder.type = StandardHandlers.SET; |
| 31 | + holder.compile = false; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public void compile(Context context, Pattern.Match match) throws Throwable { |
| 36 | + if (!(context.getTree(context.getSection(1)) instanceof TryCatchTree tree)) |
| 37 | + throw new ScriptCompileError(context.lineNumber(), "Catch used without preceding try-section."); |
| 38 | + final ElementTree holder = context.getLine().nested()[0]; |
| 39 | + if (!(holder.current() instanceof VariableExpression)) |
| 40 | + throw new ScriptParseError(context.lineNumber(), "The error must be a variable: 'catch {varname}'"); |
| 41 | + final Label label = tree.getEnd().use(); |
| 42 | + final Label next = tree.getStartCatch(); |
| 43 | + final MethodBuilder method = context.getMethod(); |
| 44 | + if (method == null) throw new ScriptCompileError(context.lineNumber(), "Try/catch used outside method."); |
| 45 | + context.getMethod().writeCode(((writer, visitor) -> { |
| 46 | + visitor.visitJumpInsn(Opcodes.GOTO, label); |
| 47 | + visitor.visitLabel(next); |
| 48 | + })); |
| 49 | + final int slot = context.slotOf(this.getHolderVariable(context, match)); |
| 50 | + method.writeCode(WriteInstruction.storeObject(slot)); |
| 51 | + context.setState(CompileState.CODE_BODY); |
| 52 | + } |
| 53 | + |
| 54 | + private PreVariable getHolderVariable(Context context, Pattern.Match match) { |
| 55 | + final String pattern = match.groups()[0].trim(); |
| 56 | + assert pattern.startsWith("{") && pattern.endsWith("}"); |
| 57 | + final String name = pattern.substring(1, pattern.length() - 1); |
| 58 | + if (name.charAt(0) == '@' || name.charAt(0) == '_' || name.charAt(0) == '!') |
| 59 | + throw new ScriptCompileError(context.lineNumber(), "Holder variable must be a normal variable: '{var}'"); |
| 60 | + return context.getVariable(name); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Pattern.Match match(String thing, Context context) { |
| 65 | + if (!thing.startsWith("catch ")) return null; |
| 66 | + return super.match(thing, context); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public boolean allowedIn(State state, Context context) { |
| 71 | + return super.allowedIn(state, context) |
| 72 | + && context.getSection() != null |
| 73 | + && context.getMethod() != null; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void onSectionExit(Context context, SectionMeta meta) { |
| 78 | + if (!(context.getTree(context.getSection()) instanceof TryCatchTree tree)) |
| 79 | + throw new ScriptCompileError(context.lineNumber(), "Unable to balance try/catch flow tree."); |
| 80 | + context.setState(CompileState.CODE_BODY); |
| 81 | + tree.close(context); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void compileInline(Context context, Pattern.Match match) throws Throwable { |
| 86 | + throw new ScriptCompileError(context.lineNumber(), "'Catch' must be used as section-header."); |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments