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 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 javax.imageio.*; import java.math.*; import x30_pkg.x30_util; import static x30_pkg.x30_util.VF1; import static x30_pkg.x30_util.l; import static x30_pkg.x30_util.fail; import static x30_pkg.x30_util.indexOf; import static x30_pkg.x30_util.getOpt; import static x30_pkg.x30_util.setOpt; import static x30_pkg.x30_util.callOpt; import static x30_pkg.x30_util.newWeakHashMap; import static x30_pkg.x30_util.newDangerousWeakHashMap; import static x30_pkg.x30_util.get; import static x30_pkg.x30_util.get_raw; import static x30_pkg.x30_util.assertTrue; import static x30_pkg.x30_util.isHeadless; import static x30_pkg.x30_util.isAndroid; import static x30_pkg.x30_util.isTrue; import static x30_pkg.x30_util.asList; import x30_pkg.x30_util.DynamicObject; import loadableUtils.utils; import static loadableUtils.utils._threadInfo; import static loadableUtils.utils._threadInheritInfo; import static loadableUtils.utils._threadInfo_addMakerAndRetriever; import static loadableUtils.utils.dm_currentModule; import static loadableUtils.utils.dm_current_mandatory; import static loadableUtils.utils.match; import static loadableUtils.utils.getOpt_raw; import static loadableUtils.utils.setOpt_raw; import static loadableUtils.utils.getField; import static loadableUtils.utils.fieldType; import static loadableUtils.utils.format3; import static loadableUtils.utils.vm_generalIdentityHashSet; import static loadableUtils.utils.vm_generalHashMap; import static loadableUtils.utils.vm_generalWeakSubMap; import static loadableUtils.utils.bindToComponent; import static loadableUtils.utils.struct; import static loadableUtils.utils.structure; import static loadableUtils.utils.loadPage; import static loadableUtils.utils.loadPage_utf8; import static loadableUtils.utils.loadPageSilentlyWithTimeout; import static loadableUtils.utils.loadPageSilently; import static loadableUtils.utils.loadSnippet; import static loadableUtils.utils.loadSnippetQuietly; import static loadableUtils.utils.sendToLocalBot; import static loadableUtils.utils.componentPopupMenu; import static loadableUtils.utils.componentPopupMenu_top; import static loadableUtils.utils.componentPopupMenu_getEvent; import static loadableUtils.utils.listPopupMenu; import static loadableUtils.utils.tablePopupMenu; import static loadableUtils.utils.sexyTableWithoutDrag; import static loadableUtils.utils.dm_current_generic; import static loadableUtils.utils.dm_current_mandatory_generic; import static loadableUtils.utils.cset; import static loadableUtils.utils.DynamicObject_loading; import static loadableUtils.utils.concepts_unlisted; import static loadableUtils.utils.makePopupMenuConditional; import static loadableUtils.utils.makeConceptsTable_idWidth; import static loadableUtils.utils.showConceptsTable_afterUpdate; import static loadableUtils.utils.dynamicObjectIsLoading; import loadableUtils.utils.F0; import loadableUtils.utils.F1; import loadableUtils.utils.Matches; import loadableUtils.utils.BetterLabel; import loadableUtils.utils.SingleComponentPanel; import loadableUtils.utils.Snippet; import loadableUtils.utils.Q; import loadableUtils.utils.ImageSurface; import loadableUtils.utils.structure_Data; import loadableUtils.utils.RGBImage; import loadableUtils.utils.RGB; import loadableUtils.utils.BWImage; import loadableUtils.utils.MakesBufferedImage; import loadableUtils.utils.Concept; import loadableUtils.utils.Concepts; import loadableUtils.utils.IConceptIndex; import loadableUtils.utils.IFieldIndex; import loadableUtils.utils.Derefable; import loadableUtils.utils.ImageSurfaceSelector; import loadableUtils.utils.SimpleCRUD; import loadableUtils.utils.PersistableThrowable; import loadableUtils.utils.DynModule; import loadableUtils.utils.DynPrintLog; import loadableUtils.utils.DynObjectTable; import loadableUtils.utils.DynImageSurface; import loadableUtils.utils.DynCalculatedList; import java.text.NumberFormat; import javax.net.ssl.*; import java.security.SecureRandom; import java.security.cert.X509Certificate; import java.text.SimpleDateFormat; import java.nio.file.Files; import java.nio.file.Path; class main { public static class GLineRevisiter extends DynPrintLog { public transient GazelleContextCache_v2 contextMaker = new GazelleContextCache_v2(); public void start() { super.start(); dm_useLocalMechListCopies(); ownResource(contextMaker.listenToMessages()); contextMaker.alwaysFullRefresh(); dm_registerAs("gazelleLineRevisiter", "gazelleInputTester", "gazelleThoughtHelper"); } public List revisitChatLine(long msgID, Object... __) { return dm_evalInQ(new F0>() { public List get() { try { return dm_gazelle_revisitChatLine(msgID, paramsPlus(__, "contextMaker", contextMaker)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ret dm_gazelle_revisitChatLine(msgID, paramsPlus(_, +contextMaker));"; } }); } public List reason(String input, Object... __) { return dm_evalInQ(new F0>() { public List get() { try { return gazelle_reason_repeat(input, paramsPlus(__, "contextMaker", contextMaker, "interpretables_out", importCollector(optPar("interpretables_out", __)))); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ret gazelle_reason_repeat(input,\r\n paramsPlus(_,\r\n +contextMa..."; } }); } public List autoTestRule(String ruleID, Object... __) { return gazelle_autoTestRule(ruleID, paramsPlus(__, "contextMaker", contextMaker)); } } public static boolean _moduleClass_GLineRevisiter; public static Object callF(Object f, Object... args) { try { if (f instanceof String) return callMC((String) f, args); return x30_util.callF(f, args); } catch (Exception __e) { throw rethrow(__e); } } public static A callF(F0 f) { return f == null ? null : f.get(); } public static B callF(F1 f, A a) { return f == null ? null : f.get(a); } public static C callF(F2 f, A a, B b) { return f == null ? null : f.get(a, b); } public static void callF(VF1 f, A a) { if (f != null) f.get(a); } public static Object callMC(String method, Object... args) { return call(mc(), method, args); } public static Object call(Object o) { return callF(o); } public static Object call(Object o, String method, Object... args) { return call_withVarargs(o, method, args); } public static void _onLoad_initUtils() { utils.__javax = javax(); } public static void _onLoad_defaultClassFinder() { setDefaultClassFinder(new F1() { public Class get(String name) { try { Class c = findClass_fullName(name); if (c != null) return c; if (startsWith(name, "main$")) return loadableUtils.utils.findClass_fullName("loadableUtils.utils" + substring(name, 4)); return null; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Class c = findClass_fullName(name);\r\n if (c != null) ret c;\r\n if (start..."; } }); } public static String programID() { return getProgramID(); } public static String programID(Object o) { return getProgramID(o); } public static volatile StringBuffer local_log = new StringBuffer(); public static volatile Appendable print_log = local_log; public static volatile int print_log_max = 1024 * 1024; public static volatile int local_log_max = 100 * 1024; public static boolean print_silent; public static Object print_byThread_lock = new Object(); public static volatile ThreadLocal print_byThread; public static volatile Object print_allThreads; public static volatile Object print_preprocess; public static void print() { print(""); } public static A print(String s, A o) { print((endsWithLetterOrDigit(s) ? s + ": " : s) + o); return o; } public static A print(A o) { ping_okInCleanUp(); if (print_silent) return o; String s = String.valueOf(o) + "\n"; print_noNewLine(s); return o; } public static void print_noNewLine(String s) { Object f = getThreadLocal(print_byThread_dontCreate()); if (f == null) f = print_allThreads; if (f != null) if (isFalse(f instanceof F1 ? ((F1) f).get(s) : callF(f, s))) return; print_raw(s); } public static void print_raw(String s) { if (print_preprocess != null) s = (String) callF(print_preprocess, s); s = fixNewLines(s); Appendable loc = local_log; Appendable buf = print_log; int loc_max = print_log_max; if (buf != loc && buf != null) { print_append(buf, s, print_log_max); loc_max = local_log_max; } if (loc != null) print_append(loc, s, loc_max); System.out.print(s); } public static void dm_useLocalMechListCopies() { dm_useLocallyCopiedMechLists(); } public static void ownResource(AutoCloseable c) { _registerAutoCloseable(c); } public static AutoCloseable dm_registerAs(String... msgs) { F0 f = new F0() { public String get() { try { return dm_moduleID(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ret dm_moduleID();"; } }; return combineAutoCloseables(map(msgs, msg -> dm_vmBus_answerToMessage(msg, f))); } public static A dm_evalInQ(F0 f) { return dm_evalInQ(dm_current_mandatory(), f); } public static A dm_evalInQ(DynModule module, F0 f) { return evalInQ(module.q(), f); } public static List dm_gazelle_revisitChatLine(long msgID, Object... __) { Object line = dm_call(dm_gazelle_linesCRUD(), "lineForID", msgID); if (line == null) return null; long channelID = toLong(call(line, "channelID")); List lines = (List) (dm_call(dm_gazelle_linesCRUD(), "linesInChannel", channelID)); int i = indexOf(lines, line); if (i < 0) { print("Line not found in channel " + channelID); return null; } List preContext = collect("text", takeLast(10, takeFirst(i, lines))); print("preContext (last first): " + sfu(reversed(preContext))); return gazelle_reason_repeat(getString("text", line), paramsPlus(__, "userName", call(line, "userName"), "preContext", preContext)); } public static Object[] paramsPlus(Object[] a1, Object... a2) { if (a2 == null) return a1; if (a1 == null) return a2; if (l(a1) == 1 && a1[0] instanceof Map) return new Object[] { mapPlus((Map) a1[0], a2) }; assertEvenLength(a1); assertEvenLength(a2); Map map = paramsToOrderedMap(a1); int n = l(a2); for (int i = 0; i < n; i += 2) { Object key = a2[i]; if (key != null) map.put(key, a2[i + 1]); } return mapToParams(map); } public static RuntimeException rethrow(Throwable t) { if (t instanceof Error) _handleError((Error) t); throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t); } public static List gazelle_reason_repeat(String input, Object... __) { List out = new ArrayList(); int steps = 0, max = 10; while (steps++ < max) { addAll(out, (List) callF(optPar("sendToModules", __), input)); List l = gazelle_reasonWithPreAndPost(input, paramsMinus(__, "sendToModules")); if (empty(l)) break; gazelle_sortBeforeCommit(l, __); GazelleTree t = first(l); if (boolPar("debug", __) || boolPar("debugCommit", __)) print("Committing to: " + t.line + " (out of " + l(l) + ")"); List lines = tok_splitAtPlusAtBeginningOfLine(t.line); if (l(lines) == 2 && t.rule() != null && contains(t.rule().comments, "out 2 = process")) { print("splitting!"); t.line = first(lines); out.add(t); input = second(lines); continue; } out.add(t); break; } return out; } public static Collector importCollector(Object collector) { return new Collector() { public boolean add(Object o) { return isTrue(call(collector, "add", o)); } public boolean full() { return isTrue(call(collector, "full")); } public boolean contains(Object o) { return isTrue(call(collector, "contains", o)); } }; } public static A optPar(ThreadLocal tl, A defaultValue) { A a = tl.get(); if (a != null) { tl.set(null); return a; } return defaultValue; } public static A optPar(ThreadLocal tl) { return optPar(tl, null); } public static Object optPar(Object[] params, String name) { return optParam(params, name); } public static Object optPar(String name, Object[] params) { return optParam(params, name); } public static A optPar(Object[] params, String name, A defaultValue) { return optParam(params, name, defaultValue); } public static A optPar(String name, Object[] params, A defaultValue) { return optParam(params, name, defaultValue); } public static List gazelle_autoTestRule(String ruleID, Object... __) { List comments = gazelle_ruleComments(ruleID); List warnings = new ArrayList(); for (Matches m : getJMatches_all("var * should be in list *", comments)) { String var = m.unq(0), list = m.unq(1); if (!mechCISetContains(list, var)) warnings.add(quote(var) + " is not in list " + quote(list)); } return warnings; } public static Class mc() { return main.class; } public static Object call_withVarargs(Object o, String method, Object... args) { try { if (o == null) return null; if (o instanceof Class) { Class c = (Class) o; _MethodCache cache = callOpt_getCache(c); Method me = cache.findStaticMethod(method, args); if (me != null) return invokeMethod(me, null, args); List methods = cache.cache.get(method); if (methods != null) methodSearch: for (Method m : methods) { { if (!(m.isVarArgs())) continue; } { if (!(isStaticMethod(m))) continue; } Object[] newArgs = massageArgsForVarArgsCall(m, args); if (newArgs != null) return invokeMethod(m, null, newArgs); } throw fail("Method " + c.getName() + "." + method + "(" + joinWithComma(classNames(args)) + ") not found"); } else { Class c = o.getClass(); _MethodCache cache = callOpt_getCache(c); Method me = cache.findMethod(method, args); if (me != null) return invokeMethod(me, o, args); List methods = cache.cache.get(method); if (methods != null) methodSearch: for (Method m : methods) { { if (!(m.isVarArgs())) continue; } Object[] newArgs = massageArgsForVarArgsCall(m, args); if (newArgs != null) return invokeMethod(m, o, newArgs); } throw fail("Method " + c.getName() + "." + method + "(" + joinWithComma(classNames(args)) + ") not found"); } } catch (Exception __e) { throw rethrow(__e); } } public static Class javax() { return getJavaX(); } public static void setDefaultClassFinder(Object cf) { _defaultClassFinder_value = cf; } public static HashMap findClass_fullName_cache = new HashMap(); public static Class findClass_fullName(String name) { synchronized (findClass_fullName_cache) { if (findClass_fullName_cache.containsKey(name)) return findClass_fullName_cache.get(name); Class c; try { c = Class.forName(name); } catch (ClassNotFoundException e) { c = null; } findClass_fullName_cache.put(name, c); return c; } } public static boolean startsWith(String a, String b) { return a != null && a.startsWith(b); } public static boolean startsWith(String a, char c) { return nemptyString(a) && a.charAt(0) == c; } public static boolean startsWith(String a, String b, Matches m) { if (!startsWith(a, b)) return false; m.m = new String[] { substring(a, strL(b)) }; return true; } public static boolean startsWith(List a, List b) { if (a == null || listL(b) > listL(a)) return false; for (int i = 0; i < listL(b); i++) if (neq(a.get(i), b.get(i))) return false; return true; } public static String substring(String s, int x) { return substring(s, x, strL(s)); } public static String substring(String s, int x, int y) { if (s == null) return null; if (x < 0) x = 0; if (x >= s.length()) return ""; if (y < x) y = x; if (y > s.length()) y = s.length(); return s.substring(x, y); } public static Object _defaultClassFinder_value; public static Object _defaultClassFinder() { return _defaultClassFinder_value; } public static String programID; public static String getProgramID() { return nempty(programID) ? formatSnippetIDOpt(programID) : "?"; } public static String getProgramID(Class c) { String id = (String) getOpt(c, "programID"); if (nempty(id)) return formatSnippetID(id); return "?"; } public static String getProgramID(Object o) { return getProgramID(getMainClass(o)); } public static boolean endsWithLetterOrDigit(String s) { return s != null && s.length() > 0 && Character.isLetterOrDigit(s.charAt(s.length() - 1)); } public static void ping_okInCleanUp() { if (ping_pauseAll || ping_anyActions) ping_impl(true); } public static Object getThreadLocal(Object o, String name) { ThreadLocal t = (ThreadLocal) (getOpt(o, name)); return t != null ? t.get() : null; } public static A getThreadLocal(ThreadLocal tl) { return tl == null ? null : tl.get(); } public static ThreadLocal print_byThread_dontCreate() { return print_byThread; } public static boolean isFalse(Object o) { return eq(false, o); } public static String fixNewLines(String s) { int i = indexOf(s, '\r'); if (i < 0) return s; int l = s.length(); StringBuilder out = new StringBuilder(l); out.append(s, 0, i); for (; i < l; i++) { char c = s.charAt(i); if (c != '\r') out.append(c); else { out.append('\n'); if (i + 1 < l && s.charAt(i + 1) == '\n') ++i; } } return out.toString(); } public static void print_append(Appendable _buf, String s, int max) { try { synchronized (_buf) { _buf.append(s); if (!(_buf instanceof StringBuilder)) return; StringBuilder buf = (StringBuilder) _buf; max /= 2; if (buf.length() > max) try { int newLength = max / 2; int ofs = buf.length() - newLength; String newString = buf.substring(ofs); buf.setLength(0); buf.append("[...] ").append(newString); } catch (Exception e) { buf.setLength(0); } } } catch (Exception __e) { throw rethrow(__e); } } public static void dm_useLocallyCopiedMechLists() { dm_requireModule("#1016073/MechLists"); useLocalMechListCopies(true); setOptMC("serverMechList_raw_fresh_verbose", true); setOptMC("mechList_goToCreator_dont", true); } public static Set _registerAutoCloseable_set = synchroHashSet(); public static void _registerAutoCloseable(AutoCloseable c) { addIfNotNull(_registerAutoCloseable_set, c); } public static void cleanMeUp__registerAutoCloseable() { closeAutoCloseables(getAndClearList(_registerAutoCloseable_set)); } public static String dm_moduleID(Object module) { return strOrNull(getOpt(dm_getStem(module), "id")); } public static String dm_moduleID() { return dm_moduleID(dm_current_mandatory()); } public static AutoCloseable combineAutoCloseables(final AutoCloseable a, final AutoCloseable b) { return a == null ? b : b == null ? a : new AutoCloseable() { public String toString() { return "a.close(); b.close();"; } public void close() throws Exception { a.close(); b.close(); } }; } public static AutoCloseable combineAutoCloseables(AutoCloseable a, AutoCloseable b, AutoCloseable c, AutoCloseable... more) { return combineAutoCloseables(concatLists(ll(a, b, c), asList(more))); } public static AutoCloseable combineAutoCloseables(Iterable l) { return foldl(new F2() { public AutoCloseable get(AutoCloseable a, AutoCloseable b) { try { return combineAutoCloseables(a, b); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "combineAutoCloseables(a,b)"; } }, null, l); } public static List map(Iterable l, Object f) { return map(f, l); } public static List map(Object f, Iterable l) { List x = emptyList(l); if (l != null) for (Object o : l) x.add(callF(f, o)); return x; } public static List map(Iterable l, F1 f) { return map(f, l); } public static List map(F1 f, Iterable l) { List x = emptyList(l); if (l != null) for (A o : l) x.add(callF(f, o)); return x; } public static List map(Iterable l, IF1 f) { List x = emptyList(l); if (l != null) for (A o : l) x.add(f.get(o)); return x; } public static List map(A[] l, IF1 f) { List x = emptyList(l); if (l != null) for (A o : l) x.add(f.get(o)); return x; } public static List map(Object f, Object[] l) { return map(f, asList(l)); } public static List map(Object[] l, Object f) { return map(f, l); } public static List map(Object f, Map map) { return map(map, f); } public static List map(Map map, Object f) { List x = new ArrayList(); if (map != null) for (Object _e : map.entrySet()) { Map.Entry e = (Map.Entry) _e; x.add(callF(f, e.getKey(), e.getValue())); } return x; } public static AutoCloseable dm_vmBus_answerToMessage(String msg, final F0 f) { final DynModule m = dm_current_mandatory(); return dm_ownResource(vmBus_addListener_basic(msg, new F2() { public Object get(String _msg, Object arg) { try { AutoCloseable __12 = m.enter(); try { return callF(f); } finally { _close(__12); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "temp m.enter();\r\n ret callF(f);"; } })); } public static AutoCloseable dm_vmBus_answerToMessage(String msg, final F1 f) { final DynModule m = dm_current_mandatory(); return dm_ownResource(vmBus_addListener_basic(msg, new F2() { public Object get(String _msg, Object arg) { try { AutoCloseable __13 = m.enter(); try { return callF(f, arg); } finally { _close(__13); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "temp m.enter();\r\n ret callF(f, arg);"; } })); } public static A evalInQ(Q q, final F0 f) { if (isInQ(q)) return callF(f); final Var> var = new Var(); q.add(new Runnable() { public void run() { try { try { var.set(main.eitherA(callF(f))); } catch (Throwable e) { var.set(main.eitherB(e)); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "try {\r\n var.set(main. eitherA(callF(f)));\r\n } catch (Th..."; } }); return returnOrThrow_either(waitForVarToBeNotNull(var)); } public static Object dm_call(Object moduleOrID, String method, Object... args) { Object mod = dm_getModule(moduleOrID); if (mod == null) return null; AutoCloseable __134 = dm_enter(mod); try { return call_withVarargs(mod, method, args); } finally { _close(__134); } } public static String dm_gazelle_linesCRUD() { return dm_require("#1021628/GLines"); } public static long toLong(Object o) { if (o instanceof Number) return ((Number) o).longValue(); if (o instanceof String) return parseLong((String) o); return 0; } public static List collect(Collection c, String field) { return collectField(c, field); } public static List collect(String field, Collection c) { return collectField(c, field); } public static List takeLast(List l, int n) { return newSubList(l, l(l) - n); } public static List takeLast(int n, List l) { return takeLast(l, n); } public static String takeLast(int n, String s) { return substring(s, l(s) - n); } public static String takeLast(String s, int n) { return substring(s, l(s) - n); } public static List takeFirst(List l, int n) { return l(l) <= n ? l : newSubListOrSame(l, 0, n); } public static List takeFirst(int n, List l) { return takeFirst(l, n); } public static String takeFirst(int n, String s) { return substring(s, 0, n); } public static String takeFirst(String s, int n) { return substring(s, 0, n); } public static List takeFirst(int n, Iterable i) { List l = new ArrayList(); Iterator it = i.iterator(); for (int _repeat_739 = 0; _repeat_739 < n; _repeat_739++) { if (it.hasNext()) l.add(it.next()); else break; } return l; } public static String sfu(Object o) { return structureForUser(o); } public static List reversed(Collection l) { return reversedList(l); } public static List reversed(A[] l) { return reversedList(asList(l)); } public static String reversed(String s) { return reversedString(s); } public static String getString(Map map, Object key) { return map == null ? null : (String) map.get(key); } public static String getString(List l, int idx) { return (String) get(l, idx); } public static String getString(Object o, Object key) { if (o instanceof Map) return getString((Map) o, key); if (key instanceof String) return (String) getOpt(o, (String) key); throw fail("Not a string key: " + getClassName(key)); } public static String getString(String key, Object o) { return getString(o, (Object) key); } public static Map mapPlus(Map m, Object... data) { m = cloneMap(m); litmap_impl(m, data); return m; } public static Object[] assertEvenLength(Object[] a) { assertTrue(even(l(a))); return a; } public static LinkedHashMap paramsToOrderedMap(Object... params) { return asLinkedHashMap(paramsToMap(params)); } public static Object[] mapToParams(Map map) { return mapToObjectArray(map); } public static void _handleError(Error e) { call(javax(), "_handleError", e); } public static void addAll(Collection c, Iterable b) { if (c != null && b != null) for (A a : b) c.add(a); } public static boolean addAll(Collection c, Collection b) { return c != null && b != null && c.addAll(b); } public static boolean addAll(Collection c, B... b) { return c != null && c.addAll(Arrays.asList(b)); } public static void addAll(Collector c, Iterable b) { if (c != null && b != null) for (A a : b) c.add(a); } public static List gazelle_reasonWithPreAndPost(String input, Object... __) { return gazelle_postprocess(gazelle_reasonWithPreprocessing(input, __), __); } public static Object[] paramsMinus(Object[] a1, Object... keys) { return paramsWithout(a1, keys); } public static boolean empty(Collection c) { return c == null || c.isEmpty(); } public static boolean empty(CharSequence s) { return s == null || s.length() == 0; } public static boolean empty(Map map) { return map == null || map.isEmpty(); } public static boolean empty(Object[] o) { return o == null || o.length == 0; } public static boolean empty(Object o) { if (o instanceof Collection) return empty((Collection) o); if (o instanceof String) return empty((String) o); if (o instanceof Map) return empty((Map) o); if (o instanceof Object[]) return empty((Object[]) o); if (o instanceof byte[]) return empty((byte[]) o); if (o == null) return true; throw fail("unknown type for 'empty': " + getType(o)); } public static boolean empty(float[] a) { return a == null || a.length == 0; } public static boolean empty(int[] a) { return a == null || a.length == 0; } public static boolean empty(long[] a) { return a == null || a.length == 0; } public static boolean empty(byte[] a) { return a == null || a.length == 0; } public static boolean empty(MultiSet ms) { return ms == null || ms.isEmpty(); } public static boolean empty(File f) { return getFileSize(f) == 0; } public static void gazelle_sortBeforeCommit(List l, Object... __) { boolean debug = boolPar("debugSorting", __); sort(l, new Comparator() { public int compare(GazelleTree a, GazelleTree b) { if (debug) print("sorting: " + a.line + " / " + b.line); int c = ai_goodBadToInt(b.prediction) - ai_goodBadToInt(a.prediction); if (debug) print(" " + c); if (c != 0) return c; c = gazelle_historyLevelRequired(b.rule()) - gazelle_historyLevelRequired(a.rule()); if (debug) print(" " + c); return c; } }); } public static Object first(Object list) { return first((Iterable) list); } public static A first(List list) { return empty(list) ? null : list.get(0); } public static A first(A[] bla) { return bla == null || bla.length == 0 ? null : bla[0]; } public static A first(IterableIterator i) { return first((Iterator) i); } public static A first(Iterator i) { return i == null || !i.hasNext() ? null : i.next(); } public static A first(Iterable i) { if (i == null) return null; Iterator it = i.iterator(); return it.hasNext() ? it.next() : null; } public static Character first(String s) { return empty(s) ? null : s.charAt(0); } public static A first(Pair p) { return p == null ? null : p.a; } public static A first(T3 t) { return t == null ? null : t.a; } public static boolean boolPar(ThreadLocal tl) { return boolOptParam(tl); } public static boolean boolPar(Object[] __, String name) { return boolOptParam(__, name); } public static boolean boolPar(String name, Object[] __) { return boolOptParam(__, name); } public static boolean boolPar(String name, Object[] params, boolean defaultValue) { return optParam(params, name, defaultValue); } public static List tok_splitAtPlusAtBeginningOfLine(String s) { List indices = new ArrayList(); List tok = javaTokWithAllBrackets(s); for (int i = 1; i < l(tok); i += 2) if (eqGet(tok, i, "+") && containsNewLine(get(tok, i - 1))) indices.add(i); return splitAtTokenIndices(tok, indices); } public static boolean contains(Collection c, Object o) { return c != null && c.contains(o); } public static boolean contains(Object[] x, Object o) { if (x != null) for (Object a : x) if (eq(a, o)) return true; return false; } public static boolean contains(String s, char c) { return s != null && s.indexOf(c) >= 0; } public static boolean contains(String s, String b) { return s != null && s.indexOf(b) >= 0; } public static boolean contains(BitSet bs, int i) { return bs != null && bs.get(i); } public static A second(List l) { return get(l, 1); } public static A second(Iterable l) { if (l == null) return null; Iterator it = iterator(l); if (!it.hasNext()) return null; it.next(); return it.hasNext() ? it.next() : null; } public static A second(A[] bla) { return bla == null || bla.length <= 1 ? null : bla[1]; } public static B second(Pair p) { return p == null ? null : p.b; } public static B second(T3 t) { return t == null ? null : t.b; } public static A optParam(ThreadLocal tl, A defaultValue) { return optPar(tl, defaultValue); } public static A optParam(ThreadLocal tl) { return optPar(tl); } public static A optParam(Object[] opt, String name, A defaultValue) { int n = l(opt); if (n == 1 && opt[0] instanceof Map) { Map map = (Map) (opt[0]); return map.containsKey(name) ? (A) map.get(name) : defaultValue; } if (!even(l(opt))) throw fail("Odd parameter length"); for (int i = 0; i < l(opt); i += 2) if (eq(opt[i], name)) return (A) opt[i + 1]; return defaultValue; } public static Object optParam(Object[] opt, String name) { return optParam(opt, name, null); } public static List gazelle_ruleComments(String ruleID) { return dm_commentsForRule(ruleID); } public static List getJMatches_all(String pat, Iterable l) { Matches m = new Matches(); List out = new ArrayList(); for (String s : unnull(l)) if (jmatch(pat, s, m)) { out.add(m); m = new Matches(); } return out; } public static boolean mechCISetContains(String listName, String a) { return mechCISet(listName).contains(a); } public static String quote(Object o) { if (o == null) return "null"; return quote(str(o)); } public static String quote(String s) { if (s == null) return "null"; StringBuilder out = new StringBuilder((int) (l(s) * 1.5 + 2)); quote_impl(s, out); return out.toString(); } public static void quote_impl(String s, StringBuilder out) { out.append('"'); int l = s.length(); for (int i = 0; i < l; i++) { char c = s.charAt(i); if (c == '\\' || c == '"') out.append('\\').append(c); else if (c == '\r') out.append("\\r"); else if (c == '\n') out.append("\\n"); else if (c == '\0') out.append("\\0"); else out.append(c); } out.append('"'); } public static final Map callOpt_cache = newDangerousWeakHashMap(); public static Object callOpt_cached(Object o, String methodName, Object... args) { try { if (o == null) return null; if (o instanceof Class) { Class c = (Class) o; _MethodCache cache = callOpt_getCache(c); Method me = cache.findMethod(methodName, args); if (me == null || (me.getModifiers() & Modifier.STATIC) == 0) return null; return invokeMethod(me, null, args); } else { Class c = o.getClass(); _MethodCache cache = callOpt_getCache(c); Method me = cache.findMethod(methodName, args); if (me == null) return null; return invokeMethod(me, o, args); } } catch (Exception __e) { throw rethrow(__e); } } public static _MethodCache callOpt_getCache(Class c) { synchronized (callOpt_cache) { _MethodCache cache = callOpt_cache.get(c); if (cache == null) callOpt_cache.put(c, cache = new _MethodCache(c)); return cache; } } public static Object invokeMethod(Method m, Object o, Object... args) { try { try { return m.invoke(o, args); } catch (InvocationTargetException e) { throw rethrow(getExceptionCause(e)); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(e.getMessage() + " - was calling: " + m + ", args: " + joinWithSpace(classNames(args))); } } catch (Exception __e) { throw rethrow(__e); } } public static boolean isStaticMethod(Method m) { return methodIsStatic(m); } public static Object[] massageArgsForVarArgsCall(Method m, Object[] args) { Class[] types = m.getParameterTypes(); int n = types.length - 1, nArgs = args.length; if (nArgs < n) return null; for (int i = 0; i < n; i++) if (!argumentCompatibleWithType(args[i], types[i])) return null; Class varArgType = types[n].getComponentType(); for (int i = n; i < nArgs; i++) if (!argumentCompatibleWithType(args[i], varArgType)) return null; Object[] newArgs = new Object[n + 1]; arraycopy(args, 0, newArgs, 0, n); Object[] varArgs = arrayOfType(varArgType, nArgs - n); arraycopy(args, n, varArgs, 0, nArgs - n); newArgs[n] = varArgs; return newArgs; } public static String joinWithComma(Collection c) { return join(", ", c); } public static String joinWithComma(String... c) { return join(", ", c); } public static String joinWithComma(Pair p) { return p == null ? "" : joinWithComma(str(p.a), str(p.b)); } public static List classNames(Collection l) { return getClassNames(l); } public static List classNames(Object[] l) { return getClassNames(Arrays.asList(l)); } public static Class __javax; public static Class getJavaX() { try { return __javax; } catch (Exception __e) { throw rethrow(__e); } } public static boolean nemptyString(String s) { return s != null && s.length() > 0; } public static int strL(String s) { return s == null ? 0 : s.length(); } public static int listL(Collection l) { return l == null ? 0 : l.size(); } public static boolean neq(Object a, Object b) { return !eq(a, b); } public static boolean nempty(Collection c) { return !empty(c); } public static boolean nempty(CharSequence s) { return !empty(s); } public static boolean nempty(Object[] o) { return !empty(o); } public static boolean nempty(byte[] o) { return !empty(o); } public static boolean nempty(int[] o) { return !empty(o); } public static boolean nempty(Map m) { return !empty(m); } public static boolean nempty(Iterator i) { return i != null && i.hasNext(); } public static boolean nempty(Object o) { return !empty(o); } public static String formatSnippetIDOpt(String s) { return isSnippetID(s) ? formatSnippetID(s) : s; } public static String formatSnippetID(String id) { return "#" + parseSnippetID(id); } public static String formatSnippetID(long id) { return "#" + id; } public static Class getMainClass() { return mc(); } public static Class getMainClass(Object o) { try { if (o == null) return null; if (o instanceof Class && eq(((Class) o).getName(), "x30")) return (Class) o; return (o instanceof Class ? (Class) o : o.getClass()).getClassLoader().loadClass("main"); } catch (Exception __e) { throw rethrow(__e); } } public static volatile boolean ping_pauseAll; public static int ping_sleep = 100; public static volatile boolean ping_anyActions; public static Map ping_actions = newWeakHashMap(); public static ThreadLocal ping_isCleanUpThread = new ThreadLocal(); public static boolean ping() { if (ping_pauseAll || ping_anyActions) ping_impl(true); return true; } public static boolean ping_impl(boolean okInCleanUp) { try { if (ping_pauseAll && !isAWTThread()) { do Thread.sleep(ping_sleep); while (ping_pauseAll); return true; } if (ping_anyActions) { if (!okInCleanUp && !isTrue(ping_isCleanUpThread.get())) failIfUnlicensed(); Object action = null; synchronized (ping_actions) { if (!ping_actions.isEmpty()) { action = ping_actions.get(currentThread()); if (action instanceof Runnable) ping_actions.remove(currentThread()); if (ping_actions.isEmpty()) ping_anyActions = false; } } if (action instanceof Runnable) ((Runnable) action).run(); else if (eq(action, "cancelled")) throw fail("Thread cancelled."); } return false; } catch (Exception __e) { throw rethrow(__e); } } public static boolean eq(Object a, Object b) { return a == null ? b == null : a == b || b != null && a.equals(b); } public static String dm_requireModule(String moduleLibID) { return dm_makeModule(moduleLibID); } public static boolean useLocalMechListCopies_flag; public static boolean useLocalMechListCopies() { return useLocalMechListCopies_flag; } public static void useLocalMechListCopies(boolean flag) { useLocalMechListCopies_flag = flag; if (flag) mechMode().readMode = "localCopies"; } public static void setOptMC(String field, Object value) { setOpt(mc(), field, value); } public static Set synchroHashSet() { return Collections.synchronizedSet(new HashSet()); } public static void addIfNotNull(Collection l, A a) { if (a != null && l != null) l.add(a); } public static void addIfNotNull(MultiSet ms, A a) { if (a != null && ms != null) ms.add(a); } public static void closeAutoCloseables(Collection l) { if (l != null) for (AutoCloseable c : l) { try { c.close(); } catch (Throwable __e) { _handleException(__e); } } } public static List getAndClearList(Collection l) { if (l == null) return emptyList(); synchronized (collectionMutex(l)) { List out = cloneList(l); l.clear(); return out; } } public static String strOrNull(Object o) { return o == null ? null : str(o); } public static Object dm_getStem(Object moduleOrID) { if (isStringOrIntOrLong(moduleOrID)) return dm_getStemByID(moduleOrID); return or(getOpt(dm_getModule(moduleOrID), "_host"), moduleOrID); } public static List concatLists(Collection... lists) { List l = new ArrayList(); if (lists != null) for (Collection list : lists) if (list != null) l.addAll(list); return l; } public static List concatLists(Collection> lists) { List l = new ArrayList(); if (lists != null) for (Collection list : lists) if (list != null) l.addAll(list); return l; } public static List ll(A... a) { ArrayList l = new ArrayList(a.length); for (A x : a) l.add(x); return l; } public static A foldl(F2 f, A seed, Iterable l) { A a = seed; if (l != null) for (B b : l) a = callF(f, a, b); return a; } public static ArrayList emptyList() { return new ArrayList(); } public static ArrayList emptyList(int capacity) { return new ArrayList(max(0, capacity)); } public static ArrayList emptyList(Iterable l) { return l instanceof Collection ? emptyList(((Collection) l).size()) : emptyList(); } public static ArrayList emptyList(Object[] l) { return emptyList(l(l)); } public static ArrayList emptyList(Class c) { return new ArrayList(); } public static AutoCloseable dm_ownResource(AutoCloseable resource) { dm_currentModuleMandatory().ownResource(resource); return resource; } public static AutoCloseable vmBus_addListener_basic(String msg, Object listener) { Map map = vm_busListenersByMessage_live(); synchronized (map) { Set listeners = map.get(msg); if (listeners == null) map.put(msg, listeners = syncIdentityHashSet()); return tempAdd(listeners, listener); } } public static void _close(AutoCloseable c) { if (c != null) try { c.close(); } catch (Throwable e) { if (c instanceof javax.imageio.stream.ImageOutputStream) return; else throw rethrow(e); } } public static boolean isInQ(Q q) { return q != null && isCurrentThread(q.rst.thread); } public static Either eitherA(A a) { return new Either(1, a); } public static Either eitherB(B b) { return new Either(2, b); } public static A returnOrThrow_either(Either e) { if (isEitherB(e)) throw rethrow(e.b()); return eitherAOpt(e); } public static A waitForVarToBeNotNull(Var v) { try { synchronized (v) { while (!v.has()) v.wait(); return v.get(); } } catch (Exception __e) { throw rethrow(__e); } } public static Object dm_getModule(Object moduleOrID) { if (moduleOrID == null || eq(moduleOrID, "")) return null; if (isString(moduleOrID) && isIdentifier(((String) moduleOrID))) return dm_getService(((String) moduleOrID)); if (isStringOrIntOrLong(moduleOrID)) return dm_callOS("getDynModuleByID", str(moduleOrID)); return dm_resolveModule(moduleOrID); } public static AutoCloseable dm_enter(Object mod) { return (AutoCloseable) callOpt(dm_getModule(mod), "enter"); } public static String dm_require(String moduleLibID) { return dm_makeModule(moduleLibID); } public static long parseLong(String s) { if (s == null) return 0; return Long.parseLong(dropSuffix("L", s)); } public static long parseLong(Object s) { return Long.parseLong((String) s); } public static List collectField(Collection c, String field) { List l = new ArrayList(); if (c != null) for (Object a : c) l.add(getOpt(a, field)); return l; } public static List collectField(String field, Collection c) { return collectField(c, field); } public static List newSubList(List l, int startIndex, int endIndex) { return cloneList(subList(l, startIndex, endIndex)); } public static List newSubList(List l, int startIndex) { return cloneList(subList(l, startIndex)); } public static List newSubListOrSame(List l, int startIndex) { return newSubListOrSame(l, startIndex, l(l)); } public static List newSubListOrSame(List l, int startIndex, int endIndex) { if (l == null) return null; int n = l(l); startIndex = max(0, startIndex); endIndex = min(n, endIndex); if (startIndex >= endIndex) return ll(); if (startIndex == 0 && endIndex == n) return l; return cloneList(l.subList(startIndex, endIndex)); } public static String structureForUser(Object o) { return beautifyStructure(struct_noStringSharing(o)); } public static List reversedList(Collection l) { List x = cloneList(l); Collections.reverse(x); return x; } public static String reversedString(String s) { return reverseString(s); } public static String getClassName(Object o) { return o == null ? "null" : o instanceof Class ? ((Class) o).getName() : o.getClass().getName(); } public static Map cloneMap(Map map) { if (map == null) return new HashMap(); synchronized (map) { return map instanceof TreeMap ? new TreeMap((TreeMap) map) : map instanceof LinkedHashMap ? new LinkedHashMap(map) : new HashMap(map); } } public static HashMap litmap(Object... x) { HashMap map = new HashMap(); litmap_impl(map, x); return map; } public static void litmap_impl(Map map, Object... x) { for (int i = 0; i < x.length - 1; i += 2) if (x[i + 1] != null) map.put(x[i], x[i + 1]); } public static boolean even(int i) { return (i & 1) == 0; } public static boolean even(long i) { return (i & 1) == 0; } public static LinkedHashMap asLinkedHashMap(Map map) { if (map instanceof LinkedHashMap) return (LinkedHashMap) map; LinkedHashMap m = new LinkedHashMap(); if (map != null) synchronized (collectionMutex(map)) { m.putAll(map); } return m; } public static Map paramsToMap(Object... params) { int n = l(params); if (l(params) == 1 && params[0] instanceof Map) return (Map) params[0]; LinkedHashMap map = new LinkedHashMap(); for (int i = 0; i + 1 < n; i += 2) mapPut(map, params[i], params[i + 1]); return map; } public static Object[] mapToObjectArray(Map map) { List l = new ArrayList(); for (Object o : keys(map)) { l.add(o); l.add(map.get(o)); } return toObjectArray(l); } public static List gazelle_postprocess(List l, Object... __) { l = takeFirst_clone(10, l); if (!boolPar("noEvals", __)) gazelle_processSafeEvalsInTree(l); return l; } public static List gazelle_reasonWithPreprocessing(String input1, Object... __) { F0 contextMaker = (F0) (assertNotNull("Need contextMaker", optPar("contextMaker", __))); String userName = stringPar("userName", __); boolean debug = boolPar("debug", __); boolean debugPreprocessing = boolPar("debugPreprocessing", __); Collector interpretables_out = (Collector) (optPar("interpretables_out", __)); Collection interpretables = gazelle_preprocess(input1, __); if (debugPreprocessing) { print("Got " + n2(interpretables, "interpretable") + "."); printIndentLines(interpretables); } if (interpretables != null) addAll(interpretables_out, interpretables); Set acceptablePurposes = (Set) (optPar("acceptablePurposes", __)); if (acceptablePurposes == null) acceptablePurposes = litset(""); List l2 = new ArrayList(); Set outputs = ciSet(); for (GInterpretable intp : interpretables) { List results = dm_gazelle_reasonAboutChatInput_v2(userName, intp.text, paramsPlus(__, "acceptablePurposes", acceptablePurposes)); print(intp.text + " => " + n2(results, "result")); for (GazelleTree tree : results) if (outputs.add(tree.line)) { if (tree.mr != null) tree.mr.interpretable = intp; l2.add(tree); } } gazelle_dropMatchedDefaultRules(l2, __); return l2; } public static Object[] paramsWithout(Object[] a1, Object... keys) { if (l(a1) == 1 && a1[0] instanceof Map) return new Object[] { mapMinus((Map) a1[0], keys) }; Set set = lithashset(keys); List l = new ArrayList(); for (int i = 0; i + 1 < l(a1); i += 2) if (!set.contains(a1[i])) { l.add(a1[i]); l.add(a1[i + 1]); } return toObjectArray(l); } public static String getType(Object o) { return getClassName(o); } public static long getFileSize(String path) { return path == null ? 0 : new File(path).length(); } public static long getFileSize(File f) { return f == null ? 0 : f.length(); } public static void sort(T[] a, Comparator c) { Arrays.sort(a, c); } public static void sort(T[] a) { Arrays.sort(a); } public static void sort(List a, Comparator c) { Collections.sort(a, c); } public static void sort(List a) { Collections.sort(a); } public static int ai_goodBadToInt(String s) { if (eq(s, "good")) return 1; if (eq(s, "bad")) return -1; return 0; } public static int gazelle_historyLevelRequired(RuleEngine2.Rule rule) { if (rule == null) return 0; int n = 0; Matches m = new Matches(); for (GRuleLine l : unnull(rule.insWithType)) if (eq(l.type, "standard")) n = max(n, 1); else if (startsWith(l.type, "history-", m)) n = max(n, parseIntOpt(m.rest())); return n; } public static boolean boolOptParam(ThreadLocal tl) { return isTrue(optPar(tl)); } public static boolean boolOptParam(Object[] __, String name) { return isTrue(optParam(__, name)); } public static boolean boolOptParam(String name, Object[] __) { return boolOptParam(__, name); } public static List javaTokWithAllBrackets(String s) { return javaTokPlusBrackets2(s); } public static boolean eqGet(List l, int i, Object o) { return eq(get(l, i), o); } public static boolean eqGet(Map map, A key, Object o) { return eq(mapGet(map, key), o); } public static boolean containsNewLine(String s) { return contains(s, '\n'); } public static List splitAtTokenIndices(List tok, List indices) { List l = new ArrayList(); for (int iI = -1; iI < l(indices); iI++) { int i = or(get(indices, iI), 0) + 1; int j = or(get(indices, iI + 1), l(tok)); l.add(trimJoin(tok.subList(i, j))); } return l; } public static Iterator iterator(Iterable c) { return c == null ? emptyIterator() : c.iterator(); } public static List dm_commentsForRule(String ruleID) { return tlft(pairB(dm_textAndCommentForRule(ruleID))); } public static String unnull(String s) { return s == null ? "" : s; } public static Collection unnull(Collection l) { return l == null ? emptyList() : l; } public static List unnull(List l) { return l == null ? emptyList() : l; } public static Map unnull(Map l) { return l == null ? emptyMap() : l; } public static Iterable unnull(Iterable i) { return i == null ? emptyList() : i; } public static A[] unnull(A[] a) { return a == null ? (A[]) new Object[0] : a; } public static BitSet unnull(BitSet b) { return b == null ? new BitSet() : b; } public static Pair unnull(Pair p) { return p != null ? p : new Pair(null, null); } public static boolean jmatch(String pat, String s) { return jmatch(pat, s, null); } public static boolean jmatch(String pat, String s, Matches matches) { if (s == null) return false; return jmatch(pat, javaTok(s), matches); } public static boolean jmatch(String pat, List toks) { return jmatch(pat, toks, null); } public static boolean jmatch(String pat, List toks, Matches matches) { List tokpat = javaTok(pat); String[] m = match2(tokpat, toks); if (m == null) return false; else { if (matches != null) matches.m = m; return true; } } public static MechListDependentCaches> mechCISet_cache = new MechListDependentCaches(new F1>() { public TreeSet get(String text) { try { return asCISet(tlft(text)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "asCISet(tlft(text))"; } }); public static TreeSet mechCISet(String name) { return mechCISet_cache.get(name); } public static String str(Object o) { return o == null ? "null" : o.toString(); } public static String str(char[] c) { return new String(c); } public static Throwable getExceptionCause(Throwable e) { Throwable c = e.getCause(); return c != null ? c : e; } public static String joinWithSpace(Collection c) { return join(" ", c); } public static String joinWithSpace(String... c) { return join(" ", c); } public static boolean methodIsStatic(Method m) { return (m.getModifiers() & Modifier.STATIC) != 0; } public static boolean argumentCompatibleWithType(Object arg, Class type) { return arg == null ? !type.isPrimitive() : isInstanceX(type, arg); } public static void arraycopy(Object[] a, Object[] b) { int n = min(a.length, b.length); for (int i = 0; i < n; i++) b[i] = a[i]; } public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int n) { System.arraycopy(src, srcPos, dest, destPos, n); } public static A[] arrayOfType(Class type, int n) { return makeArray(type, n); } public static A[] arrayOfType(int n, Class type) { return arrayOfType(type, n); } public static String join(String glue, Iterable strings) { if (strings == null) return ""; if (strings instanceof Collection) { if (((Collection) strings).size() == 1) return str(first(((Collection) strings))); } StringBuilder buf = new StringBuilder(); Iterator i = strings.iterator(); if (i.hasNext()) { buf.append(i.next()); while (i.hasNext()) buf.append(glue).append(i.next()); } return buf.toString(); } public static String join(String glue, String... strings) { return join(glue, Arrays.asList(strings)); } public static String join(Iterable strings) { return join("", strings); } public static String join(Iterable strings, String glue) { return join(glue, strings); } public static String join(String[] strings) { return join("", strings); } public static String join(String glue, Pair p) { return p == null ? "" : str(p.a) + glue + str(p.b); } public static List getClassNames(Collection l) { List out = new ArrayList(); if (l != null) for (Object o : l) out.add(o == null ? null : getClassName(o)); return out; } public static boolean isSnippetID(String s) { try { parseSnippetID(s); return true; } catch (RuntimeException e) { return false; } } public static long parseSnippetID(String snippetID) { long id = Long.parseLong(shortenSnippetID(snippetID)); if (id == 0) throw fail("0 is not a snippet ID"); return id; } public static boolean isAWTThread() { if (isAndroid()) return false; if (isHeadless()) return false; return isAWTThread_awt(); } public static boolean isAWTThread_awt() { return SwingUtilities.isEventDispatchThread(); } public static void failIfUnlicensed() { assertTrue("license off", licensed()); } public static Thread currentThread() { return Thread.currentThread(); } public static String dm_makeModule(String moduleLibID) { return (String) dm_callOS("makeModule", moduleLibID); } public static MechMode mechMode_value = new MechMode(); public static MechMode mechMode() { return mechMode_value; } public static volatile PersistableThrowable _handleException_lastException; public static List _handleException_onException = synchroList(ll("printStackTrace2")); public static void _handleException(Throwable e) { _handleException_lastException = persistableThrowable(e); Throwable e2 = innerException(e); if (e2.getClass() == RuntimeException.class && eq(e2.getMessage(), "Thread cancelled.") || e2 instanceof InterruptedException) return; for (Object f : cloneList(_handleException_onException)) try { callF(f, e); } catch (Throwable e3) { printStackTrace2(e3); } } public static Object collectionMutex(Object o) { String c = className(o); if (eq(c, "java.util.TreeMap$KeySet")) c = className(o = getOpt(o, "m")); else if (eq(c, "java.util.HashMap$KeySet")) c = className(o = get_raw(o, "this$0")); if (eqOneOf(c, "java.util.TreeMap$AscendingSubMap", "java.util.TreeMap$DescendingSubMap")) c = className(o = get_raw(o, "m")); return o; } public static ArrayList cloneList(Iterable l) { return l instanceof Collection ? cloneList((Collection) l) : asList(l); } public static ArrayList cloneList(Collection l) { if (l == null) return new ArrayList(); synchronized (collectionMutex(l)) { return new ArrayList(l); } } public static boolean isStringOrIntOrLong(Object o) { return o instanceof String || o instanceof Integer || o instanceof Long; } public static Object dm_getStemByID(Object id) { return dm_callOS("getModuleByID", str(id)); } public static A or(A a, A b) { return a != null ? a : b; } public static int max(int a, int b) { return Math.max(a, b); } public static int max(int a, int b, int c) { return max(max(a, b), c); } public static long max(int a, long b) { return Math.max((long) a, b); } public static long max(long a, long b) { return Math.max(a, b); } public static double max(int a, double b) { return Math.max((double) a, b); } public static float max(float a, float b) { return Math.max(a, b); } public static double max(double a, double b) { return Math.max(a, b); } public static int max(Collection c) { int x = Integer.MIN_VALUE; for (int i : c) x = max(x, i); return x; } public static double max(double[] c) { if (c.length == 0) return Double.MIN_VALUE; double x = c[0]; for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]); return x; } public static float max(float[] c) { if (c.length == 0) return Float.MAX_VALUE; float x = c[0]; for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]); return x; } public static byte max(byte[] c) { byte x = -128; for (byte d : c) if (d > x) x = d; return x; } public static short max(short[] c) { short x = -0x8000; for (short d : c) if (d > x) x = d; return x; } public static int max(int[] c) { int x = Integer.MIN_VALUE; for (int d : c) if (d > x) x = d; return x; } public static DynModule dm_currentModuleMandatory() { return dm_current_mandatory(); } public static Map vm_busListenersByMessage_live_cache; public static Map vm_busListenersByMessage_live() { if (vm_busListenersByMessage_live_cache == null) vm_busListenersByMessage_live_cache = vm_busListenersByMessage_live_load(); return vm_busListenersByMessage_live_cache; } public static Map vm_busListenersByMessage_live_load() { return vm_generalHashMap("busListenersByMessage"); } public static Set syncIdentityHashSet() { return (Set) synchronizedSet(identityHashSet()); } public static AutoCloseable tempAdd(final Collection l, final A a) { if (l == null || l.contains(a)) return null; l.add(a); return new AutoCloseable() { public String toString() { return "l.remove(a);"; } public void close() throws Exception { l.remove(a); } }; } public static boolean isCurrentThread(Thread t) { return t != null && t == currentThread(); } public static boolean isEitherB(Either e) { return eitherIsB(e); } public static A eitherAOpt(Either e) { return e != null && e.isA() ? e.a() : null; } public static boolean isString(Object o) { return o instanceof String; } public static boolean isIdentifier(String s) { return isJavaIdentifier(s); } public static Object dm_getService(String serviceName) { return empty(serviceName) ? null : dm_getModule(vmBus_query(assertIdentifier(serviceName))); } public static Object dm_callOS(String functionName, Object... args) { return call(dm_os(), functionName, args); } public static Object dm_resolveModule(Object moduleOrStem) { return dm_callOS("resolveModule", moduleOrStem); } public static String dropSuffix(String suffix, String s) { return s.endsWith(suffix) ? s.substring(0, l(s) - l(suffix)) : s; } public static List subList(List l, int startIndex) { return subList(l, startIndex, l(l)); } public static List subList(List l, int startIndex, int endIndex) { if (l == null) return null; int n = l(l); startIndex = Math.max(0, startIndex); endIndex = Math.min(n, endIndex); if (startIndex >= endIndex) return ll(); if (startIndex == 0 && endIndex == n) return l; return l.subList(startIndex, endIndex); } public static int min(int a, int b) { return Math.min(a, b); } public static long min(long a, long b) { return Math.min(a, b); } public static float min(float a, float b) { return Math.min(a, b); } public static float min(float a, float b, float c) { return min(min(a, b), c); } public static double min(double a, double b) { return Math.min(a, b); } public static double min(double[] c) { double x = Double.MAX_VALUE; for (double d : c) x = Math.min(x, d); return x; } public static float min(float[] c) { float x = Float.MAX_VALUE; for (float d : c) x = Math.min(x, d); return x; } public static byte min(byte[] c) { byte x = 127; for (byte d : c) if (d < x) x = d; return x; } public static short min(short[] c) { short x = 0x7FFF; for (short d : c) if (d < x) x = d; return x; } public static int min(int[] c) { int x = Integer.MAX_VALUE; for (int d : c) if (d < x) x = d; return x; } public static String beautifyStructure(String s) { List tok = javaTok(s); structure_addTokenMarkers(tok); jreplace(tok, "lhm", ""); return join(tok); } public static String struct_noStringSharing(Object o) { structure_Data d = new structure_Data(); d.noStringSharing = true; return structure(o, d); } public static String reverseString(String s) { return new StringBuilder(s).reverse().toString(); } public static void mapPut(Map map, A key, B value) { if (map != null && key != null && value != null) map.put(key, value); } public static Set keys(Map map) { return map == null ? new HashSet() : map.keySet(); } public static Set keys(Object map) { return keys((Map) map); } public static Set keys(MultiSet ms) { return ms.keySet(); } public static Set keys(MultiMap mm) { return mm.keySet(); } public static Object[] toObjectArray(Collection c) { List l = asList(c); return l.toArray(new Object[l.size()]); } public static List takeFirst_clone(List l, int n) { return cloneSubList(l, 0, n); } public static List takeFirst_clone(int n, List l) { return takeFirst_clone(l, n); } public static List takeFirst_clone(int n, Iterable i) { List l = new ArrayList(); Iterator it = i.iterator(); for (int _repeat_53 = 0; _repeat_53 < n; _repeat_53++) { if (it.hasNext()) l.add(it.next()); else break; } return l; } public static void gazelle_processSafeEvalsInTree(List tree) { Map evalCache = new HashMap(); if (tree != null) for (GazelleTree t : tree) { String l = dm_processEmbeddedSafeEvals(t.line, "evalCache", evalCache); if (l != t.line) { print("Processed eval: " + t.line + " => " + l); t.line = l; } } } public static A assertNotNull(A a) { assertTrue(a != null); return a; } public static A assertNotNull(String msg, A a) { assertTrue(msg, a != null); return a; } public static String stringPar(Object[] params, String name) { return stringOptPar(params, name); } public static String stringPar(String name, Object[] params) { return stringOptPar(params, name); } public static String stringPar(String name, Object[] params, String defaultValue) { return optPar(name, params, defaultValue); } public static Collection gazelle_preprocess(String input1, Object... __) { List interpretables = new ArrayList(); int max = optPar("max", __, 20); Collector collector = limitedCollector(interpretables, max); Map evalCache = new HashMap(); F1 eval = boolPar("noEvals", __) ? f_id() : new F1() { public String get(String s) { try { return gazelle_processSafeEvals(s, evalCache); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "gazelle_processSafeEvals(s, evalCache)"; } }; input1 = callF(eval, input1); Set texts = litCISet(input1); collector.add(new GInterpretable(input1)); RuleEngine2 engine = ((F0) optPar("contextMaker", __)).get().engine; int nInterpreted = 0; bigLoop: while (!collector.full() && nInterpreted < l(interpretables)) { GInterpretable workingOn = get(interpretables, nInterpreted); String input = workingOn.text; String ruleBase = empty(workingOn.ruleID) ? "" : workingOn.ruleID + "+"; ++nInterpreted; for (RuleEngine2.SimplifyWithRule rule : engine.splitterRules()) { try { for (String out : gazelle_executeSplitterRule(engine, rule, input)) if (texts.add(out = callF(eval, out))) if (collector.add(new GInterpretable(out, ruleBase + rule.globalID))) break bigLoop; } catch (Throwable __e) { _handleException(__e); } } for (GazelleTree t : dm_gazelle_reasonAboutChatInput_v2(null, input, paramsPlus(__, "skipSplitters", true, "acceptablePurposes", litset("preprocess"), "skipUserName", true, "skipUserID", true))) { String s = callF(eval, t.line); if (texts.add(s)) if (collector.add(new GInterpretable(s, ruleBase + t.ruleID(), t.mr))) break bigLoop; } } return interpretables; } public static String n2(long l) { return formatWithThousands(l); } public static String n2(Collection l) { return n2(l(l)); } public static String n2(double l, String singular) { return n2(l, singular, singular + "s"); } public static String n2(double l, String singular, String plural) { if (fraction(l) == 0) return n2((long) l, singular, plural); else return l + " " + plural; } public static String n2(long l, String singular, String plural) { return n_fancy2(l, singular, plural); } public static String n2(long l, String singular) { return n_fancy2(l, singular, singular + "s"); } public static String n2(Collection l, String singular) { return n2(l(l), singular); } public static String n2(Collection l, String singular, String plural) { return n_fancy2(l, singular, plural); } public static String n2(Map m, String singular, String plural) { return n_fancy2(m, singular, plural); } public static String n2(Map m, String singular) { return n2(l(m), singular); } public static String n2(Object[] a, String singular) { return n2(l(a), singular); } public static String n2(Object[] a, String singular, String plural) { return n_fancy2(a, singular, plural); } public static String n2(MultiSet ms, String singular, String plural) { return n_fancy2(ms, singular, plural); } public static > A printIndentLines(String indent, A lines) { return printLinesWithIndent(indent, lines); } public static > A printIndentLines(A lines) { return printLinesWithIndent(defaultIndent(), lines); } public static HashSet litset(A... items) { return lithashset(items); } public static TreeSet ciSet() { return caseInsensitiveSet(); } public static List dm_gazelle_reasonAboutChatInput_v2(String userName, String content, Object... __) { boolean debug = boolPar("debug", __); boolean debugRules = boolPar("debugRules", __) || debug; List preContext = (List) (optPar("preContext", __)); GazelleEvalContext ctx = (GazelleEvalContext) (optPar("ctx", __)); F0 contextMaker = (F0) (optPar("contextMaker", __)); if (debugRules) print("Have ctx: " + (ctx != null) + ", contextMaker: " + (contextMaker != null)); if (ctx == null && contextMaker != null) { ctx = contextMaker.get(); if (debugRules) print("Called contextMaker, got " + n2(ctx.engine.rules, "rule")); } if (ctx == null) ctx = dm_gazelle_stdEvalContext(dm_gazelle_allRulesWithComment(optPar("requiredComment", __, "discord"))); Set acceptablePurposes = (Set) (optPar("acceptablePurposes", __)); if (debug) print(" == PROCESSING INPUT: " + content); gazelle_dropRulesNotOnInput(ctx); if (acceptablePurposes != null) { if (debugRules) { print("Purposes: " + acceptablePurposes); print("Have purposes: " + collectUnique("purpose", ctx.engine.rules)); } int nBefore = l(ctx.engine.rules); ctx.engine.dropRulesWhere(r -> !acceptablePurposes.contains(unnull(r.purpose))); if (debugRules) print("Retaining " + n2(ctx.engine.rules, "rule") + " of " + nBefore); } if (debugRules) print("Have rules: " + collect("globalID", ctx.engine.rules)); gazelle_addHelpers(ctx, __); Object[] params = paramsPlus(paramsMinus(__, "ctx"), "firstConditionOnly", true, "dialogHistory", preContext); GazelleTree tree1 = (GazelleTree) (optPar("tree", __)); if (tree1 == null) tree1 = new GazelleTree(ctx, content); List l = gazelle_getChildren_withContinuations(tree1, params); if (nempty(userName) && !boolPar("skipUserName", __)) { GazelleTree tree2 = new GazelleTree(ctx, optCurly(userName) + " says: " + content); if (debug) print("Tree2: " + tree2); l.addAll(gazelle_getChildren_withContinuations(tree2, paramsPlus(__, "tree2", true))); } Long userID = (Long) (optPar("userID", __)); if (userID != null && userID != 0 && !boolPar("skipUserID", __)) { GazelleTree tree3 = new GazelleTree(ctx, "user with id " + userID + " says: " + content); l.addAll(gazelle_getChildren_withContinuations(tree3, paramsPlus(__, "tree3", true))); } if (boolPar("skipBad", __, true)) l = filter(l, new F1() { public Boolean get(GazelleTree t) { try { return neq(t.prediction, "bad"); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "neq(t.prediction, \"bad\")"; } }); gazelle_sortChildren(l); Map inputMap = gazelle_inputMapFromLineAndHistory(content, listPlus(preContext, content)); if (debug) print("reasonAbout inputMap=" + sfu(asTreeMap(inputMap))); for (GazelleTree t : l) { if (t.mr == null) continue; int iCond; do { iCond = t.mr.iCond; dm_gazelle_matchStep_statement(t, passParams(__, "debug", "dm_gazelle_matchStatementsDebug")); dm_gazelle_matchStep_additionalInput(t, inputMap, __); dm_gazelle_matchStep_condition(t, __); } while (t.mr.iCond > iCond); } for (GazelleTree t : l) t.flush(); int n = l(l); l = gazelle_dropUnsatisfiedEntries(l); if (debug) print("Filtered out " + (n - l(l)) + " unsatisfied entries (keeping " + l(l) + ")"); if (!boolPar("keepDefaults", __)) gazelle_dropMatchedDefaultRules(l, __); return l; } public static void gazelle_dropMatchedDefaultRules(List l, Object... __) { List l2 = filter(l, t -> !eqic(t.lineType, "temporary fact")); Set trumpedRules = new HashSet(); for (GazelleTree t : l2) addAll(trumpedRules, matchAll_first("trumps rule *", t.ruleComments())); List trumped = filter(l2, new F1() { public Boolean get(GazelleTree t) { try { return contains(t.ruleComments(), "default") || trumpedRules.contains(t.ruleID()); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "contains(t.ruleComments(), \"default\")\r\n || trumpedRules.contains(t.ruleID())"; } }); if (nempty(trumped) && l(trumped) < l(l2)) { if (boolPar("debug", __) || boolPar("debugDefaults", __)) { print("Removing default/trumped rules:"); printIndentLines(trumped); print("Because there are also:"); printIndentLines(listMinusSet(l2, trumped)); } l.removeAll(trumped); } } public static Map mapMinus(Map map, Object... keys) { if (empty(keys)) return map; Map m2 = cloneMap(map); for (Object key : keys) m2.remove(key); return m2; } public static HashSet lithashset(A... items) { HashSet set = new HashSet(); for (A a : items) set.add(a); return set; } public static int parseIntOpt(String s) { return isInteger(s) ? parseInt(s) : 0; } public static List javaTokPlusBrackets2(String s) { return tok_combineRoundCurlySquareBrackets_keep(javaTok(s)); } public static B mapGet(Map map, A a) { return map == null || a == null ? null : map.get(a); } public static B mapGet(A a, Map map) { return map == null || a == null ? null : map.get(a); } public static String trimJoin(List s) { return trim(join(s)); } public static Iterator emptyIterator() { return Collections.emptyIterator(); } public static List tlft(String s) { return toLinesFullTrim(s); } public static List tlft(File f) { return toLinesFullTrim(f); } public static B pairB(Pair p) { return p == null ? null : p.b; } public static Pair dm_textAndCommentForRule(String ruleID) { return (Pair) quickImport(dm_call(dm_gazelle_rulesModule(), "textAndCommentForRule", ruleID)); } public static Map emptyMap() { return new HashMap(); } public static int javaTok_n, javaTok_elements; public static boolean javaTok_opt; public static List javaTok(String s) { ++javaTok_n; ArrayList tok = new ArrayList(); int l = s == null ? 0 : s.length(); int i = 0, n = 0; while (i < l) { int j = i; char c, d; 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; } tok.add(javaTok_substringN(s, i, j)); ++n; i = j; if (i >= l) break; c = s.charAt(i); d = i + 1 >= l ? '\0' : s.charAt(i + 1); 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 == '\'') { ++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)) || "'".indexOf(s.charAt(j)) >= 0)); else if (Character.isDigit(c)) { do ++j; while (j < l && Character.isDigit(s.charAt(j))); if (j < l && s.charAt(j) == 'L') ++j; } 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)); ++n; i = j; } if ((tok.size() % 2) == 0) tok.add(""); javaTok_elements += tok.size(); return tok; } public static List javaTok(List tok) { return javaTokWithExisting(join(tok), tok); } public static String[] match2(List pat, List tok) { int i = pat.indexOf("..."); if (i < 0) return match2_match(pat, tok); pat = new ArrayList(pat); pat.set(i, "*"); while (pat.size() < tok.size()) { pat.add(i, "*"); pat.add(i + 1, ""); } return match2_match(pat, tok); } public static String[] match2_match(List pat, List tok) { List result = new ArrayList(); if (pat.size() != tok.size()) { return null; } for (int i = 1; i < pat.size(); i += 2) { String p = pat.get(i), t = tok.get(i); if (eq(p, "*")) result.add(t); else if (!equalsIgnoreCase(unquote(p), unquote(t))) return null; } return result.toArray(new String[result.size()]); } public static TreeSet asCISet(Iterable c) { return toCaseInsensitiveSet(c); } public static TreeSet asCISet(String... x) { return toCaseInsensitiveSet(x); } public static boolean isInstanceX(Class type, Object arg) { if (type == boolean.class) return arg instanceof Boolean; if (type == int.class) return arg instanceof Integer; if (type == long.class) return arg instanceof Long; if (type == float.class) return arg instanceof Float; if (type == short.class) return arg instanceof Short; if (type == char.class) return arg instanceof Character; if (type == byte.class) return arg instanceof Byte; if (type == double.class) return arg instanceof Double; return type.isInstance(arg); } public static A[] makeArray(Class type, int n) { return (A[]) Array.newInstance(type, n); } public static Throwable printStackTrace2(Throwable e) { print(getStackTrace2(e)); return e; } public static void printStackTrace2() { printStackTrace2(new Throwable()); } public static void printStackTrace2(String msg) { printStackTrace2(new Throwable(msg)); } public static String shortenSnippetID(String snippetID) { if (snippetID.startsWith("#")) snippetID = snippetID.substring(1); String httpBlaBla = "http://tinybrain.de/"; if (snippetID.startsWith(httpBlaBla)) snippetID = snippetID.substring(httpBlaBla.length()); return "" + parseLong(snippetID); } public static volatile boolean licensed_yes = true; public static boolean licensed() { if (!licensed_yes) return false; ping_okInCleanUp(); return true; } public static void licensed_off() { licensed_yes = false; } public static List synchroList() { return Collections.synchronizedList(new ArrayList()); } public static List synchroList(List l) { return Collections.synchronizedList(l); } public static PersistableThrowable persistableThrowable(Throwable e) { return e == null ? null : new PersistableThrowable(e); } public static Throwable innerException(Throwable e) { return getInnerException(e); } public static String className(Object o) { return getClassName(o); } public static boolean eqOneOf(Object o, Object... l) { for (Object x : l) if (eq(o, x)) return true; return false; } public static Set synchronizedSet() { return synchroHashSet(); } public static Set synchronizedSet(Set set) { return Collections.synchronizedSet(set); } public static Set identityHashSet() { return Collections.newSetFromMap(new IdentityHashMap()); } public static boolean eitherIsB(Either e) { return e != null && e.isB(); } public 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; } public static Object vmBus_query(String msg, Object... args) { Object arg = vmBus_wrapArgs(args); { Object __444 = pcallFAll_returnFirstNotNull(vm_busListeners_live(), msg, arg); if (__444 != null) return __444; } return pcallFAll_returnFirstNotNull(vm_busListenersByMessage_live().get(msg), msg, arg); } public static Object vmBus_query(String msg) { return vmBus_query(msg, (Object) null); } public static String assertIdentifier(String s) { return assertIsIdentifier(s); } public static Object dm_os() { { Object __1563 = vm_generalMap_get("stefansOS"); if (__1563 != null) return __1563; } return creator(); } public static String structure_addTokenMarkers(String s) { return join(structure_addTokenMarkers(javaTok(s))); } public static List structure_addTokenMarkers(List tok) { TreeSet refs = new TreeSet(); for (int i = 1; i < l(tok); i += 2) { String t = tok.get(i); if (t.startsWith("t") && isInteger(t.substring(1))) refs.add(parseInt(t.substring(1))); } if (empty(refs)) return tok; for (int i : refs) { int idx = i * 2 + 1; String t = ""; if (endsWithLetterOrDigit(tok.get(idx - 1))) t = " "; tok.set(idx, t + "m" + i + " " + tok.get(idx)); } return tok; } public static String jreplace(String s, String in, String out) { return jreplace(s, in, out, null); } public static String jreplace(String s, String in, String out, Object condition) { List tok = javaTok(s); return jreplace(tok, in, out, condition) ? join(tok) : s; } public static boolean jreplace(List tok, String in, String out) { return jreplace(tok, in, out, false, true, null); } public static boolean jreplace(List tok, String in, String out, Object condition) { return jreplace(tok, in, out, false, true, condition); } public static boolean jreplace(List tok, String in, String out, boolean ignoreCase, boolean reTok, Object condition) { String[] toks = javaTokForJFind_array(in); int lTokin = toks.length * 2 + 1; boolean anyChange = false; for (int n = 0; n < 10000; n++) { int i = findCodeTokens(tok, 1, ignoreCase, toks, condition); if (i < 0) return anyChange; List subList = tok.subList(i - 1, i + lTokin - 1); String expansion = jreplaceExpandRefs(out, subList); int end = i + lTokin - 2; clearAllTokens(tok, i, end); tok.set(i, expansion); if (reTok) reTok(tok, i, end); anyChange = true; } throw fail("woot? 10000! " + quote(in) + " => " + quote(out)); } public static boolean jreplace_debug; public static List cloneSubList(List l, int startIndex, int endIndex) { return newSubList(l, startIndex, endIndex); } public static List cloneSubList(List l, int startIndex) { return newSubList(l, startIndex); } public static String dm_processEmbeddedSafeEvals(String s, Object... __) { Map evalCache = (Map) (optPar("evalCache", __)); if (!contains(s, "{=")) return s; List tok = javaTok(s); Map bracketMap = curlyBracketMap(tok); for (int i = 1; i < l(tok) - 4; i += 2) { { if (!(eq(tok.get(i), "{") && eqGet(tok, i + 1, "") && eqGet(tok, i + 2, "="))) continue; } int j = or(bracketMap.get(i), l(tok)); String code = joinSubList(tok, i + 4, j - 1); if (!isSafeCodeFragment(code)) replaceTokens(tok, i + 2, i + 4, "UNSAFE: "); else { String result = mapGet(evalCache, code); if (result == null) { print("Evaluating: " + code); try { result = strOrEmpty(dm_javaEvalOrInterpret(code)); } catch (Throwable e) { printStackTrace(e); result = ""; } mapPut(evalCache, code, result); } replaceTokens(tok, i, j + 1, result); } i = j - 2; } return trim(join(tok)); } public static String stringOptPar(Object[] params, String name) { return (String) optPar(params, name); } public static Collector limitedCollector(Collection l, int max) { return limitedListCollector(l, max); } public static F1 f_id_value; public static F1 f_id() { if (f_id_value == null) f_id_value = new F1() { public A get(A a) { try { return a; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ret a;"; } }; return f_id_value; } public static String gazelle_processSafeEvals(String text, Map evalCache) { String l = dm_processEmbeddedSafeEvals(text, "evalCache", evalCache); if (l != text) { print("Processed eval: " + text + " => " + l); text = l; } return text; } public static TreeSet litCISet(String... items) { return litciset(items); } public static Collection gazelle_executeSplitterRule(RuleEngine2 engine, RuleEngine2.SimplifyWithRule rule, String input) { Object f = rule.function(); Collection out = (Collection) ((f instanceof String ? callF(engine.callFunctionOnString, (String) f, input) : callF(f, input))); if (l(out) == 1 && eq(first(out), input)) return emptyList(); return out; } public static String formatWithThousands(long l) { return formatWithThousandsSeparator(l); } public static double fraction(double d) { return d % 1; } public static String n_fancy2(long l, String singular, String plural) { return formatWithThousandsSeparator(l) + " " + trim(l == 1 ? singular : plural); } public static String n_fancy2(Collection l, String singular, String plural) { return n_fancy2(l(l), singular, plural); } public static String n_fancy2(Map m, String singular, String plural) { return n_fancy2(l(m), singular, plural); } public static String n_fancy2(Object[] a, String singular, String plural) { return n_fancy2(l(a), singular, plural); } public static String n_fancy2(MultiSet ms, String singular, String plural) { return n_fancy2(l(ms), singular, plural); } public static > A printLinesWithIndent(String indent, A lines) { if (lines != null) for (B s : lines) printIndent(indent, s); return lines; } public static String defaultIndent() { return indent(indent_default); } public static TreeSet caseInsensitiveSet() { return new TreeSet(caseInsensitiveComparator()); } public static TreeSet caseInsensitiveSet(Collection c) { return toCaseInsensitiveSet(c); } public static GazelleEvalContext dm_gazelle_stdEvalContext() { return dm_gazelle_stdEvalContext(dm_allRulesFromRulesModuleWithCommentsAndIDs()); } public static GazelleEvalContext dm_gazelle_stdEvalContext(List> rules, Object... __) { GazelleEvalContext ctx = gazelle_stdEvalContext_v2(rules, __); ctx.engine.callFunctionOnString = new F2() { public Object get(String function, String s) { try { return dm_callFunctionOnString(function, s); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "dm_callFunctionOnString(function, s)"; } }; return ctx; } public static List> dm_gazelle_allRulesWithComment(final String comment) { List> l = dm_allRulesFromRulesModuleWithCommentsAndIDs(); return empty(comment) ? l : filter(l, new F1, Boolean>() { public Boolean get(T3 t) { try { return cic(tlft(t.b), comment); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "cic(tlft(t.b), comment)"; } }); } public static void gazelle_dropRulesNotOnInput(GazelleEvalContext ctx) { ctx.engine.dropRulesWhere(r -> cicOneOf(r.comments, "in 1 = statement", "in = statement", "disable")); } public static HashSet collectUnique(Collection c, String field) { HashSet set = new HashSet(); for (Object a : c) { Object val = getOpt(a, field); if (val != null) set.add(val); } return set; } public static HashSet collectUnique(String field, Collection c) { return collectUnique(c, field); } public static void gazelle_addHelpers(GazelleEvalContext ctx, Object... __) { if (ctx.engine.hasHelpers) return; print("Adding helpers"); ctx.engine.hasHelpers = true; gazelle_addHelperTablesToRules(ctx.engine); gazelle_addVarSplittersToRules(ctx.engine, __); gazelle_addVarDiscriminators(ctx.engine, __); gazelle_addVarConvertersToRules(ctx.engine, __); gazelle_addCalculationFunctionsToRules(ctx.engine, __); gazelle_addRespondToHumanOnlyHelpers(ctx.engine, __); gazelle_addForbiddenBindingHelpers(ctx.engine, __); } public static List gazelle_getChildren_withContinuations(GazelleTree tree, Object... __) { List l = gazelle_getChildren(tree, __); boolean debug = boolPar("debug", __) || boolPar("debugContinuations", __); Matches m = new Matches(); boolean changes = false; for (int i = 0; i < l(l); i++) { GazelleTree t = l.get(i); { if (!(eq(t.lineType, "instruction") && match("continue with line * of rule * without variables", t.line, m))) continue; } if (debug) print("Processing continuation: " + sfu(m)); String ruleID = m.unq(1); RuleEngine2.Rule rule = t.ctx.engine.getRule(ruleID); if (rule == null) { print("Follow-up rule not found: " + ruleID); { l.remove(i--); continue; } } changes = true; t.mr.rule = rule; t.mr.iCond = parseInt(m.unq(0)) - 1; t.mr.map = ciMap(); if (!t.mr.moreConditions()) { t.line = rule.out; t.lineType = rule.outType(); } gazelle_addPredictionToTreeNode(t); } if (changes) gazelle_sortChildren(l); return l; } public static String optCurly(String s) { return curlyBraceOptIfMoreThanOneToken(s); } public static List filter(Iterable c, Object pred) { if (pred instanceof F1) return filter(c, (F1) pred); List x = new ArrayList(); if (c != null) for (Object o : c) if (isTrue(callF(pred, o))) x.add(o); return x; } public static List filter(Object pred, Iterable c) { return filter(c, pred); } public static List filter(Iterable c, F1 pred) { List x = new ArrayList(); if (c != null) for (B o : c) if (pred.get(o).booleanValue()) x.add(o); return x; } public static List filter(F1 pred, Iterable c) { return filter(c, pred); } public static List filter(Iterable c, IF1 pred) { List x = new ArrayList(); if (c != null) for (B o : c) if (pred.get(o).booleanValue()) x.add(o); return x; } public static List filter(IF1 pred, Iterable c) { return filter(c, pred); } public static void gazelle_sortChildren(GazelleTree tree) { if (tree == null) return; gazelle_sortChildren(tree.children); } public static void gazelle_sortChildren(List children) { if (children != null) sortByCalculatedFieldDesc(children, new F1() { public Integer get(GazelleTree c) { try { return ai_goodBadToInt(c.prediction); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ai_goodBadToInt(c.prediction)"; } }); } public static Map gazelle_inputMapFromLineAndHistory(String line, List history) { Map inputMap = litmap("standard", line); for (int i = 0; i < l(history); i++) inputMap.put("history-" + (l(history) - i), history.get(i)); return inputMap; } public static List listPlus(Collection l, A... more) { return concatLists(l, asList(more)); } public static TreeMap asTreeMap(Map map) { return map instanceof TreeMap ? (TreeMap) map : new TreeMap(map); } public static boolean dm_gazelle_matchStep_statement(GazelleTree t, Object... __) { if (t.rule() == null) return false; boolean debug = boolPar("debug", __) || boolPar("matchStatementsDebug", __); RuleEngine2_MatchedRule mr = t.mr; List in = t.rule().in; int iCond = t.mr.iCond; if (debug) print("matchStatements: iCond=" + iCond + " in " + in); if (iCond >= l(in)) return false; List conditions = dropFirst(iCond, in); if (debug) print("Remaining conditions for " + t.ruleID() + " with " + t.mr.map + ": " + conditions); Matches m = new Matches(); if (!matchAny("in " + (iCond + 1) + " = statement *", t.rule().comments, m)) return false; String subRuleID = m.unq(0); if (debug) print("Found subrule: " + subRuleID); List fulfillments = dm_gazelle_fulfillmentsForRule(subRuleID); Lowest> bestMap = new Lowest(); if (debug) print("Have " + n2(fulfillments, "fulfillment") + "."); for (Object f : fulfillments) { Map varMap = asCIMap(safeUnstructMap(getString("varMap", f))); if (debug) print("fulfillment => " + sfu(varMap)); boolean selfVarsFix = boolPar("selfVarsFix", __, true); if (selfVarsFix) { String cond = first(conditions); for (String x : identifiersOnly(codeTokens(mr.tokenize(cond)))) if (!varMap.containsKey(x)) { if (debug) print("Self-linking " + x); varMap.put(x, x); } } if (debug) print("Original map: " + t.mr.map); Map newMap = mergeMapsStrictlyIC(asCIMap(t.mr.map), varMap); if (newMap == null) { if (debug) printIndent("Can't merge maps"); continue; } if (debug) printIndent("Maps merged! => " + sfu(newMap)); newMap = ciMapWithoutKeysEqualToValues(newMap); if (debug) printIndent("Map simplified => " + sfu(newMap)); { if (gazelle_isRecentlyUsedMapping(t, newMap, __)) continue; } bestMap.put(newMap, l(newMap)); } Map newMap = bestMap.get(); if (newMap == null) { if (debug) print("No matching statement found"); return false; } t.mr.map = newMap; t.mr.iCond++; t.mr.remainingConditions = dropFirst(t.mr.remainingConditions); t.mr.remainingConditionsWithType = dropFirst(t.mr.remainingConditionsWithType); t.mr.out = null; t.line = t.mr.outText(); if (debug) print("Rewrote " + t.mr.rule.out + " to: " + t.line); return true; } public static Object[] passParams(Object[] __, String... params) { Map map = paramsToMap(__); List l = new ArrayList(); int n = l(l) - 1; for (String key : params) if (map.containsKey(key)) { l.add(key); l.add(map.get(key)); } return empty(l) ? null : toObjectArray(l); } public static void dm_gazelle_matchStep_additionalInput(GazelleTree t, Map inputsByType, Object... __) { if (t.rule() == null) return; boolean debug = boolPar("debug", __); TextAndType next = get(t.rule().insWithType, t.mr.iCond); if (next == null || !startsWith(next.type, "history-")) return; if (ai_ruleEngine2_matchStep(t.mr, inputsByType, true, __)) { if (debug) print("Input match OK. new map: " + t.mr.map + ". Updating prediction."); gazelle_addPredictionToTreeNode(t); } else if (debug) print("Input match failed"); } public static void dm_gazelle_matchStep_condition(GazelleTree t, Object... __) { if (t.rule() == null) return; boolean debug = boolPar("debug", __) || boolPar("debugConditions", __); GRuleLine next = get(t.rule().insWithType, t.mr.iCond); if (debug) print("matchStep_condition: " + getString("type", next)); if (next == null || !eq(next.type, "condition")) return; boolean satisfied = false; String cond = t.mr.applyMappingTo(next); if (gazelle_isEmbeddedEval(cond)) { if (debug) print("Embedded eval: " + cond); satisfied = eqic(dm_processEmbeddedSafeEvals(cond), "true"); } else { GazelleEvalContext subCtx = (GazelleEvalContext) (callF(requiredPar("contextMaker", __))); subCtx.engine.dropRulesWhere(r -> neq(r.purpose, "condition to eval")); List l = dm_gazelle_reasonAboutChatInput_v2(null, cond, paramsPlus(paramsMinus(__, "acceptablePurposes"), "ctx", subCtx)); gazelle_processSafeEvalsInTree(l); if (debug) { print("Sub-Tree for condition " + cond + ":"); printIndentLines(l); } for (GazelleTree tree : l) if (eqic(tree.line, "true")) { satisfied = true; break; } else if (eqic(tree.line, "false")) break; } if (debug) print("satisfied=" + satisfied); if (!satisfied) return; t.mr.iCond++; gazelle_addPredictionToTreeNode(t); t.mr.remainingConditions = dropFirst(t.mr.remainingConditions); t.mr.remainingConditionsWithType = dropFirst(t.mr.remainingConditionsWithType); } public static List gazelle_dropUnsatisfiedEntries(List l) { return antiFilter(l, t -> t.mr != null && t.mr.moreConditions()); } public static boolean eqic(String a, String b) { if ((a == null) != (b == null)) return false; if (a == null) return true; return a.equalsIgnoreCase(b); } public static boolean eqic(char a, char b) { if (a == b) return true; char u1 = Character.toUpperCase(a); char u2 = Character.toUpperCase(b); if (u1 == u2) return true; return Character.toLowerCase(u1) == Character.toLowerCase(u2); } public static List matchAll_first(String pat, Iterable l) { return map(matchAll(pat, l), new F1() { public String get(Matches m) { try { return m.get(0); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "m.get(0)"; } }); } public static List listMinusSet(Iterable l, Collection stuff) { if (empty(stuff)) return asList(l); Set set = asSet(stuff); List l2 = new ArrayList(); for (A a : l) if (!set.contains(a)) l2.add(a); return l2; } public static List listMinusSet(Iterable l, Collection stuff, Collection stuff2) { return listMinusSet(listMinusSet(l, stuff), stuff2); } public static boolean isInteger(String s) { int n = l(s); if (n == 0) return false; int i = 0; if (s.charAt(0) == '-') if (++i >= n) return false; while (i < n) { char c = s.charAt(i); if (c < '0' || c > '9') return false; ++i; } return true; } public static int parseInt(String s) { return empty(s) ? 0 : Integer.parseInt(s); } public static int parseInt(char c) { return Integer.parseInt(str(c)); } public static List tok_combineRoundCurlySquareBrackets_keep(List tok) { List l = new ArrayList(); for (int i = 0; i < l(tok); i++) { String t = tok.get(i); if (odd(i) && eqOneOf(t, "{", "(", "[")) { int j = findEndOfBracketPart2(tok, i); l.add(joinSubList(tok, i, j)); i = j - 1; } else l.add(t); } return l; } public static String trim(String s) { return s == null ? null : s.trim(); } public static String trim(StringBuilder buf) { return buf.toString().trim(); } public static String trim(StringBuffer buf) { return buf.toString().trim(); } public static List toLinesFullTrim(String s) { List l = new ArrayList(); for (String line : toLines(s)) if (nempty(line = trim(line))) l.add(line); return l; } public static List toLinesFullTrim(File f) { List l = new ArrayList(); for (String line : linesFromFile(f)) if (nempty(line = trim(line))) l.add(line); return l; } public static Object quickImport(Object o) { return quickExport(o, mc()); } public static String dm_gazelle_rulesModule() { return dm_require("#1021352/Rules"); } public static String javaTok_substringN(String s, int i, int j) { if (i == j) return ""; if (j == i + 1 && s.charAt(i) == ' ') return " "; return s.substring(i, j); } public static String javaTok_substringC(String s, int i, int j) { return s.substring(i, j); } public static List javaTokWithExisting(String s, List existing) { ++javaTok_n; int nExisting = javaTok_opt && existing != null ? existing.size() : 0; ArrayList tok = existing != null ? new ArrayList(nExisting) : new ArrayList(); int l = s.length(); int i = 0, n = 0; while (i < l) { int j = i; char c, d; 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; } if (n < nExisting && javaTokWithExisting_isCopyable(existing.get(n), s, i, j)) tok.add(existing.get(n)); else tok.add(javaTok_substringN(s, i, j)); ++n; i = j; if (i >= l) break; c = s.charAt(i); d = i + 1 >= l ? '\0' : s.charAt(i + 1); 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) { if (s.charAt(j) == opener) { ++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)); else if (Character.isDigit(c)) { do ++j; while (j < l && Character.isDigit(s.charAt(j))); if (j < l && s.charAt(j) == 'L') ++j; } 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; if (n < nExisting && javaTokWithExisting_isCopyable(existing.get(n), s, i, j)) tok.add(existing.get(n)); else tok.add(javaTok_substringC(s, i, j)); ++n; i = j; } if ((tok.size() % 2) == 0) tok.add(""); javaTok_elements += tok.size(); return tok; } public static boolean javaTokWithExisting_isCopyable(String t, String s, int i, int j) { return t.length() == j - i && s.regionMatches(i, t, 0, j - i); } public static boolean equalsIgnoreCase(String a, String b) { return eqic(a, b); } public static boolean equalsIgnoreCase(char a, char b) { return eqic(a, b); } public static String unquote(String s) { if (s == null) return null; if (startsWith(s, '[')) { int i = 1; while (i < s.length() && s.charAt(i) == '=') ++i; if (i < s.length() && s.charAt(i) == '[') { String m = s.substring(1, i); if (s.endsWith("]" + m + "]")) return s.substring(i + 1, s.length() - i - 1); } } if (s.length() > 1) { char c = s.charAt(0); if (c == '\"' || c == '\'') { int l = endsWith(s, c) ? s.length() - 1 : s.length(); StringBuilder sb = new StringBuilder(l - 1); for (int i = 1; i < l; i++) { char ch = s.charAt(i); if (ch == '\\') { char nextChar = (i == l - 1) ? '\\' : s.charAt(i + 1); if (nextChar >= '0' && nextChar <= '7') { String code = "" + nextChar; i++; if ((i < l - 1) && s.charAt(i + 1) >= '0' && s.charAt(i + 1) <= '7') { code += s.charAt(i + 1); i++; if ((i < l - 1) && s.charAt(i + 1) >= '0' && s.charAt(i + 1) <= '7') { code += s.charAt(i + 1); i++; } } sb.append((char) Integer.parseInt(code, 8)); continue; } switch(nextChar) { case '\"': ch = '\"'; break; case '\\': ch = '\\'; break; case 'b': ch = '\b'; break; case 'f': ch = '\f'; break; case 'n': ch = '\n'; break; case 'r': ch = '\r'; break; case 't': ch = '\t'; break; case '\'': ch = '\''; break; case 'u': if (i >= l - 5) { ch = 'u'; break; } int code = Integer.parseInt("" + s.charAt(i + 2) + s.charAt(i + 3) + s.charAt(i + 4) + s.charAt(i + 5), 16); sb.append(Character.toChars(code)); i += 5; continue; default: ch = nextChar; } i++; } sb.append(ch); } return sb.toString(); } } return s; } public static TreeSet toCaseInsensitiveSet(Iterable c) { if (isCISet(c)) return (TreeSet) c; TreeSet set = caseInsensitiveSet(); addAll(set, c); return set; } public static TreeSet toCaseInsensitiveSet(String... x) { TreeSet set = caseInsensitiveSet(); addAll(set, x); return set; } public static List splitAtEllipsis(String s) { return splitAtTokens(javaTokWithBrackets(s), ".", "", ".", "", "."); } public static String getStackTrace2(Throwable e) { return hideCredentials(getStackTrace(unwrapTrivialExceptionWraps(e)) + replacePrefix("java.lang.RuntimeException: ", "FAIL: ", hideCredentials(str(innerException2(e)))) + "\n"); } public static Throwable getInnerException(Throwable e) { if (e == null) return null; while (e.getCause() != null) e = e.getCause(); return e; } public static Throwable getInnerException(Runnable r) { return getInnerException(getException(r)); } public static Object vmBus_wrapArgs(Object... args) { return empty(args) ? null : l(args) == 1 ? args[0] : args; } public static Object pcallFAll_returnFirstNotNull(Collection l, Object... args) { if (l != null) for (Object f : cloneList(l)) { Object __907 = pcallF(f, args); if (__907 != null) return __907; } return null; } public static Object pcallFAll_returnFirstNotNull(Iterator it, Object... args) { while (it.hasNext()) { Object __908 = pcallF(it.next(), args); if (__908 != null) return __908; } return null; } public static Set vm_busListeners_live_cache; public static Set vm_busListeners_live() { if (vm_busListeners_live_cache == null) vm_busListeners_live_cache = vm_busListeners_live_load(); return vm_busListeners_live_cache; } public static Set vm_busListeners_live_load() { return vm_generalIdentityHashSet("busListeners"); } public static String assertIsIdentifier(String s) { if (!isIdentifier(s)) throw fail("Not an identifier: " + quote(s)); return s; } public static Object vm_generalMap_get(Object key) { return vm_generalMap().get(key); } public static WeakReference creator_class; public static Object creator() { return creator_class == null ? null : creator_class.get(); } public static Map javaTokForJFind_array_cache = synchronizedMRUCache(1000); public 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; } public static int findCodeTokens(List tok, String... tokens) { return findCodeTokens(tok, 1, false, tokens); } public static int findCodeTokens(List tok, boolean ignoreCase, String... tokens) { return findCodeTokens(tok, 1, ignoreCase, tokens); } public static int findCodeTokens(List tok, int startIdx, boolean ignoreCase, String... tokens) { return findCodeTokens(tok, startIdx, ignoreCase, tokens, null); } public static HashSet findCodeTokens_specials = lithashset("*", "", "", "", "\\*"); public static boolean findCodeTokens_debug; public static int findCodeTokens_indexed, findCodeTokens_unindexed; public static int findCodeTokens_bails, findCodeTokens_nonbails; public static int findCodeTokens(List tok, int startIdx, boolean ignoreCase, String[] tokens, Object condition) { if (findCodeTokens_debug) { if (eq(getClassName(tok), "main$IndexedList2")) findCodeTokens_indexed++; else findCodeTokens_unindexed++; } int end = tok.size() - tokens.length * 2 + 2, nTokens = tokens.length; int i = startIdx | 1; String firstToken = tokens[0]; if (!ignoreCase && !findCodeTokens_specials.contains(firstToken)) { while (i < end && !firstToken.equals(tok.get(i))) i += 2; } outer: for (; i < end; i += 2) { for (int j = 0; j < nTokens; j++) { String p = tokens[j], t = tok.get(i + j * 2); boolean match; if (eq(p, "*")) match = true; else if (eq(p, "")) match = isQuoted(t); else if (eq(p, "")) match = isIdentifier(t); else if (eq(p, "")) match = isInteger(t); else if (eq(p, "\\*")) match = eq("*", t); else match = ignoreCase ? eqic(p, t) : eq(p, t); if (!match) continue outer; } if (condition == null || checkTokCondition(condition, tok, i - 1)) return i; } return -1; } public static String jreplaceExpandRefs(String s, List tokref) { List tok = javaTok(s); for (int i = 1; i < l(tok); i += 2) { if (tok.get(i).startsWith("$") && isInteger(tok.get(i).substring(1))) { String x = tokref.get(-1 + parseInt(tok.get(i).substring(1)) * 2); tok.set(i, x); } } return join(tok); } public static void clearAllTokens(List tok) { for (int i = 0; i < tok.size(); i++) tok.set(i, ""); } public static void clearAllTokens(List tok, int i, int j) { for (; i < j; i++) tok.set(i, ""); } public static List reTok(List tok) { replaceCollection(tok, javaTok(tok)); return tok; } public static List reTok(List tok, int i) { return reTok(tok, i, i + 1); } public static List reTok(List tok, int i, int j) { i = i & ~1; j = j | 1; List t = javaTok(join(subList(tok, i, j))); replaceListPart(tok, i, j, t); return tok; } public static Map curlyBracketMap(List tok) { TreeMap map = new TreeMap(); List stack = new ArrayList(); int n = l(tok); for (int i = 1; i < n; i += 2) { String t = tok.get(i); if (eq(t, "{")) stack.add(i); else if (eq(t, "}") && nempty(stack)) map.put(liftLast(stack), i); } return map; } public static String joinSubList(List l, int i, int j) { return join(subList(l, i, j)); } public static String joinSubList(List l, int i) { return join(subList(l, i)); } public static boolean isSafeCodeFragment(String code) { return eq("safe", joinWithComma(getCodeFragmentSafety(code))); } public static void replaceTokens(List tok, int i, int j, String s) { clearAllTokens(subList(tok, i, j)); tok.set(i, s); } public static void replaceTokens(List tok, String s) { clearAllTokens(tok); tok.set(0, s); } public static String strOrEmpty(Object o) { return o == null ? "" : str(o); } public static Object dm_javaEvalOrInterpret(String code) { final Object module = dm_current_generic(); code = trim(code); if (canQuickEval(code)) return quickEval(code); Pair> p = tok_parseFunctionCall(code); if (p != null) { if (module != null) makeAndCall_initChild.set(new F1() { public AutoCloseable get(Class c) { try { shareMyCreatorWith(c); InheritableThreadLocal tl = (InheritableThreadLocal) (getOpt(c, "dm_currentModule_generic")); return tempSetTL(tl, weakRef(module)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "shareMyCreatorWith(c);\r\n InheritableThreadLocal tl = cast..."; } }); return callAndMake(p.a, toObjectArray(map("dm_javaEvalOrInterpret", p.b))); } return dm_javaEval(code); } public static Throwable printStackTrace(Throwable e) { print(getStackTrace(e)); return e; } public static void printStackTrace() { printStackTrace(new Throwable()); } public static void printStackTrace(String msg) { printStackTrace(new Throwable(msg)); } public static Collector limitedListCollector(final Collection l, final int max) { return new Collector() { public boolean full() { return l(l) >= max; } public boolean add(A a) { if (full()) return false; l.add(a); return full(); } public boolean contains(A a) { return l.contains(a); } }; } public static TreeSet litciset(String... items) { TreeSet set = caseInsensitiveSet(); for (String a : items) set.add(a); return set; } public static String formatWithThousandsSeparator(long l) { return NumberFormat.getInstance(new Locale("en_US")).format(l); } public static A printIndent(A o) { print(indentx(str(o))); return o; } public static A printIndent(String indent, A o) { print(indentx(indent, str(o))); return o; } public static void printIndent(int indent, Object o) { print(indentx(indent, str(o))); } public static int indent_default = 2; public static String indent(int indent) { return repeat(' ', indent); } public static String indent(int indent, String s) { return indent(repeat(' ', indent), s); } public static String indent(String indent, String s) { return indent + s.replace("\n", "\n" + indent); } public static String indent(String s) { return indent(indent_default, s); } public static List indent(String indent, List lines) { List l = new ArrayList(); if (lines != null) for (String s : lines) l.add(indent + s); return l; } public static Comparator caseInsensitiveComparator() { return betterCIComparator(); } public static List> dm_allRulesFromRulesModuleWithCommentsAndIDs() { return collectThreeFieldsAsTriple("text", "comments", "globalID", (List) dm_call(dm_gazelle_rulesModule(), "concepts")); } public static GazelleEvalContext gazelle_stdEvalContext_v2(List> rules, Object... __) { long startTime = sysNow(); GazelleEvalContext ctx = new GazelleEvalContext(); ctx.engine = new RuleEngine2(); ctx.engine.addRules2(rules); gazelle_changeInTypesForRulesWithMultipleInputs(ctx.engine); MultiSet statements = dm_gazelle_statementsFromAppliedRules_multiSet(); ctx.dataByRule = new HashMap(); Map> statementsByRule = ai_gazelle_indexStatementsByRule_multiSet(statements); for (String ruleID : keys(statementsByRule)) { MultiSet statements2 = statementsByRule.get(ruleID); ctx.dataByRule.put(ruleID, ai_gazelle_analyzeStatementsForRule_multiSet(statements2)); } ctx.predictor = dm_gazelle_standardPredictor(__); done2(startTime, "Making engine with " + n2(rules, "rule")); return ctx; } public static Object dm_callFunctionOnString(String code, String s) { return callF(dm_codeWithHintsToFunctionOnString(code), s); } public static boolean cic(Collection l, String s) { return containsIgnoreCase(l, s); } public static boolean cic(String[] l, String s) { return containsIgnoreCase(l, s); } public static boolean cic(String s, char c) { return containsIgnoreCase(s, c); } public static boolean cic(String a, String b) { return containsIgnoreCase(a, b); } public static boolean cicOneOf(String s, String... l) { for (String x : l) if (cic(s, x)) return true; return false; } public static boolean cicOneOf(Collection l, String... bla) { for (String x : unnull(l)) if (eqicOneOf(x, bla)) return true; return false; } public static class gazelle_addHelperTablesToRules_Massager { public List> helperTable; public Map> helperIndex; public gazelle_addHelperTablesToRules_Massager() { } public gazelle_addHelperTablesToRules_Massager(List> helperTable, Map> helperIndex) { this.helperIndex = helperIndex; this.helperTable = helperTable; } public Map get(Map map, List tokC, List tokI, RuleEngine2_MatchedRule matched) { if (map == null) return null; for (String t : codeTokens(matched.tokenize(matched.rule.out))) { if (map.containsKey(t)) continue; Pair idx = helperIndex.get(t); if (idx == null) continue; { final Map __2871 = cloneMap(map); for (String key : keys(__2871)) { String val = __2871.get(key); int idx2 = indexOfIC(helperTable.get(idx.a), key); if (idx2 < 0) continue; Pair idxVal = helperIndex.get(val); if (idxVal == null) continue; String out = _get(_get(helperTable, idxVal.a), idx.b); print("Massaged: " + t + " <-> " + key + " => " + out); mapPut(map, t, out); } } } return map; } } public static void gazelle_addHelperTablesToRules(RuleEngine2 engine) { Matches m = new Matches(); for (final RuleEngine2.Rule r : engine.rules) { { if (!(matchAny("use helper table mech list *", r.comments, m))) continue; } List entries = mL(m.unq(0)); { if (empty(entries)) continue; } final List> helperTable = map("tok_splitAtCommaOrDoubleArrow", entries); final Map> helperIndex = indexTwoDArrayIC(helperTable); r.addMapMassager(new gazelle_addHelperTablesToRules_Massager(helperTable, helperIndex)); } } public static class gazelle_addVarSplittersToRules_VarSplitter { public String var; public boolean debug; public gazelle_addVarSplittersToRules_VarSplitter() { } public gazelle_addVarSplittersToRules_VarSplitter(String var, boolean debug) { this.debug = debug; this.var = var; } public Map get(Map map, List tokC, List tokI, RuleEngine2_MatchedRule matched) { if (map == null) return null; String value = getOrKeep(map, curly(var)); List tok1 = matched.tokenize(var); List tok2 = matched.tokenize(uncurly(value)); if (debug) print("VarSplitter: " + tok1 + " / " + tok2); if (l(tok1) != l(tok2)) return null; int oldSize = l(map); for (int i = 1; i < l(tok1); i += 2) if (!strictPutIC(map, tok1.get(i), tok2.get(i))) return null; if (debug) print("VarSplitter done => " + map); matched.nPossibleVars = matched.nPossibleVars() + l(map) - oldSize; return map; } } public static void gazelle_addVarSplittersToRules(RuleEngine2 engine, Object... __) { Matches m = new Matches(); boolean debug = boolPar("debug", __); for (final RuleEngine2.Rule r : engine.rules) { { if (!(jmatchAny("split *", r.comments, m) && isQuoted(m.get(0)))) continue; } String var = m.unq(0); if (debug) print("Made splitter for: " + var); r.addMapMassager(new gazelle_addVarSplittersToRules_VarSplitter(var, debug)); } } public static class gazelle_addVarDiscriminators_MechListContains extends F1 { public String list; public gazelle_addVarDiscriminators_MechListContains() { } public gazelle_addVarDiscriminators_MechListContains(String list) { this.list = list; } public Boolean get(String s) { boolean ok = mechCISetContains(list, s); print("MechListContains: " + list + " / " + s + " => " + ok); return ok; } } public static class gazelle_addVarDiscriminators_VarDiscriminator { public String var; public Object discriminator; public boolean debug; public gazelle_addVarDiscriminators_VarDiscriminator() { } public gazelle_addVarDiscriminators_VarDiscriminator(String var, Object discriminator, boolean debug) { this.debug = debug; this.discriminator = discriminator; this.var = var; } public Map get(Map map, List tokC, List tokI, RuleEngine2_MatchedRule matched) { if (map == null) return null; String value = map.get(var); if (value != null && !isTrue(callAndMake_orF1(discriminator, value))) { if (debug) print("Rejecting var: " + var + " -> " + value + " because of " + discriminator); return null; } return map; } } public static void gazelle_addVarDiscriminators(RuleEngine2 engine, Object... __) { boolean debug = boolPar("debug", __) || boolPar("debugVarDiscriminators", __); for (final RuleEngine2.Rule r : engine.rules) { for (Matches m : getJMatches_all("var * should satisfy *", r.comments)) { String var = m.unq(0), discriminator = m.unq(1); if (debug) print("Made discriminator for: " + var); r.addMapMassager(new gazelle_addVarDiscriminators_VarDiscriminator(var, discriminator, debug)); } for (Matches m : getJMatches_all("var * should be in list *", r.comments)) { String var = m.unq(0), list = m.unq(1); r.addMapMassager(new gazelle_addVarDiscriminators_VarDiscriminator(var, new gazelle_addVarDiscriminators_MechListContains(list), debug)); } for (Matches m : getJMatches_all("* has to be between * and *", r.comments)) { String var = m.unq(0); double min = parseDouble(m.unq(1)), max = parseDouble(m.unq(2)); if (debug) print("Made discriminator for: " + var); r.addMapMassager(new gazelle_addVarDiscriminators_VarDiscriminator(var, new F1() { public Boolean get(String value) { try { try { return isBetween_double(parseDouble(or(value, var)), min, max); } catch (Throwable __e) { silentException(__e); } return false; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "pcall-silent {\r\n ret isBetween_double(parseDouble(or(value, var)), m..."; } }, debug)); } for (Matches m : getJMatches_all("* has to be * or *", r.comments)) { String var = m.unq(0); Set values = litciset(m.unq(1), m.unq(2)); if (debug) print("Made discriminator for: " + var); r.addMapMassager(new gazelle_addVarDiscriminators_VarDiscriminator(var, new F1() { public Boolean get(String value) { try { return contains(values, or(value, var)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "contains(values, or(value, var))"; } }, debug)); } } } public static class gazelle_addVarConvertersToRules_VarConverter { public String var1, var2; public Object converter; public boolean debug; public gazelle_addVarConvertersToRules_VarConverter() { } public gazelle_addVarConvertersToRules_VarConverter(String var1, String var2, Object converter, boolean debug) { this.debug = debug; this.converter = converter; this.var2 = var2; this.var1 = var1; } public Map get(Map map, List tokC, List tokI, RuleEngine2_MatchedRule matched) { if (map == null) return null; String value = map.get(var1); if (value == null) return map; String value2 = (String) callAndMake_orF1(converter, value); if (debug) print("VarConverter: " + value + " => " + value2); if (!strictPutIC(map, var2, value2)) return null; return map; } } public static void gazelle_addVarConvertersToRules(RuleEngine2 engine, Object... __) { boolean debug = boolPar("debug", __) || boolPar("varConverterDebug", __); for (final RuleEngine2.Rule r : engine.rules) for (Matches m : getJMatches_all("convert var * to * with *", r.comments)) r.addMapMassager(new gazelle_addVarConvertersToRules_VarConverter(m.unq(0), m.unq(1), m.unq(2), debug)); } public static class gazelle_addCalculationFunctionsToRules_Calculator { public String var1, var2, var3; public Object function; public boolean debug; public gazelle_addCalculationFunctionsToRules_Calculator() { } public gazelle_addCalculationFunctionsToRules_Calculator(String var1, String var2, String var3, Object function, boolean debug) { this.debug = debug; this.function = function; this.var3 = var3; this.var2 = var2; this.var1 = var1; } public Map get(Map map, List tokC, List tokI, RuleEngine2_MatchedRule matched) { if (map == null) return null; double result; try { String value1 = getOrKeep(map, var1), value2 = getOrKeep(map, var2); result = (double) callAndMake_orF2(function, parseDouble(value1), parseDouble(value2)); } catch (Throwable _e) { return null; } if (!strictPutIC(map, var3, str(iround(result)))) return null; return map; } } public static void gazelle_addCalculationFunctionsToRules(RuleEngine2 engine, Object... __) { boolean debug = boolPar("debug", __) || boolPar("calcDebug", __); for (final RuleEngine2.Rule r : engine.rules) for (Matches m : getJMatches_all("compute var * from * and * using *", r.comments)) { r.addMapMassager(new gazelle_addCalculationFunctionsToRules_Calculator(m.unq(1), m.unq(2), m.unq(0), m.unq(3), debug)); if (debug) print("Calculator made"); } } public static class gazelle_addRespondToHumanOnlyHelpers_Check { public boolean debug; public gazelle_addRespondToHumanOnlyHelpers_Check() { } public gazelle_addRespondToHumanOnlyHelpers_Check(boolean debug) { this.debug = debug; } public Map get(Map map, List tokC, List tokI, RuleEngine2_MatchedRule matched) { if (map == null) return null; if (debug) print("humanOnlyHelper for " + matched.ruleID() + ": " + matched.inputsByType); if (!eq("true", matched.inputsByType.get("_respondingToHuman"))) return null; return map; } } public static void gazelle_addRespondToHumanOnlyHelpers(RuleEngine2 engine, Object... __) { boolean debug = boolPar("debug", __) || boolPar("humanOnlyDebug", __); for (final RuleEngine2.Rule r : engine.rules) if (cic(r.comments, "only answer to human")) { if (debug) print("Made human-only helper"); r.addMapMassager(new gazelle_addRespondToHumanOnlyHelpers_Check(debug)); } } public static class gazelle_addForbiddenBindingHelpers_Blocker { public String var, value; public boolean debug; public gazelle_addForbiddenBindingHelpers_Blocker() { } public gazelle_addForbiddenBindingHelpers_Blocker(String var, String value, boolean debug) { this.debug = debug; this.value = value; this.var = var; } public Map get(Map map, List tokC, List tokI, RuleEngine2_MatchedRule matched) { if (map == null) return null; if (value == null) { if (map.containsKey(var)) return null; } else if (eqic(value, map.get(var))) return null; return map; } } public static class gazelle_addForbiddenBindingHelpers_TotalBlocker { public Map get(Map map, List tokC, List tokI, RuleEngine2_MatchedRule matched) { if (nempty(map)) return null; return map; } } public static class gazelle_addForbiddenBindingHelpers_BlockAllExcept { public Set allowedVars; public gazelle_addForbiddenBindingHelpers_BlockAllExcept() { } public gazelle_addForbiddenBindingHelpers_BlockAllExcept(Set allowedVars) { this.allowedVars = allowedVars; } public Map get(Map map, List tokC, List tokI, RuleEngine2_MatchedRule matched) { if (containsKeysExcept(map, allowedVars)) return null; return map; } } public static void gazelle_addForbiddenBindingHelpers(RuleEngine2 engine, Object... __) { boolean debug = boolPar("debug", __) || boolPar("forbiddenBindingsDebug", __); for (final RuleEngine2.Rule r : engine.rules) { for (Matches m : getJMatches_all("don't change * to *", r.comments)) r.addMapMassager(new gazelle_addForbiddenBindingHelpers_Blocker(m.unq(0), m.unq(1), debug)); for (Matches m : getJMatches_all("don't change *", r.comments)) if (isQuoted(m.get(0))) r.addMapMassager(new gazelle_addForbiddenBindingHelpers_Blocker(m.unq(0), null, debug)); if (cic(r.comments, "don't change anything")) r.addMapMassager(new gazelle_addForbiddenBindingHelpers_TotalBlocker()); String var = matchAny_firstGroup("only var is *", r.comments); if (var != null) r.addMapMassager(new gazelle_addForbiddenBindingHelpers_BlockAllExcept(litciset(var))); } } public static List gazelle_getChildren(GazelleTree tree, Object... __) { if (tree == null) return null; if (tree.children != null) return tree.children; tree.children = new ArrayList(); double qualityCutOff = optPar("qualityCutOff", __, -0.75); List dialogHistory = (List) (optPar("dialogHistory", __)); boolean debug = boolPar("debug", __); if (tree.ctx == null) tree.setContext(assertNotNull("evalContext", (GazelleEvalContext) callF(assertNotNull("makeEvalContext", (Object) optPar("makeEvalContext", __))))); GazelleEvalContext ctx = tree.ctx; RuleEngine2 engine = ctx.engine; Map inputMap = gazelle_inputMapFromLineAndHistory(tree.line, concatLists(dialogHistory, tree.history())); if (boolPar("respondingToHuman", __)) inputMap.put("_respondingToHuman", "true"); if (debug) print("inputMap=" + sfu(asTreeMap(inputMap))); if (!boolPar("skipSplitters", __)) for (RuleEngine2.SimplifyWithRule rule : engine.splitterRules()) { try { Collection out = gazelle_executeSplitterRule(engine, rule, tree.line); if (empty(out)) continue; GazelleTree child = new GazelleTree(joinWithVBar(out)); child.ctx = ctx; child.isSplitNode = true; tree.add(child); for (String line : out) child.add(new GazelleTree(line)); } catch (Throwable __e) { _handleException(__e); } } boolean firstConditionOnly = boolPar("firstConditionOnly", __); Object[] params = litobjectarray("debug", optPar("debug", __), "tree", tree); List l = sortByMethodDesc("qualitySum", firstConditionOnly ? ai_ruleEngine2_rulesForInput_5(engine, inputMap, params) : ai_ruleEngine2_rulesForInput_5_allConditions(engine, inputMap, params)); for (RuleEngine2_MatchedRule r : l) { double quality = r.relativeQuality(); double cutOff = qualityCutOff; if (r.rule != null && r.rule.qualityCutOff != null) cutOff = r.rule.qualityCutOff; if (quality < cutOff && boolPar("skipBad", __, true)) { if (debug) print("Match under quality (" + quality + "/" + qualityCutOff + "): " + r); continue; } String line = r.outText(); GazelleTree child = new GazelleTree(line); child.ctx = ctx; child.mr = r; if (r.rule != null) child.lineType = r.rule.outType(); gazelle_addPredictionToTreeNode(child); tree.add(child); } gazelle_sortChildren(tree); return tree.children; } public static TreeMap ciMap() { return caseInsensitiveMap(); } public static void gazelle_addPredictionToTreeNode(GazelleTree child, Object... __) { boolean debug = boolPar("debug", __); Set badComments = (Set) (optPar("badComments", __)); Set acceptablePurposes = (Set) (optPar("acceptablePurposes", __)); RuleEngine2_MatchedRule r = child.mr; GazelleEvalContext ctx = child.ctx; if (containsAny(r.rule.comments, badComments) || acceptablePurposes != null && !acceptablePurposes.contains(unnull(r.rule.purpose))) { if (debug) print("prediction=bad for various reasons"); child.prediction = "bad"; } else { if (ctx.predictor == null) child.prediction = null; else { child.prediction = ctx.predictor.get(ctx, r); if (debug) print("Prediction=" + child.prediction + " for " + r + " (" + (r.moreConditions() ? "unsatisfied" : "satisfied") + ") by " + ctx.predictor); } } } public static String curlyBraceOptIfMoreThanOneToken(String s) { return !isCurlyBraced(s) && numberOfJavaTokens(s) > 1 ? curlyBrace(s) : s; } public static List sortByCalculatedFieldDesc(Collection c, final Object f) { return sortByCalculatedFieldDesc_inPlace(cloneList(c), f); } public static List sortByCalculatedFieldDesc(Object f, Collection c) { return sortByCalculatedFieldDesc(c, f); } public static String[] dropFirst(int n, String[] a) { return drop(n, a); } public static String[] dropFirst(String[] a) { return drop(1, a); } public static Object[] dropFirst(Object[] a) { return drop(1, a); } public static List dropFirst(List l) { return dropFirst(1, l); } public static List dropFirst(int n, Iterable i) { return dropFirst(n, toList(i)); } public static List dropFirst(Iterable i) { return dropFirst(toList(i)); } public static List dropFirst(int n, List l) { return n <= 0 ? l : new ArrayList(l.subList(Math.min(n, l.size()), l.size())); } public static List dropFirst(List l, int n) { return dropFirst(n, l); } public static String dropFirst(int n, String s) { return substring(s, n); } public static String dropFirst(String s, int n) { return substring(s, n); } public static String dropFirst(String s) { return substring(s, 1); } public static boolean matchAny(Collection patterns, String s, Matches m) { for (String pattern : patterns) if (match(pattern, s, m)) return true; return false; } public static boolean matchAny(Collection patterns, String s) { return matchAny(patterns, s, null); } public static boolean matchAny(String pat, Iterable l) { return matchAny(pat, l, null); } public static boolean matchAny(String pat, Iterable l, Matches m) { if (l != null) for (String s : l) if (match(pat, s, m)) return true; return false; } public static List dm_gazelle_fulfillmentsForRule(String ruleID) { return (List) dm_call(dm_gazelle_fulfillmentsCRUD(), "entriesForRule", ruleID); } public static TreeMap asCIMap(Map map) { return asCaseInsensitiveMap(map); } public static Map safeUnstructMap(String s) { return safeUnstructureMap(s); } public static List identifiersOnly(Collection l) { List out = new ArrayList(); for (String a : unnull(l)) if (isIdentifier(a)) out.add(a); return out; } public static List codeTokens(List tok) { return codeTokensOnly(tok); } public static Map mergeMapsStrictlyIC(Map map1, Map map2) { if (map2 == null) return map1; if (map1 == null) return map2; if (!canPutAllStrictlyIC(map1, map2)) return null; return mergeMaps(map1, map2); } public static Map ciMapWithoutKeysEqualToValues(Map map) { if (map == null) return null; Map map2 = ciMap(); for (String key : keys(map)) { String value = map.get(key); if (!eqic(key, value)) map2.put(key, value); } return map2; } public static boolean gazelle_isRecentlyUsedMapping(GazelleTree t, Map varMap, Object... __) { String context = stringPar("context", __); if (empty(context)) return false; return t != null && t.rule() != null && t.rule().avoidRecentlyUsedMappings && isTrue(dm_call(gazelle_rumCRUD(), "hasRecentlyUsedMapping", context, t.rule().globalID, varMap)); } public static boolean ai_ruleEngine2_matchStep(final RuleEngine2_MatchedRule matched, Map inputsByType, boolean fillRemainingConditions, Object... __) { boolean debug = boolPar("debug", __); RuleEngine2.Rule rule = matched.rule; TextAndType in = rule.insWithType.get(matched.iCond); String input = inputsByType.get(in.type); if (input == null) return false; Object inputMassager = get(rule.inputMassagers, matched.iCond); Object checkerFunction = get(rule.checkerFunctions, matched.iCond); Object matcher = get(rule.matchers, matched.iCond); List tokC = null, tokI = null; Map map; if (inputMassager != null) { input = (String) callF(inputMassager, input); if (debug) print("Massaged input to: " + input); if (input == null) return false; } if (checkerFunction != null) map = (Map) callF(checkerFunction, input); else { String cond = in.text; String tokenize = matched.tokenizationFunction; tokC = (List) callAndMake(tokenize, cond); tokI = (List) callAndMake(tokenize, input); if (debug) print("matchStep inputsByType=" + sfu(asTreeMap(inputsByType))); if (debug) print("matchStep in=" + in + ", iCond=" + matched.iCond + ", insWithType: " + rule.insWithType); if (debug) print("matchStep tokI/tokC: " + codeTokens(tokI) + " / " + codeTokens(tokC)); if (contains(cond, "{}")) tokI = ai_applyWordSplitPattern1(tokC, tokI); if (matcher != null) map = (Map) callF(matcher, tokC, tokI, matched); else map = zipCodeTokensToCIMap_strict_withoutEquals(tokC, tokI); } if (debug) print("matchStep for rule " + rule.globalID + " map: " + map); if (!gazelle_addMatch(matched, map, tokC, tokI, __)) { if (debug) print("addMatch failed - " + strWithIdentityHashCode(matched)); return false; } matched.iCond++; if (debug) print("matchStep succeeded. " + matched); matched.matchedLines.add(pair(tokC, tokI)); if (fillRemainingConditions) { matched.remainingConditions = nullIfEmpty(map(dropFirst(matched.iCond, rule.in), new F1() { public String get(String s) { try { return matched.applyMappingTo(s); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "matched.applyMappingTo(s)"; } })); matched.remainingConditionsWithType = nullIfEmpty(map(dropFirst(matched.iCond, rule.insWithType), new F1() { public TextAndType get(TextAndType in) { try { return new TextAndType(matched.applyMappingTo(in.text), in.type); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "TextAndType(matched.applyMappingTo(in.text), in.type)"; } })); } Map, Integer> qualities = ai_ruleEngine2_mappingQualities(rule); for (String a : keys(map)) { String b = map.get(a); mapPut(matched.mappingQuality, a, qualities.get(pair(a, b))); } if (debug) print("iCond now=" + matched.iCond + ", next input: " + get(matched.rule.insWithType, matched.iCond)); return true; } public static boolean gazelle_isEmbeddedEval(String s) { return startsWithEndsWith(s, "{=", "}"); } public static A requiredPar(ThreadLocal tl) { return assertNotNull(optPar(tl)); } public static Object requiredPar(Object[] params, String name) { Object o = optPar(params, name); if (o == null) throw fail("Need " + name); return o; } public static Object requiredPar(String name, Object[] params) { return requiredPar(params, name); } public static List antiFilter(Iterable c, Object pred) { if (pred instanceof F1) return antiFilter(c, (F1) pred); List x = new ArrayList(); if (c != null) for (Object o : c) if (!isTrue(callF(pred, o))) x.add(o); return x; } public static List antiFilter(Object pred, Iterable c) { return antiFilter(c, pred); } public static List antiFilter(Object pred, Object[] c) { return antiFilter(pred, wrapArrayAsList(c)); } public static List antiFilter(Iterable c, F1 pred) { List x = new ArrayList(); if (c != null) for (B o : c) if (!pred.get(o).booleanValue()) x.add(o); return x; } public static List antiFilter(F1 pred, Iterable c) { return antiFilter(c, pred); } public static List antiFilter(Iterable c, IF1 pred) { List x = new ArrayList(); if (c != null) for (B o : c) if (!pred.get(o).booleanValue()) x.add(o); return x; } public static List antiFilter(IF1 pred, Iterable c) { return antiFilter(c, pred); } public static String asString(Object o) { return o == null ? null : o.toString(); } public static List matchAll(String pat, Iterable l) { List l2 = new ArrayList(); Matches m = new Matches(); if (l != null) for (String s : l) if (match(pat, s, m)) { l2.add(m); m = new Matches(); } return l2; } public static Set asSet(Object[] array) { HashSet set = new HashSet(); for (Object o : array) if (o != null) set.add(o); return set; } public static Set asSet(String[] array) { TreeSet set = new TreeSet(); for (String o : array) if (o != null) set.add(o); return set; } public static Set asSet(Iterable l) { if (l instanceof Set) return (Set) l; HashSet set = new HashSet(); for (A o : unnull(l)) if (o != null) set.add(o); return set; } public static boolean odd(int i) { return (i & 1) != 0; } public static boolean odd(long i) { return (i & 1) != 0; } public static boolean odd(BigInteger i) { return odd(toInt(i)); } public static int findEndOfBracketPart2(List cnc, int i) { int j = i + 2, level = 1; while (j < cnc.size()) { if (eqOneOf(cnc.get(j), "{", "(", "[")) ++level; else if (eqOneOf(cnc.get(j), "}", ")", "]")) --level; if (level == 0) return j + 1; ++j; } return cnc.size(); } public static IterableIterator toLines(File f) { return linesFromFile(f); } public static List toLines(String s) { List lines = new ArrayList(); if (s == null) return lines; int start = 0; while (true) { int i = toLines_nextLineBreak(s, start); if (i < 0) { if (s.length() > start) lines.add(s.substring(start)); break; } lines.add(s.substring(start, i)); if (s.charAt(i) == '\r' && i + 1 < s.length() && s.charAt(i + 1) == '\n') i += 2; else ++i; start = i; } return lines; } public static int toLines_nextLineBreak(String s, int start) { for (int i = start; i < s.length(); i++) { char c = s.charAt(i); if (c == '\r' || c == '\n') return i; } return -1; } public static CloseableIterableIterator linesFromFile(File f) { try { if (!f.exists()) return emptyCloseableIterableIterator(); if (ewic(f.getName(), ".gz")) return linesFromReader(utf8bufferedReader(newGZIPInputStream(f))); return linesFromReader(utf8bufferedReader(f)); } catch (Exception __e) { throw rethrow(__e); } } public static Object quickExport(Object o, Object dest) { return quickExport_impl(o, dest, new IdentityHashMap(), new HashMap()); } public static Object quickExport(Object o, Object dest, int expectedNumberOfObjects) { return quickExport_impl(o, dest, new IdentityHashMap(expectedNumberOfObjects), new HashMap()); } public static Object quickExport_impl(Object o, Object dest, IdentityHashMap seen, HashMap classMap) { try { if (o == null || o instanceof String || o instanceof Number) return o; Object oo = seen.get(o); if (oo != null) return oo; if (o instanceof Object[]) { Object[] l = (Object[]) o; Object[] destO = newObjectArrayOfSameType(l, l.length); seen.put(o, destO); for (int i = 0; i < l.length; i++) destO[i] = quickExport_impl(l[i], dest, seen, classMap); return destO; } if (o instanceof List) { List l = (List) o; List destO = new ArrayList(l.size()); seen.put(o, destO); for (int i = 0; i < l.size(); i++) destO.add(quickExport_impl(l.get(i), dest, seen, classMap)); return destO; } if (o instanceof Map) { Map m = (Map) o; Map destO = new HashMap(); seen.put(o, destO); for (Object e : ((Map) o).entrySet()) destO.put(quickExport_impl(((Map.Entry) e).getKey(), dest, seen, classMap), quickExport_impl(((Map.Entry) e).getValue(), dest, seen, classMap)); return destO; } Class c = o.getClass(); String className = c.getName(); if (className.startsWith("main$")) { Class destClass = classMap.get(c); if (destClass == null) { if (!classMap.containsKey(c)) { if (!isAnonymousClassName(className)) destClass = getClass_vmName(dest, className); classMap.put(c, destClass); } if (destClass == null) return o; } if (c == destClass) return o; Object destO = nuObjectWithoutArguments(destClass); seen.put(o, destO); while (c != Object.class) { Field[] fields = c.getDeclaredFields(); for (Field field : fields) { if ((field.getModifiers() & Modifier.STATIC) != 0) continue; field.setAccessible(true); Object value = field.get(o); setOpt(destO, field.getName(), quickExport_impl(value, dest, seen, classMap)); } c = c.getSuperclass(); } return destO; } return o; } catch (Exception __e) { throw rethrow(__e); } } public static boolean endsWith(String a, String b) { return a != null && a.endsWith(b); } public static boolean endsWith(String a, char c) { return nempty(a) && lastChar(a) == c; } public static boolean endsWith(String a, String b, Matches m) { if (!endsWith(a, b)) return false; m.m = new String[] { dropLast(l(b), a) }; return true; } public static boolean isCISet(Iterable l) { return l instanceof TreeSet && ((TreeSet) l).comparator() == caseInsensitiveComparator(); } public static List splitAtTokens(String s, List tokens) { return splitAtTokens(javaTok(s), tokens); } public static List splitAtTokens(List tok, List tokens) { List l = new ArrayList(); int i = 0; while (i < l(tok)) { int j = indexOfSubList(tok, tokens, i); if (i >= l(tok)) break; if (j < 0) j = l(tok); l.add(trimJoin(tok.subList(i, j))); i = j + l(tokens); } return l; } public static List splitAtTokens(List tok, String... tokens) { List l = new ArrayList(); int i = 0; while (i < l(tok)) { int j = indexOfSubList(tok, tokens, i); if (i >= l(tok)) break; if (j < 0) j = l(tok); l.add(trimJoin(tok.subList(i, j))); i = j + l(tokens); } return l; } public static List javaTokWithBrackets(String s) { return javaTokPlusBrackets(s); } public static String hideCredentials(URL url) { return url == null ? null : hideCredentials(str(url)); } public static String hideCredentials(String url) { return url.replaceAll("([&?])(_pass|key)=[^&\\s\"]*", "$1$2="); } public static String hideCredentials(Object o) { return hideCredentials(str(o)); } public static String getStackTrace(Throwable throwable) { lastException(throwable); return getStackTrace_noRecord(throwable); } public static String getStackTrace_noRecord(Throwable throwable) { StringWriter writer = new StringWriter(); throwable.printStackTrace(new PrintWriter(writer)); return hideCredentials(writer.toString()); } public static String getStackTrace() { return getStackTrace_noRecord(new Throwable()); } public static Throwable unwrapTrivialExceptionWraps(Throwable e) { if (e == null) return e; while (e.getClass() == RuntimeException.class && e.getCause() != null && eq(e.getMessage(), str(e.getCause()))) e = e.getCause(); return e; } public static String replacePrefix(String prefix, String replacement, String s) { if (!startsWith(s, prefix)) return s; return replacement + substring(s, l(prefix)); } public static Throwable innerException2(Throwable e) { if (e == null) return null; while (empty(e.getMessage()) && e.getCause() != null) e = e.getCause(); return e; } public static Throwable getException(Runnable r) { try { callF(r); return null; } catch (Throwable e) { return e; } } public static Object pcallF(Object f, Object... args) { return pcallFunction(f, args); } public static A pcallF(F0 f) { try { return f == null ? null : f.get(); } catch (Throwable __e) { return null; } } public static B pcallF(F1 f, A a) { try { return f == null ? null : f.get(a); } catch (Throwable __e) { return null; } } public static Map vm_generalMap_map; public static Map vm_generalMap() { if (vm_generalMap_map == null) vm_generalMap_map = (Map) get(javax(), "generalMap"); return vm_generalMap_map; } public static Map synchronizedMRUCache(int maxSize) { return synchroMap(new MRUCache(maxSize)); } public 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; } public static int jfind(String s, String in) { return jfind(javaTok(s), in); } public static int jfind(List tok, String in) { return jfind(tok, 1, in); } public static int jfind(List tok, int startIdx, String in) { return jfind(tok, startIdx, in, null); } public static int jfind(List tok, String in, Object condition) { return jfind(tok, 1, in, condition); } public static int jfind(List tok, int startIdx, String in, Object condition) { return jfind(tok, startIdx, javaTokForJFind_array(in), condition); } public static int jfind(List tok, List tokin) { return jfind(tok, 1, tokin); } public static int jfind(List tok, int startIdx, List tokin) { return jfind(tok, startIdx, tokin, null); } public static int jfind(List tok, int startIdx, String[] tokinC, Object condition) { return findCodeTokens(tok, startIdx, false, tokinC, condition); } public static int jfind(List tok, int startIdx, List tokin, Object condition) { return findCodeTokens(tok, startIdx, false, codeTokensAsStringArray(tokin), condition); } public 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; } public static boolean isQuoted(String s) { if (isNormalQuoted(s)) return true; return isMultilineQuoted(s); } public static boolean checkTokCondition(Object condition, List tok, int i) { if (condition instanceof TokCondition) return ((TokCondition) condition).get(tok, i); return checkCondition(condition, tok, i); } public static void replaceCollection(Collection dest, Collection src) { dest.clear(); dest.addAll(src); } public static void replaceListPart(List l, int i, int j, List l2) { int j2 = i + l(l2); if (j2 == j) { copyListPart(l2, 0, l, i, l(l2)); return; } l.subList(i, j).clear(); l.addAll(i, l2); } public static A liftLast(List l) { if (l.isEmpty()) return null; int i = l(l) - 1; A a = l.get(i); l.remove(i); return a; } public static Collection getCodeFragmentSafety(String code) { Map map = codeAnalysis_identifierSafetyMap(); Set identifiers = tok_allIdentifiers(code); Collection tags = treeSet(); for (String id : identifiers) { String tag; if (codeAnalysis_isSafeIdentifier(id)) tag = "safe"; else tag = or2(map.get(id), "?"); tags.addAll(tokSplitAtComma(tag)); } tags = simplifySafetyTags(tags); if (empty(tags)) tags.add("safe"); return tags; } public static boolean canQuickEval(String t) { return isQuoted(t) || isIdentifier(t) || isInteger(t) || isSnippetID(t); } public static Object quickEval(String t) { if (isQuoted(t)) return unquote(t); if (isInteger(t)) return parseIntOrLong(t); if (isLongConstant(t)) return parseLong(t); if (eq(t, "null")) return null; if (eq(t, "false")) return false; if (eq(t, "true")) return true; if (isIdentifier(t)) return getMC(t); if (isSnippetID(t)) return t; throw quickFail("Can't quick eval: " + t); } public static Pair> tok_parseFunctionCall(String s) { return tok_parseFunctionCall(javaTok(s)); } public static Pair> tok_parseFunctionCall(List tok) { if (!(isIdentifier(get(tok, 1)) && eqGet(tok, 3, "("))) return null; Map bracketMap = getBracketMap(tok); Integer iClosing = bracketMap.get(3); if (!(eq(iClosing, l(tok) - 4) && eqGet(tok, l(tok) - 2, ";")) && neq(iClosing, l(tok) - 2)) return null; int i = 5, argStart = 5; List args = new ArrayList(); while (i < iClosing) { Integer j = bracketMap.get(i); if (j != null) { i = j + 2; continue; } if (eqGetOneOf(tok, i, ",")) { if (i > argStart) args.add(trimJoinSubList(tok, argStart, i)); argStart = i + 2; } i += 2; } if (i > argStart) args.add(trimJoinSubList(tok, argStart, i)); return pair(tok.get(1), args); } public static void shareMyCreatorWith(Object o) { setCreatorTo(o, creator()); } public static AutoCloseable tempSetTL(ThreadLocal tl, A a) { return tempSetThreadLocal(tl, a); } public static WeakReference weakRef(A a) { return newWeakReference(a); } public static Object callAndMake(String functionName, Object... args) { return callAndMake_orDirect(functionName, args); } public static Object dm_javaEval(String expression) { return dm_evalJava_withModule(expression); } public static TreeSet treeSet() { return new TreeSet(); } public static String indentx(String s) { return indentx(indent_default, s); } public static String indentx(int n, String s) { return dropSuffix(repeat(' ', n), indent(n, s)); } public static String indentx(String indent, String s) { return dropSuffix(indent, indent(indent, s)); } public static String repeat(char c, int n) { n = Math.max(n, 0); char[] chars = new char[n]; for (int i = 0; i < n; i++) chars[i] = c; return new String(chars); } public static List repeat(A a, int n) { n = Math.max(n, 0); List l = new ArrayList(n); for (int i = 0; i < n; i++) l.add(a); return l; } public static List repeat(int n, A a) { return repeat(a, n); } public static betterCIComparator_C betterCIComparator_instance; public static betterCIComparator_C betterCIComparator() { if (betterCIComparator_instance == null) betterCIComparator_instance = new betterCIComparator_C(); return betterCIComparator_instance; } public static class betterCIComparator_C implements Comparator { public int compare(String s1, String s2) { if (s1 == null) return s2 == null ? 0 : -1; if (s2 == null) return 1; int n1 = s1.length(); int n2 = s2.length(); int min = Math.min(n1, n2); for (int i = 0; i < min; i++) { char c1 = s1.charAt(i); char c2 = s2.charAt(i); if (c1 != c2) { c1 = Character.toUpperCase(c1); c2 = Character.toUpperCase(c2); if (c1 != c2) { c1 = Character.toLowerCase(c1); c2 = Character.toLowerCase(c2); if (c1 != c2) { return c1 - c2; } } } } return n1 - n2; } } public static List> collectThreeFieldsAsTriple(final String fieldA, final String fieldB, final String fieldC, Iterable l) { return map(l, new F1>() { public T3 get(C c) { try { return t3((A) getOpt(c, fieldA), (B) getOpt(c, fieldB), (C) getOpt(c, fieldC)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "t3((A) getOpt(c, fieldA), (B) getOpt(c, fieldB), (C) getOpt(c, fieldC))"; } }); } public static long sysNow() { ping(); return System.nanoTime() / 1000000; } public static void gazelle_changeInTypesForRulesWithMultipleInputs(RuleEngine2 e) { for (RuleEngine2.Rule r : e.rules) { int i = 0; while (i < l(r.insWithType) && eq(r.insWithType.get(i).type, "standard")) ++i; if (i > 1) { reverseInPlace(subList(r.insWithType, 0, i)); for (int j = 1; j < i; j++) r.insWithType.get(j).type = "history-" + (j + 1); } } } public static class dm_gazelle_statementsFromAppliedRules_ByRule { public String ruleID; public MultiSet statements = new MultiSet(); } public static MultiSet dm_gazelle_statementsFromAppliedRules_multiSet() { List concepts = cloneList((List) dm_requireAndCall("#1021413/AppliedRules", "concepts")); Map ruleMap = new HashMap(); for (Object c : concepts) { try { String judgement = getString("judgement", c); String struct = getString("matchedRuleStruct", c); RuleEngine2_MatchedRule mr = (RuleEngine2_MatchedRule) (unstructureUnlessContainsAnonymousClasses(struct)); if (mr == null) continue; String ruleID = mr.rule.globalID; dm_gazelle_statementsFromAppliedRules_ByRule r = getOrCreate_f1(ruleMap, ruleID, new F1() { public dm_gazelle_statementsFromAppliedRules_ByRule get(String ruleID) { try { return nu(dm_gazelle_statementsFromAppliedRules_ByRule.class, "ruleID", ruleID); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "nu(dm_gazelle_statementsFromAppliedRules_ByRule, +ruleID)"; } }); r.statements.addAll(ai_gazelle_statementsFromAppliedRule(mr, judgement)); } catch (Throwable __e) { _handleException(__e); } } MultiSet out = new MultiSet(); for (dm_gazelle_statementsFromAppliedRules_ByRule r : values(ruleMap)) for (String s : keys(r.statements)) out.add("Rule " + r.ruleID + ": " + s, r.statements.get(s)); return out; } public static Map> ai_gazelle_indexStatementsByRule_multiSet(MultiSet statements) { Map> map = new HashMap(); Matches m = new Matches(); for (String s : keys(statements)) if (matchStartX("Rule *: ...", s, m)) { String ruleID = m.unq(0); getOrCreateMultiSet(map, ruleID).add(m.rest(), statements.get(s)); } return map; } public static ai_gazelle_analyzeStatementsForRule_Data ai_gazelle_analyzeStatementsForRule_multiSet(MultiSet l) { ai_gazelle_analyzeStatementsForRule_Data data = ai_gazelle_analyzeStatementsForRule(keys(l)); data.statements = l; data.varScores = new HashMap(); Matches m = new Matches(); for (String s : keys(l)) if (match("in a * mapping, variable * was mapped to *", s, m)) { Pair p = pair(m.unq(1), m.unq(2)); PosNegScore score = getOrCreatePosNegScore(data.varScores, p); if (eq(m.unq(0), "good")) score.pos += l.get(s); else if (eq(m.unq(0), "bad")) score.neg += l.get(s); } return data; } public static GazellePredictor dm_gazelle_standardPredictor(Object... __) { return (ctx, r) -> { ai_gazelle_analyzeStatementsForRule_Data data = ctx.dataByRule.get(r.rule.globalID); return data == null ? null : ai_gazelle_predictor1_onData(r, data, __); }; } public static int done2_minPrint = 10; public static long done2(long startTime, String desc) { return done2(startTime, desc, done2_minPrint); } public static long done2(long startTime, String desc, int minPrint) { long time = sysNow() - startTime; saveTiming_noPrint(time); if (time >= minPrint) print(desc + " [" + time + " ms]"); return time; } public static long done2(String desc, long startTime) { return done2(startTime, desc); } public static long done2(long startTime) { return done2(startTime, ""); } public static Object dm_codeWithHintsToFunctionOnString(String code) { return dm_codeWithHintsToFunctionOnString(code, ""); } public static Object dm_codeWithHintsToFunctionOnString(String code, String hints) { return dm_requireAndCallModule("#1021247/FunctionsOnStrings", "getFunction", code, hints); } public static boolean containsIgnoreCase(Collection l, String s) { if (l != null) for (String x : l) if (eqic(x, s)) return true; return false; } public static boolean containsIgnoreCase(String[] l, String s) { if (l != null) for (String x : l) if (eqic(x, s)) return true; return false; } public static boolean containsIgnoreCase(String s, char c) { return indexOfIgnoreCase(s, String.valueOf(c)) >= 0; } public static boolean containsIgnoreCase(String a, String b) { return indexOfIgnoreCase(a, b) >= 0; } public static boolean eqicOneOf(String s, String... l) { for (String x : l) if (eqic(s, x)) return true; return false; } public static int indexOfIC(List a, String b) { return indexOfIgnoreCase(a, b); } public static int indexOfIC(List a, String b, int i) { return indexOfIgnoreCase(a, b, i); } public static int indexOfIC(String a, String b) { return indexOfIgnoreCase(a, b); } public static A _get(List l, int idx) { return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null; } public static Object _get(Object o, String field) { return get(o, field); } public static Object _get(String field, Object o) { return get(o, field); } public static A _get(A[] l, int idx) { return idx >= 0 && idx < l(l) ? l[idx] : null; } public static List mL(String name) { return mechList_opt(name); } public static List tok_splitAtCommaOrDoubleArrow(String s) { return tok_splitAtCommaOrDoubleArrow(javaTok(s)); } public static List tok_splitAtCommaOrDoubleArrow(List tok) { List l = splitAtDoubleArrow(tok); if (l(l) > 1) return l; return tok_splitAtComma(tok); } public static Map> indexTwoDArrayIC(List> table) { if (table == null) return null; Map> map = ciMap(); for (int i = 0; i < l(table); i++) { List row = table.get(i); for (int j = 0; j < l(row); j++) map.put(row.get(j), pair(i, j)); } return map; } public static A getOrKeep(Map map, A a) { if (map == null) return a; A v = map.get(a); return v != null ? v : a; } public static String curly(String s) { return optionalCurlyBrace(s); } public static String uncurly(String s) { return tok_unCurlyBracket(s); } public static boolean strictPutIC(Map map, A key, String value) { if (!(map != null && key != null && value != null)) return true; String old = map.get(key); if (old == null) { map.put(key, value); return true; } return eqic(old, value); } public static boolean jmatchAny(String pat, Iterable l) { return jmatch_any(pat, l); } public static boolean jmatchAny(String pat, Iterable l, Matches m) { return jmatch_any(pat, l, m); } public static boolean jmatchAny(List patterns, Iterable l) { return jmatch_any(patterns, l, null); } public static boolean jmatchAny(List patterns, Iterable l, Matches m) { return jmatch_any(patterns, l, m); } public static Object callAndMake_orF1(Object function, Object... args) { if (function instanceof F1) return callF(function, args); if (function == null) return null; return callAndMake_orDirect((String) function, args); } public static double parseDouble(String s) { return Double.parseDouble(s); } public static boolean isBetween_double(double x, double min, double max) { return x >= min && x <= max; } public static volatile int silentException_count; public static volatile Throwable silentException_lastException; public static Throwable silentException(Throwable e) { ++silentException_count; lastException(e); return silentException_lastException = e; } public static Throwable lastSilentException() { return silentException_lastException; } public static Object callAndMake_orF2(Object function, Object... args) { if (function instanceof F2) return callF(function, args); if (function == null) return null; return callAndMake_orDirect((String) function, args); } public static int iround(double d) { return (int) Math.round(d); } public static int iround(Number n) { return iround(toDouble(n)); } public static boolean containsKeysExcept(Map map, Set set) { if (map != null) for (A a : keys(map)) if (!contains(set, a)) return true; return false; } public static String matchAny_firstGroup(String pat, Iterable l) { Matches m = new Matches(); return matchAny(pat, l, m) ? m.unq(0) : null; } public static String joinWithVBar(Iterable l) { return join(" | ", l); } public static Object[] litobjectarray(Object... l) { return litObjectArray(l); } public static List sortByMethodDesc(Collection c, final String method) { return sortByCalculatedFieldDesc(c, new F1() { public Object get(A a) { try { return callOpt(a, method); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callOpt(a, method)"; } }); } public static List sortByMethodDesc(String method, Collection c) { return sortByMethodDesc(c, method); } public static List ai_ruleEngine2_rulesForInput_5(RuleEngine2 engine, Map inputsByType, Object... __) { boolean debug = boolPar("debug", __); List l = new ArrayList(); for (RuleEngine2.Rule rule : engine.rules) { if (rule instanceof RuleEngine2.SimplifyWithRule) { if (((RuleEngine2.SimplifyWithRule) rule).isSplitRule) continue; String in = inputsByType.get("standard"); Object f = ((RuleEngine2.SimplifyWithRule) rule).function(); if (debug) print("Running simplifier " + ((RuleEngine2.SimplifyWithRule) rule).globalID + ": " + f); Object arg = ((RuleEngine2.SimplifyWithRule) rule).callOnTree ? optPar("tree", __) : in; if (arg == null) continue; Object result = f instanceof String ? arg instanceof String ? callF(engine.callFunctionOnString, (String) f, arg) : callAndMake((String) f, arg) : callF(f, arg); if (result == null) continue; List list = result instanceof Collection ? allToString((Collection) result) : ll(str(result)); if (debug) print("Simplifier returned: " + list); theLoop: for (String x : list) { if (nempty(x) && neq(x, in)) { final RuleEngine2_MatchedRule matched = new RuleEngine2_MatchedRule(); matched.rule = ((RuleEngine2.SimplifyWithRule) rule); matched.out = x; matched.inputsByType = inputsByType; for (Object massager : unnull(matched.rule.mapMassagers)) if (callF(massager, new HashMap(), null, null, matched) == null) { if (debug) print("mapMassager returned null: " + massager); continue theLoop; } l.add(matched); } } continue; } final RuleEngine2_MatchedRule matched = new RuleEngine2_MatchedRule(); matched.rule = rule; matched.inputsByType = inputsByType; matched.map = ciMap(); matched.tokenizationFunction = or(matchAny_firstGroup("tokenize with *", rule.comments), "javaTokNPunctuationWithBrackets"); matched.outTokenizer = matchAny_firstGroup("tokenize out with *", rule.comments); if (!ai_ruleEngine2_matchStep(matched, inputsByType, true, __)) continue; l.add(matched); } return l; } public static List ai_ruleEngine2_rulesForInput_5_allConditions(RuleEngine2 engine, Map inputsByType, Object... __) { List notDone = ai_ruleEngine2_rulesForInput_5(engine, inputsByType, __); List done = new ArrayList(); boolean debug = boolPar("debug", __); while (nempty(notDone)) { List l = notDone; notDone = new ArrayList(); for (RuleEngine2_MatchedRule mr : l) { if (!mr.moreConditions()) { if (debug) print("Done: " + mr.ruleID()); done.add(mr); } else { if (debug) print("Matching step " + mr.iCond + " in " + mr.ruleID()); if (ai_ruleEngine2_matchStep(mr, inputsByType, false)) { if (debug) print("Step succeeded"); notDone.add(mr); } else done.add(mr); } } } return done; } public static TreeMap caseInsensitiveMap() { return new TreeMap(caseInsensitiveComparator()); } public static boolean containsAny(Collection a, Collection b) { if (empty(a) || empty(b)) return false; Set set = asSet(b); for (A x : a) if (contains(set, x)) return true; return false; } public static boolean containsAny(Map a, Collection b) { if (empty(a)) return false; if (b != null) for (A x : b) if (a.containsKey(x)) return true; return false; } public static boolean isCurlyBraced(String s) { List tok = tok_combineCurlyBrackets_keep(javaTok(s)); return l(tok) == 3 && startsWithAndEndsWith(tok.get(1), "{", "}"); } public static int numberOfJavaTokens(String s) { return l(javaTokC(s)); } public static String curlyBrace(String s) { return "{" + s + "}"; } public static List sortByCalculatedFieldDesc_inPlace(List l, final Object f) { sort(l, new Comparator() { public int compare(A b, A a) { return stdcompare((Object) callF(f, a), (Object) callF(f, b)); } }); return l; } public static List sortByCalculatedFieldDesc_inPlace(Object f, List c) { return sortByCalculatedFieldDesc_inPlace(c, f); } public static String[] drop(int n, String[] a) { n = Math.min(n, a.length); String[] b = new String[a.length - n]; System.arraycopy(a, n, b, 0, b.length); return b; } public static Object[] drop(int n, Object[] a) { n = Math.min(n, a.length); Object[] b = new Object[a.length - n]; System.arraycopy(a, n, b, 0, b.length); return b; } public static ArrayList toList(A[] a) { return asList(a); } public static ArrayList toList(int[] a) { return asList(a); } public static ArrayList toList(Set s) { return asList(s); } public static ArrayList toList(Iterable s) { return asList(s); } public static String dm_gazelle_fulfillmentsCRUD() { return dm_require("#1022289/StatementFulfillments"); } public static TreeMap asCaseInsensitiveMap(Map map) { if (isCIMap(map)) return (TreeMap) map; TreeMap m = ciMap(); m.putAll(map); return m; } public static Map safeUnstructureMap(String s) { return (Map) safeUnstructure(s); } public static List codeTokensOnly(List tok) { int n = tok.size(); List l = emptyList(n / 2); for (int i = 1; i < n; i += 2) l.add(tok.get(i)); return l; } public static boolean canPutAllStrictlyIC(Map map, Map map2) { { final Map __174 = unnull(map2); for (A key : keys(__174)) { String value = __174.get(key); if (!canPutStrictlyIC(map, key, value)) return false; } } return true; } public static Map mergeMaps(Map... maps) { Map map = null; for (Map m : maps) { if (map == null) map = similarEmptyMap(m); map.putAll(m); } return map; } public static String gazelle_rumCRUD() { return dm_require("#1022322/RecentlyUsedMappings"); } public static List ai_applyWordSplitPattern1(List tokP, List tokI) { List indices = indicesOfSubList(tokP, ll("", "{}", "")); if (empty(indices)) return tokI; List tokI_original = tokI; tokI = cloneList(tokI); for (int i : indices) { String l = get(tokP, i - 1), r = get(tokP, i + 3); if (startsWith(l, "$") && isIdentifier(l) && isIdentifier(r)) { String x = get(tokI, i - 1); if (!ewic(x, r)) return tokI_original; tokI.set(i - 1, dropLast(x, l(r))); tokI.add(i, ""); tokI.add(i + 1, "{}"); tokI.add(i + 2, ""); tokI.add(i + 3, takeLast(x, l(r))); } } return tokI; } public static Map zipCodeTokensToCIMap_strict_withoutEquals(List tok1, List tok2) { int n = l(tok1); if (n != l(tok2)) return null; Map map = ciMap(); for (int i = 1; i < n; i += 2) if (!strictPutICIfKeyNotEqualToValue(map, tok1.get(i), tok2.get(i))) return null; return map; } public static boolean gazelle_addMatch(RuleEngine2_MatchedRule mr, Map map, List tokC, List tokI, Object... __) { boolean debug = boolPar("debug", __); for (Object massager : unnull(mr.rule.mapMassagers)) { boolean wasNull = map == null; map = (Map) callF(massager, map, tokC, tokI, mr); if (debug && map == null && !wasNull) print("mapMassager returned null: " + massager); } if (map == null) return false; if (!addMapToMapWithoutOverwritingIC(mr.map, map)) { if (debug) print("Adding map failed"); return false; } mr.flush(); return true; } public static String strWithIdentityHashCode(Object o) { return o == null ? "null" : "[" + identityHashCode(o) + "] " + str(o); } public static Pair pair(A a, B b) { return new Pair(a, b); } public static Pair pair(A a) { return new Pair(a, a); } public static String nullIfEmpty(String s) { return isEmpty(s) ? null : s; } public static Map nullIfEmpty(Map map) { return isEmpty(map) ? null : map; } public static List nullIfEmpty(List l) { return isEmpty(l) ? null : l; } public static Map, Integer> ai_ruleEngine2_mappingQualities(RuleEngine2.Rule rule) { Map, Integer> map = new HashMap(); Matches m = new Matches(); for (String s : rule.comments) if (match("changing * to * is *", s, m)) { int value = eqic(m.unq(0), "ok") ? 1 : eqic(m.unq(0), "bad") ? -1 : 0; map.put(pair(m.unq(1), m.unq(2)), value); } return map; } public static boolean startsWithEndsWith(String s, String prefix, String suffix) { return startsWith(s, prefix) && endsWith(s, suffix); } public static List wrapArrayAsList(A[] a) { return a == null ? null : Arrays.asList(a); } public static int toInt(Object o) { if (o == null) return 0; if (o instanceof Number) return ((Number) o).intValue(); if (o instanceof String) return parseInt((String) o); throw fail("woot not int: " + getClassName(o)); } public static int toInt(long l) { if (l != (int) l) throw fail("Too large for int: " + l); return (int) l; } public static CloseableIterableIterator emptyCloseableIterableIterator_instance = new CloseableIterableIterator() { public Object next() { throw fail(); } public boolean hasNext() { return false; } }; public static CloseableIterableIterator emptyCloseableIterableIterator() { return emptyCloseableIterableIterator_instance; } public static boolean ewic(String a, String b) { return endsWithIgnoreCase(a, b); } public static boolean ewic(String a, String b, Matches m) { return endsWithIgnoreCase(a, b, m); } public static CloseableIterableIterator linesFromReader(Reader r) { final BufferedReader br = bufferedReader(r); return iteratorFromFunction_f0_autoCloseable(new F0() { public String get() { try { return readLineFromReaderWithClose(br); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ret readLineFromReaderWithClose(br);"; } }, _wrapIOCloseable(r)); } public static BufferedReader utf8bufferedReader(InputStream in) { try { return bufferedReader(_registerIOWrap(new InputStreamReader(in, "UTF-8"), in)); } catch (Exception __e) { throw rethrow(__e); } } public static BufferedReader utf8bufferedReader(File f) { try { return utf8bufferedReader(newFileInputStream(f)); } catch (Exception __e) { throw rethrow(__e); } } public static GZIPInputStream newGZIPInputStream(File f) { return gzInputStream(f); } public static GZIPInputStream newGZIPInputStream(InputStream in) { return gzInputStream(in); } public static A[] newObjectArrayOfSameType(A[] a, int n) { return (A[]) Array.newInstance(a.getClass().getComponentType(), n); } public static boolean isAnonymousClassName(String s) { for (int i = 0; i < l(s); i++) if (s.charAt(i) == '$' && Character.isDigit(s.charAt(i + 1))) return true; return false; } public static Class getClass_vmName(Object realm, String name) { try { try { return getClass(realm).getClassLoader().loadClass(name); } catch (ClassNotFoundException e) { return null; } } catch (Exception __e) { throw rethrow(__e); } } public static Map nuObjectWithoutArguments_cache = newDangerousWeakHashMap(); public static Object nuObjectWithoutArguments(String className) { try { return nuObjectWithoutArguments(classForName(className)); } catch (Exception __e) { throw rethrow(__e); } } public static A nuObjectWithoutArguments(Class c) { try { if (nuObjectWithoutArguments_cache == null) return (A) nuObjectWithoutArguments_findConstructor(c).newInstance(); Constructor m = nuObjectWithoutArguments_cache.get(c); if (m == null) nuObjectWithoutArguments_cache.put(c, m = nuObjectWithoutArguments_findConstructor(c)); return (A) m.newInstance(); } catch (Exception __e) { throw rethrow(__e); } } public static Constructor nuObjectWithoutArguments_findConstructor(Class c) { for (Constructor m : c.getDeclaredConstructors()) if (empty(m.getParameterTypes())) { m.setAccessible(true); return m; } throw fail("No default constructor found in " + c.getName()); } public static char lastChar(String s) { return empty(s) ? '\0' : s.charAt(l(s) - 1); } public static String[] dropLast(String[] a, int n) { n = Math.min(n, a.length); String[] b = new String[a.length - n]; System.arraycopy(a, 0, b, 0, b.length); return b; } public static List dropLast(List l) { return subList(l, 0, l(l) - 1); } public static List dropLast(int n, List l) { return subList(l, 0, l(l) - n); } public static List dropLast(Iterable l) { return dropLast(asList(l)); } public static String dropLast(String s) { return substring(s, 0, l(s) - 1); } public static String dropLast(String s, int n) { return substring(s, 0, l(s) - n); } public static String dropLast(int n, String s) { return dropLast(s, n); } public static Map> javaTokNPunctuationWithBrackets_cache = synchronizedMRUCache(1000); public static List javaTokNPunctuationWithBrackets(String s) { List tok = javaTokNPunctuationWithBrackets_cache.get(s); if (tok == null) javaTokNPunctuationWithBrackets_cache.put(s, tok = javaTokNPunctuationWithBrackets_uncached(s)); return tok; } public static List javaTokNPunctuationWithBrackets_uncached(String s) { return tok_combineRoundOrCurlyBrackets_keep(javaTokNPunctuation_bracketsInC(s)); } public static int indexOfSubList(List x, List y) { return indexOfSubList(x, y, 0); } public static int indexOfSubList(List x, List y, int i) { outer: for (; i + l(y) <= l(x); i++) { for (int j = 0; j < l(y); j++) if (neq(x.get(i + j), y.get(j))) continue outer; return i; } return -1; } public static int indexOfSubList(List x, A[] y, int i) { outer: for (; i + l(y) <= l(x); i++) { for (int j = 0; j < l(y); j++) if (neq(x.get(i + j), y[j])) continue outer; return i; } return -1; } public static List javaTokPlusBrackets(String s) { return tok_combineRoundOrCurlyBrackets_keep(javaTok(s)); } public static volatile PersistableThrowable lastException_lastException; public static PersistableThrowable lastException() { return lastException_lastException; } public static void lastException(Throwable e) { lastException_lastException = persistableThrowable(e); } public static Object pcallFunction(Object f, Object... args) { try { return callFunction(f, args); } catch (Throwable __e) { _handleException(__e); } return null; } public static Map synchroMap() { return synchroHashMap(); } public static Map synchroMap(Map map) { return Collections.synchronizedMap(map); } public static ArrayList litlist(A... a) { ArrayList l = new ArrayList(a.length); for (A x : a) l.add(x); return l; } public 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; removeSubList(l, i, i + l(x)); l.addAll(i, y); i += l(y); } return l; } public static List replaceSublist(List l, int fromIndex, int toIndex, List y) { removeSubList(l, fromIndex, toIndex); l.addAll(fromIndex, y); return l; } public static boolean isNormalQuoted(String s) { int l = l(s); if (!(l >= 2 && s.charAt(0) == '"' && lastChar(s) == '"')) return false; int j = 1; while (j < l) if (s.charAt(j) == '"') return j == l - 1; else if (s.charAt(j) == '\\' && j + 1 < l) j += 2; else ++j; return false; } public static boolean isMultilineQuoted(String s) { if (!startsWith(s, "[")) return false; int i = 1; while (i < s.length() && s.charAt(i) == '=') ++i; return i < s.length() && s.charAt(i) == '['; } public static boolean checkCondition(Object condition, Object... args) { return isTrue(callF(condition, args)); } public static void copyListPart(List a, int i1, List b, int i2, int n) { if (a == null || b == null) return; for (int i = 0; i < n; i++) b.set(i2 + i, a.get(i1 + i)); } public static Map codeAnalysis_identifierSafetyMap() { return mechHashMap("JavaX Identifier Safety"); } public static Set tok_allIdentifiers(String s) { return new HashSet(filter("isIdentifier", javaTokC(s))); } public static boolean codeAnalysis_isSafeIdentifier(String id) { return startsWith(id, "$"); } public static String or2(String a, String b) { return nempty(a) ? a : b; } public static String or2(String a, String b, String c) { return or2(or2(a, b), c); } public static List tokSplitAtComma(String s) { List tok = javaTok(s); List l = new ArrayList(); while (l(tok) > 1) { int i = smartIndexOf(tok, ","); l.add(trim(join(subList(tok, 0, i)))); tok = subList(tok, i + 1); } return l; } public static Collection simplifySafetyTags(Collection tags) { List l = uniquifyList(tags); if (l.contains("safe") && containsAnythingNeq(l, "safe")) return listMinus(l, "safe"); return l; } public static String simplifySafetyTags(String tags) { List l = tokSplitAtComma(tags); Collection l2 = simplifySafetyTags(l); return l(l2) != l(l) ? joinWithComma(l2) : tags; } public static Number parseIntOrLong(String s) { long l = parseLong(s); return longIsInt(l) ? (Number) (int) l : (Number) l; } public static boolean isLongConstant(String s) { if (!s.endsWith("L")) return false; s = s.substring(0, l(s) - 1); return isInteger(s); } public static Object getMC(String field) { return get(mc(), field); } public static RuntimeException quickFail() { throw new QuickException("fail"); } public static RuntimeException quickFail(Throwable e) { throw asQuickException(e); } public static RuntimeException quickFail(Object msg) { throw new QuickException(String.valueOf(msg)); } public static RuntimeException quickFail(String msg) { throw new QuickException(unnull(msg)); } public static RuntimeException quickFail(String msg, Throwable innerException) { throw new QuickException(msg, innerException); } public static Map getBracketMap(List tok) { return getBracketMap(tok, getBracketMap_opening, getBracketMap_closing); } public static Map getBracketMap(List tok, Collection opening, Collection closing) { return getBracketMap(tok, opening, closing, 0, l(tok)); } public static Map getBracketMap(List tok, Collection opening, Collection closing, int from, int to) { TreeMap map = new TreeMap(); List stack = new ArrayList(); for (int i = from | 1; i < to; i += 2) { if (opening.contains(tok.get(i))) stack.add(i); else if (closing.contains(tok.get(i))) { if (!empty(stack)) map.put(liftLast(stack), i); } } return map; } public static Set getBracketMap_opening = lithashset("{", "("); public static Set getBracketMap_closing = lithashset("}", ")"); public static boolean eqGetOneOf(List l, int i, A... options) { return eqOneOf(get(l, i), options); } public static String trimJoinSubList(List l, int i, int j) { return trim(join(subList(l, i, j))); } public static String trimJoinSubList(List l, int i) { return trim(join(subList(l, i))); } public static void setCreatorTo(Object o, Object creator) { setOpt(o, "creator_class", newWeakReference(creator)); } public static AutoCloseable tempSetThreadLocal(final ThreadLocal tl, A a) { if (tl == null) return null; final A prev = setThreadLocal(tl, a); return new AutoCloseable() { public String toString() { return "tl.set(prev);"; } public void close() throws Exception { tl.set(prev); } }; } public static WeakReference newWeakReference(A a) { return a == null ? null : new WeakReference(a); } public static Object callAndMake_orDirect(String functionName, List args) { return callAndMake_orDirect(functionName, toObjectArray(args)); } public static Object callAndMake_orDirect(String functionName, Object... args) { if (functionName == null) return null; if (hasMethod(mc(), functionName, args)) return callMC(functionName, args); else return makeAndCall_cached(functionName, args); } public static Object dm_evalJava_veryQuickJava; public static Object dm_evalJava_withModule(String expression, Object... args) { Object m = dm_current_generic(); String code = evalJava_prep(expression) + "\r\n sO dm_current_mandatory_generic_value;\r\n \r\n sO dm_current_mandatory_generic() { ret dm_current_mandatory_generic_value; }\r\n sO dm_current_generic() { ret dm_current_mandatory_generic(); }\r\n "; Object obj; if (dm_evalJava_veryQuickJava != null) obj = callF(dm_evalJava_veryQuickJava, code); else obj = veryQuickJava(code); setOpt(obj, "programID", programID()); makeDependent(obj); setOpt(obj, "creator_class", newWeakReference(dm_os())); set(obj, "dm_current_mandatory_generic_value", m); return evalJava_myProgramID_main_2(obj, args); } public static T3 t3(A a, B b, C c) { return new T3(a, b, c); } public static List reverseInPlace(List l) { return reverseList(l); } public static Object dm_requireAndCall(String moduleLibID, String method, Object... args) { return dm_requireAndCallModule(moduleLibID, method, args); } public static Object unstructureUnlessContainsAnonymousClasses(String s) { return structContainsAnonymousClasses(s) ? null : unstruct(s); } public static B getOrCreate_f1(Map map, A key, F1 create) { try { B b = map.get(key); if (b == null) mapPut(map, key, b = callF(create, key)); return b; } catch (Exception __e) { throw rethrow(__e); } } public static A nu(Class c, Object... values) { A a = nuObject(c); setAll(a, values); return a; } public static List ai_gazelle_statementsFromAppliedRule(RuleEngine2_MatchedRule r, String judgement) { if (r == null || r.rule == null) return new ArrayList(); if (nempty(judgement) && neq(judgement, "good")) judgement = "bad"; String ruleID = r.rule.globalID; String ruleIn = first(r.rule.in); if (ruleIn == null) return new ArrayList(); Map varMap = r.map; String tokenize = r.tokenizationFunction; List tok = (List) (callAndMake(tokenize, ruleIn)); List tokC = codeTokens(tok); Set possibleVars = asCISet(tokC); List statements = new ArrayList(); for (String a : keys(varMap)) { String b = varMap.get(a); statements.add(format("in a * mapping, variable " + quote(a) + " was used", judgement)); statements.add(format("in a * mapping, variable " + quote(a) + " was mapped to " + quote(b), judgement)); } for (String var : possibleVars) statements.add("possible variable " + quote(var)); statements.add(format("a * mapping is *", judgement, sfu(varMap))); return statements; } public static Collection values(Map map) { return map == null ? emptyList() : map.values(); } public static Collection values(MultiMap mm) { return mm == null ? emptyList() : concatLists(values(mm.data)); } public static boolean matchStartX(String pat, String s, Matches matches) { return matchStart(dropSuffix("...", pat), s, matches); } public static MultiSet getOrCreateMultiSet(Map> map, A key) { MultiSet b = map.get(key); if (b == null) map.put(key, b = new MultiSet()); return b; } public static class ai_gazelle_analyzeStatementsForRule_Data { public MultiSet statements; public Set possibleVars, usedVars, unusedVars, varsUsedInGood, varsUsedInBad, varsUsedInGoodOnly, varsUsedInBadOnly; public Set unclearVars; public MultiMap goodMappings, badMappings, alwaysGoodMappings, alwaysBadMappings; public List> completeGoodMappings, completeBadMappings; public Map, PosNegScore> varScores; } public static ai_gazelle_analyzeStatementsForRule_Data ai_gazelle_analyzeStatementsForRule(Collection l) { Set possibleVars = asCISet(matchAny_firstGroups("possible variable *", l)); Set usedVars = asCISet(matchX_any_firstGroups("... variable * was used", l)); Set unusedVars = asCISet(listMinusSet(possibleVars, usedVars)); Set varsUsedInGood = asCISet(matchX_any_firstGroups("in a good mapping, variable * was used", l)); Set varsUsedInBad = asCISet(matchX_any_firstGroups("in a bad mapping, variable * was used", l)); Set unclearVars = setIntersection(varsUsedInGood, varsUsedInBad); MultiMap goodMappings = uniquifyMultiMapValues(pairsToMultiMap(matchX_any_firstAndSecondGroups("in a good mapping, variable * was mapped to *", l))); MultiMap badMappings = uniquifyMultiMapValues(pairsToMultiMap(matchX_any_firstAndSecondGroups("in a bad mapping, variable * was mapped to *", l))); MultiMap alwaysGoodMappings = multiMapDiff(goodMappings, badMappings); MultiMap alwaysBadMappings = multiMapDiff(badMappings, goodMappings); List> completeGoodMappings = map("safeUnstruct", matchX_any_firstGroups("a good mapping is *", l)); List> completeBadMappings = map("safeUnstruct", matchX_any_firstGroups("a bad mapping is *", l)); return nu(ai_gazelle_analyzeStatementsForRule_Data.class, "possibleVars", possibleVars, "usedVars", usedVars, "unusedVars", unusedVars, "varsUsedInGood", varsUsedInGood, "varsUsedInBad", varsUsedInBad, "unclearVars", unclearVars, "varsUsedInGoodOnly", setDiff(varsUsedInGood, varsUsedInBad), "varsUsedInBadOnly", setDiff(varsUsedInBad, varsUsedInGood), "goodMappings", goodMappings, "badMappings", badMappings, "alwaysGoodMappings", alwaysGoodMappings, "alwaysBadMappings", alwaysBadMappings, "completeGoodMappings", completeGoodMappings, "completeBadMappings", completeBadMappings); } public static PosNegScore getOrCreatePosNegScore(Map map, A key) { return getOrCreate((Map) map, key, PosNegScore.class); } public static String ai_gazelle_predictor1_onData(RuleEngine2_MatchedRule r, ai_gazelle_analyzeStatementsForRule_Data data, Object... __) { boolean debug = boolPar("debug", __); String prediction = containsAny(keys(r.map), data.varsUsedInBadOnly) ? "bad" : "good"; if (debug) print("predictor1: " + keys(r.map) + " => " + prediction); return prediction; } public static ThreadLocal saveTiming_last = new ThreadLocal(); public static void saveTiming(long ms) { print(ms + " ms"); saveTiming_noPrint(ms); } public static void saveTiming_noPrint(long ms) { saveTiming_last.set(ms); } public static Object dm_requireAndCallModule(String moduleLibID, String method, Object... args) { return dm_callModule(dm_requireModule(moduleLibID), method, args); } public static int indexOfIgnoreCase(List a, String b) { return indexOfIgnoreCase(a, b, 0); } public static int indexOfIgnoreCase(List a, String b, int i) { int n = l(a); for (; i < n; i++) if (eqic(a.get(i), b)) return i; return -1; } public static int indexOfIgnoreCase(String a, String b) { return indexOfIgnoreCase_manual(a, b); } public static List mechList_opt(String name) { return mechList_opt_tlft(name); } public static List splitAtDoubleArrow(String s) { return splitAtDoubleArrow(javaTok(s)); } public static List splitAtDoubleArrow(List tok) { List l = new ArrayList(); int i = 0; while (i < l(tok)) { int j = indexOfSubList(tok, ll("=", "", ">"), i); if (i >= l(tok)) break; if (j < 0) j = l(tok); l.add(trimJoin(tok.subList(i, j))); i = j + 3; } return l; } public static List tok_splitAtComma(String s) { return tok_splitAtComma(javaTok(s)); } public static List tok_splitAtComma(List tok) { List out = new ArrayList(); for (int i = 0; i < l(tok); i++) { int j = smartIndexOf(tok, ",", i); out.add(joinSubList(tok, i + 1, j - 1)); i = j; } return out; } public static String optionalCurlyBrace(String s) { return isCurlyBraced(s) ? s : curlyBrace(s); } public static String tok_unCurlyBracket(String s) { return isCurlyBraced(s) ? join(dropFirstThreeAndLastThree(javaTok(s))) : s; } public static boolean jmatch_any(String pat, Iterable l) { return jmatch_any(pat, l, null); } public static boolean jmatch_any(String pat, Iterable l, Matches m) { for (String s : unnull(l)) if (jmatch(pat, s, m)) return true; return false; } public static boolean jmatch_any(List patterns, Iterable l, Matches m) { if (nempty(patterns)) for (String s : unnull(l)) for (String pat : patterns) if (jmatch(pat, s, m)) return true; return false; } public static double toDouble(Object o) { if (o instanceof Number) return ((Number) o).doubleValue(); if (o instanceof BigInteger) return ((BigInteger) o).doubleValue(); if (o == null) return 0.0; throw fail(o); } public static Object[] litObjectArray(Object... l) { return l; } public static List allToString(Collection c) { List l = new ArrayList(); for (Object o : unnull(c)) l.add(str(o)); return l; } public static List allToString(Object[] c) { List l = new ArrayList(); for (Object o : unnull(c)) l.add(str(o)); return l; } public static List tok_combineCurlyBrackets_keep(List tok) { List l = new ArrayList(); for (int i = 0; i < l(tok); i++) { String t = tok.get(i); if (odd(i) && eq(t, "{")) { int j = findEndOfCurlyBracketPart(tok, i); l.add(joinSubList(tok, i, j)); i = j - 1; } else l.add(t); } return l; } public static boolean startsWithAndEndsWith(String s, String prefix, String suffix) { return startsWith(s, prefix) && endsWith(s, suffix); } public 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; 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); if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { if (s.charAt(j) == opener || s.charAt(j) == '\n') { ++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)); else if (Character.isDigit(c)) { do ++j; while (j < l && Character.isDigit(s.charAt(j))); if (j < l && s.charAt(j) == 'L') ++j; } 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; } public static int stdcompare(Number a, Number b) { return cmp(a, b); } public static int stdcompare(String a, String b) { return cmp(a, b); } public static int stdcompare(long a, long b) { return a < b ? -1 : a > b ? 1 : 0; } public static int stdcompare(Object a, Object b) { return cmp(a, b); } public static boolean isCIMap(Map m) { return m instanceof TreeMap && ((TreeMap) m).comparator() == caseInsensitiveComparator(); } public static Object safeUnstructure(String s) { return unstructure(s, true); } public static boolean canPutStrictlyIC(Map map, A key, String value) { if (!(map != null && key != null && value != null)) return true; String old = map.get(key); return old == null || eqic(old, value); } public static Map similarEmptyMap(Map m) { if (m instanceof TreeMap) return new TreeMap(((TreeMap) m).comparator()); if (m instanceof LinkedHashMap) return new LinkedHashMap(); return new HashMap(); } public static List indicesOfSubList(List x, List y) { List l = new ArrayList(); int i = 0; while (true) { int j = indexOfSubList(x, y, i); if (j < 0) break; l.add(j); i = j + 1; } return l; } public static boolean strictPutICIfKeyNotEqualToValue(Map map, String key, String value) { if (eqic(key, value)) return true; return strictPutIC(map, key, value); } public static boolean addMapToMapWithoutOverwritingIC(Map map, Map adding) { if (!canPutAllStrictlyIC(map, adding)) return false; { final Map __220 = unnull(adding); for (A key : keys(__220)) { String value = __220.get(key); strictPutIC(map, key, value); } } return true; } public static int identityHashCode(Object o) { return System.identityHashCode(o); } public static boolean isEmpty(Collection c) { return c == null || c.isEmpty(); } public static boolean isEmpty(CharSequence s) { return s == null || s.length() == 0; } public static boolean isEmpty(Object[] a) { return a == null || a.length == 0; } public static boolean isEmpty(byte[] a) { return a == null || a.length == 0; } public static boolean isEmpty(Map map) { return map == null || map.isEmpty(); } public static boolean endsWithIgnoreCase(String a, String b) { int la = l(a), lb = l(b); return la >= lb && regionMatchesIC(a, la - lb, b, 0, lb); } public static boolean endsWithIgnoreCase(String a, String b, Matches m) { if (!endsWithIgnoreCase(a, b)) return false; m.m = new String[] { substring(a, 0, l(a) - l(b)) }; return true; } public static BufferedReader bufferedReader(Reader r) { return r instanceof BufferedReader ? (BufferedReader) r : _registerIOWrap(new BufferedReader(r), r); } public static CloseableIterableIterator iteratorFromFunction_f0_autoCloseable(final F0 f, final AutoCloseable closeable) { class IFF2 extends CloseableIterableIterator { public A a; public boolean done; public boolean hasNext() { getNext(); return !done; } public A next() { getNext(); if (done) throw fail(); A _a = a; a = null; return _a; } public void getNext() { if (done || a != null) return; a = f.get(); done = a == null; } public void close() throws Exception { if (closeable != null) closeable.close(); } } ; return new IFF2(); } public static String readLineFromReaderWithClose(BufferedReader r) { try { String s = r.readLine(); if (s == null) r.close(); return s; } catch (Exception __e) { throw rethrow(__e); } } public static AutoCloseable _wrapIOCloseable(final AutoCloseable c) { return c == null ? null : new AutoCloseable() { public String toString() { return "c.close();\r\n _registerIO(c, null, false);"; } public void close() throws Exception { c.close(); _registerIO(c, null, false); } }; } public static A _registerIOWrap(A wrapper, Object wrapped) { return wrapper; } public static FileInputStream newFileInputStream(File path) throws IOException { return newFileInputStream(path.getPath()); } public static FileInputStream newFileInputStream(String path) throws IOException { FileInputStream f = new FileInputStream(path); _registerIO(f, path, true); return f; } public static int gzInputStream_defaultBufferSize = 65536; public static GZIPInputStream gzInputStream(File f) { try { return gzInputStream(new FileInputStream(f)); } catch (Exception __e) { throw rethrow(__e); } } public static GZIPInputStream gzInputStream(File f, int bufferSize) { try { return gzInputStream(new FileInputStream(f), bufferSize); } catch (Exception __e) { throw rethrow(__e); } } public static GZIPInputStream gzInputStream(InputStream in) { return gzInputStream(in, gzInputStream_defaultBufferSize); } public static GZIPInputStream gzInputStream(InputStream in, int bufferSize) { try { return _registerIOWrap(new GZIPInputStream(in, gzInputStream_defaultBufferSize), in); } catch (Exception __e) { throw rethrow(__e); } } public static Class getClass(String name) { try { return Class.forName(name); } catch (ClassNotFoundException e) { return null; } } public static Class getClass(Object o) { return o instanceof Class ? (Class) o : o.getClass(); } public static Class getClass(Object realm, String name) { try { try { return getClass(realm).getClassLoader().loadClass(classNameToVM(name)); } catch (ClassNotFoundException e) { return null; } } catch (Exception __e) { throw rethrow(__e); } } public static Map classForName_cache = synchroHashMap(); public static Class classForName(String name) { try { Class c = classForName_cache.get(name); if (c == null) classForName_cache.put(name, c = Class.forName(name)); return c; } catch (Exception __e) { throw rethrow(__e); } } public static List javaTokNoQuotes(String s) { return quotelessJavaTok(s); } public static List javaTokNoQuotesWithBrackets(String s) { return tok_combineRoundOrCurlyBrackets_keep(javaTokNoQuotes(s)); } public static List tok_combineRoundOrCurlyBrackets_keep(List tok) { List l = new ArrayList(); for (int i = 0; i < l(tok); i++) { String t = tok.get(i); if (odd(i) && eqOneOf(t, "{", "(")) { int j = findEndOfBracketPart(tok, i); l.add(joinSubList(tok, i, j)); i = j - 1; } else l.add(t); } return l; } public static List javaTokNPunctuation_bracketsInC(String s) { List tok = javaTok(s); for (int i = 1; i < l(tok); i += 2) { int j = i; String t; char c; while (j < l(tok) && l(t = tok.get(j)) == 1 && !Character.isLetterOrDigit(c = t.charAt(0)) && "{}()".indexOf(c) < 0) j += 2; if (j > i) replaceSubList(tok, i - 1, j, ll(joinSubList(tok, i - 1, j))); } return tok; } public static Object callFunction(Object f, Object... args) { return callF(f, args); } public static Map synchroHashMap() { return Collections.synchronizedMap(new HashMap()); } public static void removeSubList(List l, int from, int to) { if (l != null) subList(l, from, to).clear(); } public static void removeSubList(List l, int from) { if (l != null) subList(l, from).clear(); } public static Map>> mechHashMap_cache = synchroMap(); public static Map mechHashMap(String listName) { Pair> p = mechHashMap_cache.get(listName); String text = mechList_raw(listName); if (p != null && eq(p.a, text)) return p.b; String parsing = first(mechList_opt(listName + " | Parsing")); p = pair(text, (Map) (parsing != null ? asHashMap((Map) callAndMake(parsing, text)) : parseDoubleArrowMap(new HashMap(), tlft(text)))); mechHashMap_cache.put(listName, p); return p.b; } public static int smartIndexOf(String s, String sub, int i) { if (s == null) return 0; i = s.indexOf(sub, min(i, l(s))); return i >= 0 ? i : l(s); } public static int smartIndexOf(String s, int i, char c) { return smartIndexOf(s, c, i); } public static int smartIndexOf(String s, char c, int i) { if (s == null) return 0; i = s.indexOf(c, min(i, l(s))); return i >= 0 ? i : l(s); } public static int smartIndexOf(String s, String sub) { return smartIndexOf(s, sub, 0); } public static int smartIndexOf(String s, char c) { return smartIndexOf(s, c, 0); } public static int smartIndexOf(List l, A sub) { return smartIndexOf(l, sub, 0); } public static int smartIndexOf(List l, int start, A sub) { return smartIndexOf(l, sub, start); } public static int smartIndexOf(List l, A sub, int start) { int i = indexOf(l, sub, start); return i < 0 ? l(l) : i; } public static List uniquifyList(Collection l) { if (l == null) return null; if (l(l) < 2) return asList(l); HashSet set = new HashSet(); List out = new ArrayList(); for (A a : l) if (set.add(a)) out.add(a); return out; } public static boolean containsAnythingNeq(Collection l, A a) { for (A x : unnull(l)) if (neq(x, a)) return true; return false; } public static List listMinus(Collection l, Object... stuff) { if (l == null) return null; List l2 = similarEmptyList(l); Set set = lithashset(stuff); for (Object o : l) if (!set.contains(o)) l2.add(o); return l2; } public static boolean longIsInt(long l) { return l == (int) l; } public static RuntimeException asQuickException(Throwable t) { return t instanceof RuntimeException ? (RuntimeException) t : new QuickException(t); } public static A setThreadLocal(ThreadLocal tl, A value) { if (tl == null) return null; A old = tl.get(); tl.set(value); return old; } public static boolean hasMethod(Object o, String method, Object... args) { return findMethod_cached(o, method, args) != null; } public static ThreadLocal makeAndCall_initChild = new ThreadLocal(); public static Object makeAndCall_cached(String functionName, Object... args) { Class c = loadFunctions_cached(functionName); AutoCloseable closer = (AutoCloseable) (callF(makeAndCall_initChild.get(), c)); AutoCloseable __2016 = closer; try { return preciseCall(c, functionName, args); } finally { _close(__2016); } } public static String evalJava_prep(String code) { return evalJava_prep(code, "calc"); } public static String evalJava_prep(String code, String mainName) { return evalJava_prep(code, mainName, ""); } public static ThreadLocal evalJava_prep_args = new ThreadLocal(); public static ThreadLocal evalJava_prep_voidMagic = new ThreadLocal(); public static String evalJava_prep(String code, String mainName, String preCode) { List tok = javaTok(trim(code)); if (!isFalse(evalJava_prep_voidMagic.get()) && tok_shouldAddReturn(tok) && eqGet(tok, 3, "(") && isIdentifier(get(tok, 1)) && isKnownVoidFunction(get(tok, 1))) tokAppend_reTok(tok, l(tok) - 2, ";"); code = tok_addReturn(tok); String returnType = containsReturnWithArgument(code) ? "O" : "void"; String main = "static " + returnType + " " + mainName + "(" + unnull(evalJava_prep_args.get()) + ") throws Exception { " + preCode + code + "\n" + "}"; return main; } public static Class veryQuickJava(CharSequence mainJava) { return veryQuickJava3(str(mainJava)); } public static Object makeDependent_postProcess; public static void makeDependent(Object c) { if (c == null) return; assertTrue("Not a class", c instanceof Class); dependentClasses(); hotwire_classes.add(new WeakReference(c)); Object local_log = getOpt(mc(), "local_log"); if (local_log != null) setOpt(c, "local_log", local_log); Object print_byThread = getOpt(mc(), "print_byThread"); if (print_byThread != null) setOpt(c, "print_byThread", print_byThread); callF(makeDependent_postProcess, c); } public static void set(Object o, String field, Object value) { if (o == null) return; if (o instanceof Class) set((Class) o, field, value); else try { Field f = set_findField(o.getClass(), field); f.setAccessible(true); smartSet(f, o, value); } catch (Exception e) { throw new RuntimeException(e); } } public static void set(Class c, String field, Object value) { if (c == null) return; try { Field f = set_findStaticField(c, field); f.setAccessible(true); smartSet(f, null, value); } catch (Exception e) { throw new RuntimeException(e); } } public static Field set_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()); } public static Field set_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()); } public static ThreadLocal evalJava_myProgramID_main_time = new ThreadLocal(); public static Object evalJava_myProgramID_main(String main, Object... args) { Object obj = veryQuickJava(main); setOpt(obj, "programID", programID()); makeDependent(obj); return evalJava_myProgramID_main_2(obj, args); } public static Object evalJava_myProgramID_main_2(Object obj, Object... args) { long time = sysNow(); try { return callCalc(obj, args); } finally { evalJava_myProgramID_main_time.set(sysNow() - time); } } public static List reverseList(List l) { Collections.reverse(l); return l; } public static boolean structContainsAnonymousClasses(String s) { Pattern p = compileRegexp("\\$\\d"); Producer it = javaTokC_noMLS_iterator(s); String t; while ((t = it.next()) != null) if (isIdentifier(s) && regexpContains(p, t)) return true; return false; } public static Object unstruct(String text) { return unstructure(text); } public static Object nuObject(String className, Object... args) { try { return nuObject(classForName(className), args); } catch (Exception __e) { throw rethrow(__e); } } public static A nuObject(Class c, Object... args) { try { if (args.length == 0) return nuObjectWithoutArguments(c); Constructor m = nuObject_findConstructor(c, args); m.setAccessible(true); return (A) m.newInstance(args); } catch (Exception __e) { throw rethrow(__e); } } public static Constructor nuObject_findConstructor(Class c, Object... args) { for (Constructor m : c.getDeclaredConstructors()) { if (!nuObject_checkArgs(m.getParameterTypes(), args, false)) continue; return m; } throw fail("Constructor " + c.getName() + getClasses(args) + " not found" + (args.length == 0 && (c.getModifiers() & java.lang.reflect.Modifier.STATIC) == 0 ? " - hint: it's a non-static class!" : "")); } public static boolean nuObject_checkArgs(Class[] types, Object[] args, boolean debug) { if (types.length != args.length) { if (debug) System.out.println("Bad parameter length: " + args.length + " vs " + types.length); return false; } for (int i = 0; i < types.length; i++) if (!(args[i] == null || isInstanceX(types[i], args[i]))) { if (debug) System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]); return false; } return true; } public static void setAll(Object o, Map fields) { if (fields == null) return; for (String field : keys(fields)) set(o, field, fields.get(field)); } public static void setAll(Object o, Object... values) { failIfOddCount(values); for (int i = 0; i + 1 < l(values); i += 2) { String field = (String) values[i]; Object value = values[i + 1]; set(o, field, value); } } public static String format(String pat, Object... args) { return format3(pat, args); } public static boolean matchStart(String pat, String s) { return matchStart(pat, s, null); } public static boolean matchStart(String pat, String s, Matches matches) { if (s == null) return false; return matchStart(pat, parse3_cachedInput(s), matches); } public static boolean matchStart(String pat, List toks, Matches matches) { if (toks == null) return false; List tokpat = parse3_cachedPattern(pat); if (toks.size() < tokpat.size()) return false; String[] m = match2(tokpat, toks.subList(0, tokpat.size())); if (m == null) return false; if (matches != null) { matches.m = new String[m.length + 1]; arraycopy(m, matches.m); matches.m[m.length] = join(toks.subList(tokpat.size(), toks.size())); } return true; } public static List matchAny_firstGroups(String pat, Iterable l) { Matches m = new Matches(); List out = new ArrayList(); for (String s : unnull(l)) if (match(pat, s, m)) out.add(m.unq(0)); return out; } public static List matchX_any_firstGroups(String pat, Iterable l) { Matches m = new Matches(); List out = new ArrayList(); for (String s : unnull(l)) if (matchX(pat, s, m)) out.add(m.unq(0)); return out; } public static Set setIntersection(Set a, Collection b) { Set set = similarEmptySet(a); Set bSet = asSet(b); for (A x : a) if (bSet.contains(x)) set.add(x); return set; } public static MultiMap uniquifyMultiMapValues(MultiMap mm) { return mm == null ? null : new MultiMap(mapValues("uniquify", mm.data)); } public static MultiMap pairsToMultiMap(List> l) { MultiMap mm = new MultiMap(); for (Pair p : unnull(l)) mm.put(p.a, p.b); return mm; } public static List> matchX_any_firstAndSecondGroups(String pat, Iterable l) { Matches m = new Matches(); List> out = new ArrayList(); for (String s : unnull(l)) if (matchX(pat, s, m)) out.add(pair(m.unq(0), m.unq(1))); return out; } public static MultiMap multiMapDiff(MultiMap mm1, MultiMap mm2) { return pairsToMultiMap(listMinusSet(multiMapToPairs(mm1), multiMapToPairs(mm2))); } public static Object safeUnstruct(String s) { return unstructure(s, true); } public static Set setDiff(Set l, Collection stuff) { return setMinusSet(l, stuff); } public static B getOrCreate(Map map, A key, Class c) { try { B b = map.get(key); if (b == null) map.put(key, b = c.newInstance()); return b; } catch (Exception __e) { throw rethrow(__e); } } public static B getOrCreate(Map map, A key, Object f) { try { B b = map.get(key); if (b == null) map.put(key, b = (B) callF(f)); return b; } catch (Exception __e) { throw rethrow(__e); } } public static B getOrCreate(Class c, Map map, A key) { return getOrCreate(map, key, c); } public static Object dm_callModule(Object moduleOrID, String method, Object... args) { Object mod = dm_getModule(moduleOrID); if (mod == null) return null; AutoCloseable __1347 = dm_enter(mod); try { return call(mod, method, args); } finally { _close(__1347); } } public static int indexOfIgnoreCase_manual(String a, String b) { int la = l(a), lb = l(b); if (la < lb) return -1; int n = la - lb; loop: for (int i = 0; i <= n; i++) { for (int j = 0; j < lb; j++) { char c1 = a.charAt(i + j), c2 = b.charAt(j); if (!eqic(c1, c2)) continue loop; } return i; } return -1; } public static List mechList_opt_tlft(String name) { return mechList_opt_tlft_cached(name); } public static List dropFirstThreeAndLastThree(List l) { return dropFirstAndLast(3, l); } public static int findEndOfCurlyBracketPart(List cnc, int i) { int j = i + 2, level = 1; while (j < cnc.size()) { if (eq(cnc.get(j), "{")) ++level; else if (eq(cnc.get(j), "}")) --level; if (level == 0) return j + 1; ++j; } return cnc.size(); } public static int cmp(Number a, Number b) { return a == null ? b == null ? 0 : -1 : cmp(a.doubleValue(), b.doubleValue()); } public static int cmp(double a, double b) { return a < b ? -1 : a == b ? 0 : 1; } public static int cmp(Object a, Object b) { if (a == null) return b == null ? 0 : -1; if (b == null) return 1; return ((Comparable) a).compareTo(b); } public static Object unstructure(String text) { return unstructure(text, false); } public static Object unstructure(String text, final boolean allDynamic) { return unstructure(text, allDynamic, null); } public static int structure_internStringsLongerThan = 50; public static int unstructure_unquoteBufSize = 100; public static int unstructure_tokrefs; public abstract static class unstructure_Receiver { public abstract void set(Object o); } public static Object unstructure(String text, boolean allDynamic, Object classFinder) { if (text == null) return null; return unstructure_tok(javaTokC_noMLS_iterator(text), allDynamic, classFinder); } public static Object unstructure_reader(BufferedReader reader) { return unstructure_tok(javaTokC_noMLS_onReader(reader), false, null); } public static Object unstructure_tok(final Producer tok, final boolean allDynamic, final Object _classFinder) { final boolean debug = unstructure_debug; final class X { public int i = -1; public final Object classFinder = _classFinder != null ? _classFinder : _defaultClassFinder(); public HashMap refs = new HashMap(); public HashMap tokrefs = new HashMap(); public HashSet concepts = new HashSet(); public HashMap classesMap = new HashMap(); public List stack = new ArrayList(); public String curT; public char[] unquoteBuf = new char[unstructure_unquoteBufSize]; public String unquote(String s) { return unquoteUsingCharArray(s, unquoteBuf); } public String t() { return curT; } public String tpp() { String t = curT; consume(); return t; } public void parse(final unstructure_Receiver out) { String t = t(); int refID = 0; if (structure_isMarker(t, 0, l(t))) { refID = parseInt(t.substring(1)); consume(); } final int _refID = refID; final int tokIndex = i; parse_inner(refID, tokIndex, new unstructure_Receiver() { public void set(Object o) { if (_refID != 0) refs.put(_refID, o); if (o != null) tokrefs.put(tokIndex, o); out.set(o); } }); } public void parse_inner(int refID, int tokIndex, final unstructure_Receiver out) { String t = t(); Class c = classesMap.get(t); if (c == null) { if (t.startsWith("\"")) { String s = internIfLongerThan(unquote(tpp()), structure_internStringsLongerThan); out.set(s); return; } if (t.startsWith("'")) { out.set(unquoteCharacter(tpp())); return; } if (t.equals("bigint")) { out.set(parseBigInt()); return; } if (t.equals("d")) { out.set(parseDouble()); return; } if (t.equals("fl")) { out.set(parseFloat()); return; } if (t.equals("sh")) { consume(); t = tpp(); if (t.equals("-")) { t = tpp(); out.set((short) (-parseInt(t))); return; } out.set((short) parseInt(t)); return; } if (t.equals("-")) { consume(); t = tpp(); out.set(isLongConstant(t) ? (Object) (-parseLong(t)) : (Object) (-parseInt(t))); return; } if (isInteger(t) || isLongConstant(t)) { consume(); if (isLongConstant(t)) { out.set(parseLong(t)); return; } long l = parseLong(t); boolean isInt = l == (int) l; out.set(isInt ? (Object) Integer.valueOf((int) l) : (Object) Long.valueOf(l)); return; } if (t.equals("false") || t.equals("f")) { consume(); out.set(false); return; } if (t.equals("true") || t.equals("t")) { consume(); out.set(true); return; } if (t.equals("-")) { consume(); t = tpp(); out.set(isLongConstant(t) ? (Object) (-parseLong(t)) : (Object) (-parseInt(t))); return; } if (isInteger(t) || isLongConstant(t)) { consume(); if (isLongConstant(t)) { out.set(parseLong(t)); return; } long l = parseLong(t); boolean isInt = l == (int) l; out.set(isInt ? (Object) Integer.valueOf((int) l) : (Object) Long.valueOf(l)); return; } if (t.equals("File")) { consume(); File f = new File(unquote(tpp())); out.set(f); return; } if (t.startsWith("r") && isInteger(t.substring(1))) { consume(); int ref = Integer.parseInt(t.substring(1)); Object o = refs.get(ref); if (o == null) print("Warning: unsatisfied back reference " + ref); out.set(o); return; } if (t.startsWith("t") && isInteger(t.substring(1))) { consume(); int ref = Integer.parseInt(t.substring(1)); Object o = tokrefs.get(ref); if (o == null) print("Warning: unsatisfied token reference " + ref); out.set(o); return; } if (t.equals("hashset")) { parseHashSet(out); return; } if (t.equals("lhs")) { parseLinkedHashSet(out); return; } if (t.equals("treeset")) { parseTreeSet(out); return; } if (t.equals("ciset")) { parseCISet(out); return; } if (eqOneOf(t, "hashmap", "hm")) { consume(); parseMap(new HashMap(), out); return; } if (t.equals("lhm")) { consume(); parseMap(new LinkedHashMap(), out); return; } if (t.equals("tm")) { consume(); parseMap(new TreeMap(), out); return; } if (t.equals("cimap")) { consume(); parseMap(ciMap(), out); return; } if (t.equals("sync")) { consume(); if (t().equals("tm")) { consume(); { parseMap(synchronizedTreeMap(), out); return; } } if (t().equals("[")) { parseList(synchroList(), out); return; } { parseMap(synchronizedMap(), out); return; } } if (t.equals("{")) { parseMap(out); return; } if (t.equals("[")) { this.parseList(new ArrayList(), out); return; } if (t.equals("bitset")) { parseBitSet(out); return; } if (t.equals("array") || t.equals("intarray")) { parseArray(out); return; } if (t.equals("ba")) { consume(); String hex = unquote(tpp()); out.set(hexToBytes(hex)); return; } if (t.equals("boolarray")) { consume(); int n = parseInt(tpp()); String hex = unquote(tpp()); out.set(boolArrayFromBytes(hexToBytes(hex), n)); return; } if (t.equals("class")) { out.set(parseClass()); return; } if (t.equals("l")) { parseLisp(out); return; } if (t.equals("null")) { consume(); out.set(null); return; } if (eq(t, "c")) { consume("c"); t = t(); assertTrue(isJavaIdentifier(t)); concepts.add(t); } } if (eq(t, "j")) { consume("j"); out.set(parseJava()); return; } if (c == null && !isJavaIdentifier(t)) throw new RuntimeException("Unknown token " + (i + 1) + ": " + t); consume(); String className, fullClassName; if (eq(t(), ".")) { consume(); className = fullClassName = t + "." + assertIdentifier(tpp()); } else { className = t; fullClassName = "main$" + t; } if (c == null) { if (allDynamic) c = null; else c = classFinder != null ? (Class) callF(classFinder, fullClassName) : findClass_fullName(fullClassName); if (c != null) classesMap.put(className, c); } boolean hasBracket = eq(t(), "("); if (hasBracket) consume(); boolean hasOuter = hasBracket && eq(t(), "this$1"); DynamicObject dO = null; Object o = null; final String thingName = t; if (c != null) { o = hasOuter ? nuStubInnerObject(c, classFinder) : nuEmptyObject(c); if (o instanceof DynamicObject) dO = (DynamicObject) o; } else { if (concepts.contains(t) && (c = findClass("Concept")) != null) o = dO = (DynamicObject) nuEmptyObject(c); else dO = new DynamicObject(); dO.className = className; } if (refID != 0) refs.put(refID, o != null ? o : dO); tokrefs.put(tokIndex, o != null ? o : dO); final LinkedHashMap fields = new LinkedHashMap(); final Object _o = o; final DynamicObject _dO = dO; if (hasBracket) { stack.add(new Runnable() { public void run() { try { if (eq(t(), ")")) { consume(")"); objRead(_o, _dO, fields); out.set(_o != null ? _o : _dO); } else { final String key = unquote(tpp()); if (!eq(tpp(), "=")) throw fail("= expected, got " + t() + " after " + quote(key) + " in object " + thingName); stack.add(this); parse(new unstructure_Receiver() { public void set(Object value) { fields.put(key, value); if (eq(t(), ",")) consume(); } }); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (eq(t(), \")\")) {\r\n consume(\")\");\r\n objRead(_o, _dO, ..."; } }); } else { objRead(o, dO, fields); out.set(o != null ? o : dO); } } public void objRead(Object o, DynamicObject dO, Map fields) { if (o != null) if (dO != null) { setOptAllDyn(dO, fields); } else { setOptAll_pcall(o, fields); } else for (String field : keys(fields)) dO.fieldValues.put(intern(field), fields.get(field)); if (o != null) pcallOpt_noArgs(o, "_doneLoading"); } public void parseSet(final Set set, final unstructure_Receiver out) { this.parseList(new ArrayList(), new unstructure_Receiver() { public void set(Object o) { set.addAll((List) o); out.set(set); } }); } public void parseLisp(final unstructure_Receiver out) { throw fail("class Lisp not included"); } public void parseBitSet(final unstructure_Receiver out) { consume("bitset"); consume("{"); final BitSet bs = new BitSet(); stack.add(new Runnable() { public void run() { try { if (eq(t(), "}")) { consume("}"); out.set(bs); } else { stack.add(this); parse(new unstructure_Receiver() { public void set(Object o) { bs.set((Integer) o); if (eq(t(), ",")) consume(); } }); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (eq(t(), \"}\")) {\r\n consume(\"}\");\r\n out.set(bs);\r\n ..."; } }); } public void parseList(final List list, final unstructure_Receiver out) { consume("["); stack.add(new Runnable() { public void run() { try { if (eq(t(), "]")) { consume("]"); out.set(list); } else { stack.add(this); parse(new unstructure_Receiver() { public void set(Object o) { list.add(o); if (eq(t(), ",")) consume(); } }); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (eq(t(), \"]\")) {\r\n consume(\"]\");\r\n out.set(list);\r\n ..."; } }); } public void parseArray(final unstructure_Receiver out) { final String type = tpp(); consume("{"); final List list = new ArrayList(); stack.add(new Runnable() { public void run() { try { if (eq(t(), "}")) { consume("}"); out.set(type.equals("intarray") ? toIntArray(list) : list.toArray()); } else { stack.add(this); parse(new unstructure_Receiver() { public void set(Object o) { list.add(o); if (eq(t(), ",")) consume(); } }); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (eq(t(), \"}\")) {\r\n consume(\"}\");\r\n out.set(type.equals(\"..."; } }); } public Object parseClass() { consume("class"); consume("("); String name = unquote(tpp()); consume(")"); Class c = allDynamic ? null : classFinder != null ? (Class) callF(classFinder, name) : findClass_fullName(name); if (c != null) return c; DynamicObject dO = new DynamicObject(); dO.className = "java.lang.Class"; name = dropPrefix("main$", name); dO.fieldValues.put("name", name); return dO; } public Object parseBigInt() { consume("bigint"); consume("("); String val = tpp(); if (eq(val, "-")) val = "-" + tpp(); consume(")"); return new BigInteger(val); } public Object parseDouble() { consume("d"); consume("("); String val = unquote(tpp()); consume(")"); return Double.parseDouble(val); } public Object parseFloat() { consume("fl"); String val; if (eq(t(), "(")) { consume("("); val = unquote(tpp()); consume(")"); } else { val = unquote(tpp()); } return Float.parseFloat(val); } public void parseHashSet(unstructure_Receiver out) { consume("hashset"); parseSet(new HashSet(), out); } public void parseLinkedHashSet(unstructure_Receiver out) { consume("lhs"); parseSet(new LinkedHashSet(), out); } public void parseTreeSet(unstructure_Receiver out) { consume("treeset"); parseSet(new TreeSet(), out); } public void parseCISet(unstructure_Receiver out) { consume("ciset"); parseSet(ciSet(), out); } public void parseMap(unstructure_Receiver out) { parseMap(new TreeMap(), out); } public Object parseJava() { String j = unquote(tpp()); Matches m = new Matches(); if (jmatch("java.awt.Color[r=*,g=*,b=*]", j, m)) return nuObject("java.awt.Color", parseInt(m.unq(0)), parseInt(m.unq(1)), parseInt(m.unq(2))); else { warn("Unknown Java object: " + j); return null; } } public void parseMap(final Map map, final unstructure_Receiver out) { consume("{"); stack.add(new Runnable() { public boolean v; public Object key; public void run() { if (v) { v = false; stack.add(this); if (!eq(tpp(), "=")) throw fail("= expected, got " + t() + " in map of size " + l(map)); parse(new unstructure_Receiver() { public void set(Object value) { map.put(key, value); if (eq(t(), ",")) consume(); } }); } else { if (eq(t(), "}")) { consume("}"); out.set(map); } else { v = true; stack.add(this); parse(new unstructure_Receiver() { public void set(Object o) { key = o; } }); } } } }); } public void consume() { curT = tok.next(); ++i; } public void consume(String s) { if (!eq(t(), s)) { throw fail(quote(s) + " expected, got " + quote(t())); } consume(); } public void parse_x(unstructure_Receiver out) { consume(); parse(out); while (nempty(stack)) popLast(stack).run(); } } Boolean b = DynamicObject_loading.get(); DynamicObject_loading.set(true); try { final Var v = new Var(); X x = new X(); x.parse_x(new unstructure_Receiver() { public void set(Object o) { v.set(o); } }); unstructure_tokrefs = x.tokrefs.size(); return v.get(); } finally { DynamicObject_loading.set(b); } } public static boolean unstructure_debug; public static boolean regionMatchesIC(String a, int offsetA, String b, int offsetB, int len) { return a != null && a.regionMatches(true, offsetA, b, offsetB, len); } public static void _registerIO(Object object, String path, boolean opened) { } public static String classNameToVM(String name) { return name.replace(".", "$"); } public static List quotelessJavaTok(String s) { ArrayList tok = new ArrayList(); int l = s.length(); int i = 0, n = 0; while (i < l) { int j = i; char c, d; 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; } tok.add(javaTok_substringN(s, i, j)); ++n; i = j; if (i >= l) break; c = s.charAt(i); d = i + 1 >= l ? '\0' : s.charAt(i + 1); if (Character.isJavaIdentifierStart(c)) do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); else if (Character.isDigit(c)) { do ++j; while (j < l && Character.isDigit(s.charAt(j))); if (j < l && s.charAt(j) == 'L') ++j; } else ++j; tok.add(javaTok_substringC(s, i, j)); ++n; i = j; } if ((tok.size() % 2) == 0) tok.add(""); return tok; } public static int findEndOfBracketPart(List cnc, int i) { int j = i + 2, level = 1; while (j < cnc.size()) { if (eqOneOf(cnc.get(j), "{", "(")) ++level; else if (eqOneOf(cnc.get(j), "}", ")")) --level; if (level == 0) return j + 1; ++j; } return cnc.size(); } public static List replaceSubList(List l, List x, List y) { return replaceSublist(l, x, y); } public static List replaceSubList(List l, int fromIndex, int toIndex, List y) { return replaceSublist(l, fromIndex, toIndex, y); } public static ExpiringMap2 mechList_raw_cache = new ExpiringMap2(10000).setMap(ciMap()); public static Lock mechList_raw_lock = lock(); public static int mechList_raw_timeout = 60000; public static String mechList_raw(String name) { try { Lock __1624 = mechList_raw_lock; lock(__1624); try { if (empty(name)) return ""; String src = mechList_raw_cache.get(name); if (src != null) return src; src = mechList_raw_fresh(name); if (src != null) mechList_raw_cache.put(name, src); return src; } finally { unlock(__1624); } } catch (Exception __e) { throw rethrow(__e); } } public static VF2 mechList_raw_listener = new VF2() { public void get(String msg, Object arg) { try { if (eq(msg, "mechChange") && arg instanceof String) { mechList_raw_cache.remove((String) arg); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (eq(msg, 'mechChange) && arg instanceof S) {\r\n //print(\"Got change noti..."; } }; public static void _onLoad_mechList_raw() { add(vm_busListeners_live(), mechList_raw_listener); } public static void cleanMeUp_mechList_raw() { remove(vm_busListeners_live(), mechList_raw_listener); } public static HashMap asHashMap(Map map) { if (map instanceof HashMap) return (HashMap) map; HashMap m = new HashMap(); m.putAll(map); return m; } public static Map parseDoubleArrowMap(Map map, List l) { for (String s : l) { List x = splitAtDoubleArrow_bothDirections(s); if (l(x) == 2) map.put(first(x), second(x)); } return map; } public static List similarEmptyList(Collection m) { return new ArrayList(); } public static Method findMethod_cached(Object o, String method, Object... args) { try { if (o == null) return null; if (o instanceof Class) { _MethodCache cache = callOpt_getCache(((Class) o)); List methods = cache.cache.get(method); if (methods != null) for (Method m : methods) if (isStaticMethod(m) && findMethod_checkArgs(m, args, false)) return m; return null; } else { _MethodCache cache = callOpt_getCache(o.getClass()); List methods = cache.cache.get(method); if (methods != null) for (Method m : methods) if (findMethod_checkArgs(m, args, false)) return m; return null; } } catch (Exception __e) { throw rethrow(__e); } } public static HashMap, Class> loadFunctions_cache = new HashMap(); public static Lock loadFunctions_cached_lock = lock(); public static boolean loadFunctions_preferDiskCache; public static Class loadFunctions_cached(String... functions) { return loadFunctions_cached(asList(functions)); } public static Class loadFunctions_cached(Collection functions) { TreeSet functionSet = new TreeSet(functions); Lock __2150 = loadFunctions_cached_lock; lock(__2150); try { Class c = loadFunctions_cache.get(functionSet); if (c != null) return c; String cached = getTranspiledStandardFunctionsIfCached(functionSet); String transpilation = null; if (cached != null && (loadFunctions_preferDiskCache || standardFunctionsCacheUpToDate(functionSet))) transpilation = cached; if (transpilation != null) { print("Loading " + joinWithPlus(functionSet)); c = veryQuickJava_finish(transpilation, emptyList()); } if (c == null) c = loadFunctions(functionSet); loadFunctions_cache.put(functionSet, c); assertNotNull(loadFunctions_cache.get(functionSet)); return c; } finally { unlock(__2150); } } public static void loadFunctions_clearCache() { Lock __2151 = loadFunctions_cached_lock; lock(__2151); try { loadFunctions_cache.clear(); } finally { unlock(__2151); } } public static void _onLoad_loadFunctions_cached() { ownResource(vmBus_onMessage("refreshedTranspiler", new Runnable() { public void run() { try { loadFunctions_clearCache(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "loadFunctions_clearCache();"; } })); } public static boolean preciseCall_debug; public static Object preciseCall(Object o, String method, Object... args) { try { if (o instanceof Class) { _MethodCache cache = callOpt_getCache(((Class) o)); List methods = cache.cache.get(method); Lowest best = new Lowest(); if (methods != null) for (Method m : methods) { { if (!(isStaticMethod(m))) continue; } int score = methodApplicabilityScore(m, args); if (score < Integer.MAX_VALUE) { if (preciseCall_debug) print("Method score: " + m + " " + score); best.put(m, score); } } Method m = best.get(); if (m != null) return invokeMethod(m, null, args); return call_withVarargs(((Class) o), method, args); } else throw todo(); } catch (Exception __e) { throw rethrow(__e); } } public static boolean tok_shouldAddReturn(List tok) { String lastToken = get(tok, l(tok) - 2); return !eqOneOf(lastToken, "}", ";"); } public static boolean tok_shouldAddReturn(String s) { return tok_shouldAddReturn(javaTok(s)); } public static boolean isKnownVoidFunction(String name) { return standardFunctionAlwaysReturnsVoid(name); } public static void tokAppend_reTok(List tok, int i, String s) { tok.set(i, tok.get(i) + s); reTok(tok, i, i + 1); } public static String tok_addReturn(List tok) { if (tok_shouldAddReturn(tok)) { tokPrepend(tok, 1, "ret "); tokAppend(tok, l(tok) - 2, ";"); } return join(tok); } public static String tok_addReturn(String s) { return tok_addReturn(javaTok(s)); } public static boolean containsReturnWithArgument(List tok) { for (int i = 1; i + 2 < l(tok); i += 2) if (eqOneOf(tok.get(i), "ret", "return") && neq(tok.get(i + 2), ";")) return true; return false; } public static boolean containsReturnWithArgument(String code) { return containsReturnWithArgument(javaTok(code)); } public static boolean veryQuickJava_silent = true; public static boolean veryQuickJava_useCompilerBot = true; public static ThreadLocal veryQuickJava_transpiled = new ThreadLocal(); public static Object veryQuickJava3_cacheFunction; public static Class veryQuickJava3(String mainJava) { return veryQuickJava3(mainJava, emptyList()); } public static Class veryQuickJava3(String mainJava, List libs) { Class c = (Class) (callF(veryQuickJava3_cacheFunction, mainJava, libs)); if (c != null) return c; transpileRaw_silent = veryQuickJava_silent; String src = transpileRaw(mainJava); if (veryQuickJava_transpiled.get() != null) veryQuickJava_transpiled.set(src); return veryQuickJava_finish(src, libs); } public static List dependentClasses() { return cleanUpAndGetWeakReferencesList(hotwire_classes); } public static void smartSet(Field f, Object o, Object value) throws Exception { try { f.set(o, value); } catch (Exception e) { Class type = f.getType(); if (type == int.class && value instanceof Long) value = ((Long) value).intValue(); if (type == LinkedHashMap.class && value instanceof Map) { f.set(o, asLinkedHashMap((Map) value)); return; } try { if (f.getType() == Concept.Ref.class) { f.set(o, ((Concept) o).new Ref((Concept) value)); return; } if (o instanceof Concept.Ref) { f.set(o, ((Concept.Ref) o).get()); return; } } catch (Throwable _e) { } throw e; } } public static Object callCalc(Object o, Object... args) { return call(o, "calc", args); } public static Map compileRegexp_cache = syncMRUCache(10); public static java.util.regex.Pattern compileRegexp(String pat) { java.util.regex.Pattern p = compileRegexp_cache.get(pat); if (p == null) { compileRegexp_cache.put(pat, p = java.util.regex.Pattern.compile(pat)); } return p; } public static Producer javaTokC_noMLS_iterator(final String s) { return javaTokC_noMLS_iterator(s, 0); } public static Producer javaTokC_noMLS_iterator(final String s, final int startIndex) { return new Producer() { public final int l = s.length(); public int i = startIndex; public String next() { if (i >= l) return null; int j = i; char c, d; 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) return null; c = s.charAt(i); d = i + 1 >= l ? '\0' : s.charAt(i + 1); if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { if (s.charAt(j) == opener || s.charAt(j) == '\n') { ++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)); else if (Character.isDigit(c)) { do ++j; while (j < l && Character.isDigit(s.charAt(j))); if (j < l && s.charAt(j) == 'L') ++j; } else ++j; String t = quickSubstring(s, i, j); i = j; return t; } }; } public static boolean regexpContains(String pat, String s) { return regexpFinds(pat, s); } public static boolean regexpContains(Pattern pat, String s) { return regexpFinds(pat, s); } public static List getClasses(Object[] array) { List l = new ArrayList(); for (Object o : array) l.add(_getClass(o)); return l; } public static void failIfOddCount(Object... list) { if (odd(l(list))) throw fail("Odd list size: " + list); } public static Map> parse3_cachedInput_cache = synchronizedMRUCache(1000); public static List parse3_cachedInput(String s) { List tok = parse3_cachedInput_cache.get(s); if (tok == null) parse3_cachedInput_cache.put(s, tok = parse3(s)); return tok; } public static Map> parse3_cachedPattern_cache = synchronizedMRUCache(1000); public static synchronized List parse3_cachedPattern(String s) { List tok = parse3_cachedPattern_cache.get(s); if (tok == null) parse3_cachedPattern_cache.put(s, tok = parse3(s)); return tok; } public static boolean matchX(String pat, String s) { return matchX(pat, s, null); } public static boolean matchX(String pat, String s, Matches m) { if (endsWith(pat, "...")) return matchStartX(pat, s, m); if (startsWith(pat, "...")) return matchEndX(pat, s, m); return match(pat, s, m); } public static Set similarEmptySet(Collection m) { if (m instanceof TreeSet) return new TreeSet(((TreeSet) m).comparator()); if (m instanceof LinkedHashSet) return new LinkedHashSet(); return new HashSet(); } public static Map mapValues(Object func, Map map) { Map m = similarEmptyMap(map); for (Object key : keys(map)) m.put(key, callF(func, map.get(key))); return m; } public static Map mapValues(Map map, Object func) { return mapValues(func, map); } public static List uniquify(Collection l) { return uniquifyList(l); } public static List> multiMapToPairs(MultiMap mm) { List> out = new ArrayList(); for (A key : keys(mm)) for (B value : mm.get(key)) out.add(pair(key, value)); return out; } public static Set setMinusSet(Set l, Collection stuff) { if (empty(stuff)) return l; Set set = asSet(stuff); Set l2 = similarEmptySet(l); for (A a : l) if (!set.contains(a)) l2.add(a); return l2; } public static Map>> mechList_opt_tlft_cached_cache = ciMap(); public static Lock mechList_opt_tlft_cached_lock = lock(); public static List mechList_opt_tlft_cached(String name) { MechListDependentCache> listCache; { Lock __2056 = mechList_opt_tlft_cached_lock; lock(__2056); try { listCache = mechList_opt_tlft_cached_cache.get(name); if (listCache == null) mechList_opt_tlft_cached_cache.put(name, listCache = new MechListDependentCache(name, new F1>() { public List get(String x) { try { return tlft(x); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "tlft(x)"; } })); } finally { unlock(__2056); } } return listCache.get(); } public static List dropFirstAndLast(int n, List l) { return new ArrayList(subList(l, n, l(l) - n)); } public static List dropFirstAndLast(List l) { return dropFirstAndLast(1, l); } public static String dropFirstAndLast(String s) { return substring(s, 1, l(s) - 1); } public static Producer javaTokC_noMLS_onReader(final BufferedReader r) { final class X implements Producer { public StringBuilder buf = new StringBuilder(); public char c, d, e = 'x'; public X() { nc(); nc(); nc(); } public void nc() { try { c = d; d = e; if (e == '\0') return; int i = r.read(); e = i < 0 ? '\0' : i == '\0' ? '_' : (char) i; } catch (Exception __e) { throw rethrow(__e); } } public void ncSave() { if (c != '\0') { buf.append(c); nc(); } } public String next() { while (c != '\0') { if (c == ' ' || c == '\t' || c == '\r' || c == '\n') nc(); else if (c == '/' && d == '*') { do nc(); while (c != '\0' && !(c == '*' && d == '/')); nc(); nc(); } else if (c == '/' && d == '/') { do nc(); while (c != '\0' && "\r\n".indexOf(c) < 0); } else break; } if (c == '\0') return null; if (c == '\'' || c == '"') { char opener = c; ncSave(); while (c != '\0') { if (c == opener || c == '\n') { ncSave(); break; } else if (c == '\\') { ncSave(); ncSave(); } else ncSave(); } } else if (Character.isJavaIdentifierStart(c)) do ncSave(); while (Character.isJavaIdentifierPart(c) || c == '\''); else if (Character.isDigit(c)) { do ncSave(); while (Character.isDigit(c)); if (c == 'L') ncSave(); } else ncSave(); String t = buf.toString(); buf.setLength(0); return t; } } return new X(); } public static String unquoteUsingCharArray(String s, char[] buf) { if (s == null) return null; if (startsWith(s, '[')) { int i = 1; while (i < s.length() && s.charAt(i) == '=') ++i; if (i < s.length() && s.charAt(i) == '[') { String m = s.substring(1, i); if (s.endsWith("]" + m + "]")) return s.substring(i + 1, s.length() - i - 1); } } if (s.length() > 1) { char c = s.charAt(0); if (c == '\"' || c == '\'') { int l = endsWith(s, c) ? s.length() - 1 : s.length(); if (l > buf.length) return unquote(s); int n = 0; for (int i = 1; i < l; i++) { char ch = s.charAt(i); if (ch == '\\') { char nextChar = (i == l - 1) ? '\\' : s.charAt(i + 1); if (nextChar >= '0' && nextChar <= '7') { String code = "" + nextChar; i++; if ((i < l - 1) && s.charAt(i + 1) >= '0' && s.charAt(i + 1) <= '7') { code += s.charAt(i + 1); i++; if ((i < l - 1) && s.charAt(i + 1) >= '0' && s.charAt(i + 1) <= '7') { code += s.charAt(i + 1); i++; } } buf[n++] = (char) Integer.parseInt(code, 8); continue; } switch(nextChar) { case '\"': ch = '\"'; break; case '\\': ch = '\\'; break; case 'b': ch = '\b'; break; case 'f': ch = '\f'; break; case 'n': ch = '\n'; break; case 'r': ch = '\r'; break; case 't': ch = '\t'; break; case '\'': ch = '\''; break; case 'u': if (i >= l - 5) { ch = 'u'; break; } int code = Integer.parseInt("" + s.charAt(i + 2) + s.charAt(i + 3) + s.charAt(i + 4) + s.charAt(i + 5), 16); char[] x = Character.toChars(code); int lx = x.length; for (int j = 0; j < lx; j++) buf[n++] = x[j]; i += 5; continue; default: ch = nextChar; } i++; } buf[n++] = ch; } return new String(buf, 0, n); } } return s; } public static boolean structure_isMarker(String s, int i, int j) { if (i >= j) return false; if (s.charAt(i) != 'm') return false; ++i; while (i < j) { char c = s.charAt(i); if (c < '0' || c > '9') return false; ++i; } return true; } public static String internIfLongerThan(String s, int l) { return s == null ? null : l(s) >= l ? intern(s) : s; } public static char unquoteCharacter(String s) { assertTrue(s.startsWith("'") && s.length() > 1); return unquote("\"" + s.substring(1, s.endsWith("'") ? s.length() - 1 : s.length()) + "\"").charAt(0); } public static float parseFloat(String s) { return Float.parseFloat(s); } public static SortedMap synchronizedTreeMap() { return synchroTreeMap(); } public static List parseList(String s) { return (List) safeUnstructure(s); } public static Map synchronizedMap() { return synchroMap(); } public static Map synchronizedMap(Map map) { return synchroMap(map); } public static byte[] hexToBytes(String s) { if (odd(l(s))) throw fail("Hex string has odd length: " + quote(shorten(10, s))); int n = l(s) / 2; byte[] bytes = new byte[n]; for (int i = 0; i < n; i++) { int a = parseHexChar(s.charAt(i * 2)); int b = parseHexChar(s.charAt(i * 2 + 1)); if (a < 0 || b < 0) throw fail("Bad hex byte: " + quote(substring(s, i * 2, i * 2 + 2)) + " at " + i * 2 + "/" + l(s)); bytes[i] = (byte) ((a << 4) | b); } return bytes; } public static boolean[] boolArrayFromBytes(byte[] a, int n) { boolean[] b = new boolean[n]; int m = min(n, l(a) * 8); for (int i = 0; i < m; i++) b[i] = (a[i / 8] & 1 << (i & 7)) != 0; return b; } public static A nuStubInnerObject(Class c) { return nuStubInnerObject(c, null); } public static A nuStubInnerObject(Class c, Object classFinder) { try { Class outerType = getOuterClass(c, classFinder); Constructor m = c.getDeclaredConstructor(outerType); m.setAccessible(true); return (A) m.newInstance(new Object[] { null }); } catch (Exception __e) { throw rethrow(__e); } } public static Map nuEmptyObject_cache = newDangerousWeakHashMap(); public static A nuEmptyObject(Class c) { try { Constructor ctr; synchronized (nuEmptyObject_cache) { ctr = nuEmptyObject_cache.get(c); if (ctr == null) { nuEmptyObject_cache.put(c, ctr = nuEmptyObject_findConstructor(c)); ctr.setAccessible(true); } } try { return (A) ctr.newInstance(); } catch (InstantiationException e) { if (empty(e.getMessage())) if ((c.getModifiers() & Modifier.ABSTRACT) != 0) throw fail("Can't instantiate abstract class " + className(c), e); else throw fail("Can't instantiate " + className(c), e); else throw rethrow(e); } } catch (Exception __e) { throw rethrow(__e); } } public static Constructor nuEmptyObject_findConstructor(Class c) { for (Constructor m : c.getDeclaredConstructors()) if (m.getParameterTypes().length == 0) return m; throw fail("No default constructor declared in " + c.getName()); } public static HashMap findClass_cache = new HashMap(); public static Class findClass(String name) { synchronized (findClass_cache) { if (findClass_cache.containsKey(name)) return findClass_cache.get(name); if (!isJavaIdentifier(name)) return null; Class c; try { c = Class.forName("main$" + name); } catch (ClassNotFoundException e) { c = null; } findClass_cache.put(name, c); return c; } } public static void setOptAllDyn(DynamicObject o, Map fields) { if (fields == null) return; HashMap fieldMap = instanceFieldsMap(o); for (Map.Entry e : fields.entrySet()) { String field = e.getKey(); Object val = e.getValue(); boolean has = fieldMap.containsKey(field); if (has) setOpt(o, field, val); else { o.fieldValues.put(intern(field), val); } } } public static void setOptAll_pcall(Object o, Map fields) { if (fields == null) return; for (String field : keys(fields)) try { setOpt(o, field, fields.get(field)); } catch (Throwable __e) { print(exceptionToStringShort(__e)); } } public static void setOptAll_pcall(Object o, Object... values) { warnIfOddCount(values); for (int i = 0; i + 1 < l(values); i += 2) { String field = (String) values[i]; Object value = values[i + 1]; try { setOpt(o, field, value); } catch (Throwable __e) { print(exceptionToStringShort(__e)); } } } public static String intern(String s) { return fastIntern(s); } public static void pcallOpt_noArgs(Object o, String method) { try { callOpt_noArgs(o, method); } catch (Throwable __e) { _handleException(__e); } } public static int[] toIntArray(List l) { int[] a = new int[l(l)]; for (int i = 0; i < a.length; i++) a[i] = l.get(i); return a; } public static String dropPrefix(String prefix, String s) { return s == null ? null : s.startsWith(prefix) ? s.substring(l(prefix)) : s; } public static boolean warn_on = true; public static void warn(String s) { if (warn_on) print("Warning: " + s); } public static void warn(String s, List warnings) { warn(s); if (warnings != null) warnings.add(s); } public static A popLast(List l) { return liftLast(l); } public static List> hotwire_classes = synchroList(); public static Class hotwireDependent(String src) { Class c = hotwire(src); makeDependent(c); return c; } public static void lock(Lock lock) { try { ping(); if (lock == null) return; try { lock.lockInterruptibly(); } catch (InterruptedException e) { print("Locking interrupted! I probably deadlocked, oops."); printStackTrace(e); rethrow(e); } ping(); } catch (Exception __e) { throw rethrow(__e); } } public static void lock(Lock lock, String msg) { print("Locking: " + msg); lock(lock); } public static void lock(Lock lock, String msg, long timeout) { print("Locking: " + msg); lockOrFail(lock, timeout); } public static ReentrantLock lock() { return fairLock(); } public static String mechList_raw_fresh(String name) { return mechList_opt_raw_fresh(name); } public static void unlock(Lock lock, String msg) { if (lock == null) return; print("Unlocking: " + msg); lock.unlock(); } public static void unlock(Lock lock) { if (lock == null) return; lock.unlock(); } public static void add(BitSet bs, int i) { bs.set(i); } public static boolean add(Collection c, A a) { return c != null && c.add(a); } public static void add(Container c, Component x) { addToContainer(c, x); } public static void remove(List l, int i) { if (l != null && i >= 0 && i < l(l)) l.remove(i); } public static void remove(Collection l, A a) { if (l != null) l.remove(a); } public static List splitAtDoubleArrow_bothDirections(String s) { return splitAtDoubleArrow_bothDirections(javaTok(s)); } public static List splitAtDoubleArrow_bothDirections(List tok) { List l = splitAtDoubleArrow(tok); if (l(l) != 2) l = reversedList(splitAtDoubleLeftArrow(tok)); return l; } public static Method findMethod(Object o, String method, Object... args) { return findMethod_cached(o, method, args); } public static boolean findMethod_checkArgs(Method m, Object[] args, boolean debug) { Class[] types = m.getParameterTypes(); if (types.length != args.length) { if (debug) System.out.println("Bad parameter length: " + args.length + " vs " + types.length); return false; } for (int i = 0; i < types.length; i++) if (!(args[i] == null || isInstanceX(types[i], args[i]))) { if (debug) System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]); return false; } return true; } public static String getTranspiledStandardFunctionsIfCached(TreeSet functionSet) { try { File f = transpiledStandardFunctions_file(functionSet); return loadTextFile(f); } catch (Throwable __e) { return null; } } public static boolean standardFunctionsCacheUpToDate(TreeSet functionSet) { File f = transpiledStandardFunctions_file(functionSet); long date = recommendedTranspilationDateForStandardFunctions(functionSet); return date != 0 && f.lastModified() >= date; } public static String joinWithPlus(Collection c) { return join("+", c); } public static String joinWithPlus(String... c) { return join("+", c); } public static Class veryQuickJava_finish(String src, List libs) { libs = cloneList(libs); src = findTranslators2(src, libs); String dehlibs = join(" ", libs); File bytecode = null; try { bytecode = javaCompile_overInternalBot(src, dehlibs); } catch (Throwable _e) { print("Was compiling: " + src + "\n"); throw rethrow(_e); } return hotwireCore(concatLists(ll(bytecode), loadLibraries(libs))); } public static boolean loadFunctions_debug; public static Object loadFunctions_veryQuickJava; public static Class loadFunctions(String... functions) { return loadFunctions(asList(functions)); } public static Class loadFunctions(Collection functions) { print("Compiling " + joinWithPlus(functions)); TreeSet functionSet = new TreeSet(functions); StringBuilder buf = new StringBuilder(); for (String s : functionSet) buf.append("please include function " + assertIdentifier(s) + ".\n"); long time = sysNow(); Class c; if (loadFunctions_veryQuickJava != null) c = (Class) callF(loadFunctions_veryQuickJava, str(buf)); else { veryQuickJava_transpiled.set(""); c = veryQuickJava(str(buf)); saveTranspiledStandardFunctions(functionSet, getAndClearThreadLocal(veryQuickJava_transpiled)); } if (loadFunctions_debug) done2_always(time, "loadFunctions: " + join(" ", functions)); return c; } public static AutoCloseable vmBus_onMessage(final String msg, final VF1 onMessage) { Map map = vm_busListenersByMessage_live(); synchronized (map) { Set listeners = map.get(msg); if (listeners == null) map.put(msg, listeners = syncIdentityHashSet()); return tempAdd(listeners, new VF2() { public void get(String _msg, Object arg) { try { callF(onMessage, arg); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callF(onMessage, arg)"; } }); } } public static AutoCloseable vmBus_onMessage(String msg, final VF2 onMessage) { return vmBus_onMessage(msg, new VF1() { public void get(Object[] o) { try { callF(onMessage, first(o), second(o)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callF(onMessage, first(o), second(o));"; } }); } public static AutoCloseable vmBus_onMessage(String msg, Runnable onMessage) { return vmBus_onMessage(msg, runnableToVF1(onMessage)); } public static int methodApplicabilityScore(Method m, Object[] args) { Class[] types = m.getParameterTypes(); if (types.length != args.length) return Integer.MAX_VALUE; int score = 0; for (int i = 0; i < types.length; i++) { Object a = args[i]; Class c = types[i]; if (a == null) ++score; else if (c == a.getClass()) { } else if (isInstanceX(c, a)) ++score; else return Integer.MAX_VALUE; } return score; } public static RuntimeException todo() { throw new RuntimeException("TODO"); } public static RuntimeException todo(String msg) { throw new RuntimeException("TODO: " + msg); } public static boolean standardFunctionAlwaysReturnsVoid(String name) { return tok_staticFunctionAlwaysReturnsVoid(javaTok(textOfStandardFunction_cached(name)), name); } public static void tokPrepend(List tok, int i, String s) { tok.set(i, s + tok.get(i)); } public static void tokAppend(List tok, int i, String s) { tok.set(i, tok.get(i) + s); } public static boolean transpileRaw_silent = true; public static boolean transpileRaw_useDiskCache; public static String transpileRaw(String mainJava) { return (String) call(dm_transpiler(), "transpileRaw", mainJava, false, transpileRaw_useDiskCache); } public static List cleanUpAndGetWeakReferencesList(List> l) { if (l == null) return null; synchronized (l) { List out = new ArrayList(); for (int i = 0; i < l(l); i++) { A a = l.get(i).get(); if (a == null) l.remove(i--); else out.add(a); } return out; } } public static Map syncMRUCache(int size) { return synchroMap(new MRUCache(size)); } public static String quickSubstring(String s, int i, int j) { if (i == j) return ""; return s.substring(i, j); } public static boolean regexpFinds(String pat, String s) { return regexp(pat, s).find(); } public static boolean regexpFinds(Pattern pat, String s) { return regexp(pat, s).find(); } public static Class _getClass(String name) { try { return Class.forName(name); } catch (ClassNotFoundException e) { return null; } } public static Class _getClass(Object o) { return o == null ? null : o instanceof Class ? (Class) o : o.getClass(); } public static Class _getClass(Object realm, String name) { try { return getClass(realm).getClassLoader().loadClass(classNameToVM(name)); } catch (ClassNotFoundException e) { return null; } } public static List parse3(String s) { return dropPunctuation(javaTokPlusPeriod(s)); } public static boolean matchEndX(String pat, String s, Matches matches) { return matchEnd(dropPrefix("...", pat), s, matches); } public static NavigableMap synchroTreeMap() { return Collections.synchronizedNavigableMap(new TreeMap()); } public static int shorten_default = 100; public static String shorten(String s) { return shorten(s, shorten_default); } public static String shorten(String s, int max) { return shorten(s, max, "..."); } public static String shorten(String s, int max, String shortener) { if (s == null) return ""; if (max < 0) return s; return s.length() <= max ? s : substring(s, 0, min(s.length(), max - l(shortener))) + shortener; } public static String shorten(int max, String s) { return shorten(s, max); } public static int parseHexChar(char c) { if (c >= '0' && c <= '9') return charDiff(c, '0'); if (c >= 'a' && c <= 'f') return charDiff(c, 'a') + 10; if (c >= 'A' && c <= 'F') return charDiff(c, 'A') + 10; return -1; } public static Class getOuterClass(Class c) { return getOuterClass(c, null); } public static Class getOuterClass(Class c, Object classFinder) { try { String s = c.getName(); int i = s.lastIndexOf('$'); String name = substring(s, 0, i); if (classFinder != null) return (Class) callF(classFinder, name); return Class.forName(name); } catch (Exception __e) { throw rethrow(__e); } } public static HashMap instanceFieldsMap(Object o) { Class c = o.getClass(); HashMap map; synchronized (getOpt_cache) { map = getOpt_cache.get(c); if (map == null) map = getOpt_makeCache(c); } return map; } public static String exceptionToStringShort(Throwable e) { lastException(e); e = getInnerException(e); String msg = hideCredentials(unnull(e.getMessage())); if (msg.indexOf("Error") < 0 && msg.indexOf("Exception") < 0) return baseClassName(e) + prependIfNempty(": ", msg); else return msg; } public static void warnIfOddCount(Object... list) { if (odd(l(list))) printStackTrace("Odd list size: " + list); } public static Method fastIntern_method; public static String fastIntern(String s) { try { if (s == null) return null; if (fastIntern_method == null) { fastIntern_method = findMethodNamed(javax(), "internPerProgram"); if (fastIntern_method == null) upgradeJavaXAndRestart(); } return (String) fastIntern_method.invoke(null, s); } catch (Exception __e) { throw rethrow(__e); } } public static Map> callOpt_noArgs_cache = newDangerousWeakHashMap(); public static Object callOpt_noArgs(Object o, String method) { try { if (o == null) return null; if (o instanceof Class) return callOpt(o, method); Class c = o.getClass(); HashMap map; synchronized (callOpt_noArgs_cache) { map = callOpt_noArgs_cache.get(c); if (map == null) map = callOpt_noArgs_makeCache(c); } Method m = map.get(method); return m != null ? m.invoke(o) : null; } catch (Exception __e) { throw rethrow(__e); } } public static HashMap callOpt_noArgs_makeCache(Class c) { HashMap map = new HashMap(); Class _c = c; do { for (Method m : c.getDeclaredMethods()) if (m.getParameterTypes().length == 0) { m.setAccessible(true); String name = m.getName(); if (!map.containsKey(name)) map.put(name, m); } _c = _c.getSuperclass(); } while (_c != null); callOpt_noArgs_cache.put(c, map); return map; } public static class getOpt_Map extends WeakHashMap { public getOpt_Map() { if (getOpt_special == null) getOpt_special = new HashMap(); clear(); } public void clear() { super.clear(); put(Class.class, getOpt_special); put(String.class, getOpt_special); } } public static final Map> getOpt_cache = _registerDangerousWeakMap(synchroMap(new getOpt_Map())); public static HashMap getOpt_special; public static Object getOpt_cached(Object o, String field) { try { if (o == null) return null; Class c = o.getClass(); HashMap map; synchronized (getOpt_cache) { map = getOpt_cache.get(c); if (map == null) map = getOpt_makeCache(c); } if (map == getOpt_special) { if (o instanceof Class) return getOpt((Class) o, field); if (o instanceof Map) return ((Map) o).get(field); } Field f = map.get(field); if (f != null) return f.get(o); if (o instanceof DynamicObject) return ((DynamicObject) o).fieldValues.get(field); return null; } catch (Exception __e) { throw rethrow(__e); } } public static HashMap getOpt_makeCache(Class c) { HashMap map; if (isSubtypeOf(c, Map.class)) map = getOpt_special; else { map = new HashMap(); Class _c = c; do { for (Field f : _c.getDeclaredFields()) { f.setAccessible(true); String name = f.getName(); if (!map.containsKey(name)) map.put(name, f); } _c = _c.getSuperclass(); } while (_c != null); } if (getOpt_cache != null) getOpt_cache.put(c, map); return map; } public static Class hotwire(String src) { assertFalse(_inCore()); Class j = getJavaX(); if (isAndroid()) { synchronized (j) { List libraries = new ArrayList(); File srcDir = (File) call(j, "transpileMain", src, libraries); if (srcDir == null) throw fail("transpileMain returned null (src=" + quote(src) + ")"); Object androidContext = get(j, "androidContext"); return (Class) call(j, "loadx2android", srcDir, src); } } else { Class c = (Class) (call(j, "hotwire", src)); hotwire_copyOver(c); return c; } } public static void lockOrFail(Lock lock, long timeout) { try { ping(); if (!lock.tryLock(timeout, TimeUnit.MILLISECONDS)) { String s = "Couldn't acquire lock after " + timeout + " ms."; if (lock instanceof ReentrantLock) { ReentrantLock l = (ReentrantLock) lock; s += " Hold count: " + l.getHoldCount() + ", owner: " + call(l, "getOwner"); } throw fail(s); } ping(); } catch (Exception __e) { throw rethrow(__e); } } public static ReentrantLock fairLock() { return new ReentrantLock(true); } public static String mechList_opt_raw_fresh(String name) { Object readMode = mechMode().readMode; if (readMode instanceof File) { String md5Name = uniqueFileNameUsingMD5_80_v2(upper(name)); return loadTextFileFromZipFile((File) readMode, md5Name); } boolean useLocal = false, useRemote = true; if (eq(readMode, "mergeLocalAndRemote")) useLocal = true; else if (eq(readMode, "local")) { useLocal = true; useRemote = false; } String s = ""; if (useRemote) { if (eq(mechMode().readMode, "localCopies")) s = unnull(loadTextFile(remoteMechListMirrorFile(name))); else s = serverMechList_raw_fresh(name, true); } if (useLocal) s = appendNewLineIfNempty(s) + localMechList_opt_raw_fresh(name); return s; } public static void addToContainer(final Container a, final Component b) { if (a != null && b != null) { swing(new Runnable() { public void run() { try { a.add(b); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "a.add(b);"; } }); } } public static List splitAtDoubleLeftArrow(String s) { return splitAtDoubleLeftArrow(javaTok(s)); } public static List splitAtDoubleLeftArrow(List tok) { List l = new ArrayList(); int i = 0; while (i < l(tok)) { int j = indexOfSubList(tok, ll("<", "", "="), i); if (i >= l(tok)) break; if (j < 0) j = l(tok); l.add(trimJoin(tok.subList(i, j))); i = j + 3; } return l; } public static File transpiledStandardFunctions_file(TreeSet functionSet) { String name = uniqueFileNameUsingMD5_80_v2(join("+", functionSet)); File dir = javaxCachesDir("Transpiled Standard Functions"); saveTextFileUnlessExists(newFile(dir, name + ".functions"), lines(functionSet)); return newFile(dir, name + ".java"); } public static String loadTextFile(String fileName) { return loadTextFile(fileName, null); } public static String loadTextFile(File f, String defaultContents) { try { checkFileNotTooBigToRead(f); if (f == null || !f.exists()) return defaultContents; FileInputStream fileInputStream = new FileInputStream(f); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); return loadTextFile(inputStreamReader); } catch (Exception __e) { throw rethrow(__e); } } public static String loadTextFile(File fileName) { return loadTextFile(fileName, null); } public static String loadTextFile(String fileName, String defaultContents) { return fileName == null ? defaultContents : loadTextFile(newFile(fileName), defaultContents); } public static String loadTextFile(Reader reader) throws IOException { StringBuilder builder = new StringBuilder(); try { char[] buffer = new char[1024]; int n; while (-1 != (n = reader.read(buffer))) builder.append(buffer, 0, n); } finally { reader.close(); } return str(builder); } public static long recommendedTranspilationDateForStandardFunctions(Collection sfNames) { long date = 0; for (String name : sfNames) { long d = recommendedTranspilationDateForStandardFunction(name); if (d == 0) return 0; date = max(date, d); } return date; } public static String findTranslators2(String src, List libsOut) { return join(findTranslators2(javaTok(src), libsOut)); } public static List findTranslators2(List tok, List libsOut) { int i; while ((i = jfind(tok, "!")) >= 0) { setAdd(libsOut, tok.get(i + 2)); clearTokens(tok, i, i + 3); } return tok; } public static File javaCompile_overInternalBot(String src) { return javaCompile_overInternalBot(src, ""); } public static synchronized File javaCompile_overInternalBot(String src, String dehlibs) { return CompilerBot.compile(src, dehlibs); } public static Class hotwireCore(List files) { try { JavaXClassLoader classLoader = hotwire_makeClassLoader(files); Class theClass = classLoader.loadClass("main"); setOpt(theClass, "__javax", getJavaX()); if (getOpt(theClass, "programID") == null) setOpt(theClass, "programID", "#3999999"); if (!_inCore()) hotwire_copyOver(theClass); return theClass; } catch (Exception __e) { throw rethrow(__e); } } public static List loadLibraries(List snippetIDs) { return map("loadLibrary", snippetIDs); } public static void saveTranspiledStandardFunctions(TreeSet functionSet, String transpiledSrc) { try { saveTextFile(transpiledStandardFunctions_file(functionSet), transpiledSrc); } catch (Throwable __e) { _handleException(__e); } } public static A getAndClearThreadLocal(ThreadLocal tl) { A a = tl.get(); tl.set(null); return a; } public static long done2_always(long startTime, String desc) { long time = sysNow() - startTime; print(desc + " [" + time + " ms]"); return time; } public static long done2_always(String desc, long startTime) { return done2_always(startTime, desc); } public static long done2_always(long startTime) { return done2_always(startTime, ""); } public static A third(List l) { return _get(l, 2); } public static A third(A[] bla) { return bla == null || bla.length <= 2 ? null : bla[2]; } public static C third(T3 t) { return t == null ? null : t.c; } public static VF1 runnableToVF1(final Runnable r) { return r == null ? null : new VF1() { public void get(A a) { try { r.run(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "r.run()"; } }; } public static boolean tok_staticFunctionAlwaysReturnsVoid(List tok, String name) { Set types = tok_returnTypesOfStaticFunction_uncleaned(tok, name); if (empty(types)) return false; for (String type : types) if (!containsOneOf(javaTokC(type), "void", "svoid")) return false; return true; } public static String textOfStandardFunction_cached(String sfName) { return textOfStandardFunction(sfName); } public static Object dm_transpiler() { return dm_getModule(dm_requireModule("#1017065/Transpiler")); } public static Matcher regexp(String pat, String s) { return regexp(compileRegexp(pat), unnull(s)); } public static Matcher regexp(Pattern pat, String s) { return pat.matcher(unnull(s)); } public static List dropPunctuation_keep = litlist("*", "<", ">"); public static List dropPunctuation(List tok) { tok = new ArrayList(tok); for (int i = 1; i < tok.size(); i += 2) { String t = tok.get(i); if (t.length() == 1 && !Character.isLetter(t.charAt(0)) && !Character.isDigit(t.charAt(0)) && !dropPunctuation_keep.contains(t)) { tok.set(i - 1, tok.get(i - 1) + tok.get(i + 1)); tok.remove(i); tok.remove(i); i -= 2; } } return tok; } public static String dropPunctuation(String s) { return join(dropPunctuation(nlTok(s))); } public static List javaTokPlusPeriod(String s) { List tok = new ArrayList(); int l = s.length(); int i = 0; while (i < l) { int j = i; char c; String cc; while (j < l) { c = s.charAt(j); cc = s.substring(j, Math.min(j + 2, l)); if (c == ' ' || c == '\t' || c == '\r' || c == '\n') ++j; else if (cc.equals("/*")) { do ++j; while (j < l && !s.substring(j, Math.min(j + 2, l)).equals("*/")); j = Math.min(j + 2, l); } else if (cc.equals("//")) { do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0); } else break; } tok.add(s.substring(i, j)); i = j; if (i >= l) break; c = s.charAt(i); cc = s.substring(i, Math.min(i + 2, l)); if (c == (char) 0x201C || c == (char) 0x201D) c = '"'; if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { char _c = s.charAt(j); if (_c == (char) 0x201C || _c == (char) 0x201D) _c = '"'; if (_c == opener) { ++j; break; } else if (s.charAt(j) == '\\' && j + 1 < l) j += 2; else ++j; } if (j - 1 >= i + 1) { tok.add(opener + s.substring(i + 1, j - 1) + opener); i = j; continue; } } else if (Character.isJavaIdentifierStart(c)) do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || s.charAt(j) == '\'')); else if (Character.isDigit(c)) do ++j; while (j < l && Character.isDigit(s.charAt(j))); else if (cc.equals("[[")) { do ++j; while (j + 1 < l && !s.substring(j, j + 2).equals("]]")); j = Math.min(j + 2, l); } else if (cc.equals("[=") && 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 if (s.substring(j, Math.min(j + 3, l)).equals("...")) j += 3; else if (c == '$' || c == '#') do ++j; while (j < l && Character.isDigit(s.charAt(j))); else ++j; tok.add(s.substring(i, j)); i = j; } if ((tok.size() % 2) == 0) tok.add(""); return tok; } public static boolean matchEnd(String pat, String s) { return matchEnd(pat, s, null); } public static boolean matchEnd(String pat, String s, Matches matches) { if (s == null) return false; List tokpat = parse3(pat), toks = parse3(s); if (toks.size() < tokpat.size()) return false; String[] m = match2(tokpat, takeLast(l(tokpat), toks)); if (m == null) return false; if (matches != null) { matches.m = new String[m.length + 1]; arraycopy(m, matches.m); matches.m[m.length] = join(dropLast(l(tokpat), toks)); } return true; } public static int charDiff(char a, char b) { return (int) a - (int) b; } public static int charDiff(String a, char b) { return charDiff(stringToChar(a), b); } public static String baseClassName(String className) { return substring(className, className.lastIndexOf('.') + 1); } public static String baseClassName(Object o) { return baseClassName(getClassName(o)); } public static String prependIfNempty(String prefix, String s) { return empty(s) ? s : prefix + s; } public static Method findMethodNamed(Object obj, String method) { if (obj == null) return null; if (obj instanceof Class) return findMethodNamed((Class) obj, method); return findMethodNamed(obj.getClass(), method); } public static Method findMethodNamed(Class c, String method) { while (c != null) { for (Method m : c.getDeclaredMethods()) if (m.getName().equals(method)) { m.setAccessible(true); return m; } c = c.getSuperclass(); } return null; } public static void upgradeJavaXAndRestart() { run("#1001639"); restart(); sleep(); } public static File loadLibrary(String snippetID) { return loadBinarySnippet(snippetID); } public static void clear(Collection c) { if (c != null) c.clear(); } public static void put(Map map, A a, B b) { if (map != null) map.put(a, b); } public static List _registerDangerousWeakMap_preList; public static A _registerDangerousWeakMap(A map) { return _registerDangerousWeakMap(map, null); } public static A _registerDangerousWeakMap(A map, Object init) { callF(init, map); if (init instanceof String) { final String f = (String) init; init = new VF1() { public void get(Map map) { try { callMC(f, map); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callMC(f, map)"; } }; } if (javax() == null) { if (_registerDangerousWeakMap_preList == null) _registerDangerousWeakMap_preList = synchroList(); _registerDangerousWeakMap_preList.add(pair(map, init)); return map; } call(javax(), "_registerDangerousWeakMap", map, init); return map; } public static void _onLoad_registerDangerousWeakMap() { assertNotNull(javax()); if (_registerDangerousWeakMap_preList == null) return; for (Pair p : _registerDangerousWeakMap_preList) _registerDangerousWeakMap(p.a, p.b); _registerDangerousWeakMap_preList = null; } public static boolean isSubtypeOf(Class a, Class b) { return b.isAssignableFrom(a); } public static void assertFalse(Object o) { if (!(eq(o, false))) throw fail(str(o)); } public static boolean assertFalse(boolean b) { if (b) throw fail("oops"); return b; } public static boolean assertFalse(String msg, boolean b) { if (b) throw fail(msg); return b; } public static boolean _inCore() { return false; } public static List hotwire_copyOver_after = synchroList(); public static void hotwire_copyOver(Class c) { for (String field : ll("print_log", "print_silent", "androidContext", "_userHome")) setOptIfNotNull(c, field, getOpt(mc(), field)); setOptIfNotNull(c, "mainBot", getMainBot()); setOpt(c, "creator_class", new WeakReference(mc())); pcallFAll(hotwire_copyOver_after, c); } public static String uniqueFileNameUsingMD5_80_v2(String fullName) { return uniqueFileNameUsingMD5_80_v2(fullName, md5(fullName)); } public static String uniqueFileNameUsingMD5_80_v2(String fullName, String md5) { return takeFirst(80 - 33, fileNameEncode(fullName)) + " - " + md5; } public static String upper(String s) { return s == null ? null : s.toUpperCase(); } public static char upper(char c) { return Character.toUpperCase(c); } public static String loadTextFileFromZipFile(File inZip, String fileName) { try { if (!fileExists(inZip)) return null; try { ZipFile zip = new ZipFile(inZip); try { return loadTextFileFromZipFile(zip, fileName); } finally { _close(zip); } } catch (Throwable e) { throw fail(f2s(inZip), e); } } catch (Exception __e) { throw rethrow(__e); } } public static String loadTextFileFromZipFile(ZipFile zip, String fileName) { try { ZipEntry entry = zip.getEntry(fileName); if (entry == null) return null; InputStream fin = zip.getInputStream(entry); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); copyStream(fin, baos); return fromUTF8(baos.toByteArray()); } finally { _close(fin); } } catch (Exception __e) { throw rethrow(__e); } } public static File remoteMechListMirrorFile(String listName) { return newFile(remoteMechListMirrorsDir(), uniqueFileNameUsingMD5_80_v2(upper(listName))); } public static boolean serverMechList_raw_fresh_verbose; public static String serverMechList_raw_fresh(String name) { return serverMechList_raw_fresh(name, false); } public static String serverMechList_raw_fresh(String name, boolean opt) { Lock __2189 = downloadLock(); lock(__2189); try { String text = null; try { text = loadTextFile(remoteMechListMirrorFile(name)); } catch (Throwable __e) { _handleException(__e); } Object[] params = muricaCredentialsPlus("md5", md5OrNull(text), "l", l(text), "opt", opt ? 1 : 0, "withStatus", 1); String url = "http://butter.botcompany.de:8080/mech/raw/list-text/" + urlencode(name); String page = postPageSilently(url, params); Map map = jsonDecodeMap(page); boolean same = eq(map.get("Same"), true); boolean appended = eq(map.get("Appended"), true); saveTextFile(remoteMechListMirrorMetaFile(name), struct(getMultipleKeys(map, "Name", "Status"))); if (!same) { if (appended) text += (String) map.get("Text"); else text = (String) map.get("Text"); saveTextFile(remoteMechListMirrorFile(name), text); File nameFile = remoteMechListMirrorNameFile(name); if (!fileExists(nameFile)) { String actualName = or((String) map.get("Name"), name); saveTextFile(nameFile, actualName); } } if (serverMechList_raw_fresh_verbose) print("Mech list " + name + ": " + (appended ? "appended" : same ? "same" : "downloaded") + ": " + n2(countLines(text), "line")); if (!same) vmBus_send("remoteMechListMirrorChanged", name); return text; } finally { unlock(__2189); } } public static String appendNewLineIfNempty(String s) { return empty(s) ? "" : s + "\n"; } public static String localMechList_opt_raw_fresh(String name) { return unnull(loadTextFile(localMechListFile(name))); } public static Object swing(Object f) { return swingAndWait(f); } public static A swing(F0 f) { return (A) swingAndWait(f); } public static File javaxCachesDir_dir; public static File javaxCachesDir() { return javaxCachesDir_dir != null ? javaxCachesDir_dir : new File(userHome(), "JavaX-Caches"); } public static File javaxCachesDir(String sub) { return newFile(javaxCachesDir(), sub); } public static void saveTextFileUnlessExists(File f, String contents) { if (f != null && !f.exists()) saveTextFile(f, contents); } public static File newFile(File base, String... names) { for (String name : names) base = new File(base, name); return base; } public static File newFile(String name) { return name == null ? null : new File(name); } public static String lines(Iterable lines) { return fromLines(lines); } public static String lines(Object[] lines) { return fromLines(asList(lines)); } public static List lines(String s) { return toLines(s); } public static ThreadLocal> checkFileNotTooBigToRead_tl = new ThreadLocal(); public static void checkFileNotTooBigToRead(File f) { callF(checkFileNotTooBigToRead_tl.get(), f); } public static long recommendedTranspilationDateForStandardFunction(String sfName) { return toLong(mechMap("Recommended SF Transpilation Date").get(sfName)); } public static boolean setAdd(Collection c, A a) { if (c == null || c.contains(a)) return false; c.add(a); return true; } public static void clearTokens(List tok) { clearAllTokens(tok); } public static void clearTokens(List tok, int i, int j) { clearAllTokens(tok, i, j); } public static JavaXClassLoader hotwire_makeClassLoader(List files) { Collection toShare = hotwire_classesToShare(); return nempty(toShare) ? new JavaXClassLoaderWithParent2(null, files, myClassLoader(), cloneList(toShare)) : new JavaXClassLoader(null, files); } public static File saveTextFile(String fileName, String contents) throws IOException { CriticalAction action = beginCriticalAction("Saving file " + fileName + " (" + l(contents) + " chars)"); try { File file = new File(fileName); mkdirsForFile(file); String tempFileName = fileName + "_temp"; File tempFile = new File(tempFileName); if (contents != null) { if (tempFile.exists()) try { String saveName = tempFileName + ".saved." + now(); copyFile(tempFile, new File(saveName)); } catch (Throwable e) { printStackTrace(e); } FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath()); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8"); PrintWriter printWriter = new PrintWriter(outputStreamWriter); printWriter.print(contents); printWriter.close(); } if (file.exists() && !file.delete()) throw new IOException("Can't delete " + fileName); if (contents != null) if (!tempFile.renameTo(file)) throw new IOException("Can't rename " + tempFile + " to " + file); vmBus_send("wroteFile", file); return file; } finally { action.done(); } } public static File saveTextFile(File fileName, String contents) { try { saveTextFile(fileName.getPath(), contents); return fileName; } catch (Exception __e) { throw rethrow(__e); } } public static Set tok_returnTypesOfStaticFunction_uncleaned(List tok, String functionName) { List> funcs = findFullFunctionDefs(tok, true); TreeSet out = new TreeSet(); for (List tokF : funcs) { int i = indexOfAny(tokF, 0, "(", "{"); if (i < 0) continue; String fname = get(tokF, i - 2); if (!eq(fname, functionName)) continue; out.add(joinSubList(tokF, 1, i - 3)); } return out; } public static boolean containsOneOf(Collection l, A... x) { for (A a : unnull(l)) if (eqOneOf(a, x)) return true; return false; } public static boolean containsOneOf(Collection l, Set set) { if (set == null) return false; for (A a : unnull(l)) if (set.contains(a)) return true; return false; } public static boolean containsOneOf(String s, String... x) { for (String o : x) if (contains(s, o)) return true; return false; } public static String textOfStandardFunction(String sfName) { return loadSnippet_cached(stdFunctions_cached().get(sfName)); } public static List nlTok(String s) { return javaTokPlusPeriod(s); } public static char stringToChar(String s) { if (l(s) != 1) throw fail("bad stringToChar: " + s); return firstChar(s); } public static Class run(String progID, String... args) { Class main = hotwire(progID); callMain(main, args); return main; } public static void restart() { Object j = getJavaX(); call(j, "cleanRestart", get(j, "fullArgs")); } public static volatile boolean sleep_noSleep; public static void sleep(long ms) { ping(); if (ms < 0) return; if (isAWTThread() && ms > 100) throw fail("Should not sleep on AWT thread"); try { Thread.sleep(ms); } catch (Exception e) { throw new RuntimeException(e); } } public static void sleep() { try { if (sleep_noSleep) throw fail("nosleep"); print("Sleeping."); sleepQuietly(); } catch (Exception __e) { throw rethrow(__e); } } public static File loadBinarySnippet(String snippetID) { try { long id = parseSnippetID(snippetID); if (isImageServerSnippet(id)) return loadImageAsFile(snippetID); File f = DiskSnippetCache_getLibrary(id); if (fileSize(f) == 0) f = loadDataSnippetToFile(snippetID); return f; } catch (Exception __e) { throw rethrow(__e); } } public static void setOptIfNotNull(Object o, String field, Object value) { if (value != null) setOpt(o, field, value); } public static Object mainBot; public static Object getMainBot() { return mainBot; } public static void pcallFAll(Collection l, Object... args) { if (l != null) for (Object f : cloneList(l)) pcallF(f, args); } public static void pcallFAll(Iterator it, Object... args) { while (it.hasNext()) pcallF(it.next(), args); } public static String md5(String text) { try { if (text == null) return "-"; return bytesToHex(md5_impl(toUtf8(text))); } catch (Exception __e) { throw rethrow(__e); } } public static String md5(byte[] data) { if (data == null) return "-"; return bytesToHex(md5_impl(data)); } public static MessageDigest md5_md; public static byte[] md5_impl(byte[] data) { try { return MessageDigest.getInstance("MD5").digest(data); } catch (Exception __e) { throw rethrow(__e); } } public static String md5(File file) { return md5OfFile(file); } public static String fileNameEncode_safeChars = " ()[]#,!"; public static String fileNameEncode(String s) { StringBuilder buf = new StringBuilder(); int n = l(s); for (int i = 0; i < n; i++) { char c = s.charAt(i); if (contains(fileNameEncode_safeChars, c)) buf.append(c); else buf.append(urlencode(str(c))); } return str(buf); } public static boolean fileExists(String path) { return path != null && new File(path).exists(); } public static boolean fileExists(File f) { return f != null && f.exists(); } public static String f2s(File f) { return f == null ? null : f.getAbsolutePath(); } public static String f2s(java.nio.file.Path p) { return p == null ? null : f2s(p.toFile()); } public static void copyStream(InputStream in, OutputStream out) { try { byte[] buf = new byte[65536]; while (true) { int n = in.read(buf); if (n <= 0) return; out.write(buf, 0, n); } } catch (Exception __e) { throw rethrow(__e); } } public static String fromUTF8(byte[] bytes) { return fromUtf8(bytes); } public static File remoteMechListMirrorsDir() { return javaxDataDir("Remote Mech Lists"); } public static Lock downloadLock_lock = fairLock(); public static Lock downloadLock() { return downloadLock_lock; } public static Object[] muricaCredentialsPlus(Object... params) { return concatArrays(muricaCredentials(), params); } public static String md5OrNull(String s) { return s == null ? null : md5(s); } public static String urlencode(String x) { try { return URLEncoder.encode(unnull(x), "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } public static String postPageSilently(String url, Object... params) { return doPostSilently(litmap(params), url); } public static Map jsonDecodeMap(String s) { Object o = jsonDecode(s); if (o instanceof List && empty((List) o)) return new HashMap(); if (o instanceof Map) return (Map) o; else throw fail("Not a JSON map: " + s); } public static File remoteMechListMirrorMetaFile(String listName) { return javaxDataDir("Remote Mech Lists/" + uniqueFileNameUsingMD5_80_v2(upper(listName)) + ".meta"); } public static Map getMultipleKeys(Map map, A... keys) { Map map2 = similarEmptyMap(map); if (map != null) for (A key : keys) map2.put(key, map.get(key)); return map2; } public static File remoteMechListMirrorNameFile(String listName) { return javaxDataDir("Remote Mech Lists/" + uniqueFileNameUsingMD5_80_v2(upper(listName)) + ".name"); } public static int countLines(String s) { return l(toLines(s)); } public static void vmBus_send(String msg, Object... args) { Object arg = vmBus_wrapArgs(args); pcallFAll(vm_busListeners_live(), msg, arg); pcallFAll(vm_busListenersByMessage_live().get(msg), msg, arg); } public static void vmBus_send(String msg) { vmBus_send(msg, (Object) null); } public static File localMechListFile(String listName) { return newFile(localMechListsDir(), uniqueFileNameUsingMD5_80_v2(listName) + ".text"); } public static void swingAndWait(Runnable r) { try { if (isAWTThread()) r.run(); else EventQueue.invokeAndWait(addThreadInfoToRunnable(r)); } catch (Exception __e) { throw rethrow(__e); } } public static Object swingAndWait(final Object f) { if (isAWTThread()) return callF(f); else { final Var result = new Var(); swingAndWait(new Runnable() { public void run() { try { result.set(callF(f)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "result.set(callF(f));"; } }); return result.get(); } } public static String _userHome; public static String userHome() { if (_userHome == null) { if (isAndroid()) _userHome = "/storage/sdcard0/"; else _userHome = System.getProperty("user.home"); } return _userHome; } public static File userHome(String path) { return new File(userDir(), path); } public static String fromLines(Iterable lines) { StringBuilder buf = new StringBuilder(); if (lines != null) for (Object line : lines) buf.append(str(line)).append('\n'); return buf.toString(); } public static String fromLines(String... lines) { return fromLines(asList(lines)); } public static Map mechMap(String listName) { return mechMapCI(listName); } public static Set hotwire_classesToShare = synchroSet(); public static Set hotwire_classesToShare() { return hotwire_classesToShare; } public static ClassLoader myClassLoader() { return _getClass(mc()).getClassLoader(); } public static List beginCriticalAction_inFlight = synchroList(); public static class CriticalAction { public String description; public CriticalAction() { } public CriticalAction(String description) { this.description = description; } public void done() { beginCriticalAction_inFlight.remove(this); } } public static CriticalAction beginCriticalAction(String description) { ping(); CriticalAction c = new CriticalAction(description); beginCriticalAction_inFlight.add(c); return c; } public static void cleanMeUp_beginCriticalAction() { int n = 0; while (nempty(beginCriticalAction_inFlight)) { int m = l(beginCriticalAction_inFlight); if (m != n) { n = m; try { print("Waiting for " + n2(n, "critical actions") + ": " + join(", ", collect(beginCriticalAction_inFlight, "description"))); } catch (Throwable __e) { _handleException(__e); } } sleepInCleanUp(10); } } public static File mkdirsForFile(File file) { File dir = file.getParentFile(); if (dir != null) { dir.mkdirs(); if (!dir.isDirectory()) if (dir.isFile()) throw fail("Please delete the file " + f2s(dir) + " - it is supposed to be a directory!"); else throw fail("Unknown IO exception during mkdirs of " + f2s(file)); } return file; } public static String mkdirsForFile(String path) { mkdirsForFile(new File(path)); return path; } public static long now_virtualTime; public static long now() { return now_virtualTime != 0 ? now_virtualTime : System.currentTimeMillis(); } public static void copyFile(File src, File dest) { try { FileInputStream inputStream = new FileInputStream(src.getPath()); FileOutputStream outputStream = newFileOutputStream(dest.getPath()); try { copyStream(inputStream, outputStream); inputStream.close(); } finally { outputStream.close(); } } catch (Exception __e) { throw rethrow(__e); } } public static FileOutputStream newFileOutputStream(File path) throws IOException { return newFileOutputStream(path.getPath()); } public static FileOutputStream newFileOutputStream(String path) throws IOException { return newFileOutputStream(path, false); } public static FileOutputStream newFileOutputStream(File path, boolean append) throws IOException { return newFileOutputStream(path.getPath(), append); } public static FileOutputStream newFileOutputStream(String path, boolean append) throws IOException { mkdirsForFile(path); FileOutputStream f = new FileOutputStream(path, append); _registerIO(f, path, true); return f; } public static Set findFullFunctionDefs_keywords = new HashSet(splitAtSpace("static svoid ssvoid ssynchronized sbool sS sO sL")); public static List> findFullFunctionDefs(List tok, boolean topLevelOnly) { int n = l(tok); List> functions = new ArrayList(); for (int i = 1; i < n; i += 2) { String t = tok.get(i); if (topLevelOnly && eq(t, "{")) i = findEndOfBlock(tok, i) - 1; else if (findFullFunctionDefs_keywords.contains(t)) { int j = i + 2; while (j < n && !eqOneOf(tok.get(j), ";", "=", "(", "{")) j += 2; if ((eqGet(tok, j, "(") || eq(t, "svoid") && eqGet(tok, j, "{")) && isIdentifier(tok.get(j - 2))) { int k = smartIndexOf(tok, "{", j); if (k < l(tok)) { k = findEndOfBlock(tok, k) + 1; functions.add(subList(tok, i - 1, k)); i = k - 2; } } } } return functions; } public static List> findFullFunctionDefs(String s, boolean topLevelOnly) { return findFullFunctionDefs(javaTok(s), topLevelOnly); } public static int indexOfAny(List l, int i, A... x) { while (i < l(l)) if (eqOneOf(l.get(i), x)) return i; else ++i; return -1; } public static int indexOfAny(List l, Collection x) { return indexOfAny(l, 0, x); } public static int indexOfAny(List l, int i, Collection x) { if (nempty(x)) while (i < l(l)) if (x.contains(l.get(i))) return i; else ++i; return -1; } public static int indexOfAny(String s, int i, String chars) { for (; i < l(s); i++) if (chars.indexOf(s.charAt(i)) >= 0) return i; return -1; } public static String loadSnippet_cached(String id) { return loadSnippet_simpleCache(id); } public static Map stdFunctions_cached_map; public static Lock stdFunctions_cached_lock = lock(); public static Map stdFunctions_cached() { Lock __1045 = stdFunctions_cached_lock; lock(__1045); try { if (stdFunctions_cached_map == null) stdFunctions_cached_map = stdFunctions_uncached(); return stdFunctions_cached_map; } finally { unlock(__1045); } } public static synchronized void stdFunctions_clearCache() { stdFunctions_cached_map = null; } public static char firstChar(String s) { return s.charAt(0); } public static A callMain(A c, String... args) { callOpt(c, "main", new Object[] { args }); return c; } public static void callMain() { callMain(mc()); } public static Object sleepQuietly_monitor = new Object(); public static void sleepQuietly() { try { assertFalse(isAWTThread()); synchronized (sleepQuietly_monitor) { sleepQuietly_monitor.wait(); } } catch (Exception __e) { throw rethrow(__e); } } public static boolean isImageServerSnippet(long id) { return id >= 1100000 && id < 1200000; } public static File loadImageAsFile(String snippetIDOrURL) { try { if (isURL(snippetIDOrURL)) throw fail("not implemented"); if (!isSnippetID(snippetIDOrURL)) throw fail("Not a URL or snippet ID: " + snippetIDOrURL); String snippetID = "" + parseSnippetID(snippetIDOrURL); File dir = imageSnippetsCacheDir(); if (loadBufferedImage_useImageCache) { dir.mkdirs(); File file = new File(dir, snippetID + ".png"); if (file.exists() && file.length() != 0) return file; } String imageURL = snippetImageURL_noHttps(snippetID); System.err.println("Loading image: " + imageURL); byte[] data = loadBinaryPage(imageURL); File file = new File(dir, snippetID + ".png"); saveBinaryFile(file, data); return file; } catch (Exception __e) { throw rethrow(__e); } } public static File DiskSnippetCache_file(long snippetID) { return new File(getGlobalCache(), "data_" + snippetID + ".jar"); } public static File DiskSnippetCache_getLibrary(long snippetID) throws IOException { File file = DiskSnippetCache_file(snippetID); return file.exists() ? file : null; } public static void DiskSnippetCache_putLibrary(long snippetID, byte[] data) throws IOException { saveBinaryFile(DiskSnippetCache_file(snippetID), data); } public static byte[] loadDataSnippetImpl(String snippetID) throws IOException { byte[] data; try { URL url = new URL(dataSnippetLink(snippetID)); print("Loading library: " + hideCredentials(url)); try { data = loadBinaryPage(url.openConnection()); } catch (RuntimeException e) { data = null; } if (data == null || data.length == 0) { url = new URL("http://data.tinybrain.de/blobs/" + parseSnippetID(snippetID)); print("Loading library: " + hideCredentials(url)); data = loadBinaryPage(url.openConnection()); } print("Bytes loaded: " + data.length); } catch (FileNotFoundException e) { throw new IOException("Binary snippet #" + snippetID + " not found or not public"); } return data; } public static long fileSize(String path) { return getFileSize(path); } public static long fileSize(File f) { return getFileSize(f); } public static File loadDataSnippetToFile(String snippetID) { try { snippetID = fsI(snippetID); File f = DiskSnippetCache_file(parseSnippetID(snippetID)); List urlsTried = new ArrayList(); List errors = new ArrayList(); try { URL url = addAndReturn(urlsTried, new URL(dataSnippetLink(snippetID))); print("Loading library: " + hideCredentials(url)); try { loadBinaryPageToFile(openConnection(url), f); if (fileSize(f) == 0) throw fail(); } catch (Throwable e) { errors.add(e); url = addAndReturn(urlsTried, new URL("http://data.tinybrain.de/blobs/" + psI(snippetID))); print("Trying other server: " + hideCredentials(url)); loadBinaryPageToFile(openConnection(url), f); print("Got bytes: " + fileSize(f)); } if (fileSize(f) == 0) throw fail(); System.err.println("Bytes loaded: " + fileSize(f)); } catch (Throwable e) { printStackTrace(e); errors.add(e); throw fail("Binary snippet " + snippetID + " not found or not public. URLs tried: " + allToString(urlsTried) + ", errors: " + allToString(errors)); } return f; } catch (Exception __e) { throw rethrow(__e); } } public static String bytesToHex(byte[] bytes) { return bytesToHex(bytes, 0, bytes.length); } public static String bytesToHex(byte[] bytes, int ofs, int len) { StringBuilder stringBuilder = new StringBuilder(len * 2); for (int i = 0; i < len; i++) { String s = "0" + Integer.toHexString(bytes[ofs + i]); stringBuilder.append(s.substring(s.length() - 2, s.length())); } return stringBuilder.toString(); } public static byte[] toUtf8(String s) { try { return s.getBytes("UTF-8"); } catch (Exception __e) { throw rethrow(__e); } } public static boolean md5OfFile_verbose; public static String md5OfFile(String path) { return md5OfFile(newFile(path)); } public static String md5OfFile(File f) { try { if (!f.exists()) return "-"; if (md5OfFile_verbose) print("Getting MD5 of " + f); MessageDigest md5 = MessageDigest.getInstance("MD5"); FileInputStream in = new FileInputStream(f); try { byte[] buf = new byte[65536]; int l; while (true) { l = in.read(buf); if (l <= 0) break; md5.update(buf, 0, l); } return bytesToHex(md5.digest()); } finally { _close(in); } } catch (Exception __e) { throw rethrow(__e); } } public static String fromUtf8(byte[] bytes) { try { return new String(bytes, "UTF-8"); } catch (Exception __e) { throw rethrow(__e); } } public static File javaxDataDir_dir; public static File javaxDataDir() { return javaxDataDir_dir != null ? javaxDataDir_dir : new File(userHome(), "JavaX-Data"); } public static File javaxDataDir(String... subs) { return newFile(javaxDataDir(), subs); } public static Object[] concatArrays(Object[]... arrays) { int l = 0; for (Object[] a : arrays) l += l(a); Object[] x = new Object[l]; int i = 0; for (Object[] a : arrays) if (a != null) { System.arraycopy(a, 0, x, i, l(a)); i += l(a); } return x; } public static Object[] muricaCredentials() { String pass = muricaPassword(); return nempty(pass) ? new Object[] { "_pass", pass } : new Object[0]; } public static String doPostSilently(Map urlParameters, String url) { return doPostSilently(makePostData(urlParameters), url); } public static String doPostSilently(String urlParameters, String url) { doPost_silently.set(true); return doPost(urlParameters, url); } public static boolean jsonDecode_useOrderedMaps = true; public static Object jsonDecode(final String text) { final List tok = jsonTok(text); if (l(tok) == 1) return null; class Y { public int i = 1; public Object parse() { String t = tok.get(i); if (t.startsWith("\"")) { String s = unquote(tok.get(i)); i += 2; return s; } if (t.equals("{")) return parseMap(); if (t.equals("[")) return this.parseList(); if (t.equals("null")) { i += 2; return null; } if (t.equals("false")) { i += 2; return false; } if (t.equals("true")) { i += 2; return true; } boolean minus = false; if (t.equals("-")) { minus = true; i += 2; t = get(tok, i); } if (isInteger(t)) { i += 2; if (eq(get(tok, i), ".")) { String x = t + "." + get(tok, i + 2); i += 4; double d = parseDouble(x); if (minus) d = -d; return d; } else { long l = parseLong(t); if (minus) l = -l; return l != (int) l ? new Long(l) : new Integer((int) l); } } throw new RuntimeException("Unknown token " + (i + 1) + ": " + t + ": " + text); } public Object parseList() { consume("["); List list = new ArrayList(); while (!tok.get(i).equals("]")) { list.add(parse()); if (tok.get(i).equals(",")) i += 2; } consume("]"); return list; } public Object parseMap() { consume("{"); Map map = jsonDecode_useOrderedMaps ? new LinkedHashMap() : new TreeMap(); while (!tok.get(i).equals("}")) { String key = unquote(tok.get(i)); i += 2; consume(":"); Object value = parse(); map.put(key, value); if (tok.get(i).equals(",")) i += 2; } consume("}"); return map; } public void consume(String s) { if (!tok.get(i).equals(s)) { String prevToken = i - 2 >= 0 ? tok.get(i - 2) : ""; String nextTokens = join(tok.subList(i, Math.min(i + 4, tok.size()))); throw fail(quote(s) + " expected: " + prevToken + " " + nextTokens + " (" + i + "/" + tok.size() + ")"); } i += 2; } } return new Y().parse(); } public static File localMechListsDir() { return javaxDataDir("Mech Lists"); } public static Runnable addThreadInfoToRunnable(final Object r) { final Object info = _threadInfo(); return info == null ? asRunnable(r) : new Runnable() { public void run() { try { _inheritThreadInfo(info); callF(r); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "_inheritThreadInfo(info); callF(r);"; } }; } public static File userDir() { return new File(userHome()); } public static File userDir(String path) { return new File(userHome(), path); } public static Map>> mechMapCI_cache = synchroMap(); public static Map mechMapCI(String listName) { Pair> p = mechMapCI_cache.get(listName); String text = mechList_raw(listName); if (p != null && eq(p.a, text)) return p.b; String parsing = first(mechList_opt(listName + " | Parsing")); Map map; if (parsing == null) map = parseDoubleArrowMapCI(tlft(text)); else map = asCIMap((Map) callAndMake(parsing, text)); p = pair(text, map); mechMapCI_cache.put(listName, p); return p.b; } public static Set synchroSet() { return synchroHashSet(); } public static Set synchroSet(Set set) { return Collections.synchronizedSet(set); } public static void sleepInCleanUp(long ms) { try { if (ms < 0) return; Thread.sleep(ms); } catch (Exception __e) { throw rethrow(__e); } } public static List splitAtSpace(String s) { return empty(s) ? emptyList() : asList(s.split("\\s+")); } public static int findEndOfBlock(List cnc, int i) { int j = i + 2, level = 1, n = cnc.size(); while (j < n) { String t = cnc.get(j); if ("{".equals(t)) ++level; else if ("}".equals(t)) --level; if (level == 0) return j + 1; j += 2; } return n; } public static ExpiringMap2 loadSnippet_simpleCache_map = new ExpiringMap2(10000); public static Lock loadSnippet_simpleCache_lock = lock(); public static int loadSnippet_simpleCache_timeout = 60000; public static String loadSnippet_simpleCache(String id) { if (id == null) return null; Lock __264 = loadSnippet_simpleCache_lock; lock(__264); try { id = fsI(id); String src = loadSnippet_simpleCache_map.get(id); if (src != null) return src; Integer oldTimeout = setThreadLocal(loadPage_forcedTimeout_byThread, loadSnippet_simpleCache_timeout); try { src = loadSnippet(id); if (src != null) loadSnippet_simpleCache_map.put(id, src); return src; } finally { loadPage_forcedTimeout_byThread.set(oldTimeout); } } finally { unlock(__264); } } public static Map stdFunctions_uncached() { return stdFunctions_uncached(new HashMap()); } public static Map stdFunctions_uncached(Map map) { parseStdFunctionsList(loadSnippetSilently("#1006654"), map); parseStdFunctionsList(loadSnippetSilently("#761"), map); return map; } public static boolean loadBufferedImage_useImageCache = true; public static BufferedImage loadBufferedImage(String snippetIDOrURLOrFile) { try { if (snippetIDOrURLOrFile == null) return null; if (isURL(snippetIDOrURLOrFile)) return imageIO_readURL(snippetIDOrURLOrFile); if (isAbsolutePath(snippetIDOrURLOrFile)) return loadBufferedImage(new File(snippetIDOrURLOrFile)); if (!isSnippetID(snippetIDOrURLOrFile)) throw fail("Not a URL or snippet ID or file: " + snippetIDOrURLOrFile); String snippetID = "" + parseSnippetID(snippetIDOrURLOrFile); File dir = imageSnippetsCacheDir(); if (loadBufferedImage_useImageCache) { dir.mkdirs(); File file = new File(dir, snippetID + ".png"); if (file.exists() && file.length() != 0) try { return ImageIO.read(file); } catch (Throwable e) { e.printStackTrace(); } } String imageURL = snippetImageURL_http(snippetID); print("Loading image: " + imageURL); BufferedImage image = imageIO_readURL(imageURL); if (loadBufferedImage_useImageCache) { File tempFile = new File(dir, snippetID + ".tmp." + System.currentTimeMillis()); ImageIO.write(image, "png", tempFile); tempFile.renameTo(new File(dir, snippetID + ".png")); } return image; } catch (Exception __e) { throw rethrow(__e); } } public static BufferedImage loadBufferedImage(File file) { try { return file.isFile() ? ImageIO.read(file) : null; } catch (Exception __e) { throw rethrow(__e); } } public static Map parseDoubleLeftArrowMapCI_tlft(String s) { return parseDoubleLeftArrowMapCI(tlft(s)); } public static boolean isURL(String s) { return startsWithOneOf(s, "http://", "https://", "file:"); } public static File imageSnippetsCacheDir() { return javaxCachesDir("Image-Snippets"); } public static String snippetImageURL_noHttps(String snippetID) { return snippetImageURL_noHttps(snippetID, "png"); } public static String snippetImageURL_noHttps(String snippetID, String contentType) { return snippetImageURL(snippetID, contentType).replace("https://www.botcompany.de:8443/", "http://www.botcompany.de:8080/").replace("https://botcompany.de/", "http://botcompany.de/"); } public static ThreadLocal>> loadBinaryPage_responseHeaders = new ThreadLocal(); public static ThreadLocal> loadBinaryPage_extraHeaders = new ThreadLocal(); public static byte[] loadBinaryPage(String url) { try { print("Loading " + url); return loadBinaryPage(loadPage_openConnection(new URL(url))); } catch (Exception __e) { throw rethrow(__e); } } public static byte[] loadBinaryPage(URLConnection con) { try { Map extraHeaders = getAndClearThreadLocal(loadBinaryPage_extraHeaders); setHeaders(con); for (String key : keys(extraHeaders)) con.setRequestProperty(key, extraHeaders.get(key)); return loadBinaryPage_noHeaders(con); } catch (Exception __e) { throw rethrow(__e); } } public static byte[] loadBinaryPage_noHeaders(URLConnection con) { try { ByteArrayOutputStream buf = new ByteArrayOutputStream(); InputStream inputStream = con.getInputStream(); loadBinaryPage_responseHeaders.set(con.getHeaderFields()); long len = 0; try { len = con.getContentLength(); } catch (Throwable e) { printStackTrace(e); } int n = 0; while (true) { int ch = inputStream.read(); if (ch < 0) break; buf.write(ch); if (++n % 100000 == 0) println(" " + n + (len != 0 ? "/" + len : "") + " bytes loaded."); } inputStream.close(); return buf.toByteArray(); } catch (Exception __e) { throw rethrow(__e); } } public static byte[] saveBinaryFile(String fileName, byte[] contents) { try { File file = new File(fileName); File parentFile = file.getParentFile(); if (parentFile != null) parentFile.mkdirs(); String tempFileName = fileName + "_temp"; FileOutputStream fileOutputStream = newFileOutputStream(tempFileName); fileOutputStream.write(contents); fileOutputStream.close(); if (file.exists() && !file.delete()) throw new IOException("Can't delete " + fileName); if (!new File(tempFileName).renameTo(file)) throw new IOException("Can't rename " + tempFileName + " to " + fileName); vmBus_send("wroteFile", file); return contents; } catch (Exception __e) { throw rethrow(__e); } } public static byte[] saveBinaryFile(File fileName, byte[] contents) { return saveBinaryFile(fileName.getPath(), contents); } public static File getGlobalCache() { File file = new File(javaxCachesDir(), "Binary Snippets"); file.mkdirs(); return file; } public static String dataSnippetLink(String snippetID) { long id = parseSnippetID(snippetID); if (id >= 1100000 && id < 1200000) return imageServerURL() + id; if (id >= 1400000 && id < 1500000) return "http://butter.botcompany.de:8080/files/" + id + "?_pass=" + muricaPassword(); if (id >= 1200000 && id < 1300000) { String pw = muricaPassword(); if (empty(pw)) throw fail("Please set 'murica password by running #1008829"); return "http://butter.botcompany.de:8080/1008823/raw/" + id + "?_pass=" + pw; } else return "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_" + id + "&contentType=application/binary"; } public static String fsI(String id) { return formatSnippetID(id); } public static String fsI(long id) { return formatSnippetID(id); } public static A addAndReturn(Collection c, A a) { if (c != null) c.add(a); return a; } public static void loadBinaryPageToFile(String url, File file) { try { print("Loading " + url); loadBinaryPageToFile(openConnection(new URL(url)), file); } catch (Exception __e) { throw rethrow(__e); } } public static void loadBinaryPageToFile(URLConnection con, File file) { try { setHeaders(con); loadBinaryPageToFile_noHeaders(con, file); } catch (Exception __e) { throw rethrow(__e); } } public static void loadBinaryPageToFile_noHeaders(URLConnection con, File file) { try { File ftemp = new File(f2s(file) + "_temp"); FileOutputStream buf = newFileOutputStream(mkdirsFor(ftemp)); try { InputStream inputStream = con.getInputStream(); long len = 0; try { len = con.getContentLength(); } catch (Throwable e) { printStackTrace(e); } String pat = " {*}" + (len != 0 ? "/" + len : "") + " bytes loaded."; copyStreamWithPrints(inputStream, buf, pat); inputStream.close(); buf.close(); file.delete(); renameFile_assertTrue(ftemp, file); } finally { if (buf != null) buf.close(); } } catch (Exception __e) { throw rethrow(__e); } } public static URLConnection openConnection(URL url) { try { ping(); callOpt(javax(), "recordOpenURLConnection", str(url)); return url.openConnection(); } catch (Exception __e) { throw rethrow(__e); } } public static long psI(String snippetID) { return parseSnippetID(snippetID); } public static volatile boolean muricaPassword_pretendNotAuthed; public static String muricaPassword() { if (muricaPassword_pretendNotAuthed) return null; return trim(loadTextFile(muricaPasswordFile())); } public static String makePostData(Map map) { List l = new ArrayList(); for (Map.Entry e : map.entrySet()) { String key = (String) (e.getKey()); Object val = e.getValue(); if (val != null) { String value = str(val); l.add(urlencode(key) + "=" + urlencode((value))); } } return join("&", l); } public static String makePostData(Object... params) { return makePostData(litorderedmap(params)); } public static ThreadLocal doPost_silently = new ThreadLocal(); public static ThreadLocal doPost_timeout = new ThreadLocal(); public static String doPost(Map urlParameters, String url) { return doPost(makePostData(urlParameters), url); } public static String doPost(String urlParameters, String url) { try { URL _url = new URL(url); ping(); return doPost(urlParameters, _url.openConnection(), _url); } catch (Exception __e) { throw rethrow(__e); } } public static String doPost(String urlParameters, URLConnection conn, URL url) { try { boolean silently = isTrue(optParam(doPost_silently)); Long timeout = optParam(doPost_timeout); setHeaders(conn); int l = lUtf8(urlParameters); if (!silently) print("Sending POST request: " + hideCredentials(url) + " (" + l + " bytes)"); if (timeout != null) setURLConnectionTimeouts(conn, timeout); ((HttpURLConnection) conn).setRequestMethod("POST"); conn.setDoOutput(true); conn.setRequestProperty("Content-Length", str(l)); OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); writer.write(urlParameters); writer.flush(); String contents = loadPage_utf8(conn, url, false); writer.close(); return contents; } catch (Exception __e) { throw rethrow(__e); } } public static List jsonTok(String s) { List tok = new ArrayList(); int l = l(s); int i = 0; while (i < l) { int j = i; char c; String cc; while (j < l) { c = s.charAt(j); cc = s.substring(j, Math.min(j + 2, l)); if (c == ' ' || c == '\t' || c == '\r' || c == '\n') ++j; else if (cc.equals("/*")) { do ++j; while (j < l && !s.substring(j, Math.min(j + 2, l)).equals("*/")); j = Math.min(j + 2, l); } else if (cc.equals("//")) { do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0); } else break; } tok.add(s.substring(i, j)); i = j; if (i >= l) break; c = s.charAt(i); if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { if (s.charAt(j) == opener) { ++j; break; } else if (s.charAt(j) == '\\' && j + 1 < l) j += 2; else ++j; } } else if (Character.isLetter(c)) do ++j; while (j < l && Character.isLetter(s.charAt(j))); else if (Character.isDigit(c)) do ++j; while (j < l && Character.isDigit(s.charAt(j))); else ++j; tok.add(s.substring(i, j)); i = j; } if ((tok.size() % 2) == 0) tok.add(""); return tok; } public static Runnable asRunnable(Object o) { return toRunnable(o); } public static void _inheritThreadInfo(Object info) { _threadInheritInfo(info); } public static Map parseDoubleArrowMapCI(List l) { return parseDoubleArrowMap((Map) ciMap(), l); } public static Map parseStdFunctionsList(String snippetSrc) { return parseStdFunctionsList(snippetSrc, new LinkedHashMap()); } public static Map parseStdFunctionsList(String snippetSrc, Map map) { List tok = javaTok(snippetSrc); int i = findCodeTokens(tok, "standardFunctions", "=", "litlist", "("); int opening = i + 6; int closing = indexOf(tok, ")", opening) - 1; for (i = opening + 2; i < closing; i += 4) { String[] f = unquote(tok.get(i)).split("/"); map.put(f[1], f[0]); } return map; } public static String loadSnippetSilently(Snippet s) { return loadSnippetQuietly(s); } public static String loadSnippetSilently(String snippetID) { return loadSnippetQuietly(snippetID); } public static BufferedImage imageIO_readURL(String url) { try { if (startsWith(url, "https:")) disableCertificateValidation(); return ImageIO.read(new URL(url)); } catch (Exception __e) { throw rethrow(__e); } } public static boolean isAbsolutePath(String s) { return s != null && new File(s).isAbsolute(); } public static boolean isAbsolutePath(File f) { return f != null && f.isAbsolute(); } public static String snippetImageURL_http(String snippetID) { return snippetImageURL_http(snippetID, "png"); } public static String snippetImageURL_http(String snippetID, String contentType) { return replacePrefix("https://", "http://", snippetImageURL(snippetID, contentType)).replace(":8443", ":8080"); } public static Map parseDoubleLeftArrowMapCI(List l) { return parseDoubleLeftArrowMap((Map) ciMap(), l); } public static boolean startsWithOneOf(String s, String... l) { for (String x : l) if (startsWith(s, x)) return true; return false; } public static String snippetImageURL(String snippetID) { return snippetImageURL(snippetID, "png"); } public static String snippetImageURL(String snippetID, String contentType) { if (isURL(snippetID)) return snippetID; long id = parseSnippetID(snippetID); String url; if (id == 1000010 || id == 1000012) url = "http://tinybrain.de:8080/tb/show-blobimage.php?id=" + id; else if (isImageServerSnippet(id)) url = imageServerLink(id); else url = "https://www.botcompany.de:8443/img/" + id; return url; } public static int loadPage_defaultTimeout = 60000; public static ThreadLocal loadPage_charset = new ThreadLocal(); public static boolean loadPage_allowGzip = true, loadPage_debug; public static boolean loadPage_anonymous; public static int loadPage_verboseness = 100000; public static int loadPage_retries = 1; public static ThreadLocal loadPage_silent = new ThreadLocal(); public static volatile int loadPage_forcedTimeout; public static ThreadLocal loadPage_forcedTimeout_byThread = new ThreadLocal(); public static ThreadLocal>> loadPage_responseHeaders = new ThreadLocal(); public static ThreadLocal> loadPage_extraHeaders = new ThreadLocal(); public static String loadPageSilently(String url) { try { return loadPageSilently(new URL(loadPage_preprocess(url))); } catch (Exception __e) { throw rethrow(__e); } } public static String loadPageSilently(URL url) { try { if (url.getProtocol().equals("https")) disableCertificateValidation(); if (!networkAllowanceTest(str(url))) throw fail("Not allowed: " + url); IOException e = null; for (int tries = 0; tries < loadPage_retries; tries++) try { URLConnection con = loadPage_openConnection(url); return loadPage(con, url); } catch (IOException _e) { e = _e; if (loadPageThroughProxy_enabled) { print("Trying proxy because of: " + e); try { return loadPageThroughProxy(str(url)); } catch (Throwable e2) { print(" " + exceptionToStringShort(e2)); } } else if (loadPage_debug) print(exceptionToStringShort(e)); if (tries < loadPage_retries - 1) sleepSeconds(1); } throw e; } catch (Exception __e) { throw rethrow(__e); } } public static String loadPage_preprocess(String url) { if (url.startsWith("tb/")) url = tb_mainServer() + "/" + url; if (url.indexOf("://") < 0) url = "http://" + url; return url; } public static String loadPage(String url) { try { url = loadPage_preprocess(url); if (!isTrue(loadPage_silent.get())) printWithTime("Loading: " + hideCredentials(url)); return loadPageSilently(new URL(url)); } catch (Exception __e) { throw rethrow(__e); } } public static String loadPage(URL url) { return loadPage(url.toExternalForm()); } public static String loadPage(URLConnection con, URL url) throws IOException { return loadPage(con, url, true); } public static String loadPage(URLConnection con, URL url, boolean addHeaders) throws IOException { Map extraHeaders = getAndClearThreadLocal(loadPage_extraHeaders); if (addHeaders) try { if (!loadPage_anonymous) setHeaders(con); if (loadPage_allowGzip) con.setRequestProperty("Accept-Encoding", "gzip"); con.setRequestProperty("X-No-Cookies", "1"); for (String key : keys(extraHeaders)) con.setRequestProperty(key, extraHeaders.get(key)); } catch (Throwable e) { } vm_generalSubMap("URLConnection per thread").put(currentThread(), con); loadPage_responseHeaders.set(con.getHeaderFields()); InputStream in = null; try { in = con.getInputStream(); if (loadPage_debug) print("Put stream in map: " + currentThread()); String contentType = con.getContentType(); if (contentType == null) { throw new IOException("Page could not be read: " + hideCredentials(url)); } String charset = loadPage_charset == null ? null : loadPage_charset.get(); if (charset == null) charset = loadPage_guessCharset(contentType); if ("gzip".equals(con.getContentEncoding())) { if (loadPage_debug) print("loadPage: Using gzip."); in = newGZIPInputStream(in); } Reader r; try { r = new InputStreamReader(in, unquote(charset)); } catch (UnsupportedEncodingException e) { print(toHex(utf8(charset))); throw e; } StringBuilder buf = new StringBuilder(); int n = 0; while (true) { int ch = r.read(); if (ch < 0) break; buf.append((char) ch); ++n; if ((n % loadPage_verboseness) == 0) print(" " + n + " chars read"); } return buf.toString(); } finally { if (loadPage_debug) print("loadPage done"); vm_generalSubMap("URLConnection per thread").remove(currentThread()); if (in != null) in.close(); } } public static String loadPage_guessCharset(String contentType) { Matcher m = regexpMatcher("text/[a-z]+;\\s*charset=([^\\s]+)\\s*", contentType); String match = m.matches() ? m.group(1) : null; if (loadPage_debug) print("loadPage: contentType=" + contentType + ", match: " + match); return or(match, "UTF-8"); } public static URLConnection loadPage_openConnection(URL url) { URLConnection con = openConnection(url); int timeout = toInt(loadPage_forcedTimeout_byThread.get()); if (timeout == 0) timeout = loadPage_forcedTimeout; if (timeout != 0) setURLConnectionTimeouts(con, loadPage_forcedTimeout); else setURLConnectionDefaultTimeouts(con, loadPage_defaultTimeout); return con; } public static void setHeaders(URLConnection con) throws IOException { String computerID = getComputerID_quick(); if (computerID != null) try { con.setRequestProperty("X-ComputerID", computerID); con.setRequestProperty("X-OS", System.getProperty("os.name") + " " + System.getProperty("os.version")); } catch (Throwable e) { } } public static A println(A a) { return print(a); } public static String imageServerURL() { return or2(trim(loadTextFile(javaxDataDir("image-server-url.txt"))), "http://botcompany.de/images/raw/"); } public static File mkdirsFor(File file) { return mkdirsForFile(file); } public static void copyStreamWithPrints(InputStream in, OutputStream out, String pat) { try { byte[] buf = new byte[65536]; int total = 0; while (true) { int n = in.read(buf); if (n <= 0) return; out.write(buf, 0, n); if ((total + n) / 100000 > total / 100000) print(pat.replace("{*}", str(roundDownTo(total, 100000)))); total += n; } } catch (Exception __e) { throw rethrow(__e); } } public static void renameFile_assertTrue(File a, File b) { try { if (!a.exists()) throw fail("Source file not found: " + f2s(a)); if (b.exists()) throw fail("Target file exists: " + f2s(b)); mkdirsForFile(b); if (!a.renameTo(b)) throw fail("Can't rename " + f2s(a) + " to " + f2s(b)); } catch (Exception __e) { throw rethrow(__e); } } public static File muricaPasswordFile() { return new File(javaxSecretDir(), "murica/muricaPasswordFile"); } public static LinkedHashMap litorderedmap(Object... x) { LinkedHashMap map = new LinkedHashMap(); litmap_impl(map, x); return map; } public static int lUtf8(String s) { return l(utf8(s)); } public static URLConnection setURLConnectionTimeouts(URLConnection con, long timeout) { con.setConnectTimeout(toInt(timeout)); con.setReadTimeout(toInt(timeout)); if (con.getConnectTimeout() != timeout || con.getReadTimeout() != timeout) print("Warning: Timeouts not set by JDK."); return con; } public static Runnable toRunnable(final Object o) { if (o instanceof Runnable) return (Runnable) o; return new Runnable() { public void run() { try { callF(o); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callF(o)"; } }; } public static volatile boolean disableCertificateValidation_attempted; public static void disableCertificateValidation() { try { if (disableCertificateValidation_attempted) return; disableCertificateValidation_attempted = true; try { TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } public void checkClientTrusted(X509Certificate[] certs, String authType) { } public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }; HostnameVerifier hv = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }; SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); HttpsURLConnection.setDefaultHostnameVerifier(hv); } catch (Throwable __e) { _handleException(__e); } } catch (Exception __e) { throw rethrow(__e); } } public static Map parseDoubleLeftArrowMap(Map map, List l) { for (String s : l) { List x = splitAtDoubleLeftArrow(s); if (l(x) == 2) map.put(second(x), first(x)); } return map; } public static String imageServerLink(String md5OrID) { if (possibleMD5(md5OrID)) return "https://botcompany.de/images/md5/" + md5OrID; return imageServerLink(parseSnippetID(md5OrID)); } public static String imageServerLink(long id) { return "https://botcompany.de/images/" + id; } public static boolean networkAllowanceTest(String url) { return isAllowed("networkAllowanceTest", url); } public static final boolean loadPageThroughProxy_enabled = false; public static String loadPageThroughProxy(String url) { return null; } public static void sleepSeconds(double s) { if (s > 0) sleep(round(s * 1000)); } public static String tb_mainServer_default = "http://tinybrain.de:8080"; public static Object tb_mainServer_override; public static String tb_mainServer() { if (tb_mainServer_override != null) return (String) callF(tb_mainServer_override); return trim(loadTextFile(tb_mainServer_file(), tb_mainServer_default)); } public static File tb_mainServer_file() { return getProgramFile("#1001638", "mainserver.txt"); } public static boolean tb_mainServer_isDefault() { return eq(tb_mainServer(), tb_mainServer_default); } public static A printWithTime(A a) { print(hmsWithColons() + ": " + a); return a; } public static Map vm_generalSubMap(Object name) { synchronized (get(javax(), "generalMap")) { Map map = (Map) (vm_generalMap_get(name)); if (map == null) vm_generalMap_put(name, map = synchroMap()); return map; } } public static String toHex(byte[] bytes) { return bytesToHex(bytes); } public static String toHex(byte[] bytes, int ofs, int len) { return bytesToHex(bytes, ofs, len); } public static byte[] utf8(String s) { return toUtf8(s); } public static Matcher regexpMatcher(String pat, String s) { return compileRegexp(pat).matcher(unnull(s)); } public static URLConnection setURLConnectionDefaultTimeouts(URLConnection con, long timeout) { if (con.getConnectTimeout() == 0) { con.setConnectTimeout(toInt(timeout)); if (con.getConnectTimeout() != timeout) print("Warning: URL connect timeout not set by JDK."); } if (con.getReadTimeout() == 0) { con.setReadTimeout(toInt(timeout)); if (con.getReadTimeout() != timeout) print("Warning: URL read timeout not set by JDK."); } return con; } public static String getComputerID_quick() { return computerID(); } public static int roundDownTo(int x, int n) { return x / n * n; } public static long roundDownTo(long x, long n) { return x / n * n; } public static File javaxSecretDir_dir; public static File javaxSecretDir() { return javaxSecretDir_dir != null ? javaxSecretDir_dir : new File(userHome(), "JavaX-Secret"); } public static File javaxSecretDir(String sub) { return newFile(javaxSecretDir(), sub); } public static boolean possibleMD5(String s) { return isMD5(s); } public static volatile Object isAllowed_function; public static volatile boolean isAllowed_all = true; public static boolean isAllowed(String askingMethod, Object... args) { Object f = vm_generalMap_get("isAllowed_function"); if (f != null && !isTrue(callF(f, askingMethod, args))) return false; return isAllowed_all || isTrue(callF(isAllowed_function, askingMethod, args)); } public static long round(double d) { return Math.round(d); } public static File getProgramFile(String progID, String fileName) { if (new File(fileName).isAbsolute()) return new File(fileName); return new File(getProgramDir(progID), fileName); } public static File getProgramFile(String fileName) { return getProgramFile(getProgramID(), fileName); } public static String hmsWithColons() { return hmsWithColons(now()); } public static String hmsWithColons(long time) { return new SimpleDateFormat("HH:mm:ss").format(time); } public static Object vm_generalMap_put(Object key, Object value) { return mapPutOrRemove(vm_generalMap(), key, value); } public static String _computerID; public static Lock computerID_lock = lock(); public static String computerID() { if (_computerID == null) { Lock __1914 = computerID_lock; lock(__1914); try { if (_computerID != null) return _computerID; File file = computerIDFile(); _computerID = loadTextFile(file.getPath()); if (_computerID == null) { _computerID = loadTextFile(userDir(".tinybrain/computer-id")); if (_computerID == null) _computerID = makeRandomID(12, new SecureRandom()); saveTextFile(file, _computerID); } } finally { unlock(__1914); } } return _computerID; } public static boolean isMD5(String s) { return l(s) == 32 && isLowerHexString(s); } public static File getProgramDir() { return programDir(); } public static File getProgramDir(String snippetID) { return programDir(snippetID); } public static B mapPutOrRemove(Map map, A key, B value) { if (map != null && key != null) if (value != null) return map.put(key, value); else return map.remove(key); return null; } public static File computerIDFile() { return javaxDataDir("Basic Info/computer-id.txt"); } public static String makeRandomID(int length) { return makeRandomID(length, defaultRandomGenerator()); } public static String makeRandomID(int length, Random random) { char[] id = new char[length]; for (int i = 0; i < id.length; i++) id[i] = (char) ((int) 'a' + random.nextInt(26)); return new String(id); } public static boolean isLowerHexString(String s) { for (int i = 0; i < l(s); i++) { char c = s.charAt(i); if (c >= '0' && c <= '9' || c >= 'a' && c <= 'f') { } else return false; } return true; } public static File programDir_mine; public static File programDir() { return programDir(getProgramID()); } public static File programDir(String snippetID) { boolean me = sameSnippetID(snippetID, programID()); if (programDir_mine != null && me) return programDir_mine; File dir = new File(javaxDataDir(), formatSnippetIDOpt(snippetID)); if (me) { String c = caseID(); if (nempty(c)) dir = newFile(dir, c); } return dir; } public static File programDir(String snippetID, String subPath) { return new File(programDir(snippetID), subPath); } public static Random defaultRandomGenerator() { return ThreadLocalRandom.current(); } public static boolean sameSnippetID(String a, String b) { if (!isSnippetID(a) || !isSnippetID(b)) return false; return parseSnippetID(a) == parseSnippetID(b); } public static volatile String caseID_caseID; public static String caseID() { return caseID_caseID; } public static void caseID(String id) { caseID_caseID = id; } public static final class _MethodCache { public final Class c; public final HashMap> cache = new HashMap(); public _MethodCache(Class c) { this.c = c; _init(); } public void _init() { Class _c = c; while (_c != null) { for (Method m : _c.getDeclaredMethods()) multiMapPut(cache, m.getName(), makeAccessible(m)); _c = _c.getSuperclass(); } for (Class intf : allInterfacesImplementedBy(c)) for (Method m : intf.getDeclaredMethods()) if (m.isDefault()) multiMapPut(cache, m.getName(), makeAccessible(m)); } public Method findMethod(String method, Object[] args) { try { List m = cache.get(method); if (m == null) return null; int n = m.size(); for (int i = 0; i < n; i++) { Method me = m.get(i); if (call_checkArgs(me, args, false)) return me; } return null; } catch (Exception __e) { throw rethrow(__e); } } public Method findStaticMethod(String method, Object[] args) { try { List m = cache.get(method); if (m == null) return null; int n = m.size(); for (int i = 0; i < n; i++) { Method me = m.get(i); if (isStaticMethod(me) && call_checkArgs(me, args, false)) return me; } return null; } catch (Exception __e) { throw rethrow(__e); } } } public static class GazelleEvalContext { public RuleEngine2 engine; public Map dataByRule; public Set linesSet; public transient List onNewNode; public GazellePredictor predictor; public String mechListPrefix = ""; public GazelleEvalContext() { gazelle_addSharedClasses(); } } public abstract static class VF2 { public abstract void get(A a, B b); } public static class GazelleTree { public List children; public GazelleTree parent; public double weight = 1, totalWeight; public String line, lineType; public String prediction, judgement; public RuleEngine2_MatchedRule mr; public boolean isSplitNode; public long created = now(); public transient GazelleEvalContext ctx; public GazelleTree() { } public GazelleTree(String line) { this.line = line; } public GazelleTree(GazelleEvalContext ctx, String line) { this.line = line; this.ctx = ctx; } public String toString() { return (isSplitNode ? "[split] " : "") + line + appendSquareBracketed(joinWithComma(listPlusNempties(renderQualityElements(), ruleID()))); } public List renderQualityElements() { List l = new ArrayList(); addIfNempty(l, prediction); if (mr != null) { l.add(mr.qualitySum() + " / " + formatDouble(mr.relativeQuality(), 2)); if (mr.moreConditions()) l.add("unsatisfied"); } return l; } public List history() { List l = new ArrayList(); GazelleTree e = this; while (e != null) { l.add(e.line); e = e.parent; } return reversed(l); } public void add(GazelleTree child) { if (children == null) children = new ArrayList(); child.parent = this; child.setContext(ctx); children.add(child); } public void setContext(GazelleEvalContext ctx) { this.ctx = ctx; pcallFAll(ctx.onNewNode, this); _add(ctx.linesSet, line); } public RuleEngine2.Rule rule() { return mr == null ? null : mr.rule; } public String ruleID() { return rule() == null ? null : rule().globalID; } public Map varMap() { return mr == null ? null : mr.map; } public void flush() { if (mr != null) { mr.flush(); line = mr.outText(); } } public Collection ruleComments() { RuleEngine2.Rule rule = rule(); return rule == null ? null : rule.comments; } } public static class Var implements IVar { public A v; public Var() { } public Var(A v) { this.v = v; } public synchronized void set(A a) { if (v != a) { v = a; notifyAll(); } } public synchronized A get() { return v; } public synchronized boolean has() { return v != null; } public synchronized void clear() { v = null; } public String toString() { return str(get()); } } public abstract static interface GazellePredictor { public String get(GazelleEvalContext ctx, RuleEngine2_MatchedRule mr); } public static class MechListDependentCache extends Cache { public String listName, lastListContents; public MechListDependentCache() { } public MechListDependentCache(String listName, Object maker) { this.maker = maker; this.listName = listName; } public A make() { return (A) callF(maker, lastListContents); } public A get() { if (hasLock(lock)) return value; { String listContents = mL_raw(listName); Lock __2463 = lock; lock(__2463); try { if (!eqOrSame(listContents, lastListContents)) clear(); lastListContents = listContents; } finally { unlock(__2463); } } return super.get(); } } public abstract static class TokCondition { public abstract boolean get(List tok, int i); } public static class MechListDependentCaches { public Map> byList = ciMap(); public Object maker; public transient Lock lock = lock(); public MechListDependentCaches(Object maker) { this.maker = maker; } public A get(String name) { MechListDependentCache listCache; { Lock __3573 = lock; lock(__3573); try { listCache = byList.get(name); if (listCache == null) byList.put(name, listCache = new MechListDependentCache(name, maker)); } finally { unlock(__3573); } } return listCache.get(); } } public static class MultiMap { public Map> data = new HashMap>(); public MultiMap() { } public MultiMap(boolean useTreeMap) { if (useTreeMap) data = new TreeMap(); } public MultiMap(MultiMap map) { putAll(map); } public MultiMap(Map> data) { this.data = data; } public void put(A key, B value) { synchronized (data) { List list = data.get(key); if (list == null) data.put(key, list = _makeEmptyList()); list.add(value); } } public void addAll(A key, Collection values) { synchronized (data) { putAll(key, values); } } public void addAllIfNotThere(A key, Collection values) { synchronized (data) { for (B value : values) setPut(key, value); } } public void setPut(A key, B value) { synchronized (data) { if (!containsPair(key, value)) put(key, value); } } public boolean containsPair(A key, B value) { synchronized (data) { return get(key).contains(value); } } public void putAll(A key, Collection values) { synchronized (data) { for (B value : values) put(key, value); } } public void removeAll(A key, Collection values) { synchronized (data) { for (B value : values) remove(key, value); } } public List get(A key) { synchronized (data) { List list = data.get(key); return list == null ? Collections.emptyList() : list; } } public List getActual(A key) { synchronized (data) { List list = data.get(key); if (list == null) data.put(key, list = _makeEmptyList()); return list; } } public void clean(A key) { synchronized (data) { List list = data.get(key); if (list != null && list.isEmpty()) data.remove(key); } } public Set keySet() { synchronized (data) { return data.keySet(); } } public Set keys() { synchronized (data) { return data.keySet(); } } public void remove(A key) { synchronized (data) { data.remove(key); } } public void remove(A key, B value) { synchronized (data) { List list = data.get(key); if (list != null) { list.remove(value); if (list.isEmpty()) data.remove(key); } } } public void clear() { synchronized (data) { data.clear(); } } public boolean containsKey(A key) { synchronized (data) { return data.containsKey(key); } } public B getFirst(A key) { synchronized (data) { List list = get(key); return list.isEmpty() ? null : list.get(0); } } public void addAll(MultiMap map) { putAll(map); } public void putAll(MultiMap map) { synchronized (data) { for (A key : map.keySet()) putAll(key, map.get(key)); } } public int keysSize() { synchronized (data) { return l(data); } } public int size() { synchronized (data) { int n = 0; for (List l : data.values()) n += l(l); return n; } } public List reverseGet(B b) { synchronized (data) { List l = new ArrayList(); for (A key : data.keySet()) if (data.get(key).contains(b)) l.add(key); return l; } } public Map> asMap() { synchronized (data) { return cloneMap(data); } } public boolean isEmpty() { synchronized (data) { return data.isEmpty(); } } public List _makeEmptyList() { return new ArrayList(); } public Collection> allLists() { synchronized (data) { return new ArrayList(data.values()); } } public List allValues() { return concatLists(values(data)); } public Object mutex() { return data; } public String toString() { return "mm" + str(data); } } public static class QuickException extends RuntimeException { public Throwable fillInStackTrace() { return this; } public QuickException() { } public QuickException(Throwable e) { super(e); } public QuickException(String msg) { super(msg); } public QuickException(String msg, Throwable e) { super(msg, e); } } public static class RuleEngine2 { public static class SimplifyWithRule extends Rule { public transient Object _function; public boolean isSplitRule; public boolean callOnTree; public Object function() { if (_function == null) { gazelle_parseInstruction(this); assertNotNull(_function); } return _function; } } public abstract static class QuickCheck { public abstract boolean check(String input); } public static class CicQuickCheck extends QuickCheck { public String string; public boolean check(String input) { return cic(input, string); } } public static class Rule { public String globalID; public List in; public String out; public List comments; public Set vars; public List insWithType; public List outTypes; public transient List inputMassagers; public transient List checkerFunctions; public transient List matchers; public String text; public QuickCheck applicabilityQuickCheck; public String purpose = ""; public Double qualityCutOff; public boolean avoidRecentlyUsedMappings; public List mapMassagers; public String asText() { if (l(in) > 1 && eq(last(in), out)) return join(" \n+ ", dropLast(in)) + " \n+=> " + out; return join(" \n+ ", in) + " \n=> " + out; } public void addMapMassager(Object massager) { if (massager != null) mapMassagers = addToOrCreateList(mapMassagers, massager); } public void setInputMassager(int i, Object massager) { inputMassagers = listSetWithCreate(inputMassagers, i, massager); } public void setMatcher(int i, Object matcher) { matchers = listSetWithCreate(matchers, i, matcher); } public void setOutType(int i, String type) { outTypes = listSetWithCreate(outTypes, i, type); } public String outType() { return first(outTypes); } public void parseGeneralComments() { purpose = gazelle_purposeFromComments(comments); try { String s = matchAny_firstGroup("quality cutoff = *", comments); if (s != null) qualityCutOff = parseDouble(s); } catch (Throwable __e) { _handleException(__e); } avoidRecentlyUsedMappings = cic(comments, "avoid recently used mapping"); } public String toString() { return globalID + ": " + text; } } public int minScore = 50; public int maxLevel = 100, maxOutLines = 10000; public List rules = new ArrayList(); public boolean printMappings, printGroupedData, printRejectedVars; public Set facts = ciSet(); public transient F2 callFunctionOnString = new F2() { public Object get(String function, String s) { try { return callAndMake(function, s); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callAndMake(function, s)"; } }; public boolean hasHelpers; public RuleEngine2() { } public RuleEngine2(List> rulesWithComments) { for (Pair p : unnull(rulesWithComments)) addRule(p.a, splitAtDoubleArrow_pair(p.a), p.b, null); } public void copyRulesFrom(RuleEngine2 engine) { if (engine == null) return; rules.addAll(engine.rules); hasHelpers = engine.hasHelpers; } public void addRules2(List> rulesWithCommentsAndID) { Matches m = new Matches(); for (T3 p : unnull(rulesWithCommentsAndID)) { Pair p2 = splitAtDoubleArrow_pair(p.a); if (p2 != null) { addRule(p.a, p2, p.b, p.c); continue; } SimplifyWithRule r = new SimplifyWithRule(); r.globalID = p.c; r.text = p.a; r.comments = lines(p.b); r.parseGeneralComments(); String s = p.a; gazelle_parseInstruction(r); if (r._function == null) continue; rules.add(r); } } public void addRule(String text, Pair rule, String comments, String globalID) { try { if (rule == null) return; rule = ai_rule_pair_expandPlusArrow(rule); List conditions = tok_splitAtPlusAtBeginningOfLine(rule.a); Rule r = new Rule(); r.globalID = globalID; r.text = text; r.comments = lines(comments); r.parseGeneralComments(); r.in = conditions; r.insWithType = map(conditions, new F1() { public GRuleLine get(String s) { try { return new GRuleLine(s, "standard"); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "GRuleLine(s, \"standard\")"; } }); for (int i = 0; i < l(r.in); i++) { String s = r.in.get(i); { if (!(endsWith(s, ""))) continue; } if (eq(s, "")) r.checkerFunctions = listSetWithCreate(r.checkerFunctions, i, new F1>() { public Map get(String s) { try { return nempty(s) ? litmap() : null; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "nempty(s) ? litmap() : null"; } }); else if (jmatch("* says: ", javaTokWithBrackets(s))) { r.setInputMassager(i, new F1() { public String get(String s) { try { Matches m = new Matches(); return jMatchStart("* says:", javaTokWithBrackets(s), m) ? m.get(0) + " says: " : s; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "new Matches m; ret jMatchStart(\"* says:\", javaTokWithBrackets(s), m) ? m.get(..."; } }); } } for (Matches m : getJMatches_all("expand *", r.comments)) if (isQuoted(m.get(0))) { final String var = m.unq(0); for (int i = 0; i < l(r.in); i++) r.setMatcher(i, new Object() { public Map get(List tokC, List tokI, RuleEngine2_MatchedRule matched) { if (eqic(nextToLast(tokC), var)) tokI = tok_groupLastTokensToMatchPattern(tokI, tokC); return zipCodeTokensToCIMap_strict_withoutEquals(tokC, tokI); } }); } if (jmatchAny(ll("in = dialog", "fact + event => replacement fact"), r.comments)) for (int i = 0; i < l(r.insWithType); i++) r.insWithType.get(i).type = "dialog-" + (l(r.insWithType) - i); Matches m = new Matches(); for (String s : r.comments) { if (jMatchStart("in * =", s, m) && isInteger(m.unq(0)) && (startsWith(m.unq(1), "statement") || eq(m.unq(1), "condition"))) set(get(r.insWithType, parseInt(m.unq(0)) - 1), "type", m.unq(1)); else if (jMatchStart("out * =", s, m) && isInteger(m.unq(0))) r.setOutType(parseInt(m.unq(0)) - 1, m.unq(1)); else if (jMatchStart("out = ", s, m)) r.setOutType(0, m.rest()); else if (jMatchStart("in * :", s, m) && isInteger(m.unq(0))) { int i = parseInt(m.unq(0)) - 1; String comment = m.rest(); GRuleLine line = r.insWithType.get(i); line.comments = addToOrCreateList(line.comments, comment); if (match("tokenize with *", comment, m)) line.tokenizer = m.unq(0); } } r.out = rule.b; r.vars = ai_wordsInBothSidesOfPair_uncurly(rule); rules.add(r); } catch (Throwable __e) { _handleException(__e); } } public List, String>> rulesAsPairs() { return map(rules, new F1, String>>() { public Pair, String> get(Rule r) { try { return pair(r.in, r.out); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "pair(r.in, r.out)"; } }); } public List splitterRules() { return (List) filter(rules, new F1() { public Boolean get(Rule r) { try { return r instanceof SimplifyWithRule && ((SimplifyWithRule) r).isSplitRule; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "r instanceof SimplifyWithRule && ((SimplifyWithRule) r).isSplitRule"; } }); } public void dropRulesWhere(IF1 pred) { rules = antiFilter(pred, rules); } public void deleteRule(String ruleID) { dropRulesWhere(r -> eq(r.globalID, ruleID)); } public Rule getRule(String ruleID) { return firstWhere(rules, "globalID", ruleID); } } public static class Lowest { public A best; public double score; public transient Object onChange; public synchronized boolean isNewBest(double score) { return best == null || score < this.score; } public synchronized double bestScore() { return best == null ? Double.NaN : score; } public double score() { return bestScore(); } public synchronized float floatScore() { return best == null ? Float.NaN : (float) score; } public synchronized float floatScoreOr(float defaultValue) { return best == null ? defaultValue : (float) score; } public boolean put(A a, double score) { boolean change = false; synchronized (this) { if (a != null && isNewBest(score)) { best = a; this.score = score; change = true; } } if (change) pcallF(onChange); return change; } public synchronized A get() { return best; } public synchronized boolean has() { return best != null; } } public static class TextAndType { public String text; public String type; public TextAndType() { } public TextAndType(String text, String type) { this.type = type; this.text = text; } public String toString() { return "TextAndType(" + text + ", " + type + ")"; } public boolean equals(Object o) { if (!(o instanceof TextAndType)) return false; TextAndType x = (TextAndType) o; return eq(text, x.text) && eq(type, x.type); } public int hashCode() { int h = -34343004; h = h * 2 + _hashCode(text); h = h * 2 + _hashCode(type); return h; } } public abstract static class Collector { public abstract boolean add(A a); public boolean full() { return false; } public boolean contains(A a) { return false; } } public static class Either { public byte which; public Object value; public Either() { } public Either(int which, Object value) { this.value = value; this.which = (byte) which; } public boolean isA() { return which == 1; } public boolean isB() { return which == 2; } public A a() { if (which != 1) _failMe(); return (A) value; } public B b() { if (which != 2) _failMe(); return (B) value; } public void _failMe() { throw fail("Either object is of wrong type: " + shortClassName(value)); } } public static class MultiSet { public Map map = new HashMap(); public MultiSet(boolean useTreeMap) { if (useTreeMap) map = new TreeMap(); } public MultiSet() { } public MultiSet(Iterable c) { addAll(c); } public MultiSet(MultiSet ms) { synchronized (ms) { for (A a : ms.keySet()) add(a, ms.get(a)); } } public synchronized void add(A key) { add(key, 1); } public synchronized void addAll(Iterable c) { if (c != null) for (A a : c) add(a); } public synchronized void addAll(MultiSet ms) { for (A a : ms.keySet()) add(a, ms.get(a)); } public synchronized void add(A key, int count) { if (count <= 0) return; if (map.containsKey(key)) map.put(key, map.get(key) + count); else map.put(key, count); } public synchronized int get(A key) { Integer i = map.get(key); return i != null ? i : 0; } public synchronized boolean contains(A key) { return map.containsKey(key); } public synchronized void remove(A key) { Integer i = map.get(key); if (i != null && i > 1) map.put(key, i - 1); else map.remove(key); } public synchronized List topTen() { return getTopTen(); } public synchronized List getTopTen() { return getTopTen(10); } public synchronized List getTopTen(int maxSize) { List list = getSortedListDescending(); return list.size() > maxSize ? list.subList(0, maxSize) : list; } public synchronized List highestFirst() { return getSortedListDescending(); } public synchronized List lowestFirst() { return reversedList(getSortedListDescending()); } public synchronized List getSortedListDescending() { List list = new ArrayList(map.keySet()); Collections.sort(list, new Comparator() { public int compare(A a, A b) { return map.get(b).compareTo(map.get(a)); } }); return list; } public synchronized int getNumberOfUniqueElements() { return map.size(); } public synchronized int uniqueSize() { return map.size(); } public synchronized Set asSet() { return map.keySet(); } public synchronized NavigableSet navigableSet() { return navigableKeys((NavigableMap) map); } public synchronized Set keySet() { return map.keySet(); } public synchronized A getMostPopularEntry() { int max = 0; A a = null; for (Map.Entry entry : map.entrySet()) { if (entry.getValue() > max) { max = entry.getValue(); a = entry.getKey(); } } return a; } public synchronized void removeAll(A key) { map.remove(key); } public synchronized int size() { int size = 0; for (int i : map.values()) size += i; return size; } public synchronized MultiSet mergeWith(MultiSet set) { MultiSet result = new MultiSet(); for (A a : set.asSet()) { result.add(a, set.get(a)); } return result; } public synchronized boolean isEmpty() { return map.isEmpty(); } public synchronized String toString() { return str(map); } public synchronized void clear() { map.clear(); } public synchronized Map asMap() { return cloneMap(map); } } public static class GInterpretable { public String text; public String ruleID; public Object mr; public GInterpretable() { } public GInterpretable(String text) { this.text = text; } public GInterpretable(String text, String ruleID) { this.ruleID = ruleID; this.text = text; } public GInterpretable(String text, String ruleID, Object mr) { this.mr = mr; this.ruleID = ruleID; this.text = text; } public String toString() { return text; } } public static class GazelleContextCache_v2 extends F0 { public transient Lock lock = lock(); public transient GazelleEvalContext cachedCtx; public DynModule module; public boolean debug = true; public F0>> grabRules = new F0>>() { public List> get() { try { return gazelle_allRuleTriples(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "//dm_gazelle_allRulesWithComment(gazelle_requiredComment())\r\n ret gazelle_..."; } }; public String mechListPrefix; public boolean alwaysFullRefresh; public GazelleContextCache_v2() { } public GazelleContextCache_v2(DynModule module) { this.module = module; } public GazelleContextCache_v2 fill() { loadRules(callF(grabRules)); return this; } public void loadRules(List> rules) { Lock __2492 = lock; lock(__2492); try { cachedCtx = dm_gazelle_stdEvalContext(rules); cachedCtx.mechListPrefix = unnull(mechListPrefix); gazelle_addHelpers(cachedCtx); } finally { unlock(__2492); } } public GazelleEvalContext get() { Lock __2493 = lock; lock(__2493); try { return gazelle_cloneCtx(cachedCtx); } finally { unlock(__2493); } } public void ruleChanged(String ruleID) { if (ruleID == null) return; dm_q(module, new Runnable() { public void run() { try { Lock __2494 = lock; lock(__2494); try { if (alwaysFullRefresh) { fill(); return; } if (cachedCtx == null) return; print("GazelleContextCache_v2: processing rule update " + ruleID); RuleEngine2 engine = cachedCtx.engine; RuleEngine2.Rule rule = engine.getRule(ruleID); if (rule == null) print("GazelleContextCache_v2: rule not found, assuming it's new"); else engine.rules.remove(rule); Pair p = dm_textAndCommentForRule(ruleID); if (p == null) { print("GazelleContextCache_v2: no text found for rule"); return; } engine.addRules2(ll(t3(p.a, p.b, ruleID))); rule = cachedCtx.engine.getRule(ruleID); if (rule == null) { print("GazelleContextCache_v2: rule not properly added"); return; } gazelle_addHelpers(nu(GazelleEvalContext.class, "engine", gazelle_dummyEngineWithRule(rule))); gazelle_updateStatsForRule(cachedCtx, ruleID); print("GazelleContextCache_v2: rule update complete"); } finally { unlock(__2494); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Lock __2494 = lock; lock(__2494); try {\r\n if (alwaysFullRefresh) return ..."; } }); } public AutoCloseable listenToMessages() { return combineAutoCloseables(vmBus_onMessage("gazellePleaseClearRuleCaches", new Runnable() { public void run() { try { dm_q(module, new Runnable() { public void run() { try { fill(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "fill();"; } }); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "dm_q(module, r fill)"; } }), vmBus_onMessage("gazelleRuleFeedbackChanged", new VF1() { public void get(String a) { try { ruleChanged(a); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ruleChanged(a)"; } }), vmBus_onMessage("gazelleRuleChanged", new VF1() { public void get(Object ruleObj) { try { ruleChanged(getString("globalID", ruleObj)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ruleChanged(getString globalID(ruleObj))"; } }), vmBus_onMessage("gazelleRuleDeleted", new VF1() { public void get(Object rule) { try { String ruleID = getString("globalID", rule); dm_q(module, new Runnable() { public void run() { try { Lock __2495 = lock; lock(__2495); try { if (cachedCtx == null) return; cachedCtx.engine.deleteRule(ruleID); print("GazelleContextCache_v2: rule " + ruleID + " deleted"); } finally { unlock(__2495); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Lock __2495 = lock; lock(__2495); try {\r\n if (cachedCtx == null) ret..."; } }); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "S ruleID = getString globalID(rule);\r\n dm_q(module, r {\r\n loc..."; } })); } public void alwaysFullRefresh() { alwaysFullRefresh = true; fill(); } } public static class CompilerBot { public static boolean verbose; public static File compileSnippet(String snippetID) { return compileSnippet(snippetID, ""); } public static Pair compileSnippet2(String snippetID) { return compileSnippet2(snippetID, ""); } public static File compileSnippet(String snippetID, String javaTarget) { return compileSnippet2(snippetID, javaTarget).a; } public static Pair compileSnippet2(String snippetID, String javaTarget) { String transpiledSrc = getServerTranspiled2(snippetID); int i = transpiledSrc.indexOf('\n'); String libs = transpiledSrc.substring(0, Math.max(0, i)); if (verbose) print("Compiling snippet: " + snippetID + ". Libs: " + libs); transpiledSrc = transpiledSrc.substring(i + 1); return pair(compile(transpiledSrc, libs, javaTarget, snippetID), transpiledSrc); } public static File compile(String src) { return compile(src, ""); } public static File compile(String src, String libs) { return compile(src, libs, null); } public static File compile(String src, String dehlibs, String javaTarget) { return compile(src, dehlibs, javaTarget, null); } public static File compile(String src, String dehlibs, String javaTarget, String progID) { if (verbose) print("Compiling " + l(src) + " chars"); String md5 = md5(dehlibs + "\n" + src + "\n" + progID); File jar = getJarFile(md5); if (jar == null || jar.length() <= 22) { List mainClass = findMainClass(javaTok(src)); boolean canRename = mainClass != null && useDummyMainClasses() && isSnippetID(progID) && !tok_classHasModifier(mainClass, "public"); if (verbose) print("useRenaming: " + useDummyMainClasses() + ", canRename: " + canRename + ", progID: " + progID); javaCompileToJar_optionalRename(src, dehlibs, jar, canRename ? progID : null, progID); } else { if (verbose) print("Getting classes from cache (" + jar.getAbsolutePath() + ", " + jar.length() + " bytes)"); touchFile(jar); } return jar; } public static File getJarFile(String md5) { assertTrue(isMD5(md5)); return new File(getCacheProgramDir("#1002203"), md5 + ".jar"); } } public static class T3 { public A a; public B b; public C c; public T3() { } public T3(A a, B b, C c) { this.c = c; this.b = b; this.a = a; } public T3(T3 t) { a = t.a; b = t.b; c = t.c; } public int hashCode() { return _hashCode(a) + 2 * _hashCode(b) - 4 * _hashCode(c); } public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof T3)) return false; T3 t = (T3) o; return eq(a, t.a) && eq(b, t.b) && eq(c, t.c); } public String toString() { return "(" + quoteBorderless(a) + ", " + quoteBorderless(b) + ", " + quoteBorderless(c) + ")"; } } public static class Pair implements Comparable> { public A a; public B b; public Pair() { } public Pair(A a, B b) { this.b = b; this.a = a; } public int hashCode() { return hashCodeFor(a) + 2 * hashCodeFor(b); } public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof Pair)) return false; Pair t = (Pair) o; return eq(a, t.a) && eq(b, t.b); } public String toString() { return "<" + a + ", " + b + ">"; } public int compareTo(Pair p) { if (p == null) return 1; int i = ((Comparable) a).compareTo(p.a); if (i != 0) return i; return ((Comparable) b).compareTo(p.b); } } public static class RuleEngine2_MatchedRule { public String globalID = aGlobalID(); public RuleEngine2.Rule rule; public int iCond; public Map map; public Map mappingQuality = new HashMap(); public List, List>> matchedLines = new ArrayList(); public String tokenizationFunction; public String outTokenizer; public List remainingConditions; public List remainingConditionsWithType; public Integer qualitySum; public Integer nPossibleVars; public String out; public Map inputsByType; public GInterpretable interpretable; public int qualitySum() { if (qualitySum == null) qualitySum = intSum(values(mappingQuality)) - l(map); return qualitySum; } public int nPossibleVars() { if (nPossibleVars == null) nPossibleVars = l(possibleVars()); return nPossibleVars; } public boolean isSpecialRule() { return rule instanceof RuleEngine2.SimplifyWithRule; } public double relativeQuality() { return isSpecialRule() ? 0 : doubleRatio(qualitySum(), nPossibleVars()); } public Set possibleVars() { return asCISet(codeTokens(tokenize(first(rule.in)))); } public RuleEngine2.Rule applyMapping() { RuleEngine2.Rule r = new RuleEngine2.Rule(); r.in = map(rule.in, new F1() { public String get(String x) { try { return applyMappingTo(x); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "applyMappingTo(x)"; } }); r.out = applyMappingTo(rule.out); return r; } public String appliedRuleText() { return applyMapping().asText(); } public String outText() { if (out == null) out = join(ai_mapCodeTokensWithMap(outTokenize(rule.out), map)); return out; } public String applyMappingTo(GRuleLine line) { return line == null ? null : join(ai_mapCodeTokensWithMap(tokenize(line), map)); } public String applyMappingTo(String s) { return s == null ? null : join(ai_mapCodeTokensWithMap(tokenize(s), map)); } public List tokenize(String s) { return (List) callAndMake(assertNotNull(tokenizationFunction), s); } public List outTokenize(String s) { return (List) callAndMake(assertNotNull(or(outTokenizer, tokenizationFunction)), s); } public List tokenize(GRuleLine line) { return (List) callAndMake(assertNotNull(or(line.tokenizer, tokenizationFunction)), line.text); } public boolean moreConditions() { return rule != null && iCond < l(rule.in); } public String ruleID() { return rule.globalID; } public void flush() { if (tokenizationFunction != null || outTokenizer != null) out = null; } public String toString() { return "MatchedRule " + ruleID() + " with " + map + (moreConditions() ? " [unsatisfied]" : ""); } } public static class GRuleLine extends TextAndType { public String tokenizer; public List comments; public GRuleLine() { } public GRuleLine(String text, String type) { this.type = type; this.text = text; } public boolean equals(Object o) { return stdEq2(this, o); } public int hashCode() { return stdHash2(this); } } public static class JavaXClassLoader extends URLClassLoader { public String progID; public List files; public Set triedToLoad = synchroSet(); public Set loadedClasses = synchroSet(); public boolean retired; public Object retiredMarker; public JavaXClassLoader(String progID, List files) { this(progID, files, getSystemClassLoader()); } public JavaXClassLoader(String progID, List files, ClassLoader parent) { super(new URL[0], parent); this.progID = progID; this.files = files; { try { for (File f : files) addURL(f.toURI().toURL()); } catch (Exception __e) { throw rethrow(__e); } } fixACCInClassLoader(this); } public Class findClass(String name) throws ClassNotFoundException { boolean triedBefore = !triedToLoad.add(name); try { Class c = super.findClass(name); loadedClasses.add(c); if (eq(name, "main")) callOpt(javax(), "registerAMainClass", c); return c; } catch (ClassNotFoundException e) { throw new ClassNotFoundException("Class " + name + " not found in " + joinWithComma(map("f2s", files)) + " (progID=" + progID + ")" + (triedBefore ? ", tried to load before" : ""), e); } } public String toString() { return "JavaXClassLoader[" + systemHashCodeHex(this) + "] - " + progID; } } public abstract static class IterableIterator implements Iterator, Iterable { public Iterator iterator() { return this; } public void remove() { unsupportedOperation(); } } public abstract static class F2 { public abstract C get(A a, B b); } public static class MRUCache extends LinkedHashMap { public int maxSize = 10; public MRUCache() { } public MRUCache(int maxSize) { this.maxSize = maxSize; } public boolean removeEldestEntry(Map.Entry eldest) { return size() > maxSize; } } public abstract static class CloseableIterableIterator extends IterableIterator implements AutoCloseable { public void close() throws Exception { } } public static class ExpiringMap2 extends AbstractMap { public Map> byKey = new HashMap(); public PriorityBlockingQueue> queue = new PriorityBlockingQueue(); public long standardExpiryTime; public boolean renewOnOverwrite = true, renewOnGet; public Object onChange; public ExpiringMap2() { } public ExpiringMap2(long standardExpiryTime) { this.standardExpiryTime = standardExpiryTime; } public ExpiringMap2(long standardExpiryTime, Object onChange) { this.onChange = onChange; this.standardExpiryTime = standardExpiryTime; } public synchronized boolean clean() { boolean changes = false; Pair p; while ((p = queue.peek()) != null && sysTime() >= p.a) { p = queue.poll(); Pair v = byKey.get(p.b); if (v != null) { byKey.remove(p.b); changes = true; change(); } } return changes; } public void change() { callF(onChange); } public synchronized B put(A a, B b) { clean(); long timeout = sysTime() + standardExpiryTime; Pair p = byKey.get(a); if (p != null && renewOnOverwrite) queue.remove(new Pair(p.a, a)); byKey.put(a, pair(timeout, b)); change(); if (p == null || renewOnOverwrite) queue.add(new Pair(timeout, a)); return pairB(p); } public synchronized B remove(Object a) { clean(); Pair p = byKey.get(a); if (p == null) return null; queue.remove(new Pair(p.a, a)); byKey.remove(a); change(); return p.b; } public synchronized B get(Object a) { clean(); Pair p = byKey.get(a); if (renewOnGet && p != null) { queue.remove(new Pair(p.a, a)); long timeout = sysTime() + standardExpiryTime; byKey.put((A) a, pair(timeout, p.b)); queue.add(new Pair(timeout, a)); } return pairB(p); } public synchronized Set> entrySet() { clean(); return synchronizedSet(mapValues("pairB", byKey).entrySet()); } public synchronized Set keySet() { clean(); return synchronizedSet(byKey.keySet()); } public synchronized int size() { clean(); return byKey.size(); } public void setStandardExpiryTime(long ms) { standardExpiryTime = ms; } public synchronized ExpiringMap2 setMap(Map innerMap) { byKey = innerMap; return this; } } public static class JavaXClassLoaderWithParent2 extends JavaXClassLoader { public ClassLoader virtualParent; public List classesToSkip; public JavaXClassLoaderWithParent2(String progID, List files, ClassLoader virtualParent, List classesToSkip) { super(progID, files); this.virtualParent = virtualParent; this.classesToSkip = classesToSkip; } public Class findClass(String name) throws ClassNotFoundException { if (shouldDelegate(name)) { Class c = virtualParent.loadClass(name); if (c != null) return c; } return super.findClass(name); } public boolean shouldDelegate(String name) { for (String s : classesToSkip) if (eq(name, s) || startsWith(name, s + "$")) return true; return false; } } public static class PosNegScore { public static String _fieldOrder = "pos neg"; public double pos; public double neg; public PosNegScore() { } public PosNegScore(double pos, double neg) { this.neg = neg; this.pos = pos; } public String toString() { return "PosNegScore(" + pos + ", " + neg + ")"; } public boolean equals(Object o) { if (!(o instanceof PosNegScore)) return false; PosNegScore x = (PosNegScore) o; return eq(pos, x.pos) && eq(neg, x.neg); } public int hashCode() { int h = 1208109366; h = h * 2 + _hashCode(pos); h = h * 2 + _hashCode(neg); return h; } } public static interface Producer { public A next(); } public static interface IF1 { public B get(A a); } public static class MechMode { public Object readMode; public Object writeMode; public MechMode() { } public MechMode(Object readMode, Object writeMode) { this.writeMode = writeMode; this.readMode = readMode; } public String toString() { return "MechMode(" + readMode + ", " + writeMode + ")"; } public boolean equals(Object o) { if (!(o instanceof MechMode)) return false; MechMode x = (MechMode) o; return eq(readMode, x.readMode) && eq(writeMode, x.writeMode); } public int hashCode() { int h = -866263200; h = h * 2 + _hashCode(readMode); h = h * 2 + _hashCode(writeMode); return h; } } public static interface IVar { public void set(A a); public A get(); public boolean has(); public void clear(); } public static class Cache { public Object maker; public A value; public long loaded; public static boolean debug; public long changeCount; public Lock lock = lock(); public Cache() { } public Cache(Object maker) { this.maker = maker; } public A get() { if (hasLock(lock)) return value; Lock __2552 = lock; lock(__2552); try { if (loaded == 0) { value = make(); changeCount++; loaded = sysNow(); } return value; } finally { unlock(__2552); } } public void clear() { Lock __2553 = lock; lock(__2553); try { if (debug && loaded != 0) print("Clearing cache"); value = null; changeCount++; loaded = 0; } finally { unlock(__2553); } } public void clear(double seconds) { Lock __2554 = lock; lock(__2554); try { if (seconds != 0 && loaded != 0 && sysNow() >= loaded + seconds * 1000) clear(); } finally { unlock(__2554); } } public void set(A a) { Lock __2555 = lock; lock(__2555); try { value = a; ++changeCount; loaded = sysNow(); } finally { unlock(__2555); } } public A make() { return (A) callF(maker); } } public static void multiMapPut(Map> map, A a, B b) { List l = map.get(a); if (l == null) map.put(a, l = new ArrayList()); l.add(b); } public static void multiMapPut(MultiMap mm, A key, B value) { if (mm != null && key != null && value != null) mm.put(key, value); } public static Field makeAccessible(Field f) { f.setAccessible(true); return f; } public static Method makeAccessible(Method m) { m.setAccessible(true); return m; } public static Set allInterfacesImplementedBy(Class c) { if (c == null) return null; HashSet set = new HashSet(); allInterfacesImplementedBy_find(c, set); return set; } public static void allInterfacesImplementedBy_find(Class c, Set set) { if (c.isInterface() && !set.add(c)) return; do { for (Class intf : c.getInterfaces()) allInterfacesImplementedBy_find(intf, set); } while ((c = c.getSuperclass()) != null); } public static boolean call_checkArgs(Method m, Object[] args, boolean debug) { Class[] types = m.getParameterTypes(); if (types.length != args.length) { if (debug) print("Bad parameter length: " + args.length + " vs " + types.length); return false; } for (int i = 0; i < types.length; i++) { Object arg = args[i]; if (!(arg == null ? !types[i].isPrimitive() : isInstanceX(types[i], arg))) { if (debug) print("Bad parameter " + i + ": " + arg + " vs " + types[i]); return false; } } return true; } public static Method findStaticMethod(Class c, String method, Object... args) { Class _c = c; while (c != null) { for (Method m : c.getDeclaredMethods()) { if (!m.getName().equals(method)) continue; if ((m.getModifiers() & Modifier.STATIC) == 0 || !findStaticMethod_checkArgs(m, args)) continue; return m; } c = c.getSuperclass(); } return null; } public static boolean findStaticMethod_checkArgs(Method m, Object[] args) { Class[] types = m.getParameterTypes(); if (types.length != args.length) return false; for (int i = 0; i < types.length; i++) if (!(args[i] == null || isInstanceX(types[i], args[i]))) return false; return true; } public static void gazelle_addSharedClasses() { hotwire_addSharedClasses(RuleEngine2.class, GazelleTree.class, GazelleEvalContext.class, GInterpretable.class, Pair.class); } public static String appendSquareBracketed(String b) { return empty(b) ? "" : "" + " [" + b + "]"; } public static String appendSquareBracketed(String a, String b) { return empty(b) ? a : a + appendSquareBracketed(b); } public static List listPlusNempties(Collection l, String... more) { return concatLists_conservative(l, nempties(asList(more))); } public static void addIfNempty(Collection l, String s) { if (nempty(s)) l.add(s); } public static void addIfNempty(Collection l, Map m) { if (nempty(m)) l.add(m); } public static void addIfNempty(Collection l, Collection c) { if (nempty(c)) l.add(c); } public static String formatDouble(double d, int digits) { String format = digits <= 0 ? "0" : "0." + rep(digits, '#'); return new java.text.DecimalFormat(format, new java.text.DecimalFormatSymbols(Locale.ENGLISH)).format(d); } public static void _add(BitSet bs, int i) { add(bs, i); } public static boolean _add(Collection c, A a) { return add(c, a); } public static void _add(Container c, Component x) { add(c, x); } public static boolean hasLock(Lock lock) { return ((ReentrantLock) lock).isHeldByCurrentThread(); } public static String mL_raw(String name) { return mechList_raw(name); } public static boolean eqOrSame(Object a, Object b) { return a == null ? b == null : a == b || b != null && a.equals(b); } public static Map putAll(Map a, Map b) { if (a != null) a.putAll(b); return a; } public static Set keySet(Map map) { return map == null ? new HashSet() : map.keySet(); } public static Set keySet(Object map) { return keys((Map) map); } public static Set keySet(MultiSet ms) { return ms.keySet(); } public static Set keySet(MultiMap mm) { return mm.keySet(); } public static A reverseGet(List l, int idx) { if (l == null || idx < 0) return null; int n = l(l); return idx < n ? l.get(n - 1 - idx) : null; } public static void gazelle_parseInstruction(RuleEngine2.SimplifyWithRule r) { Matches m = new Matches(); String s = r.text; if (match("simplify with *", s, m)) { r._function = m.unq(0); return; } else if (match("simplify with * on tree", s, m)) { r.callOnTree = true; { r._function = m.unq(0); return; } } else if (match("drop after word *", s, m)) { final String word = m.unq(0); { r._function = new F1() { public Object get(String s) { try { return tok_dropStartingWithWord(word, s); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "tok_dropStartingWithWord(word, s)"; } }; return; } } else if (match("group words *", s, m)) { final String words = m.unq(0); { r._function = new F1() { public Object get(String s) { try { return tok_groupPhrase(s, words); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "tok_groupPhrase(s, words)"; } }; return; } } else if (match("split with *", s, m)) { r.isSplitRule = true; { r._function = m.unq(0); return; } } } public static A last(List l) { return empty(l) ? null : l.get(l.size() - 1); } public static char last(String s) { return empty(s) ? '#' : s.charAt(l(s) - 1); } public static int last(int[] a) { return l(a) != 0 ? a[l(a) - 1] : 0; } public static A last(A[] a) { return l(a) != 0 ? a[l(a) - 1] : null; } public static A last(Iterator it) { A a = null; while (it.hasNext()) { ping(); a = it.next(); } return a; } public static List addToOrCreateList(List l, A a) { return addDyn(l, a); } public static List listSetWithCreate(List l, int i, A a) { if (l == null) l = new ArrayList(); listSet(l, i, a); return l; } public static String gazelle_purposeFromComments(List comments) { Matches m = new Matches(); for (String s : unnull(comments)) { if (swic_trim(s, "purpose:", m)) return m.rest(); } return ""; } public static String gazelle_purposeFromComments(String comments) { return gazelle_purposeFromComments(tlft(comments)); } public static Pair splitAtDoubleArrow_pair(String s) { return splitAtDoubleArrow_pair(javaTokWithBrackets(s)); } public static Pair splitAtDoubleArrow_pair(List tok) { return listToPair(splitAtDoubleArrow(tok)); } public static Pair ai_rule_pair_expandPlusArrow(Pair rule) { if (endsWith(pairA(rule), "+")) return pair(rule.a + " " + rule.b, rule.b); return rule; } public static boolean jMatchStart(String pat, String s) { return jMatchStart(pat, s, null); } public static boolean jMatchStart(String pat, String s, Matches matches) { if (s == null) return false; return jMatchStart(pat, javaTok(s), matches); } public static boolean jMatchStart(String pat, List toks, Matches matches) { List tokpat = jfind_preprocess(javaTok(pat)); if (toks.size() < tokpat.size()) return false; String[] m = match2x(tokpat, toks.subList(0, tokpat.size())); if (m == null) return false; if (matches != null) { matches.m = new String[m.length + 1]; arraycopy(m, matches.m); matches.m[m.length] = join(toks.subList(tokpat.size(), toks.size())); } return true; } public static A nextToLast(List l) { return get(l, l(l) - 2); } public static List tok_groupLastTokensToMatchPattern(List tok, List tokPat) { return concatLists(subList(tok, 0, l(tokPat) - 2), ll(curly(joinSubList(tok, l(tokPat) - 2, l(tok) - 1)), last(tok))); } public static Set ai_wordsInBothSidesOfPair_uncurly(Pair p) { return mapCISet("uncurly", ai_wordsInBothSidesOfPair(p)); } public static A firstWhere(Collection c, Object... data) { return findWhere(c, data); } public static int _hashCode(Object a) { return a == null ? 0 : a.hashCode(); } public static String b(Object contents, Object... params) { return tag("b", contents, params); } public static String shortClassName(Object o) { if (o == null) return null; Class c = o instanceof Class ? (Class) o : o.getClass(); String name = c.getName(); return shortenClassName(name); } public static NavigableSet navigableKeys(NavigableMap map) { return map == null ? new TreeSet() : map.navigableKeySet(); } public static NavigableSet navigableKeys(MultiSet ms) { return ((NavigableMap) ms.map).navigableKeySet(); } public static NavigableSet navigableKeys(MultiMap mm) { return ((NavigableMap) mm.data).navigableKeySet(); } public static List> gazelle_allRuleTriples() { return dm_allRulesFromRulesModuleWithCommentsAndIDs(); } public static void fill(RGBImage img, String col) { RGB rgb = new RGB(col); int w = img.getWidth(), h = img.getHeight(); for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) img.setPixel(x, y, rgb); } public static GazelleEvalContext gazelle_cloneCtx(GazelleEvalContext ctx) { GazelleEvalContext ctx2 = shallowClone(ctx); ctx2.engine = new RuleEngine2(); ctx2.engine.copyRulesFrom(ctx.engine); return ctx2; } public static Q dm_q() { return dm_current_mandatory().q(); } public static void dm_q(Runnable r) { dm_inQ(r); } public static void dm_q(DynModule module, Runnable r) { module.q().add(r); } public static RuleEngine2 gazelle_dummyEngineWithRule(RuleEngine2.Rule rule) { RuleEngine2 engine = new RuleEngine2(); engine.rules.add(rule); return engine; } public static void gazelle_updateStatsForRule(GazelleEvalContext ctx, String ruleID) { MultiSet statements = dm_gazelle_statementsForRule_multiSet(ruleID); ctx.dataByRule.put(ruleID, ai_gazelle_analyzeStatementsForRule_multiSet(statements)); } public static boolean getServerTranspiled2_allowLocalFallback = true, getServerTranspiled2_localFallbackVerbose = true; public static String getServerTranspiled2(String id) { String transpiled = loadCachedTranspilation(id); String md5 = null; if (machineIsOffline() || isOfflineMode() || isLocalSnippet(id)) return transpiled; if (transpiled != null) md5 = md5(transpiled); String transpiledSrc; try { transpiledSrc = getServerTranspiled(formatSnippetID(id), md5); } catch (Throwable e) { if (!getServerTranspiled2_allowLocalFallback) rethrow(e); printExceptionShort(e); if (getServerTranspiled2_localFallbackVerbose) print("Fallback to local code"); return transpiled; } if (eq(transpiledSrc, "SAME")) { if (!isTrue(loadPage_silent.get())) print("SAME"); return transpiled; } return transpiledSrc; } public static List findMainClass(List tok) { for (List c : reversedList(allClasses(tok))) { String name = getClassDeclarationName(c); if (eq(name, "main") || name.startsWith("x")) return c; } return findBlock("m {", tok); } public static boolean useDummyMainClasses() { return true; } public static boolean tok_classHasModifier(List classDecl, String modifier) { if (classDecl == null) return false; int i = classDecl.indexOf("class"); return subList(classDecl, 0, i).contains(modifier); } public static boolean javaCompileToJar_useRAMDisk; public static File javaCompileToJar_optionalRename(String src, File destJar, String progIDForRename) { return javaCompileToJar_optionalRename(src, "", destJar, progIDForRename); } public static synchronized File javaCompileToJar_optionalRename(String src, String dehlibs, File destJar, String progIDForRename) { return javaCompileToJar_optionalRename(src, dehlibs, destJar, progIDForRename, null); } public static synchronized File javaCompileToJar_optionalRename(String src, String dehlibs, File destJar, String progIDForRename, String progID) { String javaTarget = null; String dummyClass = "main"; if (progIDForRename != null) { dummyClass = dummyMainClassName(progIDForRename); src += "\nclass " + dummyClass + "{}"; } String md5 = md5(src); File jar = destJar; Class j = getJavaX(); if (javaTarget != null) setOpt(j, "javaTarget", javaTarget); File srcDir = tempDir(); String className = "main"; String fileName = dummyClass + ".java"; List tok = javaTok(src); String packageName = tok_packageName(tok); if (packageName != null) fileName = packageName.replace(".", "/") + "/" + tok_firstClassName(tok) + ".java"; File mainJava = new File(srcDir, fileName); saveTextFile(mainJava, src); File classesDir = javaCompileToJar_useRAMDisk ? tempDirPossiblyInRAMDisk() : tempDir(); try { List libraries = new ArrayList(); Matcher m = Pattern.compile("\\d+").matcher(dehlibs); while (m.find()) { String libID = m.group(); assertTrue(isSnippetID(libID)); libraries.add(loadLibraryOrSrcLib(libID)); } libraries.add(pathToJavaxJar()); String compilerOutput; try { compilerOutput = (String) call(j, "compileJava", srcDir, libraries, classesDir); } catch (Throwable e) { compilerOutput = (String) get(getJavaX(), "javaCompilerOutput"); String msg = cleanJavaCompilerOutput(compilerOutput); if (neqic(e.getMessage(), "Java compiler returned errors.") || empty(msg)) msg = appendWithNewLine(msg, str(e)); throw fail(msg); } compilerOutput = cleanJavaCompilerOutput("Annotation processing got disabled, since it requires a 1.6 compliant JVM"); if (nempty(compilerOutput)) { print("Compiler said: " + compilerOutput); } if (!anyFileWithExtensionInDir(classesDir, ".class")) throw fail("No classes generated"); saveTextFile(new File(classesDir, "main.java"), src); if (nempty(dehlibs)) saveTextFile(new File(classesDir, "libraries"), dehlibs); saveTextFile(new File(classesDir, "progID"), progID); dir2zip_recurse_verbose = false; int n = dir2zip_recurse(classesDir, jar); return jar; } finally { if (isInRAMDisk(classesDir)) deleteDirectory(classesDir); } } public static void touchFile(File file) { try { closeRandomAccessFile(newRandomAccessFile(mkdirsForFile(file), "rw")); } catch (Exception __e) { throw rethrow(__e); } } public static File getCacheProgramDir() { return getCacheProgramDir(getProgramID()); } public static File getCacheProgramDir(String snippetID) { return new File(userHome(), "JavaX-Caches/" + formatSnippetIDOpt(snippetID)); } public static String quoteBorderless(Object o) { if (o == null) return "null"; return quoteBorderless(str(o)); } public static String quoteBorderless(String s) { if (s == null) return "null"; StringBuilder out = new StringBuilder((int) (l(s) * 1.5)); quoteBorderless_impl(s, out); return out.toString(); } public static void quoteBorderless_impl(String s, StringBuilder out) { int l = s.length(); for (int i = 0; i < l; i++) { char c = s.charAt(i); if (c == '\\' || c == '"') out.append('\\').append(c); else if (c == '\r') out.append("\\r"); else if (c == '\n') out.append("\\n"); else out.append(c); } } public static int hashCodeFor(Object a) { return a == null ? 0 : a.hashCode(); } public static String aGlobalID() { return randomID(globalIDLength()); } public static int intSum(Collection c, String field) { int sum = 0; for (Object o : unnull(c)) sum += toInt(getOpt(o, field)); return sum; } public static int intSum(Iterable l) { int sum = 0; for (Integer i : unnull(l)) if (i != null) sum += i; return sum; } public static double doubleRatio(double x, double y) { return y == 0 ? 0 : x / y; } public static List ai_mapCodeTokensWithMap(List tok, final Map map) { return mapCodeTokens(tok, new F1() { public String get(String s) { try { return getOrKeep_tryUncurlied(map, s); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "getOrKeep_tryUncurlied(map, s)"; } }); } public static boolean stdEq2(Object a, Object b) { if (a == null) return b == null; if (b == null) return false; if (a.getClass() != b.getClass()) return false; for (String field : allFields(a)) if (neq(getOpt(a, field), getOpt(b, field))) return false; return true; } public static int stdHash2(Object a) { if (a == null) return 0; return stdHash(a, toStringArray(allFields(a))); } public static void fixACCInClassLoader(Object o) { try { AccessControlContext acc = vm_globalACC(); if (acc != null) replaceACCInClassLoader(o, acc); } catch (Throwable __e) { _handleException(__e); } } public static String systemHashCodeHex(Object o) { return intToHex(identityHashCode(o)); } public static UnsupportedOperationException unsupportedOperation() { throw new UnsupportedOperationException(); } public static long sysTime() { return sysNow(); } public static void change() { callOpt(getOptMC("mainConcepts"), "allChanged"); } public static void hotwire_addSharedClasses(Collection classes) { addAll(hotwire_classesToShare, map("className", classes)); } public static void hotwire_addSharedClasses(Class... classes) { hotwire_addSharedClasses(asList(classes)); } public static List concatLists_conservative(List a, List b) { if (empty(a)) return b; if (empty(b)) return a; return concatLists(a, b); } public static List concatLists_conservative(Collection a, Collection b) { if (empty(a) && b instanceof List) return ((List) b); if (empty(b) && a instanceof List) return ((List) a); return concatLists(a, b); } public static List nempties(Collection c) { return filterNempty(c); } public static String rep(int n, char c) { return repeat(c, n); } public static String rep(char c, int n) { return repeat(c, n); } public static List rep(A a, int n) { return repeat(a, n); } public static List rep(int n, A a) { return repeat(n, a); } public static String tok_dropStartingWithWord(String word, String s) { List tok = javaTokWithBrackets(s); int i = indexOfIC(tok, word); return i < 0 ? s : joinSubList(tok, 0, i - 1); } public static List tok_groupPhrase(List tok, String phrase) { List tokPhrase = dropFirstAndLast(javaTok_cached(phrase)); List l = indicesOfSubListIC(tok, tokPhrase); for (int i : l) { int j = i + l(tokPhrase) - 1; if (i == 1 && j == l(tok) - 2) continue; String pre = get(tok, i - 1), post = get(tok, j + 1); if (endsWith(pre, "{") && startsWith(post, "}")) continue; tokPrepend(tok, i, "{"); tokAppend(tok, j, "}"); } return tok; } public static String tok_groupPhrase(String s, String phrase) { return join(tok_groupPhrase(javaTok(s), phrase)); } public static List addDyn(List l, A a) { if (l == null) l = new ArrayList(); l.add(a); return l; } public static void listSet(List l, int i, A a, A emptyElement) { if (i < 0) return; while (i >= l(l)) l.add(emptyElement); l.set(i, a); } public static void listSet(List l, int i, A a) { listSet(l, i, a, null); } public static boolean swic_trim(String a, String b, Matches m) { if (!swic(a, b)) return false; m.m = new String[] { trim(substring(a, l(b))) }; return true; } public static Pair listToPair(List l) { return l(l) != 2 ? null : pair(first(l), second(l)); } public static A pairA(Pair p) { return p == null ? null : p.a; } public static String[] match2x(List pat, List tok) { ArrayList result = new ArrayList(); if (pat.size() != tok.size()) return null; for (int i = 1; i < pat.size(); i += 2) { String p = pat.get(i), t = tok.get(i); boolean match; if (eq(p, "*")) match = true; else if (eq(p, "")) match = isQuoted(t); else if (eq(p, "")) match = isIdentifier(t); else if (eq(p, "")) match = isInteger(t); else { if (!eq(p, t)) return null; continue; } if (!match) return null; result.add(t); } return result.toArray(new String[result.size()]); } public static TreeSet mapCISet(Object f, Iterable l) { return mapToCISet(f, l); } public static Set ai_wordsInBothSidesOfPair(Pair p) { return filterCISet("hasLetterOrDigit", ciSetIntersection(javaTokWithBracketsPlusJavaTokC(p.a), javaTokWithBracketsPlusJavaTokC(p.b))); } public static A findWhere(Collection c, Object... data) { if (c != null) for (A x : c) if (checkFields(x, data)) return x; return null; } public static String tag(String tag) { return htag(tag); } public static String tag(String tag, Object contents, Object... params) { return htag(tag, str(contents), params); } public static String tag(String tag, StringBuilder contents, Object... params) { return htag(tag, contents, params); } public static String tag(String tag, StringBuffer contents, Object... params) { return htag(tag, contents, params); } public static String shortenClassName(String name) { if (name == null) return null; int i = lastIndexOf(name, "$"); if (i < 0) i = lastIndexOf(name, "."); return i < 0 ? name : substring(name, i + 1); } public static A shallowClone(A o) { return (A) shallowClone_impl(o); } public static Object shallowClone_impl(Object o) { if (o == null) return o; if (o instanceof List) return cloneList((List) o); if (o instanceof Map) return cloneMap(((Map) o)); if (o instanceof String || o instanceof Number || o instanceof Boolean) return o; if (o instanceof Object[]) { Object[] l = (Object[]) o; return l.clone(); } Class c = o.getClass(); final Object clone = nuEmptyObject(c); copyFields(o, clone); return clone; } public static void dm_inQ(Runnable r) { dm_q().add(r); } public static MultiSet dm_gazelle_statementsForRule_multiSet(String ruleID) { List concepts = cloneList((List) dm_requireAndCall("#1021413/AppliedRules", "feedbackForRule", ruleID)); MultiSet statements = new MultiSet(); for (Object c : concepts) { try { String judgement = getString("judgement", c); String struct = getString("matchedRuleStruct", c); RuleEngine2_MatchedRule mr = (RuleEngine2_MatchedRule) (unstructureUnlessContainsAnonymousClasses(struct)); if (mr == null) continue; statements.addAll(ai_gazelle_statementsFromAppliedRule(mr, judgement)); } catch (Throwable __e) { _handleException(__e); } } return statements; } public static String loadCachedTranspilation(String id) { try { return loadTextFilePossiblyGZipped(getCachedTranspilationFile(id)); } catch (Throwable __e) { return null; } } public static boolean machineIsOffline() { return isFalse(callF(vmGeneralMap_get("areWeOnline"))); } public static boolean isOfflineMode() { return eq("1", trim(loadProgramTextFile("#1005806", "offline-mode"))); } public static boolean isLocalSnippet(String snippetID) { return isLocalSnippetID(snippetID); } public static boolean isLocalSnippet(long snippetID) { return isLocalSnippetID(snippetID); } public static String getServerTranspiled(String snippetID) { return getServerTranspiled(snippetID, null); } public static String getServerTranspiled(String snippetID, String expectedMD5) { try { long id = parseSnippetID(snippetID); String text = loadPage_utf8(tb_mainServer() + "/tb-int/get-transpiled.php?raw=1&withlibs=1&id=" + id + "&utf8=1" + (l(expectedMD5) > 1 ? "&md5=" + urlencode(expectedMD5) : "") + standardCredentials()); if (nempty(text) && neq(text, "SAME")) saveTranspiledCode(snippetID, text); return text; } catch (Exception __e) { throw rethrow(__e); } } public static void printExceptionShort(Throwable e) { print(exceptionToStringShort(e)); } public static HashSet allClasses_keywords = lithashset("class", "interface", "enum", "sclass", "sinterface", "record", "srecord"); public static List> allClasses(List tok) { List> l = new ArrayList(); int n = tok.size(); HashSet _allClasses_keywords = allClasses_keywords; for (int i = 1; i < n; i += 2) { String t = tok.get(i); if ("{".equals(t)) i = findEndOfBlock(tok, i) - 1; else if (_allClasses_keywords.contains(t) && (i == 1 || !eqGetOneOf(tok, i - 2, ".", "include"))) { int j = i; while (j < n && !tok.get(j).equals("{")) j += 2; j = findEndOfBlock(tok, j) + 1; i = leftScanModifiers(tok, i); l.add(tok.subList(i - 1, Math.min(n, j))); i = j - 2; } } return l; } public static List> allClasses(String text) { return allClasses(javaTok(text)); } public static String getClassDeclarationName(List tok) { if (tok != null) for (int i = 1; i + 2 < tok.size(); i += 2) if (allClasses_keywords.contains(tok.get(i)) && isIdentifier(tok.get(i + 2))) return tok.get(i + 2); return null; } public static List findBlock(String pat, List tok) { List tokpat = javaTok(pat); int i = findCodeTokens(tok, toStringArray(codeTokensOnly(tokpat))); if (i < 0) return null; int bracketIdx = i + tokpat.size() - 3; assertEquals("{", tok.get(bracketIdx)); int endIdx = findEndOfBlock(tok, bracketIdx); return subList(tok, i - 1, endIdx + 1); } public static String dummyMainClassName(String progID) { return "m" + psI(progID); } public static File tempDir() { return makeTempDir(); } public static String tok_packageName(List tok) { int i = jfind(tok, "package"); if (i < 0) return ""; i += 2; int j = jfind(tok, i, ";"); if (j < 0) return ""; return join(codeTokensOnly(subList(tok, i - 1, j))); } public static String tok_firstClassName(List tok) { int i = jfind(tok, "class "); return i < 0 ? null : tok.get(i + 2); } public static File tempDirPossiblyInRAMDisk() { File f = linux_fileInRamDisk(aGlobalID()); if (f != null) { f.mkdirs(); return f; } return makeTempDir(); } public static boolean loadLibraryOrSrcLib_srcLibsEnabled = true; public static File loadLibraryOrSrcLib(String snippetID) { try { long id = parseSnippetID(snippetID); boolean srcLib = loadLibraryOrSrcLib_srcLibsEnabled && isMarkedAsSrcLib(snippetID); if (srcLib) return pairA(hotwire_compile(snippetID)); File f = DiskSnippetCache_getLibrary(id); if (fileSize(f) != 0) return f; try { return loadDataSnippetToFile(snippetID); } catch (Throwable e) { if (loadLibraryOrSrcLib_srcLibsEnabled && nempty(loadSnippet(snippetID))) { markAsSrcLib(snippetID); return pairA(hotwire_compile(snippetID)); } throw rethrow(e); } } catch (Exception __e) { throw rethrow(__e); } } public static File pathToJavaxJar() { try { int x = latestInstalledJavaX(); File xfile = new File(userHome(), ".javax/x" + Math.max(x, 30) + ".jar"); if (!xfile.isFile()) { print("Saving " + f2s(xfile)); String url = "http://tinybrain.de/x30.jar"; byte[] data = loadBinaryPage(url); if (data.length < 1000000) throw fail("Could not load " + url); saveBinaryFile(xfile.getPath(), data); } return xfile; } catch (Exception __e) { throw rethrow(__e); } } public static String cleanJavaCompilerOutput(String s) { return dropPrefixTrim("Annotation processing got disabled, since it requires a 1.6 compliant JVM", s); } public static boolean neqic(String a, String b) { return !eqic(a, b); } public static String appendWithNewLine(String a, String b) { if (empty(b)) return a; if (empty(a)) return b; return addSuffix(a, "\n") + b; } public static boolean anyFileWithExtensionInDir(File dir, String ext) { return nempty(filesWithExtension(ext, findAllFiles_noDirs(dir))); } public static boolean dir2zip_recurse_verbose; public static int dir2zip_recurse(File inDir, File zip) { return dir2zip_recurse(inDir, zip, ""); } public static int dir2zip_recurse(File inDir, File zip, String outPrefix) { try { mkdirsForFile(zip); FileOutputStream fout = newFileOutputStream(zip); ZipOutputStream outZip = new ZipOutputStream(fout); try { return dir2zip_recurse(inDir, outZip, outPrefix, 0); } finally { outZip.close(); } } catch (Exception __e) { throw rethrow(__e); } } public static int dir2zip_recurse(File inDir, ZipOutputStream outZip) { return dir2zip_recurse(inDir, outZip, "", 0); } public static int dir2zip_recurse(File inDir, ZipOutputStream outZip, String outPrefix, int level) { try { if (++level >= 20) throw fail("woot? 20 levels in zip?"); List files = new ArrayList(); for (File f : listFiles(inDir)) files.add(f); int n = 0; sortFilesByName(files); for (File f : files) { if (f.isDirectory()) { print("dir2zip_recurse: Scanning " + f.getAbsolutePath()); n += dir2zip_recurse(f, outZip, outPrefix + f.getName() + "/", level); } else { if (dir2zip_recurse_verbose) print("Copying " + f.getName()); outZip.putNextEntry(new ZipEntry(outPrefix + f.getName())); InputStream fin = new FileInputStream(f); copyStream(fin, outZip); fin.close(); ++n; } } return n; } catch (Exception __e) { throw rethrow(__e); } } public static boolean isInRAMDisk(File f) { return startsWith(f2s(f), "/dev/shm"); } public static void deleteDirectory(File dir) { deleteDirectory(dir, false, false); } public static void deleteDirectory(File dir, boolean verbose, boolean testRun) { deleteAllFilesInDirectory(dir, verbose, testRun); if (verbose) print((testRun ? "Would delete " : "Deleting ") + dir.getAbsolutePath()); if (!testRun) dir.delete(); } public static void closeRandomAccessFile(RandomAccessFile f) { if (f != null) try { f.close(); callJavaX("dropIO", f); } catch (Throwable e) { printStackTrace(e); } } public static RandomAccessFile newRandomAccessFile(File path, String mode) throws IOException { boolean forWrite = mode.indexOf('w') >= 0; if (forWrite) mkdirsForFile(path); RandomAccessFile f = new RandomAccessFile(path, mode); callJavaX("registerIO", f, path, forWrite); return f; } public static int randomID_defaultLength = 12; public static String randomID(int length) { return makeRandomID(length); } public static String randomID() { return randomID(randomID_defaultLength); } public static int globalIDLength() { return 16; } public static List mapCodeTokens(Object f, List l) { List out = new ArrayList(); for (int i = 0; i < l(l); i++) { Object o = l.get(i); out.add(odd(i) ? callF(f, o) : o); } return out; } public static List mapCodeTokens(List l, Object f) { return mapCodeTokens(f, l); } public static String getOrKeep_tryUncurlied(Map map, String a) { String v = map.get(a); if (v != null) return v; String b = uncurly(a); if (a != b && (v = map.get(b)) != null) return v; return a; } public static Set allFields(Object o) { TreeSet fields = new TreeSet(); Class _c = _getClass(o); do { for (Field f : _c.getDeclaredFields()) fields.add(f.getName()); _c = _c.getSuperclass(); } while (_c != null); return fields; } public static int stdHash(Object a, String... fields) { if (a == null) return 0; int hash = getClassName(a).hashCode(); for (String field : fields) hash = hash * 2 + hashCode(getOpt(a, field)); return hash; } public static String[] toStringArray(Collection c) { String[] a = new String[l(c)]; Iterator it = c.iterator(); for (int i = 0; i < l(a); i++) a[i] = it.next(); return a; } public static String[] toStringArray(Object o) { if (o instanceof String[]) return (String[]) o; else if (o instanceof Collection) return toStringArray((Collection) o); else throw fail("Not a collection or array: " + getClassName(o)); } public static AccessControlContext vm_globalACC() { return (AccessControlContext) vm_generalMap_get("Global ACC"); } public static void replaceACCInClassLoader(Object o, AccessControlContext newACC) { ClassLoader cl = getClassLoaderOrSame(o); if (cl instanceof URLClassLoader) { try { setOpt(cl, "acc", newACC); try { setOpt(getOpt(cl, "ucp"), "acc", newACC); } catch (Throwable e) { printShortException(e); if (java10OrHigher()) if (addDefaultVMOption("--add-opens java.base/jdk.internal.loader=ALL-UNNAMED")) print("Please restart the OS"); } } catch (Throwable __e) { _handleException(__e); } } } public static String intToHex(int i) { return bytesToHex(intToBytes(i)); } public static Object getOptMC(String field) { return getOpt(mc(), field); } public static List filterNempty(Collection c) { List l = new ArrayList(); for (String x : unnull(c)) if (nempty(x)) l.add(x); return l; } public static Map> javaTok_cached_cache = synchronizedMRUCache(100); public static List javaTok_cached(String s) { List tok = javaTok_cached_cache.get(s); if (tok == null) javaTok_cached_cache.put(s, tok = javaTok(s)); return tok; } public static List indicesOfSubListIC(List x, List y) { List l = new ArrayList(); int i = 0; while (true) { int j = indexOfSubListIC(x, y, i); if (j < 0) break; l.add(j); i = j + 1; } return l; } public static boolean swic(String a, String b) { return startsWithIgnoreCase(a, b); } public static boolean swic(String a, String b, Matches m) { if (!swic(a, b)) return false; m.m = new String[] { substring(a, l(b)) }; return true; } public static TreeSet mapToCISet(Object f, Iterable l) { TreeSet x = ciSet(); if (l != null) for (Object o : l) addIfNotNull(x, (String) callF(f, o)); return x; } public static Set filterCISet(Iterable c, Object pred) { Set x = ciSet(); if (c != null) for (String o : c) if (isTrue(callF(pred, o))) x.add(o); return x; } public static Set filterCISet(Object pred, Iterable c) { return filterCISet(c, pred); } public static boolean hasLetterOrDigit(String s) { return containsLetterOrDigit(s); } public static Set ciSetIntersection(Collection a, Collection b) { Set set = ciSet(); Set bSet = asCISet(b); for (String x : a) if (bSet.contains(x)) set.add(x); return set; } public static List javaTokWithBracketsPlusJavaTokC(String s) { return concatLists(javaTokPlusBracketsC(s), javaTokC(s)); } public static boolean checkFields(Object x, Object... data) { for (int i = 0; i < l(data); i += 2) if (neq(getOpt(x, (String) data[i]), data[i + 1])) return false; return true; } public static String htag(String tag) { return htag(tag, ""); } public static String htag(String tag, Object contents, Object... params) { String openingTag = hopeningTag(tag, params); String s = str(contents); if (empty(s) && neqic(tag, "script")) return dropLast(openingTag) + "/>"; return openingTag + s + ""; } public static int lastIndexOf(String a, String b) { return a == null || b == null ? -1 : a.lastIndexOf(b); } public static int lastIndexOf(String a, char b) { return a == null ? -1 : a.lastIndexOf(b); } public static A copyFields(Object x, A y, String... fields) { if (empty(fields)) { Map map = objectToMap(x); for (String field : map.keySet()) setOpt(y, field, map.get(field)); } else for (String field : fields) { Object o = getOpt(x, field); if (o != null) setOpt(y, field, o); } return y; } public static A copyFields(Object x, A y, Collection fields) { return copyFields(x, y, asStringArray(fields)); } public static String loadTextFilePossiblyGZipped(String fileName) { return loadTextFilePossiblyGZipped(fileName, null); } public static String loadTextFilePossiblyGZipped(String fileName, String defaultContents) { File gz = new File(fileName + ".gz"); return gz.exists() ? loadGZTextFile(gz) : loadTextFile(fileName, defaultContents); } public static String loadTextFilePossiblyGZipped(File fileName) { return loadTextFilePossiblyGZipped(fileName, null); } public static String loadTextFilePossiblyGZipped(File fileName, String defaultContents) { return loadTextFilePossiblyGZipped(fileName.getPath(), defaultContents); } public static File getCachedTranspilationFile(String id) { return newFile(getCodeProgramDir(id), "Transpilation"); } public static Object vmGeneralMap_get(Object key) { return vm_generalMap_get(key); } public static String loadProgramTextFile(String name) { return loadTextFile(getProgramFile(name)); } public static String loadProgramTextFile(String progID, String name) { return loadTextFile(getProgramFile(progID, name)); } public static String loadProgramTextFile(String progID, String name, String defaultText) { return loadTextFile(getProgramFile(progID, name), defaultText); } public static boolean isLocalSnippetID(String snippetID) { return isSnippetID(snippetID) && isLocalSnippetID(psI(snippetID)); } public static boolean isLocalSnippetID(long snippetID) { return snippetID >= 1000 && snippetID <= 9999; } public static String standardCredentials() { String user = standardCredentialsUser(); String pass = standardCredentialsPass(); if (nempty(user) && nempty(pass)) return "&_user=" + urlencode(user) + "&_pass=" + urlencode(pass); return ""; } public static void saveTranspiledCode(String progID, String code) { File dir = getCodeProgramDir(progID); new File(dir, "Transpilation").delete(); saveGZTextFile(new File(dir, "Transpilation.gz"), code); } public static int leftScanModifiers(List tok, int i) { List mod = getJavaModifiers(); while (i > 1 && mod.contains(tok.get(i - 2))) i -= 2; return i; } public static A assertEquals(Object x, A y) { return assertEquals(null, x, y); } public static A assertEquals(String msg, Object x, A y) { if (assertVerbose()) return assertEqualsVerbose(msg, x, y); if (!(x == null ? y == null : x.equals(y))) throw fail((msg != null ? msg + ": " : "") + y + " != " + x); return y; } public static File makeTempDir() { return (File) call(getJavaX(), "TempDirMaker_make"); } public static File linux_fileInRamDisk(String name) { if (!isLinux()) return null; File dir = newFile("/dev/shm"); if (dir.isDirectory()) return newFile(dir, name); return null; } public static boolean isMarkedAsSrcLib(String snippetID) { return snippetID != null && fileExists(javaxCodeDir("srclibs/" + psI(snippetID))); } public static Object hotwire_onCompile; public static boolean hotwire_serially; public static Lock hotwire_overInternalBot_lock = lock(); public static boolean hotwire_compileOnServer; public static Class hotwire_overInternalBot(String progID) { return hotwire_overInternalBot(progID, "main"); } public static Class hotwire_overInternalBot(String progID, String mainClass) { try { Pair p = hotwire_compile(progID); File jar = p.a; assertTrue(jar.getAbsolutePath(), jar.isFile()); List files = hotwire_collectJars(jar); JavaXClassLoader classLoader = hotwire_makeClassLoader(files); classLoader.progID = progID; return hotwire_finish(classLoader, progID, p.b, mainClass); } catch (Exception __e) { throw rethrow(__e); } } public static Pair hotwire_compile(String progID) { Pair p = hotwire_compileOnServer && !isLocalSnippetID(progID) ? compileSnippetThroughServer(progID) : CompilerBot.compileSnippet2(progID); Lock __3122 = hotwire_serially ? hotwire_overInternalBot_lock : null; lock(__3122); try { callF(hotwire_onCompile, p); return p; } finally { unlock(__3122); } } public static void markAsSrcLib(String snippetID) { saveTextFile(javaxCodeDir("srclibs/" + psI(snippetID)), ""); } public static int latestInstalledJavaX() { File[] files = new File(userHome(), ".javax").listFiles(); int v = 0; if (files != null) for (File f : files) { Matcher m = Pattern.compile("x(\\d\\d\\d?)\\.jar").matcher(f.getName()); if (m.matches()) v = Math.max(v, Integer.parseInt(m.group(1))); } return v; } public static String dropPrefixTrim(String prefix, String s) { return trim(dropPrefix(prefix, s)); } public static String addSuffix(String s, String suffix) { return s == null || s.endsWith(suffix) ? s : s + suffix; } public static List filesWithExtension(String ext, List files) { return filesEndingWith(files, addPrefixIfNotEmpty2(".", ext)); } public static List findAllFiles_noDirs(List dirs) { return findAllFiles_noDirs(asObjectArray(dirs)); } public static List findAllFiles_noDirs(Object... dirs) { List l = new ArrayList(); for (Object dir : dirs) { if (dir instanceof String && ((String) dir).endsWith("/.")) { for (File f : listFiles(dropSuffix("/.", (String) dir))) if (f.isFile()) l.add(f); } else findAllFiles_noDirs_impl(toFile(dir), l); } return l; } public static void findAllFiles_noDirs_impl(File dir, List l) { for (File f : listFiles(dir)) { if (f.isDirectory()) findAllFiles_noDirs_impl(f, l); else l.add(f); } } public static File[] listFiles(File dir) { File[] files = dir.listFiles(); return files == null ? new File[0] : files; } public static File[] listFiles(String dir) { return listFiles(new File(dir)); } public static List sortFilesByName(List l) { sort(l, new Comparator() { public int compare(File a, File b) { return stdcompare(a.getName(), b.getName()); } }); return l; } public static int deleteAllFilesInDirectory_minPathLength = 10; public static void deleteAllFilesInDirectory(File dir) { deleteAllFilesInDirectory(dir, false, false); } public static void deleteAllFilesInDirectory(File dir, boolean verbose, boolean testRun) { dir = getCanonicalFile(dir); assertTrue(f2s(dir), l(f2s(dir)) >= deleteAllFilesInDirectory_minPathLength); File[] files = dir.listFiles(); if (files == null) return; for (File f : files) { if (!isSymLink(f) && f.isDirectory()) deleteDirectory(f, verbose, testRun); else { if (verbose) print((testRun ? "Would delete " : "Deleting ") + f.getAbsolutePath()); if (!testRun) f.delete(); } } } public static Object callJavaX(String method, Object... args) { return callOpt(getJavaX(), method, args); } public static int hashCode(Object a) { return a == null ? 0 : a.hashCode(); } public static ClassLoader getClassLoaderOrSame(Object o) { return o instanceof ClassLoader ? (ClassLoader) o : getClassLoader(o); } public static void printShortException(Throwable e) { print(exceptionToStringShort(e)); } public static boolean java10OrHigher() { return parseFirstInt(javaVersion()) >= 10; } public static boolean addDefaultVMOption(String option) { String s = defaultVMArgs(), old = s; if (!s.contains(option)) s = trim(s + " " + option); if (eq(old, s)) return false; { setDefaultVMArgs(s); return true; } } public static byte[] intToBytes(int i) { return new byte[] { (byte) (i >>> 24), (byte) (i >>> 16), (byte) (i >>> 8), (byte) i }; } public static int indexOfSubListIC(List x, List y) { return indexOfSubListIC(x, y, 0); } public static int indexOfSubListIC(List x, List y, int i) { outer: for (; i + l(y) <= l(x); i++) { for (int j = 0; j < l(y); j++) if (neqic(x.get(i + j), y.get(j))) continue outer; return i; } return -1; } public static boolean startsWithIgnoreCase(String a, String b) { return regionMatchesIC(a, 0, b, 0, b.length()); } public static boolean containsLetterOrDigit(String s) { for (int i = 0; i < l(s); i++) if (isLetterOrDigit(s.charAt(i))) return true; return false; } public static List javaTokPlusBracketsC(String s) { return codeTokens(javaTokPlusBrackets(s)); } public static String hopeningTag(String tag, Map params) { return hopeningTag(tag, mapToParams(params)); } public static String hopeningTag(String tag, Object... params) { StringBuilder buf = new StringBuilder(); buf.append("<" + tag); for (int i = 0; i < l(params); i += 2) { String name = (String) get(params, i); Object val = get(params, i + 1); if (nempty(name) && val != null) { if (val == html_valueLessParam()) buf.append(" " + name); else { String s = str(val); if (!empty(s)) buf.append(" " + name + "=" + htmlQuote(s)); } } } buf.append(">"); return str(buf); } public static Map objectToMap(Object o) { try { if (o instanceof Map) return (Map) o; TreeMap map = new TreeMap(); Class c = o.getClass(); while (c != Object.class) { Field[] fields = c.getDeclaredFields(); for (final Field field : fields) { if ((field.getModifiers() & Modifier.STATIC) != 0) continue; field.setAccessible(true); final Object value = field.get(o); if (value != null) map.put(field.getName(), value); } c = c.getSuperclass(); } if (o instanceof DynamicObject) map.putAll(((DynamicObject) o).fieldValues); return map; } catch (Exception __e) { throw rethrow(__e); } } public static List> objectToMap(Iterable l) { if (l == null) return null; List x = new ArrayList(); for (Object o : l) x.add(objectToMap(o)); return x; } public static String[] asStringArray(Collection c) { return toStringArray(c); } public static String[] asStringArray(Object o) { return toStringArray(o); } public static String loadGZTextFile(File file) { try { if (!file.isFile()) return null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); InputStream fis = new FileInputStream(file); GZIPInputStream gis = newGZIPInputStream(fis); try { byte[] buffer = new byte[1024]; int len; while ((len = gis.read(buffer)) != -1) { baos.write(buffer, 0, len); } } finally { fis.close(); } baos.close(); return fromUtf8(baos.toByteArray()); } catch (Exception __e) { throw rethrow(__e); } } public static File getCodeProgramDir() { return getCodeProgramDir(getProgramID()); } public static File getCodeProgramDir(String snippetID) { return new File(javaxCodeDir(), formatSnippetID(snippetID)); } public static File getCodeProgramDir(long snippetID) { return getCodeProgramDir(formatSnippetID(snippetID)); } public static String standardCredentialsUser() { return trim(loadTextFile(oneOfTheFiles(javaxSecretDir("tinybrain-username"), userDir(".tinybrain/username")))); } public static String standardCredentialsPass() { return trim(loadTextFile(oneOfTheFiles(javaxSecretDir("tinybrain-userpass"), userDir(".tinybrain/userpass")))); } public static void saveGZTextFile(File file, String contents) { try { File parentFile = file.getParentFile(); if (parentFile != null) parentFile.mkdirs(); String tempFileName = file.getPath() + "_temp"; File tempFile = new File(tempFileName); if (contents != null) { if (tempFile.exists()) try { String saveName = tempFileName + ".saved." + now(); copyFile(tempFile, new File(saveName)); } catch (Throwable e) { printStackTrace(e); } FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath()); GZIPOutputStream gos = new GZIPOutputStream(fileOutputStream); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(gos, "UTF-8"); PrintWriter printWriter = new PrintWriter(outputStreamWriter); printWriter.print(contents); printWriter.close(); gos.close(); fileOutputStream.close(); } if (file.exists() && !file.delete()) throw new IOException("Can't delete " + file.getPath()); if (contents != null) if (!tempFile.renameTo(file)) throw new IOException("Can't rename " + tempFile + " to " + file); } catch (Exception __e) { throw rethrow(__e); } } public static List getJavaModifiers_list = litlist("static", "abstract", "public", "private", "protected", "final", "native", "volatile", "synchronized", "transient"); public static List getJavaModifiers() { return getJavaModifiers_list; } public static ThreadLocal assertVerbose_value = new ThreadLocal(); public static void assertVerbose(boolean b) { assertVerbose_value.set(b); } public static boolean assertVerbose() { return isTrue(assertVerbose_value.get()); } public static A assertEqualsVerbose(Object x, A y) { assertEqualsVerbose((String) null, x, y); return y; } public static A assertEqualsVerbose(String msg, Object x, A y) { if (!eq(x, y)) { throw fail((msg != null ? msg + ": " : "") + (y) + " != " + (x)); } else print("OK: " + (x)); return y; } public static Cache isLinux_cache = new Cache("isLinux_load"); public static boolean isLinux() { return isLinux_cache.get(); } public static Boolean isLinux_load() { return !isWindows() && !isMac() && !isAndroid(); } public static File javaxCodeDir_dir; public static File javaxCodeDir() { return javaxCodeDir_dir != null ? javaxCodeDir_dir : new File(userHome(), "JavaX-Code"); } public static File javaxCodeDir(String sub) { return newFile(javaxCodeDir(), sub); } public static List hotwire_collectJars(File jar) { List libIDs = hotwire_libraryIDsFromJar_deleteJarOnFail(jar); List files = ll(jar); for (String libID : libIDs) files.add(loadLibraryOrSrcLib(libID)); return files; } public static Class hotwire_finish(ClassLoader classLoader, String progID, String javaSource) { return hotwire_finish(classLoader, progID, javaSource, "main"); } public static Class hotwire_finish(ClassLoader classLoader, String progID, String javaSource, String mainClass) { try { Class theClass = classLoader.loadClass(mainClass); Class j = getJavaX(); setOpt(theClass, "myJavaSource_code", javaSource); synchronized (j) { call(j, "setVars", theClass, progID); callOpt(j, "addInstance", progID, theClass); } hotwire_copyOver(theClass); return theClass; } catch (Exception __e) { throw rethrow(__e); } } public static Pair compileSnippetThroughServer(String progID) { String transpiledSrc = getServerTranspiled2(progID); String md5 = md5(transpiledSrc + "\n" + progID); File jar = CompilerBot.getJarFile(md5); if (jar == null || jar.length() <= 22) { byte[] jarData = null; try { jarData = loadBinaryPage("http://www.botcompany.de/jar/" + psI(progID) + "?md5=" + md5(transpiledSrc)); } catch (Throwable __e) { _handleException(__e); } if (!isJAR(jarData)) { print(bytesToHex(takeFirstOfByteArray(8, jarData))); print("fallback to CompilerBot: " + fromUtf8(takeFirstOfByteArray(80, jarData))); return CompilerBot.compileSnippet2(progID); } saveBinaryFile(jar, jarData); } return pair(jar, transpiledSrc); } public static List filesEndingWith(File dir, String suffix) { return listFilesWithSuffix(dir, suffix); } public static List filesEndingWith(List l, String suffix) { List out = new ArrayList(); for (File f : unnull(l)) if (!f.isDirectory() && (empty(suffix) || endsWithIgnoreCase(f.getName(), suffix))) out.add(f); return out; } public static String addPrefixIfNotEmpty2(String prefix, String s) { return empty(s) ? "" : addPrefix(prefix, s); } public static Object[] asObjectArray(List l) { return toObjectArray(l); } public static File toFile(Object o) { if (o instanceof File) return (File) o; if (o instanceof String) return new File((String) o); throw fail("Not a file: " + o); } public static File getCanonicalFile(File f) { try { return f == null ? null : f.getCanonicalFile(); } catch (Exception __e) { throw rethrow(__e); } } public static boolean isSymLink(File f) { return f != null && Files.isSymbolicLink(toPath(f)); } public static ClassLoader getClassLoader(Object o) { return o == null ? null : _getClass(o).getClassLoader(); } public static int parseFirstInt(String s) { return parseInt(jextract("", s)); } public static String javaVersion() { return System.getProperty("java.version"); } public static String defaultVMArgs() { return javaxDefaultVMArgs(); } public static void setDefaultVMArgs(String args) { String oldArgs = javaxDefaultVMArgs(); args = trim(args); if (neq(unnull(oldArgs), unnull(args))) { print(); print("Changing default VM arguments from"); print(" " + oldArgs); print("to"); print(" " + args); print(); saveTextFile(javaxDataDir("default-vm-args"), nullIfEmpty(args)); } } public static boolean isLetterOrDigit(char c) { return Character.isLetterOrDigit(c); } public static Object html_valueLessParam_cache; public static Object html_valueLessParam() { if (html_valueLessParam_cache == null) html_valueLessParam_cache = html_valueLessParam_load(); return html_valueLessParam_cache; } public static Object html_valueLessParam_load() { return new Object(); } public static String htmlQuote(String s) { return "\"" + htmlencode_forParams(s) + "\""; } public static File oneOfTheFiles(String... paths) { if (paths != null) for (String path : paths) if (fileExists(path)) return newFile(path); return null; } public static File oneOfTheFiles(File... files) { if (files != null) for (File f : files) if (fileExists(f)) return f; return null; } public static boolean isWindows() { return System.getProperty("os.name").contains("Windows"); } public static boolean isMac() { return System.getProperty("os.name").toLowerCase().contains("mac"); } public static List hotwire_libraryIDsFromJar_deleteJarOnFail(File jar) { try { return hotwire_libraryIDsFromJar(jar); } catch (Throwable _e) { jar.delete(); throw rethrow(_e); } } public static byte[] isJAR_magic = bytesFromHex("504B0304"); public static boolean isJAR(byte[] data) { return byteArrayStartsWith(data, isJAR_magic); } public static boolean isJAR(File f) { return isJAR(loadBeginningOfBinaryFile(f, l(isJAR_magic))); } public static byte[] takeFirstOfByteArray(byte[] b, int n) { return subByteArray(b, 0, n); } public static byte[] takeFirstOfByteArray(int n, byte[] b) { return takeFirstOfByteArray(b, n); } public static List listFilesWithSuffix(File dir, String suffix) { List l = new ArrayList(); for (File f : listFiles(dir)) if (!f.isDirectory() && (empty(suffix) || endsWithIgnoreCase(f.getName(), suffix))) l.add(f); return l; } public static String addPrefix(String prefix, String s) { return s.startsWith(prefix) ? s : prefix + s; } public static Path toPath(File f) { return f == null ? null : f.toPath(); } public static String jextract(String pat, String s) { return jextract(pat, javaTok(s)); } public static String jextract(String pat, List tok) { List tokpat = javaTok(pat); jfind_preprocess(tokpat); int i = jfind(tok, tokpat); if (i < 0) return null; int j = i + l(tokpat) - 2; return joinSubList(tok, i, j); } public static String javaxDefaultVMArgs() { File fileNew, fileOld; String text = loadTextFile(fileNew = javaxDataDir("default-vm-args")); if (text == null) { text = loadTextFile(fileOld = programFile("#1005850", "default-vm-args")); if (text != null) moveFile(fileOld, fileNew); } return trim(unnull(text)); } public static String htmlencode_forParams(String s) { if (s == null) return ""; StringBuilder out = new StringBuilder(Math.max(16, s.length())); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c > 127 || c == '"' || c == '<' || c == '>') { out.append("&#"); out.append((int) c); out.append(';'); } else out.append(c); } return out.toString(); } public static List hotwire_libraryIDsFromJar(File jar) { String dehlibs = unnull(loadTextFileFromZip(jar, "libraries")); return regexpExtractAll("\\d+", dehlibs); } public static byte[] bytesFromHex(String s) { return hexToBytes(s); } public static boolean byteArrayStartsWith(byte[] a, byte[] b) { if (a == null || b == null) return false; if (a.length < b.length) return false; for (int i = 0; i < b.length; i++) if (a[i] != b[i]) return false; return true; } public static byte[] loadBeginningOfBinaryFile(File file, int maxBytes) { return loadBinaryFilePart(file, 0, maxBytes); } public static byte[] subByteArray(byte[] b, int start) { return subByteArray(b, start, l(b)); } public static byte[] subByteArray(byte[] b, int start, int end) { start = max(start, 0); end = min(end, l(b)); if (start == 0 && end == l(b)) return b; if (start >= end) return new byte[0]; byte[] x = new byte[end - start]; System.arraycopy(b, start, x, 0, end - start); return x; } public static File programFile(String name) { return prepareProgramFile(name); } public static File programFile(String progID, String name) { return prepareProgramFile(progID, name); } public static void moveFile(File a, File b) { if (!renameFile(a, b)) throw fail("File move failed: " + a + " to " + b); } public static String loadTextFileFromZip(File inZip, String fileName) { return loadTextFileFromZipFile(inZip, fileName); } public static List regexpExtractAll(String pat, String s) { if (s == null) return null; Matcher m = regexpMatcher(pat, s); List out = new ArrayList(); while (m.find()) out.add(m.group()); return out; } public static byte[] loadBinaryFilePart(File file, long start, long end) { try { RandomAccessFile raf = new RandomAccessFile(file, "r"); int n = toInt(min(raf.length(), end - start)); byte[] buffer = new byte[n]; try { raf.seek(start); raf.readFully(buffer, 0, n); return buffer; } finally { raf.close(); } } catch (Exception __e) { throw rethrow(__e); } } public static File prepareProgramFile(String name) { return mkdirsForFile(getProgramFile(name)); } public static File prepareProgramFile(String progID, String name) { return mkdirsForFile(getProgramFile(progID, name)); } public static boolean renameFile(File a, File b) { mkdirsForFile(b); return a.renameTo(b); } public static boolean renameFile(File a, String newName) { return renameFile(a, fileInSameDir(a, newName)); } public static File fileInSameDir(File f, String newName) { return newFile(parentFile(f), newName); } public static File parentFile(File f) { return dirOfFile(f); } public static File dirOfFile(File f) { return f == null ? null : f.getParentFile(); } }