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

34
LINES

< > BotCompany Repo | #1000685 // matchTokensList etc

JavaX fragment (include)

  // returns a list of the matched tokens. understands ... (n tokens) and * (one token)
  static List<int[]> matchTokensList(L<S> inp, L<S> pat) {
    new List<int[]> list;
    for (int i = 1; i+pat.size()-3 < inp.size(); i += 2) {
      int j = matchTokens_step(inp, pat, i, 1);
      if (j >= 0)
        list.add(new int[] {i, j});
    }
    return list;
  }
  
 
  // returns last token matched + ofs 1 (in inp), or -1
  static int matchTokens_step(L<S> inp, L<S> pat, int i, int j) {
    boolean debug = false;
     if (i+j-1 >= inp.size()) return -1;
    for (; j < pat.size(); j += 2) {
      String p = pat.get(j);
      if (debug)
        System.out.println("step " + p + " " + inp.get(i+j-1));
      if (p == "*") {
        // ok, consume
      } else if (p == "." && pat.get(j+2).equals(".") && pat.get(j+4).equals(".")) {
        int bla = matchTokens_step(inp, pat, i, j+6); // end ...
        if (bla >= 0) return bla;
        return matchTokens_step(inp, pat, i+2, j); // continue ...
      } else {
        String found = inp.get(i + j - 1);
        if (!found.equalsIgnoreCase(unquote(p)))
          return -1;
      }
    }
    return i+j-2;
  }

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1000685
Snippet name: matchTokensList etc
Eternal ID of this version: #1000685/1
Text MD5: 9c7408e4835413e4a34790eca255ce7e
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-26 19:44:09
Source code size: 1195 bytes / 34 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 791 / 1017
Referenced in: [show references]