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.

// Note: In the transpiler, this version is used: #1025802

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(LS tok, int startIdx, boolean ignoreCase, S[] tokens, O condition) {
  int end = tok.size()-tokens.length*2+2, nTokens = tokens.length;
  int i = startIdx | 1;
  if (i >= end) ret -1;
  
  // bail out early if first token not found (works great with IndexedList)
  S firstToken = tokens[0];
  if (!ignoreCase && !findCodeTokens_specials.contains(firstToken)) {
    ifclass IndexedList2
    if (tok instanceof IndexedList2 && !tok.contains(firstToken)) {
      ++findCodeTokens_bails;
      ret -1;
    }
    ++findCodeTokens_nonbails;
    endif
    
    // quickly scan for first token
    while (i < end && !firstToken.equals(tok.get(i)))
      i += 2;
  }
  
  findCodeTokens_Matcher[] matchers = new[nTokens];
  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>"))
      ifdef findCodeTokens_quickIsIdentifier
      matcher = t -> startsLikeIdentifier(t);
      endifdef
      ifndef findCodeTokens_quickIsIdentifier
      matcher = t -> isIdentifier(t);
      endifndef
    else if (p.equals("<int>"))
      ifdef findCodeTokens_quickIsInteger
      matcher = t -> startsWithDigit(t);
      endifdef
      ifndef findCodeTokens_quickIsInteger
      matcher = t -> isInteger(t);
      endifndef
    else if (p.equals("\\*"))
      matcher = t -> t.equals("*");
    else if (ignoreCase)
      matcher = t -> eqic(p, t);
    else
      matcher = t -> t.equals(p);
    matchers[j] = matcher;
  }
 
  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
      return i;
  }
  return -1;
}

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: 852 / 2996
Version history: 11 change(s)
Referenced in: [show references]