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

34
LINES

< > BotCompany Repo | #1000685 // matchTokensList etc

JavaX fragment (include)

1  
  // returns a list of the matched tokens. understands ... (n tokens) and * (one token)
2  
  static List<int[]> matchTokensList(L<S> inp, L<S> pat) {
3  
    new List<int[]> list;
4  
    for (int i = 1; i+pat.size()-3 < inp.size(); i += 2) {
5  
      int j = matchTokens_step(inp, pat, i, 1);
6  
      if (j >= 0)
7  
        list.add(new int[] {i, j});
8  
    }
9  
    return list;
10  
  }
11  
  
12  
 
13  
  // returns last token matched + ofs 1 (in inp), or -1
14  
  static int matchTokens_step(L<S> inp, L<S> pat, int i, int j) {
15  
    boolean debug = false;
16  
     if (i+j-1 >= inp.size()) return -1;
17  
    for (; j < pat.size(); j += 2) {
18  
      String p = pat.get(j);
19  
      if (debug)
20  
        System.out.println("step " + p + " " + inp.get(i+j-1));
21  
      if (p == "*") {
22  
        // ok, consume
23  
      } else if (p == "." && pat.get(j+2).equals(".") && pat.get(j+4).equals(".")) {
24  
        int bla = matchTokens_step(inp, pat, i, j+6); // end ...
25  
        if (bla >= 0) return bla;
26  
        return matchTokens_step(inp, pat, i+2, j); // continue ...
27  
      } else {
28  
        String found = inp.get(i + j - 1);
29  
        if (!found.equalsIgnoreCase(unquote(p)))
30  
          return -1;
31  
      }
32  
    }
33  
    return i+j-2;
34  
  }

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: 793 / 1021
Referenced in: [show references]