mirror of
https://github.com/GTNewHorizons/CraftTweaker.git
synced 2026-11-18 03:15:14 +00:00
Added projects and basic build scripts. Project for scripting engine, API, Main 1.7 version and one for each mod supported in 1.7.
This commit is contained in:
1 parent
219106f392
commit
38b78ce305
281 files changed
+25090
-49
No files matched your search
@@ -0,0 +1,15 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.util;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public interface MethodCompiler {
|
||||
public void compile(MethodOutput output);
|
||||
}
|
||||
@@ -0,0 +1,617 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.util;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import org.objectweb.asm.ClassVisitor;
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import static org.objectweb.asm.Opcodes.*;
|
||||
import org.objectweb.asm.Type;
|
||||
import org.objectweb.asm.commons.LocalVariablesSorter;
|
||||
import stanhebben.zenscript.type.natives.JavaMethod;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.internal;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class MethodOutput {
|
||||
private final LocalVariablesSorter visitor;
|
||||
|
||||
public MethodOutput(ClassVisitor cls, int access, String name, String descriptor, String signature, String[] exceptions) {
|
||||
MethodVisitor methodVisitor = cls.visitMethod(access, name, descriptor, signature, exceptions);
|
||||
visitor = new LocalVariablesSorter(access, descriptor, methodVisitor);
|
||||
}
|
||||
|
||||
public MethodOutput(LocalVariablesSorter visitor) {
|
||||
this.visitor = visitor;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
visitor.visitCode();
|
||||
}
|
||||
|
||||
public void end() {
|
||||
visitor.visitMaxs(0, 0);
|
||||
visitor.visitEnd();
|
||||
}
|
||||
|
||||
public void label(Label label) {
|
||||
visitor.visitLabel(label);
|
||||
}
|
||||
|
||||
public int local(Type type) {
|
||||
return visitor.newLocal(type);
|
||||
}
|
||||
|
||||
public int local(Class cls) {
|
||||
return visitor.newLocal(Type.getType(cls));
|
||||
}
|
||||
|
||||
public void iConst0() {
|
||||
visitor.visitInsn(ICONST_0);
|
||||
}
|
||||
|
||||
public void iConst1() {
|
||||
visitor.visitInsn(ICONST_1);
|
||||
}
|
||||
|
||||
public void biPush(byte value) {
|
||||
visitor.visitIntInsn(BIPUSH, value);
|
||||
}
|
||||
|
||||
public void siPush(short value) {
|
||||
visitor.visitIntInsn(SIPUSH, value);
|
||||
}
|
||||
|
||||
public void aConstNull() {
|
||||
visitor.visitInsn(ACONST_NULL);
|
||||
}
|
||||
|
||||
public void constant(Object value) {
|
||||
visitor.visitLdcInsn(value);
|
||||
}
|
||||
|
||||
public void pop() {
|
||||
visitor.visitInsn(POP);
|
||||
}
|
||||
|
||||
public void pop(boolean large) {
|
||||
visitor.visitInsn(large ? POP2 : POP);
|
||||
}
|
||||
|
||||
public void dup() {
|
||||
visitor.visitInsn(DUP);
|
||||
}
|
||||
|
||||
public void dup(boolean large) {
|
||||
visitor.visitInsn(large ? DUP2 : DUP);
|
||||
}
|
||||
|
||||
public void dup2() {
|
||||
visitor.visitInsn(DUP2);
|
||||
}
|
||||
|
||||
public void dupX1() {
|
||||
visitor.visitInsn(DUP_X1);
|
||||
}
|
||||
|
||||
public void dupX2() {
|
||||
visitor.visitInsn(DUP_X2);
|
||||
}
|
||||
|
||||
public void dup2X1() {
|
||||
visitor.visitInsn(DUP2_X1);
|
||||
}
|
||||
|
||||
public void dup2X2() {
|
||||
visitor.visitInsn(DUP2_X2);
|
||||
}
|
||||
|
||||
public void store(Type type, int local) {
|
||||
visitor.visitVarInsn(type.getOpcode(ISTORE), local);
|
||||
}
|
||||
|
||||
public void load(Type type, int local) {
|
||||
visitor.visitVarInsn(type.getOpcode(ILOAD), local);
|
||||
}
|
||||
|
||||
public void storeInt(int local) {
|
||||
visitor.visitVarInsn(ISTORE, local);
|
||||
}
|
||||
|
||||
public void loadInt(int local) {
|
||||
visitor.visitVarInsn(ILOAD, local);
|
||||
}
|
||||
|
||||
public void storeObject(int local) {
|
||||
visitor.visitVarInsn(ASTORE, local);
|
||||
}
|
||||
|
||||
public void loadObject(int local) {
|
||||
visitor.visitVarInsn(ALOAD, local);
|
||||
}
|
||||
|
||||
public void arrayLength() {
|
||||
visitor.visitInsn(ARRAYLENGTH);
|
||||
}
|
||||
|
||||
public void arrayLoad(Type type) {
|
||||
visitor.visitInsn(type.getOpcode(IALOAD));
|
||||
}
|
||||
|
||||
public void arrayStore(Type type) {
|
||||
visitor.visitInsn(type.getOpcode(IASTORE));
|
||||
}
|
||||
|
||||
public void newArray(Type componentType) {
|
||||
int sort = componentType.getSort();
|
||||
if (sort == Type.METHOD) {
|
||||
throw new RuntimeException("Unsupported array type: " + componentType);
|
||||
} else if (sort == Type.OBJECT || sort == Type.ARRAY) {
|
||||
visitor.visitTypeInsn(ANEWARRAY, componentType.getInternalName());
|
||||
} else {
|
||||
int type = 0;
|
||||
switch (sort) {
|
||||
case Type.BOOLEAN: type = Opcodes.T_BOOLEAN; break;
|
||||
case Type.BYTE: type = Opcodes.T_BYTE; break;
|
||||
case Type.SHORT: type = Opcodes.T_SHORT; break;
|
||||
case Type.INT: type = Opcodes.T_INT; break;
|
||||
case Type.LONG: type = Opcodes.T_LONG; break;
|
||||
case Type.FLOAT: type = Opcodes.T_FLOAT; break;
|
||||
case Type.DOUBLE: type = Opcodes.T_DOUBLE; break;
|
||||
default:
|
||||
throw new RuntimeException("Unsupported array type: " + componentType);
|
||||
}
|
||||
visitor.visitIntInsn(NEWARRAY, type);
|
||||
}
|
||||
}
|
||||
|
||||
public void newArray(Class componentType) {
|
||||
visitor.visitTypeInsn(NEWARRAY, internal(componentType));
|
||||
}
|
||||
|
||||
public void checkCast(Class newClass) {
|
||||
visitor.visitTypeInsn(CHECKCAST, signature(newClass));
|
||||
}
|
||||
|
||||
public void iNeg() {
|
||||
visitor.visitInsn(INEG);
|
||||
}
|
||||
|
||||
public void iAdd() {
|
||||
visitor.visitInsn(IADD);
|
||||
}
|
||||
|
||||
public void iSub() {
|
||||
visitor.visitInsn(ISUB);
|
||||
}
|
||||
|
||||
public void iMul() {
|
||||
visitor.visitInsn(IMUL);
|
||||
}
|
||||
|
||||
public void iDiv() {
|
||||
visitor.visitInsn(IDIV);
|
||||
}
|
||||
|
||||
public void iRem() {
|
||||
visitor.visitInsn(IREM);
|
||||
}
|
||||
|
||||
public void iAnd() {
|
||||
visitor.visitInsn(IAND);
|
||||
}
|
||||
|
||||
public void iOr() {
|
||||
visitor.visitInsn(IOR);
|
||||
}
|
||||
|
||||
public void iXor() {
|
||||
visitor.visitInsn(IXOR);
|
||||
}
|
||||
|
||||
public void iNot() {
|
||||
visitor.visitInsn(ICONST_M1);
|
||||
visitor.visitInsn(IXOR);
|
||||
}
|
||||
|
||||
public void lNeg() {
|
||||
visitor.visitInsn(LNEG);
|
||||
}
|
||||
|
||||
public void lAdd() {
|
||||
visitor.visitInsn(LADD);
|
||||
}
|
||||
|
||||
public void lSub() {
|
||||
visitor.visitInsn(LSUB);
|
||||
}
|
||||
|
||||
public void lMul() {
|
||||
visitor.visitInsn(LMUL);
|
||||
}
|
||||
|
||||
public void lDiv() {
|
||||
visitor.visitInsn(LDIV);
|
||||
}
|
||||
|
||||
public void lRem() {
|
||||
visitor.visitInsn(LREM);
|
||||
}
|
||||
|
||||
public void lAnd() {
|
||||
visitor.visitInsn(LAND);
|
||||
}
|
||||
|
||||
public void lOr() {
|
||||
visitor.visitInsn(LOR);
|
||||
}
|
||||
|
||||
public void lXor() {
|
||||
visitor.visitInsn(LXOR);
|
||||
}
|
||||
|
||||
public void lNot() {
|
||||
constant((long) -1);
|
||||
lXor();
|
||||
}
|
||||
|
||||
public void fNeg() {
|
||||
visitor.visitInsn(FNEG);
|
||||
}
|
||||
|
||||
public void fAdd() {
|
||||
visitor.visitInsn(FADD);
|
||||
}
|
||||
|
||||
public void fSub() {
|
||||
visitor.visitInsn(FSUB);
|
||||
}
|
||||
|
||||
public void fMul() {
|
||||
visitor.visitInsn(FMUL);
|
||||
}
|
||||
|
||||
public void fDiv() {
|
||||
visitor.visitInsn(FDIV);
|
||||
}
|
||||
|
||||
public void fRem() {
|
||||
visitor.visitInsn(FREM);
|
||||
}
|
||||
|
||||
public void dNeg() {
|
||||
visitor.visitInsn(DNEG);
|
||||
}
|
||||
|
||||
public void dAdd() {
|
||||
visitor.visitInsn(DADD);
|
||||
}
|
||||
|
||||
public void dSub() {
|
||||
visitor.visitInsn(DSUB);
|
||||
}
|
||||
|
||||
public void dMul() {
|
||||
visitor.visitInsn(DMUL);
|
||||
}
|
||||
|
||||
public void dDiv() {
|
||||
visitor.visitInsn(DDIV);
|
||||
}
|
||||
|
||||
public void dRem() {
|
||||
visitor.visitInsn(DREM);
|
||||
}
|
||||
|
||||
public void iinc(int local) {
|
||||
visitor.visitIincInsn(local, 1);
|
||||
}
|
||||
|
||||
public void iinc(int local, int increment) {
|
||||
visitor.visitIincInsn(local, increment);
|
||||
}
|
||||
|
||||
public void i2b() {
|
||||
visitor.visitInsn(I2B);
|
||||
}
|
||||
|
||||
public void i2s() {
|
||||
visitor.visitInsn(I2S);
|
||||
}
|
||||
|
||||
public void i2l() {
|
||||
visitor.visitInsn(I2L);
|
||||
}
|
||||
|
||||
public void i2f() {
|
||||
visitor.visitInsn(I2F);
|
||||
}
|
||||
|
||||
public void i2d() {
|
||||
visitor.visitInsn(I2D);
|
||||
}
|
||||
|
||||
public void l2i() {
|
||||
visitor.visitInsn(L2I);
|
||||
}
|
||||
|
||||
public void l2f() {
|
||||
visitor.visitInsn(L2F);
|
||||
}
|
||||
|
||||
public void l2d() {
|
||||
visitor.visitInsn(L2D);
|
||||
}
|
||||
|
||||
public void f2i() {
|
||||
visitor.visitInsn(F2I);
|
||||
}
|
||||
|
||||
public void f2l() {
|
||||
visitor.visitInsn(F2L);
|
||||
}
|
||||
|
||||
public void f2d() {
|
||||
visitor.visitInsn(F2D);
|
||||
}
|
||||
|
||||
public void d2i() {
|
||||
visitor.visitInsn(D2I);
|
||||
}
|
||||
|
||||
public void d2l() {
|
||||
visitor.visitInsn(D2L);
|
||||
}
|
||||
|
||||
public void d2f() {
|
||||
visitor.visitInsn(D2F);
|
||||
}
|
||||
|
||||
public void lCmp() {
|
||||
visitor.visitInsn(LCMP);
|
||||
}
|
||||
|
||||
public void fCmp() {
|
||||
visitor.visitInsn(FCMPL);
|
||||
}
|
||||
|
||||
public void dCmp() {
|
||||
visitor.visitInsn(DCMPL);
|
||||
}
|
||||
|
||||
public void invokeStatic(String owner, String name, String descriptor) {
|
||||
visitor.visitMethodInsn(INVOKESTATIC, owner, name, descriptor);
|
||||
}
|
||||
|
||||
public void invokeStatic(Class owner, String name, Class result, Class... arguments) {
|
||||
StringBuilder descriptor = new StringBuilder();
|
||||
descriptor.append('(');
|
||||
for (Class argument : arguments) {
|
||||
descriptor.append(signature(argument));
|
||||
}
|
||||
descriptor.append(')');
|
||||
descriptor.append(result == null ? 'V' : signature(result));
|
||||
visitor.visitMethodInsn(INVOKESTATIC, internal(owner), name, descriptor.toString());
|
||||
}
|
||||
|
||||
public void invokeStatic(JavaMethod method) {
|
||||
StringBuilder descriptor = new StringBuilder();
|
||||
descriptor.append('(');
|
||||
for (Class argument : method.getMethod().getParameterTypes()) {
|
||||
descriptor.append(signature(argument));
|
||||
}
|
||||
descriptor.append(')');
|
||||
descriptor.append(signature(method.getMethod().getReturnType()));
|
||||
visitor.visitMethodInsn(INVOKESTATIC, internal(method.getOwner()), method.getMethod().getName(), descriptor.toString());
|
||||
}
|
||||
|
||||
public void invokeSpecial(String owner, String name, String descriptor) {
|
||||
visitor.visitMethodInsn(INVOKESPECIAL, owner, name, descriptor);
|
||||
}
|
||||
|
||||
public void invoke(Class owner, String name, Class result, Class... arguments) {
|
||||
if (owner.isInterface()) {
|
||||
invokeInterface(owner, name, result, arguments);
|
||||
} else {
|
||||
invokeVirtual(owner, name, result, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
public void invoke(JavaMethod method) {
|
||||
if (method.getOwner().isInterface()) {
|
||||
invokeInterface(
|
||||
method.getOwner(),
|
||||
method.getMethod().getName(),
|
||||
method.getMethod().getReturnType(),
|
||||
method.getMethod().getParameterTypes());
|
||||
} else {
|
||||
invokeVirtual(
|
||||
method.getOwner(),
|
||||
method.getMethod().getName(),
|
||||
method.getMethod().getReturnType(),
|
||||
method.getMethod().getParameterTypes());
|
||||
}
|
||||
}
|
||||
|
||||
public void invokeVirtual(String owner, String name, String descriptor) {
|
||||
visitor.visitMethodInsn(INVOKEVIRTUAL, owner, name, descriptor);
|
||||
}
|
||||
|
||||
public void invokeVirtual(Class owner, String name, Class result, Class... arguments) {
|
||||
StringBuilder descriptor = new StringBuilder();
|
||||
descriptor.append('(');
|
||||
for (Class argument : arguments) {
|
||||
descriptor.append(signature(argument));
|
||||
}
|
||||
descriptor.append(')');
|
||||
descriptor.append(result == null ? 'V' : signature(result));
|
||||
visitor.visitMethodInsn(INVOKEVIRTUAL, internal(owner), name, descriptor.toString());
|
||||
}
|
||||
|
||||
public void invokeInterface(String owner, String name, String descriptor) {
|
||||
visitor.visitMethodInsn(INVOKEINTERFACE, owner, name, descriptor);
|
||||
}
|
||||
|
||||
public void invokeInterface(Class owner, String name, Class result, Class... arguments) {
|
||||
StringBuilder descriptor = new StringBuilder();
|
||||
descriptor.append('(');
|
||||
for (Class argument : arguments) {
|
||||
descriptor.append(signature(argument));
|
||||
}
|
||||
descriptor.append(')');
|
||||
descriptor.append(result == null ? 'V' : signature(result));
|
||||
visitor.visitMethodInsn(INVOKEINTERFACE, internal(owner), name, descriptor.toString());
|
||||
}
|
||||
|
||||
public void newObject(Class type) {
|
||||
visitor.visitTypeInsn(NEW, internal(type));
|
||||
}
|
||||
|
||||
public void newObject(String type) {
|
||||
visitor.visitTypeInsn(NEW, type);
|
||||
}
|
||||
|
||||
public void construct(Class type, Class... arguments) {
|
||||
StringBuilder descriptor = new StringBuilder();
|
||||
descriptor.append('(');
|
||||
for (Class argument : arguments) {
|
||||
descriptor.append(signature(argument));
|
||||
}
|
||||
descriptor.append(")V");
|
||||
visitor.visitMethodInsn(INVOKESPECIAL, internal(type), "<init>", descriptor.toString());
|
||||
}
|
||||
|
||||
public void construct(String type, String... arguments) {
|
||||
StringBuilder descriptor = new StringBuilder();
|
||||
descriptor.append('(');
|
||||
for (String argument : arguments) {
|
||||
descriptor.append(argument);
|
||||
}
|
||||
descriptor.append(")V");
|
||||
visitor.visitMethodInsn(INVOKESPECIAL, type, "<init>", descriptor.toString());
|
||||
}
|
||||
|
||||
public void goTo(Label lbl) {
|
||||
visitor.visitJumpInsn(GOTO, lbl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Jump if TOS == 0.
|
||||
*
|
||||
* @param lbl target label
|
||||
*/
|
||||
public void ifEQ(Label lbl) {
|
||||
visitor.visitJumpInsn(IFEQ, lbl);
|
||||
}
|
||||
|
||||
public void ifNE(Label lbl) {
|
||||
visitor.visitJumpInsn(IFNE, lbl);
|
||||
}
|
||||
|
||||
public void ifLT(Label lbl) {
|
||||
visitor.visitJumpInsn(IFLT, lbl);
|
||||
}
|
||||
|
||||
public void ifGT(Label lbl) {
|
||||
visitor.visitJumpInsn(IFGT, lbl);
|
||||
}
|
||||
|
||||
public void ifGE(Label lbl) {
|
||||
visitor.visitJumpInsn(IFGE, lbl);
|
||||
}
|
||||
|
||||
public void ifLE(Label lbl) {
|
||||
visitor.visitJumpInsn(IFLE, lbl);
|
||||
}
|
||||
|
||||
public void ifICmpLE(Label lbl) {
|
||||
visitor.visitJumpInsn(IF_ICMPLE, lbl);
|
||||
}
|
||||
|
||||
public void ifICmpGE(Label lbl) {
|
||||
visitor.visitJumpInsn(IF_ICMPGE, lbl);
|
||||
}
|
||||
|
||||
public void ifICmpEQ(Label lbl) {
|
||||
visitor.visitJumpInsn(IF_ICMPEQ, lbl);
|
||||
}
|
||||
|
||||
public void ifICmpNE(Label lbl) {
|
||||
visitor.visitJumpInsn(IF_ICMPNE, lbl);
|
||||
}
|
||||
|
||||
public void ifICmpGT(Label lbl) {
|
||||
visitor.visitJumpInsn(IF_ICMPGT, lbl);
|
||||
}
|
||||
|
||||
public void ifICmpLT(Label lbl) {
|
||||
visitor.visitJumpInsn(IF_ICMPLT, lbl);
|
||||
}
|
||||
|
||||
public void ifNull(Label lbl) {
|
||||
visitor.visitJumpInsn(IFNULL, lbl);
|
||||
}
|
||||
|
||||
public void ifNonNull(Label lbl) {
|
||||
visitor.visitJumpInsn(IFNONNULL, lbl);
|
||||
}
|
||||
|
||||
public void ret() {
|
||||
visitor.visitInsn(RETURN);
|
||||
}
|
||||
|
||||
public void returnType(Type type) {
|
||||
visitor.visitInsn(type.getOpcode(IRETURN));
|
||||
}
|
||||
|
||||
public void returnInt() {
|
||||
visitor.visitInsn(IRETURN);
|
||||
}
|
||||
|
||||
public void returnObject() {
|
||||
visitor.visitInsn(ARETURN);
|
||||
}
|
||||
|
||||
public void getField(String owner, String name, String descriptor) {
|
||||
visitor.visitFieldInsn(GETFIELD, owner, name, descriptor);
|
||||
}
|
||||
|
||||
public void getField(Class owner, String name, Class descriptor) {
|
||||
visitor.visitFieldInsn(GETFIELD, internal(owner), name, signature(descriptor));
|
||||
}
|
||||
|
||||
public void putField(String owner, String name, String descriptor) {
|
||||
visitor.visitFieldInsn(PUTFIELD, owner, name, descriptor);
|
||||
}
|
||||
|
||||
public void putField(Class owner, String name, Class descriptor) {
|
||||
visitor.visitFieldInsn(PUTFIELD, internal(owner), name, signature(descriptor));
|
||||
}
|
||||
|
||||
public void getStaticField(Class owner, Field field) {
|
||||
visitor.visitFieldInsn(GETSTATIC, internal(owner), field.getName(), signature(field.getType()));
|
||||
}
|
||||
|
||||
public void putStaticField(Class owner, Field field) {
|
||||
visitor.visitFieldInsn(PUTSTATIC, internal(owner), field.getName(), signature(field.getType()));
|
||||
}
|
||||
|
||||
public void aThrow() {
|
||||
visitor.visitInsn(ATHROW);
|
||||
}
|
||||
|
||||
public void position(ZenPosition position) {
|
||||
Label label = new Label();
|
||||
visitor.visitLabel(label);
|
||||
visitor.visitLineNumber(position.getLine(), label);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,451 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class StringUtil {
|
||||
private StringUtil() {}
|
||||
|
||||
public static String join(String[] values, String separator) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
boolean first = true;
|
||||
for (String value : values) {
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
result.append('.');
|
||||
}
|
||||
result.append(value);
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public static String join(Iterable<String> values, String separator) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
boolean first = true;
|
||||
for (String value : values) {
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
result.append('.');
|
||||
}
|
||||
result.append(value);
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits a string in parts, given a specified delimiter.
|
||||
*
|
||||
* @param value string to be split
|
||||
* @param delimiter delimiter
|
||||
* @return
|
||||
*/
|
||||
public static String[] split(String value, char delimiter) {
|
||||
if (value == null) return null;
|
||||
|
||||
ArrayList<String> result = new ArrayList<String>();
|
||||
int start = 0;
|
||||
outer: for (int i = 0; i < value.length(); i++) {
|
||||
if (value.charAt(i) == delimiter) {
|
||||
result.add(value.substring(start, i));
|
||||
start = i + 1;
|
||||
}
|
||||
}
|
||||
result.add(value.substring(start));
|
||||
return result.toArray(new String[result.size()]);
|
||||
}
|
||||
|
||||
// ########################
|
||||
// ### String utilities ###
|
||||
// ########################
|
||||
|
||||
/**
|
||||
*
|
||||
* unescape_perl_string()
|
||||
*
|
||||
* Tom Christiansen <[email protected]> Sun Nov 28 12:55:24 MST 2010
|
||||
*
|
||||
* It's completely ridiculous that there's no standard unescape_java_string
|
||||
* function. Since I have to do the damn thing myself, I might as well make
|
||||
* it halfway useful by supporting things Java was too stupid to consider in
|
||||
* strings:
|
||||
*
|
||||
* => "?" items are additions to Java string escapes but normal in Java
|
||||
* regexes
|
||||
*
|
||||
* => "!" items are also additions to Java regex escapes
|
||||
*
|
||||
* Standard singletons: ?\a ?\e \f \n \r \t
|
||||
*
|
||||
* NB: \b is unsupported as backspace so it can pass-through to the regex
|
||||
* translator untouched; I refuse to make anyone doublebackslash it as
|
||||
* doublebackslashing is a Java idiocy I desperately wish would die out.
|
||||
* There are plenty of other ways to write it:
|
||||
*
|
||||
* \cH, \12, \012, \x08 \x{8}, \u0008, \U00000008
|
||||
*
|
||||
* Octal escapes: \0 \0N \0NN \N \NN \NNN Can range up to !\777 not \377
|
||||
*
|
||||
* TODO: add !\o{NNNNN} last Unicode is 4177777 maxint is 37777777777
|
||||
*
|
||||
* Control chars: ?\cX Means: ord(X) ^ ord('@')
|
||||
*
|
||||
* Old hex escapes: \xXX unbraced must be 2 xdigits
|
||||
*
|
||||
* Perl hex escapes: !\x{XXX} braced may be 1-8 xdigits NB: proper Unicode
|
||||
* never needs more than 6, as highest valid codepoint is 0x10FFFF, not
|
||||
* maxint 0xFFFFFFFF
|
||||
*
|
||||
* Lame Java escape: \[IDIOT JAVA PREPROCESSOR]uXXXX must be exactly 4
|
||||
* xdigits;
|
||||
*
|
||||
* I can't write XXXX in this comment where it belongs because the damned
|
||||
* Java Preprocessor can't mind its own business. Idiots!
|
||||
*
|
||||
* Lame Python escape: !\UXXXXXXXX must be exactly 8 xdigits
|
||||
*
|
||||
* TODO: Perl translation escapes: \Q \U \L \E \[IDIOT JAVA PREPROCESSOR]u
|
||||
* \l These are not so important to cover if you're passing the result to
|
||||
* Pattern.compile(), since it handles them for you further downstream. Hm,
|
||||
* what about \[IDIOT JAVA PREPROCESSOR]u?
|
||||
*
|
||||
* @param oldstr
|
||||
* @return
|
||||
*/
|
||||
public static String unescapeString(String oldstr) {
|
||||
if (oldstr.charAt(0) != '"' || oldstr.charAt(oldstr.length() - 1) != '"') {
|
||||
// TODO: error
|
||||
//throw new TweakerExecuteException("Not a valid string constant: "
|
||||
// + oldstr);
|
||||
}
|
||||
oldstr = oldstr.substring(1, oldstr.length() - 1);
|
||||
|
||||
/*
|
||||
* In contrast to fixing Java's broken regex charclasses, this one need
|
||||
* be no bigger, as unescaping shrinks the string here, where in the
|
||||
* other one, it grows it.
|
||||
*/
|
||||
|
||||
StringBuilder newstr = new StringBuilder(oldstr.length());
|
||||
|
||||
boolean saw_backslash = false;
|
||||
|
||||
for (int i = 0; i < oldstr.length(); i++) {
|
||||
int cp = oldstr.codePointAt(i);
|
||||
if (oldstr.codePointAt(i) > Character.MAX_VALUE) {
|
||||
i++;
|
||||
/**** WE HATES UTF-16! WE HATES IT FOREVERSES!!! ****/
|
||||
}
|
||||
|
||||
if (!saw_backslash) {
|
||||
if (cp == '\\') {
|
||||
saw_backslash = true;
|
||||
} else {
|
||||
newstr.append(Character.toChars(cp));
|
||||
}
|
||||
continue; /* switch */
|
||||
}
|
||||
|
||||
if (cp == '\\') {
|
||||
saw_backslash = false;
|
||||
newstr.append('\\');
|
||||
newstr.append('\\');
|
||||
continue; /* switch */
|
||||
}
|
||||
|
||||
switch (cp) {
|
||||
|
||||
case 'r':
|
||||
newstr.append('\r');
|
||||
break; /* switch */
|
||||
|
||||
case 'n':
|
||||
newstr.append('\n');
|
||||
break; /* switch */
|
||||
|
||||
case 'f':
|
||||
newstr.append('\f');
|
||||
break; /* switch */
|
||||
|
||||
/* PASS a \b THROUGH!! */
|
||||
case 'b':
|
||||
newstr.append("\\b");
|
||||
break; /* switch */
|
||||
|
||||
case 't':
|
||||
newstr.append('\t');
|
||||
break; /* switch */
|
||||
|
||||
case 'a':
|
||||
newstr.append('\007');
|
||||
break; /* switch */
|
||||
|
||||
case 'e':
|
||||
newstr.append('\033');
|
||||
break; /* switch */
|
||||
|
||||
/*
|
||||
* A "control" character is what you get when you xor its codepoint
|
||||
* with '@'==64. This only makes sense for ASCII, and may not yield
|
||||
* a "control" character after all.
|
||||
*
|
||||
* Strange but true: "\c{" is ";", "\c}" is "=", etc.
|
||||
*/
|
||||
case 'c': {
|
||||
if (++i == oldstr.length()) {
|
||||
// TODO: error
|
||||
//throw new TweakerExecuteException("trailing \\c");
|
||||
}
|
||||
cp = oldstr.codePointAt(i);
|
||||
/*
|
||||
* don't need to grok surrogates, as next line blows them up
|
||||
*/
|
||||
if (cp > 0x7f) {
|
||||
// TODO: error
|
||||
//throw new TweakerExecuteException(
|
||||
// "expected ASCII after \\c");
|
||||
}
|
||||
newstr.append(Character.toChars(cp ^ 64));
|
||||
break; /* switch */
|
||||
}
|
||||
|
||||
case '8':
|
||||
case '9':
|
||||
// TODO: error
|
||||
//throw new TweakerExecuteException("illegal octal digit");
|
||||
/* NOTREACHED */
|
||||
|
||||
/*
|
||||
* may be 0 to 2 octal digits following this one so back up one
|
||||
* for fallthrough to next case; unread this digit and fall
|
||||
* through to next case.
|
||||
*/
|
||||
case '1':
|
||||
case '2':
|
||||
case '3':
|
||||
case '4':
|
||||
case '5':
|
||||
case '6':
|
||||
case '7':
|
||||
--i;
|
||||
/* FALLTHROUGH */
|
||||
|
||||
/*
|
||||
* Can have 0, 1, or 2 octal digits following a 0 this permits
|
||||
* larger values than octal 377, up to octal 777.
|
||||
*/
|
||||
case '0': {
|
||||
if (i + 1 == oldstr.length()) {
|
||||
/* found \0 at end of string */
|
||||
newstr.append(Character.toChars(0));
|
||||
break; /* switch */
|
||||
}
|
||||
i++;
|
||||
int digits = 0;
|
||||
int j;
|
||||
for (j = 0; j <= 2; j++) {
|
||||
if (i + j == oldstr.length()) {
|
||||
break; /* for */
|
||||
}
|
||||
/* safe because will unread surrogate */
|
||||
int ch = oldstr.charAt(i + j);
|
||||
if (ch < '0' || ch > '7') {
|
||||
break; /* for */
|
||||
}
|
||||
digits++;
|
||||
}
|
||||
if (digits == 0) {
|
||||
--i;
|
||||
newstr.append('\0');
|
||||
break; /* switch */
|
||||
}
|
||||
int value = 0;
|
||||
try {
|
||||
value = Integer
|
||||
.parseInt(oldstr.substring(i, i + digits), 8);
|
||||
} catch (NumberFormatException nfe) {
|
||||
// TODO: error
|
||||
//throw new TweakerExecuteException(
|
||||
// "invalid octal value for \\0 escape");
|
||||
}
|
||||
newstr.append(Character.toChars(value));
|
||||
i += digits - 1;
|
||||
break; /* switch */
|
||||
} /* end case '0' */
|
||||
|
||||
case 'x': {
|
||||
if (i + 2 > oldstr.length()) {
|
||||
// TODO: error
|
||||
//throw new TweakerExecuteException(
|
||||
// "string too short for \\x escape");
|
||||
}
|
||||
i++;
|
||||
boolean saw_brace = false;
|
||||
if (oldstr.charAt(i) == '{') {
|
||||
/* ^^^^^^ ok to ignore surrogates here */
|
||||
i++;
|
||||
saw_brace = true;
|
||||
}
|
||||
int j;
|
||||
for (j = 0; j < 8; j++) {
|
||||
|
||||
if (!saw_brace && j == 2) {
|
||||
break; /* for */
|
||||
}
|
||||
|
||||
/*
|
||||
* ASCII test also catches surrogates
|
||||
*/
|
||||
int ch = oldstr.charAt(i + j);
|
||||
if (ch > 127) {
|
||||
// TODO: error
|
||||
//throw new TweakerExecuteException(
|
||||
// "illegal non-ASCII hex digit in \\x escape");
|
||||
}
|
||||
|
||||
if (saw_brace && ch == '}') {
|
||||
break; /* for */
|
||||
}
|
||||
|
||||
if (!((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F'))) {
|
||||
// TODO: error
|
||||
//throw new TweakerExecuteException(String.format(
|
||||
// "illegal hex digit #%d '%c' in \\x", ch, ch));
|
||||
}
|
||||
|
||||
}
|
||||
if (j == 0) {
|
||||
// TODO: error
|
||||
//throw new TweakerExecuteException(
|
||||
// "empty braces in \\x{} escape");
|
||||
}
|
||||
int value = 0;
|
||||
try {
|
||||
value = Integer.parseInt(oldstr.substring(i, i + j), 16);
|
||||
} catch (NumberFormatException nfe) {
|
||||
// TODO: error
|
||||
//throw new TweakerExecuteException(
|
||||
// "invalid hex value for \\x escape");
|
||||
}
|
||||
newstr.append(Character.toChars(value));
|
||||
if (saw_brace) {
|
||||
j++;
|
||||
}
|
||||
i += j - 1;
|
||||
break; /* switch */
|
||||
}
|
||||
|
||||
case 'u': {
|
||||
if (i + 4 > oldstr.length()) {
|
||||
// TODO: error
|
||||
//throw new TweakerExecuteException(
|
||||
// "string too short for \\u escape");
|
||||
}
|
||||
i++;
|
||||
int j;
|
||||
for (j = 0; j < 4; j++) {
|
||||
/* this also handles the surrogate issue */
|
||||
if (oldstr.charAt(i + j) > 127) {
|
||||
// TODO: error
|
||||
//throw new TweakerExecuteException(
|
||||
// "illegal non-ASCII hex digit in \\u escape");
|
||||
}
|
||||
}
|
||||
int value = 0;
|
||||
try {
|
||||
value = Integer.parseInt(oldstr.substring(i, i + j), 16);
|
||||
} catch (NumberFormatException nfe) {
|
||||
// TODO: error
|
||||
//throw new TweakerExecuteException(
|
||||
// "invalid hex value for \\u escape");
|
||||
}
|
||||
newstr.append(Character.toChars(value));
|
||||
i += j - 1;
|
||||
break; /* switch */
|
||||
}
|
||||
|
||||
case 'U': {
|
||||
if (i + 8 > oldstr.length()) {
|
||||
// TODO: error
|
||||
//throw new TweakerExecuteException(
|
||||
// "string too short for \\U escape");
|
||||
}
|
||||
i++;
|
||||
int j;
|
||||
for (j = 0; j < 8; j++) {
|
||||
/* this also handles the surrogate issue */
|
||||
if (oldstr.charAt(i + j) > 127) {
|
||||
// TODO: error
|
||||
//throw new TweakerExecuteException(
|
||||
// "illegal non-ASCII hex digit in \\U escape");
|
||||
}
|
||||
}
|
||||
int value = 0;
|
||||
try {
|
||||
value = Integer.parseInt(oldstr.substring(i, i + j), 16);
|
||||
} catch (NumberFormatException nfe) {
|
||||
// TODO: error
|
||||
//throw new TweakerExecuteException(
|
||||
// "invalid hex value for \\U escape");
|
||||
}
|
||||
newstr.append(Character.toChars(value));
|
||||
i += j - 1;
|
||||
break; /* switch */
|
||||
}
|
||||
|
||||
default:
|
||||
newstr.append('\\');
|
||||
newstr.append(Character.toChars(cp));
|
||||
/*
|
||||
* say(String.format(
|
||||
* "DEFAULT unrecognized escape %c passed through", cp));
|
||||
*/
|
||||
break; /* switch */
|
||||
|
||||
}
|
||||
saw_backslash = false;
|
||||
}
|
||||
|
||||
/* weird to leave one at the end */
|
||||
if (saw_backslash) {
|
||||
newstr.append('\\');
|
||||
}
|
||||
|
||||
return newstr.toString();
|
||||
}
|
||||
|
||||
/*
|
||||
* Return a string "U+XX.XXX.XXXX" etc, where each XX set is the xdigits of
|
||||
* the logical Unicode code point. No bloody brain-damaged UTF-16 surrogate
|
||||
* crap, just true logical characters.
|
||||
*/
|
||||
private static String uniplus(String s) {
|
||||
if (s.length() == 0) {
|
||||
return "";
|
||||
}
|
||||
/* This is just the minimum; sb will grow as needed. */
|
||||
StringBuilder sb = new StringBuilder(2 + 3 * s.length());
|
||||
sb.append("U+");
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
sb.append(String.format("%X", s.codePointAt(i)));
|
||||
if (s.codePointAt(i) > Character.MAX_VALUE) {
|
||||
i++;
|
||||
/**** WE HATES UTF-16! WE HATES IT FOREVERSES!!! ****/
|
||||
}
|
||||
if (i + 1 < s.length()) {
|
||||
sb.append(".");
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.util;
|
||||
|
||||
import stanhebben.zenscript.ZenParsedFile;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ZenPosition {
|
||||
private final ZenParsedFile file;
|
||||
private final int line;
|
||||
private final int offset;
|
||||
|
||||
public ZenPosition(ZenParsedFile file, int line, int offset) {
|
||||
this.file = file;
|
||||
this.line = line;
|
||||
this.offset = offset;
|
||||
}
|
||||
|
||||
public ZenParsedFile getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public int getLine() {
|
||||
return line;
|
||||
}
|
||||
|
||||
public int getLineOffset() {
|
||||
return offset;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return Integer.toString(line) + ":" + offset;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.util;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ZenTypeUtil {
|
||||
private static final Map<String, String> SIGNATURE_MAP;
|
||||
|
||||
static {
|
||||
SIGNATURE_MAP = new HashMap<String, String>();
|
||||
SIGNATURE_MAP.put(boolean.class.getName(), "Z");
|
||||
SIGNATURE_MAP.put(byte.class.getName(), "B");
|
||||
SIGNATURE_MAP.put(short.class.getName(), "S");
|
||||
SIGNATURE_MAP.put(int.class.getName(), "I");
|
||||
SIGNATURE_MAP.put(long.class.getName(), "J");
|
||||
SIGNATURE_MAP.put(float.class.getName(), "F");
|
||||
SIGNATURE_MAP.put(double.class.getName(), "D");
|
||||
SIGNATURE_MAP.put(char.class.getName(), "C");
|
||||
SIGNATURE_MAP.put(void.class.getName(), "V");
|
||||
}
|
||||
|
||||
public static String signature(Class<?> cls) {
|
||||
if (SIGNATURE_MAP.containsKey(cls.getName())) {
|
||||
return SIGNATURE_MAP.get(cls.getName());
|
||||
} else if (cls.isArray()) {
|
||||
return "[" + signature(cls.getComponentType());
|
||||
} else {
|
||||
String signature = "L" + internal(cls) + ";";
|
||||
SIGNATURE_MAP.put(cls.getName(), signature);
|
||||
return signature;
|
||||
}
|
||||
}
|
||||
|
||||
public static String internal(Class<?> cls) {
|
||||
return cls.getName().replace('.', '/');
|
||||
}
|
||||
|
||||
public static String descriptor(Method method) {
|
||||
StringBuilder output = new StringBuilder();
|
||||
output.append('(');
|
||||
for (Class param : method.getParameterTypes()) {
|
||||
output.append(signature(param));
|
||||
}
|
||||
output.append(')').append(signature(method.getReturnType()));
|
||||
return output.toString();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user