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

70
LINES

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

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

Libraryless. Click here for Pure Java version (2214L/14K).

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;
  
  // 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>"))
      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);
    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   

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

No comments. add comment

Snippet ID: #1001193
Snippet name: findCodeTokens - returns C index
Eternal ID of this version: #1001193/8
Text MD5: a76fb518de16d817e56d1adb15d61cb4
Transpilation MD5: f9af0f02e4bb103fd88785de401c6d7c
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-09-15 02:06:23
Source code size: 2171 bytes / 70 lines
Pitched / IR pitched: No / No
Views / Downloads: 529 / 2495
Version history: 7 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1001193 & http://1001193.tinybrain.de