Libraryless. Click here for Pure Java version (801L/7K/20K).
1 | import java.util.*; |
2 | import java.io.*; |
3 | import java.util.regex.*; |
4 | import java.net.*; |
5 | import java.math.*; |
6 | |
7 | public class main { |
8 | static boolean debug = false; |
9 | |
10 | static List<String> standardFunctions; |
11 | |
12 | public static void main(String[] args) throws IOException { |
13 | long startTime = now(); |
14 | |
15 | standardFunctions = new ArrayList(); |
16 | standardFunctions.addAll((List) loadVariableDefinition(loadSnippet("#761"), "standardFunctions")); |
17 | standardFunctions.addAll((List) loadVariableDefinition(loadSnippet("#1006654"), "standardFunctions")); |
18 | |
19 | String s = loadMainJava(); |
20 | |
21 | Map<String, String> sf = new HashMap(); |
22 | for (String x : standardFunctions) { |
23 | String[] f = x.split("/"); |
24 | sf.put(f[1], f[0]); |
25 | } |
26 | |
27 | for (int i = 0; ; i++) { |
28 | Set<String> defd = new HashSet(findFunctions(s)); |
29 | List<String> tok = javaTok(s); |
30 | |
31 | // changes tok |
32 | Set<String> invocations = findInvocations(tok, sf); |
33 | s = join(tok); |
34 | |
35 | List<String> needed = diff(invocations, defd); |
36 | print("Functions needed: " + needed); |
37 | if (needed.isEmpty()) |
38 | break; |
39 | |
40 | for (String x : needed) { |
41 | if (defd.contains(x)) continue; |
42 | |
43 | String id = sf.get(x); |
44 | System.out.println("Adding function: " + x + " (" + id + ")"); |
45 | s = addFunction(s, id); |
46 | defd = new HashSet(findFunctions(s)); |
47 | } |
48 | |
49 | for (String x : needed) |
50 | if (!defd.contains(x)) |
51 | fail("Function not defined properly: " + x); |
52 | System.out.println("Iteration " + (i+2)); |
53 | if (i >= 1000) fail("Too many iterations"); |
54 | } |
55 | saveMainJava(s); |
56 | print("629: " + (now()-startTime) + " ms"); |
57 | } |
58 | |
59 | static Set<String> findInvocations(List<String> tok, Map<String, String> sf) { |
60 | int i; |
61 | Set<String> l = new HashSet(); |
62 | while ((i = jfind(tok, "please include function *.")) >= 0) { |
63 | String fname = tok.get(i+6); |
64 | l.add(fname); |
65 | clearAllTokens(tok.subList(i, i+10)); |
66 | } |
67 | |
68 | boolean result = false; |
69 | for (i = 1; i+2 < tok.size(); i += 2) { |
70 | String f = tok.get(i); |
71 | if (!isIdentifier(f)) continue; |
72 | if ((i == 1 || !tok.get(i-2).equals(".")) && tok.get(i+2).equals("(")) { |
73 | boolean inSF = sf.containsKey(f); |
74 | if (debug) |
75 | print("Possible invocation: " + f + ", inSF: " + inSF); |
76 | if (inSF) |
77 | l.add(f); |
78 | } |
79 | } |
80 | |
81 | return l; |
82 | } |
83 | |
84 | static boolean substringIs(String s, int i, String pat) { |
85 | return i >= 0 && i+pat.length() < s.length() && s.substring(i, i+pat.length()).equals(pat); |
86 | } |
87 | |
88 | // OK, this should be fast enough. |
89 | static List<String> findFunctions(String src) { |
90 | int idx = src.indexOf("main {"); // yes it's a hack... |
91 | if (idx >= 0) src = src.substring(idx); |
92 | |
93 | Pattern pattern = Pattern.compile("static[^={]*\\s+(\\w+)\\("); |
94 | |
95 | //System.out.println("Scanning for functions"); |
96 | List<String> functions = new ArrayList<String>(); |
97 | for (String line : toLines(src)) { |
98 | Matcher matcher = pattern.matcher(line); |
99 | if (matcher.find()) { |
100 | String f = matcher.group(1); |
101 | functions.add(f); |
102 | //System.out.println("Function found: " + f); |
103 | } |
104 | } |
105 | return functions; |
106 | } |
107 | |
108 | public static String addFunction(String s, String fID) throws IOException { |
109 | int i = s.lastIndexOf('}'); |
110 | String function = loadSnippet(fID, false); |
111 | return s.substring(0, i) + "\n" + function +"\n" + s.substring(i); |
112 | } |
113 | |
114 | public static List<String> toLines(String s) { |
115 | List<String> lines = new ArrayList<String>(); |
116 | int start = 0; |
117 | while (true) { |
118 | int i = toLines_nextLineBreak(s, start); |
119 | if (i < 0) { |
120 | if (s.length() > start) lines.add(s.substring(start)); |
121 | break; |
122 | } |
123 | |
124 | lines.add(s.substring(start, i)); |
125 | if (s.charAt(i) == '\r' && i+1 < s.length() && s.charAt(i+1) == '\n') |
126 | i += 2; |
127 | else |
128 | ++i; |
129 | |
130 | start = i; |
131 | } |
132 | return lines; |
133 | } |
134 | |
135 | private static int toLines_nextLineBreak(String s, int start) { |
136 | for (int i = start; i < s.length(); i++) { |
137 | char c = s.charAt(i); |
138 | if (c == '\r' || c == '\n') |
139 | return i; |
140 | } |
141 | return -1; |
142 | } |
143 | |
144 | static String mainJava; |
145 | |
146 | static String loadMainJava() throws IOException { |
147 | if (mainJava != null) return mainJava; |
148 | return loadTextFile("input/main.java", ""); |
149 | } |
150 | |
151 | static void saveMainJava(String s) throws IOException { |
152 | if (mainJava != null) |
153 | mainJava = s; |
154 | else |
155 | saveTextFile("output/main.java", s); |
156 | } |
157 | |
158 | static void saveMainJava(List<String> tok) throws IOException { |
159 | saveMainJava(join(tok)); |
160 | } |
161 | |
162 | static String charsetForTextFiles = "UTF-8"; |
163 | |
164 | public static String loadTextFile(String fileName, String defaultContents) throws IOException { |
165 | if (!new File(fileName).exists()) |
166 | return defaultContents; |
167 | |
168 | FileInputStream fileInputStream = new FileInputStream(fileName); |
169 | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, charsetForTextFiles); |
170 | return loadTextFile(inputStreamReader); |
171 | } |
172 | |
173 | public static String loadTextFile(Reader reader) throws IOException { |
174 | StringBuilder builder = new StringBuilder(); |
175 | try { |
176 | BufferedReader bufferedReader = new BufferedReader(reader); |
177 | String line; |
178 | while ((line = bufferedReader.readLine()) != null) |
179 | builder.append(line).append('\n'); |
180 | } finally { |
181 | reader.close(); |
182 | } |
183 | return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1); |
184 | } |
185 | |
186 | /** writes safely (to temp file, then rename) */ |
187 | public static void saveTextFile(String fileName, String contents) throws IOException { |
188 | File file = new File(fileName); |
189 | File parentFile = file.getParentFile(); |
190 | if (parentFile != null) |
191 | parentFile.mkdirs(); |
192 | String tempFileName = fileName + "_temp"; |
193 | FileOutputStream fileOutputStream = new FileOutputStream(tempFileName); |
194 | OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, charsetForTextFiles); |
195 | PrintWriter printWriter = new PrintWriter(outputStreamWriter); |
196 | printWriter.print(contents); |
197 | printWriter.close(); |
198 | if (file.exists() && !file.delete()) |
199 | throw new IOException("Can't delete " + fileName); |
200 | |
201 | if (!new File(tempFileName).renameTo(file)) |
202 | throw new IOException("Can't rename " + tempFileName + " to " + fileName); |
203 | } |
204 | |
205 | public static String loadSnippet(String snippetID) { |
206 | try { |
207 | return loadSnippet(snippetID, preferCached); |
208 | } catch (IOException e) { |
209 | throw new RuntimeException(e); |
210 | } |
211 | } |
212 | |
213 | public static String loadSnippet(String snippetID, boolean preferCached) throws IOException { |
214 | return loadSnippet(parseSnippetID(snippetID), preferCached); |
215 | } |
216 | |
217 | public static long parseSnippetID(String snippetID) { |
218 | return Long.parseLong(shortenSnippetID(snippetID)); |
219 | } |
220 | |
221 | private static String shortenSnippetID(String snippetID) { |
222 | if (snippetID.startsWith("#")) |
223 | snippetID = snippetID.substring(1); |
224 | String httpBlaBla = "http://tinybrain.de/"; |
225 | if (snippetID.startsWith(httpBlaBla)) |
226 | snippetID = snippetID.substring(httpBlaBla.length()); |
227 | return snippetID; |
228 | } |
229 | |
230 | public static boolean isSnippetID(String snippetID) { |
231 | snippetID = shortenSnippetID(snippetID); |
232 | return isInteger(snippetID) && Long.parseLong(snippetID) != 0; |
233 | } |
234 | |
235 | public static boolean isInteger(String s) { |
236 | return Pattern.matches("\\-?\\d+", s); |
237 | } |
238 | |
239 | static boolean preferCached = false; |
240 | |
241 | public static String loadSnippet(long snippetID) throws IOException { |
242 | return loadSnippet(snippetID, preferCached); |
243 | } |
244 | |
245 | public static String loadSnippet(long snippetID, boolean preferCached) throws IOException { |
246 | if (preferCached) { |
247 | initSnippetCache(); |
248 | String text = DiskSnippetCache_get(snippetID); |
249 | if (text != null) |
250 | return text; |
251 | } |
252 | |
253 | String text; |
254 | try { |
255 | URL url = new URL("http://tinybrain.de:8080/getraw.php?id=" + snippetID); |
256 | text = loadPage(url); |
257 | } catch (FileNotFoundException e) { |
258 | throw new IOException("Snippet #" + snippetID + " not found or not public"); |
259 | } |
260 | |
261 | try { |
262 | initSnippetCache(); |
263 | DiskSnippetCache_put(snippetID, text); |
264 | } catch (IOException e) { |
265 | System.err.println("Minor warning: Couldn't save snippet to cache (" + DiskSnippetCache_getDir() + ")"); |
266 | } |
267 | |
268 | return text; |
269 | } |
270 | |
271 | private static String loadPage(URL url) throws IOException { |
272 | System.out.println("Loading: " + url.toExternalForm()); |
273 | URLConnection con = url.openConnection(); |
274 | return loadPage(con, url); |
275 | } |
276 | |
277 | public static String loadPage(URLConnection con, URL url) throws IOException { |
278 | String contentType = con.getContentType(); |
279 | if (contentType == null) |
280 | throw new IOException("Page could not be read: " + url); |
281 | //Log.info("Content-Type: " + contentType); |
282 | String charset = guessCharset(contentType); |
283 | Reader r = new InputStreamReader(con.getInputStream(), charset); |
284 | StringBuilder buf = new StringBuilder(); |
285 | while (true) { |
286 | int ch = r.read(); |
287 | if (ch < 0) |
288 | break; |
289 | //Log.info("Chars read: " + buf.length()); |
290 | buf.append((char) ch); |
291 | } |
292 | return buf.toString(); |
293 | } |
294 | |
295 | public static String guessCharset(String contentType) { |
296 | Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*"); |
297 | Matcher m = p.matcher(contentType); |
298 | /* If Content-Type doesn't match this pre-conception, choose default and hope for the best. */ |
299 | return m.matches() ? m.group(1) : "ISO-8859-1"; |
300 | } |
301 | |
302 | static File DiskSnippetCache_dir; |
303 | |
304 | public static void initDiskSnippetCache(File dir) { |
305 | DiskSnippetCache_dir = dir; |
306 | dir.mkdirs(); |
307 | } |
308 | |
309 | public static synchronized String DiskSnippetCache_get(long snippetID) throws IOException { |
310 | return loadTextFile(DiskSnippetCache_getFile(snippetID).getPath(), null); |
311 | } |
312 | |
313 | private static File DiskSnippetCache_getFile(long snippetID) { |
314 | return new File(DiskSnippetCache_dir, "" + snippetID); |
315 | } |
316 | |
317 | public static synchronized void DiskSnippetCache_put(long snippetID, String snippet) throws IOException { |
318 | saveTextFile(DiskSnippetCache_getFile(snippetID).getPath(), snippet); |
319 | } |
320 | |
321 | public static File DiskSnippetCache_getDir() { |
322 | return DiskSnippetCache_dir; |
323 | } |
324 | |
325 | public static void initSnippetCache() { |
326 | if (DiskSnippetCache_dir == null) |
327 | initDiskSnippetCache(new File(System.getProperty("user.home"), ".tinybrain/snippet-cache")); |
328 | } |
329 | |
330 | static RuntimeException fail() { |
331 | throw new RuntimeException("fail"); |
332 | } |
333 | |
334 | static RuntimeException fail(Object msg) { |
335 | throw new RuntimeException(String.valueOf(msg)); |
336 | } |
337 | |
338 | // replacement for class JavaTok |
339 | // maybe incomplete, might want to add floating point numbers |
340 | // todo also: extended multi-line strings |
341 | |
342 | static List<String> javaTok(String s) { |
343 | List<String> tok = new ArrayList<String>(); |
344 | int l = s.length(); |
345 | |
346 | int i = 0; |
347 | while (i < l) { |
348 | int j = i; |
349 | char c; String cc; |
350 | |
351 | // scan for whitespace |
352 | while (j < l) { |
353 | c = s.charAt(j); |
354 | cc = s.substring(j, Math.min(j+2, l)); |
355 | if (c == ' ' || c == '\t' || c == '\r' || c == '\n') |
356 | ++j; |
357 | else if (cc.equals("/*")) { |
358 | do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/")); |
359 | j = Math.min(j+2, l); |
360 | } else if (cc.equals("//")) { |
361 | do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0); |
362 | } else |
363 | break; |
364 | } |
365 | |
366 | tok.add(s.substring(i, j)); |
367 | i = j; |
368 | if (i >= l) break; |
369 | c = s.charAt(i); // cc is not needed in rest of loop body |
370 | cc = s.substring(i, Math.min(i+2, l)); |
371 | |
372 | // scan for non-whitespace |
373 | if (c == '\'' || c == '"') { |
374 | char opener = c; |
375 | ++j; |
376 | while (j < l) { |
377 | if (s.charAt(j) == opener) { |
378 | ++j; |
379 | break; |
380 | } else if (s.charAt(j) == '\\' && j+1 < l) |
381 | j += 2; |
382 | else |
383 | ++j; |
384 | } |
385 | } else if (Character.isJavaIdentifierStart(c)) |
386 | do ++j; while (j < l && Character.isJavaIdentifierPart(s.charAt(j))); |
387 | else if (Character.isDigit(c)) { |
388 | do ++j; while (j < l && Character.isDigit(s.charAt(j))); |
389 | if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L |
390 | } else if (cc.equals("[[")) { |
391 | do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]")); |
392 | j = Math.min(j+2, l); |
393 | } else |
394 | ++j; |
395 | |
396 | tok.add(s.substring(i, j)); |
397 | i = j; |
398 | } |
399 | |
400 | if ((tok.size() % 2) == 0) tok.add(""); |
401 | return tok; |
402 | } |
403 | |
404 | static List<String> javaTok(List<String> tok) { |
405 | return javaTok(join(tok)); |
406 | } |
407 | |
408 | public static String join(String glue, Iterable<String> strings) { |
409 | StringBuilder buf = new StringBuilder(); |
410 | Iterator<String> i = strings.iterator(); |
411 | if (i.hasNext()) { |
412 | buf.append(i.next()); |
413 | while (i.hasNext()) |
414 | buf.append(glue).append(i.next()); |
415 | } |
416 | return buf.toString(); |
417 | } |
418 | |
419 | public static String join(String glue, String[] strings) { |
420 | return join(glue, Arrays.asList(strings)); |
421 | } |
422 | |
423 | public static String join(Iterable<String> strings) { |
424 | return join("", strings); |
425 | } |
426 | |
427 | public static String join(String[] strings) { |
428 | return join("", strings); |
429 | } |
430 | |
431 | // leaves tok properly tokenized |
432 | // returns true iff anything was replaced |
433 | static boolean jreplace(List<String> tok, String in, String out) { |
434 | return jreplace(tok, in, out, false, true); |
435 | } |
436 | |
437 | static boolean jreplace(List<String> tok, String in, String out, boolean ignoreCase, boolean reTok) { |
438 | List<String> tokin = javaTok(in); |
439 | replaceSublist(tokin, litlist("<", "", "quoted", "", ">"), litlist("<quoted>")); |
440 | replaceSublist(tokin, litlist("<", "", "id", "", ">"), litlist("<id>")); |
441 | |
442 | boolean anyChange = false; |
443 | for (int n = 0; n < 10000; n++) { |
444 | int i = findCodeTokens(tok, ignoreCase, toStringArray(codeTokensOnly(tokin))); |
445 | if (i < 0) { |
446 | if (anyChange && reTok) |
447 | replaceCollection(tok, javaTok(tok)); |
448 | return anyChange; |
449 | } |
450 | List<String> subList = tok.subList(i-1, i+l(tokin)-1); // N to N |
451 | String expansion = jreplace_expandRefs(out, subList); |
452 | clearAllTokens(tok.subList(i, i+l(tokin)-2)); // code to code |
453 | tok.set(i, expansion); |
454 | anyChange = true; |
455 | } |
456 | throw fail("woot? 10000!"); |
457 | } |
458 | |
459 | // "$1" is first code token, "$2" second code token etc. |
460 | static String jreplace_expandRefs(String s, List<String> tokref) { |
461 | List<String> tok = javaTok(s); |
462 | for (int i = 1; i < l(tok)-2; i += 2) { |
463 | if (tok.get(i).startsWith("$") && isInteger(tok.get(i).substring(1))) { |
464 | String x = tokref.get(-1+parseInt(tok.get(i).substring(1))*2); |
465 | tok.set(i, x); |
466 | } |
467 | } |
468 | return join(tok); |
469 | } |
470 | |
471 | static void replaceToken(List<String> tok, String in, String out) { |
472 | renameToken(tok, in, out); |
473 | } |
474 | |
475 | static <A> void replaceCollection(Collection<A> dest, Collection<A> src) { |
476 | dest.clear(); |
477 | dest.addAll(src); |
478 | } |
479 | |
480 | static <A> ArrayList<A> litlist(A... a) { |
481 | return new ArrayList<A>(Arrays.asList(a)); |
482 | } |
483 | static List<String> codeTokensOnly(List<String> tok) { |
484 | List<String> l = new ArrayList<String>(); |
485 | for (int i = 1; i < tok.size(); i += 2) |
486 | l.add(tok.get(i)); |
487 | return l; |
488 | } |
489 | |
490 | static void replaceSublist(List<String> l, List<String> x, List<String> y) { |
491 | int i = 0; |
492 | while (true) { |
493 | i = indexOfSubList(l, x, i); |
494 | if (i < 0) return; |
495 | |
496 | // It's inefficient :D |
497 | for (int j = 0; j < l(x); j++) l.remove(i); |
498 | l.addAll(i, y); |
499 | i += l(y); |
500 | } |
501 | } |
502 | |
503 | static int l(Object[] array) { |
504 | return array == null ? 0 : array.length; |
505 | } |
506 | |
507 | static int l(Collection c) { |
508 | return c == null ? 0 : c.size(); |
509 | } |
510 | |
511 | static int l(Map m) { |
512 | return m == null ? 0 : m.size(); |
513 | } |
514 | |
515 | static int l(String s) { |
516 | return s == null ? 0 : s.length(); |
517 | } |
518 | |
519 | |
520 | static int parseInt(String s) { |
521 | return Integer.parseInt(s); |
522 | } |
523 | |
524 | static void renameToken(List<String> tok, String in, String out) { |
525 | int renames = 0; |
526 | for (int i = 1; i < tok.size(); i += 2) { |
527 | if (tok.get(i).equals(in)) { |
528 | tok.set(i, out); |
529 | ++renames; |
530 | } |
531 | } |
532 | } |
533 | |
534 | |
535 | static String[] toStringArray(List<String> list) { |
536 | return list.toArray(new String[list.size()]); |
537 | } |
538 | |
539 | static String[] toStringArray(Object o) { |
540 | if (o instanceof String[]) |
541 | return (String[]) o; |
542 | else if (o instanceof List) |
543 | return toStringArray((List<String>) o); |
544 | else |
545 | throw fail("Not a list or array: " + o); |
546 | } |
547 | |
548 | static int findCodeTokens(List<String> tok, String... tokens) { |
549 | return findCodeTokens(tok, 1, false, tokens); |
550 | } |
551 | |
552 | static int findCodeTokens(List<String> tok, boolean ignoreCase, String... tokens) { |
553 | return findCodeTokens(tok, 1, ignoreCase, tokens); |
554 | } |
555 | |
556 | static int findCodeTokens(List<String> tok, int startIdx, boolean ignoreCase, String... tokens) { |
557 | outer: for (int i = startIdx | 1; i+tokens.length*2-2 < tok.size(); i += 2) { |
558 | for (int j = 0; j < tokens.length; j++) { |
559 | String p = tokens[j], t = tok.get(i+j*2); |
560 | boolean match; |
561 | if (eq(p, "*")) match = true; |
562 | else if (eq(p, "<quoted>")) match = isQuoted(t); |
563 | else if (eq(p, "<id>")) match = isIdentifier(t); |
564 | else match = ignoreCase ? eqic(p, t) : eq(p, t); |
565 | |
566 | if (!match) |
567 | continue outer; |
568 | } |
569 | return i; |
570 | } |
571 | return -1; |
572 | } |
573 | |
574 | |
575 | static void clearAllTokens(List<String> tok) { |
576 | for (int i = 0; i < tok.size(); i++) |
577 | tok.set(i, ""); |
578 | } |
579 | |
580 | static void clearAllTokens(List<String> tok, int i, int j) { |
581 | for (; i < j; i++) |
582 | tok.set(i, ""); |
583 | } |
584 | |
585 | static boolean isIdentifier(String s) { |
586 | return isJavaIdentifier(s); |
587 | } |
588 | |
589 | static boolean eqic(String a, String b) { |
590 | if ((a == null) != (b == null)) return false; |
591 | if (a == null) return true; |
592 | return a.equalsIgnoreCase(b); |
593 | } |
594 | |
595 | |
596 | // supports the usual quotings (', ", variable length double brackets) |
597 | static boolean isQuoted(String s) { |
598 | if (s.startsWith("'") || s.startsWith("\"")) return true; |
599 | if (!s.startsWith("[")) return false; |
600 | int i = 1; |
601 | while (i < s.length() && s.charAt(i) == '=') ++i; |
602 | return i < s.length() && s.charAt(i) == '['; |
603 | //return Pattern.compile("^\\[=*\\[").matcher(s).find(); |
604 | } |
605 | |
606 | |
607 | static boolean eq(Object a, Object b) { |
608 | if (a == null) return b == null; |
609 | if (a.equals(b)) return true; |
610 | if (a instanceof BigInteger) { |
611 | if (b instanceof Integer) return a.equals(BigInteger.valueOf((Integer) b)); |
612 | if (b instanceof Long) return a.equals(BigInteger.valueOf((Long) b)); |
613 | } |
614 | return false; |
615 | } |
616 | |
617 | static boolean isJavaIdentifier(String s) { |
618 | if (s.length() == 0 || !Character.isJavaIdentifierStart(s.charAt(0))) |
619 | return false; |
620 | for (int i = 1; i < s.length(); i++) |
621 | if (!Character.isJavaIdentifierPart(s.charAt(i))) |
622 | return false; |
623 | return true; |
624 | } |
625 | |
626 | static <A> int indexOfSubList(List<A> x, List<A> y, int i) { |
627 | outer: for (; i+l(y) <= l(x); i++) { |
628 | for (int j = 0; j < l(y); j++) |
629 | if (neq(x.get(i+j), y.get(j))) |
630 | continue outer; |
631 | return i; |
632 | } |
633 | return -1; |
634 | } |
635 | |
636 | static boolean neq(Object a, Object b) { |
637 | return !eq(a, b); |
638 | } |
639 | |
640 | static long now() { |
641 | return System.currentTimeMillis(); |
642 | } |
643 | |
644 | static void print(String s) { |
645 | System.out.println(s); |
646 | } |
647 | |
648 | static List<String> diff(Collection<String> a, Set<String> b) { |
649 | List<String> l = new ArrayList(); |
650 | for (String s : a) |
651 | if (!b.contains(s)) |
652 | l.add(s); |
653 | return l; |
654 | } |
655 | |
656 | static int jfind(List<String> tok, String in) { |
657 | List<String> tokin = javaTok(in); |
658 | replaceSublist(tokin, litlist("<", "", "quoted", "", ">"), litlist("<quoted>")); |
659 | replaceSublist(tokin, litlist("<", "", "id", "", ">"), litlist("<id>")); |
660 | |
661 | return findCodeTokens(tok, false, toStringArray(codeTokensOnly(tokin))); |
662 | } |
663 | |
664 | // currently only works with string lists ("= litlist(...)") |
665 | // and strings. |
666 | static Object loadVariableDefinition(String progIDOrSrc, String varName) { |
667 | if (isSnippetID(progIDOrSrc)) |
668 | progIDOrSrc = loadSnippet(progIDOrSrc); |
669 | List<String> tok = javaTok(progIDOrSrc); |
670 | |
671 | int i = findCodeTokens(tok, varName, "="); |
672 | if (i < 0) return null; |
673 | |
674 | i += 4; |
675 | if (isQuoted(tok.get(i))) |
676 | return unquote(tok.get(i)); |
677 | |
678 | if (eq(get(tok, i), "litlist") && eq(get(tok, i+2), "(")) { |
679 | int opening = i+2; |
680 | int closing = findEndOfBracketPart(tok, opening)-1; |
681 | List l = new ArrayList(); |
682 | for (i = opening+2; i < closing; i += 4) |
683 | l.add(unquote(tok.get(i))); |
684 | return l; |
685 | } |
686 | |
687 | throw fail("Unknown variable type or no definition in source: " + shorten(progIDOrSrc, 100) + "/" + varName); |
688 | } |
689 | |
690 | static <A> A get(List<A> l, int idx) { |
691 | return idx >= 0 && idx < l(l) ? l.get(idx) : null; |
692 | } |
693 | |
694 | public static String unquote(String s) { |
695 | if (s == null) return null; |
696 | if (s.startsWith("[")) { |
697 | int i = 1; |
698 | while (i < s.length() && s.charAt(i) == '=') ++i; |
699 | if (i < s.length() && s.charAt(i) == '[') { |
700 | String m = s.substring(1, i); |
701 | if (s.endsWith("]" + m + "]")) |
702 | return s.substring(i+1, s.length()-i-1); |
703 | } |
704 | } |
705 | |
706 | if (s.startsWith("\"") /*&& s.endsWith("\"")*/ && s.length() > 1) { |
707 | String st = s.substring(1, s.endsWith("\"") ? s.length()-1 : s.length()); |
708 | StringBuilder sb = new StringBuilder(st.length()); |
709 | |
710 | for (int i = 0; i < st.length(); i++) { |
711 | char ch = st.charAt(i); |
712 | if (ch == '\\') { |
713 | char nextChar = (i == st.length() - 1) ? '\\' : st |
714 | .charAt(i + 1); |
715 | // Octal escape? |
716 | if (nextChar >= '0' && nextChar <= '7') { |
717 | String code = "" + nextChar; |
718 | i++; |
719 | if ((i < st.length() - 1) && st.charAt(i + 1) >= '0' |
720 | && st.charAt(i + 1) <= '7') { |
721 | code += st.charAt(i + 1); |
722 | i++; |
723 | if ((i < st.length() - 1) && st.charAt(i + 1) >= '0' |
724 | && st.charAt(i + 1) <= '7') { |
725 | code += st.charAt(i + 1); |
726 | i++; |
727 | } |
728 | } |
729 | sb.append((char) Integer.parseInt(code, 8)); |
730 | continue; |
731 | } |
732 | switch (nextChar) { |
733 | case '\\': |
734 | ch = '\\'; |
735 | break; |
736 | case 'b': |
737 | ch = '\b'; |
738 | break; |
739 | case 'f': |
740 | ch = '\f'; |
741 | break; |
742 | case 'n': |
743 | ch = '\n'; |
744 | break; |
745 | case 'r': |
746 | ch = '\r'; |
747 | break; |
748 | case 't': |
749 | ch = '\t'; |
750 | break; |
751 | case '\"': |
752 | ch = '\"'; |
753 | break; |
754 | case '\'': |
755 | ch = '\''; |
756 | break; |
757 | // Hex Unicode: u???? |
758 | case 'u': |
759 | if (i >= st.length() - 5) { |
760 | ch = 'u'; |
761 | break; |
762 | } |
763 | int code = Integer.parseInt( |
764 | "" + st.charAt(i + 2) + st.charAt(i + 3) |
765 | + st.charAt(i + 4) + st.charAt(i + 5), 16); |
766 | sb.append(Character.toChars(code)); |
767 | i += 5; |
768 | continue; |
769 | default: |
770 | ch = nextChar; // added by Stefan |
771 | } |
772 | i++; |
773 | } |
774 | sb.append(ch); |
775 | } |
776 | return sb.toString(); |
777 | } else |
778 | return s; // return original |
779 | } |
780 | |
781 | |
782 | static String shorten(String s, int max) { |
783 | if (s == null) return ""; |
784 | return s.length() <= max ? s : s.substring(0, Math.min(s.length(), max)) + "..."; |
785 | } |
786 | |
787 | // i must point at the opening bracket (any of the 2 types, not type parameters) |
788 | // index returned is index of closing bracket + 1 |
789 | static int findEndOfBracketPart(List<String> cnc, int i) { |
790 | int j = i+2, level = 1; |
791 | while (j < cnc.size()) { |
792 | if (litlist("{", "(").contains(cnc.get(j))) ++level; |
793 | else if (litlist("}", ")").contains(cnc.get(j))) --level; |
794 | if (level == 0) |
795 | return j+1; |
796 | ++j; |
797 | } |
798 | return cnc.size(); |
799 | } |
800 | |
801 | } |
Supersedes #1000265
download show line numbers debug dex old transpilations
Travelled to 34 computer(s): ajlfxifxfcul, aoiabmzegqzx, bhatertpkbcr, cahelewubzku, cbybwowwnfue, cfunsshuasjs, cmhtpxxajurv, ddnzoavkxhuk, dhtvkmknsjym, exkalrxbqyxc, gkwdfizivqfm, gwrvuhgaqvyk, hxnwyiuffukg, ishqpsrjomds, jlatgrcjtklg, jtubtzbbkimh, kajysqoxcvfe, kmhbujppghqa, liwcxgsjrgqn, lpdgvwnxivlt, mqqgnosmbjvj, mrjhfnjfopze, nrtiiiyxqhmw, onfqjnomoxuw, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, qbtsjoyahagl, teubizvjbppd, tslmcundralx, tvejysmllsmz, ugbnzuvxksoj, vouqrxazstgt, xinetxnxrdbb
ID | Author/Program | Comment | Date |
---|---|---|---|
200 | #1000604 (pitcher) | 2015-08-19 17:56:34 | |
185 | #1000610 (pitcher) | 2015-08-19 17:56:34 |
Snippet ID: | #629 |
Snippet name: | !standard functions (old but used in lower levels of translation engine) |
Eternal ID of this version: | #629/1 |
Text MD5: | 38273cf9b9f0536417601f02769f9521 |
Transpilation MD5: | 38273cf9b9f0536417601f02769f9521 |
Author: | stefan |
Category: | javax |
Type: | JavaX translator |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2017-01-25 18:12:00 |
Source code size: | 24483 bytes / 801 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 3139 / 24043 |
Referenced in: | [show references] |