Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

269
LINES

< > BotCompany Repo | #1008705 - Hyperlink+explain standard functions etc. in JavaX source

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (10125L/75K).

!7

static Set<S> tooComplicatedSnippets = lithashset(#1006654);

static SS shortFor = litmap(
  "LS", "List<String>",
  "sclass", "static class",
  "asclass", "abstract static class",
  "svoid", "static void",
  "SS", "Map<String, String>",
  "S", "String",
  "ret", "return",
  "L", "List",
  "Cl", "Collection",
  "O", "Object",
  "sO", "static Object",
  "sS", "static String",
  "fO", "final Object",
  "fS", "final String",
  "sS", "static String",
  "sbool", "static boolean",
  "fbool", "final boolean",
  "bool", "boolean",
  "Int", "Integer",
  "cast", "casting to the type required on the left-hand side",
  "°", "()",
  "ItIt", "IterableIterator",
  "ES", "Ext<S> (string plus extended information)",
  "CloseableItIt", "CloseableIterableIterator",
  "LPairS", "List<Pair<String>>",
  "ISegmenter", "IF1<BufferedImage, L<Rect>>",
);

sS explFunc = "anonymous function declaration (similar to Java 8 lambdas)";
sS explPNoconsole = "Main program including Substance L&F, started in non-AWT thread, hiding the console";

static SS tokenExplanations = litmap(
  "p-exp {", "main program with automatic upgrade",
  "mapLike", "a function that takes a global function name as first argument and can be called like this: <mapLikeFunction> <otherFunction>(...)",
  "mapMethodLike", "a function that takes a method or field name as first argument and can be called like this: <mapMethodLikeFunction> <methodOrFieldName>(...)",
  "func(", explFunc, "func{", explFunc,
  //"f(", explFunc, "f{", explFunc,
  "voidfunc", "anonymous function declaration without return value (similar to Java 8 lambdas)",
  "ctex", "ctex rethrows exceptions as RuntimeExceptions so you don't have to declare them",
  "p {", "short for: public static void main(String[] args)",
  "p-experiment {", "Main program with a nice big console & auto-restart",
  "p-substance {", "Main program including Substance L&F, started in AWT thread",
  "p-subst {", "Main program including Substance L&F, started in non-AWT thread",
  "p-noconsole {", explPNoconsole,
  "pn {", explPNoconsole,
  "answer {", "Answer function - static S answer(S s) { new Matches m; ...; null; }",
  "time {", "Run the code block and print how long it took",
  "semiauto {", "In a semiauto block, JavaX automatically adds a ; to (almost) every line",
  "autosemi {", "In an autosemi block, JavaX automatically adds a ; to (almost) every line",
  "Clusters", "Clusters<A> is short for Map<A, Collection<A>>",
  "temp", "temp is like try (...) extending to the end of the current block",
  "pcall {", "protected call - try { ... } catch { print exception }",
  "r {", "r { ... } is short for: new Runnable() { public void run() { ... } }",
  "runnable {", "runnable { ... } is short for: new Runnable() { public void run() { ... } }",
  "pcall-short {", "protected call, shorter output - try { ... } catch { printShortException }",
  "LPair<", "LPair<...> is short for: L<Pair<...>>",
  "visualize {", "Visualisation method for an OS module; returns a Swing component",
  "visualize as", "define a visualization for the module",
  "enhanceFrame {", "Method that changes properties of an OS module's frame",
  "run {", "short for: public void run()",
  "start {", "Method that is run when an OS module is loaded",
  "enter {", "Methods in a module should be marked 'enter' for bookkeeping",
  "vf<", "reference to function as voidfunc",
  "compact module", "try to save memory by reusing code between modules",
  "cmodule", "cmodule = compact module for Stefan's OS. compact = try to save memory by reusing code between modules",
  "rThread {", "Runnable in a new thread",
  "shit(", "short for: return with print",
  "shit:", "short for: return with print",
  ":=", [[the := operator translates to a simple comma, but converts an identifier on its left-hand side into a string]],
  "noeq", "don't auto-generate equals+hashCode methods",
  "for single (", "for single is for ping + singletonUnlessNull",
  "selfType", "selfType is replaced with the enclosing class's name",
);

static SS sc, sf;
static StringTree2<S> tokenExplanationsTree;

p {
  clearCaches();
  serveHttp(7777);
  tokenExplanationsTree = stringTree2_javaTok(tokenExplanations);
  printStruct(tokenExplanationsTree);
}

html {
  uri = dropPrefix("/", uri);
  bool real = uri.startsWith("real/");
  if (real) uri = dropPrefix("real/", uri);
  S snippetID = #1007528;
  if (isSnippetID(uri)) snippetID = fsI(uri);
  
  if (contains(tooComplicatedSnippets, snippetID)) ret ""; //fail("too complicated");
  
  S code = loadSnippet(snippetID);
  L<S> tok = javaTok(code);
  
  new Map<Int, S> links; // token index -> sf snippet ID
  new Map<Int, S> explanations; // token index -> explanation text
  
  putMultipleKeys(explanations, allPlus(4, jfindAll(tok, "void <id> q {")),
    "Function will execute in module's queue");
  
  new Matches m;
  for (int i = 1; i < l(tok); i += 2) {
    S t = tok.get(i);
    S prev = get(tok, i-2);
    S next = get(tok, i+2);
    S sfID = sf.get(t);
    
    if (startsWith(t, "lambda", m) && isInteger(m.rest()) && isIdentifier(next))
      explanations.put(i, "lambda reference to a \*m.rest()*/-argument function");
      
    if (eq(t, "autoDispose") && isIdentifier(next))
      explanations.put(i, [[autoDispose adds a cleanMeUp method that properly disposes of this variable]]);
      
    if (eq(t, "virtual") && isIdentifier(next))
      explanations.put(i, [["virtual" represents a type that is not visible in this realm, so it is transpiled to just Object]]);
      
    if (eq(t, "concept") && isIdentifier(next))
      explanations.put(i, "A concept is like a Java class, but persistable");
      
    if (eq(t, "switchable") && isIdentifier(next))
      explanations.put(i, "A field that can be changed through the module's popup menu");
      
    if (eq(t, "embedded") && isIdentifier(next))
      explanations.put(i, [["embedded" allows you to put a function where they would not normally be allowed]]);
      
    if (eq(t, "visual") && isIdentifier(next))
      explanations.put(i, "short definition of the visualize() function");
      
    if (eq(t, "dm_q") && isIdentifier(next))
      explanations.put(i, "Function reference delegating to module queue");
      
    if (eq(t, "!") && isIdentifier(prev) && neq(next, "="))
      explanations.put(i, "! is short for .get()");
      
    /*if (eq(t, "!") && containsNewLine(get(tok, i-1)) && isInteger(next))
      explanations.put(i, "Translator invocation");*/
      
    if (eq(t, "#") && isIdentifier(next))
      explanations.put(i, "#<name> makes an identifier local to the scope");
    
    if (eq(t, "f") && isIdentifier(next))
      explanations.put(i, "f <name> references a static function in the main class");
    
    if (eq(t, "r") && isIdentifier(next))
      explanations.put(i, "short for: r { " + next + "() }");
    
    if (eqOneOf(t, "rThread", "rThreadEnter") && isIdentifier(next))
      explanations.put(i, "short for: " + t + " { " + next + "() }");
    
    if (eq(t, "dispose") && isIdentifier(next))
      explanations.put(i, "short for: cleanUp(" + next + "); " + next + " = null;");
    
    if (eq(t, "*") && eq(next, "("))
      explanations.put(i, "Short syntax for a constructor declaration");
  
    if (eq(t, "thread") && eq(next, "{"))
      explanations.put(i, "Start a new thread with the following code");
      
    if (eq(t, "module") && isIdentifier(next))
      explanations.put(i, "A module is a class that can be loaded in Stefan's OS");
      
    if (eq(t, "record") && isIdentifier(next))
      explanations.put(i, "A record is a value-based class");
      
    if (eq(t, "srecord") && isIdentifier(next))
      explanations.put(i, "An srecord is a static value-based class");
      
    if (eqOneOf(t, "cached", "simplyCached") && isIdentifier(next))
      explanations.put(i, "A function that caches its return value");
      
    if (eq(t, "thread") && isQuoted(next))
      explanations.put(i, "Start a new thread with the following name & code");
      
    if (eq(t, "html") && eq(next, "{"))
      explanations.put(i, "short for: static Object html(String uri, final Map<String, String> params) ctex {");
      
    if (eq(t, 'try) && eq(next, 'answer))
      doublePut(explanations, i, i+2, "\"try answer\" returns the expression if it isn't null or empty");
      
    if (isSingleQuoteIdentifier(t))
      explanations.put(i, "string constant, " + quote(fromSingleQuoteIdentifier(t)));
      
    if (eqOneOf(t, 'null, 'false, 'true, 'this)
      && eq(next, ";")
      && tok_tokenBeforeLonelyReturnValue(tok, i-2))
      doublePut(explanations, i, i+2, "short for: return " + t + ";");
      
    S e = shortFor.get(t);
    if (e != null)
      mapPut(explanations, i, "short for: " + e);
      
    if (!explanations.containsKey(i) && sfID != null) {
      if (eqOneOf(prev, "f", "r", "rThread")
        || startsWith(prev, "lambda")
        || isIdentifier(next) && eqGet(tok, i+4, "(")
        || eqOneOf(next, "(", "°")
          && (neq(prev, ".") || eq(get(tok, i-4), "main") && neq(get(tok, i-6), "."))
        || eq(prev, "{") && eq(next, "}") && eqGet(tok, i-4, "postProcess"))
        links.put(i, sfID);
    }
      
    L<S> fewTokens = codeTokens(subList(tok, i-1, i+2*5));
    Pair<S, Int> p = stringTreeLeafValue2(tokenExplanationsTree, fewTokens);
    if (p != null) {
      //print(struct(p));
      int lastCodeToken = i+p.b*2-2;
      if (eq(get(tok, lastCodeToken), "{")) lastCodeToken -= 2;
      mapPutInRange(explanations, i, lastCodeToken+1, p.a);
    }
    
    //mapPut(explanations, i, tokenExplanations.get(t));
      
    S id = sc.get(t);
    if (id != null)
      links.put(i, id);
  }
  new StringBuilder out;
  SS titles = getSnippetTitles(values(links));
  for i over tok: {
    S t = tok.get(i), id = links.get(i), ex = explanations.get(i);
    if (empty(t)) continue;
    if (t.startsWith("[[") && t.endsWith("]]")) {
      S explanation = "[[...]] denotes a multi-line string constant (as in Lua)";
      out.append(dottedSpan("[[", explanation);
      S inner = htmlencode(dropPrefix("[[", dropSuffix("]]", t)));
      out.append(span(inner, style := "background-color: #77FF77"));
      out.append(dottedSpan("]]", explanation);
      continue;
    }
    if (t.startsWith("[=[") && t.endsWith("]=]")) {
      S explanation = "[=[...]=] denotes a multi-line string constant (as in Lua)";
      out.append(dottedSpan("[=[", explanation);
      S inner = htmlencode(dropPrefix("[=[", dropSuffix("]=]", t)));
      out.append(span(inner, style := "background-color: #77FF77"));
      out.append(dottedSpan("]=]", explanation);
      continue;
    }
    S enc = htmlencode(t);
    out.append(id != null
      ? ahref(makeLink(real, id), enc,
        title := titles.get(fsI(id)), style := "text-decoration: none; color: black; border-bottom: dotted 1px")
      : ex != null ? dottedSpan(enc, ex)
      : enc);
  }
  S html = str(out);
  html = dynamize_noEncode(html);
  if (real) ret html;
  ret h3_title("Snippet " + snippetID) + hpre(html);
}

sS makeLink(bool real, S id) {
  if (real)
    ret longSnippetLink(id);
  ret "/" + psI(id);
}

svoid clearCaches {
  stdFunctions_clearCache();
  sc = standardClassesMap();
  sf = stdFunctions_cached();
  sf.putAll(tok_findStandardFunctionDefinitions(javaTokSnippet(#1022367)));
}

download  show line numbers  debug dex   

Travelled to 11 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, tslmcundralx, tvejysmllsmz

No comments. add comment

Snippet ID: #1008705
Snippet name: Hyperlink+explain standard functions etc. in JavaX source
Eternal ID of this version: #1008705/159
Text MD5: 683ad6c053c09c0a49423b2363f54456
Transpilation MD5: 36f2ae943446a654a4e8d3405c69746b
Author: stefan
Category: javax
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-01-15 15:22:36
Source code size: 11608 bytes / 269 lines
Pitched / IR pitched: No / No
Views / Downloads: 879 / 22651
Version history: 158 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1008705 & http://1008705.tinybrain.de