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.

static int findCodeTokens(L<S> tok, S... tokens) {
  ret findCodeTokens(tok, 1, false, tokens);
}

static int findCodeTokens(L<S> tok, boolean ignoreCase, S... tokens) {
  ret findCodeTokens(tok, 1, ignoreCase, tokens);
}

static int findCodeTokens(L<S> tok, int startIdx, boolean ignoreCase, S... tokens) {
  ret findCodeTokens(tok, startIdx, ignoreCase, tokens, null);
}

static HashSet<S> findCodeTokens_specials = lithashset("*", "<quoted>", "<id>", "<int>", "\\*");
static int findCodeTokens_bails, findCodeTokens_nonbails;

sinterface findCodeTokens_Matcher {
  bool get(S token);
}

static int findCodeTokens(L<S> tok, int startIdx, boolean ignoreCase, S[] tokens, O condition) {
  int end = tok.size()-tokens.length*2+2, nTokens = tokens.length;
  int i = startIdx | 1;

  findCodeTokens_Matcher[] matchers = new[nTokens];
  IContentsIndexedList2 indexedList = tok instanceof IContentsIndexedList2 ? (IContentsIndexedList2) tok : null;
  ifdef findCodeTokens_debug
    print("fct " + joinWithSpace(tokens) + " have indexed list: " + (indexedList != null));
  endifdef
  
  TreeSet<HasIndex> indices = null;
  int indicesOfs = 0;

  for j to nTokens: {
    S p = tokens[j];
    findCodeTokens_Matcher matcher;
    if (p.equals("*"))
      matcher = t -> true;
    else if (p.equals("<quoted>"))
      matcher = t -> isQuoted(t);
    else if (p.equals("<id>"))
      matcher = t -> isIdentifier(t);
    else if (p.equals("<int>"))
      matcher = t -> isInteger(t);
    else if (p.equals("\\*"))
      matcher = t -> t.equals("*");
    else if (ignoreCase)
      matcher = t -> eqic(p, t);
    else {
      matcher = t -> t.equals(p);
      if (indexedList != null) {
        TreeSet<HasIndex> indices2 = indexedList.indicesOf_treeSetOfHasIndex(p);
        ifdef findCodeTokens_debug
          print("fct indices for token " + p + ": " + indices2);
        endifdef
        if (indices2 == null) ret -1;
        if (indices == null || indices2.size() < indices.size()) {
          // found shorter list
          indices = indices2;
          indicesOfs = j;
        }
      }
    }
    matchers[j] = matcher;
  }
  
  // go over shortest index
  if (indices != null) {
    int min = i+indicesOfs*2;
    SortedSet<HasIndex> tailSet = min == 1 ? indices : indices.tailSet(HasIndex(min));
    ifdef findCodeTokens_debug
      print("fct i=" + i + ", tailSet: " + tailSet);
    endifdef
    outer: for (HasIndex h : tailSet) {
      int idx = h.idx-indicesOfs*2;
      if (idx >= end) break;
      ifdef findCodeTokens_debug
        print("fct have first token " + idx + ", indicesOfs=" + indicesOfs + ": " + tok.get(idx));
      endifdef
      for (int j = 0; j < nTokens; j++) try {
        if (!matchers[j].get(tok.get(idx+j*2)))
          continue outer;
      } catch (IndexOutOfBoundsException e) {
        print("fct indicesOfs=" + indicesOfs + ", h=" + h + ", idx=" + idx);
        throw e;
      }

      if (condition == null || checkTokCondition(condition, tok, idx-1))
        ret idx;
    }
    ret -1;
  }
 
  outer: for (; i < end; i += 2) {
    for (int j = 0; j < nTokens; j++)
      if (!matchers[j].get(tok.get(i+j*2)))
        continue outer;

    if (condition == null || checkTokCondition(condition, tok, i-1)) // pass N index
      ret i;
  }
  ret -1;
}

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: 233 / 2692
Version history: 16 change(s)
Referenced in: [show references]