move JSON and parsing logic to compiler project, decouple JSON and parser from engine logic
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package me.topchetoeu.j2s.common.parsing;
|
||||
package me.topchetoeu.j2s.common;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@@ -4,8 +4,6 @@ import java.util.HashMap;
|
||||
import java.util.function.IntFunction;
|
||||
import java.util.function.IntSupplier;
|
||||
|
||||
import me.topchetoeu.j2s.common.parsing.Location;
|
||||
|
||||
public class Instruction {
|
||||
public static enum Type {
|
||||
RETURN(0x00),
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
package me.topchetoeu.j2s.common.parsing;
|
||||
package me.topchetoeu.j2s.common;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Objects;
|
||||
|
||||
import me.topchetoeu.j2s.common.Metadata;
|
||||
|
||||
public abstract class Location implements Comparable<Location> {
|
||||
public static final Location INTERNAL = Location.of(new Filename(Metadata.name(), "native"), -1, -1);
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
package me.topchetoeu.j2s.common;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class StringifyUtils {
|
||||
public static String quoteString(String raw) {
|
||||
var res = new StringBuilder("\"");
|
||||
var alphabet = "0123456789ABCDEF".toCharArray();
|
||||
|
||||
for (var c : raw.toCharArray()) {
|
||||
if (c < 32 || c >= 127) {
|
||||
res
|
||||
.append("\\u")
|
||||
.append(alphabet[(c >> 12) & 0xF])
|
||||
.append(alphabet[(c >> 8) & 0xF])
|
||||
.append(alphabet[(c >> 4) & 0xF])
|
||||
.append(alphabet[(c >> 0) & 0xF]);
|
||||
}
|
||||
else if (c == '\\')
|
||||
res.append("\\\\");
|
||||
else if (c == '"')
|
||||
res.append("\\\"");
|
||||
else res.append(c);
|
||||
}
|
||||
|
||||
return res.append('"').toString();
|
||||
}
|
||||
|
||||
public static String quoteNumber(Double num) {
|
||||
if (num == Double.NEGATIVE_INFINITY) return "-Infinity";
|
||||
if (num == Double.POSITIVE_INFINITY) return "Infinity";
|
||||
if (Double.isNaN(num)) return "NaN";
|
||||
return BigDecimal.valueOf(num).stripTrailingZeros().toPlainString();
|
||||
}
|
||||
|
||||
private static double power(double a, long b) {
|
||||
if (b == 0) return 1;
|
||||
if (b == 1) return a;
|
||||
if (b < 0) return 1 / power(a, -b);
|
||||
|
||||
if ((b & 1) == 0) return power(a * a, b / 2);
|
||||
else return a * power(a * a, b / 2);
|
||||
}
|
||||
|
||||
public static Double unqoteNumber(String src) {
|
||||
var i = 0;
|
||||
|
||||
double whole = 0;
|
||||
double fract = 0;
|
||||
long exponent = 0;
|
||||
boolean parsedAny = false;
|
||||
boolean negative = false;
|
||||
|
||||
if (src.charAt(i) == '-') {
|
||||
negative = true;
|
||||
i++;
|
||||
}
|
||||
|
||||
while (i < src.length()) {
|
||||
var c = src.charAt(i);
|
||||
if (c < '0' || c > '9') break;
|
||||
|
||||
parsedAny = true;
|
||||
whole *= 10;
|
||||
whole += src.charAt(i++) - '0';
|
||||
}
|
||||
|
||||
if (i < src.length() && src.charAt(i) == '.') {
|
||||
parsedAny = true;
|
||||
i++;
|
||||
|
||||
while (i < src.length()) {
|
||||
var c = src.charAt(i);
|
||||
if (c < '0' || c > '9') break;
|
||||
|
||||
parsedAny = true;
|
||||
fract += src.charAt(i++) - '0';
|
||||
fract /= 10;
|
||||
}
|
||||
}
|
||||
|
||||
if (i < src.length() && (src.charAt(i) == 'e' || src.charAt(i) == 'E')) {
|
||||
i++;
|
||||
parsedAny = true;
|
||||
boolean expNegative = false;
|
||||
boolean parsedE = false;
|
||||
|
||||
if (i < src.length()) {
|
||||
if (src.charAt(i) == '-') {
|
||||
expNegative = true;
|
||||
i++;
|
||||
}
|
||||
else if (src.charAt(i) == '+') {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
while (i < src.length()) {
|
||||
var c = src.charAt(i);
|
||||
if (c < '0' || c > '9') break;
|
||||
|
||||
parsedE = true;
|
||||
exponent *= 10;
|
||||
|
||||
if (expNegative) exponent -= src.charAt(i) - '0';
|
||||
else exponent += src.charAt(i) - '0';
|
||||
}
|
||||
|
||||
if (!parsedE) return Double.NaN;
|
||||
}
|
||||
|
||||
if (i != src.length()) return Double.NaN;
|
||||
|
||||
if (!parsedAny) {
|
||||
if (negative) return Double.NaN;
|
||||
return 0.;
|
||||
}
|
||||
else if (negative) return -(whole + fract) * power(10, exponent);
|
||||
else return (whole + fract) * power(10, exponent);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package me.topchetoeu.j2s.common;
|
||||
|
||||
import me.topchetoeu.j2s.common.parsing.Location;
|
||||
|
||||
public class SyntaxException extends RuntimeException {
|
||||
public final Location loc;
|
||||
public final String msg;
|
||||
|
||||
@@ -1,174 +0,0 @@
|
||||
package me.topchetoeu.j2s.common.json;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.HashMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import me.topchetoeu.j2s.common.Metadata;
|
||||
import me.topchetoeu.j2s.common.SyntaxException;
|
||||
import me.topchetoeu.j2s.common.parsing.Filename;
|
||||
import me.topchetoeu.j2s.common.parsing.Location;
|
||||
import me.topchetoeu.j2s.common.parsing.ParseRes;
|
||||
import me.topchetoeu.j2s.common.parsing.Parsing;
|
||||
import me.topchetoeu.j2s.common.parsing.Source;
|
||||
|
||||
public class JSON {
|
||||
public static ParseRes<JSONElement> parseString(Source src, int i) {
|
||||
var res = Parsing.parseString(src, i);
|
||||
if (!res.isSuccess()) return res.chainError();
|
||||
return ParseRes.res(JSONElement.string(res.result), res.n);
|
||||
}
|
||||
public static ParseRes<JSONElement> parseNumber(Source src, int i) {
|
||||
var res = Parsing.parseNumber(src, i, true);
|
||||
if (!res.isSuccess()) return res.chainError();
|
||||
else return ParseRes.res(JSONElement.number(res.result), res.n);
|
||||
}
|
||||
public static ParseRes<JSONElement> parseLiteral(Source src, int i) {
|
||||
var id = Parsing.parseIdentifier(src, i);
|
||||
|
||||
if (!id.isSuccess()) return ParseRes.failed();
|
||||
else if (id.result.equals("true")) return ParseRes.res(JSONElement.bool(true), id.n);
|
||||
else if (id.result.equals("false")) return ParseRes.res(JSONElement.bool(false), id.n);
|
||||
else if (id.result.equals("null")) return ParseRes.res(JSONElement.NULL, id.n);
|
||||
else return ParseRes.failed();
|
||||
}
|
||||
|
||||
public static ParseRes<JSONElement> parseValue(Source src, int i) {
|
||||
return ParseRes.first(src, i,
|
||||
JSON::parseString,
|
||||
JSON::parseNumber,
|
||||
JSON::parseLiteral,
|
||||
JSON::parseMap,
|
||||
JSON::parseList
|
||||
);
|
||||
}
|
||||
|
||||
public static ParseRes<JSONElement> parseMap(Source src, int i) {
|
||||
var n = Parsing.skipEmpty(src, i);
|
||||
|
||||
if (!src.is(i + n, "{")) return ParseRes.failed();
|
||||
n++;
|
||||
|
||||
var values = new JSONMap();
|
||||
|
||||
if (src.is(i + n, "}")) return ParseRes.res(JSONElement.map(new JSONMap(new HashMap<>())), n + 1);
|
||||
while (true) {
|
||||
var name = parseString(src, i + n);
|
||||
if (!name.isSuccess()) return name.chainError(src.loc(i + n), "Expected an index");
|
||||
n += name.n;
|
||||
n += Parsing.skipEmpty(src, i + n);
|
||||
|
||||
if (!src.is(i + n, ":")) return name.chainError(src.loc(i + n), "Expected a colon");
|
||||
n++;
|
||||
|
||||
var res = parseValue(src, i + n);
|
||||
if (!res.isSuccess()) return res.chainError(src.loc(i + n), "Expected a list element");
|
||||
values.put(name.result.toString(), res.result);
|
||||
n += res.n;
|
||||
n += Parsing.skipEmpty(src, i + n);
|
||||
|
||||
if (src.is(i + n, ",")) n++;
|
||||
else if (src.is(i + n, "}")) {
|
||||
n++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ParseRes.res(JSONElement.map(values), n);
|
||||
}
|
||||
public static ParseRes<JSONElement> parseList(Source src, int i) {
|
||||
var n = Parsing.skipEmpty(src, i);
|
||||
|
||||
if (!src.is(i + n++, "[")) return ParseRes.failed();
|
||||
|
||||
var values = new JSONList();
|
||||
|
||||
if (src.is(i + n, "]")) return ParseRes.res(JSONElement.list(new JSONList()), n + 1);
|
||||
while (true) {
|
||||
var res = parseValue(src, i + n);
|
||||
if (!res.isSuccess()) return res.chainError(src.loc(i + n), "Expected a list element");
|
||||
values.add(res.result);
|
||||
n += res.n;
|
||||
n += Parsing.skipEmpty(src, i + n);
|
||||
|
||||
if (src.is(i + n, ",")) n++;
|
||||
else if (src.is(i + n, "]")) {
|
||||
n++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ParseRes.res(JSONElement.list(values), n);
|
||||
}
|
||||
public static JSONElement parse(Filename filename, String raw) {
|
||||
if (filename == null) filename = new Filename(Metadata.name(), "json");
|
||||
|
||||
var res = parseValue(new Source(null, filename, raw), 0);
|
||||
if (res.isFailed()) throw new SyntaxException(Location.of(filename, 0, 0), "Invalid JSON given");
|
||||
else if (res.isError()) throw new SyntaxException(res.errorLocation, res.error);
|
||||
else return JSONElement.of(res.result);
|
||||
}
|
||||
|
||||
public static String stringify(JSONElement el) {
|
||||
if (el.isNumber()) {
|
||||
var d = el.number();
|
||||
if (d == Double.NEGATIVE_INFINITY) return "-Infinity";
|
||||
if (d == Double.POSITIVE_INFINITY) return "Infinity";
|
||||
if (Double.isNaN(d)) return "NaN";
|
||||
return BigDecimal.valueOf(d).stripTrailingZeros().toPlainString();
|
||||
}
|
||||
if (el.isBoolean()) return el.bool() ? "true" : "false";
|
||||
if (el.isNull()) return "null";
|
||||
if (el.isString()) {
|
||||
var res = new StringBuilder("\"");
|
||||
var alphabet = "0123456789ABCDEF".toCharArray();
|
||||
|
||||
for (var c : el.string().toCharArray()) {
|
||||
if (c < 32 || c >= 127) {
|
||||
res
|
||||
.append("\\u")
|
||||
.append(alphabet[(c >> 12) & 0xF])
|
||||
.append(alphabet[(c >> 8) & 0xF])
|
||||
.append(alphabet[(c >> 4) & 0xF])
|
||||
.append(alphabet[(c >> 0) & 0xF]);
|
||||
}
|
||||
else if (c == '\\')
|
||||
res.append("\\\\");
|
||||
else if (c == '"')
|
||||
res.append("\\\"");
|
||||
else res.append(c);
|
||||
}
|
||||
|
||||
return res.append('"').toString();
|
||||
}
|
||||
if (el.isList()) {
|
||||
var res = new StringBuilder().append("[");
|
||||
for (int i = 0; i < el.list().size(); i++) {
|
||||
if (i != 0) res.append(",");
|
||||
res.append(stringify(el.list().get(i)));
|
||||
}
|
||||
res.append("]");
|
||||
return res.toString();
|
||||
}
|
||||
if (el.isMap()) {
|
||||
var res = new StringBuilder().append("{");
|
||||
var entries = el.map().entrySet().stream().collect(Collectors.toList());
|
||||
|
||||
for (int i = 0; i < entries.size(); i++) {
|
||||
if (i != 0) res.append(",");
|
||||
res.append(stringify(JSONElement.string(entries.get(i).getKey())));
|
||||
res.append(":");
|
||||
res.append(stringify(entries.get(i).getValue()));
|
||||
}
|
||||
res.append("}");
|
||||
return res.toString();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public static String stringify(JSONMap map) {
|
||||
return stringify(JSONElement.of(map));
|
||||
}
|
||||
public static String stringify(JSONList list) {
|
||||
return stringify(JSONElement.of(list));
|
||||
}
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
package me.topchetoeu.j2s.common.json;
|
||||
|
||||
public class JSONElement {
|
||||
public static enum Type {
|
||||
STRING,
|
||||
NUMBER,
|
||||
BOOLEAN,
|
||||
NULL,
|
||||
LIST,
|
||||
MAP,
|
||||
}
|
||||
|
||||
public static final JSONElement NULL = new JSONElement(Type.NULL, null);
|
||||
|
||||
public static JSONElement map(JSONMap val) {
|
||||
return new JSONElement(Type.MAP, val);
|
||||
}
|
||||
public static JSONElement list(JSONList val) {
|
||||
return new JSONElement(Type.LIST, val);
|
||||
}
|
||||
public static JSONElement string(String val) {
|
||||
return new JSONElement(Type.STRING, val);
|
||||
}
|
||||
public static JSONElement number(double val) {
|
||||
return new JSONElement(Type.NUMBER, val);
|
||||
}
|
||||
public static JSONElement bool(boolean val) {
|
||||
return new JSONElement(Type.BOOLEAN, val);
|
||||
}
|
||||
|
||||
public static JSONElement of(Object val) {
|
||||
if (val instanceof JSONElement el) return el;
|
||||
else if (val instanceof JSONMap map) return map(map);
|
||||
else if (val instanceof JSONList list) return list(list);
|
||||
else if (val instanceof String str) return string(str);
|
||||
else if (val instanceof Boolean bool) return bool(bool);
|
||||
else if (val instanceof Number num) return number(num.doubleValue());
|
||||
else if (val == null) return NULL;
|
||||
else throw new IllegalArgumentException("val must be: String, Boolean, Number, JSONList or JSONMap");
|
||||
}
|
||||
|
||||
public final Type type;
|
||||
private final Object value;
|
||||
|
||||
public boolean isMap() { return type == Type.MAP; }
|
||||
public boolean isList() { return type == Type.LIST; }
|
||||
public boolean isString() { return type == Type.STRING; }
|
||||
public boolean isNumber() { return type == Type.NUMBER; }
|
||||
public boolean isBoolean() { return type == Type.BOOLEAN; }
|
||||
public boolean isNull() { return type == Type.NULL; }
|
||||
|
||||
public JSONMap map() {
|
||||
if (!isMap()) throw new IllegalStateException("Element is not a map");
|
||||
return (JSONMap)value;
|
||||
}
|
||||
public JSONList list() {
|
||||
if (!isList()) throw new IllegalStateException("Element is not a map");
|
||||
return (JSONList)value;
|
||||
}
|
||||
public String string() {
|
||||
if (!isString()) throw new IllegalStateException("Element is not a string");
|
||||
return (String)value;
|
||||
}
|
||||
public double number() {
|
||||
if (!isNumber()) throw new IllegalStateException("Element is not a number");
|
||||
return (double)value;
|
||||
}
|
||||
public boolean bool() {
|
||||
if (!isBoolean()) throw new IllegalStateException("Element is not a boolean");
|
||||
return (boolean)value;
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
if (isMap()) return "{...}";
|
||||
if (isList()) return "[...]";
|
||||
if (isString()) return (String)value;
|
||||
if (isString()) return (String)value;
|
||||
if (isNumber()) return (double)value + "";
|
||||
if (isBoolean()) return (boolean)value + "";
|
||||
if (isNull()) return "null";
|
||||
return "";
|
||||
}
|
||||
|
||||
private JSONElement(Type type, Object val) {
|
||||
this.type = type;
|
||||
this.value = val;
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package me.topchetoeu.j2s.common.json;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
public class JSONList extends ArrayList<JSONElement> {
|
||||
public JSONList() {}
|
||||
public JSONList(JSONElement ...els) {
|
||||
super(Arrays.asList(els));
|
||||
}
|
||||
public JSONList(Collection<JSONElement> els) {
|
||||
super(els);
|
||||
}
|
||||
|
||||
public JSONList addNull() { this.add(JSONElement.NULL); return this; }
|
||||
public JSONList add(String val) { this.add(JSONElement.of(val)); return this; }
|
||||
public JSONList add(double val) { this.add(JSONElement.of(val)); return this; }
|
||||
public JSONList add(boolean val) { this.add(JSONElement.of(val)); return this; }
|
||||
public JSONList add(Map<String, JSONElement> val) { this.add(JSONElement.of(val)); return this; }
|
||||
public JSONList add(Collection<JSONElement> val) { this.add(JSONElement.of(val)); return this; }
|
||||
public JSONList add(JSONMap val) { this.add(JSONElement.of(val)); return this; }
|
||||
public JSONList add(JSONList val) { this.add(JSONElement.of(val)); return this; }
|
||||
|
||||
}
|
||||
@@ -1,136 +0,0 @@
|
||||
package me.topchetoeu.j2s.common.json;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class JSONMap implements Map<String, JSONElement> {
|
||||
private Map<String, JSONElement> elements = new HashMap<>();
|
||||
|
||||
public JSONElement get(String path) {
|
||||
JSONElement curr = JSONElement.map(this);
|
||||
|
||||
for (var seg : path.split("\\.")) {
|
||||
if (!curr.isMap()) return null;
|
||||
curr = curr.map().elements.get(seg);
|
||||
}
|
||||
|
||||
return curr;
|
||||
}
|
||||
|
||||
public boolean isMap(String path) {
|
||||
var el = get(path);
|
||||
return el != null && el.isMap();
|
||||
}
|
||||
public boolean isList(String path) {
|
||||
var el = get(path);
|
||||
return el != null && el.isList();
|
||||
}
|
||||
public boolean isString(String path) {
|
||||
var el = get(path);
|
||||
return el != null && el.isString();
|
||||
}
|
||||
public boolean isNumber(String path) {
|
||||
var el = get(path);
|
||||
return el != null && el.isNumber();
|
||||
}
|
||||
public boolean isBoolean(String path) {
|
||||
var el = get(path);
|
||||
return el != null && el.isBoolean();
|
||||
}
|
||||
public boolean isNull(String path) {
|
||||
var el = get(path);
|
||||
return el != null && el.isNull();
|
||||
}
|
||||
public boolean contains(String path) {
|
||||
return get(path) != null;
|
||||
}
|
||||
|
||||
public JSONMap map(String path) {
|
||||
var el = get(path);
|
||||
if (el == null) throw new RuntimeException(String.format("'%s' doesn't exist", path));
|
||||
return el.map();
|
||||
}
|
||||
public JSONMap map(String path, JSONMap defaultVal) {
|
||||
var el = get(path);
|
||||
if (el == null) return defaultVal;
|
||||
if (el.isMap()) return el.map();
|
||||
return defaultVal;
|
||||
}
|
||||
|
||||
public JSONList list(String path) {
|
||||
var el = get(path);
|
||||
if (el == null) throw new RuntimeException(String.format("'%s' doesn't exist", path));
|
||||
return el.list();
|
||||
}
|
||||
public JSONList list(String path, JSONList defaultVal) {
|
||||
var el = get(path);
|
||||
if (el == null) return defaultVal;
|
||||
if (el.isList()) return el.list();
|
||||
return defaultVal;
|
||||
}
|
||||
|
||||
public String string(String path) {
|
||||
var el = get(path);
|
||||
if (el == null) throw new RuntimeException(String.format("'%s' doesn't exist", path));
|
||||
return el.string();
|
||||
}
|
||||
public String string(String path, String defaultVal) {
|
||||
var el = get(path);
|
||||
if (el == null) return defaultVal;
|
||||
if (el.isString()) return el.string();
|
||||
return defaultVal;
|
||||
}
|
||||
|
||||
public double number(String path) {
|
||||
var el = get(path);
|
||||
if (el == null) throw new RuntimeException(String.format("'%s' doesn't exist", path));
|
||||
return el.number();
|
||||
}
|
||||
public double number(String path, double defaultVal) {
|
||||
var el = get(path);
|
||||
if (el == null) return defaultVal;
|
||||
if (el.isNumber()) return el.number();
|
||||
return defaultVal;
|
||||
}
|
||||
|
||||
public boolean bool(String path) {
|
||||
var el = get(path);
|
||||
if (el == null) throw new RuntimeException(String.format("'%s' doesn't exist", path));
|
||||
return el.bool();
|
||||
}
|
||||
public boolean bool(String path, boolean defaultVal) {
|
||||
var el = get(path);
|
||||
if (el == null) return defaultVal;
|
||||
if (el.isBoolean()) return el.bool();
|
||||
return defaultVal;
|
||||
}
|
||||
|
||||
public JSONMap setNull(String key) { elements.put(key, JSONElement.NULL); return this; }
|
||||
public JSONMap set(String key, String val) { elements.put(key, JSONElement.of(val)); return this; }
|
||||
public JSONMap set(String key, double val) { elements.put(key, JSONElement.of(val)); return this; }
|
||||
public JSONMap set(String key, boolean val) { elements.put(key, JSONElement.of(val)); return this; }
|
||||
public JSONMap set(String key, Map<String, JSONElement> val) { elements.put(key, JSONElement.of(val)); return this; }
|
||||
public JSONMap set(String key, Collection<JSONElement> val) { elements.put(key, JSONElement.of(val)); return this; }
|
||||
|
||||
@Override public int size() { return elements.size(); }
|
||||
@Override public boolean isEmpty() { return elements.isEmpty(); }
|
||||
@Override public boolean containsKey(Object key) { return elements.containsKey(key); }
|
||||
@Override public boolean containsValue(Object value) { return elements.containsValue(value); }
|
||||
@Override public JSONElement get(Object key) { return elements.get(key); }
|
||||
@Override public JSONElement put(String key, JSONElement value) { return elements.put(key, value); }
|
||||
@Override public JSONElement remove(Object key) { return elements.remove(key); }
|
||||
@Override public void putAll(Map<? extends String, ? extends JSONElement> m) { elements.putAll(m); }
|
||||
|
||||
@Override public void clear() { elements.clear(); }
|
||||
|
||||
@Override public Set<String> keySet() { return elements.keySet(); }
|
||||
@Override public Collection<JSONElement> values() { return elements.values(); }
|
||||
@Override public Set<Entry<String, JSONElement>> entrySet() { return elements.entrySet(); }
|
||||
|
||||
public JSONMap() { }
|
||||
public JSONMap(Map<String, JSONElement> els) {
|
||||
this.elements = new HashMap<>(els);
|
||||
}
|
||||
}
|
||||
@@ -13,9 +13,9 @@ import java.util.function.Function;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import me.topchetoeu.j2s.common.Filename;
|
||||
import me.topchetoeu.j2s.common.Location;
|
||||
import me.topchetoeu.j2s.common.Instruction.BreakpointType;
|
||||
import me.topchetoeu.j2s.common.parsing.Filename;
|
||||
import me.topchetoeu.j2s.common.parsing.Location;
|
||||
|
||||
public class FunctionMap {
|
||||
public static class FunctionMapBuilder {
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
package me.topchetoeu.j2s.common.parsing;
|
||||
|
||||
public class ParseRes<T> {
|
||||
public static enum State {
|
||||
SUCCESS,
|
||||
FAILED,
|
||||
ERROR;
|
||||
|
||||
public boolean isSuccess() { return this == SUCCESS; }
|
||||
public boolean isFailed() { return this == FAILED; }
|
||||
public boolean isError() { return this == ERROR; }
|
||||
}
|
||||
|
||||
public final ParseRes.State state;
|
||||
public final Location errorLocation;
|
||||
public final String error;
|
||||
public final T result;
|
||||
public final int n;
|
||||
|
||||
private ParseRes(ParseRes.State state, Location errorLocation, String error, T result, int readN) {
|
||||
this.result = result;
|
||||
this.n = readN;
|
||||
this.state = state;
|
||||
this.error = error;
|
||||
this.errorLocation = errorLocation;
|
||||
}
|
||||
|
||||
public ParseRes<T> setN(int i) {
|
||||
if (!state.isSuccess()) return this;
|
||||
return new ParseRes<>(state, null, null, result, i);
|
||||
}
|
||||
public ParseRes<T> addN(int n) {
|
||||
if (!state.isSuccess()) return this;
|
||||
return new ParseRes<>(state, null, null, result, this.n + n);
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T2> ParseRes<T2> chainError() {
|
||||
if (isSuccess()) throw new RuntimeException("Can't transform a ParseRes that hasn't failed");
|
||||
return (ParseRes<T2>)this;
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T2> ParseRes<T2> chainError(ParseRes<?> other) {
|
||||
if (this.isError()) return other.chainError();
|
||||
return (ParseRes<T2>)this;
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T2> ParseRes<T2> chainError(Location loc, String error) {
|
||||
if (!this.isError()) return new ParseRes<>(State.ERROR, loc, error, null, 0);
|
||||
return (ParseRes<T2>)this;
|
||||
}
|
||||
|
||||
public boolean isSuccess() { return state.isSuccess(); }
|
||||
public boolean isFailed() { return state.isFailed(); }
|
||||
public boolean isError() { return state.isError(); }
|
||||
|
||||
public static <T> ParseRes<T> failed() {
|
||||
return new ParseRes<T>(State.FAILED, null, null, null, 0);
|
||||
}
|
||||
public static <T> ParseRes<T> error(Location loc, String error) {
|
||||
// TODO: differentiate definitive and probable errors
|
||||
return new ParseRes<>(State.ERROR, loc, error, null, 0);
|
||||
}
|
||||
public static <T> ParseRes<T> res(T val, int i) {
|
||||
return new ParseRes<>(State.SUCCESS, null, null, val, i);
|
||||
}
|
||||
|
||||
@SuppressWarnings("all")
|
||||
public static <T> ParseRes<T> first(Source src, int i, Parser ...parsers) {
|
||||
int n = Parsing.skipEmpty(src, i);
|
||||
ParseRes<T> error = ParseRes.failed();
|
||||
|
||||
for (var parser : parsers) {
|
||||
var res = parser.parse(src, i + n);
|
||||
if (res.isSuccess()) return res.addN(n);
|
||||
if (res.isError() && error.isFailed()) error = res.chainError();
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package me.topchetoeu.j2s.common.parsing;
|
||||
|
||||
public interface Parser<T> {
|
||||
public ParseRes<T> parse(Source src, int i);
|
||||
}
|
||||
@@ -1,467 +0,0 @@
|
||||
package me.topchetoeu.j2s.common.parsing;
|
||||
|
||||
import me.topchetoeu.j2s.common.SyntaxException;
|
||||
|
||||
public class Parsing {
|
||||
public static boolean isDigit(Character c) {
|
||||
return c != null && c >= '0' && c <= '9';
|
||||
}
|
||||
public static boolean isAny(char c, String alphabet) {
|
||||
return alphabet.contains(Character.toString(c));
|
||||
}
|
||||
|
||||
public static int fromHex(char c) {
|
||||
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
|
||||
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
|
||||
if (c >= '0' && c <= '9') return c - '0';
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static int skipEmpty(Source src, int i) {
|
||||
return skipEmpty(src, i, true);
|
||||
}
|
||||
|
||||
public static int skipEmpty(Source src, int i, boolean noComments) {
|
||||
int n = 0;
|
||||
|
||||
if (i == 0 && src.is(0, "#!")) {
|
||||
while (!src.is(n, '\n')) n++;
|
||||
n++;
|
||||
}
|
||||
|
||||
var isSingle = false;
|
||||
var isMulti = false;
|
||||
|
||||
while (i + n < src.size()) {
|
||||
if (isSingle) {
|
||||
if (src.is(i + n, '\n')) {
|
||||
n++;
|
||||
isSingle = false;
|
||||
}
|
||||
else n++;
|
||||
}
|
||||
else if (isMulti) {
|
||||
if (src.is(i + n, "*/")) {
|
||||
n += 2;
|
||||
isMulti = false;
|
||||
}
|
||||
else n++;
|
||||
}
|
||||
else if (src.is(i + n, "//")) {
|
||||
n += 2;
|
||||
isSingle = true;
|
||||
}
|
||||
else if (src.is(i + n, "/*")) {
|
||||
n += 2;
|
||||
isMulti = true;
|
||||
}
|
||||
else if (src.is(i + n, Character::isWhitespace)) {
|
||||
n++;
|
||||
}
|
||||
else break;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
public static ParseRes<Character> parseChar(Source src, int i) {
|
||||
int n = 0;
|
||||
|
||||
if (src.is(i + n, '\\')) {
|
||||
n++;
|
||||
char c = src.at(i + n++);
|
||||
|
||||
if (c == 'b') return ParseRes.res('\b', n);
|
||||
else if (c == 't') return ParseRes.res('\t', n);
|
||||
else if (c == 'n') return ParseRes.res('\n', n);
|
||||
else if (c == 'f') return ParseRes.res('\f', n);
|
||||
else if (c == 'r') return ParseRes.res('\r', n);
|
||||
else if (c == '0') {
|
||||
if (src.is(i + n, Parsing::isDigit)) return ParseRes.error(src.loc(i), "Octal escape sequences are not allowed");
|
||||
else return ParseRes.res('\0', n);
|
||||
}
|
||||
else if (c >= '1' && c <= '9') return ParseRes.error(src.loc(i), "Octal escape sequences are not allowed");
|
||||
else if (c == 'x') {
|
||||
var newC = 0;
|
||||
|
||||
for (var j = 0; j < 2; j++) {
|
||||
if (i + n >= src.size()) return ParseRes.error(src.loc(i), "Invalid hexadecimal escape sequence");
|
||||
|
||||
int val = fromHex(src.at(i + n));
|
||||
if (val == -1) throw new SyntaxException(src.loc(i + n), "Invalid hexadecimal escape sequence");
|
||||
n++;
|
||||
|
||||
newC = (newC << 4) | val;
|
||||
}
|
||||
|
||||
return ParseRes.res((char)newC, n);
|
||||
}
|
||||
else if (c == 'u') {
|
||||
var newC = 0;
|
||||
|
||||
for (var j = 0; j < 4; j++) {
|
||||
if (i + n >= src.size()) return ParseRes.error(src.loc(i), "Invalid Unicode escape sequence");
|
||||
|
||||
int val = fromHex(src.at(i + n));
|
||||
if (val == -1) throw new SyntaxException(src.loc(i + n), "Invalid Unicode escape sequence");
|
||||
n++;
|
||||
|
||||
newC = (newC << 4) | val;
|
||||
}
|
||||
|
||||
return ParseRes.res((char)newC, n);
|
||||
}
|
||||
else if (c == '\n') return ParseRes.res(null, n);
|
||||
else n--;
|
||||
}
|
||||
|
||||
return ParseRes.res(src.at(i + n), n + 1);
|
||||
}
|
||||
|
||||
public static ParseRes<String> parseIdentifier(Source src, int i) {
|
||||
var n = skipEmpty(src, i);
|
||||
var res = new StringBuilder();
|
||||
var first = true;
|
||||
|
||||
while (true) {
|
||||
if (i + n > src.size()) break;
|
||||
char c = src.at(i + n, '\0');
|
||||
|
||||
if (first && Parsing.isDigit(c)) break;
|
||||
if (!Character.isLetterOrDigit(c) && c != '_' && c != '$') break;
|
||||
res.append(c);
|
||||
n++;
|
||||
first = false;
|
||||
}
|
||||
|
||||
if (res.length() <= 0) return ParseRes.failed();
|
||||
else return ParseRes.res(res.toString(), n);
|
||||
}
|
||||
public static ParseRes<String> parseIdentifier(Source src, int i, String test) {
|
||||
var n = skipEmpty(src, i);
|
||||
var res = new StringBuilder();
|
||||
var first = true;
|
||||
|
||||
while (true) {
|
||||
if (i + n > src.size()) break;
|
||||
char c = src.at(i + n, '\0');
|
||||
|
||||
if (first && Parsing.isDigit(c)) break;
|
||||
if (!Character.isLetterOrDigit(c) && c != '_' && c != '$') break;
|
||||
res.append(c);
|
||||
n++;
|
||||
first = false;
|
||||
}
|
||||
|
||||
if (res.length() <= 0) return ParseRes.failed();
|
||||
else if (test == null || res.toString().equals(test)) return ParseRes.res(res.toString(), n);
|
||||
else return ParseRes.failed();
|
||||
}
|
||||
public static boolean isIdentifier(Source src, int i, String test) {
|
||||
return parseIdentifier(src, i, test).isSuccess();
|
||||
}
|
||||
|
||||
public static ParseRes<String> parseOperator(Source src, int i, String op) {
|
||||
var n = skipEmpty(src, i);
|
||||
|
||||
if (src.is(i + n, op)) return ParseRes.res(op, n + op.length());
|
||||
else return ParseRes.failed();
|
||||
}
|
||||
|
||||
private static ParseRes<Double> parseHex(Source src, int i) {
|
||||
int n = 0;
|
||||
double res = 0;
|
||||
|
||||
while (true) {
|
||||
int digit = Parsing.fromHex(src.at(i + n, '\0'));
|
||||
if (digit < 0) {
|
||||
if (n <= 0) return ParseRes.failed();
|
||||
else return ParseRes.res(res, n);
|
||||
}
|
||||
n++;
|
||||
|
||||
res *= 16;
|
||||
res += digit;
|
||||
}
|
||||
}
|
||||
private static ParseRes<Double> parseOct(Source src, int i) {
|
||||
int n = 0;
|
||||
double res = 0;
|
||||
|
||||
while (true) {
|
||||
int digit = src.at(i + n, '\0') - '0';
|
||||
if (digit < 0 || digit > 9) break;
|
||||
if (digit > 7) return ParseRes.error(src.loc(i + n), "Digits in octal literals must be from 0 to 7, encountered " + digit);
|
||||
|
||||
if (digit < 0) {
|
||||
if (n <= 0) return ParseRes.failed();
|
||||
else return ParseRes.res(res, n);
|
||||
}
|
||||
n++;
|
||||
|
||||
res *= 8;
|
||||
res += digit;
|
||||
}
|
||||
|
||||
return ParseRes.res(res, n);
|
||||
}
|
||||
private static ParseRes<Double> parseBin(Source src, int i) {
|
||||
int n = 0;
|
||||
double res = 0;
|
||||
|
||||
while (true) {
|
||||
int digit = src.at(i + n, '\0') - '0';
|
||||
if (digit < 0 || digit > 9) break;
|
||||
if (digit > 1) return ParseRes.error(src.loc(i + n), "Digits in binary literals must be from 0 to 1, encountered " + digit);
|
||||
|
||||
if (digit < 0) {
|
||||
if (n <= 0) return ParseRes.failed();
|
||||
else return ParseRes.res(res, n);
|
||||
}
|
||||
n++;
|
||||
|
||||
res *= 2;
|
||||
res += digit;
|
||||
}
|
||||
|
||||
return ParseRes.res(res, n);
|
||||
}
|
||||
|
||||
public static ParseRes<String> parseString(Source src, int i) {
|
||||
var n = skipEmpty(src, i);
|
||||
|
||||
char quote;
|
||||
|
||||
if (src.is(i + n, '\'')) quote = '\'';
|
||||
else if (src.is(i + n, '"')) quote = '"';
|
||||
else return ParseRes.failed();
|
||||
n++;
|
||||
|
||||
var res = new StringBuilder();
|
||||
|
||||
while (true) {
|
||||
if (i + n >= src.size()) return ParseRes.error(src.loc(i + n), "Unterminated string literal");
|
||||
if (src.is(i + n, quote)) {
|
||||
n++;
|
||||
return ParseRes.res(res.toString(), n);
|
||||
}
|
||||
|
||||
var charRes = parseChar(src, i + n);
|
||||
if (!charRes.isSuccess()) return charRes.chainError(src.loc(i + n), "Invalid character");
|
||||
n += charRes.n;
|
||||
|
||||
if (charRes.result != null) res.append(charRes.result);
|
||||
}
|
||||
}
|
||||
public static ParseRes<Double> parseNumber(Source src, int i, boolean withMinus) {
|
||||
var n = skipEmpty(src, i);
|
||||
|
||||
double whole = 0;
|
||||
double fract = 0;
|
||||
long exponent = 0;
|
||||
boolean parsedAny = false;
|
||||
boolean negative = false;
|
||||
|
||||
if (withMinus && src.is(i + n, "-")) {
|
||||
negative = true;
|
||||
n++;
|
||||
}
|
||||
|
||||
if (src.is(i + n, "0x") || src.is(i + n, "0X")) {
|
||||
n += 2;
|
||||
|
||||
var res = parseHex(src, i + n);
|
||||
if (!res.isSuccess()) return res.chainError(src.loc(i + n), "Incomplete hexadecimal literal");
|
||||
n += res.n;
|
||||
|
||||
if (negative) return ParseRes.res(-res.result, n);
|
||||
else return ParseRes.res(res.result, n);
|
||||
}
|
||||
else if (src.is(i + n, "0o") || src.is(i + n, "0O")) {
|
||||
n += 2;
|
||||
|
||||
var res = parseOct(src, i + n);
|
||||
if (!res.isSuccess()) return res.chainError(src.loc(i + n), "Incomplete octal literal");
|
||||
n += res.n;
|
||||
|
||||
if (negative) return ParseRes.res(-res.result, n);
|
||||
else return ParseRes.res(res.result, n);
|
||||
}
|
||||
else if (src.is(i + n, "0b") || src.is(i + n, "0B")) {
|
||||
n += 2;
|
||||
|
||||
var res = parseBin(src, i + n);
|
||||
if (!res.isSuccess()) return res.chainError(src.loc(i + n), "Incomplete binary literal");
|
||||
n += res.n;
|
||||
|
||||
if (negative) return ParseRes.res(-res.result, n);
|
||||
else return ParseRes.res(res.result, n);
|
||||
}
|
||||
else if (src.is(i + n, '0')) {
|
||||
n++;
|
||||
parsedAny = true;
|
||||
if (src.is(i + n, Parsing::isDigit)) return ParseRes.error(src.loc(i + n), "Decimals with leading zeroes are not allowed");
|
||||
}
|
||||
|
||||
while (src.is(i + n, Parsing::isDigit)) {
|
||||
parsedAny = true;
|
||||
whole *= 10;
|
||||
whole += src.at(i + n++) - '0';
|
||||
}
|
||||
|
||||
if (src.is(i + n, '.')) {
|
||||
parsedAny = true;
|
||||
n++;
|
||||
|
||||
while (src.is(i + n, Parsing::isDigit)) {
|
||||
fract += src.at(i + n++) - '0';
|
||||
fract /= 10;
|
||||
}
|
||||
}
|
||||
|
||||
if (parsedAny && src.is(i + n, 'e') || src.is(i + n, 'E')) {
|
||||
n++;
|
||||
boolean expNegative = false;
|
||||
boolean parsedE = false;
|
||||
|
||||
if (src.is(i + n, '-')) {
|
||||
expNegative = true;
|
||||
n++;
|
||||
}
|
||||
else if (src.is(i + n, '+')) n++;
|
||||
|
||||
while (src.is(i + n, Parsing::isDigit)) {
|
||||
parsedE = true;
|
||||
exponent *= 10;
|
||||
|
||||
if (expNegative) exponent -= src.at(i + n++) - '0';
|
||||
else exponent += src.at(i + n++) - '0';
|
||||
}
|
||||
|
||||
if (!parsedE) return ParseRes.error(src.loc(i + n), "Incomplete number exponent");
|
||||
}
|
||||
|
||||
if (!parsedAny) {
|
||||
if (negative) return ParseRes.error(src.loc(i + n), "Expected number immediatly after minus");
|
||||
return ParseRes.failed();
|
||||
}
|
||||
else if (negative) return ParseRes.res(-(whole + fract) * power(10, exponent), n);
|
||||
else return ParseRes.res((whole + fract) * power(10, exponent), n);
|
||||
}
|
||||
public static ParseRes<Double> parseFloat(Source src, int i, boolean withMinus) {
|
||||
var n = skipEmpty(src, i);
|
||||
|
||||
double whole = 0;
|
||||
double fract = 0;
|
||||
long exponent = 0;
|
||||
boolean parsedAny = false;
|
||||
boolean negative = false;
|
||||
|
||||
if (withMinus && src.is(i + n, "-")) {
|
||||
negative = true;
|
||||
n++;
|
||||
}
|
||||
|
||||
while (src.is(i + n, Parsing::isDigit)) {
|
||||
parsedAny = true;
|
||||
whole *= 10;
|
||||
whole += src.at(i + n++) - '0';
|
||||
}
|
||||
|
||||
if (src.is(i + n, '.')) {
|
||||
parsedAny = true;
|
||||
n++;
|
||||
|
||||
while (src.is(i + n, Parsing::isDigit)) {
|
||||
fract += src.at(i + n++) - '0';
|
||||
fract /= 10;
|
||||
}
|
||||
}
|
||||
|
||||
if (src.is(i + n, 'e') || src.is(i + n, 'E')) {
|
||||
n++;
|
||||
parsedAny = true;
|
||||
boolean expNegative = false;
|
||||
boolean parsedE = false;
|
||||
|
||||
if (src.is(i + n, '-')) {
|
||||
expNegative = true;
|
||||
n++;
|
||||
}
|
||||
else if (src.is(i + n, '+')) n++;
|
||||
|
||||
while (src.is(i + n, Parsing::isDigit)) {
|
||||
parsedE = true;
|
||||
exponent *= 10;
|
||||
|
||||
if (expNegative) exponent -= src.at(i + n++) - '0';
|
||||
else exponent += src.at(i + n++) - '0';
|
||||
}
|
||||
|
||||
if (!parsedE) return ParseRes.error(src.loc(i + n), "Incomplete number exponent");
|
||||
}
|
||||
|
||||
if (!parsedAny) {
|
||||
if (negative) return ParseRes.error(src.loc(i + n), "Expected number immediatly after minus");
|
||||
return ParseRes.failed();
|
||||
}
|
||||
else if (negative) return ParseRes.res(-(whole + fract) * power(10, exponent), n);
|
||||
else return ParseRes.res((whole + fract) * power(10, exponent), n);
|
||||
}
|
||||
|
||||
public static ParseRes<Double> parseInt(Source src, int i, String alphabet, boolean withMinus) {
|
||||
var n = skipEmpty(src, i);
|
||||
|
||||
double result = 0;
|
||||
boolean parsedAny = false;
|
||||
boolean negative = false;
|
||||
|
||||
if (withMinus && src.is(i + n, "-")) {
|
||||
negative = true;
|
||||
n++;
|
||||
}
|
||||
|
||||
if (alphabet == null && src.is(i + n, "0x") || src.is(i + n, "0X")) {
|
||||
n += 2;
|
||||
|
||||
var res = parseHex(src, i);
|
||||
if (!res.isSuccess())
|
||||
return res.chainError(src.loc(i + n), "Incomplete hexadecimal literal");
|
||||
n += res.n;
|
||||
|
||||
if (negative)
|
||||
return ParseRes.res(-res.result, n);
|
||||
else
|
||||
return ParseRes.res(res.result, n);
|
||||
}
|
||||
|
||||
while (true) {
|
||||
var digit = alphabet.indexOf(Character.toLowerCase(src.at(i + n, '\0')));
|
||||
if (digit < 0)
|
||||
break;
|
||||
|
||||
parsedAny = true;
|
||||
result *= alphabet.length();
|
||||
result += digit;
|
||||
n++;
|
||||
}
|
||||
|
||||
if (!parsedAny) {
|
||||
if (negative)
|
||||
return ParseRes.error(src.loc(i + n), "Expected number immediatly after minus");
|
||||
return ParseRes.failed();
|
||||
} else if (negative)
|
||||
return ParseRes.res(-result, n);
|
||||
else
|
||||
return ParseRes.res(result, n);
|
||||
}
|
||||
|
||||
private static double power(double a, long b) {
|
||||
if (b == 0) return 1;
|
||||
if (b == 1) return a;
|
||||
if (b < 0) return 1 / power(a, -b);
|
||||
|
||||
if ((b & 1) == 0) return power(a * a, b / 2);
|
||||
else return a * power(a * a, b / 2);
|
||||
}
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
package me.topchetoeu.j2s.common.parsing;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import me.topchetoeu.j2s.common.environment.Environment;
|
||||
|
||||
public class Source {
|
||||
public final Environment env;
|
||||
public final Filename filename;
|
||||
public final String src;
|
||||
|
||||
private int[] lineStarts;
|
||||
|
||||
public Location loc(int offset) {
|
||||
if (offset < 0) offset = 0;
|
||||
if (offset > src.length()) offset = src.length();
|
||||
return new SourceLocation(filename, lineStarts, offset);
|
||||
}
|
||||
public boolean is(int i, char c) {
|
||||
return i >= 0 && i < src.length() && src.charAt(i) == c;
|
||||
}
|
||||
public boolean is(int i, String src) {
|
||||
if (i < 0 || i + src.length() > size()) return false;
|
||||
|
||||
for (int j = 0; j < src.length(); j++) {
|
||||
if (at(i + j) != src.charAt(j)) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
public boolean is(int i, Predicate<Character> predicate) {
|
||||
if (i < 0 || i >= src.length()) return false;
|
||||
return predicate.test(at(i));
|
||||
}
|
||||
public char at(int i) {
|
||||
return src.charAt(i);
|
||||
}
|
||||
public char at(int i, char defaultVal) {
|
||||
if (i < 0 || i >= src.length()) return defaultVal;
|
||||
else return src.charAt(i);
|
||||
}
|
||||
public int size() {
|
||||
return src.length();
|
||||
}
|
||||
public String slice(int start, int end) {
|
||||
return src.substring(start, end);
|
||||
}
|
||||
|
||||
public Source(Environment env, Filename filename, String src) {
|
||||
if (env == null) this.env = new Environment();
|
||||
else this.env = env;
|
||||
|
||||
this.filename = filename;
|
||||
this.src = src;
|
||||
|
||||
int n = 1;
|
||||
lineStarts = new int[16];
|
||||
lineStarts[0] = 0;
|
||||
|
||||
for (int i = src.indexOf("\n"); i > 0; i = src.indexOf("\n", i + 1)) {
|
||||
if (n >= lineStarts.length) {
|
||||
var newArr = new int[lineStarts.length * 2];
|
||||
System.arraycopy(lineStarts, 0, newArr, 0, n);
|
||||
lineStarts = newArr;
|
||||
}
|
||||
|
||||
lineStarts[n++] = i + 1;
|
||||
}
|
||||
|
||||
var newArr = new int[n];
|
||||
System.arraycopy(lineStarts, 0, newArr, 0, n);
|
||||
lineStarts = newArr;
|
||||
}
|
||||
public Source(String src) {
|
||||
this(null, null, src);
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
package me.topchetoeu.j2s.common.parsing;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class SourceLocation extends Location {
|
||||
private int[] lineStarts;
|
||||
private int line;
|
||||
private int start;
|
||||
private final Filename filename;
|
||||
private final int offset;
|
||||
|
||||
private void update() {
|
||||
if (lineStarts == null) return;
|
||||
|
||||
int a = 0;
|
||||
int b = lineStarts.length;
|
||||
|
||||
while (true) {
|
||||
if (a + 1 >= b) break;
|
||||
var mid = -((-a - b) >> 1);
|
||||
var el = lineStarts[mid];
|
||||
|
||||
if (el < offset) a = mid;
|
||||
else if (el > offset) b = mid;
|
||||
else {
|
||||
this.line = mid;
|
||||
this.start = 0;
|
||||
this.lineStarts = null;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
this.line = a;
|
||||
this.start = offset - lineStarts[a];
|
||||
this.lineStarts = null;
|
||||
return;
|
||||
}
|
||||
|
||||
@Override public Filename filename() { return filename; }
|
||||
@Override public int line() {
|
||||
update();
|
||||
return line;
|
||||
}
|
||||
@Override public int start() {
|
||||
update();
|
||||
return start;
|
||||
}
|
||||
|
||||
@Override public int hashCode() {
|
||||
return Objects.hash(offset);
|
||||
}
|
||||
@Override public int compareTo(Location other) {
|
||||
if (other instanceof SourceLocation srcLoc) return Integer.compare(offset, srcLoc.offset);
|
||||
else return super.compareTo(other);
|
||||
}
|
||||
@Override public boolean equals(Object obj) {
|
||||
if (obj instanceof SourceLocation other) return this.offset == other.offset;
|
||||
else return super.equals(obj);
|
||||
}
|
||||
|
||||
public SourceLocation(Filename filename, int[] lineStarts, int offset) {
|
||||
this.filename = filename;
|
||||
this.lineStarts = lineStarts;
|
||||
this.offset = offset;
|
||||
}
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
package me.topchetoeu.j2s.common;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import me.topchetoeu.j2s.common.environment.Environment;
|
||||
import me.topchetoeu.j2s.common.environment.Key;
|
||||
|
||||
public class TestEnvironment {
|
||||
private final Key<String> FOO = new Key<>();
|
||||
private final Key<Void> MARKER = new Key<>();
|
||||
|
||||
@Test public void testShouldCreate() {
|
||||
new Environment();
|
||||
Environment.empty();
|
||||
}
|
||||
@Test public void testShouldNotExist() {
|
||||
var env = new Environment();
|
||||
assertEquals(null, env.get(FOO));
|
||||
assertEquals(false, env.has(FOO));
|
||||
}
|
||||
|
||||
@Test public void testShouldAdd() {
|
||||
var env = new Environment();
|
||||
env.add(FOO, "test");
|
||||
assertEquals("test", env.get(FOO));
|
||||
}
|
||||
|
||||
@Test public void testShouldGetFromParent() {
|
||||
var parent = new Environment();
|
||||
parent.add(FOO, "test");
|
||||
var child = parent.child();
|
||||
assertEquals("test", child.get(FOO));
|
||||
assertEquals(true, child.has(FOO));
|
||||
}
|
||||
@Test public void testShouldHideParent() {
|
||||
var parent = new Environment();
|
||||
parent.add(FOO, "test");
|
||||
var child = parent.child();
|
||||
child.remove(FOO);
|
||||
assertEquals(null, child.get(FOO));
|
||||
assertEquals(false, child.has(FOO));
|
||||
}
|
||||
|
||||
@Test public void testShouldAddMarker() {
|
||||
var env = new Environment();
|
||||
env.add(MARKER);
|
||||
assertEquals(true, env.has(MARKER));
|
||||
assertEquals(false, env.hasNotNull(MARKER));
|
||||
}
|
||||
|
||||
@Test public void testShouldInitOnce() {
|
||||
var env = new Environment();
|
||||
assertEquals("a", env.init(FOO, "a"));
|
||||
assertEquals("a", env.init(FOO, "b"));
|
||||
assertEquals("a", env.get(FOO));
|
||||
}
|
||||
@Test public void testShouldInitOnceFrom() {
|
||||
var env = new Environment();
|
||||
assertEquals("a", env.initFrom(FOO, () -> "a"));
|
||||
assertEquals("a", env.initFrom(FOO, () -> "b"));
|
||||
assertEquals("a", env.get(FOO));
|
||||
}
|
||||
|
||||
@Test public void testShouldWrap() {
|
||||
var env = new Environment();
|
||||
assertEquals(env, Environment.wrap(env));
|
||||
assertInstanceOf(Environment.class, Environment.wrap(null));
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,6 @@ package me.topchetoeu.j2s.common;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import me.topchetoeu.j2s.common.parsing.Filename;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestFilename {
|
||||
|
||||
@@ -2,9 +2,6 @@ package me.topchetoeu.j2s.common;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import me.topchetoeu.j2s.common.parsing.Filename;
|
||||
import me.topchetoeu.j2s.common.parsing.Location;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestLocation {
|
||||
|
||||
@@ -1,98 +0,0 @@
|
||||
package me.topchetoeu.j2s.common;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import me.topchetoeu.j2s.common.parsing.Location;
|
||||
import me.topchetoeu.j2s.common.parsing.ParseRes;
|
||||
|
||||
public class TestParseRes {
|
||||
@Test public void testCreateFailed() {
|
||||
var res = ParseRes.failed();
|
||||
assertEquals(false, res.isSuccess());
|
||||
assertEquals(true, res.isFailed());
|
||||
assertEquals(false, res.isError());
|
||||
assertEquals(0, res.n);
|
||||
assertEquals(null, res.result);
|
||||
assertEquals(null, res.errorLocation);
|
||||
assertEquals(null, res.error);
|
||||
}
|
||||
@Test public void testCreateError() {
|
||||
var res = ParseRes.error(Location.of("test:10:5"), "test");
|
||||
assertEquals(false, res.isSuccess());
|
||||
assertEquals(false, res.isFailed());
|
||||
assertEquals(true, res.isError());
|
||||
assertEquals(0, res.n);
|
||||
assertEquals(null, res.result);
|
||||
assertEquals(Location.of("test:10:5"), res.errorLocation);
|
||||
assertEquals("test", res.error);
|
||||
}
|
||||
@Test public void testCreateResult() {
|
||||
var res = ParseRes.res("test", 10);
|
||||
assertEquals(true, res.isSuccess());
|
||||
assertEquals(false, res.isFailed());
|
||||
assertEquals(false, res.isError());
|
||||
assertEquals(10, res.n);
|
||||
assertEquals("test", res.result);
|
||||
assertEquals(null, res.errorLocation);
|
||||
assertEquals(null, res.error);
|
||||
}
|
||||
|
||||
@Test public void testChainFailed() {
|
||||
var a = ParseRes.<Integer>failed();
|
||||
|
||||
var b1 = a.<String>chainError();
|
||||
assertEquals(a, b1);
|
||||
|
||||
var b2 = a.<String>chainError(Location.of("test:1:2"), "test");
|
||||
assertEquals(true, b2.isError());
|
||||
assertEquals("test", b2.error);
|
||||
}
|
||||
@Test public void testChainError() {
|
||||
var a = ParseRes.<Integer>error(Location.of("test:1:2"), "test");
|
||||
|
||||
var b1 = a.<String>chainError();
|
||||
assertEquals(a, b1);
|
||||
|
||||
var b2 = a.<String>chainError(Location.of("test:1:2"), "test");
|
||||
assertEquals(a, b2);
|
||||
}
|
||||
@Test public void testChainResult() {
|
||||
var a = ParseRes.<Integer>res(10, 6);
|
||||
|
||||
assertThrows(RuntimeException.class, () -> a.<String>chainError());
|
||||
|
||||
var b1 = a.<String>chainError(Location.of("test:1:2"), "test");
|
||||
assertEquals(true, b1.isError());
|
||||
assertEquals("test", b1.error);
|
||||
}
|
||||
|
||||
|
||||
@Test public void testShouldAdd5() {
|
||||
var a = ParseRes.res("test", 6);
|
||||
var b = a.addN(5);
|
||||
|
||||
assertEquals(11, b.n);
|
||||
}
|
||||
@Test public void testShouldSet5() {
|
||||
var a = ParseRes.res("test", 6);
|
||||
var b = a.setN(5);
|
||||
|
||||
assertEquals(5, b.n);
|
||||
}
|
||||
|
||||
@Test public void testShouldNotAdd() {
|
||||
var a = ParseRes.failed();
|
||||
var b = a.addN(5);
|
||||
|
||||
assertEquals(0, b.n);
|
||||
}
|
||||
@Test public void testShouldNotSet() {
|
||||
var a = ParseRes.failed();
|
||||
var b = a.setN(5);
|
||||
|
||||
assertEquals(0, b.n);
|
||||
}
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
package me.topchetoeu.j2s.common;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import me.topchetoeu.j2s.common.environment.Environment;
|
||||
import me.topchetoeu.j2s.common.parsing.Filename;
|
||||
import me.topchetoeu.j2s.common.parsing.Location;
|
||||
import me.topchetoeu.j2s.common.parsing.Source;
|
||||
|
||||
public class TestSource {
|
||||
private Source mkSource(String src) {
|
||||
return new Source(new Environment(), Filename.parse("test"), src);
|
||||
}
|
||||
|
||||
@Test public void testShouldCreate() {
|
||||
new Source(new Environment(), Filename.parse("test"), "my little source :)");
|
||||
new Source(null, Filename.parse("test"), "my little source :)");
|
||||
new Source("my little source :)");
|
||||
}
|
||||
|
||||
@Test public void testShouldGet() {
|
||||
var src = mkSource("1234567890");
|
||||
assertEquals('1', src.at(0));
|
||||
assertEquals('6', src.at(5));
|
||||
}
|
||||
@Test public void testShouldThrowOutOfRange() {
|
||||
var src = mkSource("1234567890");
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> src.at(-1));
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> src.at(10));
|
||||
}
|
||||
@Test public void testImmutableSrcLoc() {
|
||||
var src = mkSource("1234567890");
|
||||
var loc = src.loc(5);
|
||||
// kinda stupid
|
||||
for (var i = 0; i < 1000; i++) {
|
||||
assertEquals(5, loc.start());
|
||||
assertEquals(0, loc.line());
|
||||
assertEquals(Filename.parse("test"), loc.filename());
|
||||
}
|
||||
}
|
||||
@Test public void testSingleLineSourceLocation() {
|
||||
var src = mkSource("1234567890");
|
||||
assertEquals(Location.of("test:1:1"), src.loc(-5));
|
||||
assertEquals(Location.of("test:1:1"), src.loc(0));
|
||||
assertEquals(Location.of("test:1:10"), src.loc(9));
|
||||
assertEquals(Location.of("test:1:11"), src.loc(14));
|
||||
}
|
||||
@Test public void testMultilineSourceLocation() {
|
||||
var src = mkSource("123\n456\n\n789\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
|
||||
assertEquals(Location.of("test:1:1"), src.loc(-5));
|
||||
assertEquals(Location.of("test:1:1"), src.loc(0));
|
||||
assertEquals(Location.of("test:1:4"), src.loc(3));
|
||||
assertEquals(Location.of("test:2:1"), src.loc(4));
|
||||
assertEquals(Location.of("test:2:4"), src.loc(7));
|
||||
assertEquals(Location.of("test:3:1"), src.loc(8));
|
||||
assertEquals(Location.of("test:4:1"), src.loc(9));
|
||||
assertEquals(Location.of("test:4:2"), src.loc(10));
|
||||
assertEquals(Location.of("test:4:4"), src.loc(12));
|
||||
assertEquals(Location.of("test:5:1"), src.loc(13));
|
||||
assertEquals(Location.of("test:19:1"), src.loc(50));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user