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

102
LINES

< > BotCompany Repo | #1025802 // findCodeTokens with IContentsIndexedList2 [OK]

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (2300L) is out of date.

1  
static int findCodeTokens(L<S> tok, S... tokens) {
2  
  ret findCodeTokens(tok, 1, false, tokens);
3  
}
4  
5  
static int findCodeTokens(L<S> tok, boolean ignoreCase, S... tokens) {
6  
  ret findCodeTokens(tok, 1, ignoreCase, tokens);
7  
}
8  
9  
static int findCodeTokens(L<S> tok, int startIdx, boolean ignoreCase, S... tokens) {
10  
  ret findCodeTokens(tok, startIdx, ignoreCase, tokens, null);
11  
}
12  
13  
static HashSet<S> findCodeTokens_specials = lithashset("*", "<quoted>", "<id>", "<int>", "\\*");
14  
static int findCodeTokens_bails, findCodeTokens_nonbails;
15  
16  
sinterface findCodeTokens_Matcher {
17  
  bool get(S token);
18  
}
19  
20  
static int findCodeTokens(L<S> tok, int startIdx, boolean ignoreCase, S[] tokens, O condition) {
21  
  int end = tok.size()-tokens.length*2+2, nTokens = tokens.length;
22  
  int i = startIdx | 1;
23  
24  
  findCodeTokens_Matcher[] matchers = new[nTokens];
25  
  IContentsIndexedList2 indexedList = tok instanceof IContentsIndexedList2 ? (IContentsIndexedList2) tok : null;
26  
  ifdef findCodeTokens_debug
27  
    print("fct " + joinWithSpace(tokens) + " have indexed list: " + (indexedList != null));
28  
  endifdef
29  
  
30  
  TreeSet<HasIndex> indices = null;
31  
  int indicesOfs = 0;
32  
33  
  for j to nTokens: {
34  
    S p = tokens[j];
35  
    findCodeTokens_Matcher matcher;
36  
    if (p.equals("*"))
37  
      matcher = t -> true;
38  
    else if (p.equals("<quoted>"))
39  
      matcher = t -> isQuoted(t);
40  
    else if (p.equals("<id>"))
41  
      matcher = t -> isIdentifier(t);
42  
    else if (p.equals("<int>"))
43  
      matcher = t -> isInteger(t);
44  
    else if (p.equals("\\*"))
45  
      matcher = t -> t.equals("*");
46  
    else if (ignoreCase)
47  
      matcher = t -> eqic(p, t);
48  
    else {
49  
      matcher = t -> t.equals(p);
50  
      if (indexedList != null) {
51  
        TreeSet<HasIndex> indices2 = indexedList.indicesOf_treeSetOfHasIndex(p);
52  
        ifdef findCodeTokens_debug
53  
          print("fct indices for token " + p + ": " + indices2);
54  
        endifdef
55  
        if (indices2 == null) ret -1;
56  
        if (indices == null || indices2.size() < indices.size()) {
57  
          // found shorter list
58  
          indices = indices2;
59  
          indicesOfs = j;
60  
        }
61  
      }
62  
    }
63  
    matchers[j] = matcher;
64  
  }
65  
  
66  
  // go over shortest index
67  
  if (indices != null) {
68  
    int min = i+indicesOfs*2;
69  
    SortedSet<HasIndex> tailSet = min == 1 ? indices : indices.tailSet(HasIndex(min));
70  
    ifdef findCodeTokens_debug
71  
      print("fct i=" + i + ", tailSet: " + tailSet);
72  
    endifdef
73  
    outer: for (HasIndex h : tailSet) {
74  
      int idx = h.idx-indicesOfs*2;
75  
      if (idx >= end) break;
76  
      ifdef findCodeTokens_debug
77  
        print("fct have first token " + idx + ", indicesOfs=" + indicesOfs + ": " + tok.get(idx));
78  
      endifdef
79  
      for (int j = 0; j < nTokens; j++) try {
80  
        if (!matchers[j].get(tok.get(idx+j*2)))
81  
          continue outer;
82  
      } catch (IndexOutOfBoundsException e) {
83  
        print("fct indicesOfs=" + indicesOfs + ", h=" + h + ", idx=" + idx);
84  
        throw e;
85  
      }
86  
87  
      if (condition == null || checkTokCondition(condition, tok, idx-1))
88  
        ret idx;
89  
    }
90  
    ret -1;
91  
  }
92  
 
93  
  outer: for (; i < end; i += 2) {
94  
    for (int j = 0; j < nTokens; j++)
95  
      if (!matchers[j].get(tok.get(i+j*2)))
96  
        continue outer;
97  
98  
    if (condition == null || checkTokCondition(condition, tok, i-1)) // pass N index
99  
      ret i;
100  
  }
101  
  ret -1;
102  
}

Author comment

Began life as a copy of #1001193

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1025802
Snippet name: findCodeTokens with IContentsIndexedList2 [OK]
Eternal ID of this version: #1025802/17
Text MD5: 9b114629070c9ddb70da32bc3eaa8aad
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-10-21 14:55:03
Source code size: 3381 bytes / 102 lines
Pitched / IR pitched: No / No
Views / Downloads: 236 / 2695
Version history: 16 change(s)
Referenced in: [show references]