!7 static SS shortFor = litmap( "sclass", "static class", "svoid", "static void", "SS", "Map", "S", "String", "ret", "return", "L", "List", "O", "Object", "sO", "static Object", "sS", "static String", "fO", "final Object", "fS", "final String", "sbool", "static boolean", "bool", "boolean", "Int", "Integer", "cast", "casting to the type required on the left-hand side", ); static SS tokenExplanations = litmap( "func", "anonymous function declaration (similar to Java 8 lambdas)", "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-substance {", "Main program including Substance L&F, started in AWT thread", ); static SS sc; static StringTree2 tokenExplanationsTree; p { clearCaches(); serveHttpOpenBrowser(7777); tokenExplanationsTree = stringTree2_javaTok(tokenExplanations); } html { uri = dropPrefix("/", uri); bool real = uri.startsWith("real/"); if (real) uri = dropPrefix("real/", uri); S snippetID = #1007528; if (isSnippetID(uri)) snippetID = fsI(uri); S code = loadSnippet(snippetID); L tok = javaTok(code); new Map links; // token index -> sf snippet ID new Map explanations; // token index -> explanation text SS sf = stdFunctions_cached(); 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 id = sf.get(t); if (id != null) { if (eq(prev, "f") || eq(next, "(") && (neq(prev, ".") || eq(get(tok, i-4), "main") && neq(get(tok, i-6), "."))) links.put(i, id); } if (eq(t, "concept") && isIdentifier(next)) explanations.put(i, "A concept is like a Java class, but persistable"); if (eq(t, "!") && isIdentifier(prev) && neq(next, "=")) explanations.put(i, "! is short for .get()"); if (eq(t, "f") && isIdentifier(next)) explanations.put(i, "f references a static function in the main class"); if (eq(t, "*") && eq(next, "(")) explanations.put(i, "Short syntax for a constructor declaration"); if (eq(t, "p") && eq(next, "{")) explanations.put(i, "short for: public static void main(String[] args)"); if (eq(t, "html") && eq(next, "{")) explanations.put(i, "short for: static Object html(String uri, final Map params) ctex {"); if (eq(t, "pcall") && eq(next, "{")) explanations.put(i, "protected call - try { ... } catch { print exception }"); if (eq(t, "try") && eq(next, "answer")) doublePut(explanations, i, i+2, "\"try answer\" returns the expression if it isn't null or empty"); S e = shortFor.get(t); if (e != null) mapPut(explanations, i, "short for: " + e); L fewTokens = codeTokens(subList(tok, i+1, i+2+2*5)); mapPut(explanations, i, stringTreeLeafValue(tokenExplanationsTree, fewTokens)); //mapPut(explanations, i, tokenExplanations.get(t)); 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 (t.startsWith("[[") && t.endsWith("]]")) { S explanation = "[[...]] denotes a multi-line string constant (as in Lua)"; out.append(dottedSpan("[[", explanation); out.append(htmlencode(dropPrefix("[[", dropSuffix("]]", t)))); 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); 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(); } sS dottedSpan(S contents, S title) { ret span(contents, +title, style := "border-bottom: dotted 1px"); }