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

109
LINES

< > BotCompany Repo | #1007988 // flexMatchIC2_left - flexMatch (ignore case, with (a|b), left-associative)

JavaX fragment (include)

1  
static bool flexMatchIC2_left_debug;
2  
3  
static bool flexMatchIC2_left(S pat, S s) {
4  
  ret flexMatchIC2_left(pat, s, null);
5  
}
6  
7  
static bool flexMatchIC2_left(S pat, S s, Matches m) {
8  
  ret flexMatchIC2_left(javaTok(pat), javaTok_cached(unnull(s)), m);
9  
}
10  
11  
static bool flexMatchIC2_left(S pat, S s, Matches m, bool joinBrackets) {
12  
  ret flexMatchIC2_left(javaTok(pat), javaTok_cached(unnull(s)), m, joinBrackets);
13  
}
14  
15  
static bool flexMatchIC2_left(L<S> tokpat, L<S> tokfull, Matches m) {
16  
  ret flexMatchIC2_left(tokpat, tokfull, m, true);
17  
}
18  
19  
static bool flexMatchIC2_left(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 (!flexMatchIC2_left_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 flexMatchIC2_left_impl(L<S> pat, int ipat, L<S> tok, int itok, BitSet bla, BitSet bla2) {
46  
  if (flexMatchIC2_left_debug)
47  
    print("flexMatchIC2_left 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  
    bla.set(itok);
54  
    if (itok < l(tok)) {
55  
      if (flexMatchIC2_left_debug) print("Trying one or more tokens");
56  
      if (flexMatchIC2_left_impl(pat, ipat, tok, itok+1, bla, bla2)) {
57  
        if (flexMatchIC2_left_debug) print("Success!");
58  
        ret true; // success, leave mark
59  
      }
60  
    }
61  
    bla.clear(itok); // fail, undo marking
62  
    
63  
    if (flexMatchIC2_left_debug) print("Trying zero tokens");
64  
    if (flexMatchIC2_left_impl(pat, ipat+1, tok, itok, bla, bla2)) {
65  
      if (flexMatchIC2_left_debug) print("Success!");
66  
      ret true;
67  
    }
68  
    
69  
    if (flexMatchIC2_left_debug) print("Failed * matching");
70  
    ret false;
71  
  }
72  
  if (itok >= l(tok)) {
73  
    if (flexMatchIC2_left_debug)
74  
      print("too much pattern");
75  
    ret false;
76  
  }
77  
  if (eq(t, "!*")) { // the single-token wildcard
78  
    bla.set(itok);
79  
    if (flexMatchIC2_left_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 (flexMatchIC2_left_debug)
88  
      print("flexMatchIC2_left precheck " + t + " " + realt);
89  
    if (!containsIgnoreCase(t, realt)) false;
90  
    // real check
91  
    L<S> list = splitAt(dropFirstAndLast(t), "|");
92  
    if (flexMatchIC2_left_debug)
93  
      print("flexMatchIC2_left real check " + struct(list));
94  
    if (!containsIgnoreCase(list, realt)) false;
95  
    bla2.set(itok);
96  
  } else if (neqic(realt, t)) {
97  
    if (flexMatchIC2_left_debug)
98  
      print("mismatch");
99  
    ret false;
100  
  }
101  
  
102  
  // it is a token match. consume and proceed
103  
  if (flexMatchIC2_left_impl(pat, ipat+1, tok, itok+1, bla, bla2))
104  
    true;
105  
  else {
106  
    bla2.clear(itok);
107  
    false;
108  
  }
109  
}

Author comment

Began life as a copy of #1005501

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: #1007988
Snippet name: flexMatchIC2_left - flexMatch (ignore case, with (a|b), left-associative)
Eternal ID of this version: #1007988/2
Text MD5: 34a60a5746ebae9c57e48195ceff31aa
Author: stefan
Category: javax / parsing
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-04-18 03:07:07
Source code size: 3656 bytes / 109 lines
Pitched / IR pitched: No / No
Views / Downloads: 460 / 453
Version history: 1 change(s)
Referenced in: [show references]