import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import java.util.function.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.imageio.*;
import java.math.*;
import java.time.Duration;
import java.lang.invoke.VarHandle;
import java.lang.invoke.MethodHandles;
import java.awt.geom.*;
import static x30_pkg.x30_util.DynamicObject;
import java.text.*;
import java.text.NumberFormat;
import java.util.TimeZone;
class main {
static void test_tok_selfType() {
testTranspilationFunction2(__23 -> tok_selfType(__23),
"class Bla { selfType x() { this; } }",
"class Bla { Bla x() { this; } }",
"class Bla { selfType x() { this; } }",
"class Bla { Bla x() { this; } }",
"interface BB { selfType x(); }",
"interface BB { BB x(); }",
"class Blume { Class x = selfType.class; Class y() { ret selfType.class; } }",
"class Blume { Class x = Blume.class; Class y() { ret Blume.class; } }",
"class Blume { Class x = selfType.class; Class y() { ret selfType.class; } }",
"class Blume { Class x = Blume.class; Class y() { ret Blume.class; } }",
"enum BB { a; selfType x(); }",
"enum BB { a; BB x(); }",
"class Bla { selfType x() { new selfType(); } }",
"class Bla { Bla x() { new Bla(); } }",
"class Bla> { selfType x() { new selfType(); } }",
"class Bla> { Bla x() { new Bla(); } }",
"class HelloMe { selfType x(O o) { ret (selfType) o; } }",
"class HelloMe { HelloMe x(O o) { ret (HelloMe) o; } }");
}
// test against cases
static void testTranspilationFunction2(IVF1> f, String in, String out, String... moreInOut) {
testTranspilationFunction_v2(f, in, out, moreInOut);
}
// just apply and return transpilation
static String testTranspilationFunction2(IVF1> f, String s) {
return testTranspilationFunction_v2(f, s);
}
static void tok_selfType(List tok) {
int i;
mainLoop: while ((i = jfind(tok, "selfType", (_tok, nIdx) -> {
// TODO: Do we even need any conditions here?
String prev = get(_tok, nIdx-1);
String next = get(_tok, nIdx+3);
return isIdentifier(next) || eqOneOf(next, ".", "(")
|| eq(prev, "(") && eq(next, ")");
})) >= 0) {
// Now find class name by going backwards.
int j = i, level = 1;
while (j > 0 && level > 0) {
String t = tok.get(j);
if (t.equals("}")) ++level;
if (t.equals("{")) --level;
j -= 2;
}
// search for class name
while (j > 0) {
String t = tok.get(j);
if (eqOneOf(t, "class", "interface", "enum") && isIdentifier(get(tok, j+2))) {
String type = tok_typeArgs_declToInvocation(tok_scanType(tok, j+2));
if (eqGet(tok, i+2, ".") && eqGet(tok, i+4, "class"))
type = tok_dropTypeParameters(type);
tokSet_reTok(tok, i, type);
continue mainLoop;
}
j -= 2;
}
tok.set(i, "Object"); // avoid endless loop if no outer class found
}
}
static void testTranspilationFunction_v2(IVF1> f, String in, String out, String... moreInOut) {
List tok = jtok(in);
makeVar_varCount.set(0);
f.get(tok);
assertJavaSourceEqualsVerbose_v2(in, out, tok);
for (Pair p : unnullForIteration(paramsToPairs(moreInOut)))
testTranspilationFunction_v2(f, p.a, p.b);
}
// just apply and return transpilation
static String testTranspilationFunction_v2(IVF1> f, String s) {
return applyTranspilationFunction(f, s);
}
static int jfind(String s, String in) {
return jfind(javaTok(s), in);
}
static int jfind(List tok, String in) {
return jfind(tok, 1, in);
}
static int jfind(List tok, int startIdx, String in) {
return jfind(tok, startIdx, in, (ITokCondition) null);
}
static int jfind(List tok, String in, Object condition) {
return jfind(tok, 1, in, condition);
}
static int jfind(List tok, String in, IIntPred condition) { return jfind(tok, 1, in, condition); }
static int jfind(List tok, int startIndex, String in, IIntPred condition) {
return jfind(tok, startIndex, in, tokCondition(condition));
}
static int jfind(List tok, String in, ITokCondition condition) { return jfind(tok, 1, in, condition); }
static int jfind(List tok, int startIndex, String in, ITokCondition condition) {
return jfind(tok, startIndex, in, (Object) condition);
}
static int jfind(List tok, int startIdx, String in, Object condition) {
//LS tokin = jfind_preprocess(javaTok(in));
return jfind(tok, startIdx, javaTokForJFind_array(in), condition);
}
// assumes you preprocessed tokin
static int jfind(List tok, List tokin) {
return jfind(tok, 1, tokin);
}
static int jfind(List tok, int startIdx, List tokin) {
return jfind(tok, startIdx, tokin, null);
}
static int jfind(List tok, int startIdx, String[] tokinC, Object condition) {
return findCodeTokens(tok, startIdx, false, tokinC, condition);
}
static int jfind(List tok, int startIdx, List tokin, Object condition) {
return jfind(tok, startIdx, codeTokensAsStringArray(tokin), condition);
}
static List jfind_preprocess(List tok) {
for (String type : litlist("quoted", "id", "int"))
replaceSublist(tok, ll("<", "", type, "", ">"), ll("<" + type + ">"));
replaceSublist(tok, ll("\\", "", "*"), ll("\\*"));
return tok;
}
// get purpose 1: access a list/array/map (safer version of x.get(y))
static A get(List l, int idx) {
return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null;
}
// seems to conflict with other signatures
/*static B get(Map map, A key) {
ret map != null ? map.get(key) : null;
}*/
static A get(A[] l, int idx) {
return idx >= 0 && idx < l(l) ? l[idx] : null;
}
// default to false
static boolean get(boolean[] l, int idx) {
return idx >= 0 && idx < l(l) ? l[idx] : false;
}
// get purpose 2: access a field by reflection or a map
static Object get(Object o, String field) {
try {
if (o == null) return null;
if (o instanceof Class) return get((Class) o, field);
if (o instanceof Map)
return ((Map) o).get(field);
Field f = getOpt_findField(o.getClass(), field);
if (f != null) {
makeAccessible(f);
return f.get(o);
}
if (o instanceof DynamicObject)
return getOptDynOnly(((DynamicObject) o), field);
} catch (Exception e) {
throw asRuntimeException(e);
}
throw new RuntimeException("Field '" + field + "' not found in " + o.getClass().getName());
}
static Object get_raw(String field, Object o) {
return get_raw(o, field);
}
static Object get_raw(Object o, String field) { try {
if (o == null) return null;
Field f = get_findField(o.getClass(), field);
makeAccessible(f);
return f.get(o);
} catch (Exception __e) { throw rethrow(__e); } }
static Object get(Class c, String field) {
try {
Field f = get_findStaticField(c, field);
makeAccessible(f);
return f.get(null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Field get_findStaticField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Static field '" + field + "' not found in " + c.getName());
}
static Field get_findField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field))
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Field '" + field + "' not found in " + c.getName());
}
static Object get(String field, Object o) {
return get(o, field);
}
static boolean get(BitSet bs, int idx) {
return bs != null && bs.get(idx);
}
static boolean isIdentifier(String s) {
return isJavaIdentifier(s);
}
static boolean eqOneOf(Object o, Object... l) {
if (l != null) for (Object x : l) if (eq(o, x)) return true; return false;
}
static boolean eq(Object a, Object b) {
return a == b || a != null && b != null && a.equals(b);
}
// a little kludge for stuff like eq(symbol, "$X")
static boolean eq(Symbol a, String b) {
return eq(str(a), b);
}
// TODO: more cases
static String tok_typeArgs_declToInvocation(String typeArgs) {
List tok = javaTok(typeArgs);
int i;
while ((i = jfind(tok, "extends ")) >= 0) {
int j = tok_endOfType(tok, i+2);
clearTokens(tok, i, j);
}
return join(tok);
}
static String tok_scanType(List tok, int iTypeStart) {
int iEndOfType = tok_endOfType(tok, iTypeStart);
return joinSubList_cToC(tok, iTypeStart, iEndOfType);
}
static boolean eqGet(List l, int i, Object o) {
return eq(get(l, i), o);
}
static boolean eqGet(Map map, A key, Object o) {
return eq(mapGet(map, key), o);
}
static String tok_dropTypeParameters(String type) {
return join(dropAfter("<", javaTok(type)));
}
static void tokSet_reTok(List tok, int i, String t) {
tokSet_withReTok(tok, i, t);
}
static AtomicInteger makeVar_varCount = new AtomicInteger();
static String makeVar() { return makeVar(""); }
static String makeVar(String name) {
return anonVar(name, incAndGet(makeVar_varCount));
}
static List jtok(String s) {
return javaTok(s);
}
static List jtok(List tok) {
return javaTok(tok);
}
static void assertJavaSourceEqualsVerbose_v2(String src, List tok) { assertJavaSourceEqualsVerbose_v2("", src, tok); }
static void assertJavaSourceEqualsVerbose_v2(String msg, String src, List tok) {
List tokens1 = javaTokC(src), tokens2 = codeTokens(tok);
int iDiff = indexOfDifference(tokens1, tokens2);
if (iDiff < 0) print(msg, "OK: \n" + join(tok));
else {
print(sfu(tok));
throw fail(prependWithColonSpace(msg) + "Error in token " + iDiff + " (" + get(tokens1, iDiff) + "/" + get(tokens2, iDiff) + "):\n"
+ "= got =\n"
+ join(tok) + "\n"
+ "= expected =\n"
+ src);
}
}
static String unnullForIteration(String s) {
return s == null ? "" : s;
}
static Collection unnullForIteration(Collection l) {
return l == null ? immutableEmptyList() : l;
}
static List unnullForIteration(List l) { return l == null ? immutableEmptyList() : l; }
static byte[] unnullForIteration(byte[] l) { return l == null ? emptyByteArray() : l; }
static int[] unnullForIteration(int[] l) { return l == null ? emptyIntArray() : l; }
static char[] unnullForIteration(char[] l) { return l == null ? emptyCharArray() : l; }
static double[] unnullForIteration(double[] l) { return l == null ? emptyDoubleArray() : l; }
static short[] unnullForIteration(short[] l) { return l == null ? emptyShortArray() : l; }
static Map unnullForIteration(Map l) {
return l == null ? immutableEmptyMap() : l;
}
static Iterable unnullForIteration(Iterable i) {
return i == null ? immutableEmptyList() : i;
}
static A[] unnullForIteration(A[] a) {
return a == null ? (A[]) emptyObjectArray() : a;
}
static BitSet unnullForIteration(BitSet b) {
return b == null ? new BitSet() : b;
}
static Pt unnullForIteration(Pt p) {
return p == null ? new Pt() : p;
}
//ifclass Symbol
static Symbol unnullForIteration(Symbol s) {
return s == null ? emptySymbol() : s;
}
//endif
static Pair unnullForIteration(Pair p) {
return p != null ? p : new Pair(null, null);
}
static long unnullForIteration(Long l) { return l == null ? 0L : l; }
static List> paramsToPairs(A... params) {
if (l(params) == 1 && params[0] instanceof Map)
return mapToPairs((Map) params[0]);
List> l = emptyList(l(params)/2);
for (int i = 0; i+1 < l(params); i += 2)
l.add(pair(params[i], params[i+1]));
return l;
}
static String applyTranspilationFunction(IVF1> f, String s) {
if (f == null) return s;
List tok = javaTok(s);
f.get(tok);
return join(tok);
}
// TODO: extended multi-line strings
static int javaTok_n, javaTok_elements;
static boolean javaTok_opt = false;
static List javaTok(String s) {
++javaTok_n;
ArrayList tok = new ArrayList();
int l = s == null ? 0 : s.length();
int i = 0;
while (i < l) {
int j = i;
char c, d;
// scan for whitespace
while (j < l) {
c = s.charAt(j);
d = j+1 >= l ? '\0' : s.charAt(j+1);
if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
++j;
else if (c == '/' && d == '*') {
do ++j; while (j < l && !regionMatches(s, j, "*/"));
j = Math.min(j+2, l);
} else if (c == '/' && d == '/') {
do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
} else
break;
}
tok.add(javaTok_substringN(s, i, j));
i = j;
if (i >= l) break;
c = s.charAt(i);
d = i+1 >= l ? '\0' : s.charAt(i+1);
// scan for non-whitespace
// Special JavaX syntax: 'identifier
if (c == '\'' && Character.isJavaIdentifierStart(d) && i+2 < l && "'\\".indexOf(s.charAt(i+2)) < 0) {
j += 2;
while (j < l && Character.isJavaIdentifierPart(s.charAt(j)))
++j;
} else if (c == '\'' || c == '"') {
char opener = c;
++j;
while (j < l) {
int c2 = s.charAt(j);
if (c2 == opener || c2 == '\n' && opener == '\'') { // allow multi-line strings, but not for '
++j;
break;
} else if (c2 == '\\' && j+1 < l)
j += 2;
else
++j;
}
} else if (Character.isJavaIdentifierStart(c))
do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || s.charAt(j) == '\'')); // for stuff like "don't"
else if (Character.isDigit(c)) {
do ++j; while (j < l && Character.isDigit(s.charAt(j)));
if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L
} else if (c == '[' && d == '[') {
do ++j; while (j < l && !regionMatches(s, j, "]]"));
j = Math.min(j+2, l);
} else if (c == '[' && d == '=' && i+2 < l && s.charAt(i+2) == '[') {
do ++j; while (j+2 < l && !regionMatches(s, j, "]=]"));
j = Math.min(j+3, l);
} else
++j;
tok.add(javaTok_substringC(s, i, j));
i = j;
}
if ((tok.size() % 2) == 0) tok.add("");
javaTok_elements += tok.size();
return tok;
}
static List javaTok(List tok) {
return javaTokWithExisting(join(tok), tok);
}
static ITokCondition tokCondition(IIntPred condition) {
return condition == null ? null : (tok, nIdx) -> condition.get(nIdx);
}
static Map javaTokForJFind_array_cache = synchronizedMRUCache(1000);
static String[] javaTokForJFind_array(String s) {
String[] tok = javaTokForJFind_array_cache.get(s);
if (tok == null)
javaTokForJFind_array_cache.put(s, tok = codeTokensAsStringArray(jfind_preprocess(javaTok(s))));
return tok;
}
static int findCodeTokens(List tok, String... tokens) {
return findCodeTokens(tok, 1, false, tokens);
}
static int findCodeTokens(List tok, boolean ignoreCase, String... tokens) {
return findCodeTokens(tok, 1, ignoreCase, tokens);
}
static int findCodeTokens(List tok, int startIdx, boolean ignoreCase, String... tokens) {
return findCodeTokens(tok, startIdx, ignoreCase, tokens, null);
}
static HashSet findCodeTokens_specials = lithashset("*", "", "", "", "\\*");
static int findCodeTokens_bails, findCodeTokens_nonbails;
static interface findCodeTokens_Matcher {
boolean get(String token);
}
static int findCodeTokens(List tok, int startIdx, boolean ignoreCase, String[] tokens, Object condition) {
int end = tok.size()-tokens.length*2+2, nTokens = tokens.length;
int i = startIdx | 1;
findCodeTokens_Matcher[] matchers = new findCodeTokens_Matcher[nTokens];
IContentsIndexedList2 indexedList = tok instanceof IContentsIndexedList2 ? (IContentsIndexedList2) tok : null;
TreeSet indices = null;
int indicesOfs = 0;
for (int j = 0; j < nTokens; j++) {
String p = tokens[j];
findCodeTokens_Matcher matcher;
if (p.equals("*"))
matcher = t -> true;
else if (p.equals(""))
matcher = t -> isQuoted(t);
else if (p.equals(""))
matcher = t -> isIdentifier(t);
else if (p.equals(""))
matcher = t -> isInteger(t);
else if (p.equals("\\*"))
matcher = t -> t.equals("*");
else if (ignoreCase)
matcher = t -> eqic(p, t);
else {
matcher = t -> t.equals(p);
if (indexedList != null) {
TreeSet indices2 = indexedList.indicesOf_treeSetOfHasIndex(p);
if (indices2 == null) return -1;
if (indices == null || indices2.size() < indices.size()) {
// found shorter list
indices = indices2;
indicesOfs = j;
}
}
}
matchers[j] = matcher;
}
// go over shortest index
if (indices != null) {
int min = i+indicesOfs*2;
SortedSet tailSet = min == 1 ? indices : indices.tailSet(new HasIndex(min));
outer: for (HasIndex h : tailSet) {
int idx = h.idx-indicesOfs*2;
if (idx >= end) break;
for (int j = 0; j < nTokens; j++) try {
if (!matchers[j].get(tok.get(idx+j*2)))
continue outer;
} catch (IndexOutOfBoundsException e) {
print("fct indicesOfs=" + indicesOfs + ", h=" + h + ", idx=" + idx);
throw e;
}
if (condition == null || checkTokCondition(condition, tok, idx-1))
return idx;
}
return -1;
}
outer: for (; i < end; i += 2) {
for (int j = 0; j < nTokens; j++)
if (!matchers[j].get(tok.get(i+j*2)))
continue outer;
if (condition == null || checkTokCondition(condition, tok, i-1)) // pass N index
return i;
}
return -1;
}
static String[] codeTokensAsStringArray(List tok) {
int n = max(0, (l(tok)-1)/2);
String[] out = new String[n];
for (int i = 0; i < n; i++)
out[i] = tok.get(i*2+1);
return out;
}
static ArrayList litlist(A... a) {
ArrayList l = new ArrayList(a.length);
for (A x : a) l.add(x);
return l;
}
// syntax 1: replace all occurrences of x in l with y
static List replaceSublist(List l, List x, List y) {
if (x == null) return l;
int i = 0;
while (true) {
i = indexOfSubList(l, x, i);
if (i < 0) break;
replaceSublist(l, i, i+l(x), y);
i += l(y);
}
return l;
}
// syntax 2: splice l at fromIndex-toIndex and replace middle part with y
static List replaceSublist(List l, int fromIndex, int toIndex, List y) {
int n = y.size(), toIndex_new = fromIndex+n;
if (toIndex_new < toIndex) {
removeSubList(l, toIndex_new, toIndex);
copyListPart(y, 0, l, fromIndex, n);
} else {
copyListPart(y, 0, l, fromIndex, toIndex-fromIndex);
if (toIndex_new > toIndex)
l.addAll(toIndex, subList(y, toIndex-fromIndex));
}
return l;
}
static List ll(A... a) {
ArrayList l = new ArrayList(a.length);
if (a != null) for (A x : a) l.add(x);
return l;
}
static int l(Object[] a) { return a == null ? 0 : a.length; }
static int l(boolean[] a) { return a == null ? 0 : a.length; }
static int l(byte[] a) { return a == null ? 0 : a.length; }
static int l(short[] a) { return a == null ? 0 : a.length; }
static int l(long[] a) { return a == null ? 0 : a.length; }
static int l(int[] a) { return a == null ? 0 : a.length; }
static int l(float[] a) { return a == null ? 0 : a.length; }
static int l(double[] a) { return a == null ? 0 : a.length; }
static int l(char[] a) { return a == null ? 0 : a.length; }
static int l(Collection c) { return c == null ? 0 : c.size(); }
static int l(Iterator i) { return iteratorCount_int_close(i); } // consumes the iterator && closes it if possible
static int l(Map m) { return m == null ? 0 : m.size(); }
static int l(CharSequence s) { return s == null ? 0 : s.length(); }
static long l(File f) { return f == null ? 0 : f.length(); }
static int l(IMultiMap mm) { return mm == null ? 0 : mm.size(); }
static int l(IntSize o) { return o == null ? 0 : o.size(); }
static Field getOpt_findField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field))
return f;
_c = _c.getSuperclass();
} while (_c != null);
return null;
}
static Field makeAccessible(Field f) {
try {
f.setAccessible(true);
} catch (Throwable e) {
// Note: The error reporting only works with Java VM option --illegal-access=deny
vmBus_send("makeAccessible_error", e, f);
}
return f;
}
static Method makeAccessible(Method m) {
try {
m.setAccessible(true);
} catch (Throwable e) {
vmBus_send("makeAccessible_error", e, m);
}
return m;
}
static Constructor makeAccessible(Constructor c) {
try {
c.setAccessible(true);
} catch (Throwable e) {
vmBus_send("makeAccessible_error", e, c);
}
return c;
}
static Object getOptDynOnly(DynamicObject o, String field) {
if (o == null || o.fieldValues == null) return null;
return o.fieldValues.get(field);
}
static RuntimeException asRuntimeException(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
return t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static RuntimeException rethrow(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static RuntimeException rethrow(String msg, Throwable t) {
throw new RuntimeException(msg, t);
}
static boolean isJavaIdentifier(String s) {
if (empty(s) || !Character.isJavaIdentifierStart(s.charAt(0)))
return false;
for (int i = 1; i < s.length(); i++)
if (!Character.isJavaIdentifierPart(s.charAt(i)))
return false;
return true;
}
static String str(Object o) {
return o == null ? "null" : o.toString();
}
static String str(char[] c) {
return c == null ? "null" : new String(c);
}
static String str(char[] c, int offset, int count) {
return new String(c, offset, count);
}
// return index of code token after type
static int tok_endOfType(List tok, int iTypeStart) {
return tok_findEndOfType(tok, iTypeStart);
}
static void clearTokens(List tok) {
clearAllTokens(tok);
}
static void clearTokens(List tok, int i, int j) {
clearAllTokens(tok, i, j);
}
public static String join(String glue, Iterable strings) {
if (strings == null) return "";
if (strings instanceof Collection) {
if (((Collection) strings).size() == 1) return strOrEmpty(first((Collection) strings));
}
StringBuilder buf = new StringBuilder();
Iterator i = strings.iterator();
if (i.hasNext()) {
buf.append(strOrEmpty(i.next()));
while (i.hasNext())
buf.append(glue).append(strOrEmpty(i.next()));
}
return buf.toString();
}
public static String join(String glue, String... strings) {
return join(glue, Arrays.asList(strings));
}
public static String join(String glue, Object... strings) {
return join(glue, Arrays.asList(strings));
}
static String join(Iterable strings) {
return join("", strings);
}
static String join(Iterable strings, String glue) {
return join(glue, strings);
}
public static String join(String[] strings) {
return join("", strings);
}
static String join(String glue, Pair p) {
return p == null ? "" : str(p.a) + glue + str(p.b);
}
static String joinSubList_cToC(List l, int i, int j) {
return joinSubList(l, i | 1, j & ~1);
}
static B mapGet(Map map, A a) {
return map == null || a == null ? null : map.get(a);
}
static B mapGet(A a, Map map) {
return map == null || a == null ? null : map.get(a);
}
static List dropAfter(A a, List l) {
int n = l(l);
for (int i = 0; i < n; i++)
if (eq(a, l.get(i)))
return takeFirst(l, i);
return l;
}
static void tokSet_withReTok(List tok, int i, String t) {
put(tok, i, t);
reTok(tok, i, i+1);
}
static String anonVar(int counter) { return anonVar("", counter); }
static String anonVar(String name, int counter) {
return "_" + name + "_" + stringIf(counter < 1, "_")
+ abs(counter-1);
}
static int incAndGet(AtomicInteger i) {
return i.incrementAndGet();
}
static List javaTokC(String s) {
if (s == null) return null;
int l = s.length();
ArrayList tok = new ArrayList();
int i = 0;
while (i < l) {
int j = i;
char c, d;
// scan for whitespace
while (j < l) {
c = s.charAt(j);
d = j+1 >= l ? '\0' : s.charAt(j+1);
if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
++j;
else if (c == '/' && d == '*') {
do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
j = Math.min(j+2, l);
} else if (c == '/' && d == '/') {
do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
} else
break;
}
i = j;
if (i >= l) break;
c = s.charAt(i);
d = i+1 >= l ? '\0' : s.charAt(i+1);
// scan for non-whitespace
if (c == '\'' || c == '"') {
char opener = c;
++j;
while (j < l) {
if (s.charAt(j) == opener || s.charAt(j) == '\n') { // end at \n to not propagate unclosed string literal errors
++j;
break;
} else if (s.charAt(j) == '\\' && j+1 < l)
j += 2;
else
++j;
}
} else if (Character.isJavaIdentifierStart(c))
do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); // for stuff like "don't"
else if (Character.isDigit(c)) {
do ++j; while (j < l && Character.isDigit(s.charAt(j)));
if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L
} else if (c == '[' && d == '[') {
do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
j = Math.min(j+2, l);
} else if (c == '[' && d == '=' && i+2 < l && s.charAt(i+2) == '[') {
do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]"));
j = Math.min(j+3, l);
} else
++j;
tok.add(javaTok_substringC(s, i, j));
i = j;
}
return tok;
}
static List codeTokens(List tok) {
return codeTokensOnly(tok);
}
static int indexOfDifference(Iterable i1, Iterable i2) {
var it1 = iterator(i1);
var it2 = iterator(i2);
int i = 0;
while (true) { ping();
boolean a = it1.hasNext(), b = it2.hasNext();
if (a != b)
return i;
if (!a)
return -1;
A el1 = it1.next();
A el2 = it2.next();
if (!eq(el1, el2))
return i;
++i;
}
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static boolean printAlsoToSystemOut = true;
static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot
// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
static boolean print_silent = false; // total mute if set
static Object print_byThread_lock = new Object();
static volatile ThreadLocal