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

83
LINES

< > BotCompany Repo | #1001193 // findCodeTokens - returns C index

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

Transpiled version (4712L) is out of date.

1  
// Note: In the transpiler, this version is used: #1025802
2  
3  
static int findCodeTokens(L<S> tok, S... tokens) {
4  
  ret findCodeTokens(tok, 1, false, tokens);
5  
}
6  
7  
static int findCodeTokens(L<S> tok, boolean ignoreCase, S... tokens) {
8  
  ret findCodeTokens(tok, 1, ignoreCase, tokens);
9  
}
10  
11  
static int findCodeTokens(L<S> tok, int startIdx, boolean ignoreCase, S... tokens) {
12  
  ret findCodeTokens(tok, startIdx, ignoreCase, tokens, null);
13  
}
14  
15  
static HashSet<S> findCodeTokens_specials = lithashset("*", "<quoted>", "<id>", "<int>", "\\*");
16  
static int findCodeTokens_bails, findCodeTokens_nonbails;
17  
18  
sinterface findCodeTokens_Matcher {
19  
  bool get(S token);
20  
}
21  
22  
static int findCodeTokens(LS tok, int startIdx, boolean ignoreCase, S[] tokens, O condition) {
23  
  int end = tok.size()-tokens.length*2+2, nTokens = tokens.length;
24  
  int i = startIdx | 1;
25  
  if (i >= end) ret -1;
26  
  
27  
  // bail out early if first token not found (works great with IndexedList)
28  
  S firstToken = tokens[0];
29  
  if (!ignoreCase && !findCodeTokens_specials.contains(firstToken)) {
30  
    ifclass IndexedList2
31  
    if (tok instanceof IndexedList2 && !tok.contains(firstToken)) {
32  
      ++findCodeTokens_bails;
33  
      ret -1;
34  
    }
35  
    ++findCodeTokens_nonbails;
36  
    endif
37  
    
38  
    // quickly scan for first token
39  
    while (i < end && !firstToken.equals(tok.get(i)))
40  
      i += 2;
41  
  }
42  
  
43  
  findCodeTokens_Matcher[] matchers = new[nTokens];
44  
  for j to nTokens: {
45  
    S p = tokens[j];
46  
    findCodeTokens_Matcher matcher;
47  
    if (p.equals("*"))
48  
      matcher = t -> true;
49  
    else if (p.equals("<quoted>"))
50  
      matcher = t -> isQuoted(t);
51  
    else if (p.equals("<id>"))
52  
      ifdef findCodeTokens_quickIsIdentifier
53  
      matcher = t -> startsLikeIdentifier(t);
54  
      endifdef
55  
      ifndef findCodeTokens_quickIsIdentifier
56  
      matcher = t -> isIdentifier(t);
57  
      endifndef
58  
    else if (p.equals("<int>"))
59  
      ifdef findCodeTokens_quickIsInteger
60  
      matcher = t -> startsWithDigit(t);
61  
      endifdef
62  
      ifndef findCodeTokens_quickIsInteger
63  
      matcher = t -> isInteger(t);
64  
      endifndef
65  
    else if (p.equals("\\*"))
66  
      matcher = t -> t.equals("*");
67  
    else if (ignoreCase)
68  
      matcher = t -> eqic(p, t);
69  
    else
70  
      matcher = t -> t.equals(p);
71  
    matchers[j] = matcher;
72  
  }
73  
 
74  
  outer: for (; i < end; i += 2) {
75  
    for (int j = 0; j < nTokens; j++)
76  
      if (!matchers[j].get(tok.get(i+j*2)))
77  
        continue outer;
78  
79  
    if (condition == null || checkTokCondition(condition, tok, i-1)) // pass N index
80  
      return i;
81  
  }
82  
  return -1;
83  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1001193
Snippet name: findCodeTokens - returns C index
Eternal ID of this version: #1001193/12
Text MD5: f60b2660c5a8cbb37e506120064b3c27
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-08 15:26:40
Source code size: 2591 bytes / 83 lines
Pitched / IR pitched: No / No
Views / Downloads: 790 / 2908
Version history: 11 change(s)
Referenced in: [show references]