Libraryless. Click here for Pure Java version (4005L/27K/73K).
1 | !636 |
2 | !sf |
3 | !multi-line strings |
4 | !1000635 // stringfunc { |
5 | !L<S> |
6 | !1000636 // compare strings with == |
7 | !1000482 // quick cast |
8 | !JavaTok |
9 | !class _javax 19 |
10 | |
11 | interface StringFunc { |
12 | S get(S s); |
13 | } |
14 | |
15 | main { |
16 | static O in; |
17 | static L<S> markedStrings; |
18 | static boolean debug; |
19 | |
20 | psvm { |
21 | if (in == null) // may have been set by hotwire client |
22 | in = "create table `bla`(hello int);"; |
23 | |
24 | if (args.length != 0) in = loadSnippet(args[0]); |
25 | |
26 | S s = [[ |
27 | mysqltok input |
28 | find "create" "table" ... ";" |
29 | ]]; |
30 | L<S> ll = toLinesFullTrim(s); |
31 | //System.out.println(ll); |
32 | |
33 | // remove comments |
34 | ll = map(ll, stringfunc { s.replaceAll("//.*$", "").trim() }); |
35 | |
36 | L<S> tok = null; |
37 | |
38 | for (S c : ll) { |
39 | if (c == "mysqltok input") |
40 | in = tok = mysqlTok((String) in); |
41 | else if (c.startsWith("find")) { |
42 | L<S> pat = JavaTok.split(c); |
43 | pat.remove(0); pat.remove(0); |
44 | L<S> inp = cast in; |
45 | //System.out.println("inp: " + inp); |
46 | //System.out.println("pat: " + pat); |
47 | in = matchTokensList(inp, pat); |
48 | System.out.println("Result: " + structure(in)); |
49 | } |
50 | } |
51 | |
52 | if (in instanceof List) { |
53 | List<int[]> list = cast in; |
54 | markedStrings = getMarkedStrings(tok, list); |
55 | markTokens(tok, list); |
56 | in = JavaTok.join(tok); |
57 | |
58 | System.out.println("Markings found: " + size(list)); |
59 | for (int[] ij : list) |
60 | System.out.println("Got: " + JavaTok.join(tok.subList(ij[0], ij[1]+1))); |
61 | } |
62 | } |
63 | |
64 | static List<String> getMarkedStrings(L<S> tok, L<int[]> markings) { |
65 | new L<S> result; |
66 | for (int i = 0; i < markings.size(); i++) |
67 | result.add(JavaTok.join(tok.subList(markings.get(i)[0], markings.get(i)[1]))); |
68 | return result; |
69 | } |
70 | |
71 | static int size(Object o) { |
72 | return ((List) o).size(); |
73 | } |
74 | |
75 | static void markTokens(L<S> tok, L<int[]> marks) { |
76 | System.out.println(tok.size()); |
77 | for (int i = 0; i < tok.size(); i++) { |
78 | String t = tok.get(i); |
79 | if (isStartMark(i, marks)) |
80 | t = "[[" + t; |
81 | if (isEndMark(i, marks)) |
82 | t += "]]"; |
83 | tok.set(i, t); |
84 | } |
85 | } |
86 | |
87 | static boolean isStartMark(int i, L<int[]> marks) { |
88 | for (int[] ij : marks) |
89 | if (ij[0] == i) |
90 | return true; |
91 | return false; |
92 | } |
93 | |
94 | static boolean isEndMark(int i, L<int[]> marks) { |
95 | for (int[] ij : marks) |
96 | if (ij[1] == i+1) |
97 | return true; |
98 | return false; |
99 | } |
100 | |
101 | static S join(S glue, O o) { |
102 | return join(glue, (L<S>) o); |
103 | } |
104 | |
105 | !include #2000507 // original join function |
106 | |
107 | |
108 | static L<S> map(L<S> l, StringFunc f) { |
109 | new L<S> l2; |
110 | for (S s : l) |
111 | l2.add(f.get(s)); |
112 | return l2; |
113 | } |
114 | } |
Began life as a copy of #1000634
download show line numbers debug dex old transpilations
Travelled to 17 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1000642 |
Snippet name: | Find create statements in MySQL dump |
Eternal ID of this version: | #1000642/1 |
Text MD5: | 0790405ded83f301a22849dfcc71e9ea |
Transpilation MD5: | 699f264f9ae09a20b0a708f48b7480e1 |
Author: | stefan |
Category: | |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-08-22 12:38:33 |
Source code size: | 2780 bytes / 114 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 796 / 1375 |
Referenced in: | [show references] |