1 | static bool flexMatch_debug; |
2 | |
3 | static bool flexMatch(S pat, S s) { |
4 | ret flexMatch(pat, s, null); |
5 | } |
6 | |
7 | static bool flexMatch(S pat, S s, Matches m) { |
8 | ret flexMatch(javaTok(pat), javaTok(unnull(s)), m); |
9 | } |
10 | |
11 | static bool flexMatch(S pat, S s, Matches m, bool joinBrackets) { |
12 | ret flexMatch(javaTok(pat), javaTok(unnull(s)), m, joinBrackets); |
13 | } |
14 | |
15 | static bool flexMatch(L<S> tokpat, L<S> tokfull, Matches m) { |
16 | ret flexMatch(tokpat, tokfull, m, true); |
17 | } |
18 | |
19 | static bool flexMatch(L<S> tokpat, L<S> tokfull, Matches m, bool joinBrackets) { |
20 | tokpat = codeTokens(joinBrackets ? joinBrackets(tokpat) : tokpat); |
21 | for (int i = 0; i < l(tokpat); i++) |
22 | if (eq(tokpat.get(i), "*")) |
23 | tokpat.add(i++, "!*"); // insert single-token wildcard in front to avoid empty matches |
24 | if (joinBrackets) tokfull = joinBrackets(tokfull); |
25 | L<S> tok = codeTokens(tokfull); |
26 | new BitSet bla; |
27 | new BitSet bla2; |
28 | if (!flexMatch_impl(tokpat, 0, tok, 0, bla, bla2)) ret false; |
29 | if (m != null) { |
30 | new L<S> l; |
31 | for (int i = 1; i < l(tokfull); i += 2) { |
32 | if (bla.get(i/2)) { |
33 | int j = i; |
34 | while (j < l(tokfull) && bla.get(j/2)) j += 2; |
35 | l.add(join(subList(tokfull, i, j-1))); |
36 | i = j-2; |
37 | } else if (bla2.get(i/2)) |
38 | l.add(tokfull.get(i)); |
39 | } |
40 | m.m = toStringArray(l); |
41 | } |
42 | ret true; |
43 | } |
44 | |
45 | static bool flexMatch_impl(L<S> pat, int ipat, L<S> tok, int itok, BitSet bla, BitSet bla2) { |
46 | if (flexMatch_debug) |
47 | print("flexMatch pat=" + structure(subList(pat, ipat)) + " tok=" + structure(subList(tok, itok)) + " " + structure(bla)); |
48 | if (ipat >= l(pat)) |
49 | ret itok >= l(tok); |
50 | S t = pat.get(ipat); |
51 | |
52 | if (eq(t, "*")) { // the flex wildcard (0 or more tokens) |
53 | if (flexMatch_debug) print("Trying zero tokens"); |
54 | if (flexMatch_impl(pat, ipat+1, tok, itok, bla, bla2)) { |
55 | if (flexMatch_debug) print("Success!"); |
56 | ret true; |
57 | } |
58 | |
59 | bla.set(itok); |
60 | if (itok < l(tok)) { |
61 | if (flexMatch_debug) print("Trying one or more tokens"); |
62 | if (flexMatch_impl(pat, ipat, tok, itok+1, bla, bla2)) { |
63 | if (flexMatch_debug) print("Success!"); |
64 | ret true; // success, leave mark |
65 | } |
66 | } |
67 | |
68 | if (flexMatch_debug) print("Failed * matching"); |
69 | bla.clear(itok); // fail, undo marking |
70 | ret false; |
71 | } |
72 | if (itok >= l(tok)) { |
73 | if (flexMatch_debug) |
74 | print("too much pattern"); |
75 | ret false; |
76 | } |
77 | if (eq(t, "!*")) { // the single-token wildcard |
78 | bla.set(itok); |
79 | if (flexMatch_impl(pat, ipat+1, tok, itok+1, bla, bla2)) |
80 | ret true; // success, leave mark |
81 | bla.clear(itok); // fail, undo marking |
82 | ret false; |
83 | } |
84 | S realt = tok.get(itok); |
85 | if (t.startsWith("(") && t.endsWith(")")) { |
86 | // quick pre-check |
87 | if (flexMatch_debug) |
88 | print("flexMatch precheck " + t + " " + realt); |
89 | if (!contains(t, realt)) false; |
90 | // real check |
91 | L<S> list = splitAt(dropFirstAndLast(t), "|"); |
92 | if (flexMatch_debug) |
93 | print("flexMatch real check " + struct(list)); |
94 | if (!contains(list, realt)) false; |
95 | bla2.set(itok); |
96 | } else if (neq(realt, t)) { |
97 | if (flexMatch_debug) |
98 | print("mismatch"); |
99 | ret false; |
100 | } |
101 | |
102 | // it is a token match. consume and proceed |
103 | if (flexMatch_impl(pat, ipat+1, tok, itok+1, bla, bla2)) |
104 | true; |
105 | else { |
106 | bla2.clear(itok); |
107 | false; |
108 | } |
109 | } |
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1003148 |
Snippet name: | flexMatch (case-sensitive) |
Eternal ID of this version: | #1003148/7 |
Text MD5: | 64b87fd6de7c33731de6f706b8ef9202 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2019-01-20 13:56:19 |
Source code size: | 3388 bytes / 109 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 844 / 729 |
Version history: | 6 change(s) |
Referenced in: | [show references] |